From 316587775aa73d0530826f343d27b5556393db36 Mon Sep 17 00:00:00 2001 From: TSUYUSATO Kitsune Date: Mon, 6 Mar 2023 01:09:25 +0900 Subject: [PATCH] Remove the global `Buffer` dependency The global `Buffer` is used for loading base64 encoded string. However, this **implicit** and **global** dependency makes some confusions and problems (#294, #305, and ruby/ruby.wasm#182). The use of `Buffer` here is limited, so we can easily replace this by using `atob` function. This simplifies user's build settings and reduces the file size since there is no need to include Buffer. --- lib.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib.ts b/lib.ts index 12dc66cb..4279e3e0 100644 --- a/lib.ts +++ b/lib.ts @@ -2,7 +2,7 @@ export * from "./pkg/wasmer_wasi_js"; import load from "./pkg/wasmer_wasi_js"; import wasm_bytes from "./pkg/wasmer_wasi_js_bg.wasm"; -interface MimeBuffer extends Buffer { +interface MimeBuffer extends ArrayBuffer { type: string; typeFull: string; charset: string; @@ -55,9 +55,8 @@ function dataUriToBuffer(uri: string): MimeBuffer { } // get the encoded data portion and decode URI-encoded chars - const encoding = base64 ? 'base64' : 'ascii'; const data = unescape(uri.substring(firstComma + 1)); - const buffer = Buffer.from(data, encoding) as MimeBuffer; + const buffer = Uint8Array.from(base64 ? atob(data) : data, c => c.charCodeAt(0)).buffer as MimeBuffer; // set `.type` and `.typeFull` properties to MIME type buffer.type = type;