Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 31ca4e2

Browse files
committed
Update HTMLRewriter to include onDocument, switch to miniflare.dev
1 parent de3fc1a commit 31ca4e2

File tree

10 files changed

+37
-25
lines changed

10 files changed

+37
-25
lines changed

src/cli.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ export default function parseArgv(raw: string[]): Options {
7777
},
7878
modules: {
7979
type: "boolean",
80-
description: "Enable ES modules",
80+
description: "Enable modules",
8181
alias: "m",
8282
},
8383
"modules-rule": {
8484
type: "array",
85-
description: "ES modules import rule (TYPE=GLOB)",
85+
description: "Modules import rule (TYPE=GLOB)",
8686
},
8787
"build-command": {
8888
type: "string",

src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class Miniflare {
5454

5555
readonly #wss: WebSocket.Server;
5656

57-
constructor(options: Options) {
57+
constructor(options: Options = {}) {
5858
if (options.sourceMap) {
5959
sourceMap.install({ emptyCacheBetweenOperations: true });
6060
}
@@ -396,7 +396,7 @@ export class Miniflare {
396396
return [
397397
'<a href="https://developers.cloudflare.com/workers/" target="_blank" style="text-decoration:none">📚 Workers Docs</a>',
398398
'<a href="https://discord.gg/cloudflaredev" target="_blank" style="text-decoration:none">💬 Workers Discord</a>',
399-
'<a href="https://github.com/mrbbot/miniflare" target="_blank" style="text-decoration:none">🔥 Miniflare Docs</a>',
399+
'<a href="https://miniflare.dev" target="_blank" style="text-decoration:none">🔥 Miniflare Docs</a>',
400400
].join("");
401401
});
402402
const errorHtml = await youch.toHTML();

src/modules/events.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ export class EventsModule extends Module {
152152
}
153153
} catch (e) {
154154
if (passThroughMap.get(event)) {
155-
this.log.error(e.stack);
155+
// warn instead of error so we don't throw an exception when not logging
156+
this.log.warn(e.stack);
156157
break;
157158
}
158159
throw e;

src/modules/standards.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import originalFetch, {
88
RequestInit,
99
Response,
1010
} from "@mrbbot/node-fetch";
11+
import { ParsedHTMLRewriter } from "@mrbbot/parsed-html-rewriter";
1112
import { Crypto } from "@peculiar/webcrypto";
12-
import { ParsedHTMLRewriter } from "@worker-tools/parsed-html-rewriter";
1313
import {
1414
ByteLengthQueuingStrategy,
1515
CountQueuingStrategy,

test/cli.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ test("parseArgv: parses complete argv", (t) => {
2727
"build_watch",
2828
"--watch",
2929
"--upstream",
30-
"https://mrbbot.dev",
30+
"https://miniflare.dev",
3131
"--cron",
3232
"15 * * * *",
3333
"--cron",
@@ -77,7 +77,7 @@ test("parseArgv: parses complete argv", (t) => {
7777
buildBasePath: "build",
7878
buildWatchPath: "build_watch",
7979
watch: true,
80-
upstream: "https://mrbbot.dev",
80+
upstream: "https://miniflare.dev",
8181
crons: ["15 * * * *", "45 * * * *"],
8282
kvNamespaces: ["TEST_NAMESPACE1", "TEST_NAMESPACE2"],
8383
kvPersist: true,
@@ -112,7 +112,7 @@ test("parseArgv: parses aliased argv", (t) => {
112112
"-c",
113113
"wrangler.test.toml",
114114
"-u",
115-
"https://mrbbot.dev",
115+
"https://miniflare.dev",
116116
"-t",
117117
"15 * * * *",
118118
"-t",
@@ -142,7 +142,7 @@ test("parseArgv: parses aliased argv", (t) => {
142142
wranglerConfigPath: "wrangler.test.toml",
143143
modules: true,
144144
watch: true,
145-
upstream: "https://mrbbot.dev",
145+
upstream: "https://miniflare.dev",
146146
crons: ["15 * * * *", "45 * * * *"],
147147
kvNamespaces: ["TEST_NAMESPACE1", "TEST_NAMESPACE2"],
148148
sitePath: "public",

test/index.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -101,23 +101,23 @@ test("getCache: gets cache for manipulation", async (t) => {
101101
const url = new URL(request.url);
102102
const cache = caches.default;
103103
if(url.pathname === "/put") {
104-
await cache.put("https://mrbbot.dev/", new Response("1", {
104+
await cache.put("https://miniflare.dev/", new Response("1", {
105105
headers: { "Cache-Control": "max-age=3600" },
106106
}));
107107
}
108-
return cache.match("https://mrbbot.dev/");
108+
return cache.match("https://miniflare.dev/");
109109
}
110110
}`,
111111
});
112112
let res = await mf.dispatchFetch("http://localhost:8787/put");
113113
t.is(await res.text(), "1");
114114

115115
const cache = await mf.getCache();
116-
const cachedRes = await cache.match("https://mrbbot.dev/");
116+
const cachedRes = await cache.match("https://miniflare.dev/");
117117
t.is(await cachedRes?.text(), "1");
118118

119119
await cache.put(
120-
"https://mrbbot.dev",
120+
"https://miniflare.dev",
121121
new Response("2", {
122122
headers: { "Cache-Control": "max-age=3600" },
123123
})

test/modules/standards.spec.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,16 @@ test("HTMLRewriter: transforms responses", async (t) => {
131131
element.setInnerContent("new");
132132
},
133133
};
134-
// TODO: add onDocument handler here too
135-
const rewriter = new sandbox.HTMLRewriter().on("p", elementHandler);
134+
const documentHandler = {
135+
end(end: {
136+
append: (content: string, options?: { html?: boolean }) => void;
137+
}) {
138+
end.append("<!--after-->", { html: true });
139+
},
140+
};
141+
const rewriter = new sandbox.HTMLRewriter()
142+
.on("p", elementHandler)
143+
.onDocument(documentHandler);
136144
137145
e.respondWith(
138146
rewriter.transform(
@@ -143,7 +151,10 @@ test("HTMLRewriter: transforms responses", async (t) => {
143151
}).toString()})()`;
144152
const mf = new Miniflare({ script });
145153
const res = await mf.dispatchFetch(new Request("http://localhost:8787/"));
146-
t.is(await res.text(), '<html lang="en"><body><p>new</p></body></html>');
154+
t.is(
155+
await res.text(),
156+
'<html lang="en"><body><p>new</p></body></html><!--after-->'
157+
);
147158

148159
// Make sure globals are restored (even if they were undefined)
149160
t.is(global.Response, originalResponse);

test/options/index.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ test("logOptions: logs all options", (t) => {
4949
{ type: "ESModule", include: [regexp1, regexp2] },
5050
{ type: "Text", include: [regexp3] },
5151
],
52-
upstreamUrl: new URL("https://mrbbot.dev/"),
52+
upstreamUrl: new URL("https://miniflare.dev/"),
5353
validatedCrons: ["30 * * * *"],
5454
kvNamespaces: ["NAMESPACE1", "NAMESPACE2"],
5555
kvPersist: "kv-data",
@@ -71,7 +71,7 @@ test("logOptions: logs all options", (t) => {
7171
`- Scripts: <script>, ${path.join("src", "index.js")}`,
7272
"- Modules: true",
7373
"- Modules Rules: {ESModule: regexp1, regexp2}, {Text: regexp3}",
74-
"- Upstream: https://mrbbot.dev",
74+
"- Upstream: https://miniflare.dev",
7575
"- Crons: 30 * * * *",
7676
"- KV Namespaces: NAMESPACE1, NAMESPACE2",
7777
"- KV Persistence: kv-data",

test/options/processor.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,8 @@ test("getWasmBindings: defaults to empty object", async (t) => {
299299

300300
test("getUpstreamUrl: parses upstream url", (t) => {
301301
const processor = new OptionsProcessor(new NoOpLog(), {});
302-
const url = processor.getUpstreamUrl({ upstream: "https://mrbbot.dev" });
303-
t.deepEqual(url, new URL("https://mrbbot.dev"));
302+
const url = processor.getUpstreamUrl({ upstream: "https://miniflare.dev" });
303+
t.deepEqual(url, new URL("https://miniflare.dev"));
304304
});
305305
test("getUpstreamUrl: logs error if cannot parse upstream url", (t) => {
306306
const log = new TestLog();
@@ -355,7 +355,7 @@ test("getProcessedOptions: includes all processed options", async (t) => {
355355
wranglerConfigPath,
356356
modules: true,
357357
modulesRules: [{ type: "Text", include: ["**/*.txt"] }],
358-
upstream: "https://mrbbot.dev",
358+
upstream: "https://miniflare.dev",
359359
crons: ["* * * * *"],
360360
siteInclude: ["**/*.html"],
361361
siteExclude: ["**/*.png"],
@@ -412,7 +412,7 @@ test("getProcessedOptions: includes all processed options", async (t) => {
412412
t.is(instance.exports.add(1, 2), 3);
413413

414414
// Check upstream url
415-
t.deepEqual(processedOptions.upstreamUrl, new URL("https://mrbbot.dev"));
415+
t.deepEqual(processedOptions.upstreamUrl, new URL("https://miniflare.dev"));
416416

417417
// Check validated crons
418418
t.deepEqual(processedOptions.validatedCrons, ["* * * * *"]);

test/options/wrangler.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ test("getWranglerOptions: maps all options", (t) => {
9595
globs = ["**/*.js"]
9696
9797
[miniflare]
98-
upstream = "https://mrbbot.dev"
98+
upstream = "https://miniflare.dev"
9999
kv_persist = true
100100
cache_persist = "./cache"
101101
durable_objects_persist = true
@@ -126,7 +126,7 @@ test("getWranglerOptions: maps all options", (t) => {
126126
modulesRules: [
127127
{ type: "ESModule", include: ["**/*.js"], fallthrough: undefined },
128128
],
129-
upstream: "https://mrbbot.dev",
129+
upstream: "https://miniflare.dev",
130130
kvPersist: true,
131131
cachePersist: "./cache",
132132
durableObjectsPersist: true,

0 commit comments

Comments
 (0)