aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-05-28 12:24:16 +0100
committerMichael Williamson <mike@zwobble.org>2023-05-28 12:24:16 +0100
commitb2b2c35715985f624e6b696fd87140fa6056318f (patch)
treea8015940e1d4b4bae1422af8e99277126e1df029 /lib
parent1ce7c085668fd978f4f343b41db7f76f33145de5 (diff)
Avoid use of Buffer
Diffstat (limited to 'lib')
-rw-r--r--lib/documents.js20
-rw-r--r--lib/images.js2
-rw-r--r--lib/index.d.ts2
-rw-r--r--lib/zipfile.js19
4 files changed, 24 insertions, 19 deletions
diff --git a/lib/documents.js b/lib/documents.js
index 26c0ce7..9088ed9 100644
--- a/lib/documents.js
+++ b/lib/documents.js
@@ -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
diff --git a/lib/images.js b/lib/images.js
index 07cfc2d..40aa378 100644
--- a/lib/images.js
+++ b/lib/images.js
@@ -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
};
diff --git a/lib/index.d.ts b/lib/index.d.ts
index 9666a9e..c73eeb5 100644
--- a/lib/index.d.ts
+++ b/lib/index.d.ts
@@ -39,7 +39,7 @@ interface ImageConverter {
interface Image {
contentType: string;
- readAsText: (encoding: string) => Promise<string>;
+ readAsBase64String: () => Promise<string>;
read: ImageRead;
}
diff --git a/lib/zipfile.js b/lib/zipfile.js
index d38a9a4..85dde81 100644
--- a/lib/zipfile.js
+++ b/lib/zipfile.js
@@ -1,3 +1,4 @@
+var base64js = require("base64-js");
var JSZip = require("jszip");
exports.openArrayBuffer = openArrayBuffer;
@@ -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;
}
});
}
@@ -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) {