summaryrefslogtreecommitdiff
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
parent0d125d27710b15a68e4ea5dbd7317d54e73a43cb (diff)
Use Buffer.from() when available
-rw-r--r--.eslintrc.json5
-rw-r--r--lib/zipfile.js24
2 files changed, 19 insertions, 10 deletions
diff --git a/.eslintrc.json b/.eslintrc.json
index 5e1f0e2..6de03b6 100644
--- a/.eslintrc.json
+++ b/.eslintrc.json
@@ -30,7 +30,7 @@
"no-unused-vars": ["error", {"args": "none"}],
"object-curly-spacing": ["error", "never"],
"radix": ["error", "always"],
-
+
"array-bracket-spacing": ["error", "never"],
"brace-style": ["error", "1tbs"],
"camelcase": ["error"],
@@ -69,6 +69,7 @@
"console": true,
"exports": true,
"module": true,
- "require": false
+ "require": false,
+ "Uint8Array": false
}
}
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("/");
}