Skip to content

Instantly share code, notes, and snippets.

@Kattoor
Created June 11, 2025 10:53
Show Gist options
  • Save Kattoor/b476164f13d763f008261c28b02901e6 to your computer and use it in GitHub Desktop.
Save Kattoor/b476164f13d763f008261c28b02901e6 to your computer and use it in GitHub Desktop.
dump-bitcraft-wasm.js
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
import fs from 'fs';
import {WebSocket} from 'ws';
import {execSync} from 'child_process';
async function getIdentityToken() {
const response = await fetch('https://131.153.155.197/v1/identity', {method: 'POST'});
const {token: identityToken} = await response.json();
return identityToken;
}
function getWasmModules(identityToken) {
return new Promise((resolve) => {
const headers = {Authorization: `Bearer ${identityToken}`};
const ws = new WebSocket(
'wss://131.153.155.197/v1/database/spacetime-control/subscribe',
'v1.json.spacetimedb',
{headers});
ws.on('open', () => {
const subscriptionMessage = {
Subscribe: {
query_strings: ['SELECT * FROM program;'],
request_id: 1
}
};
ws.send(JSON.stringify(subscriptionMessage));
});
ws.on('message', (data) => {
const message = JSON.parse(data.toString());
if (message.InitialSubscription) {
const inserts =
message
.InitialSubscription
.database_update
.tables[0]
.updates[0]
.inserts;
const wasmModules =
inserts
.map(JSON.parse)
.map((insert) => ({
hash: insert.hash,
wasmBuffer: Buffer.from(String(insert.bytes), 'hex')
}))
.filter(({wasmBuffer}) => wasmBuffer.length > 0);
resolve(wasmModules);
ws.close();
}
});
});
}
function saveWasmModulesAndGenerateClient(wasmModules) {
for (const {hash, wasmBuffer} of wasmModules) {
const outputPath = `./out/${hash}`;
fs.mkdirSync(outputPath, {recursive: true});
fs.writeFileSync(`${outputPath}/module.wasm`, wasmBuffer);
execSync('spacetime generate ' +
'--lang typescript ' +
`--out-dir ${outputPath}/module_bindings ` +
`--bin-path ${outputPath}/module.wasm`);
}
}
const identityToken = await getIdentityToken();
const wasmModules = await getWasmModules(identityToken);
saveWasmModulesAndGenerateClient(wasmModules);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment