@powersync/common 0.0.0-dev-20251209082930 → 0.0.0-dev-20260120150240

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 (39) hide show
  1. package/dist/bundle.cjs +366 -2142
  2. package/dist/bundle.cjs.map +1 -1
  3. package/dist/bundle.mjs +365 -2143
  4. package/dist/bundle.mjs.map +1 -1
  5. package/dist/bundle.node.cjs +153 -100
  6. package/dist/bundle.node.cjs.map +1 -1
  7. package/dist/bundle.node.mjs +152 -101
  8. package/dist/bundle.node.mjs.map +1 -1
  9. package/dist/index.d.cts +26 -3
  10. package/lib/client/AbstractPowerSyncDatabase.d.ts +1 -1
  11. package/lib/client/AbstractPowerSyncDatabase.js +2 -2
  12. package/lib/client/AbstractPowerSyncDatabase.js.map +1 -1
  13. package/lib/client/sync/stream/AbstractRemote.js +41 -32
  14. package/lib/client/sync/stream/AbstractRemote.js.map +1 -1
  15. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +4 -0
  16. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +8 -0
  17. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +1 -1
  18. package/lib/client/sync/stream/streaming-sync-types.d.ts +4 -0
  19. package/lib/client/sync/stream/streaming-sync-types.js.map +1 -1
  20. package/lib/db/ConnectionClosedError.d.ts +10 -0
  21. package/lib/db/ConnectionClosedError.js +21 -0
  22. package/lib/db/ConnectionClosedError.js.map +1 -0
  23. package/lib/db/crud/SyncStatus.d.ts +4 -0
  24. package/lib/db/crud/SyncStatus.js +4 -0
  25. package/lib/db/crud/SyncStatus.js.map +1 -1
  26. package/lib/index.d.ts +2 -0
  27. package/lib/index.js +2 -0
  28. package/lib/index.js.map +1 -1
  29. package/lib/utils/DataStream.js +11 -2
  30. package/lib/utils/DataStream.js.map +1 -1
  31. package/package.json +4 -3
  32. package/src/client/AbstractPowerSyncDatabase.ts +2 -2
  33. package/src/client/sync/stream/AbstractRemote.ts +47 -35
  34. package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +19 -2
  35. package/src/client/sync/stream/streaming-sync-types.ts +5 -0
  36. package/src/db/ConnectionClosedError.ts +23 -0
  37. package/src/db/crud/SyncStatus.ts +4 -0
  38. package/src/index.ts +2 -0
  39. package/src/utils/DataStream.ts +13 -2
@@ -581,6 +581,10 @@ class SyncStatus {
581
581
  priorityStatusEntries: this.priorityStatusEntries
582
582
  };
583
583
  }
584
+ /**
585
+ * Not all errors are serializable over a MessagePort. E.g. some `DomExceptions` fail to be passed across workers.
586
+ * This explicitly serializes errors in the SyncStatus.
587
+ */
584
588
  serializeError(error) {
585
589
  if (typeof error == 'undefined') {
586
590
  return undefined;
@@ -1945,7 +1949,7 @@ var hasRequiredFrames;
1945
1949
  function requireFrames () {
1946
1950
  if (hasRequiredFrames) return Frames;
1947
1951
  hasRequiredFrames = 1;
1948
- (function (exports) {
1952
+ (function (exports$1) {
1949
1953
  /*
1950
1954
  * Copyright 2021-2022 the original author or authors.
1951
1955
  *
@@ -1961,8 +1965,8 @@ function requireFrames () {
1961
1965
  * See the License for the specific language governing permissions and
1962
1966
  * limitations under the License.
1963
1967
  */
1964
- Object.defineProperty(exports, "__esModule", { value: true });
1965
- exports.Frame = exports.Lengths = exports.Flags = exports.FrameTypes = void 0;
1968
+ Object.defineProperty(exports$1, "__esModule", { value: true });
1969
+ exports$1.Frame = exports$1.Lengths = exports$1.Flags = exports$1.FrameTypes = void 0;
1966
1970
  var FrameTypes;
1967
1971
  (function (FrameTypes) {
1968
1972
  FrameTypes[FrameTypes["RESERVED"] = 0] = "RESERVED";
@@ -1981,7 +1985,7 @@ function requireFrames () {
1981
1985
  FrameTypes[FrameTypes["RESUME"] = 13] = "RESUME";
1982
1986
  FrameTypes[FrameTypes["RESUME_OK"] = 14] = "RESUME_OK";
1983
1987
  FrameTypes[FrameTypes["EXT"] = 63] = "EXT";
1984
- })(FrameTypes = exports.FrameTypes || (exports.FrameTypes = {}));
1988
+ })(FrameTypes = exports$1.FrameTypes || (exports$1.FrameTypes = {}));
1985
1989
  (function (Flags) {
1986
1990
  Flags[Flags["NONE"] = 0] = "NONE";
1987
1991
  Flags[Flags["COMPLETE"] = 64] = "COMPLETE";
@@ -1992,7 +1996,7 @@ function requireFrames () {
1992
1996
  Flags[Flags["NEXT"] = 32] = "NEXT";
1993
1997
  Flags[Flags["RESPOND"] = 128] = "RESPOND";
1994
1998
  Flags[Flags["RESUME_ENABLE"] = 128] = "RESUME_ENABLE";
1995
- })(exports.Flags || (exports.Flags = {}));
1999
+ })(exports$1.Flags || (exports$1.Flags = {}));
1996
2000
  (function (Flags) {
1997
2001
  function hasMetadata(flags) {
1998
2002
  return (flags & Flags.METADATA) === Flags.METADATA;
@@ -2026,13 +2030,13 @@ function requireFrames () {
2026
2030
  return (flags & Flags.RESUME_ENABLE) === Flags.RESUME_ENABLE;
2027
2031
  }
2028
2032
  Flags.hasResume = hasResume;
2029
- })(exports.Flags || (exports.Flags = {}));
2033
+ })(exports$1.Flags || (exports$1.Flags = {}));
2030
2034
  (function (Lengths) {
2031
2035
  Lengths[Lengths["FRAME"] = 3] = "FRAME";
2032
2036
  Lengths[Lengths["HEADER"] = 6] = "HEADER";
2033
2037
  Lengths[Lengths["METADATA"] = 3] = "METADATA";
2034
2038
  Lengths[Lengths["REQUEST"] = 3] = "REQUEST";
2035
- })(exports.Lengths || (exports.Lengths = {}));
2039
+ })(exports$1.Lengths || (exports$1.Lengths = {}));
2036
2040
  (function (Frame) {
2037
2041
  function isConnection(frame) {
2038
2042
  return frame.streamId === 0;
@@ -2043,7 +2047,7 @@ function requireFrames () {
2043
2047
  frame.type <= FrameTypes.REQUEST_CHANNEL);
2044
2048
  }
2045
2049
  Frame.isRequest = isRequest;
2046
- })(exports.Frame || (exports.Frame = {}));
2050
+ })(exports$1.Frame || (exports$1.Frame = {}));
2047
2051
 
