@open-e2ee/signal-protocol-sdk 9.3.0 → 9.5.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 (92) hide show
  1. package/ADAPTERS.md +1 -0
  2. package/CHANGELOG.md +269 -0
  3. package/README.md +1 -1
  4. package/SECURITY.md +2 -2
  5. package/client/README.md +11 -14
  6. package/dist/client/client.d.ts +134 -64
  7. package/dist/client/client.d.ts.map +1 -1
  8. package/dist/client/client.js +333 -179
  9. package/dist/client/client.js.map +1 -1
  10. package/dist/client/config.d.ts +4 -7
  11. package/dist/client/config.d.ts.map +1 -1
  12. package/dist/client/config.js.map +1 -1
  13. package/dist/client/content-adapter.d.ts +8 -0
  14. package/dist/client/content-adapter.d.ts.map +1 -1
  15. package/dist/client/content-adapter.js +4 -0
  16. package/dist/client/content-adapter.js.map +1 -1
  17. package/dist/client/event-hooks.d.ts +1 -1
  18. package/dist/client/group-outbox.d.ts +2 -4
  19. package/dist/client/group-outbox.d.ts.map +1 -1
  20. package/dist/client/group-outbox.js +7 -1
  21. package/dist/client/group-outbox.js.map +1 -1
  22. package/dist/client/hosted-anonymous-delivery.d.ts +1 -1
  23. package/dist/client/hosted-anonymous-delivery.d.ts.map +1 -1
  24. package/dist/client/hosted-anonymous-delivery.js +5 -14
  25. package/dist/client/hosted-anonymous-delivery.js.map +1 -1
  26. package/dist/client/hosted-mailbox-subscription.d.ts.map +1 -1
  27. package/dist/client/hosted-mailbox-subscription.js +136 -15
  28. package/dist/client/hosted-mailbox-subscription.js.map +1 -1
  29. package/dist/client/hosted-transport.d.ts +2 -2
  30. package/dist/client/hosted-transport.d.ts.map +1 -1
  31. package/dist/client/hosted-transport.js +20 -5
  32. package/dist/client/hosted-transport.js.map +1 -1
  33. package/dist/client/messages.d.ts.map +1 -1
  34. package/dist/client/messages.js +12 -2
  35. package/dist/client/messages.js.map +1 -1
  36. package/dist/client/relay-acceptance.d.ts +29 -0
  37. package/dist/client/relay-acceptance.d.ts.map +1 -0
  38. package/dist/client/relay-acceptance.js +29 -0
  39. package/dist/client/relay-acceptance.js.map +1 -0
  40. package/dist/client/relay-subscription.d.ts +43 -1
  41. package/dist/client/relay-subscription.d.ts.map +1 -1
  42. package/dist/client/relay-subscription.js +278 -100
  43. package/dist/client/relay-subscription.js.map +1 -1
  44. package/dist/client/relay-work.d.ts +7 -6
  45. package/dist/client/relay-work.d.ts.map +1 -1
  46. package/dist/client/relay-work.js +34 -9
  47. package/dist/client/relay-work.js.map +1 -1
  48. package/dist/client/retry-family.d.ts +119 -0
  49. package/dist/client/retry-family.d.ts.map +1 -0
  50. package/dist/client/retry-family.js +170 -0
  51. package/dist/client/retry-family.js.map +1 -0
  52. package/dist/client/retry-request-envelope.d.ts +106 -0
  53. package/dist/client/retry-request-envelope.d.ts.map +1 -0
  54. package/dist/client/retry-request-envelope.js +245 -0
  55. package/dist/client/retry-request-envelope.js.map +1 -0
  56. package/dist/client/retry.d.ts +66 -24
  57. package/dist/client/retry.d.ts.map +1 -1
  58. package/dist/client/retry.js +239 -139
  59. package/dist/client/retry.js.map +1 -1
  60. package/dist/client/sealed-sender.d.ts +15 -3
  61. package/dist/client/sealed-sender.d.ts.map +1 -1
  62. package/dist/client/sealed-sender.js +22 -3
  63. package/dist/client/sealed-sender.js.map +1 -1
  64. package/dist/client/signal-service-cipher.d.ts +52 -0
  65. package/dist/client/signal-service-cipher.d.ts.map +1 -1
  66. package/dist/client/signal-service-cipher.js +185 -15
  67. package/dist/client/signal-service-cipher.js.map +1 -1
  68. package/dist/client/state.d.ts +12 -64
  69. package/dist/client/state.d.ts.map +1 -1
  70. package/dist/client/state.js +22 -98
  71. package/dist/client/state.js.map +1 -1
  72. package/dist/client/types.d.ts +28 -2
  73. package/dist/client/types.d.ts.map +1 -1
  74. package/dist/client/types.js.map +1 -1
  75. package/dist/internal/protocol/sealed-sender/types.d.ts +1 -1
  76. package/dist/internal/protocol/sealed-sender/types.js +1 -1
  77. package/dist/local/store/received-content.d.ts +16 -2
  78. package/dist/local/store/received-content.d.ts.map +1 -1
  79. package/dist/local/store/received-content.js +18 -3
  80. package/dist/local/store/received-content.js.map +1 -1
  81. package/dist/local/store/reliability.d.ts +260 -2
  82. package/dist/local/store/reliability.d.ts.map +1 -1
  83. package/dist/local/store/reliability.js +390 -12
  84. package/dist/local/store/reliability.js.map +1 -1
  85. package/dist/remote/relay/types.d.ts +31 -31
  86. package/dist/remote/relay/types.d.ts.map +1 -1
  87. package/dist/types/api.d.ts +74 -26
  88. package/dist/types/api.d.ts.map +1 -1
  89. package/docs/ASSURANCE.md +5 -5
  90. package/docs/DEVIATIONS.md +4 -5
  91. package/docs/INTERFACES.md +2 -0
  92. package/package.json +1 -1
