Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: BYOB streams, basic usage, _pending WPT_
- Loading branch information
1 parent
0307ca0
commit ab97e75
Showing
6 changed files
with
176 additions
and
8 deletions.
There are no files selected for viewing
Submodule spidermonkey
updated
3 files
+1 −1 | gecko-repository | |
+1 −1 | gecko-revision | |
+83 −0 | rebuild.sh |
120 changes: 120 additions & 0 deletions
120
integration-tests/js-compute/fixtures/byob/bin/index.js
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,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'); | ||
} | ||
} | ||
}); |
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,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" |
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,17 @@ | ||
{ | ||
"GET /": { | ||
"environments": ["viceroy"], | ||
"downstream_request": { | ||
"method": "GET", | ||
"pathname": "/" | ||
}, | ||
"downstream_response": { | ||
"status": 200, | ||
"body": "heyheyhey" | ||
}, | ||
"logs": [ | ||
"stdout :: Log: Passed 1", | ||
"stdout :: Log: Passed 2" | ||
] | ||
} | ||
} |
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