Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Timestamp to the SDK, moving it out of client_api #114

Draft
wants to merge 25 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 1 addition & 4 deletions DEVELOP.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ Whenever the `client-api-messages` crate changes, you'll have to manually re-gen
See that crate's DEVELOP.md for how to do this.

The generated files must be manually modified to fix their imports from the rest of the SDK.
Within each generated file:

- Change the import from `"@clockworklabs/spacetimedb-sdk"` to `"../index"`.
- If the type has generated a `class`, remove its `extends DatabaseTable`, remove the `public static db` member, and remove the call to `super()` within the constructor.
Within each generated file, change the import from `"@clockworklabs/spacetimedb-sdk"` to `".."`.

## Releases and publishing

Expand Down
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
37 changes: 35 additions & 2 deletions packages/sdk/src/algebraic_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,24 @@ export class AlgebraicType {
}
static createScheduleAtType(): AlgebraicType {
return AlgebraicType.createSumType([
new SumTypeVariant('Interval', AlgebraicType.createU64Type()),
new SumTypeVariant('Time', AlgebraicType.createU64Type()),
new SumTypeVariant('Interval', AlgebraicType.createTimeDurationType()),
new SumTypeVariant('Time', AlgebraicType.createTimestampType()),
]);
}
static createTimestampType(): AlgebraicType {
return AlgebraicType.createProductType([
new ProductTypeElement(
'__timestamp_micros_since_unix_epoch__',
AlgebraicType.createI64Type()
),
]);
}
static createTimeDurationType(): AlgebraicType {
return AlgebraicType.createProductType([
new ProductTypeElement(
'__time_duration_micros__',
AlgebraicType.createI64Type()
),
]);
}

Expand Down Expand Up @@ -382,6 +398,15 @@ export class AlgebraicType {
);
}

#isI64Newtype(tag: string): boolean {
return (
this.isProductType() &&
this.product.elements.length === 1 &&
this.product.elements[0].algebraicType.type === Type.I64 &&
this.product.elements[0].name === tag
);
}

isIdentity(): boolean {
return this.#isBytesNewtype('__identity_bytes');
}
Expand All @@ -390,6 +415,14 @@ export class AlgebraicType {
return this.#isBytesNewtype('__address_bytes');
}

isTimestamp(): boolean {
return this.#isI64Newtype('__timestamp_micros_since_unix_epoch__');
}

isTimeDuration(): boolean {
return this.#isI64Newtype('__time_duration_micros__');
}

serialize(writer: BinaryWriter, value: any): void {
switch (this.type) {
case Type.ProductType:
Expand Down
8 changes: 7 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,13 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
TimeDuration,
// @ts-ignore
Timestamp,
// @ts-ignore
deepEqual,
} from '..';
// @ts-ignore
import { RowSizeHint as __RowSizeHint } from './row_size_hint_type';

Expand Down
8 changes: 7 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,13 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
TimeDuration,
// @ts-ignore
Timestamp,
// @ts-ignore
deepEqual,
} from '..';
export type CallReducer = {
reducer: string;
args: Uint8Array;
Expand Down
8 changes: 7 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,13 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
TimeDuration,
// @ts-ignore
Timestamp,
// @ts-ignore
deepEqual,
} from '..';
// @ts-ignore
import { CallReducer as __CallReducer } from './call_reducer_type';
// @ts-ignore
Expand Down
20 changes: 18 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,13 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
TimeDuration,
// @ts-ignore
Timestamp,
// @ts-ignore
deepEqual,
} from '..';
// @ts-ignore
import { QueryUpdate as __QueryUpdate } from './query_update_type';

Expand All @@ -45,6 +51,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 +66,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 +81,10 @@ export namespace CompressableQueryUpdate {
'Brotli',
AlgebraicType.createArrayType(AlgebraicType.createU8Type())
),
new SumTypeVariant(
'Gzip',
AlgebraicType.createArrayType(AlgebraicType.createU8Type())
),
]);
}

Expand All @@ -93,6 +108,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;
8 changes: 7 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,13 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
TimeDuration,
// @ts-ignore
Timestamp,
// @ts-ignore
deepEqual,
} from '..';
// @ts-ignore
import { TableUpdate as __TableUpdate } from './table_update_type';

