diff options
author | Bartek Iwańczuk <biwanczuk@gmail.com> | 2020-03-05 13:05:41 +0100 |
---|---|---|
committer | Bartek Iwańczuk <biwanczuk@gmail.com> | 2020-03-05 18:48:55 +0100 |
commit | 2e590072148c85bbc479ab49aa9556b0a2cfaff2 (patch) | |
tree | 89747e7b6762158e35f9120ca0ef0fa14991ae34 /cli/js | |
parent | 9b59ed7c7907c5f365e72b5c5a74114eb8ff1488 (diff) |
move Web APIs to cli/js/web/
Diffstat (limited to 'cli/js')
-rw-r--r-- | cli/js/buffer.ts | 2 | ||||
-rw-r--r-- | cli/js/compiler.ts | 5 | ||||
-rw-r--r-- | cli/js/compiler_bundler.ts | 4 | ||||
-rw-r--r-- | cli/js/compiler_imports.ts | 7 | ||||
-rw-r--r-- | cli/js/compiler_util.ts | 133 | ||||
-rw-r--r-- | cli/js/console.ts | 2 | ||||
-rw-r--r-- | cli/js/console_table.ts | 2 | ||||
-rw-r--r-- | cli/js/dispatch_json.ts | 2 | ||||
-rw-r--r-- | cli/js/dispatch_minimal.ts | 2 | ||||
-rw-r--r-- | cli/js/globals.ts | 26 | ||||
-rw-r--r-- | cli/js/runtime.ts | 2 | ||||
-rw-r--r-- | cli/js/runtime_main.ts | 2 | ||||
-rw-r--r-- | cli/js/runtime_worker.ts | 2 | ||||
-rw-r--r-- | cli/js/util.ts | 193 | ||||
-rw-r--r-- | cli/js/web/base64.ts (renamed from cli/js/base64.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/blob.ts (renamed from cli/js/blob.ts) | 11 | ||||
-rw-r--r-- | cli/js/web/body.ts (renamed from cli/js/body.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/custom_event.ts (renamed from cli/js/custom_event.ts) | 2 | ||||
-rw-r--r-- | cli/js/web/decode_utf8.ts (renamed from cli/js/decode_utf8.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/dom_file.ts (renamed from cli/js/dom_file.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/dom_iterable.ts (renamed from cli/js/mixins/dom_iterable.ts) | 2 | ||||
-rw-r--r-- | cli/js/web/dom_types.ts (renamed from cli/js/dom_types.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/dom_util.ts (renamed from cli/js/dom_util.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/encode_utf8.ts (renamed from cli/js/encode_utf8.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/event.ts (renamed from cli/js/event.ts) | 2 | ||||
-rw-r--r-- | cli/js/web/event_target.ts (renamed from cli/js/event_target.ts) | 2 | ||||
-rw-r--r-- | cli/js/web/fetch.ts (renamed from cli/js/fetch.ts) | 10 | ||||
-rw-r--r-- | cli/js/web/form_data.ts (renamed from cli/js/form_data.ts) | 4 | ||||
-rw-r--r-- | cli/js/web/headers.ts (renamed from cli/js/headers.ts) | 6 | ||||
-rw-r--r-- | cli/js/web/location.ts (renamed from cli/js/location.ts) | 2 | ||||
-rw-r--r-- | cli/js/web/request.ts (renamed from cli/js/request.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/streams/mod.ts (renamed from cli/js/streams/mod.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/streams/pipe-to.ts (renamed from cli/js/streams/pipe-to.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/streams/queue-mixin.ts (renamed from cli/js/streams/queue-mixin.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/streams/queue.ts (renamed from cli/js/streams/queue.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/streams/readable-byte-stream-controller.ts (renamed from cli/js/streams/readable-byte-stream-controller.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/streams/readable-internals.ts (renamed from cli/js/streams/readable-internals.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/streams/readable-stream-byob-reader.ts (renamed from cli/js/streams/readable-stream-byob-reader.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/streams/readable-stream-byob-request.ts (renamed from cli/js/streams/readable-stream-byob-request.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/streams/readable-stream-default-controller.ts (renamed from cli/js/streams/readable-stream-default-controller.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/streams/readable-stream-default-reader.ts (renamed from cli/js/streams/readable-stream-default-reader.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/streams/readable-stream.ts (renamed from cli/js/streams/readable-stream.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/streams/shared-internals.ts (renamed from cli/js/streams/shared-internals.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/streams/strategies.ts (renamed from cli/js/streams/strategies.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/streams/transform-internals.ts (renamed from cli/js/streams/transform-internals.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/streams/transform-stream-default-controller.ts (renamed from cli/js/streams/transform-stream-default-controller.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/streams/transform-stream.ts (renamed from cli/js/streams/transform-stream.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/streams/writable-internals.ts (renamed from cli/js/streams/writable-internals.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/streams/writable-stream-default-controller.ts (renamed from cli/js/streams/writable-stream-default-controller.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/streams/writable-stream-default-writer.ts (renamed from cli/js/streams/writable-stream-default-writer.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/streams/writable-stream.ts (renamed from cli/js/streams/writable-stream.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/text_encoding.ts (renamed from cli/js/text_encoding.ts) | 0 | ||||
-rw-r--r-- | cli/js/web/url.ts (renamed from cli/js/url.ts) | 4 | ||||
-rw-r--r-- | cli/js/web/url_search_params.ts (renamed from cli/js/url_search_params.ts) | 16 | ||||
-rw-r--r-- | cli/js/workers.ts | 10 |
55 files changed, 207 insertions, 248 deletions
diff --git a/cli/js/buffer.ts b/cli/js/buffer.ts index dab5a4bfc..f0893a533 100644 --- a/cli/js/buffer.ts +++ b/cli/js/buffer.ts @@ -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 diff --git a/cli/js/compiler.ts b/cli/js/compiler.ts index 391f23d93..80ea16fb0 100644 --- a/cli/js/compiler.ts +++ b/cli/js/compiler.ts @@ -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); diff --git a/cli/js/compiler_bundler.ts b/cli/js/compiler_bundler.ts index a7a021470..3a9d3a212 100644 --- a/cli/js/compiler_bundler.ts +++ b/cli/js/compiler_bundler.ts @@ -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; diff --git a/cli/js/compiler_imports.ts b/cli/js/compiler_imports.ts index 903bcfe4d..387c47fc4 100644 --- a/cli/js/compiler_imports.ts +++ b/cli/js/compiler_imports.ts @@ -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) { diff --git a/cli/js/compiler_util.ts b/cli/js/compiler_util.ts index 379099f79..57ba9589b 100644 --- a/cli/js/compiler_util.ts +++ b/cli/js/compiler_util.ts @@ -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; +} diff --git a/cli/js/console.ts b/cli/js/console.ts index f5830b4a1..05e983ec7 100644 --- a/cli/js/console.ts +++ b/cli/js/console.ts @@ -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"; diff --git a/cli/js/console_table.ts b/cli/js/console_table.ts index 7e698f712..882f1243b 100644 --- a/cli/js/console_table.ts +++ b/cli/js/console_table.ts @@ -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(); diff --git a/cli/js/dispatch_json.ts b/cli/js/dispatch_json.ts index 63789044c..f7f389b79 100644 --- a/cli/js/dispatch_json.ts +++ b/cli/js/dispatch_json.ts @@ -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"; diff --git a/cli/js/dispatch_minimal.ts b/cli/js/dispatch_minimal.ts index 567dcc963..a62cc7c85 100644 --- a/cli/js/dispatch_minimal.ts +++ b/cli/js/dispatch_minimal.ts @@ -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>>(); diff --git a/cli/js/globals.ts b/cli/js/globals.ts index 0e3ae8fd8..7033afd92 100644 --- a/cli/js/globals.ts +++ b/cli/js/globals.ts @@ -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. diff --git a/cli/js/runtime.ts b/cli/js/runtime.ts index 8149c065c..ffa943501 100644 --- a/cli/js/runtime.ts +++ b/cli/js/runtime.ts @@ -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 { diff --git a/cli/js/runtime_main.ts b/cli/js/runtime_main.ts index 1298fe90e..e05790e90 100644 --- a/cli/js/runtime_main.ts +++ b/cli/js/runtime_main.ts @@ -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, diff --git a/cli/js/runtime_worker.ts b/cli/js/runtime_worker.ts index cdc7d52dd..b3a5250fa 100644 --- a/cli/js/runtime_worker.ts +++ b/cli/js/runtime_worker.ts @@ -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(); diff --git a/cli/js/util.ts b/cli/js/util.ts index faf4c99c6..9f0373721 100644 --- a/cli/js/util.ts +++ b/cli/js/util.ts @@ -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; -} diff --git a/cli/js/base64.ts b/cli/js/web/base64.ts index 4d30e00f1..4d30e00f1 100644 --- a/cli/js/base64.ts +++ b/cli/js/web/base64.ts diff --git a/cli/js/blob.ts b/cli/js/web/blob.ts index e140477fb..896337b10 100644 --- a/cli/js/blob.ts +++ b/cli/js/web/blob.ts @@ -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"; diff --git a/cli/js/body.ts b/cli/js/web/body.ts index ed21fa0ec..ed21fa0ec 100644 --- a/cli/js/body.ts +++ b/cli/js/web/body.ts diff --git a/cli/js/custom_event.ts b/cli/js/web/custom_event.ts index 2d94df56d..6c8a3c19b 100644 --- a/cli/js/custom_event.ts +++ b/cli/js/web/custom_event.ts @@ -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 diff --git a/cli/js/decode_utf8.ts b/cli/js/web/decode_utf8.ts index 32d67b0e4..32d67b0e4 100644 --- a/cli/js/decode_utf8.ts +++ b/cli/js/web/decode_utf8.ts diff --git a/cli/js/dom_file.ts b/cli/js/web/dom_file.ts index 2b9dbff24..2b9dbff24 100644 --- a/cli/js/dom_file.ts +++ b/cli/js/web/dom_file.ts diff --git a/cli/js/mixins/dom_iterable.ts b/cli/js/web/dom_iterable.ts index 976d81be7..bd8e7d8cd 100644 --- a/cli/js/mixins/dom_iterable.ts +++ b/cli/js/web/dom_iterable.ts @@ -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"; diff --git a/cli/js/dom_types.ts b/cli/js/web/dom_types.ts index cdd681615..cdd681615 100644 --- a/cli/js/dom_types.ts +++ b/cli/js/web/dom_types.ts diff --git a/cli/js/dom_util.ts b/cli/js/web/dom_util.ts index 5780d9c52..5780d9c52 100644 --- a/cli/js/dom_util.ts +++ b/cli/js/web/dom_util.ts diff --git a/cli/js/encode_utf8.ts b/cli/js/web/encode_utf8.ts index 04e2560b7..04e2560b7 100644 --- a/cli/js/encode_utf8.ts +++ b/cli/js/web/encode_utf8.ts diff --git a/cli/js/event.ts b/cli/js/web/event.ts index a30e33447..e365fb6b2 100644 --- a/cli/js/event.ts +++ b/cli/js/web/event.ts @@ -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 diff --git a/cli/js/event_target.ts b/cli/js/web/event_target.ts index 03557526a..09e80a731 100644 --- a/cli/js/event_target.ts +++ b/cli/js/web/event_target.ts @@ -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, diff --git a/cli/js/fetch.ts b/cli/js/web/fetch.ts index 2f493c02e..17cd43129 100644 --- a/cli/js/fetch.ts +++ b/cli/js/web/fetch.ts @@ -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(); diff --git a/cli/js/form_data.ts b/cli/js/web/form_data.ts index e2dee9050..9c0590c32 100644 --- a/cli/js/form_data.ts +++ b/cli/js/web/form_data.ts @@ -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"); diff --git a/cli/js/headers.ts b/cli/js/web/headers.ts index 36fdd824a..65d52cacd 100644 --- a/cli/js/headers.ts +++ b/cli/js/web/headers.ts @@ -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. diff --git a/cli/js/location.ts b/cli/js/web/location.ts index 303374055..d48cce3c7 100644 --- a/cli/js/location.ts +++ b/cli/js/web/location.ts @@ -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 { diff --git a/cli/js/request.ts b/cli/js/web/request.ts index 1416a95d6..1416a95d6 100644 --- a/cli/js/request.ts +++ b/cli/js/web/request.ts diff --git a/cli/js/streams/mod.ts b/cli/js/web/streams/mod.ts index 5389aaf6d..5389aaf6d 100644 --- a/cli/js/streams/mod.ts +++ b/cli/js/web/streams/mod.ts diff --git a/cli/js/streams/pipe-to.ts b/cli/js/web/streams/pipe-to.ts index 1d5579217..1d5579217 100644 --- a/cli/js/streams/pipe-to.ts +++ b/cli/js/web/streams/pipe-to.ts diff --git a/cli/js/streams/queue-mixin.ts b/cli/js/web/streams/queue-mixin.ts index 23c57d75f..23c57d75f 100644 --- a/cli/js/streams/queue-mixin.ts +++ b/cli/js/web/streams/queue-mixin.ts diff --git a/cli/js/streams/queue.ts b/cli/js/web/streams/queue.ts index 264851baf..264851baf 100644 --- a/cli/js/streams/queue.ts +++ b/cli/js/web/streams/queue.ts diff --git a/cli/js/streams/readable-byte-stream-controller.ts b/cli/js/web/streams/readable-byte-stream-controller.ts index 86efd416c..86efd416c 100644 --- a/cli/js/streams/readable-byte-stream-controller.ts +++ b/cli/js/web/streams/readable-byte-stream-controller.ts diff --git a/cli/js/streams/readable-internals.ts b/cli/js/web/streams/readable-internals.ts index 67f5a69b1..67f5a69b1 100644 --- a/cli/js/streams/readable-internals.ts +++ b/cli/js/web/streams/readable-internals.ts diff --git a/cli/js/streams/readable-stream-byob-reader.ts b/cli/js/web/streams/readable-stream-byob-reader.ts index 0f9bfb037..0f9bfb037 100644 --- a/cli/js/streams/readable-stream-byob-reader.ts +++ b/cli/js/web/streams/readable-stream-byob-reader.ts diff --git a/cli/js/streams/readable-stream-byob-request.ts b/cli/js/web/streams/readable-stream-byob-request.ts index 25b937f10..25b937f10 100644 --- a/cli/js/streams/readable-stream-byob-request.ts +++ b/cli/js/web/streams/readable-stream-byob-request.ts diff --git a/cli/js/streams/readable-stream-default-controller.ts b/cli/js/web/streams/readable-stream-default-controller.ts index e9ddce1bc..e9ddce1bc 100644 --- a/cli/js/streams/readable-stream-default-controller.ts +++ b/cli/js/web/streams/readable-stream-default-controller.ts diff --git a/cli/js/streams/readable-stream-default-reader.ts b/cli/js/web/streams/readable-stream-default-reader.ts index eb1910a9d..eb1910a9d 100644 --- a/cli/js/streams/readable-stream-default-reader.ts +++ b/cli/js/web/streams/readable-stream-default-reader.ts diff --git a/cli/js/streams/readable-stream.ts b/cli/js/web/streams/readable-stream.ts index 4d9d85889..4d9d85889 100644 --- a/cli/js/streams/readable-stream.ts +++ b/cli/js/web/streams/readable-stream.ts diff --git a/cli/js/streams/shared-internals.ts b/cli/js/web/streams/shared-internals.ts index 93155fecc..93155fecc 100644 --- a/cli/js/streams/shared-internals.ts +++ b/cli/js/web/streams/shared-internals.ts diff --git a/cli/js/streams/strategies.ts b/cli/js/web/streams/strategies.ts index 5f7ffc632..5f7ffc632 100644 --- a/cli/js/streams/strategies.ts +++ b/cli/js/web/streams/strategies.ts diff --git a/cli/js/streams/transform-internals.ts b/cli/js/web/streams/transform-internals.ts index 4c5e3657d..4c5e3657d 100644 --- a/cli/js/streams/transform-internals.ts +++ b/cli/js/web/streams/transform-internals.ts diff --git a/cli/js/streams/transform-stream-default-controller.ts b/cli/js/web/streams/transform-stream-default-controller.ts index 24a8d08fd..24a8d08fd 100644 --- a/cli/js/streams/transform-stream-default-controller.ts +++ b/cli/js/web/streams/transform-stream-default-controller.ts diff --git a/cli/js/streams/transform-stream.ts b/cli/js/web/streams/transform-stream.ts index 090f78135..090f78135 100644 --- a/cli/js/streams/transform-stream.ts +++ b/cli/js/web/streams/transform-stream.ts diff --git a/cli/js/streams/writable-internals.ts b/cli/js/web/streams/writable-internals.ts index 78bb19a28..78bb19a28 100644 --- a/cli/js/streams/writable-internals.ts +++ b/cli/js/web/streams/writable-internals.ts diff --git a/cli/js/streams/writable-stream-default-controller.ts b/cli/js/web/streams/writable-stream-default-controller.ts index 57ffe08fd..57ffe08fd 100644 --- a/cli/js/streams/writable-stream-default-controller.ts +++ b/cli/js/web/streams/writable-stream-default-controller.ts diff --git a/cli/js/streams/writable-stream-default-writer.ts b/cli/js/web/streams/writable-stream-default-writer.ts index f38aa26bb..f38aa26bb 100644 --- a/cli/js/streams/writable-stream-default-writer.ts +++ b/cli/js/web/streams/writable-stream-default-writer.ts diff --git a/cli/js/streams/writable-stream.ts b/cli/js/web/streams/writable-stream.ts index a6131c5d0..a6131c5d0 100644 --- a/cli/js/streams/writable-stream.ts +++ b/cli/js/web/streams/writable-stream.ts diff --git a/cli/js/text_encoding.ts b/cli/js/web/text_encoding.ts index 0709e7123..0709e7123 100644 --- a/cli/js/text_encoding.ts +++ b/cli/js/web/text_encoding.ts diff --git a/cli/js/url.ts b/cli/js/web/url.ts index 477ca7c2a..4cf9ae257 100644 --- a/cli/js/url.ts +++ b/cli/js/web/url.ts @@ -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; diff --git a/cli/js/url_search_params.ts b/cli/js/web/url_search_params.ts index 5b7f0ecd8..2248a5388 100644 --- a/cli/js/url_search_params.ts +++ b/cli/js/web/url_search_params.ts @@ -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]> = []; diff --git a/cli/js/workers.ts b/cli/js/workers.ts index 3258e4137..95012e1b8 100644 --- a/cli/js/workers.ts +++ b/cli/js/workers.ts @@ -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(); |