0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -05:00

move Web APIs to cli/js/web/

This commit is contained in:
Bartek Iwańczuk 2020-03-05 13:05:41 +01:00
parent 9b59ed7c79
commit 2e59007214
55 changed files with 207 additions and 248 deletions

View file

@ -6,7 +6,7 @@
import { Reader, Writer, EOF, SyncReader, SyncWriter } from "./io.ts";
import { assert } from "./util.ts";
import { TextDecoder } from "./text_encoding.ts";
import { TextDecoder } from "./web/text_encoding.ts";
// MIN_READ is the minimum ArrayBuffer size passed to a read call by
// buffer.ReadFrom. As long as the Buffer has at least MIN_READ bytes beyond

View file

@ -34,7 +34,8 @@ import {
convertCompilerOptions,
ignoredDiagnostics,
WriteFileState,
processConfigureResponse
processConfigureResponse,
base64ToUint8Array
} from "./compiler_util.ts";
import { Diagnostic, DiagnosticItem } from "./diagnostics.ts";
import { fromTypeScriptDiagnostic } from "./diagnostics_util.ts";
@ -379,7 +380,7 @@ async function wasmCompilerOnMessage({
}: {
data: string;
}): Promise<void> {
const buffer = util.base64ToUint8Array(binary);
const buffer = base64ToUint8Array(binary);
// @ts-ignore
const compiled = await WebAssembly.compile(buffer);

View file

@ -2,11 +2,11 @@
import { SYSTEM_LOADER } from "./compiler_bootstrap.ts";
import {
assert,
commonPath,
normalizeString,
CHAR_FORWARD_SLASH
} from "./util.ts";
} from "./compiler_util.ts";
import { assert } from "./util.ts";
/** Local state of what the root exports are of a root module. */
let rootExports: string[] | undefined;

View file

