Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3c2a936

Browse filesBrowse files
authored
fs: remove fs.read's string interface
It is a maintenance burden that was removed from the docs in 2010 (c93e0aa) and runtime-deprecated in v6.0 (1124de2). PR-URL: #9683 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 2685464 commit 3c2a936
Copy full SHA for 3c2a936

File tree

Expand file treeCollapse file tree

7 files changed

+57
-218
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

7 files changed

+57
-218
lines changed
Open diff view settings
Collapse file

‎lib/fs.js‎

Copy file name to clipboardExpand all lines: lib/fs.js
+2-80Lines changed: 2 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -555,40 +555,7 @@ fs.openSync = function(path, flags, mode) {
555555
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
556556
};
557557

558-
var readWarned = false;
559558
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-
592559
if (length === 0) {
593560
return process.nextTick(function() {
594561
callback && callback(null, 0, buffer);
@@ -606,57 +573,12 @@ fs.read = function(fd, buffer, offset, length, position, callback) {
606573
binding.read(fd, buffer, offset, length, position, req);
607574
};
608575

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;
620576
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-
645577
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;
656579
}
657580

658-
var str = (r > 0) ? buffer.toString(encoding, 0, r) : '';
659-
return [str, r];
581+
return binding.read(fd, buffer, offset, length, position);
660582
};
661583

662584
// usage:
Collapse file

‎test/parallel/test-fs-read-buffer-tostring-fail.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-fs-read-buffer-tostring-fail.js
-64Lines changed: 0 additions & 64 deletions
This file was deleted.
Collapse file

‎test/parallel/test-fs-read-buffer-zero-length.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-fs-read-buffer-zero-length.js
-19Lines changed: 0 additions & 19 deletions
This file was deleted.
Collapse file

‎test/parallel/test-fs-read-buffer.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-fs-read-buffer.js
-35Lines changed: 0 additions & 35 deletions
This file was deleted.
Collapse file

‎test/parallel/test-fs-read-type.js‎

Copy file name to clipboard
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff 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/);
Collapse file

‎test/parallel/test-fs-read-zero-length.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-fs-read-zero-length.js
+8-7Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22
const common = require('../common');
33
const assert = require('assert');
44
const path = require('path');
5+
const Buffer = require('buffer').Buffer;
56
const fs = require('fs');
67
const filepath = path.join(common.fixturesDir, 'x.txt');
78
const fd = fs.openSync(filepath, 'r');
8-
const expected = '';
9+
const bufferAsync = Buffer.alloc(0);
10+
const bufferSync = Buffer.alloc(0);
911

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) {
1313
assert.equal(bytesRead, 0);
14+
assert.deepStrictEqual(bufferAsync, Buffer.alloc(0));
1415
}));
1516

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);
Collapse file

‎test/parallel/test-fs-read.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-fs-read.js
+26-13Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,34 @@
22
const common = require('../common');
33
const assert = require('assert');
44
const path = require('path');
5+
const Buffer = require('buffer').Buffer;
56
const fs = require('fs');
67
const filepath = path.join(common.fixturesDir, 'x.txt');
78
const fd = fs.openSync(filepath, 'r');
8-
const expected = 'xyz\n';
99

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');
1911

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));

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.