Skip to content

Commit 2f6c82c

Browse files
authored
fix: websocket message handling, Buffer, onConnect (#107)
* Push * Buffer.from * Format * Add changeset
1 parent b9db9b6 commit 2f6c82c

13 files changed

+80
-41
lines changed

.changeset/dull-mangos-appear.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clockworklabs/spacetimedb-sdk': patch
3+
---
4+
5+
fix: websocket message handling, Buffer, onConnect

packages/sdk/src/db_connection_impl.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { TableCache, type Operation, type TableUpdate } from './table_cache.ts';
3131
import { deepEqual, toPascalCase } from './utils.ts';
3232
import { WebsocketDecompressAdapter } from './websocket_decompress_adapter.ts';
3333
import type { WebsocketTestAdapter } from './websocket_test_adapter.ts';
34+
import { Buffer } from 'buffer';
3435

3536
export {
3637
AlgebraicType,
@@ -146,7 +147,7 @@ export class DBConnectionImpl<DBView = any, Reducers = any>
146147
for (const update of rawTableUpdate.updates) {
147148
let decompressed: ws.QueryUpdate;
148149
if (update.tag === 'Brotli') {
149-
const decompressedBuffer = decompress(new Buffer(update.value));
150+
const decompressedBuffer = decompress(Buffer.from(update.value));
150151
decompressed = ws.QueryUpdate.deserialize(
151152
new BinaryReader(decompressedBuffer)
152153
);
@@ -390,12 +391,7 @@ export class DBConnectionImpl<DBView = any, Reducers = any>
390391
this.token = message.token;
391392
}
392393
this.clientAddress = message.address;
393-
this.#emitter.emit(
394-
'connect',
395-
this.token,
396-
this.identity,
397-
this.clientAddress
398-
);
394+
this.#emitter.emit('connect', this, this.identity, this.token);
399395
}
400396
});
401397
}

