Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorMinar committed May 16, 2024
1 parent cf05860 commit d28893a
Showing 1 changed file with 60 additions and 6 deletions.
66 changes: 60 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
import { Miniflare } from "miniflare";

const modules = {
"/hello.js": {
esModule: 'export const hello = "Hello";',
"/foo/hello.js": {
esModule: `
import {foo} from "./foo.js";
export const hello = "Hello" + foo;`,
},
"/foo/foo.js": {
esModule: `
import {bar} from "bar";
export const foo = " foo" + bar`,
},
// TODO: shouldn't this be "/_bare_/bar", or even "bar/", or ideally just "bar"?
"/_bare_/bar/index": {
esModule: `
import {baz} from "./baz.js";
export const bar = " bar" + baz`,
},
"/_bare_/bar/baz.js": {
esModule: `
export const baz = " baz"`,
},
};

Expand All @@ -15,17 +35,51 @@ const mf = new Miniflare({

const url = new URL(request.url);
const specifier = url.searchParams.get("specifier");
if (!specifier) {
const referrer = url.searchParams.get("referrer")
const rawSpecifier = url.searchParams.get("raw");

if (!rawSpecifier) {
throw new Error("no specifier provided");
}

if (!modules[specifier]) {
// hacky way to resolve url paths - this is good enough for this demo, but likely not good enough for production use
const resolvedSpecifier = rawSpecifier.startsWith('.') ?
new URL(rawSpecifier, `https://hostname${referrer}`).pathname :
rawSpecifier.startsWith('/') ?
// TODO(discuss): what does it mean to import '/foo.js' vs 'foo.js' vs './foo.js' in workerd?!?
rawSpecifier :
// TODO(discuss): workerd crashes if we 301 redirect with bare specifier,
// so prefix with /_bare_/ - this is likely a bad idea!
rawSpecifier.endsWith('.js') ?
`/_bare_/${rawSpecifier}`:
// TODO(discuss): workerd crashes if we 301 redirect to location that ends with /,
// so we have to use /index suffix instead
`/_bare_/${rawSpecifier}/index`;

console.log(`--- Fallback service debug info ---
resolve method: ${resolveMethod}
url: ${url}
specifier: ${specifier}
raw specifier: ${rawSpecifier}
referrer: ${referrer}
resolved specifier: ${resolvedSpecifier}`
);

if (specifier !== resolvedSpecifier) {
console.log(`redirecting module ${specifier} to ${resolvedSpecifier}`);
return new Response(null, {headers: {location: resolvedSpecifier}, status: 301});
}


const resolvedModule = modules[resolvedSpecifier];

if (!resolvedModule) {
return new Response(null, { status: 404 });
}

return new Response(
JSON.stringify({
...modules[specifier],
...resolvedModule,
})
);
},
Expand All @@ -41,7 +95,7 @@ const mf = new Miniflare({
contents: `
export default {
async fetch() {
const { hello } = await import("./hello.js");
const { hello } = await import("./foo/hello.js");
return new Response(hello + " World");
}
}
Expand Down

0 comments on commit d28893a

Please sign in to comment.