@lark-sh/client 0.1.18 → 0.1.20

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.
@@ -1536,17 +1536,14 @@ var SubscriptionManager = class {
1536
1536
  queryParamsChanged = view.updateQueryParams(queryParams);
1537
1537
  tag = this.viewKeyToTag.get(viewKey);
1538
1538
  }
1539
- const existingEventTypes = view.getEventTypes();
1540
- const isNewEventType = !existingEventTypes.includes(eventType);
1541
1539
  const unsubscribe = view.addCallback(eventType, callback);
1542
1540
  const wrappedUnsubscribe = () => {
1543
1541
  this.unsubscribeCallback(normalizedPath, eventType, callback, queryId);
1544
1542
  };
1545
- if (isNewView || isNewEventType || queryParamsChanged) {
1543
+ if (isNewView || queryParamsChanged) {
1546
1544
  const hasAncestorComplete = this.hasAncestorCompleteView(normalizedPath);
1547
1545
  if (!hasAncestorComplete) {
1548
- const allEventTypes = view.getEventTypes();
1549
- this.sendSubscribe?.(normalizedPath, allEventTypes, view.queryParams ?? void 0, tag).catch((err) => {
1546
+ this.sendSubscribe?.(normalizedPath, view.queryParams ?? void 0, tag).catch((err) => {
1550
1547
  console.error("Failed to subscribe:", err);
1551
1548
  });
1552
1549
  }
@@ -1638,11 +1635,6 @@ var SubscriptionManager = class {
1638
1635
  this.sendUnsubscribe?.(normalizedPath, queryParams).catch((err) => {
1639
1636
  console.error("Failed to unsubscribe:", err);
1640
1637
  });
1641
- } else {
1642
- const remainingEventTypes = view.getEventTypes();
1643
- this.sendSubscribe?.(normalizedPath, remainingEventTypes, queryParams).catch((err) => {
1644
- console.error("Failed to update subscription:", err);
1645
- });
1646
1638
  }
1647
1639
  }
1648
1640
  if (queryIds.size === 0) {
@@ -2070,10 +2062,9 @@ var SubscriptionManager = class {
2070
2062
  */
2071
2063
  async resubscribeAll() {
2072
2064
  for (const view of this.views.values()) {
2073
- const eventTypes = view.getEventTypes();
2074
- if (eventTypes.length > 0) {
2065
+ if (view.hasCallbacks()) {
2075
2066
  try {
2076
- await this.sendSubscribe?.(view.path, eventTypes, view.queryParams ?? void 0);
2067
+ await this.sendSubscribe?.(view.path, view.queryParams ?? void 0);
2077
2068
  } catch (err) {
2078
2069
  console.error(`Failed to resubscribe to ${view.path}:`, err);
2079
2070
  }
@@ -2829,6 +2820,8 @@ function generatePushId() {
2829
2820
  // src/utils/validation.ts
2830
2821
  var MAX_KEY_BYTES = 768;
2831
2822
  var MAX_STRING_VALUE_BYTES = 10 * 1024 * 1024;
2823
+ var MAX_WRITE_BYTES = 16 * 1024 * 1024;
2824
+ var MAX_VOLATILE_WRITE_BYTES = 2 * 1024;
2832
2825
  var INVALID_KEY_CHARS = /[.#$\[\]\/]/;
2833
2826
  var CONTROL_CHARS = /[\x00-\x1f\x7f]/;
2834
2827
  function hasControlChars(str) {
@@ -2946,6 +2939,30 @@ function validateValue(value, context, path = "") {
2946
2939
  function validateWriteData(value, context) {
2947
2940
  validateValue(value, context);
2948
2941
  }
2942
+ function getJsonByteSize(value) {
2943
+ const json = JSON.stringify(value);
2944
+ return getByteLength(json);
2945
+ }
2946
+ function validateWriteSize(value, context) {
2947
+ const byteSize = getJsonByteSize(value);
2948
+ if (byteSize > MAX_WRITE_BYTES) {
2949
+ const sizeMB = Math.round(byteSize / 1024 / 1024 * 100) / 100;
2950
+ throw new LarkError(
2951
+ ErrorCode.INVALID_DATA,
2952
+ `${context ? `${context}: ` : ""}write exceeds 16 MB limit (got ${sizeMB} MB)`
2953
+ );
2954
+ }
2955
+ }
2956
+ function validateVolatileWriteSize(value, context) {
2957
+ const byteSize = getJsonByteSize(value);
2958
+ if (byteSize > MAX_VOLATILE_WRITE_BYTES) {
2959
+ const sizeKB = Math.round(byteSize / 1024 * 100) / 100;
2960
+ throw new LarkError(
2961
+ ErrorCode.INVALID_DATA,
2962
+ `${context ? `${context}: ` : ""}volatile write exceeds 2 KB limit (got ${sizeKB} KB)`
2963
+ );
2964
+ }
2965
+ }
2949
2966
 
2950
2967
  // src/DatabaseReference.ts
2951
2968
  function isInfoPath(path) {
@@ -4217,21 +4234,33 @@ var ServerValue = {
4217
4234
  increment: (delta) => ({ ".sv": { increment: delta } })
4218
4235
  };
4219
4236
  var LarkDatabase = class {
4220
- constructor() {
4237
+ /**
4238
+ * Create a new LarkDatabase instance.
4239
+ *
4240
+ * @param databaseId - Database ID in format "project/database"
4241
+ * @param options - Connection options (token, anonymous, domain, transport).
4242
+ * Defaults to anonymous auth if not specified.
4243
+ *
4244
+ * @example
4245
+ * ```typescript
4246
+ * // Lazy connection - connects on first operation
4247
+ * const db = new LarkDatabase('project/database', { anonymous: true });
4248
+ * db.ref('/users').on('value', cb); // Auto-connects here
4249
+ *
4250
+ * // Explicit connection - for UI feedback or error handling
4251
+ * const db = new LarkDatabase('project/database', { anonymous: true });
4252
+ * await db.connect(); // Explicitly await connection
4253
+ * db.ref('/users').on('value', cb);
4254
+ * ```
4255
+ */
4256
+ constructor(databaseId, options = {}) {
4221
4257
  this._state = "disconnected";
4222
4258
  this._auth = null;
4223
- this._databaseId = null;
4224
- this._domain = null;
4225
4259
  this._volatilePaths = [];
4226
4260
  this._transportType = null;
4227
- // Auth state
4228
- this._currentToken = null;
4229
- // Token for auth (empty string = anonymous)
4230
- this._isAnonymous = false;
4231
4261
  // True if connected anonymously
4232
4262
  // Reconnection state
4233
4263
  this._connectionId = null;
4234
- this._connectOptions = null;
4235
4264
  this._intentionalDisconnect = false;
4236
4265
  this._reconnectAttempt = 0;
4237
4266
  this._reconnectTimer = null;
@@ -4244,13 +4273,21 @@ var LarkDatabase = class {
4244
4273
  this.authStateChangedCallbacks = /* @__PURE__ */ new Set();
4245
4274
  // .info path subscriptions (handled locally, not sent to server)
4246
4275
  this.infoSubscriptions = [];
4247
- // Authentication promise - resolves when auth completes, allows operations to queue
4248
- this.authenticationPromise = null;
4249
- this.authenticationResolve = null;
4276
+ // Connection promise - shared by connect() and lazy operations
4277
+ this._connectionPromise = null;
4250
4278
  this._serverTimeOffset = 0;
4251
4279
  this.messageQueue = new MessageQueue();
4252
4280
  this.subscriptionManager = new SubscriptionManager();
4253
4281
  this.pendingWrites = new PendingWriteManager();
4282
+ const projectId = databaseId.split("/")[0];
4283
+ if (!projectId) {
4284
+ throw new Error('Invalid database ID: must be in format "projectId/databaseName"');
4285
+ }
4286
+ this._databaseId = databaseId;
4287
+ this._connectOptions = options;
4288
+ this._domain = options.domain || DEFAULT_LARK_DOMAIN;
4289
+ this._currentToken = options.token || "";
4290
+ this._isAnonymous = !options.token && options.anonymous !== false;
4254
4291
  }
4255
4292
  // ============================================
4256
4293
  // Connection State
@@ -4335,21 +4372,38 @@ var LarkDatabase = class {
4335
4372
  // Connection Management
4336
4373
  // ============================================
4337
4374
  /**
4338
- * Connect to a database.
4375
+ * Explicitly connect to the database.
4339
4376
  *
4340
- * @param databaseId - Database ID in format "project/database"
4341
- * @param options - Connection options (token, anonymous, domain)
4377
+ * This is optional - operations will auto-connect if needed.
4378
+ * Use this when you want to:
4379
+ * - Await connection completion for UI feedback
4380
+ * - Handle connection errors separately from operation errors
4381
+ * - Pre-warm the connection before first operation
4382
+ *
4383
+ * @returns Promise that resolves when fully authenticated
4342
4384
  */
4343
- async connect(databaseId, options = {}) {
4344
- if (this._state !== "disconnected") {
4345
- throw new Error("Already connected or connecting");
4385
+ async connect() {
4386
+ return this.ensureConnected();
4387
+ }
4388
+ /**
4389
+ * Ensure connection is established, triggering it if needed.
4390
+ * Multiple concurrent calls share the same connection promise.
4391
+ */
4392
+ async ensureConnected() {
4393
+ if (this._state === "authenticated") {
4394
+ return;
4395
+ }
4396
+ if (this._connectionPromise) {
4397
+ return this._connectionPromise;
4346
4398
  }
4347
- this._connectOptions = options;
4348
4399
  this._intentionalDisconnect = false;
4349
- this.authenticationPromise = new Promise((resolve) => {
4350
- this.authenticationResolve = resolve;
4351
- });
4352
- await this.performConnect(databaseId, options);
4400
+ this._connectionPromise = this.performConnect(this._databaseId, this._connectOptions);
4401
+ try {
4402
+ await this._connectionPromise;
4403
+ } catch (error) {
4404
+ this._connectionPromise = null;
4405
+ throw error;
4406
+ }
4353
4407
  }
4354
4408
  /**
4355
4409
  * Internal connect implementation used by both initial connect and reconnect.
@@ -4359,19 +4413,9 @@ var LarkDatabase = class {
4359
4413
  * 3. Send auth (authenticates user - required even for anonymous)
4360
4414
  */
4361
4415
  async performConnect(databaseId, options, isReconnect = false) {
4362
- const previousState = this._state;
4363
4416
  this._state = isReconnect ? "reconnecting" : "connecting";
4364
- this._databaseId = databaseId;
4365
- this._domain = options.domain || DEFAULT_LARK_DOMAIN;
4366
- if (!isReconnect) {
4367
- this._currentToken = options.token || "";
4368
- this._isAnonymous = !options.token && options.anonymous !== false;
4369
- }
4370
4417
  try {
4371
4418
  const projectId = databaseId.split("/")[0];
4372
- if (!projectId) {
4373
- throw new Error('Invalid database ID: must be in format "projectId/databaseName"');
4374
- }
4375
4419
  const wsUrl = `wss://${projectId}.${this._domain}/ws`;
4376
4420
  const transportResult = await createTransport(
4377
4421
  wsUrl,
@@ -4424,10 +4468,6 @@ var LarkDatabase = class {
4424
4468
  };
4425
4469
  this._state = "authenticated";
4426
4470
  this._reconnectAttempt = 0;
4427
- if (this.authenticationResolve) {
4428
- this.authenticationResolve();
4429
- this.authenticationResolve = null;
4430
- }
4431
4471
  this.fireConnectionStateChange();
4432
4472
  if (!isReconnect) {
4433
4473
  this.subscriptionManager.initialize({
@@ -4449,12 +4489,8 @@ var LarkDatabase = class {
4449
4489
  }
4450
4490
  this._state = "disconnected";
4451
4491
  this._auth = null;
4452
- this._databaseId = null;
4453
- this._connectOptions = null;
4454
4492
  this._connectionId = null;
4455
4493
  this._transportType = null;
4456
- this._currentToken = null;
4457
- this._isAnonymous = false;
4458
4494
  this.transport?.close();
4459
4495
  this.transport = null;
4460
4496
  throw error;
@@ -4522,8 +4558,9 @@ var LarkDatabase = class {
4522
4558
  }
4523
4559
  }
4524
4560
  /**
4525
- * Full cleanup - clears all state including subscriptions.
4561
+ * Full cleanup - clears connection state including subscriptions.
4526
4562
  * Used for intentional disconnect.
4563
+ * Preserves config (databaseId, domain, token) so connect() can be called again.
4527
4564
  */
4528
4565
  cleanupFull() {
4529
4566
  const wasAuthenticated = this._state === "authenticated";
@@ -4531,17 +4568,11 @@ var LarkDatabase = class {
4531
4568
  this.transport = null;
4532
4569
  this._state = "disconnected";
4533
4570
  this._auth = null;
4534
- this._databaseId = null;
4535
4571
  this._volatilePaths = [];
4536
- this._domain = null;
4537
4572
  this._connectionId = null;
4538
- this._connectOptions = null;
4539
4573
  this._transportType = null;
4540
- this._currentToken = null;
4541
- this._isAnonymous = false;
4542
4574
  this._reconnectAttempt = 0;
4543
- this.authenticationPromise = null;
4544
- this.authenticationResolve = null;
4575
+ this._connectionPromise = null;
4545
4576
  this.subscriptionManager.clear();
4546
4577
  this.messageQueue.rejectAll(new Error("Connection closed"));
4547
4578
  this.pendingWrites.clear();
@@ -4642,15 +4673,14 @@ var LarkDatabase = class {
4642
4673
  * Attempt to reconnect to the database.
4643
4674
  */
4644
4675
  async attemptReconnect() {
4645
- if (this._intentionalDisconnect || !this._databaseId || !this._connectOptions) {
4676
+ if (this._intentionalDisconnect) {
4646
4677
  return;
4647
4678
  }
4648
- this.authenticationPromise = new Promise((resolve) => {
4649
- this.authenticationResolve = resolve;
4650
- });
4679
+ this._connectionPromise = this.performConnect(this._databaseId, this._connectOptions, true);
4651
4680
  try {
4652
- await this.performConnect(this._databaseId, this._connectOptions, true);
4681
+ await this._connectionPromise;
4653
4682
  } catch {
4683
+ this._connectionPromise = null;
4654
4684
  }
4655
4685
  }
4656
4686
  /**
@@ -4814,9 +4844,7 @@ var LarkDatabase = class {
4814
4844
  * @internal Send a transaction to the server.
4815
4845
  */
4816
4846
  async _sendTransaction(ops) {
4817
- if (!this.isAuthenticatedOrThrow()) {
4818
- if (!this.isAuthenticatedOrThrow()) await this.waitForAuthenticated();
4819
- }
4847
+ if (this._state !== "authenticated") await this.ensureConnected();
4820
4848
  const requestId = this.messageQueue.nextRequestId();
4821
4849
  this.pendingWrites.trackWrite(requestId, "transaction", "/", ops);
4822
4850
  const message = {
@@ -5010,41 +5038,6 @@ var LarkDatabase = class {
5010
5038
  // ============================================
5011
5039
  // Internal: Sending Messages
5012
5040
  // ============================================
5013
- /**
5014
- * Check if authenticated synchronously.
5015
- * Returns true if authenticated, false if connecting (should wait), throws if disconnected.
5016
- */
5017
- isAuthenticatedOrThrow() {
5018
- if (this._state === "authenticated") {
5019
- return true;
5020
- }
5021
- if (this._state === "connecting" || this._state === "connected" || this._state === "joined" || this._state === "reconnecting") {
5022
- return false;
5023
- }
5024
- throw new LarkError("not_connected", "Not connected - call connect() first");
5025
- }
5026
- /**
5027
- * Wait for authentication to complete before performing an operation.
5028
- * If already authenticated, returns immediately (synchronously).
5029
- * If connecting/reconnecting, waits for auth to complete.
5030
- * If disconnected and no connect in progress, throws.
5031
- *
5032
- * IMPORTANT: This returns a Promise only if waiting is needed.
5033
- * Callers should use: `if (!this.isAuthenticatedOrThrow()) if (!this.isAuthenticatedOrThrow()) await this.waitForAuthenticated();`
5034
- * to preserve synchronous execution when already authenticated.
5035
- */
5036
- async waitForAuthenticated() {
5037
- if (this._state === "authenticated") {
5038
- return;
5039
- }
5040
- if (this._state === "connecting" || this._state === "connected" || this._state === "joined" || this._state === "reconnecting") {
5041
- if (this.authenticationPromise) {
5042
- await this.authenticationPromise;
5043
- return;
5044
- }
5045
- }
5046
- throw new LarkError("not_connected", "Not connected - call connect() first");
5047
- }
5048
5041
  send(message) {
5049
5042
  if (!this.transport || !this.transport.connected) {
5050
5043
  throw new LarkError("not_connected", "Not connected to database");
@@ -5059,9 +5052,10 @@ var LarkDatabase = class {
5059
5052
  * Note: Priority is now part of the value (as .priority), not a separate field.
5060
5053
  */
5061
5054
  async _sendSet(path, value) {
5062
- if (!this.isAuthenticatedOrThrow()) await this.waitForAuthenticated();
5055
+ if (this._state !== "authenticated") await this.ensureConnected();
5063
5056
  const normalizedPath = normalizePath(path) || "/";
5064
5057
  validateWriteData(value, normalizedPath);
5058
+ validateWriteSize(value, normalizedPath);
5065
5059
  const requestId = this.messageQueue.nextRequestId();
5066
5060
  const pendingWriteIds = this.subscriptionManager.getPendingWriteIdsForPath(normalizedPath);
5067
5061
  this.pendingWrites.trackWrite(requestId, "set", normalizedPath, value);
@@ -5084,12 +5078,13 @@ var LarkDatabase = class {
5084
5078
  * @internal Send an update operation.
5085
5079
  */
5086
5080
  async _sendUpdate(path, values) {
5087
- if (!this.isAuthenticatedOrThrow()) await this.waitForAuthenticated();
5081
+ if (this._state !== "authenticated") await this.ensureConnected();
5088
5082
  const normalizedPath = normalizePath(path) || "/";
5089
5083
  for (const [key, value] of Object.entries(values)) {
5090
5084
  const fullPath = key.startsWith("/") ? key : `${normalizedPath}/${key}`;
5091
5085
  validateWriteData(value, fullPath);
5092
5086
  }
5087
+ validateWriteSize(values, normalizedPath);
5093
5088
  const requestId = this.messageQueue.nextRequestId();
5094
5089
  const pendingWriteIds = this.subscriptionManager.getPendingWriteIdsForPath(normalizedPath);
5095
5090
  this.pendingWrites.trackWrite(requestId, "update", normalizedPath, values);
@@ -5116,7 +5111,7 @@ var LarkDatabase = class {
5116
5111
  * @internal Send a delete operation.
5117
5112
  */
5118
5113
  async _sendDelete(path) {
5119
- if (!this.isAuthenticatedOrThrow()) await this.waitForAuthenticated();
5114
+ if (this._state !== "authenticated") await this.ensureConnected();
5120
5115
  const normalizedPath = normalizePath(path) || "/";
5121
5116
  const requestId = this.messageQueue.nextRequestId();
5122
5117
  const pendingWriteIds = this.subscriptionManager.getPendingWriteIdsForPath(normalizedPath);
@@ -5151,6 +5146,7 @@ var LarkDatabase = class {
5151
5146
  */
5152
5147
  _sendVolatileSet(path, value) {
5153
5148
  const normalizedPath = normalizePath(path) || "/";
5149
+ validateVolatileWriteSize(value, normalizedPath);
5154
5150
  this.subscriptionManager.applyOptimisticWrite(normalizedPath, value, "", "set");
5155
5151
  if (this._state !== "authenticated" || !this.transport || !this.transport.connected) {
5156
5152
  return;
@@ -5167,6 +5163,7 @@ var LarkDatabase = class {
5167
5163
  */
5168
5164
  _sendVolatileUpdate(path, values) {
5169
5165
  const normalizedPath = normalizePath(path) || "/";
5166
+ validateVolatileWriteSize(values, normalizedPath);
5170
5167
  this.subscriptionManager.applyOptimisticWrite(normalizedPath, values, "", "update");
5171
5168
  if (this._state !== "authenticated" || !this.transport || !this.transport.connected) {
5172
5169
  return;
@@ -5223,7 +5220,7 @@ var LarkDatabase = class {
5223
5220
  return new DataSnapshot(cached.value, path, this);
5224
5221
  }
5225
5222
  }
5226
- if (!this.isAuthenticatedOrThrow()) await this.waitForAuthenticated();
5223
+ if (this._state !== "authenticated") await this.ensureConnected();
5227
5224
  const requestId = this.messageQueue.nextRequestId();
5228
5225
  const message = {
5229
5226
  o: "o",
@@ -5240,7 +5237,7 @@ var LarkDatabase = class {
5240
5237
  * @internal Send an onDisconnect operation.
5241
5238
  */
5242
5239
  async _sendOnDisconnect(path, action, value) {
5243
- if (!this.isAuthenticatedOrThrow()) await this.waitForAuthenticated();
5240
+ if (this._state !== "authenticated") await this.ensureConnected();
5244
5241
  const requestId = this.messageQueue.nextRequestId();
5245
5242
  const message = {
5246
5243
  o: "od",
@@ -5266,13 +5263,12 @@ var LarkDatabase = class {
5266
5263
  * @internal Send a subscribe message to server.
5267
5264
  * Includes tag for non-default queries to enable proper event routing.
5268
5265
  */
5269
- async sendSubscribeMessage(path, eventTypes, queryParams, tag) {
5270
- if (!this.isAuthenticatedOrThrow()) await this.waitForAuthenticated();
5266
+ async sendSubscribeMessage(path, queryParams, tag) {
5267
+ if (this._state !== "authenticated") await this.ensureConnected();
5271
5268
  const requestId = this.messageQueue.nextRequestId();
5272
5269
  const message = {
5273
5270
  o: "sb",
5274
5271
  p: normalizePath(path) || "/",
5275
- e: eventTypes,
5276
5272
  r: requestId,
5277
5273
  ...queryParams,
5278
5274
  ...tag !== void 0 ? { tag } : {}
@@ -5285,7 +5281,7 @@ var LarkDatabase = class {
5285
5281
  * Includes query params and tag so server can identify which specific subscription to remove.
5286
5282
  */
5287
5283
  async sendUnsubscribeMessage(path, queryParams, tag) {
5288
- if (!this.isAuthenticatedOrThrow()) await this.waitForAuthenticated();
5284
+ if (this._state !== "authenticated") await this.ensureConnected();
5289
5285
  const requestId = this.messageQueue.nextRequestId();
5290
5286
  const message = {
5291
5287
  o: "us",
@@ -5350,4 +5346,4 @@ export {
5350
5346
  ServerValue,
5351
5347
  LarkDatabase
5352
5348
  };
5353
- //# sourceMappingURL=chunk-UVJBN3NR.mjs.map
5349
+ //# sourceMappingURL=chunk-ELNTJSEE.mjs.map