@netgreener/runtime 0.1.8 → 0.1.10

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 (114) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/TUTORIAL.md +120 -29
  3. package/dist/aggregator.d.ts +12 -2
  4. package/dist/aggregator.js +135 -8
  5. package/dist/analyze/buildResourceFindings.d.ts +4 -0
  6. package/dist/analyze/buildResourceFindings.js +17 -0
  7. package/dist/analyze/cli.d.ts +1 -0
  8. package/dist/analyze/cli.js +22 -1
  9. package/dist/analyze/declaredExternalApis.d.ts +31 -0
  10. package/dist/analyze/declaredExternalApis.js +174 -0
  11. package/dist/analyze/index.d.ts +1 -0
  12. package/dist/analyze/index.js +1 -0
  13. package/dist/auto.d.ts +7 -0
  14. package/dist/auto.js +8 -0
  15. package/dist/bullmq.d.ts +2 -2
  16. package/dist/bullmq.js +28 -58
  17. package/dist/carbonLedger.d.ts +28 -0
  18. package/dist/carbonLedger.js +52 -0
  19. package/dist/cgroupV2.d.ts +44 -0
  20. package/dist/cgroupV2.js +174 -0
  21. package/dist/cli/helpText.d.ts +3 -3
  22. package/dist/cli/helpText.js +42 -35
  23. package/dist/cli/netgreener.d.ts +7 -7
  24. package/dist/cli/netgreener.js +23 -16
  25. package/dist/cli/runCli.d.ts +20 -0
  26. package/dist/cli/runCli.js +249 -0
  27. package/dist/cli/runPreload.d.ts +5 -0
  28. package/dist/cli/runPreload.js +43 -0
  29. package/dist/collectorMetadata.d.ts +1 -1
  30. package/dist/collectorMetadata.js +2 -0
  31. package/dist/config.d.ts +2 -0
  32. package/dist/config.js +6 -2
  33. package/dist/exporter.d.ts +7 -2
  34. package/dist/exporter.js +12 -6
  35. package/dist/express.js +17 -6
  36. package/dist/externalApiMeter.d.ts +51 -2
  37. package/dist/externalApiMeter.js +379 -26
  38. package/dist/fastify.js +17 -6
  39. package/dist/index.d.ts +12 -2
  40. package/dist/index.js +11 -1
  41. package/dist/init.d.ts +30 -0
  42. package/dist/init.js +31 -0
  43. package/dist/lifecycle.d.ts +45 -0
  44. package/dist/lifecycle.js +112 -0
  45. package/dist/managedCollectorLauncher.d.ts +37 -0
  46. package/dist/managedCollectorLauncher.js +162 -0
  47. package/dist/mcpToolMeter.d.ts +101 -0
  48. package/dist/mcpToolMeter.js +496 -0
  49. package/dist/n4/deploymentMatrix.d.ts +9 -5
  50. package/dist/n4/deploymentMatrix.js +13 -9
  51. package/dist/n4/missingnessInventory.js +3 -3
  52. package/dist/next.d.ts +89 -0
  53. package/dist/next.js +328 -0
  54. package/dist/nextConfig.d.ts +34 -0
  55. package/dist/nextConfig.js +73 -0
  56. package/dist/nextEntryTransform.d.ts +9 -0
  57. package/dist/nextEntryTransform.js +181 -0
  58. package/dist/occupancyTracker.d.ts +27 -0
  59. package/dist/occupancyTracker.js +87 -0
  60. package/dist/optimize/apply.d.ts +38 -0
  61. package/dist/optimize/apply.js +173 -0
  62. package/dist/optimize/cli.d.ts +26 -0
  63. package/dist/optimize/cli.js +323 -0
  64. package/dist/optimize/index.d.ts +6 -0
  65. package/dist/optimize/index.js +5 -0
  66. package/dist/optimize/llmApply.d.ts +22 -0
  67. package/dist/optimize/llmApply.js +80 -0
  68. package/dist/optimize/llmSession.d.ts +88 -0
  69. package/dist/optimize/llmSession.js +135 -0
  70. package/dist/optimize/provider.d.ts +17 -0
  71. package/dist/optimize/provider.js +119 -0
  72. package/dist/optimize/safePatches.d.ts +18 -0
  73. package/dist/optimize/safePatches.js +247 -0
  74. package/dist/optimize/types.d.ts +79 -0
  75. package/dist/optimize/types.js +6 -0
  76. package/dist/optimize/verification.d.ts +16 -0
  77. package/dist/optimize/verification.js +165 -0
  78. package/dist/processResources.d.ts +5 -0
  79. package/dist/processResources.js +28 -4
  80. package/dist/processRuntime.js +7 -28
  81. package/dist/processTree.d.ts +47 -0
  82. package/dist/processTree.js +148 -0
  83. package/dist/runContextEnrichment.d.ts +11 -0
  84. package/dist/runContextEnrichment.js +92 -0
  85. package/dist/runtime.d.ts +36 -1
  86. package/dist/runtime.js +140 -17
  87. package/dist/sdk/client.d.ts +152 -0
  88. package/dist/sdk/client.js +396 -0
  89. package/dist/sdk/gitUrl.d.ts +6 -0
  90. package/dist/sdk/gitUrl.js +87 -0
  91. package/dist/sdk/index.d.ts +5 -0
  92. package/dist/sdk/index.js +5 -0
  93. package/dist/serverlessHints.d.ts +9 -6
  94. package/dist/serverlessHints.js +10 -7
  95. package/dist/taskMeter.d.ts +14 -0
  96. package/dist/taskMeter.js +53 -0
  97. package/dist/types.d.ts +43 -0
  98. package/examples/aca-container-gate/Dockerfile +20 -0
  99. package/examples/aca-container-gate/azure-container-app.yaml +27 -0
  100. package/examples/aca-container-gate/gate.env +7 -0
  101. package/examples/aca-container-gate/server.mjs +117 -0
  102. package/examples/azure-functions-gate/.funcignore +6 -0
  103. package/examples/azure-functions-gate/README.md +25 -0
  104. package/examples/azure-functions-gate/host.json +15 -0
  105. package/examples/azure-functions-gate/package-lock.json +161 -0
  106. package/examples/azure-functions-gate/package.json +14 -0
  107. package/examples/azure-functions-gate/src/functions/health.js +27 -0
  108. package/examples/express-local-live-smoke.mjs +127 -0
  109. package/examples/extractRunId.mjs +11 -0
  110. package/examples/next-pilot-dry-run.mjs +32 -0
  111. package/examples/serverless-thin-gate.mjs +89 -0
  112. package/fixtures/run-smoke/hello.mjs +4 -0
  113. package/loaders/next-entry-loader.cjs +14 -0
  114. package/package.json +25 -2
