@nxtedition/lib 20.1.1 → 20.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/couch.js +22 -5
- package/mutex.js +50 -0
- package/package.json +4 -2
- package/platform.js +2 -2
- package/s3.js +1 -1
package/couch.js
CHANGED
|
@@ -132,7 +132,7 @@ export function makeCouch(opts) {
|
|
|
132
132
|
* @param {string} [options.since=null] - The sequence number to start from.
|
|
133
133
|
* @yields {Array<{ id: string, seq?: string, doc?: Object, deleted?: boolean, changes: Array<{ rev: string }> }>}
|
|
134
134
|
*/
|
|
135
|
-
async function* changes({ client = defaultClient, signal = null, ...options } = {}) {
|
|
135
|
+
async function* changes({ client = defaultClient, signal = null, logger, ...options } = {}) {
|
|
136
136
|
const params = {}
|
|
137
137
|
|
|
138
138
|
let body
|
|
@@ -221,7 +221,13 @@ export function makeCouch(opts) {
|
|
|
221
221
|
}
|
|
222
222
|
|
|
223
223
|
const live = options.live == null || !!options.live
|
|
224
|
-
const retry =
|
|
224
|
+
const retry =
|
|
225
|
+
options.retry ??
|
|
226
|
+
(async (err, retryCount, params, { signal }, next) => {
|
|
227
|
+
logger?.error({ err, retryCount, params }, 'changes error')
|
|
228
|
+
return next()
|
|
229
|
+
})
|
|
230
|
+
|
|
225
231
|
const limit = options.limit
|
|
226
232
|
|
|
227
233
|
try {
|
|
@@ -230,10 +236,11 @@ export function makeCouch(opts) {
|
|
|
230
236
|
let remaining = Number(limit) || Infinity
|
|
231
237
|
while (remaining > 0) {
|
|
232
238
|
let count = 0
|
|
239
|
+
const limit = Math.min(remaining, socketCount)
|
|
233
240
|
for await (const changes of _changes({
|
|
234
241
|
live,
|
|
235
242
|
retry,
|
|
236
|
-
limit
|
|
243
|
+
limit,
|
|
237
244
|
params,
|
|
238
245
|
method,
|
|
239
246
|
body,
|
|
@@ -263,7 +270,17 @@ export function makeCouch(opts) {
|
|
|
263
270
|
}
|
|
264
271
|
}
|
|
265
272
|
|
|
266
|
-
async function* _changes({
|
|
273
|
+
async function* _changes({
|
|
274
|
+
live,
|
|
275
|
+
retry,
|
|
276
|
+
limit,
|
|
277
|
+
params,
|
|
278
|
+
method,
|
|
279
|
+
body,
|
|
280
|
+
signal,
|
|
281
|
+
client,
|
|
282
|
+
blocking = live || !limit || limit > 256,
|
|
283
|
+
}) {
|
|
267
284
|
let retryCount = 0
|
|
268
285
|
let remaining = Number(limit) || Infinity
|
|
269
286
|
while (true) {
|
|
@@ -280,7 +297,7 @@ export function makeCouch(opts) {
|
|
|
280
297
|
origin: dbOrigin,
|
|
281
298
|
query,
|
|
282
299
|
idempotent: false,
|
|
283
|
-
blocking
|
|
300
|
+
blocking,
|
|
284
301
|
method,
|
|
285
302
|
body: JSON.stringify(body),
|
|
286
303
|
signal,
|
package/mutex.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import Queue from 'yocto-queue'
|
|
2
|
+
|
|
3
|
+
export default class Mutex {
|
|
4
|
+
#queue = new Queue()
|
|
5
|
+
#isLocked = false
|
|
6
|
+
|
|
7
|
+
tryLock() {
|
|
8
|
+
if (!this.#isLocked) {
|
|
9
|
+
this.#isLocked = true
|
|
10
|
+
return true
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return false
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async lock() {
|
|
17
|
+
if (!this.#isLocked) {
|
|
18
|
+
this.#isLocked = true
|
|
19
|
+
return
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return new Promise((resolve) => {
|
|
23
|
+
this.#queue.enqueue(resolve)
|
|
24
|
+
})
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
unlock() {
|
|
28
|
+
if (this.#queue.size > 0) {
|
|
29
|
+
const resolve = this.#queue.dequeue()
|
|
30
|
+
resolve()
|
|
31
|
+
} else {
|
|
32
|
+
this.#isLocked = false
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async withLock(task) {
|
|
37
|
+
try {
|
|
38
|
+
if (!this.tryLock()) {
|
|
39
|
+
await this.lock()
|
|
40
|
+
}
|
|
41
|
+
return await task()
|
|
42
|
+
} finally {
|
|
43
|
+
this.unlock()
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
get isLocked() {
|
|
48
|
+
return this.#isLocked
|
|
49
|
+
}
|
|
50
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nxtedition/lib",
|
|
3
|
-
"version": "20.
|
|
3
|
+
"version": "20.2.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Robert Nagy <robert.nagy@boffins.se>",
|
|
6
6
|
"type": "module",
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
"elasticsearch.js",
|
|
17
17
|
"merge-ranges.js",
|
|
18
18
|
"http.js",
|
|
19
|
+
"mutex.js",
|
|
19
20
|
"s3.js",
|
|
20
21
|
"time.js",
|
|
21
22
|
"deepstream.js",
|
|
@@ -79,7 +80,8 @@
|
|
|
79
80
|
"split-string": "^6.0.0",
|
|
80
81
|
"undici": "^6.19.7",
|
|
81
82
|
"url-join": "^5.0.0",
|
|
82
|
-
"xuid": "^4.1.3"
|
|
83
|
+
"xuid": "^4.1.3",
|
|
84
|
+
"yocto-queue": "^1.1.1"
|
|
83
85
|
},
|
|
84
86
|
"devDependencies": {
|
|
85
87
|
"@nxtedition/deepstream.io-client-js": ">=25.6.3",
|
package/platform.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export const SIGNALS =
|
|
2
2
|
typeof process === 'object'
|
|
3
|
-
? {
|
|
3
|
+
? ({
|
|
4
4
|
linux: {
|
|
5
5
|
129: 'SIGHUP', // 1
|
|
6
6
|
130: 'SIGINT', // 2
|
|
@@ -34,5 +34,5 @@ export const SIGNALS =
|
|
|
34
34
|
158: 'SIGPWR', // 30
|
|
35
35
|
159: 'SIGSYS', // 31
|
|
36
36
|
},
|
|
37
|
-
}[process.platform] ?? {}
|
|
37
|
+
}[process.platform] ?? {})
|
|
38
38
|
: {}
|
package/s3.js
CHANGED
|
@@ -321,7 +321,7 @@ function getTransformedHeaders(headers) {
|
|
|
321
321
|
const headerValues = headers[name]
|
|
322
322
|
transformedHeaders[name] = Array.isArray(headerValues)
|
|
323
323
|
? headerValues.join(',')
|
|
324
|
-
: headerValues ?? ''
|
|
324
|
+
: (headerValues ?? '')
|
|
325
325
|
}
|
|
326
326
|
|
|
327
327
|
return transformedHeaders
|