1
1
import fs from "fs" ;
2
2
import 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/[email protected] " ,
12
+ "file-system-resolver" :
"wrapscan.io/polywrap/[email protected] " ,
13
+ "http-resolver" :
"wrapscan.io/polywrap/[email protected] " ,
14
+ "ipfs-http-client" :
"https://wraps.wrapscan.io/r/polywrap/[email protected] "
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
+ }
4
28
5
29
async function main ( ) {
6
30
7
31
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 ;
9
42
10
- const wrapperDirs : string [ ] = [ ] ;
43
+ for ( const [ embedName , embedUri ] of Object . entries ( embeds ) ) {
11
44
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 ) ;
15
48
}
16
- }
17
49
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 ) ) ;
25
51
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 ;
31
55
}
32
56
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
+ ) ;
33
93
fs . writeFileSync (
34
- path . join ( wrapperDir , "wrap.ts" ) ,
94
+ path . join ( wrapDir , "wrap.ts" ) ,
35
95
`// NOTE: This file is auto-generated, do not modify by hand!
36
96
// See: ./scripts/embed-wrappers.ts
37
97
import { WasmPackage } from "@polywrap/wasm-js";
38
98
import toUint8Array from "base64-to-uint8array";
39
99
40
100
const wrap_wasm = toUint8Array(
41
- "${ wasmBytes . toString ( "base64" ) } "
101
+ "${ toBase64 ( wrapWasm ) } "
42
102
);
43
103
44
104
const wrap_info = toUint8Array(
45
- "${ infoBytes . toString ( "base64" ) } "
105
+ "${ toBase64 ( wrapInfo ) } "
46
106
);
47
107
48
108
export const wasmPackage = WasmPackage.from(
@@ -52,6 +112,10 @@ export const wasmPackage = WasmPackage.from(
52
112
`
53
113
) ;
54
114
}
115
+
116
+ if ( fail ) {
117
+ throw Error ( "Failed to embed all wraps." )
118
+ }
55
119
}
56
120
57
121
main ( )
0 commit comments