lambder 7.3.1 → 8.1.1

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 (237) hide show
  1. package/CHANGELOG.md +1047 -3
  2. package/README.md +46 -21
  3. package/dist/api/LambderApiAnswer.d.ts +18 -22
  4. package/dist/api/LambderApiAnswer.js +6 -7
  5. package/dist/api/LambderApiCallContext.d.ts +21 -8
  6. package/dist/api/LambderApiCallContext.js +22 -4
  7. package/dist/api/LambderApiDefinition.d.ts +4 -3
  8. package/dist/api/LambderApiEnvelope.d.ts +14 -9
  9. package/dist/api/LambderApiEnvelope.js +33 -34
  10. package/dist/api/LambderApiGuards.d.ts +78 -51
  11. package/dist/api/LambderApiGuards.js +34 -36
  12. package/dist/api/LambderApiIdempotency.d.ts +68 -62
  13. package/dist/api/LambderApiIdempotency.js +214 -151
  14. package/dist/api/LambderApiOutputValidationError.d.ts +32 -0
  15. package/dist/api/LambderApiOutputValidationError.js +50 -0
  16. package/dist/api/LambderApiPipeline.d.ts +47 -38
  17. package/dist/api/LambderApiPipeline.js +122 -63
  18. package/dist/api/LambderApiRateLimits.d.ts +201 -54
  19. package/dist/api/LambderApiRateLimits.js +185 -108
  20. package/dist/api/LambderApiRequest.d.ts +27 -21
  21. package/dist/api/LambderApiRequest.js +26 -19
  22. package/dist/api/LambderApiSignature.d.ts +12 -15
  23. package/dist/api/LambderApiSignature.js +28 -51
  24. package/dist/api/LambderApiValidationRefusal.d.ts +9 -9
  25. package/dist/api/LambderApiValidationRefusal.js +10 -10
  26. package/dist/build/ContractTypePrinter.d.ts +85 -0
  27. package/dist/build/ContractTypePrinter.js +402 -0
  28. package/dist/build/freshProcessVerifier.d.ts +13 -0
  29. package/dist/build/freshProcessVerifier.js +19 -0
  30. package/dist/build/moduleLocation.d.ts +11 -0
  31. package/dist/build/moduleLocation.js +6 -0
  32. package/dist/build/writeApiContract.d.ts +78 -0
  33. package/dist/build/writeApiContract.js +302 -0
  34. package/dist/build/writeApiSignatures.d.ts +114 -0
  35. package/dist/build/writeApiSignatures.js +217 -0
  36. package/dist/build/writeFileAtomically.d.ts +8 -0
  37. package/dist/build/writeFileAtomically.js +22 -0
  38. package/dist/build.d.ts +14 -0
  39. package/dist/build.js +11 -0
  40. package/dist/client/LambderCaller.d.ts +13 -44
  41. package/dist/client/LambderCaller.js +77 -84
  42. package/dist/client/LambderReloadLoopBreaker.d.ts +56 -26
  43. package/dist/client/LambderReloadLoopBreaker.js +90 -46
  44. package/dist/client/LambderUploadRunner.d.ts +96 -0
  45. package/dist/client/LambderUploadRunner.js +234 -0
  46. package/dist/client/lambderFetchTransport.d.ts +4 -1
  47. package/dist/client/lambderFetchTransport.js +52 -28
  48. package/dist/client.d.ts +9 -3
  49. package/dist/client.js +6 -1
  50. package/dist/core/Lambder.d.ts +143 -79
  51. package/dist/core/Lambder.js +350 -231
  52. package/dist/core/LambderContext.d.ts +82 -15
  53. package/dist/core/LambderContext.js +107 -20
  54. package/dist/core/LambderCors.d.ts +21 -3
  55. package/dist/core/LambderCors.js +35 -16
  56. package/dist/core/LambderCrashHandling.d.ts +40 -0
  57. package/dist/core/LambderCrashHandling.js +97 -0
  58. package/dist/core/LambderCreateOptions.d.ts +151 -75
  59. package/dist/core/LambderCreateOptions.js +16 -23
  60. package/dist/core/LambderFiles.d.ts +21 -7
  61. package/dist/core/LambderFiles.js +62 -34
  62. package/dist/core/LambderIndexHtml.js +12 -11
  63. package/dist/core/LambderPolicyBuilders.d.ts +17 -5
  64. package/dist/core/LambderPolicyBuilders.js +17 -5
  65. package/dist/core/LambderPublicFiles.d.ts +11 -5
  66. package/dist/core/LambderPublicFiles.js +32 -4
  67. package/dist/core/LambderRequestPath.d.ts +43 -0
  68. package/dist/core/LambderRequestPath.js +63 -0
  69. package/dist/core/LambderResponse.d.ts +26 -5
  70. package/dist/core/LambderResponse.js +157 -70
  71. package/dist/core/LambderResponseBuilder.d.ts +49 -4
  72. package/dist/core/LambderResponseBuilder.js +64 -3
  73. package/dist/core/LambderRouting.d.ts +2 -3
  74. package/dist/core/LambderRouting.js +22 -7
  75. package/dist/core/LambderTemplatingEngine.js +211 -32
  76. package/dist/index.d.ts +25 -8
  77. package/dist/index.js +13 -4
  78. package/dist/invoke/LambderInvokeCaller.d.ts +37 -42
  79. package/dist/invoke/LambderInvokeCaller.js +76 -66
  80. package/dist/invoke/LambderInvokeOutcome.d.ts +27 -26
  81. package/dist/invoke/LambderInvokeOutcome.js +9 -22
  82. package/dist/invoke/LambderLambdaEvent.d.ts +29 -9
  83. package/dist/invoke/LambderLambdaEvent.js +40 -22
  84. package/dist/invoke/lambderHandlerTransport.d.ts +9 -10
  85. package/dist/invoke/lambderHandlerTransport.js +15 -18
  86. package/dist/mock/LambderMockApp.d.ts +67 -83
  87. package/dist/mock/LambderMockApp.js +167 -153
  88. package/dist/mock/LambderMockBrowserCookies.d.ts +24 -28
  89. package/dist/mock/LambderMockBrowserCookies.js +24 -28
  90. package/dist/mock/LambderMockCallRecorder.d.ts +15 -22
  91. package/dist/mock/LambderMockCallRecorder.js +19 -28
  92. package/dist/mock/LambderMockCreateOptions.d.ts +42 -24
  93. package/dist/mock/LambderMockEntryRegistry.d.ts +11 -12
  94. package/dist/mock/LambderMockEntryRegistry.js +24 -29
  95. package/dist/mock/LambderMockFailureInjector.d.ts +3 -6
  96. package/dist/mock/LambderMockFailureInjector.js +3 -6
  97. package/dist/mock/LambderMockTypes.d.ts +78 -108
  98. package/dist/mock/lambderMockInvokeTransport.d.ts +11 -13
  99. package/dist/mock/lambderMockInvokeTransport.js +11 -10
  100. package/dist/mock/lambderMockMswHandler.d.ts +43 -33
  101. package/dist/mock/lambderMockMswHandler.js +50 -39
  102. package/dist/mock/lambderMockUploadMswHandler.d.ts +26 -0
  103. package/dist/mock/lambderMockUploadMswHandler.js +28 -0
  104. package/dist/mock.d.ts +4 -1
  105. package/dist/mock.js +6 -3
  106. package/dist/session/LambderSessionController.d.ts +108 -89
  107. package/dist/session/LambderSessionController.js +187 -168
  108. package/dist/session/LambderSessionCrypto.d.ts +16 -7
  109. package/dist/session/LambderSessionCrypto.js +26 -12
  110. package/dist/session/LambderSessionManager.d.ts +124 -46
  111. package/dist/session/LambderSessionManager.js +262 -137
  112. package/dist/shared/LambderHtml.d.ts +42 -3
  113. package/dist/shared/LambderHtml.js +127 -7
  114. package/dist/shared/LambderHtmlPositions.d.ts +173 -0
  115. package/dist/shared/LambderHtmlPositions.js +652 -0
  116. package/dist/shared/LambderI18n.d.ts +10 -11
  117. package/dist/shared/LambderI18n.js +33 -21
  118. package/dist/shared/contracts/LambderCache.d.ts +66 -0
  119. package/dist/shared/contracts/LambderCache.js +11 -0
  120. package/dist/shared/contracts/LambderFileSource.d.ts +6 -6
  121. package/dist/shared/contracts/LambderFileSource.js +5 -8
  122. package/dist/shared/contracts/LambderIdempotencyStore.d.ts +51 -22
  123. package/dist/shared/contracts/LambderIdempotencyStore.js +4 -5
  124. package/dist/shared/contracts/LambderRateLimiter.d.ts +27 -15
  125. package/dist/shared/contracts/LambderRateLimiter.js +4 -5
  126. package/dist/shared/contracts/LambderSessionStore.d.ts +65 -26
  127. package/dist/shared/contracts/LambderSessionStore.js +5 -6
  128. package/dist/shared/contracts/LambderUploadBucket.d.ts +154 -0
  129. package/dist/shared/contracts/LambderUploadBucket.js +74 -0
  130. package/dist/shared/transport/LambderApiTransport.d.ts +27 -27
  131. package/dist/shared/transport/LambderApiTransport.js +7 -7
  132. package/dist/shared/transport/LambderCookieJar.d.ts +28 -35
  133. package/dist/shared/transport/LambderCookieJar.js +54 -66
  134. package/dist/shared/transport/lambderCookieJarTransport.d.ts +11 -13
  135. package/dist/shared/transport/lambderCookieJarTransport.js +24 -23
  136. package/dist/shared/util/LambderCallAbort.d.ts +5 -5
  137. package/dist/shared/util/LambderCallAbort.js +5 -5
  138. package/dist/shared/util/LambderClientIp.d.ts +27 -11
  139. package/dist/shared/util/LambderClientIp.js +96 -13
  140. package/dist/shared/util/LambderContentDisposition.d.ts +10 -0
  141. package/dist/shared/util/LambderContentDisposition.js +13 -0
  142. package/dist/shared/util/LambderExpiringMap.d.ts +35 -49
  143. package/dist/shared/util/LambderExpiringMap.js +41 -57
  144. package/dist/shared/util/LambderNodeModules.js +6 -7
  145. package/dist/shared/util/LambderOptionChecks.d.ts +4 -4
  146. package/dist/shared/util/LambderOptionChecks.js +4 -4
  147. package/dist/shared/util/LambderResponseBrand.d.ts +5 -5
  148. package/dist/shared/util/LambderResponseBrand.js +5 -5
  149. package/dist/shared/util/LambderTextDigest.d.ts +7 -5
  150. package/dist/shared/util/LambderTextDigest.js +11 -5
  151. package/dist/shared/util/LambderTypeUtilities.d.ts +7 -8
  152. package/dist/shared/util/LambderTypeUtilities.js +3 -3
  153. package/dist/shared/util/boundKeyField.d.ts +20 -0
  154. package/dist/shared/util/boundKeyField.js +34 -0
  155. package/dist/shared/util/canonicalJson.d.ts +11 -0
  156. package/dist/shared/util/canonicalJson.js +28 -0
  157. package/dist/shared/util/joinKeyFields.d.ts +20 -0
  158. package/dist/shared/util/joinKeyFields.js +22 -0
  159. package/dist/shared/wire/LambderAnswerHeaders.d.ts +12 -16
  160. package/dist/shared/wire/LambderAnswerHeaders.js +12 -16
  161. package/dist/shared/wire/LambderApiContract.d.ts +98 -53
  162. package/dist/shared/wire/LambderApiOutcome.d.ts +43 -31
  163. package/dist/shared/wire/LambderApiOutcome.js +48 -23
  164. package/dist/shared/wire/LambderApiRefusal.d.ts +45 -27
  165. package/dist/shared/wire/LambderApiRefusal.js +42 -7
  166. package/dist/shared/wire/LambderApiSignature.d.ts +18 -22
  167. package/dist/shared/wire/LambderApiSignature.js +16 -19
  168. package/dist/shared/wire/LambderCallOptions.d.ts +38 -47
  169. package/dist/shared/wire/LambderCallOptions.js +9 -11
  170. package/dist/shared/wire/LambderCompressionCodec.d.ts +29 -34
  171. package/dist/shared/wire/LambderCompressionCodec.js +31 -36
  172. package/dist/shared/wire/LambderCompressionOption.d.ts +9 -9
  173. package/dist/shared/wire/LambderCompressionOption.js +9 -9
  174. package/dist/shared/wire/LambderCrashDetail.d.ts +12 -15
  175. package/dist/shared/wire/LambderCrashDetail.js +12 -15
  176. package/dist/shared/wire/LambderDefaultApiPath.d.ts +6 -0
  177. package/dist/shared/wire/LambderDefaultApiPath.js +6 -0
  178. package/dist/shared/wire/LambderHttpStatus.d.ts +6 -7
  179. package/dist/shared/wire/LambderIdempotencyKeyScope.d.ts +89 -0
  180. package/dist/shared/wire/LambderIdempotencyKeyScope.js +146 -0
  181. package/dist/shared/wire/LambderInvokeApiId.d.ts +27 -0
  182. package/dist/shared/wire/LambderInvokeApiId.js +27 -0
  183. package/dist/shared/wire/LambderOutcomeAssertions.d.ts +6 -7
  184. package/dist/shared/wire/LambderOutcomeAssertions.js +6 -7
  185. package/dist/shared/wire/LambderRequestPayload.d.ts +18 -20
  186. package/dist/shared/wire/LambderRequestPayload.js +4 -6
  187. package/dist/shared/wire/LambderUploadObjectFields.d.ts +10 -0
  188. package/dist/shared/wire/LambderUploadObjectFields.js +24 -0
  189. package/dist/shared/wire/LambderUploadRefusal.d.ts +9 -0
  190. package/dist/shared/wire/LambderUploadRefusal.js +18 -0
  191. package/dist/shared/wire/LambderUploadSchemas.d.ts +12 -0
  192. package/dist/shared/wire/LambderUploadSchemas.js +30 -0
  193. package/dist/stores/LambderCacheFiller.d.ts +48 -0
  194. package/dist/stores/LambderCacheFiller.js +119 -0
  195. package/dist/stores/LambderCacheKeys.d.ts +26 -0
  196. package/dist/stores/LambderCacheKeys.js +54 -0
  197. package/dist/stores/LambderCacheValues.d.ts +45 -0
  198. package/dist/stores/LambderCacheValues.js +74 -0
  199. package/dist/stores/LambderDdbCache.d.ts +121 -56
  200. package/dist/stores/LambderDdbCache.js +528 -225
  201. package/dist/stores/LambderDdbIdempotencyStore.d.ts +33 -22
  202. package/dist/stores/LambderDdbIdempotencyStore.js +75 -50
  203. package/dist/stores/LambderDdbRateLimiter.d.ts +76 -20
  204. package/dist/stores/LambderDdbRateLimiter.js +151 -39
  205. package/dist/stores/LambderDdbSdk.d.ts +43 -31
  206. package/dist/stores/LambderDdbSdk.js +80 -38
  207. package/dist/stores/LambderDdbSessionStore.d.ts +27 -14
  208. package/dist/stores/LambderDdbSessionStore.js +119 -47
  209. package/dist/stores/LambderHttpFileSource.d.ts +15 -6
  210. package/dist/stores/LambderHttpFileSource.js +15 -13
  211. package/dist/stores/LambderMemoryCache.d.ts +49 -0
  212. package/dist/stores/LambderMemoryCache.js +113 -0
  213. package/dist/stores/LambderMemoryIdempotencyStore.d.ts +13 -12
  214. package/dist/stores/LambderMemoryIdempotencyStore.js +31 -30
  215. package/dist/stores/LambderMemoryRateLimiter.d.ts +8 -9
  216. package/dist/stores/LambderMemoryRateLimiter.js +14 -13
  217. package/dist/stores/LambderMemorySessionStore.d.ts +14 -11
  218. package/dist/stores/LambderMemorySessionStore.js +38 -19
  219. package/dist/stores/LambderMemoryUploadBucket.d.ts +99 -0
  220. package/dist/stores/LambderMemoryUploadBucket.js +219 -0
  221. package/dist/stores/LambderS3FileSource.d.ts +21 -6
  222. package/dist/stores/LambderS3FileSource.js +12 -7
  223. package/dist/stores/LambderS3UploadBucket.d.ts +73 -0
  224. package/dist/stores/LambderS3UploadBucket.js +144 -0
  225. package/dist/stores/LambderSdkInstallHint.d.ts +11 -0
  226. package/dist/stores/LambderSdkInstallHint.js +14 -0
  227. package/dist/testing/LambderTestApp.d.ts +23 -25
  228. package/dist/testing/LambderTestApp.js +22 -24
  229. package/dist/testing/LambderTestVisitor.d.ts +10 -12
  230. package/dist/testing/LambderTestVisitor.js +15 -15
  231. package/dist/testing.d.ts +3 -0
  232. package/dist/testing.js +2 -0
  233. package/package.json +26 -3
  234. package/dist/api/LambderApiPolicyEngine.d.ts +0 -47
  235. package/dist/api/LambderApiPolicyEngine.js +0 -85
  236. package/dist/shared/util/LambderKeyFields.d.ts +0 -32
  237. package/dist/shared/util/LambderKeyFields.js +0 -34
