1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4const path = require('path'); 5const fs = require('fs').promises; 6const tmpdir = require('../common/tmpdir'); 7const expected = 'ümlaut. Лорем 運務ホソモ指及 आपको करने विकास 紙読決多密所 أضف'; 8let cnt = 0; 9 10function getFileName() { 11 return path.join(tmpdir.path, `writev_promises_${++cnt}.txt`); 12} 13 14tmpdir.refresh(); 15 16(async () => { 17 { 18 const filename = getFileName(); 19 const handle = await fs.open(filename, 'w'); 20 const buffer = Buffer.from(expected); 21 const bufferArr = [buffer, buffer]; 22 const expectedLength = bufferArr.length * buffer.byteLength; 23 let { bytesWritten, buffers } = await handle.writev([Buffer.from('')], 24 null); 25 assert.strictEqual(bytesWritten, 0); 26 assert.deepStrictEqual(buffers, [Buffer.from('')]); 27 ({ bytesWritten, buffers } = await handle.writev(bufferArr, null)); 28 assert.deepStrictEqual(bytesWritten, expectedLength); 29 assert.deepStrictEqual(buffers, bufferArr); 30 assert(Buffer.concat(bufferArr).equals(await fs.readFile(filename))); 31 handle.close(); 32 } 33 34 // fs.promises.writev() with an array of buffers without position. 35 { 36 const filename = getFileName(); 37 const handle = await fs.open(filename, 'w'); 38 const buffer = Buffer.from(expected); 39 const bufferArr = [buffer, buffer, buffer]; 40 const expectedLength = bufferArr.length * buffer.byteLength; 41 let { bytesWritten, buffers } = await handle.writev([Buffer.from('')]); 42 assert.strictEqual(bytesWritten, 0); 43 assert.deepStrictEqual(buffers, [Buffer.from('')]); 44 ({ bytesWritten, buffers } = await handle.writev(bufferArr)); 45 assert.deepStrictEqual(bytesWritten, expectedLength); 46 assert.deepStrictEqual(buffers, bufferArr); 47 assert(Buffer.concat(bufferArr).equals(await fs.readFile(filename))); 48 handle.close(); 49 } 50 51 { 52 // Writev with empty array behavior 53 const handle = await fs.open(getFileName(), 'w'); 54 const result = await handle.writev([]); 55 assert.strictEqual(result.bytesWritten, 0); 56 assert.strictEqual(result.buffers.length, 0); 57 handle.close(); 58 } 59})().then(common.mustCall()); 60