Skip to content

Commit c04b8b2

Browse files
authored
Merge pull request #1201 from joshunrau/interactive-features
add force flag to serve-instrument cli and support text-based static assets
2 parents bf78bab + ae216c7 commit c04b8b2

File tree

2 files changed

+29
-15
lines changed
  • packages

2 files changed

+29
-15
lines changed

packages/runtime-internal/src/interactive/worker.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,38 @@ const staticAssets = new Map();
66
/**
77
* Converts a data URL (base64 or plain) into a Fetch API Response object.
88
*
9-
* @param {string} dataUrl - The data URL to convert. Example: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
9+
* @param {string} dataUrl - The data URL to convert. Example: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..." or "data:text/plain,Hello%20World"
1010
* @returns {Response} A Response object representing the decoded data URL content.
1111
*/
1212
function dataUrlToResponse(dataUrl) {
13-
const match = /^data:(image\/[a-zA-Z+]+);base64,([A-Za-z0-9+/=]+)$/.exec(dataUrl);
14-
if (!match) {
15-
throw new Error('Invalid or unsupported data URL: must be a base64-encoded image');
16-
}
13+
// Try base64 image format first
14+
const base64Match = /^data:(image\/[a-zA-Z+]+);base64,([A-Za-z0-9+/=]+)$/.exec(dataUrl);
15+
if (base64Match) {
16+
const [, mimeType, base64Data] = /** @type {any[]} */ (base64Match);
1717

18-
const [, mimeType, base64Data] = /** @type {any[]} */ (match);
18+
// Decode base64 to Uint8Array
19+
const binaryString = atob(base64Data);
20+
const bytes = new Uint8Array(binaryString.length);
21+
for (let i = 0; i < binaryString.length; i++) {
22+
bytes[i] = binaryString.charCodeAt(i);
23+
}
1924

20-
// Decode base64 to Uint8Array
21-
const binaryString = atob(base64Data);
22-
const bytes = new Uint8Array(binaryString.length);
23-
for (let i = 0; i < binaryString.length; i++) {
24-
bytes[i] = binaryString.charCodeAt(i);
25+
return new Response(bytes, {
26+
headers: { 'Content-Type': mimeType }
27+
});
2528
}
2629

27-
return new Response(bytes, {
28-
headers: { 'Content-Type': mimeType }
29-
});
30+
// Try plain text format
31+
const textMatch = /^data:(text\/[a-zA-Z+]+),(.*)$/.exec(dataUrl);
32+
if (textMatch) {
33+
const [, mimeType, textData] = /** @type {any[]} */ (textMatch);
34+
const decodedText = decodeURIComponent(textData);
35+
return new Response(decodedText, {
36+
headers: { 'Content-Type': mimeType }
37+
});
38+
}
39+
40+
throw new Error('Invalid or unsupported data URL: must be a base64-encoded image or plain text');
3041
}
3142

3243
addEventListener('message', (event) => {

packages/serve-instrument/src/cli.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ program.argument('<target>', 'the directory containing the instrument', (target:
2727
return target;
2828
});
2929

30+
program.option('-f --force', 'force dependency reoptimization');
31+
3032
program.option(
3133
'-p --port <number>',
3234
'the port to run the dev server on',
@@ -41,7 +43,7 @@ program.option(
4143
);
4244

4345
program.action(async (target: string) => {
44-
const { port } = program.opts<{ port: number }>();
46+
const { force, port } = program.opts<{ force?: boolean; port: number }>();
4547

4648
const getEncodedBundle = async () => {
4749
const inputs: BundlerInput[] = [];
@@ -60,6 +62,7 @@ program.action(async (target: string) => {
6062
};
6163

6264
const server = await createServer({
65+
forceOptimizeDeps: force,
6366
plugins: [
6467
{
6568
configureServer: (server): void => {

0 commit comments

Comments
 (0)