-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathsandbox-child.mjs
109 lines (92 loc) · 3 KB
/
sandbox-child.mjs
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { Buffer } from 'node:buffer'
import { join } from 'node:path'
import { execute, getLogger } from 'lambda-local'
const SERVER_HANDLER_NAME = '___netlify-server-handler'
const BLOB_TOKEN = 'secret-token'
getLogger().level = 'alert'
const createBlobContext = (ctx) =>
Buffer.from(
JSON.stringify({
edgeURL: `http://${ctx.blobStoreHost}`,
uncachedEdgeURL: `http://${ctx.blobStoreHost}`,
token: BLOB_TOKEN,
siteID: ctx.siteID,
deployID: ctx.deployID,
primaryRegion: 'us-test-1',
}),
).toString('base64')
function streamToBuffer(stream) {
const chunks = []
return new Promise((resolve, reject) => {
stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)))
stream.on('error', (err) => reject(err))
stream.on('end', () => resolve(Buffer.concat(chunks)))
})
}
process.on('message', async (msg) => {
if (msg?.action === 'exit') {
process.exit(0)
} else if (msg?.action === 'invokeFunction') {
try {
const [ctx, options] = msg.args
const { httpMethod, headers, body, url, env } = options
const { handler } = await import(
'file:///' + join(ctx.functionDist, SERVER_HANDLER_NAME, '___netlify-entry-point.mjs')
)
const environment = {
NODE_ENV: 'production',
NETLIFY_BLOBS_CONTEXT: createBlobContext(ctx),
...(env || {}),
}
const envVarsToRestore = {}
// We are not using lambda-local's environment variable setting because it cleans up
// environment vars to early (before stream is closed)
Object.keys(environment).forEach(function (key) {
if (typeof process.env[key] !== 'undefined') {
envVarsToRestore[key] = process.env[key]
}
process.env[key] = environment[key]
})
const response = await execute({
event: {
headers: headers || {},
httpMethod: httpMethod || 'GET',
rawUrl: new URL(url || '/', 'https://example.netlify').href,
},
lambdaFunc: { handler },
timeoutMs: 4_000,
})
const responseHeaders = Object.entries(response.multiValueHeaders || {}).reduce(
(prev, [key, value]) => ({
...prev,
[key]: value.length === 1 ? `${value}` : value.join(', '),
}),
response.headers || {},
)
const bodyBuffer = await streamToBuffer(response.body)
Object.keys(environment).forEach(function (key) {
if (typeof envVarsToRestore[key] !== 'undefined') {
process.env[key] = envVarsToRestore[key]
} else {
delete process.env[key]
}
})
const result = {
statusCode: response.statusCode,
bodyBuffer,
body: bodyBuffer.toString('utf-8'),
headers: responseHeaders,
isBase64Encoded: response.isBase64Encoded,
}
if (process.send) {
process.send({
action: 'invokeFunctionResult',
result,
})
}
} catch (e) {
console.log('error', e)
process.exit(1)
}
}
})