Skip to content

Commit 2141d44

Browse files
committed
error instead of falling back to fd
1 parent 1bd6db1 commit 2141d44

File tree

2 files changed

+9
-26
lines changed

2 files changed

+9
-26
lines changed

ext/node/polyfills/fs.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,16 +400,17 @@ function readv(
400400
buffers: readonly ArrayBufferView[],
401401
position: number | null,
402402
) => {
403+
const rid = getRid(fd);
403404
if (typeof position === "number") {
404-
await op_fs_seek_async(fd, position, io.SeekMode.Start);
405+
await op_fs_seek_async(rid, position, io.SeekMode.Start);
405406
}
406407

407408
let readTotal = 0;
408409
let readInBuf = 0;
409410
let bufIdx = 0;
410411
let buf = buffers[bufIdx];
411412
while (bufIdx < buffers.length) {
412-
const nread = await io.read(fd, buf);
413+
const nread = await io.read(rid, buf);
413414
if (nread === null) {
414415
break;
415416
}
@@ -455,17 +456,18 @@ function readvSync(
455456
if (buffers.length === 0) {
456457
return 0;
457458
}
459+
const rid = getRid(fd);
458460
if (typeof position === "number") {
459461
validateInteger(position, "position", 0);
460-
op_fs_seek_sync(fd, position, io.SeekMode.Start);
462+
op_fs_seek_sync(rid, position, io.SeekMode.Start);
461463
}
462464

463465
let readTotal = 0;
464466
let readInBuf = 0;
465467
let bufIdx = 0;
466468
let buf = buffers[bufIdx];
467469
while (bufIdx < buffers.length) {
468-
const nread = io.readSync(fd, buf);
470+
const nread = io.readSync(rid, buf);
469471
if (nread === null) {
470472
break;
471473
}

ext/node/polyfills/internal/fs/fd_map.ts

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@ import { primordials } from "ext:core/mod.js";
44
import { op_node_dup_fd } from "ext:core/ops";
55

66
const {
7-
ErrorPrototype,
87
MapPrototypeGet,
98
MapPrototypeSet,
109
MapPrototypeDelete,
11-
ObjectPrototypeIsPrototypeOf,
1210
SafeMap,
13-
StringPrototypeIncludes,
1411
} = primordials;
1512

1613
// Maps OS file descriptors to Deno resource IDs.
@@ -37,25 +34,9 @@ export function getRid(fd: number): number {
3734
// and closeable. op_node_dup_fd denies fds managed by internal Deno
3835
// resources (non-fsFile) to prevent unauthorized access.
3936
if (fd >= 3) {
40-
try {
41-
const newRid = op_node_dup_fd(fd);
42-
MapPrototypeSet(fdMap, fd, newRid);
43-
return newRid;
44-
} catch (e) {
45-
// Unsupported means the platform doesn't support dup (Windows).
46-
// Fall through to return fd as-is.
47-
// For any other error (including PermissionDenied for internal Deno
48-
// resources, EBADF, etc.), re-throw to prevent using the raw fd
49-
// number as a resource ID.
50-
if (
51-
ObjectPrototypeIsPrototypeOf(ErrorPrototype, e) &&
52-
StringPrototypeIncludes(e.message, "not supported")
53-
) {
54-
// Fall through -- on Windows, dup is unavailable
55-
} else {
56-
throw e;
57-
}
58-
}
37+
const newRid = op_node_dup_fd(fd);
38+
MapPrototypeSet(fdMap, fd, newRid);
39+
return newRid;
5940
}
6041
return fd;
6142
}

0 commit comments

Comments
 (0)