Skip to content

Commit 151a2b8

Browse files
committed
Handle 2 nodejs deprecations
- [DEP0005] is a documentation-only deprecation about buffers - [DEP0013] displayed warnings in the tests [DEP0005]: https://nodejs.org/api/deprecations.html#deprecations_dep0005_buffer_constructor [DEP0013]: https://nodejs.org/api/deprecations.html#deprecations_dep0013_fs_async_function_without_callback
1 parent a8e6831 commit 151a2b8

File tree

4 files changed

+50
-16
lines changed

4 files changed

+50
-16
lines changed

lib/nodejsUtils.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,32 @@ module.exports = {
88
*/
99
isNode : typeof Buffer !== "undefined",
1010
/**
11-
* Create a new nodejs Buffer.
11+
* Create a new nodejs Buffer from an existing content.
1212
* @param {Object} data the data to pass to the constructor.
1313
* @param {String} encoding the encoding to use.
1414
* @return {Buffer} a new Buffer.
1515
*/
16-
newBuffer : function(data, encoding){
16+
newBufferFrom: function(data, encoding) {
17+
// XXX We can't use `Buffer.from` which comes from `Uint8Array.from`
18+
// in nodejs v4 (< v.4.5). It's not the expected implementation (and
19+
// has a different signature).
20+
// see https://github.com/nodejs/node/issues/8053
21+
// A condition on nodejs' version won't solve the issue as we don't
22+
// control the Buffer polyfills that may or may not be used.
1723
return new Buffer(data, encoding);
1824
},
25+
/**
26+
* Create a new nodejs Buffer with the specified size.
27+
* @param {Integer} size the size of the buffer.
28+
* @return {Buffer} a new Buffer.
29+
*/
30+
allocBuffer: function (size) {
31+
if (Buffer.alloc) {
32+
return Buffer.alloc(size);
33+
} else {
34+
return new Buffer(size);
35+
}
36+
},
1937
/**
2038
* Find out if an object is a Buffer.
2139
* @param {Object} b the object to test.

lib/utf8.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ var buf2string = function (buf) {
168168
*/
169169
exports.utf8encode = function utf8encode(str) {
170170
if (support.nodebuffer) {
171-
return nodejsUtils.newBuffer(str, "utf-8");
171+
return nodejsUtils.newBufferFrom(str, "utf-8");
172172
}
173173

174174
return string2buf(str);

lib/utils.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ var arrayToStringHelper = {
145145
*/
146146
nodebuffer : (function () {
147147
try {
148-
return support.nodebuffer && String.fromCharCode.apply(null, nodejsUtils.newBuffer(1)).length === 1;
148+
return support.nodebuffer && String.fromCharCode.apply(null, nodejsUtils.allocBuffer(1)).length === 1;
149149
} catch (e) {
150150
return false;
151151
}
@@ -225,7 +225,7 @@ transform["string"] = {
225225
return stringToArrayLike(input, new Uint8Array(input.length));
226226
},
227227
"nodebuffer": function(input) {
228-
return stringToArrayLike(input, nodejsUtils.newBuffer(input.length));
228+
return stringToArrayLike(input, nodejsUtils.allocBuffer(input.length));
229229
}
230230
};
231231

@@ -240,7 +240,7 @@ transform["array"] = {
240240
return new Uint8Array(input);
241241
},
242242
"nodebuffer": function(input) {
243-
return nodejsUtils.newBuffer(input);
243+
return nodejsUtils.newBufferFrom(input);
244244
}
245245
};
246246

@@ -257,7 +257,7 @@ transform["arraybuffer"] = {
257257
return new Uint8Array(input);
258258
},
259259
"nodebuffer": function(input) {
260-
return nodejsUtils.newBuffer(new Uint8Array(input));
260+
return nodejsUtils.newBufferFrom(new Uint8Array(input));
261261
}
262262
};
263263

@@ -278,7 +278,7 @@ transform["uint8array"] = {
278278
},
279279
"uint8array": identity,
280280
"nodebuffer": function(input) {
281-
return nodejsUtils.newBuffer(input);
281+
return nodejsUtils.newBufferFrom(input);
282282
}
283283
};
284284

test/asserts/stream.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,22 @@ QUnit.module("stream", function () {
6868
fs.readFile(tempFile, function (e, data) {
6969
var actual = JSZipTestUtils.toString(data);
7070
ok(JSZipTestUtils.similar(actual, expected, 3 * JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "generated ZIP matches reference ZIP");
71-
start();
72-
fs.unlink(tempFile);
71+
fs.unlink(tempFile, function (err) {
72+
if (err) {
73+
ok(false, err);
74+
}
75+
start();
76+
});
7377
});
7478
})
7579
.on("error", function (e) {
7680
ok(false, e.message);
77-
start();
78-
fs.unlink(tempFile);
81+
fs.unlink(tempFile, function (err) {
82+
if (err) {
83+
ok(false, err);
84+
}
85+
start();
86+
});
7987
});
8088
});
8189
}
@@ -88,14 +96,22 @@ QUnit.module("stream", function () {
8896
fs.readFile(tempFile, function (e, data) {
8997
var actual = JSZipTestUtils.toString(data);
9098
equal(actual, "Hello World\n", "the generated content is ok");
91-
done();
92-
fs.unlink(tempFile);
99+
fs.unlink(tempFile, function (err) {
100+
if (err) {
101+
assert.ok(false, err);
102+
}
103+
done();
104+
});
93105
});
94106
})
95107
.on("error", function (e) {
96108
ok(false, e.message);
97-
done();
98-
fs.unlink(tempFile);
109+
fs.unlink(tempFile, function (err) {
110+
if (err) {
111+
assert.ok(false, err);
112+
}
113+
done();
114+
});
99115
});
100116
});
101117
}

0 commit comments

Comments
 (0)