Skip to content

Commit

Permalink
feat: BYOB streams, basic usage, _pending WPT_
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford authored and JakeChampion committed Apr 11, 2023
1 parent 0307ca0 commit ab97e75
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 8 deletions.
2 changes: 1 addition & 1 deletion c-dependencies/spidermonkey
120 changes: 120 additions & 0 deletions integration-tests/js-compute/fixtures/byob/bin/index.js
@@ -0,0 +1,120 @@
/* eslint-env serviceworker */
/* global fastly */
function strictEqual (a, b) {
if (a !== b) {
console.log('LHS: ', a);
console.log('RHS: ', b);
throw new Error(`Assertion failure`);
}
}

addEventListener("fetch", async (event) => {
let cnt = 0;
const stream = new ReadableStream({
// type: 'bytes',
pull (controller) {
cnt++;
const view = controller.byobRequest?.view;
if (view) {
view[0] = 104;
view[1] = 101;
view[2] = 121;
controller.byobRequest.respond(3);
} else {
controller.enqueue(new Uint8Array([104, 101, 106]));
}
if (cnt === 3)
return controller.close();
}
});
event.respondWith(new Response(stream));

{
let cnt = 0;
const stream = new ReadableStream({
type: 'bytes',
autoAllocateChunkSize: 1024,
start (controller) {
},
pull (controller) {
cnt++;
const view = controller.byobRequest?.view;
if (cnt < 3) {
view[0] = 1;
view[1] = 2;
view[2] = 3;
controller.byobRequest.respond(3);
} else if (cnt == 3) {
controller.enqueue(new Uint8Array([1,2,4]));
}
if (cnt === 3)
return controller.close();
}
});

const byobReader = stream.getReader();
let buf;
let result;
let offset = 0;
while (true) {
if (buf && cnt < 3) {
const output = new Uint8Array(buf);
strictEqual(output[0], 1);
strictEqual(output[1], 2);
strictEqual(output[2], 3);
strictEqual(output[3], 0);
strictEqual(output.byteLength, 1024);
}
result = await byobReader.read();
if (result.done)
break;
offset += result.value.byteLength;
buf = result.value.buffer;
}

strictEqual(cnt, 3);
const output = new Uint8Array(buf);
strictEqual(output[0], 1);
strictEqual(output[1], 2);
strictEqual(output[2], 4);
strictEqual(output[3], undefined);
strictEqual(output.byteLength, 3);
console.log('Passed 1');
}

{
let cnt = 0;
const stream = new ReadableStream({
type: 'bytes',
start (controller) {
},
pull (controller) {
cnt++;
const view = controller.byobRequest?.view;
if (view) {
view[0] = 1;
view[1] = 2;
view[2] = 3;
controller.byobRequest.respond(3);
}
if (cnt === 3)
return controller.close();
}
});

const byobReader = stream.getReader({ mode: 'byob' });
let buf = new ArrayBuffer(10);
let result;
let offset = 0;
do {
result = await byobReader.read(new Uint8Array(buf, offset));
offset += result.value.byteLength;
buf = result.value.buffer;
} while(!result.done);

const out = new Uint8Array(buf);
if (out[0] === 1 && out[1] === 2 && out[2] === 3 && out[3] === 1 && out[4] === 2 && out[5] === 3 && out[6] === 1 && out[7] === 2 && out[8] === 3) {
console.log('Passed 2');
}
}
});
12 changes: 12 additions & 0 deletions integration-tests/js-compute/fixtures/byob/fastly.toml.in
@@ -0,0 +1,12 @@
# This file describes a Fastly Compute@Edge package. To learn more visit:
# https://developer.fastly.com/reference/fastly-toml/

authors = ["jchampion@fastly.com"]
description = ""
language = "other"
manifest_version = 2
name = "console"
service_id = ""

[scripts]
build = "node ../../../../js-compute-runtime-cli.js"
17 changes: 17 additions & 0 deletions integration-tests/js-compute/fixtures/byob/tests.json
@@ -0,0 +1,17 @@
{
"GET /": {
"environments": ["viceroy"],
"downstream_request": {
"method": "GET",
"pathname": "/"
},
"downstream_response": {
"status": 200,
"body": "heyheyhey"
},
"logs": [
"stdout :: Log: Passed 1",
"stdout :: Log: Passed 2"
]
}
}
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -31,7 +31,8 @@
"test": "npm run test:types && npm run test:cli",
"test:cli": "brittle --bail integration-tests/cli/**.test.js",
"test:types": "tsd",
"build": "make -j8 -C c-dependencies/js-compute-runtime && cp c-dependencies/js-compute-runtime/*.wasm c-dependencies/js-compute-runtime/fastly.wit ."
"build": "make -j8 -C c-dependencies/js-compute-runtime && cp c-dependencies/js-compute-runtime/*.wasm c-dependencies/js-compute-runtime/fastly.wit .",
"build:debug": "DEBUG=true make -j8 -C c-dependencies/js-compute-runtime && cp c-dependencies/js-compute-runtime/*.wasm c-dependencies/js-compute-runtime/fastly.wit ."
},
"devDependencies": {
"@jakechampion/cli-testing-library": "^1.0.0",
Expand Down
30 changes: 24 additions & 6 deletions run-local
Expand Up @@ -4,6 +4,7 @@ set -euo pipefail

cd "$(dirname "${BASH_SOURCE[0]}")"
root="$(pwd)"
mode=${MODE:-release}

if [ "$#" -lt 1 ]; then
echo "Usage: $0 <application_name>"
Expand All @@ -22,10 +23,27 @@ if [ ! -d $application_path ]; then
exit 1
fi

case "$mode" in
release)
yarn build
(
cd $application_path
../../replace-host.sh $application_name "https://compute-sdk-test-backend.edgecompute.app"
fastly compute serve --quiet
)
;;

yarn build
(
cd $application_path
../../replace-host.sh $application_name "https://compute-sdk-test-backend.edgecompute.app"
fastly compute serve --quiet
)
debug)
yarn build:debug
(
cd $application_path
../../replace-host.sh $application_name "https://compute-sdk-test-backend.edgecompute.app"
fastly compute serve --verbose
)
;;

*)
echo "Unknown build type: $mode"
exit 1
;;
esac

0 comments on commit ab97e75

Please sign in to comment.