@pikku/skills 0.12.4 → 0.12.6

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 (64) hide show
  1. package/dist/skills.gen.js +1 -1
  2. package/package.json +1 -1
  3. package/skills/pikku-addon/SKILL.md +56 -29
  4. package/skills/pikku-ai-agent/SKILL.md +197 -105
  5. package/skills/pikku-ai-vercel/SKILL.md +57 -18
  6. package/skills/pikku-ai-voice/SKILL.md +126 -52
  7. package/skills/pikku-audit/SKILL.md +35 -13
  8. package/skills/pikku-aws/SKILL.md +66 -16
  9. package/skills/pikku-backblaze/SKILL.md +44 -11
  10. package/skills/pikku-better-auth/SKILL.md +45 -10
  11. package/skills/pikku-cli/SKILL.md +67 -18
  12. package/skills/pikku-cli/references/complete-example.md +2 -0
  13. package/skills/pikku-concepts/SKILL.md +75 -10
  14. package/skills/pikku-config/SKILL.md +56 -14
  15. package/skills/pikku-cron/SKILL.md +13 -6
  16. package/skills/pikku-deploy-azure/SKILL.md +83 -28
  17. package/skills/pikku-deploy-cloudflare/SKILL.md +79 -37
  18. package/skills/pikku-deploy-express/SKILL.md +40 -4
  19. package/skills/pikku-deploy-fastify/SKILL.md +22 -1
  20. package/skills/pikku-deploy-lambda/SKILL.md +99 -19
  21. package/skills/pikku-deploy-nextjs/SKILL.md +49 -5
  22. package/skills/pikku-deploy-uws/SKILL.md +54 -1
  23. package/skills/pikku-deps/SKILL.md +29 -8
  24. package/skills/pikku-emails/SKILL.md +36 -5
  25. package/skills/pikku-fabric/SKILL.md +27 -2
  26. package/skills/pikku-fabric-debug/SKILL.md +5 -1
  27. package/skills/pikku-feature/SKILL.md +6 -1
  28. package/skills/pikku-gateway-slack/SKILL.md +72 -11
  29. package/skills/pikku-http/SKILL.md +18 -5
  30. package/skills/pikku-http/references/http-options.md +10 -5
  31. package/skills/pikku-i18n/SKILL.md +18 -7
  32. package/skills/pikku-info/SKILL.md +18 -8
  33. package/skills/pikku-jose/SKILL.md +35 -6
  34. package/skills/pikku-kysely/SKILL.md +78 -15
  35. package/skills/pikku-machine-auth/SKILL.md +36 -1
  36. package/skills/pikku-mcp/SKILL.md +159 -149
  37. package/skills/pikku-middleware/SKILL.md +17 -5
  38. package/skills/pikku-mongodb/SKILL.md +10 -2
  39. package/skills/pikku-n8n-import/SKILL.md +14 -6
  40. package/skills/pikku-permissions/SKILL.md +102 -22
  41. package/skills/pikku-pino/SKILL.md +12 -4
  42. package/skills/pikku-product-second-opinion/SKILL.md +3 -3
  43. package/skills/pikku-queue/SKILL.md +45 -16
  44. package/skills/pikku-react/SKILL.md +41 -14
  45. package/skills/pikku-react-query/SKILL.md +14 -10
  46. package/skills/pikku-realtime/SKILL.md +44 -22
  47. package/skills/pikku-redis/SKILL.md +12 -3
  48. package/skills/pikku-rpc/SKILL.md +23 -12
  49. package/skills/pikku-rtl/SKILL.md +21 -17
  50. package/skills/pikku-scenario/SKILL.md +108 -75
  51. package/skills/pikku-schedule/SKILL.md +39 -6
  52. package/skills/pikku-schema-ajv/SKILL.md +24 -2
  53. package/skills/pikku-schema-cfworker/SKILL.md +22 -2
  54. package/skills/pikku-security/SKILL.md +54 -9
  55. package/skills/pikku-services/SKILL.md +49 -9
  56. package/skills/pikku-services/references/audit-wire-service.md +2 -1
  57. package/skills/pikku-template-clone/SKILL.md +10 -5
  58. package/skills/pikku-trigger/SKILL.md +50 -6
  59. package/skills/pikku-versioning/SKILL.md +46 -17
  60. package/skills/pikku-websocket/SKILL.md +72 -44
  61. package/skills/pikku-workflow/SKILL.md +35 -1
  62. package/skills/pikku-workflow/references/workflow-reference.md +13 -8
  63. package/skills/pikku-workflows-client/SKILL.md +13 -6
  64. package/skills/pikku-ws/SKILL.md +44 -8
@@ -26,57 +26,99 @@ yarn add @pikku/cloudflare
26
26
 
27
27
  ## Worker Entry
28
28
 
