@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
@@ -579,6 +579,10 @@ class SyncStatus {
579
579
  priorityStatusEntries: this.priorityStatusEntries
580
580
  };
581
581
  }
582
+ /**
583
+ * Not all errors are serializable over a MessagePort. E.g. some `DomExceptions` fail to be passed across workers.
584
+ * This explicitly serializes errors in the SyncStatus.
585
+ */
582
586
  serializeError(error) {
583
587
  if (typeof error == 'undefined') {
584
588
  return undefined;
@@ -1943,7 +1947,7 @@ var hasRequiredFrames;
1943
1947
  function requireFrames () {
1944
1948
  if (hasRequiredFrames) return Frames;
1945
1949
  hasRequiredFrames = 1;
1946
- (function (exports) {
1950
+ (function (exports$1) {
1947
1951
  /*
1948
1952
  * Copyright 2021-2022 the original author or authors.
1949
1953
  *
@@ -1959,8 +1963,8 @@ function requireFrames () {
1959
1963
  * See the License for the specific language governing permissions and
1960
1964
  * limitations under the License.
1961
1965
  */
1962
- Object.defineProperty(exports, "__esModule", { value: true });
1963
- exports.Frame = exports.Lengths = exports.Flags = exports.FrameTypes = void 0;
1966
+ Object.defineProperty(exports$1, "__esModule", { value: true });
1967
+ exports$1.Frame = exports$1.Lengths = exports$1.Flags = exports$1.FrameTypes = void 0;
1964
1968
  var FrameTypes;
1965
1969
  (function (FrameTypes) {
1966
1970
  FrameTypes[FrameTypes["RESERVED"] = 0] = "RESERVED";
@@ -1979,7 +1983,7 @@ function requireFrames () {
1979
1983
  FrameTypes[FrameTypes["RESUME"] = 13] = "RESUME";
1980
1984
  FrameTypes[FrameTypes["RESUME_OK"] = 14] = "RESUME_OK";
1981
1985
  FrameTypes[FrameTypes["EXT"] = 63] = "EXT";
1982
- })(FrameTypes = exports.FrameTypes || (exports.FrameTypes = {}));
1986
+ })(FrameTypes = exports$1.FrameTypes || (exports$1.FrameTypes = {}));
1983
1987
  (function (Flags) {
1984
1988
  Flags[Flags["NONE"] = 0] = "NONE";
1985
1989
  Flags[Flags["COMPLETE"] = 64] = "COMPLETE";
@@ -1990,7 +1994,7 @@ function requireFrames () {
1990
1994
  Flags[Flags["NEXT"] = 32] = "NEXT";
1991
1995
  Flags[Flags["RESPOND"] = 128] = "RESPOND";
1992
1996
  Flags[Flags["RESUME_ENABLE"] = 128] = "RESUME_ENABLE";
1993
- })(exports.Flags || (exports.Flags = {}));
1997
+ })(exports$1.Flags || (exports$1.Flags = {}));
1994
1998
  (function (Flags) {
1995
1999
  function hasMetadata(flags) {
1996
2000
  return (flags & Flags.METADATA) === Flags.METADATA;
@@ -2024,13 +2028,13 @@ function requireFrames () {
2024
2028
  return (flags & Flags.RESUME_ENABLE) === Flags.RESUME_ENABLE;
2025
2029
  }
2026
2030
  Flags.hasResume = hasResume;
2027
- })(exports.Flags || (exports.Flags = {}));
2031
+ })(exports$1.Flags || (exports$1.Flags = {}));
2028
2032
  (function (Lengths) {
2029
2033
  Lengths[Lengths["FRAME"] = 3] = "FRAME";
2030
2034
  Lengths[Lengths["HEADER"] = 6] = "HEADER";
2031
2035
  Lengths[Lengths["METADATA"] = 3] = "METADATA";
2032
2036
  Lengths[Lengths["REQUEST"] = 3] = "REQUEST";
2033
- })(exports.Lengths || (exports.Lengths = {}));
2037
+ })(exports$1.Lengths || (exports$1.Lengths = {}));
2034
2038
  (function (Frame) {
2035
2039
  function isConnection(frame) {
2036
2040
  return frame.streamId === 0;
@@ -2041,7 +2045,7 @@ function requireFrames () {
2041
2045
  frame.type <= FrameTypes.REQUEST_CHANNEL);
2042
2046
  }
2043
2047
  Frame.isRequest = isRequest;
2044
- })(exports.Frame || (exports.Frame = {}));
2048
+ })(exports$1.Frame || (exports$1.Frame = {}));
2045
2049
 
2046
2050
  } (Frames));
2047
2051
  return Frames;
