-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeploy.js
executable file
·116 lines (100 loc) · 3.34 KB
/
deploy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env node
const fs = require('fs');
const http = require('http');
class WorkerClient {
constructor(baseUrl) {
this.baseUrl = baseUrl;
}
async init() {
await this.initWorkerIfNot();
}
async initWorkerIfNot() {
console.log('Checking worker info...');
const workerInfo = await this.rpcCall("Info", {});
if (!workerInfo.session || workerInfo.session === "0x") {
console.log('No active session found, initializing worker...');
await this.rpcCall("WorkerInit", {});
console.log('Worker initialized.');
} else {
console.log('Active session found, worker already initialized.');
}
}
async uploadFile(fileName) {
console.log('Uploading file:', fileName);
const data = fs.readFileSync(fileName);
return await this.rpcCall("BlobPut", {
body: data.toString('hex')
});
}
async deploy(manifest) {
return await this.rpcCall("AppDeploy", { manifest });
}
async rpcCall(method, params) {
const url = `${this.baseUrl}/prpc/Operation.${method}?json`;
const response = await httpPost(url, params);
return JSON.parse(response);
}
}
function httpPost(url, jsonData) {
return new Promise((resolve, reject) => {
const data = JSON.stringify(jsonData);
const { hostname, pathname, port } = new URL(url);
const options = {
hostname,
port: port || 80,
path: pathname,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
};
const req = http.request(options, (res) => {
let responseData = '';
res.on('data', chunk => responseData += chunk);
res.on('end', () => {
if (res.statusCode >= 200 && res.statusCode < 300) {
resolve(responseData);
} else {
const errorMsg = `HTTP status code ${res.statusCode}: ${responseData}`;
reject(new Error(errorMsg));
}
});
});
req.on('error', error => {
reject(error);
});
req.write(data);
req.end();
});
}
async function main() {
const WAPOD_URL = process.env.WAPOD_URL || "http://127.0.0.1:8001";
const wasmFile = process.argv[2];
if (!wasmFile) {
console.error('Usage: deploy.js <wasm_file>');
console.error('Please provide a wasm file to deploy.');
process.exit(1);
}
try {
const client = new WorkerClient(WAPOD_URL);
await client.init();
const wasmFileInfo = await client.uploadFile(wasmFile);
const manifest = {
version: 1,
code_hash: wasmFileInfo.hash,
args: [],
env_vars: [["RUST_LOG", "debug"]],
on_demand: false,
resizable: true,
max_query_size: 10240,
label: "Test App",
};
console.log('Deploying app...');
const appInfo = await client.deploy(manifest);
console.log('App deployed, address is', appInfo.address);
} catch (error) {
console.error('An error occurred during the main process:', error);
}
}
main().catch(console.error);