Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/componentize.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { fileURLToPath, URL } from 'node:url';
import { cwd, stdout, platform } from 'node:process';
import { spawnSync } from 'node:child_process';
import { tmpdir } from 'node:os';
import { resolve, join, dirname } from 'node:path';
import { resolve, join, dirname, relative } from 'node:path';
import { readFile, writeFile, mkdir, rm, stat } from 'node:fs/promises';
import { rmSync, existsSync } from 'node:fs';
import { createHash } from 'node:crypto';
Expand Down Expand Up @@ -249,11 +249,18 @@ export async function componentize(
sourcePath = sourceName;
}
let currentDir = maybeWindowsPath(cwd());

if (workspacePrefix.startsWith(currentDir)) {
workspacePrefix = currentDir;
sourcePath = sourcePath.slice(workspacePrefix.length + 1);
}

// Ensure source path is relative to workspacePrefix for the args list,
// regardless of the directory
if (resolve(sourcePath).startsWith(resolve(workspacePrefix))) {
sourcePath = relative(workspacePrefix, sourcePath);
}

let args = `--initializer-script-path ${initializerPath} --strip-path-prefix ${workspacePrefix}/ ${sourcePath}`;
runtimeArgs = runtimeArgs ? `${runtimeArgs} ${args}` : args;

Expand Down
39 changes: 39 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
import { fileURLToPath, URL } from 'node:url';
import { copyFile, mkdtemp } from 'node:fs/promises';

import { suite, test, assert } from 'vitest';

import { setupComponent } from "./util.js";

import {
DEBUG_TRACING_ENABLED,
DEBUG_TEST_ENABLED,
} from './util.js';

suite('API', () => {
// When using a different directory sourcePath as an arg to componentize()
// (called via setupComponent() in this test), wizer would fail to initialize the component
// due to a missing file -- the path prefix stripping was not correctly being resolved.
test('componentize() in a different dir', async () => {
const tmpDir = await mkdtemp(join(tmpdir(), 'componentize-diff-dir-'));
const outputPath = join(tmpDir, "index.js");
await copyFile(resolve('./test/api/index.js'), outputPath);
const { instance } = await setupComponent({
componentize: {
opts: {
sourcePath: outputPath,
witPath: fileURLToPath(new URL('./wit', import.meta.url)),
worldName: 'test1',
debugBuild: DEBUG_TEST_ENABLED,
},
},
transpile: {
opts: {
tracing: DEBUG_TRACING_ENABLED,
},
},
});
});
});
45 changes: 45 additions & 0 deletions test/util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { join } from 'node:path';
import { env } from 'node:process';
import { readFile, readdir, mkdir, writeFile, mkdtemp } from 'node:fs/promises';
import { createServer } from 'node:net';

import { componentize } from '@bytecodealliance/componentize-js';
import { transpile } from '@bytecodealliance/jco';

export const DEBUG_TRACING_ENABLED = isEnabledEnvVar(env.DEBUG_TRACING);
export const LOG_DEBUGGING_ENABLED = isEnabledEnvVar(env.LOG_DEBUGGING);
export const DEBUG_TEST_ENABLED = isEnabledEnvVar(env.DEBUG_TEST);
Expand All @@ -18,3 +23,43 @@ export function maybeLogging(disableFeatures) {
}
return disableFeatures;
}

export async function setupComponent(opts) {
const componentizeSrc = opts?.componentize?.src;
const componentizeOpts = opts?.componentize?.opts;
const transpileOpts = opts?.transpile?.opts;

let component;
if (componentizeSrc) {
const srcBuild = await componentize(componentizeSrc, componentizeOpts);
component = srcBuild.component;
} else if (!componentizeSrc && componentizeOpts) {
const optsBuild = await componentize(componentizeOpts);
component = optsBuild.component;
} else {
throw new Error('no componentize options or src provided');
}

const outputDir = join('./test/output', 'wasi-test');
await mkdir(outputDir, { recursive: true });

await writeFile(join(outputDir, 'wasi.component.wasm'), component);

const { files } = await transpile(component, transpileOpts);

const wasiDir = join(outputDir, 'wasi');
const interfacesDir = join(wasiDir, 'interfaces');
await mkdir(interfacesDir, { recursive: true });

for (const file of Object.keys(files)) {
await writeFile(join(wasiDir, file), files[file]);
}

const componentJsPath = join(wasiDir, 'component.js');
var instance = await import(componentJsPath);

return {
instance,
outputDir,
};
}
41 changes: 2 additions & 39 deletions test/wasi.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { transpile } from '@bytecodealliance/jco';

import { suite, test, assert } from 'vitest';

import { setupComponent } from "./util.js";

import {
DEBUG_TRACING_ENABLED,
DEBUG_TEST_ENABLED,
Expand Down Expand Up @@ -85,42 +87,3 @@ suite('WASI', () => {
});
});

async function setupComponent(opts) {
const componentizeSrc = opts?.componentize?.src;
const componentizeOpts = opts?.componentize?.opts;
const transpileOpts = opts?.transpile?.opts;

let component;
if (componentizeSrc) {
const srcBuild = await componentize(componentizeSrc, componentizeOpts);
component = srcBuild.component;
} else if (!componentizeSrc && componentizeOpts) {
const optsBuild = await componentize(componentizeOpts);
component = optsBuild.component;
} else {
throw new Error('no componentize options or src provided');
}

const outputDir = join('./test/output', 'wasi-test');
await mkdir(outputDir, { recursive: true });

await writeFile(join(outputDir, 'wasi.component.wasm'), component);

const { files } = await transpile(component, transpileOpts);

const wasiDir = join(outputDir, 'wasi');
const interfacesDir = join(wasiDir, 'interfaces');
await mkdir(interfacesDir, { recursive: true });

for (const file of Object.keys(files)) {
await writeFile(join(wasiDir, file), files[file]);
}

const componentJsPath = join(wasiDir, 'component.js');
var instance = await import(componentJsPath);

return {
instance,
outputDir,
};
}
Loading