@bytecodealliance/jco 1.16.1 → 1.17.1

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/src/cmd/run.js CHANGED
@@ -1,13 +1,13 @@
1
- import { rm, mkdir, writeFile, symlink } from 'node:fs/promises';
2
- import { basename, resolve, extname } from 'node:path';
3
- import { spawn } from 'node:child_process';
4
- import process from 'node:process';
5
- import { fileURLToPath, pathToFileURL } from 'node:url';
1
+ import { rm, mkdir, writeFile, symlink } from "node:fs/promises";
2
+ import { basename, resolve, extname } from "node:path";
3
+ import { spawn } from "node:child_process";
4
+ import process from "node:process";
5
+ import { fileURLToPath, pathToFileURL } from "node:url";
6
6
 
7
- import { getTmpDir, styleText } from '../common.js';
8
- import { transpile } from './transpile.js';
7
+ import { getTmpDir, styleText } from "../common.js";
8
+ import { transpile } from "./transpile.js";
9
9
 
10
- const DEFAULT_SERVE_HOST = 'localhost';
10
+ const DEFAULT_SERVE_HOST = "localhost";
11
11
 
12
12
  export async function run(componentPath, args, opts) {
13
13
  // Ensure that `args` is an array
@@ -31,7 +31,7 @@ export async function run(componentPath, args, opts) {
31
31
  console.error(e);
32
32
  process.exit(1);
33
33
  }
34
- `
34
+ `,
35
35
  );
36
36
  }
37
37
 
