From 9851be5f4553f169e15f56c86f4d26a6fd6c5188 Mon Sep 17 00:00:00 2001 From: Sarat Addepalli Date: Mon, 6 Aug 2018 15:25:59 +0530 Subject: [PATCH 01/16] fs: update read to work with any TypedArray or DataView --- doc/api/fs.md | 6 +++++- lib/fs.js | 4 ++-- lib/internal/fs/utils.js | 6 ++++-- test/parallel/test-fs-read-type.js | 8 ++++---- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 493093eee01155..5ffffb3442a9ed 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -2345,6 +2345,10 @@ this API: [`fs.open()`][]. * `fd` {integer} -* `buffer` {Buffer|Uint8Array} +* `buffer` {Buffer|TypedArray|DataView} * `offset` {integer} * `length` {integer} * `position` {integer} diff --git a/lib/fs.js b/lib/fs.js index a0e5f64476c0fd..2edc783dc05b6e 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -458,12 +458,12 @@ function read(fd, buffer, offset, length, position, callback) { }); } - if (buffer.length === 0) { + if (buffer.byteLength === 0) { throw new ERR_INVALID_ARG_VALUE('buffer', buffer, 'is empty and cannot be written'); } - validateOffsetLengthRead(offset, length, buffer.length); + validateOffsetLengthRead(offset, length, buffer.byteLength); if (!Number.isSafeInteger(position)) position = -1; diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index f065ba41e89f2a..272bd047b79ddc 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -394,9 +394,11 @@ function toUnixTimestamp(time, name = 'time') { } function validateBuffer(buffer) { - if (!isUint8Array(buffer)) { + if (!ArrayBuffer.isView(buffer)) { const err = new ERR_INVALID_ARG_TYPE('buffer', - ['Buffer', 'Uint8Array'], buffer); + ['Buffer', + 'TypedArray', + 'DataView'], buffer); Error.captureStackTrace(err, validateBuffer); throw err; } diff --git a/test/parallel/test-fs-read-type.js b/test/parallel/test-fs-read-type.js index 2980bca7d2038e..d75464ce0ab1f3 100644 --- a/test/parallel/test-fs-read-type.js +++ b/test/parallel/test-fs-read-type.js @@ -15,8 +15,8 @@ assert.throws( { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError [ERR_INVALID_ARG_TYPE]', - message: 'The "buffer" argument must be one of type Buffer or Uint8Array.' + - ' Received type number' + message: 'The "buffer" argument must be one of type Buffer, TypedArray, ' + + 'or DataView. Received type number' } ); @@ -70,8 +70,8 @@ assert.throws( { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError [ERR_INVALID_ARG_TYPE]', - message: 'The "buffer" argument must be one of type Buffer or Uint8Array.' + - ' Received type number' + message: 'The "buffer" argument must be one of type Buffer, TypedArray, ' + + 'or DataView. Received type number' } ); From ececdf886bbb012f492a66de98ae625ac5c6a81e Mon Sep 17 00:00:00 2001 From: Sarat Addepalli Date: Mon, 6 Aug 2018 15:43:18 +0530 Subject: [PATCH 02/16] fs: update write and writeFile to work with TypedArrays and DataViews --- doc/api/fs.md | 24 +++++++++++++++++------- lib/fs.js | 6 +++--- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 5ffffb3442a9ed..70b78fa8815ee2 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -3358,6 +3358,10 @@ This happens when: * `fd` {integer} -* `buffer` {Buffer|Uint8Array} +* `buffer` {Buffer|TypedArray|DataView} * `offset` {integer} * `length` {integer} * `position` {integer} * `callback` {Function} * `err` {Error} * `bytesWritten` {integer} - * `buffer` {Buffer|Uint8Array} + * `buffer` {Buffer|TypedArray|DataView} Write `buffer` to the file specified by `fd`. @@ -3457,6 +3461,9 @@ the end of the file. * `file` {string|Buffer|URL|integer} filename or file descriptor -* `data` {string|Buffer|Uint8Array} +* `data` {string|Buffer|TypedArray|DataView} * `options` {Object|string} * `encoding` {string|null} **Default:** `'utf8'` * `mode` {integer} **Default:** `0o666` @@ -3490,10 +3497,13 @@ The `encoding` option is ignored if `data` is a buffer. Example: ```js -fs.writeFile('message.txt', 'Hello Node.js', (err) => { - if (err) throw err; - console.log('The file has been saved!'); -}); +fs.writeFile('message.txt', + new Uint8Array(Buffer.from('Hello Node.js')), + (err) => { + if (err) throw err; + console.log('The file has been saved!'); + } +); ``` If `options` is a string, then it specifies the encoding. Example: diff --git a/lib/fs.js b/lib/fs.js index 2edc783dc05b6e..b846da9aef035b 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -525,7 +525,7 @@ function write(fd, buffer, offset, length, position, callback) { const req = new FSReqCallback(); req.oncomplete = wrapper; - if (isUint8Array(buffer)) { + if (ArrayBuffer.isView(buffer)) { callback = maybeCallback(callback || position || length || offset); if (typeof offset !== 'number') offset = 0; @@ -1189,11 +1189,11 @@ function writeFile(path, data, options, callback) { }); function writeFd(fd, isUserFd) { - const buffer = isUint8Array(data) ? + const buffer = ArrayBuffer.isView(data) ? data : Buffer.from('' + data, options.encoding || 'utf8'); const position = /a/.test(flag) ? null : 0; - writeAll(fd, isUserFd, buffer, 0, buffer.length, position, callback); + writeAll(fd, isUserFd, buffer, 0, buffer.byteLength, position, callback); } } From 8aeab8b792151466ab76fbdc397fb83a9d45ae58 Mon Sep 17 00:00:00 2001 From: Sarat Addepalli Date: Mon, 6 Aug 2018 15:53:40 +0530 Subject: [PATCH 03/16] fs: update readSync, writeFile, writeFileSync --- doc/api/fs.md | 15 ++++++++++++--- lib/fs.js | 13 ++++++------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 70b78fa8815ee2..33f1a8cef2ee2e 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -2628,13 +2628,16 @@ the link path returned will be passed as a `Buffer` object. * `fd` {integer} -* `buffer` {Buffer|Uint8Array} +* `buffer` {Buffer|TypedArray|DataView} * `offset` {integer} * `length` {integer} * `position` {integer} @@ -3525,6 +3528,9 @@ automatically. * `file` {string|Buffer|URL|integer} filename or file descriptor -* `data` {string|Buffer|Uint8Array} +* `data` {string|Buffer|TypedArray|DataView} * `options` {Object|string} * `encoding` {string|null} **Default:** `'utf8'` * `mode` {integer} **Default:** `0o666` @@ -3549,6 +3555,9 @@ this API: [`fs.writeFile()`][]. * `fd` {integer} -* `buffer` {Buffer|Uint8Array} +* `buffer` {Buffer|TypedArray|DataView} * `offset` {integer} * `length` {integer} * `position` {integer} diff --git a/lib/fs.js b/lib/fs.js index b846da9aef035b..10d13739c5c153 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -41,7 +41,6 @@ const { const { _extend } = require('util'); const pathModule = require('path'); -const { isUint8Array } = require('internal/util/types'); const binding = process.binding('fs'); const { Buffer, kMaxLength } = require('buffer'); const errors = require('internal/errors'); @@ -493,12 +492,12 @@ function readSync(fd, buffer, offset, length, position) { return 0; } - if (buffer.length === 0) { + if (buffer.byteLength === 0) { throw new ERR_INVALID_ARG_VALUE('buffer', buffer, 'is empty and cannot be written'); } - validateOffsetLengthRead(offset, length, buffer.length); + validateOffsetLengthRead(offset, length, buffer.byteLength); if (!Number.isSafeInteger(position)) position = -1; @@ -563,13 +562,13 @@ function writeSync(fd, buffer, offset, length, position) { validateUint32(fd, 'fd'); const ctx = {}; let result; - if (isUint8Array(buffer)) { + if (ArrayBuffer.isView(buffer)) { if (position === undefined) position = null; if (typeof offset !== 'number') offset = 0; if (typeof length !== 'number') - length = buffer.length - offset; + length = buffer.byteLength - offset; validateOffsetLengthWrite(offset, length, buffer.byteLength); result = binding.writeBuffer(fd, buffer, offset, length, position, undefined, ctx); @@ -1204,11 +1203,11 @@ function writeFileSync(path, data, options) { const isUserFd = isFd(path); // file descriptor ownership const fd = isUserFd ? path : fs.openSync(path, flag, options.mode); - if (!isUint8Array(data)) { + if (!ArrayBuffer.isView(data)) { data = Buffer.from('' + data, options.encoding || 'utf8'); } let offset = 0; - let length = data.length; + let length = data.byteLength; let position = /a/.test(flag) ? null : 0; try { while (length > 0) { From d75c103fd6f47bcca304f5734bcb8156ee889a1f Mon Sep 17 00:00:00 2001 From: Sarat Addepalli Date: Mon, 6 Aug 2018 16:02:15 +0530 Subject: [PATCH 04/16] doc: fix linter issues in fs.md --- doc/api/fs.md | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 33f1a8cef2ee2e..0ef90118911821 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -2630,7 +2630,8 @@ added: v0.1.21 changes: - version: REPLACEME pr-url: REPLACEME - description: The `buffer` parameter can now be any `TypedArray` or a `DataView`. + description: The `buffer` parameter can now be any `TypedArray` or a + `DataView`. - version: v6.0.0 pr-url: https://github.com/nodejs/node/pull/4518 description: The `length` parameter can now be `0`. @@ -3466,7 +3467,8 @@ added: v0.1.29 changes: - version: REPLACEME pr-url: REPLACEME - description: The `data` parameter can now be any `TypedArray` or a `DataView`. + description: The `data` parameter can now be any `TypedArray` or a + `DataView`. - version: v10.0.0 pr-url: https://github.com/nodejs/node/pull/12562 description: The `callback` parameter is no longer optional. Not passing @@ -3500,13 +3502,11 @@ The `encoding` option is ignored if `data` is a buffer. Example: ```js -fs.writeFile('message.txt', - new Uint8Array(Buffer.from('Hello Node.js')), - (err) => { - if (err) throw err; - console.log('The file has been saved!'); - } -); +const data = new Uint8Array(Buffer.from('Hello Node.js')); +fs.writeFile('message.txt', data, (err) => { + if (err) throw err; + console.log('The file has been saved!'); +}); ``` If `options` is a string, then it specifies the encoding. Example: @@ -3530,7 +3530,8 @@ added: v0.1.29 changes: - version: REPLACEME pr-url: REPLACEME - description: The `data` parameter can now be any `TypedArray` or a `DataView`. + description: The `data` parameter can now be any `TypedArray` or a + `DataView`. - version: v7.4.0 pr-url: https://github.com/nodejs/node/pull/10382 description: The `data` parameter can now be a `Uint8Array`. @@ -3557,7 +3558,8 @@ added: v0.1.21 changes: - version: REPLACEME pr-url: REPLACEME - description: The `buffer` parameter can now be any `TypedArray` or a `DataView`. + description: The `buffer` parameter can now be any `TypedArray` or a + `DataView`. - version: v7.4.0 pr-url: https://github.com/nodejs/node/pull/10382 description: The `buffer` parameter can now be a `Uint8Array`. From 1b9ae0b5e0c8c2eef8a9b6cd2d3b9fa0841f2575 Mon Sep 17 00:00:00 2001 From: Sarat Addepalli Date: Mon, 6 Aug 2018 16:44:54 +0530 Subject: [PATCH 05/16] doc: fix yaml indentation for fs.md --- doc/api/fs.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 0ef90118911821..a2cbe085f0e4be 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -2348,7 +2348,7 @@ changes: - version: REPLACEME pr-url: REPLACEME description: The `buffer` parameter can now be any `TypedArray`, or a - `DataView`. + `DataView`. - version: v7.4.0 pr-url: https://github.com/nodejs/node/pull/10382 description: The `buffer` parameter can now be a `Uint8Array`. @@ -2631,7 +2631,7 @@ changes: - version: REPLACEME pr-url: REPLACEME description: The `buffer` parameter can now be any `TypedArray` or a - `DataView`. + `DataView`. - version: v6.0.0 pr-url: https://github.com/nodejs/node/pull/4518 description: The `length` parameter can now be `0`. @@ -3365,7 +3365,7 @@ changes: - version: REPLACEME pr-url: REPLACEME description: The `buffer` parameter can now be any `TypedArray` or a - `DataView` + `DataView` - version: v10.0.0 pr-url: https://github.com/nodejs/node/pull/12562 description: The `callback` parameter is no longer optional. Not passing @@ -3468,7 +3468,7 @@ changes: - version: REPLACEME pr-url: REPLACEME description: The `data` parameter can now be any `TypedArray` or a - `DataView`. + `DataView`. - version: v10.0.0 pr-url: https://github.com/nodejs/node/pull/12562 description: The `callback` parameter is no longer optional. Not passing @@ -3531,7 +3531,7 @@ changes: - version: REPLACEME pr-url: REPLACEME description: The `data` parameter can now be any `TypedArray` or a - `DataView`. + `DataView`. - version: v7.4.0 pr-url: https://github.com/nodejs/node/pull/10382 description: The `data` parameter can now be a `Uint8Array`. @@ -3559,7 +3559,7 @@ changes: - version: REPLACEME pr-url: REPLACEME description: The `buffer` parameter can now be any `TypedArray` or a - `DataView`. + `DataView`. - version: v7.4.0 pr-url: https://github.com/nodejs/node/pull/10382 description: The `buffer` parameter can now be a `Uint8Array`. From 4a8cb024a2e4f13fe66e0eb0046b57cddf35b8e6 Mon Sep 17 00:00:00 2001 From: Sarat Addepalli Date: Tue, 7 Aug 2018 15:05:55 +0530 Subject: [PATCH 06/16] fs: add tests for working with typed arrays --- test/parallel/test-fs-file-typedarrays.js | 51 +++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 test/parallel/test-fs-file-typedarrays.js diff --git a/test/parallel/test-fs-file-typedarrays.js b/test/parallel/test-fs-file-typedarrays.js new file mode 100644 index 00000000000000..53ed7839552202 --- /dev/null +++ b/test/parallel/test-fs-file-typedarrays.js @@ -0,0 +1,51 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const fs = require('fs'); +const join = require('path').join; +const tmpdir = require('../common/tmpdir'); +tmpdir.refresh(); + +const filename = join(tmpdir.path, 'test.txt'); + +const s = '南越国是前203年至前111年存在于岭南地区的一个国家,国都位于番禺,疆域包括今天中国的广东、' + + '广西两省区的大部份地区,福建省、湖南、贵州、云南的一小部份地区和越南的北部。' + + '南越国是秦朝灭亡后,由南海郡尉赵佗于前203年起兵兼并桂林郡和象郡后建立。' + + '前196年和前179年,南越国曾先后两次名义上臣属于西汉,成为西汉的“外臣”。前112年,' + + '南越国末代君主赵建德与西汉发生战争,被汉武帝于前111年所灭。南越国共存在93年,' + + '历经五代君主。南越国是岭南地区的第一个有记载的政权国家,采用封建制和郡县制并存的制度,' + + '它的建立保证了秦末乱世岭南地区社会秩序的稳定,有效的改善了岭南地区落后的政治、##济现状。\n'; + +// length must be a multiple of 8? +const expectStr = s.padEnd(s.length % 8, '-'); +const expectBuf = Buffer.from(expectStr, 'utf8'); + +const types = [ + Int8Array, + Uint8Array, + Uint8ClampedArray, + Int16Array, + Uint16Array, + Int32Array, + Uint32Array, + Float32Array, + Float64Array, + DataView +]; + +const convertToType = (arrayBufferType, input) => + arrayBufferType.from(Buffer.from(input)); +const readInput = (filename) => fs.readFileSync(filename); + + +for (const type of types) { + const expect = convertToType(type, expectBuf); + fs.writeFileSync(filename, expect); + assert.strictEqual(convertToType(type, readInput(filename)), expect); + + fs.writeFile(filename, expect, common.mustCall((e) => { + assert.ifError(e); + + assert.strictEqual(convertToType(type, readInput(filename)), expect); + })); +} From eda78b7ff56e80abe1b3c32f50f62e8b8e7fb5e2 Mon Sep 17 00:00:00 2001 From: Sarat Addepalli Date: Wed, 8 Aug 2018 10:19:39 +0530 Subject: [PATCH 07/16] fs: modify existing test case --- test/parallel/test-fs-file-typedarrays.js | 51 ------------------- .../parallel/test-fs-write-file-uint8array.js | 19 ++++--- 2 files changed, 12 insertions(+), 58 deletions(-) delete mode 100644 test/parallel/test-fs-file-typedarrays.js diff --git a/test/parallel/test-fs-file-typedarrays.js b/test/parallel/test-fs-file-typedarrays.js deleted file mode 100644 index 53ed7839552202..00000000000000 --- a/test/parallel/test-fs-file-typedarrays.js +++ /dev/null @@ -1,51 +0,0 @@ -'use strict'; -const common = require('../common'); -const assert = require('assert'); -const fs = require('fs'); -const join = require('path').join; -const tmpdir = require('../common/tmpdir'); -tmpdir.refresh(); - -const filename = join(tmpdir.path, 'test.txt'); - -const s = '南越国是前203年至前111年存在于岭南地区的一个国家,国都位于番禺,疆域包括今天中国的广东、' + - '广西两省区的大部份地区,福建省、湖南、贵州、云南的一小部份地区和越南的北部。' + - '南越国是秦朝灭亡后,由南海郡尉赵佗于前203年起兵兼并桂林郡和象郡后建立。' + - '前196年和前179年,南越国曾先后两次名义上臣属于西汉,成为西汉的“外臣”。前112年,' + - '南越国末代君主赵建德与西汉发生战争,被汉武帝于前111年所灭。南越国共存在93年,' + - '历经五代君主。南越国是岭南地区的第一个有记载的政权国家,采用封建制和郡县制并存的制度,' + - '它的建立保证了秦末乱世岭南地区社会秩序的稳定,有效的改善了岭南地区落后的政治、##济现状。\n'; - -// length must be a multiple of 8? -const expectStr = s.padEnd(s.length % 8, '-'); -const expectBuf = Buffer.from(expectStr, 'utf8'); - -const types = [ - Int8Array, - Uint8Array, - Uint8ClampedArray, - Int16Array, - Uint16Array, - Int32Array, - Uint32Array, - Float32Array, - Float64Array, - DataView -]; - -const convertToType = (arrayBufferType, input) => - arrayBufferType.from(Buffer.from(input)); -const readInput = (filename) => fs.readFileSync(filename); - - -for (const type of types) { - const expect = convertToType(type, expectBuf); - fs.writeFileSync(filename, expect); - assert.strictEqual(convertToType(type, readInput(filename)), expect); - - fs.writeFile(filename, expect, common.mustCall((e) => { - assert.ifError(e); - - assert.strictEqual(convertToType(type, readInput(filename)), expect); - })); -} diff --git a/test/parallel/test-fs-write-file-uint8array.js b/test/parallel/test-fs-write-file-uint8array.js index 592bdb05814e06..496fd76aa3be97 100644 --- a/test/parallel/test-fs-write-file-uint8array.js +++ b/test/parallel/test-fs-write-file-uint8array.js @@ -17,13 +17,18 @@ const s = '南越国是前203年至前111年存在于岭南地区的一个国家 '历经五代君主。南越国是岭南地区的第一个有记载的政权国家,采用封建制和郡县制并存的制度,' + '它的建立保证了秦末乱世岭南地区社会秩序的稳定,有效的改善了岭南地区落后的政治、##济现状。\n'; -const input = Uint8Array.from(Buffer.from(s, 'utf8')); +const inputBuffer = Buffer.from(s.repeat(8), 'utf8'); -fs.writeFileSync(filename, input); -assert.strictEqual(fs.readFileSync(filename, 'utf8'), s); +for (const expectView of common.getArrayBufferViews(inputBuffer)) { + fs.writeFileSync(filename, expectView); + assert.strictEqual(fs.readFileSync(filename, 'utf8'), expectView); -fs.writeFile(filename, input, common.mustCall((e) => { - assert.ifError(e); + fs.writeFile(filename, expectView, common.mustCall((e) => { + assert.ifError(e); - assert.strictEqual(fs.readFileSync(filename, 'utf8'), s); -})); + fs.readFile(filename, 'utf8', common.mustCall((err, data) => { + assert.ifError(err); + assert.strictEqual(data, expectView); + })); + })); +} From c9bb952b652cc90783bbee72517eb5ce04043530 Mon Sep 17 00:00:00 2001 From: Sarat Addepalli Date: Tue, 7 Aug 2018 15:05:55 +0530 Subject: [PATCH 08/16] fs: add tests for working with typed arrays --- test/parallel/test-fs-file-typedarrays.js | 51 +++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 test/parallel/test-fs-file-typedarrays.js diff --git a/test/parallel/test-fs-file-typedarrays.js b/test/parallel/test-fs-file-typedarrays.js new file mode 100644 index 00000000000000..53ed7839552202 --- /dev/null +++ b/test/parallel/test-fs-file-typedarrays.js @@ -0,0 +1,51 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const fs = require('fs'); +const join = require('path').join; +const tmpdir = require('../common/tmpdir'); +tmpdir.refresh(); + +const filename = join(tmpdir.path, 'test.txt'); + +const s = '南越国是前203年至前111年存在于岭南地区的一个国家,国都位于番禺,疆域包括今天中国的广东、' + + '广西两省区的大部份地区,福建省、湖南、贵州、云南的一小部份地区和越南的北部。' + + '南越国是秦朝灭亡后,由南海郡尉赵佗于前203年起兵兼并桂林郡和象郡后建立。' + + '前196年和前179年,南越国曾先后两次名义上臣属于西汉,成为西汉的“外臣”。前112年,' + + '南越国末代君主赵建德与西汉发生战争,被汉武帝于前111年所灭。南越国共存在93年,' + + '历经五代君主。南越国是岭南地区的第一个有记载的政权国家,采用封建制和郡县制并存的制度,' + + '它的建立保证了秦末乱世岭南地区社会秩序的稳定,有效的改善了岭南地区落后的政治、##济现状。\n'; + +// length must be a multiple of 8? +const expectStr = s.padEnd(s.length % 8, '-'); +const expectBuf = Buffer.from(expectStr, 'utf8'); + +const types = [ + Int8Array, + Uint8Array, + Uint8ClampedArray, + Int16Array, + Uint16Array, + Int32Array, + Uint32Array, + Float32Array, + Float64Array, + DataView +]; + +const convertToType = (arrayBufferType, input) => + arrayBufferType.from(Buffer.from(input)); +const readInput = (filename) => fs.readFileSync(filename); + + +for (const type of types) { + const expect = convertToType(type, expectBuf); + fs.writeFileSync(filename, expect); + assert.strictEqual(convertToType(type, readInput(filename)), expect); + + fs.writeFile(filename, expect, common.mustCall((e) => { + assert.ifError(e); + + assert.strictEqual(convertToType(type, readInput(filename)), expect); + })); +} From 7084577d713d46af6100a8ad3dba87fbdc5099a7 Mon Sep 17 00:00:00 2001 From: Sarat Addepalli Date: Wed, 8 Aug 2018 10:19:39 +0530 Subject: [PATCH 09/16] fs: modify existing test case --- test/parallel/test-fs-file-typedarrays.js | 51 ----------------------- 1 file changed, 51 deletions(-) delete mode 100644 test/parallel/test-fs-file-typedarrays.js diff --git a/test/parallel/test-fs-file-typedarrays.js b/test/parallel/test-fs-file-typedarrays.js deleted file mode 100644 index 53ed7839552202..00000000000000 --- a/test/parallel/test-fs-file-typedarrays.js +++ /dev/null @@ -1,51 +0,0 @@ -'use strict'; -const common = require('../common'); -const assert = require('assert'); -const fs = require('fs'); -const join = require('path').join; -const tmpdir = require('../common/tmpdir'); -tmpdir.refresh(); - -const filename = join(tmpdir.path, 'test.txt'); - -const s = '南越国是前203年至前111年存在于岭南地区的一个国家,国都位于番禺,疆域包括今天中国的广东、' + - '广西两省区的大部份地区,福建省、湖南、贵州、云南的一小部份地区和越南的北部。' + - '南越国是秦朝灭亡后,由南海郡尉赵佗于前203年起兵兼并桂林郡和象郡后建立。' + - '前196年和前179年,南越国曾先后两次名义上臣属于西汉,成为西汉的“外臣”。前112年,' + - '南越国末代君主赵建德与西汉发生战争,被汉武帝于前111年所灭。南越国共存在93年,' + - '历经五代君主。南越国是岭南地区的第一个有记载的政权国家,采用封建制和郡县制并存的制度,' + - '它的建立保证了秦末乱世岭南地区社会秩序的稳定,有效的改善了岭南地区落后的政治、##济现状。\n'; - -// length must be a multiple of 8? -const expectStr = s.padEnd(s.length % 8, '-'); -const expectBuf = Buffer.from(expectStr, 'utf8'); - -const types = [ - Int8Array, - Uint8Array, - Uint8ClampedArray, - Int16Array, - Uint16Array, - Int32Array, - Uint32Array, - Float32Array, - Float64Array, - DataView -]; - -const convertToType = (arrayBufferType, input) => - arrayBufferType.from(Buffer.from(input)); -const readInput = (filename) => fs.readFileSync(filename); - - -for (const type of types) { - const expect = convertToType(type, expectBuf); - fs.writeFileSync(filename, expect); - assert.strictEqual(convertToType(type, readInput(filename)), expect); - - fs.writeFile(filename, expect, common.mustCall((e) => { - assert.ifError(e); - - assert.strictEqual(convertToType(type, readInput(filename)), expect); - })); -} From 1ca1b0229957048a76333f179fe1c500cb073bd9 Mon Sep 17 00:00:00 2001 From: Sarat Addepalli Date: Thu, 16 Aug 2018 14:22:11 +0530 Subject: [PATCH 10/16] test: update tests for typed arrays --- ...int8array.js => test-fs-write-file-typedarrays.js} | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) rename test/parallel/{test-fs-write-file-uint8array.js => test-fs-write-file-typedarrays.js} (80%) diff --git a/test/parallel/test-fs-write-file-uint8array.js b/test/parallel/test-fs-write-file-typedarrays.js similarity index 80% rename from test/parallel/test-fs-write-file-uint8array.js rename to test/parallel/test-fs-write-file-typedarrays.js index 496fd76aa3be97..4ee3ca131d2518 100644 --- a/test/parallel/test-fs-write-file-uint8array.js +++ b/test/parallel/test-fs-write-file-typedarrays.js @@ -3,6 +3,7 @@ const common = require('../common'); const assert = require('assert'); const fs = require('fs'); const join = require('path').join; +const debuglog = require('util').debuglog('test'); const tmpdir = require('../common/tmpdir'); tmpdir.refresh(); @@ -17,18 +18,24 @@ const s = '南越国是前203年至前111年存在于岭南地区的一个国家 '历经五代君主。南越国是岭南地区的第一个有记载的政权国家,采用封建制和郡县制并存的制度,' + '它的建立保证了秦末乱世岭南地区社会秩序的稳定,有效的改善了岭南地区落后的政治、##济现状。\n'; +// The length of the buffer should be a multiple of 8 +// as required by common.getArrayBufferViews() const inputBuffer = Buffer.from(s.repeat(8), 'utf8'); for (const expectView of common.getArrayBufferViews(inputBuffer)) { + debuglog('Running test for: ', expectView[Symbol.toStringTag]); fs.writeFileSync(filename, expectView); - assert.strictEqual(fs.readFileSync(filename, 'utf8'), expectView); + assert.strictEqual( + fs.readFileSync(filename, 'utf8'), + inputBuffer.toString('utf8') + ); fs.writeFile(filename, expectView, common.mustCall((e) => { assert.ifError(e); fs.readFile(filename, 'utf8', common.mustCall((err, data) => { assert.ifError(err); - assert.strictEqual(data, expectView); + assert.strictEqual(data, inputBuffer.toString('utf8')); })); })); } From 9087cc904e24514bc33b821deb4f45dd986a9a32 Mon Sep 17 00:00:00 2001 From: Sarat Addepalli Date: Tue, 7 Aug 2018 15:05:55 +0530 Subject: [PATCH 11/16] fs: add tests for working with typed arrays --- test/parallel/test-fs-file-typedarrays.js | 51 +++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 test/parallel/test-fs-file-typedarrays.js diff --git a/test/parallel/test-fs-file-typedarrays.js b/test/parallel/test-fs-file-typedarrays.js new file mode 100644 index 00000000000000..53ed7839552202 --- /dev/null +++ b/test/parallel/test-fs-file-typedarrays.js @@ -0,0 +1,51 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const fs = require('fs'); +const join = require('path').join; +const tmpdir = require('../common/tmpdir'); +tmpdir.refresh(); + +const filename = join(tmpdir.path, 'test.txt'); + +const s = '南越国是前203年至前111年存在于岭南地区的一个国家,国都位于番禺,疆域包括今天中国的广东、' + + '广西两省区的大部份地区,福建省、湖南、贵州、云南的一小部份地区和越南的北部。' + + '南越国是秦朝灭亡后,由南海郡尉赵佗于前203年起兵兼并桂林郡和象郡后建立。' + + '前196年和前179年,南越国曾先后两次名义上臣属于西汉,成为西汉的“外臣”。前112年,' + + '南越国末代君主赵建德与西汉发生战争,被汉武帝于前111年所灭。南越国共存在93年,' + + '历经五代君主。南越国是岭南地区的第一个有记载的政权国家,采用封建制和郡县制并存的制度,' + + '它的建立保证了秦末乱世岭南地区社会秩序的稳定,有效的改善了岭南地区落后的政治、##济现状。\n'; + +// length must be a multiple of 8? +const expectStr = s.padEnd(s.length % 8, '-'); +const expectBuf = Buffer.from(expectStr, 'utf8'); + +const types = [ + Int8Array, + Uint8Array, + Uint8ClampedArray, + Int16Array, + Uint16Array, + Int32Array, + Uint32Array, + Float32Array, + Float64Array, + DataView +]; + +const convertToType = (arrayBufferType, input) => + arrayBufferType.from(Buffer.from(input)); +const readInput = (filename) => fs.readFileSync(filename); + + +for (const type of types) { + const expect = convertToType(type, expectBuf); + fs.writeFileSync(filename, expect); + assert.strictEqual(convertToType(type, readInput(filename)), expect); + + fs.writeFile(filename, expect, common.mustCall((e) => { + assert.ifError(e); + + assert.strictEqual(convertToType(type, readInput(filename)), expect); + })); +} From 53472c635d9bd06858e650a3c55fdbd548c092a6 Mon Sep 17 00:00:00 2001 From: Sarat Addepalli Date: Wed, 8 Aug 2018 10:19:39 +0530 Subject: [PATCH 12/16] fs: modify existing test case --- test/parallel/test-fs-file-typedarrays.js | 51 ----------------------- 1 file changed, 51 deletions(-) delete mode 100644 test/parallel/test-fs-file-typedarrays.js diff --git a/test/parallel/test-fs-file-typedarrays.js b/test/parallel/test-fs-file-typedarrays.js deleted file mode 100644 index 53ed7839552202..00000000000000 --- a/test/parallel/test-fs-file-typedarrays.js +++ /dev/null @@ -1,51 +0,0 @@ -'use strict'; -const common = require('../common'); -const assert = require('assert'); -const fs = require('fs'); -const join = require('path').join; -const tmpdir = require('../common/tmpdir'); -tmpdir.refresh(); - -const filename = join(tmpdir.path, 'test.txt'); - -const s = '南越国是前203年至前111年存在于岭南地区的一个国家,国都位于番禺,疆域包括今天中国的广东、' + - '广西两省区的大部份地区,福建省、湖南、贵州、云南的一小部份地区和越南的北部。' + - '南越国是秦朝灭亡后,由南海郡尉赵佗于前203年起兵兼并桂林郡和象郡后建立。' + - '前196年和前179年,南越国曾先后两次名义上臣属于西汉,成为西汉的“外臣”。前112年,' + - '南越国末代君主赵建德与西汉发生战争,被汉武帝于前111年所灭。南越国共存在93年,' + - '历经五代君主。南越国是岭南地区的第一个有记载的政权国家,采用封建制和郡县制并存的制度,' + - '它的建立保证了秦末乱世岭南地区社会秩序的稳定,有效的改善了岭南地区落后的政治、##济现状。\n'; - -// length must be a multiple of 8? -const expectStr = s.padEnd(s.length % 8, '-'); -const expectBuf = Buffer.from(expectStr, 'utf8'); - -const types = [ - Int8Array, - Uint8Array, - Uint8ClampedArray, - Int16Array, - Uint16Array, - Int32Array, - Uint32Array, - Float32Array, - Float64Array, - DataView -]; - -const convertToType = (arrayBufferType, input) => - arrayBufferType.from(Buffer.from(input)); -const readInput = (filename) => fs.readFileSync(filename); - - -for (const type of types) { - const expect = convertToType(type, expectBuf); - fs.writeFileSync(filename, expect); - assert.strictEqual(convertToType(type, readInput(filename)), expect); - - fs.writeFile(filename, expect, common.mustCall((e) => { - assert.ifError(e); - - assert.strictEqual(convertToType(type, readInput(filename)), expect); - })); -} From 1b9b24518dd7c7507d31bf4632b954b33aca5a37 Mon Sep 17 00:00:00 2001 From: Sarat Addepalli Date: Sun, 19 Aug 2018 09:29:20 +0530 Subject: [PATCH 13/16] fs: use isArrayBufferView instead of ArrayBuffer.isView --- lib/fs.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 10d13739c5c153..e1fb2c022f8c68 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -41,6 +41,7 @@ const { const { _extend } = require('util'); const pathModule = require('path'); +const { isArrayBufferView } = require('internal/util/types'); const binding = process.binding('fs'); const { Buffer, kMaxLength } = require('buffer'); const errors = require('internal/errors'); @@ -524,7 +525,7 @@ function write(fd, buffer, offset, length, position, callback) { const req = new FSReqCallback(); req.oncomplete = wrapper; - if (ArrayBuffer.isView(buffer)) { + if (isArrayBufferView(buffer)) { callback = maybeCallback(callback || position || length || offset); if (typeof offset !== 'number') offset = 0; @@ -562,7 +563,7 @@ function writeSync(fd, buffer, offset, length, position) { validateUint32(fd, 'fd'); const ctx = {}; let result; - if (ArrayBuffer.isView(buffer)) { + if (isArrayBufferView(buffer)) { if (position === undefined) position = null; if (typeof offset !== 'number') @@ -1188,7 +1189,7 @@ function writeFile(path, data, options, callback) { }); function writeFd(fd, isUserFd) { - const buffer = ArrayBuffer.isView(data) ? + const buffer = isArrayBufferView(data) ? data : Buffer.from('' + data, options.encoding || 'utf8'); const position = /a/.test(flag) ? null : 0; @@ -1203,7 +1204,7 @@ function writeFileSync(path, data, options) { const isUserFd = isFd(path); // file descriptor ownership const fd = isUserFd ? path : fs.openSync(path, flag, options.mode); - if (!ArrayBuffer.isView(data)) { + if (!isArrayBufferView(data)) { data = Buffer.from('' + data, options.encoding || 'utf8'); } let offset = 0; From 654944fe3892f7eba8297195ca51b5e8812126f5 Mon Sep 17 00:00:00 2001 From: Sarat Addepalli Date: Sun, 19 Aug 2018 13:30:34 +0530 Subject: [PATCH 14/16] test: use console.log instead of util.debuglog --- test/parallel/test-fs-write-file-typedarrays.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-fs-write-file-typedarrays.js b/test/parallel/test-fs-write-file-typedarrays.js index 4ee3ca131d2518..e9bd9fe9a7a540 100644 --- a/test/parallel/test-fs-write-file-typedarrays.js +++ b/test/parallel/test-fs-write-file-typedarrays.js @@ -3,7 +3,6 @@ const common = require('../common'); const assert = require('assert'); const fs = require('fs'); const join = require('path').join; -const debuglog = require('util').debuglog('test'); const tmpdir = require('../common/tmpdir'); tmpdir.refresh(); @@ -23,13 +22,16 @@ const s = '南越国是前203年至前111年存在于岭南地区的一个国家 const inputBuffer = Buffer.from(s.repeat(8), 'utf8'); for (const expectView of common.getArrayBufferViews(inputBuffer)) { - debuglog('Running test for: ', expectView[Symbol.toStringTag]); + console.log('Sync test for ', expectView[Symbol.toStringTag]); fs.writeFileSync(filename, expectView); assert.strictEqual( fs.readFileSync(filename, 'utf8'), inputBuffer.toString('utf8') ); +} +for (const expectView of common.getArrayBufferViews(inputBuffer)) { + console.log('Async test for ', expectView[Symbol.toStringTag]); fs.writeFile(filename, expectView, common.mustCall((e) => { assert.ifError(e); From 20ab8f6e55c184273053ee78ee2d3536836c2628 Mon Sep 17 00:00:00 2001 From: Sarat Addepalli Date: Mon, 20 Aug 2018 10:13:03 +0530 Subject: [PATCH 15/16] fs: use isArrayBufferView instead of ArrayBuffer.isView --- lib/internal/fs/utils.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index 272bd047b79ddc..acc566e37bb4bf 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -9,7 +9,7 @@ const { ERR_INVALID_OPT_VALUE_ENCODING, ERR_OUT_OF_RANGE } = require('internal/errors').codes; -const { isUint8Array } = require('internal/util/types'); +const { isUint8Array, isArrayBufferView } = require('internal/util/types'); const pathModule = require('path'); const util = require('util'); const kType = Symbol('type'); @@ -394,11 +394,10 @@ function toUnixTimestamp(time, name = 'time') { } function validateBuffer(buffer) { - if (!ArrayBuffer.isView(buffer)) { + if (!isArrayBufferView(buffer)) { const err = new ERR_INVALID_ARG_TYPE('buffer', - ['Buffer', - 'TypedArray', - 'DataView'], buffer); + ['Buffer', 'TypedArray', 'DataView'], + buffer); Error.captureStackTrace(err, validateBuffer); throw err; } From 8f9347351958a06f3133f16babf5e19fd6b6aced Mon Sep 17 00:00:00 2001 From: Sarat Addepalli Date: Mon, 20 Aug 2018 16:23:01 +0530 Subject: [PATCH 16/16] doc: update fs.md with pr-urls --- doc/api/fs.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index a2cbe085f0e4be..ee5b0a7d9dce38 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -2346,7 +2346,7 @@ this API: [`fs.open()`][]. added: v0.0.2 changes: - version: REPLACEME - pr-url: REPLACEME + pr-url: https://github.com/nodejs/node/pull/22150 description: The `buffer` parameter can now be any `TypedArray`, or a `DataView`. - version: v7.4.0 @@ -2629,7 +2629,7 @@ the link path returned will be passed as a `Buffer` object. added: v0.1.21 changes: - version: REPLACEME - pr-url: REPLACEME + pr-url: https://github.com/nodejs/node/pull/22150 description: The `buffer` parameter can now be any `TypedArray` or a `DataView`. - version: v6.0.0 @@ -3363,7 +3363,7 @@ This happens when: added: v0.0.2 changes: - version: REPLACEME - pr-url: REPLACEME + pr-url: https://github.com/nodejs/node/pull/22150 description: The `buffer` parameter can now be any `TypedArray` or a `DataView` - version: v10.0.0 @@ -3466,7 +3466,7 @@ the end of the file. added: v0.1.29 changes: - version: REPLACEME - pr-url: REPLACEME + pr-url: https://github.com/nodejs/node/pull/22150 description: The `data` parameter can now be any `TypedArray` or a `DataView`. - version: v10.0.0 @@ -3529,7 +3529,7 @@ automatically. added: v0.1.29 changes: - version: REPLACEME - pr-url: REPLACEME + pr-url: https://github.com/nodejs/node/pull/22150 description: The `data` parameter can now be any `TypedArray` or a `DataView`. - version: v7.4.0 @@ -3557,7 +3557,7 @@ this API: [`fs.writeFile()`][]. added: v0.1.21 changes: - version: REPLACEME - pr-url: REPLACEME + pr-url: https://github.com/nodejs/node/pull/22150 description: The `buffer` parameter can now be any `TypedArray` or a `DataView`. - version: v7.4.0