@mcp-abap-adt/connection 15.1.0 → 17.0.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 +187 -1
- package/README.md +137 -33
- package/dist/connection/AbstractAbapConnection.d.ts +140 -58
- package/dist/connection/AbstractAbapConnection.d.ts.map +1 -1
- package/dist/connection/AbstractAbapConnection.js +349 -255
- package/dist/connection/AdtCloudConnector.d.ts +6 -1
- package/dist/connection/AdtCloudConnector.d.ts.map +1 -1
- package/dist/connection/AdtCloudConnector.js +7 -2
- package/dist/connection/AdtOnPremConnector.d.ts +6 -1
- package/dist/connection/AdtOnPremConnector.d.ts.map +1 -1
- package/dist/connection/AdtOnPremConnector.js +7 -2
- package/dist/connection/CloudHttpTransport.d.ts +8 -1
- package/dist/connection/CloudHttpTransport.d.ts.map +1 -1
- package/dist/connection/CloudHttpTransport.js +30 -18
- package/dist/connection/CredentialAbapConnection.d.ts +3 -1
- package/dist/connection/CredentialAbapConnection.d.ts.map +1 -1
- package/dist/connection/CredentialAbapConnection.js +4 -2
- package/dist/connection/GenericWebSocketTransport.d.ts.map +1 -1
- package/dist/connection/GenericWebSocketTransport.js +7 -4
- package/dist/connection/HttpTransport.d.ts +80 -6
- package/dist/connection/HttpTransport.d.ts.map +1 -1
- package/dist/connection/HttpTransport.js +258 -25
- package/dist/connection/IAdtTransport.d.ts +46 -22
- package/dist/connection/IAdtTransport.d.ts.map +1 -1
- package/dist/connection/OnPremHttpTransport.d.ts +2 -13
- package/dist/connection/OnPremHttpTransport.d.ts.map +1 -1
- package/dist/connection/OnPremHttpTransport.js +16 -32
- package/dist/connection/RfcTransport.d.ts +7 -6
- package/dist/connection/RfcTransport.d.ts.map +1 -1
- package/dist/connection/RfcTransport.js +27 -49
- package/dist/connection/createConnection.d.ts +95 -0
- package/dist/connection/createConnection.d.ts.map +1 -0
- package/dist/connection/createConnection.js +44 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -5
- package/docs/INDEX.md +6 -2
- package/docs/MIGRATION-16.0.md +123 -0
- package/docs/MIGRATION-17.0.md +119 -0
- package/docs/SCOPE.md +22 -45
- package/docs/STATEFUL_SESSION_GUIDE.md +49 -49
- package/docs/USAGE.md +244 -120
- package/package.json +2 -2
- package/dist/connection/requestAbort.d.ts +0 -32
- package/dist/connection/requestAbort.d.ts.map +0 -1
- package/dist/connection/requestAbort.js +0 -69
- package/dist/utils/timeouts.d.ts +0 -18
- package/dist/utils/timeouts.d.ts.map +0 -1
- package/dist/utils/timeouts.js +0 -37
|
@@ -5,11 +5,9 @@ const node_crypto_1 = require("node:crypto");
|
|
|
5
5
|
const interfaces_adt_connection_1 = require("@mcp-abap-adt/interfaces-adt-connection");
|
|
6
6
|
const SessionLifecycle_js_1 = require("../session/SessionLifecycle.js");
|
|
7
7
|
const networkErrors_js_1 = require("../utils/networkErrors.js");
|
|
8
|
-
const timeouts_js_1 = require("../utils/timeouts.js");
|
|
9
8
|
const authErrors_js_1 = require("./authErrors.js");
|
|
10
9
|
const csrfConfig_js_1 = require("./csrfConfig.js");
|
|
11
10
|
const IAdtTransport_js_1 = require("./IAdtTransport.js");
|
|
12
|
-
const requestAbort_js_1 = require("./requestAbort.js");
|
|
13
11
|
/**
|
|
14
12
|
* Declares the capabilities explicitly rather than satisfying them by accident.
|
|
15
13
|
* `AbapConnection` is the base contract every transport honours; these two are
|
|
@@ -49,6 +47,10 @@ function hasHeader(headers, name) {
|
|
|
49
47
|
* wire's session token and the request's content negotiation.
|
|
50
48
|
*/
|
|
51
49
|
const OUTRANK_THE_CREDENTIAL = ['x-csrf-token', 'content-type', 'accept'];
|
|
50
|
+
/** What a spent instance answers to `connect()`. Fixed words. */
|
|
51
|
+
function spentError() {
|
|
52
|
+
return (0, SessionLifecycle_js_1.sessionError)(interfaces_adt_connection_1.ADT_SESSION_ERROR.NOT_CONNECTED, "This connection's SAP session has ended (it was disconnected, logged off or lost); one connection instance is one session. Create a new connection for a new session.");
|
|
53
|
+
}
|
|
52
54
|
class AbstractAbapConnection {
|
|
53
55
|
config;
|
|
54
56
|
transport;
|
|
@@ -80,22 +82,20 @@ class AbstractAbapConnection {
|
|
|
80
82
|
profilingRequest = 'server-time';
|
|
81
83
|
/**
|
|
82
84
|
* When true, requests are treated as part of an uninterruptible critical
|
|
83
|
-
* section (e.g. a lock → modify → unlock chain). In this state a
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
* lost while the lock lives on in that session. While set, makeAdtRequest
|
|
89
|
-
* raises the effective timeout to CRITICAL_SECTION_TIMEOUT (a large ceiling)
|
|
90
|
-
* so the request runs to completion instead of being interrupted.
|
|
85
|
+
* section (e.g. a lock → modify → unlock chain). In this state a request
|
|
86
|
+
* carries no deadline at all, not even the caller's: a client deadline closes
|
|
87
|
+
* the socket, SAP then ends the request running on it — a write in it lost —
|
|
88
|
+
* and what this side knew goes with it: whether the change was applied, and
|
|
89
|
+
* the handle `unlock` needs, while the lock lives on in that session.
|
|
91
90
|
*/
|
|
92
91
|
/**
|
|
93
92
|
* The last goodbye this connection dispatched, so a caller can wait for it.
|
|
94
93
|
*
|
|
95
|
-
* `disconnect()`
|
|
96
|
-
* there. That is right for a teardown and wrong for a caller
|
|
97
|
-
* `connect()` again: the next session opens while the
|
|
98
|
-
* is still
|
|
94
|
+
* `disconnect()` waits for the logoff to be written, not answered, for the
|
|
95
|
+
* reason stated there. That is right for a teardown and wrong for a caller
|
|
96
|
+
* who is about to `connect()` again: the next session opens while the
|
|
97
|
+
* previous one's goodbye is still unanswered, and a caller who recycles in a
|
|
98
|
+
* loop leaves one
|
|
99
99
|
* abandoned session per iteration. Measured against E19 from
|
|
100
100
|
* `@mcp-abap-adt/adt-clients`: a session every 1-2 seconds for a whole test
|
|
101
101
|
* run, none of them released, each surviving to its 30-minute idle timeout.
|
|
@@ -104,6 +104,32 @@ class AbstractAbapConnection {
|
|
|
104
104
|
* before this, the promise was created and dropped on the floor.
|
|
105
105
|
*/
|
|
106
106
|
goodbye = Promise.resolve();
|
|
107
|
+
/**
|
|
108
|
+
* Whether `disconnect()` was called on this instance. A second call
|
|
109
|
+
* rejects: `disconnect()` is called once per instance, and a caller that calls it
|
|
110
|
+
* twice is told so rather than handed a success that waited for nothing.
|
|
111
|
+
*/
|
|
112
|
+
disconnectCalled = false;
|
|
113
|
+
/**
|
|
114
|
+
* Whether this instance's one SAP session has ended: it was disconnected,
|
|
115
|
+
* a failed `connect()` logged off the session it had received, or the
|
|
116
|
+
* session was lost (`raiseSessionLost`). One instance is one session —
|
|
117
|
+
* after its logoff it is spent, and `connect()` refuses. A new session is a
|
|
118
|
+
* new instance, which the consumer creates (its own factory, or
|
|
119
|
+
* `createConnection`).
|
|
120
|
+
*/
|
|
121
|
+
spent = false;
|
|
122
|
+
/**
|
|
123
|
+
* Every logoff `endSession()` dispatched for this instance, merged: what
|
|
124
|
+
* `disconnect()` waits for (the writes) and `flushGoodbye()` (the answers).
|
|
125
|
+
*/
|
|
126
|
+
ended = null;
|
|
127
|
+
/**
|
|
128
|
+
* The existing session this instance takes over at `connect()`, instead of
|
|
129
|
+
* logging on: its cookies, as `sessionCookies()` handed them out. Dropped
|
|
130
|
+
* once the takeover is attempted — a session is taken over once.
|
|
131
|
+
*/
|
|
132
|
+
resumeFrom;
|
|
107
133
|
inCriticalSection = false;
|
|
108
134
|
/** Reference count for nested beginCriticalSection()/endCriticalSection() pairs. */
|
|
109
135
|
criticalSectionDepth = 0;
|
|
@@ -161,10 +187,22 @@ class AbstractAbapConnection {
|
|
|
161
187
|
* arrives built. The credential's TLS material is not among them: the
|
|
162
188
|
* provider offers it to the wire at logon.
|
|
163
189
|
*/
|
|
164
|
-
transport, logger, sessionId
|
|
190
|
+
transport, logger, sessionId,
|
|
191
|
+
/**
|
|
192
|
+
* An existing session to take over instead of logging on: the cookies
|
|
193
|
+
* `sessionCookies()` handed out for it. `connect()` then sends no logon
|
|
194
|
+
* and asks SAP, without the credential, whether that session is alive.
|
|
195
|
+
*/
|
|
196
|
+
session) {
|
|
165
197
|
this.config = config;
|
|
166
198
|
this.transport = transport;
|
|
167
199
|
this.logger = logger;
|
|
200
|
+
// Fixed words: the session's cookies are never echoed.
|
|
201
|
+
if (session !== undefined &&
|
|
202
|
+
(typeof session !== 'string' || session.trim() === '')) {
|
|
203
|
+
throw new TypeError('session: the cookies of an existing session, as sessionCookies() returned them; an empty one names no session.');
|
|
204
|
+
}
|
|
205
|
+
this.resumeFrom = session ?? null;
|
|
168
206
|
// Generate sessionId (used for sap-adt-connection-id header)
|
|
169
207
|
this.sessionId = sessionId || (0, node_crypto_1.randomUUID)();
|
|
170
208
|
// Initialize baseUrl from config (required, will throw if invalid)
|
|
@@ -218,14 +256,13 @@ class AbstractAbapConnection {
|
|
|
218
256
|
* Enter an uninterruptible critical section.
|
|
219
257
|
*
|
|
220
258
|
* Call this BEFORE acquiring a lock (and pair it with endCriticalSection()
|
|
221
|
-
* in a finally, AFTER unlocking). While in a critical section
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
*
|
|
225
|
-
*
|
|
226
|
-
*
|
|
227
|
-
*
|
|
228
|
-
* lock survives inside the ABAP session that owns it.
|
|
259
|
+
* in a finally, AFTER unlocking). While in a critical section a request
|
|
260
|
+
* carries no deadline — the caller's `timeout` is not applied — so a slow
|
|
261
|
+
* PUT/activate/unlock is not cut mid-flight. A client deadline closes the
|
|
262
|
+
* socket, and SAP then ends the request that was running on it (a write in
|
|
263
|
+
* it lost); and it strands the caller: the outcome is unknown and the handle
|
|
264
|
+
* `unlock` needs is gone, while the lock survives inside the ABAP session
|
|
265
|
+
* that owns it.
|
|
229
266
|
*
|
|
230
267
|
* Nesting is reference-counted so nested begin/end pairs are safe.
|
|
231
268
|
*/
|
|
@@ -236,8 +273,8 @@ class AbstractAbapConnection {
|
|
|
236
273
|
}
|
|
237
274
|
/**
|
|
238
275
|
* Leave the uninterruptible critical section. See beginCriticalSection().
|
|
239
|
-
* Safe to call more times than begin (clamped at 0).
|
|
240
|
-
* timeouts
|
|
276
|
+
* Safe to call more times than begin (clamped at 0). The caller's own
|
|
277
|
+
* per-request timeouts apply again once the outermost section ends.
|
|
241
278
|
*/
|
|
242
279
|
endCriticalSection() {
|
|
243
280
|
if (this.criticalSectionDepth > 0) {
|
|
@@ -295,13 +332,31 @@ class AbstractAbapConnection {
|
|
|
295
332
|
// requested afterwards has already bumped the epoch — and comparing it
|
|
296
333
|
// against itself would let the connect publish a session the caller had
|
|
297
334
|
// asked to stop. The baseline is "when the caller asked to connect".
|
|
335
|
+
// One instance is one SAP session: once that session has ended — a
|
|
336
|
+
// disconnect(), a failed connect() that logged it off, or a lost
|
|
337
|
+
// session — the instance is spent, and a new session needs a new
|
|
338
|
+
// instance.
|
|
339
|
+
if (this.spent)
|
|
340
|
+
throw spentError();
|
|
298
341
|
const baselineEpoch = this.lifecycle.teardownEpoch;
|
|
299
342
|
await this.lifecycle.transition('connect', async () => {
|
|
343
|
+
// Again here: a disconnect() queued ahead of this connect — called in
|
|
344
|
+
// the same turn, before it — spends the instance only when its
|
|
345
|
+
// transition runs, after the check at entry. Nothing is asked of the
|
|
346
|
+
// credential on a spent instance.
|
|
347
|
+
if (this.spent)
|
|
348
|
+
throw spentError();
|
|
300
349
|
if (this.lifecycle.connected)
|
|
301
350
|
return;
|
|
302
351
|
// Before the wire opens, so that a credential which cannot get ready
|
|
303
352
|
// sends nothing and the wire's logon has something to bring.
|
|
304
353
|
await this.prepareCredential();
|
|
354
|
+
// An instance built on an existing session takes it over: no logon,
|
|
355
|
+
// and SAP's answer to one request says whether the session is alive.
|
|
356
|
+
if (this.resumeFrom !== null) {
|
|
357
|
+
await this.resumeAndCommit(baselineEpoch);
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
305
360
|
// The wire logs on inside, through the context, and then the session is
|
|
306
361
|
// established: one step, which a refused logon repeats once.
|
|
307
362
|
try {
|
|
@@ -309,8 +364,12 @@ class AbstractAbapConnection {
|
|
|
309
364
|
}
|
|
310
365
|
catch (error) {
|
|
311
366
|
const rejection = this.credentialRejection(error);
|
|
367
|
+
// A failed establishment that ended a session it had received (a
|
|
368
|
+
// cloud preflight's, say) has spent the instance: no second attempt
|
|
369
|
+
// on it — the original failure is the answer.
|
|
312
370
|
if (!(error instanceof authErrors_js_1.WireLogonError) ||
|
|
313
371
|
!rejection ||
|
|
372
|
+
this.spent ||
|
|
314
373
|
this.lifecycle.teardownEpoch !== baselineEpoch) {
|
|
315
374
|
throw error;
|
|
316
375
|
}
|
|
@@ -344,12 +403,29 @@ class AbstractAbapConnection {
|
|
|
344
403
|
* request cannot use, so the leak surfaces as a half-written object rather
|
|
345
404
|
* than as anything about sessions.
|
|
346
405
|
*
|
|
347
|
-
* **
|
|
348
|
-
*
|
|
349
|
-
*
|
|
350
|
-
*
|
|
351
|
-
*
|
|
352
|
-
*
|
|
406
|
+
* **Resolves once the logoff is WRITTEN, not answered.** The logoff's bytes
|
|
407
|
+
* handed to the socket (over RFC: the close call returned) is the point a
|
|
408
|
+
* caller needs: SAP queues a logoff behind a request still running in the
|
|
409
|
+
* same stateful ABAP session and answers it only after — ~96 s in one
|
|
410
|
+
* measurement, never in another — yet once it has arrived the ENQUEUE locks
|
|
411
|
+
* go with it, even if the client is killed while waiting for the answer
|
|
412
|
+
* (measured on an on-premise system, 2026-10-12; checked against 16.0.0 on 2026-10-13 with
|
|
413
|
+
* the process exiting right after this resolved: the lock was free ~70 s
|
|
414
|
+
* later, long before the idle timeout). A connection dropped WITHOUT a
|
|
415
|
+
* logoff keeps its locks. So a caller about to exit awaits this, and one
|
|
416
|
+
* that needs the answer awaits `flushGoodbye()` as well. The answer itself
|
|
417
|
+
* is not waited for here, and nothing bounds the write with a timer of its
|
|
418
|
+
* own: a server that never takes the bytes holds this until the socket
|
|
419
|
+
* fails, and that failure is logged, not thrown. Finishing chains and
|
|
420
|
+
* releasing locks stay the caller's to do BEFORE calling.
|
|
421
|
+
*
|
|
422
|
+
* **Called once per `connect()`.** A second call — while the first is still
|
|
423
|
+
* writing, or after it — rejects with `ADT_SESSION_ERROR.NOT_CONNECTED`; the
|
|
424
|
+
* first call is unaffected. A goodbye a failed `connect()` dispatched — to
|
|
425
|
+
* the HTTP security session a connect failing after authentication leaves
|
|
426
|
+
* alive — is written before that
|
|
427
|
+
* `connect()` rejects, so a caller's `disconnect()` in its `finally` is the
|
|
428
|
+
* first of that life and has nothing of it to wait for.
|
|
353
429
|
*
|
|
354
430
|
* **Requests already in flight are not waited for, and the logoff ends the
|
|
355
431
|
* session they are running on** — so they will start failing against a
|
|
@@ -358,55 +434,80 @@ class AbstractAbapConnection {
|
|
|
358
434
|
* dropped the cookie. Generation fencing (see `SessionLifecycle.isCurrent`)
|
|
359
435
|
* keeps their results from reaching this connection either way.
|
|
360
436
|
*
|
|
361
|
-
* **
|
|
362
|
-
* does not act on the answer — whether and when the session is
|
|
363
|
-
* server's affair
|
|
364
|
-
*
|
|
365
|
-
*
|
|
437
|
+
* **The answer is not waited for.** This TELLS the server the session is
|
|
438
|
+
* finished and does not act on the answer — whether and when the session is
|
|
439
|
+
* freed is the server's affair, and with a request still running in it SAP
|
|
440
|
+
* answers the logoff only once that request is done. Waiting for a reply
|
|
441
|
+
* nobody reads would make a teardown as long as the longest request; the
|
|
442
|
+
* write is what a caller needs, and `flushGoodbye()` is the wait for the
|
|
443
|
+
* answer.
|
|
366
444
|
*/
|
|
367
445
|
async disconnect() {
|
|
368
|
-
//
|
|
369
|
-
//
|
|
370
|
-
//
|
|
446
|
+
// Called once. A second call — while the first is writing its logoff, or
|
|
447
|
+
// after — rejects, in fixed words: it has nothing to end and nothing to
|
|
448
|
+
// wait for, and resolving would tell a caller about to exit that a logoff
|
|
449
|
+
// it never waited for has left. The first call is unaffected.
|
|
450
|
+
if (this.disconnectCalled) {
|
|
451
|
+
throw (0, SessionLifecycle_js_1.sessionError)(interfaces_adt_connection_1.ADT_SESSION_ERROR.NOT_CONNECTED, 'disconnect() was already called for this connection; call it once per instance');
|
|
452
|
+
}
|
|
453
|
+
this.disconnectCalled = true;
|
|
454
|
+
// Beyond that, nothing here throws. This method's place is a `finally` — a
|
|
455
|
+
// connection that was connected must be disconnected — and an exception
|
|
456
|
+
// raised there replaces the error that sent the caller into it.
|
|
457
|
+
let written = Promise.resolve();
|
|
371
458
|
// Synchronous, at the call: admission shuts and the generation moves before
|
|
372
459
|
// anything is queued, so a caller who has asked to disconnect cannot have
|
|
373
460
|
// requests still going through while this waits its turn.
|
|
374
461
|
this.lifecycle.beginTeardown({ origin: 'caller', sessionLost: false });
|
|
375
462
|
await this.lifecycle.transition('disconnect', async () => {
|
|
376
|
-
//
|
|
377
|
-
// design — a server that never answers
|
|
378
|
-
//
|
|
379
|
-
// awaiting an unbounded request is precisely what would make every
|
|
380
|
-
// teardown unbounded.
|
|
463
|
+
// Dispatched here; awaited below only until it is WRITTEN. The goodbye
|
|
464
|
+
// carries no request timeout by design — a server that never answers
|
|
465
|
+
// must not hold a teardown open — and its answer is not waited for.
|
|
381
466
|
//
|
|
382
467
|
// The context is taken now, while the session is still true: the clear
|
|
383
468
|
// below runs while the close is suspended on its first await.
|
|
384
469
|
const context = this.sessionContext();
|
|
385
470
|
// Kept, not dropped: `flushGoodbye()` is how a caller who is about to
|
|
386
|
-
//
|
|
471
|
+
// open a new connection waits for it. Rejections are absorbed here so that holding
|
|
387
472
|
// the promise never turns a failed goodbye into an unhandled rejection —
|
|
388
473
|
// the transport already logs what went wrong.
|
|
389
|
-
|
|
474
|
+
// The terminal teardown: the logoff (when the wire holds a session),
|
|
475
|
+
// the instance spent, and — merged with any logoff the connection
|
|
476
|
+
// already sent on its own — the write the caller waits for and the
|
|
477
|
+
// answer `flushGoodbye()` does.
|
|
478
|
+
written = this.endSession(context).written;
|
|
390
479
|
this.clearSessionState();
|
|
391
480
|
this.lifecycle.markDisconnected();
|
|
392
481
|
});
|
|
482
|
+
// The point this resolves at: the logoff written. Awaited after the
|
|
483
|
+
// transition, so the transition queue — a later connect() — is not held
|
|
484
|
+
// by the write.
|
|
485
|
+
await written;
|
|
393
486
|
}
|
|
394
487
|
/**
|
|
395
|
-
* Wait for the last dispatched goodbye
|
|
488
|
+
* Wait for SAP's answer to the last dispatched goodbye.
|
|
396
489
|
*
|
|
397
490
|
* For the caller who ends a session and immediately opens another. Without
|
|
398
491
|
* it the two overlap and the server keeps both; with it the previous session
|
|
399
492
|
* is gone — or known not to be going — before the next one is asked for.
|
|
400
493
|
*
|
|
401
|
-
*
|
|
402
|
-
*
|
|
403
|
-
*
|
|
404
|
-
* "the goodbye has not
|
|
405
|
-
*
|
|
494
|
+
* **The bound is the caller's, and optional.** Without `timeoutMs` this waits
|
|
495
|
+
* for the answer as long as it takes, and creates no timer: a timeout of the
|
|
496
|
+
* library's own choosing is not one. With it, this waits at most that long
|
|
497
|
+
* and then returns rather than throwing — "the goodbye has not answered yet"
|
|
498
|
+
* is not a failure of the caller's next step. SAP answers a logoff queued
|
|
499
|
+
* behind a running request only once that request is done (~96 s in one
|
|
500
|
+
* measurement, never in another), so a caller that cannot wait that long
|
|
501
|
+
* passes a budget.
|
|
406
502
|
*
|
|
407
|
-
* Safe to call at any time:
|
|
503
|
+
* Never rejects: a failed goodbye is absorbed. Safe to call at any time:
|
|
504
|
+
* with nothing dispatched it returns immediately.
|
|
408
505
|
*/
|
|
409
|
-
async flushGoodbye(timeoutMs
|
|
506
|
+
async flushGoodbye(timeoutMs) {
|
|
507
|
+
if (timeoutMs === undefined) {
|
|
508
|
+
await this.goodbye;
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
410
511
|
let timer;
|
|
411
512
|
try {
|
|
412
513
|
await Promise.race([
|
|
@@ -423,6 +524,29 @@ class AbstractAbapConnection {
|
|
|
423
524
|
clearTimeout(timer);
|
|
424
525
|
}
|
|
425
526
|
}
|
|
527
|
+
/**
|
|
528
|
+
* The one terminal teardown of this instance's session: `disconnect()`, and
|
|
529
|
+
* a failed `connect()` that had received a session.
|
|
530
|
+
*
|
|
531
|
+
* Dispatches the logoff — the wire snapshots its cookies synchronously,
|
|
532
|
+
* without `sap-contextid`, and sends it only when it holds a live
|
|
533
|
+
* `SAP_SESSIONID` — marks the instance spent, and keeps the logoff's write
|
|
534
|
+
* and answer, merged with any earlier one of this instance, for
|
|
535
|
+
* `disconnect()` and `flushGoodbye()`. The caller clears the cookies after.
|
|
536
|
+
*/
|
|
537
|
+
endSession(context) {
|
|
538
|
+
const goodbye = this.dispatchGoodbye(context);
|
|
539
|
+
this.spent = true;
|
|
540
|
+
const previous = this.ended;
|
|
541
|
+
this.ended = previous
|
|
542
|
+
? {
|
|
543
|
+
answered: Promise.all([previous.answered, goodbye.answered]).then(() => { }),
|
|
544
|
+
written: Promise.all([previous.written, goodbye.written]).then(() => { }),
|
|
545
|
+
}
|
|
546
|
+
: goodbye;
|
|
547
|
+
this.goodbye = this.ended.answered;
|
|
548
|
+
return this.ended;
|
|
549
|
+
}
|
|
426
550
|
/**
|
|
427
551
|
* Say goodbye to the session `context` describes, without waiting for it.
|
|
428
552
|
*
|
|
@@ -439,16 +563,25 @@ class AbstractAbapConnection {
|
|
|
439
563
|
* would send the goodbye without the session it is ending.
|
|
440
564
|
*/
|
|
441
565
|
dispatchGoodbye(context) {
|
|
566
|
+
let wroteIt;
|
|
567
|
+
const written = new Promise((resolve) => {
|
|
568
|
+
wroteIt = resolve;
|
|
569
|
+
});
|
|
442
570
|
let closing;
|
|
443
571
|
try {
|
|
444
|
-
closing = Promise.resolve(this.transport.close(context));
|
|
572
|
+
closing = Promise.resolve(this.transport.close(context, () => wroteIt()));
|
|
445
573
|
}
|
|
446
574
|
catch (error) {
|
|
447
575
|
closing = Promise.reject(error);
|
|
448
576
|
}
|
|
449
|
-
|
|
577
|
+
const answered = closing.catch((error) => {
|
|
450
578
|
this.logger?.debug(`Could not tell the server the session is finished: ${error instanceof Error ? error.message : String(error)}`);
|
|
451
579
|
});
|
|
580
|
+
// A close that is over is past its write: it sent nothing, it failed to
|
|
581
|
+
// send — logged above, and reported by `answered` settling — or its wire
|
|
582
|
+
// does not tell the write apart (RFC, a custom wire).
|
|
583
|
+
void answered.then(wroteIt);
|
|
584
|
+
return { answered, written };
|
|
452
585
|
}
|
|
453
586
|
isConnected() {
|
|
454
587
|
return this.lifecycle.connected;
|
|
@@ -511,6 +644,10 @@ class AbstractAbapConnection {
|
|
|
511
644
|
* than written twice — the two would drift apart, as they did once before.
|
|
512
645
|
*/
|
|
513
646
|
async establishAndCommit(baselineEpoch) {
|
|
647
|
+
// Every establishment attempt, the internal retry included: a spent
|
|
648
|
+
// instance opens no session.
|
|
649
|
+
if (this.spent)
|
|
650
|
+
throw spentError();
|
|
514
651
|
if (this.lifecycle.teardownEpoch !== baselineEpoch) {
|
|
515
652
|
throw (0, SessionLifecycle_js_1.sessionError)(interfaces_adt_connection_1.ADT_SESSION_ERROR.NOT_CONNECTED, 'Establishment abandoned: a teardown was requested for this connection');
|
|
516
653
|
}
|
|
@@ -561,15 +698,24 @@ class AbstractAbapConnection {
|
|
|
561
698
|
// finished with something we never had sends a request nobody asked for
|
|
562
699
|
// — into the middle of an authentication exchange, in the case that found
|
|
563
700
|
// this.
|
|
564
|
-
// Whether there is anything to say is the wire's to know
|
|
565
|
-
//
|
|
566
|
-
//
|
|
567
|
-
//
|
|
701
|
+
// Whether there is anything to say is the wire's to know, and both
|
|
702
|
+
// shipped wires answer by one rule: a logoff iff they hold a non-empty
|
|
703
|
+
// SAP_SESSIONID they received, whatever the status that brought it — a
|
|
704
|
+
// 404/403 at logon carries one (a live security session), a 401's
|
|
705
|
+
// debris cookie does not. Reached only when connect() as a whole failed:
|
|
706
|
+
// a 404 on core discovery is not that, its fallback runs in the same
|
|
707
|
+
// session. The connection asks, and each wire answers by doing nothing
|
|
708
|
+
// when it has nothing.
|
|
568
709
|
// Its failure is absorbed, a synchronous throw included: a wire's
|
|
569
710
|
// close() may reject — its context.authorize throws when the provider
|
|
570
711
|
// refuses — and nobody awaits this one. A throw that escaped here would
|
|
571
712
|
// replace the establishment error and skip the clearing below.
|
|
572
|
-
|
|
713
|
+
// A session was held — the wire's logoff gate — so this ends it, and
|
|
714
|
+
// the instance is spent; with none held, nothing is sent and connect()
|
|
715
|
+
// may be retried.
|
|
716
|
+
const goodbye = this.transport.sessionFingerprint().size > 0
|
|
717
|
+
? this.endSession(this.sessionContext())
|
|
718
|
+
: this.dispatchGoodbye(this.sessionContext());
|
|
573
719
|
this.invalidateSession();
|
|
574
720
|
// And the identity with it. The rejecting response was still observed, so
|
|
575
721
|
// its cookie was recorded as a session that had just been established —
|
|
@@ -577,10 +723,19 @@ class AbstractAbapConnection {
|
|
|
577
723
|
// isConnected() says false. Two answers to one question is worse than
|
|
578
724
|
// either.
|
|
579
725
|
this.lifecycle.markDisconnected();
|
|
580
|
-
//
|
|
581
|
-
// is
|
|
582
|
-
//
|
|
583
|
-
|
|
726
|
+
// The goodbye is the connector's own, so connect() rejects only once it
|
|
727
|
+
// is WRITTEN: a caller that exits on the error must not lose it. When
|
|
728
|
+
// the wire dispatches none (nothing to end), this resolves at once.
|
|
729
|
+
// A failure after authentication leaves a live HTTP security session
|
|
730
|
+
// (ICF, SAP_SESSIONID) on both wires: the cloud preflight opens one
|
|
731
|
+
// (BTP trial, 2026-10-12), and on-prem an authenticated error answer
|
|
732
|
+
// still sets a live SAP_SESSIONID (measured on an on-premise system, 2026-10-13: 404 and
|
|
733
|
+
// 403 each did; ABAP code: CL_HTTP_SERVER_NET->authentication creates
|
|
734
|
+
// it before any handler runs, send_response sets the cookie whatever
|
|
735
|
+
// the status). A failed connect() takes no ENQUEUE lock. The answer is not
|
|
736
|
+
// waited for, as a teardown does not wait for one; its failure was
|
|
737
|
+
// absorbed above, so nothing goes unhandled.
|
|
738
|
+
await goodbye.written;
|
|
584
739
|
throw error;
|
|
585
740
|
}
|
|
586
741
|
if (this.lifecycle.teardownEpoch !== baselineEpoch) {
|
|
@@ -631,12 +786,20 @@ class AbstractAbapConnection {
|
|
|
631
786
|
// preflight may have opened a session — on cloud it does — and this path
|
|
632
787
|
// is about to drop the cookies that are the only permission to close it.
|
|
633
788
|
// Refusing to connect must not leak the session the refusal is about.
|
|
634
|
-
//
|
|
635
|
-
//
|
|
636
|
-
|
|
789
|
+
// Written before connect() rejects, as above, when the wire sends one.
|
|
790
|
+
// A shipped wire sends none here: this path means the wire holds no
|
|
791
|
+
// SAP_SESSIONID, and without it the logoff would end nothing —
|
|
792
|
+
// CL_HTTP_SERVER_NET->if_http_server~logoff terminates the security
|
|
793
|
+
// session only when that cookie is present (an on-premise system's ABAP code). A custom
|
|
794
|
+
// wire's close() may still send one, and is waited for. Its answer is not waited for, and a close that throws or
|
|
795
|
+
// rejects is logged and absorbed.
|
|
796
|
+
// No session held — that is what this path means — so nothing was
|
|
797
|
+
// logged off, and the instance stays usable: connect() may be retried.
|
|
798
|
+
const goodbye = this.dispatchGoodbye(this.sessionContext());
|
|
637
799
|
this.invalidateSession();
|
|
638
800
|
this.lifecycle.forgetIdentity();
|
|
639
801
|
this.lifecycle.markDisconnected();
|
|
802
|
+
await goodbye.written;
|
|
640
803
|
throw (0, SessionLifecycle_js_1.sessionError)(interfaces_adt_connection_1.ADT_SESSION_ERROR.NOT_CONNECTED, `The server authenticated the request but opened no ABAP session: the ${this.transport.kind} wire reports it is on none, so there is nothing for a lock to be bound to. The wire itself is fine — the request was carried and answered — which is why this is not a transport failure and does not look like one. ` +
|
|
641
804
|
'Stateless reads would still work over it, but a lock, and any write under that lock, is dead the moment it is issued. ' +
|
|
642
805
|
'The usual cause is the system declining to open another session for this user: they are limited per user, shared with every other tool logged on as them, and released either by disconnecting or by their own idle timeout. ' +
|
|
@@ -644,6 +807,47 @@ class AbstractAbapConnection {
|
|
|
644
807
|
}
|
|
645
808
|
this.lifecycle.markConnected(this.sessionFingerprint());
|
|
646
809
|
}
|
|
810
|
+
/**
|
|
811
|
+
* Takes over the session `resumeFrom` names, and publishes it — the
|
|
812
|
+
* counterpart of `establishAndCommit` for an instance built on an existing
|
|
813
|
+
* session.
|
|
814
|
+
*
|
|
815
|
+
* No logon: the wire makes one request in that session without the
|
|
816
|
+
* credential (`IAdtTransport.resume`). Alive, the session is this
|
|
817
|
+
* instance's — its `SAP_SESSIONID`, and its `sap-contextid` when the caller
|
|
818
|
+
* kept one — and `disconnect()` logs it off as for any instance. Gone, or
|
|
819
|
+
* any answer that is not the session's: `connect()` rejects with SAP's
|
|
820
|
+
* answer as it is, nothing is logged off — nothing was opened — and the
|
|
821
|
+
* instance is spent.
|
|
822
|
+
*/
|
|
823
|
+
async resumeAndCommit(baselineEpoch) {
|
|
824
|
+
if (this.spent)
|
|
825
|
+
throw spentError();
|
|
826
|
+
if (this.lifecycle.teardownEpoch !== baselineEpoch) {
|
|
827
|
+
throw (0, SessionLifecycle_js_1.sessionError)(interfaces_adt_connection_1.ADT_SESSION_ERROR.NOT_CONNECTED, 'Establishment abandoned: a teardown was requested for this connection');
|
|
828
|
+
}
|
|
829
|
+
const session = this.resumeFrom;
|
|
830
|
+
this.resumeFrom = null;
|
|
831
|
+
this.lifecycle.forgetIdentity();
|
|
832
|
+
try {
|
|
833
|
+
await this.transport.resume(this.sessionContext(), session);
|
|
834
|
+
}
|
|
835
|
+
catch (error) {
|
|
836
|
+
// Nothing was opened, so nothing is logged off: the session was the
|
|
837
|
+
// caller's, and whatever it is now, it stays theirs. What the wire was
|
|
838
|
+
// seeded with goes, and the instance is spent.
|
|
839
|
+
this.spent = true;
|
|
840
|
+
this.invalidateSession();
|
|
841
|
+
this.lifecycle.markDisconnected();
|
|
842
|
+
throw error;
|
|
843
|
+
}
|
|
844
|
+
if (this.lifecycle.teardownEpoch !== baselineEpoch) {
|
|
845
|
+
// As in `establishAndCommit`: the queued teardown clears, and — the
|
|
846
|
+
// session now being this instance's — logs it off.
|
|
847
|
+
throw (0, SessionLifecycle_js_1.sessionError)(interfaces_adt_connection_1.ADT_SESSION_ERROR.NOT_CONNECTED, 'Establishment abandoned: a teardown was requested while it was in flight');
|
|
848
|
+
}
|
|
849
|
+
this.lifecycle.markConnected(this.sessionFingerprint());
|
|
850
|
+
}
|
|
647
851
|
/** The teardown epoch, for a recovery to capture before it starts. */
|
|
648
852
|
get teardownEpoch() {
|
|
649
853
|
return this.lifecycle.teardownEpoch;
|
|
@@ -682,6 +886,9 @@ class AbstractAbapConnection {
|
|
|
682
886
|
*/
|
|
683
887
|
raiseSessionLost(reason) {
|
|
684
888
|
this.logger?.warn(`Session lost: ${reason}`);
|
|
889
|
+
// The instance's one session is gone: it is spent, as after disconnect().
|
|
890
|
+
// Nothing else is decided here — the caller is told, and decides.
|
|
891
|
+
this.spent = true;
|
|
685
892
|
this.lifecycle.beginTeardown({ origin: 'internal', sessionLost: true });
|
|
686
893
|
void this.lifecycle.transition('cleanup', async () => {
|
|
687
894
|
this.clearSessionState();
|
|
@@ -798,6 +1005,18 @@ class AbstractAbapConnection {
|
|
|
798
1005
|
sessionFingerprint() {
|
|
799
1006
|
return this.transport.sessionFingerprint();
|
|
800
1007
|
}
|
|
1008
|
+
/**
|
|
1009
|
+
* The cookies of this instance's SAP session, for the caller to keep: the
|
|
1010
|
+
* string `createConnection({ …, session })` takes to build a later instance
|
|
1011
|
+
* on the same session. `null` before a session, after it ended, and over
|
|
1012
|
+
* RFC, whose session is a conversation no cookie names.
|
|
1013
|
+
*
|
|
1014
|
+
* It authenticates as the session's user for as long as the session lives:
|
|
1015
|
+
* keep it as a secret. Nothing here logs it.
|
|
1016
|
+
*/
|
|
1017
|
+
sessionCookies() {
|
|
1018
|
+
return this.transport.sessionCookies();
|
|
1019
|
+
}
|
|
801
1020
|
async getBaseUrl() {
|
|
802
1021
|
return this.baseUrl;
|
|
803
1022
|
}
|
|
@@ -856,11 +1075,8 @@ class AbstractAbapConnection {
|
|
|
856
1075
|
}
|
|
857
1076
|
}
|
|
858
1077
|
async performRequest(options, lease) {
|
|
859
|
-
const { url: endpoint, method, timeout, data, params, headers: customHeaders,
|
|
1078
|
+
const { url: endpoint, method, timeout, data, params, headers: customHeaders, } = options;
|
|
860
1079
|
const normalizedMethod = method.toUpperCase();
|
|
861
|
-
// Before the first send: a request its caller has already given up on
|
|
862
|
-
// asks the provider nothing and sends nothing.
|
|
863
|
-
(0, requestAbort_js_1.throwIfAborted)(signal);
|
|
864
1080
|
// The PATH, not an address. Which server it belongs in front of — or
|
|
865
1081
|
// whether it belongs in front of one at all — is the wire's to say.
|
|
866
1082
|
const requestUrl = endpoint;
|
|
@@ -875,7 +1091,7 @@ class AbstractAbapConnection {
|
|
|
875
1091
|
// exchange itself does not stop the request — the CSRF recovery below
|
|
876
1092
|
// handles a token that is missing — but a refused credential does.
|
|
877
1093
|
if (mutation && !this.transport.csrfToken()) {
|
|
878
|
-
await this.readyWireUpfront(renewal, lease
|
|
1094
|
+
await this.readyWireUpfront(renewal, lease);
|
|
879
1095
|
}
|
|
880
1096
|
// The request's own headers: everything but the credential's, which is
|
|
881
1097
|
// written fresh onto a copy of these for every attempt (`authorizedFrom`).
|
|
@@ -944,16 +1160,13 @@ class AbstractAbapConnection {
|
|
|
944
1160
|
}
|
|
945
1161
|
}
|
|
946
1162
|
}
|
|
947
|
-
//
|
|
948
|
-
//
|
|
949
|
-
//
|
|
950
|
-
//
|
|
951
|
-
// the lock stays held in that session.
|
|
952
|
-
//
|
|
953
|
-
|
|
954
|
-
const effectiveTimeout = this.inCriticalSection
|
|
955
|
-
? Math.max(timeout ?? 0, (0, timeouts_js_1.getCriticalSectionTimeout)())
|
|
956
|
-
: timeout;
|
|
1163
|
+
// The only deadline a request carries is the caller's own, and inside an
|
|
1164
|
+
// uninterruptible critical section (lock → modify → unlock) not even that:
|
|
1165
|
+
// a client deadline closes the socket, SAP then ends the request running
|
|
1166
|
+
// on it — a write in it lost — and the outcome is unknown, the unlock
|
|
1167
|
+
// handle lost while the lock stays held in that session. Decided once for
|
|
1168
|
+
// the whole request: the retry paths below reuse requestConfig.
|
|
1169
|
+
const callerTimeout = this.inCriticalSection ? undefined : timeout;
|
|
957
1170
|
const requestConfig = {
|
|
958
1171
|
method: normalizedMethod,
|
|
959
1172
|
url: requestUrl,
|
|
@@ -962,17 +1175,14 @@ class AbstractAbapConnection {
|
|
|
962
1175
|
// the request's content negotiation outrank it (`authorizedFrom`).
|
|
963
1176
|
// A provider that answers after the session went away answers for a
|
|
964
1177
|
// request that no longer has one.
|
|
965
|
-
headers: await this.authorizedFrom(requestHeaders
|
|
1178
|
+
headers: await this.authorizedFrom(requestHeaders).catch((error) => {
|
|
966
1179
|
throw this.staleOr(error, lease);
|
|
967
1180
|
}),
|
|
968
|
-
timeout:
|
|
1181
|
+
...(callerTimeout !== undefined ? { timeout: callerTimeout } : {}),
|
|
969
1182
|
// `unknown` on the caller's options, a record on the seam: the two
|
|
970
1183
|
// transports serialise a query differently and both need the pairs.
|
|
971
1184
|
params: params,
|
|
972
1185
|
stateful: this.sessionMode === 'stateful',
|
|
973
|
-
// For the waits the wire makes on this request's behalf — over RFC, the
|
|
974
|
-
// logon and the open of a conversation of its own.
|
|
975
|
-
...(signal ? { signal } : {}),
|
|
976
1186
|
};
|
|
977
1187
|
if (data !== undefined) {
|
|
978
1188
|
requestConfig.data = data;
|
|
@@ -993,17 +1203,14 @@ class AbstractAbapConnection {
|
|
|
993
1203
|
return response;
|
|
994
1204
|
}
|
|
995
1205
|
catch (error) {
|
|
996
|
-
// The wire ended one of its waits for this request: nothing to recover.
|
|
997
|
-
if ((0, requestAbort_js_1.isRequestAborted)(error))
|
|
998
|
-
throw error;
|
|
999
1206
|
let survived;
|
|
1000
1207
|
try {
|
|
1001
|
-
return await this.recoverOnWire(error, requestConfig, requestHeaders, lease
|
|
1208
|
+
return await this.recoverOnWire(error, requestConfig, requestHeaders, lease);
|
|
1002
1209
|
}
|
|
1003
1210
|
catch (failure) {
|
|
1004
1211
|
survived = failure;
|
|
1005
1212
|
}
|
|
1006
|
-
return await this.answerCredentialFailure(survived, requestConfig, requestHeaders, lease, renewal
|
|
1213
|
+
return await this.answerCredentialFailure(survived, requestConfig, requestHeaders, lease, renewal);
|
|
1007
1214
|
}
|
|
1008
1215
|
}
|
|
1009
1216
|
/**
|
|
@@ -1019,10 +1226,6 @@ class AbstractAbapConnection {
|
|
|
1019
1226
|
if (!this.lifecycle.isCurrent(lease)) {
|
|
1020
1227
|
throw (0, SessionLifecycle_js_1.sessionError)(interfaces_adt_connection_1.ADT_SESSION_ERROR.NOT_CONNECTED, 'The session this request was admitted to is gone; nothing was sent');
|
|
1021
1228
|
}
|
|
1022
|
-
// The last point before the wire, for the same reason: a raced wait that
|
|
1023
|
-
// answered still leaves turns before this one, and an abort in any of
|
|
1024
|
-
// them must stop the send. Covers the first send and every resend.
|
|
1025
|
-
(0, requestAbort_js_1.throwIfAborted)(requestConfig.signal);
|
|
1026
1229
|
const response = await this.transport.send(requestConfig);
|
|
1027
1230
|
this.observeResponse(response.headers, lease.generation);
|
|
1028
1231
|
return response;
|
|
@@ -1043,11 +1246,9 @@ class AbstractAbapConnection {
|
|
|
1043
1246
|
* and `Accept`: those are put back from `base` whichever way the provider
|
|
1044
1247
|
* spelled them — as the wire's own token fetch already does.
|
|
1045
1248
|
*/
|
|
1046
|
-
async authorizedFrom(base
|
|
1249
|
+
async authorizedFrom(base) {
|
|
1047
1250
|
const headers = { ...base };
|
|
1048
|
-
|
|
1049
|
-
// ends the wait at once, and the provider runs on for whoever shares it.
|
|
1050
|
-
await (0, requestAbort_js_1.untilAborted)(signal, this.credentialHeaders(headers));
|
|
1251
|
+
await this.credentialHeaders(headers);
|
|
1051
1252
|
for (const name of OUTRANK_THE_CREDENTIAL) {
|
|
1052
1253
|
const own = Object.keys(base).filter((key) => key.toLowerCase() === name);
|
|
1053
1254
|
if (own.length === 0)
|
|
@@ -1062,22 +1263,22 @@ class AbstractAbapConnection {
|
|
|
1062
1263
|
return headers;
|
|
1063
1264
|
}
|
|
1064
1265
|
/**
|
|
1065
|
-
* The wire's own recovery from a failed attempt: the
|
|
1066
|
-
*
|
|
1067
|
-
*
|
|
1266
|
+
* The wire's own recovery from a failed attempt: the one fault it can cure
|
|
1267
|
+
* in the same session — a CSRF token SAP refused (a 403 "CSRF token
|
|
1268
|
+
* validation failed", or a 401 to a mutation holding no token yet) — and
|
|
1269
|
+
* the verdicts it must not hide.
|
|
1068
1270
|
*
|
|
1069
|
-
* Runs before anything asks the credential
|
|
1070
|
-
*
|
|
1071
|
-
*
|
|
1072
|
-
*
|
|
1073
|
-
*
|
|
1074
|
-
* here, because curing it discards the session and the lock with it.
|
|
1271
|
+
* Runs before anything asks the credential: a stale CSRF token under Basic
|
|
1272
|
+
* must not become "the user or password was refused". What it cannot cure
|
|
1273
|
+
* it throws, and a credential failure among that — any other 401, a GET's
|
|
1274
|
+
* included — goes on to the provider. Nothing here resends a 401 as it is,
|
|
1275
|
+
* and nothing discards the session to retry in another.
|
|
1075
1276
|
*
|
|
1076
1277
|
* `requestHeaders` are the request's own, without the credential's: what
|
|
1077
1278
|
* the recovery adds — a new token, the jar's cookies — goes there, and each
|
|
1078
1279
|
* resend is authorized anew on top (`authorizedFrom`).
|
|
1079
1280
|
*/
|
|
1080
|
-
async recoverOnWire(error, requestConfig, requestHeaders, lease
|
|
1281
|
+
async recoverOnWire(error, requestConfig, requestHeaders, lease) {
|
|
1081
1282
|
const requestUrl = requestConfig.url;
|
|
1082
1283
|
const normalizedMethod = requestConfig.method;
|
|
1083
1284
|
// FENCE FIRST, before anything reads or writes shared state.
|
|
@@ -1085,9 +1286,9 @@ class AbstractAbapConnection {
|
|
|
1085
1286
|
// Fencing observeResponse() alone was not enough, and the gap was wide:
|
|
1086
1287
|
// everything below acts on this connection, not on the request. A late
|
|
1087
1288
|
// 400 "session not found" would call raiseSessionLost() and tear down the
|
|
1088
|
-
// healthy session established since; a late
|
|
1089
|
-
//
|
|
1090
|
-
//
|
|
1289
|
+
// healthy session established since; a late 403 would write a fresh CSRF
|
|
1290
|
+
// token and RETRY — replaying a mutation from a dead session inside the
|
|
1291
|
+
// live one.
|
|
1091
1292
|
//
|
|
1092
1293
|
// A stale request gets its error back and nothing else happens.
|
|
1093
1294
|
if (!this.lifecycle.isCurrent(lease)) {
|
|
@@ -1127,46 +1328,20 @@ class AbstractAbapConnection {
|
|
|
1127
1328
|
else {
|
|
1128
1329
|
this.logger?.error(errorDetails.message, errorDetails);
|
|
1129
1330
|
}
|
|
1130
|
-
//
|
|
1131
|
-
//
|
|
1132
|
-
//
|
|
1133
|
-
//
|
|
1134
|
-
//
|
|
1135
|
-
//
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
(0, IAdtTransport_js_1.refusalOf)(error)?.status === 401 &&
|
|
1140
|
-
this.getCsrfToken() !== null;
|
|
1141
|
-
// Not inside a critical section. Curing the login-form 401 discards the
|
|
1142
|
-
// session, and with it the lock the section exists to hold; a renewal
|
|
1143
|
-
// must keep it (goal decision 2). There the 401 goes to the provider,
|
|
1144
|
-
// whose Ok resends on the same session — and a session SAP really
|
|
1145
|
-
// replaced is reported as SESSION_REPLACED from that resend's refusal.
|
|
1146
|
-
// Keyed on the section, not the session mode: the write under a lock is
|
|
1147
|
-
// itself stateless.
|
|
1148
|
-
if (isCachedTokenStale && this.inCriticalSection)
|
|
1149
|
-
throw error;
|
|
1150
|
-
// Retry logic for CSRF token errors (403 with CSRF message) and the
|
|
1151
|
-
// login-form 401 pattern.
|
|
1152
|
-
if (this.shouldRetryCsrf(error, normalizedMethod) || isCachedTokenStale) {
|
|
1153
|
-
this.logger?.debug(isCachedTokenStale
|
|
1154
|
-
? 'Stale CSRF token / SAP session — invalidating and retrying'
|
|
1155
|
-
: 'CSRF token validation failed, fetching new token and retrying request', {
|
|
1331
|
+
// No session is discarded and reopened on this instance to retry a
|
|
1332
|
+
// request: one instance is one session. A 401 on a mutation that holds a
|
|
1333
|
+
// CSRF token (a login form) is not cured here; it goes to the provider
|
|
1334
|
+
// like any other 401, and SAP's answer reaches the caller.
|
|
1335
|
+
// A CSRF complaint (403 "CSRF token validation failed", or a 401 to a
|
|
1336
|
+
// mutation that holds no token yet): a token earned in the SAME session —
|
|
1337
|
+
// the cookies are kept — and one resend.
|
|
1338
|
+
if (this.shouldRetryCsrf(error, normalizedMethod)) {
|
|
1339
|
+
this.logger?.debug('CSRF token validation failed, fetching new token and retrying request', {
|
|
1156
1340
|
url: requestUrl,
|
|
1157
1341
|
method: normalizedMethod,
|
|
1158
1342
|
});
|
|
1159
|
-
// Before the CSRF recovery and its resend. The session's token and
|
|
1160
|
-
// cookies are every caller's: an aborted request neither discards them
|
|
1161
|
-
// nor earns new ones.
|
|
1162
|
-
(0, requestAbort_js_1.throwIfAborted)(signal);
|
|
1163
|
-
if (isCachedTokenStale) {
|
|
1164
|
-
this.invalidateSession();
|
|
1165
|
-
delete requestHeaders.Cookie;
|
|
1166
|
-
delete requestHeaders.cookie;
|
|
1167
|
-
}
|
|
1168
1343
|
try {
|
|
1169
|
-
this.setCsrfToken(await
|
|
1344
|
+
this.setCsrfToken(await this.fetchCsrfToken(requestUrl, 5, 2000, lease));
|
|
1170
1345
|
const refreshedToken = this.getCsrfToken();
|
|
1171
1346
|
if (refreshedToken) {
|
|
1172
1347
|
requestHeaders['x-csrf-token'] = refreshedToken;
|
|
@@ -1176,7 +1351,7 @@ class AbstractAbapConnection {
|
|
|
1176
1351
|
requestHeaders.Cookie = refreshedCookies;
|
|
1177
1352
|
}
|
|
1178
1353
|
// Re-authorized: a provider may have renewed since the first attempt.
|
|
1179
|
-
requestConfig.headers = await this.authorizedFrom(requestHeaders
|
|
1354
|
+
requestConfig.headers = await this.authorizedFrom(requestHeaders);
|
|
1180
1355
|
return await this.sendObserved(requestConfig, lease);
|
|
1181
1356
|
}
|
|
1182
1357
|
catch (retryError) {
|
|
@@ -1199,54 +1374,6 @@ class AbstractAbapConnection {
|
|
|
1199
1374
|
throw error;
|
|
1200
1375
|
}
|
|
1201
1376
|
}
|
|
1202
|
-
// A 401 on a GET where cookies have since arrived: the first request had
|
|
1203
|
-
// none, and the session they name is what the retry needs. Guarded below
|
|
1204
|
-
// on actually holding some, so a wire that issues no cookies — RFC —
|
|
1205
|
-
// never takes it.
|
|
1206
|
-
if ((0, IAdtTransport_js_1.refusalOf)(error)?.status === 401 && normalizedMethod === 'GET') {
|
|
1207
|
-
// Before the resend, and before the token fetch that would earn it.
|
|
1208
|
-
(0, requestAbort_js_1.throwIfAborted)(signal);
|
|
1209
|
-
// If we already have cookies from error response, retry immediately
|
|
1210
|
-
const afterError = this.transport.cookies();
|
|
1211
|
-
if (afterError) {
|
|
1212
|
-
this.logger?.debug(`401 on GET request, retrying with cookies from error response`);
|
|
1213
|
-
requestHeaders.Cookie = afterError;
|
|
1214
|
-
try {
|
|
1215
|
-
requestConfig.headers = await this.authorizedFrom(requestHeaders, signal);
|
|
1216
|
-
return await this.sendObserved(requestConfig, lease);
|
|
1217
|
-
}
|
|
1218
|
-
catch (resendError) {
|
|
1219
|
-
throw this.staleOr(resendError, lease);
|
|
1220
|
-
}
|
|
1221
|
-
}
|
|
1222
|
-
// If no cookies, try to get them via CSRF token fetch
|
|
1223
|
-
this.logger?.debug(`401 on GET request, attempting to get cookies via CSRF token fetch`);
|
|
1224
|
-
try {
|
|
1225
|
-
// Try to get CSRF token (this will also get cookies)
|
|
1226
|
-
this.setCsrfToken(await (0, requestAbort_js_1.untilAborted)(signal, this.fetchCsrfToken(requestUrl, 3, 1000, lease, signal)));
|
|
1227
|
-
const afterCsrf = this.transport.cookies();
|
|
1228
|
-
if (afterCsrf) {
|
|
1229
|
-
requestHeaders.Cookie = afterCsrf;
|
|
1230
|
-
this.logger?.debug(`Retrying GET request with cookies from CSRF fetch`);
|
|
1231
|
-
requestConfig.headers = await this.authorizedFrom(requestHeaders, signal);
|
|
1232
|
-
return await this.sendObserved(requestConfig, lease);
|
|
1233
|
-
}
|
|
1234
|
-
}
|
|
1235
|
-
catch (csrfError) {
|
|
1236
|
-
// As in path (a): a stale request gets the session verdict.
|
|
1237
|
-
if (!this.lifecycle.isCurrent(lease)) {
|
|
1238
|
-
throw this.staleVerdict(csrfError);
|
|
1239
|
-
}
|
|
1240
|
-
this.throwIfSessionDead(csrfError, lease);
|
|
1241
|
-
// Swallowed, unless it says more than the 401 that started this —
|
|
1242
|
-
// a session verdict, or the credential's failure.
|
|
1243
|
-
if (this.outranksOriginal(csrfError)) {
|
|
1244
|
-
throw csrfError;
|
|
1245
|
-
}
|
|
1246
|
-
this.logger?.debug(`Failed to get CSRF token for 401 retry: ${csrfError instanceof Error ? csrfError.message : String(csrfError)}`);
|
|
1247
|
-
// Fall through to throw original error
|
|
1248
|
-
}
|
|
1249
|
-
}
|
|
1250
1377
|
throw error;
|
|
1251
1378
|
}
|
|
1252
1379
|
/**
|
|
@@ -1261,7 +1388,7 @@ class AbstractAbapConnection {
|
|
|
1261
1388
|
* credential failure in either is the verdict. As in `recoverOnWire`,
|
|
1262
1389
|
* `requestHeaders` are the request's own, and the resend is authorized anew.
|
|
1263
1390
|
*/
|
|
1264
|
-
async answerCredentialFailure(failure, requestConfig, requestHeaders, lease, renewal
|
|
1391
|
+
async answerCredentialFailure(failure, requestConfig, requestHeaders, lease, renewal) {
|
|
1265
1392
|
// A resend inside the wire's recovery that met a dead session.
|
|
1266
1393
|
this.throwIfSessionDead(failure, lease);
|
|
1267
1394
|
const rejection = this.credentialRejection(failure);
|
|
@@ -1271,7 +1398,7 @@ class AbstractAbapConnection {
|
|
|
1271
1398
|
throw failure;
|
|
1272
1399
|
if (!this.lifecycle.isCurrent(lease))
|
|
1273
1400
|
throw this.staleVerdict(failure);
|
|
1274
|
-
await this.renewCredential(rejection, renewal
|
|
1401
|
+
await this.renewCredential(rejection, renewal).catch((error) => {
|
|
1275
1402
|
throw this.staleOr(error, lease);
|
|
1276
1403
|
});
|
|
1277
1404
|
if (!this.lifecycle.isCurrent(lease))
|
|
@@ -1279,7 +1406,7 @@ class AbstractAbapConnection {
|
|
|
1279
1406
|
try {
|
|
1280
1407
|
if (rejection.at === 'logon') {
|
|
1281
1408
|
this.transport.adoptCsrfToken(null);
|
|
1282
|
-
await this.ensureWireReady(lease
|
|
1409
|
+
await this.ensureWireReady(lease);
|
|
1283
1410
|
const token = this.transport.csrfToken();
|
|
1284
1411
|
if (token && ['POST', 'PUT', 'DELETE'].includes(requestConfig.method)) {
|
|
1285
1412
|
requestHeaders['x-csrf-token'] = token;
|
|
@@ -1288,7 +1415,7 @@ class AbstractAbapConnection {
|
|
|
1288
1415
|
if (cookies)
|
|
1289
1416
|
requestHeaders.Cookie = cookies;
|
|
1290
1417
|
}
|
|
1291
|
-
requestConfig.headers = await this.authorizedFrom(requestHeaders
|
|
1418
|
+
requestConfig.headers = await this.authorizedFrom(requestHeaders);
|
|
1292
1419
|
return await this.sendObserved(requestConfig, lease);
|
|
1293
1420
|
}
|
|
1294
1421
|
catch (again) {
|
|
@@ -1340,8 +1467,7 @@ class AbstractAbapConnection {
|
|
|
1340
1467
|
* NOT_CONNECTED, because it is about a session the request no longer holds.
|
|
1341
1468
|
*/
|
|
1342
1469
|
staleVerdict(error) {
|
|
1343
|
-
|
|
1344
|
-
if (this.isSessionVerdict(error) || (0, requestAbort_js_1.isRequestAborted)(error))
|
|
1470
|
+
if (this.isSessionVerdict(error))
|
|
1345
1471
|
return error;
|
|
1346
1472
|
return (0, SessionLifecycle_js_1.sessionError)(interfaces_adt_connection_1.ADT_SESSION_ERROR.NOT_CONNECTED, 'The session this request was admitted to is gone');
|
|
1347
1473
|
}
|
|
@@ -1357,18 +1483,12 @@ class AbstractAbapConnection {
|
|
|
1357
1483
|
* that throw. A refusal after the retry was spent is the verdict, and the
|
|
1358
1484
|
* provider is not asked again — asking would invite a second renewal.
|
|
1359
1485
|
*/
|
|
1360
|
-
async renewCredential(rejection, renewal
|
|
1361
|
-
/** The request's; `connect()` has none. */
|
|
1362
|
-
signal) {
|
|
1486
|
+
async renewCredential(rejection, renewal) {
|
|
1363
1487
|
if (renewal.spent) {
|
|
1364
1488
|
throw new authErrors_js_1.AuthRefusedError((0, authErrors_js_1.refusedAgain)(rejection.at), rejection.at, rejection.error);
|
|
1365
1489
|
}
|
|
1366
|
-
// Before rejected(): an aborted request asks the provider nothing.
|
|
1367
|
-
(0, requestAbort_js_1.throwIfAborted)(signal);
|
|
1368
1490
|
renewal.spent = true;
|
|
1369
|
-
|
|
1370
|
-
// refused; this request stops waiting for it at its abort.
|
|
1371
|
-
const answer = await (0, requestAbort_js_1.untilAborted)(signal, this.credentialRejected(rejection));
|
|
1491
|
+
const answer = await this.credentialRejected(rejection);
|
|
1372
1492
|
if (!answer.outcome.ok) {
|
|
1373
1493
|
throw new authErrors_js_1.AuthRefusedError(answer.outcome.refusal, rejection.at, 'thrown' in answer ? answer.thrown : rejection.error);
|
|
1374
1494
|
}
|
|
@@ -1396,7 +1516,6 @@ class AbstractAbapConnection {
|
|
|
1396
1516
|
*/
|
|
1397
1517
|
outranksOriginal(retryError) {
|
|
1398
1518
|
return (this.isSessionVerdict(retryError) ||
|
|
1399
|
-
(0, requestAbort_js_1.isRequestAborted)(retryError) ||
|
|
1400
1519
|
retryError instanceof authErrors_js_1.AuthRefusedError ||
|
|
1401
1520
|
this.credentialRejection(retryError) !== null);
|
|
1402
1521
|
}
|
|
@@ -1408,16 +1527,13 @@ class AbstractAbapConnection {
|
|
|
1408
1527
|
* fetch within the request's one retry — unless the session the request
|
|
1409
1528
|
* was admitted to is gone by then, when nothing more is sent.
|
|
1410
1529
|
*/
|
|
1411
|
-
async readyWireUpfront(renewal, lease
|
|
1530
|
+
async readyWireUpfront(renewal, lease) {
|
|
1412
1531
|
for (;;) {
|
|
1413
1532
|
try {
|
|
1414
|
-
await this.ensureWireReady(lease
|
|
1533
|
+
await this.ensureWireReady(lease);
|
|
1415
1534
|
return;
|
|
1416
1535
|
}
|
|
1417
1536
|
catch (error) {
|
|
1418
|
-
// The caller's abort is not a failed exchange to shrug off.
|
|
1419
|
-
if ((0, requestAbort_js_1.isRequestAborted)(error))
|
|
1420
|
-
throw error;
|
|
1421
1537
|
// The session the request was admitted to is gone: nothing more of it
|
|
1422
1538
|
// is sent, the provider is not asked, and what the caller gets is the
|
|
1423
1539
|
// session verdict — never the wire's error, nor the provider's, about
|
|
@@ -1431,7 +1547,7 @@ class AbstractAbapConnection {
|
|
|
1431
1547
|
return;
|
|
1432
1548
|
}
|
|
1433
1549
|
const rejection = this.credentialRejection(error);
|
|
1434
|
-
await this.renewCredential(rejection, renewal
|
|
1550
|
+
await this.renewCredential(rejection, renewal).catch((refused) => {
|
|
1435
1551
|
throw this.staleOr(refused, lease);
|
|
1436
1552
|
});
|
|
1437
1553
|
if (!this.lifecycle.isCurrent(lease)) {
|
|
@@ -1458,25 +1574,18 @@ class AbstractAbapConnection {
|
|
|
1458
1574
|
* wire's own ingest of it, and never stops the request being sent.
|
|
1459
1575
|
* Omitted during connect(), which has no lease.
|
|
1460
1576
|
*/
|
|
1461
|
-
fence
|
|
1462
|
-
/**
|
|
1463
|
-
* The request's signal: the exchange's own attempts stop once it aborts.
|
|
1464
|
-
* Omitted during connect(), whose exchange is nobody's single request.
|
|
1465
|
-
*/
|
|
1466
|
-
signal) {
|
|
1577
|
+
fence) {
|
|
1467
1578
|
// Dropped first, because this is only ever reached to REPLACE one: the
|
|
1468
1579
|
// establishment is idempotent and would hand back the very token the
|
|
1469
1580
|
// caller has just been told is stale.
|
|
1470
1581
|
this.transport.adoptCsrfToken(null);
|
|
1471
1582
|
await this.transport.establish({
|
|
1472
1583
|
...(typeof fence === 'number'
|
|
1473
|
-
? this.leasedContext(undefined, fence
|
|
1474
|
-
: this.leasedContext(fence
|
|
1584
|
+
? this.leasedContext(undefined, fence)
|
|
1585
|
+
: this.leasedContext(fence)),
|
|
1475
1586
|
retries: retryCount,
|
|
1476
1587
|
retryDelayMs: retryDelay,
|
|
1477
|
-
timeoutMs: (0, timeouts_js_1.getTimeout)('csrf'),
|
|
1478
1588
|
isFatal: (error) => this.endsTheExchange(error),
|
|
1479
|
-
...(signal ? { signal } : {}),
|
|
1480
1589
|
});
|
|
1481
1590
|
const token = this.transport.csrfToken();
|
|
1482
1591
|
if (!token)
|
|
@@ -1523,16 +1632,11 @@ class AbstractAbapConnection {
|
|
|
1523
1632
|
*/
|
|
1524
1633
|
async ensureWireReady(
|
|
1525
1634
|
/** The request's lease: fences what is sent and the response effects. */
|
|
1526
|
-
lease
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
// Raced: an exchange already sent is not recalled, and what it earns is
|
|
1530
|
-
// the wire's, for every caller — but this request stops waiting for it.
|
|
1531
|
-
await (0, requestAbort_js_1.untilAborted)(signal, this.transport.establish({
|
|
1532
|
-
...this.leasedContext(lease, lease.generation, signal),
|
|
1635
|
+
lease) {
|
|
1636
|
+
await this.transport.establish({
|
|
1637
|
+
...this.leasedContext(lease),
|
|
1533
1638
|
isFatal: (error) => this.endsTheExchange(error),
|
|
1534
|
-
|
|
1535
|
-
}));
|
|
1639
|
+
});
|
|
1536
1640
|
}
|
|
1537
1641
|
/**
|
|
1538
1642
|
* The context for a token exchange a request makes on its own behalf — its
|
|
@@ -1550,24 +1654,16 @@ class AbstractAbapConnection {
|
|
|
1550
1654
|
* what the connection does with an answer, and nothing else: the request is
|
|
1551
1655
|
* sent, and the wire still folds the answer into its jar.
|
|
1552
1656
|
*/
|
|
1553
|
-
leasedContext(lease, generation = lease?.generation
|
|
1554
|
-
/**
|
|
1555
|
-
* The request's signal. Each attempt of the exchange is authorized
|
|
1556
|
-
* raced against it, so an exchange whose request was aborted sends
|
|
1557
|
-
* nothing more: the abort ends the exchange (`endsTheExchange`).
|
|
1558
|
-
*/
|
|
1559
|
-
signal) {
|
|
1657
|
+
leasedContext(lease, generation = lease?.generation) {
|
|
1560
1658
|
return {
|
|
1561
1659
|
baseUrl: this.baseUrl,
|
|
1562
1660
|
authorize: async (headers) => {
|
|
1563
|
-
await
|
|
1661
|
+
await this.credentialHeaders(headers);
|
|
1564
1662
|
if (lease && !this.lifecycle.isCurrent(lease)) {
|
|
1565
1663
|
throw (0, SessionLifecycle_js_1.sessionError)(interfaces_adt_connection_1.ADT_SESSION_ERROR.NOT_CONNECTED, 'The session this request was admitted to is gone; nothing was sent');
|
|
1566
1664
|
}
|
|
1567
1665
|
},
|
|
1568
|
-
|
|
1569
|
-
// the request's wait like any other, and the exchange stops with it.
|
|
1570
|
-
logon: (target) => (0, requestAbort_js_1.untilAborted)(signal, this.logon(target)),
|
|
1666
|
+
logon: (target) => this.logon(target),
|
|
1571
1667
|
extraHeaders: { 'sap-adt-connection-id': this.sessionId ?? '' },
|
|
1572
1668
|
// An answer that arrives after the lease went stale belongs to a session
|
|
1573
1669
|
// that is gone. Refused with the session verdict, which every wire hands
|
|
@@ -1583,13 +1679,11 @@ class AbstractAbapConnection {
|
|
|
1583
1679
|
}
|
|
1584
1680
|
/**
|
|
1585
1681
|
* What stops a token exchange at once, past its retries: a session verdict,
|
|
1586
|
-
* the provider's refusal to authorize it — asking again would only ask
|
|
1587
|
-
* the provider again
|
|
1682
|
+
* or the provider's refusal to authorize it — asking again would only ask
|
|
1683
|
+
* the provider again.
|
|
1588
1684
|
*/
|
|
1589
1685
|
endsTheExchange(error) {
|
|
1590
|
-
return
|
|
1591
|
-
(0, requestAbort_js_1.isRequestAborted)(error) ||
|
|
1592
|
-
error instanceof authErrors_js_1.AuthRefusedError);
|
|
1686
|
+
return this.isSessionVerdict(error) || error instanceof authErrors_js_1.AuthRefusedError;
|
|
1593
1687
|
}
|
|
1594
1688
|
/**
|
|
1595
1689
|
* Clear SAP-side session state when SAP rejects the cached CSRF token + session
|