From 684b041262cbecdb267788cf34445a0b1cff5270 Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Thu, 24 Sep 2020 13:29:32 +0100 Subject: [PATCH] Add feature detection to OxiPNG --- codecs/oxipng/build.sh | 2 +- codecs/oxipng/index.ts | 10 ++ codecs/oxipng/pkg/squoosh_oxipng.d.ts | 22 ----- codecs/oxipng/pkg/squoosh_oxipng.js | 120 +---------------------- codecs/oxipng/pkg/squoosh_oxipng_bg.js | 66 +++++++++++++ codecs/oxipng/pkg/squoosh_oxipng_bg.wasm | Bin 342992 -> 343011 bytes src/codecs/oxipng/encoder.ts | 2 +- webpack.config.js | 4 +- 8 files changed, 82 insertions(+), 144 deletions(-) create mode 100644 codecs/oxipng/index.ts create mode 100644 codecs/oxipng/pkg/squoosh_oxipng_bg.js diff --git a/codecs/oxipng/build.sh b/codecs/oxipng/build.sh index c730d6d6..d9dff0b9 100644 --- a/codecs/oxipng/build.sh +++ b/codecs/oxipng/build.sh @@ -3,7 +3,7 @@ set -e rm -rf pkg,{-parallel} -wasm-pack build -t web +wasm-pack build RUSTFLAGS='-C target-feature=+atomics,+bulk-memory' wasm-pack build -t web -d pkg-parallel -- -Z build-std=panic_abort,std --features=parallel sed -i "s|input = import.meta.url.replace(/\\\.js$/, '_bg.wasm');||" pkg{,-parallel}/squoosh_oxipng.js rm pkg{,-parallel}/.gitignore diff --git a/codecs/oxipng/index.ts b/codecs/oxipng/index.ts new file mode 100644 index 00000000..285d916e --- /dev/null +++ b/codecs/oxipng/index.ts @@ -0,0 +1,10 @@ +import { threads } from 'wasm-feature-detect'; + +async function init() { + if (await threads()) { + return (await import('./spawn')).default; + } + return import('./pkg'); +} + +export default init(); diff --git a/codecs/oxipng/pkg/squoosh_oxipng.d.ts b/codecs/oxipng/pkg/squoosh_oxipng.d.ts index 196593fb..1c95bae4 100644 --- a/codecs/oxipng/pkg/squoosh_oxipng.d.ts +++ b/codecs/oxipng/pkg/squoosh_oxipng.d.ts @@ -6,25 +6,3 @@ * @returns {Uint8Array} */ export function optimise(data: Uint8Array, level: number): Uint8Array; - -export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module; - -export interface InitOutput { - readonly memory: WebAssembly.Memory; - readonly optimise: (a: number, b: number, c: number, d: number) => void; - readonly malloc: (a: number) => number; - readonly free: (a: number) => void; - readonly __wbindgen_malloc: (a: number) => number; - readonly __wbindgen_free: (a: number, b: number) => void; -} - -/** -* If `module_or_path` is {RequestInfo} or {URL}, makes a request and -* for everything else, calls `WebAssembly.instantiate` directly. -* -* @param {InitInput | Promise} module_or_path -* -* @returns {Promise} -*/ -export default function init (module_or_path?: InitInput | Promise): Promise; - \ No newline at end of file diff --git a/codecs/oxipng/pkg/squoosh_oxipng.js b/codecs/oxipng/pkg/squoosh_oxipng.js index a894a364..24abe864 100644 --- a/codecs/oxipng/pkg/squoosh_oxipng.js +++ b/codecs/oxipng/pkg/squoosh_oxipng.js @@ -1,118 +1,2 @@ - -let wasm; - -let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }); - -cachedTextDecoder.decode(); - -let cachegetUint8Memory0 = null; -function getUint8Memory0() { - if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) { - cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer); - } - return cachegetUint8Memory0; -} - -function getStringFromWasm0(ptr, len) { - return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len)); -} - -let WASM_VECTOR_LEN = 0; - -function passArray8ToWasm0(arg, malloc) { - const ptr = malloc(arg.length * 1); - getUint8Memory0().set(arg, ptr / 1); - WASM_VECTOR_LEN = arg.length; - return ptr; -} - -let cachegetInt32Memory0 = null; -function getInt32Memory0() { - if (cachegetInt32Memory0 === null || cachegetInt32Memory0.buffer !== wasm.memory.buffer) { - cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer); - } - return cachegetInt32Memory0; -} - -function getArrayU8FromWasm0(ptr, len) { - return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len); -} -/** -* @param {Uint8Array} data -* @param {number} level -* @returns {Uint8Array} -*/ -export function optimise(data, level) { - try { - const retptr = wasm.__wbindgen_export_0.value - 16; - wasm.__wbindgen_export_0.value = retptr; - var ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc); - var len0 = WASM_VECTOR_LEN; - wasm.optimise(retptr, ptr0, len0, level); - var r0 = getInt32Memory0()[retptr / 4 + 0]; - var r1 = getInt32Memory0()[retptr / 4 + 1]; - var v1 = getArrayU8FromWasm0(r0, r1).slice(); - wasm.__wbindgen_free(r0, r1 * 1); - return v1; - } finally { - wasm.__wbindgen_export_0.value += 16; - } -} - -async function load(module, imports) { - if (typeof Response === 'function' && module instanceof Response) { - - if (typeof WebAssembly.instantiateStreaming === 'function') { - try { - return await WebAssembly.instantiateStreaming(module, imports); - - } catch (e) { - if (module.headers.get('Content-Type') != 'application/wasm') { - console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e); - - } else { - throw e; - } - } - } - - const bytes = await module.arrayBuffer(); - return await WebAssembly.instantiate(bytes, imports); - - } else { - - const instance = await WebAssembly.instantiate(module, imports); - - if (instance instanceof WebAssembly.Instance) { - return { instance, module }; - - } else { - return instance; - } - } -} - -async function init(input) { - if (typeof input === 'undefined') { - - } - const imports = {}; - imports.wbg = {}; - imports.wbg.__wbindgen_throw = function(arg0, arg1) { - throw new Error(getStringFromWasm0(arg0, arg1)); - }; - - if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) { - input = fetch(input); - } - - const { instance, module } = await load(await input, imports); - - wasm = instance.exports; - init.__wbindgen_wasm_module = module; - - return wasm; -} - -export default init; - +import * as wasm from "./squoosh_oxipng_bg.wasm"; +export * from "./squoosh_oxipng_bg.js"; \ No newline at end of file diff --git a/codecs/oxipng/pkg/squoosh_oxipng_bg.js b/codecs/oxipng/pkg/squoosh_oxipng_bg.js new file mode 100644 index 00000000..c9da8937 --- /dev/null +++ b/codecs/oxipng/pkg/squoosh_oxipng_bg.js @@ -0,0 +1,66 @@ +import * as wasm from './squoosh_oxipng_bg.wasm'; + +const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder; + +let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true }); + +cachedTextDecoder.decode(); + +let cachegetUint8Memory0 = null; +function getUint8Memory0() { + if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) { + cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer); + } + return cachegetUint8Memory0; +} + +function getStringFromWasm0(ptr, len) { + return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len)); +} + +let WASM_VECTOR_LEN = 0; + +function passArray8ToWasm0(arg, malloc) { + const ptr = malloc(arg.length * 1); + getUint8Memory0().set(arg, ptr / 1); + WASM_VECTOR_LEN = arg.length; + return ptr; +} + +let cachegetInt32Memory0 = null; +function getInt32Memory0() { + if (cachegetInt32Memory0 === null || cachegetInt32Memory0.buffer !== wasm.memory.buffer) { + cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer); + } + return cachegetInt32Memory0; +} + +function getArrayU8FromWasm0(ptr, len) { + return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len); +} +/** +* @param {Uint8Array} data +* @param {number} level +* @returns {Uint8Array} +*/ +export function optimise(data, level) { + try { + const retptr = wasm.__wbindgen_export_0.value - 16; + wasm.__wbindgen_export_0.value = retptr; + var ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc); + var len0 = WASM_VECTOR_LEN; + wasm.optimise(retptr, ptr0, len0, level); + var r0 = getInt32Memory0()[retptr / 4 + 0]; + var r1 = getInt32Memory0()[retptr / 4 + 1]; + var v1 = getArrayU8FromWasm0(r0, r1).slice(); + wasm.__wbindgen_free(r0, r1 * 1); + return v1; + } finally { + wasm.__wbindgen_export_0.value += 16; + } +} + +export const __wbindgen_throw = function(arg0, arg1) { + throw new Error(getStringFromWasm0(arg0, arg1)); +}; + diff --git a/codecs/oxipng/pkg/squoosh_oxipng_bg.wasm b/codecs/oxipng/pkg/squoosh_oxipng_bg.wasm index 34f4f5a2ea9ab521e9963850adbaeef872e52349..aef8cca866c8b32af4c731aecdc664c11d9e19cc 100644 GIT binary patch delta 60 zcmcccUF7k1kqyF(lG==7diuqMrTO{A8S(iQnFV?2@k!}=S;ftgjO~()Aj|~B%s|Yt KU6PS?yA=S8HWG0F delta 41 rcmaF-UF5=dkqyF(Y!Zyj { diff --git a/webpack.config.js b/webpack.config.js index 4e3e902d..03a837fd 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -149,7 +149,7 @@ module.exports = async function(_, env) { { // Emscripten modules don't work with Webpack's Wasm loader. test: /\.wasm$/, - exclude: /(?