Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(sia_js): universal JS SDK #73

Open
wants to merge 1 commit into
base: nate/js-sdk
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion sia_js/example/nodejs/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Seed, UnlockConditions } from "sia_js";
import { Seed, UnlockConditions, Transaction } from "sia_js";

try {
const s = Seed.generate(),
Expand Down
1 change: 1 addition & 0 deletions sia_js/example/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"author": "",
"license": "MIT",
"description": "",
"type":"module",
"dependencies": {
"sia_js": "file:../../pkg"
}
Expand Down
36 changes: 36 additions & 0 deletions sia_js/scripts/patch.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import fs from 'node:fs/promises';

const packageName = 'sia_js';
const wasmFilename = 'sia_js_bg.wasm';
const jsFilename = 'sia_js.js';

const rawWasmFile = await fs.readFile(`pkg/${wasmFilename}`);
const origJsFile = await fs.readFile(`pkg/${jsFilename}`, 'utf8');

const base64 = rawWasmFile.toString('base64');

// Remove NodeJS specific APIs and inline WASM.
const patchedJsFile = origJsFile
// TextEncoder and TextDecoder are globally available in NodeJS and browsers.
// inspect and inspect.custom are NodeJS specific APIs, replace with polyfill.
.replace('const { TextEncoder, TextDecoder, inspect } = require(`util`);',`
const inspect = (obj) => JSON.stringify(obj, null, 2); `)
.replace('[inspect.custom]', `[Symbol.for('nodejs.util.inspect.custom')]`)
// Inline WASM.
.replace(`const path = require('path').join(__dirname, '${wasmFilename}');`, '')
.replace(`const bytes = require('fs').readFileSync(path);`, `
const wasmBase64 = '${base64}';
const bytes = Uint8Array.from(atob(wasmBase64), c => c.charCodeAt(0));`);

await fs.writeFile(`pkg/${jsFilename}`, patchedJsFile);

// Remove WASM files.
await fs.unlink(`pkg/${wasmFilename}`);
await fs.unlink(`pkg/${wasmFilename}.d.ts`);

// Remove WASM from .files section of package.json.
const pkgJsonFile = await fs.readFile('pkg/package.json', 'utf8');
const pkgJson = JSON.parse(pkgJsonFile);
pkgJson.name = packageName;
pkgJson.files = pkgJson.files.filter(file => file !== wasmFilename);
await fs.writeFile('pkg/package.json', JSON.stringify(pkgJson, null, 2));
Loading