@jini-ai/capability-providers 0.1.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +95 -0
- package/README.md +174 -0
- package/dist/adapters/blob-storage/index.d.ts +67 -0
- package/dist/adapters/blob-storage/index.js +62 -0
- package/dist/adapters/jwt-auth/index.d.ts +53 -0
- package/dist/adapters/jwt-auth/index.js +182 -0
- package/dist/adapters/sqlite/index.d.ts +56 -0
- package/dist/adapters/sqlite/index.js +83 -0
- package/dist/adapters/stripe/index.d.ts +94 -0
- package/dist/adapters/stripe/index.js +167 -0
- package/dist/adapters/ws/index.d.ts +95 -0
- package/dist/adapters/ws/index.js +161 -0
- package/dist/auth.d.ts +22 -47
- package/dist/auth.js +10 -173
- package/dist/db.d.ts +11 -48
- package/dist/db.js +15 -79
- package/dist/index.d.ts +54 -1
- package/dist/index.js +54 -1
- package/dist/payments.d.ts +21 -74
- package/dist/payments.js +13 -175
- package/dist/realtime.d.ts +24 -73
- package/dist/realtime.js +14 -140
- package/dist/storage.d.ts +23 -49
- package/dist/storage.js +13 -57
- package/dist/tokens.js +6 -6
- package/dist/unsafe-reference/auth.d.ts +1 -1
- package/dist/unsafe-reference/auth.js +3 -3
- package/dist/unsafe-reference/db.d.ts +2 -2
- package/dist/unsafe-reference/index.d.ts +1 -1
- package/dist/unsafe-reference/index.js +1 -1
- package/dist/unsafe-reference/payments.d.ts +3 -1
- package/dist/unsafe-reference/payments.js +6 -5
- package/dist/unsafe-reference/realtime.d.ts +1 -1
- package/dist/unsafe-reference/realtime.js +4 -4
- package/dist/unsafe-reference/storage.d.ts +3 -1
- package/dist/unsafe-reference/storage.js +7 -6
- package/dist/visitor-auth/definitions.d.ts +32 -0
- package/dist/visitor-auth/definitions.js +57 -0
- package/dist/visitor-auth/index.d.ts +17 -0
- package/dist/visitor-auth/index.js +17 -0
- package/dist/visitor-auth/lifecycle.d.ts +101 -0
- package/dist/visitor-auth/lifecycle.js +326 -0
- package/dist/visitor-auth/ports.d.ts +134 -0
- package/dist/visitor-auth/ports.js +2 -0
- package/dist/visitor-auth/registry.d.ts +38 -0
- package/dist/visitor-auth/registry.js +78 -0
- package/dist/visitor-auth/tokens.d.ts +10 -0
- package/dist/visitor-auth/tokens.js +9 -0
- package/package.json +68 -9
- package/dist/auth.d.ts.map +0 -1
- package/dist/auth.js.map +0 -1
- package/dist/db.d.ts.map +0 -1
- package/dist/db.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/payments.d.ts.map +0 -1
- package/dist/payments.js.map +0 -1
- package/dist/realtime.d.ts.map +0 -1
- package/dist/realtime.js.map +0 -1
- package/dist/storage.d.ts.map +0 -1
- package/dist/storage.js.map +0 -1
- package/dist/tokens.d.ts.map +0 -1
- package/dist/tokens.js.map +0 -1
- package/dist/unsafe-reference/auth.d.ts.map +0 -1
- package/dist/unsafe-reference/auth.js.map +0 -1
- package/dist/unsafe-reference/db.d.ts.map +0 -1
- package/dist/unsafe-reference/db.js.map +0 -1
- package/dist/unsafe-reference/index.d.ts.map +0 -1
- package/dist/unsafe-reference/index.js.map +0 -1
- package/dist/unsafe-reference/payments.d.ts.map +0 -1
- package/dist/unsafe-reference/payments.js.map +0 -1
- package/dist/unsafe-reference/realtime.d.ts.map +0 -1
- package/dist/unsafe-reference/realtime.js.map +0 -1
- package/dist/unsafe-reference/storage.d.ts.map +0 -1
- package/dist/unsafe-reference/storage.js.map +0 -1
package/dist/payments.d.ts
CHANGED
|
@@ -1,28 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `PaymentsProvider` — a swappable payment-charge port. Speculative
|
|
3
|
-
* port-design exploration (see `
|
|
4
|
-
* explicitly in `
|
|
5
|
-
* of the capabilities Zana
|
|
6
|
-
* (alongside auth/storage/db/realtime).
|
|
3
|
+
* port-design exploration (see `archived provenance ledger`) — no OD source; named
|
|
4
|
+
* explicitly in `ADS-memory/reports/jini-port/recon/r5b-consumers-matrix.md` §3.3 as one
|
|
5
|
+
* of the capabilities Zana's and a fleet orchestrator's independent provider
|
|
6
|
+
* layers converge on (alongside auth/storage/db/realtime).
|
|
7
7
|
*
|
|
8
|
-
* This file defines the port's stable interface/type surface
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* Charges/Refunds REST API
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* separate, non-production stub that lives under `src/unsafe-reference/`,
|
|
17
|
-
* exported only from the separate `@jini-ai/capability-providers/unsafe-reference`
|
|
18
|
-
* entry point — see that directory's `index.ts` header for the full warning.
|
|
8
|
+
* This file defines the port's stable interface/type surface and nothing else —
|
|
9
|
+
* it has no imports at all, so a consumer implementing `PaymentsProvider`
|
|
10
|
+
* themselves installs nothing. The one real, production-quality adapter
|
|
11
|
+
* (`StripePaymentsProvider`, against Stripe's Charges/Refunds REST API) lives
|
|
12
|
+
* at the separate `@jini-ai/capability-providers/adapters/stripe` entry point;
|
|
13
|
+
* the non-production in-memory reference stub
|
|
14
|
+
* (`createInMemoryPaymentsProvider`) lives under `src/unsafe-reference/`,
|
|
15
|
+
* exported only from `@jini-ai/capability-providers/unsafe-reference`.
|
|
19
16
|
*/
|
|
20
17
|
export type ChargeStatus = 'pending' | 'succeeded' | 'failed' | 'refunded';
|
|
21
18
|
export interface ChargeInput {
|
|
22
19
|
readonly amountCents: number;
|
|
23
20
|
readonly currency: string;
|
|
24
21
|
readonly customerRef: string;
|
|
25
|
-
readonly description?: string;
|
|
26
22
|
}
|
|
27
23
|
export interface Charge {
|
|
28
24
|
readonly id: string;
|
|
@@ -34,65 +30,16 @@ export interface Charge {
|
|
|
34
30
|
}
|
|
35
31
|
export interface PaymentsProvider {
|
|
36
32
|
/** Creates and (in the reference stub) immediately settles a charge. Rejects on a non-positive amount. */
|
|
37
|
-
charge(input: ChargeInput
|
|
33
|
+
charge(input: ChargeInput, optional?: {
|
|
34
|
+
description?: string;
|
|
35
|
+
}): Promise<Charge>;
|
|
38
36
|
/** Looks up a previously created charge by id, or `null` if unknown. */
|
|
39
|
-
getCharge(
|
|
37
|
+
getCharge(required: {
|
|
38
|
+
id: string;
|
|
39
|
+
}): Promise<Charge | null>;
|
|
40
40
|
/** Refunds a `'succeeded'` charge. Rejects if the charge is unknown or not in a refundable state. */
|
|
41
|
-
refund(
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
export declare class StripePaymentsProviderError extends Error {
|
|
45
|
-
/** The HTTP status Stripe (or a local pre-flight check) returned. */
|
|
46
|
-
readonly status: number;
|
|
47
|
-
/** Stripe's `error.type` — one of `api_error`, `card_error`, `idempotency_error`, `invalid_request_error`. Undefined for a local pre-flight rejection (e.g. a non-positive amount) that never reached Stripe. */
|
|
48
|
-
readonly stripeType?: string;
|
|
49
|
-
/** Stripe's `error.code`, when present. */
|
|
50
|
-
readonly stripeCode?: string;
|
|
51
|
-
constructor(message: string, status: number, stripeType?: string, stripeCode?: string);
|
|
52
|
-
}
|
|
53
|
-
export interface StripePaymentsProviderOptions {
|
|
54
|
-
/** Stripe secret key (`sk_live_...`/`sk_test_...`). Required and explicit — never read from an environment variable inside this adapter. */
|
|
55
|
-
readonly secretKey: string;
|
|
56
|
-
/** Pluggable fetch for tests. Defaults to `globalThis.fetch`, matching `S3BlobStorage`/`netlify.ts`'s precedent in this repo. */
|
|
57
|
-
readonly fetchFn?: typeof fetch;
|
|
58
|
-
/** Overrides Stripe's API base URL. Tests only — production leaves this unset. */
|
|
59
|
-
readonly apiBase?: string;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* `PaymentsProvider` adapter against Stripe's real Charges/Refunds REST API. No mock/simulated
|
|
63
|
-
* money movement — every `charge`/`getCharge`/`refund` call is a real HTTP request to
|
|
64
|
-
* `api.stripe.com` (or `options.apiBase` in tests), authenticated the way Stripe's own docs
|
|
65
|
-
* specify: HTTP Basic auth with the secret key as the username and an empty password
|
|
66
|
-
* (`Authorization: Basic base64("sk_...:")`), request bodies form-encoded
|
|
67
|
-
* (`application/x-www-form-urlencoded`), not JSON.
|
|
68
|
-
*
|
|
69
|
-
* `ChargeInput.customerRef` is sent as Stripe's `customer` parameter — this adapter always
|
|
70
|
-
* charges an existing Stripe Customer (who must already have a default/attached payment method),
|
|
71
|
-
* never a one-off `source`/token; `ChargeInput` has no field for a raw card token, and inventing
|
|
72
|
-
* one wasn't part of this port's brief.
|
|
73
|
-
*/
|
|
74
|
-
export declare class StripePaymentsProvider implements PaymentsProvider {
|
|
75
|
-
private readonly options;
|
|
76
|
-
private readonly fetchFn;
|
|
77
|
-
private readonly apiBase;
|
|
78
|
-
constructor(options: StripePaymentsProviderOptions);
|
|
79
|
-
charge(input: ChargeInput): Promise<Charge>;
|
|
80
|
-
getCharge(id: string): Promise<Charge | null>;
|
|
81
|
-
/**
|
|
82
|
-
* `PaymentsProvider.refund` returns a completed `Charge`, but a real Stripe `Refund` object's
|
|
83
|
-
* own `status` can be `'succeeded'`, `'pending'` (common for bank-sourced refunds that settle
|
|
84
|
-
* over days), `'failed'`, or `'canceled'` — a richer state machine than this port's
|
|
85
|
-
* `ChargeStatus` has room for. Since `ChargeStatus` has no "refund in flight" state, `'pending'`
|
|
86
|
-
* is treated the same as `'succeeded'` here (Stripe accepted the refund; it will settle) and
|
|
87
|
-
* only `'failed'`/`'canceled'` reject. A host that needs to distinguish "refund accepted" from
|
|
88
|
-
* "refund settled" should track Stripe's own refund object separately — out of scope for this
|
|
89
|
-
* port's `Charge`-shaped return value.
|
|
90
|
-
*/
|
|
91
|
-
refund(id: string): Promise<Charge>;
|
|
92
|
-
private authHeader;
|
|
93
|
-
private request;
|
|
94
|
-
private safeJson;
|
|
95
|
-
private toError;
|
|
96
|
-
private toCharge;
|
|
41
|
+
refund(required: {
|
|
42
|
+
id: string;
|
|
43
|
+
}): Promise<Charge>;
|
|
97
44
|
}
|
|
98
45
|
//# sourceMappingURL=payments.d.ts.map
|
package/dist/payments.js
CHANGED
|
@@ -1,180 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `PaymentsProvider` — a swappable payment-charge port. Speculative
|
|
3
|
-
* port-design exploration (see `
|
|
4
|
-
* explicitly in `
|
|
5
|
-
* of the capabilities Zana
|
|
6
|
-
* (alongside auth/storage/db/realtime).
|
|
3
|
+
* port-design exploration (see `archived provenance ledger`) — no OD source; named
|
|
4
|
+
* explicitly in `ADS-memory/reports/jini-port/recon/r5b-consumers-matrix.md` §3.3 as one
|
|
5
|
+
* of the capabilities Zana's and a fleet orchestrator's independent provider
|
|
6
|
+
* layers converge on (alongside auth/storage/db/realtime).
|
|
7
7
|
*
|
|
8
|
-
* This file defines the port's stable interface/type surface
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* Charges/Refunds REST API
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* separate, non-production stub that lives under `src/unsafe-reference/`,
|
|
17
|
-
* exported only from the separate `@jini-ai/capability-providers/unsafe-reference`
|
|
18
|
-
* entry point — see that directory's `index.ts` header for the full warning.
|
|
8
|
+
* This file defines the port's stable interface/type surface and nothing else —
|
|
9
|
+
* it has no imports at all, so a consumer implementing `PaymentsProvider`
|
|
10
|
+
* themselves installs nothing. The one real, production-quality adapter
|
|
11
|
+
* (`StripePaymentsProvider`, against Stripe's Charges/Refunds REST API) lives
|
|
12
|
+
* at the separate `@jini-ai/capability-providers/adapters/stripe` entry point;
|
|
13
|
+
* the non-production in-memory reference stub
|
|
14
|
+
* (`createInMemoryPaymentsProvider`) lives under `src/unsafe-reference/`,
|
|
15
|
+
* exported only from `@jini-ai/capability-providers/unsafe-reference`.
|
|
19
16
|
*/
|
|
20
|
-
|
|
21
|
-
/** Error thrown by {@link StripePaymentsProvider}, carrying Stripe's own error envelope fields (`https://docs.stripe.com/api/errors`) when the failure came from a Stripe API response. */
|
|
22
|
-
export class StripePaymentsProviderError extends Error {
|
|
23
|
-
/** The HTTP status Stripe (or a local pre-flight check) returned. */
|
|
24
|
-
status;
|
|
25
|
-
/** Stripe's `error.type` — one of `api_error`, `card_error`, `idempotency_error`, `invalid_request_error`. Undefined for a local pre-flight rejection (e.g. a non-positive amount) that never reached Stripe. */
|
|
26
|
-
stripeType;
|
|
27
|
-
/** Stripe's `error.code`, when present. */
|
|
28
|
-
stripeCode;
|
|
29
|
-
constructor(message, status, stripeType, stripeCode) {
|
|
30
|
-
super(message);
|
|
31
|
-
this.name = 'StripePaymentsProviderError';
|
|
32
|
-
this.status = status;
|
|
33
|
-
if (stripeType !== undefined)
|
|
34
|
-
this.stripeType = stripeType;
|
|
35
|
-
if (stripeCode !== undefined)
|
|
36
|
-
this.stripeCode = stripeCode;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Maps a raw Stripe Charge JSON object's `status` (`'succeeded' | 'pending' | 'failed'` per
|
|
41
|
-
* Stripe's documented Charge object) plus its separate `refunded` boolean onto this port's
|
|
42
|
-
* `ChargeStatus`. `refunded: true` always wins — Stripe leaves `status` at `'succeeded'` even
|
|
43
|
-
* after a full refund, but this port's `ChargeStatus` models "refunded" as its own terminal
|
|
44
|
-
* state distinct from "succeeded", so a charge this adapter itself refunded (or that was
|
|
45
|
-
* refunded directly in Stripe) reports as `'refunded'` here.
|
|
46
|
-
*/
|
|
47
|
-
function mapStripeChargeStatus(raw, refunded) {
|
|
48
|
-
if (refunded === true)
|
|
49
|
-
return 'refunded';
|
|
50
|
-
if (raw === 'succeeded' || raw === 'pending' || raw === 'failed')
|
|
51
|
-
return raw;
|
|
52
|
-
// Stripe's Charge.status is documented as exactly succeeded | pending | failed — this branch
|
|
53
|
-
// has no real reachable path against Stripe's actual contract, but keeps the mapping total
|
|
54
|
-
// against `unknown` JSON (an arbitrary HTTP response body) rather than asserting.
|
|
55
|
-
return 'pending';
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* `PaymentsProvider` adapter against Stripe's real Charges/Refunds REST API. No mock/simulated
|
|
59
|
-
* money movement — every `charge`/`getCharge`/`refund` call is a real HTTP request to
|
|
60
|
-
* `api.stripe.com` (or `options.apiBase` in tests), authenticated the way Stripe's own docs
|
|
61
|
-
* specify: HTTP Basic auth with the secret key as the username and an empty password
|
|
62
|
-
* (`Authorization: Basic base64("sk_...:")`), request bodies form-encoded
|
|
63
|
-
* (`application/x-www-form-urlencoded`), not JSON.
|
|
64
|
-
*
|
|
65
|
-
* `ChargeInput.customerRef` is sent as Stripe's `customer` parameter — this adapter always
|
|
66
|
-
* charges an existing Stripe Customer (who must already have a default/attached payment method),
|
|
67
|
-
* never a one-off `source`/token; `ChargeInput` has no field for a raw card token, and inventing
|
|
68
|
-
* one wasn't part of this port's brief.
|
|
69
|
-
*/
|
|
70
|
-
export class StripePaymentsProvider {
|
|
71
|
-
options;
|
|
72
|
-
fetchFn;
|
|
73
|
-
apiBase;
|
|
74
|
-
constructor(options) {
|
|
75
|
-
this.options = options;
|
|
76
|
-
if (!options.secretKey) {
|
|
77
|
-
throw new StripePaymentsProviderError('StripePaymentsProvider requires a non-empty options.secretKey', 401);
|
|
78
|
-
}
|
|
79
|
-
const fn = options.fetchFn ?? globalThis.fetch;
|
|
80
|
-
if (!fn) {
|
|
81
|
-
throw new StripePaymentsProviderError('StripePaymentsProvider requires a fetch implementation', 500);
|
|
82
|
-
}
|
|
83
|
-
this.fetchFn = fn;
|
|
84
|
-
this.apiBase = (options.apiBase ?? STRIPE_API_BASE).replace(/\/+$/, '');
|
|
85
|
-
}
|
|
86
|
-
async charge(input) {
|
|
87
|
-
if (input.amountCents <= 0) {
|
|
88
|
-
throw new StripePaymentsProviderError('amountCents must be positive', 400);
|
|
89
|
-
}
|
|
90
|
-
const body = new URLSearchParams({
|
|
91
|
-
amount: String(input.amountCents),
|
|
92
|
-
currency: input.currency,
|
|
93
|
-
customer: input.customerRef,
|
|
94
|
-
});
|
|
95
|
-
if (input.description !== undefined)
|
|
96
|
-
body.set('description', input.description);
|
|
97
|
-
const json = await this.request('POST', '/charges', body);
|
|
98
|
-
return this.toCharge(json);
|
|
99
|
-
}
|
|
100
|
-
async getCharge(id) {
|
|
101
|
-
try {
|
|
102
|
-
const json = await this.request('GET', `/charges/${encodeURIComponent(id)}`);
|
|
103
|
-
return this.toCharge(json);
|
|
104
|
-
}
|
|
105
|
-
catch (err) {
|
|
106
|
-
if (err instanceof StripePaymentsProviderError && err.status === 404)
|
|
107
|
-
return null;
|
|
108
|
-
throw err;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* `PaymentsProvider.refund` returns a completed `Charge`, but a real Stripe `Refund` object's
|
|
113
|
-
* own `status` can be `'succeeded'`, `'pending'` (common for bank-sourced refunds that settle
|
|
114
|
-
* over days), `'failed'`, or `'canceled'` — a richer state machine than this port's
|
|
115
|
-
* `ChargeStatus` has room for. Since `ChargeStatus` has no "refund in flight" state, `'pending'`
|
|
116
|
-
* is treated the same as `'succeeded'` here (Stripe accepted the refund; it will settle) and
|
|
117
|
-
* only `'failed'`/`'canceled'` reject. A host that needs to distinguish "refund accepted" from
|
|
118
|
-
* "refund settled" should track Stripe's own refund object separately — out of scope for this
|
|
119
|
-
* port's `Charge`-shaped return value.
|
|
120
|
-
*/
|
|
121
|
-
async refund(id) {
|
|
122
|
-
const existing = await this.getCharge(id);
|
|
123
|
-
if (!existing) {
|
|
124
|
-
throw new StripePaymentsProviderError(`unknown charge: ${id}`, 404);
|
|
125
|
-
}
|
|
126
|
-
if (existing.status !== 'succeeded') {
|
|
127
|
-
throw new StripePaymentsProviderError(`charge ${id} is not refundable from status "${existing.status}"`, 400);
|
|
128
|
-
}
|
|
129
|
-
const json = await this.request('POST', '/refunds', new URLSearchParams({ charge: id }));
|
|
130
|
-
const refundStatus = json.status;
|
|
131
|
-
if (refundStatus === 'failed' || refundStatus === 'canceled') {
|
|
132
|
-
throw new StripePaymentsProviderError(`Stripe refund ${String(refundStatus)} for charge ${id}`, 402, 'api_error');
|
|
133
|
-
}
|
|
134
|
-
return { ...existing, status: 'refunded' };
|
|
135
|
-
}
|
|
136
|
-
authHeader() {
|
|
137
|
-
return `Basic ${Buffer.from(`${this.options.secretKey}:`).toString('base64')}`;
|
|
138
|
-
}
|
|
139
|
-
async request(method, path, formBody) {
|
|
140
|
-
const headers = { Authorization: this.authHeader() };
|
|
141
|
-
const init = { method, headers };
|
|
142
|
-
if (formBody) {
|
|
143
|
-
headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
|
144
|
-
init.body = formBody.toString();
|
|
145
|
-
}
|
|
146
|
-
const res = await this.fetchFn(`${this.apiBase}${path}`, init);
|
|
147
|
-
const json = await this.safeJson(res);
|
|
148
|
-
if (!res.ok)
|
|
149
|
-
throw this.toError(res.status, json);
|
|
150
|
-
return json;
|
|
151
|
-
}
|
|
152
|
-
async safeJson(res) {
|
|
153
|
-
try {
|
|
154
|
-
const parsed = await res.json();
|
|
155
|
-
return parsed && typeof parsed === 'object' ? parsed : {};
|
|
156
|
-
}
|
|
157
|
-
catch {
|
|
158
|
-
return {};
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
toError(status, json) {
|
|
162
|
-
const errorObj = json.error && typeof json.error === 'object' ? json.error : {};
|
|
163
|
-
const message = typeof errorObj.message === 'string' && errorObj.message ? errorObj.message : `Stripe request failed (${status}).`;
|
|
164
|
-
const stripeType = typeof errorObj.type === 'string' ? errorObj.type : undefined;
|
|
165
|
-
const stripeCode = typeof errorObj.code === 'string' ? errorObj.code : undefined;
|
|
166
|
-
return new StripePaymentsProviderError(message, status, stripeType, stripeCode);
|
|
167
|
-
}
|
|
168
|
-
toCharge(json) {
|
|
169
|
-
return {
|
|
170
|
-
id: typeof json.id === 'string' ? json.id : '',
|
|
171
|
-
status: mapStripeChargeStatus(json.status, json.refunded),
|
|
172
|
-
amountCents: typeof json.amount === 'number' ? json.amount : 0,
|
|
173
|
-
currency: typeof json.currency === 'string' ? json.currency : '',
|
|
174
|
-
customerRef: typeof json.customer === 'string' ? json.customer : '',
|
|
175
|
-
// Stripe's `created` is Unix seconds; this port's `createdAt` is epoch milliseconds.
|
|
176
|
-
createdAt: typeof json.created === 'number' ? json.created * 1000 : Date.now(),
|
|
177
|
-
};
|
|
178
|
-
}
|
|
179
|
-
}
|
|
17
|
+
export {};
|
|
180
18
|
//# sourceMappingURL=payments.js.map
|
package/dist/realtime.d.ts
CHANGED
|
@@ -1,86 +1,37 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `RealtimeProvider` — a swappable pub/sub port (live updates pushed to
|
|
3
|
-
* subscribers). Speculative port-design exploration (see `
|
|
4
|
-
* no OD source; named in `
|
|
5
|
-
* as part of the
|
|
6
|
-
* Zana's reference adapter).
|
|
3
|
+
* subscribers). Speculative port-design exploration (see `archived provenance ledger`) —
|
|
4
|
+
* no OD source; named in `ADS-memory/reports/jini-port/recon/r5b-consumers-matrix.md` §3.3
|
|
5
|
+
* as part of the capability set Zana and a fleet orchestrator converge on
|
|
6
|
+
* (Supabase Realtime is Zana's reference adapter).
|
|
7
7
|
*
|
|
8
|
-
* This file defines the port's stable interface/type surface
|
|
9
|
-
* production-quality adapter (`WebSocketRealtimeProvider` +
|
|
10
|
-
* `createWebSocketRealtimeProvider`,
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* (`createInMemoryRealtimeProvider`) is a
|
|
15
|
-
* lives under `src/unsafe-reference/`,
|
|
8
|
+
* This file defines only the port's stable interface/type surface. The one
|
|
9
|
+
* real, production-quality adapter (`WebSocketRealtimeProvider` +
|
|
10
|
+
* `createWebSocketRealtimeProvider`, using the `ws` package) lives at the
|
|
11
|
+
* separate `@jini-ai/capability-providers/adapters/ws` entry point — moved
|
|
12
|
+
* there (2026-07-29) so this package's main barrel, which imports this
|
|
13
|
+
* module, is never forced to resolve `ws` just for the port type. The
|
|
14
|
+
* in-memory reference implementation (`createInMemoryRealtimeProvider`) is a
|
|
15
|
+
* separate, non-production stub that lives under `src/unsafe-reference/`,
|
|
16
|
+
* exported only from the separate
|
|
16
17
|
* `@jini-ai/capability-providers/unsafe-reference` entry point — see that
|
|
17
18
|
* directory's `index.ts` header for the full warning.
|
|
18
19
|
*/
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
export type RealtimeHandler<T = unknown> = (required: {
|
|
21
|
+
event: T;
|
|
22
|
+
}) => void;
|
|
21
23
|
/** Call to stop receiving events for the subscription that returned it. Idempotent. */
|
|
22
24
|
export type RealtimeUnsubscribe = () => void;
|
|
23
25
|
export interface RealtimeProvider {
|
|
24
26
|
/** Delivers `event` synchronously to every current subscriber of `channel`. Resolves once all handlers have run. */
|
|
25
|
-
publish<T>(
|
|
27
|
+
publish<T>(required: {
|
|
28
|
+
channel: string;
|
|
29
|
+
event: T;
|
|
30
|
+
}): Promise<void>;
|
|
26
31
|
/** Registers `handler` for every future `publish` on `channel`. Returns an unsubscribe function. */
|
|
27
|
-
subscribe<T>(
|
|
32
|
+
subscribe<T>(required: {
|
|
33
|
+
channel: string;
|
|
34
|
+
handler: RealtimeHandler<T>;
|
|
35
|
+
}): RealtimeUnsubscribe;
|
|
28
36
|
}
|
|
29
|
-
/**
|
|
30
|
-
* The minimal structural subset of a `ws` `WebSocket` connection this adapter depends on. A real
|
|
31
|
-
* `ws.WebSocket` instance satisfies this without any adaptation; tests construct a plain fake
|
|
32
|
-
* (an `EventEmitter`-backed object) satisfying the same shape, so `WebSocketRealtimeProvider`'s
|
|
33
|
-
* core logic is fully unit-testable with zero real sockets/ports.
|
|
34
|
-
*/
|
|
35
|
-
export interface RealtimeWebSocketLike {
|
|
36
|
-
readonly readyState: number;
|
|
37
|
-
send(data: string): void;
|
|
38
|
-
on(event: 'message', listener: (data: unknown) => void): void;
|
|
39
|
-
on(event: 'close', listener: () => void): void;
|
|
40
|
-
on(event: 'error', listener: (err: Error) => void): void;
|
|
41
|
-
}
|
|
42
|
-
/** The minimal structural subset of a `ws` `WebSocketServer` this adapter depends on. */
|
|
43
|
-
export interface RealtimeWebSocketServerLike {
|
|
44
|
-
on(event: 'connection', listener: (socket: RealtimeWebSocketLike) => void): void;
|
|
45
|
-
}
|
|
46
|
-
export interface WebSocketRealtimeProviderOptions {
|
|
47
|
-
/** The (real or fake) WebSocket server to accept subscriber connections from. */
|
|
48
|
-
readonly server: RealtimeWebSocketServerLike;
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* `RealtimeProvider` adapter using WebSockets: a self-contained pub/sub server, no external
|
|
52
|
-
* hosted-realtime-service dependency. Two subscriber shapes fan out from the same `publish()`:
|
|
53
|
-
*
|
|
54
|
-
* 1. **In-process** subscribers registered via `subscribe()` — called synchronously, exactly
|
|
55
|
-
* like the in-memory reference adapter (same handler-Set-per-channel bookkeeping).
|
|
56
|
-
* 2. **Remote WebSocket clients** — connect through the injected server, then send
|
|
57
|
-
* `{"type":"subscribe","channel":"..."}` / `{"type":"unsubscribe","channel":"..."}` JSON
|
|
58
|
-
* messages to opt in/out of channels. `publish()` sends each currently-subscribed, still-open
|
|
59
|
-
* socket a `{"type":"event","channel":"...","event":...}` JSON message. A closed connection is
|
|
60
|
-
* cleaned out of every channel it was subscribed to.
|
|
61
|
-
*
|
|
62
|
-
* The constructor takes a `RealtimeWebSocketServerLike` — structurally satisfied by a real
|
|
63
|
-
* `ws.WebSocketServer` (see `createWebSocketRealtimeProvider` below) or, in tests, a fake with no
|
|
64
|
-
* real socket/port — so the channel-routing/subscription-bookkeeping core here is fully
|
|
65
|
-
* unit-testable without any real network I/O.
|
|
66
|
-
*/
|
|
67
|
-
export declare class WebSocketRealtimeProvider implements RealtimeProvider {
|
|
68
|
-
private readonly handlersByChannel;
|
|
69
|
-
private readonly socketsByChannel;
|
|
70
|
-
constructor(options: WebSocketRealtimeProviderOptions);
|
|
71
|
-
private handleConnection;
|
|
72
|
-
private socketsFor;
|
|
73
|
-
private handlersFor;
|
|
74
|
-
publish<T>(channel: string, event: T): Promise<void>;
|
|
75
|
-
subscribe<T>(channel: string, handler: RealtimeHandler<T>): RealtimeUnsubscribe;
|
|
76
|
-
}
|
|
77
|
-
export interface CreateWebSocketRealtimeProviderOptions {
|
|
78
|
-
/** Passed straight through to `ws`'s `WebSocketServer` constructor — e.g. `{ port: 8080 }` to listen standalone, or `{ server: httpServer }`/`{ noServer: true }` to share an existing HTTP server's upgrade handling. */
|
|
79
|
-
readonly wsOptions: ServerOptions;
|
|
80
|
-
}
|
|
81
|
-
/** 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. */
|
|
82
|
-
export declare function createWebSocketRealtimeProvider(options: CreateWebSocketRealtimeProviderOptions): {
|
|
83
|
-
readonly provider: WebSocketRealtimeProvider;
|
|
84
|
-
readonly server: WebSocketServer;
|
|
85
|
-
};
|
|
86
37
|
//# sourceMappingURL=realtime.d.ts.map
|
package/dist/realtime.js
CHANGED
|
@@ -1,147 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `RealtimeProvider` — a swappable pub/sub port (live updates pushed to
|
|
3
|
-
* subscribers). Speculative port-design exploration (see `
|
|
4
|
-
* no OD source; named in `
|
|
5
|
-
* as part of the
|
|
6
|
-
* Zana's reference adapter).
|
|
3
|
+
* subscribers). Speculative port-design exploration (see `archived provenance ledger`) —
|
|
4
|
+
* no OD source; named in `ADS-memory/reports/jini-port/recon/r5b-consumers-matrix.md` §3.3
|
|
5
|
+
* as part of the capability set Zana and a fleet orchestrator converge on
|
|
6
|
+
* (Supabase Realtime is Zana's reference adapter).
|
|
7
7
|
*
|
|
8
|
-
* This file defines the port's stable interface/type surface
|
|
9
|
-
* production-quality adapter (`WebSocketRealtimeProvider` +
|
|
10
|
-
* `createWebSocketRealtimeProvider`,
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* (`createInMemoryRealtimeProvider`) is a
|
|
15
|
-
* lives under `src/unsafe-reference/`,
|
|
8
|
+
* This file defines only the port's stable interface/type surface. The one
|
|
9
|
+
* real, production-quality adapter (`WebSocketRealtimeProvider` +
|
|
10
|
+
* `createWebSocketRealtimeProvider`, using the `ws` package) lives at the
|
|
11
|
+
* separate `@jini-ai/capability-providers/adapters/ws` entry point — moved
|
|
12
|
+
* there (2026-07-29) so this package's main barrel, which imports this
|
|
13
|
+
* module, is never forced to resolve `ws` just for the port type. The
|
|
14
|
+
* in-memory reference implementation (`createInMemoryRealtimeProvider`) is a
|
|
15
|
+
* separate, non-production stub that lives under `src/unsafe-reference/`,
|
|
16
|
+
* exported only from the separate
|
|
16
17
|
* `@jini-ai/capability-providers/unsafe-reference` entry point — see that
|
|
17
18
|
* directory's `index.ts` header for the full warning.
|
|
18
19
|
*/
|
|
19
|
-
|
|
20
|
-
/** 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. */
|
|
21
|
-
const WEBSOCKET_OPEN_STATE = 1;
|
|
22
|
-
/** 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). */
|
|
23
|
-
function parseClientMessage(raw) {
|
|
24
|
-
let text;
|
|
25
|
-
if (typeof raw === 'string') {
|
|
26
|
-
text = raw;
|
|
27
|
-
}
|
|
28
|
-
else if (Buffer.isBuffer(raw)) {
|
|
29
|
-
text = raw.toString('utf8');
|
|
30
|
-
}
|
|
31
|
-
else if (raw instanceof ArrayBuffer) {
|
|
32
|
-
text = Buffer.from(raw).toString('utf8');
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
return null;
|
|
36
|
-
}
|
|
37
|
-
let parsed;
|
|
38
|
-
try {
|
|
39
|
-
parsed = JSON.parse(text);
|
|
40
|
-
}
|
|
41
|
-
catch {
|
|
42
|
-
return null;
|
|
43
|
-
}
|
|
44
|
-
if (!parsed || typeof parsed !== 'object')
|
|
45
|
-
return null;
|
|
46
|
-
const obj = parsed;
|
|
47
|
-
if ((obj.type !== 'subscribe' && obj.type !== 'unsubscribe') || typeof obj.channel !== 'string')
|
|
48
|
-
return null;
|
|
49
|
-
return { type: obj.type, channel: obj.channel };
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* `RealtimeProvider` adapter using WebSockets: a self-contained pub/sub server, no external
|
|
53
|
-
* hosted-realtime-service dependency. Two subscriber shapes fan out from the same `publish()`:
|
|
54
|
-
*
|
|
55
|
-
* 1. **In-process** subscribers registered via `subscribe()` — called synchronously, exactly
|
|
56
|
-
* like the in-memory reference adapter (same handler-Set-per-channel bookkeeping).
|
|
57
|
-
* 2. **Remote WebSocket clients** — connect through the injected server, then send
|
|
58
|
-
* `{"type":"subscribe","channel":"..."}` / `{"type":"unsubscribe","channel":"..."}` JSON
|
|
59
|
-
* messages to opt in/out of channels. `publish()` sends each currently-subscribed, still-open
|
|
60
|
-
* socket a `{"type":"event","channel":"...","event":...}` JSON message. A closed connection is
|
|
61
|
-
* cleaned out of every channel it was subscribed to.
|
|
62
|
-
*
|
|
63
|
-
* The constructor takes a `RealtimeWebSocketServerLike` — structurally satisfied by a real
|
|
64
|
-
* `ws.WebSocketServer` (see `createWebSocketRealtimeProvider` below) or, in tests, a fake with no
|
|
65
|
-
* real socket/port — so the channel-routing/subscription-bookkeeping core here is fully
|
|
66
|
-
* unit-testable without any real network I/O.
|
|
67
|
-
*/
|
|
68
|
-
export class WebSocketRealtimeProvider {
|
|
69
|
-
handlersByChannel = new Map();
|
|
70
|
-
socketsByChannel = new Map();
|
|
71
|
-
constructor(options) {
|
|
72
|
-
options.server.on('connection', (socket) => this.handleConnection(socket));
|
|
73
|
-
}
|
|
74
|
-
handleConnection(socket) {
|
|
75
|
-
const subscribedChannels = new Set();
|
|
76
|
-
socket.on('message', (raw) => {
|
|
77
|
-
const message = parseClientMessage(raw);
|
|
78
|
-
if (!message)
|
|
79
|
-
return;
|
|
80
|
-
if (message.type === 'subscribe') {
|
|
81
|
-
subscribedChannels.add(message.channel);
|
|
82
|
-
this.socketsFor(message.channel).add(socket);
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
subscribedChannels.delete(message.channel);
|
|
86
|
-
this.socketsByChannel.get(message.channel)?.delete(socket);
|
|
87
|
-
}
|
|
88
|
-
});
|
|
89
|
-
socket.on('close', () => {
|
|
90
|
-
for (const channel of subscribedChannels) {
|
|
91
|
-
this.socketsByChannel.get(channel)?.delete(socket);
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
// Swallow — `ws` always follows an 'error' with a 'close' for the same socket, which already
|
|
95
|
-
// performs the subscription cleanup above; there is nothing additional to do here.
|
|
96
|
-
socket.on('error', () => { });
|
|
97
|
-
}
|
|
98
|
-
socketsFor(channel) {
|
|
99
|
-
let sockets = this.socketsByChannel.get(channel);
|
|
100
|
-
if (!sockets) {
|
|
101
|
-
sockets = new Set();
|
|
102
|
-
this.socketsByChannel.set(channel, sockets);
|
|
103
|
-
}
|
|
104
|
-
return sockets;
|
|
105
|
-
}
|
|
106
|
-
handlersFor(channel) {
|
|
107
|
-
let handlers = this.handlersByChannel.get(channel);
|
|
108
|
-
if (!handlers) {
|
|
109
|
-
handlers = new Set();
|
|
110
|
-
this.handlersByChannel.set(channel, handlers);
|
|
111
|
-
}
|
|
112
|
-
return handlers;
|
|
113
|
-
}
|
|
114
|
-
async publish(channel, event) {
|
|
115
|
-
const handlers = this.handlersByChannel.get(channel);
|
|
116
|
-
if (handlers) {
|
|
117
|
-
for (const handler of [...handlers])
|
|
118
|
-
handler(event);
|
|
119
|
-
}
|
|
120
|
-
const sockets = this.socketsByChannel.get(channel);
|
|
121
|
-
if (sockets && sockets.size > 0) {
|
|
122
|
-
const payload = JSON.stringify({ type: 'event', channel, event });
|
|
123
|
-
for (const socket of [...sockets]) {
|
|
124
|
-
if (socket.readyState === WEBSOCKET_OPEN_STATE)
|
|
125
|
-
socket.send(payload);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
subscribe(channel, handler) {
|
|
130
|
-
const handlers = this.handlersFor(channel);
|
|
131
|
-
handlers.add(handler);
|
|
132
|
-
let unsubscribed = false;
|
|
133
|
-
return () => {
|
|
134
|
-
if (unsubscribed)
|
|
135
|
-
return;
|
|
136
|
-
unsubscribed = true;
|
|
137
|
-
handlers.delete(handler);
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
/** 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. */
|
|
142
|
-
export function createWebSocketRealtimeProvider(options) {
|
|
143
|
-
const server = new WebSocketServer(options.wsOptions);
|
|
144
|
-
const provider = new WebSocketRealtimeProvider({ server });
|
|
145
|
-
return { provider, server };
|
|
146
|
-
}
|
|
20
|
+
export {};
|
|
147
21
|
//# sourceMappingURL=realtime.js.map
|