@instantdb/core 0.22.139 → 0.22.140

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/dist/commonjs/Connection.d.ts +2 -1
  2. package/dist/commonjs/Connection.d.ts.map +1 -1
  3. package/dist/commonjs/Connection.js +6 -2
  4. package/dist/commonjs/Connection.js.map +1 -1
  5. package/dist/commonjs/Reactor.d.ts +12 -0
  6. package/dist/commonjs/Reactor.d.ts.map +1 -1
  7. package/dist/commonjs/Reactor.js +63 -18
  8. package/dist/commonjs/Reactor.js.map +1 -1
  9. package/dist/commonjs/Stream.d.ts +112 -0
  10. package/dist/commonjs/Stream.d.ts.map +1 -0
  11. package/dist/commonjs/Stream.js +708 -0
  12. package/dist/commonjs/Stream.js.map +1 -0
  13. package/dist/commonjs/index.d.ts +41 -2
  14. package/dist/commonjs/index.d.ts.map +1 -1
  15. package/dist/commonjs/index.js +41 -1
  16. package/dist/commonjs/index.js.map +1 -1
  17. package/dist/esm/Connection.d.ts +2 -1
  18. package/dist/esm/Connection.d.ts.map +1 -1
  19. package/dist/esm/Connection.js +6 -2
  20. package/dist/esm/Connection.js.map +1 -1
  21. package/dist/esm/Reactor.d.ts +12 -0
  22. package/dist/esm/Reactor.d.ts.map +1 -1
  23. package/dist/esm/Reactor.js +53 -9
  24. package/dist/esm/Reactor.js.map +1 -1
  25. package/dist/esm/Stream.d.ts +112 -0
  26. package/dist/esm/Stream.d.ts.map +1 -0
  27. package/dist/esm/Stream.js +701 -0
  28. package/dist/esm/Stream.js.map +1 -0
  29. package/dist/esm/index.d.ts +41 -2
  30. package/dist/esm/index.d.ts.map +1 -1
  31. package/dist/esm/index.js +42 -1
  32. package/dist/esm/index.js.map +1 -1
  33. package/dist/standalone/index.js +2203 -1604
  34. package/dist/standalone/index.umd.cjs +3 -3
  35. package/package.json +2 -2
  36. package/src/Connection.ts +6 -2
  37. package/src/Reactor.js +56 -10
  38. package/src/Stream.ts +1016 -0
  39. package/src/index.ts +71 -1
  40. package/tsconfig.json +2 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@instantdb/core",
3
- "version": "0.22.139",
3
+ "version": "0.22.140",
4
4
  "description": "Instant's core local abstraction",
5
5
  "homepage": "https://github.com/instantdb/instant/tree/main/client/packages/core",
