@pikku/inspector 0.12.75 → 0.12.82

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 (223) hide show
  1. package/CHANGELOG.md +357 -0
  2. package/dist/add/add-analytics.d.ts +9 -0
  3. package/dist/add/add-analytics.js +178 -0
  4. package/dist/add/add-auth.js +0 -54
  5. package/dist/add/add-feature-flag.d.ts +2 -0
  6. package/dist/add/add-feature-flag.js +162 -0
  7. package/dist/add/add-middleware.js +66 -42
  8. package/dist/add/add-wire-addon.js +7 -4
  9. package/dist/add/add-wire-remote-addon.js +1 -0
  10. package/dist/error-codes.d.ts +1 -0
  11. package/dist/error-codes.js +1 -0
  12. package/dist/index.d.ts +3 -1
  13. package/dist/index.js +3 -1
  14. package/dist/inspector.js +5 -0
  15. package/dist/surface/collect-surface.d.ts +58 -0
  16. package/dist/surface/collect-surface.js +613 -0
  17. package/dist/types.d.ts +36 -12
  18. package/dist/utils/filter-inspector-state.js +14 -0
  19. package/dist/utils/load-addon-functions-meta.js +24 -11
  20. package/dist/utils/post-process.js +15 -0
  21. package/dist/utils/read-module-specifiers.d.ts +8 -0
  22. package/dist/utils/read-module-specifiers.js +23 -0
  23. package/dist/utils/read-tsconfig-out-dir.d.ts +5 -0
  24. package/dist/utils/read-tsconfig-out-dir.js +12 -0
  25. package/dist/utils/resolve-deploy-target.d.ts +7 -0
  26. package/dist/utils/resolve-deploy-target.js +15 -7
  27. package/dist/utils/serialize-inspector-state.d.ts +6 -0
  28. package/dist/utils/serialize-inspector-state.js +10 -0
  29. package/dist/visit.js +4 -0
  30. package/package.json +16 -6
  31. package/run-tests.sh +21 -6
  32. package/src/add/add-addon-bans.ts +0 -85
  33. package/src/add/add-agent-scorer.test.ts +0 -197
  34. package/src/add/add-agent-scorer.ts +0 -83
  35. package/src/add/add-agent.test.ts +0 -87
  36. package/src/add/add-agent.ts +0 -638
  37. package/src/add/add-approval-description.ts +0 -76
  38. package/src/add/add-auth.test.ts +0 -791
  39. package/src/add/add-auth.ts +0 -424
  40. package/src/add/add-channel.ts +0 -778
  41. package/src/add/add-cli-renderers.test.ts +0 -74
  42. package/src/add/add-cli.ts +0 -1006
  43. package/src/add/add-credential.ts +0 -186
  44. package/src/add/add-feature.test.ts +0 -306
  45. package/src/add/add-feature.ts +0 -253
  46. package/src/add/add-file-extends-core-type.ts +0 -66
  47. package/src/add/add-file-with-config.ts +0 -45
  48. package/src/add/add-file-with-factory.test.ts +0 -134
  49. package/src/add/add-file-with-factory.ts +0 -288
  50. package/src/add/add-functions.test.ts +0 -743
  51. package/src/add/add-functions.ts +0 -1548
  52. package/src/add/add-gateway.ts +0 -143
  53. package/src/add/add-http-route-ref.test.ts +0 -149
  54. package/src/add/add-http-route.ts +0 -530
  55. package/src/add/add-http-routes.ts +0 -471
  56. package/src/add/add-keyed-wiring.ts +0 -180
  57. package/src/add/add-mcp-prompt.ts +0 -157
  58. package/src/add/add-mcp-resource.ts +0 -175
  59. package/src/add/add-middleware.test.ts +0 -196
  60. package/src/add/add-middleware.ts +0 -879
  61. package/src/add/add-permission-auth.test.ts +0 -59
  62. package/src/add/add-permission.ts +0 -311
  63. package/src/add/add-personas.test.ts +0 -406
  64. package/src/add/add-personas.ts +0 -287
  65. package/src/add/add-queue-worker.ts +0 -117
  66. package/src/add/add-rpc-invocations.test.ts +0 -287
  67. package/src/add/add-rpc-invocations.ts +0 -221
  68. package/src/add/add-scenario-step.test.ts +0 -509
  69. package/src/add/add-schedule.ts +0 -120
  70. package/src/add/add-scope.test.ts +0 -419
  71. package/src/add/add-scope.ts +0 -221
  72. package/src/add/add-secret.ts +0 -10
  73. package/src/add/add-subject-scenario-steps.test.ts +0 -161
  74. package/src/add/add-system-role.test.ts +0 -297
  75. package/src/add/add-system-role.ts +0 -162
  76. package/src/add/add-trigger.ts +0 -165
  77. package/src/add/add-variable.ts +0 -10
  78. package/src/add/add-wire-addon.test.ts +0 -185
  79. package/src/add/add-wire-addon.ts +0 -146
  80. package/src/add/add-wire-remote-addon.ts +0 -80
  81. package/src/add/add-workflow-edge-cases.test.ts +0 -201
  82. package/src/add/add-workflow-fanout.test.ts +0 -166
  83. package/src/add/add-workflow-graph-foreach.test.ts +0 -174
  84. package/src/add/add-workflow-graph-input.test.ts +0 -94
  85. package/src/add/add-workflow-graph-notes.test.ts +0 -74
  86. package/src/add/add-workflow-graph.ts +0 -644
  87. package/src/add/add-workflow-on-error.test.ts +0 -118
  88. package/src/add/add-workflow-return-switch.test.ts +0 -121
  89. package/src/add/add-workflow-silent-drops.test.ts +0 -261
  90. package/src/add/add-workflow-sleep-expression.test.ts +0 -96
  91. package/src/add/add-workflow.test.ts +0 -155
  92. package/src/add/add-workflow.ts +0 -482
  93. package/src/add/addon-bans.test.ts +0 -121
  94. package/src/add/addon-contracts.test.ts +0 -221
  95. package/src/add/allow-escape-hatches.test.ts +0 -144
  96. package/src/add/audit-meta.test.ts +0 -75
  97. package/src/add/classified-sink-check.test.ts +0 -347
  98. package/src/add/config-value-metadata.test.ts +0 -263
  99. package/src/add/dynamic-import-check.test.ts +0 -119
  100. package/src/add/expect-eventually-scenario-only.test.ts +0 -108
  101. package/src/add/function-body-span.test.ts +0 -96
  102. package/src/add/inline-wiring-function.test.ts +0 -104
  103. package/src/add/pii-check.test.ts +0 -258
  104. package/src/add/rpc-type-cast.test.ts +0 -123
  105. package/src/add/scenario-instrumentation.test.ts +0 -89
  106. package/src/add/scenario-instrumentation.ts +0 -42
  107. package/src/add/scenario-marker.test.ts +0 -89
  108. package/src/add/scenario-not-extractable.test.ts +0 -159
  109. package/src/add/scenario-services.test.ts +0 -103
  110. package/src/add/scenario-skip.test.ts +0 -83
  111. package/src/add/validate-workflow-graph-addons.test.ts +0 -102
  112. package/src/add/wire-name-literal.test.ts +0 -117
  113. package/src/error-codes-emitted.test.ts +0 -76
  114. package/src/error-codes.ts +0 -145
  115. package/src/index.ts +0 -66
  116. package/src/inspector.ts +0 -575
  117. package/src/schema-generator.ts +0 -1
  118. package/src/source-file-cache.ts +0 -73
  119. package/src/tsconfig-paths.test.ts +0 -90
  120. package/src/types-map.ts +0 -141
  121. package/src/types.ts +0 -775
  122. package/src/utils/annotate-http-route-auth.test.ts +0 -161
  123. package/src/utils/annotate-http-route-auth.ts +0 -53
  124. package/src/utils/check-classified-sinks.ts +0 -215
  125. package/src/utils/check-pii-output.ts +0 -120
  126. package/src/utils/compute-diagnostics.test.ts +0 -106
  127. package/src/utils/compute-required-schemas.ts +0 -48
  128. package/src/utils/contract-hashes.test.ts +0 -553
  129. package/src/utils/contract-hashes.ts +0 -387
  130. package/src/utils/custom-types-generator.test.ts +0 -124
  131. package/src/utils/custom-types-generator.ts +0 -150
  132. package/src/utils/detect-schema-vendor.test.ts +0 -123
  133. package/src/utils/detect-schema-vendor.ts +0 -120
  134. package/src/utils/does-type-extend-core-type.ts +0 -53
  135. package/src/utils/ensure-function-metadata.ts +0 -385
  136. package/src/utils/extract-function-name.ts +0 -612
  137. package/src/utils/extract-function-node.ts +0 -70
  138. package/src/utils/extract-node-value.test.ts +0 -117
  139. package/src/utils/extract-node-value.ts +0 -172
  140. package/src/utils/extract-secret-usage.test.ts +0 -81
  141. package/src/utils/extract-secret-usage.ts +0 -73
  142. package/src/utils/extract-services.ts +0 -70
  143. package/src/utils/extract-surface-usage.test.ts +0 -147
  144. package/src/utils/extract-surface-usage.ts +0 -121
  145. package/src/utils/filter-inspector-state.test.ts +0 -2232
  146. package/src/utils/filter-inspector-state.ts +0 -1298
  147. package/src/utils/filter-utils.test.ts +0 -843
  148. package/src/utils/filter-utils.ts +0 -161
  149. package/src/utils/find-root-dir.ts +0 -68
  150. package/src/utils/find-zod-transform.test.ts +0 -70
  151. package/src/utils/find-zod-transform.ts +0 -103
  152. package/src/utils/get-exported-variable-name.ts +0 -48
  153. package/src/utils/get-files-and-methods.ts +0 -157
  154. package/src/utils/get-property-value.test.ts +0 -141
  155. package/src/utils/get-property-value.ts +0 -331
  156. package/src/utils/hash.ts +0 -26
  157. package/src/utils/literal-properties.ts +0 -97
  158. package/src/utils/load-addon-functions-meta.test.ts +0 -157
  159. package/src/utils/load-addon-functions-meta.ts +0 -508
  160. package/src/utils/load-addon-scopes.test.ts +0 -138
  161. package/src/utils/middleware.test.ts +0 -204
  162. package/src/utils/middleware.ts +0 -303
  163. package/src/utils/nested-project-filter.test.ts +0 -53
  164. package/src/utils/nested-project-filter.ts +0 -28
  165. package/src/utils/permissions-shorthand.test.ts +0 -79
  166. package/src/utils/permissions.test.ts +0 -125
  167. package/src/utils/permissions.ts +0 -129
  168. package/src/utils/post-process.test.ts +0 -1094
  169. package/src/utils/post-process.ts +0 -1604
  170. package/src/utils/resolve-addon-package.ts +0 -106
  171. package/src/utils/resolve-core-type.test.ts +0 -142
  172. package/src/utils/resolve-core-type.ts +0 -35
  173. package/src/utils/resolve-deploy-target.test.ts +0 -135
  174. package/src/utils/resolve-deploy-target.ts +0 -65
  175. package/src/utils/resolve-function-meta.ts +0 -25
  176. package/src/utils/resolve-function-types.ts +0 -42
  177. package/src/utils/resolve-identifier.ts +0 -46
  178. package/src/utils/resolve-ref-contract.ts +0 -71
  179. package/src/utils/resolve-versions.test.ts +0 -499
  180. package/src/utils/resolve-versions.ts +0 -228
  181. package/src/utils/schema-cache.test.ts +0 -124
  182. package/src/utils/schema-generator.test.ts +0 -272
  183. package/src/utils/schema-generator.ts +0 -780
  184. package/src/utils/secret-alias-services.test.ts +0 -146
  185. package/src/utils/secret-alias-services.ts +0 -58
  186. package/src/utils/serialize-inspector-state.ts +0 -742
  187. package/src/utils/serialize-mcp-json.ts +0 -150
  188. package/src/utils/serialize-middleware-groups-meta.ts +0 -36
  189. package/src/utils/serialize-openapi-json.ts +0 -277
  190. package/src/utils/serialize-permissions-groups-meta.ts +0 -7
  191. package/src/utils/single-declaration.ts +0 -57
  192. package/src/utils/type-utils.ts +0 -145
  193. package/src/utils/validate-auth-sessionless.ts +0 -30
  194. package/src/utils/validate-exposed-functions-gated.test.ts +0 -230
  195. package/src/utils/validate-exposed-functions-gated.ts +0 -103
  196. package/src/utils/validate-tags-resolve-to-middleware.test.ts +0 -108
  197. package/src/utils/validate-tags-resolve-to-middleware.ts +0 -48
  198. package/src/utils/workflow/derive-workflow-plan.test.ts +0 -178
  199. package/src/utils/workflow/derive-workflow-plan.ts +0 -117
  200. package/src/utils/workflow/dsl/deserialize-dsl-workflow.ts +0 -1465
  201. package/src/utils/workflow/dsl/deserialize-fanout-body.test.ts +0 -71
  202. package/src/utils/workflow/dsl/deserialize-fanout-fidelity.test.ts +0 -99
  203. package/src/utils/workflow/dsl/deserialize-graph-next.test.ts +0 -161
  204. package/src/utils/workflow/dsl/deserialize-roundtrip-bindings.test.ts +0 -121
  205. package/src/utils/workflow/dsl/deserialize-roundtrip-values.test.ts +0 -116
  206. package/src/utils/workflow/dsl/deserialize-roundtrip.test.ts +0 -145
  207. package/src/utils/workflow/dsl/extract-approval-policy.test.ts +0 -71
  208. package/src/utils/workflow/dsl/extract-dsl-workflow.ts +0 -2254
  209. package/src/utils/workflow/dsl/index.ts +0 -11
  210. package/src/utils/workflow/dsl/patterns.ts +0 -440
  211. package/src/utils/workflow/dsl/validation.ts +0 -203
  212. package/src/utils/workflow/graph/convert-dsl-to-graph-skip.test.ts +0 -35
  213. package/src/utils/workflow/graph/convert-dsl-to-graph.ts +0 -543
  214. package/src/utils/workflow/graph/fanout-node-identity.test.ts +0 -105
  215. package/src/utils/workflow/graph/finalize-workflow-wires.ts +0 -310
  216. package/src/utils/workflow/graph/finalize-workflows.test.ts +0 -50
  217. package/src/utils/workflow/graph/finalize-workflows.ts +0 -130
  218. package/src/utils/workflow/graph/index.ts +0 -28
  219. package/src/utils/workflow/graph/serialize-workflow-graph.ts +0 -240
  220. package/src/utils/workflow/graph/workflow-graph.types.ts +0 -281
  221. package/src/visit.ts +0 -181
  222. package/tsconfig.json +0 -19
  223. package/tsconfig.tsbuildinfo +0 -1
