@bytecodealliance/jco 0.4.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 ADDED
@@ -0,0 +1,136 @@
1
+ <div align="center">
2
+ <h1><code>jco</code></h1>
3
+
4
+ <p>
5
+ <strong>JavaScript component toolchain for working with <a href="https://github.com/WebAssembly/component-model">WebAssembly Components</a></strong>
6
+ </p>
7
+
8
+ <strong>A <a href="https://bytecodealliance.org/">Bytecode Alliance</a> project</strong>
9
+
10
+ <p>
11
+ <a href="https://github.com/bytecodealliance/jco/actions?query=workflow%3ACI"><img src="https://github.com/bytecodealliance/jco/workflows/CI/badge.svg" alt="build status" /></a>
12
+ </p>
13
+ </div>
14
+
15
+ ## Overview
16
+
17
+ `jco` is a fully native JS tool for working with the emerging [WebAssembly Components](https://github.com/WebAssembly/component-model) specification in JavaScript.
18
+
19
+ Features include:
20
+
21
+ * "Transpiling" Wasm Component binaries into ES modules that can run in any JS environment.
22
+ * Optimization helpers for Components via Binaryen
23
+ * Component builds of [Wasm Tools](https://github.com/bytecodealliance/wasm-tools) helpers, available for use as a library or CLI commands for use in native JS environments.
24
+
25
+ For creating components, see the [Cargo Component](https://github.com/bytecodealliance/cargo-Component) project for Rust and [Wit Bindgen](https://github.com/bytecodealliance/wit-bindgen) for various guest bindgen helpers.
26
+
27
+ > **Note**: This is an experimental project, no guarantees are provided for stability or support and breaking changes may be made in future.
28
+
29
+ ## Installation
30
+
31
+ ```shell
32
+ npm install @bytecodealliance/jco
33
+ ```
34
+
35
+ jco can be used as either a library or as a CLI via the `jco` CLI command.
36
+
37
+ ## Example
38
+
39
+ See the [example workflow](EXAMPLE.md) page for a full usage example.
40
+
41
+ ## API
42
+
43
+ The below is an outline of the available API functions, see [api.d.ts](api.d.ts) file for the exact options.
44
+
45
+ #### `transpile(component: Uint8Array, opts?): Promise<{ files: Record<string, Uint8Array> }>`
46
+
47
+ Transpile a Component to JS.
48
+
49
+ **Transpilation options:**
50
+
51
+ * `name?: string` - name for the generated JS file.
52
+ * `instantiation?: bool` - instead of a direct ES module, output the raw instantiation function for custom virtualization.
53
+ * `map?: Record<string, string>` - remap component imports
54
+ * `validLiftingOptimization?: bool` - optimization to reduce code size
55
+ * `noNodejsCompat?: bool` - disables Node.js compatible output
56
+ * `tlaCompat?: bool` - enable compat in JS runtimes without TLA support
57
+ * `base64Cutoff?: number` - size in bytes, under which Wasm modules get inlined as base64.
58
+ * `js?: bool` - convert Wasm into JS instead for execution compatibility in JS environments without Wasm support.
59
+ * `minify?: bool` - minify the output JS.
60
+ * `optimize?: bool` - optimize the component with Binaryen wasm-opt first.
61
+ * `optArgs?: string[]` - if using optimize, custom optimization options (defaults to best optimization, but this is very slow)
62
+
63
+ #### `opt(component: Uint8Array, opts?): Promise<{ component: Uint8Array }>`
64
+
65
+ Optimize a Component with the [Binaryen Wasm-opt](https://www.npmjs.com/package/binaryen) project.
66
+
67
+ #### `componentWit(component: Uint8Array, document?: string): string`
68
+
69
+ Extract the WIT world from a component binary.
70
+
71
+ #### `print(component: Uint8Array): string`
72
+
73
+ Print the WAT for a Component binary.
74
+
75
+ #### `metadataShow(wasm: Uint8Array): Metadata`
76
+
77
+ Extract the producer toolchain metadata for a component and its nested modules.
78
+
79
+ #### `parse(wat: string): Uint8Array`
80
+
81
+ Parse a compoment WAT to output a Component binary.
82
+
83
+ #### `componentNew(coreWasm: Uint8Array | null, adapters?: [String, Uint8Array][]): Uint8Array`
84
+
85
+ "WIT Component" Component creation tool, optionally providing a set of named adapter binaries.
86
+
87
+ #### `componentEmbed(coreWasm: Uint8Array | null, wit: String, opts?: { stringEncoding?, dummy?, world?, metadata? }): Uint8Array`
88
+
89
+ "WIT Component" Component embedding tool, for embedding component types into core binaries, as an advanced use case of component generation.
90
+
91
+ #### `metadataAdd(wasm: Uint8Array, metadata): Uint8Array`
92
+
93
+ Add new producer metadata to a component or core Wasm binary.
94
+
95
+ ## CLI
96
+
97
+ ```shell
98
+ Usage: jco <command> [options]
99
+
100
+ jco - WebAssembly JS Component Tools
101
+ JS Component Bindgen & Wasm Tools for JS
102
+
103
+ Options:
104
+ -V, --version output the version number
105
+ -h, --help display help for command
106
+
107
+ Commands:
108
+ transpile [options] <component-path> Transpile a WebAssembly Component to JS + core Wasm for JavaScript execution
109
+ opt [options] <component-file> optimizes a Wasm component, including running wasm-opt Binaryen optimizations
110
+ wit [options] <component-path> extract the WIT from a WebAssembly Component [wasm-tools component wit]
111
+ print [options] <input> print the WebAssembly WAT text for a binary file [wasm-tools print]
112
+ metadata [options] [module] extract the producer metadata for a Wasm binary [wasm-tools metadata show]
113
+ parse [options] <input> parses the Wasm text format into a binary file [wasm-tools parse]
114
+ new [options] <core-module> create a WebAssembly component adapted from a component core Wasm [wasm-tools component new]
115
+ embed [options] [core-module] embed the component typing section into a core Wasm module [wasm-tools component embed]
116
+ help [command] display help for command
117
+ ```
118
+
119
+ ## Contributing
120
+
121
+ Development is based on a standard `npm install && npm run build && npm run test` workflow.
122
+
123
+ Tests can be run without bundling via `npm run build:dev && npm run test:dev`.
124
+
125
+ Specific tests can be run adding the mocha `--grep` flag, for example: `npm run test:dev -- --grep exports_only`.
126
+
127
+ # License
128
+
129
+ This project is licensed under the Apache 2.0 license with the LLVM exception.
130
+ See [LICENSE](LICENSE) for more details.
131
+
132
+ ### Contribution
133
+
134
+ Unless you explicitly state otherwise, any contribution intentionally submitted
135
+ for inclusion in this project by you, as defined in the Apache-2.0 license,
136
+ shall be licensed as above, without any additional terms or conditions.
package/api.d.ts ADDED
@@ -0,0 +1,93 @@
1
+ /**
2
+ * Optimize a Component with Binaryen wasm-opt optimizations
3
+ */
4
+ export function opt(componentBytes: Uint8Array, opts?: { quiet: boolean; optArgs?: string[] }): Promise<{
5
+ component: Uint8Array,
6
+ compressionInfo: { beforeBytes: number, afterBytes: number }[]
7
+ }>;
8
+
9
+ export interface TranspileOpts {
10
+ /// name for the generated JS file.
11
+ name?: string,
12
+ /// instead of a direct ES module, output the raw
13
+ /// instantiation function for custom virtualization.
14
+ instantiation?: boolean,
15
+ /// remap Component imports
16
+ map?: Record<string, string>,
17
+ /// optimization to reduce code size
18
+ validLiftingOptimization?: boolean,
19
+ /// disables Node.js compatible output
20
+ noNodejsCompat?: boolean,
21
+ /// enable compat in JS runtimes without TLA support
22
+ tlaCompat?: boolean,
23
+ /// size in bytes, under which Wasm modules get inlined as base64.
24
+ base64Cutoff?: number,
25
+ /// use asm.js instead of core WebAssembly for execution.
26
+ asm?: boolean,
27
+ /// minify the output JS.
28
+ minify?: boolean,
29
+ /// optimize the Component with Binaryen wasm-opt first.
30
+ optimize?: boolean,
31
+ /// if using optimize, custom optimization options
32
+ /// (defaults to best optimization, but this is very slow)
33
+ optArgs?: string[],
34
+ }
35
+
36
+ /**
37
+ * Transpile a Component into a JS-executable package
38
+ */
39
+ export function transpile(component: Uint8Array, opts?: TranspileOpts): Promise<{
40
+ files: Record<string, Uint8Array>,
41
+ imports: string[],
42
+ exports: [string, 'function' | 'instance'][]
43
+ }>;
44
+
45
+ /**
46
+ * Parse a WAT string into a Wasm binary
47
+ */
48
+ export function parse(wat: string): Uint8Array;
49
+
50
+ /**
51
+ * Print a Wasm binary as a WAT string
52
+ */
53
+ export function print(binary: Uint8Array | ArrayBuffer): string;
54
+
55
+ /**
56
+ * WIT Component - create a Component from a Wasm core binary
57
+ */
58
+ export function componentNew(binary: Uint8Array | ArrayBuffer, adapters?: [string, Uint8Array][] | null): Uint8Array;
59
+
60
+ type Metadata = [string, [string, string][]][];
61
+
62
+ /**
63
+ * Embed a world into a Wasm core binary
64
+ */
65
+ export function componentEmbed(binary: Uint8Array | ArrayBuffer | null, wit: string, opts: {
66
+ stringEncoding?: 'utf8' | 'utf16' | 'compact-utf16',
67
+ dummy?: boolean,
68
+ world?: string,
69
+ metadata?: Metadata
70
+ }): Uint8Array;
71
+
72
+ /**
73
+ * Extract the producer metadata for a Wasm component or core module
74
+ */
75
+ export function metadataShow(binary: Uint8Array | ArrayBuffer): {
76
+ name?: string,
77
+ metaType: { tag: 'module' } | { tag: 'component', val: number },
78
+ metadata: Metadata
79
+ }[];
80
+
81
+ /**
82
+ * Extract the producer metadata for a Wasm component or core module
83
+ */
84
+ export function metadataAdd(binary: Uint8Array | ArrayBuffer, metadata: Metadata): Uint8Array;
85
+
86
+ /**
87
+ * Extract the WIT world from a Wasm Component
88
+ */
89
+ export function componentWit(binary: Uint8Array | ArrayBuffer, document?: string | null): string;
90
+
91
+ export type StringEncoding = 'utf8' | 'utf16' | 'compact-utf16';
92
+
93
+ export const $init: Promise<void>;