@blamejs/core 0.7.18 → 0.7.19

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 (167) hide show
  1. package/CHANGELOG.md +425 -423
  2. package/README.md +150 -150
  3. package/bin/blamejs.js +0 -0
  4. package/index.js +310 -308
  5. package/lib/api-key.js +660 -660
  6. package/lib/api-snapshot.js +338 -338
  7. package/lib/app-shutdown.js +385 -385
  8. package/lib/app.js +365 -365
  9. package/lib/archive.js +250 -250
  10. package/lib/atomic-file.js +544 -544
  11. package/lib/audit-chain.js +177 -177
  12. package/lib/audit-sign.js +344 -344
  13. package/lib/audit-tools.js +677 -677
  14. package/lib/audit.js +766 -766
  15. package/lib/auth/jwt-external.js +365 -0
  16. package/lib/auth/jwt.js +337 -311
  17. package/lib/auth/lockout.js +436 -436
  18. package/lib/auth/oauth.js +721 -721
  19. package/lib/auth/passkey.js +181 -181
  20. package/lib/auth/password.js +628 -594
  21. package/lib/backup/bundle.js +217 -217
  22. package/lib/backup/crypto.js +176 -176
  23. package/lib/backup/index.js +515 -515
  24. package/lib/backup/manifest.js +282 -282
  25. package/lib/break-glass.js +1338 -1338
  26. package/lib/bundler.js +441 -441
  27. package/lib/cache-redis.js +256 -256
  28. package/lib/cache.js +1206 -1206
  29. package/lib/canonical-json.js +115 -115
  30. package/lib/chain-writer.js +234 -234
  31. package/lib/cli-helpers.js +206 -206
  32. package/lib/cli.js +2334 -2334
  33. package/lib/cluster-provider-db.js +317 -317
  34. package/lib/cluster-storage.js +226 -226
  35. package/lib/cluster.js +703 -703
  36. package/lib/config-drift.js +301 -301
  37. package/lib/consent.js +222 -222
  38. package/lib/constants.js +191 -191
  39. package/lib/cookies.js +315 -315
  40. package/lib/credential-hash.js +322 -322
  41. package/lib/crypto.js +266 -266
  42. package/lib/csv.js +275 -275
  43. package/lib/db-declare-row-policy.js +267 -267
  44. package/lib/db-declare-view.js +420 -420
  45. package/lib/db-query.js +406 -406
  46. package/lib/db-schema.js +319 -319
  47. package/lib/db.js +1288 -1288
  48. package/lib/deprecate.js +222 -222
  49. package/lib/dev.js +335 -335
  50. package/lib/dual-control.js +473 -473
  51. package/lib/error-page.js +420 -420
  52. package/lib/external-db-migrate.js +441 -441
  53. package/lib/external-db.js +1061 -1061
  54. package/lib/file-type.js +273 -273
  55. package/lib/forms.js +422 -422
  56. package/lib/framework-error.js +293 -293
  57. package/lib/framework-schema.js +717 -717
  58. package/lib/handlers.js +350 -350
  59. package/lib/http-client-cookie-jar.js +508 -508
  60. package/lib/http-client.js +1195 -1195
  61. package/lib/i18n.js +878 -878
  62. package/lib/jobs.js +185 -185
  63. package/lib/log-stream-cloudwatch.js +369 -369
  64. package/lib/log-stream-local.js +146 -146
  65. package/lib/log-stream-otlp-grpc.js +410 -410
  66. package/lib/log-stream-otlp.js +286 -286
  67. package/lib/log-stream-syslog.js +302 -302
  68. package/lib/log-stream-webhook.js +199 -199
  69. package/lib/log-stream.js +330 -330
  70. package/lib/log.js +500 -500
  71. package/lib/mail-bounce.js +528 -528
  72. package/lib/mail-dkim.js +369 -369
  73. package/lib/mail.js +981 -981
  74. package/lib/metrics.js +683 -683
  75. package/lib/middleware/api-encrypt.js +936 -936
  76. package/lib/middleware/attach-user.js +157 -157
  77. package/lib/middleware/bearer-auth.js +152 -0
  78. package/lib/middleware/body-parser.js +1170 -1170
  79. package/lib/middleware/bot-guard.js +178 -178
  80. package/lib/middleware/compression.js +452 -452
  81. package/lib/middleware/cors.js +314 -314
  82. package/lib/middleware/csp-nonce.js +348 -348
  83. package/lib/middleware/csrf-protect.js +316 -316
  84. package/lib/middleware/db-role-for.js +264 -264
  85. package/lib/middleware/health.js +392 -392
  86. package/lib/middleware/index.js +82 -79
  87. package/lib/middleware/rate-limit.js +358 -358
  88. package/lib/middleware/request-id.js +61 -61
  89. package/lib/middleware/request-log.js +168 -168
  90. package/lib/middleware/require-auth.js +104 -104
  91. package/lib/middleware/security-headers.js +116 -116
  92. package/lib/middleware/sse.js +166 -166
  93. package/lib/migrations.js +383 -383
  94. package/lib/mtls-ca.js +518 -518
  95. package/lib/mtls-engine-default.js +481 -481
  96. package/lib/network-dns.js +632 -632
  97. package/lib/network-heartbeat.js +290 -290
  98. package/lib/network-nts.js +574 -574
  99. package/lib/network-proxy.js +265 -265
  100. package/lib/network-tls.js +328 -328
  101. package/lib/network.js +233 -233
  102. package/lib/notify.js +612 -612
  103. package/lib/ntp-check.js +229 -229
  104. package/lib/numeric-bounds.js +111 -111
  105. package/lib/object-store/azure-blob-bucket-ops.js +349 -349
  106. package/lib/object-store/azure-blob.js +488 -488
  107. package/lib/object-store/gcs-bucket-ops.js +351 -351
  108. package/lib/object-store/gcs.js +519 -519
  109. package/lib/object-store/http-put.js +153 -153
  110. package/lib/object-store/index.js +197 -197
  111. package/lib/object-store/sigv4-bucket-ops.js +1092 -1092
  112. package/lib/object-store/sigv4.js +903 -903
  113. package/lib/observability.js +151 -151
  114. package/lib/otel-export.js +269 -269
  115. package/lib/pagination.js +464 -464
  116. package/lib/parsers/index.js +80 -80
  117. package/lib/parsers/safe-env.js +642 -642
  118. package/lib/parsers/safe-ini.js +292 -292
  119. package/lib/parsers/safe-toml.js +784 -784
  120. package/lib/parsers/safe-xml.js +390 -390
  121. package/lib/parsers/safe-yaml.js +1015 -1015
  122. package/lib/permissions.js +708 -708
  123. package/lib/pqc-agent.js +87 -87
  124. package/lib/pqc-gate.js +279 -279
  125. package/lib/protobuf-encoder.js +190 -190
  126. package/lib/protocol-dispatcher.js +161 -161
  127. package/lib/pubsub-redis.js +167 -167
  128. package/lib/pubsub.js +429 -429
  129. package/lib/queue-local.js +476 -476
  130. package/lib/queue-redis.js +745 -745
  131. package/lib/queue-sqs.js +319 -319
  132. package/lib/queue.js +695 -695
  133. package/lib/redis-client.js +519 -519
  134. package/lib/request-helpers.js +340 -340
  135. package/lib/restore-bundle.js +237 -237
  136. package/lib/restore-rollback.js +259 -259
  137. package/lib/restore.js +409 -409
  138. package/lib/retry.js +376 -376
  139. package/lib/router.js +748 -748
  140. package/lib/safe-async.js +735 -735
  141. package/lib/safe-buffer.js +237 -237
  142. package/lib/safe-json.js +541 -541
  143. package/lib/safe-schema.js +1266 -1266
  144. package/lib/safe-url.js +159 -159
  145. package/lib/scheduler.js +706 -706
  146. package/lib/security-assert.js +373 -373
  147. package/lib/seeders.js +618 -618
  148. package/lib/session.js +535 -478
  149. package/lib/slug.js +269 -269
  150. package/lib/ssrf-guard.js +401 -401
  151. package/lib/storage.js +471 -471
  152. package/lib/subject.js +281 -281
  153. package/lib/template.js +791 -791
  154. package/lib/testing.js +798 -798
  155. package/lib/time.js +310 -310
  156. package/lib/totp.js +302 -302
  157. package/lib/tracing.js +494 -494
  158. package/lib/uuid.js +132 -132
  159. package/lib/validate-opts.js +340 -340
  160. package/lib/vault/index.js +308 -308
  161. package/lib/vault/rotate.js +784 -784
  162. package/lib/vault/wrap.js +296 -296
  163. package/lib/vendor/noble-ciphers.cjs +9 -9
  164. package/lib/webhook.js +595 -595
  165. package/lib/websocket.js +1048 -1048
  166. package/package.json +77 -77
  167. package/sbom.cyclonedx.json +7 -7
