@nxtedition/lib 19.8.5 → 19.8.7
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/couch.js +38 -33
- package/package.json +2 -2
package/couch.js
CHANGED
|
@@ -6,9 +6,8 @@ import { makeWeakCache } from './weakCache.js'
|
|
|
6
6
|
import tp from 'timers/promises'
|
|
7
7
|
import { defaultDelay as delay } from './http.js'
|
|
8
8
|
import urljoin from 'url-join'
|
|
9
|
-
import undici, { util as undiciUtil } from 'undici'
|
|
10
9
|
import { AbortError } from './errors.js'
|
|
11
|
-
import { interceptors } from '@nxtedition/nxt-undici'
|
|
10
|
+
import { interceptors, Agent, Pool, parseHeaders } from '@nxtedition/nxt-undici'
|
|
12
11
|
|
|
13
12
|
// https://github.com/fastify/fastify/blob/main/lib/reqIdGenFactory.js
|
|
14
13
|
// 2,147,483,647 (2^31 − 1) stands for max SMI value (an internal optimization of V8).
|
|
@@ -23,23 +22,6 @@ function genReqId() {
|
|
|
23
22
|
return `req-${nextReqId.toString(36)}`
|
|
24
23
|
}
|
|
25
24
|
|
|
26
|
-
function parseHeaders(headers, obj = {}) {
|
|
27
|
-
for (let i = 0; i < headers.length; i += 2) {
|
|
28
|
-
const key = headers[i].toString().toLowerCase()
|
|
29
|
-
let val = obj[key]
|
|
30
|
-
if (!val) {
|
|
31
|
-
obj[key] = headers[i + 1].toString()
|
|
32
|
-
} else {
|
|
33
|
-
if (!Array.isArray(val)) {
|
|
34
|
-
val = [val]
|
|
35
|
-
obj[key] = val
|
|
36
|
-
}
|
|
37
|
-
val.push(headers[i + 1].toString())
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
return obj
|
|
41
|
-
}
|
|
42
|
-
|
|
43
25
|
export function makeCouch(opts) {
|
|
44
26
|
let config
|
|
45
27
|
if (typeof opts === 'string') {
|
|
@@ -75,13 +57,13 @@ export function makeCouch(opts) {
|
|
|
75
57
|
}
|
|
76
58
|
|
|
77
59
|
const userAgent = config.userAgent || globalThis.userAgent
|
|
78
|
-
const defaultClient = new
|
|
60
|
+
const defaultClient = new Pool(dbOrigin, defaultClientOpts)
|
|
79
61
|
|
|
80
62
|
const getClient =
|
|
81
63
|
config.getClient ??
|
|
82
64
|
makeWeakCache(
|
|
83
65
|
() =>
|
|
84
|
-
new
|
|
66
|
+
new Pool(dbOrigin, {
|
|
85
67
|
...defaultClientOpts,
|
|
86
68
|
connections: 4, // TODO (fix): Global limit?
|
|
87
69
|
pipelining: 2,
|
|
@@ -133,7 +115,12 @@ export function makeCouch(opts) {
|
|
|
133
115
|
})
|
|
134
116
|
}
|
|
135
117
|
|
|
136
|
-
async function* changes({
|
|
118
|
+
async function* changes({
|
|
119
|
+
client = defaultClient,
|
|
120
|
+
signal = null,
|
|
121
|
+
highWaterMark = 128 * 1024,
|
|
122
|
+
...options
|
|
123
|
+
} = {}) {
|
|
137
124
|
const params = {}
|
|
138
125
|
|
|
139
126
|
let body
|
|
@@ -594,7 +581,12 @@ export function makeCouch(opts) {
|
|
|
594
581
|
return res.data
|
|
595
582
|
}
|
|
596
583
|
|
|
597
|
-
async function put(
|
|
584
|
+
async function put(
|
|
585
|
+
pathname,
|
|
586
|
+
params,
|
|
587
|
+
body,
|
|
588
|
+
{ client = defaultClient, signal = null, idempotent = true, headers = null } = {},
|
|
589
|
+
) {
|
|
598
590
|
const req = {
|
|
599
591
|
pathname,
|
|
600
592
|
params,
|
|
@@ -639,7 +631,7 @@ export function makeCouch(opts) {
|
|
|
639
631
|
pathname,
|
|
640
632
|
params,
|
|
641
633
|
body,
|
|
642
|
-
{ client = getClient('_all_docs'), signal, idempotent = true, headers } = {},
|
|
634
|
+
{ client = getClient('_all_docs'), signal = null, idempotent = true, headers = null } = {},
|
|
643
635
|
) {
|
|
644
636
|
const req = {
|
|
645
637
|
pathname,
|
|
@@ -664,7 +656,7 @@ export function makeCouch(opts) {
|
|
|
664
656
|
pathname,
|
|
665
657
|
params,
|
|
666
658
|
body,
|
|
667
|
-
{ client, signal, idempotent = true, headers } = {},
|
|
659
|
+
{ client = defaultClient, signal = null, idempotent = true, headers = null } = {},
|
|
668
660
|
) {
|
|
669
661
|
const req = {
|
|
670
662
|
pathname,
|
|
@@ -685,7 +677,11 @@ export function makeCouch(opts) {
|
|
|
685
677
|
return res.data
|
|
686
678
|
}
|
|
687
679
|
|
|
688
|
-
async function info(
|
|
680
|
+
async function info(
|
|
681
|
+
params,
|
|
682
|
+
body,
|
|
683
|
+
{ client = defaultClient, signal = null, idempotent = true, headers = null } = {},
|
|
684
|
+
) {
|
|
689
685
|
const req = {
|
|
690
686
|
pathname: null,
|
|
691
687
|
params,
|
|
@@ -705,7 +701,7 @@ export function makeCouch(opts) {
|
|
|
705
701
|
return res.data
|
|
706
702
|
}
|
|
707
703
|
|
|
708
|
-
async function up(params, body, { client = defaultClient, signal } = {}) {
|
|
704
|
+
async function up(params, body, { client = defaultClient, signal = null } = {}) {
|
|
709
705
|
const res = await client.request({
|
|
710
706
|
path: '/_up',
|
|
711
707
|
method: 'GET',
|
|
@@ -714,7 +710,8 @@ export function makeCouch(opts) {
|
|
|
714
710
|
})
|
|
715
711
|
return await res.body.json()
|
|
716
712
|
}
|
|
717
|
-
|
|
713
|
+
|
|
714
|
+
async function upsert(pathname, diffFun, { client = defaultClient, signal = null } = {}) {
|
|
718
715
|
while (true) {
|
|
719
716
|
let doc
|
|
720
717
|
try {
|
|
@@ -949,7 +946,7 @@ class StreamOutput extends stream.Readable {
|
|
|
949
946
|
}
|
|
950
947
|
}
|
|
951
948
|
|
|
952
|
-
onHeaders(statusCode, rawHeaders, resume, statusText) {
|
|
949
|
+
onHeaders(statusCode, rawHeaders, resume, statusText, headers = parseHeaders(rawHeaders)) {
|
|
953
950
|
if (this.#stats.headers === -1) {
|
|
954
951
|
this.#stats.headers = performance.now() - this.#startTime - this.#stats.connect
|
|
955
952
|
}
|
|
@@ -958,7 +955,7 @@ class StreamOutput extends stream.Readable {
|
|
|
958
955
|
throw new Error('invalid status code: ' + statusCode)
|
|
959
956
|
}
|
|
960
957
|
|
|
961
|
-
this.#headers =
|
|
958
|
+
this.#headers = headers
|
|
962
959
|
this.#resume = resume
|
|
963
960
|
}
|
|
964
961
|
|
|
@@ -1042,7 +1039,7 @@ class PromiseOutput {
|
|
|
1042
1039
|
}
|
|
1043
1040
|
}
|
|
1044
1041
|
|
|
1045
|
-
onHeaders(statusCode, rawHeaders, resume, statusText) {
|
|
1042
|
+
onHeaders(statusCode, rawHeaders, resume, statusText, headers = parseHeaders(rawHeaders)) {
|
|
1046
1043
|
if (this.#stats.headers === -1) {
|
|
1047
1044
|
this.#stats.headers = performance.now() - this.#startTime - this.#stats.connect
|
|
1048
1045
|
}
|
|
@@ -1050,7 +1047,8 @@ class PromiseOutput {
|
|
|
1050
1047
|
if (statusCode >= 300 || statusCode < 200) {
|
|
1051
1048
|
throw new Error('invalid status code: ' + statusCode)
|
|
1052
1049
|
}
|
|
1053
|
-
|
|
1050
|
+
|
|
1051
|
+
this.#headers = headers
|
|
1054
1052
|
}
|
|
1055
1053
|
|
|
1056
1054
|
onData(data) {
|
|
@@ -1094,6 +1092,10 @@ class PromiseOutput {
|
|
|
1094
1092
|
}
|
|
1095
1093
|
|
|
1096
1094
|
const dispatcherCache = new WeakMap()
|
|
1095
|
+
const defaultDispatcher = new Agent({
|
|
1096
|
+
pipelining: 4,
|
|
1097
|
+
connections: 8,
|
|
1098
|
+
})
|
|
1097
1099
|
export function request(
|
|
1098
1100
|
url,
|
|
1099
1101
|
{
|
|
@@ -1101,10 +1103,11 @@ export function request(
|
|
|
1101
1103
|
method = body ? 'POST' : 'GET',
|
|
1102
1104
|
query,
|
|
1103
1105
|
headers,
|
|
1104
|
-
dispatcher =
|
|
1106
|
+
dispatcher = defaultDispatcher,
|
|
1105
1107
|
signal,
|
|
1106
1108
|
logger,
|
|
1107
1109
|
stream,
|
|
1110
|
+
blocking = Boolean(stream),
|
|
1108
1111
|
},
|
|
1109
1112
|
) {
|
|
1110
1113
|
signal?.throwIfAborted()
|
|
@@ -1118,6 +1121,7 @@ export function request(
|
|
|
1118
1121
|
origin: url.origin,
|
|
1119
1122
|
path: url.pathname + '?' + querystring.stringify(query),
|
|
1120
1123
|
method,
|
|
1124
|
+
blocking,
|
|
1121
1125
|
headers: {
|
|
1122
1126
|
'content-type': body != null && typeof body === 'object' ? 'application/json' : 'plain/text',
|
|
1123
1127
|
'user-agent': globalThis.userAgent ?? 'nxt-lib',
|
|
@@ -1133,6 +1137,7 @@ export function request(
|
|
|
1133
1137
|
wrappedDispatcher = dispatcher.compose(
|
|
1134
1138
|
interceptors.responseError(),
|
|
1135
1139
|
interceptors.log(),
|
|
1140
|
+
interceptors.dns(),
|
|
1136
1141
|
interceptors.requestId(),
|
|
1137
1142
|
interceptors.responseRetry(),
|
|
1138
1143
|
interceptors.responseVerify(),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nxtedition/lib",
|
|
3
|
-
"version": "19.8.
|
|
3
|
+
"version": "19.8.7",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Robert Nagy <robert.nagy@boffins.se>",
|
|
6
6
|
"type": "module",
|
|
@@ -82,7 +82,7 @@
|
|
|
82
82
|
"@aws-sdk/client-s3": "^3.600.0",
|
|
83
83
|
"@elastic/elasticsearch": "^8.14.0",
|
|
84
84
|
"@elastic/transport": "^8.6.1",
|
|
85
|
-
"@nxtedition/nxt-undici": "^3.0
|
|
85
|
+
"@nxtedition/nxt-undici": "^3.2.0",
|
|
86
86
|
"content-type": "^1.0.5",
|
|
87
87
|
"date-fns": "^3.6.0",
|
|
88
88
|
"fast-querystring": "^1.1.1",
|