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/_fs/_fs_read.ts | |
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/_fs/_fs_read.ts')
-rw-r--r-- | ext/node/polyfills/_fs/_fs_read.ts | 197 |
1 files changed, 197 insertions, 0 deletions
diff --git a/ext/node/polyfills/_fs/_fs_read.ts b/ext/node/polyfills/_fs/_fs_read.ts new file mode 100644 index 000000000..d74445829 --- /dev/null +++ b/ext/node/polyfills/_fs/_fs_read.ts @@ -0,0 +1,197 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +import { Buffer } from "internal:deno_node/polyfills/buffer.ts"; +import { ERR_INVALID_ARG_TYPE } from "internal:deno_node/polyfills/internal/errors.ts"; +import { + validateOffsetLengthRead, + validatePosition, +} from "internal:deno_node/polyfills/internal/fs/utils.mjs"; +import { + validateBuffer, + validateInteger, +} from "internal:deno_node/polyfills/internal/validators.mjs"; + +type readOptions = { + buffer: Buffer | Uint8Array; + offset: number; + length: number; + position: number | null; +}; + +type readSyncOptions = { + offset: number; + length: number; + position: number | null; +}; + +type BinaryCallback = ( + err: Error | null, + bytesRead: number | null, + data?: Buffer, +) => void; +type Callback = BinaryCallback; + +export function read(fd: number, callback: Callback): void; +export function read( + fd: number, + options: readOptions, + callback: Callback, +): void; +export function read( + fd: number, + buffer: Buffer | Uint8Array, + offset: number, + length: number, + position: number | null, + callback: Callback, +): void; +export function read( + fd: number, + optOrBufferOrCb?: Buffer | Uint8Array | readOptions | Callback, + offsetOrCallback?: number | Callback, + length?: number, + position?: number | null, + callback?: Callback, +) { + let cb: Callback | undefined; + let offset = 0, + buffer: Buffer | Uint8Array; + + if (typeof fd !== "number") { + throw new ERR_INVALID_ARG_TYPE("fd", "number", fd); + } + + if (length == null) { + length = 0; + } + + if (typeof offsetOrCallback === "function") { + cb = offsetOrCallback; + } else if (typeof optOrBufferOrCb === "function") { + cb = optOrBufferOrCb; + } else { + offset = offsetOrCallback as number; + validateInteger(offset, "offset", 0); + cb = callback; + } + + if ( + optOrBufferOrCb instanceof Buffer || optOrBufferOrCb instanceof Uint8Array + ) { + buffer = optOrBufferOrCb; + } else if (typeof optOrBufferOrCb === "function") { + offset = 0; + buffer = Buffer.alloc(16384); + length = buffer.byteLength; + position = null; + } else { + const opt = optOrBufferOrCb as readOptions; + if ( + !(opt.buffer instanceof Buffer) && !(opt.buffer instanceof Uint8Array) + ) { + if (opt.buffer === null) { + // @ts-ignore: Intentionally create TypeError for passing test-fs-read.js#L87 + length = opt.buffer.byteLength; + } + throw new ERR_INVALID_ARG_TYPE("buffer", [ + "Buffer", + "TypedArray", + "DataView", + ], optOrBufferOrCb); + } + offset = opt.offset ?? 0; + buffer = opt.buffer ?? Buffer.alloc(16384); + length = opt.length ?? buffer.byteLength; + position = opt.position ?? null; + } + + if (position == null) { + position = -1; + } + + validatePosition(position); + validateOffsetLengthRead(offset, length, buffer.byteLength); + + if (!cb) throw new ERR_INVALID_ARG_TYPE("cb", "Callback", cb); + + (async () => { + try { + let nread: number | null; + if (typeof position === "number" && position >= 0) { + const currentPosition = await Deno.seek(fd, 0, Deno.SeekMode.Current); + // We use sync calls below to avoid being affected by others during + // these calls. + Deno.seekSync(fd, position, Deno.SeekMode.Start); + nread = Deno.readSync(fd, buffer); + Deno.seekSync(fd, currentPosition, Deno.SeekMode.Start); + } else { + nread = await Deno.read(fd, buffer); + } + cb(null, nread ?? 0, Buffer.from(buffer.buffer, offset, length)); + } catch (error) { + cb(error as Error, null); + } + })(); +} + +export function readSync( + fd: number, + buffer: Buffer | Uint8Array, + offset: number, + length: number, + position: number | null, +): number; +export function readSync( + fd: number, + buffer: Buffer | Uint8Array, + opt: readSyncOptions, +): number; +export function readSync( + fd: number, + buffer: Buffer | Uint8Array, + offsetOrOpt?: number | readSyncOptions, + length?: number, + position?: number | null, +): number { + let offset = 0; + + if (typeof fd !== "number") { + throw new ERR_INVALID_ARG_TYPE("fd", "number", fd); + } + + validateBuffer(buffer); + + if (length == null) { + length = 0; + } + + if (typeof offsetOrOpt === "number") { + offset = offsetOrOpt; + validateInteger(offset, "offset", 0); + } else { + const opt = offsetOrOpt as readSyncOptions; + offset = opt.offset ?? 0; + length = opt.length ?? buffer.byteLength; + position = opt.position ?? null; + } + + if (position == null) { + position = -1; + } + + validatePosition(position); + validateOffsetLengthRead(offset, length, buffer.byteLength); + + let currentPosition = 0; + if (typeof position === "number" && position >= 0) { + currentPosition = Deno.seekSync(fd, 0, Deno.SeekMode.Current); + Deno.seekSync(fd, position, Deno.SeekMode.Start); + } + + const numberOfBytesRead = Deno.readSync(fd, buffer); + + if (typeof position === "number" && position >= 0) { + Deno.seekSync(fd, currentPosition, Deno.SeekMode.Start); + } + + return numberOfBytesRead ?? 0; +} |