2048
2052
  } (Frames));
2049
2053
  return Frames;
@@ -2054,7 +2058,7 @@ var hasRequiredCodecs;
2054
2058
  function requireCodecs () {
2055
2059
  if (hasRequiredCodecs) return Codecs;
2056
2060
  hasRequiredCodecs = 1;
2057
- (function (exports) {
2061
+ (function (exports$1) {
2058
2062
  var __generator = (Codecs && Codecs.__generator) || function (thisArg, body) {
2059
2063
  var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
2060
2064
  return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
@@ -2082,22 +2086,22 @@ function requireCodecs () {
2082
2086
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
2083
2087
  }
2084
2088
  };
2085
- Object.defineProperty(exports, "__esModule", { value: true });
2086
- exports.Deserializer = exports.sizeOfFrame = exports.serializeFrame = exports.deserializeFrame = exports.serializeFrameWithLength = exports.deserializeFrames = exports.deserializeFrameWithLength = exports.writeUInt64BE = exports.readUInt64BE = exports.writeUInt24BE = exports.readUInt24BE = exports.MAX_VERSION = exports.MAX_TTL = exports.MAX_STREAM_ID = exports.MAX_RESUME_LENGTH = exports.MAX_REQUEST_N = exports.MAX_REQUEST_COUNT = exports.MAX_MIME_LENGTH = exports.MAX_METADATA_LENGTH = exports.MAX_LIFETIME = exports.MAX_KEEPALIVE = exports.MAX_CODE = exports.FRAME_TYPE_OFFFSET = exports.FLAGS_MASK = void 0;
2089
+ Object.defineProperty(exports$1, "__esModule", { value: true });
2090
+ exports$1.Deserializer = exports$1.sizeOfFrame = exports$1.serializeFrame = exports$1.deserializeFrame = exports$1.serializeFrameWithLength = exports$1.deserializeFrames = exports$1.deserializeFrameWithLength = exports$1.writeUInt64BE = exports$1.readUInt64BE = exports$1.writeUInt24BE = exports$1.readUInt24BE = exports$1.MAX_VERSION = exports$1.MAX_TTL = exports$1.MAX_STREAM_ID = exports$1.MAX_RESUME_LENGTH = exports$1.MAX_REQUEST_N = exports$1.MAX_REQUEST_COUNT = exports$1.MAX_MIME_LENGTH = exports$1.MAX_METADATA_LENGTH = exports$1.MAX_LIFETIME = exports$1.MAX_KEEPALIVE = exports$1.MAX_CODE = exports$1.FRAME_TYPE_OFFFSET = exports$1.FLAGS_MASK = void 0;
2087
2091
  var Frames_1 = requireFrames();
2088
- exports.FLAGS_MASK = 0x3ff; // low 10 bits
2089
- exports.FRAME_TYPE_OFFFSET = 10; // frame type is offset 10 bytes within the uint16 containing type + flags
2090
- exports.MAX_CODE = 0x7fffffff; // uint31
2091
- exports.MAX_KEEPALIVE = 0x7fffffff; // uint31
2092
- exports.MAX_LIFETIME = 0x7fffffff; // uint31
2093
- exports.MAX_METADATA_LENGTH = 0xffffff; // uint24
2094
- exports.MAX_MIME_LENGTH = 0xff; // int8
2095
- exports.MAX_REQUEST_COUNT = 0x7fffffff; // uint31
2096
- exports.MAX_REQUEST_N = 0x7fffffff; // uint31
2097
- exports.MAX_RESUME_LENGTH = 0xffff; // uint16
2098
- exports.MAX_STREAM_ID = 0x7fffffff; // uint31
2099
- exports.MAX_TTL = 0x7fffffff; // uint31
2100
- exports.MAX_VERSION = 0xffff; // uint16
2092
+ exports$1.FLAGS_MASK = 0x3ff; // low 10 bits
2093
+ exports$1.FRAME_TYPE_OFFFSET = 10; // frame type is offset 10 bytes within the uint16 containing type + flags
2094
+ exports$1.MAX_CODE = 0x7fffffff; // uint31
2095
+ exports$1.MAX_KEEPALIVE = 0x7fffffff; // uint31
2096
+ exports$1.MAX_LIFETIME = 0x7fffffff; // uint31
2097
+ exports$1.MAX_METADATA_LENGTH = 0xffffff; // uint24
2098
+ exports$1.MAX_MIME_LENGTH = 0xff; // int8
2099
+ exports$1.MAX_REQUEST_COUNT = 0x7fffffff; // uint31
2100
+ exports$1.MAX_REQUEST_N = 0x7fffffff; // uint31
2101
+ exports$1.MAX_RESUME_LENGTH = 0xffff; // uint16
2102
+ exports$1.MAX_STREAM_ID = 0x7fffffff; // uint31
2103
+ exports$1.MAX_TTL = 0x7fffffff; // uint31
2104
+ exports$1.MAX_VERSION = 0xffff; // uint16
2101
2105
  /**
2102
2106
  * Mimimum value that would overflow bitwise operators (2^32).
2103
2107
  */
@@ -2111,7 +2115,7 @@ function requireCodecs () {
2111
2115
  var val3 = buffer.readUInt8(offset + 2);
2112
2116
  return val1 | val2 | val3;
2113
2117
  }
2114
- exports.readUInt24BE = readUInt24BE;
2118
+ exports$1.readUInt24BE = readUInt24BE;
2115
2119
  /**
2116
2120
  * Writes a uint24 to a buffer starting at the given offset, returning the
2117
2121
  * offset of the next byte.
@@ -2121,7 +2125,7 @@ function requireCodecs () {
2121
2125
  offset = buffer.writeUInt8((value >>> 8) & 0xff, offset); // 2nd byte
2122
2126
  return buffer.writeUInt8(value & 0xff, offset); // 1st byte
2123
2127
  }
2124
- exports.writeUInt24BE = writeUInt24BE;
2128
+ exports$1.writeUInt24BE = writeUInt24BE;
2125
2129
  /**
2126
2130
  * Read a uint64 (technically supports up to 53 bits per JS number
2127
2131
  * representation).
@@ -2131,7 +2135,7 @@ function requireCodecs () {
2131
2135
  var low = buffer.readUInt32BE(offset + 4);
2132
2136
  return high * BITWISE_OVERFLOW + low;
2133
2137
  }
2134
- exports.readUInt64BE = readUInt64BE;
2138
+ exports$1.readUInt64BE = readUInt64BE;
2135
2139
  /**
2136
2140
  * Write a uint64 (technically supports up to 53 bits per JS number
2137
2141
  * representation).
@@ -2142,7 +2146,7 @@ function requireCodecs () {
2142
2146
  offset = buffer.writeUInt32BE(high, offset); // first half of uint64
2143
2147
  return buffer.writeUInt32BE(low, offset); // second half of uint64
2144
2148
  }
2145
- exports.writeUInt64BE = writeUInt64BE;
2149
+ exports$1.writeUInt64BE = writeUInt64BE;
2146
2150
  /**
2147
2151
  * Frame header is:
2148
2152
  * - stream id (uint32 = 4)
@@ -2160,7 +2164,7 @@ function requireCodecs () {
2160
2164
  var frameLength = readUInt24BE(buffer, 0);
2161
2165
  return deserializeFrame(buffer.slice(UINT24_SIZE, UINT24_SIZE + frameLength));
2162
2166
  }
2163
- exports.deserializeFrameWithLength = deserializeFrameWithLength;
2167
+ exports$1.deserializeFrameWithLength = deserializeFrameWithLength;
2164
2168
  /**
2165
2169
  * Given a buffer that may contain zero or more length-prefixed frames followed
2166
2170
  * by zero or more bytes of a (partial) subsequent frame, returns an array of
@@ -2193,7 +2197,7 @@ function requireCodecs () {
2193
2197
  }
2194
2198
  });
2195
2199
  }
2196
- exports.deserializeFrames = deserializeFrames;
2200
+ exports$1.deserializeFrames = deserializeFrames;
2197
2201
  /**
2198
2202
  * Writes a frame to a buffer with a length prefix.
2199
2203
  */
@@ -2204,7 +2208,7 @@ function requireCodecs () {
2204
2208
  buffer.copy(lengthPrefixed, UINT24_SIZE);
2205
2209
  return lengthPrefixed;
2206
2210
  }
2207
- exports.serializeFrameWithLength = serializeFrameWithLength;
2211
+ exports$1.serializeFrameWithLength = serializeFrameWithLength;
2208
2212
  /**
2209
2213
  * Read a frame from the buffer.
2210
2214
  */
@@ -2219,8 +2223,8 @@ function requireCodecs () {
2219
2223
  // );
2220
2224
  var typeAndFlags = buffer.readUInt16BE(offset);
2221
2225
  offset += 2;
2222
- var type = typeAndFlags >>> exports.FRAME_TYPE_OFFFSET; // keep highest 6 bits
2223
- var flags = typeAndFlags & exports.FLAGS_MASK; // keep lowest 10 bits
2226
+ var type = typeAndFlags >>> exports$1.FRAME_TYPE_OFFFSET; // keep highest 6 bits
2227
+ var flags = typeAndFlags & exports$1.FLAGS_MASK; // keep lowest 10 bits
2224
2228
  switch (type) {
2225
2229
  case Frames_1.FrameTypes.SETUP:
2226
2230
  return deserializeSetupFrame(buffer, streamId, flags);
@@ -2257,7 +2261,7 @@ function requireCodecs () {
2257
2261
  // );
2258
2262
  }
2259
2263
  }
2260
- exports.deserializeFrame = deserializeFrame;
2264
+ exports$1.deserializeFrame = deserializeFrame;
2261
2265
  /**
2262
2266
  * Convert the frame to a (binary) buffer.
2263
2267
  */
@@ -2296,7 +2300,7 @@ function requireCodecs () {
2296
2300
  // );
2297
2301
  }
2298
2302
  }
2299
- exports.serializeFrame = serializeFrame;
2303
+ exports$1.serializeFrame = serializeFrame;
2300
2304
  /**
2301
2305
  * Byte size of frame without size prefix
2302
2306
  */
@@ -2335,7 +2339,7 @@ function requireCodecs () {
2335
2339
  // );
2336
2340
  }
2337
2341
  }
2338
- exports.sizeOfFrame = sizeOfFrame;
2342
+ exports$1.sizeOfFrame = sizeOfFrame;
2339
2343
  /**
2340
2344
  * Writes a SETUP frame into a new buffer and returns it.
2341
2345
  *
@@ -2970,7 +2974,7 @@ function requireCodecs () {
2970
2974
  function writeHeader(frame, buffer) {
2971
2975
  var offset = buffer.writeInt32BE(frame.streamId, 0);
2972
2976
  // shift frame to high 6 bits, extract lowest 10 bits from flags
2973
- return buffer.writeUInt16BE((frame.type << exports.FRAME_TYPE_OFFFSET) | (frame.flags & exports.FLAGS_MASK), offset);
2977
+ return buffer.writeUInt16BE((frame.type << exports$1.FRAME_TYPE_OFFFSET) | (frame.flags & exports$1.FLAGS_MASK), offset);
2974
2978
  }
2975
2979
  /**
2976
2980
  * Determine the length of the payload section of a frame. Only applies to
@@ -3051,7 +3055,7 @@ function requireCodecs () {
3051
3055
  };
3052
3056
  return Deserializer;
3053
3057
  }());
3054
- exports.Deserializer = Deserializer;
3058
+ exports$1.Deserializer = Deserializer;
3055
3059
 
3056
3060
  } (Codecs));
3057
3061
  return Codecs;
@@ -3197,7 +3201,7 @@ var hasRequiredErrors;
3197
3201
  function requireErrors () {
3198
3202
  if (hasRequiredErrors) return Errors;
3199
3203
  hasRequiredErrors = 1;
3200
- (function (exports) {
3204
+ (function (exports$1) {
3201
3205
  /*
3202
3206
  * Copyright 2021-2022 the original author or authors.
3203
3207
  *
@@ -3228,8 +3232,8 @@ function requireErrors () {
3228
3232
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
3229
3233
  };
3230
3234
  })();
3231
- Object.defineProperty(exports, "__esModule", { value: true });
3232
- exports.ErrorCodes = exports.RSocketError = void 0;
3235
+ Object.defineProperty(exports$1, "__esModule", { value: true });
3236
+ exports$1.ErrorCodes = exports$1.RSocketError = void 0;
3233
3237
  var RSocketError = /** @class */ (function (_super) {
3234
3238
  __extends(RSocketError, _super);
3235
3239
  function RSocketError(code, message) {
@@ -3239,7 +3243,7 @@ function requireErrors () {
3239
3243
  }
3240
3244
  return RSocketError;
3241
3245
  }(Error));
3242
- exports.RSocketError = RSocketError;
3246
+ exports$1.RSocketError = RSocketError;
3243
3247
  (function (ErrorCodes) {
3244
3248
  ErrorCodes[ErrorCodes["RESERVED"] = 0] = "RESERVED";
3245
3249
  ErrorCodes[ErrorCodes["INVALID_SETUP"] = 1] = "INVALID_SETUP";
@@ -3253,7 +3257,7 @@ function requireErrors () {
3253
3257
  ErrorCodes[ErrorCodes["CANCELED"] = 515] = "CANCELED";
3254
3258
  ErrorCodes[ErrorCodes["INVALID"] = 516] = "INVALID";
3255
3259
  ErrorCodes[ErrorCodes["RESERVED_EXTENSION"] = 4294967295] = "RESERVED_EXTENSION";
3256
- })(exports.ErrorCodes || (exports.ErrorCodes = {}));
3260
+ })(exports$1.ErrorCodes || (exports$1.ErrorCodes = {}));
3257
3261
 
3258
3262
  } (Errors));
3259
3263
  return Errors;
@@ -3295,7 +3299,7 @@ var hasRequiredClientServerMultiplexerDemultiplexer;
3295
3299
  function requireClientServerMultiplexerDemultiplexer () {
3296
3300
  if (hasRequiredClientServerMultiplexerDemultiplexer) return ClientServerMultiplexerDemultiplexer;
3297
3301
  hasRequiredClientServerMultiplexerDemultiplexer = 1;
3298
- (function (exports) {
3302
+ (function (exports$1) {
3299
3303
  /*
3300
3304
  * Copyright 2021-2022 the original author or authors.
3301
3305
  *
@@ -3362,8 +3366,8 @@ function requireClientServerMultiplexerDemultiplexer () {
3362
3366
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
3363
3367
  }
3364
3368
  };
3365
- Object.defineProperty(exports, "__esModule", { value: true });
3366
- exports.ResumeOkAwaitingResumableClientServerInputMultiplexerDemultiplexer = exports.ResumableClientServerInputMultiplexerDemultiplexer = exports.ClientServerInputMultiplexerDemultiplexer = exports.StreamIdGenerator = void 0;
3369
+ Object.defineProperty(exports$1, "__esModule", { value: true });
3370
+ exports$1.ResumeOkAwaitingResumableClientServerInputMultiplexerDemultiplexer = exports$1.ResumableClientServerInputMultiplexerDemultiplexer = exports$1.ClientServerInputMultiplexerDemultiplexer = exports$1.StreamIdGenerator = void 0;
3367
3371
  var _1 = requireDist();
3368
3372
  var Deferred_1 = requireDeferred();
3369
3373
  var Errors_1 = requireErrors();
@@ -3386,7 +3390,7 @@ function requireClientServerMultiplexerDemultiplexer () {
3386
3390
  };
3387
3391
  return StreamIdGeneratorImpl;
3388
3392
  }());
3389
- })(exports.StreamIdGenerator || (exports.StreamIdGenerator = {}));
3393
+ })(exports$1.StreamIdGenerator || (exports$1.StreamIdGenerator = {}));
3390
3394
  var ClientServerInputMultiplexerDemultiplexer = /** @class */ (function (_super) {
3391
3395
  __extends(ClientServerInputMultiplexerDemultiplexer, _super);
3392
3396
  function ClientServerInputMultiplexerDemultiplexer(streamIdSupplier, outbound, closeable) {
@@ -3475,7 +3479,7 @@ function requireClientServerMultiplexerDemultiplexer () {
3475
3479
  };
3476
3480
  return ClientServerInputMultiplexerDemultiplexer;
3477
3481
  }(Deferred_1.Deferred));
3478
- exports.ClientServerInputMultiplexerDemultiplexer = ClientServerInputMultiplexerDemultiplexer;
3482
+ exports$1.ClientServerInputMultiplexerDemultiplexer = ClientServerInputMultiplexerDemultiplexer;
3479
3483
  var ResumableClientServerInputMultiplexerDemultiplexer = /** @class */ (function (_super) {
3480
3484
  __extends(ResumableClientServerInputMultiplexerDemultiplexer, _super);
3481
3485
  function ResumableClientServerInputMultiplexerDemultiplexer(streamIdSupplier, outbound, closeable, frameStore, token, sessionStoreOrReconnector, sessionTimeout) {
@@ -3632,7 +3636,7 @@ function requireClientServerMultiplexerDemultiplexer () {
3632
3636
  };
3633
3637
  return ResumableClientServerInputMultiplexerDemultiplexer;
3634
3638
  }(ClientServerInputMultiplexerDemultiplexer));
3635
- exports.ResumableClientServerInputMultiplexerDemultiplexer = ResumableClientServerInputMultiplexerDemultiplexer;
3639
+ exports$1.ResumableClientServerInputMultiplexerDemultiplexer = ResumableClientServerInputMultiplexerDemultiplexer;
3636
3640
  var ResumeOkAwaitingResumableClientServerInputMultiplexerDemultiplexer = /** @class */ (function () {
3637
3641
  function ResumeOkAwaitingResumableClientServerInputMultiplexerDemultiplexer(outbound, closeable, delegate) {
3638
3642
  this.outbound = outbound;
@@ -3689,7 +3693,7 @@ function requireClientServerMultiplexerDemultiplexer () {
3689
3693
  };
3690
3694
  return ResumeOkAwaitingResumableClientServerInputMultiplexerDemultiplexer;
3691
3695
  }());
3692
- exports.ResumeOkAwaitingResumableClientServerInputMultiplexerDemultiplexer = ResumeOkAwaitingResumableClientServerInputMultiplexerDemultiplexer;
3696
+ exports$1.ResumeOkAwaitingResumableClientServerInputMultiplexerDemultiplexer = ResumeOkAwaitingResumableClientServerInputMultiplexerDemultiplexer;
3693
3697
 
3694
3698
  } (ClientServerMultiplexerDemultiplexer));
3695
3699
  return ClientServerMultiplexerDemultiplexer;
@@ -6675,7 +6679,7 @@ var hasRequiredDist;
6675
6679
  function requireDist () {
6676
6680
  if (hasRequiredDist) return dist;
6677
6681
  hasRequiredDist = 1;
6678
- (function (exports) {
6682
+ (function (exports$1) {
6679
6683
  /*
6680
6684
  * Copyright 2021-2022 the original author or authors.
6681
6685
  *
@@ -6698,19 +6702,19 @@ function requireDist () {
6698
6702
  if (k2 === undefined) k2 = k;
6699
6703
  o[k2] = m[k];
6700
6704
  }));
6701
- var __exportStar = (dist && dist.__exportStar) || function(m, exports) {
6702
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
6705
+ var __exportStar = (dist && dist.__exportStar) || function(m, exports$1) {
6706
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports$1, p)) __createBinding(exports$1, m, p);
6703
6707
  };
6704
- Object.defineProperty(exports, "__esModule", { value: true });
6705
- __exportStar(requireCodecs(), exports);
6706
- __exportStar(requireCommon(), exports);
6707
- __exportStar(requireDeferred(), exports);
6708
- __exportStar(requireErrors(), exports);
6709
- __exportStar(requireFrames(), exports);
6710
- __exportStar(requireRSocket(), exports);
6711
- __exportStar(requireRSocketConnector(), exports);
6712
- __exportStar(requireRSocketServer(), exports);
6713
- __exportStar(requireTransport(), exports);
6708
+ Object.defineProperty(exports$1, "__esModule", { value: true });
6709
+ __exportStar(requireCodecs(), exports$1);
6710
+ __exportStar(requireCommon(), exports$1);
6711
+ __exportStar(requireDeferred(), exports$1);
6712
+ __exportStar(requireErrors(), exports$1);
6713
+ __exportStar(requireFrames(), exports$1);
6714
+ __exportStar(requireRSocket(), exports$1);
6715
+ __exportStar(requireRSocketConnector(), exports$1);
6716
+ __exportStar(requireRSocketServer(), exports$1);
6717
+ __exportStar(requireTransport(), exports$1);
6714
6718
 
6715
6719
  } (dist));
6716
6720
  return dist;
@@ -6718,7 +6722,7 @@ function requireDist () {
6718
6722
 
6719
6723
  var distExports = requireDist();
6720
6724
 
6721
- var version = "1.43.1";
6725
+ var version = "1.45.0";
6722
6726
  var PACKAGE = {
6723
6727
  version: version};
6724
6728
 
@@ -6789,6 +6793,16 @@ class DataStream extends BaseObserver {
6789
6793
  * @returns a Data payload or Null if the stream closed.
6790
6794
  */
6791
6795
  async read() {
6796
+ if (this.closed) {
6797
+ return null;
6798
+ }
6799
+ // Wait for any pending processing to complete first.
6800
+ // This ensures we register our listener before calling processQueue(),
6801
+ // avoiding a race where processQueue() sees no reader and returns early.
6802
+ if (this.processingPromise) {
6803
+ await this.processingPromise;
6804
+ }
6805
+ // Re-check after await - stream may have closed while we were waiting
6792
6806
  if (this.closed) {
6793
6807
  return null;
6794
6808
  }
@@ -6828,7 +6842,7 @@ class DataStream extends BaseObserver {
6828
6842
  }
6829
6843
  const promise = (this.processingPromise = this._processQueue());
6830
6844
  promise.finally(() => {
6831
- return (this.processingPromise = null);
6845
+ this.processingPromise = null;
6832
6846
  });
6833
6847
  return promise;
6834
6848
  }
@@ -6860,7 +6874,6 @@ class DataStream extends BaseObserver {
6860
6874
  this.notifyDataAdded = null;
6861
6875
  }
6862
6876
  if (this.dataQueue.length > 0) {
6863
- // Next tick
6864
6877
  setTimeout(() => this.processQueue());
6865
6878
  }
6866
6879
  }
@@ -7480,7 +7493,11 @@ class AbstractRemote {
7480
7493
  };
7481
7494
  const stream = new DataStream({
7482
7495
  logger: this.logger,
7483
- mapLine: mapLine
7496
+ mapLine: mapLine,
7497
+ pressure: {
7498
+ highWaterMark: 20,
7499
+ lowWaterMark: 10
7500
+ }
7484
7501
  });
7485
7502
  abortSignal?.addEventListener('abort', () => {
7486
7503
  closeReader();
@@ -7488,42 +7505,47 @@ class AbstractRemote {
7488
7505
  });
7489
7506
  const decoder = this.createTextDecoder();
7490
7507
  let buffer = '';
7491
- const l = stream.registerListener({
7492
- lowWater: async () => {
7493
- if (stream.closed || abortSignal?.aborted || readerReleased) {
7508
+ const consumeStream = async () => {
7509
+ while (!stream.closed && !abortSignal?.aborted && !readerReleased) {
7510
+ const { done, value } = await reader.read();
7511
+ if (done) {
7512
+ const remaining = buffer.trim();
7513
+ if (remaining.length != 0) {
7514
+ stream.enqueueData(remaining);
7515
+ }
7516
+ stream.close();
7517
+ await closeReader();
7494
7518
  return;
7495
7519
  }
7496
- try {
7497
- let didCompleteLine = false;
7498
- while (!didCompleteLine) {
7499
- const { done, value } = await reader.read();
7500
- if (done) {
7501
- const remaining = buffer.trim();
7502
- if (remaining.length != 0) {
7503
- stream.enqueueData(remaining);
7504
- }
7505
- stream.close();
7506
- await closeReader();
7507
- return;
7508
- }
7509
- const data = decoder.decode(value, { stream: true });
7510
- buffer += data;
7511
- const lines = buffer.split('\n');
7512
- for (var i = 0; i < lines.length - 1; i++) {
7513
- var l = lines[i].trim();
7514
- if (l.length > 0) {
7515
- stream.enqueueData(l);
7516
- didCompleteLine = true;
7517
- }
7518
- }
7519
- buffer = lines[lines.length - 1];
7520
+ const data = decoder.decode(value, { stream: true });
7521
+ buffer += data;
7522
+ const lines = buffer.split('\n');
7523
+ for (var i = 0; i < lines.length - 1; i++) {
7524
+ var l = lines[i].trim();
7525
+ if (l.length > 0) {
7526
+ stream.enqueueData(l);
7520
7527
  }
7521
7528
  }
7522
- catch (ex) {
7523
- stream.close();
7524
- throw ex;
7529
+ buffer = lines[lines.length - 1];
7530
+ // Implement backpressure by waiting for the low water mark to be reached
7531
+ if (stream.dataQueue.length > stream.highWatermark) {
7532
+ await new Promise((resolve) => {
7533
+ const dispose = stream.registerListener({
7534
+ lowWater: async () => {
7535
+ resolve();
7536
+ dispose();
7537
+ },
7538
+ closed: () => {
7539
+ resolve();
7540
+ dispose();
7541
+ }
7542
+ });
7543
+ });
7525
7544
  }
7526
- },
7545
+ }
7546
+ };
7547
+ consumeStream().catch(ex => this.logger.error('Error consuming stream', ex));
7548
+ const l = stream.registerListener({
7527
7549
  closed: () => {
7528
7550
  closeReader();
7529
7551
  l?.();
@@ -7645,6 +7667,7 @@ const DEFAULT_STREAMING_SYNC_OPTIONS = {
7645
7667
  crudUploadThrottleMs: DEFAULT_CRUD_UPLOAD_THROTTLE_MS
7646
7668
  };
7647
7669
  const DEFAULT_STREAM_CONNECTION_OPTIONS = {
7670
+ appMetadata: {},
7648
7671
  connectionMethod: exports.SyncStreamConnectionMethod.WEB_SOCKET,
7649
7672
  clientImplementation: DEFAULT_SYNC_CLIENT_IMPLEMENTATION,
7650
7673
  fetchStrategy: exports.FetchStrategy.Buffered,
@@ -8023,6 +8046,11 @@ The next upload iteration will be delayed.`);
8023
8046
  ...DEFAULT_STREAM_CONNECTION_OPTIONS,
8024
8047
  ...(options ?? {})
8025
8048
  };
8049
+ // Validate app metadata
8050
+ const invalidMetadata = Object.entries(resolvedOptions.appMetadata).filter(([_, value]) => typeof value != 'string');
8051
+ if (invalidMetadata.length > 0) {
8052
+ throw new Error(`Invalid appMetadata provided. Only string values are allowed. Invalid values: ${invalidMetadata.map(([key, value]) => `${key}: ${value}`).join(', ')}`);
8053
+ }
8026
8054
  const clientImplementation = resolvedOptions.clientImplementation;
8027
8055
  this.updateSyncStatus({ clientImplementation });
8028
8056
  if (clientImplementation == exports.SyncClientImplementation.JAVASCRIPT) {
@@ -8058,6 +8086,7 @@ The next upload iteration will be delayed.`);
8058
8086
  include_checksum: true,
8059
8087
  raw_data: true,
8060
8088
  parameters: resolvedOptions.params,
8089
+ app_metadata: resolvedOptions.appMetadata,
8061
8090
  client_id: clientId
8062
8091
  }
8063
8092
  };
@@ -8417,6 +8446,7 @@ The next upload iteration will be delayed.`);
8417
8446
  try {
8418
8447
  const options = {
8419
8448
  parameters: resolvedOptions.params,
8449
+ app_metadata: resolvedOptions.appMetadata,
8420
8450
  active_streams: this.activeStreams,
8421
8451
  include_defaults: resolvedOptions.includeDefaultStreams
8422
8452
  };
@@ -9053,14 +9083,14 @@ class AbstractPowerSyncDatabase extends BaseObserver {
9053
9083
  async initialize() {
9054
9084
  await this._initialize();
9055
9085
  await this.bucketStorageAdapter.init();
9056
- await this._loadVersion();
9086
+ await this.loadVersion();
9057
9087
  await this.updateSchema(this.options.schema);
9058
9088
  await this.resolveOfflineSyncStatus();
9059
9089
  await this.database.execute('PRAGMA RECURSIVE_TRIGGERS=TRUE');
9060
9090
  this.ready = true;
9061
9091
  this.iterateListeners((cb) => cb.initialized?.());
9062
9092
  }
9063
- async _loadVersion() {
9093
+ async loadVersion() {
9064
9094
  try {
9065
9095
  const { version } = await this.database.get('SELECT powersync_rs_version() as version');
9066
9096
  this.sdkVersion = version;
@@ -10199,6 +10229,27 @@ class SyncDataBatch {
10199
10229
  }
10200
10230
  }
10201
10231
 
10232
+ /**
10233
+ * Thrown when an underlying database connection is closed.
10234
+ * This is particularly relevant when worker connections are marked as closed while
10235
+ * operations are still in progress.
10236
+ */
10237
+ class ConnectionClosedError extends Error {
10238
+ static NAME = 'ConnectionClosedError';
10239
+ static MATCHES(input) {
10240
+ /**
10241
+ * If there are weird package issues which cause multiple versions of classes to be present, the instanceof
10242
+ * check might fail. This also performs a failsafe check.
10243
+ * This might also happen if the Error is serialized and parsed over a bridging channel like a MessagePort.
10244
+ */
10245
+ return (input instanceof ConnectionClosedError || (input instanceof Error && input.name == ConnectionClosedError.NAME));
10246
+ }
10247
+ constructor(message) {
10248
+ super(message);
10249
+ this.name = ConnectionClosedError.NAME;
10250
+ }
10251
+ }
10252
+
10202
10253
  // https://www.sqlite.org/lang_expr.html#castexpr
10203
10254
  exports.ColumnType = void 0;
10204
10255
  (function (ColumnType) {
@@ -10762,6 +10813,7 @@ exports.AbstractStreamingSyncImplementation = AbstractStreamingSyncImplementatio
10762
10813
  exports.ArrayComparator = ArrayComparator;
10763
10814
  exports.BaseObserver = BaseObserver;
10764
10815
  exports.Column = Column;
10816
+ exports.ConnectionClosedError = ConnectionClosedError;
10765
10817
  exports.ConnectionManager = ConnectionManager;
10766
10818
  exports.ControlledExecutor = ControlledExecutor;
10767
10819
  exports.CrudBatch = CrudBatch;
@@ -10800,6 +10852,7 @@ exports.MAX_OP_ID = MAX_OP_ID;
10800
10852
  exports.OnChangeQueryProcessor = OnChangeQueryProcessor;
10801
10853
  exports.OpType = OpType;
10802
10854
  exports.OplogEntry = OplogEntry;
10855
+ exports.RawTable = RawTable;
10803
10856
  exports.Schema = Schema;
10804
10857
  exports.SqliteBucketStorage = SqliteBucketStorage;
10805
10858
  exports.SyncDataBatch = SyncDataBatch;