29
+ `@pikku/cloudflare` ships the handler factories the deploy codegen emits — use
30
+ them rather than hand-rolling an `ExportedHandler`. Each returns a
31
+ `WorkerEntrypoint` class that sets services up on every invocation (cached after
32
+ the first) and adds an RPC-callable `runRpc(name, args)`:
33
+
29
34
  ```typescript
30
- import { runFetch, runScheduled } from '@pikku/cloudflare'
31
- import { setupServices } from './setup-services.js'
35
+ import { createCloudflareHandler } from '@pikku/cloudflare'
36
+ import { createConfig, createSingletonServices } from './services.js'
32
37
  import './.pikku/pikku-bootstrap.gen.js'
33
38
 
34
- export default {
35
- async scheduled(controller, env) {
36
- await setupServices(env)
37
- await runScheduled(controller)
38
- },
39
-
40
- async fetch(request, env): Promise<Response> {
41
- await setupServices(env)
42
- return await runFetch(request as unknown as Request)
43
- },
44
- } satisfies ExportedHandler<Record<string, string>>
39
+ export default createCloudflareHandler(
40
+ { createConfig, createSingletonServices },
41
+ ['fetch', 'scheduled']
42
+ )
45
43
  ```
46
44
 
45
+ | Factory | For |
46
+ | --- | --- |
47
+ | `createCloudflareHandler(factories, handlerTypes)` | combined `fetch`/`queue`/`scheduled` |
48
+ | `createCloudflareWorkerHandler(factories)` | HTTP / agent / RPC / workflow-orchestrator units |
49
+ | `createCloudflareCronHandler(factories)` | cron units |
50
+ | `createCloudflareQueueHandler(factories)` | queue-consumer and workflow-step units |
51
+ | `createCloudflareMCPHandler(factories)` | MCP units (same HTTP transport) |
52
+ | `createCloudflareWebSocketHandler(factories)` | channel units, delegating to the DO |
53
+
54
+ `factories` is `{ createConfig, createSingletonServices, createPlatformServices? }`.
55
+
47
56
  ## Service Setup
48
57
 
49
- Cloudflare passes env variables per-request — wrap them with Pikku services:
58
+ Cloudflare passes env bindings per-request, so services are built from `env`
59
+ rather than at module load. `setupServices(env, factories)` is exported from
60
+ `@pikku/cloudflare` and is what the factories call:
50
61
 
51
62
  ```typescript
52
- // setup-services.ts
53
- import { LocalVariablesService, LocalSecretService } from '@pikku/core/services'
54
- import { createConfig, createSingletonServices } from './services.js'
63
+ import { setupServices } from '@pikku/cloudflare'
55
64
 
56
- export const setupServices = async (
57
- env: Record<string, string | undefined>
58
- ) => {
59
- const localVariables = new LocalVariablesService(env)
60
- const config = await createConfig(localVariables)
61
- const localSecrets = new LocalSecretService(localVariables)
62
- return await createSingletonServices(config, {
63
- variables: localVariables,
64
- secrets: localSecrets,
65
- })
66
- }
65
+ const services = await setupServices(env, {
66
+ createConfig,
67
+ createSingletonServices,
68
+ })
67
69
  ```
68
70
 
71
+ **Do not hand-roll this.** Beyond building `LocalVariablesService` /
72
+ `LocalSecretService` and caching the result, it calls `setSingletonServices()` —
73
+ and the core runners (`fetchData`, `runQueueJob`, `runScheduled`) resolve
74
+ services through that global slot, *not* through the value you were returned. A
75
+ setup function that only returns the services leaves every request throwing
76
+ "Singleton services not initialized" as a CF `1101`. It also stashes the env via
77
+ `setCloudflareEnv`, which `getCloudflareEnv()` reads for bindings.
78
+
79
+ ## HTTP
80
+
81
+ `runFetch(request, websocketHibernationServer?, options?)`:
82
+
83
+ - A `GET` with `Upgrade: websocket` is routed to the hibernation server. Without
84
+ one passed in it answers **426**, so a channel worker that forgets the second
85
+ argument fails every upgrade while plain HTTP keeps working.
86
+ - `CF-Ray` becomes the traceId when present, so a Cloudflare trace and a Pikku
87
+ trace line up without extra wiring.
88
+ - `options.exposeErrors` defaults to **`false`** — error detail is withheld from
89
+ responses unless you opt in.
90
+
91
+ ## Scheduled Tasks
92
+
93
+ `runScheduled(controller)` matches registered tasks against
94
+ `controller.cron` and **returns after the first match**. Two tasks sharing one
95
+ cron expression means only one of them ever runs — give each its own expression,
96
+ or invoke `runScheduledTask({ name })` per task yourself.
97
+
69
98
  ## WebSocket (Durable Objects)
70
99
 
