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/esm/index.mjs
CHANGED
|
@@ -7,149 +7,6 @@ import Pick from "stream-json/filters/Pick.js";
|
|
|
7
7
|
import StreamArray from "stream-json/streamers/StreamArray.js";
|
|
8
8
|
import { EventEmitter } from "events";
|
|
9
9
|
|
|
10
|
-
//#region impl/utils/queryBuilder.mts
|
|
11
|
-
/**
|
|
12
|
-
* A builder class for constructing CouchDB view query options.
|
|
13
|
-
* Provides a fluent API for setting various query parameters.
|
|
14
|
-
* @example
|
|
15
|
-
* const queryOptions = new QueryBuilder()
|
|
16
|
-
* .limit(10)
|
|
17
|
-
* .include_docs()
|
|
18
|
-
* .startKey('someKey')
|
|
19
|
-
* .build();
|
|
20
|
-
* @see SimpleViewOptions for the full list of options.
|
|
21
|
-
*
|
|
22
|
-
* @remarks
|
|
23
|
-
* Each method corresponds to a CouchDB view option and returns the builder instance for chaining.
|
|
24
|
-
*
|
|
25
|
-
* @returns The constructed SimpleViewOptions object.
|
|
26
|
-
*/
|
|
27
|
-
var QueryBuilder = class {
|
|
28
|
-
#options = {};
|
|
29
|
-
descending(descending = true) {
|
|
30
|
-
this.#options.descending = descending;
|
|
31
|
-
return this;
|
|
32
|
-
}
|
|
33
|
-
endkey_docid(endkeyDocId) {
|
|
34
|
-
this.#options.endkey_docid = endkeyDocId;
|
|
35
|
-
return this;
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Alias for endkey_docid
|
|
39
|
-
*/
|
|
40
|
-
end_key_doc_id(endkeyDocId) {
|
|
41
|
-
this.#options.endkey_docid = endkeyDocId;
|
|
42
|
-
return this;
|
|
43
|
-
}
|
|
44
|
-
endkey(endkey) {
|
|
45
|
-
this.#options.endkey = endkey;
|
|
46
|
-
return this;
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Alias for endkey
|
|
50
|
-
*/
|
|
51
|
-
endKey(endkey) {
|
|
52
|
-
this.#options.endkey = endkey;
|
|
53
|
-
return this;
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Alias for endkey
|
|
57
|
-
*/
|
|
58
|
-
end_key(endkey) {
|
|
59
|
-
this.#options.endkey = endkey;
|
|
60
|
-
return this;
|
|
61
|
-
}
|
|
62
|
-
group(group = true) {
|
|
63
|
-
this.#options.group = group;
|
|
64
|
-
return this;
|
|
65
|
-
}
|
|
66
|
-
group_level(level) {
|
|
67
|
-
this.#options.group_level = level;
|
|
68
|
-
return this;
|
|
69
|
-
}
|
|
70
|
-
include_docs(includeDocs = true) {
|
|
71
|
-
this.#options.include_docs = includeDocs;
|
|
72
|
-
return this;
|
|
73
|
-
}
|
|
74
|
-
inclusive_end(inclusiveEnd = true) {
|
|
75
|
-
this.#options.inclusive_end = inclusiveEnd;
|
|
76
|
-
return this;
|
|
77
|
-
}
|
|
78
|
-
key(key) {
|
|
79
|
-
this.#options.key = key;
|
|
80
|
-
return this;
|
|
81
|
-
}
|
|
82
|
-
keys(keys) {
|
|
83
|
-
this.#options.keys = keys;
|
|
84
|
-
return this;
|
|
85
|
-
}
|
|
86
|
-
limit(limit) {
|
|
87
|
-
this.#options.limit = limit;
|
|
88
|
-
return this;
|
|
89
|
-
}
|
|
90
|
-
reduce(reduce = true) {
|
|
91
|
-
this.#options.reduce = reduce;
|
|
92
|
-
return this;
|
|
93
|
-
}
|
|
94
|
-
skip(skip) {
|
|
95
|
-
this.#options.skip = skip;
|
|
96
|
-
return this;
|
|
97
|
-
}
|
|
98
|
-
sorted(sorted = true) {
|
|
99
|
-
this.#options.sorted = sorted;
|
|
100
|
-
return this;
|
|
101
|
-
}
|
|
102
|
-
stable(stable = true) {
|
|
103
|
-
this.#options.stable = stable;
|
|
104
|
-
return this;
|
|
105
|
-
}
|
|
106
|
-
startkey(startkey) {
|
|
107
|
-
this.#options.startkey = startkey;
|
|
108
|
-
return this;
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Alias for startkey
|
|
112
|
-
*/
|
|
113
|
-
startKey(startkey) {
|
|
114
|
-
this.#options.startkey = startkey;
|
|
115
|
-
return this;
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Alias for startkey
|
|
119
|
-
*/
|
|
120
|
-
start_key(startkey) {
|
|
121
|
-
this.#options.startkey = startkey;
|
|
122
|
-
return this;
|
|
123
|
-
}
|
|
124
|
-
startkey_docid(startkeyDocId) {
|
|
125
|
-
this.#options.startkey_docid = startkeyDocId;
|
|
126
|
-
return this;
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Alias for startkey_docid
|
|
130
|
-
*/
|
|
131
|
-
start_key_doc_id(startkeyDocId) {
|
|
132
|
-
this.#options.startkey_docid = startkeyDocId;
|
|
133
|
-
return this;
|
|
134
|
-
}
|
|
135
|
-
update(update) {
|
|
136
|
-
this.#options.update = update;
|
|
137
|
-
return this;
|
|
138
|
-
}
|
|
139
|
-
update_seq(updateSeq = true) {
|
|
140
|
-
this.#options.update_seq = updateSeq;
|
|
141
|
-
return this;
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Builds and returns the ViewOptions object.
|
|
145
|
-
*/
|
|
146
|
-
build() {
|
|
147
|
-
return { ...this.#options };
|
|
148
|
-
}
|
|
149
|
-
};
|
|
150
|
-
const createQuery = () => new QueryBuilder();
|
|
151
|
-
|
|
152
|
-
//#endregion
|
|
153
10
|
//#region schema/request.mts
|
|
154
11
|
const isAbortSignal = (value) => {
|
|
155
12
|
return value instanceof AbortSignal;
|
|
@@ -494,77 +351,419 @@ function isConflictError(err) {
|
|
|
494
351
|
}
|
|
495
352
|
|
|
496
353
|
//#endregion
|
|
497
|
-
//#region impl/
|
|
498
|
-
const
|
|
499
|
-
return
|
|
354
|
+
//#region impl/utils/request.mts
|
|
355
|
+
const definedSignals = (signals) => {
|
|
356
|
+
return signals.filter((signal) => signal != null);
|
|
500
357
|
};
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
let delay = initialDelay;
|
|
512
|
-
for (let attempt = 0; attempt <= maxRetries; attempt++) try {
|
|
513
|
-
return await fn(...args);
|
|
514
|
-
} catch (error) {
|
|
515
|
-
if (!shouldRetryError(error, attempt)) throw error;
|
|
516
|
-
if (attempt === maxRetries) throw error;
|
|
517
|
-
await setTimeout$1(Math.min(delay, maxDelay));
|
|
518
|
-
delay *= backoffFactor;
|
|
519
|
-
}
|
|
520
|
-
throw new RetryableError("withRetry exhausted retry attempts without resolving the operation");
|
|
358
|
+
const composeAbortSignal = (internalSignal, request) => {
|
|
359
|
+
const timeoutSignal = typeof request?.timeout === "number" ? AbortSignal.timeout(request.timeout) : void 0;
|
|
360
|
+
const signals = definedSignals([
|
|
361
|
+
internalSignal,
|
|
362
|
+
request?.signal,
|
|
363
|
+
timeoutSignal
|
|
364
|
+
]);
|
|
365
|
+
return {
|
|
366
|
+
signal: signals.length > 1 ? AbortSignal.any(signals) : signals[0],
|
|
367
|
+
timedOut: () => timeoutSignal?.aborted === true
|
|
521
368
|
};
|
|
522
|
-
}
|
|
369
|
+
};
|
|
523
370
|
|
|
524
371
|
//#endregion
|
|
525
|
-
//#region impl/utils/
|
|
526
|
-
const
|
|
527
|
-
const
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
info: (...args) => console.info(...args),
|
|
531
|
-
debug: (...args) => console.debug(...args)
|
|
532
|
-
});
|
|
533
|
-
const createNoopLogger = () => ({
|
|
534
|
-
error: noop,
|
|
535
|
-
warn: noop,
|
|
536
|
-
info: noop,
|
|
537
|
-
debug: noop
|
|
538
|
-
});
|
|
539
|
-
const bindLoggerMethod = (logger, level) => {
|
|
540
|
-
const method = logger[level];
|
|
541
|
-
return typeof method === "function" ? method.bind(logger) : noop;
|
|
372
|
+
//#region impl/utils/fetch.mts
|
|
373
|
+
const JSON_HEADERS = { "Content-Type": "application/json" };
|
|
374
|
+
const hasHeader = (headers, name) => {
|
|
375
|
+
const expected = name.toLowerCase();
|
|
376
|
+
return Object.keys(headers).some((header) => header.toLowerCase() === expected);
|
|
542
377
|
};
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
378
|
+
const toBasicAuthHeader = ({ username, password }) => {
|
|
379
|
+
return `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`;
|
|
380
|
+
};
|
|
381
|
+
const prepareRequest = (options) => {
|
|
382
|
+
const auth = options.auth;
|
|
383
|
+
const headers = { ...options.headers ?? {} };
|
|
384
|
+
if (auth && !hasHeader(headers, "Authorization")) headers.Authorization = toBasicAuthHeader(auth);
|
|
385
|
+
return { headers };
|
|
386
|
+
};
|
|
387
|
+
const isAbortError = (err) => {
|
|
388
|
+
return err instanceof DOMException && err.name === "AbortError";
|
|
389
|
+
};
|
|
390
|
+
const isTimeoutError = (err) => {
|
|
391
|
+
return err instanceof DOMException && err.name === "TimeoutError";
|
|
392
|
+
};
|
|
393
|
+
const encodeBody = (body) => {
|
|
394
|
+
if (body == null) return void 0;
|
|
395
|
+
if (typeof body === "string" || body instanceof ArrayBuffer || ArrayBuffer.isView(body) || body instanceof Blob || body instanceof FormData || body instanceof URLSearchParams || body instanceof ReadableStream) return body;
|
|
396
|
+
return JSON.stringify(body);
|
|
397
|
+
};
|
|
398
|
+
const parseJsonResponse = async (response) => {
|
|
399
|
+
if (response.status === 204 || response.status === 205) return null;
|
|
400
|
+
const text = await response.text();
|
|
401
|
+
if (text.trim() === "") return null;
|
|
402
|
+
try {
|
|
403
|
+
return JSON.parse(text);
|
|
404
|
+
} catch (err) {
|
|
405
|
+
if (response.ok) throw err;
|
|
406
|
+
return text;
|
|
560
407
|
}
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
408
|
+
};
|
|
409
|
+
async function fetchCouchJson(options) {
|
|
410
|
+
let response;
|
|
411
|
+
const { headers } = prepareRequest(options);
|
|
412
|
+
const { signal, timedOut } = composeAbortSignal(options.signal, options.request);
|
|
413
|
+
try {
|
|
414
|
+
response = await fetch(options.url, {
|
|
415
|
+
method: options.method,
|
|
416
|
+
headers: {
|
|
417
|
+
...JSON_HEADERS,
|
|
418
|
+
...headers
|
|
419
|
+
},
|
|
420
|
+
body: encodeBody(options.body),
|
|
421
|
+
signal,
|
|
422
|
+
dispatcher: options.request?.dispatcher
|
|
423
|
+
});
|
|
424
|
+
} catch (err) {
|
|
425
|
+
if (timedOut() || isTimeoutError(err)) throw new RetryableError("Request timed out", 503, {
|
|
426
|
+
category: "network",
|
|
427
|
+
cause: err,
|
|
428
|
+
operation: options.operation
|
|
429
|
+
});
|
|
430
|
+
if (isAbortError(err)) throw err;
|
|
431
|
+
RetryableError.handleNetworkError(err, options.operation);
|
|
432
|
+
}
|
|
433
|
+
return {
|
|
434
|
+
body: await parseJsonResponse(response),
|
|
435
|
+
headers: response.headers,
|
|
436
|
+
statusCode: response.status
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
async function fetchCouchStream(options) {
|
|
440
|
+
let response;
|
|
441
|
+
const { headers } = prepareRequest(options);
|
|
442
|
+
const { signal, timedOut } = composeAbortSignal(options.signal, options.request);
|
|
443
|
+
try {
|
|
444
|
+
response = await fetch(options.url, {
|
|
445
|
+
method: options.method,
|
|
446
|
+
headers,
|
|
447
|
+
body: encodeBody(options.body),
|
|
448
|
+
signal,
|
|
449
|
+
dispatcher: options.request?.dispatcher
|
|
450
|
+
});
|
|
451
|
+
} catch (err) {
|
|
452
|
+
if (timedOut() || isTimeoutError(err)) throw new RetryableError("Request timed out", 503, {
|
|
453
|
+
category: "network",
|
|
454
|
+
cause: err,
|
|
455
|
+
operation: options.operation
|
|
456
|
+
});
|
|
457
|
+
if (isAbortError(err)) throw err;
|
|
458
|
+
RetryableError.handleNetworkError(err, options.operation);
|
|
459
|
+
}
|
|
460
|
+
return {
|
|
461
|
+
body: response.body,
|
|
462
|
+
headers: response.headers,
|
|
463
|
+
statusCode: response.status
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
//#endregion
|
|
468
|
+
//#region impl/utils/url.mts
|
|
469
|
+
const ensureDirectoryUrl = (value) => {
|
|
470
|
+
const url = new URL(value);
|
|
471
|
+
if (!url.pathname.endsWith("/")) url.pathname = `${url.pathname}/`;
|
|
472
|
+
return url;
|
|
473
|
+
};
|
|
474
|
+
const createCouchDbUrl = (value) => {
|
|
475
|
+
return new URL(value);
|
|
476
|
+
};
|
|
477
|
+
const createCouchPathUrl = (path, base) => {
|
|
478
|
+
return new URL(path, ensureDirectoryUrl(base));
|
|
479
|
+
};
|
|
480
|
+
const createCouchDocUrl = (docId, base) => {
|
|
481
|
+
return new URL(encodeURIComponent(docId), ensureDirectoryUrl(base));
|
|
482
|
+
};
|
|
483
|
+
|
|
484
|
+
//#endregion
|
|
485
|
+
//#region schema/couch/mango.schema.ts
|
|
486
|
+
/** Selector operators are interpreted by CouchDB, including nested selectors. */
|
|
487
|
+
const MangoSelectorSchema = z$1.record(z$1.string(), z$1.unknown());
|
|
488
|
+
const MangoSortSchema = z$1.array(z$1.union([z$1.string(), z$1.record(z$1.string(), z$1.enum(["asc", "desc"]))]));
|
|
489
|
+
/** Native CouchDB _find options; supported fields depend on the server version. */
|
|
490
|
+
const MangoQuerySchema = z$1.object({
|
|
491
|
+
selector: MangoSelectorSchema,
|
|
492
|
+
limit: z$1.number().int().positive().optional(),
|
|
493
|
+
skip: z$1.number().int().nonnegative().optional(),
|
|
494
|
+
sort: MangoSortSchema.optional(),
|
|
495
|
+
fields: z$1.array(z$1.string()).optional(),
|
|
496
|
+
use_index: z$1.union([z$1.string(), z$1.tuple([z$1.string(), z$1.string()])]).optional(),
|
|
497
|
+
conflicts: z$1.boolean().optional(),
|
|
498
|
+
r: z$1.number().int().positive().optional(),
|
|
499
|
+
bookmark: z$1.string().optional(),
|
|
500
|
+
update: z$1.boolean().optional(),
|
|
501
|
+
stable: z$1.boolean().optional(),
|
|
502
|
+
stale: z$1.union([z$1.literal("ok"), z$1.literal(false)]).optional().describe("Deprecated CouchDB option; prefer stable and update"),
|
|
503
|
+
execution_stats: z$1.boolean().optional(),
|
|
504
|
+
allow_fallback: z$1.boolean().optional()
|
|
505
|
+
}).strict();
|
|
506
|
+
const MangoExecutionStatsSchema = z$1.looseObject({
|
|
507
|
+
total_keys_examined: z$1.number(),
|
|
508
|
+
total_docs_examined: z$1.number(),
|
|
509
|
+
total_quorum_docs_examined: z$1.number(),
|
|
510
|
+
results_returned: z$1.number(),
|
|
511
|
+
execution_time_ms: z$1.number()
|
|
512
|
+
});
|
|
513
|
+
/** Opt into document validation by supplying the schema for your projection. */
|
|
514
|
+
const createMangoResponseSchema = (docSchema) => z$1.looseObject({
|
|
515
|
+
docs: z$1.array(docSchema),
|
|
516
|
+
bookmark: z$1.string().optional(),
|
|
517
|
+
warning: z$1.string().optional(),
|
|
518
|
+
execution_stats: MangoExecutionStatsSchema.optional()
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
//#endregion
|
|
522
|
+
//#region impl/find.mts
|
|
523
|
+
const MangoResponseSchema = createMangoResponseSchema(z$1.record(z$1.string(), z$1.unknown()));
|
|
524
|
+
/** Query a Mango JSON index. Request controls come from config, as with query(). */
|
|
525
|
+
async function find(configInput, options) {
|
|
526
|
+
const config = CouchConfig.parse(configInput);
|
|
527
|
+
const body = MangoQuerySchema.parse(options);
|
|
528
|
+
let response;
|
|
529
|
+
try {
|
|
530
|
+
response = await fetchCouchJson({
|
|
531
|
+
auth: config.auth,
|
|
532
|
+
request: config.request,
|
|
533
|
+
method: "POST",
|
|
534
|
+
operation: "find",
|
|
535
|
+
url: createCouchPathUrl("_find", config.couch),
|
|
536
|
+
body
|
|
537
|
+
});
|
|
538
|
+
} catch (error) {
|
|
539
|
+
RetryableError.handleNetworkError(error, "find");
|
|
540
|
+
}
|
|
541
|
+
if (response.statusCode !== 200 || response.body?.error) throw createResponseError({
|
|
542
|
+
body: response.body,
|
|
543
|
+
defaultMessage: "Mango query failed",
|
|
544
|
+
operation: "find",
|
|
545
|
+
statusCode: response.statusCode
|
|
546
|
+
});
|
|
547
|
+
const parsed = MangoResponseSchema.safeParse(response.body);
|
|
548
|
+
if (!parsed.success) throw new OperationError("Invalid Mango response", { operation: "find" });
|
|
549
|
+
return parsed.data;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
//#endregion
|
|
553
|
+
//#region impl/utils/queryBuilder.mts
|
|
554
|
+
/**
|
|
555
|
+
* A builder class for constructing CouchDB view query options.
|
|
556
|
+
* Provides a fluent API for setting various query parameters.
|
|
557
|
+
* @example
|
|
558
|
+
* const queryOptions = new QueryBuilder()
|
|
559
|
+
* .limit(10)
|
|
560
|
+
* .include_docs()
|
|
561
|
+
* .startKey('someKey')
|
|
562
|
+
* .build();
|
|
563
|
+
* @see SimpleViewOptions for the full list of options.
|
|
564
|
+
*
|
|
565
|
+
* @remarks
|
|
566
|
+
* Each method corresponds to a CouchDB view option and returns the builder instance for chaining.
|
|
567
|
+
*
|
|
568
|
+
* @returns The constructed SimpleViewOptions object.
|
|
569
|
+
*/
|
|
570
|
+
var QueryBuilder = class {
|
|
571
|
+
#options = {};
|
|
572
|
+
descending(descending = true) {
|
|
573
|
+
this.#options.descending = descending;
|
|
574
|
+
return this;
|
|
575
|
+
}
|
|
576
|
+
endkey_docid(endkeyDocId) {
|
|
577
|
+
this.#options.endkey_docid = endkeyDocId;
|
|
578
|
+
return this;
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* Alias for endkey_docid
|
|
582
|
+
*/
|
|
583
|
+
end_key_doc_id(endkeyDocId) {
|
|
584
|
+
this.#options.endkey_docid = endkeyDocId;
|
|
585
|
+
return this;
|
|
586
|
+
}
|
|
587
|
+
endkey(endkey) {
|
|
588
|
+
this.#options.endkey = endkey;
|
|
589
|
+
return this;
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* Alias for endkey
|
|
593
|
+
*/
|
|
594
|
+
endKey(endkey) {
|
|
595
|
+
this.#options.endkey = endkey;
|
|
596
|
+
return this;
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* Alias for endkey
|
|
600
|
+
*/
|
|
601
|
+
end_key(endkey) {
|
|
602
|
+
this.#options.endkey = endkey;
|
|
603
|
+
return this;
|
|
604
|
+
}
|
|
605
|
+
group(group = true) {
|
|
606
|
+
this.#options.group = group;
|
|
607
|
+
return this;
|
|
608
|
+
}
|
|
609
|
+
group_level(level) {
|
|
610
|
+
this.#options.group_level = level;
|
|
611
|
+
return this;
|
|
612
|
+
}
|
|
613
|
+
include_docs(includeDocs = true) {
|
|
614
|
+
this.#options.include_docs = includeDocs;
|
|
615
|
+
return this;
|
|
616
|
+
}
|
|
617
|
+
inclusive_end(inclusiveEnd = true) {
|
|
618
|
+
this.#options.inclusive_end = inclusiveEnd;
|
|
619
|
+
return this;
|
|
620
|
+
}
|
|
621
|
+
key(key) {
|
|
622
|
+
this.#options.key = key;
|
|
623
|
+
return this;
|
|
624
|
+
}
|
|
625
|
+
keys(keys) {
|
|
626
|
+
this.#options.keys = keys;
|
|
627
|
+
return this;
|
|
628
|
+
}
|
|
629
|
+
limit(limit) {
|
|
630
|
+
this.#options.limit = limit;
|
|
631
|
+
return this;
|
|
632
|
+
}
|
|
633
|
+
reduce(reduce = true) {
|
|
634
|
+
this.#options.reduce = reduce;
|
|
635
|
+
return this;
|
|
636
|
+
}
|
|
637
|
+
skip(skip) {
|
|
638
|
+
this.#options.skip = skip;
|
|
639
|
+
return this;
|
|
640
|
+
}
|
|
641
|
+
sorted(sorted = true) {
|
|
642
|
+
this.#options.sorted = sorted;
|
|
643
|
+
return this;
|
|
644
|
+
}
|
|
645
|
+
stable(stable = true) {
|
|
646
|
+
this.#options.stable = stable;
|
|
647
|
+
return this;
|
|
648
|
+
}
|
|
649
|
+
startkey(startkey) {
|
|
650
|
+
this.#options.startkey = startkey;
|
|
651
|
+
return this;
|
|
652
|
+
}
|
|
653
|
+
/**
|
|
654
|
+
* Alias for startkey
|
|
655
|
+
*/
|
|
656
|
+
startKey(startkey) {
|
|
657
|
+
this.#options.startkey = startkey;
|
|
658
|
+
return this;
|
|
659
|
+
}
|
|
660
|
+
/**
|
|
661
|
+
* Alias for startkey
|
|
662
|
+
*/
|
|
663
|
+
start_key(startkey) {
|
|
664
|
+
this.#options.startkey = startkey;
|
|
665
|
+
return this;
|
|
666
|
+
}
|
|
667
|
+
startkey_docid(startkeyDocId) {
|
|
668
|
+
this.#options.startkey_docid = startkeyDocId;
|
|
669
|
+
return this;
|
|
670
|
+
}
|
|
671
|
+
/**
|
|
672
|
+
* Alias for startkey_docid
|
|
673
|
+
*/
|
|
674
|
+
start_key_doc_id(startkeyDocId) {
|
|
675
|
+
this.#options.startkey_docid = startkeyDocId;
|
|
676
|
+
return this;
|
|
677
|
+
}
|
|
678
|
+
update(update) {
|
|
679
|
+
this.#options.update = update;
|
|
680
|
+
return this;
|
|
681
|
+
}
|
|
682
|
+
update_seq(updateSeq = true) {
|
|
683
|
+
this.#options.update_seq = updateSeq;
|
|
684
|
+
return this;
|
|
685
|
+
}
|
|
686
|
+
/**
|
|
687
|
+
* Builds and returns the ViewOptions object.
|
|
688
|
+
*/
|
|
689
|
+
build() {
|
|
690
|
+
return { ...this.#options };
|
|
691
|
+
}
|
|
692
|
+
};
|
|
693
|
+
const createQuery = () => new QueryBuilder();
|
|
694
|
+
|
|
695
|
+
//#endregion
|
|
696
|
+
//#region impl/retry.mts
|
|
697
|
+
const shouldRetryError = (error, attempt) => {
|
|
698
|
+
return error instanceof RetryableError || isTransientAuthError(error, attempt);
|
|
699
|
+
};
|
|
700
|
+
/**
|
|
701
|
+
* Wrap an async-capable function with retry semantics that respect {@link RetryableError}.
|
|
702
|
+
* @typeParam Fn - The function signature to decorate with retry handling.
|
|
703
|
+
* @param fn The function to invoke with retry support.
|
|
704
|
+
* @param options Retry tuning parameters.
|
|
705
|
+
* @returns A function mirroring `fn` that automatically retries on {@link RetryableError}.
|
|
706
|
+
*/
|
|
707
|
+
function withRetry(fn, options = {}) {
|
|
708
|
+
const { maxRetries = 3, initialDelay = 1e3, backoffFactor = 2, maxDelay = 3e4 } = options;
|
|
709
|
+
return async (...args) => {
|
|
710
|
+
let delay = initialDelay;
|
|
711
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) try {
|
|
712
|
+
return await fn(...args);
|
|
713
|
+
} catch (error) {
|
|
714
|
+
if (!shouldRetryError(error, attempt)) throw error;
|
|
715
|
+
if (attempt === maxRetries) throw error;
|
|
716
|
+
await setTimeout$1(Math.min(delay, maxDelay));
|
|
717
|
+
delay *= backoffFactor;
|
|
718
|
+
}
|
|
719
|
+
throw new RetryableError("withRetry exhausted retry attempts without resolving the operation");
|
|
720
|
+
};
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
//#endregion
|
|
724
|
+
//#region impl/utils/logger.mts
|
|
725
|
+
const noop = () => {};
|
|
726
|
+
const createConsoleLogger = () => ({
|
|
727
|
+
error: (...args) => console.error(...args),
|
|
728
|
+
warn: (...args) => console.warn(...args),
|
|
729
|
+
info: (...args) => console.info(...args),
|
|
730
|
+
debug: (...args) => console.debug(...args)
|
|
731
|
+
});
|
|
732
|
+
const createNoopLogger = () => ({
|
|
733
|
+
error: noop,
|
|
734
|
+
warn: noop,
|
|
735
|
+
info: noop,
|
|
736
|
+
debug: noop
|
|
737
|
+
});
|
|
738
|
+
const bindLoggerMethod = (logger, level) => {
|
|
739
|
+
const method = logger[level];
|
|
740
|
+
return typeof method === "function" ? method.bind(logger) : noop;
|
|
741
|
+
};
|
|
742
|
+
function createLogger(config) {
|
|
743
|
+
if (config["~normalizedLogger"]) return config["~normalizedLogger"];
|
|
744
|
+
if (!config.logger) {
|
|
745
|
+
const normalized$1 = config.useConsoleLogger ? createConsoleLogger() : createNoopLogger();
|
|
746
|
+
config["~normalizedLogger"] = normalized$1;
|
|
747
|
+
return normalized$1;
|
|
748
|
+
}
|
|
749
|
+
if (typeof config.logger === "function") {
|
|
750
|
+
const loggerFn = config.logger;
|
|
751
|
+
const normalized$1 = {
|
|
752
|
+
error: (...args) => loggerFn("error", ...args),
|
|
753
|
+
warn: (...args) => loggerFn("warn", ...args),
|
|
754
|
+
info: (...args) => loggerFn("info", ...args),
|
|
755
|
+
debug: (...args) => loggerFn("debug", ...args)
|
|
756
|
+
};
|
|
757
|
+
config["~normalizedLogger"] = normalized$1;
|
|
758
|
+
return normalized$1;
|
|
759
|
+
}
|
|
760
|
+
const loggerObj = config.logger;
|
|
761
|
+
const normalized = {
|
|
762
|
+
error: bindLoggerMethod(loggerObj, "error"),
|
|
763
|
+
warn: bindLoggerMethod(loggerObj, "warn"),
|
|
764
|
+
info: bindLoggerMethod(loggerObj, "info"),
|
|
765
|
+
debug: bindLoggerMethod(loggerObj, "debug")
|
|
766
|
+
};
|
|
568
767
|
config["~normalizedLogger"] = normalized;
|
|
569
768
|
return normalized;
|
|
570
769
|
}
|
|
@@ -726,137 +925,6 @@ async function parseRows(rows, options) {
|
|
|
726
925
|
}))).filter(isFinalRow);
|
|
727
926
|
}
|
|
728
927
|
|
|
729
|
-
//#endregion
|
|
730
|
-
//#region impl/utils/request.mts
|
|
731
|
-
const definedSignals = (signals) => {
|
|
732
|
-
return signals.filter((signal) => signal != null);
|
|
733
|
-
};
|
|
734
|
-
const composeAbortSignal = (internalSignal, request) => {
|
|
735
|
-
const timeoutSignal = typeof request?.timeout === "number" ? AbortSignal.timeout(request.timeout) : void 0;
|
|
736
|
-
const signals = definedSignals([
|
|
737
|
-
internalSignal,
|
|
738
|
-
request?.signal,
|
|
739
|
-
timeoutSignal
|
|
740
|
-
]);
|
|
741
|
-
return {
|
|
742
|
-
signal: signals.length > 1 ? AbortSignal.any(signals) : signals[0],
|
|
743
|
-
timedOut: () => timeoutSignal?.aborted === true
|
|
744
|
-
};
|
|
745
|
-
};
|
|
746
|
-
|
|
747
|
-
//#endregion
|
|
748
|
-
//#region impl/utils/fetch.mts
|
|
749
|
-
const JSON_HEADERS = { "Content-Type": "application/json" };
|
|
750
|
-
const hasHeader = (headers, name) => {
|
|
751
|
-
const expected = name.toLowerCase();
|
|
752
|
-
return Object.keys(headers).some((header) => header.toLowerCase() === expected);
|
|
753
|
-
};
|
|
754
|
-
const toBasicAuthHeader = ({ username, password }) => {
|
|
755
|
-
return `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`;
|
|
756
|
-
};
|
|
757
|
-
const prepareRequest = (options) => {
|
|
758
|
-
const auth = options.auth;
|
|
759
|
-
const headers = { ...options.headers ?? {} };
|
|
760
|
-
if (auth && !hasHeader(headers, "Authorization")) headers.Authorization = toBasicAuthHeader(auth);
|
|
761
|
-
return { headers };
|
|
762
|
-
};
|
|
763
|
-
const isAbortError = (err) => {
|
|
764
|
-
return err instanceof DOMException && err.name === "AbortError";
|
|
765
|
-
};
|
|
766
|
-
const isTimeoutError = (err) => {
|
|
767
|
-
return err instanceof DOMException && err.name === "TimeoutError";
|
|
768
|
-
};
|
|
769
|
-
const encodeBody = (body) => {
|
|
770
|
-
if (body == null) return void 0;
|
|
771
|
-
if (typeof body === "string" || body instanceof ArrayBuffer || ArrayBuffer.isView(body) || body instanceof Blob || body instanceof FormData || body instanceof URLSearchParams || body instanceof ReadableStream) return body;
|
|
772
|
-
return JSON.stringify(body);
|
|
773
|
-
};
|
|
774
|
-
const parseJsonResponse = async (response) => {
|
|
775
|
-
if (response.status === 204 || response.status === 205) return null;
|
|
776
|
-
const text = await response.text();
|
|
777
|
-
if (text.trim() === "") return null;
|
|
778
|
-
try {
|
|
779
|
-
return JSON.parse(text);
|
|
780
|
-
} catch (err) {
|
|
781
|
-
if (response.ok) throw err;
|
|
782
|
-
return text;
|
|
783
|
-
}
|
|
784
|
-
};
|
|
785
|
-
async function fetchCouchJson(options) {
|
|
786
|
-
let response;
|
|
787
|
-
const { headers } = prepareRequest(options);
|
|
788
|
-
const { signal, timedOut } = composeAbortSignal(options.signal, options.request);
|
|
789
|
-
try {
|
|
790
|
-
response = await fetch(options.url, {
|
|
791
|
-
method: options.method,
|
|
792
|
-
headers: {
|
|
793
|
-
...JSON_HEADERS,
|
|
794
|
-
...headers
|
|
795
|
-
},
|
|
796
|
-
body: encodeBody(options.body),
|
|
797
|
-
signal,
|
|
798
|
-
dispatcher: options.request?.dispatcher
|
|
799
|
-
});
|
|
800
|
-
} catch (err) {
|
|
801
|
-
if (timedOut() || isTimeoutError(err)) throw new RetryableError("Request timed out", 503, {
|
|
802
|
-
category: "network",
|
|
803
|
-
cause: err,
|
|
804
|
-
operation: options.operation
|
|
805
|
-
});
|
|
806
|
-
if (isAbortError(err)) throw err;
|
|
807
|
-
RetryableError.handleNetworkError(err, options.operation);
|
|
808
|
-
}
|
|
809
|
-
return {
|
|
810
|
-
body: await parseJsonResponse(response),
|
|
811
|
-
headers: response.headers,
|
|
812
|
-
statusCode: response.status
|
|
813
|
-
};
|
|
814
|
-
}
|
|
815
|
-
async function fetchCouchStream(options) {
|
|
816
|
-
let response;
|
|
817
|
-
const { headers } = prepareRequest(options);
|
|
818
|
-
const { signal, timedOut } = composeAbortSignal(options.signal, options.request);
|
|
819
|
-
try {
|
|
820
|
-
response = await fetch(options.url, {
|
|
821
|
-
method: options.method,
|
|
822
|
-
headers,
|
|
823
|
-
body: encodeBody(options.body),
|
|
824
|
-
signal,
|
|
825
|
-
dispatcher: options.request?.dispatcher
|
|
826
|
-
});
|
|
827
|
-
} catch (err) {
|
|
828
|
-
if (timedOut() || isTimeoutError(err)) throw new RetryableError("Request timed out", 503, {
|
|
829
|
-
category: "network",
|
|
830
|
-
cause: err,
|
|
831
|
-
operation: options.operation
|
|
832
|
-
});
|
|
833
|
-
if (isAbortError(err)) throw err;
|
|
834
|
-
RetryableError.handleNetworkError(err, options.operation);
|
|
835
|
-
}
|
|
836
|
-
return {
|
|
837
|
-
body: response.body,
|
|
838
|
-
headers: response.headers,
|
|
839
|
-
statusCode: response.status
|
|
840
|
-
};
|
|
841
|
-
}
|
|
842
|
-
|
|
843
|
-
//#endregion
|
|
844
|
-
//#region impl/utils/url.mts
|
|
845
|
-
const ensureDirectoryUrl = (value) => {
|
|
846
|
-
const url = new URL(value);
|
|
847
|
-
if (!url.pathname.endsWith("/")) url.pathname = `${url.pathname}/`;
|
|
848
|
-
return url;
|
|
849
|
-
};
|
|
850
|
-
const createCouchDbUrl = (value) => {
|
|
851
|
-
return new URL(value);
|
|
852
|
-
};
|
|
853
|
-
const createCouchPathUrl = (path, base) => {
|
|
854
|
-
return new URL(path, ensureDirectoryUrl(base));
|
|
855
|
-
};
|
|
856
|
-
const createCouchDocUrl = (docId, base) => {
|
|
857
|
-
return new URL(encodeURIComponent(docId), ensureDirectoryUrl(base));
|
|
858
|
-
};
|
|
859
|
-
|
|
860
928
|
//#endregion
|
|
861
929
|
//#region impl/bulkGet.mts
|
|
862
930
|
/**
|
|
@@ -2407,6 +2475,7 @@ function doBind(config) {
|
|
|
2407
2475
|
bulkGetDictionary: getBoundWithRetry(bulkGetDictionary, config),
|
|
2408
2476
|
get: getBoundWithRetry(get, config),
|
|
2409
2477
|
getAtRev: getBoundWithRetry(getAtRev, config),
|
|
2478
|
+
find: getBoundWithRetry(find, config),
|
|
2410
2479
|
query: getBoundWithRetry(query, config),
|
|
2411
2480
|
bulkRemove: config.bindWithRetry ? withRetry(bulkRemove.bind(null, config), retryOptions) : bulkRemove.bind(null, config),
|
|
2412
2481
|
bulkRemoveMap: config.bindWithRetry ? withRetry(bulkRemoveMap.bind(null, config), retryOptions) : bulkRemoveMap.bind(null, config),
|
|
@@ -2426,4 +2495,4 @@ function doBind(config) {
|
|
|
2426
2495
|
}
|
|
2427
2496
|
|
|
2428
2497
|
//#endregion
|
|
2429
|
-
export { ConflictError, HideABedError, NotFoundError, OperationError, QueryBuilder, RetryableError, ValidationError, bindConfig, bulkGet, bulkGetDictionary, bulkRemove, bulkRemoveMap, bulkSave, bulkSaveTransaction, createLock, createQuery, get, getAtRev, getDBInfo, headDB, patch, patchDangerously, put, query, queryStream, remove, removeLock, watchDocs, withRetry };
|
|
2498
|
+
export { ConflictError, HideABedError, MangoExecutionStatsSchema, MangoQuerySchema, MangoSelectorSchema, MangoSortSchema, NotFoundError, OperationError, QueryBuilder, RetryableError, ValidationError, bindConfig, bulkGet, bulkGetDictionary, bulkRemove, bulkRemoveMap, bulkSave, bulkSaveTransaction, createLock, createMangoResponseSchema, createQuery, find, get, getAtRev, getDBInfo, headDB, patch, patchDangerously, put, query, queryStream, remove, removeLock, watchDocs, withRetry };
|