package/lib/pagination.js CHANGED
@@ -1,464 +1,464 @@
1
- "use strict";
2
- /**
3
- * pagination — cursor + offset helpers.
4
- *
5
- * Every CRUD list endpoint reinvents pagination, usually wrong.
6
- * The two failure modes:
7
- *
8
- * - Offset pagination at depth: `LIMIT n OFFSET 50000` makes the DB
9
- * scan-and-skip 50,000 rows. O(n). With concurrent writes, rows
10
- * are also missed/duplicated as new inserts shift the offset.
11
- * - Cursor pagination without a tie-breaker: `WHERE createdAt > ?`
12
- * skips or duplicates rows when two records share createdAt.
13
- *
14
- * This module ships both, done correctly, plus the encode/decode
15
- * primitives operators reach for when their SQL doesn't fit the
16
- * Query-builder shape.
17
- *
18
- * Public API:
19
- *
20
- * var p = b.pagination;
21
- *
22
- * // Cursor: O(1) at any depth. Composite (orderBy, _id) ordering
23
- * // so ties on the orderBy column are broken by _id and rows are
24
- * // never skipped.
25
- * var page = await p.cursor(b.db.from("users"), {
26
- * cursor: req.query.cursor,
27
- * limit: req.query.limit,
28
- * max: 100,
29
- * default: 25,
30
- * orderBy: "_id", // default; use "createdAt" etc.
31
- * direction: "asc", // "asc" | "desc"
32
- * secret: pageSecret, // Buffer or string; HMAC-tag the cursor
33
- * });
34
- * // → { items: [...], nextCursor, prevCursor, limit, hasMore }
35
- *
36
- * // Offset: page-numbered. Ergonomic for legacy clients.
37
- * var off = await p.offset(b.db.from("users"), {
38
- * page: req.query.page,
39
- * perPage: req.query.perPage,
40
- * max: 100,
41
- * default: 25,
42
- * });
43
- * // → { items, total, page, perPage, totalPages, hasMore }
44
- *
45
- * // Low-level — for raw SQL or custom row sources.
46
- * var token = p.encodeCursor({ orderByVal: 12345, id: "abc" }, secret);
47
- * var state = p.decodeCursor(token, secret);
48
- *
49
- * Cursor design:
50
- * - Composite ordering: (orderBy column, _id). _id is the implicit
51
- * tie-breaker, so two rows with identical orderByVal are still
52
- * totally ordered. Forward navigation: WHERE
53
- * (orderByVal > cur.orderByVal) OR
54
- * (orderByVal = cur.orderByVal AND _id > cur.id)
55
- * Backward: same with `<`, then reverse the result set.
56
- * - Cursors are HMAC-tagged. A tampered cursor is detected at decode
57
- * time and rejected with PaginationError. Operators MUST pass
58
- * `secret` (Buffer or string) — there's no auto-derivation, since
59
- * framework-derived secrets would produce surprises across deploys.
60
- * - Cursor format: `<base64url state>.<base64url tag>`. State is
61
- * canonical JSON of `{ v, dir, orderBy, orderByVal, id }`. Tag is
62
- * SHA3-512(secret || stateJson).slice(0, 16).
63
- * - direction is part of the cursor — operators don't need to round-
64
- * trip it via query string. The cursor itself encodes whether it's
65
- * a "next" or "prev" position so navigation stays consistent.
66
- *
67
- * Limit semantics:
68
- * - Operator passes `default` and `max`. The effective limit is
69
- * min(max, requestedLimit || default). Negative or non-integer
70
- * limits are coerced to default.
71
- * - The page query fetches limit+1 to detect hasMore without a
72
- * second COUNT(*) trip.
73
- *
74
- * Offset is the legacy-client tool, not the recommended path. The
75
- * module's offset() returns a `total` (from COUNT(*)) and computes
76
- * `totalPages` so legacy clients can render numbered nav.
77
- *
78
- * Multi-column ordering:
79
- * - orderBy accepts a string (single column), an array of strings
80
- * (multiple columns, all using opts.direction), or an array of
81
- * { column, direction } objects (mixed directions per column).
82
- * The keyset WHERE expands to the standard OR cascade
83
- * (col0 [op0] ? OR (col0 = ? AND col1 [op1] ?) OR ...)
84
- * so successive pages can't repeat or skip rows when ties on the
85
- * leading columns are broken by trailing ones. _id is appended as
86
- * a tiebreaker if not already in the orderBy chain.
87
- *
88
- * Out of scope (with structural reasons documented):
89
- * - Cursor TTL / expiry. Operators who want time-limited cursors
90
- * embed a timestamp in their own state and check at decode-time
91
- * before passing to .cursor(). The framework's HMAC tag carries
92
- * no notion of time.
93
- * - Search / filter integration. Operators chain .where() on the
94
- * Query before handing to .cursor() — pagination composes with
95
- * whatever filtering the operator's already applied.
96
- */
97
-
98
- var nodeCrypto = require("node:crypto");
99
- var C = require("./constants");
100
- var canonicalJson = require("./canonical-json");
101
- var crypto = require("./crypto");
102
- var nb = require("./numeric-bounds");
103
- var safeJson = require("./safe-json");
104
- var safeSql = require("./safe-sql");
105
- var { defineClass } = require("./framework-error");
106
-
107
- var PaginationError = defineClass("PaginationError", { alwaysPermanent: true });
108
-
109
- var CURSOR_VERSION = 1;
110
- var TAG_BYTES = C.BYTES.bytes(16); // 128-bit HMAC tag truncated from SHA3-512
111
- var DEFAULT_LIMIT = 25;
112
- var DEFAULT_MAX_LIMIT = 100;
113
- var MAX_CURSOR_BYTES = C.BYTES.kib(8);
114
-
115
- // Canonical JSON via the shared lib/canonical-json walker with
116
- // bufferAs: "reject" — cursor state must contain only plain data, so
117
- // Buffer / Uint8Array reject loudly. Map / Set / RegExp / Symbol /
118
- // function / circular always reject; Date → ISO; BigInt → decimal
119
- // string. Wraps the walker's generic Error in a PaginationError so
120
- // callers can route on `pagination/bad-state`.
121
- function _canonicalize(value) {
122
- try { return canonicalJson.stringify(value, { bufferAs: "reject" }); }
123
- catch (e) {
124
- throw new PaginationError("pagination/bad-state", e.message);
125
- }
126
- }
127
-
128
- function _toBuf(secret) {
129
- if (Buffer.isBuffer(secret)) return secret;
130
- if (typeof secret === "string") return Buffer.from(secret, "utf8");
131
- throw new PaginationError("pagination/bad-secret",
132
- "secret must be a Buffer or non-empty string");
133
- }
134
-
135
- function _b64urlEncode(buf) {
136
- var b = Buffer.isBuffer(buf) ? buf : Buffer.from(buf);
137
- return b.toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
138
- }
139
-
140
- function _b64urlDecode(s) {
141
- if (typeof s !== "string") throw new PaginationError("pagination/bad-cursor", "cursor must be a string");
142
- var pad = s.length % 4;
143
- var padded = pad ? s + "=".repeat(4 - pad) : s;
144
- return Buffer.from(padded.replace(/-/g, "+").replace(/_/g, "/"), "base64");
145
- }
146
-
147
- function _tag(secretBuf, stateJson) {
148
- var h = nodeCrypto.createHash("sha3-512");
149
- h.update(secretBuf);
150
- h.update(Buffer.from(stateJson, "utf8"));
151
- return h.digest().slice(0, TAG_BYTES);
152
- }
153
-
154
- function encodeCursor(state, secret) {
155
- if (!state || typeof state !== "object") {
156
- throw new PaginationError("pagination/bad-state",
157
- "encodeCursor: state must be an object");
158
- }
159
- var sb = _toBuf(secret);
160
- if (sb.length === 0) {
161
- throw new PaginationError("pagination/bad-secret", "secret must be non-empty");
162
- }
163
- var withMeta = Object.assign({ v: CURSOR_VERSION }, state);
164
- var json = _canonicalize(withMeta);
165
- var tag = _tag(sb, json);
166
- return _b64urlEncode(json) + "." + _b64urlEncode(tag);
167
- }
168
-
169
- function decodeCursor(token, secret) {
170
- if (typeof token !== "string" || token.length === 0) {
171
- throw new PaginationError("pagination/bad-cursor", "cursor must be a non-empty string");
172
- }
173
- var dot = token.indexOf(".");
174
- if (dot === -1) {
175
- throw new PaginationError("pagination/bad-cursor", "cursor missing tag separator");
176
- }
177
- var sb = _toBuf(secret);
178
- var jsonPart = token.slice(0, dot);
179
- var tagPart = token.slice(dot + 1);
180
- var json, tag;
181
- try {
182
- json = _b64urlDecode(jsonPart).toString("utf8");
183
- tag = _b64urlDecode(tagPart);
184
- } catch (_e) {
185
- throw new PaginationError("pagination/bad-cursor", "cursor base64 decode failed");
186
- }
187
- var expected = _tag(sb, json);
188
- if (!crypto.timingSafeEqual(tag, expected)) {
189
- throw new PaginationError("pagination/cursor-tag-mismatch",
190
- "cursor HMAC verification failed (tampered or wrong secret)");
191
- }
192
- var state;
193
- try { state = safeJson.parse(json, { maxBytes: MAX_CURSOR_BYTES }); }
194
- catch (_e) {
195
- throw new PaginationError("pagination/bad-cursor", "cursor state JSON malformed");
196
- }
197
- if (!state || typeof state !== "object") {
198
- throw new PaginationError("pagination/bad-cursor", "cursor state is not an object");
199
- }
200
- if (state.v !== CURSOR_VERSION) {
201
- throw new PaginationError("pagination/cursor-version",
202
- "cursor version " + state.v + " unsupported (current: " + CURSOR_VERSION + ")");
203
- }
204
- return state;
205
- }
206
-
207
- function _resolveLimit(opts) {
208
- nb.requirePositiveFiniteIntIfPresent(opts.max,
209
- "max", PaginationError, "pagination/bad-opt");
210
- nb.requirePositiveFiniteIntIfPresent(opts.default,
211
- "default", PaginationError, "pagination/bad-opt");
212
- var max = opts.max || DEFAULT_MAX_LIMIT;
213
- var def = opts.default || DEFAULT_LIMIT;
214
- var requested = parseInt(opts.limit, 10);
215
- if (isNaN(requested) || requested < 1) requested = def;
216
- if (requested > max) requested = max;
217
- return requested;
218
- }
219
-
220
- // ---- Cursor pagination ----
221
-
222
- // Normalize opts.orderBy into an array of { column, direction } entries.
223
- // Accepts:
224
- // undefined / null → [{ column: "_id", direction: opts.direction || "asc" }]
225
- // "createdAt" → [{ column: "createdAt", direction: opts.direction || "asc" }]
226
- // ["createdAt", "_id"] → all entries default to opts.direction || "asc"
227
- // [{column:"a",direction:"desc"}, {column:"b"}]
228
- // → mixed; missing direction defaults to opts.direction || "asc"
229
- // Always appends an _id tiebreaker if not present, so cursor uniqueness
230
- // is guaranteed regardless of the operator's spec.
231
- function _normalizeOrderBy(opts) {
232
- var defaultDir = (opts && opts.direction === "desc") ? "desc" : "asc";
233
- var raw = opts && opts.orderBy;
234
- var entries;
235
- if (raw == null) {
236
- entries = [{ column: "_id", direction: defaultDir }];
237
- } else if (typeof raw === "string") {
238
- entries = [{ column: raw, direction: defaultDir }];
239
- } else if (Array.isArray(raw)) {
240
- entries = raw.map(function (e) {
241
- if (typeof e === "string") return { column: e, direction: defaultDir };
242
- if (!e || typeof e !== "object" || typeof e.column !== "string") {
243
- throw new PaginationError("pagination/bad-orderby",
244
- "orderBy[] entries must be strings or { column, direction } objects, got " +
245
- JSON.stringify(e));
246
- }
247
- var d = (e.direction || defaultDir).toLowerCase();
248
- if (d !== "asc" && d !== "desc") {
249
- throw new PaginationError("pagination/bad-orderby",
250
- "orderBy[].direction must be 'asc' | 'desc', got " + JSON.stringify(e.direction));
251
- }
252
- return { column: e.column, direction: d };
253
- });
254
- } else {
255
- throw new PaginationError("pagination/bad-orderby",
256
- "orderBy must be a string, array, or omitted; got " + typeof raw);
257
- }
258
- for (var i = 0; i < entries.length; i++) {
259
- if (typeof entries[i].column !== "string" ||
260
- entries[i].column.length === 0 ||
261
- entries[i].column.length > safeSql.MAX_IDENTIFIER_LENGTH ||
262
- !safeSql.DEFAULT_IDENTIFIER_RE.test(entries[i].column)) {
263
- throw new PaginationError("pagination/bad-orderby",
264
- "orderBy column must match " + safeSql.DEFAULT_IDENTIFIER_RE +
265
- " and be 1.." + safeSql.MAX_IDENTIFIER_LENGTH + " chars, got " +
266
- JSON.stringify(entries[i].column));
267
- }
268
- }
269
- // Append _id tiebreaker if not already in the chain. Direction
270
- // matches the chain's last entry by convention so the tiebreaker
271
- // doesn't reverse the natural reading order.
272
- var hasId = entries.some(function (e) { return e.column === "_id"; });
273
- if (!hasId) {
274
- entries.push({ column: "_id", direction: entries[entries.length - 1].direction });
275
- }
276
- return entries;
277
- }
278
-
279
- // Build the keyset WHERE clause for next-page navigation given the
280
- // cursor's column values. Each (col, dir) entry expands the OR cascade:
281
- // col0 [op0] ? OR (col0 = ? AND col1 [op1] ?) OR ... OR (col0 = ? AND ... AND coln [opn] ?)
282
- // The compareOp per column flips by direction × forward (XNOR).
283
- function _buildKeysetWhere(orderEntries, cursorVals, forward) {
284
- var clauses = [];
285
- var params = [];
286
- for (var i = 0; i < orderEntries.length; i++) {
287
- var entry = orderEntries[i];
288
- // Effective direction: asc + forward → ">", desc + forward → "<", and reversed for backward.
289
- var effectiveAsc = (entry.direction === "asc") === forward;
290
- var op = effectiveAsc ? ">" : "<";
291
- var equalChain = [];
292
- for (var j = 0; j < i; j++) {
293
- equalChain.push('"' + orderEntries[j].column + '" = ?');
294
- params.push(cursorVals[j]);
295
- }
296
- equalChain.push('"' + entry.column + '" ' + op + ' ?');
297
- params.push(cursorVals[i]);
298
- clauses.push("(" + equalChain.join(" AND ") + ")");
299
- }
300
- return { sql: clauses.join(" OR "), params: params };
301
- }
302
-
303
- async function cursor(query, opts) {
304
- if (!query || typeof query.where !== "function" || typeof query.orderBy !== "function" ||
305
- typeof query.limit !== "function" || typeof query.all !== "function") {
306
- throw new PaginationError("pagination/bad-query",
307
- "cursor: first arg must be a db Query (must support where, orderBy, limit, all)");
308
- }
309
- opts = opts || {};
310
- if (opts.secret == null) {
311
- throw new PaginationError("pagination/no-secret",
312
- "cursor: opts.secret is required (Buffer or non-empty string for HMAC tagging)");
313
- }
314
- var limit = _resolveLimit(opts);
315
- var orderEntries = _normalizeOrderBy(opts);
316
- // Cursor compatibility key: array of `column:direction` pairs canonicalizes
317
- // the orderBy spec so a stored cursor must match exactly to be replayed.
318
- var orderKey = orderEntries.map(function (e) { return e.column + ":" + e.direction; });
319
-
320
- var cursorState = null;
321
- var forward = (opts.forward !== false);
322
- if (opts.cursor) {
323
- cursorState = decodeCursor(opts.cursor, opts.secret);
324
- var cursorKey = Array.isArray(cursorState.orderKey) ? cursorState.orderKey :
325
- // Back-compat with v0.6.20 single-column cursors: synthesize the
326
- // key from the legacy orderBy/dir fields.
327
- [cursorState.orderBy + ":" + cursorState.dir];
328
- if (JSON.stringify(cursorKey) !== JSON.stringify(orderKey)) {
329
- throw new PaginationError("pagination/cursor-mismatch",
330
- "cursor orderKey [" + cursorKey.join(", ") + "] does not match call orderKey [" +
331
- orderKey.join(", ") + "] — operator must use the same orderBy/direction spec");
332
- }
333
- if (typeof cursorState.forward === "boolean") forward = cursorState.forward;
334
- }
335
-
336
- // Apply the keyset WHERE if a cursor is present.
337
- if (cursorState) {
338
- var cursorVals;
339
- if (Array.isArray(cursorState.vals)) {
340
- cursorVals = cursorState.vals;
341
- } else {
342
- // v0.6.20 single-column shape — synthesize the vals array.
343
- cursorVals = [cursorState.orderByVal, cursorState.id];
344
- // Drop the synthetic _id append if the legacy cursor already had it
345
- if (orderEntries.length === 1) cursorVals = [cursorState.orderByVal];
346
- }
347
- if (cursorVals.length !== orderEntries.length) {
348
- throw new PaginationError("pagination/cursor-mismatch",
349
- "cursor encoded " + cursorVals.length + " column value(s) but orderBy has " +
350
- orderEntries.length + " — operator changed the orderBy spec mid-flight");
351
- }
352
- var where = _buildKeysetWhere(orderEntries, cursorVals, forward);
353
- query.whereRaw(where.sql, where.params);
354
- }
355
-
356
- // Apply ORDER BY for each entry. When forward=false we reverse direction
357
- // per entry so the SQL returns rows in the right reading order; we then
358
- // reverse client-side at the end.
359
- for (var oi = 0; oi < orderEntries.length; oi++) {
360
- var entry = orderEntries[oi];
361
- var effectiveDir = ((entry.direction === "asc") === forward) ? "asc" : "desc";
362
- query.orderBy(entry.column, effectiveDir);
363
- }
364
- query.limit(limit + 1);
365
-
366
- var rows = await Promise.resolve(query.all());
367
-
368
- var hasMore = rows.length > limit;
369
- var page = hasMore ? rows.slice(0, limit) : rows.slice();
370
- if (!forward) page.reverse();
371
-
372
- function _valsForRow(row) {
373
- return orderEntries.map(function (e) {
374
- return e.column === "_id" ? String(row._id) : row[e.column];
375
- });
376
- }
377
-
378
- var nextCursor = null;
379
- var prevCursor = null;
380
- if (hasMore && page.length > 0) {
381
- var last = page[page.length - 1];
382
- nextCursor = encodeCursor({
383
- orderKey: orderKey,
384
- vals: _valsForRow(last),
385
- forward: true,
386
- }, opts.secret);
387
- }
388
- if (cursorState && page.length > 0) {
389
- var first = page[0];
390
- prevCursor = encodeCursor({
391
- orderKey: orderKey,
392
- vals: _valsForRow(first),
393
- forward: false,
394
- }, opts.secret);
395
- }
396
-
397
- return {
398
- items: page,
399
- nextCursor: nextCursor,
400
- prevCursor: prevCursor,
401
- limit: limit,
402
- hasMore: hasMore,
403
- };
404
- }
405
-
406
- // ---- Offset pagination ----
407
-
408
- async function offset(query, opts) {
409
- if (!query || typeof query.limit !== "function" || typeof query.offset !== "function" ||
410
- typeof query.all !== "function" || typeof query.count !== "function") {
411
- throw new PaginationError("pagination/bad-query",
412
- "offset: first arg must be a db Query (must support limit, offset, all, count)");
413
- }
414
- opts = opts || {};
415
- var perPage = _resolveLimit({ limit: opts.perPage, max: opts.max, default: opts.default });
416
- var page = parseInt(opts.page, 10);
417
- if (isNaN(page) || page < 1) page = 1;
418
- var orderBy = typeof opts.orderBy === "string" && opts.orderBy.length > 0 ? opts.orderBy : "_id";
419
- // Same identifier-only check on offset() as cursor() — orderBy passes
420
- // through to the db Query; throw at call site to prevent SQL injection.
421
- if (orderBy.length > safeSql.MAX_IDENTIFIER_LENGTH ||
422
- !safeSql.DEFAULT_IDENTIFIER_RE.test(orderBy)) {
423
- throw new PaginationError("pagination/bad-orderby",
424
- "offset: orderBy must match " + safeSql.DEFAULT_IDENTIFIER_RE +
425
- " and be 1.." + safeSql.MAX_IDENTIFIER_LENGTH + " chars, got " +
426
- JSON.stringify(orderBy));
427
- }
428
- var direction = (opts.direction === "desc") ? "desc" : "asc";
429
-
430
- // Count gives total — required for totalPages calculation. Cheap on
431
- // an indexed column (which most app tables have via _id).
432
- var total = await Promise.resolve(query.count());
433
-
434
- // Build the page query — the operator's existing where() chain is
435
- // already applied; we just add ordering + limit + offset.
436
- query.orderBy(orderBy, direction);
437
- query.limit(perPage);
438
- query.offset((page - 1) * perPage);
439
- var items = await Promise.resolve(query.all());
440
-
441
- var totalPages = total === 0 ? 0 : Math.ceil(total / perPage);
442
- var hasMore = page < totalPages;
443
- return {
444
- items: items,
445
- total: total,
446
- page: page,
447
- perPage: perPage,
448
- totalPages: totalPages,
449
- hasMore: hasMore,
450
- };
451
- }
452
-
453
- module.exports = {
454
- cursor: cursor,
455
- offset: offset,
456
- encodeCursor: encodeCursor,
457
- decodeCursor: decodeCursor,
458
- PaginationError: PaginationError,
459
- // Internal helpers exposed for tests
460
- _resolveLimit: _resolveLimit,
461
- _b64urlEncode: _b64urlEncode,
462
- _b64urlDecode: _b64urlDecode,
463
- CURSOR_VERSION: CURSOR_VERSION,
464
- };
1
+ "use strict";
2
+ /**
3
+ * pagination — cursor + offset helpers.
4
+ *
5
+ * Every CRUD list endpoint reinvents pagination, usually wrong.
6
+ * The two failure modes:
7
+ *
8
+ * - Offset pagination at depth: `LIMIT n OFFSET 50000` makes the DB
9
+ * scan-and-skip 50,000 rows. O(n). With concurrent writes, rows
10
+ * are also missed/duplicated as new inserts shift the offset.
11
+ * - Cursor pagination without a tie-breaker: `WHERE createdAt > ?`
12
+ * skips or duplicates rows when two records share createdAt.
13
+ *
14
+ * This module ships both, done correctly, plus the encode/decode
15
+ * primitives operators reach for when their SQL doesn't fit the
16
+ * Query-builder shape.
17
+ *
18
+ * Public API:
19
+ *
20
+ * var p = b.pagination;
21
+ *
22
+ * // Cursor: O(1) at any depth. Composite (orderBy, _id) ordering
23
+ * // so ties on the orderBy column are broken by _id and rows are
24
+ * // never skipped.
25
+ * var page = await p.cursor(b.db.from("users"), {
26
+ * cursor: req.query.cursor,
27
+ * limit: req.query.limit,
28
+ * max: 100,
29
+ * default: 25,
30
+ * orderBy: "_id", // default; use "createdAt" etc.
31
+ * direction: "asc", // "asc" | "desc"
32
+ * secret: pageSecret, // Buffer or string; HMAC-tag the cursor
33
+ * });
34
+ * // → { items: [...], nextCursor, prevCursor, limit, hasMore }
35
+ *
36
+ * // Offset: page-numbered. Ergonomic for legacy clients.
37
+ * var off = await p.offset(b.db.from("users"), {
38
+ * page: req.query.page,
39
+ * perPage: req.query.perPage,
40
+ * max: 100,
41
+ * default: 25,
42
+ * });
43
+ * // → { items, total, page, perPage, totalPages, hasMore }
44
+ *
45
+ * // Low-level — for raw SQL or custom row sources.
46
+ * var token = p.encodeCursor({ orderByVal: 12345, id: "abc" }, secret);
47
+ * var state = p.decodeCursor(token, secret);
48
+ *
49
+ * Cursor design:
50
+ * - Composite ordering: (orderBy column, _id). _id is the implicit
51
+ * tie-breaker, so two rows with identical orderByVal are still
52
+ * totally ordered. Forward navigation: WHERE
53
+ * (orderByVal > cur.orderByVal) OR
54
+ * (orderByVal = cur.orderByVal AND _id > cur.id)
55
+ * Backward: same with `<`, then reverse the result set.
56
+ * - Cursors are HMAC-tagged. A tampered cursor is detected at decode
57
+ * time and rejected with PaginationError. Operators MUST pass
58
+ * `secret` (Buffer or string) — there's no auto-derivation, since
59
+ * framework-derived secrets would produce surprises across deploys.
60
+ * - Cursor format: `<base64url state>.<base64url tag>`. State is
61
+ * canonical JSON of `{ v, dir, orderBy, orderByVal, id }`. Tag is
62
+ * SHA3-512(secret || stateJson).slice(0, 16).
63
+ * - direction is part of the cursor — operators don't need to round-
64
+ * trip it via query string. The cursor itself encodes whether it's
65
+ * a "next" or "prev" position so navigation stays consistent.
66
+ *
67
+ * Limit semantics:
68
+ * - Operator passes `default` and `max`. The effective limit is
69
+ * min(max, requestedLimit || default). Negative or non-integer
70
+ * limits are coerced to default.
71
+ * - The page query fetches limit+1 to detect hasMore without a
72
+ * second COUNT(*) trip.
73
+ *
74
+ * Offset is the legacy-client tool, not the recommended path. The
75
+ * module's offset() returns a `total` (from COUNT(*)) and computes
76
+ * `totalPages` so legacy clients can render numbered nav.
77
+ *
78
+ * Multi-column ordering:
79
+ * - orderBy accepts a string (single column), an array of strings
80
+ * (multiple columns, all using opts.direction), or an array of
81
+ * { column, direction } objects (mixed directions per column).
82
+ * The keyset WHERE expands to the standard OR cascade
83
+ * (col0 [op0] ? OR (col0 = ? AND col1 [op1] ?) OR ...)
84
+ * so successive pages can't repeat or skip rows when ties on the
85
+ * leading columns are broken by trailing ones. _id is appended as
86
+ * a tiebreaker if not already in the orderBy chain.
87
+ *
88
+ * Out of scope (with structural reasons documented):
89
+ * - Cursor TTL / expiry. Operators who want time-limited cursors
90
+ * embed a timestamp in their own state and check at decode-time
91
+ * before passing to .cursor(). The framework's HMAC tag carries
92
+ * no notion of time.
93
+ * - Search / filter integration. Operators chain .where() on the
94
+ * Query before handing to .cursor() — pagination composes with
95
+ * whatever filtering the operator's already applied.
96
+ */
97
+
98
+ var nodeCrypto = require("node:crypto");
99
+ var C = require("./constants");
100
+ var canonicalJson = require("./canonical-json");
101
+ var crypto = require("./crypto");
102
+ var nb = require("./numeric-bounds");
103
+ var safeJson = require("./safe-json");
104
+ var safeSql = require("./safe-sql");
105
+ var { defineClass } = require("./framework-error");
106
+
107
+ var PaginationError = defineClass("PaginationError", { alwaysPermanent: true });
108
+
109
+ var CURSOR_VERSION = 1;
110
+ var TAG_BYTES = C.BYTES.bytes(16); // 128-bit HMAC tag truncated from SHA3-512
111
+ var DEFAULT_LIMIT = 25;
112
+ var DEFAULT_MAX_LIMIT = 100;
113
+ var MAX_CURSOR_BYTES = C.BYTES.kib(8);
114
+
115
+ // Canonical JSON via the shared lib/canonical-json walker with
116
+ // bufferAs: "reject" — cursor state must contain only plain data, so
117
+ // Buffer / Uint8Array reject loudly. Map / Set / RegExp / Symbol /
118
+ // function / circular always reject; Date → ISO; BigInt → decimal
119
+ // string. Wraps the walker's generic Error in a PaginationError so
120
+ // callers can route on `pagination/bad-state`.
121
+ function _canonicalize(value) {
122
+ try { return canonicalJson.stringify(value, { bufferAs: "reject" }); }
123
+ catch (e) {
124
+ throw new PaginationError("pagination/bad-state", e.message);
125
+ }
126
+ }
127
+
128
+ function _toBuf(secret) {
129
+ if (Buffer.isBuffer(secret)) return secret;
130
+ if (typeof secret === "string") return Buffer.from(secret, "utf8");
131
+ throw new PaginationError("pagination/bad-secret",
132
+ "secret must be a Buffer or non-empty string");
133
+ }
134
+
135
+ function _b64urlEncode(buf) {
136
+ var b = Buffer.isBuffer(buf) ? buf : Buffer.from(buf);
137
+ return b.toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
138
+ }
139
+
140
+ function _b64urlDecode(s) {
141
+ if (typeof s !== "string") throw new PaginationError("pagination/bad-cursor", "cursor must be a string");
142
+ var pad = s.length % 4;
143
+ var padded = pad ? s + "=".repeat(4 - pad) : s;
144
+ return Buffer.from(padded.replace(/-/g, "+").replace(/_/g, "/"), "base64");
145
+ }
146
+
147
+ function _tag(secretBuf, stateJson) {
148
+ var h = nodeCrypto.createHash("sha3-512");
149
+ h.update(secretBuf);
150
+ h.update(Buffer.from(stateJson, "utf8"));
151
+ return h.digest().slice(0, TAG_BYTES);
152
+ }
153
+
154
+ function encodeCursor(state, secret) {
155
+ if (!state || typeof state !== "object") {
156
+ throw new PaginationError("pagination/bad-state",
157
+ "encodeCursor: state must be an object");
158
+ }
159
+ var sb = _toBuf(secret);
160
+ if (sb.length === 0) {
161
+ throw new PaginationError("pagination/bad-secret", "secret must be non-empty");
162
+ }
163
+ var withMeta = Object.assign({ v: CURSOR_VERSION }, state);
164
+ var json = _canonicalize(withMeta);
165
+ var tag = _tag(sb, json);
166
+ return _b64urlEncode(json) + "." + _b64urlEncode(tag);
167
+ }
168
+
169
+ function decodeCursor(token, secret) {
170
+ if (typeof token !== "string" || token.length === 0) {
171
+ throw new PaginationError("pagination/bad-cursor", "cursor must be a non-empty string");
172
+ }
173
+ var dot = token.indexOf(".");
174
+ if (dot === -1) {
175
+ throw new PaginationError("pagination/bad-cursor", "cursor missing tag separator");
176
+ }
177
+ var sb = _toBuf(secret);
178
+ var jsonPart = token.slice(0, dot);
179
+ var tagPart = token.slice(dot + 1);
180
+ var json, tag;
181
+ try {
182
+ json = _b64urlDecode(jsonPart).toString("utf8");
183
+ tag = _b64urlDecode(tagPart);
184
+ } catch (_e) {
185
+ throw new PaginationError("pagination/bad-cursor", "cursor base64 decode failed");
186
+ }
187
+ var expected = _tag(sb, json);
188
+ if (!crypto.timingSafeEqual(tag, expected)) {
189
+ throw new PaginationError("pagination/cursor-tag-mismatch",
190
+ "cursor HMAC verification failed (tampered or wrong secret)");
191
+ }
192
+ var state;
193
+ try { state = safeJson.parse(json, { maxBytes: MAX_CURSOR_BYTES }); }
194
+ catch (_e) {
195
+ throw new PaginationError("pagination/bad-cursor", "cursor state JSON malformed");
196
+ }
197
+ if (!state || typeof state !== "object") {
198
+ throw new PaginationError("pagination/bad-cursor", "cursor state is not an object");
199
+ }
200
+ if (state.v !== CURSOR_VERSION) {
201
+ throw new PaginationError("pagination/cursor-version",
202
+ "cursor version " + state.v + " unsupported (current: " + CURSOR_VERSION + ")");
203
+ }
204
+ return state;
205
+ }
206
+
207
+ function _resolveLimit(opts) {
208
+ nb.requirePositiveFiniteIntIfPresent(opts.max,
209
+ "max", PaginationError, "pagination/bad-opt");
210
+ nb.requirePositiveFiniteIntIfPresent(opts.default,
211
+ "default", PaginationError, "pagination/bad-opt");
212
+ var max = opts.max || DEFAULT_MAX_LIMIT;
213
+ var def = opts.default || DEFAULT_LIMIT;
214
+ var requested = parseInt(opts.limit, 10);
215
+ if (isNaN(requested) || requested < 1) requested = def;
216
+ if (requested > max) requested = max;
217
+ return requested;
218
+ }
219
+
220
+ // ---- Cursor pagination ----
221
+
222
+ // Normalize opts.orderBy into an array of { column, direction } entries.
223
+ // Accepts:
224
+ // undefined / null → [{ column: "_id", direction: opts.direction || "asc" }]
225
+ // "createdAt" → [{ column: "createdAt", direction: opts.direction || "asc" }]
226
+ // ["createdAt", "_id"] → all entries default to opts.direction || "asc"
227
+ // [{column:"a",direction:"desc"}, {column:"b"}]
228
+ // → mixed; missing direction defaults to opts.direction || "asc"
229
+ // Always appends an _id tiebreaker if not present, so cursor uniqueness
230
+ // is guaranteed regardless of the operator's spec.
231
+ function _normalizeOrderBy(opts) {
232
+ var defaultDir = (opts && opts.direction === "desc") ? "desc" : "asc";
233
+ var raw = opts && opts.orderBy;
234
+ var entries;
235
+ if (raw == null) {
236
+ entries = [{ column: "_id", direction: defaultDir }];
237
+ } else if (typeof raw === "string") {
238
+ entries = [{ column: raw, direction: defaultDir }];
239
+ } else if (Array.isArray(raw)) {
240
+ entries = raw.map(function (e) {
241
+ if (typeof e === "string") return { column: e, direction: defaultDir };
242
+ if (!e || typeof e !== "object" || typeof e.column !== "string") {
243
+ throw new PaginationError("pagination/bad-orderby",
244
+ "orderBy[] entries must be strings or { column, direction } objects, got " +
245
+ JSON.stringify(e));
246
+ }
247
+ var d = (e.direction || defaultDir).toLowerCase();
248
+ if (d !== "asc" && d !== "desc") {
249
+ throw new PaginationError("pagination/bad-orderby",
250
+ "orderBy[].direction must be 'asc' | 'desc', got " + JSON.stringify(e.direction));
251
+ }
252
+ return { column: e.column, direction: d };
253
+ });
254
+ } else {
255
+ throw new PaginationError("pagination/bad-orderby",
256
+ "orderBy must be a string, array, or omitted; got " + typeof raw);
257
+ }
258
+ for (var i = 0; i < entries.length; i++) {
259
+ if (typeof entries[i].column !== "string" ||
260
+ entries[i].column.length === 0 ||
261
+ entries[i].column.length > safeSql.MAX_IDENTIFIER_LENGTH ||
262
+ !safeSql.DEFAULT_IDENTIFIER_RE.test(entries[i].column)) {
263
+ throw new PaginationError("pagination/bad-orderby",
264
+ "orderBy column must match " + safeSql.DEFAULT_IDENTIFIER_RE +
265
+ " and be 1.." + safeSql.MAX_IDENTIFIER_LENGTH + " chars, got " +
266
+ JSON.stringify(entries[i].column));
267
+ }
268
+ }
269
+ // Append _id tiebreaker if not already in the chain. Direction
270
+ // matches the chain's last entry by convention so the tiebreaker
271
+ // doesn't reverse the natural reading order.
272
+ var hasId = entries.some(function (e) { return e.column === "_id"; });
273
+ if (!hasId) {
274
+ entries.push({ column: "_id", direction: entries[entries.length - 1].direction });
275
+ }
276
+ return entries;
277
+ }
278
+
279
+ // Build the keyset WHERE clause for next-page navigation given the
280
+ // cursor's column values. Each (col, dir) entry expands the OR cascade:
281
+ // col0 [op0] ? OR (col0 = ? AND col1 [op1] ?) OR ... OR (col0 = ? AND ... AND coln [opn] ?)
282
+ // The compareOp per column flips by direction × forward (XNOR).
283
+ function _buildKeysetWhere(orderEntries, cursorVals, forward) {
284
+ var clauses = [];
285
+ var params = [];
286
+ for (var i = 0; i < orderEntries.length; i++) {
287
+ var entry = orderEntries[i];
288
+ // Effective direction: asc + forward → ">", desc + forward → "<", and reversed for backward.
289
+ var effectiveAsc = (entry.direction === "asc") === forward;
290
+ var op = effectiveAsc ? ">" : "<";
291
+ var equalChain = [];
292
+ for (var j = 0; j < i; j++) {
293
+ equalChain.push('"' + orderEntries[j].column + '" = ?');
294
+ params.push(cursorVals[j]);
295
+ }
296
+ equalChain.push('"' + entry.column + '" ' + op + ' ?');
297
+ params.push(cursorVals[i]);
298
+ clauses.push("(" + equalChain.join(" AND ") + ")");
299
+ }
300
+ return { sql: clauses.join(" OR "), params: params };
301
+ }
302
+
303
+ async function cursor(query, opts) {
304
+ if (!query || typeof query.where !== "function" || typeof query.orderBy !== "function" ||
305
+ typeof query.limit !== "function" || typeof query.all !== "function") {
306
+ throw new PaginationError("pagination/bad-query",
307
+ "cursor: first arg must be a db Query (must support where, orderBy, limit, all)");
308
+ }
309
+ opts = opts || {};
310
+ if (opts.secret == null) {
311
+ throw new PaginationError("pagination/no-secret",
312
+ "cursor: opts.secret is required (Buffer or non-empty string for HMAC tagging)");
313
+ }
314
+ var limit = _resolveLimit(opts);
315
+ var orderEntries = _normalizeOrderBy(opts);
316
+ // Cursor compatibility key: array of `column:direction` pairs canonicalizes
317
+ // the orderBy spec so a stored cursor must match exactly to be replayed.
318
+ var orderKey = orderEntries.map(function (e) { return e.column + ":" + e.direction; });
319
+
320
+ var cursorState = null;
321
+ var forward = (opts.forward !== false);
322
+ if (opts.cursor) {
323
+ cursorState = decodeCursor(opts.cursor, opts.secret);
324
+ var cursorKey = Array.isArray(cursorState.orderKey) ? cursorState.orderKey :
325
+ // Back-compat with v0.6.20 single-column cursors: synthesize the
326
+ // key from the legacy orderBy/dir fields.
327
+ [cursorState.orderBy + ":" + cursorState.dir];
328
+ if (JSON.stringify(cursorKey) !== JSON.stringify(orderKey)) {
329
+ throw new PaginationError("pagination/cursor-mismatch",
330
+ "cursor orderKey [" + cursorKey.join(", ") + "] does not match call orderKey [" +
331
+ orderKey.join(", ") + "] — operator must use the same orderBy/direction spec");
332
+ }
333
+ if (typeof cursorState.forward === "boolean") forward = cursorState.forward;
334
+ }
335
+
336
+ // Apply the keyset WHERE if a cursor is present.
337
+ if (cursorState) {
338
+ var cursorVals;
339
+ if (Array.isArray(cursorState.vals)) {
340
+ cursorVals = cursorState.vals;
341
+ } else {
342
+ // v0.6.20 single-column shape — synthesize the vals array.
343
+ cursorVals = [cursorState.orderByVal, cursorState.id];
344
+ // Drop the synthetic _id append if the legacy cursor already had it
345
+ if (orderEntries.length === 1) cursorVals = [cursorState.orderByVal];
346
+ }
347
+ if (cursorVals.length !== orderEntries.length) {
348
+ throw new PaginationError("pagination/cursor-mismatch",
349
+ "cursor encoded " + cursorVals.length + " column value(s) but orderBy has " +
350
+ orderEntries.length + " — operator changed the orderBy spec mid-flight");
351
+ }
352
+ var where = _buildKeysetWhere(orderEntries, cursorVals, forward);
353
+ query.whereRaw(where.sql, where.params);
354
+ }
355
+
356
+ // Apply ORDER BY for each entry. When forward=false we reverse direction
357
+ // per entry so the SQL returns rows in the right reading order; we then
358
+ // reverse client-side at the end.
359
+ for (var oi = 0; oi < orderEntries.length; oi++) {
360
+ var entry = orderEntries[oi];
361
+ var effectiveDir = ((entry.direction === "asc") === forward) ? "asc" : "desc";
362
+ query.orderBy(entry.column, effectiveDir);
363
+ }
364
+ query.limit(limit + 1);
365
+
366
+ var rows = await Promise.resolve(query.all());
367
+
368
+ var hasMore = rows.length > limit;
369
+ var page = hasMore ? rows.slice(0, limit) : rows.slice();
370
+ if (!forward) page.reverse();
371
+
372
+ function _valsForRow(row) {
373
+ return orderEntries.map(function (e) {
374
+ return e.column === "_id" ? String(row._id) : row[e.column];
375
+ });
376
+ }
377
+
378
+ var nextCursor = null;
379
+ var prevCursor = null;
380
+ if (hasMore && page.length > 0) {
381
+ var last = page[page.length - 1];
382
+ nextCursor = encodeCursor({
383
+ orderKey: orderKey,
384
+ vals: _valsForRow(last),
385
+ forward: true,
386
+ }, opts.secret);
387
+ }
388
+ if (cursorState && page.length > 0) {
389
+ var first = page[0];
390
+ prevCursor = encodeCursor({
391
+ orderKey: orderKey,
392
+ vals: _valsForRow(first),
393
+ forward: false,
394
+ }, opts.secret);
395
+ }
396
+
397
+ return {
398
+ items: page,
399
+ nextCursor: nextCursor,
400
+ prevCursor: prevCursor,
401
+ limit: limit,
402
+ hasMore: hasMore,
403
+ };
404
+ }
405
+
406
+ // ---- Offset pagination ----
407
+
408
+ async function offset(query, opts) {
409
+ if (!query || typeof query.limit !== "function" || typeof query.offset !== "function" ||
410
+ typeof query.all !== "function" || typeof query.count !== "function") {
411
+ throw new PaginationError("pagination/bad-query",
412
+ "offset: first arg must be a db Query (must support limit, offset, all, count)");
413
+ }
414
+ opts = opts || {};
415
+ var perPage = _resolveLimit({ limit: opts.perPage, max: opts.max, default: opts.default });
416
+ var page = parseInt(opts.page, 10);
417
+ if (isNaN(page) || page < 1) page = 1;
418
+ var orderBy = typeof opts.orderBy === "string" && opts.orderBy.length > 0 ? opts.orderBy : "_id";
419
+ // Same identifier-only check on offset() as cursor() — orderBy passes
420
+ // through to the db Query; throw at call site to prevent SQL injection.
421
+ if (orderBy.length > safeSql.MAX_IDENTIFIER_LENGTH ||
422
+ !safeSql.DEFAULT_IDENTIFIER_RE.test(orderBy)) {
423
+ throw new PaginationError("pagination/bad-orderby",
424
+ "offset: orderBy must match " + safeSql.DEFAULT_IDENTIFIER_RE +
425
+ " and be 1.." + safeSql.MAX_IDENTIFIER_LENGTH + " chars, got " +
426
+ JSON.stringify(orderBy));
427
+ }
428
+ var direction = (opts.direction === "desc") ? "desc" : "asc";
429
+
430
+ // Count gives total — required for totalPages calculation. Cheap on
431
+ // an indexed column (which most app tables have via _id).
432
+ var total = await Promise.resolve(query.count());
433
+
434
+ // Build the page query — the operator's existing where() chain is
435
+ // already applied; we just add ordering + limit + offset.
436
+ query.orderBy(orderBy, direction);
437
+ query.limit(perPage);
438
+ query.offset((page - 1) * perPage);
439
+ var items = await Promise.resolve(query.all());
440
+
441
+ var totalPages = total === 0 ? 0 : Math.ceil(total / perPage);
442
+ var hasMore = page < totalPages;
443
+ return {
444
+ items: items,
445
+ total: total,
446
+ page: page,
447
+ perPage: perPage,
448
+ totalPages: totalPages,
449
+ hasMore: hasMore,
450
+ };
451
+ }
452
+
453
+ module.exports = {
454
+ cursor: cursor,
455
+ offset: offset,
456
+ encodeCursor: encodeCursor,
457
+ decodeCursor: decodeCursor,
458
+ PaginationError: PaginationError,
459
+ // Internal helpers exposed for tests
460
+ _resolveLimit: _resolveLimit,
461
+ _b64urlEncode: _b64urlEncode,
462
+ _b64urlDecode: _b64urlDecode,
463
+ CURSOR_VERSION: CURSOR_VERSION,
464
+ };