@@ -1,119 +0,0 @@
1
- import { strict as assert } from 'assert'
2
- import { describe, test } from 'node:test'
3
- import { mkdtemp, rm, writeFile } from 'node:fs/promises'
4
- import { tmpdir } from 'node:os'
5
- import { join } from 'node:path'
6
- import { inspect } from '../inspector.js'
7
- import { ErrorCode } from '../error-codes.js'
8
- import type { InspectorLogger } from '../types.js'
9
-
10
- const logger: InspectorLogger = {
11
- debug: () => {},
12
- info: () => {},
13
- warn: () => {},
14
- error: () => {},
15
- diagnostic: () => {},
16
- critical: () => {},
17
- hasCriticalErrors: () => false,
18
- }
19
-
20
- async function inspectSource(source: string) {
21
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-dynimport-'))
22
- const file = join(rootDir, 'my.function.ts')
23
- await writeFile(file, source)
24
- try {
25
- return await inspect(logger, [file], { rootDir })
26
- } finally {
27
- await rm(rootDir, { recursive: true, force: true })
28
- }
29
- }
30
-
31
- describe('dynamic import in function bodies', () => {
32
- test('flags a function whose body does `await import(...)`', async () => {
33
- const state = await inspectSource(
34
- [
35
- "import { pikkuSessionlessFunc } from '@pikku/core'",
36
- '',
37
- 'export const greedy = pikkuSessionlessFunc({',
38
- ' func: async ({ logger }) => {',
39
- " const { readFile } = await import('node:fs/promises')",
40
- ' return { readFile: typeof readFile }',
41
- ' },',
42
- '})',
43
- ].join('\n')
44
- )
45
- const meta = (state.functions.meta as any).greedy
46
- assert.ok(meta, 'greedy should be inspected')
47
- assert.ok(state.functions.dynamicImportIds.has(meta.pikkuFuncId))
48
- const diagnostic = state.diagnostics.find(
49
- (d) => d.code === ErrorCode.FUNCTION_DYNAMIC_IMPORT
50
- )
51
- assert.ok(
52
- diagnostic,
53
- 'expected a FUNCTION_DYNAMIC_IMPORT diagnostic for the function'
54
- )
55
- })
56
-
57
- test('flags a nested `import(...)` call inside a callback', async () => {
58
- const state = await inspectSource(
59
- [
60
- "import { pikkuSessionlessFunc } from '@pikku/core'",
61
- '',
62
- 'export const nested = pikkuSessionlessFunc({',
63
- ' func: async ({ logger }) => {',
64
- ' await Promise.all([',
65
- " import('node:os').then((m) => m.hostname()),",
66
- ' ])',
67
- ' return { ok: true }',
68
- ' },',
69
- '})',
70
- ].join('\n')
71
- )
72
- const meta = (state.functions.meta as any).nested
73
- assert.ok(state.functions.dynamicImportIds.has(meta.pikkuFuncId))
74
- assert.ok(
75
- state.diagnostics.some(
76
- (d) => d.code === ErrorCode.FUNCTION_DYNAMIC_IMPORT
77
- )
78
- )
79
- })
80
-
81
- test('does NOT flag a function with only static imports', async () => {
82
- const state = await inspectSource(
83
- [
84
- "import { pikkuSessionlessFunc } from '@pikku/core'",
85
- '',
86
- 'export const clean = pikkuSessionlessFunc({',
87
- ' func: async ({ logger }, data: { n: number }) => {',
88
- ' return { doubled: data.n * 2 }',
89
- ' },',
90
- '})',
91
- ].join('\n')
92
- )
93
- const meta = (state.functions.meta as any).clean
94
- assert.ok(meta, 'clean should be inspected')
95
- assert.ok(!state.functions.dynamicImportIds.has(meta.pikkuFuncId))
96
- assert.ok(
97
- !state.diagnostics.some(
98
- (d) => d.code === ErrorCode.FUNCTION_DYNAMIC_IMPORT
99
- )
100
- )
101
- })
102
-
103
- test('does NOT flag a type-only import position `import("x").T`', async () => {
104
- const state = await inspectSource(
105
- [
106
- "import { pikkuSessionlessFunc } from '@pikku/core'",
107
- '',
108
- 'export const typeOnly = pikkuSessionlessFunc({',
109
- " func: async ({ logger }, data: import('node:buffer').Blob) => {",
110
- ' return { size: data.size }',
111
- ' },',
112
- '})',
113
- ].join('\n')
114
- )
115
- const meta = (state.functions.meta as any).typeOnly
116
- assert.ok(meta, 'typeOnly should be inspected')
117
- assert.ok(!state.functions.dynamicImportIds.has(meta.pikkuFuncId))
118
- })
119
- })
@@ -1,108 +0,0 @@
1
- import { strict as assert } from 'assert'
2
- import { describe, test } from 'node:test'
3
- import { mkdtemp, rm, writeFile } from 'node:fs/promises'
4
- import { tmpdir } from 'node:os'
5
- import { join } from 'node:path'
6
- import { inspect } from '../inspector.js'
7
- import type { InspectorLogger } from '../types.js'
8
-
9
- function makeLogger(
10
- criticals: Array<{ code: string; message: string }>
11
- ): InspectorLogger {
12
- return {
13
- debug: () => {},
14
- info: () => {},
15
- warn: () => {},
16
- error: () => {},
17
- diagnostic: ({ code, message }) => {
18
- criticals.push({ code, message })
19
- },
20
- critical: (code: any, message: string) => {
21
- criticals.push({ code, message })
22
- },
23
- hasCriticalErrors: () => criticals.length > 0,
24
- }
25
- }
26
-
27
- const stepFile = (dir: string) => join(dir, 'my.steps.ts')
28
- const stepSource = [
29
- "import { pikkuSessionlessFunc } from '@pikku/core'",
30
- 'export const getTodos = pikkuSessionlessFunc({',
31
- ' func: async ({ logger }) => [{ done: true }],',
32
- '})',
33
- ].join('\n')
34
-
35
- describe('expectEventually is scenario-only', () => {
36
- test('pikkuWorkflowFunc calling expectEventually is a critical error pointing at pikkuScenario', async () => {
37
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-ee-wf-'))
38
- const wfFile = join(rootDir, 'my.workflow.ts')
39
- await writeFile(stepFile(rootDir), stepSource)
40
- await writeFile(
41
- wfFile,
42
- [
43
- "import { pikkuWorkflowFunc } from '@pikku/core/workflow'",
44
- 'export const badWorkflow = pikkuWorkflowFunc(async (_, _input, { workflow }) => {',
45
- " await workflow.expectEventually('Wait', 'getTodos', {}, (todos: any) => todos.length > 0)",
46
- ' return { ok: true }',
47
- '})',
48
- ].join('\n')
49
- )
50
-
51
- const criticals: Array<{ code: string; message: string }> = []
52
- try {
53
- await inspect(makeLogger(criticals), [stepFile(rootDir), wfFile], {
54
- rootDir,
55
- })
56
- const pku675 = criticals.find((c) => c.code === 'PKU675')
57
- assert.ok(
58
- pku675,
59
- `expected a PKU675 critical, got: ${JSON.stringify(criticals)}`
60
- )
61
- assert.ok(
62
- pku675.message.includes('pikkuScenario'),
63
- `PKU675 should point users at pikkuScenario, got: ${pku675.message}`
64
- )
65
- } finally {
66
- await rm(rootDir, { recursive: true, force: true })
67
- }
68
- })
69
-
70
- test('pikkuScenario calling expectEventually is allowed and flagged as a scenario', async () => {
71
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-ee-sc-'))
72
- const wfFile = join(rootDir, 'my.scenario.ts')
73
- await writeFile(stepFile(rootDir), stepSource)
74
- await writeFile(
75
- wfFile,
76
- [
77
- "import { pikkuScenario } from '@pikku/core/workflow'",
78
- 'declare const actors: Record<string, any>',
79
- 'export const goodFlow = pikkuScenario(async (_, _input, { workflow }) => {',
80
- " await workflow.expectEventually('Wait', 'getTodos', {}, (todos: any) => todos.length > 0, { actor: actors.pm })",
81
- ' return { ok: true }',
82
- '})',
83
- ].join('\n')
84
- )
85
-
86
- const criticals: Array<{ code: string; message: string }> = []
87
- try {
88
- const state = await inspect(
89
- makeLogger(criticals),
90
- [stepFile(rootDir), wfFile],
91
- { rootDir }
92
- )
93
- assert.ok(
94
- !criticals.some((c) => c.code === 'PKU675'),
95
- `scenarios may use expectEventually; got: ${JSON.stringify(criticals)}`
96
- )
97
- const meta = (state.workflows.meta as any).goodFlow
98
- assert.ok(meta, 'pikkuScenario export should register a workflow')
99
- assert.equal(
100
- meta.scenario,
101
- true,
102
- `scenario meta flag should be true, got: ${JSON.stringify(meta)}`
103
- )
104
- } finally {
105
- await rm(rootDir, { recursive: true, force: true })
106
- }
107
- })
108
- })
@@ -1,96 +0,0 @@
1
- import { strict as assert } from 'assert'
2
- import { describe, test } from 'node:test'
3
- import { mkdtemp, rm, writeFile } from 'node:fs/promises'
4
- import { tmpdir } from 'node:os'
5
- import { join } from 'node:path'
6
- import { inspect } from '../inspector.js'
7
- import type { InspectorLogger } from '../types.js'
8
-
9
- const logger: InspectorLogger = {
10
- debug: () => {},
11
- info: () => {},
12
- warn: () => {},
13
- error: () => {},
14
- diagnostic: () => {},
15
- critical: () => {},
16
- hasCriticalErrors: () => false,
17
- }
18
-
19
- describe('function body spans in meta', () => {
20
- test('meta records 1-indexed bodyStart/bodyEnd lines of the handler body', async () => {
21
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-span-'))
22
- const file = join(rootDir, 'my.function.ts')
23
- await writeFile(
24
- file,
25
- [
26
- "import { pikkuSessionlessFunc } from '@pikku/core'", // line 1
27
- '', // 2
28
- 'export const spanProbe = pikkuSessionlessFunc({', // 3
29
- ' func: async ({ logger }, data: { n: number }) => {', // 4
30
- ' const doubled = data.n * 2', // 5
31
- ' return { doubled }', // 6
32
- ' },', // 7
33
- '})', // 8
34
- ].join('\n')
35
- )
36
- try {
37
- const state = await inspect(logger, [file], { rootDir })
38
- const meta = (state.functions.meta as any).spanProbe
39
- assert.ok(meta, 'spanProbe should be inspected')
40
- assert.equal(
41
- meta.bodyStart,
42
- 5,
43
- `bodyStart should be 5, got ${meta.bodyStart}`
44
- )
45
- assert.equal(meta.bodyEnd, 6, `bodyEnd should be 6, got ${meta.bodyEnd}`)
46
- } finally {
47
- await rm(rootDir, { recursive: true, force: true })
48
- }
49
- })
50
-
51
- test('meta records bodySourceFile when the handler is imported from another file', async () => {
52
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-span-'))
53
- const handlerFile = join(rootDir, 'handlers.ts')
54
- const wiringFile = join(rootDir, 'my.function.ts')
55
- await writeFile(
56
- handlerFile,
57
- [
58
- 'export const importedHandler = async (', // line 1
59
- ' { logger }: any,', // 2
60
- ' data: { n: number }', // 3
61
- ') => {', // 4
62
- ' const doubled = data.n * 2', // 5
63
- ' return { doubled }', // 6
64
- '}', // 7
65
- ].join('\n')
66
- )
67
- await writeFile(
68
- wiringFile,
69
- [
70
- "import { pikkuSessionlessFunc } from '@pikku/core'",
71
- "import { importedHandler } from './handlers.js'",
72
- '',
73
- 'export const importedProbe = pikkuSessionlessFunc({',
74
- ' func: importedHandler,',
75
- '})',
76
- ].join('\n')
77
- )
78
- try {
79
- const state = await inspect(logger, [wiringFile, handlerFile], {
80
- rootDir,
81
- })
82
- const meta = (state.functions.meta as any).importedProbe
83
- assert.ok(meta, 'importedProbe should be inspected')
84
- assert.equal(meta.sourceFile, wiringFile)
85
- assert.equal(
86
- meta.bodySourceFile,
87
- handlerFile,
88
- `bodySourceFile should be the handler file, got ${meta.bodySourceFile}`
89
- )
90
- assert.equal(meta.bodyStart, 5)
91
- assert.equal(meta.bodyEnd, 6)
92
- } finally {
93
- await rm(rootDir, { recursive: true, force: true })
94
- }
95
- })
96
- })
@@ -1,104 +0,0 @@
1
- import { strict as assert } from 'assert'
2
- import { describe, test } from 'node:test'
3
- import { mkdtemp, rm, writeFile } from 'node:fs/promises'
4
- import { tmpdir } from 'node:os'
5
- import { join } from 'node:path'
6
- import { inspect } from '../inspector.js'
7
- import type { ErrorCode, InspectorLogger } from '../types.js'
8
-
9
- const silentLogger = (): InspectorLogger => ({
10
- debug: () => {},
11
- info: () => {},
12
- warn: () => {},
13
- error: () => {},
14
- diagnostic: () => {},
15
- critical: (_code: ErrorCode, _message: string) => {},
16
- hasCriticalErrors: () => false,
17
- })
18
-
19
- // A `func:` inlined into a wiring has no exported name, so addFunctions skips
20
- // it and only the wiring's context-based id exists. The wiring visitor must
21
- // register the function metadata under that id (via ensureInlineWiringFunction)
22
- // or the transport resolves to nothing at runtime ("Missing generated metadata
23
- // for ...") and, once registered, every invocation would 403 unless the
24
- // sessionless flag is carried across from the helper used.
25
- const WIRINGS = [
26
- {
27
- label: 'wireQueueWorker',
28
- wire: 'wireQueueWorker',
29
- props: "name: 'inline-queue',",
30
- expectedId: 'queue:inline-queue',
31
- },
32
- {
33
- label: 'wireScheduler',
34
- wire: 'wireScheduler',
35
- props: "name: 'inline-cron', schedule: '* * * * *',",
36
- expectedId: 'scheduler:inline-cron',
37
- },
38
- {
39
- label: 'wireTrigger',
40
- wire: 'wireTrigger',
41
- props: "name: 'inline-trigger',",
42
- expectedId: 'trigger:inline-trigger',
43
- },
44
- {
45
- label: 'wireGateway',
46
- wire: 'wireGateway',
47
- props: "name: 'inline-gw', type: 'slack',",
48
- expectedId: 'gateway:inline-gw',
49
- },
50
- ]
51
-
52
- const write = (wire: string, props: string, helper: string) =>
53
- [
54
- `import { ${helper}, ${wire} } from '@pikku/core'`,
55
- `${wire}({`,
56
- ` ${props}`,
57
- ` func: ${helper}<{ url: string }, void>({ func: async () => {} }),`,
58
- '})',
59
- ].join('\n')
60
-
61
- describe('inline wiring functions register metadata', () => {
62
- for (const { label, wire, props, expectedId } of WIRINGS) {
63
- test(`${label} registers a sessionless func inlined into it`, async () => {
64
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-inline-'))
65
- const file = join(rootDir, 'wire.ts')
66
- await writeFile(file, write(wire, props, 'pikkuSessionlessFunc'))
67
-
68
- try {
69
- const state = await inspect(silentLogger(), [file], { rootDir })
70
- const funcMeta = state.functions.meta[expectedId]
71
- assert.ok(
72
- funcMeta,
73
- `${label}: metadata must exist under the id the wiring references`
74
- )
75
- assert.equal(
76
- funcMeta!.sessionless,
77
- true,
78
- `${label}: pikkuSessionlessFunc must stay sessionless when inlined`
79
- )
80
- } finally {
81
- await rm(rootDir, { recursive: true, force: true })
82
- }
83
- })
84
-
85
- test(`${label} keeps an inlined pikkuFunc session-required`, async () => {
86
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-inline-auth-'))
87
- const file = join(rootDir, 'wire.ts')
88
- await writeFile(file, write(wire, props, 'pikkuFunc'))
89
-
90
- try {
91
- const state = await inspect(silentLogger(), [file], { rootDir })
92
- const funcMeta = state.functions.meta[expectedId]
93
- assert.ok(funcMeta, `${label}: metadata must exist`)
94
- assert.equal(
95
- funcMeta!.sessionless,
96
- false,
97
- `${label}: pikkuFunc must not be marked sessionless`
98
- )
99
- } finally {
100
- await rm(rootDir, { recursive: true, force: true })
101
- }
102
- })
103
- }
104
- })
@@ -1,258 +0,0 @@
1
- import { strict as assert } from 'assert'
2
- import { describe, test } from 'node:test'
3
- import { mkdtemp, rm, writeFile } from 'node:fs/promises'
4
- import { tmpdir } from 'node:os'
5
- import { join } from 'node:path'
6
- import { inspect } from '../inspector.js'
7
- import { ErrorCode } from '../error-codes.js'
8
- import type { InspectorLogger } from '../types.js'
9
-
10
- // ── helpers ──────────────────────────────────────────────────────────────────
11
-
12
- function makeLogger() {
13
- // Collects every coded diagnostic regardless of severity. PKU910 is now
14
- // emitted at 'error' severity (surface, don't block the dev server) so it
15
- // arrives via `diagnostic`, not `critical`.
16
- const criticals: Array<{ code: ErrorCode; message: string }> = []
17
- const logger: InspectorLogger = {
18
- debug: () => {},
19
- info: () => {},
20
- warn: () => {},
21
- error: () => {},
22
- diagnostic: ({ code, message }) => criticals.push({ code, message }),
23
- critical: (code, message) => criticals.push({ code, message }),
24
- hasCriticalErrors: () => criticals.length > 0,
25
- }
26
- return { logger, criticals }
27
- }
28
-
29
- /**
30
- * Inline Private<T>/Pii<T>/Secret<T> definitions that the test source files use.
31
- * Mirrors what schema.gen.ts emits so the TypeScript program sees the correct
32
- * structural brand type even without @pikku/core being importable from /tmp.
33
- */
34
- // Optional `__classification__?` mirrors what @pikku/core and `pikku db migrate`
35
- // actually emit (optional so plain values stay assignable to branded columns).
36
- // The `Secret`-in-sessioned-function cases below double as the level-fidelity
37
- // guard: they only pass if `findPiiPaths` reads the level union-aware
38
- // ('secret' | undefined), not via a naive `.value`.
39
- const BRAND_TYPES = `
40
- type Private<T> = T & { readonly __classification__?: 'private' }
41
- type Pii<T> = T & { readonly __classification__?: 'pii' }
42
- type Secret<T> = T & { readonly __classification__?: 'secret' }
43
- `
44
-
45
- async function runInspect(sourceCode: string) {
46
- const tmpDir = await mkdtemp(join(tmpdir(), 'pikku-pii-test-'))
47
- const file = join(tmpDir, 'funcs.ts')
48
- await writeFile(file, sourceCode)
49
- const { logger, criticals } = makeLogger()
50
- try {
51
- // The data-classification leak scan is opt-in (off by default to keep it
52
- // off the `pikku all` hot path); these tests exercise it, so enable it.
53
- await inspect(logger, [file], {
54
- rootDir: tmpDir,
55
- classificationCheck: true,
56
- })
57
- } finally {
58
- await rm(tmpDir, { recursive: true, force: true })
59
- }
60
- return criticals
61
- }
62
-
63
- // ── classification semantics:
64
- // secret → never returned by any function (sessioned or not)
65
- // private → only blocked in sessionless functions (pikkuSessionlessFunc)
66
- // public → safe for sessionless functions
67
-
68
- describe('PII output check — PKU910', () => {
69
- // ── Secret<T>: always blocked ──────────────────────────────────────────────
70
-
71
- test('flags a top-level Secret<string> field in a sessioned function', async () => {
72
- const criticals = await runInspect(`
73
- ${BRAND_TYPES}
74
- import { pikkuFunc } from '@pikku/core'
75
- export const getToken = pikkuFunc({
76
- func: async () => {
77
- const token = 'abc' as Secret<string>
78
- return { token }
79
- }
80
- })
81
- `)
82
- const hit = criticals.find((c) => c.code === ErrorCode.PII_IN_OUTPUT)
83
- assert.ok(hit)
84
- assert.match(hit.message, /token/)
85
- })
86
-
87
- test('flags a top-level Secret<string> field in a sessionless function', async () => {
88
- const criticals = await runInspect(`
89
- ${BRAND_TYPES}
90
- import { pikkuSessionlessFunc } from '@pikku/core'
91
- export const getToken = pikkuSessionlessFunc({
92
- func: async () => {
93
- const token = 'abc' as Secret<string>
94
- return { token }
95
- }
96
- })
97
- `)
98
- const hit = criticals.find((c) => c.code === ErrorCode.PII_IN_OUTPUT)
99
- assert.ok(hit)
100
- assert.match(hit.message, /token/)
101
- })
102
-
103
- // ── Private<T>: only blocked in sessionless functions ─────────────────────
104
-
105
- test('flags a top-level Private<string> field in a sessionless function', async () => {
106
- const criticals = await runInspect(`
107
- ${BRAND_TYPES}
108
- import { pikkuSessionlessFunc } from '@pikku/core'
109
- export const getUser = pikkuSessionlessFunc({
110
- func: async () => {
111
- const email = 'test@example.com' as Private<string>
112
- return { id: 1, email }
113
- }
114
- })
115
- `)
116
- const hit = criticals.find((c) => c.code === ErrorCode.PII_IN_OUTPUT)
117
- assert.ok(hit, `Expected PKU910 but got: ${JSON.stringify(criticals)}`)
118
- assert.match(hit.message, /email/)
119
- })
120
-
121
- test('does not flag a Private<string> field in a sessioned function', async () => {
122
- const criticals = await runInspect(`
123
- ${BRAND_TYPES}
124
- import { pikkuFunc } from '@pikku/core'
125
- export const getUser = pikkuFunc({
126
- func: async () => {
127
- const email = 'test@example.com' as Private<string>
128
- return { id: 1, email }
129
- }
130
- })
131
- `)
132
- const hit = criticals.find((c) => c.code === ErrorCode.PII_IN_OUTPUT)
133
- assert.equal(
134
- hit,
135
- undefined,
136
- `Expected no PKU910 (sessioned function may return Private fields) but got: ${JSON.stringify(hit)}`
137
- )
138
- })
139
-
140
- test('flags a nested Private field in a sessionless function', async () => {
141
- const criticals = await runInspect(`
142
- ${BRAND_TYPES}
143
- import { pikkuSessionlessFunc } from '@pikku/core'
144
- export const getProfile = pikkuSessionlessFunc({
145
- func: async () => {
146
- const email = 'x@y.com' as Private<string>
147
- return { user: { id: 1, email } }
148
- }
149
- })
150
- `)
151
- const hit = criticals.find((c) => c.code === ErrorCode.PII_IN_OUTPUT)
152
- assert.ok(hit)
153
- assert.match(hit.message, /user\.email/)
154
- })
155
-
156
- test('does not flag a plain string return', async () => {
157
- const criticals = await runInspect(`
158
- import { pikkuFunc } from '@pikku/core'
159
- export const getPublicData = pikkuFunc({
160
- func: async () => ({ id: 1, status: 'active', count: 42 })
161
- })
162
- `)
163
- const hit = criticals.find((c) => c.code === ErrorCode.PII_IN_OUTPUT)
164
- assert.equal(
165
- hit,
166
- undefined,
167
- `Expected no PKU910 but got: ${JSON.stringify(hit)}`
168
- )
169
- })
170
-
171
- test('does not flag a void-returning function', async () => {
172
- const criticals = await runInspect(`
173
- import { pikkuFunc } from '@pikku/core'
174
- export const doWork = pikkuFunc({
175
- func: async () => { /* no return */ }
176
- })
177
- `)
178
- const hit = criticals.find((c) => c.code === ErrorCode.PII_IN_OUTPUT)
179
- assert.equal(hit, undefined)
180
- })
181
-
182
- test('flags a sessionless function that returns a typed alias with Private field', async () => {
183
- const criticals = await runInspect(`
184
- ${BRAND_TYPES}
185
- import { pikkuSessionlessFunc } from '@pikku/core'
186
- type UserRow = { id: number; email: Private<string> }
187
- export const getUser = pikkuSessionlessFunc({
188
- func: async (): Promise<UserRow> => {
189
- return { id: 1, email: 'x' as Private<string> }
190
- }
191
- })
192
- `)
193
- const hit = criticals.find((c) => c.code === ErrorCode.PII_IN_OUTPUT)
194
- assert.ok(hit)
195
- assert.match(hit.message, /email/)
196
- })
197
-
198
- test('flags across multiple sessionless functions in the same file', async () => {
199
- const criticals = await runInspect(`
200
- ${BRAND_TYPES}
201
- import { pikkuSessionlessFunc } from '@pikku/core'
202
- export const getEmail = pikkuSessionlessFunc({
203
- func: async () => ({ email: 'x' as Private<string> })
204
- })
205
- export const getPhone = pikkuSessionlessFunc({
206
- func: async () => ({ phone: '555' as Private<string> })
207
- })
208
- export const getSafe = pikkuSessionlessFunc({
209
- func: async () => ({ name: 'Alice' })
210
- })
211
- `)
212
- const hits = criticals.filter((c) => c.code === ErrorCode.PII_IN_OUTPUT)
213
- assert.equal(hits.length, 2, `Expected 2 PKU910 but got ${hits.length}`)
214
- })
215
-
216
- test('flags branded values inside arrays (sessionless)', async () => {
217
- const criticals = await runInspect(`
218
- ${BRAND_TYPES}
219
- import { pikkuSessionlessFunc } from '@pikku/core'
220
- export const getEmails = pikkuSessionlessFunc({
221
- func: async () => ({ emails: ['x@y.com' as Private<string>] })
222
- })
223
- `)
224
- const hit = criticals.find((c) => c.code === ErrorCode.PII_IN_OUTPUT)
225
- assert.ok(hit, `Expected PKU910 but got: ${JSON.stringify(criticals)}`)
226
- assert.match(hit.message, /emails/)
227
- })
228
-
229
- test('flags branded values inside string-indexed records (sessionless)', async () => {
230
- const criticals = await runInspect(`
231
- ${BRAND_TYPES}
232
- import { pikkuSessionlessFunc } from '@pikku/core'
233
- export const getMap = pikkuSessionlessFunc({
234
- func: async () => ({ byId: { a: 'x@y.com' as Private<string> } as Record<string, Private<string>> })
235
- })
236
- `)
237
- const hit = criticals.find((c) => c.code === ErrorCode.PII_IN_OUTPUT)
238
- assert.ok(hit, `Expected PKU910 but got: ${JSON.stringify(criticals)}`)
239
- assert.match(hit.message, /byId/)
240
- })
241
-
242
- test('does not flag when branded field is stripped before return', async () => {
243
- const criticals = await runInspect(`
244
- ${BRAND_TYPES}
245
- import { pikkuSessionlessFunc } from '@pikku/core'
246
- export const getUser = pikkuSessionlessFunc({
247
- func: async () => {
248
- const raw: { email: Private<string> } = { email: 'x' as Private<string> }
249
- const safe: { email: string } = { email: raw.email as string }
250
- return safe
251
- }
252
- })
253
- `)
254
- // The explicit type annotation on 'safe' strips the brand from the inferred return type
255
- const hit = criticals.find((c) => c.code === ErrorCode.PII_IN_OUTPUT)
256
- assert.equal(hit, undefined)
257
- })
258
- })