@@ -2052,7 +2056,7 @@ var hasRequiredCodecs;
2052
2056
  function requireCodecs () {
2053
2057
  if (hasRequiredCodecs) return Codecs;
2054
2058
  hasRequiredCodecs = 1;
2055
- (function (exports) {
2059
+ (function (exports$1) {
2056
2060
  var __generator = (Codecs && Codecs.__generator) || function (thisArg, body) {
2057
2061
  var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
2058
2062
  return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
@@ -2080,22 +2084,22 @@ function requireCodecs () {
2080
2084
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
2081
2085
  }
2082
2086
  };
2083
- Object.defineProperty(exports, "__esModule", { value: true });
2084
- 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;
2087
+ Object.defineProperty(exports$1, "__esModule", { value: true });
2088
+ 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;
2085
2089
  var Frames_1 = requireFrames();
2086
- exports.FLAGS_MASK = 0x3ff; // low 10 bits
2087
- exports.FRAME_TYPE_OFFFSET = 10; // frame type is offset 10 bytes within the uint16 containing type + flags
2088
- exports.MAX_CODE = 0x7fffffff; // uint31
2089
- exports.MAX_KEEPALIVE = 0x7fffffff; // uint31
2090
- exports.MAX_LIFETIME = 0x7fffffff; // uint31
2091
- exports.MAX_METADATA_LENGTH = 0xffffff; // uint24
2092
- exports.MAX_MIME_LENGTH = 0xff; // int8
2093
- exports.MAX_REQUEST_COUNT = 0x7fffffff; // uint31
2094
- exports.MAX_REQUEST_N = 0x7fffffff; // uint31
2095
- exports.MAX_RESUME_LENGTH = 0xffff; // uint16
2096
- exports.MAX_STREAM_ID = 0x7fffffff; // uint31
2097
- exports.MAX_TTL = 0x7fffffff; // uint31
2098
- exports.MAX_VERSION = 0xffff; // uint16
2090
+ exports$1.FLAGS_MASK = 0x3ff; // low 10 bits
2091
+ exports$1.FRAME_TYPE_OFFFSET = 10; // frame type is offset 10 bytes within the uint16 containing type + flags
2092
+ exports$1.MAX_CODE = 0x7fffffff; // uint31
2093
+ exports$1.MAX_KEEPALIVE = 0x7fffffff; // uint31
2094
+ exports$1.MAX_LIFETIME = 0x7fffffff; // uint31
2095
+ exports$1.MAX_METADATA_LENGTH = 0xffffff; // uint24
2096
+ exports$1.MAX_MIME_LENGTH = 0xff; // int8
2097
+ exports$1.MAX_REQUEST_COUNT = 0x7fffffff; // uint31
2098
+ exports$1.MAX_REQUEST_N = 0x7fffffff; // uint31
2099
+ exports$1.MAX_RESUME_LENGTH = 0xffff; // uint16
2100
+ exports$1.MAX_STREAM_ID = 0x7fffffff; // uint31
2101
+ exports$1.MAX_TTL = 0x7fffffff; // uint31
2102
+ exports$1.MAX_VERSION = 0xffff; // uint16
2099
2103
  /**
2100
2104
  * Mimimum value that would overflow bitwise operators (2^32).
2101
2105
  */
@@ -2109,7 +2113,7 @@ function requireCodecs () {
2109
2113
  var val3 = buffer.readUInt8(offset + 2);
2110
2114
  return val1 | val2 | val3;
2111
2115
  }
2112
- exports.readUInt24BE = readUInt24BE;
2116
+ exports$1.readUInt24BE = readUInt24BE;
2113
2117
  /**
2114
2118
  * Writes a uint24 to a buffer starting at the given offset, returning the
2115
2119
  * offset of the next byte.
@@ -2119,7 +2123,7 @@ function requireCodecs () {
2119
2123
  offset = buffer.writeUInt8((value >>> 8) & 0xff, offset); // 2nd byte
2120
2124
  return buffer.writeUInt8(value & 0xff, offset); // 1st byte
2121
2125
  }
2122
- exports.writeUInt24BE = writeUInt24BE;
2126
+ exports$1.writeUInt24BE = writeUInt24BE;
2123
2127
  /**
2124
2128
  * Read a uint64 (technically supports up to 53 bits per JS number
2125
2129
  * representation).
@@ -2129,7 +2133,7 @@ function requireCodecs () {
2129
2133
  var low = buffer.readUInt32BE(offset + 4);
2130
2134
  return high * BITWISE_OVERFLOW + low;
2131
2135
  }
2132
- exports.readUInt64BE = readUInt64BE;
2136
+ exports$1.readUInt64BE = readUInt64BE;
2133
2137
  /**
2134
2138
  * Write a uint64 (technically supports up to 53 bits per JS number
2135
2139
  * representation).
@@ -2140,7 +2144,7 @@ function requireCodecs () {
2140
2144
  offset = buffer.writeUInt32BE(high, offset); // first half of uint64
2141
2145
  return buffer.writeUInt32BE(low, offset); // second half of uint64
2142
2146
  }
2143
- exports.writeUInt64BE = writeUInt64BE;
2147
+ exports$1.writeUInt64BE = writeUInt64BE;
2144
2148
  /**
2145
2149
  * Frame header is:
2146
2150
  * - stream id (uint32 = 4)
@@ -2158,7 +2162,7 @@ function requireCodecs () {
2158
2162
  var frameLength = readUInt24BE(buffer, 0);
2159
2163
  return deserializeFrame(buffer.slice(UINT24_SIZE, UINT24_SIZE + frameLength));
2160
2164
  }
2161
- exports.deserializeFrameWithLength = deserializeFrameWithLength;
2165
+ exports$1.deserializeFrameWithLength = deserializeFrameWithLength;
2162
2166
  /**
2163
2167
  * Given a buffer that may contain zero or more length-prefixed frames followed
2164
2168
  * by zero or more bytes of a (partial) subsequent frame, returns an array of
@@ -2191,7 +2195,7 @@ function requireCodecs () {
2191
2195
  }
2192
2196
  });
2193
2197
  }
2194
- exports.deserializeFrames = deserializeFrames;
2198
+ exports$1.deserializeFrames = deserializeFrames;
2195
2199
  /**
2196
2200
  * Writes a frame to a buffer with a length prefix.
2197
2201
  */
@@ -2202,7 +2206,7 @@ function requireCodecs () {
2202
2206
  buffer.copy(lengthPrefixed, UINT24_SIZE);
2203
2207
  return lengthPrefixed;
2204
2208
  }
2205
- exports.serializeFrameWithLength = serializeFrameWithLength;
2209
+ exports$1.serializeFrameWithLength = serializeFrameWithLength;
2206
2210
  /**
2207
2211
  * Read a frame from the buffer.
2208
2212
  */
@@ -2217,8 +2221,8 @@ function requireCodecs () {
2217
2221
  // );
2218
2222
  var typeAndFlags = buffer.readUInt16BE(offset);
2219
2223
  offset += 2;
2220
- var type = typeAndFlags >>> exports.FRAME_TYPE_OFFFSET; // keep highest 6 bits
2221
- var flags = typeAndFlags & exports.FLAGS_MASK; // keep lowest 10 bits
2224
+ var type = typeAndFlags >>> exports$1.FRAME_TYPE_OFFFSET; // keep highest 6 bits
2225
+ var flags = typeAndFlags & exports$1.FLAGS_MASK; // keep lowest 10 bits
2222
2226
  switch (type) {
2223
2227
  case Frames_1.FrameTypes.SETUP:
2224
2228
  return deserializeSetupFrame(buffer, streamId, flags);
@@ -2255,7 +2259,7 @@ function requireCodecs () {
2255
2259
  // );
2256
2260
  }
2257
2261
  }
2258
- exports.deserializeFrame = deserializeFrame;
2262
+ exports$1.deserializeFrame = deserializeFrame;
2259
2263
  /**
2260
2264
  * Convert the frame to a (binary) buffer.
2261
2265
  */
@@ -2294,7 +2298,7 @@ function requireCodecs () {
2294
2298
  // );
2295
2299
  }
2296
2300
  }
2297
- exports.serializeFrame = serializeFrame;
2301
+ exports$1.serializeFrame = serializeFrame;
2298
2302
  /**
2299
2303
  * Byte size of frame without size prefix
2300
2304
  */
@@ -2333,7 +2337,7 @@ function requireCodecs () {
2333
2337
  // );
2334
2338
  }
2335
2339
  }
2336
- exports.sizeOfFrame = sizeOfFrame;
2340
+ exports$1.sizeOfFrame = sizeOfFrame;
2337
2341
  /**
2338
2342
  * Writes a SETUP frame into a new buffer and returns it.
2339
2343
  *
@@ -2968,7 +2972,7 @@ function requireCodecs () {
2968
2972
  function writeHeader(frame, buffer) {
2969
2973
  var offset = buffer.writeInt32BE(frame.streamId, 0);
2970
2974
  // shift frame to high 6 bits, extract lowest 10 bits from flags
2971
- return buffer.writeUInt16BE((frame.type << exports.FRAME_TYPE_OFFFSET) | (frame.flags & exports.FLAGS_MASK), offset);
2975
+ return buffer.writeUInt16BE((frame.type << exports$1.FRAME_TYPE_OFFFSET) | (frame.flags & exports$1.FLAGS_MASK), offset);
2972
2976
  }
2973
2977
  /**
2974
2978
  * Determine the length of the payload section of a frame. Only applies to
@@ -3049,7 +3053,7 @@ function requireCodecs () {
3049
3053
  };
3050
3054
  return Deserializer;
3051
3055
  }());
3052
- exports.Deserializer = Deserializer;
3056
+ exports$1.Deserializer = Deserializer;
3053
3057
 
3054
3058
  } (Codecs));
3055
3059
  return Codecs;
@@ -3195,7 +3199,7 @@ var hasRequiredErrors;
3195
3199
  function requireErrors () {
3196
3200
  if (hasRequiredErrors) return Errors;
3197
3201
  hasRequiredErrors = 1;
3198
- (function (exports) {
3202
+ (function (exports$1) {
3199
3203
  /*
3200
3204
  * Copyright 2021-2022 the original author or authors.
3201
3205
  *
@@ -3226,8 +3230,8 @@ function requireErrors () {
3226
3230
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
3227
3231
  };
3228
3232
  })();
3229
- Object.defineProperty(exports, "__esModule", { value: true });
3230
- exports.ErrorCodes = exports.RSocketError = void 0;
3233
+ Object.defineProperty(exports$1, "__esModule", { value: true });
3234
+ exports$1.ErrorCodes = exports$1.RSocketError = void 0;
3231
3235
  var RSocketError = /** @class */ (function (_super) {
3232
3236
  __extends(RSocketError, _super);
3233
3237
  function RSocketError(code, message) {
@@ -3237,7 +3241,7 @@ function requireErrors () {
3237
3241
  }
3238
3242
  return RSocketError;
3239
3243
  }(Error));
3240
- exports.RSocketError = RSocketError;
3244
+ exports$1.RSocketError = RSocketError;
3241
3245
  (function (ErrorCodes) {
3242
3246
  ErrorCodes[ErrorCodes["RESERVED"] = 0] = "RESERVED";
3243
3247
  ErrorCodes[ErrorCodes["INVALID_SETUP"] = 1] = "INVALID_SETUP";
@@ -3251,7 +3255,7 @@ function requireErrors () {
3251
3255
  ErrorCodes[ErrorCodes["CANCELED"] = 515] = "CANCELED";
3252
3256
  ErrorCodes[ErrorCodes["INVALID"] = 516] = "INVALID";
3253
3257
  ErrorCodes[ErrorCodes["RESERVED_EXTENSION"] = 4294967295] = "RESERVED_EXTENSION";
3254
- })(exports.ErrorCodes || (exports.ErrorCodes = {}));
3258
+ })(exports$1.ErrorCodes || (exports$1.ErrorCodes = {}));
3255
3259
 
3256
3260
  } (Errors));
3257
3261
  return Errors;
@@ -3293,7 +3297,7 @@ var hasRequiredClientServerMultiplexerDemultiplexer;
3293
3297
  function requireClientServerMultiplexerDemultiplexer () {
3294
3298
  if (hasRequiredClientServerMultiplexerDemultiplexer) return ClientServerMultiplexerDemultiplexer;
3295
3299
  hasRequiredClientServerMultiplexerDemultiplexer = 1;
3296
- (function (exports) {
3300
+ (function (exports$1) {
3297
3301
  /*
3298
3302
  * Copyright 2021-2022 the original author or authors.
3299
3303
  *
@@ -3360,8 +3364,8 @@ function requireClientServerMultiplexerDemultiplexer () {
3360
3364
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
3361
3365
  }
3362
3366
  };
3363
- Object.defineProperty(exports, "__esModule", { value: true });
3364
- exports.ResumeOkAwaitingResumableClientServerInputMultiplexerDemultiplexer = exports.ResumableClientServerInputMultiplexerDemultiplexer = exports.ClientServerInputMultiplexerDemultiplexer = exports.StreamIdGenerator = void 0;
3367
+ Object.defineProperty(exports$1, "__esModule", { value: true });
3368
+ exports$1.ResumeOkAwaitingResumableClientServerInputMultiplexerDemultiplexer = exports$1.ResumableClientServerInputMultiplexerDemultiplexer = exports$1.ClientServerInputMultiplexerDemultiplexer = exports$1.StreamIdGenerator = void 0;
3365
3369
  var _1 = requireDist();
3366
3370
  var Deferred_1 = requireDeferred();
3367
3371
  var Errors_1 = requireErrors();
@@ -3384,7 +3388,7 @@ function requireClientServerMultiplexerDemultiplexer () {
3384
3388
  };
3385
3389
  return StreamIdGeneratorImpl;
3386
3390
  }());
3387
- })(exports.StreamIdGenerator || (exports.StreamIdGenerator = {}));
3391
+ })(exports$1.StreamIdGenerator || (exports$1.StreamIdGenerator = {}));
3388
3392
  var ClientServerInputMultiplexerDemultiplexer = /** @class */ (function (_super) {
3389
3393
  __extends(ClientServerInputMultiplexerDemultiplexer, _super);
3390
3394
  function ClientServerInputMultiplexerDemultiplexer(streamIdSupplier, outbound, closeable) {
@@ -3473,7 +3477,7 @@ function requireClientServerMultiplexerDemultiplexer () {
3473
3477
  };
3474
3478
  return ClientServerInputMultiplexerDemultiplexer;
3475
3479
  }(Deferred_1.Deferred));
3476
- exports.ClientServerInputMultiplexerDemultiplexer = ClientServerInputMultiplexerDemultiplexer;
3480
+ exports$1.ClientServerInputMultiplexerDemultiplexer = ClientServerInputMultiplexerDemultiplexer;
3477
3481
  var ResumableClientServerInputMultiplexerDemultiplexer = /** @class */ (function (_super) {
3478
3482
  __extends(ResumableClientServerInputMultiplexerDemultiplexer, _super);
3479
3483
  function ResumableClientServerInputMultiplexerDemultiplexer(streamIdSupplier, outbound, closeable, frameStore, token, sessionStoreOrReconnector, sessionTimeout) {
@@ -3630,7 +3634,7 @@ function requireClientServerMultiplexerDemultiplexer () {
3630
3634
  };
3631
3635
  return ResumableClientServerInputMultiplexerDemultiplexer;
3632
3636
  }(ClientServerInputMultiplexerDemultiplexer));
3633
- exports.ResumableClientServerInputMultiplexerDemultiplexer = ResumableClientServerInputMultiplexerDemultiplexer;
3637
+ exports$1.ResumableClientServerInputMultiplexerDemultiplexer = ResumableClientServerInputMultiplexerDemultiplexer;
3634
3638
  var ResumeOkAwaitingResumableClientServerInputMultiplexerDemultiplexer = /** @class */ (function () {
3635
3639
  function ResumeOkAwaitingResumableClientServerInputMultiplexerDemultiplexer(outbound, closeable, delegate) {
3636
3640
  this.outbound = outbound;
@@ -3687,7 +3691,7 @@ function requireClientServerMultiplexerDemultiplexer () {
3687
3691
  };
3688
3692
  return ResumeOkAwaitingResumableClientServerInputMultiplexerDemultiplexer;
3689
3693
  }());
3690
- exports.ResumeOkAwaitingResumableClientServerInputMultiplexerDemultiplexer = ResumeOkAwaitingResumableClientServerInputMultiplexerDemultiplexer;
3694
+ exports$1.ResumeOkAwaitingResumableClientServerInputMultiplexerDemultiplexer = ResumeOkAwaitingResumableClientServerInputMultiplexerDemultiplexer;
3691
3695
 
3692
3696
  } (ClientServerMultiplexerDemultiplexer));
3693
3697
  return ClientServerMultiplexerDemultiplexer;
@@ -6673,7 +6677,7 @@ var hasRequiredDist;
6673
6677
  function requireDist () {
6674
6678
  if (hasRequiredDist) return dist;
6675
6679
  hasRequiredDist = 1;
6676
- (function (exports) {
6680
+ (function (exports$1) {
6677
6681
  /*
6678
6682
  * Copyright 2021-2022 the original author or authors.
6679
6683
  *
@@ -6696,19 +6700,19 @@ function requireDist () {
6696
6700
  if (k2 === undefined) k2 = k;
6697
6701
  o[k2] = m[k];
6698
6702
  }));
6699
- var __exportStar = (dist && dist.__exportStar) || function(m, exports) {
6700
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
6703
+ var __exportStar = (dist && dist.__exportStar) || function(m, exports$1) {
6704
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports$1, p)) __createBinding(exports$1, m, p);
6701
6705
  };
6702
- Object.defineProperty(exports, "__esModule", { value: true });
6703
- __exportStar(requireCodecs(), exports);
6704
- __exportStar(requireCommon(), exports);
6705
- __exportStar(requireDeferred(), exports);
6706
- __exportStar(requireErrors(), exports);
6707
- __exportStar(requireFrames(), exports);
6708
- __exportStar(requireRSocket(), exports);
6709
- __exportStar(requireRSocketConnector(), exports);
6710
- __exportStar(requireRSocketServer(), exports);
6711
- __exportStar(requireTransport(), exports);
6706
+ Object.defineProperty(exports$1, "__esModule", { value: true });
6707
+ __exportStar(requireCodecs(), exports$1);
6708
+ __exportStar(requireCommon(), exports$1);
6709
+ __exportStar(requireDeferred(), exports$1);
6710
+ __exportStar(requireErrors(), exports$1);
6711
+ __exportStar(requireFrames(), exports$1);
6712
+ __exportStar(requireRSocket(), exports$1);
6713
+ __exportStar(requireRSocketConnector(), exports$1);
6714
+ __exportStar(requireRSocketServer(), exports$1);
6715
+ __exportStar(requireTransport(), exports$1);
6712
6716
 
6713
6717
  } (dist));
6714
6718
  return dist;
@@ -6716,7 +6720,7 @@ function requireDist () {
6716
6720
 
6717
6721
  var distExports = requireDist();
6718
6722
 
6719
- var version = "1.43.1";
6723
+ var version = "1.45.0";
6720
6724
  var PACKAGE = {
6721
6725
  version: version};
6722
6726
 
@@ -6787,6 +6791,16 @@ class DataStream extends BaseObserver {
6787
6791
  * @returns a Data payload or Null if the stream closed.
6788
6792
  */
6789
6793
  async read() {
6794
+ if (this.closed) {
6795
+ return null;
6796
+ }
6797
+ // Wait for any pending processing to complete first.
6798
+ // This ensures we register our listener before calling processQueue(),
6799
+ // avoiding a race where processQueue() sees no reader and returns early.
6800
+ if (this.processingPromise) {
6801
+ await this.processingPromise;
6802
+ }
6803
+ // Re-check after await - stream may have closed while we were waiting
6790
6804
  if (this.closed) {
6791
6805
  return null;
6792
6806
  }
@@ -6826,7 +6840,7 @@ class DataStream extends BaseObserver {
6826
6840
  }
6827
6841
  const promise = (this.processingPromise = this._processQueue());
6828
6842
  promise.finally(() => {
6829
- return (this.processingPromise = null);
6843
+ this.processingPromise = null;
6830
6844
  });
6831
6845
  return promise;
6832
6846
  }
@@ -6858,7 +6872,6 @@ class DataStream extends BaseObserver {
6858
6872
  this.notifyDataAdded = null;
6859
6873
  }
6860
6874
  if (this.dataQueue.length > 0) {
6861
- // Next tick
6862
6875
  setTimeout(() => this.processQueue());
6863
6876
  }
6864
6877
  }
@@ -7478,7 +7491,11 @@ class AbstractRemote {
7478
7491
  };
7479
7492
  const stream = new DataStream({
7480
7493
  logger: this.logger,
7481
- mapLine: mapLine
7494
+ mapLine: mapLine,
7495
+ pressure: {
7496
+ highWaterMark: 20,
7497
+ lowWaterMark: 10
7498
+ }
7482
7499
  });
7483
7500
  abortSignal?.addEventListener('abort', () => {
7484
7501
  closeReader();
@@ -7486,42 +7503,47 @@ class AbstractRemote {
7486
7503
  });
7487
7504
  const decoder = this.createTextDecoder();
7488
7505
  let buffer = '';
7489
- const l = stream.registerListener({
7490
- lowWater: async () => {
7491
- if (stream.closed || abortSignal?.aborted || readerReleased) {
7506
+ const consumeStream = async () => {
7507
+ while (!stream.closed && !abortSignal?.aborted && !readerReleased) {
7508
+ const { done, value } = await reader.read();
7509
+ if (done) {
7510
+ const remaining = buffer.trim();
7511
+ if (remaining.length != 0) {
7512
+ stream.enqueueData(remaining);
7513
+ }
7514
+ stream.close();
7515
+ await closeReader();
7492
7516
  return;
7493
7517
  }
7494
- try {
7495
- let didCompleteLine = false;
7496
- while (!didCompleteLine) {
7497
- const { done, value } = await reader.read();
7498
- if (done) {
7499
- const remaining = buffer.trim();
7500
- if (remaining.length != 0) {
7501
- stream.enqueueData(remaining);
7502
- }
7503
- stream.close();
7504
- await closeReader();
7505
- return;
7506
- }
7507
- const data = decoder.decode(value, { stream: true });
7508
- buffer += data;
7509
- const lines = buffer.split('\n');
7510
- for (var i = 0; i < lines.length - 1; i++) {
7511
- var l = lines[i].trim();
7512
- if (l.length > 0) {
7513
- stream.enqueueData(l);
7514
- didCompleteLine = true;
7515
- }
7516
- }
7517
- buffer = lines[lines.length - 1];
7518
+ const data = decoder.decode(value, { stream: true });
7519
+ buffer += data;
7520
+ const lines = buffer.split('\n');
7521
+ for (var i = 0; i < lines.length - 1; i++) {
7522
+ var l = lines[i].trim();
7523
+ if (l.length > 0) {
7524
+ stream.enqueueData(l);
7518
7525
  }
7519
7526
  }
7520
- catch (ex) {
7521
- stream.close();
7522
- throw ex;
7527
+ buffer = lines[lines.length - 1];
7528
+ // Implement backpressure by waiting for the low water mark to be reached
7529
+ if (stream.dataQueue.length > stream.highWatermark) {
7530
+ await new Promise((resolve) => {
7531
+ const dispose = stream.registerListener({
7532
+ lowWater: async () => {
7533
+ resolve();
7534
+ dispose();
7535
+ },
7536
+ closed: () => {
7537
+ resolve();
7538
+ dispose();
7539
+ }
7540
+ });
7541
+ });
7523
7542
  }
7524
- },
7543
+ }
7544
+ };
7545
+ consumeStream().catch(ex => this.logger.error('Error consuming stream', ex));
7546
+ const l = stream.registerListener({
7525
7547
  closed: () => {
7526
7548
  closeReader();
7527
7549
  l?.();
@@ -7643,6 +7665,7 @@ const DEFAULT_STREAMING_SYNC_OPTIONS = {
7643
7665
  crudUploadThrottleMs: DEFAULT_CRUD_UPLOAD_THROTTLE_MS
7644
7666
  };
7645
7667
  const DEFAULT_STREAM_CONNECTION_OPTIONS = {
7668
+ appMetadata: {},
7646
7669
  connectionMethod: SyncStreamConnectionMethod.WEB_SOCKET,
7647
7670
  clientImplementation: DEFAULT_SYNC_CLIENT_IMPLEMENTATION,
7648
7671
  fetchStrategy: FetchStrategy.Buffered,
@@ -8021,6 +8044,11 @@ The next upload iteration will be delayed.`);
8021
8044
  ...DEFAULT_STREAM_CONNECTION_OPTIONS,
8022
8045
  ...(options ?? {})
8023
8046
  };
8047
+ // Validate app metadata
8048
+ const invalidMetadata = Object.entries(resolvedOptions.appMetadata).filter(([_, value]) => typeof value != 'string');
8049
+ if (invalidMetadata.length > 0) {
8050
+ throw new Error(`Invalid appMetadata provided. Only string values are allowed. Invalid values: ${invalidMetadata.map(([key, value]) => `${key}: ${value}`).join(', ')}`);
8051
+ }
8024
8052
  const clientImplementation = resolvedOptions.clientImplementation;
8025
8053
  this.updateSyncStatus({ clientImplementation });
8026
8054
  if (clientImplementation == SyncClientImplementation.JAVASCRIPT) {
@@ -8056,6 +8084,7 @@ The next upload iteration will be delayed.`);
8056
8084
  include_checksum: true,
8057
8085
  raw_data: true,
8058
8086
  parameters: resolvedOptions.params,
8087
+ app_metadata: resolvedOptions.appMetadata,
8059
8088
  client_id: clientId
8060
8089
  }
8061
8090
  };
@@ -8415,6 +8444,7 @@ The next upload iteration will be delayed.`);
8415
8444
  try {
8416
8445
  const options = {
8417
8446
  parameters: resolvedOptions.params,
8447
+ app_metadata: resolvedOptions.appMetadata,
8418
8448
  active_streams: this.activeStreams,
8419
8449
  include_defaults: resolvedOptions.includeDefaultStreams
8420
8450
  };
@@ -9051,14 +9081,14 @@ class AbstractPowerSyncDatabase extends BaseObserver {
9051
9081
  async initialize() {
9052
9082
  await this._initialize();
9053
9083
  await this.bucketStorageAdapter.init();
9054
- await this._loadVersion();
9084
+ await this.loadVersion();
9055
9085
  await this.updateSchema(this.options.schema);
9056
9086
  await this.resolveOfflineSyncStatus();
9057
9087
  await this.database.execute('PRAGMA RECURSIVE_TRIGGERS=TRUE');
9058
9088
  this.ready = true;
9059
9089
  this.iterateListeners((cb) => cb.initialized?.());
9060
9090
  }
9061
- async _loadVersion() {
9091
+ async loadVersion() {
9062
9092
  try {
9063
9093
  const { version } = await this.database.get('SELECT powersync_rs_version() as version');
9064
9094
  this.sdkVersion = version;
@@ -10197,6 +10227,27 @@ class SyncDataBatch {
10197
10227
  }
10198
10228
  }
10199
10229
 
10230
+ /**
10231
+ * Thrown when an underlying database connection is closed.
10232
+ * This is particularly relevant when worker connections are marked as closed while
10233
+ * operations are still in progress.
10234
+ */
10235
+ class ConnectionClosedError extends Error {
10236
+ static NAME = 'ConnectionClosedError';
10237
+ static MATCHES(input) {
10238
+ /**
10239
+ * If there are weird package issues which cause multiple versions of classes to be present, the instanceof
10240
+ * check might fail. This also performs a failsafe check.
10241
+ * This might also happen if the Error is serialized and parsed over a bridging channel like a MessagePort.
10242
+ */
10243
+ return (input instanceof ConnectionClosedError || (input instanceof Error && input.name == ConnectionClosedError.NAME));
10244
+ }
10245
+ constructor(message) {
10246
+ super(message);
10247
+ this.name = ConnectionClosedError.NAME;
10248
+ }
10249
+ }
10250
+
10200
10251
  // https://www.sqlite.org/lang_expr.html#castexpr
10201
10252
  var ColumnType;
10202
10253
  (function (ColumnType) {
@@ -10751,5 +10802,5 @@ const parseQuery = (query, parameters) => {
10751
10802
  return { sqlStatement, parameters: parameters };
10752
10803
  };
10753
10804
 
10754
- export { AbortOperation, AbstractPowerSyncDatabase, AbstractPowerSyncDatabaseOpenFactory, AbstractQueryProcessor, AbstractRemote, AbstractStreamingSyncImplementation, ArrayComparator, BaseObserver, Column, ColumnType, ConnectionManager, ControlledExecutor, CrudBatch, CrudEntry, CrudTransaction, DEFAULT_CRUD_BATCH_LIMIT, DEFAULT_CRUD_UPLOAD_THROTTLE_MS, DEFAULT_INDEX_COLUMN_OPTIONS, DEFAULT_INDEX_OPTIONS, DEFAULT_LOCK_TIMEOUT_MS, DEFAULT_POWERSYNC_CLOSE_OPTIONS, DEFAULT_POWERSYNC_DB_OPTIONS, DEFAULT_PRESSURE_LIMITS, DEFAULT_REMOTE_LOGGER, DEFAULT_REMOTE_OPTIONS, DEFAULT_RETRY_DELAY_MS, DEFAULT_ROW_COMPARATOR, DEFAULT_STREAMING_SYNC_OPTIONS, DEFAULT_STREAM_CONNECTION_OPTIONS, DEFAULT_SYNC_CLIENT_IMPLEMENTATION, DEFAULT_TABLE_OPTIONS, DEFAULT_WATCH_QUERY_OPTIONS, DEFAULT_WATCH_THROTTLE_MS, DataStream, DiffTriggerOperation, DifferentialQueryProcessor, EMPTY_DIFFERENTIAL, FalsyComparator, FetchImplementationProvider, FetchStrategy, GetAllQuery, Index, IndexedColumn, InvalidSQLCharacters, LockType, LogLevel, MAX_AMOUNT_OF_COLUMNS, MAX_OP_ID, OnChangeQueryProcessor, OpType, OpTypeEnum, OplogEntry, PSInternalTable, PowerSyncControlCommand, RowUpdateType, Schema, SqliteBucketStorage, SyncClientImplementation, SyncDataBatch, SyncDataBucket, SyncProgress, SyncStatus, SyncStreamConnectionMethod, Table, TableV2, UpdateType, UploadQueueStats, WatchedQueryListenerEvent, column, compilableQueryWatch, createBaseLogger, createLogger, extractTableUpdates, isBatchedUpdateNotification, isContinueCheckpointRequest, isDBAdapter, isPowerSyncDatabaseOptionsWithSettings, isSQLOpenFactory, isSQLOpenOptions, isStreamingKeepalive, isStreamingSyncCheckpoint, isStreamingSyncCheckpointComplete, isStreamingSyncCheckpointDiff, isStreamingSyncCheckpointPartiallyComplete, isStreamingSyncData, isSyncNewCheckpointRequest, parseQuery, runOnSchemaChange, sanitizeSQL, sanitizeUUID };
10805
+ export { AbortOperation, AbstractPowerSyncDatabase, AbstractPowerSyncDatabaseOpenFactory, AbstractQueryProcessor, AbstractRemote, AbstractStreamingSyncImplementation, ArrayComparator, BaseObserver, Column, ColumnType, ConnectionClosedError, ConnectionManager, ControlledExecutor, CrudBatch, CrudEntry, CrudTransaction, DEFAULT_CRUD_BATCH_LIMIT, DEFAULT_CRUD_UPLOAD_THROTTLE_MS, DEFAULT_INDEX_COLUMN_OPTIONS, DEFAULT_INDEX_OPTIONS, DEFAULT_LOCK_TIMEOUT_MS, DEFAULT_POWERSYNC_CLOSE_OPTIONS, DEFAULT_POWERSYNC_DB_OPTIONS, DEFAULT_PRESSURE_LIMITS, DEFAULT_REMOTE_LOGGER, DEFAULT_REMOTE_OPTIONS, DEFAULT_RETRY_DELAY_MS, DEFAULT_ROW_COMPARATOR, DEFAULT_STREAMING_SYNC_OPTIONS, DEFAULT_STREAM_CONNECTION_OPTIONS, DEFAULT_SYNC_CLIENT_IMPLEMENTATION, DEFAULT_TABLE_OPTIONS, DEFAULT_WATCH_QUERY_OPTIONS, DEFAULT_WATCH_THROTTLE_MS, DataStream, DiffTriggerOperation, DifferentialQueryProcessor, EMPTY_DIFFERENTIAL, FalsyComparator, FetchImplementationProvider, FetchStrategy, GetAllQuery, Index, IndexedColumn, InvalidSQLCharacters, LockType, LogLevel, MAX_AMOUNT_OF_COLUMNS, MAX_OP_ID, OnChangeQueryProcessor, OpType, OpTypeEnum, OplogEntry, PSInternalTable, PowerSyncControlCommand, RawTable, RowUpdateType, Schema, SqliteBucketStorage, SyncClientImplementation, SyncDataBatch, SyncDataBucket, SyncProgress, SyncStatus, SyncStreamConnectionMethod, Table, TableV2, UpdateType, UploadQueueStats, WatchedQueryListenerEvent, column, compilableQueryWatch, createBaseLogger, createLogger, extractTableUpdates, isBatchedUpdateNotification, isContinueCheckpointRequest, isDBAdapter, isPowerSyncDatabaseOptionsWithSettings, isSQLOpenFactory, isSQLOpenOptions, isStreamingKeepalive, isStreamingSyncCheckpoint, isStreamingSyncCheckpointComplete, isStreamingSyncCheckpointDiff, isStreamingSyncCheckpointPartiallyComplete, isStreamingSyncData, isSyncNewCheckpointRequest, parseQuery, runOnSchemaChange, sanitizeSQL, sanitizeUUID };
10755
10806
  //# sourceMappingURL=bundle.node.mjs.map