@peac/middleware-core 0.12.5 → 0.12.7

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 (2) hide show
  1. package/README.md +48 -39
  2. package/package.json +18 -6
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @peac/middleware-core
2
2
 
3
- Framework-agnostic middleware primitives for PEAC receipt issuance.
3
+ Framework-agnostic middleware primitives for PEAC receipt issuance. Provides signing, transport selection, and rate limiting for any HTTP framework.
4
4
 
5
5
  ## Installation
6
6
 
@@ -8,72 +8,81 @@ Framework-agnostic middleware primitives for PEAC receipt issuance.
8
8
  pnpm add @peac/middleware-core
9
9
  ```
10
10
 
11
- ## Quick Start
11
+ ## What It Does
12
+
13
+ `@peac/middleware-core` is a framework-agnostic library for issuing signed PEAC receipts from HTTP servers. It handles receipt creation, transport selection (header, body, or pointer), configuration validation, and rate limiting. Framework-specific packages such as `@peac/middleware-express` build on these primitives.
14
+
15
+ ## How Do I Use It?
16
+
17
+ ### Create a receipt for a request/response pair
12
18
 
13
19
  ```typescript
14
20
  import { createReceipt, validateConfig } from '@peac/middleware-core';
21
+ import type { MiddlewareConfig, RequestContext, ResponseContext } from '@peac/middleware-core';
15
22
 
