Skip to content

Commit

Permalink
feat: GZIP (#109)
Browse files Browse the repository at this point in the history
* Pus

* gzip

* Push

* Buffer.from

* Format

* Add changeset

* Push

* Remove deps

* Rvert back to original lib.rs

* Push

* Refactor decompress function

* Autogenerate

* Format

* deferred

* Fix tests

* Remove blob coments

* Message queue. Untested

* Fix test

* Chunked decompress
  • Loading branch information
PuruVJ authored Oct 23, 2024
1 parent 221ac62 commit cf7b7d8
Show file tree
Hide file tree
Showing 33 changed files with 953 additions and 903 deletions.
5 changes: 5 additions & 0 deletions .changeset/dull-mangos-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clockworklabs/spacetimedb-sdk': patch
---

fix: websocket message handling, Buffer, onConnect
3 changes: 0 additions & 3 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@
},
"devDependencies": {
"@clockworklabs/test-app": "file:../test-app",
"@types/brotli": "1.3.3",
"brotli": "1.3.3",
"buffer": "^6.0.3",
"tsup": "^8.1.0",
"undici": "^6.19.2"
}
Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/client_api/bsatn_row_list_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
deepEqual,
} from '..';
// @ts-ignore
import { RowSizeHint as __RowSizeHint } from './row_size_hint_type';

Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/client_api/call_reducer_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
deepEqual,
} from '..';
export type CallReducer = {
reducer: string;
args: Uint8Array;
Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/client_api/client_message_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
deepEqual,
} from '..';
// @ts-ignore
import { CallReducer as __CallReducer } from './call_reducer_type';
// @ts-ignore
Expand Down
16 changes: 14 additions & 2 deletions packages/sdk/src/client_api/compressable_query_update_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
deepEqual,
} from '..';
// @ts-ignore
import { QueryUpdate as __QueryUpdate } from './query_update_type';

Expand All @@ -45,6 +47,7 @@ export namespace CompressableQueryUpdate {
// the tagged union.
export type Uncompressed = { tag: 'Uncompressed'; value: __QueryUpdate };
export type Brotli = { tag: 'Brotli'; value: Uint8Array };
export type Gzip = { tag: 'Gzip'; value: Uint8Array };

// Helper functions for constructing each variant of the tagged union.
// ```
Expand All @@ -59,6 +62,10 @@ export namespace CompressableQueryUpdate {
tag: 'Brotli',
value,
});
export const Gzip = (value: Uint8Array): CompressableQueryUpdate => ({
tag: 'Gzip',
value,
});

export function getTypeScriptAlgebraicType(): AlgebraicType {
return AlgebraicType.createSumType([
Expand All @@ -70,6 +77,10 @@ export namespace CompressableQueryUpdate {
'Brotli',
AlgebraicType.createArrayType(AlgebraicType.createU8Type())
),
new SumTypeVariant(
'Gzip',
AlgebraicType.createArrayType(AlgebraicType.createU8Type())
),
]);
}

Expand All @@ -93,6 +104,7 @@ export namespace CompressableQueryUpdate {
// The tagged union or sum type for the algebraic type `CompressableQueryUpdate`.
export type CompressableQueryUpdate =
| CompressableQueryUpdate.Uncompressed
| CompressableQueryUpdate.Brotli;
| CompressableQueryUpdate.Brotli
| CompressableQueryUpdate.Gzip;

export default CompressableQueryUpdate;
4 changes: 3 additions & 1 deletion packages/sdk/src/client_api/database_update_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
deepEqual,
} from '..';
// @ts-ignore
import { TableUpdate as __TableUpdate } from './table_update_type';

Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/client_api/energy_quanta_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
deepEqual,
} from '..';
export type EnergyQuanta = {
quanta: bigint;
};
Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/client_api/identity_token_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
deepEqual,
} from '..';
export type IdentityToken = {
identity: Identity;
token: string;
Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/client_api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
deepEqual,
} from '..';

// Import and reexport all reducer arg types

Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/client_api/initial_subscription_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
deepEqual,
} from '..';
// @ts-ignore
import { DatabaseUpdate as __DatabaseUpdate } from './database_update_type';

Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/client_api/one_off_query_response_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
deepEqual,
} from '..';
// @ts-ignore
import { OneOffTable as __OneOffTable } from './one_off_table_type';

Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/client_api/one_off_query_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
deepEqual,
} from '..';
export type OneOffQuery = {
messageId: Uint8Array;
queryString: string;
Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/client_api/one_off_table_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
deepEqual,
} from '..';
// @ts-ignore
import { BsatnRowList as __BsatnRowList } from './bsatn_row_list_type';

Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/client_api/query_update_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
deepEqual,
} from '..';
// @ts-ignore
import { BsatnRowList as __BsatnRowList } from './bsatn_row_list_type';

Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/client_api/reducer_call_info_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
deepEqual,
} from '..';
export type ReducerCallInfo = {
reducerName: string;
reducerId: number;
Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/client_api/row_size_hint_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
deepEqual,
} from '..';
// A namespace for generated variants and helper functions.
export namespace RowSizeHint {
// These are the generated variant types for each variant of the tagged union.
Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/client_api/server_message_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
deepEqual,
} from '..';
// @ts-ignore
import { InitialSubscription as __InitialSubscription } from './initial_subscription_type';
// @ts-ignore
Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/client_api/subscribe_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
deepEqual,
} from '..';
export type Subscribe = {
queryStrings: string[];
requestId: number;
Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/client_api/table_update_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
deepEqual,
} from '..';
// @ts-ignore
import { CompressableQueryUpdate as __CompressableQueryUpdate } from './compressable_query_update_type';

Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/client_api/timestamp_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
deepEqual,
} from '..';
export type Timestamp = {
microseconds: bigint;
};
Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/client_api/transaction_update_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
deepEqual,
} from '..';
// @ts-ignore
import { UpdateStatus as __UpdateStatus } from './update_status_type';
// @ts-ignore
Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/client_api/update_status_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
deepEqual,
} from '..';
// @ts-ignore
import { DatabaseUpdate as __DatabaseUpdate } from './database_update_type';

Expand Down
9 changes: 9 additions & 0 deletions packages/sdk/src/db_connection_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class DBConnectionBuilder<DBConnection> {
#token?: string;
#emitter: EventEmitter = new EventEmitter();
#createWSFn: typeof WebsocketDecompressAdapter.createWebSocketFn;
#compression: 'gzip' | 'none' = 'gzip';

/**
* Creates a new `SpacetimeDBClient` database client and set the initial parameters.
Expand Down Expand Up @@ -70,6 +71,13 @@ export class DBConnectionBuilder<DBConnection> {
return this;
}

withCompression(
compression: 'gzip' | 'none'
): DBConnectionBuilder<DBConnection> {
this.#compression = compression;
return this;
}

/**
* Connect to The SpacetimeDB Websocket For Your Module. By default, this will use a secure websocket connection. The parameters are optional, and if not provided, will use the values provided on construction of the client.
*
Expand Down Expand Up @@ -118,6 +126,7 @@ export class DBConnectionBuilder<DBConnection> {
url,
wsProtocol: 'v1.bsatn.spacetimedb',
authToken: connection.token,
compression: this.#compression,
})
.then(v => {
connection.ws = v;
Expand Down
Loading

0 comments on commit cf7b7d8

Please sign in to comment.