@@ -40,7 +40,7 @@ export async function serve(componentPath, args, opts) {
40
40
  let { port, host } = opts;
41
41
  if (port === undefined) {
42
42
  tryFindPort = true;
43
- port = '8000';
43
+ port = "8000";
44
44
  }
45
45
  // Ensure that `args` is an array
46
46
  args = [...args];
@@ -54,8 +54,8 @@ export async function serve(componentPath, args, opts) {
54
54
  const server = new HTTPServer(mod.incomingHandler);
55
55
  let port = ${port};
56
56
  ${
57
- tryFindPort
58
- ? `
57
+ tryFindPort
58
+ ? `
59
59
  while (true) {
60
60
  try {
61
61
  server.listen(port, ${JSON.stringify(host)});
@@ -67,19 +67,17 @@ export async function serve(componentPath, args, opts) {
67
67
  port++;
68
68
  }
69
69
  `
70
- : `server.listen(port, ${JSON.stringify(host)})`
71
- }
70
+ : `server.listen(port, ${JSON.stringify(host)})`
71
+ }
72
72
  console.error(\`Server listening @ ${host}:${port}...\`);
73
- `
73
+ `,
74
74
  );
75
75
  }
76
76
 
77
77
  async function runComponent(componentPath, args, opts, executor) {
78
78
  const jcoImport = opts.jcoImport ? resolve(opts.jcoImport) : null;
79
79
 
80
- const name = basename(
81
- componentPath.slice(0, -extname(componentPath).length || Infinity)
82
- );
80
+ const name = basename(componentPath.slice(0, -extname(componentPath).length || Infinity));
83
81
  const outDir = opts.jcoDir || (await getTmpDir());
84
82
  if (opts.jcoDir) {
85
83
  await mkdir(outDir, { recursive: true });
@@ -98,57 +96,48 @@ async function runComponent(componentPath, args, opts, executor) {
98
96
  importBindings: opts.jcoImportBindings,
99
97
  });
100
98
  } catch (e) {
101
- throw new Error('Unable to transpile command for execution', {
99
+ throw new Error("Unable to transpile command for execution", {
102
100
  cause: e,
103
101
  });
104
102
  }
105
103
 
106
- await writeFile(
107
- resolve(outDir, 'package.json'),
108
- JSON.stringify({ type: 'module' })
109
- );
104
+ await writeFile(resolve(outDir, "package.json"), JSON.stringify({ type: "module" }));
110
105
 
111
106
  let preview2ShimPath;
112
107
  try {
113
108
  preview2ShimPath = resolve(
114
- fileURLToPath(
115
- import.meta.resolve('@bytecodealliance/preview2-shim')
116
- ),
117
- '../../../'
109
+ fileURLToPath(import.meta.resolve("@bytecodealliance/preview2-shim")),
110
+ "../../../",
118
111
  );
119
112
  } catch (err) {
120
- let msg = `${styleText(['red', 'bold'], 'error')} Failed to resolve ${styleText('bold', '@bytecodealliance/preview2-shim')}, ensure it is installed.`;
113
+ let msg = `${styleText(["red", "bold"], "error")} Failed to resolve ${styleText("bold", "@bytecodealliance/preview2-shim")}, ensure it is installed.`;
121
114
  msg += `\nERROR:\n${err.toString()}`;
122
115
  throw new Error(msg);
123
116
  }
124
117
 
125
- const modulesDir = resolve(outDir, 'node_modules', '@bytecodealliance');
118
+ const modulesDir = resolve(outDir, "node_modules", "@bytecodealliance");
126
119
  await mkdir(modulesDir, { recursive: true });
127
120
 
128
121
  try {
129
- await symlink(
130
- preview2ShimPath,
131
- resolve(modulesDir, 'preview2-shim'),
132
- 'dir'
133
- );
122
+ await symlink(preview2ShimPath, resolve(modulesDir, "preview2-shim"), "dir");
134
123
  } catch (e) {
135
- if (e.code !== 'EEXIST') {
124
+ if (e.code !== "EEXIST") {
136
125
  throw e;
137
126
  }
138
127
  }
139
128
 
140
- const runPath = resolve(outDir, '_run.js');
129
+ const runPath = resolve(outDir, "_run.js");
141
130
  await writeFile(
142
131
  runPath,
143
132
  `
144
- ${jcoImport ? `import ${JSON.stringify(pathToFileURL(jcoImport))}` : ''}
133
+ ${jcoImport ? `import ${JSON.stringify(pathToFileURL(jcoImport))}` : ""}
145
134
  import process from 'node:process';
146
135
  try {
147
136
  process.argv[1] = "${name}";
148
137
  } catch {}
149
138
  const mod = await import('./${name}.js');
150
139
  ${executor}
151
- `
140
+ `,
152
141
  );
153
142
 
154
143
  const nodePath = process.env.JCO_RUN_PATH || process.argv[0];
@@ -156,18 +145,12 @@ async function runComponent(componentPath, args, opts, executor) {
156
145
  process.exitCode = await new Promise((resolve, reject) => {
157
146
  const cp = spawn(
158
147
  nodePath,
159
- [
160
- ...(process.env.JCO_RUN_ARGS
161
- ? process.env.JCO_RUN_ARGS.split(' ')
162
- : []),
163
- runPath,
164
- ...args,
165
- ],
166
- { stdio: 'inherit' }
148
+ [...(process.env.JCO_RUN_ARGS ? process.env.JCO_RUN_ARGS.split(" ") : []), runPath, ...args],
149
+ { stdio: "inherit" },
167
150
  );
168
151
 
169
- cp.on('error', reject);
170
- cp.on('exit', resolve);
152
+ cp.on("error", reject);
153
+ cp.on("exit", resolve);
171
154
  });
172
155
  } finally {
173
156
  try {
@@ -1,4 +1,4 @@
1
- import type { Command } from 'commander';
1
+ import type { Command } from "commander";
2
2
 
3
3
  export function types(witPath: any, opts: any): Promise<void>;
4
4
 
@@ -20,22 +20,24 @@ export function guestTypes(witPath: any, opts: any): Promise<void>;
20
20
  * }} opts
21
21
  * @returns {Promise<{ [filename: string]: Uint8Array }>}
22
22
  */
23
- export function typesComponent(witPath: string, opts: {
24
- name?: string;
25
- worldName?: string;
26
- instantiation?: "async" | "sync";
27
- tlaCompat?: boolean;
28
- asyncMode?: string;
29
- asyncImports?: string[];
30
- asyncExports?: string[];
31
- outDir?: string;
32
- features?: string[] | "all";
33
- guest?: boolean;
34
- }): Promise<{
23
+ export function typesComponent(
24
+ witPath: string,
25
+ opts: {
26
+ name?: string;
27
+ worldName?: string;
28
+ instantiation?: "async" | "sync";
29
+ tlaCompat?: boolean;
30
+ asyncMode?: string;
31
+ asyncImports?: string[];
32
+ asyncExports?: string[];
33
+ outDir?: string;
34
+ features?: string[] | "all";
35
+ guest?: boolean;
36
+ },
37
+ ): Promise<{
35
38
  [filename: string]: Uint8Array;
36
39
  }>;
37
40
 
38
-
39
41
  export function transpile(
40
42
  componentPath: string,
41
43
  opts: TranspilationOptions,
@@ -44,8 +46,8 @@ export function transpile(
44
46
 
45
47
  type TranspilationOptions = {
46
48
  name: string;
47
- instantiation?: 'async' | 'sync';
48
- importBindings?: 'js' | 'optimized' | 'hybrid' | 'direct-optimized';
49
+ instantiation?: "async" | "sync";
50
+ importBindings?: "js" | "optimized" | "hybrid" | "direct-optimized";
49
51
  map?: Record<string, string>;
50
52
  asyncMode?: string;
51
53
  asyncImports?: string[];
@@ -70,7 +72,7 @@ type TranspilationOptions = {
70
72
  type TranspilationResult = {
71
73
  files: FileBytes;
72
74
  imports: string[];
73
- exports: [string, 'function' | 'instance'][];
75
+ exports: [string, "function" | "instance"][];
74
76
  };
75
77
 
76
78
  /**
@@ -100,28 +102,31 @@ type TranspilationResult = {
100
102
  * }} opts
101
103
  * @returns {Promise<{ files: { [filename: string]: Uint8Array }, imports: string[], exports: [string, 'function' | 'instance'][] }>}
102
104
  */
103
- export function transpileComponent(component: Uint8Array, opts?: {
104
- name: string;
105
- instantiation?: "async" | "sync";
106
- importBindings?: "js" | "optimized" | "hybrid" | "direct-optimized";
107
- map?: Record<string, string>;
108
- asyncMode?: string;
109
- asyncImports?: string[];
110
- asyncExports?: string[];
111
- validLiftingOptimization?: boolean;
112
- tracing?: boolean;
113
- nodejsCompat?: boolean;
114
- tlaCompat?: boolean;
115
- base64Cutoff?: boolean;
116
- js?: boolean;
117
- minify?: boolean;
118
- optimize?: boolean;
119
- namespacedExports?: boolean;
120
- outDir?: string;
121
- multiMemory?: boolean;
122
- experimentalIdlImports?: boolean;
123
- optArgs?: string[];
124
- }): Promise<{
105
+ export function transpileComponent(
106
+ component: Uint8Array,
107
+ opts?: {
108
+ name: string;
109
+ instantiation?: "async" | "sync";
110
+ importBindings?: "js" | "optimized" | "hybrid" | "direct-optimized";
111
+ map?: Record<string, string>;
112
+ asyncMode?: string;
113
+ asyncImports?: string[];
114
+ asyncExports?: string[];
115
+ validLiftingOptimization?: boolean;
116
+ tracing?: boolean;
117
+ nodejsCompat?: boolean;
118
+ tlaCompat?: boolean;
119
+ base64Cutoff?: boolean;
120
+ js?: boolean;
121
+ minify?: boolean;
122
+ optimize?: boolean;
123
+ namespacedExports?: boolean;
124
+ outDir?: string;
125
+ multiMemory?: boolean;
126
+ experimentalIdlImports?: boolean;
127
+ optArgs?: string[];
128
+ },
129
+ ): Promise<{
125
130
  files: {
126
131
  [filename: string]: Uint8Array;
127
132
  };