Skip to content

Commit ab97e75

Browse files
Guy BedfordJakeChampion
authored andcommitted
feat: BYOB streams, basic usage, _pending WPT_
1 parent 0307ca0 commit ab97e75

File tree

6 files changed

+176
-8
lines changed

6 files changed

+176
-8
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/* eslint-env serviceworker */
2+
/* global fastly */
3+
function strictEqual (a, b) {
4+
if (a !== b) {
5+
console.log('LHS: ', a);
6+
console.log('RHS: ', b);
7+
throw new Error(`Assertion failure`);
8+
}
9+
}
10+
11+
addEventListener("fetch", async (event) => {
12+
let cnt = 0;
13+
const stream = new ReadableStream({
14+
// type: 'bytes',
15+
pull (controller) {
16+
cnt++;
17+
const view = controller.byobRequest?.view;
18+
if (view) {
19+
view[0] = 104;
20+
view[1] = 101;
21+
view[2] = 121;
22+
controller.byobRequest.respond(3);
23+
} else {
24+
controller.enqueue(new Uint8Array([104, 101, 106]));
25+
}
26+
if (cnt === 3)
27+
return controller.close();
28+
}
29+
});
30+
event.respondWith(new Response(stream));
31+
32+
{
33+
let cnt = 0;
34+
const stream = new ReadableStream({
35+
type: 'bytes',
36+
autoAllocateChunkSize: 1024,
37+
start (controller) {
38+
},
39+
pull (controller) {
40+
cnt++;
41+
const view = controller.byobRequest?.view;
42+
if (cnt < 3) {
43+
view[0] = 1;
44+
view[1] = 2;
45+
view[2] = 3;
46+
controller.byobRequest.respond(3);
47+
} else if (cnt == 3) {
48+
controller.enqueue(new Uint8Array([1,2,4]));
49+
}
50+
if (cnt === 3)
51+
return controller.close();
52+
}
53+
});
54+
55+
const byobReader = stream.getReader();
56+
let buf;
57+
let result;
58+
let offset = 0;
59+
while (true) {
60+
if (buf && cnt < 3) {
61+
const output = new Uint8Array(buf);
62+
strictEqual(output[0], 1);
63+
strictEqual(output[1], 2);
64+
strictEqual(output[2], 3);
65+
strictEqual(output[3], 0);
66+
strictEqual(output.byteLength, 1024);
67+
}
68+
result = await byobReader.read();
69+
if (result.done)
70+
break;
71+
offset += result.value.byteLength;
72+
buf = result.value.buffer;
73+
}
74+
75+
strictEqual(cnt, 3);
76+
const output = new Uint8Array(buf);
77+
strictEqual(output[0], 1);
78+
strictEqual(output[1], 2);
79+
strictEqual(output[2], 4);
80+
strictEqual(output[3], undefined);
81+
strictEqual(output.byteLength, 3);
82+
console.log('Passed 1');
83+
}
84+
85+
{
86+
let cnt = 0;
87+
const stream = new ReadableStream({
88+
type: 'bytes',
89+
start (controller) {
90+
},
91+
pull (controller) {
92+
cnt++;
93+
const view = controller.byobRequest?.view;
94+
if (view) {
95+
view[0] = 1;
96+
view[1] = 2;
97+
view[2] = 3;
98+
controller.byobRequest.respond(3);
99+
}
100+
if (cnt === 3)
101+
return controller.close();
102+
}
103+
});
104+
105+
const byobReader = stream.getReader({ mode: 'byob' });
106+
let buf = new ArrayBuffer(10);
107+
let result;
108+
let offset = 0;
109+
do {
110+
result = await byobReader.read(new Uint8Array(buf, offset));
111+
offset += result.value.byteLength;
112+
buf = result.value.buffer;
113+
} while(!result.done);
114+
115+
const out = new Uint8Array(buf);
116+
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) {
117+
console.log('Passed 2');
118+
}
119+
}
120+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This file describes a Fastly Compute@Edge package. To learn more visit:
2+
# https://developer.fastly.com/reference/fastly-toml/
3+
4+
authors = ["[email protected]"]
5+
description = ""
6+
language = "other"
7+
manifest_version = 2
8+
name = "console"
9+
service_id = ""
10+
11+
[scripts]
12+
build = "node ../../../../js-compute-runtime-cli.js"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"GET /": {
3+
"environments": ["viceroy"],
4+
"downstream_request": {
5+
"method": "GET",
6+
"pathname": "/"
7+
},
8+
"downstream_response": {
9+
"status": 200,
10+
"body": "heyheyhey"
11+
},
12+
"logs": [
13+
"stdout :: Log: Passed 1",
14+
"stdout :: Log: Passed 2"
15+
]
16+
}
17+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"test": "npm run test:types && npm run test:cli",
3232
"test:cli": "brittle --bail integration-tests/cli/**.test.js",
3333
"test:types": "tsd",
34-
"build": "make -j8 -C c-dependencies/js-compute-runtime && cp c-dependencies/js-compute-runtime/*.wasm c-dependencies/js-compute-runtime/fastly.wit ."
34+
"build": "make -j8 -C c-dependencies/js-compute-runtime && cp c-dependencies/js-compute-runtime/*.wasm c-dependencies/js-compute-runtime/fastly.wit .",
35+
"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 ."
3536
},
3637
"devDependencies": {
3738
"@jakechampion/cli-testing-library": "^1.0.0",

run-local

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set -euo pipefail
44

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

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

26+
case "$mode" in
27+
release)
28+
yarn build
29+
(
30+
cd $application_path
31+
../../replace-host.sh $application_name "https://compute-sdk-test-backend.edgecompute.app"
32+
fastly compute serve --quiet
33+
)
34+
;;
2535

26-
yarn build
27-
(
28-
cd $application_path
29-
../../replace-host.sh $application_name "https://compute-sdk-test-backend.edgecompute.app"
30-
fastly compute serve --quiet
31-
)
36+
debug)
37+
yarn build:debug
38+
(
39+
cd $application_path
40+
../../replace-host.sh $application_name "https://compute-sdk-test-backend.edgecompute.app"
41+
fastly compute serve --verbose
42+
)
43+
;;
44+
45+
*)
46+
echo "Unknown build type: $mode"
47+
exit 1
48+
;;
49+
esac

0 commit comments

Comments
 (0)