Skip to content

Commit 4a9d5ab

Browse files
authored
Assume Buffer.from/alloc exist under node (emscripten-core#14447)
I don't think there is any need to continue to support node version older than v5.10.0 which was when Buffer.from was added. If we do want to support this we should do it via a polyfill that is enabled under LEGACY_VM_SUPPORT. I noticed this issue because lz4-compress.js was generating a deprecation warning about this method. See: https://nodejs.org/api/buffer.html#buffer_static_method_buffer_alloc_size_fill_encoding And: https://nodejs.org/api/buffer.html#buffer_static_method_buffer_from_array
1 parent 9082822 commit 4a9d5ab

9 files changed

Lines changed: 31 additions & 31 deletions

File tree

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ See docs/process.md for more on how version tagging works.
2020

2121
2.0.25
2222
------
23+
- Drop support for node versions older than v5.10.0. We now assume the
24+
existence of `Buffer.from` which was added in v5.10.0. If it turns out
25+
there is still a need to support these older node versions we can
26+
add a polyfil under LEGACY_VM_SUPPORT (#14447).
2327

2428
2.0.24 - 06/10/2021
2529
-------------------

src/base64Decode.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,7 @@ base64ReverseLookup[47] = 63; // '/'
3838
function base64Decode(b64) {
3939
#if ENVIRONMENT_MAY_BE_NODE
4040
if (typeof ENVIRONMENT_IS_NODE !== 'undefined' && ENVIRONMENT_IS_NODE) {
41-
try {
42-
var buf = Buffer.from(b64, 'base64');
43-
} catch (_) {
44-
var buf = new Buffer(b64, 'base64');
45-
}
41+
var buf = Buffer.from(b64, 'base64');
4642
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
4743
}
4844
#endif

src/base64Utils.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,7 @@ var decodeBase64 = typeof atob === 'function' ? atob : function (input) {
4444
function intArrayFromBase64(s) {
4545
#if ENVIRONMENT_MAY_BE_NODE
4646
if (typeof ENVIRONMENT_IS_NODE === 'boolean' && ENVIRONMENT_IS_NODE) {
47-
var buf;
48-
try {
49-
// TODO: Update Node.js externs, Closure does not recognize the following Buffer.from()
50-
/**@suppress{checkTypes}*/
51-
buf = Buffer.from(s, 'base64');
52-
} catch (_) {
53-
buf = new Buffer(s, 'base64');
54-
}
47+
var buf = Buffer.from(s, 'base64');
5548
return new Uint8Array(buf['buffer'], buf['byteOffset'], buf['byteLength']);
5649
}
5750
#endif

src/library_nodefs.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ mergeInto(LibraryManager.library, {
2828
"{{{ cDefine('O_WRONLY') }}}": flags["O_WRONLY"]
2929
};
3030
},
31-
bufferFrom: function (arrayBuffer) {
32-
// Node.js < 4.5 compatibility: Buffer.from does not support ArrayBuffer
33-
// Buffer.from before 4.5 was just a method inherited from Uint8Array
34-
// Buffer.alloc has been added with Buffer.from together, so check it instead
35-
return Buffer["alloc"] ? Buffer.from(arrayBuffer) : new Buffer(arrayBuffer);
36-
},
3731
convertNodeCode: function(e) {
3832
var code = e.code;
3933
#if ASSERTIONS
@@ -262,14 +256,14 @@ mergeInto(LibraryManager.library, {
262256
// Node.js < 6 compatibility: node errors on 0 length reads
263257
if (length === 0) return 0;
264258
try {
265-
return fs.readSync(stream.nfd, NODEFS.bufferFrom(buffer.buffer), offset, length, position);
259+
return fs.readSync(stream.nfd, Buffer.from(buffer.buffer), offset, length, position);
266260
} catch (e) {
267261
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
268262
}
269263
},
270264
write: function (stream, buffer, offset, length, position) {
271265
try {
272-
return fs.writeSync(stream.nfd, NODEFS.bufferFrom(buffer.buffer), offset, length, position);
266+
return fs.writeSync(stream.nfd, Buffer.from(buffer.buffer), offset, length, position);
273267
} catch (e) {
274268
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
275269
}

src/library_noderawfs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ mergeInto(LibraryManager.library, {
9898
}
9999
var seeking = typeof position !== 'undefined';
100100
if (!seeking && stream.seekable) position = stream.position;
101-
var bytesRead = fs.readSync(stream.nfd, NODEFS.bufferFrom(buffer.buffer), offset, length, position);
101+
var bytesRead = fs.readSync(stream.nfd, Buffer.from(buffer.buffer), offset, length, position);
102102
// update position marker when non-seeking
103103
if (!seeking) stream.position += bytesRead;
104104
return bytesRead;
@@ -114,7 +114,7 @@ mergeInto(LibraryManager.library, {
114114
}
115115
var seeking = typeof position !== 'undefined';
116116
if (!seeking && stream.seekable) position = stream.position;
117-
var bytesWritten = fs.writeSync(stream.nfd, NODEFS.bufferFrom(buffer.buffer), offset, length, position);
117+
var bytesWritten = fs.writeSync(stream.nfd, Buffer.from(buffer.buffer), offset, length, position);
118118
// update position marker when non-seeking
119119
if (!seeking) stream.position += bytesWritten;
120120
return bytesWritten;

src/library_tty.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ mergeInto(LibraryManager.library, {
108108
if (ENVIRONMENT_IS_NODE) {
109109
// we will read data by chunks of BUFSIZE
110110
var BUFSIZE = 256;
111-
var buf = Buffer.alloc ? Buffer.alloc(BUFSIZE) : new Buffer(BUFSIZE);
111+
var buf = Buffer.alloc(BUFSIZE);
112112
var bytesRead = 0;
113113

114114
try {

tests/module/test_stdin.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
#include <errno.h>
9+
#include <string.h>
910
#include <fcntl.h>
1011
#include <stdlib.h>
1112
#include <stdio.h>
@@ -16,8 +17,7 @@
1617

1718
int line = 0;
1819

19-
void main_loop()
20-
{
20+
void main_loop() {
2121
char str[10] = {0};
2222
int ret;
2323

@@ -33,8 +33,8 @@ void main_loop()
3333
}
3434

3535
int err = ferror(stdin);
36-
if (ferror(stdin) && errno != EAGAIN) {
37-
printf("error %d\n", err);
36+
if (err && errno != EAGAIN) {
37+
printf("error %s\n", strerror(errno));
3838
exit(EXIT_FAILURE);
3939
}
4040

@@ -47,8 +47,7 @@ void main_loop()
4747
}
4848
}
4949

50-
int main(int argc, char const *argv[])
51-
{
50+
int main(int argc, char const *argv[]) {
5251
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
5352

5453
// SM shell doesn't implement an event loop and therefor doesn't support

third_party/closure-compiler/node-externs/buffer.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,20 @@ nodeBuffer.SlowBuffer.prototype.slice = function(start, end) {};
375375
*/
376376
nodeBuffer.SlowBuffer.prototype.toString = function() {};
377377

378+
/**
379+
* @param {number} size
380+
* @param {(string|!Buffer|number)=} fill
381+
* @param {string=} encoding
382+
* @return {!Buffer}
383+
*/
384+
nodeBuffer.Buffer.alloc;
385+
386+
/**
387+
* @param {Array} aray
388+
* @return {!Buffer}
389+
*/
390+
nodeBuffer.Buffer.from;
391+
378392
//
379393
// Legacy
380394
//

tools/lz4-compress.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ if (!(data instanceof ArrayBuffer)) {
150150

151151
var start = Date.now();
152152
var compressedData = MiniLZ4.compressPackage(data);
153-
nodeFS['writeFileSync'](output, Buffer(compressedData['data']));
153+
nodeFS['writeFileSync'](output, Buffer.from(compressedData['data']));
154154
compressedData['data'] = null;
155155
printErr('compressed in ' + (Date.now() - start) + ' ms');
156156
print(JSON.stringify(compressedData));

0 commit comments

Comments
 (0)