| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 2685464 commit 3c2a936
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -555,40 +555,7 @@ fs.openSync = function(path, flags, mode) { | |||
| 555 | 555 | return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode); | |
| 556 | 556 | }; | |
| 557 | 557 | ||
| 558 | - var readWarned = false; | ||
| 559 | 558 | fs.read = function(fd, buffer, offset, length, position, callback) { | |
| 560 | - if (!isUint8Array(buffer)) { | ||
| 561 | - // legacy string interface (fd, length, position, encoding, callback) | ||
| 562 | - if (!readWarned) { | ||
| 563 | - readWarned = true; | ||
| 564 | - process.emitWarning( | ||
| 565 | - 'fs.read\'s legacy String interface is deprecated. Use the Buffer ' + | ||
| 566 | - 'API as mentioned in the documentation instead.', | ||
| 567 | - 'DeprecationWarning'); | ||
| 568 | - } | ||
| 569 | - | ||
| 570 | - const cb = arguments[4]; | ||
| 571 | - const encoding = arguments[3]; | ||
| 572 | - | ||
| 573 | - assertEncoding(encoding); | ||
| 574 | - | ||
| 575 | - position = arguments[2]; | ||
| 576 | - length = arguments[1]; | ||
| 577 | - buffer = Buffer.allocUnsafe(length); | ||
| 578 | - offset = 0; | ||
| 579 | - | ||
| 580 | - callback = function(err, bytesRead) { | ||
| 581 | - if (!cb) return; | ||
| 582 | - if (err) return cb(err); | ||
| 583 | - | ||
| 584 | - if (bytesRead > 0) { | ||
| 585 | - tryToStringWithEnd(buffer, encoding, bytesRead, cb); | ||
| 586 | - } else { | ||
| 587 | - (cb)(err, '', bytesRead); | ||
| 588 | - } | ||
| 589 | - }; | ||
| 590 | - } | ||
| 591 | - | ||
| 592 | 559 | if (length === 0) { | |
| 593 | 560 | return process.nextTick(function() { | |
| 594 | 561 | callback && callback(null, 0, buffer); | |
@@ -606,57 +573,12 @@ fs.read = function(fd, buffer, offset, length, position, callback) { | |||
| 606 | 573 | binding.read(fd, buffer, offset, length, position, req); | |
| 607 | 574 | }; | |
| 608 | 575 | ||
| 609 | - function tryToStringWithEnd(buf, encoding, end, callback) { | ||
| 610 | - var e; | ||
| 611 | - try { | ||
| 612 | - buf = buf.toString(encoding, 0, end); | ||
| 613 | - } catch (err) { | ||
| 614 | - e = err; | ||
| 615 | - } | ||
| 616 | - callback(e, buf, end); | ||
| 617 | - } | ||
| 618 | - | ||
| 619 | - var readSyncWarned = false; | ||
| 620 | 576 | fs.readSync = function(fd, buffer, offset, length, position) { | |
| 621 | - var legacy = false; | ||
| 622 | - var encoding; | ||
| 623 | - | ||
| 624 | - if (!isUint8Array(buffer)) { | ||
| 625 | - // legacy string interface (fd, length, position, encoding, callback) | ||
| 626 | - if (!readSyncWarned) { | ||
| 627 | - readSyncWarned = true; | ||
| 628 | - process.emitWarning( | ||
| 629 | - 'fs.readSync\'s legacy String interface is deprecated. Use the ' + | ||
| 630 | - 'Buffer API as mentioned in the documentation instead.', | ||
| 631 | - 'DeprecationWarning'); | ||
| 632 | - } | ||
| 633 | - legacy = true; | ||
| 634 | - encoding = arguments[3]; | ||
| 635 | - | ||
| 636 | - assertEncoding(encoding); | ||
| 637 | - | ||
| 638 | - position = arguments[2]; | ||
| 639 | - length = arguments[1]; | ||
| 640 | - buffer = Buffer.allocUnsafe(length); | ||
| 641 | - | ||
| 642 | - offset = 0; | ||
| 643 | - } | ||
| 644 | - | ||
| 645 | 577 | if (length === 0) { | |
| 646 | - if (legacy) { | ||
| 647 | - return ['', 0]; | ||
| 648 | - } else { | ||
| 649 | - return 0; | ||
| 650 | - } | ||
| 651 | - } | ||
| 652 | - | ||
| 653 | - var r = binding.read(fd, buffer, offset, length, position); | ||
| 654 | - if (!legacy) { | ||
| 655 | - return r; | ||
| 578 | + return 0; | ||
| 656 | 579 | } | |
| 657 | 580 | ||
| 658 | - var str = (r > 0) ? buffer.toString(encoding, 0, r) : ''; | ||
| 659 | - return [str, r]; | ||
| 581 | + return binding.read(fd, buffer, offset, length, position); | ||
| 660 | 582 | }; | |
| 661 | 583 | ||
| 662 | 584 | // usage: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,21 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + const path = require('path'); | ||
| 5 | + const fs = require('fs'); | ||
| 6 | + const filepath = path.join(common.fixturesDir, 'x.txt'); | ||
| 7 | + const fd = fs.openSync(filepath, 'r'); | ||
| 8 | + const expected = 'xyz\n'; | ||
| 9 | + | ||
| 10 | + // Error must be thrown with string | ||
| 11 | + assert.throws(() => { | ||
| 12 | + fs.read(fd, | ||
| 13 | + expected.length, | ||
| 14 | + 0, | ||
| 15 | + 'utf-8', | ||
| 16 | + () => {}); | ||
| 17 | + }, /Second argument needs to be a buffer/); | ||
| 18 | + | ||
| 19 | + assert.throws(() => { | ||
| 20 | + fs.readSync(fd, expected.length, 0, 'utf-8'); | ||
| 21 | + }, /Second argument needs to be a buffer/); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,17 +2,18 @@ | |||
| 2 | 2 | const common = require('../common'); | |
| 3 | 3 | const assert = require('assert'); | |
| 4 | 4 | const path = require('path'); | |
| 5 | + const Buffer = require('buffer').Buffer; | ||
| 5 | 6 | const fs = require('fs'); | |
| 6 | 7 | const filepath = path.join(common.fixturesDir, 'x.txt'); | |
| 7 | 8 | const fd = fs.openSync(filepath, 'r'); | |
| 8 | - const expected = ''; | ||
| 9 | + const bufferAsync = Buffer.alloc(0); | ||
| 10 | + const bufferSync = Buffer.alloc(0); | ||
| 9 | 11 | ||
| 10 | - fs.read(fd, 0, 0, 'utf-8', common.mustCall(function(err, str, bytesRead) { | ||
| 11 | - assert.ok(!err); | ||
| 12 | - assert.equal(str, expected); | ||
| 12 | + fs.read(fd, bufferAsync, 0, 0, 0, common.mustCall(function(err, bytesRead) { | ||
| 13 | 13 | assert.equal(bytesRead, 0); | |
| 14 | + assert.deepStrictEqual(bufferAsync, Buffer.alloc(0)); | ||
| 14 | 15 | })); | |
| 15 | 16 | ||
| 16 | - const r = fs.readSync(fd, 0, 0, 'utf-8'); | ||
| 17 | - assert.equal(r[0], expected); | ||
| 18 | - assert.equal(r[1], 0); | ||
| 17 | + const r = fs.readSync(fd, bufferSync, 0, 0, 0); | ||
| 18 | + assert.deepStrictEqual(bufferSync, Buffer.alloc(0)); | ||
| 19 | + assert.equal(r, 0); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,21 +2,34 @@ | |||
| 2 | 2 | const common = require('../common'); | |
| 3 | 3 | const assert = require('assert'); | |
| 4 | 4 | const path = require('path'); | |
| 5 | + const Buffer = require('buffer').Buffer; | ||
| 5 | 6 | const fs = require('fs'); | |
| 6 | 7 | const filepath = path.join(common.fixturesDir, 'x.txt'); | |
| 7 | 8 | const fd = fs.openSync(filepath, 'r'); | |
| 8 | - const expected = 'xyz\n'; | ||
| 9 | 9 | ||
| 10 | - fs.read(fd, | ||
| 11 | - expected.length, | ||
| 12 | - 0, | ||
| 13 | - 'utf-8', | ||
| 14 | - common.mustCall((err, str, bytesRead) => { | ||
| 15 | - assert.ifError(err); | ||
| 16 | - assert.strictEqual(str, expected); | ||
| 17 | - assert.strictEqual(bytesRead, expected.length); | ||
| 18 | - })); | ||
| 10 | + const expected = Buffer.from('xyz\n'); | ||
| 19 | 11 | ||
| 20 | - var r = fs.readSync(fd, expected.length, 0, 'utf-8'); | ||
| 21 | - assert.strictEqual(r[0], expected); | ||
| 22 | - assert.strictEqual(r[1], expected.length); | ||
| 12 | + function test(bufferAsync, bufferSync, expected) { | ||
| 13 | + fs.read(fd, | ||
| 14 | + bufferAsync, | ||
| 15 | + 0, | ||
| 16 | + expected.length, | ||
| 17 | + 0, | ||
| 18 | + common.mustCall((err, bytesRead) => { | ||
| 19 | + assert.ifError(err); | ||
| 20 | + assert.strictEqual(bytesRead, expected.length); | ||
| 21 | + assert.deepStrictEqual(bufferAsync, Buffer.from(expected)); | ||
| 22 | + })); | ||
| 23 | + | ||
| 24 | + const r = fs.readSync(fd, bufferSync, 0, expected.length, 0); | ||
| 25 | + assert.deepStrictEqual(bufferSync, Buffer.from(expected)); | ||
| 26 | + assert.strictEqual(r, expected.length); | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + test(Buffer.allocUnsafe(expected.length), | ||
| 30 | + Buffer.allocUnsafe(expected.length), | ||
| 31 | + expected); | ||
| 32 | + | ||
| 33 | + test(new Uint8Array(expected.length), | ||
| 34 | + new Uint8Array(expected.length), | ||
| 35 | + Uint8Array.from(expected)); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments