hide-a-bed 7.1.3 → 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 +414 -339
- package/dist/esm/index.mjs +409 -340
- 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/index.mts +19 -0
- package/package.json +1 -1
- 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/index.d.mts +5 -1
- package/types/output/index.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;
|
|
@@ -526,77 +383,419 @@ function isConflictError(err) {
|
|
|
526
383
|
}
|
|
527
384
|
|
|
528
385
|
//#endregion
|
|
529
|
-
//#region impl/
|
|
530
|
-
const
|
|
531
|
-
return
|
|
386
|
+
//#region impl/utils/request.mts
|
|
387
|
+
const definedSignals = (signals) => {
|
|
388
|
+
return signals.filter((signal) => signal != null);
|
|
532
389
|
};
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
let delay = initialDelay;
|
|
544
|
-
for (let attempt = 0; attempt <= maxRetries; attempt++) try {
|
|
545
|
-
return await fn(...args);
|
|
546
|
-
} catch (error) {
|
|
547
|
-
if (!shouldRetryError(error, attempt)) throw error;
|
|
548
|
-
if (attempt === maxRetries) throw error;
|
|
549
|
-
await (0, node_timers_promises.setTimeout)(Math.min(delay, maxDelay));
|
|
550
|
-
delay *= backoffFactor;
|
|
551
|
-
}
|
|
552
|
-
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
|
|
553
400
|
};
|
|
554
|
-
}
|
|
401
|
+
};
|
|
555
402
|
|
|
556
403
|
//#endregion
|
|
557
|
-
//#region impl/utils/
|
|
558
|
-
const
|
|
559
|
-
const
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
info: (...args) => console.info(...args),
|
|
563
|
-
debug: (...args) => console.debug(...args)
|
|
564
|
-
});
|
|
565
|
-
const createNoopLogger = () => ({
|
|
566
|
-
error: noop,
|
|
567
|
-
warn: noop,
|
|
568
|
-
info: noop,
|
|
569
|
-
debug: noop
|
|
570
|
-
});
|
|
571
|
-
const bindLoggerMethod = (logger, level) => {
|
|
572
|
-
const method = logger[level];
|
|
573
|
-
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);
|
|
574
409
|
};
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
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;
|
|
592
439
|
}
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
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 = {
|
|
794
|
+
error: bindLoggerMethod(loggerObj, "error"),
|
|
795
|
+
warn: bindLoggerMethod(loggerObj, "warn"),
|
|
796
|
+
info: bindLoggerMethod(loggerObj, "info"),
|
|
797
|
+
debug: bindLoggerMethod(loggerObj, "debug")
|
|
798
|
+
};
|
|
600
799
|
config["~normalizedLogger"] = normalized;
|
|
601
800
|
return normalized;
|
|
602
801
|
}
|
|
@@ -758,137 +957,6 @@ async function parseRows(rows, options) {
|
|
|
758
957
|
}))).filter(isFinalRow);
|
|
759
958
|
}
|
|
760
959
|
|
|
761
|
-
//#endregion
|
|
762
|
-
//#region impl/utils/request.mts
|
|
763
|
-
const definedSignals = (signals) => {
|
|
764
|
-
return signals.filter((signal) => signal != null);
|
|
765
|
-
};
|
|
766
|
-
const composeAbortSignal = (internalSignal, request) => {
|
|
767
|
-
const timeoutSignal = typeof request?.timeout === "number" ? AbortSignal.timeout(request.timeout) : void 0;
|
|
768
|
-
const signals = definedSignals([
|
|
769
|
-
internalSignal,
|
|
770
|
-
request?.signal,
|
|
771
|
-
timeoutSignal
|
|
772
|
-
]);
|
|
773
|
-
return {
|
|
774
|
-
signal: signals.length > 1 ? AbortSignal.any(signals) : signals[0],
|
|
775
|
-
timedOut: () => timeoutSignal?.aborted === true
|
|
776
|
-
};
|
|
777
|
-
};
|
|
778
|
-
|
|
779
|
-
//#endregion
|
|
780
|
-
//#region impl/utils/fetch.mts
|
|
781
|
-
const JSON_HEADERS = { "Content-Type": "application/json" };
|
|
782
|
-
const hasHeader = (headers, name) => {
|
|
783
|
-
const expected = name.toLowerCase();
|
|
784
|
-
return Object.keys(headers).some((header) => header.toLowerCase() === expected);
|
|
785
|
-
};
|
|
786
|
-
const toBasicAuthHeader = ({ username, password }) => {
|
|
787
|
-
return `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`;
|
|
788
|
-
};
|
|
789
|
-
const prepareRequest = (options) => {
|
|
790
|
-
const auth = options.auth;
|
|
791
|
-
const headers = { ...options.headers ?? {} };
|
|
792
|
-
if (auth && !hasHeader(headers, "Authorization")) headers.Authorization = toBasicAuthHeader(auth);
|
|
793
|
-
return { headers };
|
|
794
|
-
};
|
|
795
|
-
const isAbortError = (err) => {
|
|
796
|
-
return err instanceof DOMException && err.name === "AbortError";
|
|
797
|
-
};
|
|
798
|
-
const isTimeoutError = (err) => {
|
|
799
|
-
return err instanceof DOMException && err.name === "TimeoutError";
|
|
800
|
-
};
|
|
801
|
-
const encodeBody = (body) => {
|
|
802
|
-
if (body == null) return void 0;
|
|
803
|
-
if (typeof body === "string" || body instanceof ArrayBuffer || ArrayBuffer.isView(body) || body instanceof Blob || body instanceof FormData || body instanceof URLSearchParams || body instanceof ReadableStream) return body;
|
|
804
|
-
return JSON.stringify(body);
|
|
805
|
-
};
|
|
806
|
-
const parseJsonResponse = async (response) => {
|
|
807
|
-
if (response.status === 204 || response.status === 205) return null;
|
|
808
|
-
const text = await response.text();
|
|
809
|
-
if (text.trim() === "") return null;
|
|
810
|
-
try {
|
|
811
|
-
return JSON.parse(text);
|
|
812
|
-
} catch (err) {
|
|
813
|
-
if (response.ok) throw err;
|
|
814
|
-
return text;
|
|
815
|
-
}
|
|
816
|
-
};
|
|
817
|
-
async function fetchCouchJson(options) {
|
|
818
|
-
let response;
|
|
819
|
-
const { headers } = prepareRequest(options);
|
|
820
|
-
const { signal, timedOut } = composeAbortSignal(options.signal, options.request);
|
|
821
|
-
try {
|
|
822
|
-
response = await fetch(options.url, {
|
|
823
|
-
method: options.method,
|
|
824
|
-
headers: {
|
|
825
|
-
...JSON_HEADERS,
|
|
826
|
-
...headers
|
|
827
|
-
},
|
|
828
|
-
body: encodeBody(options.body),
|
|
829
|
-
signal,
|
|
830
|
-
dispatcher: options.request?.dispatcher
|
|
831
|
-
});
|
|
832
|
-
} catch (err) {
|
|
833
|
-
if (timedOut() || isTimeoutError(err)) throw new RetryableError("Request timed out", 503, {
|
|
834
|
-
category: "network",
|
|
835
|
-
cause: err,
|
|
836
|
-
operation: options.operation
|
|
837
|
-
});
|
|
838
|
-
if (isAbortError(err)) throw err;
|
|
839
|
-
RetryableError.handleNetworkError(err, options.operation);
|
|
840
|
-
}
|
|
841
|
-
return {
|
|
842
|
-
body: await parseJsonResponse(response),
|
|
843
|
-
headers: response.headers,
|
|
844
|
-
statusCode: response.status
|
|
845
|
-
};
|
|
846
|
-
}
|
|
847
|
-
async function fetchCouchStream(options) {
|
|
848
|
-
let response;
|
|
849
|
-
const { headers } = prepareRequest(options);
|
|
850
|
-
const { signal, timedOut } = composeAbortSignal(options.signal, options.request);
|
|
851
|
-
try {
|
|
852
|
-
response = await fetch(options.url, {
|
|
853
|
-
method: options.method,
|
|
854
|
-
headers,
|
|
855
|
-
body: encodeBody(options.body),
|
|
856
|
-
signal,
|
|
857
|
-
dispatcher: options.request?.dispatcher
|
|
858
|
-
});
|
|
859
|
-
} catch (err) {
|
|
860
|
-
if (timedOut() || isTimeoutError(err)) throw new RetryableError("Request timed out", 503, {
|
|
861
|
-
category: "network",
|
|
862
|
-
cause: err,
|
|
863
|
-
operation: options.operation
|
|
864
|
-
});
|
|
865
|
-
if (isAbortError(err)) throw err;
|
|
866
|
-
RetryableError.handleNetworkError(err, options.operation);
|
|
867
|
-
}
|
|
868
|
-
return {
|
|
869
|
-
body: response.body,
|
|
870
|
-
headers: response.headers,
|
|
871
|
-
statusCode: response.status
|
|
872
|
-
};
|
|
873
|
-
}
|
|
874
|
-
|
|
875
|
-
//#endregion
|
|
876
|
-
//#region impl/utils/url.mts
|
|
877
|
-
const ensureDirectoryUrl = (value) => {
|
|
878
|
-
const url = new URL(value);
|
|
879
|
-
if (!url.pathname.endsWith("/")) url.pathname = `${url.pathname}/`;
|
|
880
|
-
return url;
|
|
881
|
-
};
|
|
882
|
-
const createCouchDbUrl = (value) => {
|
|
883
|
-
return new URL(value);
|
|
884
|
-
};
|
|
885
|
-
const createCouchPathUrl = (path, base) => {
|
|
886
|
-
return new URL(path, ensureDirectoryUrl(base));
|
|
887
|
-
};
|
|
888
|
-
const createCouchDocUrl = (docId, base) => {
|
|
889
|
-
return new URL(encodeURIComponent(docId), ensureDirectoryUrl(base));
|
|
890
|
-
};
|
|
891
|
-
|
|
892
960
|
//#endregion
|
|
893
961
|
//#region impl/bulkGet.mts
|
|
894
962
|
/**
|
|
@@ -2439,6 +2507,7 @@ function doBind(config) {
|
|
|
2439
2507
|
bulkGetDictionary: getBoundWithRetry(bulkGetDictionary, config),
|
|
2440
2508
|
get: getBoundWithRetry(get, config),
|
|
2441
2509
|
getAtRev: getBoundWithRetry(getAtRev, config),
|
|
2510
|
+
find: getBoundWithRetry(find, config),
|
|
2442
2511
|
query: getBoundWithRetry(query, config),
|
|
2443
2512
|
bulkRemove: config.bindWithRetry ? withRetry(bulkRemove.bind(null, config), retryOptions) : bulkRemove.bind(null, config),
|
|
2444
2513
|
bulkRemoveMap: config.bindWithRetry ? withRetry(bulkRemoveMap.bind(null, config), retryOptions) : bulkRemoveMap.bind(null, config),
|
|
@@ -2460,6 +2529,10 @@ function doBind(config) {
|
|
|
2460
2529
|
//#endregion
|
|
2461
2530
|
exports.ConflictError = ConflictError;
|
|
2462
2531
|
exports.HideABedError = HideABedError;
|
|
2532
|
+
exports.MangoExecutionStatsSchema = MangoExecutionStatsSchema;
|
|
2533
|
+
exports.MangoQuerySchema = MangoQuerySchema;
|
|
2534
|
+
exports.MangoSelectorSchema = MangoSelectorSchema;
|
|
2535
|
+
exports.MangoSortSchema = MangoSortSchema;
|
|
2463
2536
|
exports.NotFoundError = NotFoundError;
|
|
2464
2537
|
exports.OperationError = OperationError;
|
|
2465
2538
|
exports.QueryBuilder = QueryBuilder;
|
|
@@ -2473,7 +2546,9 @@ exports.bulkRemoveMap = bulkRemoveMap;
|
|
|
2473
2546
|
exports.bulkSave = bulkSave;
|
|
2474
2547
|
exports.bulkSaveTransaction = bulkSaveTransaction;
|
|
2475
2548
|
exports.createLock = createLock;
|
|
2549
|
+
exports.createMangoResponseSchema = createMangoResponseSchema;
|
|
2476
2550
|
exports.createQuery = createQuery;
|
|
2551
|
+
exports.find = find;
|
|
2477
2552
|
exports.get = get;
|
|
2478
2553
|
exports.getAtRev = getAtRev;
|
|
2479
2554
|
exports.getDBInfo = getDBInfo;
|