11import fs from "fs" ;
22import path from "path" ;
3- import { deserializeWrapManifest } from "@polywrap/wrap-manifest-types-js" ;
3+ import rimraf from "rimraf" ;
4+ import {
5+ PolywrapClient ,
6+ PolywrapClientConfigBuilder ,
7+ Uri
8+ } from "@polywrap/client-js" ;
9+
10+ const embeds = {
11+ "async-ipfs-resolver" : "wrapscan.io/polywrap/ipfs-uri-resolver-async@1.0" ,
12+ "file-system-resolver" : "wrapscan.io/polywrap/file-system-uri-resolver@1.0" ,
13+ "http-resolver" : "wrapscan.io/polywrap/http-uri-resolver@1.0" ,
14+ "ipfs-http-client" : "https://wraps.wrapscan.io/r/polywrap/ipfs-http-client@1.0"
15+ } ;
16+
17+ function toBase64 ( data : string | Uint8Array ) : string {
18+ if ( typeof data === "string" ) {
19+ // Convert string to base64
20+ return btoa ( unescape ( encodeURIComponent ( data ) ) ) ;
21+ } else if ( data instanceof Uint8Array ) {
22+ // Convert Uint8Array to base64
23+ const binaryString = Array . prototype . map . call ( data , ( char : number ) => String . fromCharCode ( char ) ) . join ( '' ) ;
24+ return btoa ( binaryString ) ;
25+ }
26+ throw new Error ( 'Invalid data type' ) ;
27+ }
428
529async function main ( ) {
630
731 const embedsDir = path . join ( __dirname , "../src/embeds" ) ;
8- const embedsDirents = fs . readdirSync ( embedsDir , { withFileTypes : true } ) ;
32+ const config = new PolywrapClientConfigBuilder ( )
33+ . addDefaults ( ) ;
34+
35+ // Remove any embed redirects that may exist
36+ for ( const embedUri of Object . values ( embeds ) ) {
37+ config . removeRedirect ( embedUri ) ;
38+ }
39+
40+ const client = new PolywrapClient ( config . build ( ) ) ;
41+ let fail = false ;
942
10- const wrapperDirs : string [ ] = [ ] ;
43+ for ( const [ embedName , embedUri ] of Object . entries ( embeds ) ) {
1144
12- for ( const dirent of embedsDirents ) {
13- if ( dirent . isDirectory ( ) ) {
14- wrapperDirs . push ( path . join ( embedsDir , dirent . name ) ) ;
45+ const logError = ( message : string ) => {
46+ fail = true ;
47+ console . error ( message ) ;
1548 }
16- }
1749
18- for ( const wrapperDir of wrapperDirs ) {
19- const wasmBytes = fs . readFileSync (
20- path . join ( wrapperDir , "wrap.wasm" )
21- ) ;
22- const infoBytes = fs . readFileSync (
23- path . join ( wrapperDir , "wrap.info" )
24- ) ;
50+ const result = await client . loadWrapper ( Uri . from ( embedUri ) ) ;
2551
26- try {
27- // Make sure we can load the wasm module
28- await deserializeWrapManifest ( infoBytes ) ;
29- } catch ( err ) {
30- throw Error ( `Unable to load wrapper at ${ wrapperDir } ` ) ;
52+ if ( ! result . ok ) {
53+ logError ( `Failed to load ${ embedUri } ` ) ;
54+ continue ;
3155 }
3256
57+ const wrap = result . value ;
58+
59+ const files = await Promise . all ( [
60+ wrap . getFile ( { path : "wrap.info" } ) . then ( ( result ) => {
61+ if ( ! result . ok ) {
62+ logError ( `Failed to load wrap.info from ${ embedUri } ` ) ;
63+ return undefined ;
64+ }
65+ return result . value ;
66+ } ) ,
67+ wrap . getFile ( { path : "wrap.wasm" } ) . then ( ( result ) => {
68+ if ( ! result . ok ) {
69+ logError ( `Failed to load wrap.wasm from ${ embedUri } ` ) ;
70+ return undefined ;
71+ }
72+ return result . value ;
73+ } ) ,
74+ ] ) ;
75+ const wrapInfo = files [ 0 ] ;
76+ const wrapWasm = files [ 1 ] ;
77+
78+ if ( ! wrapInfo || ! wrapWasm ) {
79+ continue ;
80+ }
81+
82+ const wrapDir = path . join ( embedsDir , embedName ) ;
83+ rimraf . sync ( wrapDir ) ;
84+ fs . mkdirSync ( wrapDir ) ;
85+ fs . writeFileSync (
86+ path . join ( wrapDir , "wrap.wasm" ) ,
87+ wrapWasm
88+ ) ;
89+ fs . writeFileSync (
90+ path . join ( wrapDir , "wrap.info" ) ,
91+ wrapInfo
92+ ) ;
3393 fs . writeFileSync (
34- path . join ( wrapperDir , "wrap.ts" ) ,
94+ path . join ( wrapDir , "wrap.ts" ) ,
3595`// NOTE: This file is auto-generated, do not modify by hand!
3696// See: ./scripts/embed-wrappers.ts
3797import { WasmPackage } from "@polywrap/wasm-js";
3898import toUint8Array from "base64-to-uint8array";
3999
40100const wrap_wasm = toUint8Array(
41- "${ wasmBytes . toString ( "base64" ) } "
101+ "${ toBase64 ( wrapWasm ) } "
42102);
43103
44104const wrap_info = toUint8Array(
45- "${ infoBytes . toString ( "base64" ) } "
105+ "${ toBase64 ( wrapInfo ) } "
46106);
47107
48108export const wasmPackage = WasmPackage.from(
@@ -52,6 +112,10 @@ export const wasmPackage = WasmPackage.from(
52112`
53113 ) ;
54114 }
115+
116+ if ( fail ) {
117+ throw Error ( "Failed to embed all wraps." )
118+ }
55119}
56120
57121main ( )
0 commit comments