-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from polywrap/embed-wraps-from-wrapscan
chore: embed wraps from wrapscan.io
- Loading branch information
Showing
8 changed files
with
612 additions
and
694 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | ||
|
@@ -52,6 +112,10 @@ export const wasmPackage = WasmPackage.from( | |
` | ||
); | ||
} | ||
|
||
if (fail) { | ||
throw Error("Failed to embed all wraps.") | ||
} | ||
} | ||
|
||
main() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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), | ||
|
Binary file modified
BIN
+2 Bytes
(100%)
packages/config-bundles/sys/src/embeds/ipfs-http-client/wrap.info
Binary file not shown.
2 changes: 1 addition & 1 deletion
2
packages/config-bundles/sys/src/embeds/ipfs-http-client/wrap.ts
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.