6
6
  "repository": {
@@ -53,7 +53,7 @@
53
53
  "dependencies": {
54
54
  "mutative": "^1.0.10",
55
55
  "uuid": "^11.1.0",
56
- "@instantdb/version": "0.22.139"
56
+ "@instantdb/version": "0.22.140"
57
57
  },
58
58
  "scripts": {
59
59
  "test": "vitest",
package/src/Connection.ts CHANGED
@@ -130,6 +130,7 @@ export class SSEConnection implements Connection<EventSourceType> {
130
130
  private closeFired: boolean = false;
131
131
  private sseInitTimeout: ReturnType<typeof setTimeout> | undefined = undefined;
132
132
  private ES: EventSourceConstructor;
133
+ private messageUrl: string;
133
134
  conn: EventSourceType;
134
135
  url: string;
135
136
  id: string;
@@ -138,9 +139,10 @@ export class SSEConnection implements Connection<EventSourceType> {
138
139
  onclose: (event: CloseEvent<EventSourceType>) => void;
139
140
  onerror: (event: ErrorEvent<EventSourceType>) => void;
140
141
 
141
- constructor(ES: EventSourceConstructor, url: string) {
142
+ constructor(ES: EventSourceConstructor, url: string, messageUrl?: string) {
142
143
  this.id = `${this.type}_${_connId++}`;
143
144
  this.url = url;
145
+ this.messageUrl = messageUrl || this.url;
144
146
  this.ES = ES;
145
147
  this.conn = new ES(url);
146
148
 
@@ -208,8 +210,10 @@ export class SSEConnection implements Connection<EventSourceType> {
208
210
  }
209
211
 
210
212
  private async postMessages(messages: any[]): Promise<void> {
213
+ // TODO(dww): Create a connection with chunked encoding so we can
214
+ // send multiple messages over one request
211
215
  try {
212
- const resp = await fetch(this.url, {
216
+ const resp = await fetch(this.messageUrl, {
213
217
  method: 'POST',
214
218
  headers: { 'Content-Type': 'application/json' },
215
219
  body: JSON.stringify({
package/src/Reactor.js CHANGED
@@ -31,6 +31,7 @@ import { InstantAPIError } from './utils/fetch.ts';
31
31
  import { validate as validateUUID } from 'uuid';
32
32
  import { WSConnection, SSEConnection } from './Connection.ts';
33
33
  import { SyncTable } from './SyncTable.ts';
34
+ import { InstantStream } from './Stream.ts';
34
35
 
35
36
  /** @typedef {import('./utils/log.ts').Logger} Logger */
36
37
  /** @typedef {import('./Connection.ts').Connection} Connection */
@@ -39,7 +40,7 @@ import { SyncTable } from './SyncTable.ts';
39
40
  /** @typedef {import('./reactorTypes.ts').QuerySub} QuerySub */
40
41
  /** @typedef {import('./reactorTypes.ts').QuerySubInStorage} QuerySubInStorage */
41
42
 
42
- const STATUS = {
43
+ export const STATUS = {
43
44
  CONNECTING: 'connecting',
44
45
  OPENED: 'opened',
45
46
  AUTHENTICATED: 'authenticated',
@@ -218,6 +219,8 @@ export default class Reactor {
218
219
 
219
220
  /** @type {SyncTable} */
220
221
  _syncTable;
222
+ /** @type {InstantStream} */
223
+ _instantStream;
221
224
 
222
225
  /** @type {Record<string, Array<{ q: any, cb: (data: any) => any }>>} */
223
226
  queryCbs = {};
@@ -348,6 +351,13 @@ export default class Reactor {
348
351
  () => this.ensureAttrs(),
349
352
  );
350
353
 
354
+ this._instantStream = new InstantStream({
355
+ WStream: this.config.WritableStream || WritableStream,
356
+ RStream: this.config.ReadableStream || ReadableStream,
357
+ trySend: this._trySendAuthed.bind(this),
358
+ log: this._log,
359
+ });
360
+
351
361
  this._oauthCallbackResponse = this._oauthLoginInit();
352
362
 
353
363
  // kick off a request to cache it
@@ -518,6 +528,7 @@ export default class Reactor {
518
528
  this.status = status;
519
529
  this._errorMessage = err;
520
530
  this.notifyConnectionStatusSubs(status);
531
+ this._instantStream.onConnectionStatusChange(status);
521
532
  }
522
533
 
523
534
  _onMergeKv = (key, storageV, inMemoryV) => {
@@ -679,6 +690,22 @@ export default class Reactor {
679
690
  this._syncTable.onSyncUpdateTriples(msg);
680
691
  break;
681
692
  }
693
+ case 'start-stream-ok': {
694
+ this._instantStream.onStartStreamOk(msg);
695
+ break;
696
+ }
697
+ case 'stream-flushed': {
698
+ this._instantStream.onStreamFlushed(msg);
699
+ break;
700
+ }
701
+ case 'append-failed': {
702
+ this._instantStream.onAppendFailed(msg);
703
+ break;
704
+ }
705
+ case 'stream-append': {
706
+ this._instantStream.onStreamAppend(msg);
707
+ break;
708
+ }
682
709
  case 'refresh-ok': {
683
710
  const { computations, attrs } = msg;
684
711
  const processedTxId = msg['processed-tx-id'];
@@ -862,6 +889,14 @@ export default class Reactor {
862
889
  }
863
890
  }
864
891
 
892
+ createWriteStream(opts) {
893
+ return this._instantStream.createWriteStream(opts);
894
+ }
895
+
896
+ createReadStream(opts) {
897
+ return this._instantStream.createReadStream(opts);
898
+ }
899
+
865
900
  _pendingMutations() {
866
901
  return this.kv.currentValue.pendingMutations ?? new Map();
867
902
  }
@@ -947,14 +982,22 @@ export default class Reactor {
947
982
  return;
948
983
  }
949
984
 
950
- if (msg['original-event']?.op === 'resync-table') {
951
- this._syncTable.onResyncError(msg);
952
- return;
953
- }
954
-
955
- if (msg['original-event']?.op === 'start-sync') {
956
- this._syncTable.onStartSyncError(msg);
957
- return;
985
+ switch (msg['original-event']?.op) {
986
+ case 'resync-table': {
987
+ this._syncTable.onResyncError(msg);
988
+ return;
989
+ }
990
+ case 'start-sync': {
991
+ this._syncTable.onStartSyncError(msg);
992
+ return;
993
+ }
994
+ case 'start-stream':
995
+ case 'append-stream':
996
+ case 'subscribe-stream':
997
+ case 'unsubscribe-stream': {
998
+ this._instantStream.onRecieveError(msg);
999
+ return;
1000
+ }
958
1001
  }
959
1002
  // We've caught some error which has no corresponding listener.
960
1003
  // Let's console.error to let the user know.
@@ -1578,7 +1621,10 @@ export default class Reactor {
1578
1621
  return;
1579
1622
  }
1580
1623
  if (!ignoreLogging[msg.op]) {
1581
- this._log.info('[send]', this._transport.id, msg.op, msg);
1624
+ this._log.info('[send]', this._transport.id, msg.op, {
1625
+ 'client-event-id': eventId,
1626
+ ...msg,
1627
+ });
1582
1628
  }
1583
1629
  switch (msg.op) {
1584
1630
  case 'transact': {