@sirosfoundation/wmp-js 0.5.2 → 0.7.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/dist/peer.d.ts CHANGED
@@ -8,6 +8,7 @@
8
8
  import { type Transport } from "./transport.js";
9
9
  import { type Profile, type PeerContext } from "./profile.js";
10
10
  import { type ValidationError } from "./schema.js";
11
+ import { CompletionResultError } from "./completion.js";
11
12
  import { type SessionCreateParams, type SessionCreateResult, type SessionResumeParams, type SessionResumeResult, type SessionCloseParams, type SessionAuthenticateParams, type SessionAuthenticateResult, type MessageDeliverParams, type MessageAckParams, type MessagePollParams, type MessagePollResult, type MessageStatusParams, type CapabilityUpdateParams, type CapabilityUpdateResult, type CapabilityListParams, type CapabilityListResult, type FlowStartParams, type FlowStartResult, type FlowProgressParams, type FlowActionParams, type FlowActionResult, type FlowCompleteParams, type FlowErrorParams, type FlowCancelParams, type FlowCancelResult, type ResolveParams, type ResolveResult, type Capabilities, type SecurityMode } from "./types.js";
12
13
  /**
13
14
  * Handler processes incoming WMP method calls.
@@ -43,6 +44,30 @@ export interface PeerOptions {
43
44
  };
44
45
  /** Optional authorization hook called before dispatching incoming requests/notifications. */
45
46
  authorize?: (method: string, params: unknown) => boolean | Promise<boolean>;
47
+ /**
48
+ * Refuse an incoming wmp.flow.complete whose result breaks the OpenID4x
49
+ * completion rules (see validateCompletionResult). The completion is not
50
+ * delivered; the flow is ended with a synthesized wmp.flow.error
51
+ * (-32602, no member of the result echoed) to the profile or handler that
52
+ * owns it. Default: true.
53
+ */
54
+ validateCompletions?: boolean;
55
+ /**
56
+ * Protocol versions this peer speaks. As a client it offers the highest and
57
+ * accepts a selected version that is in this list and not above the offered
58
+ * one (Core 2.2.2); as a responder it negotiates incoming session.create and
59
+ * session.resume against it. Default: ["0.1"].
60
+ */
61
+ supportedVersions?: string[];
62
+ /**
63
+ * Called when the session cannot be continued after an event gap (the
64
+ * transport saw 410 and the automatic wmp.session.resume failed or had no
65
+ * resumption token). The application creates a new session
66
+ * (Transport Section 4.3.4).
67
+ */
68
+ onSessionLost?: (error: Error) => void;
69
+ /** Called for every refused completion, with the violation (never a member value). */
70
+ onRefusedCompletion?: (flowId: string, error: CompletionResultError) => void;
46
71
  }
