diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2023-05-26 16:18:27 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-26 16:18:27 +0200 |
commit | a11681a9b02f2f4a0f2bf6945a44b2937c6a9af1 (patch) | |
tree | 55b3a63959b2a935a86c30d8282fbe294f2bb892 | |
parent | d72e0281ff41a87c32aefd0876bace23470a4dc5 (diff) |
refactor(node): use internal io and fs APIs (#19267)
-rw-r--r-- | ext/node/polyfills/_fs/_fs_read.ts | 20 | ||||
-rw-r--r-- | ext/node/polyfills/_fs/_fs_write.mjs | 10 | ||||
-rw-r--r-- | ext/node/polyfills/_fs/_fs_writev.mjs | 10 | ||||
-rw-r--r-- | ext/node/polyfills/_process/streams.mjs | 2 | ||||
-rw-r--r-- | ext/node/polyfills/assertion_error.ts | 5 | ||||
-rw-r--r-- | ext/node/polyfills/internal_binding/node_file.ts | 6 |
6 files changed, 31 insertions, 22 deletions
diff --git a/ext/node/polyfills/_fs/_fs_read.ts b/ext/node/polyfills/_fs/_fs_read.ts index b34384dd4..bce7d334f 100644 --- a/ext/node/polyfills/_fs/_fs_read.ts +++ b/ext/node/polyfills/_fs/_fs_read.ts @@ -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; diff --git a/ext/node/polyfills/_fs/_fs_write.mjs b/ext/node/polyfills/_fs/_fs_write.mjs index bd0ffd105..fd7a1171c 100644 --- a/ext/node/polyfills/_fs/_fs_write.mjs +++ b/ext/node/polyfills/_fs/_fs_write.mjs @@ -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), ); diff --git a/ext/node/polyfills/_fs/_fs_writev.mjs b/ext/node/polyfills/_fs/_fs_writev.mjs index 7440f4fd7..84e2b7cdd 100644 --- a/ext/node/polyfills/_fs/_fs_writev.mjs +++ b/ext/node/polyfills/_fs/_fs_writev.mjs @@ -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; }; diff --git a/ext/node/polyfills/_process/streams.mjs b/ext/node/polyfills/_process/streams.mjs index df014c11e..9096586c7 100644 --- a/ext/node/polyfills/_process/streams.mjs +++ b/ext/node/polyfills/_process/streams.mjs @@ -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; diff --git a/ext/node/polyfills/assertion_error.ts b/ext/node/polyfills/assertion_error.ts index bc3ebb2a3..0c54f12cd 100644 --- a/ext/node/polyfills/assertion_error.ts +++ b/ext/node/polyfills/assertion_error.ts @@ -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) { diff --git a/ext/node/polyfills/internal_binding/node_file.ts b/ext/node/polyfills/internal_binding/node_file.ts index 82ff55b75..c81a7d830 100644 --- a/ext/node/polyfills/internal_binding/node_file.ts +++ b/ext/node/polyfills/internal_binding/node_file.ts @@ -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; |