hide-a-bed 7.1.2 → 7.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.
- package/README.md +64 -0
- package/dist/cjs/index.cjs +424 -356
- package/dist/esm/index.mjs +419 -357
- package/impl/bindConfig.mts +3 -0
- package/impl/find.mts +59 -0
- package/impl/utils/errors.mts +1 -0
- package/impl/utils/fetch.mts +1 -0
- package/impl/utils/logger.mts +1 -4
- package/index.mts +19 -0
- package/package.json +1 -1
- package/schema/config.mts +28 -9
- package/schema/couch/mango.schema.ts +61 -0
- package/types/output/impl/bindConfig.d.mts +2 -0
- package/types/output/impl/bindConfig.d.mts.map +1 -1
- package/types/output/impl/find.d.mts +8 -0
- package/types/output/impl/find.d.mts.map +1 -0
- package/types/output/impl/find.test.d.mts +2 -0
- package/types/output/impl/find.test.d.mts.map +1 -0
- package/types/output/impl/utils/errors.d.mts +1 -1
- package/types/output/impl/utils/errors.d.mts.map +1 -1
- package/types/output/impl/utils/fetch.d.mts +1 -1
- package/types/output/impl/utils/fetch.d.mts.map +1 -1
- package/types/output/impl/utils/logger.d.mts.map +1 -1
- package/types/output/index.d.mts +5 -1
- package/types/output/index.d.mts.map +1 -1
- package/types/output/schema/config.d.mts +6 -6
- package/types/output/schema/config.d.mts.map +1 -1
- package/types/output/schema/couch/mango.schema.d.ts +58 -0
- package/types/output/schema/couch/mango.schema.d.ts.map +1 -0
- package/types/output/schema/couch/mango.schema.test.d.mts +2 -0
- package/types/output/schema/couch/mango.schema.test.d.mts.map +1 -0
package/dist/cjs/index.cjs
CHANGED
|
@@ -39,149 +39,6 @@ let stream_json_streamers_StreamArray_js = require("stream-json/streamers/Stream
|
|
|
39
39
|
stream_json_streamers_StreamArray_js = __toESM(stream_json_streamers_StreamArray_js);
|
|
40
40
|
let events = require("events");
|
|
41
41
|
|
|
42
|
-
//#region impl/utils/queryBuilder.mts
|
|
43
|
-
/**
|
|
44
|
-
* A builder class for constructing CouchDB view query options.
|
|
45
|
-
* Provides a fluent API for setting various query parameters.
|
|
46
|
-
* @example
|
|
47
|
-
* const queryOptions = new QueryBuilder()
|
|
48
|
-
* .limit(10)
|
|
49
|
-
* .include_docs()
|
|
50
|
-
* .startKey('someKey')
|
|
51
|
-
* .build();
|
|
52
|
-
* @see SimpleViewOptions for the full list of options.
|
|
53
|
-
*
|
|
54
|
-
* @remarks
|
|
55
|
-
* Each method corresponds to a CouchDB view option and returns the builder instance for chaining.
|
|
56
|
-
*
|
|
57
|
-
* @returns The constructed SimpleViewOptions object.
|
|
58
|
-
*/
|
|
59
|
-
var QueryBuilder = class {
|
|
60
|
-
#options = {};
|
|
61
|
-
descending(descending = true) {
|
|
62
|
-
this.#options.descending = descending;
|
|
63
|
-
return this;
|
|
64
|
-
}
|
|
65
|
-
endkey_docid(endkeyDocId) {
|
|
66
|
-
this.#options.endkey_docid = endkeyDocId;
|
|
67
|
-
return this;
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Alias for endkey_docid
|
|
71
|
-
*/
|
|
72
|
-
end_key_doc_id(endkeyDocId) {
|
|
73
|
-
this.#options.endkey_docid = endkeyDocId;
|
|
74
|
-
return this;
|
|
75
|
-
}
|
|
76
|
-
endkey(endkey) {
|
|
77
|
-
this.#options.endkey = endkey;
|
|
78
|
-
return this;
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Alias for endkey
|
|
82
|
-
*/
|
|
83
|
-
endKey(endkey) {
|
|
84
|
-
this.#options.endkey = endkey;
|
|
85
|
-
return this;
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Alias for endkey
|
|
89
|
-
*/
|
|
90
|
-
end_key(endkey) {
|
|
91
|
-
this.#options.endkey = endkey;
|
|
92
|
-
return this;
|
|
93
|
-
}
|
|
94
|
-
group(group = true) {
|
|
95
|
-
this.#options.group = group;
|
|
96
|
-
return this;
|
|
97
|
-
}
|
|
98
|
-
group_level(level) {
|
|
99
|
-
this.#options.group_level = level;
|
|
100
|
-
return this;
|
|
101
|
-
}
|
|
102
|
-
include_docs(includeDocs = true) {
|
|
103
|
-
this.#options.include_docs = includeDocs;
|
|
104
|
-
return this;
|
|
105
|
-
}
|
|
106
|
-
inclusive_end(inclusiveEnd = true) {
|
|
107
|
-
this.#options.inclusive_end = inclusiveEnd;
|
|
108
|
-
return this;
|
|
109
|
-
}
|
|
110
|
-
key(key) {
|
|
111
|
-
this.#options.key = key;
|
|
112
|
-
return this;
|
|
113
|
-
}
|
|
114
|
-
keys(keys) {
|
|
115
|
-
this.#options.keys = keys;
|
|
116
|
-
return this;
|
|
117
|
-
}
|
|
118
|
-
limit(limit) {
|
|
119
|
-
this.#options.limit = limit;
|
|
120
|
-
return this;
|
|
121
|
-
}
|
|
122
|
-
reduce(reduce = true) {
|
|
123
|
-
this.#options.reduce = reduce;
|
|
124
|
-
return this;
|
|
125
|
-
}
|
|
126
|
-
skip(skip) {
|
|
127
|
-
this.#options.skip = skip;
|
|
128
|
-
return this;
|
|
129
|
-
}
|
|
130
|
-
sorted(sorted = true) {
|
|
131
|
-
this.#options.sorted = sorted;
|
|
132
|
-
return this;
|
|
133
|
-
}
|
|
134
|
-
stable(stable = true) {
|
|
135
|
-
this.#options.stable = stable;
|
|
136
|
-
return this;
|
|
137
|
-
}
|
|
138
|
-
startkey(startkey) {
|
|
139
|
-
this.#options.startkey = startkey;
|
|
140
|
-
return this;
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* Alias for startkey
|
|
144
|
-
*/
|
|
145
|
-
startKey(startkey) {
|
|
146
|
-
this.#options.startkey = startkey;
|
|
147
|
-
return this;
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Alias for startkey
|
|
151
|
-
*/
|
|
152
|
-
start_key(startkey) {
|
|
153
|
-
this.#options.startkey = startkey;
|
|
154
|
-
return this;
|
|
155
|
-
}
|
|
156
|
-
startkey_docid(startkeyDocId) {
|
|
157
|
-
this.#options.startkey_docid = startkeyDocId;
|
|
158
|
-
return this;
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* Alias for startkey_docid
|
|
162
|
-
*/
|
|
163
|
-
start_key_doc_id(startkeyDocId) {
|
|
164
|
-
this.#options.startkey_docid = startkeyDocId;
|
|
165
|
-
return this;
|
|
166
|
-
}
|
|
167
|
-
update(update) {
|
|
168
|
-
this.#options.update = update;
|
|
169
|
-
return this;
|
|
170
|
-
}
|
|
171
|
-
update_seq(updateSeq = true) {
|
|
172
|
-
this.#options.update_seq = updateSeq;
|
|
173
|
-
return this;
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Builds and returns the ViewOptions object.
|
|
177
|
-
*/
|
|
178
|
-
build() {
|
|
179
|
-
return { ...this.#options };
|
|
180
|
-
}
|
|
181
|
-
};
|
|
182
|
-
const createQuery = () => new QueryBuilder();
|
|
183
|
-
|
|
184
|
-
//#endregion
|
|
185
42
|
//#region schema/request.mts
|
|
186
43
|
const isAbortSignal = (value) => {
|
|
187
44
|
return value instanceof AbortSignal;
|
|
@@ -198,28 +55,21 @@ const RequestOptions = zod.z.strictObject({
|
|
|
198
55
|
|
|
199
56
|
//#endregion
|
|
200
57
|
//#region schema/config.mts
|
|
201
|
-
const
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
})
|
|
215
|
-
|
|
216
|
-
input: anyArgs,
|
|
217
|
-
output: zod.z.void()
|
|
218
|
-
}).optional()
|
|
219
|
-
}).or(zod.z.function({
|
|
220
|
-
input: anyArgs,
|
|
221
|
-
output: zod.z.void()
|
|
222
|
-
}));
|
|
58
|
+
const loggerLevels = [
|
|
59
|
+
"error",
|
|
60
|
+
"warn",
|
|
61
|
+
"info",
|
|
62
|
+
"debug"
|
|
63
|
+
];
|
|
64
|
+
const isLoggerMethod = (value) => typeof value === "function";
|
|
65
|
+
const LoggerSchema = zod.z.custom((value) => {
|
|
66
|
+
if (isLoggerMethod(value)) return true;
|
|
67
|
+
if (value === null || typeof value !== "object") return false;
|
|
68
|
+
return loggerLevels.every((level) => {
|
|
69
|
+
const method = value[level];
|
|
70
|
+
return method === void 0 || isLoggerMethod(method);
|
|
71
|
+
});
|
|
72
|
+
}, { message: "logger must be a function or object with optional error, warn, info, and debug methods" });
|
|
223
73
|
const CouchAuth = zod.z.strictObject({
|
|
224
74
|
username: zod.z.string().describe("basic auth username for CouchDB requests"),
|
|
225
75
|
password: zod.z.string().describe("basic auth password for CouchDB requests")
|
|
@@ -533,72 +383,414 @@ function isConflictError(err) {
|
|
|
533
383
|
}
|
|
534
384
|
|
|
535
385
|
//#endregion
|
|
536
|
-
//#region impl/
|
|
537
|
-
const
|
|
538
|
-
return
|
|
386
|
+
//#region impl/utils/request.mts
|
|
387
|
+
const definedSignals = (signals) => {
|
|
388
|
+
return signals.filter((signal) => signal != null);
|
|
539
389
|
};
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
let delay = initialDelay;
|
|
551
|
-
for (let attempt = 0; attempt <= maxRetries; attempt++) try {
|
|
552
|
-
return await fn(...args);
|
|
553
|
-
} catch (error) {
|
|
554
|
-
if (!shouldRetryError(error, attempt)) throw error;
|
|
555
|
-
if (attempt === maxRetries) throw error;
|
|
556
|
-
await (0, node_timers_promises.setTimeout)(Math.min(delay, maxDelay));
|
|
557
|
-
delay *= backoffFactor;
|
|
558
|
-
}
|
|
559
|
-
throw new RetryableError("withRetry exhausted retry attempts without resolving the operation");
|
|
390
|
+
const composeAbortSignal = (internalSignal, request) => {
|
|
391
|
+
const timeoutSignal = typeof request?.timeout === "number" ? AbortSignal.timeout(request.timeout) : void 0;
|
|
392
|
+
const signals = definedSignals([
|
|
393
|
+
internalSignal,
|
|
394
|
+
request?.signal,
|
|
395
|
+
timeoutSignal
|
|
396
|
+
]);
|
|
397
|
+
return {
|
|
398
|
+
signal: signals.length > 1 ? AbortSignal.any(signals) : signals[0],
|
|
399
|
+
timedOut: () => timeoutSignal?.aborted === true
|
|
560
400
|
};
|
|
561
|
-
}
|
|
401
|
+
};
|
|
562
402
|
|
|
563
403
|
//#endregion
|
|
564
|
-
//#region impl/utils/
|
|
565
|
-
const
|
|
566
|
-
const
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
info: (...args) => console.info(...args),
|
|
570
|
-
debug: (...args) => console.debug(...args)
|
|
571
|
-
});
|
|
572
|
-
const createNoopLogger = () => ({
|
|
573
|
-
error: noop,
|
|
574
|
-
warn: noop,
|
|
575
|
-
info: noop,
|
|
576
|
-
debug: noop
|
|
577
|
-
});
|
|
578
|
-
const bindLoggerMethod = (logger, level) => {
|
|
579
|
-
const method = logger[level];
|
|
580
|
-
return typeof method === "function" ? method.bind(logger) : noop;
|
|
404
|
+
//#region impl/utils/fetch.mts
|
|
405
|
+
const JSON_HEADERS = { "Content-Type": "application/json" };
|
|
406
|
+
const hasHeader = (headers, name) => {
|
|
407
|
+
const expected = name.toLowerCase();
|
|
408
|
+
return Object.keys(headers).some((header) => header.toLowerCase() === expected);
|
|
581
409
|
};
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
410
|
+
const toBasicAuthHeader = ({ username, password }) => {
|
|
411
|
+
return `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`;
|
|
412
|
+
};
|
|
413
|
+
const prepareRequest = (options) => {
|
|
414
|
+
const auth = options.auth;
|
|
415
|
+
const headers = { ...options.headers ?? {} };
|
|
416
|
+
if (auth && !hasHeader(headers, "Authorization")) headers.Authorization = toBasicAuthHeader(auth);
|
|
417
|
+
return { headers };
|
|
418
|
+
};
|
|
419
|
+
const isAbortError = (err) => {
|
|
420
|
+
return err instanceof DOMException && err.name === "AbortError";
|
|
421
|
+
};
|
|
422
|
+
const isTimeoutError = (err) => {
|
|
423
|
+
return err instanceof DOMException && err.name === "TimeoutError";
|
|
424
|
+
};
|
|
425
|
+
const encodeBody = (body) => {
|
|
426
|
+
if (body == null) return void 0;
|
|
427
|
+
if (typeof body === "string" || body instanceof ArrayBuffer || ArrayBuffer.isView(body) || body instanceof Blob || body instanceof FormData || body instanceof URLSearchParams || body instanceof ReadableStream) return body;
|
|
428
|
+
return JSON.stringify(body);
|
|
429
|
+
};
|
|
430
|
+
const parseJsonResponse = async (response) => {
|
|
431
|
+
if (response.status === 204 || response.status === 205) return null;
|
|
432
|
+
const text = await response.text();
|
|
433
|
+
if (text.trim() === "") return null;
|
|
434
|
+
try {
|
|
435
|
+
return JSON.parse(text);
|
|
436
|
+
} catch (err) {
|
|
437
|
+
if (response.ok) throw err;
|
|
438
|
+
return text;
|
|
599
439
|
}
|
|
600
|
-
|
|
601
|
-
|
|
440
|
+
};
|
|
441
|
+
async function fetchCouchJson(options) {
|
|
442
|
+
let response;
|
|
443
|
+
const { headers } = prepareRequest(options);
|
|
444
|
+
const { signal, timedOut } = composeAbortSignal(options.signal, options.request);
|
|
445
|
+
try {
|
|
446
|
+
response = await fetch(options.url, {
|
|
447
|
+
method: options.method,
|
|
448
|
+
headers: {
|
|
449
|
+
...JSON_HEADERS,
|
|
450
|
+
...headers
|
|
451
|
+
},
|
|
452
|
+
body: encodeBody(options.body),
|
|
453
|
+
signal,
|
|
454
|
+
dispatcher: options.request?.dispatcher
|
|
455
|
+
});
|
|
456
|
+
} catch (err) {
|
|
457
|
+
if (timedOut() || isTimeoutError(err)) throw new RetryableError("Request timed out", 503, {
|
|
458
|
+
category: "network",
|
|
459
|
+
cause: err,
|
|
460
|
+
operation: options.operation
|
|
461
|
+
});
|
|
462
|
+
if (isAbortError(err)) throw err;
|
|
463
|
+
RetryableError.handleNetworkError(err, options.operation);
|
|
464
|
+
}
|
|
465
|
+
return {
|
|
466
|
+
body: await parseJsonResponse(response),
|
|
467
|
+
headers: response.headers,
|
|
468
|
+
statusCode: response.status
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
async function fetchCouchStream(options) {
|
|
472
|
+
let response;
|
|
473
|
+
const { headers } = prepareRequest(options);
|
|
474
|
+
const { signal, timedOut } = composeAbortSignal(options.signal, options.request);
|
|
475
|
+
try {
|
|
476
|
+
response = await fetch(options.url, {
|
|
477
|
+
method: options.method,
|
|
478
|
+
headers,
|
|
479
|
+
body: encodeBody(options.body),
|
|
480
|
+
signal,
|
|
481
|
+
dispatcher: options.request?.dispatcher
|
|
482
|
+
});
|
|
483
|
+
} catch (err) {
|
|
484
|
+
if (timedOut() || isTimeoutError(err)) throw new RetryableError("Request timed out", 503, {
|
|
485
|
+
category: "network",
|
|
486
|
+
cause: err,
|
|
487
|
+
operation: options.operation
|
|
488
|
+
});
|
|
489
|
+
if (isAbortError(err)) throw err;
|
|
490
|
+
RetryableError.handleNetworkError(err, options.operation);
|
|
491
|
+
}
|
|
492
|
+
return {
|
|
493
|
+
body: response.body,
|
|
494
|
+
headers: response.headers,
|
|
495
|
+
statusCode: response.status
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
//#endregion
|
|
500
|
+
//#region impl/utils/url.mts
|
|
501
|
+
const ensureDirectoryUrl = (value) => {
|
|
502
|
+
const url = new URL(value);
|
|
503
|
+
if (!url.pathname.endsWith("/")) url.pathname = `${url.pathname}/`;
|
|
504
|
+
return url;
|
|
505
|
+
};
|
|
506
|
+
const createCouchDbUrl = (value) => {
|
|
507
|
+
return new URL(value);
|
|
508
|
+
};
|
|
509
|
+
const createCouchPathUrl = (path, base) => {
|
|
510
|
+
return new URL(path, ensureDirectoryUrl(base));
|
|
511
|
+
};
|
|
512
|
+
const createCouchDocUrl = (docId, base) => {
|
|
513
|
+
return new URL(encodeURIComponent(docId), ensureDirectoryUrl(base));
|
|
514
|
+
};
|
|
515
|
+
|
|
516
|
+
//#endregion
|
|
517
|
+
//#region schema/couch/mango.schema.ts
|
|
518
|
+
/** Selector operators are interpreted by CouchDB, including nested selectors. */
|
|
519
|
+
const MangoSelectorSchema = zod.z.record(zod.z.string(), zod.z.unknown());
|
|
520
|
+
const MangoSortSchema = zod.z.array(zod.z.union([zod.z.string(), zod.z.record(zod.z.string(), zod.z.enum(["asc", "desc"]))]));
|
|
521
|
+
/** Native CouchDB _find options; supported fields depend on the server version. */
|
|
522
|
+
const MangoQuerySchema = zod.z.object({
|
|
523
|
+
selector: MangoSelectorSchema,
|
|
524
|
+
limit: zod.z.number().int().positive().optional(),
|
|
525
|
+
skip: zod.z.number().int().nonnegative().optional(),
|
|
526
|
+
sort: MangoSortSchema.optional(),
|
|
527
|
+
fields: zod.z.array(zod.z.string()).optional(),
|
|
528
|
+
use_index: zod.z.union([zod.z.string(), zod.z.tuple([zod.z.string(), zod.z.string()])]).optional(),
|
|
529
|
+
conflicts: zod.z.boolean().optional(),
|
|
530
|
+
r: zod.z.number().int().positive().optional(),
|
|
531
|
+
bookmark: zod.z.string().optional(),
|
|
532
|
+
update: zod.z.boolean().optional(),
|
|
533
|
+
stable: zod.z.boolean().optional(),
|
|
534
|
+
stale: zod.z.union([zod.z.literal("ok"), zod.z.literal(false)]).optional().describe("Deprecated CouchDB option; prefer stable and update"),
|
|
535
|
+
execution_stats: zod.z.boolean().optional(),
|
|
536
|
+
allow_fallback: zod.z.boolean().optional()
|
|
537
|
+
}).strict();
|
|
538
|
+
const MangoExecutionStatsSchema = zod.z.looseObject({
|
|
539
|
+
total_keys_examined: zod.z.number(),
|
|
540
|
+
total_docs_examined: zod.z.number(),
|
|
541
|
+
total_quorum_docs_examined: zod.z.number(),
|
|
542
|
+
results_returned: zod.z.number(),
|
|
543
|
+
execution_time_ms: zod.z.number()
|
|
544
|
+
});
|
|
545
|
+
/** Opt into document validation by supplying the schema for your projection. */
|
|
546
|
+
const createMangoResponseSchema = (docSchema) => zod.z.looseObject({
|
|
547
|
+
docs: zod.z.array(docSchema),
|
|
548
|
+
bookmark: zod.z.string().optional(),
|
|
549
|
+
warning: zod.z.string().optional(),
|
|
550
|
+
execution_stats: MangoExecutionStatsSchema.optional()
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
//#endregion
|
|
554
|
+
//#region impl/find.mts
|
|
555
|
+
const MangoResponseSchema = createMangoResponseSchema(zod.z.record(zod.z.string(), zod.z.unknown()));
|
|
556
|
+
/** Query a Mango JSON index. Request controls come from config, as with query(). */
|
|
557
|
+
async function find(configInput, options) {
|
|
558
|
+
const config = CouchConfig.parse(configInput);
|
|
559
|
+
const body = MangoQuerySchema.parse(options);
|
|
560
|
+
let response;
|
|
561
|
+
try {
|
|
562
|
+
response = await fetchCouchJson({
|
|
563
|
+
auth: config.auth,
|
|
564
|
+
request: config.request,
|
|
565
|
+
method: "POST",
|
|
566
|
+
operation: "find",
|
|
567
|
+
url: createCouchPathUrl("_find", config.couch),
|
|
568
|
+
body
|
|
569
|
+
});
|
|
570
|
+
} catch (error) {
|
|
571
|
+
RetryableError.handleNetworkError(error, "find");
|
|
572
|
+
}
|
|
573
|
+
if (response.statusCode !== 200 || response.body?.error) throw createResponseError({
|
|
574
|
+
body: response.body,
|
|
575
|
+
defaultMessage: "Mango query failed",
|
|
576
|
+
operation: "find",
|
|
577
|
+
statusCode: response.statusCode
|
|
578
|
+
});
|
|
579
|
+
const parsed = MangoResponseSchema.safeParse(response.body);
|
|
580
|
+
if (!parsed.success) throw new OperationError("Invalid Mango response", { operation: "find" });
|
|
581
|
+
return parsed.data;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
//#endregion
|
|
585
|
+
//#region impl/utils/queryBuilder.mts
|
|
586
|
+
/**
|
|
587
|
+
* A builder class for constructing CouchDB view query options.
|
|
588
|
+
* Provides a fluent API for setting various query parameters.
|
|
589
|
+
* @example
|
|
590
|
+
* const queryOptions = new QueryBuilder()
|
|
591
|
+
* .limit(10)
|
|
592
|
+
* .include_docs()
|
|
593
|
+
* .startKey('someKey')
|
|
594
|
+
* .build();
|
|
595
|
+
* @see SimpleViewOptions for the full list of options.
|
|
596
|
+
*
|
|
597
|
+
* @remarks
|
|
598
|
+
* Each method corresponds to a CouchDB view option and returns the builder instance for chaining.
|
|
599
|
+
*
|
|
600
|
+
* @returns The constructed SimpleViewOptions object.
|
|
601
|
+
*/
|
|
602
|
+
var QueryBuilder = class {
|
|
603
|
+
#options = {};
|
|
604
|
+
descending(descending = true) {
|
|
605
|
+
this.#options.descending = descending;
|
|
606
|
+
return this;
|
|
607
|
+
}
|
|
608
|
+
endkey_docid(endkeyDocId) {
|
|
609
|
+
this.#options.endkey_docid = endkeyDocId;
|
|
610
|
+
return this;
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* Alias for endkey_docid
|
|
614
|
+
*/
|
|
615
|
+
end_key_doc_id(endkeyDocId) {
|
|
616
|
+
this.#options.endkey_docid = endkeyDocId;
|
|
617
|
+
return this;
|
|
618
|
+
}
|
|
619
|
+
endkey(endkey) {
|
|
620
|
+
this.#options.endkey = endkey;
|
|
621
|
+
return this;
|
|
622
|
+
}
|
|
623
|
+
/**
|
|
624
|
+
* Alias for endkey
|
|
625
|
+
*/
|
|
626
|
+
endKey(endkey) {
|
|
627
|
+
this.#options.endkey = endkey;
|
|
628
|
+
return this;
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* Alias for endkey
|
|
632
|
+
*/
|
|
633
|
+
end_key(endkey) {
|
|
634
|
+
this.#options.endkey = endkey;
|
|
635
|
+
return this;
|
|
636
|
+
}
|
|
637
|
+
group(group = true) {
|
|
638
|
+
this.#options.group = group;
|
|
639
|
+
return this;
|
|
640
|
+
}
|
|
641
|
+
group_level(level) {
|
|
642
|
+
this.#options.group_level = level;
|
|
643
|
+
return this;
|
|
644
|
+
}
|
|
645
|
+
include_docs(includeDocs = true) {
|
|
646
|
+
this.#options.include_docs = includeDocs;
|
|
647
|
+
return this;
|
|
648
|
+
}
|
|
649
|
+
inclusive_end(inclusiveEnd = true) {
|
|
650
|
+
this.#options.inclusive_end = inclusiveEnd;
|
|
651
|
+
return this;
|
|
652
|
+
}
|
|
653
|
+
key(key) {
|
|
654
|
+
this.#options.key = key;
|
|
655
|
+
return this;
|
|
656
|
+
}
|
|
657
|
+
keys(keys) {
|
|
658
|
+
this.#options.keys = keys;
|
|
659
|
+
return this;
|
|
660
|
+
}
|
|
661
|
+
limit(limit) {
|
|
662
|
+
this.#options.limit = limit;
|
|
663
|
+
return this;
|
|
664
|
+
}
|
|
665
|
+
reduce(reduce = true) {
|
|
666
|
+
this.#options.reduce = reduce;
|
|
667
|
+
return this;
|
|
668
|
+
}
|
|
669
|
+
skip(skip) {
|
|
670
|
+
this.#options.skip = skip;
|
|
671
|
+
return this;
|
|
672
|
+
}
|
|
673
|
+
sorted(sorted = true) {
|
|
674
|
+
this.#options.sorted = sorted;
|
|
675
|
+
return this;
|
|
676
|
+
}
|
|
677
|
+
stable(stable = true) {
|
|
678
|
+
this.#options.stable = stable;
|
|
679
|
+
return this;
|
|
680
|
+
}
|
|
681
|
+
startkey(startkey) {
|
|
682
|
+
this.#options.startkey = startkey;
|
|
683
|
+
return this;
|
|
684
|
+
}
|
|
685
|
+
/**
|
|
686
|
+
* Alias for startkey
|
|
687
|
+
*/
|
|
688
|
+
startKey(startkey) {
|
|
689
|
+
this.#options.startkey = startkey;
|
|
690
|
+
return this;
|
|
691
|
+
}
|
|
692
|
+
/**
|
|
693
|
+
* Alias for startkey
|
|
694
|
+
*/
|
|
695
|
+
start_key(startkey) {
|
|
696
|
+
this.#options.startkey = startkey;
|
|
697
|
+
return this;
|
|
698
|
+
}
|
|
699
|
+
startkey_docid(startkeyDocId) {
|
|
700
|
+
this.#options.startkey_docid = startkeyDocId;
|
|
701
|
+
return this;
|
|
702
|
+
}
|
|
703
|
+
/**
|
|
704
|
+
* Alias for startkey_docid
|
|
705
|
+
*/
|
|
706
|
+
start_key_doc_id(startkeyDocId) {
|
|
707
|
+
this.#options.startkey_docid = startkeyDocId;
|
|
708
|
+
return this;
|
|
709
|
+
}
|
|
710
|
+
update(update) {
|
|
711
|
+
this.#options.update = update;
|
|
712
|
+
return this;
|
|
713
|
+
}
|
|
714
|
+
update_seq(updateSeq = true) {
|
|
715
|
+
this.#options.update_seq = updateSeq;
|
|
716
|
+
return this;
|
|
717
|
+
}
|
|
718
|
+
/**
|
|
719
|
+
* Builds and returns the ViewOptions object.
|
|
720
|
+
*/
|
|
721
|
+
build() {
|
|
722
|
+
return { ...this.#options };
|
|
723
|
+
}
|
|
724
|
+
};
|
|
725
|
+
const createQuery = () => new QueryBuilder();
|
|
726
|
+
|
|
727
|
+
//#endregion
|
|
728
|
+
//#region impl/retry.mts
|
|
729
|
+
const shouldRetryError = (error, attempt) => {
|
|
730
|
+
return error instanceof RetryableError || isTransientAuthError(error, attempt);
|
|
731
|
+
};
|
|
732
|
+
/**
|
|
733
|
+
* Wrap an async-capable function with retry semantics that respect {@link RetryableError}.
|
|
734
|
+
* @typeParam Fn - The function signature to decorate with retry handling.
|
|
735
|
+
* @param fn The function to invoke with retry support.
|
|
736
|
+
* @param options Retry tuning parameters.
|
|
737
|
+
* @returns A function mirroring `fn` that automatically retries on {@link RetryableError}.
|
|
738
|
+
*/
|
|
739
|
+
function withRetry(fn, options = {}) {
|
|
740
|
+
const { maxRetries = 3, initialDelay = 1e3, backoffFactor = 2, maxDelay = 3e4 } = options;
|
|
741
|
+
return async (...args) => {
|
|
742
|
+
let delay = initialDelay;
|
|
743
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) try {
|
|
744
|
+
return await fn(...args);
|
|
745
|
+
} catch (error) {
|
|
746
|
+
if (!shouldRetryError(error, attempt)) throw error;
|
|
747
|
+
if (attempt === maxRetries) throw error;
|
|
748
|
+
await (0, node_timers_promises.setTimeout)(Math.min(delay, maxDelay));
|
|
749
|
+
delay *= backoffFactor;
|
|
750
|
+
}
|
|
751
|
+
throw new RetryableError("withRetry exhausted retry attempts without resolving the operation");
|
|
752
|
+
};
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
//#endregion
|
|
756
|
+
//#region impl/utils/logger.mts
|
|
757
|
+
const noop = () => {};
|
|
758
|
+
const createConsoleLogger = () => ({
|
|
759
|
+
error: (...args) => console.error(...args),
|
|
760
|
+
warn: (...args) => console.warn(...args),
|
|
761
|
+
info: (...args) => console.info(...args),
|
|
762
|
+
debug: (...args) => console.debug(...args)
|
|
763
|
+
});
|
|
764
|
+
const createNoopLogger = () => ({
|
|
765
|
+
error: noop,
|
|
766
|
+
warn: noop,
|
|
767
|
+
info: noop,
|
|
768
|
+
debug: noop
|
|
769
|
+
});
|
|
770
|
+
const bindLoggerMethod = (logger, level) => {
|
|
771
|
+
const method = logger[level];
|
|
772
|
+
return typeof method === "function" ? method.bind(logger) : noop;
|
|
773
|
+
};
|
|
774
|
+
function createLogger(config) {
|
|
775
|
+
if (config["~normalizedLogger"]) return config["~normalizedLogger"];
|
|
776
|
+
if (!config.logger) {
|
|
777
|
+
const normalized$1 = config.useConsoleLogger ? createConsoleLogger() : createNoopLogger();
|
|
778
|
+
config["~normalizedLogger"] = normalized$1;
|
|
779
|
+
return normalized$1;
|
|
780
|
+
}
|
|
781
|
+
if (typeof config.logger === "function") {
|
|
782
|
+
const loggerFn = config.logger;
|
|
783
|
+
const normalized$1 = {
|
|
784
|
+
error: (...args) => loggerFn("error", ...args),
|
|
785
|
+
warn: (...args) => loggerFn("warn", ...args),
|
|
786
|
+
info: (...args) => loggerFn("info", ...args),
|
|
787
|
+
debug: (...args) => loggerFn("debug", ...args)
|
|
788
|
+
};
|
|
789
|
+
config["~normalizedLogger"] = normalized$1;
|
|
790
|
+
return normalized$1;
|
|
791
|
+
}
|
|
792
|
+
const loggerObj = config.logger;
|
|
793
|
+
const normalized = {
|
|
602
794
|
error: bindLoggerMethod(loggerObj, "error"),
|
|
603
795
|
warn: bindLoggerMethod(loggerObj, "warn"),
|
|
604
796
|
info: bindLoggerMethod(loggerObj, "info"),
|
|
@@ -765,137 +957,6 @@ async function parseRows(rows, options) {
|
|
|
765
957
|
}))).filter(isFinalRow);
|
|
766
958
|
}
|
|
767
959
|
|
|
768
|
-
//#endregion
|
|
769
|
-
//#region impl/utils/request.mts
|
|
770
|
-
const definedSignals = (signals) => {
|
|
771
|
-
return signals.filter((signal) => signal != null);
|
|
772
|
-
};
|
|
773
|
-
const composeAbortSignal = (internalSignal, request) => {
|
|
774
|
-
const timeoutSignal = typeof request?.timeout === "number" ? AbortSignal.timeout(request.timeout) : void 0;
|
|
775
|
-
const signals = definedSignals([
|
|
776
|
-
internalSignal,
|
|
777
|
-
request?.signal,
|
|
778
|
-
timeoutSignal
|
|
779
|
-
]);
|
|
780
|
-
return {
|
|
781
|
-
signal: signals.length > 1 ? AbortSignal.any(signals) : signals[0],
|
|
782
|
-
timedOut: () => timeoutSignal?.aborted === true
|
|
783
|
-
};
|
|
784
|
-
};
|
|
785
|
-
|
|
786
|
-
//#endregion
|
|
787
|
-
//#region impl/utils/fetch.mts
|
|
788
|
-
const JSON_HEADERS = { "Content-Type": "application/json" };
|
|
789
|
-
const hasHeader = (headers, name) => {
|
|
790
|
-
const expected = name.toLowerCase();
|
|
791
|
-
return Object.keys(headers).some((header) => header.toLowerCase() === expected);
|
|
792
|
-
};
|
|
793
|
-
const toBasicAuthHeader = ({ username, password }) => {
|
|
794
|
-
return `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`;
|
|
795
|
-
};
|
|
796
|
-
const prepareRequest = (options) => {
|
|
797
|
-
const auth = options.auth;
|
|
798
|
-
const headers = { ...options.headers ?? {} };
|
|
799
|
-
if (auth && !hasHeader(headers, "Authorization")) headers.Authorization = toBasicAuthHeader(auth);
|
|
800
|
-
return { headers };
|
|
801
|
-
};
|
|
802
|
-
const isAbortError = (err) => {
|
|
803
|
-
return err instanceof DOMException && err.name === "AbortError";
|
|
804
|
-
};
|
|
805
|
-
const isTimeoutError = (err) => {
|
|
806
|
-
return err instanceof DOMException && err.name === "TimeoutError";
|
|
807
|
-
};
|
|
808
|
-
const encodeBody = (body) => {
|
|
809
|
-
if (body == null) return void 0;
|
|
810
|
-
if (typeof body === "string" || body instanceof ArrayBuffer || ArrayBuffer.isView(body) || body instanceof Blob || body instanceof FormData || body instanceof URLSearchParams || body instanceof ReadableStream) return body;
|
|
811
|
-
return JSON.stringify(body);
|
|
812
|
-
};
|
|
813
|
-
const parseJsonResponse = async (response) => {
|
|
814
|
-
if (response.status === 204 || response.status === 205) return null;
|
|
815
|
-
const text = await response.text();
|
|
816
|
-
if (text.trim() === "") return null;
|
|
817
|
-
try {
|
|
818
|
-
return JSON.parse(text);
|
|
819
|
-
} catch (err) {
|
|
820
|
-
if (response.ok) throw err;
|
|
821
|
-
return text;
|
|
822
|
-
}
|
|
823
|
-
};
|
|
824
|
-
async function fetchCouchJson(options) {
|
|
825
|
-
let response;
|
|
826
|
-
const { headers } = prepareRequest(options);
|
|
827
|
-
const { signal, timedOut } = composeAbortSignal(options.signal, options.request);
|
|
828
|
-
try {
|
|
829
|
-
response = await fetch(options.url, {
|
|
830
|
-
method: options.method,
|
|
831
|
-
headers: {
|
|
832
|
-
...JSON_HEADERS,
|
|
833
|
-
...headers
|
|
834
|
-
},
|
|
835
|
-
body: encodeBody(options.body),
|
|
836
|
-
signal,
|
|
837
|
-
dispatcher: options.request?.dispatcher
|
|
838
|
-
});
|
|
839
|
-
} catch (err) {
|
|
840
|
-
if (timedOut() || isTimeoutError(err)) throw new RetryableError("Request timed out", 503, {
|
|
841
|
-
category: "network",
|
|
842
|
-
cause: err,
|
|
843
|
-
operation: options.operation
|
|
844
|
-
});
|
|
845
|
-
if (isAbortError(err)) throw err;
|
|
846
|
-
RetryableError.handleNetworkError(err, options.operation);
|
|
847
|
-
}
|
|
848
|
-
return {
|
|
849
|
-
body: await parseJsonResponse(response),
|
|
850
|
-
headers: response.headers,
|
|
851
|
-
statusCode: response.status
|
|
852
|
-
};
|
|
853
|
-
}
|
|
854
|
-
async function fetchCouchStream(options) {
|
|
855
|
-
let response;
|
|
856
|
-
const { headers } = prepareRequest(options);
|
|
857
|
-
const { signal, timedOut } = composeAbortSignal(options.signal, options.request);
|
|
858
|
-
try {
|
|
859
|
-
response = await fetch(options.url, {
|
|
860
|
-
method: options.method,
|
|
861
|
-
headers,
|
|
862
|
-
body: encodeBody(options.body),
|
|
863
|
-
signal,
|
|
864
|
-
dispatcher: options.request?.dispatcher
|
|
865
|
-
});
|
|
866
|
-
} catch (err) {
|
|
867
|
-
if (timedOut() || isTimeoutError(err)) throw new RetryableError("Request timed out", 503, {
|
|
868
|
-
category: "network",
|
|
869
|
-
cause: err,
|
|
870
|
-
operation: options.operation
|
|
871
|
-
});
|
|
872
|
-
if (isAbortError(err)) throw err;
|
|
873
|
-
RetryableError.handleNetworkError(err, options.operation);
|
|
874
|
-
}
|
|
875
|
-
return {
|
|
876
|
-
body: response.body,
|
|
877
|
-
headers: response.headers,
|
|
878
|
-
statusCode: response.status
|
|
879
|
-
};
|
|
880
|
-
}
|
|
881
|
-
|
|
882
|
-
//#endregion
|
|
883
|
-
//#region impl/utils/url.mts
|
|
884
|
-
const ensureDirectoryUrl = (value) => {
|
|
885
|
-
const url = new URL(value);
|
|
886
|
-
if (!url.pathname.endsWith("/")) url.pathname = `${url.pathname}/`;
|
|
887
|
-
return url;
|
|
888
|
-
};
|
|
889
|
-
const createCouchDbUrl = (value) => {
|
|
890
|
-
return new URL(value);
|
|
891
|
-
};
|
|
892
|
-
const createCouchPathUrl = (path, base) => {
|
|
893
|
-
return new URL(path, ensureDirectoryUrl(base));
|
|
894
|
-
};
|
|
895
|
-
const createCouchDocUrl = (docId, base) => {
|
|
896
|
-
return new URL(encodeURIComponent(docId), ensureDirectoryUrl(base));
|
|
897
|
-
};
|
|
898
|
-
|
|
899
960
|
//#endregion
|
|
900
961
|
//#region impl/bulkGet.mts
|
|
901
962
|
/**
|
|
@@ -2446,6 +2507,7 @@ function doBind(config) {
|
|
|
2446
2507
|
bulkGetDictionary: getBoundWithRetry(bulkGetDictionary, config),
|
|
2447
2508
|
get: getBoundWithRetry(get, config),
|
|
2448
2509
|
getAtRev: getBoundWithRetry(getAtRev, config),
|
|
2510
|
+
find: getBoundWithRetry(find, config),
|
|
2449
2511
|
query: getBoundWithRetry(query, config),
|
|
2450
2512
|
bulkRemove: config.bindWithRetry ? withRetry(bulkRemove.bind(null, config), retryOptions) : bulkRemove.bind(null, config),
|
|
2451
2513
|
bulkRemoveMap: config.bindWithRetry ? withRetry(bulkRemoveMap.bind(null, config), retryOptions) : bulkRemoveMap.bind(null, config),
|
|
@@ -2467,6 +2529,10 @@ function doBind(config) {
|
|
|
2467
2529
|
//#endregion
|
|
2468
2530
|
exports.ConflictError = ConflictError;
|
|
2469
2531
|
exports.HideABedError = HideABedError;
|
|
2532
|
+
exports.MangoExecutionStatsSchema = MangoExecutionStatsSchema;
|
|
2533
|
+
exports.MangoQuerySchema = MangoQuerySchema;
|
|
2534
|
+
exports.MangoSelectorSchema = MangoSelectorSchema;
|
|
2535
|
+
exports.MangoSortSchema = MangoSortSchema;
|
|
2470
2536
|
exports.NotFoundError = NotFoundError;
|
|
2471
2537
|
exports.OperationError = OperationError;
|
|
2472
2538
|
exports.QueryBuilder = QueryBuilder;
|
|
@@ -2480,7 +2546,9 @@ exports.bulkRemoveMap = bulkRemoveMap;
|
|
|
2480
2546
|
exports.bulkSave = bulkSave;
|
|
2481
2547
|
exports.bulkSaveTransaction = bulkSaveTransaction;
|
|
2482
2548
|
exports.createLock = createLock;
|
|
2549
|
+
exports.createMangoResponseSchema = createMangoResponseSchema;
|
|
2483
2550
|
exports.createQuery = createQuery;
|
|
2551
|
+
exports.find = find;
|
|
2484
2552
|
exports.get = get;
|
|
2485
2553
|
exports.getAtRev = getAtRev;
|
|
2486
2554
|
exports.getDBInfo = getDBInfo;
|