Skip to content

Commit dca56e7

Browse files
Fix identity deserialize (#8)
Remove identity from credentials (not needed to log in) Co-authored-by: Derek Brinkmann <[email protected]>
1 parent b392be1 commit dca56e7

File tree

2 files changed

+24
-36
lines changed

2 files changed

+24
-36
lines changed

src/algebraic_value.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,7 @@ export class BuiltinValue {
8989
arrayBuiltinType !== undefined &&
9090
arrayBuiltinType === BuiltinType.Type.U8
9191
) {
92-
// first let's change 0s to x, but only preceding 0s,
93-
// like "00000FF" -> "x0x0xFF"
94-
const replaced: string = value.replaceAll(
95-
/0(0)|0([^0])/g,
96-
(
97-
match: string,
98-
arg1: string | undefined,
99-
arg2: string | undefined
100-
) => {
101-
return "x" + (arg1 || arg2);
102-
}
103-
);
104-
// then split by 'x' and convert to ints
105-
let array: string[] = replaced.substring(1).split("x");
106-
let bytes: Uint8Array = new Uint8Array(
107-
array.map((hex) => Number("0x" + hex))
108-
);
109-
return new this(bytes);
92+
return new this(new TextEncoder().encode(value));
11093
} else {
11194
let result: AlgebraicValue[] = [];
11295
for (let el of value) {

src/spacetimedb.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -301,16 +301,12 @@ export class SpacetimeDBClient {
301301
private runtime: {
302302
host: string;
303303
name_or_address: string;
304-
credentials?: { identity: string; token: string };
304+
auth_token?: string;
305305
global: SpacetimeDBGlobals;
306306
};
307307
private createWSFn: CreateWSFnType;
308308

309-
constructor(
310-
host: string,
311-
name_or_address: string,
312-
credentials?: { identity: string; token: string }
313-
) {
309+
constructor(host: string, name_or_address: string, auth_token?: string) {
314310
const global = g.__SPACETIMEDB__;
315311
this.db = global.clientDB;
316312
// I don't really like it, but it seems like the only way to
@@ -332,7 +328,7 @@ export class SpacetimeDBClient {
332328
this.runtime = {
333329
host,
334330
name_or_address,
335-
credentials,
331+
auth_token,
336332
global,
337333
};
338334

@@ -361,11 +357,7 @@ export class SpacetimeDBClient {
361357
* @param name_or_address The name or address of the spacetimeDB module
362358
* @param credentials The credentials to use to connect to the spacetimeDB module
363359
*/
364-
public connect(
365-
host?: string,
366-
name_or_address?: string,
367-
credentials?: { identity: string; token: string }
368-
) {
360+
public connect(host?: string, name_or_address?: string, auth_token?: string) {
369361
if (this.live) {
370362
return;
371363
}
@@ -380,14 +372,13 @@ export class SpacetimeDBClient {
380372
this.runtime.name_or_address = name_or_address;
381373
}
382374

383-
if (credentials) {
384-
this.runtime.credentials = credentials;
375+
if (auth_token) {
376+
this.runtime.auth_token = auth_token;
385377
}
386378

387379
let headers: { [key: string]: string } = {};
388-
if (this.runtime.credentials) {
389-
this.identity = this.runtime.credentials.identity;
390-
this.token = this.runtime.credentials.token;
380+
if (this.runtime.auth_token) {
381+
this.token = this.runtime.auth_token;
391382
headers["Authorization"] = `Basic ${btoa("token:" + this.token)}`;
392383
}
393384
let url = `${this.runtime.host}/database/subscribe/${this.runtime.name_or_address}`;
@@ -400,6 +391,14 @@ export class SpacetimeDBClient {
400391

401392
this.ws = this.createWSFn(url, headers, "v1.text.spacetimedb");
402393

394+
// Create a timeout for the connection to be established
395+
var connectionTimeout = setTimeout(() => {
396+
this.ws.close();
397+
console.error("Connect failed: timeout");
398+
this.emitter.emit("disconnected");
399+
this.emitter.emit("client_error", event);
400+
}, 5000); // 5000 ms = 5 seconds
401+
403402
this.ws.onclose = (event) => {
404403
console.error("Closed: ", event);
405404
this.emitter.emit("disconnected");
@@ -413,6 +412,8 @@ export class SpacetimeDBClient {
413412
};
414413

415414
this.ws.onopen = () => {
415+
clearTimeout(connectionTimeout);
416+
416417
this.live = true;
417418

418419
if (this.queriesQueue.length > 0) {
@@ -490,7 +491,7 @@ export class SpacetimeDBClient {
490491
const token = identityToken["token"];
491492
this.identity = identity;
492493
this.token = token;
493-
this.emitter.emit("connected", identity);
494+
this.emitter.emit("connected", token, identity);
494495
}
495496
}
496497
};
@@ -570,6 +571,10 @@ export class SpacetimeDBClient {
570571
this.on("connected", callback);
571572
}
572573

574+
onError(callback: (...args: any[]) => void) {
575+
this.on("client_error", callback);
576+
}
577+
573578
_setCreateWSFn(fn: CreateWSFnType) {
574579
this.createWSFn = fn;
575580
}

0 commit comments

Comments
 (0)