@vantedev/groundwork 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (216) hide show
  1. package/CHANGELOG.md +87 -0
  2. package/LICENSE +21 -0
  3. package/README.md +350 -2
  4. package/dist/api-keys/api-keys.d.ts +56 -0
  5. package/dist/api-keys/api-keys.d.ts.map +1 -0
  6. package/dist/api-keys/api-keys.js +619 -0
  7. package/dist/api-keys/api-keys.js.map +1 -0
  8. package/dist/api-keys/audit.d.ts +17 -0
  9. package/dist/api-keys/audit.d.ts.map +1 -0
  10. package/dist/api-keys/audit.js +76 -0
  11. package/dist/api-keys/audit.js.map +1 -0
  12. package/dist/api-keys/format.d.ts +32 -0
  13. package/dist/api-keys/format.d.ts.map +1 -0
  14. package/dist/api-keys/format.js +92 -0
  15. package/dist/api-keys/format.js.map +1 -0
  16. package/dist/api-keys/index.d.ts +25 -0
  17. package/dist/api-keys/index.d.ts.map +1 -0
  18. package/dist/api-keys/index.js +23 -0
  19. package/dist/api-keys/index.js.map +1 -0
  20. package/dist/api-keys/policy.d.ts +16 -0
  21. package/dist/api-keys/policy.d.ts.map +1 -0
  22. package/dist/api-keys/policy.js +95 -0
  23. package/dist/api-keys/policy.js.map +1 -0
  24. package/dist/api-keys/responses.d.ts +6 -0
  25. package/dist/api-keys/responses.d.ts.map +1 -0
  26. package/dist/api-keys/responses.js +54 -0
  27. package/dist/api-keys/responses.js.map +1 -0
  28. package/dist/api-keys/scopes.d.ts +13 -0
  29. package/dist/api-keys/scopes.d.ts.map +1 -0
  30. package/dist/api-keys/scopes.js +49 -0
  31. package/dist/api-keys/scopes.js.map +1 -0
  32. package/dist/api-keys/types.d.ts +173 -0
  33. package/dist/api-keys/types.d.ts.map +1 -0
  34. package/dist/api-keys/types.js +2 -0
  35. package/dist/api-keys/types.js.map +1 -0
  36. package/dist/audit/audit-log.d.ts +35 -0
  37. package/dist/audit/audit-log.d.ts.map +1 -0
  38. package/dist/audit/audit-log.js +277 -0
  39. package/dist/audit/audit-log.js.map +1 -0
  40. package/dist/audit/cursor.d.ts +13 -0
  41. package/dist/audit/cursor.d.ts.map +1 -0
  42. package/dist/audit/cursor.js +26 -0
  43. package/dist/audit/cursor.js.map +1 -0
  44. package/dist/audit/diff.d.ts +19 -0
  45. package/dist/audit/diff.d.ts.map +1 -0
  46. package/dist/audit/diff.js +59 -0
  47. package/dist/audit/diff.js.map +1 -0
  48. package/dist/audit/index.d.ts +25 -0
  49. package/dist/audit/index.d.ts.map +1 -0
  50. package/dist/audit/index.js +24 -0
  51. package/dist/audit/index.js.map +1 -0
  52. package/dist/audit/json.d.ts +23 -0
  53. package/dist/audit/json.d.ts.map +1 -0
  54. package/dist/audit/json.js +120 -0
  55. package/dist/audit/json.js.map +1 -0
  56. package/dist/audit/types.d.ts +118 -0
  57. package/dist/audit/types.d.ts.map +1 -0
  58. package/dist/audit/types.js +2 -0
  59. package/dist/audit/types.js.map +1 -0
  60. package/dist/idempotency/http.d.ts +27 -0
  61. package/dist/idempotency/http.d.ts.map +1 -0
  62. package/dist/idempotency/http.js +130 -0
  63. package/dist/idempotency/http.js.map +1 -0
  64. package/dist/idempotency/idempotency.d.ts +45 -0
  65. package/dist/idempotency/idempotency.d.ts.map +1 -0
  66. package/dist/idempotency/idempotency.js +302 -0
  67. package/dist/idempotency/idempotency.js.map +1 -0
  68. package/dist/idempotency/index.d.ts +25 -0
  69. package/dist/idempotency/index.d.ts.map +1 -0
  70. package/dist/idempotency/index.js +24 -0
  71. package/dist/idempotency/index.js.map +1 -0
  72. package/dist/idempotency/keys.d.ts +13 -0
  73. package/dist/idempotency/keys.d.ts.map +1 -0
  74. package/dist/idempotency/keys.js +9 -0
  75. package/dist/idempotency/keys.js.map +1 -0
  76. package/dist/idempotency/types.d.ts +92 -0
  77. package/dist/idempotency/types.d.ts.map +1 -0
  78. package/dist/idempotency/types.js +2 -0
  79. package/dist/idempotency/types.js.map +1 -0
  80. package/dist/index.d.ts +122 -0
  81. package/dist/index.d.ts.map +1 -0
  82. package/dist/index.js +201 -0
  83. package/dist/index.js.map +1 -0
  84. package/dist/internal/cidr.d.ts +21 -0
  85. package/dist/internal/cidr.d.ts.map +1 -0
  86. package/dist/internal/cidr.js +159 -0
  87. package/dist/internal/cidr.js.map +1 -0
  88. package/dist/internal/clock.d.ts +6 -0
  89. package/dist/internal/clock.d.ts.map +1 -0
  90. package/dist/internal/clock.js +4 -0
  91. package/dist/internal/clock.js.map +1 -0
  92. package/dist/internal/crypto.d.ts +44 -0
  93. package/dist/internal/crypto.d.ts.map +1 -0
  94. package/dist/internal/crypto.js +97 -0
  95. package/dist/internal/crypto.js.map +1 -0
  96. package/dist/internal/db.d.ts +14 -0
  97. package/dist/internal/db.d.ts.map +1 -0
  98. package/dist/internal/db.js +20 -0
  99. package/dist/internal/db.js.map +1 -0
  100. package/dist/internal/duration.d.ts +25 -0
  101. package/dist/internal/duration.d.ts.map +1 -0
  102. package/dist/internal/duration.js +39 -0
  103. package/dist/internal/duration.js.map +1 -0
  104. package/dist/internal/errors.d.ts +33 -0
  105. package/dist/internal/errors.d.ts.map +1 -0
  106. package/dist/internal/errors.js +29 -0
  107. package/dist/internal/errors.js.map +1 -0
  108. package/dist/internal/logger.d.ts +37 -0
  109. package/dist/internal/logger.d.ts.map +1 -0
  110. package/dist/internal/logger.js +54 -0
  111. package/dist/internal/logger.js.map +1 -0
  112. package/dist/internal/standard-schema.d.ts +49 -0
  113. package/dist/internal/standard-schema.d.ts.map +1 -0
  114. package/dist/internal/standard-schema.js +16 -0
  115. package/dist/internal/standard-schema.js.map +1 -0
  116. package/dist/internal/table.d.ts +7 -0
  117. package/dist/internal/table.d.ts.map +1 -0
  118. package/dist/internal/table.js +20 -0
  119. package/dist/internal/table.js.map +1 -0
  120. package/dist/jobs/backoff.d.ts +7 -0
  121. package/dist/jobs/backoff.d.ts.map +1 -0
  122. package/dist/jobs/backoff.js +10 -0
  123. package/dist/jobs/backoff.js.map +1 -0
  124. package/dist/jobs/index.d.ts +31 -0
  125. package/dist/jobs/index.d.ts.map +1 -0
  126. package/dist/jobs/index.js +29 -0
  127. package/dist/jobs/index.js.map +1 -0
  128. package/dist/jobs/jobs.d.ts +42 -0
  129. package/dist/jobs/jobs.d.ts.map +1 -0
  130. package/dist/jobs/jobs.js +741 -0
  131. package/dist/jobs/jobs.js.map +1 -0
  132. package/dist/jobs/types.d.ts +199 -0
  133. package/dist/jobs/types.d.ts.map +1 -0
  134. package/dist/jobs/types.js +2 -0
  135. package/dist/jobs/types.js.map +1 -0
  136. package/dist/next/client-ip.d.ts +42 -0
  137. package/dist/next/client-ip.d.ts.map +1 -0
  138. package/dist/next/client-ip.js +59 -0
  139. package/dist/next/client-ip.js.map +1 -0
  140. package/dist/next/index.d.ts +29 -0
  141. package/dist/next/index.d.ts.map +1 -0
  142. package/dist/next/index.js +29 -0
  143. package/dist/next/index.js.map +1 -0
  144. package/dist/next/jobs-route.d.ts +31 -0
  145. package/dist/next/jobs-route.d.ts.map +1 -0
  146. package/dist/next/jobs-route.js +53 -0
  147. package/dist/next/jobs-route.js.map +1 -0
  148. package/dist/next/response.d.ts +6 -0
  149. package/dist/next/response.d.ts.map +1 -0
  150. package/dist/next/response.js +25 -0
  151. package/dist/next/response.js.map +1 -0
  152. package/dist/next/route-helpers.d.ts +67 -0
  153. package/dist/next/route-helpers.d.ts.map +1 -0
  154. package/dist/next/route-helpers.js +93 -0
  155. package/dist/next/route-helpers.js.map +1 -0
  156. package/dist/rate-limit/index.d.ts +19 -0
  157. package/dist/rate-limit/index.d.ts.map +1 -0
  158. package/dist/rate-limit/index.js +18 -0
  159. package/dist/rate-limit/index.js.map +1 -0
  160. package/dist/rate-limit/limiter.d.ts +60 -0
  161. package/dist/rate-limit/limiter.d.ts.map +1 -0
  162. package/dist/rate-limit/limiter.js +147 -0
  163. package/dist/rate-limit/limiter.js.map +1 -0
  164. package/dist/rate-limit/memory-store.d.ts +23 -0
  165. package/dist/rate-limit/memory-store.d.ts.map +1 -0
  166. package/dist/rate-limit/memory-store.js +77 -0
  167. package/dist/rate-limit/memory-store.js.map +1 -0
  168. package/dist/rate-limit/policy.d.ts +31 -0
  169. package/dist/rate-limit/policy.d.ts.map +1 -0
  170. package/dist/rate-limit/policy.js +174 -0
  171. package/dist/rate-limit/policy.js.map +1 -0
  172. package/dist/rate-limit/postgres-store.d.ts +13 -0
  173. package/dist/rate-limit/postgres-store.d.ts.map +1 -0
  174. package/dist/rate-limit/postgres-store.js +129 -0
  175. package/dist/rate-limit/postgres-store.js.map +1 -0
  176. package/dist/rate-limit/redis-store.d.ts +24 -0
  177. package/dist/rate-limit/redis-store.d.ts.map +1 -0
  178. package/dist/rate-limit/redis-store.js +97 -0
  179. package/dist/rate-limit/redis-store.js.map +1 -0
  180. package/dist/rate-limit/types.d.ts +140 -0
  181. package/dist/rate-limit/types.d.ts.map +1 -0
  182. package/dist/rate-limit/types.js +2 -0
  183. package/dist/rate-limit/types.js.map +1 -0
  184. package/dist/schema/api-keys.d.ts +545 -0
  185. package/dist/schema/api-keys.d.ts.map +1 -0
  186. package/dist/schema/api-keys.js +83 -0
  187. package/dist/schema/api-keys.js.map +1 -0
  188. package/dist/schema/audit-log.d.ts +248 -0
  189. package/dist/schema/audit-log.d.ts.map +1 -0
  190. package/dist/schema/audit-log.js +56 -0
  191. package/dist/schema/audit-log.js.map +1 -0
  192. package/dist/schema/factory.d.ts +19 -0
  193. package/dist/schema/factory.d.ts.map +1 -0
  194. package/dist/schema/factory.js +39 -0
  195. package/dist/schema/factory.js.map +1 -0
  196. package/dist/schema/idempotency-keys.d.ts +178 -0
  197. package/dist/schema/idempotency-keys.d.ts.map +1 -0
  198. package/dist/schema/idempotency-keys.js +41 -0
  199. package/dist/schema/idempotency-keys.js.map +1 -0
  200. package/dist/schema/index.d.ts +61 -0
  201. package/dist/schema/index.d.ts.map +1 -0
  202. package/dist/schema/index.js +28 -0
  203. package/dist/schema/index.js.map +1 -0
  204. package/dist/schema/jobs.d.ts +487 -0
  205. package/dist/schema/jobs.d.ts.map +1 -0
  206. package/dist/schema/jobs.js +103 -0
  207. package/dist/schema/jobs.js.map +1 -0
  208. package/dist/schema/rate-limits.d.ts +108 -0
  209. package/dist/schema/rate-limits.d.ts.map +1 -0
  210. package/dist/schema/rate-limits.js +27 -0
  211. package/dist/schema/rate-limits.js.map +1 -0
  212. package/dist/testing/index.d.ts +25 -0
  213. package/dist/testing/index.d.ts.map +1 -0
  214. package/dist/testing/index.js +37 -0
  215. package/dist/testing/index.js.map +1 -0
  216. package/package.json +117 -4
