UNPKG

@worker-tools/deno-kv-storage

Version:

An implementation of the StorageArea (1,2,3) interface for Deno with an extensible system for supporting various database backends.

147 lines 7.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.crypto = void 0; // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. const mod_js_1 = require("../_wasm_crypto/mod.js"); /** * A copy of the global WebCrypto interface, with methods bound so they're * safe to re-export. */ const webCrypto = ((crypto) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1; return ({ getRandomValues: (_a = crypto.getRandomValues) === null || _a === void 0 ? void 0 : _a.bind(crypto), randomUUID: (_b = crypto.randomUUID) === null || _b === void 0 ? void 0 : _b.bind(crypto), subtle: { decrypt: (_d = (_c = crypto.subtle) === null || _c === void 0 ? void 0 : _c.decrypt) === null || _d === void 0 ? void 0 : _d.bind(crypto.subtle), deriveBits: (_f = (_e = crypto.subtle) === null || _e === void 0 ? void 0 : _e.deriveBits) === null || _f === void 0 ? void 0 : _f.bind(crypto.subtle), deriveKey: (_h = (_g = crypto.subtle) === null || _g === void 0 ? void 0 : _g.deriveKey) === null || _h === void 0 ? void 0 : _h.bind(crypto.subtle), digest: (_k = (_j = crypto.subtle) === null || _j === void 0 ? void 0 : _j.digest) === null || _k === void 0 ? void 0 : _k.bind(crypto.subtle), encrypt: (_m = (_l = crypto.subtle) === null || _l === void 0 ? void 0 : _l.encrypt) === null || _m === void 0 ? void 0 : _m.bind(crypto.subtle), exportKey: (_p = (_o = crypto.subtle) === null || _o === void 0 ? void 0 : _o.exportKey) === null || _p === void 0 ? void 0 : _p.bind(crypto.subtle), generateKey: (_r = (_q = crypto.subtle) === null || _q === void 0 ? void 0 : _q.generateKey) === null || _r === void 0 ? void 0 : _r.bind(crypto.subtle), importKey: (_t = (_s = crypto.subtle) === null || _s === void 0 ? void 0 : _s.importKey) === null || _t === void 0 ? void 0 : _t.bind(crypto.subtle), sign: (_v = (_u = crypto.subtle) === null || _u === void 0 ? void 0 : _u.sign) === null || _v === void 0 ? void 0 : _v.bind(crypto.subtle), unwrapKey: (_x = (_w = crypto.subtle) === null || _w === void 0 ? void 0 : _w.unwrapKey) === null || _x === void 0 ? void 0 : _x.bind(crypto.subtle), verify: (_z = (_y = crypto.subtle) === null || _y === void 0 ? void 0 : _y.verify) === null || _z === void 0 ? void 0 : _z.bind(crypto.subtle), wrapKey: (_1 = (_0 = crypto.subtle) === null || _0 === void 0 ? void 0 : _0.wrapKey) === null || _1 === void 0 ? void 0 : _1.bind(crypto.subtle), }, }); })(globalThis.crypto); const bufferSourceBytes = (data) => { let bytes; if (data instanceof Uint8Array) { bytes = data; } else if (ArrayBuffer.isView(data)) { bytes = new Uint8Array(data.buffer, data.byteOffset, data.byteLength); } else if (data instanceof ArrayBuffer) { bytes = new Uint8Array(data); } return bytes; }; /** * An wrapper for WebCrypto adding support for additional non-standard * algorithms, but delegating to the runtime WebCrypto implementation whenever * possible. */ const stdCrypto = ((x) => x)({ ...webCrypto, subtle: { ...webCrypto.subtle, /** * Returns a new `Promise` object that will digest `data` using the specified * `AlgorithmIdentifier`. */ async digest(algorithm, data) { var _a, _b; const { name, length } = normalizeAlgorithm(algorithm); const bytes = bufferSourceBytes(data); // We delegate to WebCrypto whenever possible, if ( // if the SubtleCrypto interface is available, ((_a = webCrypto.subtle) === null || _a === void 0 ? void 0 : _a.digest) && // if the algorithm is supported by the WebCrypto standard, webCryptoDigestAlgorithms.includes(name) && // and the data is a single buffer, bytes) { return webCrypto.subtle.digest(algorithm, bytes); } else if (mod_js_1.digestAlgorithms.includes(name)) { if (bytes) { // Otherwise, we use our bundled WASM implementation via digestSync // if it supports the algorithm. return stdCrypto.subtle.digestSync(algorithm, bytes); } else if (data[Symbol.iterator]) { return stdCrypto.subtle.digestSync(algorithm, data); } else if (data[Symbol.asyncIterator]) { const context = new mod_js_1.crypto.DigestContext(name); for await (const chunk of data) { const chunkBytes = bufferSourceBytes(chunk); if (!chunkBytes) { throw new TypeError("data contained chunk of the wrong type"); } context.update(chunkBytes); } return context.digestAndDrop(length).buffer; } else { throw new TypeError("data must be a BufferSource or [Async]Iterable<BufferSource>"); } } else if ((_b = webCrypto.subtle) === null || _b === void 0 ? void 0 : _b.digest) { // (TypeScript type definitions prohibit this case.) If they're trying // to call an algorithm we don't recognize, pass it along to WebCrypto // in case it's a non-standard algorithm supported by the the runtime // they're using. return webCrypto.subtle.digest(algorithm, data); } else { throw new TypeError(`unsupported digest algorithm: ${algorithm}`); } }, /** * Returns a ArrayBuffer with the result of digesting `data` using the * specified `AlgorithmIdentifier`. */ digestSync(algorithm, data) { algorithm = normalizeAlgorithm(algorithm); const bytes = bufferSourceBytes(data); if (bytes) { return mod_js_1.crypto.digest(algorithm.name, bytes, algorithm.length) .buffer; } else if (data[Symbol.iterator]) { const context = new mod_js_1.crypto.DigestContext(algorithm.name); for (const chunk of data) { const chunkBytes = bufferSourceBytes(chunk); if (!chunkBytes) { throw new TypeError("data contained chunk of the wrong type"); } context.update(chunkBytes); } return context.digestAndDrop(algorithm.length).buffer; } else { throw new TypeError("data must be a BufferSource or Iterable<BufferSource>"); } }, }, }); exports.crypto = stdCrypto; /** Digest algorithms supported by WebCrypto. */ const webCryptoDigestAlgorithms = [ "SHA-384", "SHA-256", "SHA-512", // insecure (length-extendable and collidable): "SHA-1", ]; const normalizeAlgorithm = (algorithm) => ((typeof algorithm === "string") ? { name: algorithm.toUpperCase() } : { ...algorithm, name: algorithm.name.toUpperCase(), }); //# sourceMappingURL=mod.js.map