From d47147fb6ad229b1c039aff9d0959b6e281f4df5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 14 Feb 2023 17:38:45 +0100 Subject: feat(ext/node): embed std/node into the snapshot (#17724) This commit moves "deno_std/node" in "ext/node" crate. The code is transpiled and snapshotted during the build process. During the first pass a minimal amount of work was done to create the snapshot, a lot of code in "ext/node" depends on presence of "Deno" global. This code will be gradually fixed in the follow up PRs to migrate it to import relevant APIs from "internal:" modules. Currently the code from snapshot is not used in any way, and all Node/npm compatibility still uses code from "https://deno.land/std/node" (or from the location specified by "DENO_NODE_COMPAT_URL"). This will also be handled in a follow up PRs. --------- Co-authored-by: crowlkats Co-authored-by: Divy Srivastava Co-authored-by: Yoshiya Hinosawa --- ext/node/polyfills/internal_binding/_utils.ts | 88 +++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 ext/node/polyfills/internal_binding/_utils.ts (limited to 'ext/node/polyfills/internal_binding/_utils.ts') diff --git a/ext/node/polyfills/internal_binding/_utils.ts b/ext/node/polyfills/internal_binding/_utils.ts new file mode 100644 index 000000000..4d545df0e --- /dev/null +++ b/ext/node/polyfills/internal_binding/_utils.ts @@ -0,0 +1,88 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +import { + forgivingBase64Decode, + forgivingBase64UrlEncode, +} from "internal:deno_web/00_infra.js"; + +export function asciiToBytes(str: string) { + const byteArray = []; + for (let i = 0; i < str.length; ++i) { + byteArray.push(str.charCodeAt(i) & 255); + } + return new Uint8Array(byteArray); +} + +export function base64ToBytes(str: string) { + str = base64clean(str); + str = str.replaceAll("-", "+").replaceAll("_", "/"); + return forgivingBase64Decode(str); +} + +const INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g; +function base64clean(str: string) { + // Node takes equal signs as end of the Base64 encoding + str = str.split("=")[0]; + // Node strips out invalid characters like \n and \t from the string, std/base64 does not + str = str.trim().replace(INVALID_BASE64_RE, ""); + // Node converts strings with length < 2 to '' + if (str.length < 2) return ""; + // Node allows for non-padded base64 strings (missing trailing ===), std/base64 does not + while (str.length % 4 !== 0) { + str = str + "="; + } + return str; +} + +export function base64UrlToBytes(str: string) { + str = base64clean(str); + str = str.replaceAll("+", "-").replaceAll("/", "_"); + return forgivingBase64UrlEncode(str); +} + +export function hexToBytes(str: string) { + const byteArray = new Uint8Array(Math.floor((str || "").length / 2)); + let i; + for (i = 0; i < byteArray.length; i++) { + const a = Number.parseInt(str[i * 2], 16); + const b = Number.parseInt(str[i * 2 + 1], 16); + if (Number.isNaN(a) && Number.isNaN(b)) { + break; + } + byteArray[i] = (a << 4) | b; + } + return new Uint8Array( + i === byteArray.length ? byteArray : byteArray.slice(0, i), + ); +} + +export function utf16leToBytes(str: string, units: number) { + let c, hi, lo; + const byteArray = []; + for (let i = 0; i < str.length; ++i) { + if ((units -= 2) < 0) { + break; + } + c = str.charCodeAt(i); + hi = c >> 8; + lo = c % 256; + byteArray.push(lo); + byteArray.push(hi); + } + return new Uint8Array(byteArray); +} + +export function bytesToAscii(bytes: Uint8Array) { + let ret = ""; + for (let i = 0; i < bytes.length; ++i) { + ret += String.fromCharCode(bytes[i] & 127); + } + return ret; +} + +export function bytesToUtf16le(bytes: Uint8Array) { + let res = ""; + for (let i = 0; i < bytes.length - 1; i += 2) { + res += String.fromCharCode(bytes[i] + bytes[i + 1] * 256); + } + return res; +} -- cgit v1.2.3