UNPKG

@nberlette/utf8

Version:

Blazing fast universal ponyfills for TextEncoder and TextDecoder.

189 lines 8.91 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TransformStream = exports.PromiseReject = exports.PromiseResolve = exports.Promise = exports.StringPrototypeTrim = exports.StringPrototypeToLowerCase = exports.StringPrototypeCodePointAt = exports.StringPrototypeSlice = exports.StringPrototypeReplace = exports.StringPrototypeCharCodeAt = exports.StringPrototype = exports.StringFromCharCode = exports.String = exports.TypedArrayPrototypeSubarray = exports.TypedArrayPrototypeGetToStringTag = exports.TypedArrayPrototype = exports.TypedArray = exports.Uint8ArrayPrototypeSubarray = exports.Uint8ArrayPrototypeSlice = exports.Uint8Array = exports.SharedArrayBufferPrototypeGetByteLength = exports.SharedArrayBuffer = exports.ArrayBufferPrototypeGetByteLength = exports.ArrayBufferIsView = exports.ArrayBuffer = exports.Symbol = exports.Array = exports.ReferenceError = exports.RangeError = exports.TypeError = exports.Error = exports.toString = exports.ObjectGetOwnPropertyDescriptor = exports.ObjectDefineProperty = exports.ObjectGetPrototypeOf = exports.Object = exports.$global = exports.undefined = void 0; exports.isTypedArray = isTypedArray; exports.gracefulDefine = gracefulDefine; exports.getCodePoint = getCodePoint; exports.utf8BytesNeeded = utf8BytesNeeded; exports.normalizeEncoding = normalizeEncoding; exports.toUint8Array = toUint8Array; // deno-lint-ignore-file no-explicit-any // deno-lint-ignore no-var const dntShim = __importStar(require("./_dnt.shims.js")); exports.$global = (() => { try { if (typeof dntShim.dntGlobalThis === "object") return dntShim.dntGlobalThis; return (0, eval)("this"); } catch { if (typeof window === "object") return window; if (typeof self === "object") return self; if (typeof global === "object") return global; if (typeof root === "object") return root; if (typeof this === "object") return this; // ewww throw "Unable to locate global `this`"; } })(); function isTypedArray(it, type) { try { return (0, exports.TypedArrayPrototypeGetToStringTag)(it) === type; } catch { return false; } } exports.Object = exports.$global.Object; exports.ObjectGetPrototypeOf = exports.Object.getPrototypeOf; exports.ObjectDefineProperty = exports.Object.defineProperty; exports.ObjectGetOwnPropertyDescriptor = exports.Object.getOwnPropertyDescriptor; const Function = exports.$global.Function; const FunctionPrototype = Function.prototype; const { bind, call } = FunctionPrototype; const uncurryThis = (fn) => { const bound = bind.call(call, fn); (0, exports.ObjectDefineProperty)(bound, "name", { value: fn.name }); return bound; }; const FunctionPrototypeBind = uncurryThis(bind); function uncurryGetter(o, p) { return uncurryThis(lookupGetter(o, p)); } function lookupGetter(o, p, _allowUndefined) { return (0, exports.ObjectGetOwnPropertyDescriptor)(o, p)?.get ?? (() => exports.undefined); } function bindAndRename(fn, thisArg, name = fn.name) { const bound = FunctionPrototypeBind(fn, thisArg); (0, exports.ObjectDefineProperty)(bound, "name", { value: name }); return bound; } function gracefulDefine(target, key, value) { if (!(key in target && target[key] !== value)) { if (typeof value === "function" && typeof key === "string") { // ensure function names are preserved when minified etc. (0, exports.ObjectDefineProperty)(value, "name", { value: key }); } (0, exports.ObjectDefineProperty)(target, key, { value, writable: true, enumerable: false, configurable: true, }); } } exports.toString = uncurryThis(exports.Object.prototype.toString); exports.Error = exports.$global.Error; exports.TypeError = exports.$global.TypeError; exports.RangeError = exports.$global.RangeError; exports.ReferenceError = exports.$global.ReferenceError; exports.Array = exports.$global.Array; exports.Symbol = exports.$global.Symbol; exports.ArrayBuffer = exports.$global.ArrayBuffer; exports.ArrayBufferIsView = exports.ArrayBuffer.isView; exports.ArrayBufferPrototypeGetByteLength = uncurryGetter(exports.ArrayBuffer.prototype, "byteLength"); exports.SharedArrayBuffer = exports.$global.SharedArrayBuffer; exports.SharedArrayBufferPrototypeGetByteLength = uncurryGetter(exports.SharedArrayBuffer.prototype, "byteLength"); exports.Uint8Array = exports.$global.Uint8Array; exports.Uint8ArrayPrototypeSlice = uncurryThis(exports.Uint8Array.prototype.slice); exports.Uint8ArrayPrototypeSubarray = uncurryThis(exports.Uint8Array.prototype.subarray); exports.TypedArray = (0, exports.ObjectGetPrototypeOf)(exports.Uint8Array); exports.TypedArrayPrototype = exports.TypedArray?.prototype; exports.TypedArrayPrototypeGetToStringTag = uncurryGetter(exports.TypedArrayPrototype, exports.Symbol.toStringTag); exports.TypedArrayPrototypeSubarray = uncurryThis(exports.TypedArrayPrototype.subarray); exports.String = exports.$global.String; exports.StringFromCharCode = exports.String.fromCharCode; exports.StringPrototype = exports.String.prototype; exports.StringPrototypeCharCodeAt = uncurryThis(exports.StringPrototype.charCodeAt); exports.StringPrototypeReplace = uncurryThis(exports.StringPrototype.replace); exports.StringPrototypeSlice = uncurryThis(exports.StringPrototype.slice); exports.StringPrototypeCodePointAt = uncurryThis(exports.StringPrototype.codePointAt); exports.StringPrototypeToLowerCase = uncurryThis(exports.StringPrototype.toLowerCase); exports.StringPrototypeTrim = uncurryThis(exports.StringPrototype.trim); exports.Promise = exports.$global.Promise; exports.PromiseResolve = bindAndRename(exports.Promise.resolve, exports.Promise); exports.PromiseReject = bindAndRename(exports.Promise.reject, exports.Promise); exports.TransformStream = exports.$global.TransformStream || function TransformStream() { throw new exports.TypeError("TransformStream is not supported in this environment"); }; function getCodePoint(input, index) { const first = (0, exports.StringPrototypeCharCodeAt)(input, index); if (first >= 0xd800 && first <= 0xdbff) { const second = (0, exports.StringPrototypeCharCodeAt)(input, index + 1); if (second >= 0xdc00 && second <= 0xdfff) { return ((first - 0xd800) << 10) + (second - 0xdc00) + 0x10000; } } return first; } function utf8BytesNeeded(codePoint) { if (codePoint <= 0x7f) return 1; if (codePoint <= 0x7ff) return 2; if (codePoint <= 0xffff) return 3; return 4; } function normalizeEncoding(label) { let encoding = (0, exports.StringPrototypeToLowerCase)(label); encoding = (0, exports.StringPrototypeTrim)(encoding); if (encoding === "utf8" || encoding === "utf-8") return "utf-8"; throw new exports.TypeError(`The encoding label provided ('${label}') is invalid.`); } function toUint8Array(input) { if (input == null) { return new exports.Uint8Array(); } else if (isTypedArray(input, "Uint8Array")) { return input; } else if ((0, exports.ArrayBufferIsView)(input)) { return new exports.Uint8Array(input.buffer, input.byteOffset, input.byteLength); } else { try { (0, exports.SharedArrayBufferPrototypeGetByteLength)(input); return new exports.Uint8Array(input); } catch (_) { try { (0, exports.ArrayBufferPrototypeGetByteLength)(input); return new exports.Uint8Array(input); } catch (_) { throw new exports.TypeError('The "input" argument must be of type BufferSource'); } } } } //# sourceMappingURL=_internal.js.map