|
5 | 5 | // deno-lint-ignore-file prefer-primordials
|
6 | 6 |
|
7 | 7 | import { Buffer } from "node:buffer";
|
| 8 | +import process from "node:process"; |
| 9 | +import { ErrnoException } from "ext:deno_node/_global.d.ts"; |
8 | 10 | import { validateBufferArray } from "ext:deno_node/internal/fs/utils.mjs";
|
9 | 11 | import { getValidatedFd } from "ext:deno_node/internal/fs/utils.mjs";
|
| 12 | +import { WriteVResult } from "ext:deno_node/internal/fs/handle.ts"; |
10 | 13 | import { maybeCallback } from "ext:deno_node/_fs/_fs_common.ts";
|
11 | 14 | import * as io from "ext:deno_io/12_io.js";
|
12 | 15 | import { op_fs_seek_async, op_fs_seek_sync } from "ext:core/ops";
|
13 | 16 |
|
14 |
| -export function writev(fd, buffers, position, callback) { |
| 17 | +export interface WriteVResult { |
| 18 | + bytesWritten: number; |
| 19 | + buffers: ReadonlyArray<ArrayBufferView>; |
| 20 | +} |
| 21 | + |
| 22 | +type writeVCallback = ( |
| 23 | + err: ErrnoException | null, |
| 24 | + bytesWritten: number, |
| 25 | + buffers: ReadonlyArray<ArrayBufferView>, |
| 26 | +) => void; |
| 27 | + |
| 28 | +/** |
| 29 | + * Write an array of `ArrayBufferView`s to the file specified by `fd` using`writev()`. |
| 30 | + * |
| 31 | + * `position` is the offset from the beginning of the file where this data |
| 32 | + * should be written. If `typeof position !== 'number'`, the data will be written |
| 33 | + * at the current position. |
| 34 | + * |
| 35 | + * The callback will be given three arguments: `err`, `bytesWritten`, and`buffers`. `bytesWritten` is how many bytes were written from `buffers`. |
| 36 | + * |
| 37 | + * If this method is `util.promisify()` ed, it returns a promise for an`Object` with `bytesWritten` and `buffers` properties. |
| 38 | + * |
| 39 | + * It is unsafe to use `fs.writev()` multiple times on the same file without |
| 40 | + * waiting for the callback. For this scenario, use {@link createWriteStream}. |
| 41 | + * |
| 42 | + * On Linux, positional writes don't work when the file is opened in append mode. |
| 43 | + * The kernel ignores the position argument and always appends the data to |
| 44 | + * the end of the file. |
| 45 | + * @since v12.9.0 |
| 46 | + */ |
| 47 | +export function writev( |
| 48 | + fd: number, |
| 49 | + buffers: ReadonlyArray<ArrayBufferView>, |
| 50 | + position?: number | null, |
| 51 | + callback?: writeVCallback, |
| 52 | +): void { |
15 | 53 | const innerWritev = async (fd, buffers, position) => {
|
16 |
| - const chunks = []; |
| 54 | + const chunks: Buffer[] = []; |
17 | 55 | const offset = 0;
|
18 | 56 | for (let i = 0; i < buffers.length; i++) {
|
19 | 57 | if (Buffer.isBuffer(buffers[i])) {
|
@@ -45,16 +83,24 @@ export function writev(fd, buffers, position, callback) {
|
45 | 83 | if (typeof position !== "number") position = null;
|
46 | 84 |
|
47 | 85 | innerWritev(fd, buffers, position).then(
|
48 |
| - (nwritten) => { |
49 |
| - callback(null, nwritten, buffers); |
50 |
| - }, |
| 86 | + (nwritten) => callback(null, nwritten, buffers), |
51 | 87 | (err) => callback(err),
|
52 | 88 | );
|
53 | 89 | }
|
54 | 90 |
|
55 |
| -export function writevSync(fd, buffers, position) { |
| 91 | +/** |
| 92 | + * For detailed information, see the documentation of the asynchronous version of |
| 93 | + * this API: {@link writev}. |
| 94 | + * @since v12.9.0 |
| 95 | + * @return The number of bytes written. |
| 96 | + */ |
| 97 | +export function writevSync( |
| 98 | + fd: number, |
| 99 | + buffers: ArrayBufferView[], |
| 100 | + position?: number | null, |
| 101 | +): number { |
56 | 102 | const innerWritev = (fd, buffers, position) => {
|
57 |
| - const chunks = []; |
| 103 | + const chunks: Buffer[] = []; |
58 | 104 | const offset = 0;
|
59 | 105 | for (let i = 0; i < buffers.length; i++) {
|
60 | 106 | if (Buffer.isBuffer(buffers[i])) {
|
@@ -85,3 +131,16 @@ export function writevSync(fd, buffers, position) {
|
85 | 131 |
|
86 | 132 | return innerWritev(fd, buffers, position);
|
87 | 133 | }
|
| 134 | + |
| 135 | +export function writevPromise( |
| 136 | + fd: number, |
| 137 | + buffers: ArrayBufferView[], |
| 138 | + position?: number, |
| 139 | +): Promise<WriteVResult> { |
| 140 | + return new Promise((resolve, reject) => { |
| 141 | + writev(fd, buffers, position, (err, bytesWritten, buffers) => { |
| 142 | + if (err) reject(err); |
| 143 | + else resolve({ bytesWritten, buffers }); |
| 144 | + }); |
| 145 | + }); |
| 146 | +} |
0 commit comments