Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f944d76

Browse filesBrowse files
change initialization order to make wisp socket on first use of networking
1 parent 73e6830 commit f944d76
Copy full SHA for f944d76

File tree

3 files changed

+48
-55
lines changed
Filter options

3 files changed

+48
-55
lines changed

‎src/puter-js/src/index.js

Copy file name to clipboardExpand all lines: src/puter-js/src/index.js
+25-35
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { APIAccessService } from './services/APIAccess.js';
1919
import { XDIncomingService } from './services/XDIncoming.js';
2020
import { NoPuterYetService } from './services/NoPuterYet.js';
2121
import { Debug } from './modules/Debug.js';
22-
import { PSocket, wispInfo } from './modules/networking/PSocket.js';
22+
import { PSocket } from './modules/networking/PSocket.js';
2323
import { PTLSSocket } from "./modules/networking/PTLS.js"
2424
import { PWispHandler } from './modules/networking/PWispHandler.js';
2525
import { make_http_api } from './lib/http.js';
@@ -333,41 +333,31 @@ export default window.puter = (function() {
333333
// TODO: This should be separated into modules called "Net" and "Http".
334334
// Modules need to be refactored first because right now they
335335
// are too tightly-coupled with authentication state.
336-
(async () => {
337-
// === puter.net ===
338-
const { token: wispToken, server: wispServer } = (await (await fetch(this.APIOrigin + '/wisp/relay-token/create', {
339-
method: 'POST',
340-
headers: {
341-
Authorization: `Bearer ${this.authToken}`,
342-
'Content-Type': 'application/json',
343-
},
344-
body: JSON.stringify({}),
345-
})).json());
346-
wispInfo.handler = new PWispHandler(wispServer, wispToken);
347-
this.net = {
348-
generateWispV1URL: async () => {
349-
const { token: wispToken, server: wispServer } = (await (await fetch(this.APIOrigin + '/wisp/relay-token/create', {
350-
method: 'POST',
351-
headers: {
352-
Authorization: `Bearer ${this.authToken}`,
353-
'Content-Type': 'application/json',
354-
},
355-
body: JSON.stringify({}),
356-
})).json());
357-
return `${wispServer}/${wispToken}/`
358-
},
359-
Socket: PSocket,
360-
tls: {
361-
TLSSocket: PTLSSocket
362-
}
336+
337+
338+
this.net = {
339+
generateWispV1URL: async () => {
340+
const { token: wispToken, server: wispServer } = (await (await fetch(this.APIOrigin + '/wisp/relay-token/create', {
341+
method: 'POST',
342+
headers: {
343+
Authorization: `Bearer ${this.authToken}`,
344+
'Content-Type': 'application/json',
345+
},
346+
body: JSON.stringify({}),
347+
})).json());
348+
return `${wispServer}/${wispToken}/`
349+
},
350+
Socket: PSocket,
351+
tls: {
352+
TLSSocket: PTLSSocket
363353
}
364-
365-
// === puter.http ===
366-
this.http = make_http_api(
367-
{ Socket: this.net.Socket, DEFAULT_PORT: 80 });
368-
this.https = make_http_api(
369-
{ Socket: this.net.tls.TLSSocket, DEFAULT_PORT: 443 });
370-
})();
354+
}
355+
356+
// === puter.http ===
357+
this.http = make_http_api(
358+
{ Socket: this.net.Socket, DEFAULT_PORT: 80 });
359+
this.https = make_http_api(
360+
{ Socket: this.net.tls.TLSSocket, DEFAULT_PORT: 443 });
371361

372362

373363
}

‎src/puter-js/src/modules/networking/PSocket.js

Copy file name to clipboardExpand all lines: src/puter-js/src/modules/networking/PSocket.js
+20-17
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,32 @@ export class PSocket extends EventListener {
1818
if(!puter.authToken && puter.env === 'web'){
1919
try{
2020
await puter.ui.authenticateWithPuter();
21-
console.log("auth'd", puter.authToken)
2221

23-
// 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
24-
const { token: wispToken, server: wispServer } = (await (await fetch(puter.APIOrigin + '/wisp/relay-token/create', {
25-
method: 'POST',
26-
headers: {
27-
Authorization: `Bearer ${puter.authToken}`,
28-
'Content-Type': 'application/json',
29-
},
30-
body: JSON.stringify({}),
31-
})).json());
32-
33-
wispInfo.handler = new PWispHandler(wispServer, wispToken);
34-
35-
// Wait for websocket to fully open
36-
await new Promise((res, req) => {
37-
wispInfo.handler.onReady = res;
38-
});
3922
}catch(e){
4023
// if authentication fails, throw an error
4124
throw (e);
4225
}
4326
}
27+
if (!wispInfo.handler) {
28+
// first launch -- lets init the socket
29+
const { token: wispToken, server: wispServer } = (await (await fetch(puter.APIOrigin + '/wisp/relay-token/create', {
30+
method: 'POST',
31+
headers: {
32+
Authorization: `Bearer ${puter.authToken}`,
33+
'Content-Type': 'application/json',
34+
},
35+
body: JSON.stringify({}),
36+
})).json());
37+
38+
wispInfo.handler = new PWispHandler(wispServer, wispToken);
39+
}
40+
41+
42+
// Wait for websocket to fully open
43+
await new Promise((res, req) => {
44+
wispInfo.handler.onReady = res;
45+
});
46+
4447
const callbacks = {
4548
dataCallBack: (data) => {
4649
this.emit("data", data);

‎src/puter-js/src/modules/networking/PTLS.js

Copy file name to clipboardExpand all lines: src/puter-js/src/modules/networking/PTLS.js
+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ let rustls = undefined;
99
export class PTLSSocket extends PSocket {
1010
constructor(...args) {
1111
super(...args);
12-
(async() => {
12+
super.on("open", (async() => {
1313
if (!rustls) {
1414
rustls = (await import( /* webpackIgnore: true */ "https://puter-net.b-cdn.net/rustls.js"))
1515
await rustls.default("https://puter-net.b-cdn.net/rustls.wasm")
@@ -42,7 +42,7 @@ export class PTLSSocket extends PSocket {
4242

4343
const writable = new WritableStream({
4444
write: (chunk) => { super.write(chunk); },
45-
abort: () => { console.log("hello"); super.close(); },
45+
abort: () => { super.close(); },
4646
close: () => { super.close(); },
4747
})
4848

@@ -74,7 +74,7 @@ export class PTLSSocket extends PSocket {
7474
this.emit("error", e)
7575
}
7676
// this.emit("close", undefined);
77-
})();
77+
}));
7878
}
7979
on(event, callback) {
8080
if (event === "data" || event === "open") {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.