mirror of
https://github.com/denoland/deno.git
synced 2024-11-25 15:29:32 -05:00
refactor(node): use internal io and fs APIs (#19267)
This commit is contained in:
parent
d72e0281ff
commit
a11681a9b0
6 changed files with 31 additions and 22 deletions
|
@ -1,6 +1,8 @@
|
|||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
||||
import { Buffer } from "ext:deno_node/buffer.ts";
|
||||
import { ERR_INVALID_ARG_TYPE } from "ext:deno_node/internal/errors.ts";
|
||||
import * as io from "ext:deno_io/12_io.js";
|
||||
import * as fs from "ext:deno_fs/30_fs.js";
|
||||
import {
|
||||
validateOffsetLengthRead,
|
||||
validatePosition,
|
||||
|
@ -117,14 +119,14 @@ export function read(
|
|||
try {
|
||||
let nread: number | null;
|
||||
if (typeof position === "number" && position >= 0) {
|
||||
const currentPosition = await Deno.seek(fd, 0, Deno.SeekMode.Current);
|
||||
const currentPosition = await fs.seek(fd, 0, io.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);
|
||||
fs.seekSync(fd, position, io.SeekMode.Start);
|
||||
nread = io.readSync(fd, buffer);
|
||||
fs.seekSync(fd, currentPosition, io.SeekMode.Start);
|
||||
} else {
|
||||
nread = await Deno.read(fd, buffer);
|
||||
nread = await io.read(fd, buffer);
|
||||
}
|
||||
cb(null, nread ?? 0, Buffer.from(buffer.buffer, offset, length));
|
||||
} catch (error) {
|
||||
|
@ -183,14 +185,14 @@ export function readSync(
|
|||
|
||||
let currentPosition = 0;
|
||||
if (typeof position === "number" && position >= 0) {
|
||||
currentPosition = Deno.seekSync(fd, 0, Deno.SeekMode.Current);
|
||||
Deno.seekSync(fd, position, Deno.SeekMode.Start);
|
||||
currentPosition = fs.seekSync(fd, 0, io.SeekMode.Current);
|
||||
fs.seekSync(fd, position, io.SeekMode.Start);
|
||||
}
|
||||
|
||||
const numberOfBytesRead = Deno.readSync(fd, buffer);
|
||||
const numberOfBytesRead = io.readSync(fd, buffer);
|
||||
|
||||
if (typeof position === "number" && position >= 0) {
|
||||
Deno.seekSync(fd, currentPosition, Deno.SeekMode.Start);
|
||||
fs.seekSync(fd, currentPosition, io.SeekMode.Start);
|
||||
}
|
||||
|
||||
return numberOfBytesRead ?? 0;
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
// Copyright Joyent, Inc. and Node.js contributors. All rights reserved. MIT license.
|
||||
import { Buffer } from "ext:deno_node/buffer.ts";
|
||||
import { validateEncoding, validateInteger } from "ext:deno_node/internal/validators.mjs";
|
||||
import * as io from "ext:deno_io/12_io.js";
|
||||
import * as fs from "ext:deno_fs/30_fs.js";
|
||||
import {
|
||||
getValidatedFd,
|
||||
showStringCoercionDeprecation,
|
||||
|
@ -19,12 +21,12 @@ export function writeSync(fd, buffer, offset, length, position) {
|
|||
buffer = new Uint8Array(buffer.buffer);
|
||||
}
|
||||
if (typeof position === "number") {
|
||||
Deno.seekSync(fd, position, Deno.SeekMode.Start);
|
||||
fs.seekSync(fd, position, io.SeekMode.Start);
|
||||
}
|
||||
let currentOffset = offset;
|
||||
const end = offset + length;
|
||||
while (currentOffset - offset < length) {
|
||||
currentOffset += Deno.writeSync(fd, buffer.subarray(currentOffset, end));
|
||||
currentOffset += io.writeSync(fd, buffer.subarray(currentOffset, end));
|
||||
}
|
||||
return currentOffset - offset;
|
||||
};
|
||||
|
@ -65,12 +67,12 @@ export function write(fd, buffer, offset, length, position, callback) {
|
|||
buffer = new Uint8Array(buffer.buffer);
|
||||
}
|
||||
if (typeof position === "number") {
|
||||
await Deno.seek(fd, position, Deno.SeekMode.Start);
|
||||
await fs.seek(fd, position, io.SeekMode.Start);
|
||||
}
|
||||
let currentOffset = offset;
|
||||
const end = offset + length;
|
||||
while (currentOffset - offset < length) {
|
||||
currentOffset += await Deno.write(
|
||||
currentOffset += await io.write(
|
||||
fd,
|
||||
buffer.subarray(currentOffset, end),
|
||||
);
|
||||
|
|
|
@ -4,6 +4,8 @@ import { Buffer } from "ext:deno_node/buffer.ts";
|
|||
import { validateBufferArray } from "ext:deno_node/internal/fs/utils.mjs";
|
||||
import { getValidatedFd } from "ext:deno_node/internal/fs/utils.mjs";
|
||||
import { maybeCallback } from "ext:deno_node/_fs/_fs_common.ts";
|
||||
import * as io from "ext:deno_io/12_io.js";
|
||||
import * as fs from "ext:deno_fs/30_fs.js";
|
||||
|
||||
export function writev(fd, buffers, position, callback) {
|
||||
const innerWritev = async (fd, buffers, position) => {
|
||||
|
@ -17,12 +19,12 @@ export function writev(fd, buffers, position, callback) {
|
|||
}
|
||||
}
|
||||
if (typeof position === "number") {
|
||||
await Deno.seekSync(fd, position, Deno.SeekMode.Start);
|
||||
await fs.seekSync(fd, position, io.SeekMode.Start);
|
||||
}
|
||||
const buffer = Buffer.concat(chunks);
|
||||
let currentOffset = 0;
|
||||
while (currentOffset < buffer.byteLength) {
|
||||
currentOffset += await Deno.writeSync(fd, buffer.subarray(currentOffset));
|
||||
currentOffset += await io.writeSync(fd, buffer.subarray(currentOffset));
|
||||
}
|
||||
return currentOffset - offset;
|
||||
};
|
||||
|
@ -58,12 +60,12 @@ export function writevSync(fd, buffers, position) {
|
|||
}
|
||||
}
|
||||
if (typeof position === "number") {
|
||||
Deno.seekSync(fd, position, Deno.SeekMode.Start);
|
||||
fs.seekSync(fd, position, io.SeekMode.Start);
|
||||
}
|
||||
const buffer = Buffer.concat(chunks);
|
||||
let currentOffset = 0;
|
||||
while (currentOffset < buffer.byteLength) {
|
||||
currentOffset += Deno.writeSync(fd, buffer.subarray(currentOffset));
|
||||
currentOffset += io.writeSync(fd, buffer.subarray(currentOffset));
|
||||
}
|
||||
return currentOffset - offset;
|
||||
};
|
||||
|
|
|
@ -216,7 +216,7 @@ export const initStdin = () => {
|
|||
enumerable: true,
|
||||
configurable: true,
|
||||
get() {
|
||||
return Deno.isatty?.(Deno.stdin.rid);
|
||||
return Deno.isatty?.(io.stdin.rid);
|
||||
},
|
||||
});
|
||||
stdin._isRawMode = false;
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
|
||||
import { inspect } from "ext:deno_node/util.ts";
|
||||
import { stripColor as removeColors } from "ext:deno_node/_util/std_fmt_colors.ts";
|
||||
import * as io from "ext:deno_io/12_io.js";
|
||||
|
||||
function getConsoleWidth(): number {
|
||||
try {
|
||||
|
@ -159,7 +160,7 @@ export function createErrDiff(
|
|||
// If the stderr is a tty and the input length is lower than the current
|
||||
// columns per line, add a mismatch indicator below the output. If it is
|
||||
// not a tty, use a default value of 80 characters.
|
||||
const maxLength = Deno.isatty(Deno.stderr.rid) ? getConsoleWidth() : 80;
|
||||
const maxLength = Deno.isatty(io.stderr.rid) ? getConsoleWidth() : 80;
|
||||
if (inputLength < maxLength) {
|
||||
while (actualRaw[i] === expectedRaw[i]) {
|
||||
i++;
|
||||
|
@ -402,7 +403,7 @@ export class AssertionError extends Error {
|
|||
if (message != null) {
|
||||
super(String(message));
|
||||
} else {
|
||||
if (Deno.isatty(Deno.stderr.rid)) {
|
||||
if (Deno.isatty(io.stderr.rid)) {
|
||||
// Reset on each call to make sure we handle dynamically set environment
|
||||
// variables correct.
|
||||
if (Deno.noColor) {
|
||||
|
|
|
@ -26,6 +26,8 @@
|
|||
// - https://github.com/nodejs/node/blob/master/src/node_file.h
|
||||
|
||||
import { assert } from "ext:deno_node/_util/asserts.ts";
|
||||
import * as io from "ext:deno_io/12_io.js";
|
||||
import * as fs from "ext:deno_fs/30_fs.js";
|
||||
|
||||
/**
|
||||
* Write to the given file from the given buffer synchronously.
|
||||
|
@ -58,13 +60,13 @@ export function writeBuffer(
|
|||
);
|
||||
|
||||
if (position) {
|
||||
Deno.seekSync(fd, position, Deno.SeekMode.Current);
|
||||
fs.seekSync(fd, position, io.SeekMode.Current);
|
||||
}
|
||||
|
||||
const subarray = buffer.subarray(offset, offset + length);
|
||||
|
||||
try {
|
||||
return Deno.writeSync(fd, subarray);
|
||||
return io.writeSync(fd, subarray);
|
||||
} catch (e) {
|
||||
ctx.errno = extractOsErrorNumberFromErrorMessage(e);
|
||||
return 0;
|
||||
|
|
Loading…
Reference in a new issue