aboutsummaryrefslogtreecommitdiff
path: root/lib/zipfile.js
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2019-08-13 21:38:07 +0100
committerMichael Williamson <mike@zwobble.org>2019-08-13 21:39:27 +0100
commite71a154c67325387f4df2a493abe2201273b07d6 (patch)
tree36f285ec34b514fa8b5ac6b5997ce97acf52c56e /lib/zipfile.js
parent0d125d27710b15a68e4ea5dbd7317d54e73a43cb (diff)
Use Buffer.from() when available
Diffstat (limited to 'lib/zipfile.js')
-rw-r--r--lib/zipfile.js24
1 files changed, 16 insertions, 8 deletions
diff --git a/lib/zipfile.js b/lib/zipfile.js
index 8d46d92..cae9567 100644
--- a/lib/zipfile.js
+++ b/lib/zipfile.js
@@ -11,25 +11,25 @@ function openArrayBuffer(arrayBuffer) {
function exists(name) {
return zipFile.file(name) !== null;
}
-
+
function read(name, encoding) {
var array = zipFile.file(name).asUint8Array();
- var buffer = new Buffer(array);
+ var buffer = uint8ArrayToBuffer(array);
if (encoding) {
return promises.when(buffer.toString(encoding));
} else {
return promises.when(buffer);
}
}
-
+
function write(name, contents) {
zipFile.file(name, contents);
}
-
+
function toBuffer() {
return zipFile.generate({type: "nodebuffer"});
}
-
+
return {
exists: exists,
read: read,
@@ -38,6 +38,14 @@ function openArrayBuffer(arrayBuffer) {
};
}
+function uint8ArrayToBuffer(array) {
+ if (Buffer.from && Buffer.from !== Uint8Array.from) {
+ return Buffer.from(array);
+ } else {
+ return new Buffer(array);
+ }
+}
+
function splitPath(path) {
var lastIndex = path.lastIndexOf("/");
if (lastIndex === -1) {
@@ -54,9 +62,9 @@ function joinPath() {
var nonEmptyPaths = Array.prototype.filter.call(arguments, function(path) {
return path;
});
-
+
var relevantPaths = [];
-
+
nonEmptyPaths.forEach(function(path) {
if (/^\//.test(path)) {
relevantPaths = [path];
@@ -64,6 +72,6 @@ function joinPath() {
relevantPaths.push(path);
}
});
-
+
return relevantPaths.join("/");
}