Expand Down
8 changes: 7 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,13 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
TimeDuration,
// @ts-ignore
Timestamp,
// @ts-ignore
deepEqual,
} from '..';
export type EnergyQuanta = {
quanta: bigint;
};
Expand Down
8 changes: 7 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,13 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
TimeDuration,
// @ts-ignore
Timestamp,
// @ts-ignore
deepEqual,
} from '..';
export type IdentityToken = {
identity: Identity;
token: string;
Expand Down
10 changes: 7 additions & 3 deletions packages/sdk/src/client_api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
TimeDuration,
// @ts-ignore
Timestamp,
// @ts-ignore
deepEqual,
} from '..';

// Import and reexport all reducer arg types

Expand Down Expand Up @@ -75,8 +81,6 @@ import { Subscribe } from './subscribe_type.ts';
export { Subscribe };
import { TableUpdate } from './table_update_type.ts';
export { TableUpdate };
import { Timestamp } from './timestamp_type.ts';
export { Timestamp };
import { TransactionUpdate } from './transaction_update_type.ts';
export { TransactionUpdate };
import { UpdateStatus } from './update_status_type.ts';
Expand Down
14 changes: 10 additions & 4 deletions packages/sdk/src/client_api/initial_subscription_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,20 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
TimeDuration,
// @ts-ignore
Timestamp,
// @ts-ignore
deepEqual,
} from '..';
// @ts-ignore
import { DatabaseUpdate as __DatabaseUpdate } from './database_update_type';

export type InitialSubscription = {
databaseUpdate: __DatabaseUpdate;
requestId: number;
totalHostExecutionDurationMicros: bigint;
totalHostExecutionDuration: TimeDuration;
};

/**
Expand All @@ -60,8 +66,8 @@ export namespace InitialSubscription {
),
new ProductTypeElement('requestId', AlgebraicType.createU32Type()),
new ProductTypeElement(
'totalHostExecutionDurationMicros',
AlgebraicType.createU64Type()
'totalHostExecutionDuration',
AlgebraicType.createTimeDurationType()
),
]);
}
Expand Down
14 changes: 10 additions & 4 deletions packages/sdk/src/client_api/one_off_query_response_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,21 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
TimeDuration,
// @ts-ignore
Timestamp,
// @ts-ignore
deepEqual,
} from '..';
// @ts-ignore
import { OneOffTable as __OneOffTable } from './one_off_table_type';

export type OneOffQueryResponse = {
messageId: Uint8Array;
error: string | undefined;
tables: __OneOffTable[];
totalHostExecutionDurationMicros: bigint;
totalHostExecutionDuration: TimeDuration;
};

/**
Expand Down Expand Up @@ -70,8 +76,8 @@ export namespace OneOffQueryResponse {
)
),
new ProductTypeElement(
'totalHostExecutionDurationMicros',
AlgebraicType.createU64Type()
'totalHostExecutionDuration',
AlgebraicType.createTimeDurationType()
),
]);
}
Expand Down
8 changes: 7 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,13 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
TimeDuration,
// @ts-ignore
Timestamp,
// @ts-ignore
deepEqual,
} from '..';
export type OneOffQuery = {
messageId: Uint8Array;
queryString: string;
Expand Down
8 changes: 7 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,13 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
TimeDuration,
// @ts-ignore
Timestamp,
// @ts-ignore
deepEqual,
} from '..';
// @ts-ignore
import { BsatnRowList as __BsatnRowList } from './bsatn_row_list_type';

Expand Down
8 changes: 7 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,13 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
TimeDuration,
// @ts-ignore
Timestamp,
// @ts-ignore
deepEqual,
} from '..';
// @ts-ignore
import { BsatnRowList as __BsatnRowList } from './bsatn_row_list_type';

Expand Down
8 changes: 7 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,13 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
TimeDuration,
// @ts-ignore
Timestamp,
// @ts-ignore
deepEqual,
} from '..';
export type ReducerCallInfo = {
reducerName: string;
reducerId: number;
Expand Down
8 changes: 7 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,13 @@ import {
SumTypeVariant,
// @ts-ignore
TableCache,
} from '../index';
// @ts-ignore
TimeDuration,
// @ts-ignore
Timestamp,
// @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
Loading
Loading