-
Notifications
You must be signed in to change notification settings - Fork 457
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
944 additions
and
952 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,157 +1,157 @@ | ||
#!/usr/bin/env node | ||
|
||
// Native | ||
const path = require('path') | ||
const { existsSync } = require('fs') | ||
const path = require('path'); | ||
const {existsSync} = require('fs'); | ||
|
||
// Packages | ||
const parseArgs = require('mri') | ||
const parseArgs = require('mri'); | ||
|
||
// Utilities | ||
const serve = require('../lib') | ||
const handle = require('../lib/handler') | ||
const generateHelp = require('../lib/help') | ||
const { version } = require('../package') | ||
const logError = require('../lib/error') | ||
const serve = require('../lib'); | ||
const handle = require('../lib/handler'); | ||
const generateHelp = require('../lib/help'); | ||
const {version} = require('../package'); | ||
const logError = require('../lib/error'); | ||
|
||
// Check if the user defined any options | ||
const flags = parseArgs(process.argv.slice(2), { | ||
alias: { | ||
p: 'port', | ||
H: 'host', | ||
s: 'unix-socket', | ||
h: 'help', | ||
v: 'version' | ||
}, | ||
unknown(flag) { | ||
console.log(`The option "${flag}" is unknown. Use one of these:`) | ||
console.log(generateHelp()) | ||
process.exit(1) | ||
} | ||
}) | ||
alias: { | ||
p: 'port', | ||
H: 'host', | ||
s: 'unix-socket', | ||
h: 'help', | ||
v: 'version' | ||
}, | ||
unknown(flag) { | ||
console.log(`The option "${flag}" is unknown. Use one of these:`); | ||
console.log(generateHelp()); | ||
process.exit(1); | ||
} | ||
}); | ||
|
||
// When `-h` or `--help` are used, print out | ||
// the usage information | ||
if (flags.help) { | ||
console.log(generateHelp()) | ||
process.exit() | ||
console.log(generateHelp()); | ||
process.exit(); | ||
} | ||
|
||
// Print out the package's version when | ||
// `--version` or `-v` are used | ||
if (flags.version) { | ||
console.log(version) | ||
process.exit() | ||
console.log(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) | ||
logError( | ||
`Both port and socket provided. You can only use one.`, | ||
'invalid-port-socket' | ||
); | ||
process.exit(1); | ||
} | ||
|
||
let listenTo = 3000 | ||
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 | ||
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'] | ||
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] | ||
let file = flags._[0]; | ||
|
||
if (!file) { | ||
try { | ||
// eslint-disable-next-line import/no-dynamic-require | ||
const packageJson = require(path.resolve(process.cwd(), 'package.json')) | ||
file = packageJson.main || 'index.js' | ||
} catch (err) { | ||
if (err.code !== 'MODULE_NOT_FOUND') { | ||
logError( | ||
`Could not read \`package.json\`: ${err.message}`, | ||
'invalid-package-json' | ||
) | ||
process.exit(1) | ||
} | ||
} | ||
try { | ||
// eslint-disable-next-line import/no-dynamic-require | ||
const packageJson = require(path.resolve(process.cwd(), 'package.json')); | ||
file = packageJson.main || 'index.js'; | ||
} catch (err) { | ||
if (err.code !== 'MODULE_NOT_FOUND') { | ||
logError( | ||
`Could not read \`package.json\`: ${err.message}`, | ||
'invalid-package-json' | ||
); | ||
process.exit(1); | ||
} | ||
} | ||
} | ||
|
||
if (!file) { | ||
logError('Please supply a file!', 'path-missing') | ||
process.exit(1) | ||
logError('Please supply a file!', 'path-missing'); | ||
process.exit(1); | ||
} | ||
|
||
if (file[0] !== '/') { | ||
file = path.resolve(process.cwd(), file) | ||
file = path.resolve(process.cwd(), file); | ||
} | ||
|
||
if (!existsSync(file)) { | ||
logError( | ||
`The file or directory "${path.basename(file)}" doesn't exist!`, | ||
'path-not-existent' | ||
) | ||
process.exit(1) | ||
logError( | ||
`The file or directory "${path.basename(file)}" doesn't exist!`, | ||
'path-not-existent' | ||
); | ||
process.exit(1); | ||
} | ||
|
||
async function start() { | ||
const loadedModule = await handle(file) | ||
const server = serve(loadedModule) | ||
|
||
server.on('error', err => { | ||
console.error('micro:', err.stack) | ||
process.exit(1) | ||
}) | ||
|
||
const listenArgs = [listenTo] | ||
if (flags.host) { | ||
listenArgs.push(flags.host) | ||
} | ||
|
||
server.listen(...listenArgs, () => { | ||
const details = server.address() | ||
|
||
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 | ||
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') | ||
}) | ||
const loadedModule = await handle(file); | ||
const server = serve(loadedModule); | ||
|
||
server.on('error', err => { | ||
console.error('micro:', err.stack); | ||
process.exit(1); | ||
}); | ||
|
||
const listenArgs = [listenTo]; | ||
if (flags.host) { | ||
listenArgs.push(flags.host); | ||
} | ||
|
||
server.listen(...listenArgs, () => { | ||
const details = server.address(); | ||
|
||
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 | ||
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'); | ||
}); | ||
} | ||
|
||
start() | ||
start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
const fetch = require('node-fetch') | ||
const fetch = require('node-fetch'); | ||
|
||
module.exports = async function (req, res) { | ||
const response = await fetch('https://api.example.com') | ||
const json = await response.json() | ||
const response = await fetch('https://api.example.com'); | ||
const json = await response.json(); | ||
|
||
return json | ||
} | ||
return json; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
const {json} = require('micro') | ||
const {json} = require('micro'); | ||
|
||
module.exports = async function (req, res) { | ||
const data = await json(req) | ||
console.log(data) | ||
const data = await json(req); | ||
console.log(data); | ||
|
||
return 'Data logged to your console' | ||
} | ||
return 'Data logged to your console'; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
const micro = require('micro') | ||
const fs = require('fs') | ||
const path = require('path') | ||
const micro = require('micro'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const document = path.join(__dirname, 'index.html') | ||
const html = fs.readFileSync(document) | ||
const document = path.join(__dirname, 'index.html'); | ||
const html = fs.readFileSync(document); | ||
|
||
const server = micro(async (req, res) => { | ||
console.log('Serving index.html') | ||
res.end(html) | ||
}) | ||
console.log('Serving index.html'); | ||
res.end(html); | ||
}); | ||
|
||
const io = require('socket.io')(server) | ||
const io = require('socket.io')(server); | ||
|
||
// socket-io handlers are in websocket-server.js | ||
require('./websocket-server.js')(io) | ||
require('./websocket-server.js')(io); | ||
|
||
server.listen(4000) | ||
server.listen(4000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
module.exports = function startServer(io) { | ||
io.on('connection', socket => { | ||
console.log('a user connected') | ||
io.on('connection', socket => { | ||
console.log('a user connected'); | ||
|
||
socket.on('disconnect', () => { | ||
console.log('user disconnected') | ||
}); | ||
socket.on('disconnect', () => { | ||
console.log('user disconnected'); | ||
}); | ||
|
||
socket.on('chat message', msg => { | ||
console.log('message: ' + msg) | ||
io.emit('chat message', msg); | ||
}); | ||
}); | ||
} | ||
socket.on('chat message', msg => { | ||
console.log(`message: ${msg}`); | ||
io.emit('chat message', msg); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
const parse = require('urlencoded-body-parser') | ||
const parse = require('urlencoded-body-parser'); | ||
|
||
module.exports = async function (req, res) { | ||
const data = await parse(req) | ||
console.log(data) | ||
const data = await parse(req); | ||
console.log(data); | ||
|
||
return 'Data logged to your console' | ||
} | ||
return 'Data logged to your console'; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.