-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: update in-memory prerender manifest with information from full r…
…oute cache (#579) * test: nonprerendered page and sanboxed lambda invocations * fix: update prerender manifest with information from full route cache * fix: use serverDistDir from options passed to CacheHandler to figure out prerender-manifest.json path * chore: fix windows specific things --------- Co-authored-by: Philippe Serhal <[email protected]>
- Loading branch information
Showing
6 changed files
with
258 additions
and
18 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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
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 response = await execute({ | ||
event: { | ||
headers: headers || {}, | ||
httpMethod: httpMethod || 'GET', | ||
rawUrl: new URL(url || '/', 'https://example.netlify').href, | ||
}, | ||
environment, | ||
envdestroy: true, | ||
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) | ||
|
||
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) | ||
} | ||
} | ||
}) |