-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathexposeServerFunctions.js
More file actions
53 lines (52 loc) · 2.08 KB
/
exposeServerFunctions.js
File metadata and controls
53 lines (52 loc) · 2.08 KB
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
import extractParamValue from '../shared/extractParamValue'
import fs from 'fs'
import bodyParser from 'body-parser'
import path from 'path'
import deserialize from '../shared/deserialize'
import { getCurrentContext } from './context'
import printError from './printError'
import registry from './registry'
export default function exposeServerFunctions(server) {
for (const method of ['get', 'post', 'put', 'patch', 'delete', 'all']) {
const original = server[method].bind(server)
server[method] = function (...args) {
if (typeof args[1] === 'function' && args[1].name === '_invoke') {
return original(args[0], bodyParser.text({ limit: server.maximumPayloadSize }), async (request, response) => {
const params = {}
for (const key of Object.keys(request.params)) {
params[key] = extractParamValue(request.params[key])
}
for (const key of Object.keys(request.query)) {
params[key] = extractParamValue(request.query[key])
}
if (request.method !== 'GET') {
const payload = typeof request.body === 'object' ? JSON.stringify(request.body) : request.body
Object.assign(params, deserialize(payload))
}
try {
const currentContext = getCurrentContext(params)
const exposedFunction = module.hot ? registry[args[1].hash] : args[1]
const result = await exposedFunction(currentContext)
response.json(result)
} catch (error) {
printError(error)
response.status(500).json({})
}
})
}
if (module.hot) {
server._router.stack.forEach((r) => {
if (r?.route?.path === args[0]) {
const exists = r.route.stack.find((l) => l.method === method)
if (!!exists && !!process.env.__NULLSTACK_FIRST_LOAD_COMPLETE) {
const filename = path.join(process.cwd(), 'server.js')
const time = new Date()
fs.utimesSync(filename, time, time)
}
}
})
}
return original(...args)
}
}
}