@@ -0,0 +1,127 @@
1
+ /**
2
+ * Laptop Express smoke → dry-run by default; live upload when credentials set.
3
+ *
4
+ * Dry-run (no cloud):
5
+ * npm run example:express-local-live
6
+ *
7
+ * Live (needs token + project; uses your local or prod api_server):
8
+ * $env:NETGREENER_RUNTIME_DRY_RUN="0"
9
+ * $env:NETGREENER_API_URL="http://127.0.0.1:8000" # or https://core-api.netgreener.com
10
+ * $env:NETGREENER_TOKEN="ngs_..."
11
+ * $env:NETGREENER_PROJECT_ID="97"
12
+ * $env:NETGREENER_SERVICE_RUNTIME="1"
13
+ * npm run example:express-local-live
14
+ *
15
+ * Does not flip EXPORT_MODE. Prints run_id when live upload succeeds.
16
+ */
17
+
18
+ import { createServer } from 'node:http'
19
+
20
+ import { extractPersistedRunId } from '../dist/dogfood/tenantDogfoodGates.js'
21
+ import {
22
+ getExpressRuntime,
23
+ loadRuntimeConfig,
24
+ netgreenerExpressMiddleware,
25
+ } from '../dist/index.js'
26
+
27
+ const dryRun =
28
+ (process.env.NETGREENER_RUNTIME_DRY_RUN || '1').trim() !== '0' &&
29
+ (process.env.NETGREENER_RUNTIME_DRY_RUN || '1').trim().toLowerCase() !== 'false'
30
+ const live = !dryRun
31
+
32
+ if (live) {
33
+ const token = (process.env.NETGREENER_TOKEN || '').trim()
34
+ const projectId = (process.env.NETGREENER_PROJECT_ID || '').trim()
35
+ if (!token || token.startsWith('ngs_local') || !projectId) {
36
+ console.error(
37
+ 'LIVE mode requires real NETGREENER_TOKEN and NETGREENER_PROJECT_ID (not ngs_local_*).',
38
+ )
39
+ process.exit(2)
40
+ }
41
+ }
42
+
43
+ const config = loadRuntimeConfig({
44
+ ...process.env,
45
+ NETGREENER_SERVICE_RUNTIME: '1',
46
+ NETGREENER_TOKEN: process.env.NETGREENER_TOKEN || 'ngs_local_express_smoke',
47
+ NETGREENER_PROJECT_ID: process.env.NETGREENER_PROJECT_ID || '97',
48
+ NETGREENER_RUNTIME_DRY_RUN: dryRun ? '1' : '0',
49
+ NETGREENER_API_URL:
50
+ process.env.NETGREENER_API_URL ||
51
+ (live ? 'https://core-api.netgreener.com' : 'http://127.0.0.1:8000'),
52
+ })
53
+
54
+ const mw = netgreenerExpressMiddleware({
55
+ config,
56
+ onFlush: (result) => {
57
+ console.log(
58
+ 'flush:',
59
+ result?.ok
60
+ ? result.dryRun
61
+ ? 'dry-run ok'
62
+ : `HTTP ${result.status}`
63
+ : result?.error || 'failed',
64
+ )
65
+ },
66
+ })
67
+
68
+ const server = createServer((req, res) => {
69
+ const fakeReq = {
70
+ method: req.method,
71
+ path: (req.url || '/').split('?')[0],
72
+ url: req.url,
73
+ headers: req.headers,
74
+ }
75
+ const fakeRes = {
76
+ statusCode: 200,
77
+ on(event, fn) {
78
+ if (event === 'finish' || event === 'close') res.on(event, fn)
79
+ return this
80
+ },
81
+ }
82
+ mw(fakeReq, fakeRes, () => {
83
+ res.statusCode = 200
84
+ res.setHeader('content-type', 'application/json')
85
+ res.end(JSON.stringify({ ok: true, path: fakeReq.path, mode: live ? 'live' : 'dry-run' }))
86
+ })
87
+ })
88
+
89
+ await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve))
90
+ const addr = server.address()
91
+ const bound = typeof addr === 'object' && addr ? addr.port : 0
92
+
93
+ console.log(
94
+ `mode: ${live ? 'LIVE upload' : 'DRY-RUN'} http://127.0.0.1:${bound}/smoke project=${process.env.NETGREENER_PROJECT_ID || '97'}`,
95
+ )
96
+
97
+ const resp = await fetch(`http://127.0.0.1:${bound}/smoke`)
98
+ const body = await resp.json()
99
+ console.log('response:', body)
100
+
101
+ const flushResult = await getExpressRuntime().flush('manual')
102
+ server.close()
103
+
104
+ if (!flushResult?.ok) {
105
+ console.error('flush failed:', flushResult?.error || flushResult)
106
+ process.exit(1)
107
+ }
108
+
109
+ if (live) {
110
+ if (flushResult.dryRun) {
111
+ console.error('LIVE smoke returned dry-run — check NETGREENER_RUNTIME_DRY_RUN=0')
112
+ process.exit(1)
113
+ }
114
+ const runId = extractPersistedRunId(flushResult.body)
115
+ if (runId == null) {
116
+ console.warn('Upload ok but run_id not parsed — check dashboard for the latest run.')
117
+ console.log(JSON.stringify(flushResult.body, null, 2))
118
+ } else {
119
+ console.log(`PASS run_id=${runId}`)
120
+ }
121
+ } else {
122
+ if (!flushResult.dryRun) {
123
+ console.error('expected dry-run result')
124
+ process.exit(1)
125
+ }
126
+ console.log('Dry-run PASS. Set NETGREENER_RUNTIME_DRY_RUN=0 + token/project for live run_id.')
127
+ }
@@ -0,0 +1,11 @@
1
+ /** Best-effort run_id extraction from RunSession upload JSON body. */
2
+ export function extractRunId(body) {
3
+ if (body == null || typeof body !== 'object') return null
4
+ const o = body
5
+ for (const key of ['id', 'run_id', 'runId', 'run_session_id']) {
6
+ const v = o[key]
7
+ if (typeof v === 'number' && Number.isFinite(v)) return String(v)
8
+ if (typeof v === 'string' && v.trim()) return v.trim()
9
+ }
10
+ return null
11
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Wave 3 Next.js adapter dry-run (no Next server required).
3
+ */
4
+ import {
5
+ getNextRuntime,
6
+ loadRuntimeConfig,
7
+ withNetGreenerRoute,
8
+ _resetRuntimeForTests,
9
+ } from '../dist/index.js'
10
+
11
+ _resetRuntimeForTests()
12
+ const config = loadRuntimeConfig({
13
+ NETGREENER_SERVICE_RUNTIME: '1',
14
+ NETGREENER_TOKEN: 'ngs_local_dry_run',
15
+ NETGREENER_PROJECT_ID: '1',
16
+ NETGREENER_RUNTIME_DRY_RUN: '1',
17
+ })
18
+
19
+ const POST = withNetGreenerRoute(
20
+ async () => new Response(JSON.stringify({ ok: true }), { status: 200 }),
21
+ { config, routeTemplate: '/api/pilot' },
22
+ )
23
+
24
+ await POST({ method: 'POST', url: 'https://local/api/pilot', headers: {} })
25
+ const result = await getNextRuntime().flush('manual')
26
+ if (!result?.ok || !result.dryRun) {
27
+ console.error(result)
28
+ process.exit(1)
29
+ }
30
+ console.log('next pilot dry-run ok')
31
+ console.log(JSON.stringify(result.body?.session_metadata?.service_runtime_v0 || {}, null, 2))
32
+ _resetRuntimeForTests()
@@ -0,0 +1,89 @@
1
+ /**
2
+ * Wave 4 serverless thin gate — Vercel-shaped env, ephemeral flush, dry-run.
3
+ */
4
+ import { mkdirSync, writeFileSync } from 'node:fs'
5
+ import { dirname, join } from 'node:path'
6
+ import { fileURLToPath } from 'node:url'
7
+
8
+ import {
9
+ NetGreener,
10
+ _resetRuntimeForTests,
11
+ loadRuntimeConfig,
12
+ resolveLifecycle,
13
+ exportModeFromEnv,
14
+ } from '../dist/index.js'
15
+ import { extractRunId } from './extractRunId.mjs'
16
+
17
+ const __dirname = dirname(fileURLToPath(import.meta.url))
18
+ const artifactDir = join(__dirname, '../artifacts/serverless-thin-gate')
19
+
20
+ _resetRuntimeForTests()
21
+ process.env.VERCEL = '1'
22
+ process.env.VERCEL_ENV = 'preview'
23
+ process.env.NETGREENER_SERVICE_RUNTIME = '1'
24
+ process.env.NETGREENER_TOKEN = 'ngs_local_dry_run'
25
+ process.env.NETGREENER_PROJECT_ID = '1'
26
+ process.env.NETGREENER_RUNTIME_DRY_RUN = '1'
27
+ delete process.env.NETGREENER_EXPORT_MODE
28
+
29
+ const env = { ...process.env }
30
+ const lifecycle = resolveLifecycle({ mode: 'auto', env })
31
+ const exportMode = exportModeFromEnv(env, { lifecycle: lifecycle.lifecycle })
32
+ const config = loadRuntimeConfig(env)
33
+ const runtime = NetGreener.init({ config, runtime: 'auto' })
34
+
35
+ runtime.beginRequest()
36
+ runtime.recordHttp({
37
+ method: 'POST',
38
+ path: '/api/serverless-pilot',
39
+ statusCode: 200,
40
+ durationMs: 12,
41
+ cpuTimeMs: 1,
42
+ peakRssKb: 50_000,
43
+ })
44
+ runtime.endRequest()
45
+ const result = await NetGreener.flushForInvokeEnd()
46
+
47
+ mkdirSync(artifactDir, { recursive: true })
48
+ const runId = result?.ok && !result.dryRun ? extractRunId(result.body) : null
49
+ const summary = {
50
+ gate: 'CHK-R6.serverless-degraded',
51
+ label: 'Serverless Runtime (thin) pilot',
52
+ claim_ready: Boolean(runId),
53
+ lifecycle: lifecycle.lifecycle,
54
+ platform: lifecycle.platform,
55
+ export_mode: exportMode,
56
+ dry_run: Boolean(result?.dryRun),
57
+ ok: Boolean(result?.ok),
58
+ run_id: runId,
59
+ flushed_at: new Date().toISOString(),
60
+ }
61
+ writeFileSync(join(artifactDir, 'latest.json'), JSON.stringify({ summary, result }, null, 2))
62
+ writeFileSync(
63
+ join(artifactDir, 'SUMMARY.md'),
64
+ [
65
+ '# Serverless thin gate evidence',
66
+ '',
67
+ `- check_id: \`CHK-R6.serverless-degraded\``,
68
+ `- label: pilot (not production until live run_id + new approval_ref)`,
69
+ `- lifecycle: \`${lifecycle.lifecycle}\` (must be ephemeral)`,
70
+ `- platform: \`${lifecycle.platform}\``,
71
+ `- export_mode: \`${exportMode}\` (prefer thin when unset)`,
72
+ `- dry_run: \`${summary.dry_run}\``,
73
+ `- ok: \`${summary.ok}\``,
74
+ `- run_id: \`${runId ?? '(none — dry-run or upload body without id)'}\``,
75
+ `- claim_ready: \`${summary.claim_ready}\``,
76
+ `- flushed_at: \`${summary.flushed_at}\``,
77
+ '',
78
+ ].join('\n'),
79
+ )
80
+
81
+ console.log(JSON.stringify(summary, null, 2))
82
+ _resetRuntimeForTests()
83
+ if (
84
+ !result?.ok ||
85
+ lifecycle.lifecycle !== 'ephemeral' ||
86
+ exportMode !== 'thin'
87
+ ) {
88
+ process.exit(1)
89
+ }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Tiny script for ``netgreener run`` smoke (prints one line).
3
+ */
4
+ console.log('netgreener-run-smoke-ok')
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Webpack / Turbopack loader. Wraps Next.js route, page, and server-action
3
+ * exports. No-ops for every other module.
4
+ */
5
+ const { transformNextEntrySource } = require('../dist/nextEntryTransform.js')
6
+
7
+ module.exports = function netgreenerNextEntryLoader(source) {
8
+ const filename = (this && this.resourcePath) || ''
9
+ try {
10
+ return transformNextEntrySource(String(source), filename)
11
+ } catch {
12
+ return source
13
+ }
14
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@netgreener/runtime",
3
- "version": "0.1.8",
4
- "description": "NetGreener Node.js Runtime beta — thin adapter uploading RunSessions to existing api_server.",
3
+ "version": "0.1.10",
4
+ "description": "NetGreener Node.js Runtime — Express/Fastify/Nest/Next/process adapter uploading RunSessions to api_server.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
@@ -30,17 +30,31 @@
30
30
  "types": "./dist/nest.d.ts",
31
31
  "import": "./dist/nest.js"
32
32
  },
33
+ "./next": {
34
+ "types": "./dist/next.d.ts",
35
+ "import": "./dist/next.js"
36
+ },
37
+ "./auto": {
38
+ "types": "./dist/auto.d.ts",
39
+ "import": "./dist/auto.js"
40
+ },
33
41
  "./analyze": {
34
42
  "types": "./dist/analyze/index.d.ts",
35
43
  "import": "./dist/analyze/index.js"
44
+ },
45
+ "./sdk": {
46
+ "types": "./dist/sdk/index.d.ts",
47
+ "import": "./dist/sdk/index.js"
36
48
  }
37
49
  },
