Skip to content

Commit

Permalink
feat(worker): add web streams support
Browse files Browse the repository at this point in the history
Adds Web Streams API to worker context, and switches to the titelmedia fork of node-fetch which uses Web Streams instead of NodeJS ones. Also closes issue gja#39, as @titelmedia/node-fetch implements Response.redirect.
  • Loading branch information
mrbbot committed Dec 16, 2020
1 parent 56875a6 commit a59fb10
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 11 deletions.
9 changes: 8 additions & 1 deletion app/__tests__/worker_spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const express = require("express");
const { Worker } = require("../worker");
const { InMemoryKVStore } = require("../in-memory-kv-store");
const { Headers } = require("node-fetch");
const { Headers } = require("@titelmedia/node-fetch");

describe("Workers", () => {
test("It Can Create and Execute a Listener", () => {
Expand Down Expand Up @@ -103,6 +103,13 @@ describe("Workers", () => {
expect(await response.text()).toBe("hello");
});

test("It can return a redirect response", async () => {
const worker = new Worker("foo.com", 'addEventListener("fetch", (e) => e.respondWith(Response.redirect("http://bar.com", 302)))');
const response = await worker.executeFetchEvent("http://foo.com");
expect(response.status).toBe(302);
expect(response.headers.get("Location")).toBe("http://bar.com");
});

describe("Cloudflare Headers", () => {
it("Adds cloudflare headers", async () => {
const worker = new Worker(
Expand Down
37 changes: 34 additions & 3 deletions app/worker.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
const { createContext, Script } = require("vm");
const { Request, Response, Headers } = require("node-fetch");
// The titelmedia node-fetch fork uses Web Streams instead of NodeJS ones
const { Request, Response, Headers } = require("@titelmedia/node-fetch");
const { URL } = require("url");
const fetch = require("node-fetch");
const fetch = require("@titelmedia/node-fetch");
const atob = require("atob");
const btoa = require("btoa");
const crypto = new (require("node-webcrypto-ossl"))();
const { TextDecoder, TextEncoder } = require("util");
const { caches } = require("./caches");
const {
ByteLengthQueuingStrategy,
CountQueuingStrategy,
ReadableByteStreamController,
ReadableStream,
ReadableStreamBYOBReader,
ReadableStreamBYOBRequest,
ReadableStreamDefaultController,
ReadableStreamDefaultReader,
TransformStream,
TransformStreamDefaultController,
WritableStream,
WritableStreamDefaultController,
WritableStreamDefaultWriter,
} = require("web-streams-polyfill");

function chomp(str) {
return str.substr(0, str.length - 1);
Expand Down Expand Up @@ -106,7 +122,22 @@ class Worker {
clearInterval,

// Cache stubs
caches
caches,

// Streams
ByteLengthQueuingStrategy,
CountQueuingStrategy,
ReadableByteStreamController,
ReadableStream,
ReadableStreamBYOBReader,
ReadableStreamBYOBRequest,
ReadableStreamDefaultController,
ReadableStreamDefaultReader,
TransformStream,
TransformStreamDefaultController,
WritableStream,
WritableStreamDefaultController,
WritableStreamDefaultWriter
};
const script = new Script(workerContents);
script.runInContext(
Expand Down
25 changes: 20 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@
"homepage": "https://github.com/gja/cloudflare-worker-local#readme",
"dependencies": {
"@iarna/toml": "^2.2.3",
"@titelmedia/node-fetch": "^3.1.1",
"atob": "^2.1.2",
"body-parser": "^1.18.3",
"btoa": "^1.2.1",
"express": "^4.16.4",
"lodash.get": "^4.4.2",
"lodash.merge": "^4.6.2",
"mkdirp": "^1.0.4",
"node-fetch": "^2.3.0",
"node-webcrypto-ossl": "^1.0.48"
"node-webcrypto-ossl": "^1.0.48",
"web-streams-polyfill": "^3.0.1"
},
"optionalDependencies": {
"minio": "^7.0.15"
Expand Down

0 comments on commit a59fb10

Please sign in to comment.