change initialization order to make wisp socket on first use of networking

This commit is contained in:
ProgrammerIn-wonderland
2025-05-16 17:18:28 -04:00
parent 73e6830b1f
commit f944d76261
3 changed files with 48 additions and 55 deletions

View File

@@ -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 });
}

View File

@@ -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);

View File

@@ -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") {