-
Notifications
You must be signed in to change notification settings - Fork 31
/
server.js
82 lines (62 loc) · 2.03 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const path = require('path')
const readline = require('readline')
const WRITE_CHUNK_SIZE = parseInt(process.env.WRITE_CHUNK_SIZE, 10)
const PREFIX = "__elixirnodejs__UOSBsDUP6bp9IF5__";
function requireModule(modulePath) {
// When not running in production mode, refresh the cache on each call.
if (process.env.NODE_ENV !== 'production') {
delete require.cache[require.resolve(modulePath)]
}
return require(modulePath)
}
function getAncestor(parent, [key, ...keys]) {
if (typeof key === 'undefined') {
return parent
}
return getAncestor(parent[key], keys)
}
function requireModuleFunction([modulePath, ...keys]) {
const mod = requireModule(modulePath)
return getAncestor(mod, keys)
}
async function callModuleFunction(moduleFunction, args) {
const fn = requireModuleFunction(moduleFunction)
const returnValue = fn(...args)
if (returnValue instanceof Promise) {
return await returnValue
}
return returnValue
}
async function getResponse(string) {
try {
const [moduleFunction, args] = JSON.parse(string)
const result = await callModuleFunction(moduleFunction, args)
return JSON.stringify([true, result])
} catch ({ message, stack }) {
return JSON.stringify([false, `${message}\n${stack}`])
}
}
async function onLine(string) {
const buffer = Buffer.from(`${await getResponse(string)}\n`)
// The function we called might have written something to stdout without starting a new line.
// So we add one here and write the response after the prefix
process.stdout.write("\n")
process.stdout.write(PREFIX)
for (let i = 0; i < buffer.length; i += WRITE_CHUNK_SIZE) {
let chunk = buffer.slice(i, i + WRITE_CHUNK_SIZE)
process.stdout.write(chunk)
}
}
function startServer() {
process.stdin.on('end', () => process.exit())
const readLineInterface = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false,
})
readLineInterface.on('line', onLine)
}
module.exports = { startServer }
if (require.main === module) {
startServer()
}