Skip to content

Commit

Permalink
Add support for --unix-socket (#353)
Browse files Browse the repository at this point in the history
* Add support for —socket

* Use unix-socket instead of socket

* Add SIGINT and SIGTERM graceful shutdown

* Remove unneeded callback

* Use single sigterm/sigint
  • Loading branch information
timneutkens committed Apr 23, 2018
1 parent 066c06a commit f3b2978
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 19 deletions.
73 changes: 54 additions & 19 deletions bin/micro.js
Expand Up @@ -16,12 +16,10 @@ const logError = require('../lib/error')

// Check if the user defined any options
const flags = parseArgs(process.argv.slice(2), {
default: {
port: 3000
},
alias: {
p: 'port',
H: 'host',
s: 'unix-socket',
h: 'help',
v: 'version'
},
Expand All @@ -46,6 +44,40 @@ if (flags.version) {
process.exit()
}

if (flags.port && flags['unix-socket']) {
logError(
`Both port and socket provided. You can only use one.`,
'invalid-port-socket'
)
process.exit(1)
}

let listenTo = 3000

if (flags.port) {
const { isNaN } = Number
const port = Number(flags.port)
if (isNaN(port) || (!isNaN(port) && (port < 1 || port >= Math.pow(2, 16)))) {
logError(
`Port option must be a number. Supplied: ${flags.port}`,
'invalid-server-port'
)
process.exit(1)
}

listenTo = flags.port
}

if (flags['unix-socket']) {
if (typeof flags['unix-socket'] === 'boolean') {
logError(
`Socket must be a string. A boolean was provided.`,
'invalid-socket'
)
}
listenTo = flags['unix-socket']
}

let file = flags._[0]

if (!file) {
Expand Down Expand Up @@ -81,16 +113,6 @@ if (!existsSync(file)) {
process.exit(1)
}

const { isNaN } = Number
const port = Number(flags.port)
if (isNaN(port) || (!isNaN(port) && (port < 1 || port >= Math.pow(2, 16)))) {
logError(
`Port option must be a number. Supplied: ${flags.port}`,
'invalid-server-port'
)
process.exit(1)
}

async function start() {
const loadedModule = await handle(file)
const server = serve(loadedModule)
Expand All @@ -100,22 +122,35 @@ async function start() {
process.exit(1)
})

const listenArgs = [flags.port || 0]
const listenArgs = [listenTo]
if (flags.host) {
listenArgs.push(flags.host)
}

server.listen(...listenArgs, () => {
const details = server.address()

process.on('SIGTERM', () => {
console.log('\nmicro: Gracefully shutting down. Please wait...')
server.close(process.exit)
})
const shutdown = () => {
console.log('micro: Gracefully shutting down. Please wait...')
server.close()
}

process.on('SIGINT', shutdown)
process.on('SIGTERM', shutdown)

// `micro` is designed to run only in production, so
// this message is perfectly for prod
console.log(`micro: Accepting connections on port ${details.port}`)
if (typeof details === 'string') {
console.log(`micro: Accepting connections on ${details}`)
return
}

if (typeof details === 'object' && details.port) {
console.log(`micro: Accepting connections on port ${details.port}`)
return
}

console.log('micro: Accepting connections')
})
}

Expand Down
9 changes: 9 additions & 0 deletions errors/invalid-port-socket.md
@@ -0,0 +1,9 @@
# Port and socket provided

#### Why This Error Occurred

When the `micro` command was ran, you passed both a port and a socket. Node.js can only listen to either a port or a socket, not both.

#### Possible Ways to Fix It

Only provide one of the arguments. If both are needed you can start 2 instances of micro with different arguments.
13 changes: 13 additions & 0 deletions errors/invalid-socket.md
@@ -0,0 +1,13 @@
# Port and socket provided

#### Why This Error Occurred

When the `micro` command was ran, you passed `-s` or `--socket` without a value.

#### Possible Ways to Fix It

Run `micro` with a value:

```
micro -s '/tmp/micro.sock'
```
94 changes: 94 additions & 0 deletions yarn.lock
Expand Up @@ -44,6 +44,16 @@
dependencies:
arrify "^1.0.1"

"@sinonjs/formatio@^2.0.0":
version "2.0.0"
resolved "http://registry.npmjs.org/@sinonjs/formatio/-/formatio-2.0.0.tgz#84db7e9eb5531df18a8c5e0bfb6e449e55e654b2"
dependencies:
samsam "1.3.0"

"@std/esm@^0.23.3":
version "0.23.4"
resolved "https://registry.yarnpkg.com/@std/esm/-/esm-0.23.4.tgz#e047aeb8a79bbea1d2f62f862de5d88c8faafd37"

abbrev@1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8"
Expand Down Expand Up @@ -526,6 +536,16 @@ babel-plugin-transform-async-to-generator@^6.16.0:
babel-plugin-syntax-async-functions "^6.8.0"
babel-runtime "^6.22.0"

babel-plugin-transform-es2015-block-scoping@^6.26.0:
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-block-scoping/-/babel-plugin-transform-es2015-block-scoping-6.26.0.tgz#d70f5299c1308d05c12f463813b0a09e73b1895f"
dependencies:
babel-runtime "^6.26.0"
babel-template "^6.26.0"
babel-traverse "^6.26.0"
babel-types "^6.26.0"
lodash "^4.17.4"

babel-plugin-transform-es2015-destructuring@^6.19.0:
version "6.23.0"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-destructuring/-/babel-plugin-transform-es2015-destructuring-6.23.0.tgz#997bb1f1ab967f682d2b0876fe358d60e765c56d"
Expand Down Expand Up @@ -1306,6 +1326,10 @@ detect-libc@^1.0.2:
version "1.0.3"
resolved "https://registry.yarnpkg.com/detect-libc/-/detect-libc-1.0.3.tgz#fa137c4bd698edf55cd5cd02ac559f91a4c4ba9b"

diff@^3.1.0:
version "3.5.0"
resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12"

doctrine@1.5.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/doctrine/-/doctrine-1.5.0.tgz#379dce730f6166f76cefa4e6707a159b02c5a6fa"
Expand Down Expand Up @@ -2608,6 +2632,10 @@ is-windows@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/is-windows/-/is-windows-1.0.2.tgz#d1850eb9791ecd18e6182ce12a30f396634bb19d"

isarray@0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-0.0.1.tgz#8a18acfca9a8f4177e09abfc6038939b05d1eedf"

isarray@1.0.0, isarray@^1.0.0, isarray@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11"
Expand Down Expand Up @@ -2768,6 +2796,10 @@ jsprim@^1.2.2:
json-schema "0.2.3"
verror "1.10.0"

just-extend@^1.1.27:
version "1.1.27"
resolved "https://registry.yarnpkg.com/just-extend/-/just-extend-1.1.27.tgz#ec6e79410ff914e472652abfa0e603c03d60e905"

kind-of@^3.0.2, kind-of@^3.0.3, kind-of@^3.2.0:
version "3.2.2"
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-3.2.2.tgz#31ea21a734bab9bbb0f32466d893aea51e4a3c64"
Expand Down Expand Up @@ -2961,6 +2993,10 @@ lodash.flattendeep@^4.4.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/lodash.flattendeep/-/lodash.flattendeep-4.4.0.tgz#fb030917f86a3134e5bc9bec0d69e0013ddfedb2"

lodash.get@^4.4.2:
version "4.4.2"
resolved "https://registry.yarnpkg.com/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99"

lodash.isequal@^4.4.0, lodash.isequal@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0"
Expand Down Expand Up @@ -3008,6 +3044,10 @@ log-update@^1.0.2:
ansi-escapes "^1.0.0"
cli-cursor "^1.0.2"

lolex@^2.2.0, lolex@^2.3.2:
version "2.3.2"
resolved "https://registry.yarnpkg.com/lolex/-/lolex-2.3.2.tgz#85f9450425103bf9e7a60668ea25dc43274ca807"

longest@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/longest/-/longest-1.0.1.tgz#30a0b2da38f73770e8294a0d22e6625ed77d0097"
Expand Down Expand Up @@ -3240,6 +3280,16 @@ natural-compare@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"

nise@^1.2.0:
version "1.3.3"
resolved "https://registry.yarnpkg.com/nise/-/nise-1.3.3.tgz#c17a850066a8a1dfeb37f921da02441afc4a82ba"
dependencies:
"@sinonjs/formatio" "^2.0.0"
just-extend "^1.1.27"
lolex "^2.3.2"
path-to-regexp "^1.7.0"
text-encoding "^0.6.4"

node-pre-gyp@^0.6.39:
version "0.6.39"
resolved "https://registry.yarnpkg.com/node-pre-gyp/-/node-pre-gyp-0.6.39.tgz#c00e96860b23c0e1420ac7befc5044e1d78d8649"
Expand Down Expand Up @@ -3571,6 +3621,12 @@ path-parse@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.5.tgz#3c1adf871ea9cd6c9431b6ea2bd74a0ff055c4c1"

path-to-regexp@^1.7.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-1.7.0.tgz#59fde0f435badacba103a84e9d3bc64e96b9937d"
dependencies:
isarray "0.0.1"

path-type@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/path-type/-/path-type-1.1.0.tgz#59c44f7ee491da704da415da5a4070ba4f8fe441"
Expand Down Expand Up @@ -4051,6 +4107,13 @@ ret@~0.1.10:
version "0.1.15"
resolved "https://registry.yarnpkg.com/ret/-/ret-0.1.15.tgz#b8a4825d5bdb1fc3f6f53c2bc33f81388681c7bc"

rewire@3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/rewire/-/rewire-3.0.2.tgz#25e5413c4f1676eb3247d1884198b3a265408bbd"
dependencies:
babel-core "^6.26.0"
babel-plugin-transform-es2015-block-scoping "^6.26.0"

right-align@^0.1.1:
version "0.1.3"
resolved "https://registry.yarnpkg.com/right-align/-/right-align-0.1.3.tgz#61339b722fe6a3515689210d24e14c96148613ef"
Expand Down Expand Up @@ -4089,6 +4152,10 @@ safe-regex@^1.1.0:
dependencies:
ret "~0.1.10"

samsam@1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/samsam/-/samsam-1.3.0.tgz#8d1d9350e25622da30de3e44ba692b5221ab7c50"

semver-diff@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/semver-diff/-/semver-diff-2.1.0.tgz#4bbb8437c8d37e4b0cf1a68fd726ec6d645d6d36"
Expand Down Expand Up @@ -4157,6 +4224,19 @@ signal-exit@^3.0.0, signal-exit@^3.0.1, signal-exit@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.2.tgz#b5fdc08f1287ea1178628e415e25132b73646c6d"

sinon@4.4.3:
version "4.4.3"
resolved "https://registry.yarnpkg.com/sinon/-/sinon-4.4.3.tgz#f21f08a2fa8cc013c5720751b02c4484a9af31fc"
dependencies:
"@sinonjs/formatio" "^2.0.0"
"@std/esm" "^0.23.3"
diff "^3.1.0"
lodash.get "^4.4.2"
lolex "^2.2.0"
nise "^1.2.0"
supports-color "^5.1.0"
type-detect "^4.0.5"

slash@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/slash/-/slash-1.0.0.tgz#c41f2f6c39fc16d1cd17ad4b5d896114ae470d55"
Expand Down Expand Up @@ -4443,6 +4523,12 @@ supports-color@^4.0.0:
dependencies:
has-flag "^2.0.0"

supports-color@^5.1.0:
version "5.4.0"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.4.0.tgz#1c6b337402c2137605efe19f10fec390f6faab54"
dependencies:
has-flag "^3.0.0"

supports-color@^5.3.0:
version "5.3.0"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.3.0.tgz#5b24ac15db80fa927cf5227a4a33fd3c4c7676c0"
Expand Down Expand Up @@ -4513,6 +4599,10 @@ test-listen@1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/test-listen/-/test-listen-1.0.2.tgz#2f3c638044538a506a8c39cc5d845c3d2beff548"

text-encoding@^0.6.4:
version "0.6.4"
resolved "https://registry.yarnpkg.com/text-encoding/-/text-encoding-0.6.4.tgz#e399a982257a276dae428bb92845cb71bdc26d19"

text-table@^0.2.0, text-table@~0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4"
Expand Down Expand Up @@ -4615,6 +4705,10 @@ type-check@~0.3.2:
dependencies:
prelude-ls "~1.1.2"

type-detect@^4.0.5:
version "4.0.8"
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c"

typedarray@^0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
Expand Down

0 comments on commit f3b2978

Please sign in to comment.