-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1423 from cloudflare/jsnell/workerd-module-fallba…
…ck-service
- Loading branch information
Showing
15 changed files
with
569 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Module Fallback Example | ||
|
||
To run the example on http://localhost:8080 | ||
|
||
```sh | ||
$ ./workerd serve config.capnp | ||
``` | ||
|
||
To run using bazel | ||
|
||
```sh | ||
$ bazel run //src/workerd/server:workerd -- serve ~/cloudflare/workerd/samples/module_fallback/config.capnp | ||
``` | ||
|
||
To create a standalone binary that can be run: | ||
|
||
```sh | ||
$ ./workerd compile config.capnp > helloworld | ||
|
||
$ ./helloworld | ||
``` | ||
|
||
To test: | ||
|
||
```sh | ||
% curl http://localhost:8080 | ||
Hello World | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const assert = require('assert'); | ||
const vm = require('vm'); | ||
assert(vm !== undefined); | ||
|
||
module.exports = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Workerd = import "/workerd/workerd.capnp"; | ||
|
||
const helloWorldExample :Workerd.Config = ( | ||
services = [ | ||
(name = "main", worker = .helloWorld), | ||
], | ||
|
||
sockets = [ ( name = "http", address = "*:8080", http = (), service = "main" ) ] | ||
); | ||
|
||
const helloWorld :Workerd.Worker = ( | ||
modules = [ | ||
(name = "worker", esModule = embed "worker.js"), | ||
(name = "cjs", nodeJsCompatModule = embed "cjs.js"), | ||
], | ||
compatibilityDate = "2023-02-28", | ||
compatibilityFlags = ["nodejs_compat"], | ||
moduleFallback = "localhost:8888", | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/usr/bin/env node | ||
|
||
const { createServer } = require('http'); | ||
|
||
const server = createServer((req, res) => { | ||
// The response from the fallback service must be a vaid JSON | ||
// serialization of a Worker::Module config. | ||
|
||
// The x-resolve-method tells us if the module was imported or required. | ||
console.log(req.headers['x-resolve-method']); | ||
|
||
// The req.url query params tell us what we are importing | ||
const url = new URL(req.url, "http://example.org"); | ||
const specifier = url.searchParams.get('specifier'); | ||
const referrer = url.searchParams.get('referrer'); | ||
console.log(specifier, referrer); | ||
|
||
// The fallback service can tell the client to map the request | ||
// specifier to another specifier using a 301 redirect, using | ||
// the location header to specify the alternative specifier. | ||
if (specifier == "/foo") { | ||
console.log('Redirecting /foo to /baz'); | ||
res.writeHead(301, { location: '/baz' }); | ||
res.end(); | ||
return; | ||
} | ||
|
||
if (specifier == "/bar") { | ||
res.writeHead(404); | ||
res.end(); | ||
return; | ||
} | ||
|
||
console.log(`Returning module spec for ${specifier}`); | ||
// Returning the name is optional. If it is included, then it MUST match the | ||
// request specifier! | ||
res.end(`{ | ||
"name": "${specifier}", | ||
"esModule":"export default 1;" | ||
}`); | ||
}); | ||
|
||
server.listen(8888, () => { | ||
console.log('ready...'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) 2017-2023 Cloudflare, Inc. | ||
// Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
// https://opensource.org/licenses/Apache-2.0 | ||
|
||
import * as foo from 'foo'; | ||
import * as baz from 'baz'; | ||
import * as vm from 'node:vm'; | ||
import { strictEqual } from 'node:assert'; | ||
import * as cjs from 'cjs'; | ||
|
||
try { | ||
await import('bar'); | ||
throw new Error('bar should not have been imported'); | ||
} catch { | ||
console.log('tried to import bar which does not exist'); | ||
}; | ||
|
||
console.log(foo, baz, vm); | ||
|
||
export default { | ||
async fetch(req, env) { | ||
strictEqual(1 + 1, 2); | ||
return new Response("Hello World\n"); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.