-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: vendor eszip Deno module (#141)
* feat: vendor eszip Deno module * chore: reformat file * chore: add comment * chore: formatting * chore: formatting * chore: ran prettier * fix: test failed after bad merge Co-authored-by: Ewan Valentine <ewan.valentine89@gmail.com>
- Loading branch information
1 parent
b9673f6
commit cdec76c
Showing
47 changed files
with
3,782 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. | ||
|
||
export class DenoStdInternalError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
this.name = "DenoStdInternalError"; | ||
} | ||
} | ||
|
||
/** Make an assertion, if not `true`, then throw. */ | ||
export function assert(expr: unknown, msg = ""): asserts expr { | ||
if (!expr) { | ||
throw new DenoStdInternalError(msg); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. | ||
// This module is browser compatible. | ||
|
||
export type OSType = "windows" | "linux" | "darwin"; | ||
|
||
export const osType: OSType = (() => { | ||
// deno-lint-ignore no-explicit-any | ||
const { Deno } = globalThis as any; | ||
if (typeof Deno?.build?.os === "string") { | ||
return Deno.build.os; | ||
} | ||
|
||
// deno-lint-ignore no-explicit-any | ||
const { navigator } = globalThis as any; | ||
if (navigator?.appVersion?.includes?.("Win") ?? false) { | ||
return "windows"; | ||
} | ||
|
||
return "linux"; | ||
})(); | ||
|
||
export const isWindows = osType === "windows"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. | ||
// Copyright the Browserify authors. MIT License. | ||
// Ported from https://github.com/browserify/path-browserify/ | ||
// This module is browser compatible. | ||
|
||
// Alphabet chars. | ||
export const CHAR_UPPERCASE_A = 65; /* A */ | ||
export const CHAR_LOWERCASE_A = 97; /* a */ | ||
export const CHAR_UPPERCASE_Z = 90; /* Z */ | ||
export const CHAR_LOWERCASE_Z = 122; /* z */ | ||
|
||
// Non-alphabetic chars. | ||
export const CHAR_DOT = 46; /* . */ | ||
export const CHAR_FORWARD_SLASH = 47; /* / */ | ||
export const CHAR_BACKWARD_SLASH = 92; /* \ */ | ||
export const CHAR_VERTICAL_LINE = 124; /* | */ | ||
export const CHAR_COLON = 58; /* : */ | ||
export const CHAR_QUESTION_MARK = 63; /* ? */ | ||
export const CHAR_UNDERSCORE = 95; /* _ */ | ||
export const CHAR_LINE_FEED = 10; /* \n */ | ||
export const CHAR_CARRIAGE_RETURN = 13; /* \r */ | ||
export const CHAR_TAB = 9; /* \t */ | ||
export const CHAR_FORM_FEED = 12; /* \f */ | ||
export const CHAR_EXCLAMATION_MARK = 33; /* ! */ | ||
export const CHAR_HASH = 35; /* # */ | ||
export const CHAR_SPACE = 32; /* */ | ||
export const CHAR_NO_BREAK_SPACE = 160; /* \u00A0 */ | ||
export const CHAR_ZERO_WIDTH_NOBREAK_SPACE = 65279; /* \uFEFF */ | ||
export const CHAR_LEFT_SQUARE_BRACKET = 91; /* [ */ | ||
export const CHAR_RIGHT_SQUARE_BRACKET = 93; /* ] */ | ||
export const CHAR_LEFT_ANGLE_BRACKET = 60; /* < */ | ||
export const CHAR_RIGHT_ANGLE_BRACKET = 62; /* > */ | ||
export const CHAR_LEFT_CURLY_BRACKET = 123; /* { */ | ||
export const CHAR_RIGHT_CURLY_BRACKET = 125; /* } */ | ||
export const CHAR_HYPHEN_MINUS = 45; /* - */ | ||
export const CHAR_PLUS = 43; /* + */ | ||
export const CHAR_DOUBLE_QUOTE = 34; /* " */ | ||
export const CHAR_SINGLE_QUOTE = 39; /* ' */ | ||
export const CHAR_PERCENT = 37; /* % */ | ||
export const CHAR_SEMICOLON = 59; /* ; */ | ||
export const CHAR_CIRCUMFLEX_ACCENT = 94; /* ^ */ | ||
export const CHAR_GRAVE_ACCENT = 96; /* ` */ | ||
export const CHAR_AT = 64; /* @ */ | ||
export const CHAR_AMPERSAND = 38; /* & */ | ||
export const CHAR_EQUAL = 61; /* = */ | ||
|
||
// Digits | ||
export const CHAR_0 = 48; /* 0 */ | ||
export const CHAR_9 = 57; /* 9 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. | ||
// This module is browser compatible. | ||
|
||
/** | ||
* A parsed path object generated by path.parse() or consumed by path.format(). | ||
*/ | ||
export interface ParsedPath { | ||
/** | ||
* The root of the path such as '/' or 'c:\' | ||
*/ | ||
root: string; | ||
/** | ||
* The full directory path such as '/home/user/dir' or 'c:\path\dir' | ||
*/ | ||
dir: string; | ||
/** | ||
* The file name including extension (if any) such as 'index.html' | ||
*/ | ||
base: string; | ||
/** | ||
* The file extension (if any) such as '.html' | ||
*/ | ||
ext: string; | ||
/** | ||
* The file name without extension (if any) such as 'index' | ||
*/ | ||
name: string; | ||
} | ||
|
||
export type FormatInputPathObject = Partial<ParsedPath>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. | ||
// Copyright the Browserify authors. MIT License. | ||
// Ported from https://github.com/browserify/path-browserify/ | ||
// This module is browser compatible. | ||
|
||
import type { FormatInputPathObject } from "./_interface.ts"; | ||
import { | ||
CHAR_BACKWARD_SLASH, | ||
CHAR_DOT, | ||
CHAR_FORWARD_SLASH, | ||
CHAR_LOWERCASE_A, | ||
CHAR_LOWERCASE_Z, | ||
CHAR_UPPERCASE_A, | ||
CHAR_UPPERCASE_Z, | ||
} from "./_constants.ts"; | ||
|
||
export function assertPath(path: string): void { | ||
if (typeof path !== "string") { | ||
throw new TypeError( | ||
`Path must be a string. Received ${JSON.stringify(path)}`, | ||
); | ||
} | ||
} | ||
|
||
export function isPosixPathSeparator(code: number): boolean { | ||
return code === CHAR_FORWARD_SLASH; | ||
} | ||
|
||
export function isPathSeparator(code: number): boolean { | ||
return isPosixPathSeparator(code) || code === CHAR_BACKWARD_SLASH; | ||
} | ||
|
||
export function isWindowsDeviceRoot(code: number): boolean { | ||
return ( | ||
(code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z) || | ||
(code >= CHAR_UPPERCASE_A && code <= CHAR_UPPERCASE_Z) | ||
); | ||
} | ||
|
||
// 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 | undefined; | ||
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; | ||
} | ||
|
||
export function _format( | ||
sep: string, | ||
pathObject: FormatInputPathObject, | ||
): string { | ||
const dir: string | undefined = pathObject.dir || pathObject.root; | ||
const base: string = pathObject.base || | ||
(pathObject.name || "") + (pathObject.ext || ""); | ||
if (!dir) return base; | ||
if (dir === pathObject.root) return dir + base; | ||
return dir + sep + base; | ||
} | ||
|
||
const WHITESPACE_ENCODINGS: Record<string, string> = { | ||
"\u0009": "%09", | ||
"\u000A": "%0A", | ||
"\u000B": "%0B", | ||
"\u000C": "%0C", | ||
"\u000D": "%0D", | ||
"\u0020": "%20", | ||
}; | ||
|
||
export function encodeWhitespace(string: string): string { | ||
return string.replaceAll(/[\s]/g, (c) => { | ||
return WHITESPACE_ENCODINGS[c] ?? c; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. | ||
// This module is browser compatible. | ||
|
||
import { SEP } from "./separator.ts"; | ||
|
||
/** Determines the common path from a set of paths, using an optional separator, | ||
* which defaults to the OS default separator. | ||
* | ||
* ```ts | ||
* import { common } from "https://deno.land/std@$STD_VERSION/path/mod.ts"; | ||
* const p = common([ | ||
* "./deno/std/path/mod.ts", | ||
* "./deno/std/fs/mod.ts", | ||
* ]); | ||
* console.log(p); // "./deno/std/" | ||
* ``` | ||
*/ | ||
export function common(paths: string[], sep = 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}`; | ||
} |
Oops, something went wrong.