@tybys/wasm-util 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -25,7 +25,7 @@ const fs = createFsFromVolume(Volume.from({
25
25
  '/home/wasi': null
26
26
  }))
27
27
 
28
- const wasi = new WASI({
28
+ const wasi = WASI.createSync({
29
29
  args: ['chrome', 'file.wasm'],
30
30
  env: {
31
31
  NODE_ENV: 'development',
@@ -34,7 +34,7 @@ const wasi = new WASI({
34
34
  preopens: {
35
35
  '/': '/'
36
36
  },
37
- filesystem: { type: 'memfs', fs },
37
+ fs,
38
38
 
39
39
  // redirect stdout / stderr
40
40
 
@@ -2,8 +2,6 @@
2
2
  * @packageDocumentation
3
3
  */
4
4
 
5
- import type { IFs } from 'memfs-browser';
6
-
7
5
  /** @public */
8
6
  export declare class Asyncify {
9
7
  private value;
@@ -33,6 +31,12 @@ export declare type AsyncifyExports<T, U> = T extends Record<string, any> ? {
33
31
  [P in keyof T]: T[P] extends Callable ? U extends Array<Exclude<keyof T, AsyncifyExportName>> ? P extends U[number] ? AsyncifyExportFunction<T[P]> : T[P] : AsyncifyExportFunction<T[P]> : T[P];
34
32
  } : T;
35
33
 
34
+ /** @public */
35
+ export declare function asyncifyLoad(asyncify: AsyncifyOptions, urlOrBuffer: string | URL | BufferSource, imports?: WebAssembly.Imports): Promise<WebAssembly.WebAssemblyInstantiatedSource>;
36
+
37
+ /** @public */
38
+ export declare function asyncifyLoadSync(asyncify: AsyncifyOptions, buffer: BufferSource, imports?: WebAssembly.Imports): WebAssembly.WebAssemblyInstantiatedSource;
39
+
36
40
  /** @public */
37
41
  export declare interface AsyncifyOptions {
38
42
  wasm64?: boolean;
@@ -43,12 +47,213 @@ export declare interface AsyncifyOptions {
43
47
  wrapExports?: string[];
44
48
  }
45
49
 
50
+ /** @public */
51
+ export declare interface AsyncWASIOptions extends WASIOptions {
52
+ fs: {
53
+ promises: IFsPromises;
54
+ };
55
+ asyncify?: Asyncify;
56
+ }
57
+
58
+ /** @public */
59
+ export declare interface BaseEncodingOptions {
60
+ encoding?: BufferEncoding_2;
61
+ }
62
+
63
+ /** @public */
64
+ export declare interface BigIntStats extends StatsBase<bigint> {
65
+ }
66
+
67
+ /** @public */
68
+ declare type BufferEncoding_2 = 'ascii' | 'utf8' | 'utf-8' | 'utf16le' | 'ucs2' | 'ucs-2' | 'base64' | 'base64url' | 'latin1' | 'binary' | 'hex';
69
+ export { BufferEncoding_2 as BufferEncoding }
70
+
71
+ /** @public */
72
+ export declare type BufferEncodingOption = 'buffer' | {
73
+ encoding: 'buffer';
74
+ };
75
+
46
76
  /** @public */
47
77
  export declare type Callable = (...args: any[]) => any;
48
78
 
49
79
  /** @public */
50
80
  export declare function extendMemory(memory: WebAssembly.Memory): Memory;
51
81
 
82
+ /** @public */
83
+ export declare interface FileHandle {
84
+ readonly fd: number;
85
+ datasync(): Promise<void>;
86
+ sync(): Promise<void>;
87
+ read<TBuffer extends Uint8Array>(buffer: TBuffer, offset?: number, length?: number, position?: number): Promise<{
88
+ bytesRead: number;
89
+ buffer: TBuffer;
90
+ }>;
91
+ stat(opts?: StatOptions & {
92
+ bigint?: false;
93
+ }): Promise<Stats>;
94
+ stat(opts: StatOptions & {
95
+ bigint: true;
96
+ }): Promise<BigIntStats>;
97
+ stat(opts?: StatOptions): Promise<Stats | BigIntStats>;
98
+ truncate(len?: number): Promise<void>;
99
+ utimes(atime: string | number | Date, mtime: string | number | Date): Promise<void>;
100
+ write<TBuffer extends Uint8Array>(buffer: TBuffer, offset?: number, length?: number, position?: number): Promise<{
101
+ bytesWritten: number;
102
+ buffer: TBuffer;
103
+ }>;
104
+ write(data: string | Uint8Array, position?: number, encoding?: BufferEncoding_2): Promise<{
105
+ bytesWritten: number;
106
+ buffer: string;
107
+ }>;
108
+ close(): Promise<void>;
109
+ }
110
+
111
+ /** @public */
112
+ export declare interface IDirent {
113
+ isFile(): boolean;
114
+ isDirectory(): boolean;
115
+ isBlockDevice(): boolean;
116
+ isCharacterDevice(): boolean;
117
+ isSymbolicLink(): boolean;
118
+ isFIFO(): boolean;
119
+ isSocket(): boolean;
120
+ name: string;
121
+ }
122
+
123
+ /** @public */
124
+ export declare interface IFs {
125
+ fstatSync(fd: number, options?: StatOptions & {
126
+ bigint?: false;
127
+ }): Stats;
128
+ fstatSync(fd: number, options: StatOptions & {
129
+ bigint: true;
130
+ }): BigIntStats;
131
+ fstatSync(fd: number, options?: StatOptions): Stats | BigIntStats;
132
+ statSync(path: PathLike, options?: StatOptions & {
133
+ bigint?: false;
134
+ }): Stats;
135
+ statSync(path: PathLike, options: StatOptions & {
136
+ bigint: true;
137
+ }): BigIntStats;
138
+ statSync(path: PathLike, options?: StatOptions): Stats | BigIntStats;
139
+ lstatSync(path: PathLike, options?: StatOptions & {
140
+ bigint?: false;
141
+ }): Stats;
142
+ lstatSync(path: PathLike, options: StatOptions & {
143
+ bigint: true;
144
+ }): BigIntStats;
145
+ lstatSync(path: PathLike, options?: StatOptions): Stats | BigIntStats;
146
+ utimesSync(path: PathLike, atime: string | number | Date, mtime: string | number | Date): void;
147
+ openSync(path: PathLike, flags: OpenMode, mode?: Mode): number;
148
+ readSync(fd: number, buffer: ArrayBufferView, offset: number, length: number, position: number | null): number;
149
+ readSync(fd: number, buffer: ArrayBufferView, opts?: ReadSyncOptions): number;
150
+ writeSync(fd: number, buffer: ArrayBufferView, offset?: number, length?: number, position?: number): number;
151
+ writeSync(fd: number, string: string, position?: number, encoding?: BufferEncoding_2): number;
152
+ closeSync(fd: number): void;
153
+ readlinkSync(path: PathLike, options?: BaseEncodingOptions | BufferEncoding_2): string;
154
+ readlinkSync(path: PathLike, options: BufferEncodingOption): Uint8Array;
155
+ readlinkSync(path: PathLike, options?: BaseEncodingOptions | string): string | Uint8Array;
156
+ realpathSync(path: PathLike, options?: BaseEncodingOptions | BufferEncoding_2): string;
157
+ realpathSync(path: PathLike, options: BufferEncodingOption): Uint8Array;
158
+ realpathSync(path: PathLike, options?: BaseEncodingOptions | string): string | Uint8Array;
159
+ truncateSync(path: PathLike, len?: number): void;
160
+ ftruncateSync(fd: number, len?: number): void;
161
+ fdatasyncSync(fd: number): void;
162
+ futimesSync(fd: number, atime: string | number | Date, mtime: string | number | Date): void;
163
+ readdirSync(path: PathLike, options?: {
164
+ encoding: BufferEncoding_2 | null;
165
+ withFileTypes?: false;
166
+ } | BufferEncoding_2): string[];
167
+ readdirSync(path: PathLike, options: {
168
+ encoding: 'buffer';
169
+ withFileTypes?: false;
170
+ } | 'buffer'): Uint8Array[];
171
+ readdirSync(path: PathLike, options?: BaseEncodingOptions & {
172
+ withFileTypes?: false;
173
+ } | BufferEncoding_2): string[] | Uint8Array[];
174
+ readdirSync(path: PathLike, options: BaseEncodingOptions & {
175
+ withFileTypes: true;
176
+ }): IDirent[];
177
+ fsyncSync(fd: number): void;
178
+ mkdirSync(path: PathLike, options: MakeDirectoryOptions & {
179
+ recursive: true;
180
+ }): string | undefined;
181
+ mkdirSync(path: PathLike, options?: Mode | (MakeDirectoryOptions & {
182
+ recursive?: false;
183
+ })): void;
184
+ mkdirSync(path: PathLike, options?: Mode | MakeDirectoryOptions): string | undefined;
185
+ rmdirSync(path: PathLike, options?: RmDirOptions): void;
186
+ linkSync(existingPath: PathLike, newPath: PathLike): void;
187
+ unlinkSync(path: PathLike): void;
188
+ renameSync(oldPath: PathLike, newPath: PathLike): void;
189
+ symlinkSync(target: PathLike, path: PathLike, type?: 'dir' | 'file' | 'junction'): void;
190
+ }
191
+
192
+ /** @public */
193
+ export declare interface IFsPromises {
194
+ stat(path: PathLike, options?: StatOptions & {
195
+ bigint?: false;
196
+ }): Promise<Stats>;
197
+ stat(path: PathLike, options: StatOptions & {
198
+ bigint: true;
199
+ }): Promise<BigIntStats>;
200
+ stat(path: PathLike, options?: StatOptions): Promise<Stats | BigIntStats>;
201
+ lstat(path: PathLike, options?: StatOptions & {
202
+ bigint?: false;
203
+ }): Promise<Stats>;
204
+ lstat(path: PathLike, options: StatOptions & {
205
+ bigint: true;
206
+ }): Promise<BigIntStats>;
207
+ lstat(path: PathLike, options?: StatOptions): Promise<Stats | BigIntStats>;
208
+ utimes(path: PathLike, atime: string | number | Date, mtime: string | number | Date): Promise<void>;
209
+ open(path: PathLike, flags: OpenMode, mode?: Mode): Promise<FileHandle>;
210
+ read<TBuffer extends Uint8Array>(handle: FileHandle, buffer: TBuffer, offset?: number, length?: number, position?: number): Promise<{
211
+ bytesRead: number;
212
+ buffer: TBuffer;
213
+ }>;
214
+ write<TBuffer extends Uint8Array>(handle: FileHandle, buffer: TBuffer, offset?: number, length?: number, position?: number): Promise<{
215
+ bytesWritten: number;
216
+ buffer: TBuffer;
217
+ }>;
218
+ readlink(path: PathLike, options?: BaseEncodingOptions | BufferEncoding_2): Promise<string>;
219
+ readlink(path: PathLike, options: BufferEncodingOption): Promise<Uint8Array>;
220
+ readlink(path: PathLike, options?: BaseEncodingOptions | string): Promise<string | Uint8Array>;
221
+ realpath(path: PathLike, options?: BaseEncodingOptions | BufferEncoding_2): Promise<string>;
222
+ realpath(path: PathLike, options: BufferEncodingOption): Promise<Uint8Array>;
223
+ realpath(path: PathLike, options?: BaseEncodingOptions | string): Promise<string | Uint8Array>;
224
+ truncate(path: PathLike, len?: number): Promise<void>;
225
+ ftruncate(handle: FileHandle, len?: number): Promise<void>;
226
+ fdatasync(handle: FileHandle): Promise<void>;
227
+ futimes(handle: FileHandle, atime: string | number | Date, mtime: string | number | Date): Promise<void>;
228
+ readdir(path: PathLike, options?: {
229
+ encoding: BufferEncoding_2 | null;
230
+ withFileTypes?: false;
231
+ } | BufferEncoding_2): Promise<string[]>;
232
+ readdir(path: PathLike, options: {
233
+ encoding: 'buffer';
234
+ withFileTypes?: false;
235
+ } | 'buffer'): Promise<Uint8Array[]>;
236
+ readdir(path: PathLike, options?: BaseEncodingOptions & {
237
+ withFileTypes?: false;
238
+ } | BufferEncoding_2): Promise<string[] | Uint8Array[]>;
239
+ readdir(path: PathLike, options: BaseEncodingOptions & {
240
+ withFileTypes: true;
241
+ }): Promise<IDirent[]>;
242
+ fsync(handle: FileHandle): Promise<void>;
243
+ mkdir(path: PathLike, options: MakeDirectoryOptions & {
244
+ recursive: true;
245
+ }): Promise<string | undefined>;
246
+ mkdir(path: PathLike, options?: Mode | (MakeDirectoryOptions & {
247
+ recursive?: false;
248
+ })): Promise<void>;
249
+ mkdir(path: PathLike, options?: Mode | MakeDirectoryOptions): Promise<string | undefined>;
250
+ rmdir(path: PathLike, options?: RmDirOptions): Promise<void>;
251
+ link(existingPath: PathLike, newPath: PathLike): Promise<void>;
252
+ unlink(path: PathLike): Promise<void>;
253
+ rename(oldPath: PathLike, newPath: PathLike): Promise<void>;
254
+ symlink(target: PathLike, path: PathLike, type?: 'dir' | 'file' | 'junction'): Promise<void>;
255
+ }
256
+
52
257
  declare const kExitCode: unique symbol;
53
258
 
54
259
  declare const kInstance: unique symbol;
@@ -58,10 +263,16 @@ declare const kSetMemory: unique symbol;
58
263
  declare const kStarted: unique symbol;
59
264
 
60
265
  /** @public */
61
- export declare function load(urlOrBuffer: string | URL | BufferSource, imports?: WebAssembly.Imports, asyncify?: AsyncifyOptions): Promise<WebAssembly.WebAssemblyInstantiatedSource>;
266
+ export declare function load(urlOrBuffer: string | URL | BufferSource, imports?: WebAssembly.Imports): Promise<WebAssembly.WebAssemblyInstantiatedSource>;
267
+
268
+ /** @public */
269
+ export declare function loadSync(buffer: BufferSource, imports?: WebAssembly.Imports): WebAssembly.WebAssemblyInstantiatedSource;
62
270
 
63
271
  /** @public */
64
- export declare function loadSync(buffer: BufferSource, imports?: WebAssembly.Imports, asyncify?: AsyncifyOptions): WebAssembly.WebAssemblyInstantiatedSource;
272
+ export declare interface MakeDirectoryOptions {
273
+ recursive?: boolean;
274
+ mode?: Mode;
275
+ }
65
276
 
66
277
  /** @public */
67
278
  export declare class Memory extends WebAssemblyMemory {
@@ -79,11 +290,77 @@ export declare class Memory extends WebAssemblyMemory {
79
290
  get view(): DataView;
80
291
  }
81
292
 
293
+ /** @public */
294
+ export declare type Mode = number | string;
295
+
296
+ /** @public */
297
+ export declare type OpenMode = number | string;
298
+
299
+ /** @public */
300
+ export declare type PathLike = string | Uint8Array | URL;
301
+
82
302
  /** @public */
83
303
  export declare type PromisifyExports<T, U> = T extends Record<string, any> ? {
84
304
  [P in keyof T]: T[P] extends Callable ? U extends Array<keyof T> ? P extends U[number] ? AsyncifyExportFunction<T[P]> : T[P] : AsyncifyExportFunction<T[P]> : T[P];
85
305
  } : T;
86
306
 
307
+ /** @public */
308
+ export declare interface ReadSyncOptions {
309
+ offset?: number;
310
+ length?: number;
311
+ position?: number;
312
+ }
313
+
314
+ /** @public */
315
+ export declare interface RmDirOptions {
316
+ maxRetries?: number;
317
+ recursive?: boolean;
318
+ retryDelay?: number;
319
+ }
320
+
321
+ /** @public */
322
+ export declare interface StatOptions {
323
+ bigint?: boolean;
324
+ }
325
+
326
+ /** @public */
327
+ export declare interface Stats extends StatsBase<number> {
328
+ }
329
+
330
+ /** @public */
331
+ export declare interface StatsBase<T> {
332
+ isFile(): boolean;
333
+ isDirectory(): boolean;
334
+ isBlockDevice(): boolean;
335
+ isCharacterDevice(): boolean;
336
+ isSymbolicLink(): boolean;
337
+ isFIFO(): boolean;
338
+ isSocket(): boolean;
339
+ dev: T;
340
+ ino: T;
341
+ mode: T;
342
+ nlink: T;
343
+ uid: T;
344
+ gid: T;
345
+ rdev: T;
346
+ size: T;
347
+ blksize: T;
348
+ blocks: T;
349
+ atimeMs: T;
350
+ mtimeMs: T;
351
+ ctimeMs: T;
352
+ birthtimeMs: T;
353
+ atime: Date;
354
+ mtime: Date;
355
+ ctime: Date;
356
+ birthtime: Date;
357
+ }
358
+
359
+ /** @public */
360
+ export declare interface SyncWASIOptions extends WASIOptions {
361
+ fs?: IFs;
362
+ }
363
+
87
364
  /** @public */
88
365
  export declare class WASI {
89
366
  private [kSetMemory];
@@ -91,7 +368,9 @@ export declare class WASI {
91
368
  private [kExitCode];
92
369
  private [kInstance];
93
370
  readonly wasiImport: Record<string, any>;
94
- constructor(options?: WASIOptions);
371
+ static createSync(options?: SyncWASIOptions): WASI;
372
+ static createAsync(options?: AsyncWASIOptions): Promise<WASI>;
373
+ private constructor();
95
374
  start(instance: WebAssembly.Instance): number | undefined | Promise<number> | Promise<undefined>;
96
375
  initialize(instance: WebAssembly.Instance): void | Promise<void>;
97
376
  }
@@ -107,10 +386,6 @@ export declare interface WASIOptions {
107
386
  returnOnExit?: boolean | undefined;
108
387
  print?: (str: string) => void;
109
388
  printErr?: (str: string) => void;
110
- filesystem?: {
111
- type: 'memfs';
112
- fs: IFs;
113
- };
114
389
  }
115
390
 
116
391
  /** @public */
@@ -123,7 +398,7 @@ export declare const WebAssemblyMemory: {
123
398
  export declare function wrapAsyncExport<T extends Function = any>(f: Function): T;
124
399
 
125
400
  /** @public */
126
- export declare function wrapAsyncImport(f: Function, parameterType: WebAssembly.ValueType[], returnType: WebAssembly.ValueType[]): Function;
401
+ export declare function wrapAsyncImport<T extends (...args: any[]) => any>(f: T, parameterType: WebAssembly.ValueType[], returnType: WebAssembly.ValueType[]): (...args: [object, ...Parameters<T>]) => ReturnType<T>;
127
402
 
128
403
  /** @public */
129
404
  export declare function wrapExports<T extends WebAssembly.Exports>(exports: T): PromisifyExports<T, void>;