@pikku/core 0.12.0 → 0.12.2
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.
- package/CHANGELOG.md +40 -0
- package/dist/errors/errors.d.ts +6 -0
- package/dist/errors/errors.js +10 -0
- package/dist/errors/index.d.ts +1 -0
- package/dist/errors/index.js +1 -0
- package/dist/function/function-runner.d.ts +3 -3
- package/dist/function/function-runner.js +66 -44
- package/dist/function/functions.types.d.ts +1 -0
- package/dist/handle-error.d.ts +2 -2
- package/dist/handle-error.js +2 -2
- package/dist/index.d.ts +5 -3
- package/dist/index.js +2 -1
- package/dist/internal.d.ts +3 -0
- package/dist/internal.js +2 -0
- package/dist/middleware/auth-bearer.d.ts +1 -1
- package/dist/middleware/auth-bearer.js +1 -1
- package/dist/middleware/auth-cookie.d.ts +2 -2
- package/dist/middleware/auth-cookie.js +1 -1
- package/dist/middleware/cors.d.ts +2 -2
- package/dist/middleware/cors.js +45 -37
- package/dist/middleware-runner.d.ts +1 -1
- package/dist/middleware-runner.js +1 -0
- package/dist/permissions.d.ts +2 -2
- package/dist/permissions.js +1 -0
- package/dist/pikku-state.d.ts +9 -8
- package/dist/pikku-state.js +34 -17
- package/dist/schema.d.ts +4 -4
- package/dist/schema.js +9 -5
- package/dist/services/ai-agent-runner-service.d.ts +22 -3
- package/dist/services/ai-run-state-service.d.ts +1 -1
- package/dist/services/ai-storage-service.d.ts +1 -1
- package/dist/services/content-service.d.ts +6 -0
- package/dist/services/gateway-service.d.ts +19 -0
- package/dist/services/gopass-secrets.d.ts +1 -1
- package/dist/services/gopass-secrets.js +3 -0
- package/dist/services/in-memory-ai-run-state-service.d.ts +15 -0
- package/dist/services/in-memory-ai-run-state-service.js +44 -0
- package/dist/services/in-memory-trigger-service.d.ts +0 -1
- package/dist/services/in-memory-trigger-service.js +6 -8
- package/dist/services/index.d.ts +5 -2
- package/dist/services/index.js +2 -0
- package/dist/services/local-content.d.ts +2 -1
- package/dist/services/local-content.js +6 -1
- package/dist/services/local-gateway-service.d.ts +22 -0
- package/dist/services/local-gateway-service.js +51 -0
- package/dist/services/local-secrets.d.ts +2 -2
- package/dist/services/local-variables.d.ts +1 -1
- package/dist/services/logger-console.d.ts +2 -1
- package/dist/services/scheduler-service.d.ts +0 -12
- package/dist/services/scheduler-service.js +0 -6
- package/dist/services/scoped-secret-service.d.ts +1 -1
- package/dist/services/trigger-service.d.ts +0 -7
- package/dist/services/user-session-service.d.ts +3 -3
- package/dist/services/workflow-service.d.ts +2 -3
- package/dist/types/core.types.d.ts +27 -19
- package/dist/types/state.types.d.ts +22 -15
- package/dist/utils.d.ts +5 -5
- package/dist/utils.js +12 -9
- package/dist/wirings/ai-agent/ai-agent-assistant-ui.d.ts +5 -0
- package/dist/wirings/ai-agent/ai-agent-assistant-ui.js +168 -0
- package/dist/wirings/ai-agent/ai-agent-helpers.d.ts +28 -0
- package/dist/wirings/ai-agent/ai-agent-helpers.js +40 -0
- package/dist/wirings/ai-agent/ai-agent-memory.js +13 -2
- package/dist/wirings/ai-agent/ai-agent-prepare.d.ts +4 -5
- package/dist/wirings/ai-agent/ai-agent-prepare.js +80 -31
- package/dist/wirings/ai-agent/ai-agent-registry.d.ts +1 -1
- package/dist/wirings/ai-agent/ai-agent-runner.js +107 -8
- package/dist/wirings/ai-agent/ai-agent-stream.d.ts +2 -1
- package/dist/wirings/ai-agent/ai-agent-stream.js +263 -89
- package/dist/wirings/ai-agent/ai-agent.types.d.ts +96 -4
- package/dist/wirings/ai-agent/index.d.ts +3 -1
- package/dist/wirings/ai-agent/index.js +2 -0
- package/dist/wirings/channel/channel-common.d.ts +2 -2
- package/dist/wirings/channel/channel-handler.d.ts +7 -4
- package/dist/wirings/channel/channel-handler.js +15 -5
- package/dist/wirings/channel/channel-middleware-runner.js +1 -0
- package/dist/wirings/channel/channel-runner.d.ts +5 -5
- package/dist/wirings/channel/channel-runner.js +3 -2
- package/dist/wirings/channel/channel-store.d.ts +1 -1
- package/dist/wirings/channel/channel.types.d.ts +10 -6
- package/dist/wirings/channel/index.d.ts +1 -1
- package/dist/wirings/channel/local/local-channel-handler.d.ts +7 -0
- package/dist/wirings/channel/local/local-channel-handler.js +17 -0
- package/dist/wirings/channel/local/local-channel-runner.d.ts +7 -2
- package/dist/wirings/channel/local/local-channel-runner.js +18 -4
- package/dist/wirings/channel/local/local-eventhub-service.d.ts +2 -2
- package/dist/wirings/channel/log-channels.d.ts +1 -1
- package/dist/wirings/channel/pikku-abstract-channel-handler.d.ts +2 -1
- package/dist/wirings/channel/pikku-abstract-channel-handler.js +1 -0
- package/dist/wirings/channel/serverless/serverless-channel-runner.d.ts +4 -4
- package/dist/wirings/channel/serverless/serverless-channel-runner.js +26 -20
- package/dist/wirings/cli/channel/cli-channel-runner.d.ts +1 -1
- package/dist/wirings/cli/cli-runner.d.ts +2 -2
- package/dist/wirings/cli/cli-runner.js +3 -0
- package/dist/wirings/cli/cli.types.d.ts +3 -3
- package/dist/wirings/cli/command-parser.d.ts +1 -1
- package/dist/wirings/gateway/gateway-runner.d.ts +24 -0
- package/dist/wirings/gateway/gateway-runner.js +325 -0
- package/dist/wirings/gateway/gateway.types.d.ts +127 -0
- package/dist/wirings/gateway/index.d.ts +2 -0
- package/dist/wirings/gateway/index.js +1 -0
- package/dist/wirings/http/http-runner.d.ts +9 -9
- package/dist/wirings/http/http-runner.js +36 -14
- package/dist/wirings/http/http.types.d.ts +2 -6
- package/dist/wirings/http/log-http-routes.d.ts +1 -1
- package/dist/wirings/http/pikku-fetch-http-request.d.ts +1 -1
- package/dist/wirings/http/pikku-fetch-http-response.d.ts +2 -2
- package/dist/wirings/http/pikku-fetch-http-response.js +6 -1
- package/dist/wirings/http/routers/http-router.d.ts +1 -1
- package/dist/wirings/http/routers/path-to-regex.d.ts +2 -2
- package/dist/wirings/mcp/mcp-endpoint-registry.d.ts +1 -1
- package/dist/wirings/mcp/mcp-runner.d.ts +0 -3
- package/dist/wirings/mcp/mcp-runner.js +4 -2
- package/dist/wirings/mcp/mcp.types.d.ts +2 -2
- package/dist/wirings/oauth2/oauth2-client.d.ts +3 -3
- package/dist/wirings/oauth2/wire-oauth2-credential.d.ts +1 -1
- package/dist/wirings/queue/index.d.ts +1 -1
- package/dist/wirings/queue/index.js +1 -1
- package/dist/wirings/queue/queue-runner.d.ts +1 -12
- package/dist/wirings/queue/queue-runner.js +4 -12
- package/dist/wirings/queue/queue.types.d.ts +3 -9
- package/dist/wirings/queue/register-queue-helper.d.ts +2 -2
- package/dist/wirings/queue/register-queue-helper.js +1 -1
- package/dist/wirings/queue/validate-worker-config.d.ts +1 -1
- package/dist/wirings/rpc/index.d.ts +2 -0
- package/dist/wirings/rpc/index.js +1 -0
- package/dist/wirings/rpc/rpc-runner.d.ts +36 -14
- package/dist/wirings/rpc/rpc-runner.js +56 -28
- package/dist/wirings/rpc/rpc-types.d.ts +14 -2
- package/dist/wirings/rpc/wire-addon.d.ts +10 -0
- package/dist/wirings/rpc/wire-addon.js +9 -0
- package/dist/wirings/scheduler/index.d.ts +1 -1
- package/dist/wirings/scheduler/index.js +1 -1
- package/dist/wirings/scheduler/log-schedulers.d.ts +1 -1
- package/dist/wirings/scheduler/scheduler-runner.d.ts +3 -10
- package/dist/wirings/scheduler/scheduler-runner.js +4 -25
- package/dist/wirings/scheduler/scheduler.types.d.ts +2 -2
- package/dist/wirings/trigger/pikku-trigger-service.d.ts +2 -6
- package/dist/wirings/trigger/pikku-trigger-service.js +11 -16
- package/dist/wirings/trigger/trigger-runner.d.ts +1 -5
- package/dist/wirings/trigger/trigger-runner.js +4 -3
- package/dist/wirings/workflow/pikku-workflow-service.d.ts +2 -6
- package/dist/wirings/workflow/pikku-workflow-service.js +20 -29
- package/dist/wirings/workflow/workflow.types.d.ts +2 -2
- package/package.json +4 -2
- package/src/crypto-utils.test.ts +116 -0
- package/src/errors/error.test.ts +143 -2
- package/src/errors/errors.ts +10 -0
- package/src/errors/index.ts +1 -0
- package/src/function/function-runner.test.ts +2 -2
- package/src/function/function-runner.ts +72 -49
- package/src/function/functions.types.ts +1 -0
- package/src/handle-error.test.ts +361 -0
- package/src/handle-error.ts +4 -4
- package/src/index.ts +3 -10
- package/src/internal.ts +6 -0
- package/src/middleware/auth-apikey.test.ts +1 -1
- package/src/middleware/auth-bearer.test.ts +1 -1
- package/src/middleware/auth-bearer.ts +2 -5
- package/src/middleware/auth-cookie.test.ts +1 -1
- package/src/middleware/auth-cookie.ts +3 -5
- package/src/middleware/cors.test.ts +424 -0
- package/src/middleware/cors.ts +14 -6
- package/src/middleware/remote-auth.test.ts +488 -0
- package/src/middleware-runner.test.ts +1 -1
- package/src/middleware-runner.ts +2 -1
- package/src/permissions.test.ts +2 -2
- package/src/permissions.ts +3 -2
- package/src/pikku-state.test.ts +224 -0
- package/src/pikku-state.ts +45 -28
- package/src/schema.test.ts +198 -6
- package/src/schema.ts +11 -7
- package/src/services/ai-agent-runner-service.ts +15 -3
- package/src/services/ai-run-state-service.ts +1 -1
- package/src/services/ai-storage-service.ts +1 -1
- package/src/services/content-service.ts +7 -0
- package/src/services/gateway-service.ts +20 -0
- package/src/services/gopass-secrets.ts +4 -1
- package/src/services/in-memory-ai-run-state-service.ts +73 -0
- package/src/services/in-memory-trigger-service.ts +6 -10
- package/src/services/in-memory-workflow-service.test.ts +351 -0
- package/src/services/index.ts +4 -1
- package/src/services/local-content.ts +9 -3
- package/src/services/local-gateway-service.ts +62 -0
- package/src/services/local-secrets.test.ts +80 -0
- package/src/services/local-secrets.ts +2 -2
- package/src/services/local-variables.test.ts +61 -0
- package/src/services/local-variables.ts +1 -1
- package/src/services/logger-console.test.ts +118 -0
- package/src/services/logger-console.ts +2 -1
- package/src/services/scheduler-service.ts +0 -18
- package/src/services/scoped-secret-service.test.ts +74 -0
- package/src/services/scoped-secret-service.ts +1 -1
- package/src/services/trigger-service.ts +0 -21
- package/src/services/typed-secret-service.test.ts +93 -0
- package/src/services/typed-variables-service.test.ts +73 -0
- package/src/services/user-session-service.test.ts +113 -0
- package/src/services/user-session-service.ts +3 -3
- package/src/services/workflow-service.ts +2 -12
- package/src/time-utils.test.ts +1 -1
- package/src/types/core.types.ts +28 -19
- package/src/types/state.types.ts +32 -15
- package/src/utils.test.ts +350 -0
- package/src/utils.ts +14 -13
- package/src/wirings/ai-agent/ai-agent-assistant-ui.test.ts +363 -0
- package/src/wirings/ai-agent/ai-agent-assistant-ui.ts +238 -0
- package/src/wirings/ai-agent/ai-agent-helpers.test.ts +152 -0
- package/src/wirings/ai-agent/ai-agent-helpers.ts +76 -0
- package/src/wirings/ai-agent/ai-agent-memory.ts +10 -1
- package/src/wirings/ai-agent/ai-agent-model-config.test.ts +115 -0
- package/src/wirings/ai-agent/ai-agent-prepare.ts +98 -46
- package/src/wirings/ai-agent/ai-agent-registry.ts +1 -1
- package/src/wirings/ai-agent/ai-agent-runner.test.ts +245 -3
- package/src/wirings/ai-agent/ai-agent-runner.ts +121 -7
- package/src/wirings/ai-agent/ai-agent-stream.test.ts +430 -24
- package/src/wirings/ai-agent/ai-agent-stream.ts +390 -104
- package/src/wirings/ai-agent/ai-agent.types.ts +91 -4
- package/src/wirings/ai-agent/index.ts +13 -0
- package/src/wirings/channel/channel-common.ts +2 -2
- package/src/wirings/channel/channel-handler.ts +43 -11
- package/src/wirings/channel/channel-middleware-runner.ts +1 -0
- package/src/wirings/channel/channel-runner.ts +6 -6
- package/src/wirings/channel/channel-store.ts +1 -1
- package/src/wirings/channel/channel.types.ts +15 -8
- package/src/wirings/channel/index.ts +1 -0
- package/src/wirings/channel/local/local-channel-handler.ts +26 -0
- package/src/wirings/channel/local/local-channel-runner.test.ts +19 -12
- package/src/wirings/channel/local/local-channel-runner.ts +33 -14
- package/src/wirings/channel/local/local-eventhub-service.test.ts +2 -2
- package/src/wirings/channel/local/local-eventhub-service.ts +2 -2
- package/src/wirings/channel/log-channels.ts +1 -1
- package/src/wirings/channel/pikku-abstract-channel-handler.test.ts +2 -0
- package/src/wirings/channel/pikku-abstract-channel-handler.ts +8 -1
- package/src/wirings/channel/serverless/serverless-channel-runner.ts +38 -33
- package/src/wirings/cli/channel/cli-channel-runner.ts +1 -1
- package/src/wirings/cli/cli-runner.test.ts +1 -1
- package/src/wirings/cli/cli-runner.ts +14 -4
- package/src/wirings/cli/cli.types.ts +3 -3
- package/src/wirings/cli/command-parser.test.ts +1 -1
- package/src/wirings/cli/command-parser.ts +1 -1
- package/src/wirings/gateway/gateway-runner.test.ts +474 -0
- package/src/wirings/gateway/gateway-runner.ts +411 -0
- package/src/wirings/gateway/gateway.types.ts +149 -0
- package/src/wirings/gateway/index.ts +13 -0
- package/src/wirings/http/http-routes.test.ts +1 -1
- package/src/wirings/http/http-runner.test.ts +9 -17
- package/src/wirings/http/http-runner.ts +46 -27
- package/src/wirings/http/http.types.ts +1 -14
- package/src/wirings/http/log-http-routes.ts +1 -1
- package/src/wirings/http/pikku-fetch-http-request.ts +1 -1
- package/src/wirings/http/pikku-fetch-http-response.ts +12 -5
- package/src/wirings/http/routers/http-router.ts +1 -1
- package/src/wirings/http/routers/path-to-regex.test.ts +1 -1
- package/src/wirings/http/routers/path-to-regex.ts +4 -3
- package/src/wirings/mcp/mcp-endpoint-registry.ts +5 -1
- package/src/wirings/mcp/mcp-runner.ts +9 -15
- package/src/wirings/mcp/mcp.types.ts +2 -2
- package/src/wirings/oauth2/oauth2-client.ts +3 -3
- package/src/wirings/oauth2/wire-oauth2-credential.ts +1 -1
- package/src/wirings/queue/index.ts +0 -1
- package/src/wirings/queue/queue-runner.test.ts +52 -25
- package/src/wirings/queue/queue-runner.ts +8 -30
- package/src/wirings/queue/queue.types.ts +3 -13
- package/src/wirings/queue/register-queue-helper.ts +3 -5
- package/src/wirings/queue/validate-worker-config.test.ts +108 -0
- package/src/wirings/queue/validate-worker-config.ts +4 -1
- package/src/wirings/rpc/index.ts +2 -0
- package/src/wirings/rpc/rpc-runner.ts +92 -49
- package/src/wirings/rpc/rpc-types.ts +17 -2
- package/src/wirings/rpc/wire-addon.ts +20 -0
- package/src/wirings/scheduler/index.ts +0 -1
- package/src/wirings/scheduler/log-schedulers.ts +1 -1
- package/src/wirings/scheduler/scheduler-runner.test.ts +49 -60
- package/src/wirings/scheduler/scheduler-runner.ts +9 -56
- package/src/wirings/scheduler/scheduler.types.ts +2 -2
- package/src/wirings/secret/validate-secret-definitions.test.ts +140 -0
- package/src/wirings/trigger/pikku-trigger-service.ts +17 -39
- package/src/wirings/trigger/trigger-runner.test.ts +79 -0
- package/src/wirings/trigger/trigger-runner.ts +8 -11
- package/src/wirings/variable/validate-variable-definitions.test.ts +91 -0
- package/src/wirings/workflow/graph/graph-runner.test.ts +14 -22
- package/src/wirings/workflow/graph/template.test.ts +49 -0
- package/src/wirings/workflow/pikku-workflow-service.test.ts +15 -27
- package/src/wirings/workflow/pikku-workflow-service.ts +29 -45
- package/src/wirings/workflow/workflow-helpers.test.ts +129 -0
- package/src/wirings/workflow/workflow.types.ts +2 -2
- package/tsconfig.tsbuildinfo +1 -1
- package/src/wirings/workflow/workflow-utils.ts +0 -0
- /package/dist/{wirings/workflow/workflow-utils.d.ts → services/gateway-service.js} +0 -0
- /package/dist/wirings/{workflow/workflow-utils.js → gateway/gateway.types.js} +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,45 @@
|
|
|
1
1
|
## 0.12.0
|
|
2
2
|
|
|
3
|
+
## 0.12.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- cc4c9e9: Add gateway meta-wiring for messaging platforms:
|
|
8
|
+
|
|
9
|
+
- New `wireGateway()` API with three transport types: webhook, websocket, listener
|
|
10
|
+
- `GatewayAdapter` interface for platform-specific parse/send logic
|
|
11
|
+
- `PikkuGateway` wire object (`wire.gateway`) with senderId, platform, and send()
|
|
12
|
+
- `GatewayService` interface and `LocalGatewayService` for listener gateway lifecycle
|
|
13
|
+
- `createListenerMessageHandler()` helper for building listener message callbacks
|
|
14
|
+
- Add `'gateway'` to `PikkuWiringTypes` and `gateway` to `PikkuWire`
|
|
15
|
+
- Add `gateway` state block to `PikkuPackageState`
|
|
16
|
+
|
|
17
|
+
- 3e04565: chore: update dependencies to latest minor/patch versions
|
|
18
|
+
|
|
19
|
+
## 0.12.1
|
|
20
|
+
|
|
21
|
+
### Patch Changes
|
|
22
|
+
|
|
23
|
+
- 62a8725: Rename 'external' to 'addon' throughout the codebase. All types, functions, config keys, and CLI options previously named `external` or `External` are now named `addon` or `Addon` (e.g. `ExternalPackageConfig` → `AddonConfig`, `externalPackages` → `addons`, `function-external` → `function-addon`).
|
|
24
|
+
- a3bdb0d: Add AI middleware hooks for per-tool-call lifecycle and post-step observability:
|
|
25
|
+
|
|
26
|
+
- `beforeToolCall` / `afterToolCall`: per-tool-call hooks for logging, caching, input sanitization, and result transformation
|
|
27
|
+
- `afterStep`: post-step observation hook with full step context (text, toolCalls, toolResults, usage, finishReason)
|
|
28
|
+
- `onError`: error-specific hook for alerting and diagnostics (non-throwing, won't affect error flow)
|
|
29
|
+
|
|
30
|
+
- e0349ff: Fix critical security vulnerability in channel message handler: `validateAuth` was being called with `channelHandler` (always truthy) instead of the actual user session, meaning auth checks always passed and unauthenticated clients could send messages to protected channels. Also fix an information disclosure issue where the full channel config object was being logged on unhandled messages.
|
|
31
|
+
- 62a8725: Internalize singleton services management in the serverless channel runner, consistent with how other runners handle it. `createWireServices` and `singletonServices` no longer need to be passed explicitly to serverless channel runner calls.
|
|
32
|
+
- e04531f: Security hardening: improve CORS handling, redirect validation, and error logging in the HTTP runner. Export additional internal utilities needed by native runtime adapters.
|
|
33
|
+
- 62a8725: Fix security issue in `function-runner`: functions declared with `pikkuFunc` (which always require a session) now always throw `ForbiddenError` when called without a session, even if the wiring has `auth: false`. Previously a misconfigured wiring could bypass authentication entirely — the runner only logged a warning instead of blocking the call.
|
|
34
|
+
- a83efb8: Handle OPTIONS preflight requests automatically in fetchData when no explicit OPTIONS route is matched. Runs global HTTP middleware (e.g. CORS) and returns 204. Remove redundant startWorkflowRun and streamAgentRun pass-through functions from addon-console.
|
|
35
|
+
- 8eed717: Add `readonly` flag to function config and runtime enforcement. Functions can be marked `readonly: true` in their config. At runtime, if a session has `readonly: true`, only functions marked as readonly can be called — otherwise a `ReadonlySessionError` (403) is thrown.
|
|
36
|
+
- 62a8725: `pikku versions check` now prints rich, human-readable output for all contract version errors instead of raw error codes. Each error type (PKU861–PKU865) shows the function name, separate input/output schema hashes with a `prev → current` arrow, and clear next-step instructions.
|
|
37
|
+
|
|
38
|
+
The version manifest now stores separate `inputHash` and `outputHash` per version entry (backward-compatible — old string-hash manifests still load and validate correctly). `VersionValidateError` gains optional detail fields (`functionKey`, `version`, `previousInputHash`, `currentInputHash`, `previousOutputHash`, `currentOutputHash`, `nextVersion`, `latestVersion`, `expectedNextVersion`) for use by tooling.
|
|
39
|
+
|
|
40
|
+
- 62a8725: Replace config-based addon declarations with the new `wireAddon()` code-based API. Addons are now declared directly in wiring files using `wireAddon({ name, package, rpcEndpoint?, auth?, tags? })` instead of the `addons` field in `pikku.config.json`. The inspector reads these declarations from the TypeScript AST at build time.
|
|
41
|
+
- 62a8725: Add `secretOverrides` and `variableOverrides` support to `wireAddon()`. These optional maps allow an app to remap an addon's secret/variable keys to its own names (e.g. `secretOverrides: { SENDGRID_API_KEY: 'MY_EMAIL_API_KEY' }`). The inspector validates that all override keys exist in the app's own secrets/variables definitions.
|
|
42
|
+
|
|
3
43
|
### New Features
|
|
4
44
|
|
|
5
45
|
- AI agents with `pikkuAIAgent()` — define agents with tools, sub-agents, memory, structured output, and streaming via SSE
|
package/dist/errors/errors.d.ts
CHANGED
|
@@ -42,6 +42,12 @@ export declare class PaymentRequiredError extends PikkuError {
|
|
|
42
42
|
*/
|
|
43
43
|
export declare class ForbiddenError extends PikkuError {
|
|
44
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* The session is readonly and cannot access a non-readonly function.
|
|
47
|
+
* @group Error
|
|
48
|
+
*/
|
|
49
|
+
export declare class ReadonlySessionError extends PikkuError {
|
|
50
|
+
}
|
|
45
51
|
/**
|
|
46
52
|
* The request was made from an origin that is not permitted to access this resource.
|
|
47
53
|
* @group Error
|
package/dist/errors/errors.js
CHANGED
|
@@ -72,6 +72,16 @@ addError(ForbiddenError, {
|
|
|
72
72
|
status: 403,
|
|
73
73
|
message: 'The client does not have permission to access the requested resource.',
|
|
74
74
|
});
|
|
75
|
+
/**
|
|
76
|
+
* The session is readonly and cannot access a non-readonly function.
|
|
77
|
+
* @group Error
|
|
78
|
+
*/
|
|
79
|
+
export class ReadonlySessionError extends PikkuError {
|
|
80
|
+
}
|
|
81
|
+
addError(ReadonlySessionError, {
|
|
82
|
+
status: 403,
|
|
83
|
+
message: 'The session is readonly and cannot access this function.',
|
|
84
|
+
});
|
|
75
85
|
/**
|
|
76
86
|
* The request was made from an origin that is not permitted to access this resource.
|
|
77
87
|
* @group Error
|
package/dist/errors/index.d.ts
CHANGED
package/dist/errors/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { CoreServices, CoreUserSession, CorePikkuMiddleware, PikkuWiringTypes, PikkuWire, MiddlewareMetadata, PermissionMetadata, CoreSingletonServices, CreateWireServices } from '../types/core.types.js';
|
|
1
|
+
import type { CoreServices, CoreUserSession, CorePikkuMiddleware, PikkuWiringTypes, PikkuWire, MiddlewareMetadata, PermissionMetadata, CoreSingletonServices, CreateWireServices } from '../types/core.types.js';
|
|
2
2
|
import type { CorePikkuChannelMiddleware } from '../wirings/channel/channel.types.js';
|
|
3
|
-
import { CorePermissionGroup, CorePikkuFunctionConfig, CorePikkuPermission } from './functions.types.js';
|
|
4
|
-
import { SessionService } from '../services/user-session-service.js';
|
|
3
|
+
import type { CorePermissionGroup, CorePikkuFunctionConfig, CorePikkuPermission } from './functions.types.js';
|
|
4
|
+
import type { SessionService } from '../services/user-session-service.js';
|
|
5
5
|
export declare const addFunction: (funcName: string, funcConfig: CorePikkuFunctionConfig<any, any>, packageName?: string | null) => void;
|
|
6
6
|
export declare const getFunctionNames: (packageName?: string | null) => string[];
|
|
7
7
|
export declare const getAllFunctionNames: () => string[];
|
|
@@ -4,15 +4,15 @@ import { runPermissions } from '../permissions.js';
|
|
|
4
4
|
import { pikkuState } from '../pikku-state.js';
|
|
5
5
|
import { coerceTopLevelDataFromSchema, validateSchema } from '../schema.js';
|
|
6
6
|
import { parseVersionedId } from '../version.js';
|
|
7
|
-
import { createFunctionSessionWireProps
|
|
8
|
-
import { ForbiddenError } from '../errors/errors.js';
|
|
7
|
+
import { createFunctionSessionWireProps } from '../services/user-session-service.js';
|
|
8
|
+
import { ForbiddenError, ReadonlySessionError } from '../errors/errors.js';
|
|
9
9
|
import { rpcService } from '../wirings/rpc/rpc-runner.js';
|
|
10
10
|
import { closeWireServices } from '../utils.js';
|
|
11
11
|
/**
|
|
12
|
-
* Get or create singleton services for an
|
|
12
|
+
* Get or create singleton services for an addon package.
|
|
13
13
|
* Services are cached in pikkuState to avoid recreation on each call.
|
|
14
14
|
*
|
|
15
|
-
* @param packageName - The
|
|
15
|
+
* @param packageName - The addon package name
|
|
16
16
|
* @param parentServices - The parent/caller's singleton services (used as base)
|
|
17
17
|
* @returns The package's singleton services
|
|
18
18
|
*/
|
|
@@ -50,8 +50,8 @@ export const getAllFunctionNames = () => {
|
|
|
50
50
|
const functions = [];
|
|
51
51
|
const mainFunctionsMeta = pikkuState(null, 'function', 'meta');
|
|
52
52
|
functions.push(...Object.keys(mainFunctionsMeta));
|
|
53
|
-
const
|
|
54
|
-
for (const [namespace, config] of
|
|
53
|
+
const addons = pikkuState(null, 'rpc', 'addons');
|
|
54
|
+
for (const [namespace, config] of addons) {
|
|
55
55
|
const packageFunctionsMeta = pikkuState(config.package, 'function', 'meta');
|
|
56
56
|
for (const funcName of Object.keys(packageFunctionsMeta)) {
|
|
57
57
|
functions.push(`${namespace}:${funcName}`);
|
|
@@ -71,11 +71,8 @@ export const runPikkuFuncDirectly = async (funcName, allServices, wire, data, us
|
|
|
71
71
|
return (await funcConfig.func(allServices, data, wireWithSession));
|
|
72
72
|
};
|
|
73
73
|
export const runPikkuFunc = async (wireType, wireId, funcName, { singletonServices, createWireServices, data, auth: wiringAuth, inheritedMiddleware, wireMiddleware, inheritedChannelMiddleware, wireChannelMiddleware, inheritedPermissions, wirePermissions, coerceDataFromSchema, tags = [], wire, sessionService, packageName = null, }) => {
|
|
74
|
-
wire
|
|
75
|
-
|
|
76
|
-
wireType: wire.wireType ?? wireType,
|
|
77
|
-
wireId: wire.wireId ?? wireId,
|
|
78
|
-
};
|
|
74
|
+
wire.wireType ??= wireType;
|
|
75
|
+
wire.wireId ??= wireId;
|
|
79
76
|
const funcMap = pikkuState(packageName, 'function', 'functions');
|
|
80
77
|
let funcConfig = funcMap.get(funcName);
|
|
81
78
|
const allMeta = pikkuState(packageName, 'function', 'meta');
|
|
@@ -96,7 +93,7 @@ export const runPikkuFunc = async (wireType, wireId, funcName, { singletonServic
|
|
|
96
93
|
if (!funcMeta) {
|
|
97
94
|
throw new Error(`Function meta not found: ${funcName}`);
|
|
98
95
|
}
|
|
99
|
-
// For
|
|
96
|
+
// For addon packages, get or create their singleton services
|
|
100
97
|
const resolvedSingletonServices = packageName
|
|
101
98
|
? await getOrCreatePackageSingletonServices(packageName, singletonServices)
|
|
102
99
|
: singletonServices;
|
|
@@ -117,20 +114,26 @@ export const runPikkuFunc = async (wireType, wireId, funcName, { singletonServic
|
|
|
117
114
|
? wrapChannelWithMiddleware(wire, resolvedSingletonServices, allChannelMiddleware)
|
|
118
115
|
: wire;
|
|
119
116
|
// Convert tags to PermissionMetadata and merge with inheritedPermissions
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
117
|
+
let mergedInheritedPermissions;
|
|
118
|
+
if (tags && tags.length > 0) {
|
|
119
|
+
mergedInheritedPermissions = [
|
|
120
|
+
...(inheritedPermissions || []),
|
|
121
|
+
...tags.map((tag) => ({ type: 'tag', tag })),
|
|
122
|
+
];
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
mergedInheritedPermissions = inheritedPermissions || [];
|
|
126
|
+
}
|
|
124
127
|
// Helper function to run permissions and execute the function
|
|
125
128
|
const executeFunction = async () => {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
const session =
|
|
129
|
+
if (sessionService) {
|
|
130
|
+
resolvedWire.session = sessionService.freezeInitial();
|
|
131
|
+
resolvedWire.setSession = (s) => sessionService.set(s);
|
|
132
|
+
resolvedWire.clearSession = () => sessionService.clear();
|
|
133
|
+
resolvedWire.getSession = () => sessionService.get();
|
|
134
|
+
resolvedWire.hasSessionChanged = () => sessionService.sessionChanged;
|
|
135
|
+
}
|
|
136
|
+
const session = resolvedWire.session;
|
|
134
137
|
if (funcMeta.sessionless) {
|
|
135
138
|
if (wiringAuth === true || funcConfig.auth === true) {
|
|
136
139
|
if (!session) {
|
|
@@ -142,7 +145,7 @@ export const runPikkuFunc = async (wireType, wireId, funcName, { singletonServic
|
|
|
142
145
|
if (wiringAuth === false || funcConfig.auth === false) {
|
|
143
146
|
resolvedSingletonServices.logger.warn(`Function '${funcName}' requires a session but auth was explicitly disabled — use pikkuSessionlessFunc instead.`);
|
|
144
147
|
}
|
|
145
|
-
|
|
148
|
+
if (!session) {
|
|
146
149
|
throw new ForbiddenError('Authentication required');
|
|
147
150
|
}
|
|
148
151
|
}
|
|
@@ -155,6 +158,9 @@ export const runPikkuFunc = async (wireType, wireId, funcName, { singletonServic
|
|
|
155
158
|
}
|
|
156
159
|
}
|
|
157
160
|
}
|
|
161
|
+
if (session?.readonly && !funcMeta.readonly) {
|
|
162
|
+
throw new ReadonlySessionError();
|
|
163
|
+
}
|
|
158
164
|
// Evaluate the data from the lazy function
|
|
159
165
|
const actualData = await data();
|
|
160
166
|
// Validate and coerce data if schema is defined
|
|
@@ -167,29 +173,45 @@ export const runPikkuFunc = async (wireType, wireId, funcName, { singletonServic
|
|
|
167
173
|
coerceTopLevelDataFromSchema(inputSchemaName, actualData, packageName);
|
|
168
174
|
}
|
|
169
175
|
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
const rpc = rpcService.getContextRPCService(services, wireWithSession, {
|
|
184
|
-
sessionService,
|
|
176
|
+
if (mergedInheritedPermissions.length > 0 ||
|
|
177
|
+
wirePermissions ||
|
|
178
|
+
funcMeta.permissions ||
|
|
179
|
+
funcConfig.permissions) {
|
|
180
|
+
await runPermissions(wireType, wireId, {
|
|
181
|
+
wireInheritedPermissions: mergedInheritedPermissions,
|
|
182
|
+
wirePermissions: wirePermissions,
|
|
183
|
+
funcInheritedPermissions: funcMeta.permissions,
|
|
184
|
+
funcPermissions: funcConfig.permissions,
|
|
185
|
+
services: resolvedSingletonServices,
|
|
186
|
+
wire: resolvedWire,
|
|
187
|
+
data: actualData,
|
|
188
|
+
packageName,
|
|
185
189
|
});
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
190
|
+
}
|
|
191
|
+
const wireServices = await resolvedCreateWireServices?.(resolvedSingletonServices, resolvedWire);
|
|
192
|
+
try {
|
|
193
|
+
const services = wireServices && Object.keys(wireServices).length > 0
|
|
194
|
+
? { ...resolvedSingletonServices, ...wireServices }
|
|
195
|
+
: resolvedSingletonServices;
|
|
196
|
+
Object.defineProperty(resolvedWire, 'rpc', {
|
|
197
|
+
get() {
|
|
198
|
+
const rpc = rpcService.getContextRPCService(services, resolvedWire, {
|
|
199
|
+
sessionService,
|
|
200
|
+
});
|
|
201
|
+
Object.defineProperty(resolvedWire, 'rpc', {
|
|
202
|
+
value: rpc,
|
|
203
|
+
writable: true,
|
|
204
|
+
configurable: true,
|
|
205
|
+
});
|
|
206
|
+
return rpc;
|
|
207
|
+
},
|
|
208
|
+
configurable: true,
|
|
209
|
+
enumerable: true,
|
|
189
210
|
});
|
|
211
|
+
return await funcConfig.func(services, actualData, resolvedWire);
|
|
190
212
|
}
|
|
191
213
|
finally {
|
|
192
|
-
if (wireServices) {
|
|
214
|
+
if (wireServices && Object.keys(wireServices).length > 0) {
|
|
193
215
|
await closeWireServices(resolvedSingletonServices.logger, wireServices);
|
|
194
216
|
}
|
|
195
217
|
}
|
package/dist/handle-error.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Logger } from './services/logger.js';
|
|
2
|
-
import { PikkuHTTP } from './wirings/http/http.types.js';
|
|
1
|
+
import type { Logger } from './services/logger.js';
|
|
2
|
+
import type { PikkuHTTP } from './wirings/http/http.types.js';
|
|
3
3
|
/**
|
|
4
4
|
* Handle errors that occur during route processing
|
|
5
5
|
*
|
package/dist/handle-error.js
CHANGED
|
@@ -31,12 +31,12 @@ export const handleHTTPError = (e, http, trackerId, logger, logWarningsForStatus
|
|
|
31
31
|
if (trackerId) {
|
|
32
32
|
logger.warn(`Warning id: ${trackerId}`);
|
|
33
33
|
}
|
|
34
|
-
logger.warn(e);
|
|
34
|
+
logger.warn(e instanceof Error ? e.message : e);
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
else {
|
|
38
38
|
// Handle unexpected errors
|
|
39
|
-
logger.error(e);
|
|
39
|
+
logger.error(e instanceof Error ? e.message : e);
|
|
40
40
|
http?.response?.status(500);
|
|
41
41
|
if (trackerId) {
|
|
42
42
|
logger.warn(`Error id: ${trackerId}`);
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module @pikku/core
|
|
3
3
|
*/
|
|
4
|
-
export type { CommonWireMeta, CoreConfig, CorePikkuMiddleware, CorePikkuMiddlewareConfig, CorePikkuMiddlewareFactory, CorePikkuMiddlewareGroup, CoreServices, CoreSingletonServices, CoreUserSession, CreateConfig,
|
|
4
|
+
export type { CommonWireMeta, CoreConfig, CorePikkuMiddleware, CorePikkuMiddlewareConfig, CorePikkuMiddlewareFactory, CorePikkuMiddlewareGroup, CoreServices, CoreSingletonServices, CoreUserSession, CreateConfig, FunctionMeta, FunctionRuntimeMeta, FunctionServicesMeta, FunctionWiresMeta, FunctionsMeta, FunctionsRuntimeMeta, JSONPrimitive, JSONValue, MakeRequired, MiddlewareMetadata, PermissionMetadata, PickOptional, PickRequired, PikkuAIMiddlewareHooks, PikkuWire, PikkuWiringTypes, RequireAtLeastOne, SerializedError, WireServices, } from './types/core.types.js';
|
|
5
5
|
export { pikkuAIMiddleware, pikkuChannelMiddleware, pikkuChannelMiddlewareFactory, pikkuMiddleware, pikkuMiddlewareFactory, } from './types/core.types.js';
|
|
6
6
|
export type { CorePikkuAuth, CorePikkuAuthConfig, CorePikkuFunction, CorePikkuFunctionConfig, CorePikkuPermission, CorePikkuPermissionConfig, CorePikkuPermissionFactory, CorePermissionGroup, ZodLike, } from './function/functions.types.js';
|
|
7
7
|
export { pikkuAuth, pikkuPermission, pikkuPermissionFactory, } from './function/functions.types.js';
|
|
@@ -10,7 +10,6 @@ export { PikkuRequest } from './pikku-request.js';
|
|
|
10
10
|
export { getRelativeTimeOffsetFromNow, parseDurationString, } from './time-utils.js';
|
|
11
11
|
export type { RelativeTimeInput } from './time-utils.js';
|
|
12
12
|
export { formatVersionedId, isVersionedId, parseVersionedId, } from './version.js';
|
|
13
|
-
export { pikkuState, initializePikkuState, resetPikkuState, addPackageServiceFactories, getPikkuMetaDir, } from './pikku-state.js';
|
|
14
13
|
export { runPikkuFunc } from './function/function-runner.js';
|
|
15
14
|
export { runCLICommand, pikkuCLIRender } from './wirings/cli/cli-runner.js';
|
|
16
15
|
export { fetch } from './wirings/http/http-runner.js';
|
|
@@ -26,9 +25,12 @@ export type { HTTPMethod } from './wirings/http/http.types.js';
|
|
|
26
25
|
export type { GraphNodeConfig } from './wirings/workflow/graph/workflow-graph.types.js';
|
|
27
26
|
export { createGraph } from './wirings/workflow/graph/graph-node.js';
|
|
28
27
|
export { workflow as wireWorkflow } from './wirings/workflow/workflow-helpers.js';
|
|
28
|
+
export { wireAddon } from './wirings/rpc/wire-addon.js';
|
|
29
|
+
export type { WireAddonConfig } from './wirings/rpc/wire-addon.js';
|
|
29
30
|
export type { PikkuPackageState } from './types/state.types.js';
|
|
30
31
|
export { runMiddleware, addMiddleware } from './middleware-runner.js';
|
|
31
32
|
export { addPermission } from './permissions.js';
|
|
32
33
|
export { isSerializable, stopSingletonServices } from './utils.js';
|
|
33
|
-
export {
|
|
34
|
+
export { getSingletonServices, getCreateWireServices } from './pikku-state.js';
|
|
35
|
+
export { type ScheduledTaskInfo, type ScheduledTaskSummary, } from './services/scheduler-service.js';
|
|
34
36
|
export { SchedulerService } from './services/scheduler-service.js';
|
package/dist/index.js
CHANGED
|
@@ -4,7 +4,6 @@ export { addFunction, getAllFunctionNames } from './function/index.js';
|
|
|
4
4
|
export { PikkuRequest } from './pikku-request.js';
|
|
5
5
|
export { getRelativeTimeOffsetFromNow, parseDurationString, } from './time-utils.js';
|
|
6
6
|
export { formatVersionedId, isVersionedId, parseVersionedId, } from './version.js';
|
|
7
|
-
export { pikkuState, initializePikkuState, resetPikkuState, addPackageServiceFactories, getPikkuMetaDir, } from './pikku-state.js';
|
|
8
7
|
export { runPikkuFunc } from './function/function-runner.js';
|
|
9
8
|
export { runCLICommand, pikkuCLIRender } from './wirings/cli/cli-runner.js';
|
|
10
9
|
export { fetch } from './wirings/http/http-runner.js';
|
|
@@ -14,7 +13,9 @@ export { runScheduledTask } from './wirings/scheduler/scheduler-runner.js';
|
|
|
14
13
|
export { NotFoundError } from './errors/errors.js';
|
|
15
14
|
export { createGraph } from './wirings/workflow/graph/graph-node.js';
|
|
16
15
|
export { workflow as wireWorkflow } from './wirings/workflow/workflow-helpers.js';
|
|
16
|
+
export { wireAddon } from './wirings/rpc/wire-addon.js';
|
|
17
17
|
export { runMiddleware, addMiddleware } from './middleware-runner.js';
|
|
18
18
|
export { addPermission } from './permissions.js';
|
|
19
19
|
export { isSerializable, stopSingletonServices } from './utils.js';
|
|
20
|
+
export { getSingletonServices, getCreateWireServices } from './pikku-state.js';
|
|
20
21
|
export { SchedulerService } from './services/scheduler-service.js';
|
package/dist/internal.js
ADDED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { InvalidSessionError } from '../errors/errors.js';
|
|
2
|
-
import { pikkuMiddleware, pikkuMiddlewareFactory
|
|
2
|
+
import { pikkuMiddleware, pikkuMiddlewareFactory } from '../types/core.types.js';
|
|
3
3
|
/**
|
|
4
4
|
* Bearer token middleware that extracts and validates tokens from the Authorization header.
|
|
5
5
|
*
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { SerializeOptions } from 'cookie';
|
|
2
|
-
import { RelativeTimeInput } from '../time-utils.js';
|
|
1
|
+
import type { SerializeOptions } from 'cookie';
|
|
2
|
+
import type { RelativeTimeInput } from '../time-utils.js';
|
|
3
3
|
/**
|
|
4
4
|
* Cookie-based session middleware that uses JWT for encoding/decoding session data.
|
|
5
5
|
*
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { pikkuMiddleware, pikkuMiddlewareFactory } from '../types/core.types.js';
|
|
2
|
-
import { getRelativeTimeOffsetFromNow
|
|
2
|
+
import { getRelativeTimeOffsetFromNow } from '../time-utils.js';
|
|
3
3
|
/**
|
|
4
4
|
* Cookie-based session middleware that uses JWT for encoding/decoding session data.
|
|
5
5
|
*
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Sets appropriate CORS headers on all responses and short-circuits OPTIONS
|
|
5
5
|
* preflight requests with a 204 No Content response.
|
|
6
6
|
*
|
|
7
|
-
* @param options.origin - Allowed origin(s). Use `'*'` for any origin, a string for a single origin, or an array for multiple origins. Defaults to `'*'`.
|
|
7
|
+
* @param options.origin - Allowed origin(s). Use `'*'` for any origin, `true` to reflect the request origin, a string for a single origin, or an array for multiple origins. Defaults to `'*'`.
|
|
8
8
|
* @param options.methods - Allowed HTTP methods. Defaults to common methods.
|
|
9
9
|
* @param options.headers - Allowed request headers. Defaults to common headers.
|
|
10
10
|
* @param options.credentials - Whether to allow credentials. Defaults to `false`.
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
* ```
|
|
35
35
|
*/
|
|
36
36
|
export declare const cors: import("../types/core.types.js").CorePikkuMiddlewareFactory<{
|
|
37
|
-
origin?: string | string[];
|
|
37
|
+
origin?: string | string[] | true;
|
|
38
38
|
methods?: string[];
|
|
39
39
|
headers?: string[];
|
|
40
40
|
credentials?: boolean;
|
package/dist/middleware/cors.js
CHANGED
|
@@ -5,7 +5,7 @@ import { pikkuMiddleware, pikkuMiddlewareFactory } from '../types/core.types.js'
|
|
|
5
5
|
* Sets appropriate CORS headers on all responses and short-circuits OPTIONS
|
|
6
6
|
* preflight requests with a 204 No Content response.
|
|
7
7
|
*
|
|
8
|
-
* @param options.origin - Allowed origin(s). Use `'*'` for any origin, a string for a single origin, or an array for multiple origins. Defaults to `'*'`.
|
|
8
|
+
* @param options.origin - Allowed origin(s). Use `'*'` for any origin, `true` to reflect the request origin, a string for a single origin, or an array for multiple origins. Defaults to `'*'`.
|
|
9
9
|
* @param options.methods - Allowed HTTP methods. Defaults to common methods.
|
|
10
10
|
* @param options.headers - Allowed request headers. Defaults to common headers.
|
|
11
11
|
* @param options.credentials - Whether to allow credentials. Defaults to `false`.
|
|
@@ -34,40 +34,48 @@ import { pikkuMiddleware, pikkuMiddlewareFactory } from '../types/core.types.js'
|
|
|
34
34
|
* ])
|
|
35
35
|
* ```
|
|
36
36
|
*/
|
|
37
|
-
export const cors = pikkuMiddlewareFactory(({ origin = '*', methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'], headers = ['Content-Type', 'Authorization', 'x-api-key'], credentials = false, maxAge = 86400, } = {}) =>
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
37
|
+
export const cors = pikkuMiddlewareFactory(({ origin = '*', methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'], headers = ['Content-Type', 'Authorization', 'x-api-key'], credentials = false, maxAge = 86400, } = {}) => {
|
|
38
|
+
if (origin === '*' && credentials) {
|
|
39
|
+
throw new Error('CORS misconfiguration: wildcard origin (*) cannot be used with credentials: true');
|
|
40
|
+
}
|
|
41
|
+
return pikkuMiddleware({
|
|
42
|
+
name: 'CORS',
|
|
43
|
+
description: 'Handles cross-origin requests including OPTIONS preflight',
|
|
44
|
+
func: async (_services, wires, next) => {
|
|
45
|
+
const request = wires.http?.request;
|
|
46
|
+
const response = wires.http?.response;
|
|
47
|
+
if (!request || !response) {
|
|
48
|
+
return next();
|
|
49
|
+
}
|
|
50
|
+
const requestOrigin = request.header('origin');
|
|
51
|
+
let allowedOrigin;
|
|
52
|
+
if (origin === true) {
|
|
53
|
+
allowedOrigin = requestOrigin || '*';
|
|
54
|
+
}
|
|
55
|
+
else if (Array.isArray(origin)) {
|
|
56
|
+
allowedOrigin =
|
|
57
|
+
requestOrigin && origin.includes(requestOrigin)
|
|
58
|
+
? requestOrigin
|
|
59
|
+
: origin[0];
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
allowedOrigin = origin;
|
|
63
|
+
}
|
|
64
|
+
response.header('Access-Control-Allow-Origin', allowedOrigin);
|
|
65
|
+
response.header('Access-Control-Allow-Methods', methods.join(', '));
|
|
66
|
+
response.header('Access-Control-Allow-Headers', headers.join(', '));
|
|
67
|
+
if (credentials) {
|
|
68
|
+
response.header('Access-Control-Allow-Credentials', 'true');
|
|
69
|
+
}
|
|
70
|
+
if (origin === true || Array.isArray(origin)) {
|
|
71
|
+
response.header('Vary', 'Origin');
|
|
72
|
+
}
|
|
73
|
+
if (request.method() === 'options') {
|
|
74
|
+
response.header('Access-Control-Max-Age', String(maxAge));
|
|
75
|
+
response.status(204).json(undefined);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
44
78
|
return next();
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if (Array.isArray(origin)) {
|
|
49
|
-
allowedOrigin =
|
|
50
|
-
requestOrigin && origin.includes(requestOrigin)
|
|
51
|
-
? requestOrigin
|
|
52
|
-
: origin[0];
|
|
53
|
-
}
|
|
54
|
-
else {
|
|
55
|
-
allowedOrigin = origin;
|
|
56
|
-
}
|
|
57
|
-
response.header('Access-Control-Allow-Origin', allowedOrigin);
|
|
58
|
-
response.header('Access-Control-Allow-Methods', methods.join(', '));
|
|
59
|
-
response.header('Access-Control-Allow-Headers', headers.join(', '));
|
|
60
|
-
if (credentials) {
|
|
61
|
-
response.header('Access-Control-Allow-Credentials', 'true');
|
|
62
|
-
}
|
|
63
|
-
if (Array.isArray(origin)) {
|
|
64
|
-
response.header('Vary', 'Origin');
|
|
65
|
-
}
|
|
66
|
-
if (request.method() === 'options') {
|
|
67
|
-
response.header('Access-Control-Max-Age', String(maxAge));
|
|
68
|
-
response.status(204).json(undefined);
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
return next();
|
|
72
|
-
},
|
|
73
|
-
}));
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CoreSingletonServices, PikkuWire, CorePikkuMiddleware, CorePikkuMiddlewareGroup, PikkuWiringTypes, MiddlewareMetadata } from './types/core.types.js';
|
|
1
|
+
import type { CoreSingletonServices, PikkuWire, CorePikkuMiddleware, CorePikkuMiddlewareGroup, PikkuWiringTypes, MiddlewareMetadata } from './types/core.types.js';
|
|
2
2
|
/**
|
|
3
3
|
* Runs a chain of middleware functions in sequence before executing the main function.
|
|
4
4
|
*
|
package/dist/permissions.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { CoreServices, PikkuWiringTypes, PermissionMetadata, PikkuWire } from './types/core.types.js';
|
|
2
|
-
import { CorePermissionGroup, CorePikkuPermission } from './function/functions.types.js';
|
|
1
|
+
import type { CoreServices, PikkuWiringTypes, PermissionMetadata, PikkuWire } from './types/core.types.js';
|
|
2
|
+
import type { CorePermissionGroup, CorePikkuPermission } from './function/functions.types.js';
|
|
3
3
|
/**
|
|
4
4
|
* Adds global permissions for a specific tag.
|
|
5
5
|
*
|
package/dist/permissions.js
CHANGED
package/dist/pikku-state.d.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { PikkuPackageState } from './index.js';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
}
|
|
1
|
+
import type { PikkuPackageState } from './index.js';
|
|
2
|
+
import type { CoreSingletonServices, CreateWireServices } from './types/core.types.js';
|
|
3
|
+
export declare const getAllPackageStates: () => Map<string, PikkuPackageState>;
|
|
5
4
|
/**
|
|
6
5
|
* Get or set package-scoped pikku state
|
|
7
6
|
*
|
|
8
|
-
* @param packageName - Package name (null for main package, '@scope/package' for
|
|
7
|
+
* @param packageName - Package name (null for main package, '@scope/package' for addon packages)
|
|
9
8
|
* @param type - State category (function, rpc, http, etc.)
|
|
10
9
|
* @param content - Content key within the category
|
|
11
10
|
* @param value - Optional value to set
|
|
@@ -15,7 +14,7 @@ declare global {
|
|
|
15
14
|
* // Main package
|
|
16
15
|
* pikkuState(null, 'function', 'functions').get(funcName)
|
|
17
16
|
*
|
|
18
|
-
* //
|
|
17
|
+
* // Addon package
|
|
19
18
|
* pikkuState('@acme/stripe-functions', 'rpc', 'meta')
|
|
20
19
|
*/
|
|
21
20
|
export declare const pikkuState: <Type extends keyof PikkuPackageState, Content extends keyof PikkuPackageState[Type]>(packageName: string | null, type: Type, content: Content, value?: PikkuPackageState[Type][Content]) => PikkuPackageState[Type][Content];
|
|
@@ -25,11 +24,13 @@ export declare const pikkuState: <Type extends keyof PikkuPackageState, Content
|
|
|
25
24
|
export declare const initializePikkuState: (packageName: string) => void;
|
|
26
25
|
export declare const resetPikkuState: () => void;
|
|
27
26
|
export declare const getPikkuMetaDir: (packageName?: string | null) => string | null;
|
|
27
|
+
export declare const getSingletonServices: () => CoreSingletonServices;
|
|
28
|
+
export declare const getCreateWireServices: () => CreateWireServices | undefined;
|
|
28
29
|
/**
|
|
29
|
-
* Register service factories for an
|
|
30
|
+
* Register service factories for an addon package.
|
|
30
31
|
* These factories are used to create services when the package's functions are invoked.
|
|
31
32
|
*
|
|
32
|
-
* @param packageName - The package name (e.g., '@pikku/templates-function-
|
|
33
|
+
* @param packageName - The package name (e.g., '@pikku/templates-function-addon')
|
|
33
34
|
* @param factories - The service factory functions
|
|
34
35
|
*/
|
|
35
36
|
export declare const addPackageServiceFactories: (packageName: string, factories: NonNullable<PikkuPackageState["package"]["factories"]>) => void;
|