Skip to content

Commit

Permalink
Avoid use of Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilliamson committed May 28, 2023
1 parent 1ce7c08 commit b2b2c35
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 110 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"exports": true,
"module": true,
"require": false,
"TextDecoder": false,
"Uint8Array": false
}
}
20 changes: 15 additions & 5 deletions lib/documents.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,22 @@ function noteKey(noteType, id) {
function Image(options) {
return {
type: types.image,
read: options.readImage,
readAsText: function(encoding) {
if (!encoding) {
throw new Error("Missing encoding argument");
// `read` is retained for backwards compatibility, but other read
// methods should be preferred.
read: function(encoding) {
if (encoding) {
return options.readImage(encoding);
} else {
return options.readImage().then(function(arrayBuffer) {
return Buffer.from(arrayBuffer);
});
}
return options.readImage(encoding);
},
readAsArrayBuffer: function() {
return options.readImage();
},
readAsBase64String: function() {
return options.readImage("base64");
},
altText: options.altText,
contentType: options.contentType
Expand Down
2 changes: 1 addition & 1 deletion lib/images.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function imgElement(func) {
exports.inline = exports.imgElement;

exports.dataUri = imgElement(function(element) {
return element.readAsText("base64").then(function(imageBuffer) {
return element.readAsBase64String().then(function(imageBuffer) {
return {
src: "data:" + element.contentType + ";base64," + imageBuffer
};
Expand Down
2 changes: 1 addition & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ interface ImageConverter {

interface Image {
contentType: string;
readAsText: (encoding: string) => Promise<string>;
readAsBase64String: () => Promise<string>;
read: ImageRead;
}

Expand Down
19 changes: 7 additions & 12 deletions lib/zipfile.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var base64js = require("base64-js");
var JSZip = require("jszip");

exports.openArrayBuffer = openArrayBuffer;
Expand All @@ -12,11 +13,13 @@ function openArrayBuffer(arrayBuffer) {

function read(name, encoding) {
return zipFile.file(name).async("uint8array").then(function(array) {
var buffer = uint8ArrayToBuffer(array);
if (encoding) {
return buffer.toString(encoding);
if (encoding === "base64") {
return base64js.fromByteArray(array);
} else if (encoding) {
var decoder = new TextDecoder(encoding);
return decoder.decode(array);
} else {
return buffer;
return array;
}
});
}
Expand All @@ -38,14 +41,6 @@ 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) {
Expand Down
138 changes: 54 additions & 84 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"dependencies": {
"@xmldom/xmldom": "^0.8.6",
"argparse": "~1.0.3",
"base64-js": "^1.5.1",
"bluebird": "~3.4.0",
"dingbat-to-unicode": "^1.0.1",
"jszip": "^3.7.1",
Expand All @@ -32,7 +33,7 @@
"browserify-prepend-licenses": "~1.0.0",
"duck": "^0.1.12",
"eslint": "2.13.1",
"hamjest": "2.13.0",
"hamjest": "^4.0.1",
"mocha": "~2.2.5",
"temp": "^0.9.4",
"uglify-js": "~2.4.8"
Expand All @@ -50,8 +51,8 @@
"prepare": "make mammoth.browser.min.js"
},
"license": "BSD-2-Clause",
"engines" : {
"node" : ">=12.0.0"
"engines": {
"node": ">=12.0.0"
},
"volta": {
"node": "16.18.1",
Expand Down
Loading

0 comments on commit b2b2c35

Please sign in to comment.