mirror of
https://github.com/denoland/deno.git
synced 2024-11-28 16:20:57 -05:00
perf(webidl): inline ResponseInit converter (#12285)
This commit is contained in:
parent
5508a0f45e
commit
5bad8e1773
2 changed files with 46 additions and 3 deletions
|
@ -3,6 +3,7 @@ import {
|
||||||
assert,
|
assert,
|
||||||
assertEquals,
|
assertEquals,
|
||||||
assertStringIncludes,
|
assertStringIncludes,
|
||||||
|
assertThrows,
|
||||||
unitTest,
|
unitTest,
|
||||||
} from "./test_util.ts";
|
} from "./test_util.ts";
|
||||||
|
|
||||||
|
@ -56,6 +57,23 @@ unitTest(async function responseFormData() {
|
||||||
assertEquals([...formData], [...input]);
|
assertEquals([...formData], [...input]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
unitTest(function responseInvalidInit() {
|
||||||
|
// deno-lint-ignore ban-ts-comment
|
||||||
|
// @ts-expect-error
|
||||||
|
assertThrows(() => new Response("", 0));
|
||||||
|
assertThrows(() => new Response("", { status: 0 }));
|
||||||
|
// deno-lint-ignore ban-ts-comment
|
||||||
|
// @ts-expect-error
|
||||||
|
assertThrows(() => new Response("", { status: null }));
|
||||||
|
});
|
||||||
|
|
||||||
|
unitTest(function responseNullInit() {
|
||||||
|
// deno-lint-ignore ban-ts-comment
|
||||||
|
// @ts-expect-error
|
||||||
|
const response = new Response("", null);
|
||||||
|
assertEquals(response.status, 200);
|
||||||
|
});
|
||||||
|
|
||||||
unitTest(function customInspectFunction() {
|
unitTest(function customInspectFunction() {
|
||||||
const response = new Response();
|
const response = new Response();
|
||||||
assertEquals(
|
assertEquals(
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
((window) => {
|
((window) => {
|
||||||
|
const { isProxy } = Deno.core;
|
||||||
const webidl = window.__bootstrap.webidl;
|
const webidl = window.__bootstrap.webidl;
|
||||||
const consoleInternal = window.__bootstrap.console;
|
const consoleInternal = window.__bootstrap.console;
|
||||||
const { HTTP_TAB_OR_SPACE, regexMatcher } = window.__bootstrap.infra;
|
const { HTTP_TAB_OR_SPACE, regexMatcher } = window.__bootstrap.infra;
|
||||||
|
@ -249,7 +250,7 @@
|
||||||
prefix,
|
prefix,
|
||||||
context: "Argument 1",
|
context: "Argument 1",
|
||||||
});
|
});
|
||||||
init = webidl.converters["ResponseInit"](init, {
|
init = webidl.converters["ResponseInit_fast"](init, {
|
||||||
prefix,
|
prefix,
|
||||||
context: "Argument 2",
|
context: "Argument 2",
|
||||||
});
|
});
|
||||||
|
@ -260,7 +261,10 @@
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!RegExpPrototypeTest(REASON_PHRASE_RE, init.statusText)) {
|
if (
|
||||||
|
init.statusText &&
|
||||||
|
!RegExpPrototypeTest(REASON_PHRASE_RE, init.statusText)
|
||||||
|
) {
|
||||||
throw new TypeError("Status text is not valid.");
|
throw new TypeError("Status text is not valid.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -270,7 +274,7 @@
|
||||||
this[_response] = response;
|
this[_response] = response;
|
||||||
/** @type {Headers} */
|
/** @type {Headers} */
|
||||||
this[_headers] = headersFromHeaderList(response.headerList, "response");
|
this[_headers] = headersFromHeaderList(response.headerList, "response");
|
||||||
if (init.headers !== undefined) {
|
if (init.headers) {
|
||||||
fillHeaders(this[_headers], init.headers);
|
fillHeaders(this[_headers], init.headers);
|
||||||
}
|
}
|
||||||
if (body !== null) {
|
if (body !== null) {
|
||||||
|
@ -407,6 +411,27 @@
|
||||||
converter: webidl.converters["HeadersInit"],
|
converter: webidl.converters["HeadersInit"],
|
||||||
}],
|
}],
|
||||||
);
|
);
|
||||||
|
webidl.converters["ResponseInit_fast"] = function (init, opts) {
|
||||||
|
if (init === undefined || init === null) {
|
||||||
|
return { status: 200, statusText: "", headers: undefined };
|
||||||
|
}
|
||||||
|
// Fast path, if not a proxy
|
||||||
|
if (typeof init === "object" && !isProxy(init)) {
|
||||||
|
// Not a proxy fast path
|
||||||
|
const status = init.status !== undefined
|
||||||
|
? webidl.converters["unsigned short"](init.status)
|
||||||
|
: 200;
|
||||||
|
const statusText = init.statusText !== undefined
|
||||||
|
? webidl.converters["ByteString"](init.statusText)
|
||||||
|
: "";
|
||||||
|
const headers = init.headers !== undefined
|
||||||
|
? webidl.converters["HeadersInit"](init.headers)
|
||||||
|
: undefined;
|
||||||
|
return { status, statusText, headers };
|
||||||
|
}
|
||||||
|
// Slow default path
|
||||||
|
return webidl.converters["ResponseInit"](init, opts);
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Response} response
|
* @param {Response} response
|
||||||
|
|
Loading…
Reference in a new issue