Skip to content

Commit 2e367f9

Browse files
committed
feat: add option to define remote url
1 parent 4ccc31b commit 2e367f9

File tree

3 files changed

+61
-9
lines changed

3 files changed

+61
-9
lines changed

src/core/generate/index.ts

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { basename, resolve } from 'node:path';
22
import { prepareBinding } from './bindings.ts';
33
import { ensureDir, writeFileSafe } from './fs.ts';
4+
import type { DidFile } from './rs/dist/icp-js-bindgen';
45
import { type WasmGenerateResult, wasmGenerate, wasmInit } from './rs.ts';
56

67
const DID_FILE_EXTENSION = '.did';
@@ -59,7 +60,11 @@ export type GenerateOptions = {
5960
/**
6061
* The path to the `.did` file.
6162
*/
62-
didFile: string;
63+
didFile?: string;
64+
/**
65+
*
66+
*/
67+
didRemoteUrl?: string;
6368
/**
6469
* The path to the directory where the bindings will be generated.
6570
*/
@@ -93,6 +98,7 @@ export async function generate(options: GenerateOptions) {
9398

9499
const {
95100
didFile,
101+
didRemoteUrl,
96102
outDir,
97103
output = {
98104
force: false,
@@ -108,15 +114,56 @@ export async function generate(options: GenerateOptions) {
108114
const force = Boolean(output.force); // ensure force is a boolean
109115
const declarationsRootExports = Boolean(output.declarations?.rootExports ?? false); // ensure rootExports is a boolean
110116

111-
const didFilePath = resolve(didFile);
112-
const outputFileName = basename(didFile, DID_FILE_EXTENSION);
117+
if (didFile && didRemoteUrl) {
118+
throw new Error('Only one of didFile or didRemoteUrl should be provided.');
119+
}
120+
121+
function fromDidFile(didFile: string): {
122+
did_file: DidFile;
123+
service_name: string;
124+
} {
125+
return {
126+
did_file: { LocalPath: resolve(didFile) },
127+
service_name: basename(didFile, DID_FILE_EXTENSION),
128+
};
129+
}
130+
131+
async function fromDidRemoteUrl(didRemoteUrl: string): Promise<{
132+
did_file: DidFile;
133+
service_name: string;
134+
}> {
135+
const u = new URL(didRemoteUrl);
136+
const fileName = u.pathname.split('/').pop();
137+
const r = await fetch(didRemoteUrl);
138+
if (!r.ok) {
139+
throw new Error(
140+
`Failed to fetch .did file from URL: ${didRemoteUrl}. Status: ${r.status} ${r.statusText}`,
141+
);
142+
}
143+
const didFile = await r.text();
144+
return {
145+
did_file: { InlineString: didFile },
146+
service_name: fileName || 'service',
147+
};
148+
}
149+
150+
let did_file: DidFile;
151+
let service_name: string;
152+
153+
if (didFile) {
154+
({ did_file, service_name } = fromDidFile(didFile));
155+
} else if (didRemoteUrl) {
156+
({ did_file, service_name } = await fromDidRemoteUrl(didRemoteUrl));
157+
} else {
158+
throw new Error('Either didFile or didRemoteUrl must be provided.');
159+
}
113160

114161
await ensureDir(outDir);
115162
await ensureDir(resolve(outDir, 'declarations'));
116163

117164
const result = wasmGenerate({
118-
did_file: { LocalPath: didFilePath },
119-
service_name: outputFileName,
165+
did_file,
166+
service_name,
120167
declarations: {
121168
root_exports: declarationsRootExports,
122169
},
@@ -125,7 +172,7 @@ export async function generate(options: GenerateOptions) {
125172
await writeBindings({
126173
bindings: result,
127174
outDir,
128-
outputFileName,
175+
outputFileName: service_name,
129176
output,
130177
force,
131178
});

src/plugins/vite.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ export function icpBindgen(options: Options): Plugin {
104104
}
105105

106106
function watchDidFileChanges(server: ViteDevServer, options: Options) {
107+
if (!options.didFile) {
108+
return;
109+
}
110+
107111
const didFilePath = resolve(options.didFile);
108112

109113
server.watcher.add(didFilePath);
@@ -115,7 +119,8 @@ function watchDidFileChanges(server: ViteDevServer, options: Options) {
115119
}
116120

117121
async function run(options: Options) {
118-
console.log(cyan(`[${VITE_PLUGIN_NAME}] Generating bindings from`), green(options.didFile));
122+
const source = options.didFile || options.didRemoteUrl || 'unknown source';
123+
console.log(cyan(`[${VITE_PLUGIN_NAME}] Generating bindings from`), green(source));
119124

120125
await generate({
121126
didFile: options.didFile,

tests/wasm-generate.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('wasmGenerate', () => {
1616
const snapshotsDir = `${SNAPSHOTS_BASE_DIR}/no-root-export`;
1717

1818
const result = wasmGenerate({
19-
did_file_path: didFile,
19+
did_file: { LocalPath: didFile },
2020
service_name: serviceName,
2121
declarations: {
2222
root_exports: false,
@@ -43,7 +43,7 @@ describe('wasmGenerate', () => {
4343
const snapshotsDir = `${SNAPSHOTS_BASE_DIR}/root-export`;
4444

4545
const result = wasmGenerate({
46-
did_file_path: didFile,
46+
did_file: { LocalPath: didFile },
4747
service_name: serviceName,
4848
declarations: {
4949
root_exports: true,

0 commit comments

Comments
 (0)