@nxtedition/lib 22.0.11 → 22.1.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.
Files changed (3) hide show
  1. package/app.js +70 -0
  2. package/couch.js +1 -1
  3. package/package.json +1 -1
package/app.js CHANGED
@@ -56,6 +56,8 @@ export function makeApp(appConfig, onTerminate) {
56
56
  undici.setGlobalDispatcher(new undici.Agent({ connectTimeout: 2e3 }))
57
57
  }
58
58
 
59
+ v8.setHeapSnapshotNearHeapLimit(3)
60
+
59
61
  // Optimize some Node global defaults.
60
62
 
61
63
  Buffer.poolSize = 128 * 1024
@@ -940,6 +942,74 @@ export function makeApp(appConfig, onTerminate) {
940
942
  }
941
943
  }
942
944
 
945
+ if (appConfig.utils !== false && !cluster.isWorker) {
946
+ // TODO (fix): What about cluster?
947
+
948
+ const utilsBC = new BroadcastChannel('nxt:utils')
949
+
950
+ destroyers.unshift(() => utilsBC.close())
951
+
952
+ utilsBC.onmessage = ({ data }) => {
953
+ const { type } = data
954
+ try {
955
+ if (type === 'utils:gc') {
956
+ if (global.gc) {
957
+ global.gc()
958
+ logger.debug('gc')
959
+ } else {
960
+ logger.warn('gc not available')
961
+ }
962
+ } else if (type === 'utils:writeHeapSnapshot') {
963
+ const snapshotPath = `${os.tmpdir()}/heap-${Date.now()}-${serviceName}-${serviceModule}-${serviceInstanceId}-${serviceWorkerId}.heapsnapshot`
964
+ v8.writeHeapSnapshot(snapshotPath)
965
+ logger.info({ snapshotPath }, 'heap snapshot')
966
+ }
967
+ } catch (err) {
968
+ logger.error({ err, type })
969
+ }
970
+ }
971
+
972
+ if (isMainThread) {
973
+ // TODO (fix): Determinisitc port also in dev mode... hash of something?
974
+ const port =
975
+ appConfig.utils?.port ?? (isProduction ? 38604 : Math.floor(38000 + Math.random() * 10000))
976
+
977
+ const server = http
978
+ .createServer(async (req, res) => {
979
+ try {
980
+ if (req.method === 'POST' && req.url === '/gc') {
981
+ utilsBC.postMessage({ type: 'utils:gc' })
982
+ res.end()
983
+ return
984
+ }
985
+
986
+ if (req.method === 'POST' && req.url === '/writeHeapSnapshot') {
987
+ utilsBC.postMessage({ type: 'utils:writeHeapSnapshot' })
988
+ res.end()
989
+ return
990
+ }
991
+
992
+ res.statusCode = 404
993
+ res.end()
994
+ } catch (err) {
995
+ logger?.error({ err }, 'utils http error')
996
+
997
+ if (res.headersSent) {
998
+ res.destroy()
999
+ } else {
1000
+ res.statusCode = 500
1001
+ res.end()
1002
+ }
1003
+ }
1004
+ })
1005
+ .listen(port)
1006
+
1007
+ logger.debug({ port }, 'utils listening')
1008
+
1009
+ destroyers.unshift(() => new Promise((resolve) => server.close(resolve)))
1010
+ }
1011
+ }
1012
+
943
1013
  // TODO (fix): Deprecate
944
1014
  if (appConfig.http) {
945
1015
  const httpConfig = { ...appConfig.http, ...config.http }
package/couch.js CHANGED
@@ -958,7 +958,7 @@ class StreamOutput extends stream.Readable {
958
958
  if (/\[\s*$/.test(line)) {
959
959
  this.#state = 1
960
960
  } else {
961
- throw Object.assign('expected [ at end of line')
961
+ throw new Error('expected [ at end of line')
962
962
  }
963
963
  } else if (this.#state === 1) {
964
964
  if (/^\s*\]/.test(line)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "22.0.11",
3
+ "version": "22.1.0",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",