Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle 2 nodejs deprecations #459

Merged
merged 1 commit into from
Sep 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions lib/nodejsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,32 @@ module.exports = {
*/
isNode : typeof Buffer !== "undefined",
/**
* Create a new nodejs Buffer.
* Create a new nodejs Buffer from an existing content.
* @param {Object} data the data to pass to the constructor.
* @param {String} encoding the encoding to use.
* @return {Buffer} a new Buffer.
*/
newBuffer : function(data, encoding){
newBufferFrom: function(data, encoding) {
// XXX We can't use `Buffer.from` which comes from `Uint8Array.from`
// in nodejs v4 (< v.4.5). It's not the expected implementation (and
// has a different signature).
// see /~https://github.com/nodejs/node/issues/8053
// A condition on nodejs' version won't solve the issue as we don't
// control the Buffer polyfills that may or may not be used.
return new Buffer(data, encoding);
},
/**
* Create a new nodejs Buffer with the specified size.
* @param {Integer} size the size of the buffer.
* @return {Buffer} a new Buffer.
*/
allocBuffer: function (size) {
if (Buffer.alloc) {
return Buffer.alloc(size);
} else {
return new Buffer(size);
}
},
/**
* Find out if an object is a Buffer.
* @param {Object} b the object to test.
Expand Down
2 changes: 1 addition & 1 deletion lib/utf8.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ var buf2string = function (buf) {
*/
exports.utf8encode = function utf8encode(str) {
if (support.nodebuffer) {
return nodejsUtils.newBuffer(str, "utf-8");
return nodejsUtils.newBufferFrom(str, "utf-8");
}

return string2buf(str);
Expand Down
10 changes: 5 additions & 5 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ var arrayToStringHelper = {
*/
nodebuffer : (function () {
try {
return support.nodebuffer && String.fromCharCode.apply(null, nodejsUtils.newBuffer(1)).length === 1;
return support.nodebuffer && String.fromCharCode.apply(null, nodejsUtils.allocBuffer(1)).length === 1;
} catch (e) {
return false;
}
Expand Down Expand Up @@ -225,7 +225,7 @@ transform["string"] = {
return stringToArrayLike(input, new Uint8Array(input.length));
},
"nodebuffer": function(input) {
return stringToArrayLike(input, nodejsUtils.newBuffer(input.length));
return stringToArrayLike(input, nodejsUtils.allocBuffer(input.length));
}
};

Expand All @@ -240,7 +240,7 @@ transform["array"] = {
return new Uint8Array(input);
},
"nodebuffer": function(input) {
return nodejsUtils.newBuffer(input);
return nodejsUtils.newBufferFrom(input);
}
};

Expand All @@ -257,7 +257,7 @@ transform["arraybuffer"] = {
return new Uint8Array(input);
},
"nodebuffer": function(input) {
return nodejsUtils.newBuffer(new Uint8Array(input));
return nodejsUtils.newBufferFrom(new Uint8Array(input));
}
};

Expand All @@ -278,7 +278,7 @@ transform["uint8array"] = {
},
"uint8array": identity,
"nodebuffer": function(input) {
return nodejsUtils.newBuffer(input);
return nodejsUtils.newBufferFrom(input);
}
};

Expand Down
32 changes: 24 additions & 8 deletions test/asserts/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,22 @@ QUnit.module("stream", function () {
fs.readFile(tempFile, function (e, data) {
var actual = JSZipTestUtils.toString(data);
ok(JSZipTestUtils.similar(actual, expected, 3 * JSZipTestUtils.MAX_BYTES_DIFFERENCE_PER_ZIP_ENTRY) , "generated ZIP matches reference ZIP");
start();
fs.unlink(tempFile);
fs.unlink(tempFile, function (err) {
if (err) {
ok(false, err);
}
start();
});
});
})
.on("error", function (e) {
ok(false, e.message);
start();
fs.unlink(tempFile);
fs.unlink(tempFile, function (err) {
if (err) {
ok(false, err);
}
start();
});
});
});
}
Expand All @@ -88,14 +96,22 @@ QUnit.module("stream", function () {
fs.readFile(tempFile, function (e, data) {
var actual = JSZipTestUtils.toString(data);
equal(actual, "Hello World\n", "the generated content is ok");
done();
fs.unlink(tempFile);
fs.unlink(tempFile, function (err) {
if (err) {
assert.ok(false, err);
}
done();
});
});
})
.on("error", function (e) {
ok(false, e.message);
done();
fs.unlink(tempFile);
fs.unlink(tempFile, function (err) {
if (err) {
assert.ok(false, err);
}
done();
});
});
});
}
Expand Down