@@ -2,12 +2,10 @@ import { normalizePath } from "./src/convert.ts";
22import { FileManager } from "./src/fileManager/in-pg-files.ts" ;
33import { PGMem } from "./src/pgMem.ts" ;
44import { SysCalls } from "./src/syscalls.ts" ;
5- import { ExitStatus } from "./src/utils.ts" ;
5+ import { ExitStatus , getTempDirBase } from "./src/utils.ts" ;
66import { WasmLoader } from "./src/wasmLoader.ts" ;
77import type { InPgOptions } from "./types.ts" ;
88
9- const fileData = Deno . readFileSync ( import . meta. dirname + `/src/inpg.data` ) ;
10- const wasmData = Deno . readFileSync ( import . meta. dirname + `/src/inpg.wasm` ) ;
119export class InPG implements Deno . Conn {
1210 pgMem : PGMem ;
1311 wasmLoader ;
@@ -38,6 +36,15 @@ export class InPG implements Deno.Conn {
3836 debug ?: boolean ;
3937 #onStdErr: ( message : any ) => void ;
4038 #onStdOut: ( message : any ) => void ;
39+ #wasmData: Uint8Array = new Uint8Array ( 0 ) ;
40+ #fileData: Uint8Array = new Uint8Array ( 0 ) ;
41+ get wasmData ( ) {
42+ return this . #wasmData;
43+ }
44+
45+ get fileData ( ) {
46+ return this . #fileData;
47+ }
4148
4249 log ( type : "out" | "err" , chunk : Uint8Array ) {
4350 if ( ! this . debug ) {
@@ -89,11 +96,10 @@ export class InPG implements Deno.Conn {
8996 this . #bufferData = new Uint8Array ( 0 ) ;
9097 this . runtimeInitialized = false ;
9198 this . pgMem = new PGMem ( this ) ;
92- this . wasmLoader = new WasmLoader ( this , wasmData ) ;
99+ this . wasmLoader = new WasmLoader ( this ) ;
93100 this . readEmAsmArgsArray = [ ] ;
94101 this . fileManager = new FileManager ( this , {
95102 debug : options ?. debug ,
96- fileData,
97103 } ) ;
98104 this . sysCalls = new SysCalls ( this ) ;
99105
@@ -112,6 +118,7 @@ export class InPG implements Deno.Conn {
112118
113119 await this . wasmLoader . load ( ) ;
114120 }
121+
115122 sendQuery ( message : Uint8Array ) {
116123 this . wasmLoader . callExportFunction ( "use_wire" , 1 ) ;
117124 const msg_len = message . length ;
@@ -143,6 +150,7 @@ export class InPG implements Deno.Conn {
143150 }
144151
145152 async run ( ) {
153+ await this . loadRemoteFiles ( ) ;
146154 await this . #setup( ) ;
147155 await this . initRuntime ( ) ;
148156 this . #callMain( this . args ) ;
@@ -178,7 +186,39 @@ export class InPG implements Deno.Conn {
178186 Deno . exit ( 1 ) ;
179187 }
180188 }
189+ async loadRemoteFiles ( ) {
190+ const wasmUrl =
191+ "https://github.com/inspatiallabs/inspatial-cloud/releases/download/0.2.2/inpg.wasm" ;
192+ const dataUrl =
193+ "https://github.com/inspatiallabs/inspatial-cloud/releases/download/0.2.2/inpg.data" ;
194+ const tmpDirBase = getTempDirBase ( ) ;
195+ const tmpDir = `${ tmpDirBase } /in-pg` ;
196+
197+ Deno . mkdirSync ( tmpDir , { recursive : true } ) ;
181198
199+ const wasmFile = `${ tmpDir } /inpg.0.2.2.wasm` ;
200+ const dataFile = `${ tmpDir } /inpg.0.2.2.data` ;
201+ this . #wasmData = await this . loadRemoteFile ( wasmFile , wasmUrl ) ;
202+ this . #fileData = await this . loadRemoteFile ( dataFile , dataUrl ) ;
203+ }
204+ async loadRemoteFile ( fileName : string , remoteUrl : string ) {
205+ try {
206+ Deno . statSync ( fileName ) ;
207+ return Deno . readFileSync ( fileName ) ;
208+ } catch ( e ) {
209+ if ( e instanceof Deno . errors . NotFound ) {
210+ const response = await fetch ( remoteUrl ) ;
211+ if ( ! response . ok ) {
212+ throw new Error ( `Failed to fetch file: ${ response . statusText } ` ) ;
213+ }
214+ const data = new Uint8Array ( await response . arrayBuffer ( ) ) ;
215+ await Deno . writeFile ( fileName , data ) ;
216+ return data ;
217+ } else {
218+ throw e ;
219+ }
220+ }
221+ }
182222 initDB ( ) {
183223 const result = this . wasmLoader . callExportFunction ( "pgl_initdb" ) ;
184224 return result ;
0 commit comments