@astrale-os/kernel-client 0.6.0-beta.86 → 0.6.0-beta.88
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/auth/identity.d.ts +2 -1
- package/dist/client/credential.d.ts +2 -1
- package/dist/errors/stale-route-error.d.ts +4 -14
- package/dist/errors/stale-route-error.js +2 -13
- package/dist/errors/transport-error.d.ts +1 -1
- package/dist/graph/domain/domain.typecheck.js +1 -0
- package/dist/graph/graph.d.ts +1 -0
- package/dist/graph/index.d.ts +1 -1
- package/dist/graph/index.js +1 -1
- package/dist/graph/mutation.js +3 -1
- package/dist/schema/introspection.js +1 -5
- package/dist/schema/schema.js +4 -8
- package/dist/session/bound.d.ts +1 -1
- package/dist/session/bound.js +2 -1
- package/dist/session/content/api.d.ts +45 -0
- package/dist/session/content/api.js +28 -0
- package/dist/session/content/batch.d.ts +5 -0
- package/dist/session/content/batch.js +93 -0
- package/dist/session/content/index.d.ts +1 -0
- package/dist/session/content/index.js +1 -0
- package/dist/session/content/input.d.ts +9 -0
- package/dist/session/content/input.js +33 -0
- package/dist/session/content/parts.d.ts +36 -0
- package/dist/session/content/parts.js +154 -0
- package/dist/session/content/retry.d.ts +3 -0
- package/dist/session/content/retry.js +41 -0
- package/dist/session/content/wire.d.ts +12 -0
- package/dist/session/content/wire.js +60 -0
- package/dist/session/context/index.d.ts +1 -1
- package/dist/session/context/index.js +1 -1
- package/dist/session/context/limits.d.ts +0 -1
- package/dist/session/context/limits.js +0 -1
- package/dist/session/context/operation.d.ts +0 -1
- package/dist/session/context/operation.js +1 -1
- package/dist/session/context/policy.d.ts +0 -1
- package/dist/session/context/policy.js +0 -16
- package/dist/session/discovery/bundle.d.ts +4 -3
- package/dist/session/discovery/bundle.js +20 -21
- package/dist/session/discovery/discovery.d.ts +0 -3
- package/dist/session/discovery/discovery.js +6 -14
- package/dist/session/discovery/index.d.ts +0 -5
- package/dist/session/discovery/index.js +1 -2
- package/dist/session/discovery/release.d.ts +6 -5
- package/dist/session/discovery/release.js +3 -3
- package/dist/session/discovery/snapshot.d.ts +1 -8
- package/dist/session/discovery/snapshot.js +2 -2
- package/dist/session/exchange/domain.js +1 -1
- package/dist/session/exchange/source.d.ts +2 -1
- package/dist/session/index.d.ts +3 -2
- package/dist/session/index.js +1 -1
- package/dist/session/options.d.ts +65 -0
- package/dist/session/options.js +132 -0
- package/dist/session/routing/cache.d.ts +9 -14
- package/dist/session/routing/cache.js +17 -178
- package/dist/session/routing/dispatch.d.ts +4 -3
- package/dist/session/routing/dispatch.js +63 -113
- package/dist/session/routing/envelope.d.ts +3 -3
- package/dist/session/routing/expiry.d.ts +4 -0
- package/dist/session/routing/expiry.js +32 -0
- package/dist/session/routing/index.d.ts +4 -2
- package/dist/session/routing/index.js +4 -2
- package/dist/session/routing/integrity.d.ts +7 -0
- package/dist/session/routing/integrity.js +44 -0
- package/dist/session/routing/recovery.d.ts +16 -0
- package/dist/session/routing/recovery.js +41 -0
- package/dist/session/routing/result.d.ts +5 -0
- package/dist/session/routing/result.js +12 -0
- package/dist/session/routing/store.d.ts +23 -0
- package/dist/session/routing/store.js +99 -0
- package/dist/session/session.d.ts +6 -50
- package/dist/session/session.js +28 -134
- package/dist/session/session.typecheck.js +8 -3
- package/dist/session/views/catalog.d.ts +5 -9
- package/dist/session/views/catalog.js +12 -24
- package/dist/session/views/resolve.d.ts +1 -1
- package/dist/session/views/resolve.js +1 -1
- package/dist/store/entity-index.d.ts +1 -2
- package/dist/store/entity-index.js +0 -3
- package/dist/store/memory.js +2 -2
- package/dist/store/shape.d.ts +2 -1
- package/dist/transport/auto/decision.d.ts +3 -2
- package/dist/transport/http/admission.d.ts +5 -0
- package/dist/transport/http/admission.js +101 -0
- package/dist/transport/http/binary.d.ts +5 -0
- package/dist/transport/http/binary.js +70 -0
- package/dist/transport/http/configuration.d.ts +24 -0
- package/dist/transport/http/configuration.js +116 -0
- package/dist/transport/http/index.d.ts +2 -1
- package/dist/transport/http/response.d.ts +7 -0
- package/dist/transport/http/response.js +9 -0
- package/dist/transport/http/retired.d.ts +8 -0
- package/dist/transport/http/retired.js +28 -0
- package/dist/transport/http/transport.d.ts +1 -19
- package/dist/transport/http/transport.js +17 -299
- package/dist/transport/websocket/connection.d.ts +2 -1
- package/package.json +5 -5
- package/dist/session/content.d.ts +0 -27
- package/dist/session/content.js +0 -135
- package/dist/session/discovery/legacy/index.d.ts +0 -38
- package/dist/session/discovery/legacy/index.js +0 -38
- package/dist/session/discovery/legacy/intrinsic.d.ts +0 -33
- package/dist/session/discovery/legacy/intrinsic.js +0 -67
- package/dist/session/discovery/legacy/projection.d.ts +0 -8
- package/dist/session/discovery/legacy/projection.js +0 -17
- package/dist/session/discovery/legacy/publication.d.ts +0 -26
- package/dist/session/discovery/legacy/publication.js +0 -76
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { Client } from '../client/index.js';
|
|
2
|
+
import { ClientPool } from '../pool/index.js';
|
|
3
|
+
import { DEFAULT_SCHEMA_LIFECYCLE_TIMEOUT_MS } from '../schema/index.js';
|
|
4
|
+
import { AutoTransport } from '../transport/auto/index.js';
|
|
5
|
+
import { HttpTransport } from '../transport/http/index.js';
|
|
6
|
+
import { WebSocketTransport } from '../transport/websocket/index.js';
|
|
7
|
+
import { admitKernel, admitPolicy, DEFAULT_MAXIMUM_BUNDLE_BYTES, DEFAULT_MAXIMUM_RELEASE_BYTES, DEFAULT_MAXIMUM_RESPONSE_BYTES, DEFAULT_TIMEOUT_MS, kernelInvocationEndpoint, platformDuration, positive, } from './context/index.js';
|
|
8
|
+
import { admitExchangeOptions } from './exchange/index.js';
|
|
9
|
+
import { admitEnvelopeTransport, admitSessionAuth, RouteCache } from './routing/index.js';
|
|
10
|
+
/**
|
|
11
|
+
* Settle the caller's options in their public rejection order: the options object, policy, Kernel,
|
|
12
|
+
* Fetch, client/pool exclusivity, byte limits, durations, pool, route store, auth, exchange and
|
|
13
|
+
* envelope transport.
|
|
14
|
+
*/
|
|
15
|
+
export function acceptSessionOptions(options) {
|
|
16
|
+
if (options === null || typeof options !== 'object') {
|
|
17
|
+
throw new TypeError('ClientSession options are invalid.');
|
|
18
|
+
}
|
|
19
|
+
const policy = admitPolicy(options.policy);
|
|
20
|
+
const kernel = admitKernel(options.kernel, policy.allowInsecureHttp === true);
|
|
21
|
+
const sourceEndpoint = kernelInvocationEndpoint(kernel);
|
|
22
|
+
const fetch = options.fetch ?? globalThis.fetch;
|
|
23
|
+
if (typeof fetch !== 'function')
|
|
24
|
+
throw new TypeError('ClientSession requires Fetch.');
|
|
25
|
+
const fetchImpl = options.fetch === undefined ? fetch.bind(globalThis) : fetch;
|
|
26
|
+
if (options.client !== undefined && options.pool !== undefined) {
|
|
27
|
+
throw new TypeError('ClientSession accepts either client or pool, not both.');
|
|
28
|
+
}
|
|
29
|
+
const maximumReleaseBytes = positive(options.maximumReleaseBytes ?? DEFAULT_MAXIMUM_RELEASE_BYTES, 'maximumReleaseBytes');
|
|
30
|
+
const maximumBundleBytes = positive(options.maximumBundleBytes ?? DEFAULT_MAXIMUM_BUNDLE_BYTES, 'maximumBundleBytes');
|
|
31
|
+
const maximumResponseBytes = positive(options.maximumResponseBytes ?? DEFAULT_MAXIMUM_RESPONSE_BYTES, 'maximumResponseBytes');
|
|
32
|
+
const timeoutMs = platformDuration(options.timeoutMs ?? DEFAULT_TIMEOUT_MS, 'timeoutMs');
|
|
33
|
+
const schemaLifecycleTimeoutMs = platformDuration(options.schemaLifecycleTimeoutMs ?? DEFAULT_SCHEMA_LIFECYCLE_TIMEOUT_MS, 'schemaLifecycleTimeoutMs');
|
|
34
|
+
let ownedPool;
|
|
35
|
+
let pool;
|
|
36
|
+
if (options.pool === undefined) {
|
|
37
|
+
ownedPool = new ClientPool({
|
|
38
|
+
client: options.client ??
|
|
39
|
+
defaultClientFactory({
|
|
40
|
+
fetch: fetchImpl,
|
|
41
|
+
...(options.websocket === undefined ? {} : { websocket: options.websocket }),
|
|
42
|
+
...(options.onWarning === undefined ? {} : { onWarning: options.onWarning }),
|
|
43
|
+
maximumResponseBytes,
|
|
44
|
+
timeoutMs,
|
|
45
|
+
}),
|
|
46
|
+
});
|
|
47
|
+
pool = captureClientPool(ownedPool);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
pool = captureClientPool(options.pool);
|
|
51
|
+
}
|
|
52
|
+
// The route store is read while it is captured, so it keeps its place before auth and exchange.
|
|
53
|
+
const routes = new RouteCache(policy.maximumRouteAgeMs, undefined, options.routeStore);
|
|
54
|
+
const auth = admitSessionAuth(options.auth);
|
|
55
|
+
const exchange = admitExchangeOptions(options.exchange, auth);
|
|
56
|
+
return Object.freeze({
|
|
57
|
+
kernel,
|
|
58
|
+
sourceEndpoint,
|
|
59
|
+
policy,
|
|
60
|
+
fetch: fetchImpl,
|
|
61
|
+
maximumReleaseBytes,
|
|
62
|
+
maximumBundleBytes,
|
|
63
|
+
maximumResponseBytes,
|
|
64
|
+
timeoutMs,
|
|
65
|
+
schemaLifecycleTimeoutMs,
|
|
66
|
+
websocketAvailable: options.client !== undefined ||
|
|
67
|
+
options.pool !== undefined ||
|
|
68
|
+
options.websocket !== undefined ||
|
|
69
|
+
typeof WebSocket === 'function',
|
|
70
|
+
...(ownedPool === undefined ? {} : { ownedPool }),
|
|
71
|
+
pool,
|
|
72
|
+
routes,
|
|
73
|
+
auth,
|
|
74
|
+
exchange,
|
|
75
|
+
envelopeTransport: admitEnvelopeTransport(options.envelopeTransport ?? 'http'),
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
function captureClientPool(input) {
|
|
79
|
+
if (input === null ||
|
|
80
|
+
typeof input !== 'object' ||
|
|
81
|
+
typeof input.clientFor !== 'function') {
|
|
82
|
+
throw new TypeError('Session client pool capability is invalid.');
|
|
83
|
+
}
|
|
84
|
+
return Object.freeze({
|
|
85
|
+
clientFor: (target) => admitPooledClient(input.clientFor.call(input, target)),
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
function admitPooledClient(input) {
|
|
89
|
+
if (input === null ||
|
|
90
|
+
typeof input !== 'object' ||
|
|
91
|
+
typeof input.dispatch !== 'function' ||
|
|
92
|
+
typeof input.as !== 'function' ||
|
|
93
|
+
typeof input.close !== 'function') {
|
|
94
|
+
throw new TypeError('Session client pool capability returned an invalid Client.');
|
|
95
|
+
}
|
|
96
|
+
return input;
|
|
97
|
+
}
|
|
98
|
+
function defaultClientFactory(options) {
|
|
99
|
+
return (target) => {
|
|
100
|
+
switch (target.transport) {
|
|
101
|
+
case 'http':
|
|
102
|
+
return new Client({
|
|
103
|
+
transport: new HttpTransport({
|
|
104
|
+
url: target.url,
|
|
105
|
+
fetch: options.fetch,
|
|
106
|
+
timeoutMs: options.timeoutMs,
|
|
107
|
+
maximumResponseBytes: options.maximumResponseBytes,
|
|
108
|
+
}),
|
|
109
|
+
});
|
|
110
|
+
case 'websocket':
|
|
111
|
+
return new Client({
|
|
112
|
+
transport: new WebSocketTransport({
|
|
113
|
+
url: target.url,
|
|
114
|
+
timeoutMs: options.timeoutMs,
|
|
115
|
+
maximumResponseBytes: options.maximumResponseBytes,
|
|
116
|
+
...(options.websocket === undefined ? {} : { websocket: options.websocket }),
|
|
117
|
+
}),
|
|
118
|
+
});
|
|
119
|
+
case 'auto':
|
|
120
|
+
return new Client({
|
|
121
|
+
transport: new AutoTransport({
|
|
122
|
+
endpoints: target.endpoints,
|
|
123
|
+
fetch: options.fetch,
|
|
124
|
+
timeoutMs: options.timeoutMs,
|
|
125
|
+
maximumResponseBytes: options.maximumResponseBytes,
|
|
126
|
+
...(options.websocket === undefined ? {} : { websocket: options.websocket }),
|
|
127
|
+
...(options.onWarning === undefined ? {} : { onWarning: options.onWarning }),
|
|
128
|
+
}),
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
}
|
|
@@ -1,22 +1,18 @@
|
|
|
1
|
-
import { invocation } from '@astrale-os/kernel-protocol';
|
|
1
|
+
import type { invocation } from '@astrale-os/kernel-protocol';
|
|
2
2
|
import type { EffectiveAuth } from './auth.js';
|
|
3
|
+
import type { SessionRouteStore } from './store.js';
|
|
3
4
|
export interface LearnedRoute {
|
|
4
5
|
readonly route: invocation.Route;
|
|
5
6
|
readonly credential?: string;
|
|
6
7
|
readonly expiresAt: number;
|
|
7
8
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
readonly
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
readonly
|
|
14
|
-
readonly
|
|
15
|
-
}
|
|
16
|
-
/** Caller-owned persistence for confidential route artifacts. */
|
|
17
|
-
export interface SessionRouteStore {
|
|
18
|
-
read(): unknown;
|
|
19
|
-
write(artifact: SessionRouteArtifact): void;
|
|
9
|
+
/** One warm entry; `sealed` and `integrity` are present only when it can be persisted. */
|
|
10
|
+
export interface CachedRoute {
|
|
11
|
+
readonly learned: LearnedRoute;
|
|
12
|
+
/** Separate from route age; derived only from a supported finite carrier expiry. */
|
|
13
|
+
readonly credentialRefreshBoundary?: number;
|
|
14
|
+
readonly sealed?: LearnedRoute;
|
|
15
|
+
readonly integrity?: string;
|
|
20
16
|
}
|
|
21
17
|
/** Confidential warm routing state owned by exactly one ClientSession. */
|
|
22
18
|
export declare class RouteCache {
|
|
@@ -35,6 +31,5 @@ export declare class RouteCache {
|
|
|
35
31
|
private invalidate;
|
|
36
32
|
private reserve;
|
|
37
33
|
private restore;
|
|
38
|
-
private restoreInput;
|
|
39
34
|
private persist;
|
|
40
35
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { invocation } from '@astrale-os/kernel-protocol';
|
|
2
1
|
import { DEFAULT_MAXIMUM_ROUTES } from '../context/index.js';
|
|
3
|
-
|
|
2
|
+
import { credentialExpiry, refreshBoundary } from './expiry.js';
|
|
3
|
+
import { digestText, matchesIntegrity, routeIntegrity } from './integrity.js';
|
|
4
|
+
import { captureRouteStore, restoreRoutes, routeArtifact } from './store.js';
|
|
4
5
|
/** Confidential warm routing state owned by exactly one ClientSession. */
|
|
5
6
|
export class RouteCache {
|
|
6
7
|
maximumAgeMs;
|
|
@@ -11,12 +12,14 @@ export class RouteCache {
|
|
|
11
12
|
constructor(maximumAgeMs, maximumSize = DEFAULT_MAXIMUM_ROUTES, store) {
|
|
12
13
|
this.maximumAgeMs = maximumAgeMs;
|
|
13
14
|
this.maximumSize = maximumSize;
|
|
14
|
-
this.store = store === undefined ? undefined :
|
|
15
|
+
this.store = store === undefined ? undefined : captureRouteStore(store);
|
|
15
16
|
this.restore();
|
|
16
17
|
}
|
|
17
18
|
async key(source, call, auth) {
|
|
18
19
|
const credential = auth.credential === undefined ? 'anonymous' : await digestText(auth.credential);
|
|
19
|
-
const delegate = auth.delegate === undefined
|
|
20
|
+
const delegate = auth.delegate === undefined
|
|
21
|
+
? 'anonymous'
|
|
22
|
+
: await digestText(JSON.stringify({ ttlSeconds: auth.delegate.ttlSeconds }));
|
|
20
23
|
return JSON.stringify([source, call.target, credential, delegate]);
|
|
21
24
|
}
|
|
22
25
|
async get(key, sourceCredential, now, operationDeadline) {
|
|
@@ -55,16 +58,16 @@ export class RouteCache {
|
|
|
55
58
|
return cached.learned;
|
|
56
59
|
}
|
|
57
60
|
async learn(key, route, credential, sourceCredential, now = Date.now()) {
|
|
58
|
-
const
|
|
59
|
-
const credentialRefreshBoundary =
|
|
60
|
-
const expiresAt = Math.min(now + this.maximumAgeMs, refreshBoundary(
|
|
61
|
+
const expiry = credential === undefined ? undefined : credentialExpiry(credential);
|
|
62
|
+
const credentialRefreshBoundary = expiry === undefined ? undefined : refreshBoundary(expiry);
|
|
63
|
+
const expiresAt = Math.min(now + this.maximumAgeMs, refreshBoundary(expiry));
|
|
61
64
|
const entry = Object.freeze({
|
|
62
65
|
route,
|
|
63
66
|
...(credential === undefined ? {} : { credential }),
|
|
64
67
|
expiresAt,
|
|
65
68
|
});
|
|
66
69
|
// A carrier with no supported finite expiry may be used for this follow, but never warmed.
|
|
67
|
-
if (credential !== undefined &&
|
|
70
|
+
if (credential !== undefined && expiry === undefined)
|
|
68
71
|
return entry;
|
|
69
72
|
if (expiresAt <= now)
|
|
70
73
|
return entry;
|
|
@@ -119,189 +122,25 @@ export class RouteCache {
|
|
|
119
122
|
if (this.store === undefined)
|
|
120
123
|
return;
|
|
121
124
|
try {
|
|
122
|
-
|
|
125
|
+
const restored = restoreRoutes(this.store.read(), now, this.maximumAgeMs, this.maximumSize);
|
|
126
|
+
for (const [key, entry] of restored) {
|
|
127
|
+
this.reserve(key);
|
|
128
|
+
this.entries.set(key, entry);
|
|
129
|
+
}
|
|
123
130
|
}
|
|
124
131
|
catch {
|
|
125
132
|
this.keyTokens.clear();
|
|
126
133
|
this.entries.clear();
|
|
127
134
|
}
|
|
128
135
|
}
|
|
129
|
-
restoreInput(input, now) {
|
|
130
|
-
if (input === undefined)
|
|
131
|
-
return;
|
|
132
|
-
if (typeof input !== 'object' || input === null || Array.isArray(input))
|
|
133
|
-
return;
|
|
134
|
-
const artifact = input;
|
|
135
|
-
if (artifact.version !== 1 ||
|
|
136
|
-
typeof artifact.entries !== 'object' ||
|
|
137
|
-
artifact.entries === null ||
|
|
138
|
-
Array.isArray(artifact.entries)) {
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
let count = 0;
|
|
142
|
-
for (const key in artifact.entries) {
|
|
143
|
-
if (!Object.hasOwn(artifact.entries, key))
|
|
144
|
-
continue;
|
|
145
|
-
count += 1;
|
|
146
|
-
if (count > this.maximumSize) {
|
|
147
|
-
this.keyTokens.clear();
|
|
148
|
-
this.entries.clear();
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
const value = Reflect.get(artifact.entries, key);
|
|
152
|
-
const entry = admitStoredEntry(value, now, this.maximumAgeMs);
|
|
153
|
-
if (entry === undefined)
|
|
154
|
-
continue;
|
|
155
|
-
this.reserve(key);
|
|
156
|
-
this.entries.set(key, entry);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
136
|
persist() {
|
|
160
137
|
if (this.store === undefined)
|
|
161
138
|
return;
|
|
162
|
-
const entries = Object.fromEntries([...this.entries].flatMap(([key, cached]) => cached.integrity === undefined
|
|
163
|
-
? []
|
|
164
|
-
: [
|
|
165
|
-
[
|
|
166
|
-
key,
|
|
167
|
-
Object.freeze({
|
|
168
|
-
...(cached.sealed ?? cached.learned),
|
|
169
|
-
integrity: cached.integrity,
|
|
170
|
-
}),
|
|
171
|
-
],
|
|
172
|
-
]));
|
|
173
139
|
try {
|
|
174
|
-
this.store.write(
|
|
140
|
+
this.store.write(routeArtifact(this.entries));
|
|
175
141
|
}
|
|
176
142
|
catch {
|
|
177
143
|
// A cache-store outage must not turn a valid invocation into a product failure.
|
|
178
144
|
}
|
|
179
145
|
}
|
|
180
146
|
}
|
|
181
|
-
function captureSessionRouteStore(input) {
|
|
182
|
-
if (input === null ||
|
|
183
|
-
typeof input !== 'object' ||
|
|
184
|
-
typeof input.read !== 'function' ||
|
|
185
|
-
typeof input.write !== 'function') {
|
|
186
|
-
throw new TypeError('Session route store capability is invalid.');
|
|
187
|
-
}
|
|
188
|
-
const read = input.read;
|
|
189
|
-
const write = input.write;
|
|
190
|
-
return Object.freeze({
|
|
191
|
-
read: () => read.call(input),
|
|
192
|
-
write: (artifact) => write.call(input, artifact),
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
|
-
function admitStoredEntry(input, now, maximumAgeMs) {
|
|
196
|
-
try {
|
|
197
|
-
if (typeof input !== 'object' || input === null || Array.isArray(input))
|
|
198
|
-
return undefined;
|
|
199
|
-
const value = input;
|
|
200
|
-
if ((value.credential !== undefined && typeof value.credential !== 'string') ||
|
|
201
|
-
typeof value.expiresAt !== 'number' ||
|
|
202
|
-
!Number.isSafeInteger(value.expiresAt) ||
|
|
203
|
-
typeof value.integrity !== 'string' ||
|
|
204
|
-
!/^[A-Za-z0-9_-]{43}$/u.test(value.integrity)) {
|
|
205
|
-
return undefined;
|
|
206
|
-
}
|
|
207
|
-
const credentialExpiry = value.credential === undefined ? undefined : jwtExpiry(value.credential);
|
|
208
|
-
if (value.credential !== undefined && credentialExpiry === undefined)
|
|
209
|
-
return undefined;
|
|
210
|
-
const sealed = Object.freeze({
|
|
211
|
-
route: invocation.acceptRoute(value.route),
|
|
212
|
-
...(value.credential === undefined ? {} : { credential: value.credential }),
|
|
213
|
-
expiresAt: value.expiresAt,
|
|
214
|
-
});
|
|
215
|
-
const expiresAt = Math.min(value.expiresAt, now + maximumAgeMs, refreshBoundary(credentialExpiry));
|
|
216
|
-
if (expiresAt <= now)
|
|
217
|
-
return undefined;
|
|
218
|
-
return Object.freeze({
|
|
219
|
-
learned: Object.freeze({
|
|
220
|
-
route: sealed.route,
|
|
221
|
-
...(sealed.credential === undefined ? {} : { credential: sealed.credential }),
|
|
222
|
-
expiresAt,
|
|
223
|
-
}),
|
|
224
|
-
sealed,
|
|
225
|
-
integrity: value.integrity,
|
|
226
|
-
...(credentialExpiry === undefined
|
|
227
|
-
? {}
|
|
228
|
-
: { credentialRefreshBoundary: refreshBoundary(credentialExpiry) }),
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
catch {
|
|
232
|
-
return undefined;
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
function refreshBoundary(credentialExpiry) {
|
|
236
|
-
return credentialExpiry === undefined
|
|
237
|
-
? Number.POSITIVE_INFINITY
|
|
238
|
-
: credentialExpiry - DESTINATION_CREDENTIAL_REFRESH_SKEW_MS;
|
|
239
|
-
}
|
|
240
|
-
async function routeIntegrity(sourceCredential, key, learned) {
|
|
241
|
-
const webCrypto = typeof crypto === 'undefined' ? undefined : crypto;
|
|
242
|
-
if (webCrypto?.subtle === undefined) {
|
|
243
|
-
throw new TypeError('Persistent Session route integrity requires Web Crypto HMAC support.');
|
|
244
|
-
}
|
|
245
|
-
const algorithm = Object.freeze({ name: 'HMAC', hash: 'SHA-256' });
|
|
246
|
-
const cryptographicKey = await webCrypto.subtle.importKey('raw', new TextEncoder().encode(sourceCredential), algorithm, false, ['sign']);
|
|
247
|
-
const signature = await webCrypto.subtle.sign(algorithm, cryptographicKey, new TextEncoder().encode(integrityMaterial(key, learned)));
|
|
248
|
-
return encodeBase64Url(new Uint8Array(signature));
|
|
249
|
-
}
|
|
250
|
-
async function matchesIntegrity(sourceCredential, key, learned, expected) {
|
|
251
|
-
try {
|
|
252
|
-
return (await routeIntegrity(sourceCredential, key, learned)) === expected;
|
|
253
|
-
}
|
|
254
|
-
catch {
|
|
255
|
-
return false;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
function integrityMaterial(key, learned) {
|
|
259
|
-
return JSON.stringify({
|
|
260
|
-
version: 1,
|
|
261
|
-
key,
|
|
262
|
-
route: learned.route,
|
|
263
|
-
...(learned.credential === undefined ? {} : { credential: learned.credential }),
|
|
264
|
-
expiresAt: learned.expiresAt,
|
|
265
|
-
});
|
|
266
|
-
}
|
|
267
|
-
function encodeBase64Url(input) {
|
|
268
|
-
let binary = '';
|
|
269
|
-
for (const byte of input)
|
|
270
|
-
binary += String.fromCharCode(byte);
|
|
271
|
-
return globalThis.btoa(binary).replace(/\+/gu, '-').replace(/\//gu, '_').replace(/=+$/gu, '');
|
|
272
|
-
}
|
|
273
|
-
function canonicalDelegate(input) {
|
|
274
|
-
return JSON.stringify({ ttlSeconds: input.ttlSeconds });
|
|
275
|
-
}
|
|
276
|
-
async function digestText(input) {
|
|
277
|
-
const webCrypto = typeof crypto === 'undefined' ? undefined : crypto;
|
|
278
|
-
if (webCrypto?.subtle === undefined) {
|
|
279
|
-
throw new TypeError('Session route caching requires Web Crypto SHA-256 support.');
|
|
280
|
-
}
|
|
281
|
-
const bytes = await webCrypto.subtle.digest('SHA-256', new TextEncoder().encode(input));
|
|
282
|
-
return [...new Uint8Array(bytes)].map((byte) => byte.toString(16).padStart(2, '0')).join('');
|
|
283
|
-
}
|
|
284
|
-
function jwtExpiry(input) {
|
|
285
|
-
const parts = input.split('.');
|
|
286
|
-
if (parts.length !== 3)
|
|
287
|
-
return undefined;
|
|
288
|
-
try {
|
|
289
|
-
const encoded = parts[1].replace(/-/gu, '+').replace(/_/gu, '/');
|
|
290
|
-
const padding = '='.repeat((4 - (encoded.length % 4)) % 4);
|
|
291
|
-
const json = decodeBase64(encoded + padding);
|
|
292
|
-
const payload = JSON.parse(json);
|
|
293
|
-
return typeof payload.exp === 'number' && Number.isSafeInteger(payload.exp) && payload.exp > 0
|
|
294
|
-
? payload.exp * 1_000
|
|
295
|
-
: undefined;
|
|
296
|
-
}
|
|
297
|
-
catch {
|
|
298
|
-
return undefined;
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
function decodeBase64(input) {
|
|
302
|
-
if (typeof globalThis.atob === 'function') {
|
|
303
|
-
const binary = globalThis.atob(input);
|
|
304
|
-
return new TextDecoder().decode(Uint8Array.from(binary, (character) => character.charCodeAt(0)));
|
|
305
|
-
}
|
|
306
|
-
throw new TypeError('Session route caching requires base64 decoding support.');
|
|
307
|
-
}
|
|
@@ -31,12 +31,13 @@ export declare class SessionDispatcher {
|
|
|
31
31
|
/** Reuse Session authority and route recovery while forcing HTTP for streaming calls. */
|
|
32
32
|
dispatchHttp(input: Parameters<typeof acceptCall>[0], options: SessionRequestOptions, operation: SessionOperation, body?: Blob): Promise<Result>;
|
|
33
33
|
private dispatchWith;
|
|
34
|
+
/** The cached route for this partition, evicted when it no longer passes delivery checks. */
|
|
35
|
+
private warmRoute;
|
|
34
36
|
private dispatchSource;
|
|
35
37
|
private followSource;
|
|
36
38
|
private dispatchDestinationWithRecovery;
|
|
37
39
|
private dispatchDestination;
|
|
40
|
+
/** The Client for a destination: the pooled envelope, or a fresh HTTP Client when streaming. */
|
|
41
|
+
private destinationClient;
|
|
38
42
|
private streamingClientFor;
|
|
39
43
|
}
|
|
40
|
-
export declare function requireResultKind<Kind extends 'value' | 'stream' | 'binary'>(expected: Kind, result: Result): Extract<Result, {
|
|
41
|
-
kind: Kind;
|
|
42
|
-
}>;
|