2020-03-11 10:49:53 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2020-04-22 10:06:51 -04:00
|
|
|
import { DOMExceptionImpl as DOMException } from "./dom_exception.ts";
|
|
|
|
|
2020-03-30 13:45:37 -04:00
|
|
|
export type TypedArray =
|
|
|
|
| Int8Array
|
|
|
|
| Uint8Array
|
|
|
|
| Uint8ClampedArray
|
|
|
|
| Int16Array
|
|
|
|
| Uint16Array
|
|
|
|
| Int32Array
|
|
|
|
| Uint32Array
|
|
|
|
| Float32Array
|
|
|
|
| Float64Array;
|
|
|
|
|
2020-04-11 11:42:02 -04:00
|
|
|
// @internal
|
2020-03-11 10:49:53 -04:00
|
|
|
export function isTypedArray(x: unknown): x is TypedArray {
|
2020-04-22 10:06:51 -04:00
|
|
|
return ArrayBuffer.isView(x) && !(x instanceof DataView);
|
2020-03-11 10:49:53 -04:00
|
|
|
}
|
|
|
|
|
2020-04-27 13:39:39 -04:00
|
|
|
// @internal
|
|
|
|
export function isInvalidDate(x: Date): boolean {
|
|
|
|
return isNaN(x.getTime());
|
|
|
|
}
|
|
|
|
|
2020-03-11 10:49:53 -04:00
|
|
|
// @internal
|
|
|
|
export function requiredArguments(
|
|
|
|
name: string,
|
|
|
|
length: number,
|
2020-07-14 15:24:17 -04:00
|
|
|
required: number,
|
2020-03-11 10:49:53 -04:00
|
|
|
): void {
|
|
|
|
if (length < required) {
|
|
|
|
const errMsg = `${name} requires at least ${required} argument${
|
|
|
|
required === 1 ? "" : "s"
|
|
|
|
}, but only ${length} present`;
|
|
|
|
throw new TypeError(errMsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// @internal
|
|
|
|
export function immutableDefine(
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
o: any,
|
|
|
|
p: string | number | symbol,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2020-07-14 15:24:17 -04:00
|
|
|
value: any,
|
2020-03-11 10:49:53 -04:00
|
|
|
): void {
|
|
|
|
Object.defineProperty(o, p, {
|
|
|
|
value,
|
|
|
|
configurable: false,
|
2020-03-28 13:03:49 -04:00
|
|
|
writable: false,
|
2020-03-11 10:49:53 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-11 11:42:02 -04:00
|
|
|
// @internal
|
|
|
|
export function hasOwnProperty(obj: unknown, v: PropertyKey): boolean {
|
2020-03-11 10:49:53 -04:00
|
|
|
if (obj == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return Object.prototype.hasOwnProperty.call(obj, v);
|
|
|
|
}
|
2020-03-28 13:03:49 -04:00
|
|
|
|
|
|
|
/** Returns whether o is iterable.
|
|
|
|
*
|
|
|
|
* @internal */
|
|
|
|
export function isIterable<T, P extends keyof T, K extends T[P]>(
|
2020-07-14 15:24:17 -04:00
|
|
|
o: T,
|
2020-03-28 13:03:49 -04:00
|
|
|
): o is T & Iterable<[P, K]> {
|
|
|
|
// checks for null and undefined
|
|
|
|
if (o == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
typeof ((o as unknown) as Iterable<[P, K]>)[Symbol.iterator] === "function"
|
|
|
|
);
|
|
|
|
}
|
2020-04-11 11:42:02 -04:00
|
|
|
|
2020-04-22 10:06:51 -04:00
|
|
|
const objectCloneMemo = new WeakMap();
|
|
|
|
|
|
|
|
function cloneArrayBuffer(
|
|
|
|
srcBuffer: ArrayBufferLike,
|
|
|
|
srcByteOffset: number,
|
|
|
|
srcLength: number,
|
2020-07-14 15:24:17 -04:00
|
|
|
cloneConstructor: ArrayBufferConstructor | SharedArrayBufferConstructor,
|
2020-04-22 10:06:51 -04:00
|
|
|
): InstanceType<typeof cloneConstructor> {
|
|
|
|
// this function fudges the return type but SharedArrayBuffer is disabled for a while anyway
|
|
|
|
return srcBuffer.slice(
|
|
|
|
srcByteOffset,
|
2020-07-14 15:24:17 -04:00
|
|
|
srcByteOffset + srcLength,
|
2020-04-22 10:06:51 -04:00
|
|
|
) as InstanceType<typeof cloneConstructor>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Clone a value in a similar way to structured cloning. It is similar to a
|
|
|
|
* StructureDeserialize(StructuredSerialize(...)). */
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
export function cloneValue(value: any): any {
|
|
|
|
switch (typeof value) {
|
|
|
|
case "number":
|
|
|
|
case "string":
|
|
|
|
case "boolean":
|
|
|
|
case "undefined":
|
|
|
|
case "bigint":
|
|
|
|
return value;
|
|
|
|
case "object": {
|
|
|
|
if (objectCloneMemo.has(value)) {
|
|
|
|
return objectCloneMemo.get(value);
|
|
|
|
}
|
|
|
|
if (value === null) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
if (value instanceof Date) {
|
|
|
|
return new Date(value.valueOf());
|
|
|
|
}
|
|
|
|
if (value instanceof RegExp) {
|
|
|
|
return new RegExp(value);
|
|
|
|
}
|
|
|
|
if (value instanceof SharedArrayBuffer) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
if (value instanceof ArrayBuffer) {
|
|
|
|
const cloned = cloneArrayBuffer(
|
|
|
|
value,
|
|
|
|
0,
|
|
|
|
value.byteLength,
|
2020-07-14 15:24:17 -04:00
|
|
|
ArrayBuffer,
|
2020-04-22 10:06:51 -04:00
|
|
|
);
|
|
|
|
objectCloneMemo.set(value, cloned);
|
|
|
|
return cloned;
|
|
|
|
}
|
|
|
|
if (ArrayBuffer.isView(value)) {
|
|
|
|
const clonedBuffer = cloneValue(value.buffer) as ArrayBufferLike;
|
|
|
|
// Use DataViewConstructor type purely for type-checking, can be a
|
|
|
|
// DataView or TypedArray. They use the same constructor signature,
|
|
|
|
// only DataView has a length in bytes and TypedArrays use a length in
|
|
|
|
// terms of elements, so we adjust for that.
|
|
|
|
let length: number;
|
|
|
|
if (value instanceof DataView) {
|
|
|
|
length = value.byteLength;
|
|
|
|
} else {
|
|
|
|
length = (value as Uint8Array).length;
|
|
|
|
}
|
|
|
|
return new (value.constructor as DataViewConstructor)(
|
|
|
|
clonedBuffer,
|
|
|
|
value.byteOffset,
|
2020-07-14 15:24:17 -04:00
|
|
|
length,
|
2020-04-22 10:06:51 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (value instanceof Map) {
|
|
|
|
const clonedMap = new Map();
|
|
|
|
objectCloneMemo.set(value, clonedMap);
|
|
|
|
value.forEach((v, k) => clonedMap.set(k, cloneValue(v)));
|
|
|
|
return clonedMap;
|
|
|
|
}
|
|
|
|
if (value instanceof Set) {
|
|
|
|
const clonedSet = new Map();
|
|
|
|
objectCloneMemo.set(value, clonedSet);
|
|
|
|
value.forEach((v, k) => clonedSet.set(k, cloneValue(v)));
|
|
|
|
return clonedSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
const clonedObj = {} as Record<string, any>;
|
|
|
|
objectCloneMemo.set(value, clonedObj);
|
|
|
|
const sourceKeys = Object.getOwnPropertyNames(value);
|
|
|
|
for (const key of sourceKeys) {
|
|
|
|
clonedObj[key] = cloneValue(value[key]);
|
|
|
|
}
|
|
|
|
return clonedObj;
|
|
|
|
}
|
|
|
|
case "symbol":
|
|
|
|
case "function":
|
|
|
|
default:
|
|
|
|
throw new DOMException("Uncloneable value in stream", "DataCloneError");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 11:42:02 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
interface GenericConstructor<T = any> {
|
|
|
|
prototype: T;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** A helper function which ensures accessors are enumerable, as they normally
|
|
|
|
* are not. */
|
|
|
|
export function defineEnumerableProps(
|
|
|
|
Ctor: GenericConstructor,
|
2020-07-14 15:24:17 -04:00
|
|
|
props: string[],
|
2020-04-11 11:42:02 -04:00
|
|
|
): void {
|
|
|
|
for (const prop of props) {
|
|
|
|
Reflect.defineProperty(Ctor.prototype, prop, { enumerable: true });
|
|
|
|
}
|
|
|
|
}
|
2020-05-25 09:12:09 -04:00
|
|
|
|
|
|
|
// @internal
|
|
|
|
export function getHeaderValueParams(value: string): Map<string, string> {
|
|
|
|
const params = new Map();
|
|
|
|
// Forced to do so for some Map constructor param mismatch
|
|
|
|
value
|
|
|
|
.split(";")
|
|
|
|
.slice(1)
|
|
|
|
.map((s): string[] => s.trim().split("="))
|
|
|
|
.filter((arr): boolean => arr.length > 1)
|
|
|
|
.map(([k, v]): [string, string] => [k, v.replace(/^"([^"]*)"$/, "$1")])
|
|
|
|
.forEach(([k, v]): Map<string, string> => params.set(k, v));
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
// @internal
|
|
|
|
export function hasHeaderValueOf(s: string, value: string): boolean {
|
|
|
|
return new RegExp(`^${value}[\t\s]*;?`).test(s);
|
|
|
|
}
|
2020-07-10 22:38:15 -04:00
|
|
|
|
|
|
|
/** An internal function which provides a function name for some generated
|
|
|
|
* functions, so stack traces are a bit more readable.
|
|
|
|
*
|
|
|
|
* @internal */
|
|
|
|
export function setFunctionName(fn: Function, value: string): void {
|
|
|
|
Object.defineProperty(fn, "name", { value, configurable: true });
|
|
|
|
}
|