@ddd-ts-kit/http 0.10.0 → 0.12.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 (194) hide show
  1. package/README.md +204 -3
  2. package/dist/cache/assert-cache-declaration.d.ts +9 -0
  3. package/dist/cache/assert-cache-declaration.d.ts.map +1 -0
  4. package/dist/cache/assert-cache-declaration.js +82 -0
  5. package/dist/cache/assert-cache-declaration.js.map +1 -0
  6. package/dist/cache/cache-control-of.d.ts +4 -0
  7. package/dist/cache/cache-control-of.d.ts.map +1 -0
  8. package/dist/cache/cache-control-of.js +22 -0
  9. package/dist/cache/cache-control-of.js.map +1 -0
  10. package/dist/cache/cache-declaration.d.ts +14 -0
  11. package/dist/cache/cache-declaration.d.ts.map +1 -0
  12. package/dist/cache/cache-declaration.js +2 -0
  13. package/dist/cache/cache-declaration.js.map +1 -0
  14. package/dist/cache/cache-policy.d.ts +39 -0
  15. package/dist/cache/cache-policy.d.ts.map +1 -0
  16. package/dist/cache/cache-policy.js +2 -0
  17. package/dist/cache/cache-policy.js.map +1 -0
  18. package/dist/cache/cache-scope.d.ts +11 -0
  19. package/dist/cache/cache-scope.d.ts.map +1 -0
  20. package/dist/cache/cache-scope.js +10 -0
  21. package/dist/cache/cache-scope.js.map +1 -0
  22. package/dist/cache/cache-seconds.d.ts +4 -0
  23. package/dist/cache/cache-seconds.d.ts.map +1 -0
  24. package/dist/cache/cache-seconds.js +17 -0
  25. package/dist/cache/cache-seconds.js.map +1 -0
  26. package/dist/cache/cache-tag-header.d.ts +12 -0
  27. package/dist/cache/cache-tag-header.d.ts.map +1 -0
  28. package/dist/cache/cache-tag-header.js +11 -0
  29. package/dist/cache/cache-tag-header.js.map +1 -0
  30. package/dist/cache/cache-tag-source.d.ts +17 -0
  31. package/dist/cache/cache-tag-source.d.ts.map +1 -0
  32. package/dist/cache/cache-tag-source.js +2 -0
  33. package/dist/cache/cache-tag-source.js.map +1 -0
  34. package/dist/cache/entity-tag-source.d.ts +9 -0
  35. package/dist/cache/entity-tag-source.d.ts.map +1 -0
  36. package/dist/cache/entity-tag-source.js +2 -0
  37. package/dist/cache/entity-tag-source.js.map +1 -0
  38. package/dist/cache/if-match.d.ts +11 -0
  39. package/dist/cache/if-match.d.ts.map +1 -0
  40. package/dist/cache/if-match.js +10 -0
  41. package/dist/cache/if-match.js.map +1 -0
  42. package/dist/cache/vary-of.d.ts +3 -0
  43. package/dist/cache/vary-of.d.ts.map +1 -0
  44. package/dist/cache/vary-of.js +11 -0
  45. package/dist/cache/vary-of.js.map +1 -0
  46. package/dist/controller/controller-routes.d.ts.map +1 -1
  47. package/dist/controller/controller-routes.js +27 -10
  48. package/dist/controller/controller-routes.js.map +1 -1
  49. package/dist/controller/controller-spec.d.ts +6 -0
  50. package/dist/controller/controller-spec.d.ts.map +1 -1
  51. package/dist/controller/declared-route.d.ts +3 -0
  52. package/dist/controller/declared-route.d.ts.map +1 -1
  53. package/dist/controller/declared-route.js +9 -0
  54. package/dist/controller/declared-route.js.map +1 -1
  55. package/dist/controller/route-group-options.d.ts +6 -0
  56. package/dist/controller/route-group-options.d.ts.map +1 -1
  57. package/dist/error/default-error-mapping.d.ts.map +1 -1
  58. package/dist/error/default-error-mapping.js +2 -0
  59. package/dist/error/default-error-mapping.js.map +1 -1
  60. package/dist/error/precondition-required-error.d.ts +8 -0
  61. package/dist/error/precondition-required-error.d.ts.map +1 -0
  62. package/dist/error/precondition-required-error.js +8 -0
  63. package/dist/error/precondition-required-error.js.map +1 -0
  64. package/dist/express/mount-express.d.ts.map +1 -1
  65. package/dist/express/mount-express.js +2 -0
  66. package/dist/express/mount-express.js.map +1 -1
  67. package/dist/fastify/mount-fastify.d.ts.map +1 -1
  68. package/dist/fastify/mount-fastify.js +2 -0
  69. package/dist/fastify/mount-fastify.js.map +1 -1
  70. package/dist/index.d.ts +7 -0
  71. package/dist/index.d.ts.map +1 -1
  72. package/dist/index.js +4 -0
  73. package/dist/index.js.map +1 -1
  74. package/dist/openapi/cache-response-headers.d.ts +7 -0
  75. package/dist/openapi/cache-response-headers.d.ts.map +1 -0
  76. package/dist/openapi/cache-response-headers.js +35 -0
  77. package/dist/openapi/cache-response-headers.js.map +1 -0
  78. package/dist/openapi/cache-tags-extension.d.ts +9 -0
  79. package/dist/openapi/cache-tags-extension.d.ts.map +1 -0
  80. package/dist/openapi/cache-tags-extension.js +15 -0
  81. package/dist/openapi/cache-tags-extension.js.map +1 -0
  82. package/dist/openapi/conditional-parameters.d.ts +5 -0
  83. package/dist/openapi/conditional-parameters.d.ts.map +1 -0
  84. package/dist/openapi/conditional-parameters.js +26 -0
  85. package/dist/openapi/conditional-parameters.js.map +1 -0
  86. package/dist/openapi/path-items.d.ts.map +1 -1
  87. package/dist/openapi/path-items.js +4 -0
  88. package/dist/openapi/path-items.js.map +1 -1
  89. package/dist/openapi/responses.d.ts.map +1 -1
  90. package/dist/openapi/responses.js +23 -1
  91. package/dist/openapi/responses.js.map +1 -1
  92. package/dist/pipeline/assert-cached-routes.d.ts +11 -0
  93. package/dist/pipeline/assert-cached-routes.d.ts.map +1 -0
  94. package/dist/pipeline/assert-cached-routes.js +54 -0
  95. package/dist/pipeline/assert-cached-routes.js.map +1 -0
  96. package/dist/pipeline/cache-headers-of.d.ts +9 -0
  97. package/dist/pipeline/cache-headers-of.d.ts.map +1 -0
  98. package/dist/pipeline/cache-headers-of.js +24 -0
  99. package/dist/pipeline/cache-headers-of.js.map +1 -0
  100. package/dist/pipeline/cache-tags-of.d.ts +10 -0
  101. package/dist/pipeline/cache-tags-of.d.ts.map +1 -0
  102. package/dist/pipeline/cache-tags-of.js +20 -0
  103. package/dist/pipeline/cache-tags-of.js.map +1 -0
  104. package/dist/pipeline/conditional-answer.d.ts +10 -0
  105. package/dist/pipeline/conditional-answer.d.ts.map +1 -0
  106. package/dist/pipeline/conditional-answer.js +33 -0
  107. package/dist/pipeline/conditional-answer.js.map +1 -0
  108. package/dist/pipeline/entity-tag-of.d.ts +9 -0
  109. package/dist/pipeline/entity-tag-of.d.ts.map +1 -0
  110. package/dist/pipeline/entity-tag-of.js +25 -0
  111. package/dist/pipeline/entity-tag-of.js.map +1 -0
  112. package/dist/pipeline/execute-options.d.ts +23 -0
  113. package/dist/pipeline/execute-options.d.ts.map +1 -1
  114. package/dist/pipeline/execute-route.d.ts.map +1 -1
  115. package/dist/pipeline/execute-route.js +21 -2
  116. package/dist/pipeline/execute-route.js.map +1 -1
  117. package/dist/pipeline/expected-version-of.d.ts +10 -0
  118. package/dist/pipeline/expected-version-of.d.ts.map +1 -0
  119. package/dist/pipeline/expected-version-of.js +33 -0
  120. package/dist/pipeline/expected-version-of.js.map +1 -0
  121. package/dist/pipeline/is-not-modified.d.ts +8 -0
  122. package/dist/pipeline/is-not-modified.d.ts.map +1 -0
  123. package/dist/pipeline/is-not-modified.js +27 -0
  124. package/dist/pipeline/is-not-modified.js.map +1 -0
  125. package/dist/pipeline/last-modified-of.d.ts +3 -0
  126. package/dist/pipeline/last-modified-of.d.ts.map +1 -0
  127. package/dist/pipeline/last-modified-of.js +15 -0
  128. package/dist/pipeline/last-modified-of.js.map +1 -0
  129. package/dist/pipeline/tagged-answer.d.ts +19 -0
  130. package/dist/pipeline/tagged-answer.d.ts.map +1 -0
  131. package/dist/pipeline/tagged-answer.js +32 -0
  132. package/dist/pipeline/tagged-answer.js.map +1 -0
  133. package/dist/purgers/cache-purge-error.d.ts +11 -0
  134. package/dist/purgers/cache-purge-error.d.ts.map +1 -0
  135. package/dist/purgers/cache-purge-error.js +12 -0
  136. package/dist/purgers/cache-purge-error.js.map +1 -0
  137. package/dist/purgers/chunks-of.d.ts +3 -0
  138. package/dist/purgers/chunks-of.d.ts.map +1 -0
  139. package/dist/purgers/chunks-of.js +9 -0
  140. package/dist/purgers/chunks-of.js.map +1 -0
  141. package/dist/purgers/cloudflare-cache-purger.d.ts +20 -0
  142. package/dist/purgers/cloudflare-cache-purger.d.ts.map +1 -0
  143. package/dist/purgers/cloudflare-cache-purger.js +28 -0
  144. package/dist/purgers/cloudflare-cache-purger.js.map +1 -0
  145. package/dist/purgers/fastly-cache-purger.d.ts +19 -0
  146. package/dist/purgers/fastly-cache-purger.d.ts.map +1 -0
  147. package/dist/purgers/fastly-cache-purger.js +29 -0
  148. package/dist/purgers/fastly-cache-purger.js.map +1 -0
  149. package/dist/purgers/purge-request.d.ts +8 -0
  150. package/dist/purgers/purge-request.d.ts.map +1 -0
  151. package/dist/purgers/purge-request.js +2 -0
  152. package/dist/purgers/purge-request.js.map +1 -0
  153. package/dist/purgers/send-purge.d.ts +4 -0
  154. package/dist/purgers/send-purge.d.ts.map +1 -0
  155. package/dist/purgers/send-purge.js +19 -0
  156. package/dist/purgers/send-purge.js.map +1 -0
  157. package/dist/purgers/varnish-cache-purger.d.ts +27 -0
  158. package/dist/purgers/varnish-cache-purger.d.ts.map +1 -0
  159. package/dist/purgers/varnish-cache-purger.js +34 -0
  160. package/dist/purgers/varnish-cache-purger.js.map +1 -0
  161. package/dist/purgers/varnish-purge-mode.d.ts +13 -0
  162. package/dist/purgers/varnish-purge-mode.d.ts.map +1 -0
  163. package/dist/purgers/varnish-purge-mode.js +12 -0
  164. package/dist/purgers/varnish-purge-mode.js.map +1 -0
  165. package/dist/purgers.d.ts +6 -0
  166. package/dist/purgers.d.ts.map +1 -0
  167. package/dist/purgers.js +6 -0
  168. package/dist/purgers.js.map +1 -0
  169. package/dist/route/define-route.d.ts.map +1 -1
  170. package/dist/route/define-route.js +2 -0
  171. package/dist/route/define-route.js.map +1 -1
  172. package/dist/route/request-context.d.ts +2 -0
  173. package/dist/route/request-context.d.ts.map +1 -1
  174. package/dist/route/route-spec.d.ts +12 -0
  175. package/dist/route/route-spec.d.ts.map +1 -1
  176. package/dist/support/header-text.d.ts +3 -0
  177. package/dist/support/header-text.d.ts.map +1 -0
  178. package/dist/support/header-text.js +8 -0
  179. package/dist/support/header-text.js.map +1 -0
  180. package/dist/support/values-at.d.ts +6 -0
  181. package/dist/support/values-at.d.ts.map +1 -0
  182. package/dist/support/values-at.js +19 -0
  183. package/dist/support/values-at.js.map +1 -0
  184. package/dist/tenancy/first-tenant-of.d.ts +4 -1
  185. package/dist/tenancy/first-tenant-of.d.ts.map +1 -1
  186. package/dist/tenancy/first-tenant-of.js +7 -2
  187. package/dist/tenancy/first-tenant-of.js.map +1 -1
  188. package/dist/tenancy/tenant-from-header.d.ts +4 -1
  189. package/dist/tenancy/tenant-from-header.d.ts.map +1 -1
  190. package/dist/tenancy/tenant-from-header.js +6 -2
  191. package/dist/tenancy/tenant-from-header.js.map +1 -1
  192. package/dist/tenancy/tenant-resolver.d.ts +10 -1
  193. package/dist/tenancy/tenant-resolver.d.ts.map +1 -1
  194. package/package.json +8 -3
