You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

wasm2ts

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

wasm2ts

Compile WebAssembly .wasm files to a TypeScript module.

1.0.3
latest
Source
npmnpm
Version published
Weekly downloads
1
-85.71%
Maintainers
1
Weekly downloads
 
Created
Source

WASM to TypeScript

Compile WebAssembly .wasm files to a TypeScript module.

CLI

wasm2ts -i example.wasm -o example.ts

Installation

npm install wasm2ts -g

Example

Wat Code:

;; example.wasm

(module
  (type (;0;) (func (param i32) (result i32)))
  (type (;1;) (func))
  (func (;0;) (type 0) (param i32) (result i32)
    ;; Some wat code
    end)
  (func (;1;) (type 0) (param i32) (result i32)
    ;; Some wat code
    end)
  (func (;2;) (type 1))
  (table (;0;) 0 0 funcref)
  (memory (;0;) 1)
  (export "wasm.memory" (memory 0))
  (export "fib" (func 0))
  (export "fib2" (func 1))
  (export "_start" (func 2))
  (elem (;0;) (i32.const 0) func))
$ wasm2ts -i fib.wasm
Output file: D:\path\to\ts\fib.ts
File size: 1062 bytes

Generated TypeScript Code:

// Base64 2 Uint8Array
const Base2Uint8 = (base64: string) => {
    const binstr = atob(base64);
    const len = binstr.length;
    const bytes = new Uint8Array(len);
    for (let i = 0; i < len; i++) {
        bytes[i] = binstr.charCodeAt(i);
    }
    return bytes;
};

type ExportType = { fib: Function; fib2: Function; _start: Function; };

export default async (importObject: WebAssembly.Imports): Promise<ExportType> => {

    let wasmstr = "AGFzbQEA...Some Base64 Code...IAAAAs=";
    
    const wasm = Base2Uint8(wasmstr);

    const wasmModule = new WebAssembly.Module(wasm);
    const wasmInstance = new WebAssembly.Instance(wasmModule, importObject);
    const wasmExports = wasmInstance.exports as ExportType;

    return wasmExports;
};

wasm2ts will compress the wasm file using Gzip if this will make the file smaller.

License

MIT

Keywords

WebAssembly

FAQs

Package last updated on 15 Dec 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts