@pikku/core 0.9.11 → 0.10.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 (124) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/errors/errors.d.ts +2 -0
  3. package/dist/errors/errors.js +6 -0
  4. package/dist/function/function-runner.d.ts +15 -9
  5. package/dist/function/function-runner.js +55 -37
  6. package/dist/function/functions.types.d.ts +67 -1
  7. package/dist/function/functions.types.js +43 -1
  8. package/dist/index.d.ts +2 -6
  9. package/dist/index.js +2 -6
  10. package/dist/middleware/auth-apikey.d.ts +19 -11
  11. package/dist/middleware/auth-apikey.js +33 -33
  12. package/dist/middleware/auth-bearer.d.ts +34 -7
  13. package/dist/middleware/auth-bearer.js +53 -36
  14. package/dist/middleware/auth-cookie.d.ts +30 -12
  15. package/dist/middleware/auth-cookie.js +51 -43
  16. package/dist/middleware/timeout.d.ts +6 -2
  17. package/dist/middleware/timeout.js +7 -9
  18. package/dist/middleware-runner.d.ts +46 -43
  19. package/dist/middleware-runner.js +100 -69
  20. package/dist/permissions.d.ts +82 -20
  21. package/dist/permissions.js +176 -62
  22. package/dist/pikku-state.d.ts +46 -1
  23. package/dist/pikku-state.js +16 -1
  24. package/dist/services/content-service.d.ts +2 -3
  25. package/dist/services/index.d.ts +0 -1
  26. package/dist/services/index.js +0 -1
  27. package/dist/services/local-content.d.ts +2 -4
  28. package/dist/types/core.types.d.ts +110 -4
  29. package/dist/types/core.types.js +44 -1
  30. package/dist/utils.d.ts +1 -1
  31. package/dist/wirings/channel/channel-handler.d.ts +2 -2
  32. package/dist/wirings/channel/channel-handler.js +16 -12
  33. package/dist/wirings/channel/channel-runner.d.ts +2 -2
  34. package/dist/wirings/channel/channel-runner.js +6 -24
  35. package/dist/wirings/channel/channel.types.d.ts +15 -14
  36. package/dist/wirings/channel/local/local-channel-runner.js +12 -12
  37. package/dist/wirings/channel/serverless/serverless-channel-runner.js +7 -8
  38. package/dist/wirings/cli/channel/cli-channel-runner.d.ts +12 -0
  39. package/dist/wirings/cli/channel/cli-channel-runner.js +83 -0
  40. package/dist/wirings/cli/channel/index.d.ts +1 -0
  41. package/dist/wirings/cli/channel/index.js +1 -0
  42. package/dist/wirings/cli/cli-runner.d.ts +42 -0
  43. package/dist/wirings/cli/cli-runner.js +352 -0
  44. package/dist/wirings/cli/cli.types.d.ts +204 -0
  45. package/dist/wirings/cli/cli.types.js +1 -0
  46. package/dist/wirings/cli/command-parser.d.ts +19 -0
  47. package/dist/wirings/cli/command-parser.js +403 -0
  48. package/dist/wirings/cli/index.d.ts +5 -0
  49. package/dist/wirings/cli/index.js +5 -0
  50. package/dist/wirings/http/http-runner.d.ts +59 -10
  51. package/dist/wirings/http/http-runner.js +138 -131
  52. package/dist/wirings/http/http.types.d.ts +11 -10
  53. package/dist/wirings/http/index.d.ts +1 -1
  54. package/dist/wirings/http/index.js +1 -1
  55. package/dist/wirings/http/pikku-fetch-http-response.js +3 -1
  56. package/dist/wirings/http/routers/http-router.d.ts +0 -2
  57. package/dist/wirings/http/routers/path-to-regex.d.ts +1 -1
  58. package/dist/wirings/http/routers/path-to-regex.js +5 -34
  59. package/dist/wirings/mcp/mcp-endpoint-registry.d.ts +1 -1
  60. package/dist/wirings/mcp/mcp-runner.d.ts +4 -5
  61. package/dist/wirings/mcp/mcp-runner.js +32 -34
  62. package/dist/wirings/mcp/mcp.types.d.ts +38 -14
  63. package/dist/wirings/queue/queue-runner.d.ts +2 -2
  64. package/dist/wirings/queue/queue-runner.js +25 -27
  65. package/dist/wirings/queue/queue.types.d.ts +6 -5
  66. package/dist/wirings/rpc/index.d.ts +1 -1
  67. package/dist/wirings/rpc/index.js +1 -1
  68. package/dist/wirings/rpc/rpc-runner.d.ts +6 -18
  69. package/dist/wirings/rpc/rpc-runner.js +13 -8
  70. package/dist/wirings/scheduler/scheduler-runner.d.ts +2 -2
  71. package/dist/wirings/scheduler/scheduler-runner.js +38 -35
  72. package/dist/wirings/scheduler/scheduler.types.d.ts +5 -4
  73. package/package.json +9 -7
  74. package/src/errors/errors.ts +6 -0
  75. package/src/factory-functions.test.ts +60 -1
  76. package/src/function/function-runner.test.ts +81 -26
  77. package/src/function/function-runner.ts +89 -57
  78. package/src/function/functions.types.ts +85 -2
  79. package/src/index.ts +6 -19
  80. package/src/middleware/auth-apikey.ts +42 -57
  81. package/src/middleware/auth-bearer.ts +66 -49
  82. package/src/middleware/auth-cookie.ts +63 -82
  83. package/src/middleware/timeout.ts +10 -14
  84. package/src/middleware-runner.test.ts +24 -16
  85. package/src/middleware-runner.ts +115 -87
  86. package/src/permissions.test.ts +26 -27
  87. package/src/permissions.ts +216 -104
  88. package/src/pikku-state.ts +79 -2
  89. package/src/services/content-service.ts +5 -4
  90. package/src/services/index.ts +0 -1
  91. package/src/services/local-content.ts +9 -4
  92. package/src/types/core.types.ts +143 -5
  93. package/src/utils.ts +1 -1
  94. package/src/wirings/channel/channel-handler.ts +18 -21
  95. package/src/wirings/channel/channel-runner.ts +19 -32
  96. package/src/wirings/channel/channel.types.ts +29 -16
  97. package/src/wirings/channel/local/local-channel-runner.ts +25 -15
  98. package/src/wirings/channel/serverless/serverless-channel-runner.ts +15 -17
  99. package/src/wirings/cli/channel/cli-channel-runner.ts +123 -0
  100. package/src/wirings/cli/channel/index.ts +1 -0
  101. package/src/wirings/cli/cli-runner.test.ts +403 -0
  102. package/src/wirings/cli/cli-runner.ts +539 -0
  103. package/src/wirings/cli/cli.types.ts +350 -0
  104. package/src/wirings/cli/command-parser.test.ts +445 -0
  105. package/src/wirings/cli/command-parser.ts +504 -0
  106. package/src/wirings/cli/index.ts +13 -0
  107. package/src/wirings/http/http-runner.test.ts +8 -7
  108. package/src/wirings/http/http-runner.ts +165 -158
  109. package/src/wirings/http/http.types.ts +58 -11
  110. package/src/wirings/http/index.ts +7 -1
  111. package/src/wirings/http/pikku-fetch-http-response.ts +3 -1
  112. package/src/wirings/http/routers/http-router.ts +0 -2
  113. package/src/wirings/http/routers/path-to-regex.test.ts +4 -5
  114. package/src/wirings/http/routers/path-to-regex.ts +6 -43
  115. package/src/wirings/mcp/mcp-endpoint-registry.ts +1 -1
  116. package/src/wirings/mcp/mcp-runner.ts +58 -55
  117. package/src/wirings/mcp/mcp.types.ts +65 -14
  118. package/src/wirings/queue/queue-runner.ts +44 -47
  119. package/src/wirings/queue/queue.types.ts +10 -6
  120. package/src/wirings/rpc/index.ts +1 -1
  121. package/src/wirings/rpc/rpc-runner.ts +27 -9
  122. package/src/wirings/scheduler/scheduler-runner.ts +58 -56
  123. package/src/wirings/scheduler/scheduler.types.ts +10 -3
  124. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,445 @@
1
+ import { test, describe } from 'node:test'
2
+ import * as assert from 'assert'
3
+ import { parseCLIArguments, generateCommandHelp } from './command-parser.js'
4
+ import { CLIMeta } from './cli.types.js'
5
+
6
+ const testMeta: CLIMeta = {
7
+ programs: {
8
+ 'test-cli': {
9
+ program: 'test-cli',
10
+ options: {
11
+ verbose: {
12
+ description: 'Enable verbose output',
13
+ short: 'v',
14
+ default: false,
15
+ },
16
+ },
17
+ commands: {
18
+ greet: {
19
+ parameters: '<name>',
20
+ pikkuFuncName: 'greetFunc',
21
+ positionals: [{ name: 'name', required: true }],
22
+ options: {
23
+ loud: {
24
+ description: 'Use loud greeting',
25
+ short: 'l',
26
+ default: false,
27
+ },
28
+ },
29
+ },
30
+ user: {
31
+ pikkuFuncName: '',
32
+ positionals: [],
33
+ options: {},
34
+ subcommands: {
35
+ create: {
36
+ parameters: '<name> <email>',
37
+ pikkuFuncName: 'createUserFunc',
38
+ positionals: [
39
+ { name: 'name', required: true },
40
+ { name: 'email', required: true },
41
+ ],
42
+ options: {
43
+ role: {
44
+ description: 'User role',
45
+ short: 'r',
46
+ default: 'user',
47
+ choices: ['admin', 'user', 'guest'],
48
+ },
49
+ },
50
+ },
51
+ delete: {
52
+ parameters: '<id>',
53
+ pikkuFuncName: 'deleteUserFunc',
54
+ positionals: [{ name: 'id', required: true }],
55
+ options: {
56
+ force: {
57
+ description: 'Force delete',
58
+ short: 'f',
59
+ default: false,
60
+ },
61
+ },
62
+ },
63
+ },
64
+ },
65
+ files: {
66
+ parameters: '<paths...>',
67
+ pikkuFuncName: 'filesFunc',
68
+ positionals: [{ name: 'paths', required: true, variadic: true }],
69
+ options: {},
70
+ },
71
+ optional: {
72
+ parameters: '[name]',
73
+ pikkuFuncName: 'optionalFunc',
74
+ positionals: [{ name: 'name', required: false }],
75
+ options: {},
76
+ },
77
+ },
78
+ },
79
+ },
80
+ renderers: {},
81
+ }
82
+
83
+ describe('Command Parser', () => {
84
+ describe('parseCLIArguments', () => {
85
+ test('should parse simple command with required positional', () => {
86
+ const result = parseCLIArguments(['greet', 'Alice'], 'test-cli', testMeta)
87
+
88
+ assert.strictEqual(result.program, 'test-cli')
89
+ assert.deepStrictEqual(result.commandPath, ['greet'])
90
+ assert.deepStrictEqual(result.positionals, { name: 'Alice' })
91
+ assert.strictEqual(result.errors.length, 0)
92
+ })
93
+
94
+ test('should parse command with boolean flag', () => {
95
+ const result = parseCLIArguments(
96
+ ['greet', 'Alice', '--loud'],
97
+ 'test-cli',
98
+ testMeta
99
+ )
100
+
101
+ assert.deepStrictEqual(result.positionals, { name: 'Alice' })
102
+ assert.deepStrictEqual(result.options, { loud: true, verbose: false })
103
+ assert.strictEqual(result.errors.length, 0)
104
+ })
105
+
106
+ test('should parse command with short flag', () => {
107
+ const result = parseCLIArguments(
108
+ ['greet', 'Alice', '-l'],
109
+ 'test-cli',
110
+ testMeta
111
+ )
112
+
113
+ assert.deepStrictEqual(result.options, { loud: true, verbose: false })
114
+ assert.strictEqual(result.errors.length, 0)
115
+ })
116
+
117
+ test('should parse command with option value using space', () => {
118
+ const result = parseCLIArguments(
119
+ ['user', 'create', 'Bob', 'bob@example.com', '--role', 'admin'],
120
+ 'test-cli',
121
+ testMeta
122
+ )
123
+
124
+ assert.deepStrictEqual(result.commandPath, ['user', 'create'])
125
+ assert.deepStrictEqual(result.positionals, {
126
+ name: 'Bob',
127
+ email: 'bob@example.com',
128
+ })
129
+ assert.deepStrictEqual(result.options, { role: 'admin', verbose: false })
130
+ assert.strictEqual(result.errors.length, 0)
131
+ })
132
+
133
+ test('should parse command with option value using equals', () => {
134
+ const result = parseCLIArguments(
135
+ ['user', 'create', 'Bob', 'bob@example.com', '--role=admin'],
136
+ 'test-cli',
137
+ testMeta
138
+ )
139
+
140
+ assert.deepStrictEqual(result.options, { role: 'admin', verbose: false })
141
+ assert.strictEqual(result.errors.length, 0)
142
+ })
143
+
144
+ test('should parse command with short option and value', () => {
145
+ const result = parseCLIArguments(
146
+ ['user', 'create', 'Bob', 'bob@example.com', '-r', 'admin'],
147
+ 'test-cli',
148
+ testMeta
149
+ )
150
+
151
+ assert.deepStrictEqual(result.options, { role: 'admin', verbose: false })
152
+ assert.strictEqual(result.errors.length, 0)
153
+ })
154
+
155
+ test('should apply default option values', () => {
156
+ const result = parseCLIArguments(['greet', 'Alice'], 'test-cli', testMeta)
157
+
158
+ assert.deepStrictEqual(result.options, { loud: false, verbose: false })
159
+ })
160
+
161
+ test('should parse subcommands', () => {
162
+ const result = parseCLIArguments(
163
+ ['user', 'create', 'Alice', 'alice@example.com'],
164
+ 'test-cli',
165
+ testMeta
166
+ )
167
+
168
+ assert.deepStrictEqual(result.commandPath, ['user', 'create'])
169
+ assert.deepStrictEqual(result.positionals, {
170
+ name: 'Alice',
171
+ email: 'alice@example.com',
172
+ })
173
+ })
174
+
175
+ test('should parse variadic positionals', () => {
176
+ const result = parseCLIArguments(
177
+ ['files', 'file1.txt', 'file2.txt', 'file3.txt'],
178
+ 'test-cli',
179
+ testMeta
180
+ )
181
+
182
+ assert.deepStrictEqual(result.positionals, {
183
+ paths: ['file1.txt', 'file2.txt', 'file3.txt'],
184
+ })
185
+ assert.strictEqual(result.errors.length, 0)
186
+ })
187
+
188
+ test('should parse optional positionals when provided', () => {
189
+ const result = parseCLIArguments(
190
+ ['optional', 'Alice'],
191
+ 'test-cli',
192
+ testMeta
193
+ )
194
+
195
+ assert.deepStrictEqual(result.positionals, { name: 'Alice' })
196
+ assert.strictEqual(result.errors.length, 0)
197
+ })
198
+
199
+ test('should handle missing optional positionals', () => {
200
+ const result = parseCLIArguments(['optional'], 'test-cli', testMeta)
201
+
202
+ assert.deepStrictEqual(result.positionals, {})
203
+ assert.strictEqual(result.errors.length, 0)
204
+ })
205
+
206
+ test('should handle global options', () => {
207
+ const result = parseCLIArguments(
208
+ ['greet', 'Alice', '--verbose'],
209
+ 'test-cli',
210
+ testMeta
211
+ )
212
+
213
+ assert.deepStrictEqual(result.options, { loud: false, verbose: true })
214
+ assert.strictEqual(result.errors.length, 0)
215
+ })
216
+
217
+ test('should combine multiple short flags', () => {
218
+ const result = parseCLIArguments(
219
+ ['greet', 'Alice', '-lv'],
220
+ 'test-cli',
221
+ testMeta
222
+ )
223
+
224
+ assert.deepStrictEqual(result.options, { loud: true, verbose: true })
225
+ assert.strictEqual(result.errors.length, 0)
226
+ })
227
+
228
+ test('should report error for missing required positional', () => {
229
+ const result = parseCLIArguments(['greet'], 'test-cli', testMeta)
230
+
231
+ assert.ok(result.errors.length > 0)
232
+ assert.ok(result.errors[0].includes('Missing required argument: name'))
233
+ })
234
+
235
+ test('should report error for invalid choice', () => {
236
+ const result = parseCLIArguments(
237
+ ['user', 'create', 'Bob', 'bob@example.com', '--role', 'invalid'],
238
+ 'test-cli',
239
+ testMeta
240
+ )
241
+
242
+ assert.ok(result.errors.length > 0)
243
+ assert.ok(result.errors[0].includes('Invalid value'))
244
+ })
245
+
246
+ test('should report error for unknown option', () => {
247
+ const result = parseCLIArguments(
248
+ ['greet', 'Alice', '--unknown'],
249
+ 'test-cli',
250
+ testMeta
251
+ )
252
+
253
+ // Unknown options are allowed (for forward compatibility)
254
+ // They just won't have defaults or validation
255
+ assert.strictEqual(result.options.unknown, true)
256
+ })
257
+
258
+ test('should report error for unknown short flag', () => {
259
+ const result = parseCLIArguments(
260
+ ['greet', 'Alice', '-x'],
261
+ 'test-cli',
262
+ testMeta
263
+ )
264
+
265
+ assert.ok(result.errors.length > 0)
266
+ assert.ok(result.errors[0].includes('Unknown option: -x'))
267
+ })
268
+
269
+ test('should report error for extra positional arguments', () => {
270
+ const result = parseCLIArguments(
271
+ ['greet', 'Alice', 'Bob'],
272
+ 'test-cli',
273
+ testMeta
274
+ )
275
+
276
+ assert.ok(result.errors.length > 0)
277
+ assert.ok(result.errors[0].includes('Unexpected arguments'))
278
+ })
279
+
280
+ test('should report error for unknown command', () => {
281
+ const result = parseCLIArguments(['unknown'], 'test-cli', testMeta)
282
+
283
+ assert.ok(result.errors.length > 0)
284
+ assert.ok(result.errors[0].includes('Unknown command'))
285
+ })
286
+
287
+ test('should report error for nonexistent program', () => {
288
+ const result = parseCLIArguments(['test'], 'nonexistent', testMeta)
289
+
290
+ assert.ok(result.errors.length > 0)
291
+ assert.ok(result.errors[0].includes('Program not found'))
292
+ })
293
+
294
+ test('should parse number values correctly', () => {
295
+ const metaWithNumber: CLIMeta = {
296
+ programs: {
297
+ 'num-cli': {
298
+ program: 'num-cli',
299
+ options: {},
300
+ commands: {
301
+ test: {
302
+ pikkuFuncName: 'testFunc',
303
+ positionals: [],
304
+ options: {
305
+ port: {
306
+ description: 'Port number',
307
+ default: 3000,
308
+ },
309
+ },
310
+ },
311
+ },
312
+ },
313
+ },
314
+ renderers: {},
315
+ }
316
+
317
+ const result = parseCLIArguments(
318
+ ['test', '--port', '8080'],
319
+ 'num-cli',
320
+ metaWithNumber
321
+ )
322
+
323
+ assert.strictEqual(result.options.port, 8080)
324
+ assert.strictEqual(typeof result.options.port, 'number')
325
+ })
326
+
327
+ test('should parse boolean values correctly', () => {
328
+ const result = parseCLIArguments(
329
+ ['greet', 'Alice', '--loud', 'true'],
330
+ 'test-cli',
331
+ testMeta
332
+ )
333
+
334
+ assert.strictEqual(result.options.loud, true)
335
+ })
336
+
337
+ test('should handle empty variadic positionals when required', () => {
338
+ const result = parseCLIArguments(['files'], 'test-cli', testMeta)
339
+
340
+ assert.ok(result.errors.length > 0)
341
+ assert.ok(result.errors[0].includes('Missing required argument: paths'))
342
+ })
343
+ })
344
+
345
+ describe('generateCommandHelp', () => {
346
+ test('should generate help for program root', () => {
347
+ const help = generateCommandHelp('test-cli', testMeta)
348
+
349
+ assert.ok(help.includes('Usage: test-cli <command>'))
350
+ assert.ok(help.includes('Commands:'))
351
+ assert.ok(help.includes('greet'))
352
+ assert.ok(help.includes('Options:'))
353
+ assert.ok(help.includes('--verbose'))
354
+ })
355
+
356
+ test('should generate help for specific command', () => {
357
+ const help = generateCommandHelp('test-cli', testMeta, ['greet'])
358
+
359
+ assert.ok(help.includes('Usage: test-cli greet'))
360
+ assert.ok(help.includes('<name>'))
361
+ assert.ok(help.includes('--loud'))
362
+ assert.ok(help.includes('Use loud greeting'))
363
+ })
364
+
365
+ test('should generate help for subcommand', () => {
366
+ const help = generateCommandHelp('test-cli', testMeta, ['user', 'create'])
367
+
368
+ assert.ok(help.includes('Usage: test-cli user create'))
369
+ assert.ok(help.includes('<name>'))
370
+ assert.ok(help.includes('<email>'))
371
+ assert.ok(help.includes('--role'))
372
+ })
373
+
374
+ test('should show command description if available', () => {
375
+ const metaWithDesc: CLIMeta = {
376
+ programs: {
377
+ 'test-cli': {
378
+ program: 'test-cli',
379
+ options: {},
380
+ commands: {
381
+ greet: {
382
+ parameters: '<name>',
383
+ pikkuFuncName: 'greetFunc',
384
+ description: 'Greet a user',
385
+ positionals: [{ name: 'name', required: true }],
386
+ options: {},
387
+ },
388
+ },
389
+ },
390
+ },
391
+ renderers: {},
392
+ }
393
+
394
+ const help = generateCommandHelp('test-cli', metaWithDesc, ['greet'])
395
+
396
+ assert.ok(help.includes('Greet a user'))
397
+ })
398
+
399
+ test('should show option defaults in help', () => {
400
+ const help = generateCommandHelp('test-cli', testMeta, ['greet'])
401
+
402
+ assert.ok(help.includes('(default: false)'))
403
+ })
404
+
405
+ test('should show option choices in help', () => {
406
+ const help = generateCommandHelp('test-cli', testMeta, ['user', 'create'])
407
+
408
+ assert.ok(help.includes('[choices: admin, user, guest]'))
409
+ })
410
+
411
+ test('should show subcommands in help', () => {
412
+ const help = generateCommandHelp('test-cli', testMeta, ['user'])
413
+
414
+ assert.ok(help.includes('Subcommands:'))
415
+ assert.ok(help.includes('create'))
416
+ assert.ok(help.includes('delete'))
417
+ })
418
+
419
+ test('should show variadic positionals in help', () => {
420
+ const help = generateCommandHelp('test-cli', testMeta, ['files'])
421
+
422
+ assert.ok(help.includes('paths...'))
423
+ assert.ok(help.includes('<required>'))
424
+ })
425
+
426
+ test('should show optional positionals in help', () => {
427
+ const help = generateCommandHelp('test-cli', testMeta, ['optional'])
428
+
429
+ assert.ok(help.includes('name'))
430
+ assert.ok(help.includes('[optional]'))
431
+ })
432
+
433
+ test('should return error for nonexistent program', () => {
434
+ const help = generateCommandHelp('nonexistent', testMeta)
435
+
436
+ assert.ok(help.includes('Program not found'))
437
+ })
438
+
439
+ test('should return error for nonexistent command', () => {
440
+ const help = generateCommandHelp('test-cli', testMeta, ['nonexistent'])
441
+
442
+ assert.ok(help.includes('Unknown command'))
443
+ })
444
+ })
445
+ })