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/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;
|
|
@@ -166,28 +23,21 @@ const RequestOptions = z$1.strictObject({
|
|
|
166
23
|
|
|
167
24
|
//#endregion
|
|
168
25
|
//#region schema/config.mts
|
|
169
|
-
const
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
})
|
|
183
|
-
|
|
184
|
-
input: anyArgs,
|
|
185
|
-
output: z$1.void()
|
|
186
|
-
}).optional()
|
|
187
|
-
}).or(z$1.function({
|
|
188
|
-
input: anyArgs,
|
|
189
|
-
output: z$1.void()
|
|
190
|
-
}));
|
|
26
|
+
const loggerLevels = [
|
|
27
|
+
"error",
|
|
28
|
+
"warn",
|
|
29
|
+
"info",
|
|
30
|
+
"debug"
|
|
31
|
+
];
|
|
32
|
+
const isLoggerMethod = (value) => typeof value === "function";
|
|
33
|
+
const LoggerSchema = z$1.custom((value) => {
|
|
34
|
+
if (isLoggerMethod(value)) return true;
|
|
35
|
+
if (value === null || typeof value !== "object") return false;
|
|
36
|
+
return loggerLevels.every((level) => {
|
|
37
|
+
const method = value[level];
|
|
38
|
+
return method === void 0 || isLoggerMethod(method);
|
|
39
|
+
});
|
|
40
|
+
}, { message: "logger must be a function or object with optional error, warn, info, and debug methods" });
|
|
191
41
|
const CouchAuth = z$1.strictObject({
|
|
192
42
|
username: z$1.string().describe("basic auth username for CouchDB requests"),
|
|
193
43
|
password: z$1.string().describe("basic auth password for CouchDB requests")
|
|
@@ -501,72 +351,414 @@ function isConflictError(err) {
|
|
|
501
351
|
}
|
|
502
352
|
|
|
503
353
|
//#endregion
|
|
504
|
-
//#region impl/
|
|
505
|
-
const
|
|
506
|
-
return
|
|
354
|
+
//#region impl/utils/request.mts
|
|
355
|
+
const definedSignals = (signals) => {
|
|
356
|
+
return signals.filter((signal) => signal != null);
|
|
507
357
|
};
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
let delay = initialDelay;
|
|
519
|
-
for (let attempt = 0; attempt <= maxRetries; attempt++) try {
|
|
520
|
-
return await fn(...args);
|
|
521
|
-
} catch (error) {
|
|
522
|
-
if (!shouldRetryError(error, attempt)) throw error;
|
|
523
|
-
if (attempt === maxRetries) throw error;
|
|
524
|
-
await setTimeout$1(Math.min(delay, maxDelay));
|
|
525
|
-
delay *= backoffFactor;
|
|
526
|
-
}
|
|
527
|
-
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
|
|
528
368
|
};
|
|
529
|
-
}
|
|
369
|
+
};
|
|
530
370
|
|
|
531
371
|
//#endregion
|
|
532
|
-
//#region impl/utils/
|
|
533
|
-
const
|
|
534
|
-
const
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
info: (...args) => console.info(...args),
|
|
538
|
-
debug: (...args) => console.debug(...args)
|
|
539
|
-
});
|
|
540
|
-
const createNoopLogger = () => ({
|
|
541
|
-
error: noop,
|
|
542
|
-
warn: noop,
|
|
543
|
-
info: noop,
|
|
544
|
-
debug: noop
|
|
545
|
-
});
|
|
546
|
-
const bindLoggerMethod = (logger, level) => {
|
|
547
|
-
const method = logger[level];
|
|
548
|
-
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);
|
|
549
377
|
};
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
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;
|
|
567
407
|
}
|
|
568
|
-
|
|
569
|
-
|
|
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 = {
|
|
570
762
|
error: bindLoggerMethod(loggerObj, "error"),
|
|
571
763
|
warn: bindLoggerMethod(loggerObj, "warn"),
|
|
572
764
|
info: bindLoggerMethod(loggerObj, "info"),
|
|
@@ -733,137 +925,6 @@ async function parseRows(rows, options) {
|
|
|
733
925
|
}))).filter(isFinalRow);
|
|
734
926
|
}
|
|
735
927
|
|
|
736
|
-
//#endregion
|
|
737
|
-
//#region impl/utils/request.mts
|
|
738
|
-
const definedSignals = (signals) => {
|
|
739
|
-
return signals.filter((signal) => signal != null);
|
|
740
|
-
};
|
|
741
|
-
const composeAbortSignal = (internalSignal, request) => {
|
|
742
|
-
const timeoutSignal = typeof request?.timeout === "number" ? AbortSignal.timeout(request.timeout) : void 0;
|
|
743
|
-
const signals = definedSignals([
|
|
744
|
-
internalSignal,
|
|
745
|
-
request?.signal,
|
|
746
|
-
timeoutSignal
|
|
747
|
-
]);
|
|
748
|
-
return {
|
|
749
|
-
signal: signals.length > 1 ? AbortSignal.any(signals) : signals[0],
|
|
750
|
-
timedOut: () => timeoutSignal?.aborted === true
|
|
751
|
-
};
|
|
752
|
-
};
|
|
753
|
-
|
|
754
|
-
//#endregion
|
|
755
|
-
//#region impl/utils/fetch.mts
|
|
756
|
-
const JSON_HEADERS = { "Content-Type": "application/json" };
|
|
757
|
-
const hasHeader = (headers, name) => {
|
|
758
|
-
const expected = name.toLowerCase();
|
|
759
|
-
return Object.keys(headers).some((header) => header.toLowerCase() === expected);
|
|
760
|
-
};
|
|
761
|
-
const toBasicAuthHeader = ({ username, password }) => {
|
|
762
|
-
return `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`;
|
|
763
|
-
};
|
|
764
|
-
const prepareRequest = (options) => {
|
|
765
|
-
const auth = options.auth;
|
|
766
|
-
const headers = { ...options.headers ?? {} };
|
|
767
|
-
if (auth && !hasHeader(headers, "Authorization")) headers.Authorization = toBasicAuthHeader(auth);
|
|
768
|
-
return { headers };
|
|
769
|
-
};
|
|
770
|
-
const isAbortError = (err) => {
|
|
771
|
-
return err instanceof DOMException && err.name === "AbortError";
|
|
772
|
-
};
|
|
773
|
-
const isTimeoutError = (err) => {
|
|
774
|
-
return err instanceof DOMException && err.name === "TimeoutError";
|
|
775
|
-
};
|
|
776
|
-
const encodeBody = (body) => {
|
|
777
|
-
if (body == null) return void 0;
|
|
778
|
-
if (typeof body === "string" || body instanceof ArrayBuffer || ArrayBuffer.isView(body) || body instanceof Blob || body instanceof FormData || body instanceof URLSearchParams || body instanceof ReadableStream) return body;
|
|
779
|
-
return JSON.stringify(body);
|
|
780
|
-
};
|
|
781
|
-
const parseJsonResponse = async (response) => {
|
|
782
|
-
if (response.status === 204 || response.status === 205) return null;
|
|
783
|
-
const text = await response.text();
|
|
784
|
-
if (text.trim() === "") return null;
|
|
785
|
-
try {
|
|
786
|
-
return JSON.parse(text);
|
|
787
|
-
} catch (err) {
|
|
788
|
-
if (response.ok) throw err;
|
|
789
|
-
return text;
|
|
790
|
-
}
|
|
791
|
-
};
|
|
792
|
-
async function fetchCouchJson(options) {
|
|
793
|
-
let response;
|
|
794
|
-
const { headers } = prepareRequest(options);
|
|
795
|
-
const { signal, timedOut } = composeAbortSignal(options.signal, options.request);
|
|
796
|
-
try {
|
|
797
|
-
response = await fetch(options.url, {
|
|
798
|
-
method: options.method,
|
|
799
|
-
headers: {
|
|
800
|
-
...JSON_HEADERS,
|
|
801
|
-
...headers
|
|
802
|
-
},
|
|
803
|
-
body: encodeBody(options.body),
|
|
804
|
-
signal,
|
|
805
|
-
dispatcher: options.request?.dispatcher
|
|
806
|
-
});
|
|
807
|
-
} catch (err) {
|
|
808
|
-
if (timedOut() || isTimeoutError(err)) throw new RetryableError("Request timed out", 503, {
|
|
809
|
-
category: "network",
|
|
810
|
-
cause: err,
|
|
811
|
-
operation: options.operation
|
|
812
|
-
});
|
|
813
|
-
if (isAbortError(err)) throw err;
|
|
814
|
-
RetryableError.handleNetworkError(err, options.operation);
|
|
815
|
-
}
|
|
816
|
-
return {
|
|
817
|
-
body: await parseJsonResponse(response),
|
|
818
|
-
headers: response.headers,
|
|
819
|
-
statusCode: response.status
|
|
820
|
-
};
|
|
821
|
-
}
|
|
822
|
-
async function fetchCouchStream(options) {
|
|
823
|
-
let response;
|
|
824
|
-
const { headers } = prepareRequest(options);
|
|
825
|
-
const { signal, timedOut } = composeAbortSignal(options.signal, options.request);
|
|
826
|
-
try {
|
|
827
|
-
response = await fetch(options.url, {
|
|
828
|
-
method: options.method,
|
|
829
|
-
headers,
|
|
830
|
-
body: encodeBody(options.body),
|
|
831
|
-
signal,
|
|
832
|
-
dispatcher: options.request?.dispatcher
|
|
833
|
-
});
|
|
834
|
-
} catch (err) {
|
|
835
|
-
if (timedOut() || isTimeoutError(err)) throw new RetryableError("Request timed out", 503, {
|
|
836
|
-
category: "network",
|
|
837
|
-
cause: err,
|
|
838
|
-
operation: options.operation
|
|
839
|
-
});
|
|
840
|
-
if (isAbortError(err)) throw err;
|
|
841
|
-
RetryableError.handleNetworkError(err, options.operation);
|
|
842
|
-
}
|
|
843
|
-
return {
|
|
844
|
-
body: response.body,
|
|
845
|
-
headers: response.headers,
|
|
846
|
-
statusCode: response.status
|
|
847
|
-
};
|
|
848
|
-
}
|
|
849
|
-
|
|
850
|
-
//#endregion
|
|
851
|
-
//#region impl/utils/url.mts
|
|
852
|
-
const ensureDirectoryUrl = (value) => {
|
|
853
|
-
const url = new URL(value);
|
|
854
|
-
if (!url.pathname.endsWith("/")) url.pathname = `${url.pathname}/`;
|
|
855
|
-
return url;
|
|
856
|
-
};
|
|
857
|
-
const createCouchDbUrl = (value) => {
|
|
858
|
-
return new URL(value);
|
|
859
|
-
};
|
|
860
|
-
const createCouchPathUrl = (path, base) => {
|
|
861
|
-
return new URL(path, ensureDirectoryUrl(base));
|
|
862
|
-
};
|
|
863
|
-
const createCouchDocUrl = (docId, base) => {
|
|
864
|
-
return new URL(encodeURIComponent(docId), ensureDirectoryUrl(base));
|
|
865
|
-
};
|
|
866
|
-
|
|
867
928
|
//#endregion
|
|
868
929
|
//#region impl/bulkGet.mts
|
|
869
930
|
/**
|
|
@@ -2414,6 +2475,7 @@ function doBind(config) {
|
|
|
2414
2475
|
bulkGetDictionary: getBoundWithRetry(bulkGetDictionary, config),
|
|
2415
2476
|
get: getBoundWithRetry(get, config),
|
|
2416
2477
|
getAtRev: getBoundWithRetry(getAtRev, config),
|
|
2478
|
+
find: getBoundWithRetry(find, config),
|
|
2417
2479
|
query: getBoundWithRetry(query, config),
|
|
2418
2480
|
bulkRemove: config.bindWithRetry ? withRetry(bulkRemove.bind(null, config), retryOptions) : bulkRemove.bind(null, config),
|
|
2419
2481
|
bulkRemoveMap: config.bindWithRetry ? withRetry(bulkRemoveMap.bind(null, config), retryOptions) : bulkRemoveMap.bind(null, config),
|
|
@@ -2433,4 +2495,4 @@ function doBind(config) {
|
|
|
2433
2495
|
}
|
|
2434
2496
|
|
|
2435
2497
|
//#endregion
|
|
2436
|
-
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 };
|