2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-04-08 09:05:08 -04:00
|
|
|
|
|
|
|
// @ts-check
|
2021-07-03 15:32:28 -04:00
|
|
|
/// <reference path="../../core/internal.d.ts" />
|
2021-04-08 09:05:08 -04:00
|
|
|
/// <reference path="../../core/lib.deno_core.d.ts" />
|
|
|
|
/// <reference path="../web/internal.d.ts" />
|
|
|
|
/// <reference path="../web/lib.deno_web.d.ts" />
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
((window) => {
|
2021-06-05 17:10:07 -04:00
|
|
|
const core = Deno.core;
|
2022-08-11 09:56:56 -04:00
|
|
|
const ops = core.ops;
|
2021-07-03 15:32:28 -04:00
|
|
|
const {
|
2022-05-13 08:28:05 -04:00
|
|
|
ArrayPrototypeJoin,
|
2021-07-03 15:32:28 -04:00
|
|
|
ArrayPrototypeMap,
|
2022-05-13 08:28:05 -04:00
|
|
|
Error,
|
|
|
|
JSONStringify,
|
2021-07-03 15:32:28 -04:00
|
|
|
NumberPrototypeToString,
|
2022-05-13 08:28:05 -04:00
|
|
|
RegExp,
|
2022-02-07 07:54:32 -05:00
|
|
|
SafeArrayIterator,
|
2022-05-13 08:28:05 -04:00
|
|
|
String,
|
2021-07-03 15:32:28 -04:00
|
|
|
StringPrototypeCharAt,
|
2022-05-13 08:28:05 -04:00
|
|
|
StringPrototypeCharCodeAt,
|
2021-09-30 12:39:55 -04:00
|
|
|
StringPrototypeMatch,
|
2022-05-13 08:28:05 -04:00
|
|
|
StringPrototypePadStart,
|
2021-07-03 15:32:28 -04:00
|
|
|
StringPrototypeReplace,
|
2022-05-13 08:28:05 -04:00
|
|
|
StringPrototypeSlice,
|
2021-07-03 15:32:28 -04:00
|
|
|
StringPrototypeSubstring,
|
2022-05-13 08:28:05 -04:00
|
|
|
StringPrototypeToLowerCase,
|
|
|
|
StringPrototypeToUpperCase,
|
|
|
|
TypeError,
|
2021-07-03 15:32:28 -04:00
|
|
|
} = window.__bootstrap.primordials;
|
2021-06-05 17:10:07 -04:00
|
|
|
|
2021-04-18 19:00:13 -04:00
|
|
|
const ASCII_DIGIT = ["\u0030-\u0039"];
|
|
|
|
const ASCII_UPPER_ALPHA = ["\u0041-\u005A"];
|
|
|
|
const ASCII_LOWER_ALPHA = ["\u0061-\u007A"];
|
2022-02-07 07:54:32 -05:00
|
|
|
const ASCII_ALPHA = [
|
|
|
|
...new SafeArrayIterator(ASCII_UPPER_ALPHA),
|
|
|
|
...new SafeArrayIterator(ASCII_LOWER_ALPHA),
|
|
|
|
];
|
|
|
|
const ASCII_ALPHANUMERIC = [
|
|
|
|
...new SafeArrayIterator(ASCII_DIGIT),
|
|
|
|
...new SafeArrayIterator(ASCII_ALPHA),
|
|
|
|
];
|
2021-04-18 19:00:13 -04:00
|
|
|
|
|
|
|
const HTTP_TAB_OR_SPACE = ["\u0009", "\u0020"];
|
2022-02-07 07:54:32 -05:00
|
|
|
const HTTP_WHITESPACE = [
|
|
|
|
"\u000A",
|
|
|
|
"\u000D",
|
|
|
|
...new SafeArrayIterator(HTTP_TAB_OR_SPACE),
|
|
|
|
];
|
2021-04-18 19:00:13 -04:00
|
|
|
|
|
|
|
const HTTP_TOKEN_CODE_POINT = [
|
|
|
|
"\u0021",
|
|
|
|
"\u0023",
|
|
|
|
"\u0024",
|
|
|
|
"\u0025",
|
|
|
|
"\u0026",
|
|
|
|
"\u0027",
|
|
|
|
"\u002A",
|
|
|
|
"\u002B",
|
|
|
|
"\u002D",
|
|
|
|
"\u002E",
|
|
|
|
"\u005E",
|
|
|
|
"\u005F",
|
|
|
|
"\u0060",
|
|
|
|
"\u007C",
|
|
|
|
"\u007E",
|
2022-02-07 07:54:32 -05:00
|
|
|
...new SafeArrayIterator(ASCII_ALPHANUMERIC),
|
2021-04-18 19:00:13 -04:00
|
|
|
];
|
|
|
|
const HTTP_TOKEN_CODE_POINT_RE = new RegExp(
|
|
|
|
`^[${regexMatcher(HTTP_TOKEN_CODE_POINT)}]+$`,
|
|
|
|
);
|
|
|
|
const HTTP_QUOTED_STRING_TOKEN_POINT = [
|
|
|
|
"\u0009",
|
|
|
|
"\u0020-\u007E",
|
|
|
|
"\u0080-\u00FF",
|
|
|
|
];
|
|
|
|
const HTTP_QUOTED_STRING_TOKEN_POINT_RE = new RegExp(
|
|
|
|
`^[${regexMatcher(HTTP_QUOTED_STRING_TOKEN_POINT)}]+$`,
|
|
|
|
);
|
2021-04-20 08:47:22 -04:00
|
|
|
const HTTP_TAB_OR_SPACE_MATCHER = regexMatcher(HTTP_TAB_OR_SPACE);
|
|
|
|
const HTTP_TAB_OR_SPACE_PREFIX_RE = new RegExp(
|
|
|
|
`^[${HTTP_TAB_OR_SPACE_MATCHER}]+`,
|
|
|
|
"g",
|
|
|
|
);
|
|
|
|
const HTTP_TAB_OR_SPACE_SUFFIX_RE = new RegExp(
|
|
|
|
`[${HTTP_TAB_OR_SPACE_MATCHER}]+$`,
|
|
|
|
"g",
|
|
|
|
);
|
2021-04-18 19:00:13 -04:00
|
|
|
const HTTP_WHITESPACE_MATCHER = regexMatcher(HTTP_WHITESPACE);
|
2021-09-30 12:39:55 -04:00
|
|
|
const HTTP_BETWEEN_WHITESPACE = new RegExp(
|
|
|
|
`^[${HTTP_WHITESPACE_MATCHER}]*(.*?)[${HTTP_WHITESPACE_MATCHER}]*$`,
|
|
|
|
);
|
2021-04-18 19:00:13 -04:00
|
|
|
const HTTP_WHITESPACE_PREFIX_RE = new RegExp(
|
|
|
|
`^[${HTTP_WHITESPACE_MATCHER}]+`,
|
|
|
|
"g",
|
|
|
|
);
|
|
|
|
const HTTP_WHITESPACE_SUFFIX_RE = new RegExp(
|
|
|
|
`[${HTTP_WHITESPACE_MATCHER}]+$`,
|
|
|
|
"g",
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn a string of chars into a regex safe matcher.
|
2021-04-28 10:08:51 -04:00
|
|
|
* @param {string[]} chars
|
2021-04-18 19:00:13 -04:00
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function regexMatcher(chars) {
|
2021-07-03 15:32:28 -04:00
|
|
|
const matchers = ArrayPrototypeMap(chars, (char) => {
|
2021-04-18 19:00:13 -04:00
|
|
|
if (char.length === 1) {
|
2021-07-03 15:32:28 -04:00
|
|
|
const a = StringPrototypePadStart(
|
|
|
|
NumberPrototypeToString(StringPrototypeCharCodeAt(char, 0), 16),
|
|
|
|
4,
|
|
|
|
"0",
|
|
|
|
);
|
|
|
|
return `\\u${a}`;
|
2021-04-18 19:00:13 -04:00
|
|
|
} else if (char.length === 3 && char[1] === "-") {
|
2021-07-03 15:32:28 -04:00
|
|
|
const a = StringPrototypePadStart(
|
|
|
|
NumberPrototypeToString(StringPrototypeCharCodeAt(char, 0), 16),
|
|
|
|
4,
|
|
|
|
"0",
|
|
|
|
);
|
|
|
|
const b = StringPrototypePadStart(
|
|
|
|
NumberPrototypeToString(StringPrototypeCharCodeAt(char, 2), 16),
|
|
|
|
4,
|
|
|
|
"0",
|
|
|
|
);
|
|
|
|
return `\\u${a}-\\u${b}`;
|
2021-04-18 19:00:13 -04:00
|
|
|
} else {
|
|
|
|
throw TypeError("unreachable");
|
|
|
|
}
|
|
|
|
});
|
2021-07-03 15:32:28 -04:00
|
|
|
return ArrayPrototypeJoin(matchers, "");
|
2021-04-18 19:00:13 -04:00
|
|
|
}
|
|
|
|
|
2021-04-08 09:05:08 -04:00
|
|
|
/**
|
|
|
|
* https://infra.spec.whatwg.org/#collect-a-sequence-of-code-points
|
|
|
|
* @param {string} input
|
|
|
|
* @param {number} position
|
|
|
|
* @param {(char: string) => boolean} condition
|
|
|
|
* @returns {{result: string, position: number}}
|
|
|
|
*/
|
|
|
|
function collectSequenceOfCodepoints(input, position, condition) {
|
|
|
|
const start = position;
|
|
|
|
for (
|
2021-07-03 15:32:28 -04:00
|
|
|
let c = StringPrototypeCharAt(input, position);
|
2021-04-08 09:05:08 -04:00
|
|
|
position < input.length && condition(c);
|
2021-07-03 15:32:28 -04:00
|
|
|
c = StringPrototypeCharAt(input, ++position)
|
2021-04-08 09:05:08 -04:00
|
|
|
);
|
2021-07-03 15:32:28 -04:00
|
|
|
return { result: StringPrototypeSlice(input, start, position), position };
|
2021-04-08 09:05:08 -04:00
|
|
|
}
|
|
|
|
|
2021-04-18 19:00:13 -04:00
|
|
|
/**
|
|
|
|
* @param {string} s
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function byteUpperCase(s) {
|
2021-07-03 15:32:28 -04:00
|
|
|
return StringPrototypeReplace(
|
|
|
|
String(s),
|
|
|
|
/[a-z]/g,
|
|
|
|
function byteUpperCaseReplace(c) {
|
|
|
|
return StringPrototypeToUpperCase(c);
|
|
|
|
},
|
|
|
|
);
|
2021-04-18 19:00:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} s
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function byteLowerCase(s) {
|
2021-09-30 19:03:51 -04:00
|
|
|
// NOTE: correct since all callers convert to ByteString first
|
|
|
|
// TODO(@AaronO): maybe prefer a ByteString_Lower webidl converter
|
|
|
|
return StringPrototypeToLowerCase(s);
|
2021-04-18 19:00:13 -04:00
|
|
|
}
|
|
|
|
|
2021-04-20 08:47:22 -04:00
|
|
|
/**
|
|
|
|
* https://fetch.spec.whatwg.org/#collect-an-http-quoted-string
|
|
|
|
* @param {string} input
|
|
|
|
* @param {number} position
|
|
|
|
* @param {boolean} extractValue
|
|
|
|
* @returns {{result: string, position: number}}
|
|
|
|
*/
|
|
|
|
function collectHttpQuotedString(input, position, extractValue) {
|
|
|
|
// 1.
|
|
|
|
const positionStart = position;
|
|
|
|
// 2.
|
|
|
|
let value = "";
|
|
|
|
// 3.
|
2021-07-03 15:32:28 -04:00
|
|
|
if (input[position] !== "\u0022") throw new TypeError('must be "');
|
2021-04-20 08:47:22 -04:00
|
|
|
// 4.
|
|
|
|
position++;
|
|
|
|
// 5.
|
|
|
|
while (true) {
|
|
|
|
// 5.1.
|
|
|
|
const res = collectSequenceOfCodepoints(
|
|
|
|
input,
|
|
|
|
position,
|
|
|
|
(c) => c !== "\u0022" && c !== "\u005C",
|
|
|
|
);
|
|
|
|
value += res.result;
|
|
|
|
position = res.position;
|
|
|
|
// 5.2.
|
|
|
|
if (position >= input.length) break;
|
|
|
|
// 5.3.
|
|
|
|
const quoteOrBackslash = input[position];
|
|
|
|
// 5.4.
|
|
|
|
position++;
|
|
|
|
// 5.5.
|
|
|
|
if (quoteOrBackslash === "\u005C") {
|
|
|
|
// 5.5.1.
|
|
|
|
if (position >= input.length) {
|
|
|
|
value += "\u005C";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// 5.5.2.
|
|
|
|
value += input[position];
|
|
|
|
// 5.5.3.
|
|
|
|
position++;
|
|
|
|
} else { // 5.6.
|
|
|
|
// 5.6.1
|
2021-07-03 15:32:28 -04:00
|
|
|
if (quoteOrBackslash !== "\u0022") throw new TypeError('must be "');
|
2021-04-20 08:47:22 -04:00
|
|
|
// 5.6.2
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 6.
|
|
|
|
if (extractValue) return { result: value, position };
|
|
|
|
// 7.
|
2021-07-03 15:32:28 -04:00
|
|
|
return {
|
|
|
|
result: StringPrototypeSubstring(input, positionStart, position + 1),
|
|
|
|
position,
|
|
|
|
};
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
|
|
|
|
2021-06-05 17:10:07 -04:00
|
|
|
/**
|
|
|
|
* @param {Uint8Array} data
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function forgivingBase64Encode(data) {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_base64_encode(data);
|
2021-06-05 17:10:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} data
|
|
|
|
* @returns {Uint8Array}
|
|
|
|
*/
|
|
|
|
function forgivingBase64Decode(data) {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_base64_decode(data);
|
2021-06-05 17:10:07 -04:00
|
|
|
}
|
|
|
|
|
2021-09-30 12:39:55 -04:00
|
|
|
/**
|
|
|
|
* @param {string} char
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function isHttpWhitespace(char) {
|
|
|
|
switch (char) {
|
|
|
|
case "\u0009":
|
|
|
|
case "\u000A":
|
|
|
|
case "\u000D":
|
|
|
|
case "\u0020":
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} s
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function httpTrim(s) {
|
|
|
|
if (!isHttpWhitespace(s[0]) && !isHttpWhitespace(s[s.length - 1])) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
return StringPrototypeMatch(s, HTTP_BETWEEN_WHITESPACE)?.[1] ?? "";
|
|
|
|
}
|
|
|
|
|
2022-03-19 08:57:37 -04:00
|
|
|
class AssertionError extends Error {
|
|
|
|
constructor(msg) {
|
|
|
|
super(msg);
|
|
|
|
this.name = "AssertionError";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {unknown} cond
|
|
|
|
* @param {string=} msg
|
|
|
|
* @returns {asserts cond}
|
|
|
|
*/
|
|
|
|
function assert(cond, msg = "Assertion failed.") {
|
|
|
|
if (!cond) {
|
|
|
|
throw new AssertionError(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-13 08:28:05 -04:00
|
|
|
/**
|
|
|
|
* @param {unknown} value
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function serializeJSValueToJSONString(value) {
|
|
|
|
const result = JSONStringify(value);
|
|
|
|
if (result === undefined) {
|
|
|
|
throw new TypeError("Value is not JSON serializable.");
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-04-08 09:05:08 -04:00
|
|
|
window.__bootstrap.infra = {
|
|
|
|
collectSequenceOfCodepoints,
|
2021-04-18 19:00:13 -04:00
|
|
|
ASCII_DIGIT,
|
|
|
|
ASCII_UPPER_ALPHA,
|
|
|
|
ASCII_LOWER_ALPHA,
|
|
|
|
ASCII_ALPHA,
|
|
|
|
ASCII_ALPHANUMERIC,
|
|
|
|
HTTP_TAB_OR_SPACE,
|
|
|
|
HTTP_WHITESPACE,
|
|
|
|
HTTP_TOKEN_CODE_POINT,
|
|
|
|
HTTP_TOKEN_CODE_POINT_RE,
|
|
|
|
HTTP_QUOTED_STRING_TOKEN_POINT,
|
|
|
|
HTTP_QUOTED_STRING_TOKEN_POINT_RE,
|
2021-04-20 08:47:22 -04:00
|
|
|
HTTP_TAB_OR_SPACE_PREFIX_RE,
|
|
|
|
HTTP_TAB_OR_SPACE_SUFFIX_RE,
|
2021-04-18 19:00:13 -04:00
|
|
|
HTTP_WHITESPACE_PREFIX_RE,
|
|
|
|
HTTP_WHITESPACE_SUFFIX_RE,
|
2021-09-30 12:39:55 -04:00
|
|
|
httpTrim,
|
2021-04-18 19:00:13 -04:00
|
|
|
regexMatcher,
|
|
|
|
byteUpperCase,
|
|
|
|
byteLowerCase,
|
2021-04-20 08:47:22 -04:00
|
|
|
collectHttpQuotedString,
|
2021-06-05 17:10:07 -04:00
|
|
|
forgivingBase64Encode,
|
|
|
|
forgivingBase64Decode,
|
2022-03-19 08:57:37 -04:00
|
|
|
AssertionError,
|
|
|
|
assert,
|
2022-05-13 08:28:05 -04:00
|
|
|
serializeJSValueToJSONString,
|
2021-04-08 09:05:08 -04:00
|
|
|
};
|
|
|
|
})(globalThis);
|