38
50
  "files": [
39
51
  "dist",
52
+ "loaders",
40
53
  "!dist/tests",
41
54
  "!dist/tests/**",
42
55
  "fixtures",
43
56
  "examples",
57
+ "!examples/**/node_modules/**",
44
58
  "README.md",
45
59
  "TUTORIAL.md",
46
60
  "CHANGELOG.md"
@@ -53,6 +67,10 @@
53
67
  "conformance:check": "npm run build && npm run conformance:test",
54
68
  "conformance:run": "npm run conformance:check && node scripts/run-contract-conformance.mjs",
55
69
  "example:dry-run": "npm run build && node examples/express-dry-run.mjs",
70
+ "example:express-local-live": "npm run build && node examples/express-local-live-smoke.mjs",
71
+ "example:aca-container-gate": "npm run build && node --env-file=examples/aca-container-gate/gate.env examples/aca-container-gate/server.mjs",
72
+ "example:next-pilot": "npm run build && node examples/next-pilot-dry-run.mjs",
73
+ "example:serverless-thin-gate": "npm run build && node examples/serverless-thin-gate.mjs",
56
74
  "example:dogfood-tenant": "npm run build && node examples/tenant-dogfood.mjs",
57
75
  "example:bullmq-live-smoke": "npm run build && node examples/bullmq-live-smoke.mjs",
58
76
  "example:bullmq-mp3-gate": "npm run build && node examples/bullmq-mp3-worker-gate.mjs",
@@ -66,10 +84,13 @@
66
84
  "example:production-prep-smoke": "npm run build && node scripts/production-prep-smoke.mjs",
67
85
  "example:publish-prep": "npm run build && node scripts/publish-prep.mjs",
68
86
  "example:sbom-prep": "node scripts/sbom-prep.mjs",
87
+ "example:overhead-measure": "npm run build && node scripts/overhead-measure.mjs",
69
88
  "example:push-github-public": "node scripts/push-github-public.mjs",
70
89
  "example:push-github-release": "node scripts/push-github-public.mjs --push-release-tag",
71
90
  "example:retain-dry-run": "npm run build && node examples/retain-dry-run.mjs",
72
91
  "example:dry-run-artifact-smoke": "npm run build && node scripts/dry-run-artifact-smoke.mjs",
92
+ "example:n5b-linux-gate": "npm run build && node scripts/n5b-linux-gate.mjs",
93
+ "example:n5b-flush-dogfood": "npm run build && node scripts/n5b-flush-dogfood.mjs",
73
94
  "test:observation": "npm run build && node --test conformance/observation-v1.test.mjs"
74
95
  },
75
96
  "engines": {
@@ -90,6 +111,8 @@
90
111
  "carbon",
91
112
  "express",
92
113
  "fastify",
114
+ "next",
115
+ "typescript",
93
116
  "observability"
94
117
  ],
95
118
  "license": "UNLICENSED",