packages/sdk/src/websocket_decompress_adapter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ export class WebsocketDecompressAdapter {
1414
let decompressed: Uint8Array;
1515
switch (buffer[0]) {
1616
case 0:
17-
decompressed = msg.data.slice(1);
17+
decompressed = buffer.slice(1);
1818
break;
1919
case 1:
20-
decompressed = decompress(new Buffer(buffer.slice(1)));
20+
decompressed = decompress(Buffer.from(buffer.slice(1)));
2121
break;
2222
default:
2323
throw new Error('Invalid message type');

packages/test-app/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
"dev": "vite",
1111
"build": "tsc -b && vite build",
1212
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
13-
"preview": "vite preview"
13+
"preview": "vite preview",
14+
"spacetime:generate-bindings": "spacetime generate --lang typescript --out-dir src/module_bindings --project-path server",
15+
"spacetime:start": "spacetime start server",
16+
"spacetime:publish:local": "spacetime publish game --project-path server --server local",
17+
"spacetime:publish": "spacetime publish game --project-path server --server testnet"
1418
},
1519
"workspaces": [
1620
"packages/*"

packages/test-app/server/src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use spacetimedb::{reducer, table, Identity, ReducerContext, SpacetimeType, Table};
22

3-
#[table(name = player)]
3+
#[table(name = player, public)]
44
pub struct Player {
55
#[primary_key]
66
owner_id: String,
@@ -14,7 +14,7 @@ pub struct Point {
1414
pub y: u16,
1515
}
1616

17-
#[table(name = user)]
17+
#[table(name = user, public)]
1818
pub struct User {
1919
#[primary_key]
2020
pub identity: Identity,
@@ -23,5 +23,9 @@ pub struct User {
2323

2424
#[reducer]
2525
pub fn create_player(ctx: &ReducerContext, name: String, location: Point) {
26-
ctx.db.player().insert(Player { owner_id: ctx.sender.to_hex().to_string(), name, location });
26+
ctx.db.player().insert(Player {
27+
owner_id: ctx.sender.to_hex().to_string(),
28+
name,
29+
location,
30+
});
2731
}

packages/test-app/src/App.tsx

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,58 @@
11
import { DBConnection } from './module_bindings';
2-
import { useState } from 'react';
2+
import { useEffect, useState } from 'react';
33
import './App.css';
44
import { Identity } from '@clockworklabs/spacetimedb-sdk';
55

66
function App() {
7-
const [connection] = useState<DBConnection>(
7+
const [connection] = useState(() =>
88
DBConnection.builder()
99
.withUri('ws://localhost:3000')
10-
.withModuleName('goldbreezycanid')
10+
.withModuleName('game')
1111
.onDisconnect(() => {
1212
console.log('disconnected');
1313
})
1414
.onConnectError(() => {
1515
console.log('client_error');
1616
})
17-
.onConnect((_, identity, _token) => {
17+
.onConnect((conn, identity, _token) => {
1818
console.log(
1919
'Connected to SpacetimeDB with identity:',
2020
identity.toHexString()
2121
);
22+
23+
conn.subscriptionBuilder().subscribe(['SELECT * FROM player']);
2224
})
2325
.withCredentials([
2426
Identity.fromString(
25-
'49f2d472cabfbc7ded52ac1f93316750dc8ea162aac97cc52a340aed221b7ff3'
27+
'93dda09db9a56d8fa6c024d843e805d8262191db3b4ba84c5efcd1ad451fed4e'
2628
),
27-
'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJoZXhfaWRlbnRpdHkiOiI0OWYyZDQ3MmNhYmZiYzdkZWQ1MmFjMWY5MzMxNjc1MGRjOGVhMTYyYWFjOTdjYzUyYTM0MGFlZDIyMWI3ZmYzIiwiaWF0IjoxNjgwMTkwNDc5fQ.KPz0DjrWb6I5c51wa71FGTgWz0Nh6CiNycM0ynmDDNkGjRxsci5cmiEjHQdYKyIeaG9MizSVPGlaDJ2Z7uctcg',
29+
'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJoZXhfaWRlbnRpdHkiOiI5M2RkYTA5ZGI5YTU2ZDhmYTZjMDI0ZDg0M2U4MDVkODI2MjE5MWRiM2I0YmE4NGM1ZWZjZDFhZDQ1MWZlZDRlIiwiaWF0IjoxNzI4MzY3NDk0LCJleHAiOm51bGx9.ua3DQaiXo5i3Ye0okughs84PV9uZLCnov26zvCMG-3ibqGg6vH0uDDY6L-zDf8XiFgUqEIcvnJvuKLThPDipjQ',
2830
])
2931
.build()
3032
);
3133

34+
useEffect(() => {
35+
connection.db.player.onInsert(player => {
36+
console.log(player);
37+
});
38+
39+
setTimeout(() => {
40+
console.log(Array.from(connection.db.player.iter()));
41+
}, 5000);
42+
}, [connection]);
43+
3244
return (
3345
<div className="App">
3446
<h1>Typescript SDK Test!</h1>
3547
<p>{connection.identity?.toHexString()}</p>
48+
49+
<button
50+
onClick={() =>
51+
connection.reducers.createPlayer('Hello', { x: 10, y: 40 })
52+
}
53+
>
54+
Update
55+
</button>
3656
</div>
3757
);
3858
}

packages/test-app/src/module_bindings/create_player_reducer.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import {
3434
SumTypeVariant,
3535
// @ts-ignore
3636
TableCache,
37+
// @ts-ignore
38+
deepEqual,
3739
} from '@clockworklabs/spacetimedb-sdk';
3840

3941
// @ts-ignore
@@ -52,18 +54,18 @@ export namespace CreatePlayer {
5254
* A function which returns this type represented as an AlgebraicType.
5355
* This function is derived from the AlgebraicType used to generate this type.
5456
*/
55-
export function getAlgebraicType(): AlgebraicType {
57+
export function getTypeScriptAlgebraicType(): AlgebraicType {
5658
return AlgebraicType.createProductType([
5759
new ProductTypeElement('name', AlgebraicType.createStringType()),
58-
new ProductTypeElement('location', __Point.getAlgebraicType()),
60+
new ProductTypeElement('location', __Point.getTypeScriptAlgebraicType()),
5961
]);
6062
}
6163

6264
export function serialize(writer: BinaryWriter, value: CreatePlayer): void {
63-
CreatePlayer.getAlgebraicType().serialize(writer, value);
65+
CreatePlayer.getTypeScriptAlgebraicType().serialize(writer, value);
6466
}
6567

6668
export function deserialize(reader: BinaryReader): CreatePlayer {
67-
return CreatePlayer.getAlgebraicType().deserialize(reader);
69+
return CreatePlayer.getTypeScriptAlgebraicType().deserialize(reader);
6870
}
6971
}

packages/test-app/src/module_bindings/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import {
3434
SumTypeVariant,
3535
// @ts-ignore
3636
TableCache,
37+
// @ts-ignore
38+
deepEqual,
3739
} from '@clockworklabs/spacetimedb-sdk';
3840

3941
// Import and reexport all reducer arg types
@@ -58,19 +60,19 @@ const REMOTE_MODULE = {
5860
tables: {
5961
player: {
6062
tableName: 'player',
61-
rowType: Player.getAlgebraicType(),
63+
rowType: Player.getTypeScriptAlgebraicType(),
6264
primaryKey: 'owner_id',
6365
},
6466
user: {
6567
tableName: 'user',
66-
rowType: User.getAlgebraicType(),
68+
rowType: User.getTypeScriptAlgebraicType(),
6769
primaryKey: 'identity',
6870
},
6971
},
7072
reducers: {
7173
create_player: {
7274
reducerName: 'create_player',
73-
argsType: CreatePlayer.getAlgebraicType(),
75+
argsType: CreatePlayer.getTypeScriptAlgebraicType(),
7476
},
7577
},
7678
// Constructors which are used by the DBConnectionImpl to
@@ -98,7 +100,7 @@ export class RemoteReducers {
98100
createPlayer(name: string, location: Point) {
99101
const __args = { name, location };
100102
let __writer = new BinaryWriter(1024);
101-
CreatePlayer.getAlgebraicType().serialize(__writer, __args);
103+
CreatePlayer.getTypeScriptAlgebraicType().serialize(__writer, __args);
102104
let __argsBuffer = __writer.getBuffer();
103105
this.connection.callReducer('create_player', __argsBuffer);
104106
}

packages/test-app/src/module_bindings/player_table.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD.
33

44
import {
5-
// @ts-ignore
6-
deepEqual,
75
// @ts-ignore
86
Address,
97
// @ts-ignore
@@ -36,6 +34,8 @@ import {
3634
SumTypeVariant,
3735
// @ts-ignore
3836
TableCache,
37+
// @ts-ignore
38+
deepEqual,
3939
} from '@clockworklabs/spacetimedb-sdk';
4040
import { Player } from './player_type';
4141
// @ts-ignore

packages/test-app/src/module_bindings/player_type.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import {
3434
SumTypeVariant,
3535
// @ts-ignore
3636
TableCache,
37+
// @ts-ignore
38+
deepEqual,
3739
} from '@clockworklabs/spacetimedb-sdk';
3840
// @ts-ignore
3941
import { Point as __Point } from './point_type';
@@ -52,19 +54,19 @@ export namespace Player {
5254
* A function which returns this type represented as an AlgebraicType.
5355
* This function is derived from the AlgebraicType used to generate this type.
5456
*/
55-
export function getAlgebraicType(): AlgebraicType {
57+
export function getTypeScriptAlgebraicType(): AlgebraicType {
5658
return AlgebraicType.createProductType([
5759
new ProductTypeElement('ownerId', AlgebraicType.createStringType()),
5860
new ProductTypeElement('name', AlgebraicType.createStringType()),
59-
new ProductTypeElement('location', __Point.getAlgebraicType()),
61+
new ProductTypeElement('location', __Point.getTypeScriptAlgebraicType()),
6062
]);
6163
}
6264

6365
export function serialize(writer: BinaryWriter, value: Player): void {
64-
Player.getAlgebraicType().serialize(writer, value);
66+
Player.getTypeScriptAlgebraicType().serialize(writer, value);
6567
}
6668

6769
export function deserialize(reader: BinaryReader): Player {
68-
return Player.getAlgebraicType().deserialize(reader);
70+
return Player.getTypeScriptAlgebraicType().deserialize(reader);
6971
}
7072
}

packages/test-app/src/module_bindings/point_type.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import {
3434
SumTypeVariant,
3535
// @ts-ignore
3636
TableCache,
37+
// @ts-ignore
38+
deepEqual,
3739
} from '@clockworklabs/spacetimedb-sdk';
3840
export type Point = {
3941
x: number;
@@ -48,18 +50,18 @@ export namespace Point {
4850
* A function which returns this type represented as an AlgebraicType.
4951
* This function is derived from the AlgebraicType used to generate this type.
5052
*/
51-
export function getAlgebraicType(): AlgebraicType {
53+
export function getTypeScriptAlgebraicType(): AlgebraicType {
5254
return AlgebraicType.createProductType([
5355
new ProductTypeElement('x', AlgebraicType.createU16Type()),
5456
new ProductTypeElement('y', AlgebraicType.createU16Type()),
5557
]);
5658
}
5759

5860
export function serialize(writer: BinaryWriter, value: Point): void {
59-
Point.getAlgebraicType().serialize(writer, value);
61+
Point.getTypeScriptAlgebraicType().serialize(writer, value);
6062
}
6163

6264
export function deserialize(reader: BinaryReader): Point {
63-
return Point.getAlgebraicType().deserialize(reader);
65+
return Point.getTypeScriptAlgebraicType().deserialize(reader);
6466
}
6567
}

packages/test-app/src/module_bindings/user_table.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD.
33

44
import {
5-
// @ts-ignore
6-
deepEqual,
75
// @ts-ignore
86
Address,
97
// @ts-ignore
@@ -36,6 +34,8 @@ import {
3634
SumTypeVariant,
3735
// @ts-ignore
3836
TableCache,
37+
// @ts-ignore
38+
deepEqual,
3939
} from '@clockworklabs/spacetimedb-sdk';
4040
import { User } from './user_type';
4141
// @ts-ignore

packages/test-app/src/module_bindings/user_type.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import {
3434
SumTypeVariant,
3535
// @ts-ignore
3636
TableCache,
37+
// @ts-ignore
38+
deepEqual,
3739
} from '@clockworklabs/spacetimedb-sdk';
3840
export type User = {
3941
identity: Identity;
@@ -48,18 +50,18 @@ export namespace User {
4850
* A function which returns this type represented as an AlgebraicType.
4951
* This function is derived from the AlgebraicType used to generate this type.
5052
*/
51-
export function getAlgebraicType(): AlgebraicType {
53+
export function getTypeScriptAlgebraicType(): AlgebraicType {
5254
return AlgebraicType.createProductType([
5355
new ProductTypeElement('identity', AlgebraicType.createIdentityType()),
5456
new ProductTypeElement('username', AlgebraicType.createStringType()),
5557
]);
5658
}
5759

5860
export function serialize(writer: BinaryWriter, value: User): void {
59-
User.getAlgebraicType().serialize(writer, value);
61+
User.getTypeScriptAlgebraicType().serialize(writer, value);
6062
}
6163

6264
export function deserialize(reader: BinaryReader): User {
63-
return User.getAlgebraicType().deserialize(reader);
65+
return User.getTypeScriptAlgebraicType().deserialize(reader);
6466
}
6567
}

0 commit comments

Comments
 (0)