durability 5.0.0 → 5.2.0

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 (89) hide show
  1. package/README.md +238 -7
  2. package/dist/{durability-alarms.d.ts → alarms/index.d.ts} +23 -6
  3. package/dist/{durability-alarms.js → alarms/index.js} +96 -52
  4. package/dist/alarms/migrations.d.ts +9 -0
  5. package/dist/alarms/migrations.js +133 -0
  6. package/dist/errors.d.ts +15 -0
  7. package/dist/errors.js +24 -0
  8. package/dist/experimental.d.ts +11 -8
  9. package/dist/experimental.js +8 -5
  10. package/dist/{fanout.d.ts → fanout/index.d.ts} +13 -6
  11. package/dist/{fanout.js → fanout/index.js} +55 -49
  12. package/dist/fanout/migrations.d.ts +9 -0
  13. package/dist/fanout/migrations.js +181 -0
  14. package/dist/{stored-message.d.ts → fanout/stored-message.d.ts} +1 -1
  15. package/dist/index.d.ts +14 -9
  16. package/dist/index.js +13 -8
  17. package/dist/{log.d.ts → log/index.d.ts} +15 -3
  18. package/dist/{log.js → log/index.js} +152 -124
  19. package/dist/log/migrations.d.ts +18 -0
  20. package/dist/log/migrations.js +297 -0
  21. package/dist/{memoize.d.ts → memoize/index.d.ts} +1 -1
  22. package/dist/{memoize.js → memoize/index.js} +14 -11
  23. package/dist/memoize/migrations.d.ts +10 -0
  24. package/dist/memoize/migrations.js +21 -0
  25. package/dist/migrations.d.ts +1 -103
  26. package/dist/migrations.js +1 -570
  27. package/dist/{durability.d.ts → operations/index.d.ts} +13 -5
  28. package/dist/{durability.js → operations/index.js} +63 -46
  29. package/dist/operations/migrations.d.ts +10 -0
  30. package/dist/operations/migrations.js +150 -0
  31. package/dist/{outbox.d.ts → outbox/index.d.ts} +4 -4
  32. package/dist/{outbox.js → outbox/index.js} +18 -63
  33. package/dist/outbox/migrations.d.ts +9 -0
  34. package/dist/outbox/migrations.js +43 -0
  35. package/dist/{routing-client.js → routing/client.js} +1 -1
  36. package/dist/{routing.d.ts → routing/index.d.ts} +3 -3
  37. package/dist/{routing.js → routing/index.js} +3 -3
  38. package/dist/{load.d.ts → routing/load.d.ts} +1 -1
  39. package/dist/schedule/index.d.ts +165 -0
  40. package/dist/schedule/index.js +667 -0
  41. package/dist/schedule/migrations.d.ts +9 -0
  42. package/dist/schedule/migrations.js +118 -0
  43. package/dist/{scheduler.d.ts → scheduler/index.d.ts} +122 -22
  44. package/dist/scheduler/index.js +925 -0
  45. package/dist/{semaphore-client.d.ts → semaphore/client.d.ts} +1 -1
  46. package/dist/{semaphore-client.js → semaphore/client.js} +2 -2
  47. package/dist/{semaphore.d.ts → semaphore/index.d.ts} +10 -10
  48. package/dist/{semaphore.js → semaphore/index.js} +7 -7
  49. package/dist/{semaphore-leases.d.ts → semaphore/leases.d.ts} +17 -6
  50. package/dist/{semaphore-leases.js → semaphore/leases.js} +28 -12
  51. package/dist/semaphore/migrations.d.ts +18 -0
  52. package/dist/semaphore/migrations.js +444 -0
  53. package/dist/{semaphore-protocol.d.ts → semaphore/protocol.d.ts} +16 -8
  54. package/dist/{semaphore-protocol.js → semaphore/protocol.js} +37 -14
  55. package/dist/{semaphore-queue-store.d.ts → semaphore/queue-store.d.ts} +12 -6
  56. package/dist/{semaphore-queue-store.js → semaphore/queue-store.js} +87 -75
  57. package/dist/{semaphore-queue.d.ts → semaphore/queue.d.ts} +15 -5
  58. package/dist/{semaphore-queue.js → semaphore/queue.js} +105 -46
  59. package/dist/{semaphore-server.d.ts → semaphore/server.d.ts} +12 -4
  60. package/dist/{semaphore-server.js → semaphore/server.js} +51 -24
  61. package/dist/{semaphore-store.d.ts → semaphore/store.d.ts} +8 -3
  62. package/dist/{semaphore-store.js → semaphore/store.js} +35 -33
  63. package/dist/{client.d.ts → socket/client.d.ts} +3 -3
  64. package/dist/{client.js → socket/client.js} +4 -4
  65. package/dist/{socket.d.ts → socket/index.d.ts} +15 -4
  66. package/dist/{socket.js → socket/index.js} +66 -20
  67. package/dist/socket/migrations.d.ts +9 -0
  68. package/dist/socket/migrations.js +57 -0
  69. package/dist/{socket-protocol.d.ts → socket/protocol.d.ts} +1 -1
  70. package/dist/storage.d.ts +9 -2
  71. package/dist/storage.js +24 -11
  72. package/dist/{topology-client.d.ts → topology/client.d.ts} +2 -2
  73. package/dist/{topology-client.js → topology/client.js} +3 -3
  74. package/dist/{topology.d.ts → topology/index.d.ts} +17 -7
  75. package/dist/{topology.js → topology/index.js} +92 -58
  76. package/dist/topology/migrations.d.ts +10 -0
  77. package/dist/topology/migrations.js +138 -0
  78. package/dist/{tracker.d.ts → tracker/index.d.ts} +3 -3
  79. package/dist/{tracker.js → tracker/index.js} +2 -2
  80. package/package.json +11 -11
  81. package/dist/scheduler.js +0 -577
  82. /package/dist/{stored-message.js → fanout/stored-message.js} +0 -0
  83. /package/dist/{routing-client.d.ts → routing/client.d.ts} +0 -0
  84. /package/dist/{load.js → routing/load.js} +0 -0
  85. /package/dist/{socket-protocol.js → socket/protocol.js} +0 -0
  86. /package/dist/{topology-snapshot.d.ts → topology/snapshot.d.ts} +0 -0
  87. /package/dist/{topology-snapshot.js → topology/snapshot.js} +0 -0
  88. /package/dist/{tracked-source.d.ts → tracker/source.d.ts} +0 -0
  89. /package/dist/{tracked-source.js → tracker/source.js} +0 -0
