1+ import { join } from 'node:path' ;
12import { env } from 'node:process' ;
3+ import { readFile , readdir , mkdir , writeFile , mkdtemp } from 'node:fs/promises' ;
24import { createServer } from 'node:net' ;
35
6+ import { componentize } from '@bytecodealliance/componentize-js' ;
7+ import { transpile } from '@bytecodealliance/jco' ;
8+
49export const DEBUG_TRACING_ENABLED = isEnabledEnvVar ( env . DEBUG_TRACING ) ;
510export const LOG_DEBUGGING_ENABLED = isEnabledEnvVar ( env . LOG_DEBUGGING ) ;
611export const DEBUG_TEST_ENABLED = isEnabledEnvVar ( env . DEBUG_TEST ) ;
@@ -18,3 +23,43 @@ export function maybeLogging(disableFeatures) {
1823 }
1924 return disableFeatures ;
2025}
26+
27+ export async function setupComponent ( opts ) {
28+ const componentizeSrc = opts ?. componentize ?. src ;
29+ const componentizeOpts = opts ?. componentize ?. opts ;
30+ const transpileOpts = opts ?. transpile ?. opts ;
31+
32+ let component ;
33+ if ( componentizeSrc ) {
34+ const srcBuild = await componentize ( componentizeSrc , componentizeOpts ) ;
35+ component = srcBuild . component ;
36+ } else if ( ! componentizeSrc && componentizeOpts ) {
37+ const optsBuild = await componentize ( componentizeOpts ) ;
38+ component = optsBuild . component ;
39+ } else {
40+ throw new Error ( 'no componentize options or src provided' ) ;
41+ }
42+
43+ const outputDir = join ( './test/output' , 'wasi-test' ) ;
44+ await mkdir ( outputDir , { recursive : true } ) ;
45+
46+ await writeFile ( join ( outputDir , 'wasi.component.wasm' ) , component ) ;
47+
48+ const { files } = await transpile ( component , transpileOpts ) ;
49+
50+ const wasiDir = join ( outputDir , 'wasi' ) ;
51+ const interfacesDir = join ( wasiDir , 'interfaces' ) ;
52+ await mkdir ( interfacesDir , { recursive : true } ) ;
53+
54+ for ( const file of Object . keys ( files ) ) {
55+ await writeFile ( join ( wasiDir , file ) , files [ file ] ) ;
56+ }
57+
58+ const componentJsPath = join ( wasiDir , 'component.js' ) ;
59+ var instance = await import ( componentJsPath ) ;
60+
61+ return {
62+ instance,
63+ outputDir,
64+ } ;
65+ }
0 commit comments