@miniflare/html-rewriter
Advanced tools
Comparing version 2.0.0-next.2 to 2.0.0-next.3
@@ -5,21 +5,11 @@ // packages/html-rewriter/src/plugin.ts | ||
// packages/html-rewriter/src/rewriter.ts | ||
import { ReadableStream } from "stream/web"; | ||
import { TextEncoder } from "util"; | ||
import { TransformStream } from "stream/web"; | ||
import { Response } from "@miniflare/core"; | ||
var encoder = new TextEncoder(); | ||
function transformToArray(chunk) { | ||
if (chunk instanceof Uint8Array) { | ||
return chunk; | ||
} else if (ArrayBuffer.isView(chunk)) { | ||
return new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength); | ||
} else if (chunk instanceof ArrayBuffer) { | ||
return new Uint8Array(chunk); | ||
} else if (Array.isArray(chunk) && chunk.every((value) => typeof value === "number")) { | ||
return new Uint8Array(chunk); | ||
} else if (typeof chunk === "number") { | ||
return new Uint8Array([chunk]); | ||
} else if (chunk === null || chunk === void 0) { | ||
throw new TypeError("Chunk must be defined"); | ||
} else { | ||
return encoder.encode(String(chunk)); | ||
return new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength); | ||
} | ||
@@ -39,7 +29,7 @@ } | ||
transform(response) { | ||
const transformedStream = new ReadableStream({ | ||
type: "bytes", | ||
let rewriter; | ||
const transformStream = new TransformStream({ | ||
start: async (controller) => { | ||
const { HTMLRewriter: BaseHTMLRewriter } = await import("html-rewriter-wasm"); | ||
const rewriter = new BaseHTMLRewriter((output) => { | ||
rewriter = new BaseHTMLRewriter((output) => { | ||
if (output.length !== 0) | ||
@@ -54,18 +44,29 @@ controller.enqueue(output); | ||
} | ||
}, | ||
transform: async (chunk) => { | ||
if (chunk instanceof ArrayBuffer || ArrayBuffer.isView(chunk)) { | ||
try { | ||
return await rewriter.write(transformToArray(chunk)); | ||
} catch (e) { | ||
rewriter.free(); | ||
throw e; | ||
} | ||
} else if (typeof chunk === "string") { | ||
throw new TypeError("This TransformStream is being used as a byte stream, but received a string on its writable side. If you wish to write a string, you'll probably want to explicitly UTF-8-encode it with TextEncoder."); | ||
} else { | ||
throw new TypeError("This TransformStream is being used as a byte stream, but received an object of non-ArrayBuffer/ArrayBufferView type on its writable side."); | ||
} | ||
}, | ||
flush: async () => { | ||
try { | ||
if (response.body) { | ||
for await (const chunk of response.body) { | ||
await rewriter.write(transformToArray(chunk)); | ||
} | ||
} | ||
await rewriter.end(); | ||
} catch (e) { | ||
controller.error(e); | ||
return await rewriter.end(); | ||
} finally { | ||
rewriter.free(); | ||
controller.close(); | ||
} | ||
} | ||
}); | ||
return new Response(transformedStream, response); | ||
const body = response.body; | ||
const res = new Response(body?.pipeThrough(transformStream), response); | ||
res.headers.delete("Content-Length"); | ||
return res; | ||
} | ||
@@ -72,0 +73,0 @@ }; |
{ | ||
"name": "@miniflare/html-rewriter", | ||
"version": "2.0.0-next.2", | ||
"version": "2.0.0-next.3", | ||
"description": "HTMLRewriter module for Miniflare: a fun, full-featured, fully-local simulator for Cloudflare Workers", | ||
@@ -20,2 +20,5 @@ "keywords": [ | ||
], | ||
"engines": { | ||
"node": ">=16.7" | ||
}, | ||
"publishConfig": { | ||
@@ -26,9 +29,9 @@ "access": "public" | ||
"type": "git", | ||
"url": "git+https://github.com/mrbbot/miniflare.git", | ||
"url": "git+https://github.com/cloudflare/miniflare.git", | ||
"directory": "packages/html-rewriter" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/mrbbot/miniflare/issues" | ||
"url": "https://github.com/cloudflare/miniflare/issues" | ||
}, | ||
"homepage": "https://github.com/mrbbot/miniflare/tree/master/packages/html-rewriter#readme", | ||
"homepage": "https://github.com/cloudflare/miniflare/tree/master/packages/html-rewriter#readme", | ||
"volta": { | ||
@@ -38,10 +41,10 @@ "extends": "../../package.json" | ||
"dependencies": { | ||
"@miniflare/core": "2.0.0-next.2", | ||
"@miniflare/shared": "2.0.0-next.2", | ||
"@miniflare/core": "2.0.0-next.3", | ||
"@miniflare/shared": "2.0.0-next.3", | ||
"html-rewriter-wasm": "^0.3.2", | ||
"undici": "^4.8.1" | ||
"undici": "^4.9.3" | ||
}, | ||
"devDependencies": { | ||
"@miniflare/shared-test": "2.0.0-next.2" | ||
"@miniflare/shared-test": "2.0.0-next.3" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6206
102
+ Added@miniflare/core@2.0.0-next.3(transitive)
+ Added@miniflare/shared@2.0.0-next.3(transitive)
- Removed@miniflare/core@2.0.0-next.2(transitive)
- Removed@miniflare/shared@2.0.0-next.2(transitive)
Updated@miniflare/core@2.0.0-next.3
Updatedundici@^4.9.3