@@ -1,4 +1,4 @@
1
- import { type PermitGrant, type PermitRequest, type PermitSync, type PermitSyncResult, type SemaphoreRequest, type SemaphoreResponse } from './semaphore-protocol.js';
1
+ import { type PermitGrant, type PermitRequest, type PermitSync, type PermitSyncResult, type SemaphoreRequest, type SemaphoreResponse } from './protocol.js';
2
2
  export type SemaphoreAddress = {
3
3
  /** The semaphore key: the name of the Durable Object holding the limit. */
4
4
  key: string;
@@ -10,8 +10,8 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
12
  var _SemaphoreClient_instances, _SemaphoreClient_config, _SemaphoreClient_nextRequestAt, _SemaphoreClient_address;
13
- import { SemaphoreProtocolError } from './errors.js';
14
- import { permitGrantSchema, permitSyncResultSchema, } from './semaphore-protocol.js';
13
+ import { SemaphoreProtocolError } from '../errors.js';
14
+ import { permitGrantSchema, permitSyncResultSchema, } from './protocol.js';
15
15
  /**
16
16
  * Transport-independent protocol core. Validates responses and remembers each
17
17
  * key's `nextRequestAt` so callers can pace requests the way the holder asked.
@@ -5,17 +5,17 @@
5
5
  * @packageDocumentation
6
6
  */
7
7
  import { type TransformContext, type TransformStub } from '@durability/transforms';
8
- import { type DurableObjectTarget } from './durable-object-target.js';
9
- import { type PermitGrant, type PermitRequest, type PermitSync, type PermitSyncResult, type SemaphoreRequest, type SemaphoreResponse } from './semaphore-protocol.js';
10
- import { DurabilitySemaphoreServer } from './semaphore-server.js';
11
- export { SemaphoreProtocolError } from './errors.js';
8
+ import { type DurableObjectTarget } from '../durable-object-target.js';
9
+ import { type PermitGrant, type PermitRequest, type PermitSync, type PermitSyncResult, type SemaphoreRequest, type SemaphoreResponse } from './protocol.js';
10
+ import { DurabilitySemaphoreServer } from './server.js';
11
+ export { SemaphoreProtocolError } from '../errors.js';
12
12
  export { durabilitySemaphoreMigrations, durabilitySemaphoreQueueMigrations, } from './migrations.js';
13
- export type { GrantReport, GrantResult, PermitGrant, PermitRequest, PermitSync, PermitSyncResult, SemaphoreBlockedReason, SemaphoreRequest, SemaphoreResponse, } from './semaphore-protocol.js';
14
- export type { SemaphoreAddress, SemaphoreClientConfig, } from './semaphore-client.js';
15
- export { SemaphoreClient } from './semaphore-client.js';
16
- export type { DurabilitySemaphoreConfig, DurabilitySemaphoreEvent, SemaphoreUsage, } from './semaphore-server.js';
17
- export type { SemaphoreLeaseOptions, SemaphoreProvisioning, } from './semaphore-leases.js';
18
- export { DurabilitySemaphoreQueue, type AdmittedItem, type DurabilitySemaphoreQueueConfig, type DurabilitySemaphoreQueueEvent, type SemaphoreHolder, type SemaphoreInput, type SemaphoreItemStatus, } from './semaphore-queue.js';
13
+ export type { GrantReport, GrantResult, PermitGrant, PermitRequest, PermitSync, PermitSyncResult, SemaphoreBlockedReason, SemaphoreRequest, SemaphoreResponse, } from './protocol.js';
14
+ export type { SemaphoreAddress, SemaphoreClientConfig } from './client.js';
15
+ export { SemaphoreClient } from './client.js';
16
+ export type { DurabilitySemaphoreConfig, DurabilitySemaphoreEvent, SemaphoreUsage, } from './server.js';
17
+ export type { SemaphoreLeaseOptions, SemaphoreProvisioning } from './leases.js';
18
+ export { DurabilitySemaphoreQueue, type AdmittedItem, type DurabilitySemaphoreQueueConfig, type DurabilitySemaphoreQueueEvent, type SemaphoreHolder, type SemaphoreInput, type SemaphoreItemStatus, } from './queue.js';
19
19
  export type SemaphoreSession = {
20
20
  request(key: string, message: PermitRequest): Promise<PermitGrant>;
21
21
  sync(key: string, message: PermitSync): Promise<PermitSyncResult>;
@@ -5,14 +5,14 @@
5
5
  * @packageDocumentation
6
6
  */
7
7
  import { createTransformStub, } from '@durability/transforms';
8
- import { resolveNamespace, } from './durable-object-target.js';
9
- import { SemaphoreProtocolError } from './errors.js';
10
- import { SemaphoreClient } from './semaphore-client.js';
11
- import { DurabilitySemaphoreServer } from './semaphore-server.js';
12
- export { SemaphoreProtocolError } from './errors.js';
8
+ import { resolveNamespace, } from '../durable-object-target.js';
9
+ import { SemaphoreProtocolError } from '../errors.js';
10
+ import { SemaphoreClient } from './client.js';
11
+ import { DurabilitySemaphoreServer } from './server.js';
12
+ export { SemaphoreProtocolError } from '../errors.js';
13
13
  export { durabilitySemaphoreMigrations, durabilitySemaphoreQueueMigrations, } from './migrations.js';
14
- export { SemaphoreClient } from './semaphore-client.js';
15
- export { DurabilitySemaphoreQueue, } from './semaphore-queue.js';
14
+ export { SemaphoreClient } from './client.js';
15
+ export { DurabilitySemaphoreQueue, } from './queue.js';
16
16
  /**
17
17
  * Inside a DO, holds one concurrency limit for many client Durable Objects.
18
18
  * Outside the DO, `client()` routes semaphore messages to the application-owned
@@ -1,5 +1,5 @@
1
- import type { LeaseGrant, LeaseReport, SemaphoreBlockedReason } from './semaphore-protocol.js';
2
- import type { HolderStore, LeaseRow } from './semaphore-store.js';
1
+ import type { LeaseGrant, LeaseReport, SemaphoreBlockedReason } from './protocol.js';
2
+ import type { HolderStore, LeaseRow } from './store.js';
3
3
  /**
4
4
  * Where lease capacity comes from.
5
5
  *
@@ -47,9 +47,21 @@ export type LeaseEvent = {
47
47
  timestamp: number;
48
48
  leaseId: string;
49
49
  clientId: string;
50
+ clientChannel?: string;
50
51
  group: string;
51
52
  permits: number;
52
53
  };
54
+ /** A client is its object's `clientId` together with the queue channel on that object. */
55
+ export type SemaphoreClientIdentity = {
56
+ clientId: string;
57
+ clientChannel: string;
58
+ };
59
+ /** Map key for a client identity; JSON keeps raw IDs and channels unambiguous. */
60
+ export declare const clientKey: (clientId: string, clientChannel: string) => string;
61
+ export declare const ownedBy: (row: {
62
+ client_id: string;
63
+ client_channel: string;
64
+ }, client: SemaphoreClientIdentity) => boolean;
53
65
  type GroupPolicy = {
54
66
  limit?: number;
55
67
  blocked?: SemaphoreBlockedReason;
@@ -77,14 +89,13 @@ export type LeaseReportOutcome = {
77
89
  * and charges their reported live footprint so a forgotten lease cannot
78
90
  * over-admit for the rest of this round.
79
91
  */
80
- export declare const applyLeaseReports: (store: HolderStore, policy: LeasePolicy, clientId: string, reports: LeaseReport[], now: number) => LeaseReportOutcome;
81
- export type LeaseCandidate = {
82
- clientId: string;
92
+ export declare const applyLeaseReports: (store: HolderStore, policy: LeasePolicy, client: SemaphoreClientIdentity, reports: LeaseReport[], now: number) => LeaseReportOutcome;
93
+ export type LeaseCandidate = SemaphoreClientIdentity & {
83
94
  group: string;
84
95
  oldestAt: number;
85
96
  };
86
97
  export type LeaseAllocation = {
87
- /** New and retired leases per client. */
98
+ /** New and retired leases per client, keyed by {@link clientKey}. */
88
99
  grants: Map<string, LeaseGrant[]>;
89
100
  footprint: number;
90
101
  events: LeaseEvent[];
@@ -1,4 +1,8 @@
1
- import { assertPositiveInteger } from './policy.js';
1
+ import { assertPositiveInteger } from '../policy.js';
2
+ /** Map key for a client identity; JSON keeps raw IDs and channels unambiguous. */
3
+ export const clientKey = (clientId, clientChannel) => JSON.stringify([clientId, clientChannel]);
4
+ export const ownedBy = (row, client) => row.client_id === client.clientId &&
5
+ row.client_channel === client.clientChannel;
2
6
  const maxDelayMs = 2 ** 31 - 1;
3
7
  export const resolveLeasePolicy = (options) => {
4
8
  const policy = {
@@ -67,7 +71,7 @@ export const leaseEligible = (policy, group) => group.blocked === undefined &&
67
71
  * and charges their reported live footprint so a forgotten lease cannot
68
72
  * over-admit for the rest of this round.
69
73
  */
70
- export const applyLeaseReports = (store, policy, clientId, reports, now) => {
74
+ export const applyLeaseReports = (store, policy, client, reports, now) => {
71
75
  const outcome = {
72
76
  echoes: [],
73
77
  unknownDebt: 0,
@@ -75,7 +79,7 @@ export const applyLeaseReports = (store, policy, clientId, reports, now) => {
75
79
  };
76
80
  for (const report of reports) {
77
81
  const lease = store.getLease(report.leaseId);
78
- if (!lease || lease.client_id !== clientId) {
82
+ if (!lease || !ownedBy(lease, client)) {
79
83
  outcome.echoes.push({
80
84
  leaseId: report.leaseId,
81
85
  group: lease?.grp ?? '',
@@ -126,8 +130,9 @@ export const allocateLeases = (store, policy, input) => {
126
130
  const { now } = input;
127
131
  const grants = new Map();
128
132
  const events = [];
129
- const echo = (clientId, grant) => {
130
- grants.set(clientId, [...(grants.get(clientId) ?? []), grant]);
133
+ const echo = (lease, grant) => {
134
+ const key = clientKey(lease.client_id, lease.client_channel);
135
+ grants.set(key, [...(grants.get(key) ?? []), grant]);
131
136
  };
132
137
  let footprint = 0;
133
138
  const groupFootprint = new Map();
@@ -143,13 +148,14 @@ export const allocateLeases = (store, policy, input) => {
143
148
  // Stop draws now; the full footprint stays until the client confirms.
144
149
  current = { ...lease, retired: 1 };
145
150
  store.updateLease(current);
146
- echo(lease.client_id, toLeaseGrant(current, now));
151
+ echo(lease, toLeaseGrant(current, now));
147
152
  events.push({
148
153
  entityKind: 'semaphore',
149
154
  type: 'lease_retired',
150
155
  timestamp: now,
151
156
  leaseId: lease.lease_id,
152
157
  clientId: lease.client_id,
158
+ clientChannel: lease.client_channel,
153
159
  group: lease.grp,
154
160
  permits: lease.permits - (input.drawn.get(lease.lease_id) ?? lease.permits),
155
161
  });
@@ -160,17 +166,22 @@ export const allocateLeases = (store, policy, input) => {
160
166
  // An unreported lease is assumed fully drawn, as gatekeeper does.
161
167
  if (!current.retired &&
162
168
  (input.drawn.get(lease.lease_id) ?? lease.permits) < lease.permits) {
163
- usable.add(JSON.stringify([lease.client_id, lease.grp]));
169
+ usable.add(JSON.stringify([lease.client_id, lease.client_channel, lease.grp]));
164
170
  }
165
171
  }
166
172
  const byGroup = new Map();
167
173
  for (const candidate of input.candidates) {
168
174
  if (!leaseEligible(policy, input.policies.get(candidate.group) ?? {}) ||
169
- usable.has(JSON.stringify([candidate.clientId, candidate.group]))) {
175
+ usable.has(JSON.stringify([
176
+ candidate.clientId,
177
+ candidate.clientChannel,
178
+ candidate.group,
179
+ ]))) {
170
180
  continue;
171
181
  }
172
182
  const existing = byGroup.get(candidate.group) ?? [];
173
- const prior = existing.find((c) => c.clientId === candidate.clientId);
183
+ const prior = existing.find((c) => c.clientId === candidate.clientId &&
184
+ c.clientChannel === candidate.clientChannel);
174
185
  if (prior) {
175
186
  prior.oldestAt = Math.min(prior.oldestAt, candidate.oldestAt);
176
187
  }
@@ -183,7 +194,9 @@ export const allocateLeases = (store, policy, input) => {
183
194
  .map(([group, clients]) => ({
184
195
  group,
185
196
  clients: clients
186
- .sort((a, b) => a.oldestAt - b.oldestAt || a.clientId.localeCompare(b.clientId))
197
+ .sort((a, b) => a.oldestAt - b.oldestAt ||
198
+ a.clientId.localeCompare(b.clientId) ||
199
+ a.clientChannel.localeCompare(b.clientChannel))
187
200
  .slice(0, policy.maxClientsPerGroup),
188
201
  oldestAt: Math.min(...clients.map((c) => c.oldestAt)),
189
202
  }))
@@ -210,13 +223,14 @@ export const allocateLeases = (store, policy, input) => {
210
223
  const retired = { ...lease, retired: 1 };
211
224
  store.updateLease(retired);
212
225
  excess -= lease.permits - drawn;
213
- echo(lease.client_id, toLeaseGrant(retired, now));
226
+ echo(lease, toLeaseGrant(retired, now));
214
227
  events.push({
215
228
  entityKind: 'semaphore',
216
229
  type: 'lease_retired',
217
230
  timestamp: now,
218
231
  leaseId: lease.lease_id,
219
232
  clientId: lease.client_id,
233
+ clientChannel: lease.client_channel,
220
234
  group: lease.grp,
221
235
  permits: lease.permits - drawn,
222
236
  });
@@ -247,6 +261,7 @@ export const allocateLeases = (store, policy, input) => {
247
261
  const lease = {
248
262
  lease_id: crypto.randomUUID(),
249
263
  client_id: candidate.clientId,
264
+ client_channel: candidate.clientChannel,
250
265
  grp: candidate.group,
251
266
  permits,
252
267
  released: 0,
@@ -257,13 +272,14 @@ export const allocateLeases = (store, policy, input) => {
257
272
  store.insertLease(lease);
258
273
  footprint += permits;
259
274
  groupFootprint.set(candidate.group, (groupFootprint.get(candidate.group) ?? 0) + permits);
260
- echo(candidate.clientId, toLeaseGrant(lease, now));
275
+ echo(lease, toLeaseGrant(lease, now));
261
276
  events.push({
262
277
  entityKind: 'semaphore',
263
278
  type: 'lease_granted',
264
279
  timestamp: now,
265
280
  leaseId: lease.lease_id,
266
281
  clientId: lease.client_id,
282
+ clientChannel: lease.client_channel,
267
283
  group: lease.grp,
268
284
  permits,
269
285
  });
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Ordered SQLite migrations for semaphore grants. Applied automatically on
3
+ * construction.
4
+ */
5
+ export declare const durabilitySemaphoreMigrations: {
6
+ name: string;
7
+ up: string;
8
+ down: string;
9
+ }[];
10
+ /**
11
+ * Ordered SQLite migrations for a semaphore client's queue. Applied
12
+ * automatically on construction.
13
+ */
14
+ export declare const durabilitySemaphoreQueueMigrations: {
15
+ name: string;
16
+ up: string;
17
+ down: string;
18
+ }[];