package/README.md CHANGED
@@ -84,8 +84,8 @@ const routes = controllerRoutes([PlaceOrderController], { resolve: application.r
84
84
 
85
85
  | Export | What it is |
86
86
  |---|---|
87
- | `Controller(spec)` | the decorator of a controller: `group`, `method`, `path` (after the prefix of the group), `operationId` (the class name without `Controller` by default), `summary`, `description`, `tags`, `params`, `query`, `headers`, `body`, `response: { status, description?, body? }`, `errors`, `guards`, `deprecated` |
88
- | `RouteGroup({ parent, prefix, tags, guards, errors })` | names a set of routes on an empty class; a child inherits from its `parent` and adds |
87
+ | `Controller(spec)` | the decorator of a controller: `group`, `method`, `path` (after the prefix of the group), `operationId` (the class name without `Controller` by default), `summary`, `description`, `tags`, `params`, `query`, `headers`, `body`, `response: { status, description?, body? }`, `errors`, `guards`, `deprecated`, `cache` and `ifMatch` (see "HTTP caching") |
88
+ | `RouteGroup({ parent, prefix, tags, guards, errors, cache })` | names a set of routes on an empty class; a child inherits from its `parent` and adds; the nearest `cache` wins |
89
89
  | `Guard` | the interface `{ documents?, check(request: GuardRequest) }`, and the decorator of a guard class that takes dependencies |
90
90
  | `Authenticated`, `Public`, `HasRole(...roles)`, `HasScope(...scopes)`, `TenantMember` | the guards the package ships, over the `Actor` and the `Session` of the request: 401 to the anonymous caller, 403 without one of the roles, without every scope named (`Session.scopes`, typed by the `SessionScopes` of the project) or outside the tenant of the request; `Public` lets everybody in and stands alone. A refusal of a guard is reported to `onGuardRefusal` |
91
91
  | `RequestMapper<Request, Message>`, `Serializer<Result, Body>` | the interfaces of the two optional translators (`map`, `serialize`), and the decorators that declare them |
@@ -103,6 +103,205 @@ in `DeclaredEdges`. A controller has one response and answers its body: a route
103
103
  success statuses, response headers or a raw body stays a `defineRoute` function. The reasons
104
104
  are on the [presentation layer](../../docs/concepts/presentation-layer.md) page.
105
105
 
106
+ ## HTTP caching
107
+
108
+ A read says how the caches on the way may keep its answer, on its controller, on its route
109
+ group (every read of the group, `GET` and `HEAD`, unless a controller says otherwise: a write of
110
+ the group keeps the default of a write) or on a function route (`cache` of the spec):
111
+
112
+ ```ts
113
+ import { CacheScope, Public, RouteGroup } from '@ddd-ts-kit/http';
114
+
115
+ @RouteGroup({
116
+ prefix: '/catalog',
117
+ guards: [Public],
118
+ cache: { scope: CacheScope.public, maxAge: '1m', sMaxAge: '5m', staleWhileRevalidate: '30s', staleIfError: '1d' },
119
+ })
120
+ export class CatalogRoutes {}
121
+ ```
122
+
123
+ | Field | Answers |
124
+ |---|---|
125
+ | `scope` | `public` (every cache: a CDN, Varnish, the browser), `private` (the browser of the caller only), `no-store` (nobody) |
126
+ | `maxAge`, `sMaxAge` | `max-age`, and `s-maxage` read by shared caches instead; whole seconds as text: `'0s'`, `'30s'`, `'5m'`, `'1h'`, `'1d'` |
127
+ | `staleWhileRevalidate`, `staleIfError` | how long a stale answer may still be served while the cache asks again, or while the server fails |
128
+ | `mustRevalidate` | `must-revalidate`: once stale, never served without asking |
129
+ | `vary` | `Vary`: the request headers the answer depends on, besides the URL |
130
+
131
+ The pipeline sends `Cache-Control` (and `Vary`) on a successful answer, `no-store` on every
132
+ refusal of a route that declares a policy, and `no-store` on every answer of a route that knows
133
+ its caller and declares none: an authenticated answer is never kept by default. A public route
134
+ that declares nothing says nothing, as before. A header the handler of a function route answers
135
+ itself wins. The contract documents the headers on the answer they apply to.
136
+
137
+ A policy that cannot stand is refused when the route is declared, with a `DeclarationError`:
138
+
139
+ - a policy other than `no-store` on a write (only `GET` and `HEAD` are kept by a cache), the
140
+ validators of a read (`etag`, `lastModified`) on a write, `ifMatch` on a read;
141
+ - `public` or `private` without `maxAge` (`'0s'` has the answer revalidated each time), a
142
+ duration that is not whole seconds, a duration on `no-store`, `sMaxAge` on a private answer;
143
+ - **a `public` answer of a route that knows its caller** (a guard other than `Public`, a
144
+ function route with `security`) that does not name in `vary` what it depends on. A shared
145
+ cache keys an answer by its URL: without `vary: ['Authorization']` (or the header carrying the
146
+ tenant), it would hand the answer of one caller to the next. `private` is the usual answer.
147
+ The ESLint rule `presentation/shared-cache-varies` reports the same declaration in the editor,
148
+ for what one file shows.
149
+
150
+ When the routes are mounted (`mountExpress`, `mountFastify`, `createRoutesModule`), the options of
151
+ the application are known, and a public answer is held to them with a `DeclarationError`:
152
+
153
+ - on a route that knows its caller, `vary` names `Authorization`, `Cookie`, or a header the tenant
154
+ is read from (`resolveTenant`): `vary: ['Accept-Language']` alone is refused;
155
+ - behind a cache that ignores `Vary` (`cacheTags.header` of Cloudflare or Bunny), a public answer
156
+ that varies is refused, unless `cacheTags.honoursVary: true` says the cache keys on those
157
+ headers;
158
+ - with tags filed per tenant (`cacheTags.perTenant`), a tagged route anybody may call needs
159
+ `resolveTenant`: an anonymous call names no tenant otherwise, and no purge would meet its answer.
160
+
161
+ Every answer a cache may keep varies on the headers the tenant is read from (`TenantResolver.headers`:
162
+ `tenantFromHeader` names its header, `firstTenantOf` the headers of its resolvers), added to `Vary`
163
+ by the pipeline. A tagged answer filed per tenant to a request that names no tenant is `no-store`.
164
+
165
+ Never cache a decision of authorization across actors: what a caller may see is decided by the
166
+ use case for that caller, and a shared cache only ever keeps what is the same for everybody
167
+ (see [HTTP caching](../../docs/concepts/http-caching.md)).
168
+
169
+ ### Conditional requests
170
+
171
+ A read that names its version answers `ETag` or `Last-Modified`, and `304 Not Modified`
172
+ without a body to a caller that already holds that version:
173
+
174
+ ```ts
175
+ @Controller({
176
+ group: CalendarsRoutes,
177
+ method: 'GET',
178
+ path: '/:calendarId/days/:day',
179
+ summary: 'Get calendar day',
180
+ params,
181
+ response: { status: 200, body: answer },
182
+ cache: { scope: CacheScope.private, maxAge: '0s', mustRevalidate: true, etag: 'representation' },
183
+ })
184
+ ```
185
+
186
+ | Field | Answers |
187
+ |---|---|
188
+ | `etag: 'representation'` | a strong tag, the SHA-256 of the bytes sent: right for any answer, and the handler still runs (it saves the transfer, not the work) |
189
+ | `etag: { version: 'version' }` | a weak tag, `W/"7"`, from the version the body carries under that field (dotted: `calendar.version`): the version of the aggregate or of the read model shown |
190
+ | `lastModified: 'changedAt'` | `Last-Modified`, from the instant under that field (a `Date` or ISO 8601 text), compared to `If-Modified-Since` to the second |
191
+
192
+ `If-None-Match` is read first and compared weakly, `If-Modified-Since` only without it. A body
193
+ that lacks the field it names answers 500, as any answer its declaration does not allow. The
194
+ contract gains the `If-None-Match` and `If-Modified-Since` parameters, the `ETag` and
195
+ `Last-Modified` headers and the `304` answer.
196
+
197
+ A write made on the version a client read declares `ifMatch` (`IfMatch.optional` or
198
+ `IfMatch.required`). The pipeline reads the version named in `If-Match` (the weak tag the read
199
+ answered, or its strong form `"7"`) and hands it to the handler as `context.expectedVersion`; the
200
+ use case compares it with the version of the aggregate it loaded:
201
+
202
+ ```ts
203
+ // the controller: new SetOpeningHoursCommand(request.actor, calendarId, ranges, request.context.expectedVersion)
204
+ // the handler of the command:
205
+ const calendar = await this.calendars.findById(command.calendarId);
206
+ expectVersion(calendar, command.expectedVersion); // PreconditionFailedError, 412, when it changed
207
+ ```
208
+
209
+ | Request | Answer |
210
+ |---|---|
211
+ | `If-Match` names the current version, or `*` | the change runs |
212
+ | `If-Match` names another version, or a tag that is no version | `412 PRECONDITION_FAILED` |
213
+ | no `If-Match`, `ifMatch: 'optional'` | the change runs unconditionally, as before |
214
+ | no `If-Match`, `ifMatch: 'required'` | `428 PRECONDITION_REQUIRED`, before the handler runs |
215
+
216
+ This is optimistic concurrency at the edge: the version a person saw on the screen, not only the
217
+ one the handler loaded a millisecond earlier. Two writes at the same instant still meet the
218
+ `ConcurrencyError` of the repository. RFC 9110 compares `If-Match` strongly, under which a weak
219
+ tag never matches: the kit compares versions instead, which is what both forms name here, and
220
+ documents it. The contract gains the `If-Match` parameter and the `412` (and `428`) problems.
221
+
222
+ ### Cache tags and purge after the commit
223
+
224
+ A public answer names the resources it shows, so that a shared cache files it under their tags
225
+ and drops it when one of them changes, instead of waiting for `maxAge`:
226
+
227
+ ```ts
228
+ cache: {
229
+ scope: CacheScope.public,
230
+ maxAge: '1m',
231
+ sMaxAge: '1h',
232
+ tags: [{ resource: 'calendar', param: 'calendarId' }, { resource: 'appointment', field: 'items.id' }],
233
+ },
234
+ ```
235
+
236
+ | Source | Tag |
237
+ |---|---|
238
+ | `{ resource, param }` | `<resource>:<value of the path parameter>` |
239
+ | `{ resource, field }` | `<resource>:<value>` for each value at that dotted path of the body (a list is walked through) |
240
+ | `{ resource }` | `<resource>`: a change of any element of that kind purges the answer (a list, a search) |
241
+
242
+ Tags are made with `cacheTag(resource, id)` of the kernel, the identifier percent-encoded. A
243
+ multi-tenant application files them under the tenant of the request (`acme/calendar:c-1`) with
244
+ `cacheTags: { header, perTenant: true }`, and its purges with `perTenant: true` of
245
+ `PublishedCacheInvalidation`: both sides or neither, or the purges miss. Tags are sent only
246
+ when the application names the header its shared cache reads, `cacheTags: { header }` of the
247
+ options: `CacheTagHeader.xkey` (Varnish), `surrogateKey` (Fastly), `cacheTag` (Cloudflare),
248
+ `cdnTag` (Bunny); without it, no answer carries tags. The contract lists them as templates in
249
+ `x-cache-tags`. Tags on a private answer, or reading a parameter the path does not have, are
250
+ refused when the route is declared.
251
+
252
+ The purge is asked by the use case and done after its commit:
253
+
254
+ ```ts
255
+ // application/event-handlers/calendar-opening-hours-set.event-handler.ts: before-commit, the default phase
256
+ @DomainEventHandler(CalendarOpeningHoursSet, { name: CalendarOpeningHoursSet.eventName })
257
+ export class CalendarOpeningHoursSetEventHandler implements DomainEventHandler<CalendarOpeningHoursSet> {
258
+ constructor(private readonly cacheInvalidation: CacheInvalidation) {}
259
+
260
+ async handle(event: CalendarOpeningHoursSet): Promise<void> {
261
+ await this.cacheInvalidation.invalidate([cacheTag('calendar', event.aggregateId)]);
262
+ }
263
+ }
264
+ ```
265
+
266
+ `CacheInvalidation` of the kernel publishes a purge request on the publisher of the
267
+ application, which `kernelModule()` binds by default: behind an outbox, the request is a row of
268
+ the transaction of the command. A command that rolls back purges nothing; one that commits
269
+ purges even when the process stops right after the commit, since the row waits for the next
270
+ relay. The relay hands the requests to a `CachePurger` and every other event to the broker:
271
+
272
+ ```ts
273
+ import { PurgingPublisher } from '@ddd-ts-kit/kernel';
274
+ import { VarnishCachePurger } from '@ddd-ts-kit/http/purgers';
275
+
276
+ const relay = new OutboxRelay({
277
+ store,
278
+ publisher: new PurgingPublisher(new VarnishCachePurger({ url: varnishUrl, key: varnishPurgeKey }), broker),
279
+ clock,
280
+ });
281
+ ```
282
+
283
+ A purge that fails throws: the relay retries it as it retries a publication, and a request
284
+ delivered twice purges twice, which changes nothing. A read that loaded the previous state just
285
+ before the commit can still reach the cache after the purge and stay for its `s-maxage`:
286
+ `RepeatedCachePurger` of the kernel purges once more a little later (`new
287
+ RepeatedCachePurger(varnish, { againAfterMs: 2_000 })`), best effort. Without an outbox (`events: in-memory`) the
288
+ request reaches the in-memory bus, where nothing purges: pass `cacheInvalidation` to
289
+ `kernelModule` to purge otherwise.
290
+
291
+ `@ddd-ts-kit/http/purgers` holds the adapters of the `CachePurger` port; they need no
292
+ dependency (the `fetch` of Node) and are held to `cachePurgerContract` of
293
+ `@ddd-ts-kit/testing/contracts`, which the in-memory one (`InMemoryCachePurger` of the kernel)
294
+ passes too:
295
+
296
+ | Adapter | Purges | Tag header | Verified |
297
+ |---|---|---|---|
298
+ | `VarnishCachePurger({ url, key?, mode?, soft? })` | `PURGE` with `xkey-purge` (vmod_xkey), or `xkey-softpurge` with `soft`; `mode: 'ban'` sends one `x-ban-tag` per tag, its pattern characters escaped, which the VCL turns into a ban; `key` goes in `x-purge-key`, which the VCL asks of a purge from another machine | `xkey` | the contract suite on Varnish 9.1.0 with the VCL `init --http-cache varnish` writes, in CI |
299
+ | `FastlyCachePurger({ serviceId, token, soft? })` | `POST /service/<id>/purge`, 256 surrogate keys a request | `surrogate-key` | the contract suite on a fake of the documented purge API, and the requests it sends; never run against Fastly |
300
+ | `CloudflareCachePurger({ zoneId, token })` | `POST /zones/<id>/purge_cache` with `tags`, 100 a request, on every plan | `cache-tag` | the contract suite on a fake of the documented purge API, and the requests it sends; never run against Cloudflare |
301
+
302
+ A purge that the cache does not confirm with a 2xx is a `CachePurgeError`. Bunny and the other
303
+ caches are compared on the [HTTP caches](../../docs/tools/http-caches.md) page.
304
+
106
305
  ## Errors
107
306
 
108
307
  A handler throws a `DomainError`; its `code` is mapped to a status in one place and answered as
@@ -136,7 +335,7 @@ message nor its facts.
136
335
 
137
336
  Defaults: `VALIDATION_FAILED` 422 (with `issues`, paths prefixed by the part:
138
337
  `body.items.0.sku`), `NOT_FOUND` 404, `CONFLICT` 409, `INVARIANT_VIOLATION` 422, `UNAUTHORIZED`
139
- 401, `FORBIDDEN` 403. A domain code that is not mapped answers 422.
338
+ 401, `FORBIDDEN` 403, `PRECONDITION_FAILED` 412, `PRECONDITION_REQUIRED` 428. A domain code that is not mapped answers 422.
140
339
 
141
340
  ```ts
142
341
  import { createErrorMapper } from '@ddd-ts-kit/http';
@@ -173,6 +372,8 @@ const options: ExecuteOptions = {
173
372
  },
174
373
  validateResponses: process.env.NODE_ENV === 'test', // checks answers against the declaration
175
374
  around: tracedRoutes(), // of @ddd-ts-kit/tracing: the span of the request, named by its route template
375
+ // the header the shared cache in front reads the tags from; honoursVary and perTenant: see HTTP caching
376
+ cacheTags: { header: CacheTagHeader.xkey },
176
377
  };
177
378
  ```
178
379
 
@@ -0,0 +1,9 @@
1
+ import type { CacheDeclaration } from './cache-declaration.js';
2
+ /**
3
+ * Refuses a cache policy that would let a cache keep what it must not: a write, an answer with
4
+ * no stated freshness, a directive of shared caches on a private answer, and above all the
5
+ * answer of a route that knows its caller kept in a shared cache that does not know what the
6
+ * answer varies on. Refuses `ifMatch` on a read, which changes nothing.
7
+ */
8
+ export declare function assertCacheDeclaration(route: CacheDeclaration): void;
9
+ //# sourceMappingURL=assert-cache-declaration.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"assert-cache-declaration.d.ts","sourceRoot":"","sources":["../../src/cache/assert-cache-declaration.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAS/D;;;;;GAKG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,gBAAgB,GAAG,IAAI,CAmBpE"}
@@ -0,0 +1,82 @@
1
+ import { DeclarationError } from '@ddd-ts-kit/kernel/declarations';
2
+ import { CacheScope } from './cache-scope.js';
3
+ import { cacheSeconds } from './cache-seconds.js';
4
+ const READS = ['GET', 'HEAD'];
5
+ /**
6
+ * Refuses a cache policy that would let a cache keep what it must not: a write, an answer with
7
+ * no stated freshness, a directive of shared caches on a private answer, and above all the
8
+ * answer of a route that knows its caller kept in a shared cache that does not know what the
9
+ * answer varies on. Refuses `ifMatch` on a read, which changes nothing.
10
+ */
11
+ export function assertCacheDeclaration(route) {
12
+ const refused = (reason) => new DeclarationError(`Route "${route.operationId}" (${route.method} ${route.path}): ${reason}`);
13
+ if (route.ifMatch !== undefined && READS.includes(route.method)) {
14
+ throw refused('If-Match conditions a change on the version the caller read: a read changes nothing.');
15
+ }
16
+ const { cache } = route;
17
+ if (cache === undefined) {
18
+ return;
19
+ }
20
+ assertRead(route, cache, refused);
21
+ assertDurations(cache, refused);
22
+ assertScope(cache, refused);
23
+ assertSharedVaries(route, cache, refused);
24
+ assertTags(route, cache, refused);
25
+ }
26
+ function assertRead(route, cache, refused) {
27
+ if (READS.includes(route.method)) {
28
+ return;
29
+ }
30
+ if (cache.scope !== CacheScope.noStore) {
31
+ throw refused('only the answer of a read (GET, HEAD) is kept by a cache: declare scope no-store, or no policy.');
32
+ }
33
+ if (cache.etag !== undefined || cache.lastModified !== undefined) {
34
+ throw refused('ETag and Last-Modified name the version of a read; a write names the version it changes with ifMatch.');
35
+ }
36
+ }
37
+ function assertDurations(cache, refused) {
38
+ const written = [
39
+ cache.maxAge,
40
+ cache.sMaxAge,
41
+ cache.staleWhileRevalidate,
42
+ cache.staleIfError,
43
+ ];
44
+ for (const duration of written) {
45
+ if (duration !== undefined && cacheSeconds(duration) === undefined) {
46
+ throw refused(`"${duration}" is not a whole number of seconds: write '0s', '30s', '5m', '1h' or '1d'.`);
47
+ }
48
+ }
49
+ }
50
+ function assertScope(cache, refused) {
51
+ if (cache.scope === CacheScope.noStore) {
52
+ const durations = [cache.maxAge, cache.sMaxAge, cache.staleWhileRevalidate, cache.staleIfError];
53
+ if (durations.some((duration) => duration !== undefined)) {
54
+ throw refused('a no-store answer is kept by nobody: it has no freshness to declare.');
55
+ }
56
+ return;
57
+ }
58
+ if (cache.maxAge === undefined) {
59
+ throw refused(`a ${cache.scope} answer says how long it stays fresh: declare maxAge, '0s' to have it revalidated each time.`);
60
+ }
61
+ if (cache.scope === CacheScope.private && cache.sMaxAge !== undefined) {
62
+ throw refused('sMaxAge speaks to shared caches, which a private answer never reaches.');
63
+ }
64
+ }
65
+ function assertSharedVaries(route, cache, refused) {
66
+ if (cache.scope !== CacheScope.public || route.security === false || (cache.vary ?? []).length > 0) {
67
+ return;
68
+ }
69
+ throw refused('the route knows its caller, and a public answer is kept by shared caches that would hand it to the next caller. Declare scope private, or name in vary the request headers the answer depends on (Authorization, a tenant header).');
70
+ }
71
+ function assertTags(route, cache, refused) {
72
+ const sources = cache.tags ?? [];
73
+ if (sources.length > 0 && cache.scope !== CacheScope.public) {
74
+ throw refused('tags file an answer in a shared cache, which a private or no-store answer never reaches.');
75
+ }
76
+ for (const source of sources) {
77
+ if ('param' in source && !route.path.split('/').includes(`:${source.param}`)) {
78
+ throw refused(`the tag of ${source.resource} reads the parameter "${source.param}", which the path does not have.`);
79
+ }
80
+ }
81
+ }
82
+ //# sourceMappingURL=assert-cache-declaration.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"assert-cache-declaration.js","sourceRoot":"","sources":["../../src/cache/assert-cache-declaration.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AAGnE,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD,MAAM,KAAK,GAAsB,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;AAIjD;;;;;GAKG;AACH,MAAM,UAAU,sBAAsB,CAAC,KAAuB;IAC5D,MAAM,OAAO,GAAY,CAAC,MAAM,EAAE,EAAE,CAClC,IAAI,gBAAgB,CAAC,UAAU,KAAK,CAAC,WAAW,MAAM,KAAK,CAAC,MAAM,IAAI,KAAK,CAAC,IAAI,MAAM,MAAM,EAAE,CAAC,CAAC;IAElG,IAAI,KAAK,CAAC,OAAO,KAAK,SAAS,IAAI,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC;QAChE,MAAM,OAAO,CAAC,sFAAsF,CAAC,CAAC;IACxG,CAAC;IAED,MAAM,EAAE,KAAK,EAAE,GAAG,KAAK,CAAC;IAExB,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QACxB,OAAO;IACT,CAAC;IAED,UAAU,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;IAClC,eAAe,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IAChC,WAAW,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IAC5B,kBAAkB,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;IAC1C,UAAU,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;AACpC,CAAC;AAED,SAAS,UAAU,CAAC,KAAuB,EAAE,KAAkB,EAAE,OAAgB;IAC/E,IAAI,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC;QACjC,OAAO;IACT,CAAC;IAED,IAAI,KAAK,CAAC,KAAK,KAAK,UAAU,CAAC,OAAO,EAAE,CAAC;QACvC,MAAM,OAAO,CAAC,iGAAiG,CAAC,CAAC;IACnH,CAAC;IAED,IAAI,KAAK,CAAC,IAAI,KAAK,SAAS,IAAI,KAAK,CAAC,YAAY,KAAK,SAAS,EAAE,CAAC;QACjE,MAAM,OAAO,CACX,uGAAuG,CACxG,CAAC;IACJ,CAAC;AACH,CAAC;AAED,SAAS,eAAe,CAAC,KAAkB,EAAE,OAAgB;IAC3D,MAAM,OAAO,GAA0C;QACrD,KAAK,CAAC,MAAM;QACZ,KAAK,CAAC,OAAO;QACb,KAAK,CAAC,oBAAoB;QAC1B,KAAK,CAAC,YAAY;KACnB,CAAC;IAEF,KAAK,MAAM,QAAQ,IAAI,OAAO,EAAE,CAAC;QAC/B,IAAI,QAAQ,KAAK,SAAS,IAAI,YAAY,CAAC,QAAQ,CAAC,KAAK,SAAS,EAAE,CAAC;YACnE,MAAM,OAAO,CAAC,IAAI,QAAQ,4EAA4E,CAAC,CAAC;QAC1G,CAAC;IACH,CAAC;AACH,CAAC;AAED,SAAS,WAAW,CAAC,KAAkB,EAAE,OAAgB;IACvD,IAAI,KAAK,CAAC,KAAK,KAAK,UAAU,CAAC,OAAO,EAAE,CAAC;QACvC,MAAM,SAAS,GAAG,CAAC,KAAK,CAAC,MAAM,EAAE,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,oBAAoB,EAAE,KAAK,CAAC,YAAY,CAAC,CAAC;QAEhG,IAAI,SAAS,CAAC,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,KAAK,SAAS,CAAC,EAAE,CAAC;YACzD,MAAM,OAAO,CAAC,sEAAsE,CAAC,CAAC;QACxF,CAAC;QAED,OAAO;IACT,CAAC;IAED,IAAI,KAAK,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;QAC/B,MAAM,OAAO,CACX,KAAK,KAAK,CAAC,KAAK,8FAA8F,CAC/G,CAAC;IACJ,CAAC;IAED,IAAI,KAAK,CAAC,KAAK,KAAK,UAAU,CAAC,OAAO,IAAI,KAAK,CAAC,OAAO,KAAK,SAAS,EAAE,CAAC;QACtE,MAAM,OAAO,CAAC,wEAAwE,CAAC,CAAC;IAC1F,CAAC;AACH,CAAC;AAED,SAAS,kBAAkB,CAAC,KAAuB,EAAE,KAAkB,EAAE,OAAgB;IACvF,IAAI,KAAK,CAAC,KAAK,KAAK,UAAU,CAAC,MAAM,IAAI,KAAK,CAAC,QAAQ,KAAK,KAAK,IAAI,CAAC,KAAK,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACnG,OAAO;IACT,CAAC;IAED,MAAM,OAAO,CACX,oOAAoO,CACrO,CAAC;AACJ,CAAC;AAED,SAAS,UAAU,CAAC,KAAuB,EAAE,KAAkB,EAAE,OAAgB;IAC/E,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,IAAI,EAAE,CAAC;IAEjC,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,IAAI,KAAK,CAAC,KAAK,KAAK,UAAU,CAAC,MAAM,EAAE,CAAC;QAC5D,MAAM,OAAO,CAAC,0FAA0F,CAAC,CAAC;IAC5G,CAAC;IAED,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;QAC7B,IAAI,OAAO,IAAI,MAAM,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC,EAAE,CAAC;YAC7E,MAAM,OAAO,CACX,cAAc,MAAM,CAAC,QAAQ,yBAAyB,MAAM,CAAC,KAAK,kCAAkC,CACrG,CAAC;QACJ,CAAC;IACH,CAAC;AACH,CAAC"}
@@ -0,0 +1,4 @@
1
+ import type { CachePolicy } from './cache-policy.js';
2
+ /** The `Cache-Control` header a policy answers with, its directives in a fixed order. */
3
+ export declare function cacheControlOf(policy: CachePolicy): string;
4
+ //# sourceMappingURL=cache-control-of.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache-control-of.d.ts","sourceRoot":"","sources":["../../src/cache/cache-control-of.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGrD,yFAAyF;AACzF,wBAAgB,cAAc,CAAC,MAAM,EAAE,WAAW,GAAG,MAAM,CAe1D"}
@@ -0,0 +1,22 @@
1
+ import { cacheSeconds } from './cache-seconds.js';
2
+ import { CacheScope } from './cache-scope.js';
3
+ /** The `Cache-Control` header a policy answers with, its directives in a fixed order. */
4
+ export function cacheControlOf(policy) {
5
+ if (policy.scope === CacheScope.noStore) {
6
+ return CacheScope.noStore;
7
+ }
8
+ const directives = [
9
+ policy.scope,
10
+ directive('max-age', policy.maxAge),
11
+ directive('s-maxage', policy.sMaxAge),
12
+ directive('stale-while-revalidate', policy.staleWhileRevalidate),
13
+ directive('stale-if-error', policy.staleIfError),
14
+ policy.mustRevalidate === true ? 'must-revalidate' : undefined,
15
+ ];
16
+ return directives.filter((written) => written !== undefined).join(', ');
17
+ }
18
+ function directive(name, duration) {
19
+ const seconds = duration === undefined ? undefined : cacheSeconds(duration);
20
+ return seconds === undefined ? undefined : `${name}=${seconds}`;
21
+ }
22
+ //# sourceMappingURL=cache-control-of.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache-control-of.js","sourceRoot":"","sources":["../../src/cache/cache-control-of.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9C,yFAAyF;AACzF,MAAM,UAAU,cAAc,CAAC,MAAmB;IAChD,IAAI,MAAM,CAAC,KAAK,KAAK,UAAU,CAAC,OAAO,EAAE,CAAC;QACxC,OAAO,UAAU,CAAC,OAAO,CAAC;IAC5B,CAAC;IAED,MAAM,UAAU,GAAG;QACjB,MAAM,CAAC,KAAK;QACZ,SAAS,CAAC,SAAS,EAAE,MAAM,CAAC,MAAM,CAAC;QACnC,SAAS,CAAC,UAAU,EAAE,MAAM,CAAC,OAAO,CAAC;QACrC,SAAS,CAAC,wBAAwB,EAAE,MAAM,CAAC,oBAAoB,CAAC;QAChE,SAAS,CAAC,gBAAgB,EAAE,MAAM,CAAC,YAAY,CAAC;QAChD,MAAM,CAAC,cAAc,KAAK,IAAI,CAAC,CAAC,CAAC,iBAAiB,CAAC,CAAC,CAAC,SAAS;KAC/D,CAAC;IAEF,OAAO,UAAU,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,KAAK,SAAS,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC1E,CAAC;AAED,SAAS,SAAS,CAAC,IAAY,EAAE,QAAkC;IACjE,MAAM,OAAO,GAAG,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC;IAE5E,OAAO,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,IAAI,IAAI,OAAO,EAAE,CAAC;AAClE,CAAC"}
@@ -0,0 +1,14 @@
1
+ import type { HttpMethod } from '../route/http-method.js';
2
+ import type { CachePolicy } from './cache-policy.js';
3
+ import type { IfMatch } from './if-match.js';
4
+ /** What the check of a cache policy reads of the route that declares it. */
5
+ export interface CacheDeclaration {
6
+ readonly operationId: string;
7
+ readonly method: HttpMethod;
8
+ readonly path: string;
9
+ /** `false` for a route anybody may call: its answer does not depend on the caller. */
10
+ readonly security: readonly string[] | false;
11
+ readonly cache?: CachePolicy;
12
+ readonly ifMatch?: IfMatch;
13
+ }
14
+ //# sourceMappingURL=cache-declaration.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache-declaration.d.ts","sourceRoot":"","sources":["../../src/cache/cache-declaration.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACrD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAE7C,4EAA4E;AAC5E,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,MAAM,EAAE,UAAU,CAAC;IAC5B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,sFAAsF;IACtF,QAAQ,CAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,GAAG,KAAK,CAAC;IAC7C,QAAQ,CAAC,KAAK,CAAC,EAAE,WAAW,CAAC;IAC7B,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC;CAC5B"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=cache-declaration.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache-declaration.js","sourceRoot":"","sources":["../../src/cache/cache-declaration.ts"],"names":[],"mappings":""}
@@ -0,0 +1,39 @@
1
+ import type { DurationText } from '@ddd-ts-kit/kernel';
2
+ import type { CacheScope } from './cache-scope.js';
3
+ import type { CacheTagSource } from './cache-tag-source.js';
4
+ import type { EntityTagSource } from './entity-tag-source.js';
5
+ /**
6
+ * How the answer of a read may be kept by the caches on the way, declared on a controller or a
7
+ * route group (`cache`), or on a function route. Durations are whole seconds written as text:
8
+ * `'30s'`, `'5m'`, `'1h'`, `'0s'`.
9
+ */
10
+ export interface CachePolicy {
11
+ readonly scope: CacheScope;
12
+ /** How long the answer stays fresh: `max-age`. Required unless the scope is `no-store`. */
13
+ readonly maxAge?: DurationText;
14
+ /** How long it stays fresh in a shared cache, which reads it instead of `maxAge`: `s-maxage`. Public only. */
15
+ readonly sMaxAge?: DurationText;
16
+ /** How long a stale answer may still be served while the cache asks the server again: `stale-while-revalidate`. */
17
+ readonly staleWhileRevalidate?: DurationText;
18
+ /** How long a stale answer may be served when the server fails: `stale-if-error`. */
19
+ readonly staleIfError?: DurationText;
20
+ /** Once stale, never served without asking the server: `must-revalidate`. */
21
+ readonly mustRevalidate?: boolean;
22
+ /**
23
+ * The request headers the answer depends on, besides the URL: `Vary`. A public answer of a
24
+ * route that is not public names here what says who calls (`Authorization`, `Cookie`) or the
25
+ * header its tenant is read from: without it, a shared cache would hand the answer of one
26
+ * caller to the next, and the mount refuses it. The headers `resolveTenant` reads are added.
27
+ */
28
+ readonly vary?: readonly string[];
29
+ /** Answers `ETag`, and `304 Not Modified` to a request whose `If-None-Match` names it. */
30
+ readonly etag?: EntityTagSource;
31
+ /** The field of the body holding the instant the resource last changed: `Last-Modified`, and `304` to `If-Modified-Since`. */
32
+ readonly lastModified?: string;
33
+ /**
34
+ * The resources the answer shows, sent as cache tags in the header the application names
35
+ * (`cacheTags` of the options), so that a change of one of them purges the answer. Public only.
36
+ */
37
+ readonly tags?: readonly CacheTagSource[];
38
+ }
39
+ //# sourceMappingURL=cache-policy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache-policy.d.ts","sourceRoot":"","sources":["../../src/cache/cache-policy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAE9D;;;;GAIG;AACH,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAC;IAC3B,2FAA2F;IAC3F,QAAQ,CAAC,MAAM,CAAC,EAAE,YAAY,CAAC;IAC/B,8GAA8G;IAC9G,QAAQ,CAAC,OAAO,CAAC,EAAE,YAAY,CAAC;IAChC,mHAAmH;IACnH,QAAQ,CAAC,oBAAoB,CAAC,EAAE,YAAY,CAAC;IAC7C,qFAAqF;IACrF,QAAQ,CAAC,YAAY,CAAC,EAAE,YAAY,CAAC;IACrC,6EAA6E;IAC7E,QAAQ,CAAC,cAAc,CAAC,EAAE,OAAO,CAAC;IAClC;;;;;OAKG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAClC,0FAA0F;IAC1F,QAAQ,CAAC,IAAI,CAAC,EAAE,eAAe,CAAC;IAChC,8HAA8H;IAC9H,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;IAC/B;;;OAGG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,SAAS,cAAc,EAAE,CAAC;CAC3C"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=cache-policy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache-policy.js","sourceRoot":"","sources":["../../src/cache/cache-policy.ts"],"names":[],"mappings":""}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Who may keep an answer: `public`, every cache on the way (a CDN, Varnish, the browser);
3
+ * `private`, the browser of the caller only; `no-store`, nobody.
4
+ */
5
+ export declare const CacheScope: {
6
+ readonly public: 'public';
7
+ readonly private: 'private';
8
+ readonly noStore: 'no-store';
9
+ };
10
+ export type CacheScope = (typeof CacheScope)[keyof typeof CacheScope];
11
+ //# sourceMappingURL=cache-scope.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache-scope.d.ts","sourceRoot":"","sources":["../../src/cache/cache-scope.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,eAAO,MAAM,UAAU;aACrB,MAAM,EAAE,QAAQ;aAChB,OAAO,EAAE,SAAS;aAClB,OAAO,EAAE,UAAU;CACX,CAAC;AAEX,MAAM,MAAM,UAAU,GAAG,CAAC,OAAO,UAAU,CAAC,CAAC,MAAM,OAAO,UAAU,CAAC,CAAC"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Who may keep an answer: `public`, every cache on the way (a CDN, Varnish, the browser);
3
+ * `private`, the browser of the caller only; `no-store`, nobody.
4
+ */
5
+ export const CacheScope = {
6
+ public: 'public',
7
+ private: 'private',
8
+ noStore: 'no-store',
9
+ };
10
+ //# sourceMappingURL=cache-scope.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache-scope.js","sourceRoot":"","sources":["../../src/cache/cache-scope.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG;IACxB,MAAM,EAAE,QAAQ;IAChB,OAAO,EAAE,SAAS;IAClB,OAAO,EAAE,UAAU;CACX,CAAC"}
@@ -0,0 +1,4 @@
1
+ import type { DurationText } from '@ddd-ts-kit/kernel';
2
+ /** The whole seconds of a duration of a cache directive, zero included; `undefined` when it is none. */
3
+ export declare function cacheSeconds(duration: DurationText): number | undefined;
4
+ //# sourceMappingURL=cache-seconds.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache-seconds.d.ts","sourceRoot":"","sources":["../../src/cache/cache-seconds.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAMvD,wGAAwG;AACxG,wBAAgB,YAAY,CAAC,QAAQ,EAAE,YAAY,GAAG,MAAM,GAAG,SAAS,CAgBvE"}
@@ -0,0 +1,17 @@
1
+ const SECONDS_PER_UNIT = { s: 1, m: 60, h: 3_600, d: 86_400 };
2
+ const MILLISECONDS_PER_SECOND = 1_000;
3
+ const DURATION = /^(\d+)(ms|s|m|h|d)$/;
4
+ /** The whole seconds of a duration of a cache directive, zero included; `undefined` when it is none. */
5
+ export function cacheSeconds(duration) {
6
+ const [, amount, unit] = DURATION.exec(duration) ?? [];
7
+ if (amount === undefined || unit === undefined) {
8
+ return undefined;
9
+ }
10
+ const value = Number(amount);
11
+ if (unit === 'ms') {
12
+ return value % MILLISECONDS_PER_SECOND === 0 ? value / MILLISECONDS_PER_SECOND : undefined;
13
+ }
14
+ const seconds = value * (SECONDS_PER_UNIT[unit] ?? Number.NaN);
15
+ return Number.isSafeInteger(seconds) ? seconds : undefined;
16
+ }
17
+ //# sourceMappingURL=cache-seconds.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache-seconds.js","sourceRoot":"","sources":["../../src/cache/cache-seconds.ts"],"names":[],"mappings":"AAEA,MAAM,gBAAgB,GAAqC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC;AAChG,MAAM,uBAAuB,GAAG,KAAK,CAAC;AACtC,MAAM,QAAQ,GAAG,qBAAqB,CAAC;AAEvC,wGAAwG;AACxG,MAAM,UAAU,YAAY,CAAC,QAAsB;IACjD,MAAM,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;IAEvD,IAAI,MAAM,KAAK,SAAS,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;QAC/C,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;IAE7B,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;QAClB,OAAO,KAAK,GAAG,uBAAuB,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,uBAAuB,CAAC,CAAC,CAAC,SAAS,CAAC;IAC7F,CAAC;IAED,MAAM,OAAO,GAAG,KAAK,GAAG,CAAC,gBAAgB,CAAC,IAAI,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC;IAE/D,OAAO,MAAM,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC;AAC7D,CAAC"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * The response header a shared cache reads the tags of an answer from: `surrogate-key` (Fastly,
3
+ * Varnish with xkey), `xkey` (Varnish), `cache-tag` (Cloudflare), `cdn-tag` (Bunny).
4
+ */
5
+ export declare const CacheTagHeader: {
6
+ readonly surrogateKey: 'surrogate-key';
7
+ readonly xkey: 'xkey';
8
+ readonly cacheTag: 'cache-tag';
9
+ readonly cdnTag: 'cdn-tag';
10
+ };
11
+ export type CacheTagHeader = (typeof CacheTagHeader)[keyof typeof CacheTagHeader];
12
+ //# sourceMappingURL=cache-tag-header.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache-tag-header.d.ts","sourceRoot":"","sources":["../../src/cache/cache-tag-header.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,eAAO,MAAM,cAAc;aACzB,YAAY,EAAE,eAAe;aAC7B,IAAI,EAAE,MAAM;aACZ,QAAQ,EAAE,WAAW;aACrB,MAAM,EAAE,SAAS;CACT,CAAC;AAEX,MAAM,MAAM,cAAc,GAAG,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,OAAO,cAAc,CAAC,CAAC"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * The response header a shared cache reads the tags of an answer from: `surrogate-key` (Fastly,
3
+ * Varnish with xkey), `xkey` (Varnish), `cache-tag` (Cloudflare), `cdn-tag` (Bunny).
4
+ */
5
+ export const CacheTagHeader = {
6
+ surrogateKey: 'surrogate-key',
7
+ xkey: 'xkey',
8
+ cacheTag: 'cache-tag',
9
+ cdnTag: 'cdn-tag',
10
+ };
11
+ //# sourceMappingURL=cache-tag-header.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache-tag-header.js","sourceRoot":"","sources":["../../src/cache/cache-tag-header.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG;IAC5B,YAAY,EAAE,eAAe;IAC7B,IAAI,EAAE,MAAM;IACZ,QAAQ,EAAE,WAAW;IACrB,MAAM,EAAE,SAAS;CACT,CAAC"}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * A resource an answer shows, named as a cache tag so that a change of that resource purges
3
+ * the answer: `{ resource: 'calendar', param: 'calendarId' }` tags `calendar:<calendarId>`;
4
+ * `{ resource: 'appointment', field: 'items.id' }` tags each value found at that path of the
5
+ * body (a list is walked through); `{ resource: 'appointment' }` tags the whole kind, which a
6
+ * change of any appointment purges.
7
+ */
8
+ export type CacheTagSource = {
9
+ readonly resource: string;
10
+ readonly param: string;
11
+ } | {
12
+ readonly resource: string;
13
+ readonly field: string;
14
+ } | {
15
+ readonly resource: string;
16
+ };
17
+ //# sourceMappingURL=cache-tag-source.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache-tag-source.d.ts","sourceRoot":"","sources":["../../src/cache/cache-tag-source.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,MAAM,cAAc,GACtB;IAAE,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACrD;IAAE,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACrD;IAAE,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=cache-tag-source.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache-tag-source.js","sourceRoot":"","sources":["../../src/cache/cache-tag-source.ts"],"names":[],"mappings":""}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * What names the version of an answer, for a conditional request: `representation`, a strong
3
+ * tag hashed from the bytes answered; `{ version: field }`, a weak tag made of the version the
4
+ * body carries under that field (the version of the read model or of the aggregate it shows).
5
+ */
6
+ export type EntityTagSource = 'representation' | {
7
+ readonly version: string;
8
+ };
9
+ //# sourceMappingURL=entity-tag-source.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"entity-tag-source.d.ts","sourceRoot":"","sources":["../../src/cache/entity-tag-source.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,MAAM,eAAe,GAAG,gBAAgB,GAAG;IAAE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=entity-tag-source.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"entity-tag-source.js","sourceRoot":"","sources":["../../src/cache/entity-tag-source.ts"],"names":[],"mappings":""}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Whether a write is conditional on the version a client read: `required`, a request without
3
+ * `If-Match` is refused with 428; `optional`, it runs unconditionally. Either way a tag that names
4
+ * another version than the current one is refused with 412 by the use case, which compares it.
5
+ */
6
+ export declare const IfMatch: {
7
+ readonly required: 'required';
8
+ readonly optional: 'optional';
9
+ };
10
+ export type IfMatch = (typeof IfMatch)[keyof typeof IfMatch];
11
+ //# sourceMappingURL=if-match.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"if-match.d.ts","sourceRoot":"","sources":["../../src/cache/if-match.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,eAAO,MAAM,OAAO;aAClB,QAAQ,EAAE,UAAU;aACpB,QAAQ,EAAE,UAAU;CACZ,CAAC;AAEX,MAAM,MAAM,OAAO,GAAG,CAAC,OAAO,OAAO,CAAC,CAAC,MAAM,OAAO,OAAO,CAAC,CAAC"}