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