@@ -0,0 +1 @@
1
+ {"version":3,"file":"db.js","sourceRoot":"","sources":["../../src/internal/db.ts"],"names":[],"mappings":"AAWA,iGAAiG;AACjG,MAAM,UAAU,MAAM,CAAI,MAAe;IACvC,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC;QAAE,OAAO,MAAa,CAAC;IAChD,IAAI,MAAM,KAAK,IAAI,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;QAClD,MAAM,IAAI,GAAI,MAA6B,CAAC,IAAI,CAAC;QACjD,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC;YAAE,OAAO,IAAW,CAAC;IAC9C,CAAC;IACD,MAAM,IAAI,SAAS,CAAC,qEAAqE,CAAC,CAAC;AAC7F,CAAC;AAED,yFAAyF;AACzF,MAAM,UAAU,QAAQ,CAAC,KAAc;IACrC,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC;IAC5C,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;IACjF,MAAM,IAAI,SAAS,CAAC,4CAA4C,OAAO,KAAK,GAAG,CAAC,CAAC;AACnF,CAAC"}
@@ -0,0 +1,25 @@
1
+ /** Units accepted in a {@link Duration}. Months and years are left out because their length varies. */
2
+ export type DurationUnit = "ms" | "s" | "m" | "h" | "d" | "w";
3
+ /**
4
+ * A length of time written as a number and a unit: `"500ms"`, `"30s"`, `"15m"`, `"24h"`, `"90d"`, `"2w"`.
5
+ *
6
+ * Bare numbers are not accepted, so `60` can never be misread as seconds when milliseconds were meant.
7
+ * Build one from a variable with a template literal: `` `${days}d` ``.
8
+ */
9
+ export type Duration = `${number}${DurationUnit}`;
10
+ export interface ParseDurationOptions {
11
+ /** Name of the option being parsed, used in the error message (for example `"window"`). */
12
+ readonly label?: string;
13
+ /** Smallest accepted value in milliseconds. Defaults to 0. */
14
+ readonly minMs?: number;
15
+ }
16
+ /** Whether `value` is a well-formed {@link Duration} string. */
17
+ export declare function isDuration(value: unknown): value is Duration;
18
+ /**
19
+ * Converts a {@link Duration} to whole milliseconds.
20
+ *
21
+ * Throws a `GroundworkError` with code `invalid_argument` for anything malformed, negative,
22
+ * below `minMs`, or too large to be represented exactly.
23
+ */
24
+ export declare function parseDuration(value: Duration, options?: ParseDurationOptions): number;
25
+ //# sourceMappingURL=duration.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"duration.d.ts","sourceRoot":"","sources":["../../src/internal/duration.ts"],"names":[],"mappings":"AAEA,uGAAuG;AACvG,MAAM,MAAM,YAAY,GAAG,IAAI,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC;AAE9D;;;;;GAKG;AACH,MAAM,MAAM,QAAQ,GAAG,GAAG,MAAM,GAAG,YAAY,EAAE,CAAC;AAalD,MAAM,WAAW,oBAAoB;IACnC,2FAA2F;IAC3F,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,8DAA8D;IAC9D,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;CACzB;AAED,gEAAgE;AAChE,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,QAAQ,CAE5D;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,QAAQ,EAAE,OAAO,GAAE,oBAAyB,GAAG,MAAM,CAuBzF"}
@@ -0,0 +1,39 @@
1
+ import { GroundworkError } from "./errors.js";
2
+ const UNIT_MS = {
3
+ ms: 1,
4
+ s: 1_000,
5
+ m: 60_000,
6
+ h: 3_600_000,
7
+ d: 86_400_000,
8
+ w: 604_800_000,
9
+ };
10
+ const DURATION_PATTERN = /^(\d+(?:\.\d+)?)(ms|s|m|h|d|w)$/;
11
+ /** Whether `value` is a well-formed {@link Duration} string. */
12
+ export function isDuration(value) {
13
+ return typeof value === "string" && DURATION_PATTERN.test(value);
14
+ }
15
+ /**
16
+ * Converts a {@link Duration} to whole milliseconds.
17
+ *
18
+ * Throws a `GroundworkError` with code `invalid_argument` for anything malformed, negative,
19
+ * below `minMs`, or too large to be represented exactly.
20
+ */
21
+ export function parseDuration(value, options = {}) {
22
+ const label = options.label ?? "duration";
23
+ const match = typeof value === "string" ? DURATION_PATTERN.exec(value) : null;
24
+ if (!match) {
25
+ throw new GroundworkError("invalid_argument", `Invalid ${label} ${JSON.stringify(value)}: use a number followed by ms, s, m, h, d or w (for example "15m").`);
26
+ }
27
+ const amount = Number(match[1]);
28
+ const unit = match[2];
29
+ const ms = Math.round(amount * UNIT_MS[unit]);
30
+ if (!Number.isSafeInteger(ms)) {
31
+ throw new GroundworkError("invalid_argument", `Invalid ${label} "${value}": it is too large.`);
32
+ }
33
+ const minMs = options.minMs ?? 0;
34
+ if (ms < minMs) {
35
+ throw new GroundworkError("invalid_argument", `Invalid ${label} "${value}": it must be at least ${minMs}ms.`);
36
+ }
37
+ return ms;
38
+ }
39
+ //# sourceMappingURL=duration.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"duration.js","sourceRoot":"","sources":["../../src/internal/duration.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAa9C,MAAM,OAAO,GAA2C;IACtD,EAAE,EAAE,CAAC;IACL,CAAC,EAAE,KAAK;IACR,CAAC,EAAE,MAAM;IACT,CAAC,EAAE,SAAS;IACZ,CAAC,EAAE,UAAU;IACb,CAAC,EAAE,WAAW;CACf,CAAC;AAEF,MAAM,gBAAgB,GAAG,iCAAiC,CAAC;AAS3D,gEAAgE;AAChE,MAAM,UAAU,UAAU,CAAC,KAAc;IACvC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,gBAAgB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AACnE,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,aAAa,CAAC,KAAe,EAAE,OAAO,GAAyB,EAAE;IAC/E,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,IAAI,UAAU,CAAC;IAC1C,MAAM,KAAK,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,gBAAgB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IAC9E,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,MAAM,IAAI,eAAe,CACvB,kBAAkB,EAClB,WAAW,KAAK,IAAI,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,qEAAqE,CAC/G,CAAC;IACJ,CAAC;IACD,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IAChC,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAiB,CAAC;IACtC,MAAM,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;IAC9C,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,EAAE,CAAC,EAAE,CAAC;QAC9B,MAAM,IAAI,eAAe,CAAC,kBAAkB,EAAE,WAAW,KAAK,KAAK,KAAK,qBAAqB,CAAC,CAAC;IACjG,CAAC;IACD,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,IAAI,CAAC,CAAC;IACjC,IAAI,EAAE,GAAG,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,eAAe,CACvB,kBAAkB,EAClB,WAAW,KAAK,KAAK,KAAK,0BAA0B,KAAK,KAAK,CAC/D,CAAC;IACJ,CAAC;IACD,OAAO,EAAE,CAAC;AACZ,CAAC"}
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Machine-readable reason for a {@link GroundworkError}.
3
+ *
4
+ * - `invalid_config`: options passed when creating a module are wrong. Thrown at startup.
5
+ * - `invalid_argument`: a value passed to a method is wrong (for example a malformed duration).
6
+ */
7
+ export type GroundworkErrorCode = "invalid_config" | "invalid_argument";
8
+ declare const BRAND: unique symbol;
9
+ /**
10
+ * The only error class groundwork throws on purpose. Branch on `code`, not on the message.
11
+ *
12
+ * Messages never contain secrets, API keys or rate-limit subjects.
13
+ */
14
+ export declare class GroundworkError extends Error {
15
+ readonly name = "GroundworkError";
16
+ readonly code: GroundworkErrorCode;
17
+ readonly [BRAND] = true;
18
+ constructor(code: GroundworkErrorCode, message: string, options?: {
19
+ readonly cause?: unknown;
20
+ });
21
+ }
22
+ /**
23
+ * Whether `error` is a {@link GroundworkError}. Unlike `instanceof`, this also works when two
24
+ * copies of groundwork end up in one dependency tree.
25
+ */
26
+ export declare function isGroundworkError(error: unknown): error is GroundworkError;
27
+ /** An error's name and message, for log context. Never the stack, which can hold arguments. */
28
+ export declare function errorText(error: unknown): {
29
+ name: string;
30
+ message: string;
31
+ };
32
+ export {};
33
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/internal/errors.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,MAAM,mBAAmB,GAAG,gBAAgB,GAAG,kBAAkB,CAAC;AAExE,QAAA,MAAM,KAAK,EAAE,OAAO,MAAuC,CAAC;AAE5D;;;;GAIG;AACH,qBAAa,eAAgB,SAAQ,KAAK;IACxC,SAAkB,IAAI,qBAAqB;IAC3C,QAAQ,CAAC,IAAI,EAAE,mBAAmB,CAAC;IACnC,QAAQ,CAAC,CAAC,KAAK,CAAC,QAAQ;IAExB,YAAY,IAAI,EAAE,mBAAmB,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;QAAE,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE,EAG7F;CACF;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,eAAe,CAI1E;AAED,+FAA+F;AAC/F,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAG3E"}
@@ -0,0 +1,29 @@
1
+ const BRAND = Symbol.for("groundwork.error");
2
+ /**
3
+ * The only error class groundwork throws on purpose. Branch on `code`, not on the message.
4
+ *
5
+ * Messages never contain secrets, API keys or rate-limit subjects.
6
+ */
7
+ export class GroundworkError extends Error {
8
+ name = "GroundworkError";
9
+ code;
10
+ [BRAND] = true;
11
+ constructor(code, message, options) {
12
+ super(message, options);
13
+ this.code = code;
14
+ }
15
+ }
16
+ /**
17
+ * Whether `error` is a {@link GroundworkError}. Unlike `instanceof`, this also works when two
18
+ * copies of groundwork end up in one dependency tree.
19
+ */
20
+ export function isGroundworkError(error) {
21
+ return (typeof error === "object" && error !== null && error[BRAND] === true);
22
+ }
23
+ /** An error's name and message, for log context. Never the stack, which can hold arguments. */
24
+ export function errorText(error) {
25
+ if (error instanceof Error)
26
+ return { name: error.name, message: error.message };
27
+ return { name: "Error", message: String(error) };
28
+ }
29
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/internal/errors.ts"],"names":[],"mappings":"AAQA,MAAM,KAAK,GAAkB,MAAM,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAC;AAE5D;;;;GAIG;AACH,MAAM,OAAO,eAAgB,SAAQ,KAAK;IACtB,IAAI,GAAG,iBAAiB,CAAC;IAClC,IAAI,CAAsB;IAC1B,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC;IAExB,YAAY,IAAyB,EAAE,OAAe,EAAE,OAAsC;QAC5F,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAAC,KAAc;IAC9C,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAK,KAA+B,CAAC,KAAK,CAAC,KAAK,IAAI,CAChG,CAAC;AACJ,CAAC;AAED,+FAA+F;AAC/F,MAAM,UAAU,SAAS,CAAC,KAAc;IACtC,IAAI,KAAK,YAAY,KAAK;QAAE,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC;IAChF,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;AACnD,CAAC"}
@@ -0,0 +1,37 @@
1
+ /** Structured context attached to a log line. groundwork never puts secrets, raw keys or raw subjects in it. */
2
+ export type LogContext = Readonly<Record<string, unknown>>;
3
+ /** What groundwork logs through. `console` fits as is; wrap pino in {@link pinoLogger}. */
4
+ export interface Logger {
5
+ debug?(message: string, context?: LogContext): void;
6
+ info?(message: string, context?: LogContext): void;
7
+ warn(message: string, context?: LogContext): void;
8
+ error(message: string, context?: LogContext): void;
9
+ }
10
+ /** The default: warnings and errors go to the console with a `[groundwork]` prefix; debug and info are dropped. */
11
+ export declare const consoleLogger: Logger;
12
+ /** A logger whose methods take the context first, as pino's and bunyan's do. */
13
+ export interface ContextFirstLogger {
14
+ debug?(context: object, message: string): void;
15
+ info?(context: object, message: string): void;
16
+ warn(context: object, message: string): void;
17
+ error(context: object, message: string): void;
18
+ }
19
+ /**
20
+ * Adapts pino, or any logger that takes `(context, message)`, to groundwork's
21
+ * `(message, context)`: `createGroundwork({ ..., logger: pinoLogger(logger) })`. Levels the
22
+ * logger doesn't have are left out, and each call keeps the logger as `this`.
23
+ */
24
+ export declare function pinoLogger(logger: ContextFirstLogger): Logger;
25
+ /** A logger that drops everything. Handy in tests. */
26
+ export declare const silentLogger: Logger;
27
+ export type LogLevel = "debug" | "info" | "warn" | "error";
28
+ /**
29
+ * Wraps a logger so each `key` is logged at most once, for messages that would otherwise repeat on
30
+ * every request (such as "falling back to the in-memory rate limiter"). The memory of what was
31
+ * logged belongs to this wrapper, not to the module, so there is no global state.
32
+ */
33
+ export declare function createOnceLogger(logger: Logger): {
34
+ once(level: LogLevel, key: string, message: string, context?: LogContext): boolean;
35
+ reset(key?: string): void;
36
+ };
37
+ //# sourceMappingURL=logger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/internal/logger.ts"],"names":[],"mappings":"AAAA,gHAAgH;AAChH,MAAM,MAAM,UAAU,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;AAE3D,2FAA2F;AAC3F,MAAM,WAAW,MAAM;IACrB,KAAK,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,UAAU,GAAG,IAAI,CAAC;IACpD,IAAI,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,UAAU,GAAG,IAAI,CAAC;IACnD,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,UAAU,GAAG,IAAI,CAAC;IAClD,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,UAAU,GAAG,IAAI,CAAC;CACpD;AAED,mHAAmH;AACnH,eAAO,MAAM,aAAa,EAAE,MAS3B,CAAC;AAEF,gFAAgF;AAChF,MAAM,WAAW,kBAAkB;IACjC,KAAK,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/C,IAAI,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9C,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7C,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;CAC/C;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,kBAAkB,GAAG,MAAM,CAQ7D;AAED,sDAAsD;AACtD,eAAO,MAAM,YAAY,EAAE,MAG1B,CAAC;AAEF,MAAM,MAAM,QAAQ,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;AAE3D;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG;IAChD,IAAI,CAAC,KAAK,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,UAAU,GAAG,OAAO,CAAC;IACnF,KAAK,CAAC,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAC3B,CAcA"}
@@ -0,0 +1,54 @@
1
+ /** The default: warnings and errors go to the console with a `[groundwork]` prefix; debug and info are dropped. */
2
+ export const consoleLogger = {
3
+ warn: (message, context) => context
4
+ ? console.warn(`[groundwork] ${message}`, context)
5
+ : console.warn(`[groundwork] ${message}`),
6
+ error: (message, context) => context
7
+ ? console.error(`[groundwork] ${message}`, context)
8
+ : console.error(`[groundwork] ${message}`),
9
+ };
10
+ /**
11
+ * Adapts pino, or any logger that takes `(context, message)`, to groundwork's
12
+ * `(message, context)`: `createGroundwork({ ..., logger: pinoLogger(logger) })`. Levels the
13
+ * logger doesn't have are left out, and each call keeps the logger as `this`.
14
+ */
15
+ export function pinoLogger(logger) {
16
+ const adapted = {
17
+ warn: (message, context) => logger.warn(context ?? {}, message),
18
+ error: (message, context) => logger.error(context ?? {}, message),
19
+ };
20
+ if (logger.debug)
21
+ adapted.debug = (message, context) => logger.debug?.(context ?? {}, message);
22
+ if (logger.info)
23
+ adapted.info = (message, context) => logger.info?.(context ?? {}, message);
24
+ return adapted;
25
+ }
26
+ /** A logger that drops everything. Handy in tests. */
27
+ export const silentLogger = {
28
+ warn: () => { },
29
+ error: () => { },
30
+ };
31
+ /**
32
+ * Wraps a logger so each `key` is logged at most once, for messages that would otherwise repeat on
33
+ * every request (such as "falling back to the in-memory rate limiter"). The memory of what was
34
+ * logged belongs to this wrapper, not to the module, so there is no global state.
35
+ */
36
+ export function createOnceLogger(logger) {
37
+ const seen = new Set();
38
+ return {
39
+ once(level, key, message, context) {
40
+ if (seen.has(key))
41
+ return false;
42
+ seen.add(key);
43
+ logger[level]?.(message, context);
44
+ return true;
45
+ },
46
+ reset(key) {
47
+ if (key === undefined)
48
+ seen.clear();
49
+ else
50
+ seen.delete(key);
51
+ },
52
+ };
53
+ }
54
+ //# sourceMappingURL=logger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.js","sourceRoot":"","sources":["../../src/internal/logger.ts"],"names":[],"mappings":"AAWA,mHAAmH;AACnH,MAAM,CAAC,MAAM,aAAa,GAAW;IACnC,IAAI,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,CACzB,OAAO;QACL,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,gBAAgB,OAAO,EAAE,EAAE,OAAO,CAAC;QAClD,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,gBAAgB,OAAO,EAAE,CAAC;IAC7C,KAAK,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,CAC1B,OAAO;QACL,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,gBAAgB,OAAO,EAAE,EAAE,OAAO,CAAC;QACnD,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,gBAAgB,OAAO,EAAE,CAAC;CAC/C,CAAC;AAUF;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,MAA0B;IACnD,MAAM,OAAO,GAAW;QACtB,IAAI,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,IAAI,EAAE,EAAE,OAAO,CAAC;QAC/D,KAAK,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,IAAI,EAAE,EAAE,OAAO,CAAC;KAClE,CAAC;IACF,IAAI,MAAM,CAAC,KAAK;QAAE,OAAO,CAAC,KAAK,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,OAAO,IAAI,EAAE,EAAE,OAAO,CAAC,CAAC;IAC/F,IAAI,MAAM,CAAC,IAAI;QAAE,OAAO,CAAC,IAAI,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,OAAO,IAAI,EAAE,EAAE,OAAO,CAAC,CAAC;IAC5F,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,sDAAsD;AACtD,MAAM,CAAC,MAAM,YAAY,GAAW;IAClC,IAAI,EAAE,GAAG,EAAE,GAAE,CAAC;IACd,KAAK,EAAE,GAAG,EAAE,GAAE,CAAC;CAChB,CAAC;AAIF;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAAC,MAAc;IAI7C,MAAM,IAAI,GAAG,IAAI,GAAG,EAAU,CAAC;IAC/B,OAAO;QACL,IAAI,CAAC,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE,OAAO;YAC/B,IAAI,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;gBAAE,OAAO,KAAK,CAAC;YAChC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;YACd,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;YAClC,OAAO,IAAI,CAAC;QACd,CAAC;QACD,KAAK,CAAC,GAAG;YACP,IAAI,GAAG,KAAK,SAAS;gBAAE,IAAI,CAAC,KAAK,EAAE,CAAC;;gBAC/B,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACxB,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,49 @@
1
+ /**
2
+ * The Standard Schema v1 interface (https://standardschema.dev), copied as the spec invites so
3
+ * that groundwork has no runtime dependency for it. zod, valibot, arktype and others implement it.
4
+ * Source: @standard-schema/spec 1.1.0 (MIT).
5
+ */
6
+ export interface StandardSchemaV1<Input = unknown, Output = Input> {
7
+ readonly "~standard": StandardSchemaV1.Props<Input, Output>;
8
+ }
9
+ export declare namespace StandardSchemaV1 {
10
+ interface Props<Input = unknown, Output = Input> {
11
+ readonly version: 1;
12
+ readonly vendor: string;
13
+ readonly validate: (value: unknown, options?: Options | undefined) => Result<Output> | Promise<Result<Output>>;
14
+ readonly types?: Types<Input, Output> | undefined;
15
+ }
16
+ type Result<Output> = SuccessResult<Output> | FailureResult;
17
+ interface SuccessResult<Output> {
18
+ readonly value: Output;
19
+ readonly issues?: undefined;
20
+ }
21
+ interface Options {
22
+ readonly libraryOptions?: Record<string, unknown> | undefined;
23
+ }
24
+ interface FailureResult {
25
+ readonly issues: ReadonlyArray<Issue>;
26
+ }
27
+ interface Issue {
28
+ readonly message: string;
29
+ readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
30
+ }
31
+ interface PathSegment {
32
+ readonly key: PropertyKey;
33
+ }
34
+ interface Types<Input = unknown, Output = Input> {
35
+ readonly input: Input;
36
+ readonly output: Output;
37
+ }
38
+ type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["input"];
39
+ type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["output"];
40
+ }
41
+ /** Runs a schema and returns its output, or throws with every issue in one readable message. */
42
+ export declare function validateWith<Output>(schema: StandardSchemaV1<unknown, Output>, value: unknown): Promise<{
43
+ ok: true;
44
+ value: Output;
45
+ } | {
46
+ ok: false;
47
+ message: string;
48
+ }>;
49
+ //# sourceMappingURL=standard-schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"standard-schema.d.ts","sourceRoot":"","sources":["../../src/internal/standard-schema.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,WAAW,gBAAgB,CAAC,KAAK,GAAG,OAAO,EAAE,MAAM,GAAG,KAAK;IAC/D,QAAQ,CAAC,WAAW,EAAE,gBAAgB,CAAC,KAAK,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;CAC7D;AAED,MAAM,CAAC,OAAO,WAAW,gBAAgB,CAAC;IACxC,UAAU,KAAK,CAAC,KAAK,GAAG,OAAO,EAAE,MAAM,GAAG,KAAK;QAC7C,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC;QACpB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;QACxB,QAAQ,CAAC,QAAQ,EAAE,CACjB,KAAK,EAAE,OAAO,EACd,OAAO,CAAC,EAAE,OAAO,GAAG,SAAS,KAC1B,MAAM,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC;QAC9C,QAAQ,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,SAAS,CAAC;KACnD;IACD,KAAK,MAAM,CAAC,MAAM,IAAI,aAAa,CAAC,MAAM,CAAC,GAAG,aAAa,CAAC;IAC5D,UAAU,aAAa,CAAC,MAAM;QAC5B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;QACvB,QAAQ,CAAC,MAAM,CAAC,EAAE,SAAS,CAAC;KAC7B;IACD,UAAU,OAAO;QACf,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAC;KAC/D;IACD,UAAU,aAAa;QACrB,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC,KAAK,CAAC,CAAC;KACvC;IACD,UAAU,KAAK;QACb,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;QACzB,QAAQ,CAAC,IAAI,CAAC,EAAE,aAAa,CAAC,WAAW,GAAG,WAAW,CAAC,GAAG,SAAS,CAAC;KACtE;IACD,UAAU,WAAW;QACnB,QAAQ,CAAC,GAAG,EAAE,WAAW,CAAC;KAC3B;IACD,UAAU,KAAK,CAAC,KAAK,GAAG,OAAO,EAAE,MAAM,GAAG,KAAK;QAC7C,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;QACtB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;KACzB;IACD,KAAK,UAAU,CAAC,MAAM,SAAS,gBAAgB,IAAI,WAAW,CAC5D,MAAM,CAAC,WAAW,CAAC,CAAC,OAAO,CAAC,CAC7B,CAAC,OAAO,CAAC,CAAC;IACX,KAAK,WAAW,CAAC,MAAM,SAAS,gBAAgB,IAAI,WAAW,CAC7D,MAAM,CAAC,WAAW,CAAC,CAAC,OAAO,CAAC,CAC7B,CAAC,QAAQ,CAAC,CAAC;CACb;AAED,gGAAgG;AAChG,wBAAsB,YAAY,CAAC,MAAM,EACvC,MAAM,EAAE,gBAAgB,CAAC,OAAO,EAAE,MAAM,CAAC,EACzC,KAAK,EAAE,OAAO,GACb,OAAO,CAAC;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC,CAYvE"}
@@ -0,0 +1,16 @@
1
+ /** Runs a schema and returns its output, or throws with every issue in one readable message. */
2
+ export async function validateWith(schema, value) {
3
+ const result = await schema["~standard"].validate(value);
4
+ if (result.issues === undefined)
5
+ return { ok: true, value: result.value };
6
+ const message = result.issues
7
+ .map((issue) => {
8
+ const path = (issue.path ?? [])
9
+ .map((segment) => String(typeof segment === "object" ? segment.key : segment))
10
+ .join(".");
11
+ return path ? `${path}: ${issue.message}` : issue.message;
12
+ })
13
+ .join("; ");
14
+ return { ok: false, message };
15
+ }
16
+ //# sourceMappingURL=standard-schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"standard-schema.js","sourceRoot":"","sources":["../../src/internal/standard-schema.ts"],"names":[],"mappings":"AAiDA,gGAAgG;AAChG,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,MAAyC,EACzC,KAAc;IAEd,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,WAAW,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IACzD,IAAI,MAAM,CAAC,MAAM,KAAK,SAAS;QAAE,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,EAAE,CAAC;IAC1E,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM;SAC1B,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;QACb,MAAM,IAAI,GAAG,CAAC,KAAK,CAAC,IAAI,IAAI,EAAE,CAAC;aAC5B,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,MAAM,CAAC,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC;aAC7E,IAAI,CAAC,GAAG,CAAC,CAAC;QACb,OAAO,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,KAAK,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC;IAC5D,CAAC,CAAC;SACD,IAAI,CAAC,IAAI,CAAC,CAAC;IACd,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC;AAChC,CAAC"}
@@ -0,0 +1,7 @@
1
+ import { type Table } from "drizzle-orm";
2
+ /**
3
+ * Checks at startup that `table` has the columns a module's SQL relies on, so passing the wrong
4
+ * table fails with a clear message instead of a SQL error on the first request.
5
+ */
6
+ export declare function assertTableColumns(table: Table, columns: readonly string[], expected: string): void;
7
+ //# sourceMappingURL=table.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../../src/internal/table.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiC,KAAK,KAAK,EAAE,MAAM,aAAa,CAAC;AAGxE;;;GAGG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,KAAK,EACZ,OAAO,EAAE,SAAS,MAAM,EAAE,EAC1B,QAAQ,EAAE,MAAM,GACf,IAAI,CAcN"}
@@ -0,0 +1,20 @@
1
+ import { getTableColumns, getTableName } from "drizzle-orm";
2
+ import { GroundworkError } from "./errors.js";
3
+ /**
4
+ * Checks at startup that `table` has the columns a module's SQL relies on, so passing the wrong
5
+ * table fails with a clear message instead of a SQL error on the first request.
6
+ */
7
+ export function assertTableColumns(table, columns, expected) {
8
+ let names;
9
+ try {
10
+ names = Object.values(getTableColumns(table)).map((column) => column.name);
11
+ }
12
+ catch {
13
+ throw new GroundworkError("invalid_config", `table must be the ${expected}.`);
14
+ }
15
+ const missing = columns.filter((column) => !names.includes(column));
16
+ if (missing.length > 0) {
17
+ throw new GroundworkError("invalid_config", `Table "${getTableName(table)}" is not the ${expected} (missing ${missing.join(", ")}).`);
18
+ }
19
+ }
20
+ //# sourceMappingURL=table.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"table.js","sourceRoot":"","sources":["../../src/internal/table.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,YAAY,EAAc,MAAM,aAAa,CAAC;AACxE,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAE9C;;;GAGG;AACH,MAAM,UAAU,kBAAkB,CAChC,KAAY,EACZ,OAA0B,EAC1B,QAAgB;IAEhB,IAAI,KAAe,CAAC;IACpB,IAAI,CAAC;QACH,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC7E,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,IAAI,eAAe,CAAC,gBAAgB,EAAE,qBAAqB,QAAQ,GAAG,CAAC,CAAC;IAChF,CAAC;IACD,MAAM,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC;IACpE,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACvB,MAAM,IAAI,eAAe,CACvB,gBAAgB,EAChB,UAAU,YAAY,CAAC,KAAK,CAAC,gBAAgB,QAAQ,aAAa,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CACzF,CAAC;IACJ,CAAC;AACH,CAAC"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Exponential backoff with full jitter (AWS's "full jitter"): a random wait between 0 and
3
+ * `min(maxMs, baseMs * 2^(attempt - 1))`. Spreading retries out keeps a burst of failures from
4
+ * retrying in lockstep.
5
+ */
6
+ export declare function backoffDelayMs(attempt: number, baseMs: number, maxMs: number, random: () => number): number;
7
+ //# sourceMappingURL=backoff.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"backoff.d.ts","sourceRoot":"","sources":["../../src/jobs/backoff.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,cAAc,CAC5B,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,MAAM,GACnB,MAAM,CAGR"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Exponential backoff with full jitter (AWS's "full jitter"): a random wait between 0 and
3
+ * `min(maxMs, baseMs * 2^(attempt - 1))`. Spreading retries out keeps a burst of failures from
4
+ * retrying in lockstep.
5
+ */
6
+ export function backoffDelayMs(attempt, baseMs, maxMs, random) {
7
+ const ceiling = Math.min(maxMs, baseMs * 2 ** Math.max(0, attempt - 1));
8
+ return Math.floor(random() * ceiling);
9
+ }
10
+ //# sourceMappingURL=backoff.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"backoff.js","sourceRoot":"","sources":["../../src/jobs/backoff.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAC5B,OAAe,EACf,MAAc,EACd,KAAa,EACb,MAAoB;IAEpB,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,CAAC,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,CAAC,CAAC,CAAC;IACxE,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,GAAG,OAAO,CAAC,CAAC;AACxC,CAAC"}
@@ -0,0 +1,31 @@
1
+ /**
2
+ * `@vantedev/groundwork/jobs`: background jobs and schedules on Postgres, enqueued inside your
3
+ * transactions, with leases, retries with jittered backoff, timeouts and health checks.
4
+ *
5
+ * ```ts
6
+ * import { createJobs } from "@vantedev/groundwork/jobs";
7
+ *
8
+ * const jobs = createJobs({ db, table: tables.jobs, schedulesTable: tables.jobSchedules });
9
+ *
10
+ * export const sendReceipt = jobs.define("email.receipt", {
11
+ * schema: z.object({ orderId: z.string() }),
12
+ * maxAttempts: 5,
13
+ * async run({ orderId }, { signal }) { await mailer.sendReceipt(orderId, { signal }); },
14
+ * });
15
+ *
16
+ * await db.transaction(async (tx) => {
17
+ * await tx.insert(orders).values(order);
18
+ * await sendReceipt.enqueue({ orderId: order.id }, { tx, dedupeKey: `receipt:${order.id}` });
19
+ * });
20
+ *
21
+ * jobs.schedule("reports.nightly", { every: "1d" }, () => buildReports());
22
+ * await jobs.work({ concurrency: 4, signal });
23
+ * ```
24
+ *
25
+ * @module
26
+ */
27
+ export type { StandardSchemaV1 } from "../internal/standard-schema.ts";
28
+ export { backoffDelayMs } from "./backoff.ts";
29
+ export { createJobs, type JobsOptions } from "./jobs.ts";
30
+ export type { BackoffOptions, EnqueueOptions, EnqueueResult, JobContext, JobDefinition, JobDefinitionWithSchema, JobFailure, JobHandle, JobListFilter, JobRecord, JobStatus, Jobs, JobsHealth, JobTypeHealth, RunOnceOptions, RunSummary, ScheduleContext, ScheduleHealth, ScheduleOptions, WorkOptions, } from "./types.ts";
31
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/jobs/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,YAAY,EAAE,gBAAgB,EAAE,MAAM,gCAAgC,CAAC;AACvE,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,UAAU,EAAE,KAAK,WAAW,EAAE,MAAM,WAAW,CAAC;AACzD,YAAY,EACV,cAAc,EACd,cAAc,EACd,aAAa,EACb,UAAU,EACV,aAAa,EACb,uBAAuB,EACvB,UAAU,EACV,SAAS,EACT,aAAa,EACb,SAAS,EACT,SAAS,EACT,IAAI,EACJ,UAAU,EACV,aAAa,EACb,cAAc,EACd,UAAU,EACV,eAAe,EACf,cAAc,EACd,eAAe,EACf,WAAW,GACZ,MAAM,YAAY,CAAC"}
@@ -0,0 +1,29 @@
1
+ /**
2
+ * `@vantedev/groundwork/jobs`: background jobs and schedules on Postgres, enqueued inside your
3
+ * transactions, with leases, retries with jittered backoff, timeouts and health checks.
4
+ *
5
+ * ```ts
6
+ * import { createJobs } from "@vantedev/groundwork/jobs";
7
+ *
8
+ * const jobs = createJobs({ db, table: tables.jobs, schedulesTable: tables.jobSchedules });
9
+ *
10
+ * export const sendReceipt = jobs.define("email.receipt", {
11
+ * schema: z.object({ orderId: z.string() }),
12
+ * maxAttempts: 5,
13
+ * async run({ orderId }, { signal }) { await mailer.sendReceipt(orderId, { signal }); },
14
+ * });
15
+ *
16
+ * await db.transaction(async (tx) => {
17
+ * await tx.insert(orders).values(order);
18
+ * await sendReceipt.enqueue({ orderId: order.id }, { tx, dedupeKey: `receipt:${order.id}` });
19
+ * });
20
+ *
21
+ * jobs.schedule("reports.nightly", { every: "1d" }, () => buildReports());
22
+ * await jobs.work({ concurrency: 4, signal });
23
+ * ```
24
+ *
25
+ * @module
26
+ */
27
+ export { backoffDelayMs } from "./backoff.js";
28
+ export { createJobs } from "./jobs.js";
29
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/jobs/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAGH,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,UAAU,EAAoB,MAAM,WAAW,CAAC"}
@@ -0,0 +1,42 @@
1
+ import type { Clock } from "../internal/clock.ts";
2
+ import { type GroundworkDatabase } from "../internal/db.ts";
3
+ import { type Duration } from "../internal/duration.ts";
4
+ import { type Logger } from "../internal/logger.ts";
5
+ import { type JobSchedulesTable, type JobsTable } from "../schema/jobs.ts";
6
+ import type { Jobs } from "./types.ts";
7
+ export interface JobsOptions {
8
+ readonly db: GroundworkDatabase;
9
+ /** The `jobs` table from `groundworkTables()`. */
10
+ readonly table: JobsTable;
11
+ /** The `jobSchedules` table from `groundworkTables()`. */
12
+ readonly schedulesTable: JobSchedulesTable;
13
+ /**
14
+ * How long a claimed job is reserved for its worker. Workers renew it while the job runs, so a
15
+ * job is only picked up again if its worker stops (a crash, a kill, a lost connection). Defaults
16
+ * to `"1m"`.
17
+ */
18
+ readonly lease?: Duration | undefined;
19
+ /** Identifies this worker in `locked_by`. Defaults to `<hostname>:<pid>:<random>`. */
20
+ readonly workerId?: string | undefined;
21
+ /** Defaults to Postgres's time. Inject one in tests. */
22
+ readonly clock?: Clock | undefined;
23
+ /** Defaults to `console` (warnings and errors only). */
24
+ readonly logger?: Logger | undefined;
25
+ /** For tests: the random source for backoff jitter. Defaults to `Math.random`. */
26
+ readonly random?: (() => number) | undefined;
27
+ }
28
+ /**
29
+ * Creates background jobs and schedules on your Postgres.
30
+ *
31
+ * ```ts
32
+ * const jobs = createJobs({ db, table: tables.jobs, schedulesTable: tables.jobSchedules });
33
+ * export const sendReceipt = jobs.define("email.receipt", {
34
+ * schema: z.object({ orderId: z.string() }),
35
+ * async run({ orderId }, { signal }) { await mailer.send(orderId, { signal }); },
36
+ * });
37
+ * await sendReceipt.enqueue({ orderId }, { tx, dedupeKey: `receipt:${orderId}` });
38
+ * await jobs.work({ signal });
39
+ * ```
40
+ */
41
+ export declare function createJobs(options: JobsOptions): Jobs;
42
+ //# sourceMappingURL=jobs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jobs.d.ts","sourceRoot":"","sources":["../../src/jobs/jobs.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,KAAK,kBAAkB,EAAoB,MAAM,mBAAmB,CAAC;AAC9E,OAAO,EAAE,KAAK,QAAQ,EAA6B,MAAM,yBAAyB,CAAC;AAEnF,OAAO,EAAmC,KAAK,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAGrF,OAAO,EAGL,KAAK,iBAAiB,EACtB,KAAK,SAAS,EACf,MAAM,mBAAmB,CAAC;AAE3B,OAAO,KAAK,EASV,IAAI,EAOL,MAAM,YAAY,CAAC;AAEpB,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,EAAE,EAAE,kBAAkB,CAAC;IAChC,kDAAkD;IAClD,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC;IAC1B,0DAA0D;IAC1D,QAAQ,CAAC,cAAc,EAAE,iBAAiB,CAAC;IAC3C;;;;OAIG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,QAAQ,GAAG,SAAS,CAAC;IACtC,sFAAsF;IACtF,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACvC,wDAAwD;IACxD,QAAQ,CAAC,KAAK,CAAC,EAAE,KAAK,GAAG,SAAS,CAAC;IACnC,wDAAwD;IACxD,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACrC,kFAAkF;IAClF,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,MAAM,CAAC,GAAG,SAAS,CAAC;CAC9C;AAyHD;;;;;;;;;;;;GAYG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,WAAW,GAAG,IAAI,CA60BrD"}