16
- // Validate configuration at startup
17
- const config = {
23
+ const config: MiddlewareConfig = {
18
24
  issuer: 'https://api.example.com',
19
25
  signingKey: privateJwk, // Ed25519 JWK
20
26
  keyId: 'prod-2026-02',
21
27
  };
22
28
  validateConfig(config);
23
29
 
24
- // In a request handler
25
30
  const result = await createReceipt(config, requestCtx, responseCtx);
26
31
 
27
- // Add PEAC-Receipt header to response
28
32
  for (const [key, value] of Object.entries(result.headers)) {
29
33
  res.setHeader(key, value);
30
34
  }
31
35
  ```
32
36
 
33
- ## API
34
-
35
- ### `createReceipt(config, request, response)`
36
-
37
- Create a signed PEAC receipt for a request/response pair. Returns a `ReceiptResult` with the JWS receipt, transport headers, and optional body wrapper.
38
-
39
- ### `createReceiptWithClaims(config, claims)`
40
-
41
- Create a receipt from explicit claims (bypasses request/response context extraction).
42
-
43
- ### `wrapResponse(data, receipt)`
37
+ ### Select transport and wrap responses
44
38
 
45
- Wrap a response body with a PEAC receipt for body transport profile. Returns `{ data, peac_receipt }`.
39
+ ```typescript
40
+ import { selectTransport, wrapResponse, buildResponseHeaders } from '@peac/middleware-core';
41
+
42
+ const transport = selectTransport(receiptJws, config);
43
+
44
+ if (transport === 'body') {
45
+ const wrapped = wrapResponse(originalData, receiptJws);
46
+ res.json(wrapped); // { data, peac_receipt }
47
+ } else {
48
+ const headers = buildResponseHeaders(receiptJws);
49
+ for (const [key, value] of Object.entries(headers)) {
50
+ res.setHeader(key, value);
51
+ }
52
+ }
53
+ ```
46
54
 
47
- ### `selectTransport(receipt, config)`
55
+ ### Use rate limiting
48
56
 
49
- Determine appropriate transport based on receipt size: `'header'`, `'body'`, or `'pointer'`. Auto-falls back from header to body if receipt exceeds `maxHeaderSize` (default 4096 bytes).
57
+ ```typescript
58
+ import { MemoryRateLimitStore } from '@peac/middleware-core';
59
+ import type { RateLimitStore } from '@peac/middleware-core';
50
60
 
51
- ### `validateConfig(config)` / `validateConfigAsync(config)`
61
+ const limiter = new MemoryRateLimitStore({
62
+ maxRequests: 100,
63
+ windowMs: 60_000,
64
+ });
65
+ ```
52
66
 
53
- Validate middleware configuration at startup. Throws `ConfigError` if configuration is invalid.
67
+ ## Integrates With
54
68
 
55
- ### `buildResponseHeaders(receipt)` / `buildReceiptResult(receipt, transport, body?)`
69
+ - `@peac/kernel` (Layer 0): Types and constants
70
+ - `@peac/schema` (Layer 1): Zod validators for receipt claims
71
+ - `@peac/crypto` (Layer 2): Ed25519 signing
72
+ - `@peac/middleware-express`: Express.js integration built on these primitives
56
73
 
57
- Lower-level utilities for constructing response headers and receipt results.
74
+ ## For Agent Developers
58
75
 
59
- ## Configuration
76
+ If you are building an AI agent or integration that needs to issue receipts:
60
77
 
61
- ```typescript
62
- interface MiddlewareConfig {
63
- issuer: string; // Issuer URL (becomes `iss` claim)
64
- signingKey: Ed25519PrivateJwk; // Ed25519 private key (JWK format)
65
- keyId: string; // Key ID for JWKS lookup
66
- expiresIn?: number; // Receipt expiration in seconds (default: 300)
67
- transport?: 'header' | 'body' | 'pointer'; // Transport profile (default: 'header')
68
- maxHeaderSize?: number; // Max header size before fallback (default: 4096)
69
- pointerUrlGenerator?: (receipt: string) => Promise<string>;
70
- claimsGenerator?: (context: RequestContext) => Promise<Partial<ReceiptClaimsInput>>;
71
- }
72
- ```
78
+ - Use `@peac/middleware-express` for Express.js applications
79
+ - Use `@peac/middleware-core` directly when integrating with other frameworks (Hono, Koa, Fastify)
80
+ - Use `@peac/protocol` for lower-level receipt issuance without HTTP middleware
81
+ - See the [llms.txt](https://github.com/peacprotocol/peac/blob/main/llms.txt) for a concise overview
73
82
 
74
- ## Error Handling
83
+ ## For Operators
75
84
 
76
- Configuration errors throw `ConfigError` at startup. Receipt generation errors are returned in the result rather than thrown, so receipt failures never break the HTTP response.
85
+ Call `validateConfig()` or `validateConfigAsync()` at application startup to catch configuration errors early. Receipt generation failures are returned in the result rather than thrown, so receipt failures never break HTTP responses. The `MemoryRateLimitStore` is in-process only; for distributed rate limiting, implement the `RateLimitStore` interface with a shared backend.
77
86
 
78
87
  ## License
79
88
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peac/middleware-core",
3
- "version": "0.12.5",
3
+ "version": "0.12.7",
4
4
  "description": "Framework-agnostic middleware primitives for PEAC receipt issuance",
5
5
  "main": "dist/index.cjs",
6
6
  "types": "dist/index.d.ts",
@@ -15,10 +15,10 @@
15
15
  },
16
16
  "repository": {
17
17
  "type": "git",
18
- "url": "https://github.com/peacprotocol/peac.git",
18
+ "url": "git+https://github.com/peacprotocol/peac.git",
19
19
  "directory": "packages/middleware-core"
20
20
  },
21
- "author": "jithinraj <7850727+jithinraj@users.noreply.github.com>",
21
+ "author": "PEAC Protocol Contributors",
22
22
  "license": "Apache-2.0",
23
23
  "bugs": {
24
24
  "url": "https://github.com/peacprotocol/peac/issues"
@@ -34,9 +34,9 @@
34
34
  },
35
35
  "dependencies": {
36
36
  "uuidv7": "^0.6.3",
37
- "@peac/kernel": "0.12.5",
38
- "@peac/schema": "0.12.5",
39
- "@peac/crypto": "0.12.5"
37
+ "@peac/schema": "0.12.7",
38
+ "@peac/crypto": "0.12.7",
39
+ "@peac/kernel": "0.12.7"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@types/node": "^22.19.11",
@@ -44,6 +44,18 @@
44
44
  "vitest": "^4.0.0",
45
45
  "tsup": "^8.0.0"
46
46
  },
47
+ "keywords": [
48
+ "peac",
49
+ "peacprotocol",
50
+ "interaction-records",
51
+ "signed-records",
52
+ "receipts",
53
+ "originary",
54
+ "middleware",
55
+ "receipt-issuance",
56
+ "http",
57
+ "server-side"
58
+ ],
47
59
  "scripts": {
48
60
  "prebuild": "rm -rf dist tsconfig.tsbuildinfo",
49
61
  "build": "pnpm run build:js && pnpm run build:types",