0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

refactor: use primordials in extensions/net/ (#11243)

This commit is contained in:
Bartek Iwańczuk 2021-07-03 17:02:14 +02:00 committed by GitHub
parent 6137c8046d
commit bd7bb43a0e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 11 deletions

View file

@ -4,6 +4,12 @@
((window) => { ((window) => {
const core = window.Deno.core; const core = window.Deno.core;
const { BadResource } = core; const { BadResource } = core;
const {
PromiseResolve,
Symbol,
Uint8Array,
TypedArrayPrototypeSubarray,
} = window.__bootstrap.primordials;
async function read( async function read(
rid, rid,
@ -128,7 +134,7 @@
return(value) { return(value) {
this.close(); this.close();
return Promise.resolve({ value, done: true }); return PromiseResolve({ value, done: true });
} }
close() { close() {
@ -165,7 +171,7 @@
this.addr.transport, this.addr.transport,
buf, buf,
); );
const sub = buf.subarray(0, size); const sub = TypedArrayPrototypeSubarray(buf, 0, size);
return [sub, remoteAddr]; return [sub, remoteAddr];
} }

View file

@ -9,9 +9,18 @@
const { BadResource, Interrupted } = core; const { BadResource, Interrupted } = core;
const { ReadableStream } = window.__bootstrap.streams; const { ReadableStream } = window.__bootstrap.streams;
const abortSignal = window.__bootstrap.abortSignal; const abortSignal = window.__bootstrap.abortSignal;
const {
Symbol,
Uint8Array,
Promise,
StringPrototypeIncludes,
SymbolAsyncIterator,
TypeError,
TypedArrayPrototypeSubarray,
} = window.__bootstrap.primordials;
function serveHttp(conn) { function serveHttp(conn) {
const rid = Deno.core.opSync("op_http_start", conn.rid); const rid = core.opSync("op_http_start", conn.rid);
return new HttpConn(rid); return new HttpConn(rid);
} }
@ -33,7 +42,7 @@
async nextRequest() { async nextRequest() {
let nextRequest; let nextRequest;
try { try {
nextRequest = await Deno.core.opAsync( nextRequest = await core.opAsync(
"op_http_request_next", "op_http_request_next",
this.#rid, this.#rid,
); );
@ -46,7 +55,9 @@
return null; return null;
} else if (error instanceof Interrupted) { } else if (error instanceof Interrupted) {
return null; return null;
} else if (error.message.includes("connection closed")) { } else if (
StringPrototypeIncludes(error.message, "connection closed")
) {
return null; return null;
} }
throw error; throw error;
@ -86,7 +97,7 @@
core.close(this.#rid); core.close(this.#rid);
} }
[Symbol.asyncIterator]() { [SymbolAsyncIterator]() {
// deno-lint-ignore no-this-alias // deno-lint-ignore no-this-alias
const httpConn = this; const httpConn = this;
return { return {
@ -100,7 +111,7 @@
} }
function readRequest(requestRid, zeroCopyBuf) { function readRequest(requestRid, zeroCopyBuf) {
return Deno.core.opAsync( return core.opAsync(
"op_http_request_read", "op_http_request_read",
requestRid, requestRid,
zeroCopyBuf, zeroCopyBuf,
@ -152,7 +163,7 @@
let responseBodyRid; let responseBodyRid;
try { try {
responseBodyRid = await Deno.core.opAsync("op_http_response", [ responseBodyRid = await core.opAsync("op_http_response", [
responseSenderRid, responseSenderRid,
innerResp.status ?? 200, innerResp.status ?? 200,
innerResp.headerList, innerResp.headerList,
@ -185,7 +196,7 @@
break; break;
} }
try { try {
await Deno.core.opAsync( await core.opAsync(
"op_http_response_write", "op_http_response_write",
responseBodyRid, responseBodyRid,
value, value,
@ -204,7 +215,7 @@
// Once all chunks are sent, and the request body is closed, we can // Once all chunks are sent, and the request body is closed, we can
// close the response body. // close the response body.
try { try {
await Deno.core.opAsync("op_http_response_close", responseBodyRid); await core.opAsync("op_http_response_close", responseBodyRid);
} catch { /* pass */ } } catch { /* pass */ }
} }
} }
@ -225,7 +236,7 @@
); );
if (read > 0) { if (read > 0) {
// We read some data. Enqueue it onto the stream. // We read some data. Enqueue it onto the stream.
controller.enqueue(chunk.subarray(0, read)); controller.enqueue(TypedArrayPrototypeSubarray(chunk, 0, read));
} else { } else {
// We have reached the end of the body, so we close the stream. // We have reached the end of the body, so we close the stream.
controller.close(); controller.close();