Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ext/web): EventSource #14730

Merged
merged 22 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix it!
  • Loading branch information
crowlKats committed Oct 11, 2023
commit 88133893e3e5e50b205348495d85a1f43a3c51b8
21 changes: 9 additions & 12 deletions ext/fetch/27_eventsource.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 36,7 @@ export class TextLineStream extends TransformStream {

constructor(options) {
super({
transform: (chunk, controller) => {
console.error("chunk tls:", JSONStringify(chunk));
return this.#handle(chunk, controller);
},
transform: (chunk, controller) => this.#handle(chunk, controller),
flush: (controller) => {
if (this.#buf.length > 0) {
if (
Expand All @@ -66,7 63,6 @@ export class TextLineStream extends TransformStream {
crIndex !== -1 && crIndex !== (chunk.length - 1) &&
(lfIndex === -1 || (lfIndex - 1) > crIndex)
) {
console.error("enqueue1:", chunk.slice(0, crIndex));
controller.enqueue(chunk.slice(0, crIndex));
chunk = chunk.slice(crIndex 1);
continue;
Expand Down Expand Up @@ -185,21 181,22 @@ class EventSource extends EventTarget {

async [_loop]() {
let lastEventIDValue = "";
let lastEventID = "";
while (this[_readyState] !== CLOSED) {
const lastEventIDValueCopy = lastEventIDValue;
lastEventIDValue = "";
const req = newInnerRequest(
"GET",
this[_url],
() =>
lastEventIDValue === ""
lastEventIDValueCopy === ""
? [
["accept", "text/event-stream"],
]
: [
["accept", "text/event-stream"],
[
"last-event-id",
core.ops.op_utf8_to_byte_string(lastEventIDValue),
"Last-Event-Id",
core.ops.op_utf8_to_byte_string(lastEventIDValueCopy),
],
],
null,
Expand Down Expand Up @@ -248,12 245,13 @@ class EventSource extends EventTarget {

let data = "";
let eventType = "";
let lastEventID = this[_lastEventID];

for await (
const chunk of res.body.stream
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TextLineStream())
.pipeThrough(new TextLineStream({ allowCR: true }))
) {
console.error("chunk:", JSONStringify(chunk));
if (chunk === "") {
this[_lastEventID] = lastEventID;
if (data === "") {
Expand Down Expand Up @@ -297,7 295,6 @@ class EventSource extends EventTarget {
}
case "id": {
if (!value.includes("\0")) {
console.error("new id:", JSONStringify(value));
lastEventID = value;
}
break;
Expand Down
7 changes: 5 additions & 2 deletions ext/fetch/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 972,10 @@ pub fn create_http_client(
builder.build().map_err(|e| e.into())
}

#[op]
pub fn op_utf8_to_byte_string(input: String) -> Result<ByteString, AnyError> {
#[op2]
#[serde]
pub fn op_utf8_to_byte_string(
#[string] input: String,
) -> Result<ByteString, AnyError> {
Ok(input.into())
}
4 changes: 2 additions & 2 deletions tools/wpt/expectation.json
Original file line number Diff line number Diff line change
Expand Up @@ -10734,8 10734,8 @@
"format-utf-8.any.worker.html": true,
"request-accept.any.html": true,
"request-accept.any.worker.html": true,
"request-cache-control.any.html": true,
"request-cache-control.any.worker.html": true,
"request-cache-control.any.html": false,
"request-cache-control.any.worker.html": false,
"request-credentials.window.any.html": false,
"request-credentials.window.any.worker.html": false,
"request-redirect.window.any.html": false,
Expand Down
Loading