@ -5,6 +5,7 @@ import {
SourceFile,
SourceFileJson
} from "./compiler_sourcefile.ts";
import { normalizeString, CHAR_FORWARD_SLASH } from "./compiler_util.ts";
import { cwd } from "./dir.ts";
import { sendAsync, sendSync } from "./dispatch_json.ts";
import { assert } from "./util.ts";
@ -27,18 +28,18 @@ function resolvePath(...pathSegments: string[]): string {
}
resolvedPath = `${path}/${resolvedPath}`;
resolvedAbsolute = path.charCodeAt(0) === util.CHAR_FORWARD_SLASH;
resolvedAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
}
// At this point the path should be resolved to a full absolute path, but
// handle relative paths to be safe (might happen when cwd() fails)
// Normalize the path
resolvedPath = util.normalizeString(
resolvedPath = normalizeString(
resolvedPath,
!resolvedAbsolute,
"/",
code => code === util.CHAR_FORWARD_SLASH
code => code === CHAR_FORWARD_SLASH
);
if (resolvedAbsolute) {

View file

@ -6,7 +6,7 @@ import { buildBundle } from "./compiler_bundler.ts";
import { ConfigureResponse, Host } from "./compiler_host.ts";
import { SourceFile } from "./compiler_sourcefile.ts";
import { sendSync } from "./dispatch_json.ts";
import { TextDecoder, TextEncoder } from "./text_encoding.ts";
import { atob, TextDecoder, TextEncoder } from "./web/text_encoding.ts";
import { core } from "./core.ts";
import * as util from "./util.ts";
import { assert } from "./util.ts";
@ -145,7 +145,7 @@ export function createWriteFile(state: WriteFileState): WriteFileCallback {
const encodedData = encoder.encode(content);
console.warn(`Emitting bundle to "${state.outFile}"`);
writeFileSync(state.outFile, encodedData);
console.warn(`${util.humanFileSize(encodedData.length)} emitted.`);
console.warn(`${humanFileSize(encodedData.length)} emitted.`);
} else {
console.log(content);
}
@ -333,3 +333,132 @@ export function processConfigureResponse(
}
return diagnostics;
}
// Constants used by `normalizeString` and `resolvePath`
export const CHAR_DOT = 46; /* . */
export const CHAR_FORWARD_SLASH = 47; /* / */
/** Resolves `.` and `..` elements in a path with directory names */
export function normalizeString(
path: string,
allowAboveRoot: boolean,
separator: string,
isPathSeparator: (code: number) => boolean
): string {
let res = "";
let lastSegmentLength = 0;
let lastSlash = -1;
let dots = 0;
let code: number;
for (let i = 0, len = path.length; i <= len; ++i) {
if (i < len) code = path.charCodeAt(i);
else if (isPathSeparator(code!)) break;
else code = CHAR_FORWARD_SLASH;
if (isPathSeparator(code)) {
if (lastSlash === i - 1 || dots === 1) {
// NOOP
} else if (lastSlash !== i - 1 && dots === 2) {
if (
res.length < 2 ||
lastSegmentLength !== 2 ||
res.charCodeAt(res.length - 1) !== CHAR_DOT ||
res.charCodeAt(res.length - 2) !== CHAR_DOT
) {
if (res.length > 2) {
const lastSlashIndex = res.lastIndexOf(separator);
if (lastSlashIndex === -1) {
res = "";
lastSegmentLength = 0;
} else {
res = res.slice(0, lastSlashIndex);
lastSegmentLength = res.length - 1 - res.lastIndexOf(separator);
}
lastSlash = i;
dots = 0;
continue;
} else if (res.length === 2 || res.length === 1) {
res = "";
lastSegmentLength = 0;
lastSlash = i;
dots = 0;
continue;
}
}
if (allowAboveRoot) {
if (res.length > 0) res += `${separator}..`;
else res = "..";
lastSegmentLength = 2;
}
} else {
if (res.length > 0) res += separator + path.slice(lastSlash + 1, i);
else res = path.slice(lastSlash + 1, i);
lastSegmentLength = i - lastSlash - 1;
}
lastSlash = i;
dots = 0;
} else if (code === CHAR_DOT && dots !== -1) {
++dots;
} else {
dots = -1;
}
}
return res;
}
/** Return the common path shared by the `paths`.
*
* @param paths The set of paths to compare.
* @param sep An optional separator to use. Defaults to `/`.
* @internal
*/
export function commonPath(paths: string[], sep = "/"): string {
const [first = "", ...remaining] = paths;
if (first === "" || remaining.length === 0) {
return first.substring(0, first.lastIndexOf(sep) + 1);
}
const parts = first.split(sep);
let endOfPrefix = parts.length;
for (const path of remaining) {
const compare = path.split(sep);
for (let i = 0; i < endOfPrefix; i++) {
if (compare[i] !== parts[i]) {
endOfPrefix = i;
}
}
if (endOfPrefix === 0) {
return "";
}
}
const prefix = parts.slice(0, endOfPrefix).join(sep);
return prefix.endsWith(sep) ? prefix : `${prefix}${sep}`;
}
/** Utility function to turn the number of bytes into a human readable
* unit */
function humanFileSize(bytes: number): string {
const thresh = 1000;
if (Math.abs(bytes) < thresh) {
return bytes + " B";
}
const units = ["kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
let u = -1;
do {
bytes /= thresh;
++u;
} while (Math.abs(bytes) >= thresh && u < units.length - 1);
return `${bytes.toFixed(1)} ${units[u]}`;
}
// @internal
export function base64ToUint8Array(data: string): Uint8Array {
const binString = atob(data);
const size = binString.length;
const bytes = new Uint8Array(size);
for (let i = 0; i < size; i++) {
bytes[i] = binString.charCodeAt(i);
}
return bytes;
}

View file

@ -1,7 +1,7 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { isTypedArray } from "./util.ts";
import { TypedArray } from "./types.ts";
import { TextEncoder } from "./text_encoding.ts";
import { TextEncoder } from "./web/text_encoding.ts";
import { File, stdout } from "./files.ts";
import { cliTable } from "./console_table.ts";
import { exposeForTest } from "./internals.ts";

View file

@ -1,7 +1,7 @@
// Copyright Joyent, Inc. and other Node contributors. MIT license.
// Forked from Node's lib/internal/cli_table.js
import { TextEncoder } from "./text_encoding.ts";
import { TextEncoder } from "./web/text_encoding.ts";
import { hasOwnProperty } from "./util.ts";
const encoder = new TextEncoder();

View file

@ -1,6 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import * as util from "./util.ts";
import { TextEncoder, TextDecoder } from "./text_encoding.ts";
import { TextEncoder, TextDecoder } from "./web/text_encoding.ts";
import { core } from "./core.ts";
import { OPS_CACHE } from "./runtime.ts";
import { ErrorKind, getErrorClass } from "./errors.ts";

View file

@ -1,7 +1,7 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import * as util from "./util.ts";
import { core } from "./core.ts";
import { TextDecoder } from "./text_encoding.ts";
import { TextDecoder } from "./web/text_encoding.ts";
import { ErrorKind, errors, getErrorClass } from "./errors.ts";
const promiseTableMin = new Map<number, util.Resolvable<RecordMinimal>>();

View file

@ -1,22 +1,22 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import * as blob from "./blob.ts";
import * as blob from "./web/blob.ts";
import * as consoleTypes from "./console.ts";
import * as customEvent from "./custom_event.ts";
import * as domTypes from "./dom_types.ts";
import * as domFile from "./dom_file.ts";
import * as event from "./event.ts";
import * as eventTarget from "./event_target.ts";
import * as formData from "./form_data.ts";
import * as fetchTypes from "./fetch.ts";
import * as headers from "./headers.ts";
import * as textEncoding from "./text_encoding.ts";
import * as customEvent from "./web/custom_event.ts";
import * as domTypes from "./web/dom_types.ts";
import * as domFile from "./web/dom_file.ts";
import * as event from "./web/event.ts";
import * as eventTarget from "./web/event_target.ts";
import * as formData from "./web/form_data.ts";
import * as fetchTypes from "./web/fetch.ts";
import * as headers from "./web/headers.ts";
import * as textEncoding from "./web/text_encoding.ts";
import * as timers from "./timers.ts";
import * as url from "./url.ts";
import * as urlSearchParams from "./url_search_params.ts";
import * as url from "./web/url.ts";
import * as urlSearchParams from "./web/url_search_params.ts";
import * as workers from "./workers.ts";
import * as performanceUtil from "./performance.ts";
import * as request from "./request.ts";
import * as request from "./web/request.ts";
// These imports are not exposed and therefore are fine to just import the
// symbols required.

View file

@ -7,7 +7,7 @@ import * as util from "./util.ts";
import { OperatingSystem, Arch } from "./build.ts";
import { setBuildInfo } from "./build.ts";
import { setVersions } from "./version.ts";
import { setLocation } from "./location.ts";
import { setLocation } from "./web/location.ts";
import { setPrepareStackTrace } from "./error_stack.ts";
interface Start {

View file

@ -8,7 +8,7 @@
// It sets up runtime by providing globals for `WindowScope` and adds `Deno` global.
import * as Deno from "./deno.ts";
import * as domTypes from "./dom_types.ts";
import * as domTypes from "./web/dom_types.ts";
import * as csprng from "./get_random_values.ts";
import {
readOnly,

View file

@ -18,7 +18,7 @@ import {
} from "./globals.ts";
import { sendSync } from "./dispatch_json.ts";
import { log } from "./util.ts";
import { TextEncoder } from "./text_encoding.ts";
import { TextEncoder } from "./web/text_encoding.ts";
import * as runtime from "./runtime.ts";
const encoder = new TextEncoder();

View file

@ -1,5 +1,4 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { atob } from "./text_encoding.ts";
import { TypedArray } from "./types.ts";
let logDebug = false;
@ -32,17 +31,6 @@ export function assert(cond: unknown, msg = "assert"): asserts cond {
}
}
// @internal
export function typedArrayToArrayBuffer(ta: TypedArray): ArrayBuffer {
const ab = ta.buffer.slice(ta.byteOffset, ta.byteOffset + ta.byteLength);
return ab as ArrayBuffer;
}
// @internal
export function arrayToStr(ui8: Uint8Array): string {
return String.fromCharCode(...ui8);
}
/** A `Resolvable` is a Promise with the `reject` and `resolve` functions
* placed as methods on the promise object itself. It allows you to do:
*
@ -97,48 +85,11 @@ export function unreachable(): never {
throw new Error("Code not reachable");
}
// @internal
export function hexdump(u8: Uint8Array): string {
return Array.prototype.map
.call(u8, (x: number): string => {
return ("00" + x.toString(16)).slice(-2);
})
.join(" ");
}
// @internal
export function containsOnlyASCII(str: string): boolean {
if (typeof str !== "string") {
return false;
}
return /^[\x00-\x7F]*$/.test(str);
}
const TypedArrayConstructor = Object.getPrototypeOf(Uint8Array);
export function isTypedArray(x: unknown): x is TypedArray {
return x instanceof TypedArrayConstructor;
}
// Returns whether o is an object, not null, and not a function.
// @internal
export function isObject(o: unknown): o is object {
return o != null && typeof o === "object";
}
// Returns whether o is iterable.
// @internal
export function isIterable<T, P extends keyof T, K extends T[P]>(
o: T
): 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"
);
}
// @internal
export function requiredArguments(
name: string,
@ -209,147 +160,3 @@ export function hasOwnProperty<T>(obj: T, v: PropertyKey): boolean {
}
return Object.prototype.hasOwnProperty.call(obj, v);
}
/**
* Split a number into two parts: lower 32 bit and higher 32 bit
* (as if the number is represented as uint64.)
*
* @param n Number to split.
* @internal
*/
export function splitNumberToParts(n: number): number[] {
// JS bitwise operators (OR, SHIFT) operate as if number is uint32.
const lower = n | 0;
// This is also faster than Math.floor(n / 0x100000000) in V8.
const higher = (n - lower) / 0x100000000;
return [lower, higher];
}
// Constants used by `normalizeString` and `resolvePath`
export const CHAR_DOT = 46; /* . */
export const CHAR_FORWARD_SLASH = 47; /* / */
/** Resolves `.` and `..` elements in a path with directory names */
export function normalizeString(
path: string,
allowAboveRoot: boolean,
separator: string,
isPathSeparator: (code: number) => boolean
): string {
let res = "";
let lastSegmentLength = 0;
let lastSlash = -1;
let dots = 0;
let code: number;
for (let i = 0, len = path.length; i <= len; ++i) {
if (i < len) code = path.charCodeAt(i);
else if (isPathSeparator(code!)) break;
else code = CHAR_FORWARD_SLASH;
if (isPathSeparator(code)) {
if (lastSlash === i - 1 || dots === 1) {
// NOOP
} else if (lastSlash !== i - 1 && dots === 2) {
if (
res.length < 2 ||
lastSegmentLength !== 2 ||
res.charCodeAt(res.length - 1) !== CHAR_DOT ||
res.charCodeAt(res.length - 2) !== CHAR_DOT
) {
if (res.length > 2) {
const lastSlashIndex = res.lastIndexOf(separator);
if (lastSlashIndex === -1) {
res = "";
lastSegmentLength = 0;
} else {
res = res.slice(0, lastSlashIndex);
lastSegmentLength = res.length - 1 - res.lastIndexOf(separator);
}
lastSlash = i;
dots = 0;
continue;
} else if (res.length === 2 || res.length === 1) {
res = "";
lastSegmentLength = 0;
lastSlash = i;
dots = 0;
continue;
}
}
if (allowAboveRoot) {
if (res.length > 0) res += `${separator}..`;
else res = "..";
lastSegmentLength = 2;
}
} else {
if (res.length > 0) res += separator + path.slice(lastSlash + 1, i);
else res = path.slice(lastSlash + 1, i);
lastSegmentLength = i - lastSlash - 1;
}
lastSlash = i;
dots = 0;
} else if (code === CHAR_DOT && dots !== -1) {
++dots;
} else {
dots = -1;
}
}
return res;
}
/** Return the common path shared by the `paths`.
*
* @param paths The set of paths to compare.
* @param sep An optional separator to use. Defaults to `/`.
* @internal
*/
export function commonPath(paths: string[], sep = "/"): string {
const [first = "", ...remaining] = paths;
if (first === "" || remaining.length === 0) {
return first.substring(0, first.lastIndexOf(sep) + 1);
}
const parts = first.split(sep);
let endOfPrefix = parts.length;
for (const path of remaining) {
const compare = path.split(sep);
for (let i = 0; i < endOfPrefix; i++) {
if (compare[i] !== parts[i]) {
endOfPrefix = i;
}
}
if (endOfPrefix === 0) {
return "";
}
}
const prefix = parts.slice(0, endOfPrefix).join(sep);
return prefix.endsWith(sep) ? prefix : `${prefix}${sep}`;
}
/** Utility function to turn the number of bytes into a human readable
* unit */
export function humanFileSize(bytes: number): string {
const thresh = 1000;
if (Math.abs(bytes) < thresh) {
return bytes + " B";
}
const units = ["kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
let u = -1;
do {
bytes /= thresh;
++u;
} while (Math.abs(bytes) >= thresh && u < units.length - 1);
return `${bytes.toFixed(1)} ${units[u]}`;
}
// @internal
export function base64ToUint8Array(data: string): Uint8Array {
const binString = atob(data);
const size = binString.length;
const bytes = new Uint8Array(size);
for (let i = 0; i < size; i++) {
bytes[i] = binString.charCodeAt(i);
}
return bytes;
}

View file

@ -1,11 +1,18 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import * as domTypes from "./dom_types.ts";
import { containsOnlyASCII, hasOwnProperty } from "./util.ts";
import { hasOwnProperty } from "../util.ts";
import { TextEncoder } from "./text_encoding.ts";
import { build } from "./build.ts";
import { build } from "../build.ts";
export const bytesSymbol = Symbol("bytes");
export function containsOnlyASCII(str: string): boolean {
if (typeof str !== "string") {
return false;
}
return /^[\x00-\x7F]*$/.test(str);
}
function convertLineEndingsToNative(s: string): string {
const nativeLineEnd = build.os == "win" ? "\r\n" : "\n";

View file

@ -1,7 +1,7 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import * as domTypes from "./dom_types.ts";
import * as event from "./event.ts";
import { getPrivateValue, requiredArguments } from "./util.ts";
import { getPrivateValue, requiredArguments } from "../util.ts";
// WeakMaps are recommended for private attributes (see MDN link below)
// https://developer.mozilla.org/en-US/docs/Archive/Add-ons/Add-on_SDK/Guides/Contributor_s_Guide/Private_Properties#Using_WeakMaps

View file

@ -1,6 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { DomIterable } from "../dom_types.ts";
import { DomIterable } from "./dom_types.ts";
import { requiredArguments } from "../util.ts";
import { exposeForTest } from "../internals.ts";

View file

@ -1,6 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import * as domTypes from "./dom_types.ts";
import { getPrivateValue, requiredArguments } from "./util.ts";
import { getPrivateValue, requiredArguments } from "../util.ts";
// WeakMaps are recommended for private attributes (see MDN link below)
// https://developer.mozilla.org/en-US/docs/Archive/Add-ons/Add-on_SDK/Guides/Contributor_s_Guide/Private_Properties#Using_WeakMaps

View file

@ -1,6 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import * as domTypes from "./dom_types.ts";
import { hasOwnProperty, requiredArguments } from "./util.ts";
import { hasOwnProperty, requiredArguments } from "../util.ts";
import {
getRoot,
isNode,

View file

@ -4,18 +4,18 @@ import {
createResolvable,
notImplemented,
isTypedArray
} from "./util.ts";
} from "../util.ts";
import * as domTypes from "./dom_types.ts";
import { TextDecoder, TextEncoder } from "./text_encoding.ts";
import { DenoBlob, bytesSymbol as blobBytesSymbol } from "./blob.ts";
import { Headers } from "./headers.ts";
import * as io from "./io.ts";
import { read, close } from "./files.ts";
import { Buffer } from "./buffer.ts";
import * as io from "../io.ts";
import { read, close } from "../files.ts";
import { Buffer } from "../buffer.ts";
import { FormData } from "./form_data.ts";
import { URL } from "./url.ts";
import { URLSearchParams } from "./url_search_params.ts";
import { sendAsync } from "./dispatch_json.ts";
import { sendAsync } from "../dispatch_json.ts";
function getHeaderValueParams(value: string): Map<string, string> {
const params = new Map();

View file

@ -2,8 +2,8 @@
import * as domTypes from "./dom_types.ts";
import * as blob from "./blob.ts";
import * as domFile from "./dom_file.ts";
import { DomIterableMixin } from "./mixins/dom_iterable.ts";
import { requiredArguments } from "./util.ts";
import { DomIterableMixin } from "./dom_iterable.ts";
import { requiredArguments } from "../util.ts";
const dataSymbol = Symbol("data");

View file

@ -1,8 +1,8 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import * as domTypes from "./dom_types.ts";
import { DomIterableMixin } from "./mixins/dom_iterable.ts";
import { requiredArguments } from "./util.ts";
import { customInspect } from "./console.ts";
import { DomIterableMixin } from "./dom_iterable.ts";
import { requiredArguments } from "../util.ts";
import { customInspect } from "../console.ts";
// From node-fetch
// Copyright (c) 2016 David Frank. MIT License.

View file

@ -1,6 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { URL } from "./url.ts";
import { notImplemented } from "./util.ts";
import { notImplemented } from "../util.ts";
import { Location } from "./dom_types.ts";
export class LocationImpl implements Location {

View file

@ -1,8 +1,8 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import * as urlSearchParams from "./url_search_params.ts";
import * as domTypes from "./dom_types.ts";
import { getRandomValues } from "./get_random_values.ts";
import { customInspect } from "./console.ts";
import { getRandomValues } from "../get_random_values.ts";
import { customInspect } from "../console.ts";
interface URLParts {
protocol: string;

View file

@ -1,6 +1,20 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { URL } from "./url.ts";
import { requiredArguments, isIterable } from "./util.ts";
import { requiredArguments } from "../util.ts";
// Returns whether o is iterable.
// @internal
export function isIterable<T, P extends keyof T, K extends T[P]>(
o: T
): 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"
);
}
export class URLSearchParams {
private params: Array<[string, string]> = [];

View file

@ -2,13 +2,13 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { sendAsync, sendSync } from "./dispatch_json.ts";
import { log } from "./util.ts";
import { TextDecoder, TextEncoder } from "./text_encoding.ts";
import { TextDecoder, TextEncoder } from "./web/text_encoding.ts";
/*
import { blobURLMap } from "./url.ts";
import { blobBytesWeakMap } from "./blob.ts";
import { blobURLMap } from "./web/url.ts";
import { blobBytesWeakMap } from "./web/blob.ts";
*/
import { Event } from "./event.ts";
import { EventTarget } from "./event_target.ts";
import { Event } from "./web/event.ts";
import { EventTarget } from "./web/event_target.ts";
const encoder = new TextEncoder();
const decoder = new TextDecoder();