@jini-ai/capability-providers 0.6.15-rc.2 → 0.6.16
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.
|
@@ -20,8 +20,15 @@ import { createHmac, randomBytes, scryptSync, timingSafeEqual } from 'node:crypt
|
|
|
20
20
|
const SCRYPT_KEYLEN = 64;
|
|
21
21
|
const SCRYPT_SALT_BYTES = 16;
|
|
22
22
|
const DEFAULT_SESSION_TTL_MS = 60 * 60 * 1000;
|
|
23
|
+
function isSessionJwtPayload(parsed) {
|
|
24
|
+
if (!parsed || typeof parsed !== 'object')
|
|
25
|
+
return false;
|
|
26
|
+
const claims = parsed;
|
|
27
|
+
return typeof claims.sub === 'string' && typeof claims.jti === 'string' && typeof claims.exp === 'number';
|
|
28
|
+
}
|
|
23
29
|
function base64url(input) {
|
|
24
|
-
|
|
30
|
+
// Node's own base64url alphabet and unpadded form: the same bytes as translating base64 by hand.
|
|
31
|
+
return input.toString('base64url');
|
|
25
32
|
}
|
|
26
33
|
function base64urlToBuffer(input) {
|
|
27
34
|
const padded = input.replace(/-/g, '+').replace(/_/g, '/');
|
|
@@ -82,13 +89,8 @@ function verifySessionJwt(token, secret) {
|
|
|
82
89
|
catch {
|
|
83
90
|
return { ok: false };
|
|
84
91
|
}
|
|
85
|
-
if (!parsed
|
|
86
|
-
typeof parsed !== 'object' ||
|
|
87
|
-
typeof parsed.sub !== 'string' ||
|
|
88
|
-
typeof parsed.jti !== 'string' ||
|
|
89
|
-
typeof parsed.exp !== 'number') {
|
|
92
|
+
if (!isSessionJwtPayload(parsed))
|
|
90
93
|
return { ok: false };
|
|
91
|
-
}
|
|
92
94
|
return { ok: true, payload: parsed };
|
|
93
95
|
}
|
|
94
96
|
/**
|
|
@@ -134,8 +136,10 @@ export class JwtAuthProvider {
|
|
|
134
136
|
if (this.usersByEmail.has(credentials.email)) {
|
|
135
137
|
throw new Error(`email already registered: ${credentials.email}`);
|
|
136
138
|
}
|
|
139
|
+
const id = this.nextId;
|
|
140
|
+
this.nextId += 1;
|
|
137
141
|
const user = {
|
|
138
|
-
id: `user-${
|
|
142
|
+
id: `user-${id}`,
|
|
139
143
|
email: credentials.email,
|
|
140
144
|
createdAt: this.now(),
|
|
141
145
|
passwordHash: hashPassword(credentials.password),
|
|
@@ -1,16 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
* `@jini-ai/capability-providers/adapters/ws` — the one real, production-quality
|
|
3
|
-
* `RealtimeProvider` adapter this package ships, using WebSockets (the `ws`
|
|
4
|
-
* package): a self-contained pub/sub server, no external hosted-realtime-service
|
|
5
|
-
* dependency (no Pusher/Ably/Supabase Realtime).
|
|
6
|
-
*
|
|
7
|
-
* Lives at this separate entry point (not the package's main
|
|
8
|
-
* `@jini-ai/capability-providers` barrel) so importing the port
|
|
9
|
-
* interface/types/DI tokens never forces a consumer — or a bundler that
|
|
10
|
-
* can't resolve Node-only packages — to also resolve `ws`. Import from here
|
|
11
|
-
* only when you actually want this concrete adapter.
|
|
12
|
-
*/
|
|
13
|
-
import { WebSocketServer, type ServerOptions } from 'ws';
|
|
1
|
+
import type { WebSocketServer, ServerOptions } from 'ws';
|
|
14
2
|
import type { RealtimeHandler, RealtimeProvider, RealtimeUnsubscribe } from '../../realtime.js';
|
|
15
3
|
/**
|
|
16
4
|
* The minimal structural subset of a `ws` `WebSocket` connection this adapter depends on. A native socket is adapted by the factory; tests construct a plain fake
|
|
@@ -10,7 +10,9 @@
|
|
|
10
10
|
* can't resolve Node-only packages — to also resolve `ws`. Import from here
|
|
11
11
|
* only when you actually want this concrete adapter.
|
|
12
12
|
*/
|
|
13
|
-
import {
|
|
13
|
+
import { createRequire } from 'node:module';
|
|
14
|
+
import { loadOptionalPeer } from '@jini-ai/core/optional-peer';
|
|
15
|
+
const require = createRequire(import.meta.url);
|
|
14
16
|
/** WebSocket `readyState` value meaning "open and ready to communicate" (WHATWG WebSocket spec; `ws`'s `WebSocket.OPEN` is the same constant `1`). Spelled out locally so `WebSocketRealtimeProvider`'s core class has no runtime import of `ws` — only the `RealtimeWebSocketLike`/`RealtimeWebSocketServerLike` structural types below, and the `createWebSocketRealtimeProvider` factory, actually touch the real package. */
|
|
15
17
|
const WEBSOCKET_OPEN_STATE = 1;
|
|
16
18
|
/** Parses one inbound client message. Returns `null` for anything not shaped like `{type: 'subscribe'|'unsubscribe', channel: string}` — malformed/unrecognized messages are silently ignored rather than closing the connection, matching a pub/sub transport's normal tolerance for unknown message shapes (e.g. a future protocol version). */
|
|
@@ -134,7 +136,7 @@ export class WebSocketRealtimeProvider {
|
|
|
134
136
|
}
|
|
135
137
|
/** The optional createServer port injects server construction; native ws is the default adapter. The real, production entry point: constructs an actual `ws.WebSocketServer` and wires a `WebSocketRealtimeProvider` to it. Callers that want to inject a fake server for tests should construct `WebSocketRealtimeProvider` directly instead. */
|
|
136
138
|
export function createWebSocketRealtimeProvider(options, optional = {}) {
|
|
137
|
-
const server = (optional.createServer ??
|
|
139
|
+
const server = (optional.createServer ?? createNativeServer)({ wsOptions: options.wsOptions });
|
|
138
140
|
const provider = new WebSocketRealtimeProvider({
|
|
139
141
|
server: {
|
|
140
142
|
on({ listener }) {
|
|
@@ -144,6 +146,11 @@ export function createWebSocketRealtimeProvider(options, optional = {}) {
|
|
|
144
146
|
});
|
|
145
147
|
return { provider, server };
|
|
146
148
|
}
|
|
149
|
+
/** Preserve synchronous construction and let host-injected servers work without the ws peer. */
|
|
150
|
+
function createNativeServer({ wsOptions }) {
|
|
151
|
+
const { WebSocketServer } = loadOptionalPeer({ feature: 'WebSocket realtime adapter', dependency: 'ws', load: () => require('ws') });
|
|
152
|
+
return new WebSocketServer(wsOptions);
|
|
153
|
+
}
|
|
147
154
|
function adaptWebSocket(socket) {
|
|
148
155
|
return {
|
|
149
156
|
get readyState() { return socket.readyState; },
|
|
@@ -12,8 +12,10 @@ export function createInMemoryAuthProvider(_required, options = {}) {
|
|
|
12
12
|
if (usersByEmail.has(credentials.email)) {
|
|
13
13
|
throw new Error(`email already registered: ${credentials.email}`);
|
|
14
14
|
}
|
|
15
|
+
const id = nextId;
|
|
16
|
+
nextId += 1;
|
|
15
17
|
const user = {
|
|
16
|
-
id: `user-${
|
|
18
|
+
id: `user-${id}`,
|
|
17
19
|
email: credentials.email,
|
|
18
20
|
password: credentials.password,
|
|
19
21
|
createdAt: now(),
|
|
@@ -26,8 +28,10 @@ export function createInMemoryAuthProvider(_required, options = {}) {
|
|
|
26
28
|
if (!user || user.password !== credentials.password) {
|
|
27
29
|
throw new Error('invalid email or password');
|
|
28
30
|
}
|
|
31
|
+
const tokenNumber = nextToken;
|
|
32
|
+
nextToken += 1;
|
|
29
33
|
const session = {
|
|
30
|
-
token: `session-${
|
|
34
|
+
token: `session-${tokenNumber}`,
|
|
31
35
|
userId: user.id,
|
|
32
36
|
expiresAt: now() + sessionTtlMs,
|
|
33
37
|
};
|
|
@@ -8,8 +8,10 @@ export function createInMemoryPaymentsProvider(_required, optional = {}) {
|
|
|
8
8
|
if (input.amountCents <= 0) {
|
|
9
9
|
throw new Error('amountCents must be positive');
|
|
10
10
|
}
|
|
11
|
+
const chargeNumber = nextChargeId;
|
|
12
|
+
nextChargeId += 1;
|
|
11
13
|
const record = {
|
|
12
|
-
id: `charge-${
|
|
14
|
+
id: `charge-${chargeNumber}`,
|
|
13
15
|
status: 'succeeded',
|
|
14
16
|
amountCents: input.amountCents,
|
|
15
17
|
currency: input.currency,
|
|
@@ -22,13 +22,16 @@ export class VisitorAuthConfigurationError extends Error {
|
|
|
22
22
|
this.code = code;
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
|
+
function invalidInput(message) {
|
|
26
|
+
return new VisitorAuthConfigurationError({ code: 'invalid-input', message });
|
|
27
|
+
}
|
|
25
28
|
function parsedSecureUrl(value, label, allowLoopbackHttp) {
|
|
26
29
|
let url;
|
|
27
30
|
try {
|
|
28
31
|
url = new URL(value);
|
|
29
32
|
}
|
|
30
33
|
catch {
|
|
31
|
-
throw
|
|
34
|
+
throw invalidInput(`${label} must be an absolute URL`);
|
|
32
35
|
}
|
|
33
36
|
const secure = isSecureProtocol(url, allowLoopbackHttp);
|
|
34
37
|
if (!secure || url.username || url.password || url.hash) {
|
|
@@ -43,7 +46,7 @@ function isSecureProtocol(url, allowLoopbackHttp) {
|
|
|
43
46
|
}
|
|
44
47
|
function assertNonEmpty(value, label) {
|
|
45
48
|
if (!value.trim()) {
|
|
46
|
-
throw
|
|
49
|
+
throw invalidInput(`${label} must not be empty`);
|
|
47
50
|
}
|
|
48
51
|
}
|
|
49
52
|
function assertProviderAlignment(required) {
|
|
@@ -55,7 +58,7 @@ function assertProviderAlignment(required) {
|
|
|
55
58
|
function validatedTransactionTtl(ttlMs) {
|
|
56
59
|
const resolved = ttlMs ?? DEFAULT_TRANSACTION_TTL_MS;
|
|
57
60
|
if (!Number.isInteger(resolved) || resolved <= 0 || resolved > MAX_TRANSACTION_TTL_MS) {
|
|
58
|
-
throw
|
|
61
|
+
throw invalidInput(`ttlMs must be an integer between 1 and ${MAX_TRANSACTION_TTL_MS}`);
|
|
59
62
|
}
|
|
60
63
|
return resolved;
|
|
61
64
|
}
|
|
@@ -67,21 +70,21 @@ function assertPlanContext(required) {
|
|
|
67
70
|
assertNonEmpty(required.registration.clientSecretRef.id, 'clientSecretRef.id');
|
|
68
71
|
}
|
|
69
72
|
if (!Number.isFinite(required.nowMs)) {
|
|
70
|
-
throw
|
|
73
|
+
throw invalidInput('nowMs must be finite');
|
|
71
74
|
}
|
|
72
75
|
}
|
|
73
76
|
function validatedOidcNonce(protocol, security) {
|
|
74
77
|
if (!OPAQUE_SECURITY_VALUE.test(security.state)) {
|
|
75
|
-
throw
|
|
78
|
+
throw invalidInput('state is malformed or too short');
|
|
76
79
|
}
|
|
77
80
|
if (!PKCE_VALUE.test(security.codeVerifier) || !PKCE_VALUE.test(security.codeChallenge)) {
|
|
78
|
-
throw
|
|
81
|
+
throw invalidInput('PKCE verifier and S256 challenge must be 43-128 URL-safe characters');
|
|
79
82
|
}
|
|
80
83
|
if (protocol !== 'oidc')
|
|
81
84
|
return undefined;
|
|
82
85
|
const nonce = security.nonce;
|
|
83
86
|
if (!nonce || !OPAQUE_SECURITY_VALUE.test(nonce)) {
|
|
84
|
-
throw
|
|
87
|
+
throw invalidInput('OIDC authorization requires a strong nonce');
|
|
85
88
|
}
|
|
86
89
|
return nonce;
|
|
87
90
|
}
|
|
@@ -90,10 +93,10 @@ function assertAuthorizationServerSecurity(required) {
|
|
|
90
93
|
throw new VisitorAuthConfigurationError({ code: 'pkce-s256-required', message: 'visitor-auth requires authorization servers that support PKCE S256' });
|
|
91
94
|
}
|
|
92
95
|
if (required.provider.protocol === 'oidc' && !required.server.issuer) {
|
|
93
|
-
throw
|
|
96
|
+
throw invalidInput('OIDC authorization server metadata must declare an issuer');
|
|
94
97
|
}
|
|
95
98
|
if (required.server.requiresAuthorizationResponseIssuer && !required.server.issuer) {
|
|
96
|
-
throw
|
|
99
|
+
throw invalidInput('authorization response issuer validation requires a configured issuer');
|
|
97
100
|
}
|
|
98
101
|
}
|
|
99
102
|
function configuredAuthorizationUrl(required) {
|
|
@@ -242,7 +245,7 @@ export function evaluateVisitorAuthCallback(required) {
|
|
|
242
245
|
function validatedClockSkew(clockSkewSeconds) {
|
|
243
246
|
const skew = clockSkewSeconds ?? 60;
|
|
244
247
|
if (!Number.isFinite(skew) || skew < 0 || skew > 300) {
|
|
245
|
-
throw
|
|
248
|
+
throw invalidInput('clockSkewSeconds must be between 0 and 300');
|
|
246
249
|
}
|
|
247
250
|
return skew;
|
|
248
251
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jini-ai/capability-providers",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.16",
|
|
4
4
|
"description": "Swappable capability-provider ports (auth, storage, payments, db, realtime) as interfaces and DI tokens only; concrete adapters live behind ./adapters/* subpaths.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -29,6 +29,11 @@
|
|
|
29
29
|
"./adapters/blob-storage": "storage",
|
|
30
30
|
"./adapters/jwt-auth": "auth"
|
|
31
31
|
}
|
|
32
|
+
},
|
|
33
|
+
"optionalPeers": {
|
|
34
|
+
"better-sqlite3": "opt-in: sqlite storage — SQLite storage could not load better-sqlite3. Install better-sqlite3 to enable this feature, or check its installed version and native binaries.",
|
|
35
|
+
"ws": "opt-in: websocket realtime adapter; import @jini-ai/capability-providers/adapters/ws — WebSocket realtime adapter could not load ws. Install ws to enable this feature, or check its installed version and native binaries.",
|
|
36
|
+
"@jini-ai/platform": "opt-in: platform-backed adapters; import @jini-ai/capability-providers/adapters/blob-storage — Platform-backed adapters could not load @jini-ai/platform. Install @jini-ai/platform to enable this feature, or check its installed version and native binaries."
|
|
32
37
|
}
|
|
33
38
|
},
|
|
34
39
|
"type": "module",
|
|
@@ -94,12 +99,21 @@
|
|
|
94
99
|
"registry": "https://registry.npmjs.org"
|
|
95
100
|
},
|
|
96
101
|
"dependencies": {
|
|
97
|
-
"@jini-ai/core": "^0.6.
|
|
102
|
+
"@jini-ai/core": "^0.6.16"
|
|
98
103
|
},
|
|
99
104
|
"peerDependencies": {
|
|
100
105
|
"better-sqlite3": "^13.0.0",
|
|
101
106
|
"ws": "^8.18.0",
|
|
102
|
-
"@jini-ai/platform": "^0.6.
|
|
107
|
+
"@jini-ai/platform": "^0.6.10"
|
|
108
|
+
},
|
|
109
|
+
"devDependencies": {
|
|
110
|
+
"@types/better-sqlite3": "^7.6.11",
|
|
111
|
+
"@types/ws": "^8.18.0",
|
|
112
|
+
"@vitest/coverage-v8": "^2.1.9",
|
|
113
|
+
"better-sqlite3": "^13.0.0",
|
|
114
|
+
"ws": "^8.18.0",
|
|
115
|
+
"@jini-ai/core": "0.6.16",
|
|
116
|
+
"@jini-ai/platform": "0.6.16"
|
|
103
117
|
},
|
|
104
118
|
"peerDependenciesMeta": {
|
|
105
119
|
"better-sqlite3": {
|
|
@@ -112,15 +126,6 @@
|
|
|
112
126
|
"optional": true
|
|
113
127
|
}
|
|
114
128
|
},
|
|
115
|
-
"devDependencies": {
|
|
116
|
-
"@types/better-sqlite3": "^7.6.11",
|
|
117
|
-
"@types/ws": "^8.18.0",
|
|
118
|
-
"@vitest/coverage-v8": "^2.1.9",
|
|
119
|
-
"better-sqlite3": "^13.0.0",
|
|
120
|
-
"ws": "^8.18.0",
|
|
121
|
-
"@jini-ai/core": "0.6.15-rc.2",
|
|
122
|
-
"@jini-ai/platform": "0.6.15-rc.2"
|
|
123
|
-
},
|
|
124
129
|
"scripts": {
|
|
125
130
|
"build": "tsc -p tsconfig.json",
|
|
126
131
|
"typecheck": "tsc -p tsconfig.json --noEmit",
|