@nxtedition/lib 19.6.3 → 19.7.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/app.js +66 -0
- package/couch.js +2 -0
- package/package.json +1 -1
- 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
|
|
@@ -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
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) {
|