diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2023-02-14 17:38:45 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-14 17:38:45 +0100 |
commit | d47147fb6ad229b1c039aff9d0959b6e281f4df5 (patch) | |
tree | 6e9e790f2b9bc71b5f0c9c7e64b95cae31579d58 /ext/node/polyfills/_crypto/crypto_browserify/asn1.js/base/buffer.js | |
parent | 1d00bbe47e2ca14e2d2151518e02b2324461a065 (diff) |
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 <crowlkats@toaxl.com>
Co-authored-by: Divy Srivastava <dj.srivastava23@gmail.com>
Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
Diffstat (limited to 'ext/node/polyfills/_crypto/crypto_browserify/asn1.js/base/buffer.js')
-rw-r--r-- | ext/node/polyfills/_crypto/crypto_browserify/asn1.js/base/buffer.js | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/ext/node/polyfills/_crypto/crypto_browserify/asn1.js/base/buffer.js b/ext/node/polyfills/_crypto/crypto_browserify/asn1.js/base/buffer.js new file mode 100644 index 000000000..cb01476cd --- /dev/null +++ b/ext/node/polyfills/_crypto/crypto_browserify/asn1.js/base/buffer.js @@ -0,0 +1,167 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +// Copyright 2017 Fedor Indutny. All rights reserved. MIT license. + +import { Reporter } from "internal:deno_node/polyfills/_crypto/crypto_browserify/asn1.js/base/reporter.js"; +import { Buffer } from "internal:deno_node/polyfills/buffer.ts"; + +export function DecoderBuffer(base, options) { + Reporter.call(this, options); + if (!Buffer.isBuffer(base)) { + this.error("Input not Buffer"); + return; + } + + this.base = base; + this.offset = 0; + this.length = base.length; +} +// inherits(DecoderBuffer, Reporter); +DecoderBuffer.prototype = Object.create(Reporter.prototype, { + constructor: { + value: DecoderBuffer, + enumerable: false, + writable: true, + configurable: true, + }, +}); + +DecoderBuffer.isDecoderBuffer = function isDecoderBuffer(data) { + if (data instanceof DecoderBuffer) { + return true; + } + + // Or accept compatible API + const isCompatible = typeof data === "object" && + Buffer.isBuffer(data.base) && + data.constructor.name === "DecoderBuffer" && + typeof data.offset === "number" && + typeof data.length === "number" && + typeof data.save === "function" && + typeof data.restore === "function" && + typeof data.isEmpty === "function" && + typeof data.readUInt8 === "function" && + typeof data.skip === "function" && + typeof data.raw === "function"; + + return isCompatible; +}; + +DecoderBuffer.prototype.save = function save() { + return { offset: this.offset, reporter: Reporter.prototype.save.call(this) }; +}; + +DecoderBuffer.prototype.restore = function restore(save) { + // Return skipped data + const res = new DecoderBuffer(this.base); + res.offset = save.offset; + res.length = this.offset; + + this.offset = save.offset; + Reporter.prototype.restore.call(this, save.reporter); + + return res; +}; + +DecoderBuffer.prototype.isEmpty = function isEmpty() { + return this.offset === this.length; +}; + +DecoderBuffer.prototype.readUInt8 = function readUInt8(fail) { + if (this.offset + 1 <= this.length) { + return this.base.readUInt8(this.offset++, true); + } else { + return this.error(fail || "DecoderBuffer overrun"); + } +}; + +DecoderBuffer.prototype.skip = function skip(bytes, fail) { + if (!(this.offset + bytes <= this.length)) { + return this.error(fail || "DecoderBuffer overrun"); + } + + const res = new DecoderBuffer(this.base); + + // Share reporter state + res._reporterState = this._reporterState; + + res.offset = this.offset; + res.length = this.offset + bytes; + this.offset += bytes; + return res; +}; + +DecoderBuffer.prototype.raw = function raw(save) { + return this.base.slice(save ? save.offset : this.offset, this.length); +}; + +export function EncoderBuffer(value, reporter) { + if (Array.isArray(value)) { + this.length = 0; + this.value = value.map(function (item) { + if (!EncoderBuffer.isEncoderBuffer(item)) { + item = new EncoderBuffer(item, reporter); + } + this.length += item.length; + return item; + }, this); + } else if (typeof value === "number") { + if (!(0 <= value && value <= 0xff)) { + return reporter.error("non-byte EncoderBuffer value"); + } + this.value = value; + this.length = 1; + } else if (typeof value === "string") { + this.value = value; + this.length = Buffer.byteLength(value); + } else if (Buffer.isBuffer(value)) { + this.value = value; + this.length = value.length; + } else { + return reporter.error("Unsupported type: " + typeof value); + } +} + +EncoderBuffer.isEncoderBuffer = function isEncoderBuffer(data) { + if (data instanceof EncoderBuffer) { + return true; + } + + // Or accept compatible API + const isCompatible = typeof data === "object" && + data.constructor.name === "EncoderBuffer" && + typeof data.length === "number" && + typeof data.join === "function"; + + return isCompatible; +}; + +EncoderBuffer.prototype.join = function join(out, offset) { + if (!out) { + out = Buffer.alloc(this.length); + } + if (!offset) { + offset = 0; + } + + if (this.length === 0) { + return out; + } + + if (Array.isArray(this.value)) { + this.value.forEach(function (item) { + item.join(out, offset); + offset += item.length; + }); + } else { + if (typeof this.value === "number") { + out[offset] = this.value; + } else if (typeof this.value === "string") { + out.write(this.value, offset); + } else if (Buffer.isBuffer(this.value)) { + this.value.copy(out, offset); + } + offset += this.length; + } + + return out; +}; |