2021-04-08 18:34:15 -04:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
((window) => {
|
2021-04-20 08:47:22 -04:00
|
|
|
const { InnerBody } = window.__bootstrap.fetchBody;
|
|
|
|
const { Response, fromInnerRequest, toInnerResponse, newInnerRequest } =
|
2021-04-08 18:34:15 -04:00
|
|
|
window.__bootstrap.fetch;
|
|
|
|
const errors = window.__bootstrap.errors.errors;
|
|
|
|
const core = window.Deno.core;
|
|
|
|
const { ReadableStream } = window.__bootstrap.streams;
|
|
|
|
|
2021-04-09 11:54:27 -04:00
|
|
|
function serveHttp(conn) {
|
2021-04-12 15:55:05 -04:00
|
|
|
const rid = Deno.core.opSync("op_http_start", conn.rid);
|
2021-04-08 18:34:15 -04:00
|
|
|
return new HttpConn(rid);
|
|
|
|
}
|
|
|
|
|
2021-05-19 08:39:52 -04:00
|
|
|
const connErrorSymbol = Symbol("connError");
|
|
|
|
|
2021-04-08 18:34:15 -04:00
|
|
|
class HttpConn {
|
|
|
|
#rid = 0;
|
|
|
|
|
|
|
|
constructor(rid) {
|
|
|
|
this.#rid = rid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @returns {number} */
|
|
|
|
get rid() {
|
|
|
|
return this.#rid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @returns {Promise<ResponseEvent | null>} */
|
|
|
|
async nextRequest() {
|
|
|
|
let nextRequest;
|
|
|
|
try {
|
2021-04-12 15:55:05 -04:00
|
|
|
nextRequest = await Deno.core.opAsync(
|
2021-04-08 18:34:15 -04:00
|
|
|
"op_http_request_next",
|
|
|
|
this.#rid,
|
|
|
|
);
|
|
|
|
} catch (error) {
|
2021-05-19 08:39:52 -04:00
|
|
|
// A connection error seen here would cause disrupted responses to throw
|
|
|
|
// a generic `BadResource` error. Instead store this error and replace
|
|
|
|
// those with it.
|
|
|
|
this[connErrorSymbol] = error;
|
2021-04-08 18:34:15 -04:00
|
|
|
if (error instanceof errors.BadResource) {
|
|
|
|
return null;
|
|
|
|
} else if (error instanceof errors.Interrupted) {
|
|
|
|
return null;
|
2021-05-19 08:39:52 -04:00
|
|
|
} else if (error.message.includes("connection closed")) {
|
|
|
|
return null;
|
2021-04-08 18:34:15 -04:00
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
if (nextRequest === null) return null;
|
|
|
|
|
|
|
|
const [
|
|
|
|
requestBodyRid,
|
|
|
|
responseSenderRid,
|
|
|
|
method,
|
|
|
|
headersList,
|
|
|
|
url,
|
|
|
|
] = nextRequest;
|
|
|
|
|
|
|
|
/** @type {ReadableStream<Uint8Array> | undefined} */
|
2021-04-20 08:47:22 -04:00
|
|
|
let body = null;
|
2021-04-08 18:34:15 -04:00
|
|
|
if (typeof requestBodyRid === "number") {
|
|
|
|
body = createRequestBodyStream(requestBodyRid);
|
|
|
|
}
|
|
|
|
|
2021-04-20 08:47:22 -04:00
|
|
|
const innerRequest = newInnerRequest(
|
2021-04-08 18:34:15 -04:00
|
|
|
method,
|
2021-04-20 08:47:22 -04:00
|
|
|
url,
|
|
|
|
headersList,
|
|
|
|
body !== null ? new InnerBody(body) : null,
|
|
|
|
);
|
2021-06-06 09:37:17 -04:00
|
|
|
const request = fromInnerRequest(innerRequest, null, "immutable");
|
2021-04-08 18:34:15 -04:00
|
|
|
|
2021-05-19 08:39:52 -04:00
|
|
|
const respondWith = createRespondWith(this, responseSenderRid);
|
2021-04-08 18:34:15 -04:00
|
|
|
|
|
|
|
return { request, respondWith };
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @returns {void} */
|
|
|
|
close() {
|
|
|
|
core.close(this.#rid);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Symbol.asyncIterator]() {
|
2021-05-18 11:24:01 -04:00
|
|
|
// deno-lint-ignore no-this-alias
|
2021-04-08 18:34:15 -04:00
|
|
|
const httpConn = this;
|
|
|
|
return {
|
|
|
|
async next() {
|
|
|
|
const reqEvt = await httpConn.nextRequest();
|
2021-04-14 07:56:14 -04:00
|
|
|
// Change with caution, current form avoids a v8 deopt
|
|
|
|
return { value: reqEvt, done: reqEvt === null };
|
2021-04-08 18:34:15 -04:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function readRequest(requestRid, zeroCopyBuf) {
|
2021-04-12 15:55:05 -04:00
|
|
|
return Deno.core.opAsync(
|
2021-04-08 18:34:15 -04:00
|
|
|
"op_http_request_read",
|
|
|
|
requestRid,
|
|
|
|
zeroCopyBuf,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-19 08:39:52 -04:00
|
|
|
function createRespondWith(httpConn, responseSenderRid) {
|
2021-04-13 07:41:47 -04:00
|
|
|
return async function respondWith(resp) {
|
2021-04-08 18:34:15 -04:00
|
|
|
if (resp instanceof Promise) {
|
|
|
|
resp = await resp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(resp instanceof Response)) {
|
|
|
|
throw new TypeError(
|
|
|
|
"First argument to respondWith must be a Response or a promise resolving to a Response.",
|
|
|
|
);
|
|
|
|
}
|
2021-04-20 08:47:22 -04:00
|
|
|
|
|
|
|
const innerResp = toInnerResponse(resp);
|
|
|
|
|
|
|
|
// If response body length is known, it will be sent synchronously in a
|
|
|
|
// single op, in other case a "response body" resource will be created and
|
|
|
|
// we'll be streaming it.
|
|
|
|
/** @type {ReadableStream<Uint8Array> | Uint8Array | null} */
|
|
|
|
let respBody = null;
|
|
|
|
if (innerResp.body !== null) {
|
|
|
|
if (innerResp.body.unusable()) throw new TypeError("Body is unusable.");
|
|
|
|
if (innerResp.body.streamOrStatic instanceof ReadableStream) {
|
|
|
|
if (innerResp.body.length === null) {
|
|
|
|
respBody = innerResp.body.stream;
|
|
|
|
} else {
|
|
|
|
const reader = innerResp.body.stream.getReader();
|
|
|
|
const r1 = await reader.read();
|
2021-05-20 21:11:53 -04:00
|
|
|
if (r1.done) {
|
|
|
|
respBody = new Uint8Array(0);
|
|
|
|
} else {
|
|
|
|
respBody = r1.value;
|
|
|
|
const r2 = await reader.read();
|
|
|
|
if (!r2.done) throw new TypeError("Unreachable");
|
|
|
|
}
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
innerResp.body.streamOrStatic.consumed = true;
|
|
|
|
respBody = innerResp.body.streamOrStatic.body;
|
|
|
|
}
|
2021-04-08 18:34:15 -04:00
|
|
|
} else {
|
2021-04-20 08:47:22 -04:00
|
|
|
respBody = new Uint8Array(0);
|
2021-04-08 18:34:15 -04:00
|
|
|
}
|
|
|
|
|
2021-04-23 06:34:04 -04:00
|
|
|
let responseBodyRid;
|
|
|
|
try {
|
|
|
|
responseBodyRid = await Deno.core.opAsync("op_http_response", [
|
|
|
|
responseSenderRid,
|
|
|
|
innerResp.status ?? 200,
|
|
|
|
innerResp.headerList,
|
|
|
|
], respBody instanceof Uint8Array ? respBody : null);
|
|
|
|
} catch (error) {
|
2021-05-19 08:39:52 -04:00
|
|
|
const connError = httpConn[connErrorSymbol];
|
|
|
|
if (error instanceof errors.BadResource && connError != null) {
|
|
|
|
// deno-lint-ignore no-ex-assign
|
|
|
|
error = new connError.constructor(connError.message);
|
|
|
|
}
|
2021-04-23 06:34:04 -04:00
|
|
|
if (respBody !== null && respBody instanceof ReadableStream) {
|
|
|
|
await respBody.cancel(error);
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2021-04-08 18:34:15 -04:00
|
|
|
|
|
|
|
// If `respond` returns a responseBodyRid, we should stream the body
|
|
|
|
// to that resource.
|
2021-04-20 08:47:22 -04:00
|
|
|
if (responseBodyRid !== null) {
|
2021-04-23 06:34:04 -04:00
|
|
|
try {
|
|
|
|
if (respBody === null || !(respBody instanceof ReadableStream)) {
|
|
|
|
throw new TypeError("Unreachable");
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
2021-04-23 06:34:04 -04:00
|
|
|
const reader = respBody.getReader();
|
|
|
|
while (true) {
|
|
|
|
const { value, done } = await reader.read();
|
|
|
|
if (done) break;
|
|
|
|
if (!(value instanceof Uint8Array)) {
|
|
|
|
await reader.cancel(new TypeError("Value not a Uint8Array"));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await Deno.core.opAsync(
|
|
|
|
"op_http_response_write",
|
|
|
|
responseBodyRid,
|
|
|
|
value,
|
|
|
|
);
|
|
|
|
} catch (error) {
|
2021-05-19 08:39:52 -04:00
|
|
|
const connError = httpConn[connErrorSymbol];
|
|
|
|
if (error instanceof errors.BadResource && connError != null) {
|
|
|
|
// deno-lint-ignore no-ex-assign
|
|
|
|
error = new connError.constructor(connError.message);
|
|
|
|
}
|
2021-04-23 06:34:04 -04:00
|
|
|
await reader.cancel(error);
|
|
|
|
throw error;
|
|
|
|
}
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
2021-04-23 06:34:04 -04:00
|
|
|
} finally {
|
|
|
|
// Once all chunks are sent, and the request body is closed, we can
|
|
|
|
// close the response body.
|
2021-05-19 08:39:52 -04:00
|
|
|
try {
|
|
|
|
await Deno.core.opAsync("op_http_response_close", responseBodyRid);
|
|
|
|
} catch { /* pass */ }
|
2021-04-08 18:34:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function createRequestBodyStream(requestBodyRid) {
|
|
|
|
return new ReadableStream({
|
|
|
|
type: "bytes",
|
|
|
|
async pull(controller) {
|
|
|
|
try {
|
|
|
|
// This is the largest possible size for a single packet on a TLS
|
|
|
|
// stream.
|
|
|
|
const chunk = new Uint8Array(16 * 1024 + 256);
|
|
|
|
const read = await readRequest(
|
|
|
|
requestBodyRid,
|
|
|
|
chunk,
|
|
|
|
);
|
|
|
|
if (read > 0) {
|
|
|
|
// We read some data. Enqueue it onto the stream.
|
|
|
|
controller.enqueue(chunk.subarray(0, read));
|
|
|
|
} else {
|
|
|
|
// We have reached the end of the body, so we close the stream.
|
|
|
|
controller.close();
|
|
|
|
core.close(requestBodyRid);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
// There was an error while reading a chunk of the body, so we
|
|
|
|
// error.
|
|
|
|
controller.error(err);
|
|
|
|
controller.close();
|
|
|
|
core.close(requestBodyRid);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
cancel() {
|
|
|
|
core.close(requestBodyRid);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
window.__bootstrap.http = {
|
2021-04-09 11:54:27 -04:00
|
|
|
serveHttp,
|
2021-04-08 18:34:15 -04:00
|
|
|
};
|
|
|
|
})(this);
|