From a18ed360ebf77ffb5be1ed0b990e1fc8a11b622c Mon Sep 17 00:00:00 2001 From: ergunsh Date: Tue, 8 Jun 2021 20:08:10 +0200 Subject: [PATCH] Address review comments * Updated import to contain file extension `js` * Separated WebAssembly definitions from missing-types * Converted resizer.resize result to Uint8ClampedArray * Moved ResizeInstantiateOptions to an interface --- libsquoosh/src/WebAssembly.d.ts | 132 +++++++++++++++++++++++++++++ libsquoosh/src/codecs.ts | 40 +++++---- libsquoosh/src/image_data.ts | 8 +- libsquoosh/src/missing-types.d.ts | 133 ------------------------------ 4 files changed, 156 insertions(+), 157 deletions(-) create mode 100644 libsquoosh/src/WebAssembly.d.ts diff --git a/libsquoosh/src/WebAssembly.d.ts b/libsquoosh/src/WebAssembly.d.ts new file mode 100644 index 00000000..efa094bc --- /dev/null +++ b/libsquoosh/src/WebAssembly.d.ts @@ -0,0 +1,132 @@ +/** + * WebAssembly definitions are not available in `@types/node` yet, + * so these are copied from `lib.dom.d.ts` + */ +declare namespace WebAssembly { + interface CompileError {} + + var CompileError: { + prototype: CompileError; + new (): CompileError; + }; + + interface Global { + value: any; + valueOf(): any; + } + + var Global: { + prototype: Global; + new (descriptor: GlobalDescriptor, v?: any): Global; + }; + + interface Instance { + readonly exports: Exports; + } + + var Instance: { + prototype: Instance; + new (module: Module, importObject?: Imports): Instance; + }; + + interface LinkError {} + + var LinkError: { + prototype: LinkError; + new (): LinkError; + }; + + interface Memory { + readonly buffer: ArrayBuffer; + grow(delta: number): number; + } + + var Memory: { + prototype: Memory; + new (descriptor: MemoryDescriptor): Memory; + }; + + interface Module {} + + var Module: { + prototype: Module; + new (bytes: BufferSource): Module; + customSections(moduleObject: Module, sectionName: string): ArrayBuffer[]; + exports(moduleObject: Module): ModuleExportDescriptor[]; + imports(moduleObject: Module): ModuleImportDescriptor[]; + }; + + interface RuntimeError {} + + var RuntimeError: { + prototype: RuntimeError; + new (): RuntimeError; + }; + + interface Table { + readonly length: number; + get(index: number): Function | null; + grow(delta: number): number; + set(index: number, value: Function | null): void; + } + + var Table: { + prototype: Table; + new (descriptor: TableDescriptor): Table; + }; + + interface GlobalDescriptor { + mutable?: boolean; + value: ValueType; + } + + interface MemoryDescriptor { + initial: number; + maximum?: number; + } + + interface ModuleExportDescriptor { + kind: ImportExportKind; + name: string; + } + + interface ModuleImportDescriptor { + kind: ImportExportKind; + module: string; + name: string; + } + + interface TableDescriptor { + element: TableKind; + initial: number; + maximum?: number; + } + + interface WebAssemblyInstantiatedSource { + instance: Instance; + module: Module; + } + + type ImportExportKind = 'function' | 'global' | 'memory' | 'table'; + type TableKind = 'anyfunc'; + type ValueType = 'f32' | 'f64' | 'i32' | 'i64'; + type ExportValue = Function | Global | Memory | Table; + type Exports = Record; + type ImportValue = ExportValue | number; + type ModuleImports = Record; + type Imports = Record; + function compile(bytes: BufferSource): Promise; + // `compileStreaming` does not exist in NodeJS + // function compileStreaming(source: Response | Promise): Promise; + function instantiate( + bytes: BufferSource, + importObject?: Imports, + ): Promise; + function instantiate( + moduleObject: Module, + importObject?: Imports, + ): Promise; + // `instantiateStreaming` does not exist in NodeJS + // function instantiateStreaming(response: Response | PromiseLike, importObject?: Imports): Promise; + function validate(bytes: BufferSource): boolean; +} diff --git a/libsquoosh/src/codecs.ts b/libsquoosh/src/codecs.ts index a0b694be..e95cd227 100644 --- a/libsquoosh/src/codecs.ts +++ b/libsquoosh/src/codecs.ts @@ -1,5 +1,5 @@ import { promises as fsp } from 'fs'; -import { instantiateEmscriptenWasm, pathify } from './emscripten-utils'; +import { instantiateEmscriptenWasm, pathify } from './emscripten-utils.js'; interface RotateModuleInstance { exports: { @@ -15,6 +15,14 @@ interface ResizeWithAspectParams { target_height: number; } +interface ResizeInstantiateOptions { + width: number; + height: number; + method: string; + premultiply: boolean; + linearRGB: boolean; +} + declare global { // Needed for being able to use ImageData as type in codec types type ImageData = typeof import('./image_data'); @@ -147,13 +155,7 @@ export const preprocessors = { method, premultiply, linearRGB, - }: { - width: number; - height: number; - method: string; - premultiply: boolean; - linearRGB: boolean; - }, + }: ResizeInstantiateOptions, ) => { ({ width, height } = resizeWithAspect({ input_width, @@ -161,17 +163,19 @@ export const preprocessors = { target_width: width, target_height: height, })); + const resizeResult = resize.resize( + buffer, + input_width, + input_height, + width, + height, + resizeNameToIndex(method), + premultiply, + linearRGB, + ); return new ImageData( - resize.resize( - buffer, - input_width, - input_height, - width, - height, - resizeNameToIndex(method), - premultiply, - linearRGB, - ), + // ImageData does not accept Uint8Array so we convert it to a clamped array + new Uint8ClampedArray(resizeResult), width, height, ); diff --git a/libsquoosh/src/image_data.ts b/libsquoosh/src/image_data.ts index 73a3a389..9125199f 100644 --- a/libsquoosh/src/image_data.ts +++ b/libsquoosh/src/image_data.ts @@ -1,13 +1,9 @@ export default class ImageData { - readonly data: Uint8ClampedArray | Uint8Array; + readonly data: Uint8ClampedArray; readonly width: number; readonly height: number; - constructor( - data: Uint8ClampedArray | Uint8Array, - width: number, - height: number, - ) { + constructor(data: Uint8ClampedArray, width: number, height: number) { this.data = data; this.width = width; this.height = height; diff --git a/libsquoosh/src/missing-types.d.ts b/libsquoosh/src/missing-types.d.ts index fcbdbd5d..da972340 100644 --- a/libsquoosh/src/missing-types.d.ts +++ b/libsquoosh/src/missing-types.d.ts @@ -31,136 +31,3 @@ type MessageEvent = never; type BufferSource = ArrayBufferView | ArrayBuffer; type URL = import('url').URL; - -/** - * WebAssembly definitions are not available in `@types/node` yet, - * so these are copied from `lib.dom.d.ts` - */ -declare namespace WebAssembly { - interface CompileError {} - - var CompileError: { - prototype: CompileError; - new (): CompileError; - }; - - interface Global { - value: any; - valueOf(): any; - } - - var Global: { - prototype: Global; - new (descriptor: GlobalDescriptor, v?: any): Global; - }; - - interface Instance { - readonly exports: Exports; - } - - var Instance: { - prototype: Instance; - new (module: Module, importObject?: Imports): Instance; - }; - - interface LinkError {} - - var LinkError: { - prototype: LinkError; - new (): LinkError; - }; - - interface Memory { - readonly buffer: ArrayBuffer; - grow(delta: number): number; - } - - var Memory: { - prototype: Memory; - new (descriptor: MemoryDescriptor): Memory; - }; - - interface Module {} - - var Module: { - prototype: Module; - new (bytes: BufferSource): Module; - customSections(moduleObject: Module, sectionName: string): ArrayBuffer[]; - exports(moduleObject: Module): ModuleExportDescriptor[]; - imports(moduleObject: Module): ModuleImportDescriptor[]; - }; - - interface RuntimeError {} - - var RuntimeError: { - prototype: RuntimeError; - new (): RuntimeError; - }; - - interface Table { - readonly length: number; - get(index: number): Function | null; - grow(delta: number): number; - set(index: number, value: Function | null): void; - } - - var Table: { - prototype: Table; - new (descriptor: TableDescriptor): Table; - }; - - interface GlobalDescriptor { - mutable?: boolean; - value: ValueType; - } - - interface MemoryDescriptor { - initial: number; - maximum?: number; - } - - interface ModuleExportDescriptor { - kind: ImportExportKind; - name: string; - } - - interface ModuleImportDescriptor { - kind: ImportExportKind; - module: string; - name: string; - } - - interface TableDescriptor { - element: TableKind; - initial: number; - maximum?: number; - } - - interface WebAssemblyInstantiatedSource { - instance: Instance; - module: Module; - } - - type ImportExportKind = 'function' | 'global' | 'memory' | 'table'; - type TableKind = 'anyfunc'; - type ValueType = 'f32' | 'f64' | 'i32' | 'i64'; - type ExportValue = Function | Global | Memory | Table; - type Exports = Record; - type ImportValue = ExportValue | number; - type ModuleImports = Record; - type Imports = Record; - function compile(bytes: BufferSource): Promise; - // `compileStreaming` does not exist in NodeJS - // function compileStreaming(source: Response | Promise): Promise; - function instantiate( - bytes: BufferSource, - importObject?: Imports, - ): Promise; - function instantiate( - moduleObject: Module, - importObject?: Imports, - ): Promise; - // `instantiateStreaming` does not exist in NodeJS - // function instantiateStreaming(response: Response | PromiseLike, importObject?: Imports): Promise; - function validate(bytes: BufferSource): boolean; -}