@@ -2,12 +2,13 @@
2
2
  * Relay work: the deliveries, retry requests, and receipt sends that the
3
3
  * relay starts and that stop() must settle.
4
4
  *
5
- * The relay calls its handlers without waiting for them. Each call runs as
6
- * one RelayWork item. At any time an item runs SDK work, or waits in an app
7
- * hook. stop() stops every item and waits only for the items that run SDK
8
- * work. It does not wait for time in app hooks, because a hook can itself
9
- * await stop(), and without async context the SDK cannot tell that call from
10
- * a call outside the hook.
5
+ * A relay can wait for a handler, as the hosted transport does for each
6
+ * envelope, or call it without waiting, as the memory and Convex adapters do.
7
+ * In both cases each call runs as one RelayWork item. At any time an item
8
+ * runs SDK work, or waits in an app hook. stop() stops every item and waits
9
+ * only for the items that run SDK work. It does not wait for time in app
10
+ * hooks, because a hook can itself await stop(), and without async context
11
+ * the SDK cannot tell that call from a call outside the hook.
11
12
  *
12
13
  * A stopped item may not enter an app hook, and an item that leaves an app
13
14
  * hook after stop() starts throws RelayWorkStopped. So no SDK step after the
@@ -176,10 +177,20 @@ export class RelayWorkTracker {
176
177
  items = new Set();
177
178
  waiters = new Set();
178
179
  stopping = 0;
180
+ stopper = new AbortController();
181
+ /**
182
+ * Aborts when the next stop() starts to settle. Work that keeps this signal
183
+ * after it ends, such as a retry timer, sees that stop. Each stop gets a
184
+ * new signal for the work that starts after it.
185
+ */
186
+ get stopSignal() {
187
+ return this.stopper.signal;
188
+ }
179
189
  /**
180
- * Run a task as a relay work item. While stop() settles, the item starts
181
- * stopped. A RelayWorkStopped from the task resolves the returned promise.
182
- * Other errors reject it.
190
+ * Run a task as a relay work item. The task starts at once, before run()
191
+ * returns. While stop() settles, the item starts stopped. A
192
+ * RelayWorkStopped from the task resolves the returned promise. Other
193
+ * errors reject it.
183
194
  */
184
195
  async run(task) {
185
196
  const work = new RelayWork(() => this.notify());
@@ -198,6 +209,17 @@ export class RelayWorkTracker {
198
209
  this.notify();
199
210
  }
200
211
  }
212
+ /**
213
+ * Stop every item except the given items, and do not wait. A stopped item
214
+ * that leaves an app hook throws RelayWorkStopped, so it writes nothing
215
+ * more. Items that start later are not stopped. settle() stops them.
216
+ */
217
+ stopAllExcept(keep) {
218
+ for (const work of this.items) {
219
+ if (!keep.has(work))
220
+ work.stop();
221
+ }
222
+ }
201
223
  /**
202
224
  * Stop every item and wait until each item settles or waits in an app hook.
203
225
  * Items that start during the wait start stopped, and the wait includes them.
@@ -205,6 +227,7 @@ export class RelayWorkTracker {
205
227
  */
206
228
  async settle() {
207
229
  this.stopping++;
230
+ this.stopper.abort();
208
231
  try {
209
232
  for (const work of this.items)
210
233
  work.stop();
@@ -214,6 +237,8 @@ export class RelayWorkTracker {
214
237
  }
215
238
  finally {
216
239
  this.stopping--;
240
+ if (this.stopping === 0)
241
+ this.stopper = new AbortController();
217
242
  }
218
243
  }
219
244
  notify() {
@@ -1 +1 @@
1
- {"version":3,"file":"relay-work.js","sourceRoot":"","sources":["../../client/relay-work.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAIH,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAGzD;;;;;GAKG;AACH,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IACzC;QACE,KAAK,CAAC,sCAAsC,CAAC,CAAC;QAC9C,IAAI,CAAC,IAAI,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GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;gBACpD,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC;YAClE,CAAC;QACH,CAAC;gBAAS,CAAC;YACT,IAAI,CAAC,QAAQ,EAAE,CAAC;YAChB,IAAI,IAAI,CAAC,QAAQ,KAAK,CAAC;gBAAE,IAAI,CAAC,OAAO,GAAG,IAAI,eAAe,EAAE,CAAC;QAChE,CAAC;IACH,CAAC;IAEO,MAAM;QACZ,MAAM,OAAO,GAAG,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC;QAClC,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;QACrB,KAAK,MAAM,OAAO,IAAI,OAAO;YAAE,OAAO,EAAE,CAAC;IAC3C,CAAC;CACF"}
@@ -0,0 +1,119 @@
1
+ /**
2
+ * The retry family of a received envelope.
3
+ *
4
+ * A failed envelope, the request record that the requester keeps for it, and
5
+ * each resend or null message that answers that record form one retry
6
+ * family. The family index gives the failed envelope of a retry ID. Both
7
+ * receive paths, the relay subscription route and `processIncomingEnvelope`,
8
+ * receive each envelope through `receiveInRetryFamily`, so the application
9
+ * gets the content of one failed envelope at most once, from the envelope
10
+ * itself or from one family member.
11
+ */
12
+ import type { Logger } from '../logger.js';
13
+ import { withProcessedEnvelopeLock, type ResolvedRetryFamilyMember } from '../local/store/reliability.js';
14
+ import type { RetryRequestFailure } from './retry-request-envelope.js';
15
+ type RetryFamilyStore = Parameters<typeof withProcessedEnvelopeLock>[0];
16
+ /**
17
+ * One received envelope in the retry family of its failed envelope. The
18
+ * receive path calls `admit` before the decrypt, `fulfill` after a decrypt
19
+ * that succeeds, and `requestRetry` after a decrypt that fails.
20
+ */
21
+ export interface RetryFamilyReceive {
22
+ /**
23
+ * The resend or the null message that the envelope is, when the family
24
+ * index of the local device holds its retry ID. Undefined for any other
25
+ * envelope.
26
+ */
27
+ readonly member: ResolvedRetryFamilyMember | undefined;
28
+ /**
29
+ * Tell whether the envelope goes to the decrypt. False when the local
30
+ * device processed the envelope, or when the application already has the
31
+ * content of its failed envelope: the caller then acknowledges the
32
+ * envelope and drops it, with no decrypt and no retry request. This
33
+ * applies to the failed envelope and to each member. The check writes a
34
+ * debug log line for a dropped envelope. A member that goes to the decrypt
35
+ * marks the stored request attempt answered when the member has its
36
+ * attempt number.
37
+ */
38
+ admit(): Promise<boolean>;
39
+ /**
40
+ * Record a decrypt that succeeded. For a member, and for a failed
41
+ * envelope with a request record, one write marks the failed envelope
42
+ * processed and fulfilled. A redelivery of the failed envelope on either
43
+ * path is then dropped, and so is each later member. When the write
44
+ * fails, it writes one warning with the behavior
45
+ * RETRY_FULFILL_WRITE_FAILED and returns, so the caller continues as for
46
+ * any decrypted envelope. A redelivery of the failed envelope or of
47
+ * another member can then give the content again.
48
+ */
49
+ fulfill(): Promise<void>;
50
+ /**
51
+ * Record a decrypted null message, in place of `fulfill`. A null
52
+ * message carries no content, so the caller never gives it to the
53
+ * application. The null message is processed. For a member, the failed
54
+ * envelope is processed and not fulfilled: a redelivery of the failed
55
+ * envelope is dropped, the client asks for it no more, and a content
56
+ * resend that comes later is still admitted. When a processed write
57
+ * fails, it writes one warning with the behavior
58
+ * RETRY_NULL_WRITE_FAILED and returns, as `fulfill` does, so the caller
59
+ * still consumes the null message. A redelivery of the failed envelope
60
+ * can then give the content.
61
+ *
62
+ * @param fingerprint - The fingerprint of the received envelope.
63
+ */
64
+ consumeNull(fingerprint: string | undefined): Promise<void>;
65
+ /**
66
+ * Name the sender of a member whose seal fails to open. Such a member
67
+ * names no sender, and the request record that `admit` read names the
68
+ * device that the request went to. The result is that envelope with the
69
+ * sender of the record. Any other envelope, and a member with an
70
+ * identified sender or with no request record, is returned as it is.
71
+ */
72
+ withRequestedSender<E extends {
73
+ senderUserId: string;
74
+ senderDeviceId: number;
75
+ }>(envelope: E): E;
76
+ /**
77
+ * Ask the sender for a resend after a decrypt that failed. The request
78
+ * record is keyed by the failed envelope of the family. A member never
79
+ * starts a family of its own: it asks again for its failed envelope,
80
+ * under the retry ID of the record, and the fingerprint check of the
81
+ * failed envelope does not apply to it.
82
+ *
83
+ * @param fingerprint - The fingerprint of the received envelope.
84
+ * @param post - Posts the request for the given failure. It returns false
85
+ * when it posts nothing, for example under the rate limit.
86
+ * @returns True when `post` posted the request. After the post, a member
87
+ * is processed, so a redelivery of it on either path posts nothing. A
88
+ * failed post leaves it unprocessed. False with no post, and one warn
89
+ * line with the behavior RETRY_REQUEST_ENVELOPE_CHANGED, when the relay
90
+ * changed the failed envelope after its retry request.
91
+ */
92
+ requestRetry(fingerprint: string | undefined, post: (failed: RetryRequestFailure) => Promise<boolean>): Promise<boolean>;
93
+ }
94
+ /**
95
+ * Receive one envelope in the retry family of its failed envelope.
96
+ *
97
+ * A resend or a null message resolves to its failed envelope through the
98
+ * family index, by its SDK ID: the client message ID, or the envelope ID
99
+ * when the relay delivers no client message ID. A relay that assigns its
100
+ * own envelope IDs delivers the SDK ID as the client message ID. The
101
+ * receive then holds the processed-envelope lock of that failed envelope,
102
+ * so the fulfilled check, the decrypt and the fulfilled store of one
103
+ * family run one at a time on all paths. Any other envelope holds the lock
104
+ * of its own ID. An envelope with no ID holds no lock and has no family.
105
+ * The processed record and the acknowledgment use the delivered envelope
106
+ * ID.
107
+ *
108
+ * @param storage - The local store that keeps the retry records.
109
+ * @param logger - The client logger.
110
+ * @param envelope - The IDs of the received envelope: the delivered
111
+ * envelope ID, and the client message ID when the relay delivers one.
112
+ * @param receive - The receive path. It runs under the lock.
113
+ */
114
+ export declare function receiveInRetryFamily<T>(storage: RetryFamilyStore, logger: Required<Logger>, envelope: {
115
+ readonly id?: string;
116
+ readonly clientMessageId?: string;
117
+ }, receive: (family: RetryFamilyReceive) => Promise<T>): Promise<T>;
118
+ export {};
119
+ //# sourceMappingURL=retry-family.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retry-family.d.ts","sourceRoot":"","sources":["../../client/retry-family.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACxC,OAAO,EAOL,yBAAyB,EACzB,KAAK,yBAAyB,EAE/B,MAAM,4BAA4B,CAAC;AACpC,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAEpE,KAAK,gBAAgB,GAAG,UAAU,CAAC,OAAO,yBAAyB,CAAC,CAAC,CAAC,CAAC,CAAC;AAExE;;;;GAIG;AACH,MAAM,WAAW,kBAAkB;IACjC;;;;OAIG;IACH,QAAQ,CAAC,MAAM,EAAE,yBAAyB,GAAG,SAAS,CAAC;IACvD;;;;;;;;;OASG;IACH,KAAK,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IAC1B;;;;;;;;;OASG;IACH,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACzB;;;;;;;;;;;;;OAaG;IACH,WAAW,CAAC,WAAW,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5D;;;;;;OAMG;IACH,mBAAmB,CAAC,CAAC,SAAS;QAAE,YAAY,EAAE,MAAM,CAAC;QAAC,cAAc,EAAE,MAAM,CAAA;KAAE,EAC5E,QAAQ,EAAE,CAAC,GACV,CAAC,CAAC;IACL;;;;;;;;;;;;;;;OAeG;IACH,YAAY,CACV,WAAW,EAAE,MAAM,GAAG,SAAS,EAC/B,IAAI,EAAE,CAAC,MAAM,EAAE,mBAAmB,KAAK,OAAO,CAAC,OAAO,CAAC,GACtD,OAAO,CAAC,OAAO,CAAC,CAAC;CACrB;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAsB,oBAAoB,CAAC,CAAC,EAC1C,OAAO,EAAE,gBAAgB,EACzB,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,EACxB,QAAQ,EAAE;IAAE,QAAQ,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,CAAA;CAAE,EACrE,OAAO,EAAE,CAAC,MAAM,EAAE,kBAAkB,KAAK,OAAO,CAAC,CAAC,CAAC,GAClD,OAAO,CAAC,CAAC,CAAC,CAUZ"}
@@ -0,0 +1,170 @@
1
+ /**
2
+ * The retry family of a received envelope.
3
+ *
4
+ * A failed envelope, the request record that the requester keeps for it, and
5
+ * each resend or null message that answers that record form one retry
6
+ * family. The family index gives the failed envelope of a retry ID. Both
7
+ * receive paths, the relay subscription route and `processIncomingEnvelope`,
8
+ * receive each envelope through `receiveInRetryFamily`, so the application
9
+ * gets the content of one failed envelope at most once, from the envelope
10
+ * itself or from one family member.
11
+ */
12
+ import { getProcessedEnvelope, getRetryRequest, markRetryRequestAnswered, resolveRetryFamilyMember, RetryRequestedEnvelopeChangedError, storeProcessedEnvelope, withProcessedEnvelopeLock, } from '../local/store/reliability.js';
13
+ /**
14
+ * Receive one envelope in the retry family of its failed envelope.
15
+ *
16
+ * A resend or a null message resolves to its failed envelope through the
17
+ * family index, by its SDK ID: the client message ID, or the envelope ID
18
+ * when the relay delivers no client message ID. A relay that assigns its
19
+ * own envelope IDs delivers the SDK ID as the client message ID. The
20
+ * receive then holds the processed-envelope lock of that failed envelope,
21
+ * so the fulfilled check, the decrypt and the fulfilled store of one
22
+ * family run one at a time on all paths. Any other envelope holds the lock
23
+ * of its own ID. An envelope with no ID holds no lock and has no family.
24
+ * The processed record and the acknowledgment use the delivered envelope
25
+ * ID.
26
+ *
27
+ * @param storage - The local store that keeps the retry records.
28
+ * @param logger - The client logger.
29
+ * @param envelope - The IDs of the received envelope: the delivered
30
+ * envelope ID, and the client message ID when the relay delivers one.
31
+ * @param receive - The receive path. It runs under the lock.
32
+ */
33
+ export async function receiveInRetryFamily(storage, logger, envelope, receive) {
34
+ const envelopeId = envelope.id;
35
+ if (!envelopeId)
36
+ return receive(retryFamilyReceive(storage, logger, undefined, undefined));
37
+ const member = await resolveRetryFamilyMember(storage, envelope.clientMessageId ?? envelopeId);
38
+ return withProcessedEnvelopeLock(storage, member?.failedEnvelopeId ?? envelopeId, () => receive(retryFamilyReceive(storage, logger, envelopeId, member)));
39
+ }
40
+ function retryFamilyReceive(storage, logger, envelopeId, member) {
41
+ const failedEnvelopeId = member?.failedEnvelopeId ?? envelopeId;
42
+ // The read comes before the decrypt, so only writes follow the
43
+ // application hook.
44
+ let request = null;
45
+ return {
46
+ member,
47
+ async admit() {
48
+ if (failedEnvelopeId === undefined)
49
+ return true;
50
+ // A member that posted a request is processed, so its redelivery asks
51
+ // for nothing again.
52
+ if (envelopeId && (await getProcessedEnvelope(storage, envelopeId))) {
53
+ logger.debug('Acknowledging previously processed relay envelope', {
54
+ category: 'E2EE',
55
+ data: { envelopeId, behavior: 'PERSISTENT_DUPLICATE_DISCARD' },
56
+ });
57
+ return false;
58
+ }
59
+ // The Relay frees a message ID after the acknowledgment, so a sender
60
+ // can post a second resend attempt for one failed envelope, and the
61
+ // failed envelope can arrive again after its resend. Process one.
62
+ if (member && (await getProcessedEnvelope(storage, failedEnvelopeId))?.fulfilled) {
63
+ logger.debug('Acknowledging a second envelope for one failed envelope', {
64
+ category: 'E2EE',
65
+ data: { envelopeId, behavior: 'RETRY_RESEND_DUPLICATE_DISCARD' },
66
+ });
67
+ return false;
68
+ }
69
+ // A member of attempt n answers attempt n, so a member failure then
70
+ // posts the next attempt. A member of an earlier attempt answers
71
+ // nothing, so its failure posts the stored attempt again.
72
+ if (member) {
73
+ await markRetryRequestAnswered(storage, failedEnvelopeId, member.retryId, member.attempt);
74
+ }
75
+ request = await getRetryRequest(storage, failedEnvelopeId);
76
+ return true;
77
+ },
78
+ async fulfill() {
79
+ try {
80
+ // One write marks the failed envelope processed and fulfilled. A
81
+ // redelivery of it is then acknowledged with no decrypt, and each
82
+ // later member of its request is dropped.
83
+ if (failedEnvelopeId !== undefined && (member || request)) {
84
+ await storeProcessedEnvelope(storage, failedEnvelopeId, Date.now(), undefined, true);
85
+ }
86
+ }
87
+ catch (error) {
88
+ // The application gets the plaintext. Only a redelivery can
89
+ // decrypt the content again.
90
+ logger.warn('The retry family record of a decrypted envelope failed to store', {
91
+ category: 'E2EE',
92
+ data: {
93
+ envelopeId,
94
+ behavior: 'RETRY_FULFILL_WRITE_FAILED',
95
+ error: error.message,
96
+ },
97
+ });
98
+ }
99
+ },
100
+ async consumeNull(fingerprint) {
101
+ try {
102
+ if (member)
103
+ await storeProcessedEnvelope(storage, member.failedEnvelopeId);
104
+ if (envelopeId) {
105
+ await storeProcessedEnvelope(storage, envelopeId, Date.now(), fingerprint);
106
+ }
107
+ }
108
+ catch (error) {
109
+ // The null message carries no content, so the caller still
110
+ // consumes it. A redelivery of the failed envelope can then
111
+ // decrypt the content.
112
+ logger.warn('The retry family record of a null message failed to store', {
113
+ category: 'E2EE',
114
+ data: {
115
+ envelopeId,
116
+ behavior: 'RETRY_NULL_WRITE_FAILED',
117
+ error: error.message,
118
+ },
119
+ });
120
+ }
121
+ },
122
+ withRequestedSender(envelope) {
123
+ if (!member || !request)
124
+ return envelope;
125
+ if (envelope.senderUserId && envelope.senderDeviceId >= 1)
126
+ return envelope;
127
+ const { targetUserId, targetDeviceId } = request.envelope;
128
+ return { ...envelope, senderUserId: targetUserId, senderDeviceId: targetDeviceId };
129
+ },
130
+ async requestRetry(fingerprint, post) {
131
+ if (failedEnvelopeId === undefined)
132
+ return post({ stored: null });
133
+ if (!member) {
134
+ let stored;
135
+ try {
136
+ stored = await getRetryRequest(storage, failedEnvelopeId, Date.now(), fingerprint);
137
+ }
138
+ catch (error) {
139
+ if (!(error instanceof RetryRequestedEnvelopeChangedError))
140
+ throw error;
141
+ // The envelope stays unacknowledged, as for a failed retry request.
142
+ logger.warn('No retry request: the relay changed an envelope after its retry request', {
143
+ category: 'E2EE',
144
+ data: { envelopeId, behavior: 'RETRY_REQUEST_ENVELOPE_CHANGED' },
145
+ });
146
+ return false;
147
+ }
148
+ return post({
149
+ id: failedEnvelopeId,
150
+ ...(fingerprint !== undefined && { fingerprint }),
151
+ stored,
152
+ });
153
+ }
154
+ // A member has its own content, so its fingerprint is not the
155
+ // fingerprint of the failed envelope.
156
+ const stored = await getRetryRequest(storage, failedEnvelopeId);
157
+ const posted = await post({
158
+ id: failedEnvelopeId,
159
+ ...(stored?.fingerprint !== undefined && { fingerprint: stored.fingerprint }),
160
+ stored,
161
+ ...(envelopeId !== undefined && { member: envelopeId }),
162
+ });
163
+ if (posted && envelopeId) {
164
+ await storeProcessedEnvelope(storage, envelopeId, Date.now(), fingerprint);
165
+ }
166
+ return posted;
167
+ },
168
+ };
169
+ }
170
+ //# sourceMappingURL=retry-family.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retry-family.js","sourceRoot":"","sources":["../../client/retry-family.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAGH,OAAO,EACL,oBAAoB,EACpB,eAAe,EACf,wBAAwB,EACxB,wBAAwB,EACxB,kCAAkC,EAClC,sBAAsB,EACtB,yBAAyB,GAG1B,MAAM,4BAA4B,CAAC;AAsFpC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACxC,OAAyB,EACzB,MAAwB,EACxB,QAAqE,EACrE,OAAmD;IAEnD,MAAM,UAAU,GAAG,QAAQ,CAAC,EAAE,CAAC;IAC/B,IAAI,CAAC,UAAU;QAAE,OAAO,OAAO,CAAC,kBAAkB,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC;IAC3F,MAAM,MAAM,GAAG,MAAM,wBAAwB,CAC3C,OAAO,EACP,QAAQ,CAAC,eAAe,IAAI,UAAU,CACvC,CAAC;IACF,OAAO,yBAAyB,CAAC,OAAO,EAAE,MAAM,EAAE,gBAAg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AW,KAAK,SAAS,IAAI,EAAE,WAAW,EAAE,CAAC;oBACjD,MAAM;iBACP,CAAC,CAAC;YACL,CAAC;YACD,8DAA8D;YAC9D,sCAAsC;YACtC,MAAM,MAAM,GAAG,MAAM,eAAe,CAAC,OAAO,EAAE,gBAAgB,CAAC,CAAC;YAChE,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC;gBACxB,EAAE,EAAE,gBAAgB;gBACpB,GAAG,CAAC,MAAM,EAAE,WAAW,KAAK,SAAS,IAAI,EAAE,WAAW,EAAE,MAAM,CAAC,WAAW,EAAE,CAAC;gBAC7E,MAAM;gBACN,GAAG,CAAC,UAAU,KAAK,SAAS,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC;aACxD,CAAC,CAAC;YACH,IAAI,MAAM,IAAI,UAAU,EAAE,CAAC;gBACzB,MAAM,sBAAsB,CAAC,OAAO,EAAE,UAAU,EAAE,IAAI,CAAC,GAAG,EAAE,EAAE,WAAW,CAAC,CAAC;YAC7E,CAAC;YACD,OAAO,MAAM,CAAC;QAChB,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,106 @@
1
+ /**
2
+ * Wire form of a retry request.
3
+ *
4
+ * A retry request travels as an identified mailbox envelope with
5
+ * `messageType: 'retry_request'`. The envelope sender is the requester: the
6
+ * relay stamps it, so the payload never carries it. The `ciphertext` field
7
+ * holds canonical base64 of a small UTF-8 JSON payload. The payload is not
8
+ * encrypted. It holds only what the requester knows about the failed message.
9
+ *
10
+ * The requester keeps one request record for each failed envelope. The record
11
+ * holds a random retry ID, and every wire ID of the request family derives from
12
+ * it: `<retryId>:retry-request`, `<retryId>:resend` and `<retryId>:null`, with
13
+ * `:<n>` from attempt 2. The resend and the null message carry the attempt
14
+ * number of the request that they answer. The failed envelope ID travels
15
+ * only in the payload.
16
+ * The requester stores each request envelope before the post, and posts the
17
+ * stored envelope again on a later decrypt failure of the same envelope.
18
+ */
19
+ import type { Envelope } from '../remote/relay/types.js';
20
+ import { type RetryRequest } from '../internal/sesame/types.js';
21
+ import { storeRetryRequest, type RetryRequestIds, type StoredRetryRequest } from '../local/store/reliability.js';
22
+ /** The decoder refuses a longer `ciphertext` value before it decodes it. */
23
+ export declare const MAX_RETRY_REQUEST_CIPHERTEXT_LENGTH = 2048;
24
+ /** A user and one of its devices. */
25
+ export interface RetryRequestDevice {
26
+ userId: string;
27
+ deviceId: number;
28
+ }
29
+ /**
30
+ * The result of `readRetryRequestEnvelope`. `retry` holds the ID of the
31
+ * envelope that the requester could not decrypt and the retry ID of its
32
+ * request record, when the requester knew the failed envelope. The resend
33
+ * IDs of the request derive from the retry ID.
34
+ */
35
+ export type RetryRequestEnvelopeRead = {
36
+ ok: true;
37
+ request: RetryRequest;
38
+ retry?: RetryRequestIds;
39
+ } | {
40
+ ok: false;
41
+ cause: string;
42
+ };
43
+ /** The failed envelope that a retry request names, and its stored request. */
44
+ export interface RetryRequestFailure {
45
+ /** The Relay message ID of the failed envelope. Without it, nothing is stored. */
46
+ id?: string;
47
+ /** The fingerprint of the failed envelope. */
48
+ fingerprint?: string;
49
+ /** The stored attempt of the request for this envelope, from `getRetryRequest`. */
50
+ stored: StoredRetryRequest | null;
51
+ /**
52
+ * The envelope ID of the resend or the null message that failed to
53
+ * decrypt. Undefined when the failed envelope itself failed.
54
+ */
55
+ member?: string;
56
+ }
57
+ /**
58
+ * Make the envelope that asks the original sender device for a resend.
59
+ *
60
+ * @param request - The retry request. Its original sender is the target.
61
+ * @param sender - The local identity, which sends the envelope.
62
+ * @param failed - The ID of the failed envelope, the retry ID of its request
63
+ * record and the attempt number. The client message ID derives from the
64
+ * retry ID and the attempt. Without them, it is a new UUID.
65
+ */
66
+ export declare function retryRequestEnvelope(request: RetryRequest, sender: RetryRequestDevice, failed?: RetryRequestIds & {
67
+ attempt: number;
68
+ }): Promise<Envelope>;
69
+ /**
70
+ * Post the retry request for one failed envelope.
71
+ *
72
+ * Without a failed envelope ID, it creates a request and posts it under a
73
+ * new UUID, and stores nothing. With a stored attempt, it posts the stored
74
+ * envelope again. Otherwise it creates attempt 1 with a new retry ID and
75
+ * stores it before the post. Each later attempt keeps the retry ID. When
76
+ * the Relay refuses a stored attempt with OPERATION_EXPIRED, or when a
77
+ * member failed and the stored attempt is answered, it creates the next
78
+ * attempt, replaces the record in one write, and posts it. Each new
79
+ * attempt is not answered. A RETRY_CONFLICT answer to any post completes
80
+ * the attempt, because the Relay holds an earlier encryption of this
81
+ * attempt.
82
+ *
83
+ * @param store - The local store that keeps the request records.
84
+ * @param failed - The failed envelope and its stored request.
85
+ * @param createRequest - Creates the request of a new attempt. Only a new
86
+ * attempt calls it.
87
+ * @param sender - The local identity, which sends the envelope.
88
+ * @param send - Posts one request envelope.
89
+ */
90
+ export declare function postRetryRequest(store: Parameters<typeof storeRetryRequest>[0], failed: RetryRequestFailure, createRequest: () => Promise<RetryRequest>, sender: RetryRequestDevice, send: (envelope: Envelope) => Promise<unknown>): Promise<void>;
91
+ /**
92
+ * Read a `retry_request` envelope that the local device received.
93
+ *
94
+ * The requester is the envelope sender. The original sender is the local
95
+ * identity. The other fields come from the payload. A `Uint8Array`
96
+ * ciphertext is read as its base64 form. The function ignores
97
+ * every payload key that it does not read, a requester key included. It does
98
+ * not check the envelope sender: the caller does that first.
99
+ *
100
+ * @param envelope - The received envelope.
101
+ * @param local - The local identity.
102
+ * @returns The retry request, or the cause of the refusal. The cause never
103
+ * holds payload bytes.
104
+ */
105
+ export declare function readRetryRequestEnvelope(envelope: Envelope, local: RetryRequestDevice): RetryRequestEnvelopeRead;
106
+ //# sourceMappingURL=retry-request-envelope.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retry-request-envelope.d.ts","sourceRoot":"","sources":["../../client/retry-request-envelope.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAe,KAAK,YAAY,EAAE,MAAM,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