47
72
  export declare class Peer implements PeerContext {
48
73
  private transport;
@@ -54,15 +79,125 @@ export declare class Peer implements PeerContext {
54
79
  private negotiatedVersion?;
55
80
  private resumptionToken?;
56
81
  private closed;
57
- private validator?;
58
- private authorize?;
82
+ private validator;
83
+ private authorize;
84
+ private validateCompletions;
85
+ private onRefusedCompletion;
86
+ private onSessionLost;
87
+ private supportedVersions;
88
+ /**
89
+ * Capabilities negotiated for a session, bound to that session's ID; undefined
90
+ * until known or after the session ends (gating then fails closed).
91
+ */
92
+ private negotiated?;
93
+ /**
94
+ * Revision of the cached grant: bumped whenever it changes. An operation that
95
+ * obtains a grant snapshot over an RPC (capability list/update, resume) notes
96
+ * the revision when it starts; if the grant changed meanwhile the snapshot may
97
+ * be older than what is cached, so it can only NARROW the grant (never widen
98
+ * it): a revocation is never undone by a late answer, and no lock is held
99
+ * across the RPC.
100
+ */
101
+ private capRev;
102
+ /**
103
+ * Orders session establishment. Every outgoing create takes a number when it is
104
+ * issued; every installation (an outgoing create's answer, an incoming create or
105
+ * resume, a resume's answer) raises `installedSeq` to at least the numbers issued
106
+ * so far. An outgoing create's answer is installed only if its number is above
107
+ * `installedSeq`: the newest request wins whatever order answers arrive in, and
108
+ * an answer issued before another session was installed is stale.
109
+ */
110
+ private sessionSeq;
111
+ private installedSeq;
112
+ /** The grant refresh in flight (one per session lifecycle) and the refreshes requested meanwhile, per authority. */
113
+ private grantRefresh?;
114
+ private grantRefreshPending;
59
115
  constructor(transport: Transport, opts?: PeerOptions);
60
116
  /** Register a profile. Call before starting to receive messages. */
61
117
  use(profile: Profile): void;
118
+ /**
119
+ * Incremented whenever the session this peer holds state for is replaced or
120
+ * ends. A response or handler result that was requested under an earlier
121
+ * value belongs to a session that is gone and must not touch the current one.
122
+ */
123
+ private lifecycle;
124
+ /** A usable session identity: a non-empty string. Anything else is no identity (the grant is then unbound and admits nothing). */
125
+ private static usableId;
126
+ /** A new session: what it granted replaces what the previous one did (nothing if it omits them). */
127
+ private beginSession;
128
+ private endSession;
129
+ /**
130
+ * A resume of `sessionId`; `rev` is the grant revision when the resume was
131
+ * requested. The SAME session keeps its identity: a capability set the result
132
+ * lists is a snapshot (applied like a capability result), an omitted set leaves
133
+ * the grant, and neither the lifecycle nor the flow state changes. Another
134
+ * session is a replacement: its grant is what the result lists (nothing if
135
+ * omitted), the lifecycle moves on and the old session's flow state is dropped.
136
+ */
137
+ private resumeNegotiated;
138
+ /**
139
+ * Applies a grant snapshot obtained at revision `rev`: current revision means
140
+ * it is the latest word and replaces the grant; a changed revision means it may
141
+ * be stale, so it only removes capabilities that are no longer listed.
142
+ */
143
+ private applySnapshot;
144
+ /**
145
+ * One fresh capability.list at a time; its answer is applied like any other (at
146
+ * the revision it was requested at). A request that arrives while one is in
147
+ * flight is remembered and runs after it, so the grant is refreshed after the
148
+ * LAST change that may have made a snapshot stale.
149
+ */
150
+ private scheduleGrantRefresh;
151
+ /**
152
+ * A capability update/list result, applied only to an existing session that is
153
+ * still the one it was requested under: `gen` and `sessionId` are what was
154
+ * current when the operation started, so a late result cannot recreate a closed
155
+ * session's grant or overwrite a newer session's; `rev` orders it against other
156
+ * changes to the grant (see `capRev`).
157
+ */
158
+ private refreshNegotiated;
159
+ /** Whether `capability` was negotiated for the session the request names (fail closed). */
160
+ private hasNegotiated;
161
+ /** The highest supported version (what a client offers). */
162
+ private offeredVersion;
62
163
  /** The negotiated session ID (set after session.create). */
63
164
  get session(): string | undefined;
165
+ /**
166
+ * The queue of the flow.start request whose handler is being invoked, set only
167
+ * for the synchronous part of that invocation. Continuations registered with
168
+ * {@link afterResponse} belong to that request alone, so a start that fails
169
+ * (for example a duplicate) can only discard its own.
170
+ */
171
+ private afterTarget?;
172
+ /**
173
+ * PeerContext: queue `fn` to run once the response to the flow.start being
174
+ * handled was sent. Called outside that handler invocation there is no
175
+ * response to wait for, and `fn` runs on a zero-delay timer instead.
176
+ */
177
+ afterResponse(fn: () => void): void;
64
178
  notify(method: string, params: unknown): Promise<void>;
179
+ /** PeerContext: the generation token of an active flow (see {@link PeerContext.flowGeneration}). */
180
+ flowGeneration(flowId: string): object | undefined;
181
+ /**
182
+ * PeerContext: send a flow notification on behalf of one flow generation. A
183
+ * generation that is no longer the flow's (the flow ended, was cancelled, and
184
+ * the id may have been started again) sends nothing and never touches the
185
+ * current flow's record.
186
+ */
187
+ notifyFlow(generation: object | undefined, method: string, params: unknown): Promise<void>;
188
+ /** Flow ids with a notification still being sent (their reuse is refused meanwhile). */
189
+ private sendingFlow;
190
+ /** Drops the validation context of a flow that ended; the flow_id is free to start again. */
191
+ private releaseFlow;
192
+ /**
193
+ * Reclaims a flow id's generation once nothing needs it: the flow is not active
194
+ * and no cancellation for it is pending (a pending one compares against it).
195
+ * Without this every finished flow would leave an entry behind for the whole
196
+ * session. A missing entry reads as generation 0.
197
+ */
198
+ private releaseGen;
65
199
  call<T = unknown>(method: string, params: unknown): Promise<T>;
200
+ private callNow;
66
201
  /** Initiate a WMP session. */
67
202
  createSession(opts: {
68
203
  participants?: string[];
@@ -77,10 +212,26 @@ export declare class Peer implements PeerContext {
77
212
  ttl?: number;
78
213
  invitationNonce?: string;
79
214
  }): Promise<SessionCreateResult>;
80
- /** Resume a previously established WMP session using a resumption token. */
215
+ /** Sends a session.create and installs the answer (validated, ordered against other session changes). */
216
+ private createVia;
217
+ /**
218
+ * Resume a previously established WMP session using a resumption token.
219
+ * Resumes run one at a time (explicit calls and gap recovery alike): the token
220
+ * is single use, so a second resume must wait for the first to store the
221
+ * rotated one and then present that.
222
+ */
81
223
  resumeSession(opts?: {
82
224
  lastReceivedId?: string;
83
225
  }): Promise<SessionResumeResult>;
226
+ /**
227
+ * `forEpoch`: recovery work that is only valid for the session it was queued
228
+ * for; if that session was closed or replaced (or the peer closed) while it
229
+ * waited its turn, it is dropped (StaleSessionError) instead of resuming the
230
+ * session that replaced it.
231
+ */
232
+ private resumeSerialized;
233
+ private resumeChain;
234
+ private resumeNow;
84
235
  /** The current resumption token (set after session.create or session.resume). */
85
236
  get token(): string | undefined;
86
237
  /**
@@ -108,6 +259,69 @@ export declare class Peer implements PeerContext {
108
259
  private handleResponse;
109
260
  private handleRequest;
110
261
  private dispatchMethod;
262
+ /**
263
+ * The validation context of every ACTIVE flow (started, not yet ended), by
264
+ * flow_id: its type and, for match, the ids of the verifier's query. Never
265
+ * evicted to make room: a new start is refused at capacity instead of
266
+ * weakening an existing flow's checks.
267
+ */
268
+ private recordedFlows;
269
+ /**
270
+ * Flows that ended (complete, error, a refused completion or a cancel), by
271
+ * flow_id. A finished or refused flow is not an unknown one: a further terminal
272
+ * message for it is ignored instead of being judged by the general rules, and
273
+ * it holds no active-flow slot. Never evicted (it lives as long as the session)
274
+ * and cleared when the session ends or changes.
275
+ */
276
+ private terminalSeen;
277
+ private markTerminal;
278
+ /**
279
+ * A flow ended: drop its validation context and, if this peer knew the flow
280
+ * (it was started here or by the remote peer and not yet ended), remember how
281
+ * it ended. A terminal message for a flow this peer never saw start is
282
+ * delivered as before but not remembered, so a remote peer cannot grow the
283
+ * history with fresh ids.
284
+ */
285
+ private endFlow;
286
+ /** A new or ended session: the old session's flow state does not carry over. */
287
+ private resetFlows;
288
+ /**
289
+ * Records a starting flow and returns the function that undoes it. Refuses
290
+ * (-32602) a flow_id that is already active, so a second start cannot replace
291
+ * the context of the first, and (-31007) a start at capacity.
292
+ */
293
+ private beginFlow;
294
+ /**
295
+ * The violation of a completion's result, if any. The check is selected by the
296
+ * flow type recorded when the flow started, never by members of the result
297
+ * (openid4x 3.3.3): a match flow's result is the closed set, and a match
298
+ * completion without a result is refused too.
299
+ */
300
+ private completionRefusal;
301
+ /**
302
+ * A non-conforming completion is not delivered. The flow ends as failed: a
303
+ * synthesized wmp.flow.error, carrying neither the result nor the violation
304
+ * text, goes to whoever would have received the completion or the error.
305
+ */
306
+ private refuseCompletion;
307
+ /**
308
+ * Cancellations being processed, by flow_id, with the flow generation they
309
+ * belong to: a repeat for the SAME generation arriving meanwhile shares the
310
+ * outcome; a replacement flow under the same id never does.
311
+ */
312
+ private cancelling;
313
+ /** Cancellations still running per flow id, of ANY generation: a generation entry is kept while one is. */
314
+ private cancelCount;
315
+ /** Incremented whenever the flow state is reset (session closed or replaced). */
316
+ private flowEpoch;
317
+ /**
318
+ * A per-flow-id generation that survives the flow's completion: it changes only
319
+ * when a new flow starts under the id (and with every reset), so work tied to
320
+ * "this flow" can tell a replacement from the same flow having ended.
321
+ */
322
+ private flowGen;
323
+ /** Source of unique generation numbers (a rolled-back start never reuses one). */
324
+ private genCounter;
111
325
  private activeFlows;
112
326
  /** Look up the FlowHandler for an active flow by flow_id. */
113
327
  private flowHandlerForId;
@@ -117,5 +331,22 @@ export declare class Peer implements PeerContext {
117
331
  untrackFlow(flowId: string): void;
118
332
  private wmpMeta;
119
333
  private onTransportError;
334
+ /**
335
+ * The transport lost events (410): resume the session, which re-sends the
336
+ * latest progress of active flows and the terminal notification of ended
337
+ * flows on the already open stream. Without a resumption token, or if the
338
+ * resume fails, the session is lost.
339
+ */
340
+ private onTransportGap;
341
+ /**
342
+ * The session cannot be continued: the transport's binding to it is dropped
343
+ * BEFORE the application is told, so the "create a new session" fallback the
344
+ * callback is for does not send the dead session's id.
345
+ */
346
+ private sessionLost;
347
+ /** The transport is no longer bound to a session (no stale Wmp-Session-Id, cursor or event stream). */
348
+ private clearTransportBinding;
349
+ private recovery;
350
+ private recoveryQueuedEpoch;
120
351
  private onTransportClose;
121
352
  }