Skip to content

Commit

Permalink
Merge pull request #77 from polywrap/embed-wraps-from-wrapscan
Browse files Browse the repository at this point in the history
chore: embed wraps from wrapscan.io
  • Loading branch information
dOrgJelli authored Aug 30, 2023
2 parents 2744918 + 24c0798 commit 59f8314
Show file tree
Hide file tree
Showing 8 changed files with 612 additions and 694 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
"reset": "yarn clean && (yarn || yarn || yarn) && yarn build",
"clean": "npx rimraf ./**/node_modules ./**/yarn.lock ./**/build ./**/coverage ./**/.polywrap",
"install:test-wraps": "lerna run generate:wraps --scope @polywrap/test-cases",
"build": "yarn build:common && yarn build:client && yarn install:test-wraps",
"build:common": "lerna run build --ignore @polywrap/core-client-js --ignore @polywrap/client-js --ignore @polywrap/uri-resolver-extensions-js --ignore @polywrap/client-config-builder-js --ignore @polywrap/*-config-bundle-js",
"build:client": "lerna run build --scope @polywrap/core-client-js --scope @polywrap/client-js --scope @polywrap/uri-resolver-extensions-js --scope @polywrap/client-config-builder-js --scope @polywrap/*-config-bundle-js",
"build": "yarn build:common && yarn build:bundles && yarn build:client && yarn install:test-wraps",
"build:common": "lerna run build --ignore @polywrap/*-config-bundle-js --ignore @polywrap/core-client-js --ignore @polywrap/client-js --ignore @polywrap/client-config-builder-js",
"build:bundles": "lerna run build --scope @polywrap/*-config-bundle-js",
"build:client": "lerna run build --scope @polywrap/core-client-js --scope @polywrap/client-js --scope @polywrap/client-config-builder-js",
"build:docs": "lerna run build:docs",
"lint": "lerna run lint",
"lint:fix": "lerna run lint -- --fix",
Expand Down
2 changes: 1 addition & 1 deletion packages/config-bundles/sys/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@polywrap/msgpack-js": "0.12.x"
},
"devDependencies": {
"@polywrap/wrap-manifest-types-js": "0.12.2",
"@polywrap/client-js": "~0.12.2",
"doc-snippets": "~1.0.0",
"rimraf": "3.0.2",
"ts-node": "10.9.1",
Expand Down
108 changes: 86 additions & 22 deletions packages/config-bundles/sys/scripts/embed-wraps.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,108 @@
import fs from "fs";
import path from "path";
import { deserializeWrapManifest } from "@polywrap/wrap-manifest-types-js";
import rimraf from "rimraf";
import {
PolywrapClient,
PolywrapClientConfigBuilder,
Uri
} from "@polywrap/client-js";

const embeds = {
"async-ipfs-resolver": "wrapscan.io/polywrap/[email protected]",
"file-system-resolver": "wrapscan.io/polywrap/[email protected]",
"http-resolver": "wrapscan.io/polywrap/[email protected]",
"ipfs-http-client": "https://wraps.wrapscan.io/r/polywrap/[email protected]"
};

function toBase64(data: string | Uint8Array): string {
if (typeof data === "string") {
// Convert string to base64
return btoa(unescape(encodeURIComponent(data)));
} else if (data instanceof Uint8Array) {
// Convert Uint8Array to base64
const binaryString = Array.prototype.map.call(data, (char: number) => String.fromCharCode(char)).join('');
return btoa(binaryString);
}
throw new Error('Invalid data type');
}

async function main() {

const embedsDir = path.join(__dirname, "../src/embeds");
const embedsDirents = fs.readdirSync(embedsDir, { withFileTypes: true });
const config = new PolywrapClientConfigBuilder()
.addDefaults();

// Remove any embed redirects that may exist
for (const embedUri of Object.values(embeds)) {
config.removeRedirect(embedUri);
}

const client = new PolywrapClient(config.build());
let fail = false;

const wrapperDirs: string[] = [];
for (const [embedName, embedUri] of Object.entries(embeds)) {

for (const dirent of embedsDirents) {
if (dirent.isDirectory()) {
wrapperDirs.push(path.join(embedsDir, dirent.name));
const logError = (message: string) => {
fail = true;
console.error(message);
}
}

for (const wrapperDir of wrapperDirs) {
const wasmBytes = fs.readFileSync(
path.join(wrapperDir, "wrap.wasm")
);
const infoBytes = fs.readFileSync(
path.join(wrapperDir, "wrap.info")
);
const result = await client.loadWrapper(Uri.from(embedUri));

try {
// Make sure we can load the wasm module
await deserializeWrapManifest(infoBytes);
} catch (err) {
throw Error(`Unable to load wrapper at ${wrapperDir}`);
if (!result.ok) {
logError(`Failed to load ${embedUri}`);
continue;
}

const wrap = result.value;

const files = await Promise.all([
wrap.getFile({ path: "wrap.info" }).then((result) => {
if (!result.ok) {
logError(`Failed to load wrap.info from ${embedUri}`);
return undefined;
}
return result.value;
}),
wrap.getFile({ path: "wrap.wasm" }).then((result) => {
if (!result.ok) {
logError(`Failed to load wrap.wasm from ${embedUri}`);
return undefined;
}
return result.value;
}),
]);
const wrapInfo = files[0];
const wrapWasm = files[1];

if (!wrapInfo || !wrapWasm) {
continue;
}

const wrapDir = path.join(embedsDir, embedName);
rimraf.sync(wrapDir);
fs.mkdirSync(wrapDir);
fs.writeFileSync(
path.join(wrapDir, "wrap.wasm"),
wrapWasm
);
fs.writeFileSync(
path.join(wrapDir, "wrap.info"),
wrapInfo
);
fs.writeFileSync(
path.join(wrapperDir, "wrap.ts"),
path.join(wrapDir, "wrap.ts"),
`// NOTE: This file is auto-generated, do not modify by hand!
// See: ./scripts/embed-wrappers.ts
import { WasmPackage } from "@polywrap/wasm-js";
import toUint8Array from "base64-to-uint8array";
const wrap_wasm = toUint8Array(
"${wasmBytes.toString("base64")}"
"${toBase64(wrapWasm)}"
);
const wrap_info = toUint8Array(
"${infoBytes.toString("base64")}"
"${toBase64(wrapInfo)}"
);
export const wasmPackage = WasmPackage.from(
Expand All @@ -52,6 +112,10 @@ export const wasmPackage = WasmPackage.from(
`
);
}

if (fail) {
throw Error("Failed to embed all wraps.")
}
}

main()
Expand Down
6 changes: 3 additions & 3 deletions packages/config-bundles/sys/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ export const bundle: SysCommonBundle = {
redirectFrom: ["wrapscan.io/polywrap/[email protected]"],
},
ipfsResolver: {
uri: "embed/async-ipfs-uri-resolver@1.0.0",
uri: "embed/ipfs-uri-resolver-async@1.0",
package: ipfsResolver.wasmPackage,
implements: [
"wrapscan.io/polywrap/async-[email protected]",
"wrapscan.io/polywrap/ipfs-uri-resolver-async@1.0",
ExtendableUriResolver.defaultExtInterfaceUris[0].uri,
],
redirectFrom: ["wrapscan.io/polywrap/async-[email protected]"],
redirectFrom: ["wrapscan.io/polywrap/ipfs-uri-resolver-async@1.0"],
env: {
provider: ipfsProviders[0],
fallbackProviders: ipfsProviders.slice(1),
Expand Down
Binary file not shown.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/tracing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@opentelemetry/sdk-trace-web": "1.6.0"
},
"devDependencies": {
"@types/node": "^18.14.6",
"@types/node": "~18.15.0",
"rimraf": "3.0.2",
"typescript": "4.9.5"
},
Expand Down
Loading

0 comments on commit 59f8314

Please sign in to comment.