2023-02-14 11:38:45 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
// Copyright Joyent, Inc. and Node.js contributors. 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 { 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";
|
2023-05-26 10:18:27 -04:00
|
|
|
import * as io from "ext:deno_io/12_io.js";
|
|
|
|
import * as fs from "ext:deno_fs/30_fs.js";
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
export function writev(fd, buffers, position, callback) {
|
|
|
|
const innerWritev = async (fd, buffers, position) => {
|
|
|
|
const chunks = [];
|
|
|
|
const offset = 0;
|
|
|
|
for (let i = 0; i < buffers.length; i++) {
|
|
|
|
if (Buffer.isBuffer(buffers[i])) {
|
|
|
|
chunks.push(buffers[i]);
|
|
|
|
} else {
|
|
|
|
chunks.push(Buffer.from(buffers[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (typeof position === "number") {
|
2023-05-26 10:18:27 -04:00
|
|
|
await fs.seekSync(fd, position, io.SeekMode.Start);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
const buffer = Buffer.concat(chunks);
|
|
|
|
let currentOffset = 0;
|
|
|
|
while (currentOffset < buffer.byteLength) {
|
2023-05-26 10:18:27 -04:00
|
|
|
currentOffset += await io.writeSync(fd, buffer.subarray(currentOffset));
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
return currentOffset - offset;
|
|
|
|
};
|
|
|
|
|
|
|
|
fd = getValidatedFd(fd);
|
|
|
|
validateBufferArray(buffers);
|
|
|
|
callback = maybeCallback(callback || position);
|
|
|
|
|
|
|
|
if (buffers.length === 0) {
|
|
|
|
process.nextTick(callback, null, 0, buffers);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof position !== "number") position = null;
|
|
|
|
|
|
|
|
innerWritev(fd, buffers, position).then(
|
|
|
|
(nwritten) => {
|
|
|
|
callback(null, nwritten, buffers);
|
|
|
|
},
|
|
|
|
(err) => callback(err),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function writevSync(fd, buffers, position) {
|
|
|
|
const innerWritev = (fd, buffers, position) => {
|
|
|
|
const chunks = [];
|
|
|
|
const offset = 0;
|
|
|
|
for (let i = 0; i < buffers.length; i++) {
|
|
|
|
if (Buffer.isBuffer(buffers[i])) {
|
|
|
|
chunks.push(buffers[i]);
|
|
|
|
} else {
|
|
|
|
chunks.push(Buffer.from(buffers[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (typeof position === "number") {
|
2023-05-26 10:18:27 -04:00
|
|
|
fs.seekSync(fd, position, io.SeekMode.Start);
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
const buffer = Buffer.concat(chunks);
|
|
|
|
let currentOffset = 0;
|
|
|
|
while (currentOffset < buffer.byteLength) {
|
2023-05-26 10:18:27 -04:00
|
|
|
currentOffset += io.writeSync(fd, buffer.subarray(currentOffset));
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
return currentOffset - offset;
|
|
|
|
};
|
|
|
|
|
|
|
|
fd = getValidatedFd(fd);
|
|
|
|
validateBufferArray(buffers);
|
|
|
|
|
|
|
|
if (buffers.length === 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof position !== "number") position = null;
|
|
|
|
|
|
|
|
return innerWritev(fd, buffers, position);
|
|
|
|
}
|