2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-06-27 02:18:22 -04:00
|
|
|
|
|
|
|
// TODO(petamoriken): enable prefer-primordials for node polyfills
|
|
|
|
// deno-lint-ignore-file prefer-primordials
|
|
|
|
|
2023-07-02 14:19:30 -04:00
|
|
|
import { Buffer } from "node:buffer";
|
2023-03-08 06:44:54 -05:00
|
|
|
import { ERR_INVALID_ARG_TYPE } from "ext:deno_node/internal/errors.ts";
|
2023-05-26 10:18:27 -04:00
|
|
|
import * as io from "ext:deno_io/12_io.js";
|
2023-06-08 08:37:19 -04:00
|
|
|
import { ReadOptions } from "ext:deno_node/_fs/_fs_common.ts";
|
2023-02-14 11:38:45 -05:00
|
|
|
import {
|
2024-08-14 12:42:31 -04:00
|
|
|
arrayBufferViewToUint8Array,
|
2023-02-14 11:38:45 -05:00
|
|
|
validateOffsetLengthRead,
|
|
|
|
validatePosition,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_node/internal/fs/utils.mjs";
|
2023-02-14 11:38:45 -05:00
|
|
|
import {
|
|
|
|
validateBuffer,
|
|
|
|
validateInteger,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_node/internal/validators.mjs";
|
2024-08-14 12:42:31 -04:00
|
|
|
import { isArrayBufferView } from "ext:deno_node/internal/util/types.ts";
|
2024-09-05 06:37:28 -04:00
|
|
|
import { op_fs_seek_async, op_fs_seek_sync } from "ext:core/ops";
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
type readSyncOptions = {
|
|
|
|
offset: number;
|
|
|
|
length: number;
|
|
|
|
position: number | null;
|
|
|
|
};
|
|
|
|
|
|
|
|
type BinaryCallback = (
|
|
|
|
err: Error | null,
|
|
|
|
bytesRead: number | null,
|
2024-08-16 12:48:57 -04:00
|
|
|
data?: ArrayBufferView,
|
2023-02-14 11:38:45 -05:00
|
|
|
) => void;
|
|
|
|
type Callback = BinaryCallback;
|
|
|
|
|
|
|
|
export function read(fd: number, callback: Callback): void;
|
|
|
|
export function read(
|
|
|
|
fd: number,
|
2023-06-08 08:37:19 -04:00
|
|
|
options: ReadOptions,
|
2023-02-14 11:38:45 -05:00
|
|
|
callback: Callback,
|
|
|
|
): void;
|
|
|
|
export function read(
|
|
|
|
fd: number,
|
2024-08-14 12:42:31 -04:00
|
|
|
buffer: ArrayBufferView,
|
2023-02-14 11:38:45 -05:00
|
|
|
offset: number,
|
|
|
|
length: number,
|
|
|
|
position: number | null,
|
|
|
|
callback: Callback,
|
|
|
|
): void;
|
|
|
|
export function read(
|
|
|
|
fd: number,
|
2024-08-14 12:42:31 -04:00
|
|
|
optOrBufferOrCb?: ArrayBufferView | ReadOptions | Callback,
|
2023-02-14 11:38:45 -05:00
|
|
|
offsetOrCallback?: number | Callback,
|
|
|
|
length?: number,
|
|
|
|
position?: number | null,
|
|
|
|
callback?: Callback,
|
|
|
|
) {
|
|
|
|
let cb: Callback | undefined;
|
|
|
|
let offset = 0,
|
2024-08-16 12:48:57 -04:00
|
|
|
buffer: ArrayBufferView;
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
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 (
|
2024-08-14 12:42:31 -04:00
|
|
|
isArrayBufferView(optOrBufferOrCb)
|
2023-02-14 11:38:45 -05:00
|
|
|
) {
|
2024-08-16 12:48:57 -04:00
|
|
|
buffer = optOrBufferOrCb;
|
2023-02-14 11:38:45 -05:00
|
|
|
} else if (typeof optOrBufferOrCb === "function") {
|
|
|
|
offset = 0;
|
|
|
|
buffer = Buffer.alloc(16384);
|
|
|
|
length = buffer.byteLength;
|
|
|
|
position = null;
|
|
|
|
} else {
|
2023-06-08 08:37:19 -04:00
|
|
|
const opt = optOrBufferOrCb as ReadOptions;
|
2023-02-14 11:38:45 -05:00
|
|
|
if (
|
2024-08-14 12:42:31 -04:00
|
|
|
!isArrayBufferView(opt.buffer)
|
2023-02-14 11:38:45 -05:00
|
|
|
) {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE("buffer", [
|
|
|
|
"Buffer",
|
|
|
|
"TypedArray",
|
|
|
|
"DataView",
|
|
|
|
], optOrBufferOrCb);
|
|
|
|
}
|
2024-08-14 12:42:31 -04:00
|
|
|
if (opt.buffer === undefined) {
|
|
|
|
buffer = Buffer.alloc(16384);
|
|
|
|
} else {
|
2024-08-16 12:48:57 -04:00
|
|
|
buffer = opt.buffer;
|
2024-08-14 12:42:31 -04:00
|
|
|
}
|
2023-02-14 11:38:45 -05:00
|
|
|
offset = opt.offset ?? 0;
|
2024-08-16 12:48:57 -04:00
|
|
|
length = opt.length ?? buffer.byteLength - offset;
|
2023-02-14 11:38:45 -05:00
|
|
|
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) {
|
2024-09-05 06:37:28 -04:00
|
|
|
const currentPosition = await op_fs_seek_async(
|
|
|
|
fd,
|
|
|
|
0,
|
|
|
|
io.SeekMode.Current,
|
|
|
|
);
|
2023-02-14 11:38:45 -05:00
|
|
|
// We use sync calls below to avoid being affected by others during
|
|
|
|
// these calls.
|
2024-09-05 06:37:28 -04:00
|
|
|
op_fs_seek_sync(fd, position, io.SeekMode.Start);
|
2024-08-16 12:48:57 -04:00
|
|
|
nread = io.readSync(
|
|
|
|
fd,
|
|
|
|
arrayBufferViewToUint8Array(buffer).subarray(offset, offset + length),
|
|
|
|
);
|
2024-09-05 06:37:28 -04:00
|
|
|
op_fs_seek_sync(fd, currentPosition, io.SeekMode.Start);
|
2023-02-14 11:38:45 -05:00
|
|
|
} else {
|
2024-08-16 12:48:57 -04:00
|
|
|
nread = await io.read(
|
|
|
|
fd,
|
|
|
|
arrayBufferViewToUint8Array(buffer).subarray(offset, offset + length),
|
|
|
|
);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
2024-08-16 12:48:57 -04:00
|
|
|
cb(null, nread ?? 0, buffer);
|
2023-02-14 11:38:45 -05:00
|
|
|
} catch (error) {
|
|
|
|
cb(error as Error, null);
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function readSync(
|
|
|
|
fd: number,
|
2024-08-14 12:42:31 -04:00
|
|
|
buffer: ArrayBufferView,
|
2023-02-14 11:38:45 -05:00
|
|
|
offset: number,
|
|
|
|
length: number,
|
|
|
|
position: number | null,
|
|
|
|
): number;
|
|
|
|
export function readSync(
|
|
|
|
fd: number,
|
2024-08-14 12:42:31 -04:00
|
|
|
buffer: ArrayBufferView,
|
2023-02-14 11:38:45 -05:00
|
|
|
opt: readSyncOptions,
|
|
|
|
): number;
|
|
|
|
export function readSync(
|
|
|
|
fd: number,
|
2024-08-14 12:42:31 -04:00
|
|
|
buffer: ArrayBufferView,
|
2023-02-14 11:38:45 -05:00
|
|
|
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);
|
2023-10-22 04:02:55 -04:00
|
|
|
} else if (offsetOrOpt !== undefined) {
|
2023-02-14 11:38:45 -05:00
|
|
|
const opt = offsetOrOpt as readSyncOptions;
|
|
|
|
offset = opt.offset ?? 0;
|
2024-08-16 12:48:57 -04:00
|
|
|
length = opt.length ?? buffer.byteLength - offset;
|
2023-02-14 11:38:45 -05:00
|
|
|
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) {
|
2024-09-05 06:37:28 -04:00
|
|
|
currentPosition = op_fs_seek_sync(fd, 0, io.SeekMode.Current);
|
|
|
|
op_fs_seek_sync(fd, position, io.SeekMode.Start);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
2024-08-16 12:48:57 -04:00
|
|
|
const numberOfBytesRead = io.readSync(
|
|
|
|
fd,
|
|
|
|
arrayBufferViewToUint8Array(buffer).subarray(offset, offset + length),
|
|
|
|
);
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
if (typeof position === "number" && position >= 0) {
|
2024-09-05 06:37:28 -04:00
|
|
|
op_fs_seek_sync(fd, currentPosition, io.SeekMode.Start);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return numberOfBytesRead ?? 0;
|
|
|
|
}
|