diff --git a/src/puter-js/src/index.js b/src/puter-js/src/index.js index dc59a90d8..e45afa124 100644 --- a/src/puter-js/src/index.js +++ b/src/puter-js/src/index.js @@ -19,7 +19,7 @@ import { APIAccessService } from './services/APIAccess.js'; import { XDIncomingService } from './services/XDIncoming.js'; import { NoPuterYetService } from './services/NoPuterYet.js'; import { Debug } from './modules/Debug.js'; -import { PSocket, wispInfo } from './modules/networking/PSocket.js'; +import { PSocket } from './modules/networking/PSocket.js'; import { PTLSSocket } from "./modules/networking/PTLS.js" import { PWispHandler } from './modules/networking/PWispHandler.js'; import { make_http_api } from './lib/http.js'; @@ -333,41 +333,31 @@ export default window.puter = (function() { // TODO: This should be separated into modules called "Net" and "Http". // Modules need to be refactored first because right now they // are too tightly-coupled with authentication state. - (async () => { - // === puter.net === - const { token: wispToken, server: wispServer } = (await (await fetch(this.APIOrigin + '/wisp/relay-token/create', { - method: 'POST', - headers: { - Authorization: `Bearer ${this.authToken}`, - 'Content-Type': 'application/json', - }, - body: JSON.stringify({}), - })).json()); - wispInfo.handler = new PWispHandler(wispServer, wispToken); - this.net = { - generateWispV1URL: async () => { - const { token: wispToken, server: wispServer } = (await (await fetch(this.APIOrigin + '/wisp/relay-token/create', { - method: 'POST', - headers: { - Authorization: `Bearer ${this.authToken}`, - 'Content-Type': 'application/json', - }, - body: JSON.stringify({}), - })).json()); - return `${wispServer}/${wispToken}/` - }, - Socket: PSocket, - tls: { - TLSSocket: PTLSSocket - } + + + this.net = { + generateWispV1URL: async () => { + const { token: wispToken, server: wispServer } = (await (await fetch(this.APIOrigin + '/wisp/relay-token/create', { + method: 'POST', + headers: { + Authorization: `Bearer ${this.authToken}`, + 'Content-Type': 'application/json', + }, + body: JSON.stringify({}), + })).json()); + return `${wispServer}/${wispToken}/` + }, + Socket: PSocket, + tls: { + TLSSocket: PTLSSocket } - - // === puter.http === - this.http = make_http_api( - { Socket: this.net.Socket, DEFAULT_PORT: 80 }); - this.https = make_http_api( - { Socket: this.net.tls.TLSSocket, DEFAULT_PORT: 443 }); - })(); + } + + // === puter.http === + this.http = make_http_api( + { Socket: this.net.Socket, DEFAULT_PORT: 80 }); + this.https = make_http_api( + { Socket: this.net.tls.TLSSocket, DEFAULT_PORT: 443 }); } diff --git a/src/puter-js/src/modules/networking/PSocket.js b/src/puter-js/src/modules/networking/PSocket.js index 3f09a63f0..3f23683c6 100644 --- a/src/puter-js/src/modules/networking/PSocket.js +++ b/src/puter-js/src/modules/networking/PSocket.js @@ -18,29 +18,32 @@ export class PSocket extends EventListener { if(!puter.authToken && puter.env === 'web'){ try{ await puter.ui.authenticateWithPuter(); - console.log("auth'd", puter.authToken) - // We have to remake the handler since the old one was done with improper auth, so we'll just talk with the auth server directly - const { token: wispToken, server: wispServer } = (await (await fetch(puter.APIOrigin + '/wisp/relay-token/create', { - method: 'POST', - headers: { - Authorization: `Bearer ${puter.authToken}`, - 'Content-Type': 'application/json', - }, - body: JSON.stringify({}), - })).json()); - - wispInfo.handler = new PWispHandler(wispServer, wispToken); - - // Wait for websocket to fully open - await new Promise((res, req) => { - wispInfo.handler.onReady = res; - }); }catch(e){ // if authentication fails, throw an error throw (e); } } + if (!wispInfo.handler) { + // first launch -- lets init the socket + const { token: wispToken, server: wispServer } = (await (await fetch(puter.APIOrigin + '/wisp/relay-token/create', { + method: 'POST', + headers: { + Authorization: `Bearer ${puter.authToken}`, + 'Content-Type': 'application/json', + }, + body: JSON.stringify({}), + })).json()); + + wispInfo.handler = new PWispHandler(wispServer, wispToken); + } + + + // Wait for websocket to fully open + await new Promise((res, req) => { + wispInfo.handler.onReady = res; + }); + const callbacks = { dataCallBack: (data) => { this.emit("data", data); diff --git a/src/puter-js/src/modules/networking/PTLS.js b/src/puter-js/src/modules/networking/PTLS.js index 526a9c5de..ea4ca2154 100644 --- a/src/puter-js/src/modules/networking/PTLS.js +++ b/src/puter-js/src/modules/networking/PTLS.js @@ -9,7 +9,7 @@ let rustls = undefined; export class PTLSSocket extends PSocket { constructor(...args) { super(...args); - (async() => { + super.on("open", (async() => { if (!rustls) { rustls = (await import( /* webpackIgnore: true */ "https://puter-net.b-cdn.net/rustls.js")) await rustls.default("https://puter-net.b-cdn.net/rustls.wasm") @@ -42,7 +42,7 @@ export class PTLSSocket extends PSocket { const writable = new WritableStream({ write: (chunk) => { super.write(chunk); }, - abort: () => { console.log("hello"); super.close(); }, + abort: () => { super.close(); }, close: () => { super.close(); }, }) @@ -74,7 +74,7 @@ export class PTLSSocket extends PSocket { this.emit("error", e) } // this.emit("close", undefined); - })(); + })); } on(event, callback) { if (event === "data" || event === "open") {