@nxtedition/lib 23.9.14 → 23.9.16

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.
Files changed (3) hide show
  1. package/app.js +23 -9
  2. package/package.json +2 -3
  3. package/yield.js +0 -52
package/app.js CHANGED
@@ -556,6 +556,10 @@ export function makeApp(appConfig, onTerminate) {
556
556
  resourceLimits,
557
557
  utilization: performance.eventLoopUtilization?.(elu2, elu1),
558
558
  heap: v8.getHeapStatistics(),
559
+ http: {
560
+ userAgent,
561
+ pending: globalThis._nxt_lib_http_pending.size,
562
+ },
559
563
  }
560
564
  }),
561
565
  ),
@@ -1005,20 +1009,28 @@ export function makeApp(appConfig, onTerminate) {
1005
1009
  const utilsBC = new BroadcastChannel('nxt:utils')
1006
1010
  utilsBC.unref()
1007
1011
 
1012
+ function writeHeapSnapshot() {
1013
+ const snapshotPath = `${os.tmpdir()}/heap-${Date.now()}-${serviceName}-${serviceModule}-${serviceInstanceId}-${serviceWorkerId}.heapsnapshot`
1014
+ v8.writeHeapSnapshot(snapshotPath)
1015
+ logger.info({ snapshotPath }, 'heap snapshot')
1016
+ }
1017
+
1018
+ function gc() {
1019
+ if (global.gc) {
1020
+ global.gc()
1021
+ logger.debug('gc')
1022
+ } else {
1023
+ logger.warn('gc not available')
1024
+ }
1025
+ }
1026
+
1008
1027
  utilsBC.onmessage = ({ data }) => {
1009
1028
  const { type } = data
1010
1029
  try {
1011
1030
  if (type === 'utils:gc') {
1012
- if (global.gc) {
1013
- global.gc()
1014
- logger.debug('gc')
1015
- } else {
1016
- logger.warn('gc not available')
1017
- }
1031
+ gc()
1018
1032
  } else if (type === 'utils:writeHeapSnapshot') {
1019
- const snapshotPath = `${os.tmpdir()}/heap-${Date.now()}-${serviceName}-${serviceModule}-${serviceInstanceId}-${serviceWorkerId}.heapsnapshot`
1020
- v8.writeHeapSnapshot(snapshotPath)
1021
- logger.info({ snapshotPath }, 'heap snapshot')
1033
+ writeHeapSnapshot()
1022
1034
  }
1023
1035
  } catch (err) {
1024
1036
  logger.error({ err, type })
@@ -1045,12 +1057,14 @@ export function makeApp(appConfig, onTerminate) {
1045
1057
  }
1046
1058
 
1047
1059
  if (req.method === 'POST' && req.url === '/gc') {
1060
+ gc()
1048
1061
  utilsBC.postMessage({ type: 'utils:gc' })
1049
1062
  res.end()
1050
1063
  return
1051
1064
  }
1052
1065
 
1053
1066
  if (req.method === 'POST' && req.url === '/writeHeapSnapshot') {
1067
+ writeHeapSnapshot()
1054
1068
  utilsBC.postMessage({ type: 'utils:writeHeapSnapshot' })
1055
1069
  res.end()
1056
1070
  return
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "23.9.14",
3
+ "version": "23.9.16",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
@@ -44,8 +44,7 @@
44
44
  "transcript.js",
45
45
  "docker-secrets.js",
46
46
  "wordwrap.js",
47
- "under-pressure.js",
48
- "yield.js"
47
+ "under-pressure.js"
49
48
  ],
50
49
  "scripts": {
51
50
  "prepublishOnly": "pinst --disable",
package/yield.js DELETED
@@ -1,52 +0,0 @@
1
- import { AbortError } from './errors.js'
2
-
3
- let yieldTime = performance.now()
4
-
5
- export function maybeYield(callback, opaque, opts) {
6
- if (callback != null && typeof callback !== 'function') {
7
- throw new TypeError('callback must be a function')
8
- }
9
-
10
- const timeout = opts?.timeout ?? 50
11
-
12
- if (!Number.isFinite(timeout) || timeout < 0) {
13
- throw new TypeError('timeout must be a finite non-negative number')
14
- }
15
-
16
- if (performance.now() - yieldTime > timeout) {
17
- doYield(opts, callback, opaque)
18
- } else {
19
- callback(opts?.signal?.aborted ? (opts.signal.reason ?? new AbortError()) : null, opaque)
20
- }
21
- }
22
-
23
- export async function doYield(callback, opaque, opts) {
24
- if (callback != null && typeof callback !== 'function') {
25
- throw new TypeError('callback must be a function')
26
- }
27
-
28
- // TODO (fix): Use a yield queue?
29
-
30
- setImmediate(() => {
31
- resetYield(opts)
32
- callback(opts?.signal?.aborted ? (opts.signal.reason ?? new AbortError()) : null, opaque)
33
- })
34
- }
35
-
36
- function resetYield(opts) {
37
- yieldTime = performance.now()
38
- opts?.signal?.throwIfAborted()
39
- }
40
-
41
- setInterval(() => {
42
- yieldTime = performance.now()
43
- }, 500).unref()
44
-
45
- export const promises = {
46
- maybeYield(opts, opaque) {
47
- return new Promise((resolve, reject) =>
48
- maybeYield((err, val) => (err ? reject(err) : resolve(val)), opaque, opts),
49
- )
50
- },
51
- resetYield,
52
- }