@nxtedition/lib 19.8.6 → 19.8.8
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 +49 -59
- 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 {
|
|
10
|
+
import { dispatch, 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) {
|
|
@@ -1093,19 +1091,24 @@ class PromiseOutput {
|
|
|
1093
1091
|
}
|
|
1094
1092
|
}
|
|
1095
1093
|
|
|
1096
|
-
const
|
|
1094
|
+
const defaultDispatcher = new Agent({
|
|
1095
|
+
pipelining: 4,
|
|
1096
|
+
connections: 8,
|
|
1097
|
+
})
|
|
1098
|
+
|
|
1097
1099
|
export function request(
|
|
1098
1100
|
url,
|
|
1099
1101
|
{
|
|
1100
|
-
body,
|
|
1102
|
+
body = null,
|
|
1101
1103
|
method = body ? 'POST' : 'GET',
|
|
1102
|
-
query,
|
|
1103
|
-
headers,
|
|
1104
|
-
dispatcher =
|
|
1105
|
-
signal,
|
|
1106
|
-
logger,
|
|
1107
|
-
stream,
|
|
1108
|
-
|
|
1104
|
+
query = null,
|
|
1105
|
+
headers = null,
|
|
1106
|
+
dispatcher = defaultDispatcher,
|
|
1107
|
+
signal = null,
|
|
1108
|
+
logger = null,
|
|
1109
|
+
stream = false,
|
|
1110
|
+
blocking = Boolean(stream),
|
|
1111
|
+
} = {},
|
|
1109
1112
|
) {
|
|
1110
1113
|
signal?.throwIfAborted()
|
|
1111
1114
|
|
|
@@ -1115,12 +1118,12 @@ export function request(
|
|
|
1115
1118
|
}
|
|
1116
1119
|
|
|
1117
1120
|
const opts = {
|
|
1118
|
-
|
|
1119
|
-
path: url.pathname + '?' + querystring.stringify(query),
|
|
1121
|
+
url,
|
|
1120
1122
|
method,
|
|
1123
|
+
query,
|
|
1124
|
+
blocking,
|
|
1121
1125
|
headers: {
|
|
1122
1126
|
'content-type': body != null && typeof body === 'object' ? 'application/json' : 'plain/text',
|
|
1123
|
-
'user-agent': globalThis.userAgent ?? 'nxt-lib',
|
|
1124
1127
|
accept: 'application/json',
|
|
1125
1128
|
...headers,
|
|
1126
1129
|
},
|
|
@@ -1128,27 +1131,14 @@ export function request(
|
|
|
1128
1131
|
body: body != null && typeof body === 'object' ? JSON.stringify(body) : body,
|
|
1129
1132
|
}
|
|
1130
1133
|
|
|
1131
|
-
let wrappedDispatcher = dispatcherCache.get(dispatcher)
|
|
1132
|
-
if (!wrappedDispatcher) {
|
|
1133
|
-
wrappedDispatcher = dispatcher.compose(
|
|
1134
|
-
interceptors.responseError(),
|
|
1135
|
-
interceptors.log(),
|
|
1136
|
-
interceptors.dns(),
|
|
1137
|
-
interceptors.requestId(),
|
|
1138
|
-
interceptors.responseRetry(),
|
|
1139
|
-
interceptors.responseVerify(),
|
|
1140
|
-
)
|
|
1141
|
-
dispatcherCache.set(dispatcher, wrappedDispatcher)
|
|
1142
|
-
}
|
|
1143
|
-
|
|
1144
1134
|
if (stream) {
|
|
1145
1135
|
const handler = new StreamOutput({ signal, ...stream })
|
|
1146
|
-
|
|
1136
|
+
dispatch(dispatcher, opts, handler)
|
|
1147
1137
|
return handler
|
|
1148
1138
|
} else {
|
|
1149
1139
|
return new Promise((resolve, reject) => {
|
|
1150
1140
|
const handler = new PromiseOutput({ resolve, reject, signal })
|
|
1151
|
-
|
|
1141
|
+
dispatch(dispatcher, opts, handler)
|
|
1152
1142
|
})
|
|
1153
1143
|
}
|
|
1154
1144
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nxtedition/lib",
|
|
3
|
-
"version": "19.8.
|
|
3
|
+
"version": "19.8.8",
|
|
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.1
|
|
85
|
+
"@nxtedition/nxt-undici": "^3.3.1",
|
|
86
86
|
"content-type": "^1.0.5",
|
|
87
87
|
"date-fns": "^3.6.0",
|
|
88
88
|
"fast-querystring": "^1.1.1",
|