-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: use version of htmlrewriter which does not make use of asyncify, which looks to have a potential memory leak under high load #2721
Changes from all commits
0ef0b0e
8526938
a860d2b
a804602
f246247
fdb11ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import type { Context } from '@netlify/edge-functions' | ||
|
||
import { ElementHandlers } from '../vendor/deno.land/x/[email protected]/index.ts' | ||
import type { ElementHandlers } from '../vendor/deno.land/x/[email protected]/src/index.ts' | ||
|
||
type NextDataTransform = <T>(data: T) => T | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import type { Context } from '@netlify/edge-functions' | ||
import { HTMLRewriter } from '../vendor/deno.land/x/[email protected]/index.ts' | ||
import { | ||
HTMLRewriter, | ||
type TextChunk, | ||
} from '../vendor/deno.land/x/[email protected]/src/index.ts' | ||
|
||
import { updateModifiedHeaders } from './headers.ts' | ||
import type { StructuredLogger } from './logging.ts' | ||
|
@@ -79,7 +82,7 @@ export const buildResponse = async ({ | |
|
||
if (response.dataTransforms.length > 0) { | ||
rewriter.on('script[id="__NEXT_DATA__"]', { | ||
text(textChunk) { | ||
text(textChunk: TextChunk) { | ||
// Grab all the chunks in the Next data script tag | ||
buffer += textChunk.text | ||
if (textChunk.lastInTextNode) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,6 @@ import 'https://deno.land/[email protected]/node/util.ts' | |
import 'https://deno.land/[email protected]/path/mod.ts' | ||
|
||
import 'https://deno.land/x/[email protected]/index.ts' | ||
import 'https://deno.land/x/[email protected]/index.ts' | ||
import 'https://deno.land/x/[email protected]/src/index.ts' | ||
|
||
import 'https://v1-7-0--edge-utils.netlify.app/logger/mod.ts' |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -85,13 +85,27 @@ const writeHandlerFile = async (ctx: PluginContext, { matchers, name }: NextDefi | |||||||||||||||||||
JSON.stringify(minimalNextConfig), | ||||||||||||||||||||
) | ||||||||||||||||||||
|
||||||||||||||||||||
const htmlRewriterWasm = await readFile( | ||||||||||||||||||||
join( | ||||||||||||||||||||
ctx.pluginDir, | ||||||||||||||||||||
'edge-runtime/vendor/deno.land/x/[email protected]/pkg/htmlrewriter_bg.wasm', | ||||||||||||||||||||
), | ||||||||||||||||||||
) | ||||||||||||||||||||
|
||||||||||||||||||||
// Writing the function entry file. It wraps the middleware code with the | ||||||||||||||||||||
// compatibility layer mentioned above. | ||||||||||||||||||||
await writeFile( | ||||||||||||||||||||
join(handlerDirectory, `${handlerName}.js`), | ||||||||||||||||||||
` | ||||||||||||||||||||
import { decode as _base64Decode } from './edge-runtime/vendor/deno.land/[email protected]/encoding/base64.ts'; | ||||||||||||||||||||
import { init as htmlRewriterInit } from './edge-runtime/vendor/deno.land/x/[email protected]/src/index.ts' | ||||||||||||||||||||
import {handleMiddleware} from './edge-runtime/middleware.ts'; | ||||||||||||||||||||
import handler from './server/${name}.js'; | ||||||||||||||||||||
|
||||||||||||||||||||
await htmlRewriterInit({ module_or_path: _base64Decode(${JSON.stringify( | ||||||||||||||||||||
htmlRewriterWasm.toString('base64'), | ||||||||||||||||||||
)}).buffer }); | ||||||||||||||||||||
|
||||||||||||||||||||
Comment on lines
+100
to
+108
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bundling wasm file as-is doesn't seem to work, so instead we inline it using same method we do for wasm modules used by users in middleware opennextjs-netlify/src/build/functions/edge.ts Lines 125 to 133 in 6b56128
and pass module to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm gonna have to just trust you on this one 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://gist.github.com/pieh/b205a685e518ef62afaab392e61e064f this is standalone script using this method that can be tested. You can also test that passing crap to
would result in errors:
so the argument has effect. Commenting out
|
||||||||||||||||||||
export default (req, context) => handleMiddleware(req, context, handler); | ||||||||||||||||||||
`, | ||||||||||||||||||||
) | ||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import { createWriteStream } from 'node:fs' | ||
import { cp, rm } from 'node:fs/promises' | ||
import { resolve, join } from 'node:path' | ||
import { join, resolve } from 'node:path' | ||
import { Readable } from 'stream' | ||
import { finished } from 'stream/promises' | ||
|
||
import { build, context } from 'esbuild' | ||
import { execaCommand } from 'execa' | ||
|
@@ -94,6 +97,23 @@ async function vendorDeno() { | |
console.log(`📦 Vendoring Deno modules into '${vendorDest}'...`) | ||
|
||
await execaCommand(`deno vendor ${vendorSource} --output=${vendorDest} --force`) | ||
|
||
// htmlrewriter contains wasm files and those don't currently work great with vendoring | ||
// see https://github.com/denoland/deno/issues/14123 | ||
// to workaround this we copy the wasm files manually | ||
Comment on lines
+101
to
+103
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😢 I left a comment on that issue, as it seems like it should be reopened since Deno landed WASM support There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deno landed it, but in 2.1 - https://deno.com/blog/v2.1#first-class-wasm-support , so support is not guaranteed :( otherwise we could migrate to imports in https://github.com/netlify/htmlrewriter/blob/edb477af3d08359c8f25edd8d301df69ffcc8e4b/pkg/htmlrewriter.js#L1184 to make use of it |
||
const filesToDownload = ['https://deno.land/x/[email protected]/pkg/htmlrewriter_bg.wasm'] | ||
await Promise.all( | ||
filesToDownload.map(async (urlString) => { | ||
const url = new URL(urlString) | ||
|
||
const destination = join(vendorDest, url.hostname, url.pathname) | ||
|
||
const res = await fetch(url) | ||
if (!res.ok) throw new Error('Failed to fetch .wasm file to vendor', { cause: err }) | ||
const fileStream = createWriteStream(destination, { flags: 'wx' }) | ||
await finished(Readable.fromWeb(res.body).pipe(fileStream)) | ||
}), | ||
) | ||
} | ||
|
||
const args = new Set(process.argv.slice(2)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could use a uint8array instead of a base64 string here, it would save on memory usage by 33%
I did this in a similar package I made specifically for CSP support - I could do the same for the htmlrewriter package if we want:
https://github.com/netlify/csp_nonce_html_transformer/blob/main/scripts/build.ts#L13C3-L25