@@ -10,14 +10,12 @@ import { LambderExpiringMap, LambderExpiringMapFullError } from "../shared/util/
10
10
  * path can be exercised; unbounded by default. `now` is injectable so a test
11
11
  * can expire a claim without waiting.
12
12
  *
13
- * `maxEntries` is the map's ceiling, 100,000 by default, and it is the one
14
- * way this store differs from a table: a process cannot hold records without
15
- * bound, so past the ceiling the SETTLED records are dropped, soonest expiry
16
- * first, and a retry whose record was dropped executes again instead of
17
- * replaying. Pending claims are never dropped for room, because losing one
18
- * lets two concurrent retries execute at once, which is the thing idempotency
19
- * exists to prevent; a claim that cannot be made room for is reported as
20
- * "pending" instead, so the duplicate is refused rather than run.
13
+ * `maxEntries` (100,000 by default) is the one way this store differs from a
14
+ * table: past the ceiling, SETTLED records are dropped, soonest expiry first,
15
+ * and a retry whose record was dropped executes again instead of replaying.
16
+ * Pending claims are never dropped, since losing one lets two concurrent
17
+ * retries both execute; a claim that finds no room is reported as "pending"
18
+ * instead, so the duplicate is refused rather than run.
21
19
  */
22
20
  export class LambderMemoryIdempotencyStore {
23
21
  records;
@@ -34,7 +32,7 @@ export class LambderMemoryIdempotencyStore {
34
32
  nowSeconds() { return Math.floor(this.now() / 1000); }
35
33
  /** A settled record as the engine reads it: a copy, so a caller writing onto what it got back cannot rewrite the record. */
36
34
  static answerOf(record) {
37
- return { statusCode: record.statusCode, headers: structuredClone(record.headers), body: record.body };
35
+ return { statusCode: record.statusCode, headers: structuredClone(record.headers), body: record.body, fingerprint: record.fingerprint };
38
36
  }
39
37
  async peek(scopeKey) {
40
38
  const record = this.records.get(scopeKey);
@@ -42,43 +40,45 @@ export class LambderMemoryIdempotencyStore {
42
40
  return null;
43
41
  return LambderMemoryIdempotencyStore.answerOf(record);
44
42
  }
45
- async begin(scopeKey, { pendingTtlSeconds }) {
43
+ async begin(scopeKey, { pendingTtlSeconds, fingerprint }) {
46
44
  const existing = this.records.get(scopeKey);
47
45
  if (existing) {
48
46
  if (existing.state === "done")
49
47
  return { state: "done", ...LambderMemoryIdempotencyStore.answerOf(existing) };
50
- return { state: "pending" };
48
+ return { state: "pending", fingerprint: existing.fingerprint };
51
49
  }
52
50
  this.ownerCounter += 1;
53
51
  const ownerToken = `owner-${this.ownerCounter}`;
54
52
  try {
55
- // Not evictable: the claim is the only thing standing between two
56
- // concurrent retries and two executions, and it is also the entry
57
- // the soonest-expiry rule would otherwise take first, since a
58
- // claim lives for minutes and the record it becomes for a day.
59
- this.records.set(scopeKey, { state: "pending", ownerToken }, this.nowSeconds() + pendingTtlSeconds, { evictable: false });
53
+ // Not evictable: the claim is all that stands between two
54
+ // concurrent retries and two executions, and the soonest-expiry
55
+ // rule would take it first (a claim lives minutes, a record a day).
56
+ this.records.set(scopeKey, { state: "pending", ownerToken, fingerprint }, this.nowSeconds() + pendingTtlSeconds, { evictable: false });
60
57
  }
61
58
  catch (error) {
62
59
  if (!(error instanceof LambderExpiringMapFullError))
63
60
  throw error;
64
- // Every record held is a live claim, so there is no room and
65
- // nothing safe to take. "pending" is the interface's shape for
66
- // "the claim is not yours": the caller refuses the duplicate,
67
- // which is the answer that cannot execute anything twice, and a
68
- // retry once the flood drains claims normally.
61
+ // Every record held is a live claim, so nothing is safe to take.
62
+ // "pending" means "the claim is not yours": the caller refuses the
63
+ // duplicate, which cannot execute anything twice, and a retry
64
+ // after the flood drains claims normally. It carries the caller's
65
+ // own fingerprint, so the engine answers the in-flight 409 and the
66
+ // client retries under the same key; any other fingerprint would
67
+ // be the key-reused 409, which moves a client's key scope on as
68
+ // though the operation were settled.
69
69
  if (!this.claimCeilingReported) {
70
70
  this.claimCeilingReported = true;
71
71
  console.error("LambderMemoryIdempotencyStore: every record held is a live claim, so new requests are refused as duplicates until one settles. Raise maxEntries or move to LambderDdbIdempotencyStore.");
72
72
  }
73
- return { state: "pending" };
73
+ return { state: "pending", fingerprint };
74
74
  }
75
75
  this.claimCeilingReported = false;
76
76
  return { state: "new", ownerToken };
77
77
  }
78
- async complete(scopeKey, ownerToken, { statusCode, headers, body, ttlSeconds }) {
79
- // Size first, ownership second, the order LambderDdbIdempotencyStore works
80
- // in: it settles with one conditional write, so the only thing it can
81
- // decide before going to the table is whether the body fits.
78
+ async complete(scopeKey, ownerToken, { statusCode, headers, body, fingerprint, ttlSeconds }) {
79
+ // Size first, ownership second, as LambderDdbIdempotencyStore must:
80
+ // it settles with one conditional write, so whether the body fits is
81
+ // all it can decide before going to the table.
82
82
  if (new TextEncoder().encode(body).length > this.maxBodyBytes)
83
83
  return "too-large";
84
84
  const existing = this.records.get(scopeKey);
@@ -87,18 +87,19 @@ export class LambderMemoryIdempotencyStore {
87
87
  // Settled records ARE evictable: the claim's job is done, and a record
88
88
  // the ceiling drops costs a retry its replay, not its exclusivity.
89
89
  // The key is already held, so this write cannot cross the ceiling.
90
- this.records.set(scopeKey, { state: "done", ownerToken, statusCode, headers: structuredClone(headers), body }, this.nowSeconds() + ttlSeconds);
90
+ this.records.set(scopeKey, { state: "done", ownerToken, statusCode, headers: structuredClone(headers), body, fingerprint }, this.nowSeconds() + ttlSeconds);
91
91
  return "stored";
92
92
  }
93
+ /** Releases a pending claim for its owner; a settled record stays, as LambderIdempotencyStore requires. */
93
94
  async abandon(scopeKey, ownerToken) {
94
95
  const existing = this.records.get(scopeKey);
95
- if (existing && existing.ownerToken === ownerToken)
96
+ if (existing?.state === "pending" && existing.ownerToken === ownerToken)
96
97
  this.records.delete(scopeKey);
97
98
  }
98
99
  /**
99
100
  * The record under a scope, for assertions; null when absent or expired.
100
- * A copy, like every other read here, so an assertion that pokes at what
101
- * it got back cannot edit the stored record.
101
+ * A copy, like every read here, so an assertion cannot edit the stored
102
+ * record.
102
103
  */
103
104
  recordOf(scopeKey) {
104
105
  const record = this.records.get(scopeKey);
@@ -9,15 +9,13 @@ import { type LambderRateLimiter, type LambderRateLimitPolicy, type LambderRateL
9
9
  * `now` is injectable so a test can move time forward and watch a window
10
10
  * reset without waiting for it.
11
11
  *
12
- * `maxEntries` is the ceiling on counters held at once, 100,000 by default,
13
- * and it is the one way this limiter differs from the table: a process cannot
14
- * hold counters without bound, so past the ceiling the counters closest to
15
- * their window's end are dropped and a key whose counter was dropped starts
16
- * that window again from zero. It takes one distinct key per counter to get
17
- * there (a limit keyed per IP under a flood from many of them), and the
18
- * counters with the most life left, which are the long-window ones, are the
19
- * last to go. A deployment where that matters wants LambderDdbRateLimiter,
20
- * whose counters are not held in the process at all.
12
+ * `maxEntries` (100,000 counters by default) is the one way this limiter
13
+ * differs from the table: past the ceiling, the counters closest to their
14
+ * window's end are dropped, and a key whose counter was dropped starts that
15
+ * window again from zero. Reaching it takes one distinct key per counter (a
16
+ * per-IP limit under a flood from many IPs), and the long-window counters,
17
+ * with the most life left, go last. Where that matters, use
18
+ * LambderDdbRateLimiter, whose counters live outside the process.
21
19
  */
22
20
  export declare class LambderMemoryRateLimiter implements LambderRateLimiter {
23
21
  private readonly counters;
@@ -26,6 +24,7 @@ export declare class LambderMemoryRateLimiter implements LambderRateLimiter {
26
24
  now?: () => number;
27
25
  maxEntries?: number;
28
26
  });
27
+ clockMilliseconds(): number;
29
28
  isRateLimited(trackerKey: string, policy: LambderRateLimitPolicy): Promise<LambderRateLimitResult>;
30
29
  /** The attempts counted so far for a key in the current window of `window`; for assertions. */
31
30
  countOf(trackerKey: string, window: LambderRateLimitWindow): number;
@@ -1,6 +1,6 @@
1
1
  import { RATE_LIMIT_WINDOWS, } from "../shared/contracts/LambderRateLimiter.js";
2
2
  import { LambderExpiringMap } from "../shared/util/LambderExpiringMap.js";
3
- import { joinKeyFields } from "../shared/util/LambderKeyFields.js";
3
+ import { joinKeyFields } from "../shared/util/joinKeyFields.js";
4
4
  /**
5
5
  * Fixed-window rate limiter held in memory: the same windows, the same
6
6
  * attempts-count semantics and the same evaluation order as
@@ -11,15 +11,13 @@ import { joinKeyFields } from "../shared/util/LambderKeyFields.js";
11
11
  * `now` is injectable so a test can move time forward and watch a window
12
12
  * reset without waiting for it.
13
13
  *
14
- * `maxEntries` is the ceiling on counters held at once, 100,000 by default,
15
- * and it is the one way this limiter differs from the table: a process cannot
16
- * hold counters without bound, so past the ceiling the counters closest to
17
- * their window's end are dropped and a key whose counter was dropped starts
18
- * that window again from zero. It takes one distinct key per counter to get
19
- * there (a limit keyed per IP under a flood from many of them), and the
20
- * counters with the most life left, which are the long-window ones, are the
21
- * last to go. A deployment where that matters wants LambderDdbRateLimiter,
22
- * whose counters are not held in the process at all.
14
+ * `maxEntries` (100,000 counters by default) is the one way this limiter
15
+ * differs from the table: past the ceiling, the counters closest to their
16
+ * window's end are dropped, and a key whose counter was dropped starts that
17
+ * window again from zero. Reaching it takes one distinct key per counter (a
18
+ * per-IP limit under a flood from many IPs), and the long-window counters,
19
+ * with the most life left, go last. Where that matters, use
20
+ * LambderDdbRateLimiter, whose counters live outside the process.
23
21
  */
24
22
  export class LambderMemoryRateLimiter {
25
23
  counters;
@@ -28,6 +26,9 @@ export class LambderMemoryRateLimiter {
28
26
  this.now = options.now ?? (() => Date.now());
29
27
  this.counters = new LambderExpiringMap({ now: this.now, maxEntries: options.maxEntries });
30
28
  }
29
+ clockMilliseconds() {
30
+ return this.now();
31
+ }
31
32
  async isRateLimited(trackerKey, policy) {
32
33
  const nowSeconds = Math.floor(this.now() / 1000);
33
34
  for (const { key, seconds } of RATE_LIMIT_WINDOWS) {
@@ -35,9 +36,9 @@ export class LambderMemoryRateLimiter {
35
36
  if (!limit)
36
37
  continue;
37
38
  const windowStart = Math.floor(nowSeconds / seconds) * seconds;
38
- // The table gives the tracker key and the window separate
39
- // attributes; one string here has to keep them as distinct, so the
40
- // fields are joined through the escaping join rather than glued.
39
+ // The table keeps the tracker key and the window in separate
40
+ // attributes; one string has to keep them as distinct, hence the
41
+ // escaping join.
41
42
  const counterKey = joinKeyFields(trackerKey, key, String(windowStart));
42
43
  const counter = this.counters.get(counterKey) ?? { count: 0 };
43
44
  // The DynamoDB limiter's conditional ADD: allowed while the count
@@ -1,4 +1,4 @@
1
- import type { LambderSessionRecord, LambderSessionStore } from "../shared/contracts/LambderSessionStore.js";
1
+ import type { LambderSessionChanges, LambderSessionRecord, LambderSessionStore, LambderSessionUpdateResult } from "../shared/contracts/LambderSessionStore.js";
2
2
  /**
3
3
  * Session records held in memory: a Map in place of the DynamoDB table. For
4
4
  * tests and for the mock runtime, where the session model (tokens, expiry,
@@ -7,9 +7,11 @@ import type { LambderSessionRecord, LambderSessionStore } from "../shared/contra
7
7
  *
8
8
  * Records are copied on the way in and out through JSON, the way a real store
9
9
  * serializes them, so a caller mutating a fetched record changes nothing until
10
- * it is put back, and data DynamoDB would reject is rejected here too. A
11
- * record also leaves at its own expiresAt, the way the table's TTL removes
12
- * it, so a long-lived process does not hold every session it ever issued.
10
+ * it is written back, and data DynamoDB would reject is rejected here too.
11
+ * Creates and updates carry the table's conditions: a create refuses an
12
+ * existing record, and an update never brings back a deleted one. A record
13
+ * also leaves at its own expiresAt, as the table's TTL removes it, so a
14
+ * long-lived process does not hold every session it ever issued.
13
15
  */
14
16
  export declare class LambderMemorySessionStore<SessionData = unknown> implements LambderSessionStore<SessionData> {
15
17
  /** Nothing here outlives the process. */
@@ -28,17 +30,18 @@ export declare class LambderMemorySessionStore<SessionData = unknown> implements
28
30
  private static keyOf;
29
31
  /**
30
32
  * A record the way a real store hands it back: through JSON, so an
31
- * undefined field is gone and a cyclic value throws here exactly as it
32
- * throws on the way into DynamoDB. structuredClone did neither, which made
33
- * the memory store the more forgiving of the two and let a test pass over
34
- * data the production store would reject.
33
+ * undefined field is gone and a cyclic value throws here exactly as on the
34
+ * way into DynamoDB. structuredClone would do neither, and a test could
35
+ * then pass over data the production store rejects.
35
36
  */
36
37
  private static storedCopy;
37
38
  get(sessionKeyHash: string, secretHash: string): Promise<LambderSessionRecord<SessionData> | null>;
38
- put(record: LambderSessionRecord<SessionData>): Promise<void>;
39
- delete(sessionKeyHash: string, secretHash: string): Promise<void>;
39
+ create(record: LambderSessionRecord<SessionData>): Promise<void>;
40
+ update(sessionKeyHash: string, secretHash: string, changes: LambderSessionChanges<SessionData>, condition?: {
41
+ dataVersion: number;
42
+ }): Promise<LambderSessionUpdateResult>;
43
+ delete(sessionKeyHash: string, secretHash: string): Promise<LambderSessionRecord<SessionData> | null>;
40
44
  listSecretHashes(sessionKeyHash: string): Promise<string[]>;
41
- markDataExpired(sessionKeyHash: string, secretHash: string, at: number): Promise<void>;
42
45
  /** Every live record held, for assertions. */
43
46
  list(): LambderSessionRecord<SessionData>[];
44
47
  /** Number of live records held. */
@@ -1,5 +1,5 @@
1
1
  import { LambderExpiringMap } from "../shared/util/LambderExpiringMap.js";
2
- import { joinKeyFields } from "../shared/util/LambderKeyFields.js";
2
+ import { joinKeyFields } from "../shared/util/joinKeyFields.js";
3
3
  /**
4
4
  * Session records held in memory: a Map in place of the DynamoDB table. For
5
5
  * tests and for the mock runtime, where the session model (tokens, expiry,
@@ -8,9 +8,11 @@ import { joinKeyFields } from "../shared/util/LambderKeyFields.js";
8
8
  *
9
9
  * Records are copied on the way in and out through JSON, the way a real store
10
10
  * serializes them, so a caller mutating a fetched record changes nothing until
11
- * it is put back, and data DynamoDB would reject is rejected here too. A
12
- * record also leaves at its own expiresAt, the way the table's TTL removes
13
- * it, so a long-lived process does not hold every session it ever issued.
11
+ * it is written back, and data DynamoDB would reject is rejected here too.
12
+ * Creates and updates carry the table's conditions: a create refuses an
13
+ * existing record, and an update never brings back a deleted one. A record
14
+ * also leaves at its own expiresAt, as the table's TTL removes it, so a
15
+ * long-lived process does not hold every session it ever issued.
14
16
  */
15
17
  export class LambderMemorySessionStore {
16
18
  /** Nothing here outlives the process. */
@@ -26,17 +28,16 @@ export class LambderMemorySessionStore {
26
28
  this.records = new LambderExpiringMap(options);
27
29
  }
28
30
  static keyOf(sessionKeyHash, secretHash) {
29
- // Escaped rather than joined plainly: both halves are hex today, but a
30
- // custom LambderSessionCrypto writes whatever it likes into them, and
31
- // two records sharing one key would be one session silently.
31
+ // Escaped rather than joined plainly: the default crypto writes hex,
32
+ // but a custom LambderSessionCrypto may write anything, and two
33
+ // records sharing one key would silently be one session.
32
34
  return joinKeyFields(sessionKeyHash, secretHash);
33
35
  }
34
36
  /**
35
37
  * A record the way a real store hands it back: through JSON, so an
36
- * undefined field is gone and a cyclic value throws here exactly as it
37
- * throws on the way into DynamoDB. structuredClone did neither, which made
38
- * the memory store the more forgiving of the two and let a test pass over
39
- * data the production store would reject.
38
+ * undefined field is gone and a cyclic value throws here exactly as on the
39
+ * way into DynamoDB. structuredClone would do neither, and a test could
40
+ * then pass over data the production store rejects.
40
41
  */
41
42
  static storedCopy(record) {
42
43
  return JSON.parse(JSON.stringify(record));
@@ -45,22 +46,40 @@ export class LambderMemorySessionStore {
45
46
  const record = this.records.get(LambderMemorySessionStore.keyOf(sessionKeyHash, secretHash));
46
47
  return record ? LambderMemorySessionStore.storedCopy(record) : null;
47
48
  }
48
- async put(record) {
49
- this.records.set(LambderMemorySessionStore.keyOf(record.sessionKeyHash, record.secretHash), LambderMemorySessionStore.storedCopy(record), record.expiresAt);
49
+ async create(record) {
50
+ const key = LambderMemorySessionStore.keyOf(record.sessionKeyHash, record.secretHash);
51
+ if (this.records.get(key))
52
+ throw new Error("LambderMemorySessionStore: a session already exists under these hashes.");
53
+ this.records.set(key, LambderMemorySessionStore.storedCopy(record), record.expiresAt);
54
+ }
55
+ async update(sessionKeyHash, secretHash, changes, condition) {
56
+ const key = LambderMemorySessionStore.keyOf(sessionKeyHash, secretHash);
57
+ const record = this.records.get(key);
58
+ if (!record)
59
+ return "missing";
60
+ if (condition && record.dataVersion !== condition.dataVersion)
61
+ return "stale";
62
+ // A write of the data or its deadline moves the version, whatever
63
+ // value it writes, as the DynamoDB store's ADD does.
64
+ const writesData = "data" in changes || changes.dataExpiresAt !== undefined;
65
+ // Through the same JSON copy a create takes, so an update carries
66
+ // exactly what the table would hold, and the record's own expiry
67
+ // moves with it, the way the table's TTL attribute does.
68
+ const updated = LambderMemorySessionStore.storedCopy({ ...record, ...changes, dataVersion: record.dataVersion + (writesData ? 1 : 0) });
69
+ this.records.set(key, updated, updated.expiresAt);
70
+ return "updated";
50
71
  }
51
72
  async delete(sessionKeyHash, secretHash) {
52
- this.records.delete(LambderMemorySessionStore.keyOf(sessionKeyHash, secretHash));
73
+ const key = LambderMemorySessionStore.keyOf(sessionKeyHash, secretHash);
74
+ const record = this.records.get(key);
75
+ this.records.delete(key);
76
+ return record ? LambderMemorySessionStore.storedCopy(record) : null;
53
77
  }
54
78
  async listSecretHashes(sessionKeyHash) {
55
79
  return this.records.values()
56
80
  .filter((record) => record.sessionKeyHash === sessionKeyHash)
57
81
  .map((record) => record.secretHash);
58
82
  }
59
- async markDataExpired(sessionKeyHash, secretHash, at) {
60
- const record = this.records.get(LambderMemorySessionStore.keyOf(sessionKeyHash, secretHash));
61
- if (record)
62
- record.dataExpiresAt = at;
63
- }
64
83
  /** Every live record held, for assertions. */
65
84
  list() {
66
85
  return this.records.values().map((record) => LambderMemorySessionStore.storedCopy(record));
@@ -0,0 +1,99 @@
1
+ import { type LambderUploadBucket, type LambderUploadContentDisposition, type LambderUploadFileFacts, type LambderUploadObjectOptions, type LambderUploadRule, type LambderUploadTicket, type LambderUploadVerdict } from "../shared/contracts/LambderUploadBucket.js";
2
+ export type LambderMemoryUploadBucketOptions = {
3
+ /**
4
+ * The URL tickets and download links point under, which a mock's MSW
5
+ * handler (lambderMockUploadMswHandler) or a test's fetch stub answers
6
+ * for. Default: a URL of its own per bucket, on a host that cannot
7
+ * resolve (`https://upload-bucket-<id>.invalid/`), so a request nothing
8
+ * intercepts fails rather than reaching the network.
9
+ */
10
+ baseUrl?: string;
11
+ /** How long a ticket stays usable, unless a ticket says otherwise. Default: 600 seconds, as LambderS3UploadBucket. */
12
+ ticketLifetimeSeconds?: number;
13
+ /** How long a download link reads the object, unless a link says otherwise. Default: 300 seconds, as LambderS3UploadBucket. */
14
+ downloadLifetimeSeconds?: number;
15
+ /** The clock tickets and links expire by, injectable so a test can move past an expiry without waiting. Default: Date.now. */
16
+ now?: () => number;
17
+ };
18
+ /** What the memory bucket holds under a key, for a test to assert on: the object's facts and what it carries. */
19
+ export type LambderMemoryUploadObject = {
20
+ byteSize: number;
21
+ mimeType: string;
22
+ sha256Base64: string;
23
+ } & LambderUploadObjectOptions;
24
+ /**
25
+ * An upload bucket in memory (see LambderUploadBucket), for tests and the
26
+ * mock runtime.
27
+ *
28
+ * It holds a post to the rules S3 holds a presigned POST to: every field the
29
+ * ticket carries, each with the ticket's value, and no other, all ahead of
30
+ * the file (as S3 does, anything after the file is ignored); a ticket it
31
+ * issued and not yet expired; a body of exactly the size described; and bytes
32
+ * whose SHA-256 is the one described. It refuses otherwise with the status
33
+ * and the XML error S3 answers with (AccessDenied for a policy, and "Policy
34
+ * expired" for a late one, EntityTooSmall, EntityTooLarge, BadDigest), so any
35
+ * client, a LambderUploadRunner or another, takes the same path against it as
36
+ * against S3: an expired ticket is asked for again, a wrong file is refused.
37
+ * A download link reads the object until it expires.
38
+ *
39
+ * Storage requests reach it through handleStorageRequest(), which answers a
40
+ * fetch Request with a Response: lambderMockUploadMswHandler plugs that into
41
+ * MSW, and a test can route a stubbed fetch to it directly. Everything it
42
+ * uses is a web API (fetch's Request and Response, FormData, WebCrypto), so it
43
+ * runs in a browser, a service worker and Node alike.
44
+ */
45
+ export declare class LambderMemoryUploadBucket implements LambderUploadBucket {
46
+ /** Where tickets and download links point, always ending in a slash. */
47
+ readonly baseUrl: string;
48
+ private readonly ticketLifetimeSeconds;
49
+ private readonly downloadLifetimeSeconds;
50
+ private readonly now;
51
+ private readonly objects;
52
+ private readonly tickets;
53
+ private readonly links;
54
+ constructor({ baseUrl, ticketLifetimeSeconds, downloadLifetimeSeconds, now }?: LambderMemoryUploadBucketOptions);
55
+ issueUploadTicket({ objectKey, fileFacts, uploadRule, lifetimeSeconds, object }: {
56
+ objectKey: string;
57
+ fileFacts: LambderUploadFileFacts;
58
+ uploadRule: LambderUploadRule;
59
+ lifetimeSeconds?: number;
60
+ object?: LambderUploadObjectOptions;
61
+ }): Promise<LambderUploadTicket>;
62
+ verifyUploadedObject({ objectKey, fileFacts }: {
63
+ objectKey: string;
64
+ fileFacts: Pick<LambderUploadFileFacts, "byteSize" | "sha256Base64">;
65
+ }): Promise<LambderUploadVerdict>;
66
+ issueDownloadUrl({ objectKey, lifetimeSeconds, contentDisposition }: {
67
+ objectKey: string;
68
+ lifetimeSeconds?: number;
69
+ contentDisposition?: LambderUploadContentDisposition;
70
+ }): Promise<string>;
71
+ readObject(objectKey: string): Promise<Uint8Array>;
72
+ writeObject({ objectKey, body, mimeType, sha256Base64, object }: {
73
+ objectKey: string;
74
+ body: Uint8Array;
75
+ mimeType: string;
76
+ sha256Base64?: string;
77
+ object?: LambderUploadObjectOptions;
78
+ }): Promise<void>;
79
+ copyObject({ fromObjectKey, toObjectKey }: {
80
+ fromObjectKey: string;
81
+ toObjectKey: string;
82
+ }): Promise<void>;
83
+ deleteObject(objectKey: string): Promise<void>;
84
+ /** The keys that hold an object, sorted, for a test to assert on. */
85
+ listObjectKeys(): string[];
86
+ /** What is held under a key (its facts, tags, metadata and headers), for a test to assert on; null when nothing is. */
87
+ inspectObject(objectKey: string): LambderMemoryUploadObject | null;
88
+ /** Forgets every object, ticket and link. */
89
+ reset(): void;
90
+ /**
91
+ * Answers a request to storage the way S3 answers it: a post under a
92
+ * ticket stores its file, a GET or HEAD through a download link reads an
93
+ * object. A request outside baseUrl answers null, for the caller to hand
94
+ * on.
95
+ */
96
+ handleStorageRequest(request: Request): Promise<Response | null>;
97
+ private acceptUpload;
98
+ private serveDownload;
99
+ }