100
+ The ready-made DO class is exported; re-export it under the binding name and
101
+ point the worker at it:
102
+
71
103
  ```typescript
72
- import { CloudflareWebSocketHibernationServer } from '@pikku/cloudflare'
73
-
74
- export class WebSocketHibernationServer extends CloudflareWebSocketHibernationServer {
75
- protected async getParams() {
76
- const singletonServices = await setupServices(this.env)
77
- return { singletonServices }
78
- }
79
- }
104
+ export { PikkuWebSocketHibernationServer as WebSocketHibernationServer } from '@pikku/cloudflare'
105
+ export default createCloudflareWebSocketHandler({
106
+ createConfig,
107
+ createSingletonServices,
108
+ })
80
109
  ```
81
110
 
82
- Register the Durable Object in `wrangler.toml` and export from the worker entry.
111
+ Subclass `CloudflareWebSocketHibernationServer` only when you need something
112
+ `getParams()` cannot express — it is abstract with one method returning
113
+ `{ singletonServices, createWireServices? }`. The channel store
114
+ (`CloudflareWebsocketStore` over the DO's own storage), the event hub and the
115
+ channel handler factory are all built by the base class; do not supply them.
116
+
117
+ The router looks up the DO through the **`WEBSOCKET_HIBERNATION_SERVER`**
118
+ binding and answers `503` naming it if the binding is missing, so declare it in
119
+ `wrangler.toml` under exactly that name.
120
+
121
+ A throw during `onConnect` closes the socket with `1008` and answers `403
122
+ Forbidden` with a deliberately generic body — an auth denial and a genuine fault
123
+ look identical to the client. The real reason is on the logger, so read the
124
+ worker logs rather than the status code.
@@ -55,15 +55,37 @@ await appServer.start()
55
55
 
56
56
  **Methods:**
57
57
 
58
- - `init(httpOptions?): Promise<void>` — Register middleware and routes
58
+ - `init(httpOptions?: RunHTTPWiringOptions): Promise<void>` — installs the body parsers, the cookie parser and the Pikku middleware
59
59
  - `start(): Promise<void>` — Start listening
60
- - `stop(): Promise<void>` — Graceful shutdown
61
- - `enableExitOnSigInt(): Promise<void>` — SIGINT handler
60
+ - `stop(): Promise<void>` — Graceful shutdown; throws if the server was never started
61
+ - `enableExitOnSigInt(): Promise<void>` — SIGINT handler: stops the singleton services, then the server, then exits 0
62
62
  - `enableCors(options): void` — Enable CORS
63
- - `enableStaticAssets(): void` — Serve static files (requires `content` config)
63
+ - `enableStaticAssets(): void` — serve `content.localFileUploadPath` under `content.assetUrlPrefix`
64
+ - `enableReaper(): void` — a `PUT /reaper/*path` upload sink for local development, path-traversal checked and bounded by `content.sizeLimit` (default `1mb`)
65
+ - `getHttpServer(): Server` — the underlying `http.Server`, e.g. to attach a WebSocket server; throws before `start()`
66
+
67
+ `enableStaticAssets` and `enableReaper` both throw when `content` is unset.
64
68
 
65
69
  **Property:** `app: Express` — Direct access to Express instance for custom middleware.
66
70
 
71
+ ### Ordering, and what `init` installs for you
72
+
73
+ The health check is registered in the **constructor**, so it answers before any
74
+ middleware you add and cannot be wrapped in auth. It defaults to
75
+ `/health-check`; override with `healthCheckPath`.
76
+
77
+ Everything else is installed by `init()`: `express.json`, `express.text` (for
78
+ `text/xml`), `express.urlencoded`, `cookie-parser`, then the Pikku middleware.
79
+ Call `enableCors` **before** `init` if you want CORS applied to Pikku's routes.
80
+
81
+ Express buffers the body before Pikku sees it, so the parser limit is the only
82
+ place an oversized request can actually be stopped. `httpOptions.maxBodySize`
83
+ therefore feeds those parser limits, with an explicit `config.limits` entry
84
+ (`json` / `xml` / `urlencoded`) still winning. Everything defaults to `1mb`.
85
+
86
+ `init` passes `logRoutes: true` and `loadSchemas: true` by default; your
87
+ `httpOptions` spread over them, so you can turn either off.
88
+
67
89
  ## Middleware (existing Express app)
68
90
 
69
91
  ```bash
@@ -76,11 +98,25 @@ import { pikkuExpressMiddleware } from '@pikku/express-middleware'
76
98
  import './.pikku/pikku-bootstrap.gen.js'
77
99
 
78
100
  const app = express()
101
+ app.use(express.json())
102
+ app.use(cookieParser())
79
103
  app.use(
80
104
  pikkuExpressMiddleware({
81
105
  logger: singletonServices.logger,
82
106
  logRoutes: true,
83
107
  loadSchemas: true,
108
+ // plus any RunHTTPWiringOptions: maxBodySize, respondWith404, coerceDataFromSchema
84
109
  })
85
110
  )
86
111
  ```
112
+
113
+ Options beyond `logger` are all optional: `logRoutes` logs the wiring table once
114
+ at startup, `loadSchemas` compiles every schema up front, and the rest are
115
+ `RunHTTPWiringOptions` passed through per request.
116
+
117
+ On your own app **you** own the parser stack — the middleware reads
118
+ `req.body`, so a body parser and `cookie-parser` must be registered before it,
119
+ and `maxBodySize` alone will not stop an oversized request that your parser
120
+ already accepted. Unmatched requests fall through to `next()` (unless
121
+ `respondWith404` is set), so Pikku's routes coexist with your existing ones;
122
+ a streaming response is the exception and does not call `next()`.
@@ -47,10 +47,19 @@ await appServer.start()
47
47
 
48
48
  **Config extends CoreConfig with:** `port`, `hostname`, `healthCheckPath?`
49
49
 
50
- **Methods:** `init(httpOptions?)`, `start()`, `stop()`, `enableExitOnSigInt()`
50
+ **Methods:** `init(httpOptions?: RunHTTPWiringOptions)`, `start()`, `stop()`, `enableExitOnSigInt()`
51
51
 
52
52
  **Property:** `app: FastifyInstance` — Direct access to Fastify instance.
53
53
 
54
+ `enableCors` exists on the class but **throws `Method not implemented.`** — unlike
55
+ the Express server. Register `@fastify/cors` on `app` yourself before `init()`.
56
+
57
+ Unlike the Express server, the health check is registered by `init()`, not the
58
+ constructor, so nothing answers before `init` runs. `init` also passes
59
+ `logRoutes: true` and `loadSchemas: true`, which your `httpOptions` can override.
60
+ The Fastify instance is constructed with no options; reach for the plugin package
61
+ if you need `Fastify({ … })` of your own.
62
+
54
63
  ## Plugin (existing Fastify app)
55
64
 
56
65
  ```bash
@@ -68,6 +77,18 @@ app.register(pikkuFastifyPlugin, {
68
77
  logger: singletonServices.logger,
69
78
  logRoutes: true,
70
79
  loadSchemas: true,
80
+ // plus any RunHTTPWiringOptions: maxBodySize, respondWith404, …
71
81
  },
72
82
  })
73
83
  ```
84
+
85
+ Every option other than `logger` is optional, and the rest of the `pikku` object
86
+ is `RunHTTPWiringOptions` passed straight through.
87
+
88
+ The plugin registers a catch-all `fastify.all('/*')`, so mount it on a
89
+ [Fastify prefix](https://fastify.dev/docs/latest/Reference/Plugins/) if the app
90
+ has routes of its own to keep.
91
+
92
+ Fastify buffers the body itself, so its `bodyLimit` is where an oversized request
93
+ is stopped. `maxBodySize` sets it — and left unset, Fastify's stricter 1MB default
94
+ stands rather than being loosened to Pikku's 10MB fallback.
@@ -44,30 +44,75 @@ export const coldStart = async () => {
44
44
  }
45
45
  ```
46
46
 
47
+ If the deploy codegen generated your handlers, this caching is already done for
48
+ you by the factories in `@pikku/lambda` — `createLambdaHandler(factories,
49
+ handlerTypes)`, `createLambdaWorkerHandler(factories)` and
50
+ `createLambdaWebSocketHandler(factories)`. They build `variables`/`secrets` from
51
+ `process.env`, cache the singleton services in module scope, and return the
52
+ named exports (`handler`, `queue`, `scheduled`, or `connect`/`disconnect`/
53
+ `default`) that `serverless.yml` references. Hand-written handlers are for cases
54
+ the codegen does not cover.
55
+
47
56
  ## HTTP Handler
48
57
 
58
+ Pick the entry point that matches the API Gateway payload version — they take
59
+ different event types and are not interchangeable:
60
+
49
61
  ```typescript
50
- import type { APIGatewayProxyEvent } from 'aws-lambda'
51
- import { runFetch } from '@pikku/lambda/http'
62
+ import type { APIGatewayEvent } from 'aws-lambda'
63
+ import { runFetch } from '@pikku/lambda/http' // REST API / payload v1
52
64
 
53
- export const httpRoute = async (event: APIGatewayProxyEvent) => {
65
+ export const httpRoute = async (event: APIGatewayEvent) => {
54
66
  await coldStart()
55
67
  return await runFetch(event)
56
68
  }
57
69
  ```
58
70
 
71
+ ```typescript
72
+ import type { APIGatewayProxyEventV2 } from 'aws-lambda'
73
+ import { runFetchV2 } from '@pikku/lambda/http' // HTTP API / payload v2
74
+
75
+ export const httpRoute = async (event: APIGatewayProxyEventV2) => {
76
+ await coldStart()
77
+ return await runFetchV2(event)
78
+ }
79
+ ```
80
+
81
+ Both answer `OPTIONS` themselves before Pikku's wirings run, so a preflight
82
+ never reaches your middleware. Only `runFetchV2` echoes the request `Origin`
83
+ into `Access-Control-Allow-Origin`; `runFetch` sets allowed headers and methods
84
+ but **no origin header at all**, so v1 preflights fail in the browser unless
85
+ API Gateway or a CloudFront layer adds one.
86
+
87
+ They also differ on failure: `runFetchV2` logs and returns a JSON `500`, while
88
+ `runFetch` swallows the error and returns whatever status the response already
89
+ carried.
90
+
91
+ Neither takes `RunHTTPWiringOptions` — there is no `maxBodySize` or
92
+ `respondWith404` knob here; API Gateway's own payload limit is the bound.
93
+
59
94
  ## Scheduled Tasks
60
95
 
61
96
  ```typescript
62
97
  import type { ScheduledHandler } from 'aws-lambda'
63
- import { runScheduledTask } from '@pikku/core/scheduler'
98
+ import { runLambdaScheduled } from '@pikku/lambda/scheduled'
64
99
 
65
- export const myScheduledTask: ScheduledHandler = async () => {
100
+ export const scheduled: ScheduledHandler = async (event) => {
66
101
  await coldStart()
67
- await runScheduledTask({ name: 'myScheduledTask' })
102
+ await runLambdaScheduled(event)
68
103
  }
69
104
  ```
70
105
 
106
+ `runLambdaScheduled` runs **every** scheduled task registered in the bundle,
107
+ each with its own `cron-<uuid>` traceId, and logs rather than rethrows a task
108
+ failure — so one bad task cannot fail the invocation or stop the others. The
109
+ event itself is ignored; which tasks run is decided by what the unit bundled,
110
+ not by which EventBridge rule fired.
111
+
112
+ Reach for `runScheduledTask({ name })` from `@pikku/core/scheduler` directly
113
+ only when one Lambda genuinely bundles several tasks that must fire on separate
114
+ schedules.
115
+
71
116
  ## SQS Queue Worker
72
117
 
73
118
  ```typescript
@@ -80,6 +125,25 @@ export const mySQSWorker: SQSHandler = async (event) => {
80
125
  }
81
126
  ```
82
127
 
128
+ The worker returns an `SQSBatchResponse` listing the failed messages in
129
+ `batchItemFailures`, which SQS only honours when the event source mapping has
130
+ **`ReportBatchItemFailures`** enabled. Without it the whole batch is retried
131
+ when any one message fails, so successfully processed jobs run twice.
132
+
133
+ Records are processed in parallel, and the queue name is taken from the last
134
+ segment of `eventSourceARN` — it must match the name the worker was wired under.
135
+ A `QueueJobDiscardedError` counts as success (no retry); anything else is
136
+ reported as a failed item.
137
+
138
+ `waitForCompletion` throws on an SQS job: the transport is fire-and-forget.
139
+
140
+ On the producer side, `SQSQueueService` resolves each queue URL from the
141
+ constructor's `queueUrlMap` first, then from
142
+ `SQS_QUEUE_URL_<SCREAMING_SNAKE_NAME>`, and throws naming the missing variable
143
+ if neither has it. `supportsResults` is `false` and `getJob()` always throws —
144
+ use BullMQ or PgBoss if you need results. `delay` is milliseconds, rounded up to
145
+ whole seconds and capped at SQS's 900s ceiling.
146
+
83
147
  ## WebSocket (API Gateway v2)
84
148
 
85
149
  ```typescript
@@ -90,21 +154,37 @@ import {
90
154
  LambdaEventHubService,
91
155
  } from '@pikku/lambda/websocket'
92
156
 
93
- export const connectHandler = async (event) => {
94
- const params = await getParams(event)
95
- await connectWebsocket(event, params)
96
- return { statusCode: 200, body: '' }
157
+ const params = async (event) => {
158
+ const { channelStore } = await coldStart()
159
+ return { channelStore }
97
160
  }
98
161
 
99
- export const disconnectHandler = async (event) => {
100
- const params = await getParams(event)
101
- return await disconnectWebsocket(event, params)
102
- }
162
+ export const connectHandler = async (event) =>
163
+ await connectWebsocket(event, await params(event))
103
164
 
104
- export const defaultHandler = async (event) => {
105
- const params = await getParams(event)
106
- return await processWebsocketMessage(event, params)
107
- }
165
+ export const disconnectHandler = async (event) =>
166
+ await disconnectWebsocket(event, await params(event))
167
+
168
+ export const defaultHandler = async (event) =>
169
+ await processWebsocketMessage(event, await params(event))
108
170
  ```
109
171
 
110
- WebSocket requires a `ChannelStore` (e.g., `PgChannelStore`) and `LambdaEventHubService` for cross-connection messaging.
172
+ All three take the same `{ channelStore }` and **return a complete
173
+ `APIGatewayProxyResult`** — return it. Discarding `connectWebsocket`'s result
174
+ and answering a hardcoded `200` accepts every connection, including the ones
175
+ your channel's auth rejected.
176
+
177
+ `channelStore` (e.g. `PgChannelStore`) must be a real shared store: each route
178
+ is a separate invocation, so nothing survives in memory between `$connect` and
179
+ `$default`.
180
+
181
+ `LambdaEventHubService` handles cross-connection messaging and takes
182
+ `(logger, event, channelStore, eventHubStore)` — the `event` is needed to derive
183
+ the API Gateway Management endpoint, so it is constructed per invocation, not
184
+ once at cold start. It also needs an `EventHubStore` alongside the channel
185
+ store.
186
+
187
+ Two behaviours to design around: **binary payloads throw** (`Binary data is not
188
+ supported on serverless lambdas`), and any `PostToConnection` failure removes
189
+ the connection from the channel store — a transient error drops a live client,
190
+ not just a stale one.
@@ -38,6 +38,17 @@ export const PATCH = pikkuAPIRequest
38
38
  export const DELETE = pikkuAPIRequest
39
39
  ```
40
40
 
41
+ `pikkuAPIRequest` strips a leading `/api` from the pathname before routing, so
42
+ wirings are declared as `/todos`, not `/api/todos`, even though the route file
43
+ lives under `app/api`. Turn that off with `removeAPIPrefix(false)` from the same
44
+ generated file if your wirings really do carry the prefix.
45
+
46
+ It takes `(req, context)` to match Next's handler signature but ignores the
47
+ context — Pikku routes from the URL, so the catch-all segment name is yours to
48
+ choose. It also passes no `RunHTTPWiringOptions`: to set `maxBodySize` or
49
+ `respondWith404` you need your own handler over `new PikkuNextJS(...)` calling
50
+ `apiRequest(req, options)`.
51
+
41
52
  ## Server-Side Data Fetching
42
53
 
43
54
  Use the generated `pikku()` helper in Server Components or Server Actions:
@@ -45,7 +56,7 @@ Use the generated `pikku()` helper in Server Components or Server Actions:
45
56
  ```typescript
46
57
  import { pikku } from '@/pikku-nextjs.gen.js'
47
58
 
48
- const { get, post, del, rpc, staticGet, staticPost, staticRPC } = pikku()
59
+ const { get, post, patch, del, rpc, staticGet, staticPost, staticRPC } = pikku()
49
60
 
50
61
  // Dynamic (reads headers/cookies — requires request context)
51
62
  const todos = await get('/todos')
@@ -60,8 +71,19 @@ const result = await rpc('calculateTax', { amount: 100, region: 'US' })
60
71
 
61
72
  **Dynamic vs Static:**
62
73
 
63
- - `get`, `post`, `del`, `rpc` — access headers/cookies, use in dynamic Server Components
64
- - `staticGet`, `staticPost`, `staticRPC` — no request context, safe for precompile/ISR
74
+ - `get`, `post`, `patch`, `del`, `rpc` — read `next/headers` cookies and headers,
75
+ so they force the component dynamic
76
+ - `staticGet`, `staticPost`, `staticRPC` — no request context, safe for
77
+ precompile/ISR
78
+
79
+ The static variants pass `skipUserSession: true`, so a wiring that expects a
80
+ session sees none. That is the real difference — not just where they can run.
81
+ There is no `staticPatch` or `staticDel`; a mutation at build time is not a
82
+ thing the generated client offers.
83
+
84
+ Both paths run with `bubbleErrors: true`, so a failing wiring **throws** in your
85
+ Server Component rather than resolving to an error status. Wrap the call, or let
86
+ the Next.js error boundary take it.
65
87
 
66
88
  ## How It Works
67
89
 
@@ -73,6 +95,28 @@ import { PikkuNextJS } from '@pikku/next'
73
95
  const pikku = new PikkuNextJS(createConfig, createSingletonServices)
74
96
  ```
75
97
 
76
- **Constructor:** `new PikkuNextJS(createConfig?, createSingletonServices)`
98
+ **Constructor:** `new PikkuNextJS(createConfig | undefined, createSingletonServices)`
99
+
100
+ Both arguments are positional and `createConfig` is only optional in the sense
101
+ that passing `undefined` substitutes an empty config —
102
+ `createSingletonServices` is required.
103
+
104
+ Initialization is memoized on a promise, so concurrent first requests share one
105
+ setup; a failed setup clears the promise, so the next request retries rather
106
+ than caching the failure forever.
107
+
108
+ The generated `pikku-nextjs.gen.ts` wraps this with full type safety from your
109
+ route definitions.
110
+
111
+ ## Related exports
77
112
 
78
- The generated `pikku-nextjs.gen.ts` wraps this with full type safety from your route definitions.
113
+ - **`PikkuNextJSWorkerRPC({ fetcher })`** — same surface as `PikkuNextJS`, but
114
+ every call is dispatched through a `Fetcher` (a Cloudflare service binding, a
115
+ local HTTP client, a fabric dispatcher) instead of loading function code
116
+ in-process. Use it to keep functions out of the SSR bundle. A non-2xx
117
+ response throws with the status and body text.
118
+ - **`toNextJsAuthHandler(auth)`** — wraps a better-auth instance or handler
119
+ function for an auth route. The three-argument form
120
+ `(pikkuAuthFactory, createConfig, createSingletonServices)` resolves a Pikku
121
+ better-auth factory lazily; it throws if you pass `createConfig` without
122
+ `createSingletonServices`. `nextCookies` is re-exported alongside it.
@@ -47,10 +47,52 @@ await appServer.start()
47
47
 
48
48
  **Config extends CoreConfig with:** `port`, `hostname`, `healthCheckPath?`
49
49
 
50
- **Methods:** `init(httpOptions?)`, `start()`, `stop()`, `enableExitOnSigInt()`
50
+ **Methods:** `init(httpOptions?: RunHTTPWiringOptions)`, `start()`, `stop()`, `enableExitOnSigInt()`
51
51
 
52
52
  **Property:** `app: uWS.App` — Direct access to uWebSockets app instance.
53
53
 
54
+ ### What the server does and does not give you
55
+
56
+ `init()` registers three things in order: the health check (`healthCheckPath`,
57
+ default `/health-check`), a catch-all `app.any('/*')` HTTP handler, and a
58
+ catch-all `app.ws('/*')` websocket handler. Nothing is registered by the
59
+ constructor, so nothing answers before `init` runs.
60
+
61
+ **There is no `enableCors`, no static assets and no `content` support** — unlike
62
+ the Express server. The class is explicitly a prototyping convenience; for
63
+ anything that needs extra handlers, use `@pikku/uws-handler` directly and treat
64
+ `pikku-uws-server.ts` as the template (that is what its own JSDoc says).
65
+
66
+ `httpOptions` reaches the HTTP handler only. The websocket handler is
67
+ constructed with a fixed `{ logger, logRoutes: true }`, so per-request options
68
+ do not apply to the upgrade path. `loadSchemas` is also never passed by the
69
+ server, so schemas compile lazily on first use rather than at startup — pass
70
+ `loadSchemas: true` in `httpOptions` if you want the startup cost paid up front.
71
+
72
+ `stop()` closes the listen socket and then waits a fixed 2 seconds for
73
+ connections to drain. Called before `start()`, it throws a bare **string**, not
74
+ an `Error`, so `catch (e) { e.message }` reads `undefined`.
75
+
76
+ ### Body limits
77
+
78
+ uWS hands over raw chunks with no limit of its own, so the handler counts the
79
+ bytes itself. A request over `maxBodySize` (default `DEFAULT_MAX_BODY_SIZE`) is
80
+ answered `413` with a `PayloadTooLargeError` body, and the chunks are dropped
81
+ rather than concatenated — an oversized request never accumulates in memory. A
82
+ `content-length` header that already exceeds the limit short-circuits before any
83
+ data arrives.
84
+
85
+ ### Handlers directly (own uWS app)
86
+
87
+ ```typescript
88
+ import { pikkuHTTPHandler, pikkuWebsocketHandler } from '@pikku/uws-handler'
89
+
90
+ app.any('/*', pikkuHTTPHandler({ logger, logRoutes: true, loadSchemas: true }))
91
+ app.ws('/*', pikkuWebsocketHandler({ logger, logRoutes: true }))
92
+ ```
93
+
94
+ Both take `{ logger, logRoutes?, loadSchemas? } & RunHTTPWiringOptions`.
95
+
54
96
  ## WebSocket Standalone (ws library)
55
97
 
56
98
  For WebSocket-only servers using the `ws` library:
@@ -86,3 +128,14 @@ process.on('SIGINT', async () => {
86
128
  process.exit(0)
87
129
  })
88
130
  ```
131
+
132
+ `pikkuWebsocketHandler` takes `{ server, wss, logger, logRoutes?, loadSchemas? }`
133
+ plus `RunHTTPWiringOptions`, and there is no server class in `@pikku/ws` — the
134
+ handler attaches to a `Server` you own.
135
+
136
+ `noServer: true` is required, not stylistic: the handler listens for the HTTP
137
+ server's own `upgrade` event, opens the channel (running middleware and auth
138
+ first), and only then calls `wss.handleUpgrade`. A `WebSocketServer` bound to
139
+ the server would take the socket before any of that ran. An upgrade the channel
140
+ rejects gets the socket destroyed, and an auth failure is written as a real HTTP
141
+ response on the raw socket rather than a silent drop.
@@ -36,19 +36,29 @@ installGroups: [core]
36
36
 
37
37
  - `pikku audit` — reports **security advisories** only.
38
38
  - `pikku audit --outdated` — also reports **available dependency updates**.
39
- - Package-manager detection is by **lockfile** (walks up: `bun.lock`/`bun.lockb`,
40
- then `yarn.lock`). Only **bun** runs a real audit (`bun audit --json` +
39
+ - Package-manager detection is by **lockfile**, walking up to 12 levels to the
40
+ workspace root, checking in this order: `bun.lock`/`bun.lockb`,
41
+ `pnpm-lock.yaml`, `yarn.lock`, `package-lock.json`. A project with several
42
+ lockfiles resolves as bun. Only **bun** runs a real audit (`bun audit --json` +
41
43
  `bun outdated`, normalised into one `SecurityAuditReport` with per-severity /
42
44
  per-update-level counts). Other PMs are detected but **stubbed** with a `note`
43
45
  field until their shapes are normalised — issues/updates come back empty.
44
- - `bun audit` exits non-zero when it *finds* advisories but still writes a valid
45
- report — treat any non-zero exit as data, not failure.
46
+ - `bun audit` exits non-zero when it *finds* advisories but still writes the
47
+ payload to stdout, so a non-zero exit **with output** is data. A non-zero exit
48
+ with **no** output — or a launch failure, timeout, or a blown 32MB buffer —
49
+ throws, precisely so a failed run can't masquerade as "0 advisories".
46
50
 
47
51
  ## Console integration (@pikku/addon-console)
48
52
 
49
53
  Three RPCs, all reading/writing the same artifact via the meta service. Shared
50
54
  spawn/read helpers live in `lib/audit-exec.ts` (`readAuditReport`,
51
- `runPikkuAudit`, `spawnProcess`, `findBin`) — reuse them, don't re-implement.
55
+ `runPikkuAudit`, `spawnProcess`, `findBin`), alongside `lib/find-project-root.ts`
56
+ and `lib/resolve-package-manager.ts` (`resolvePackageManager`, `installArgs`,
57
+ `execPrefix`) — reuse them, don't re-implement. `resolvePackageManager` reads
58
+ package.json's corepack `packageManager` field first and only falls back to
59
+ lockfiles, because that field states intent before a lockfile exists and a
60
+ project can carry a stale one from another tool. Guessing wrong is not a soft
61
+ failure: the spawn dies with `Executable not found in $PATH`.
52
62
  Like every console RPC these require an **authenticated session** (the console
53
63
  is admin-only), so the host must have Better Auth wired — see `pikku-better-auth`.
54
64
 
@@ -84,15 +94,26 @@ is admin-only), so the host must have Better Auth wired — see `pikku-better-au
84
94
 
85
95
  ```ts
86
96
  {
97
+ schemaVersion: number
87
98
  tool: string // e.g. 'bun'
99
+ generatedAt: string // ISO timestamp
88
100
  note?: string // set when the audit could NOT run (unsupported PM);
89
101
  // render ONLY the note — never a reassuring "no vulnerabilities"
90
- summary: { critical, high, moderate, low, info: number }
91
- issues: SecurityAuditIssue[] // package, severity, title, advisoryId, cwe[], cvssScore?,
92
- // url?, vulnerableVersions, recommendedVersion?
102
+ summary: {
103
+ totalIssues, critical, high, moderate, low: number // no `info` bucket
104
+ totalUpdates, major, minor, patch: number
105
+ }
106
+ issues: SecurityAuditIssue[] // package, severity, title, advisoryId, url,
107
+ // vulnerableVersions, cwe[], cvssScore, recommendedVersion
93
108
  updates: SecurityAuditUpdate[] // package, current, latest, level (major|minor|patch|unknown)
94
109
  }
95
110
  ```
96
111
 
112
+ `severity` is one of `critical | high | moderate | low | info`, but `summary`
113
+ has no `info` count — an informational advisory raises `totalIssues` without
114
+ landing in a severity bucket, so don't sum the four to get the total. On an
115
+ issue, `url`, `cvssScore` and `recommendedVersion` are always present and
116
+ **nullable** rather than optional: check for `null`, not `undefined`.
117
+
97
118
  When `note` is present the audit did not run — show only the note (an "Audit not
98
119
  run" state), never the "no known vulnerabilities / up to date" copy.