@nxtedition/lib 19.6.3 → 19.7.1
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/app.js +67 -1
- package/couch.js +2 -0
- package/package.json +2 -1
- package/time.js +32 -0
- package/trace.js +12 -8
package/app.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as inspector from 'node:inspector'
|
|
1
2
|
import os from 'node:os'
|
|
2
3
|
import net from 'node:net'
|
|
3
4
|
import assert from 'node:assert'
|
|
@@ -21,6 +22,7 @@ import hashString from './hash.js'
|
|
|
21
22
|
import { makeTrace } from './trace.js'
|
|
22
23
|
import compose from 'koa-compose'
|
|
23
24
|
import { createServer } from './http.js'
|
|
25
|
+
import { json } from 'node:stream/consumers'
|
|
24
26
|
|
|
25
27
|
export function makeApp(appConfig, onTerminate) {
|
|
26
28
|
let ds
|
|
@@ -276,7 +278,7 @@ export function makeApp(appConfig, onTerminate) {
|
|
|
276
278
|
dsConfig.credentials.username ||
|
|
277
279
|
dsConfig.credentials.userName ||
|
|
278
280
|
dsConfig.credentials.user ||
|
|
279
|
-
serviceName
|
|
281
|
+
`_${serviceName}`
|
|
280
282
|
|
|
281
283
|
dsConfig = {
|
|
282
284
|
maxReconnectAttempts: Infinity,
|
|
@@ -678,6 +680,40 @@ export function makeApp(appConfig, onTerminate) {
|
|
|
678
680
|
}
|
|
679
681
|
}
|
|
680
682
|
|
|
683
|
+
const inspectBC = new BroadcastChannel('nxt:inspect')
|
|
684
|
+
const inspectSet = new Set()
|
|
685
|
+
|
|
686
|
+
if (serviceWorkerId) {
|
|
687
|
+
inspectBC.onmessage = ({ data }) => {
|
|
688
|
+
const { type } = data
|
|
689
|
+
|
|
690
|
+
if (type === 'inspect:open') {
|
|
691
|
+
// TODO (fix): What happens if you call inspect:open multiple times?
|
|
692
|
+
inspector.open(data.port, data.hostname)
|
|
693
|
+
} else if (type === 'inspect:close') {
|
|
694
|
+
inspector.close()
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
inspectBC.postMessage({ type: 'inspect:register', id: serviceWorkerId })
|
|
699
|
+
destroyers.push(() => {
|
|
700
|
+
inspector.close()
|
|
701
|
+
inspectBC.postMessage({ type: 'inspect:unregister', id: serviceWorkerId })
|
|
702
|
+
})
|
|
703
|
+
} else {
|
|
704
|
+
inspectBC.onmessage = ({ data }) => {
|
|
705
|
+
const { type } = data
|
|
706
|
+
|
|
707
|
+
if (type === 'inspect:register') {
|
|
708
|
+
inspectSet.add(data.id)
|
|
709
|
+
} else if (type === 'inspect:unregister') {
|
|
710
|
+
inspectSet.delete(data.id)
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
destroyers.push(() => inspectBC.close())
|
|
716
|
+
|
|
681
717
|
if (appConfig.http) {
|
|
682
718
|
const httpConfig = { ...appConfig.http, ...config.http }
|
|
683
719
|
|
|
@@ -700,6 +736,36 @@ export function makeApp(appConfig, onTerminate) {
|
|
|
700
736
|
return next()
|
|
701
737
|
}
|
|
702
738
|
},
|
|
739
|
+
async ({ req, res }, next) => {
|
|
740
|
+
if (req.url.startsWith('/inspect')) {
|
|
741
|
+
if (req.method === 'GET') {
|
|
742
|
+
res.end(JSON.stringify({ workers: Array.from(inspectSet).map((id) => ({ id })) }))
|
|
743
|
+
} else if (req.method === 'POST') {
|
|
744
|
+
const m = req.url.match(/^\/inspect\/([^/]+)/)
|
|
745
|
+
if (!m) {
|
|
746
|
+
res.statusCode = 404
|
|
747
|
+
res.end()
|
|
748
|
+
} else {
|
|
749
|
+
const { port, hostname } = await json(req)
|
|
750
|
+
|
|
751
|
+
// TODO (fix): What about return value & errors?
|
|
752
|
+
inspectBC.postMessage({
|
|
753
|
+
type: port || hostname ? 'inspect:open' : 'inspect:close',
|
|
754
|
+
id: m[1],
|
|
755
|
+
port,
|
|
756
|
+
hostname,
|
|
757
|
+
})
|
|
758
|
+
|
|
759
|
+
res.end()
|
|
760
|
+
}
|
|
761
|
+
} else {
|
|
762
|
+
res.statusCode = 405
|
|
763
|
+
res.end()
|
|
764
|
+
}
|
|
765
|
+
} else {
|
|
766
|
+
return next()
|
|
767
|
+
}
|
|
768
|
+
},
|
|
703
769
|
appConfig.http.request
|
|
704
770
|
? appConfig.http.request
|
|
705
771
|
: typeof appConfig.http === 'function'
|
package/couch.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nxtedition/lib",
|
|
3
|
-
"version": "19.
|
|
3
|
+
"version": "19.7.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Robert Nagy <robert.nagy@boffins.se>",
|
|
6
6
|
"type": "module",
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
"merge-ranges.js",
|
|
18
18
|
"http.js",
|
|
19
19
|
"s3.js",
|
|
20
|
+
"time.js",
|
|
20
21
|
"deepstream.js",
|
|
21
22
|
"sequence.js",
|
|
22
23
|
"logger.js",
|
package/time.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export function isTimeBetween(date, startTime, endTime) {
|
|
2
|
+
const currentHours = date.getHours()
|
|
3
|
+
const currentMinutes = date.getMinutes()
|
|
4
|
+
|
|
5
|
+
let [startHours, startMinutes] = startTime?.split(':').map(Number) ?? [null, 0]
|
|
6
|
+
let [endHours, endMinutes] = endTime?.split(':').map(Number) ?? [null, 0]
|
|
7
|
+
|
|
8
|
+
if (startHours == null) {
|
|
9
|
+
startHours = -Number.MAX_SAFE_INTEGER
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (endHours == null) {
|
|
13
|
+
endHours = Number.MAX_SAFE_INTEGER
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (startHours < endHours || (startHours === endHours && startMinutes <= endMinutes)) {
|
|
17
|
+
// The time range is within the same day
|
|
18
|
+
return (currentHours > startHours ||
|
|
19
|
+
(currentHours === startHours && currentMinutes >= startMinutes)) &&
|
|
20
|
+
(currentHours < endHours || (currentHours === endHours && currentMinutes <= endMinutes))
|
|
21
|
+
? { currentHours, currentMinutes, startHours, startMinutes, endHours, endMinutes }
|
|
22
|
+
: null
|
|
23
|
+
} else {
|
|
24
|
+
// The time range spans across two days
|
|
25
|
+
return currentHours > startHours ||
|
|
26
|
+
(currentHours === startHours && currentMinutes >= startMinutes) ||
|
|
27
|
+
currentHours < endHours ||
|
|
28
|
+
(currentHours === endHours && currentMinutes <= endMinutes)
|
|
29
|
+
? { currentHours, currentMinutes, startHours, startMinutes, endHours, endMinutes }
|
|
30
|
+
: null
|
|
31
|
+
}
|
|
32
|
+
}
|
package/trace.js
CHANGED
|
@@ -68,14 +68,18 @@ export function makeTrace({
|
|
|
68
68
|
const prefix = `{ "create": { "_index": "trace-${index}" } }\n{ "serviceName": "${serviceName}", "op": "`
|
|
69
69
|
|
|
70
70
|
function trace(obj, op) {
|
|
71
|
-
if (obj
|
|
72
|
-
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
71
|
+
if (typeof obj === 'string') {
|
|
72
|
+
// Do nothing...
|
|
73
|
+
} else {
|
|
74
|
+
if (obj.serviceName) {
|
|
75
|
+
throw new Error('invalid property `serviceName`')
|
|
76
|
+
}
|
|
77
|
+
if (obj.op) {
|
|
78
|
+
throw new Error('invalid property `op`')
|
|
79
|
+
}
|
|
80
|
+
if (obj['@timestamp']) {
|
|
81
|
+
throw new Error('invalid property `@timestamp`')
|
|
82
|
+
}
|
|
79
83
|
}
|
|
80
84
|
|
|
81
85
|
if (pending > limit) {
|