| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent cccf5a6 commit 1de0490
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1876,13 +1876,13 @@ you need to compare `curr.mtime` and `prev.mtime`. | |||
| 1876 | 1876 | `fs.unwatchFile`. `fs.watch` should be used instead of `fs.watchFile` and | |
| 1877 | 1877 | `fs.unwatchFile` when possible. | |
| 1878 | 1878 | ||
| 1879 | - ## fs.write(fd, buffer, offset, length[, position], callback) | ||
| 1879 | + ## fs.write(fd, buffer[, offset[, length[, position]]], callback) | ||
| 1880 | 1880 | <!-- YAML | |
| 1881 | 1881 | added: v0.0.2 | |
| 1882 | 1882 | --> | |
| 1883 | 1883 | ||
| 1884 | 1884 | * `fd` {integer} | |
| 1885 | - * `buffer` {string | Buffer} | ||
| 1885 | + * `buffer` {Buffer} | ||
| 1886 | 1886 | * `offset` {integer} | |
| 1887 | 1887 | * `length` {integer} | |
| 1888 | 1888 | * `position` {integer} | |
@@ -1908,19 +1908,19 @@ On Linux, positional writes don't work when the file is opened in append mode. | |||
| 1908 | 1908 | The kernel ignores the position argument and always appends the data to | |
| 1909 | 1909 | the end of the file. | |
| 1910 | 1910 | ||
| 1911 | - ## fs.write(fd, data[, position[, encoding]], callback) | ||
| 1911 | + ## fs.write(fd, string[, position[, encoding]], callback) | ||
| 1912 | 1912 | <!-- YAML | |
| 1913 | 1913 | added: v0.11.5 | |
| 1914 | 1914 | --> | |
| 1915 | 1915 | ||
| 1916 | 1916 | * `fd` {integer} | |
| 1917 | - * `data` {string | Buffer} | ||
| 1917 | + * `string` {string} | ||
| 1918 | 1918 | * `position` {integer} | |
| 1919 | 1919 | * `encoding` {string} | |
| 1920 | 1920 | * `callback` {Function} | |
| 1921 | 1921 | ||
| 1922 | - Write `data` to the file specified by `fd`. If `data` is not a Buffer instance | ||
| 1923 | - then the value will be coerced to a string. | ||
| 1922 | + Write `string` to the file specified by `fd`. If `string` is not a string, then | ||
| 1923 | + the value will be coerced to one. | ||
| 1924 | 1924 | ||
| 1925 | 1925 | `position` refers to the offset from the beginning of the file where this data | |
| 1926 | 1926 | should be written. If `typeof position !== 'number'` the data will be written at | |
@@ -2001,24 +2001,24 @@ added: v0.1.29 | |||
| 2001 | 2001 | ||
| 2002 | 2002 | The synchronous version of [`fs.writeFile()`][]. Returns `undefined`. | |
| 2003 | 2003 | ||
| 2004 | - ## fs.writeSync(fd, buffer, offset, length[, position]) | ||
| 2004 | + ## fs.writeSync(fd, buffer[, offset[, length[, position]]]) | ||
| 2005 | 2005 | <!-- YAML | |
| 2006 | 2006 | added: v0.1.21 | |
| 2007 | 2007 | --> | |
| 2008 | 2008 | ||
| 2009 | 2009 | * `fd` {integer} | |
| 2010 | - * `buffer` {string | Buffer} | ||
| 2010 | + * `buffer` {Buffer} | ||
| 2011 | 2011 | * `offset` {integer} | |
| 2012 | 2012 | * `length` {integer} | |
| 2013 | 2013 | * `position` {integer} | |
| 2014 | 2014 | ||
| 2015 | - ## fs.writeSync(fd, data[, position[, encoding]]) | ||
| 2015 | + ## fs.writeSync(fd, string[, position[, encoding]]) | ||
| 2016 | 2016 | <!-- YAML | |
| 2017 | 2017 | added: v0.11.5 | |
| 2018 | 2018 | --> | |
| 2019 | 2019 | ||
| 2020 | 2020 | * `fd` {integer} | |
| 2021 | - * `data` {string | Buffer} | ||
| 2021 | + * `string` {string} | ||
| 2022 | 2022 | * `position` {integer} | |
| 2023 | 2023 | * `encoding` {string} | |
| 2024 | 2024 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -740,7 +740,7 @@ fs.readSync = function(fd, buffer, offset, length, position) { | |||
| 740 | 740 | }; | |
| 741 | 741 | ||
| 742 | 742 | // usage: | |
| 743 | - // fs.write(fd, buffer, offset, length[, position], callback); | ||
| 743 | + // fs.write(fd, buffer[, offset[, length[, position]]], callback); | ||
| 744 | 744 | // OR | |
| 745 | 745 | // fs.write(fd, string[, position[, encoding]], callback); | |
| 746 | 746 | fs.write = function(fd, buffer, offset, length, position, callback) { | |
@@ -753,12 +753,16 @@ fs.write = function(fd, buffer, offset, length, position, callback) { | |||
| 753 | 753 | req.oncomplete = wrapper; | |
| 754 | 754 | ||
| 755 | 755 | if (buffer instanceof Buffer) { | |
| 756 | - // if no position is passed then assume null | ||
| 757 | - if (typeof position === 'function') { | ||
| 758 | - callback = position; | ||
| 756 | + callback = maybeCallback(callback || position || length || offset); | ||
| 757 | + if (typeof offset !== 'number') { | ||
| 758 | + offset = 0; | ||
| 759 | + } | ||
| 760 | + if (typeof length !== 'number') { | ||
| 761 | + length = buffer.length - offset; | ||
| 762 | + } | ||
| 763 | + if (typeof position !== 'number') { | ||
| 759 | 764 | position = null; | |
| 760 | 765 | } | |
| 761 | - callback = maybeCallback(callback); | ||
| 762 | 766 | return binding.writeBuffer(fd, buffer, offset, length, position, req); | |
| 763 | 767 | } | |
| 764 | 768 | ||
@@ -778,13 +782,17 @@ fs.write = function(fd, buffer, offset, length, position, callback) { | |||
| 778 | 782 | }; | |
| 779 | 783 | ||
| 780 | 784 | // usage: | |
| 781 | - // fs.writeSync(fd, buffer, offset, length[, position]); | ||
| 785 | + // fs.writeSync(fd, buffer[, offset[, length[, position]]]); | ||
| 782 | 786 | // OR | |
| 783 | 787 | // fs.writeSync(fd, string[, position[, encoding]]); | |
| 784 | 788 | fs.writeSync = function(fd, buffer, offset, length, position) { | |
| 785 | 789 | if (buffer instanceof Buffer) { | |
| 786 | 790 | if (position === undefined) | |
| 787 | 791 | position = null; | |
| 792 | + if (typeof offset !== 'number') | ||
| 793 | + offset = 0; | ||
| 794 | + if (typeof length !== 'number') | ||
| 795 | + length = buffer.length - offset; | ||
| 788 | 796 | return binding.writeBuffer(fd, buffer, offset, length, position); | |
| 789 | 797 | } | |
| 790 | 798 | if (typeof buffer !== 'string') | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,29 +2,107 @@ | |||
| 2 | 2 | const common = require('../common'); | |
| 3 | 3 | const assert = require('assert'); | |
| 4 | 4 | const path = require('path'); | |
| 5 | - const Buffer = require('buffer').Buffer; | ||
| 6 | 5 | const fs = require('fs'); | |
| 7 | - const filename = path.join(common.tmpDir, 'write.txt'); | ||
| 8 | 6 | const expected = Buffer.from('hello'); | |
| 9 | 7 | ||
| 10 | 8 | common.refreshTmpDir(); | |
| 11 | 9 | ||
| 12 | - fs.open(filename, 'w', 0o644, common.mustCall(function(err, fd) { | ||
| 13 | - if (err) throw err; | ||
| 14 | - | ||
| 15 | - fs.write(fd, | ||
| 16 | - expected, | ||
| 17 | - 0, | ||
| 18 | - expected.length, | ||
| 19 | - null, | ||
| 20 | - common.mustCall(function(err, written) { | ||
| 21 | - if (err) throw err; | ||
| 22 | - | ||
| 23 | - assert.strictEqual(expected.length, written); | ||
| 24 | - fs.closeSync(fd); | ||
| 25 | - | ||
| 26 | - const found = fs.readFileSync(filename, 'utf8'); | ||
| 27 | - assert.deepStrictEqual(expected.toString(), found); | ||
| 28 | - fs.unlinkSync(filename); | ||
| 29 | - })); | ||
| 30 | - })); | ||
| 10 | + // fs.write with all parameters provided: | ||
| 11 | + { | ||
| 12 | + const filename = path.join(common.tmpDir, 'write1.txt'); | ||
| 13 | + fs.open(filename, 'w', 0o644, common.mustCall(function(err, fd) { | ||
| 14 | + assert.ifError(err); | ||
| 15 | + | ||
| 16 | + const cb = common.mustCall(function(err, written) { | ||
| 17 | + assert.ifError(err); | ||
| 18 | + | ||
| 19 | + assert.strictEqual(expected.length, written); | ||
| 20 | + fs.closeSync(fd); | ||
| 21 | + | ||
| 22 | + const found = fs.readFileSync(filename, 'utf8'); | ||
| 23 | + assert.deepStrictEqual(expected.toString(), found); | ||
| 24 | + }); | ||
| 25 | + | ||
| 26 | + fs.write(fd, expected, 0, expected.length, null, cb); | ||
| 27 | + })); | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + // fs.write with a buffer, without the length parameter: | ||
| 31 | + { | ||
| 32 | + const filename = path.join(common.tmpDir, 'write2.txt'); | ||
| 33 | + fs.open(filename, 'w', 0o644, common.mustCall(function(err, fd) { | ||
| 34 | + assert.ifError(err); | ||
| 35 | + | ||
| 36 | + const cb = common.mustCall(function(err, written) { | ||
| 37 | + assert.ifError(err); | ||
| 38 | + | ||
| 39 | + assert.strictEqual(2, written); | ||
| 40 | + fs.closeSync(fd); | ||
| 41 | + | ||
| 42 | + const found = fs.readFileSync(filename, 'utf8'); | ||
| 43 | + assert.deepStrictEqual('lo', found); | ||
| 44 | + }); | ||
| 45 | + | ||
| 46 | + fs.write(fd, Buffer.from('hello'), 3, cb); | ||
| 47 | + })); | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + // fs.write with a buffer, without the offset and length parameters: | ||
| 51 | + { | ||
| 52 | + const filename = path.join(common.tmpDir, 'write3.txt'); | ||
| 53 | + fs.open(filename, 'w', 0o644, common.mustCall(function(err, fd) { | ||
| 54 | + assert.ifError(err); | ||
| 55 | + | ||
| 56 | + const cb = common.mustCall(function(err, written) { | ||
| 57 | + assert.ifError(err); | ||
| 58 | + | ||
| 59 | + assert.strictEqual(expected.length, written); | ||
| 60 | + fs.closeSync(fd); | ||
| 61 | + | ||
| 62 | + const found = fs.readFileSync(filename, 'utf8'); | ||
| 63 | + assert.deepStrictEqual(expected.toString(), found); | ||
| 64 | + }); | ||
| 65 | + | ||
| 66 | + fs.write(fd, expected, cb); | ||
| 67 | + })); | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + // fs.write with the offset passed as undefined followed by the callback: | ||
| 71 | + { | ||
| 72 | + const filename = path.join(common.tmpDir, 'write4.txt'); | ||
| 73 | + fs.open(filename, 'w', 0o644, common.mustCall(function(err, fd) { | ||
| 74 | + assert.ifError(err); | ||
| 75 | + | ||
| 76 | + const cb = common.mustCall(function(err, written) { | ||
| 77 | + assert.ifError(err); | ||
| 78 | + | ||
| 79 | + assert.strictEqual(expected.length, written); | ||
| 80 | + fs.closeSync(fd); | ||
| 81 | + | ||
| 82 | + const found = fs.readFileSync(filename, 'utf8'); | ||
| 83 | + assert.deepStrictEqual(expected.toString(), found); | ||
| 84 | + }); | ||
| 85 | + | ||
| 86 | + fs.write(fd, expected, undefined, cb); | ||
| 87 | + })); | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + // fs.write with offset and length passed as undefined followed by the callback: | ||
| 91 | + { | ||
| 92 | + const filename = path.join(common.tmpDir, 'write5.txt'); | ||
| 93 | + fs.open(filename, 'w', 0o644, common.mustCall(function(err, fd) { | ||
| 94 | + assert.ifError(err); | ||
| 95 | + | ||
| 96 | + const cb = common.mustCall(function(err, written) { | ||
| 97 | + assert.ifError(err); | ||
| 98 | + | ||
| 99 | + assert.strictEqual(expected.length, written); | ||
| 100 | + fs.closeSync(fd); | ||
| 101 | + | ||
| 102 | + const found = fs.readFileSync(filename, 'utf8'); | ||
| 103 | + assert.deepStrictEqual(expected.toString(), found); | ||
| 104 | + }); | ||
| 105 | + | ||
| 106 | + fs.write(fd, expected, undefined, undefined, cb); | ||
| 107 | + })); | ||
| 108 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,21 +3,54 @@ const common = require('../common'); | |||
| 3 | 3 | const assert = require('assert'); | |
| 4 | 4 | const path = require('path'); | |
| 5 | 5 | const fs = require('fs'); | |
| 6 | - const fn = path.join(common.tmpDir, 'write.txt'); | ||
| 6 | + const filename = path.join(common.tmpDir, 'write.txt'); | ||
| 7 | 7 | ||
| 8 | 8 | common.refreshTmpDir(); | |
| 9 | 9 | ||
| 10 | - const foo = 'foo'; | ||
| 11 | - const fd = fs.openSync(fn, 'w'); | ||
| 10 | + // fs.writeSync with all parameters provided: | ||
| 11 | + { | ||
| 12 | + const fd = fs.openSync(filename, 'w'); | ||
| 12 | 13 | ||
| 13 | - let written = fs.writeSync(fd, ''); | ||
| 14 | - assert.strictEqual(0, written); | ||
| 14 | + let written = fs.writeSync(fd, ''); | ||
| 15 | + assert.strictEqual(0, written); | ||
| 15 | 16 | ||
| 16 | - fs.writeSync(fd, foo); | ||
| 17 | + fs.writeSync(fd, 'foo'); | ||
| 17 | 18 | ||
| 18 | - const bar = 'bár'; | ||
| 19 | - written = fs.writeSync(fd, Buffer.from(bar), 0, Buffer.byteLength(bar)); | ||
| 20 | - assert.ok(written > 3); | ||
| 21 | - fs.closeSync(fd); | ||
| 19 | + written = fs.writeSync(fd, Buffer.from('bár'), 0, Buffer.byteLength('bár')); | ||
| 20 | + assert.ok(written > 3); | ||
| 21 | + fs.closeSync(fd); | ||
| 22 | 22 | ||
| 23 | - assert.strictEqual(fs.readFileSync(fn, 'utf8'), 'foobár'); | ||
| 23 | + assert.strictEqual(fs.readFileSync(filename, 'utf-8'), 'foobár'); | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + // fs.writeSync with a buffer, without the length parameter: | ||
| 27 | + { | ||
| 28 | + const fd = fs.openSync(filename, 'w'); | ||
| 29 | + | ||
| 30 | + let written = fs.writeSync(fd, ''); | ||
| 31 | + assert.strictEqual(0, written); | ||
| 32 | + | ||
| 33 | + fs.writeSync(fd, 'foo'); | ||
| 34 | + | ||
| 35 | + written = fs.writeSync(fd, Buffer.from('bár'), 0); | ||
| 36 | + assert.ok(written > 3); | ||
| 37 | + fs.closeSync(fd); | ||
| 38 | + | ||
| 39 | + assert.strictEqual(fs.readFileSync(filename, 'utf-8'), 'foobár'); | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + // fs.writeSync with a buffer, without the offset and length parameters: | ||
| 43 | + { | ||
| 44 | + const fd = fs.openSync(filename, 'w'); | ||
| 45 | + | ||
| 46 | + let written = fs.writeSync(fd, ''); | ||
| 47 | + assert.strictEqual(0, written); | ||
| 48 | + | ||
| 49 | + fs.writeSync(fd, 'foo'); | ||
| 50 | + | ||
| 51 | + written = fs.writeSync(fd, Buffer.from('bár')); | ||
| 52 | + assert.ok(written > 3); | ||
| 53 | + fs.closeSync(fd); | ||
| 54 | + | ||
| 55 | + assert.strictEqual(fs.readFileSync(filename, 'utf-8'), 'foobár'); | ||
| 56 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments