@mcp-abap-adt/connection 25.0.0 → 25.1.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.
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+ /**
3
+ * The one line a `noLog` request may write: the method, the path without its
4
+ * query, the integer status and the byte count — nothing of the request's
5
+ * query, headers or body, nothing of the answer's headers, status text or body.
6
+ *
7
+ * Shared by the connection and both wires, so a `noLog` line reads the same
8
+ * whichever of them wrote it.
9
+ */
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ exports.UNPARSABLE_URL = void 0;
12
+ exports.pathOf = pathOf;
13
+ exports.byteCount = byteCount;
14
+ exports.quietLine = quietLine;
15
+ /** What a quiet line names for a URL that does not parse: nothing of it. */
16
+ exports.UNPARSABLE_URL = '<unparsable url>';
17
+ /**
18
+ * The path of a request URL. A path (`/…`, not `//…`) is cut at its query or
19
+ * fragment; anything else is parsed as an absolute URL and gives its pathname
20
+ * alone — never its user, password, host, query or fragment, which the WHATWG
21
+ * parser separates. One with a scheme and no authority (`user:pass@host/x`)
22
+ * names nothing; one that does not parse (a bad port, `//host/x`) names the
23
+ * fixed placeholder, never any part of the input.
24
+ */
25
+ function pathOf(url) {
26
+ if (!url.startsWith('/') || url.startsWith('//')) {
27
+ let parsed;
28
+ try {
29
+ parsed = new URL(url);
30
+ }
31
+ catch {
32
+ return exports.UNPARSABLE_URL;
33
+ }
34
+ return parsed.host ? parsed.pathname : '';
35
+ }
36
+ let end = url.length;
37
+ const query = url.indexOf('?');
38
+ if (query !== -1)
39
+ end = query;
40
+ const fragment = url.indexOf('#');
41
+ if (fragment !== -1 && fragment < end)
42
+ end = fragment;
43
+ return escapedControls(url.slice(0, end));
44
+ }
45
+ /**
46
+ * Whether a code point can end or break a line: C0, DEL, C1 (U+0080–U+009F,
47
+ * NEL among them), and the line and paragraph separators.
48
+ */
49
+ function breaksLines(code) {
50
+ return (code < 0x20 ||
51
+ (code >= 0x7f && code <= 0x9f) ||
52
+ code === 0x2028 ||
53
+ code === 0x2029);
54
+ }
55
+ /**
56
+ * The text with every code point that can break a line written as a fixed
57
+ * escape — `\xNN` up to U+00FF, `\uNNNN` beyond: neither a path nor a method
58
+ * can split the quiet line into two, or forge a second one. Plain string code.
59
+ */
60
+ function escapedControls(text) {
61
+ let out = '';
62
+ for (const char of text) {
63
+ const code = char.codePointAt(0) ?? 0;
64
+ if (!breaksLines(code))
65
+ out += char;
66
+ else if (code <= 0xff)
67
+ out += `\\x${code.toString(16).padStart(2, '0')}`;
68
+ else
69
+ out += `\\u${code.toString(16).padStart(4, '0')}`;
70
+ }
71
+ return out;
72
+ }
73
+ /**
74
+ * The size of an answer's body in bytes. A body is the wire's, from outside the
75
+ * type system: a string or bytes as they are, anything else as it serialises;
76
+ * one that will not serialise counts as none.
77
+ */
78
+ function byteCount(data) {
79
+ if (data === undefined || data === null)
80
+ return 0;
81
+ if (typeof data === 'string')
82
+ return Buffer.byteLength(data, 'utf8');
83
+ if (ArrayBuffer.isView(data))
84
+ return data.byteLength;
85
+ if (data instanceof ArrayBuffer)
86
+ return data.byteLength;
87
+ try {
88
+ const text = JSON.stringify(data);
89
+ return text === undefined ? 0 : Buffer.byteLength(text, 'utf8');
90
+ }
91
+ catch {
92
+ return 0;
93
+ }
94
+ }
95
+ /**
96
+ * The quiet line: `METHOD /path → STATUS (N bytes)` for an answer, and
97
+ * `METHOD /path → no answer` for a request that got none.
98
+ */
99
+ function quietLine(rawMethod, url, answer) {
100
+ const path = pathOf(url);
101
+ // The caller's method, as written: escaped as the path is.
102
+ const method = escapedControls(rawMethod);
103
+ if (answer === undefined || !Number.isInteger(answer.status)) {
104
+ return { line: `${method} ${path} → no answer`, facts: { method, path } };
105
+ }
106
+ const bytes = byteCount(answer.data);
107
+ return {
108
+ line: `${method} ${path} → ${answer.status} (${bytes} bytes)`,
109
+ facts: { method, path, status: answer.status, bytes },
110
+ };
111
+ }
@@ -10,10 +10,23 @@
10
10
  import type { IAdtTransportRequest } from './IAdtTransport.js';
11
11
  /** The header that asks SAP for the stateful session over HTTP. */
12
12
  export declare const SESSION_TYPE_HEADER = "x-sap-adt-sessiontype";
13
+ /** The cookie that names a stateful ABAP context. */
14
+ export declare const CONTEXT_COOKIE = "sap-contextid";
13
15
  /**
14
16
  * Marked so by the connection, or — kept for callers that still write it
15
17
  * themselves — carrying the session header already. Taken on the request as
16
18
  * the caller made it, before a wire rewrites it.
19
+ *
20
+ * A request marked `stateless` is not stateful, whatever else it says: the
21
+ * caller's `stateless: true` wins over the flag and over its own header.
17
22
  */
18
23
  export declare function isStatefulRequest(request: IAdtTransportRequest): boolean;
24
+ /** Whether a header's name is the session header, in any letter case. */
25
+ export declare function isSessionTypeHeader(name: string): boolean;
26
+ /**
27
+ * A `Cookie` header's value without `sap-contextid`, or `undefined` when
28
+ * nothing is left. Plain string code: the pairs split at `;`, each read
29
+ * without its leading whitespace.
30
+ */
31
+ export declare function cookieWithoutContext(cookie: string): string | undefined;
19
32
  //# sourceMappingURL=statefulRequest.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"statefulRequest.d.ts","sourceRoot":"","sources":["../../src/connection/statefulRequest.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAE/D,mEAAmE;AACnE,eAAO,MAAM,mBAAmB,0BAA0B,CAAC;AAE3D;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAaxE"}
1
+ {"version":3,"file":"statefulRequest.d.ts","sourceRoot":"","sources":["../../src/connection/statefulRequest.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAE/D,mEAAmE;AACnE,eAAO,MAAM,mBAAmB,0BAA0B,CAAC;AAE3D,qDAAqD;AACrD,eAAO,MAAM,cAAc,kBAAkB,CAAC;AAE9C;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAcxE;AAED,yEAAyE;AACzE,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAEzD;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAOvE"}
@@ -1,15 +1,24 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SESSION_TYPE_HEADER = void 0;
3
+ exports.CONTEXT_COOKIE = exports.SESSION_TYPE_HEADER = void 0;
4
4
  exports.isStatefulRequest = isStatefulRequest;
5
+ exports.isSessionTypeHeader = isSessionTypeHeader;
6
+ exports.cookieWithoutContext = cookieWithoutContext;
5
7
  /** The header that asks SAP for the stateful session over HTTP. */
6
8
  exports.SESSION_TYPE_HEADER = 'x-sap-adt-sessiontype';
9
+ /** The cookie that names a stateful ABAP context. */
10
+ exports.CONTEXT_COOKIE = 'sap-contextid';
7
11
  /**
8
12
  * Marked so by the connection, or — kept for callers that still write it
9
13
  * themselves — carrying the session header already. Taken on the request as
10
14
  * the caller made it, before a wire rewrites it.
15
+ *
16
+ * A request marked `stateless` is not stateful, whatever else it says: the
17
+ * caller's `stateless: true` wins over the flag and over its own header.
11
18
  */
12
19
  function isStatefulRequest(request) {
20
+ if (request.stateless)
21
+ return false;
13
22
  if (request.stateful)
14
23
  return true;
15
24
  const headers = request.headers;
@@ -23,3 +32,20 @@ function isStatefulRequest(request) {
23
32
  }
24
33
  return false;
25
34
  }
35
+ /** Whether a header's name is the session header, in any letter case. */
36
+ function isSessionTypeHeader(name) {
37
+ return name.trim().toLowerCase() === exports.SESSION_TYPE_HEADER;
38
+ }
39
+ /**
40
+ * A `Cookie` header's value without `sap-contextid`, or `undefined` when
41
+ * nothing is left. Plain string code: the pairs split at `;`, each read
42
+ * without its leading whitespace.
43
+ */
44
+ function cookieWithoutContext(cookie) {
45
+ const kept = cookie
46
+ .split(';')
47
+ .map((pair) => pair.trimStart())
48
+ .filter((pair) => pair && pair.split('=')[0]?.trim() !== exports.CONTEXT_COOKIE)
49
+ .join('; ');
50
+ return kept || undefined;
51
+ }
@@ -0,0 +1,14 @@
1
+ import type { CreatedConnection } from './createConnection.js';
2
+ /**
3
+ * The syntax-check POST is a read. A 403 explicitly rejecting its CSRF token
4
+ * means SAP did not run it. Retry on a separate session only when that same
5
+ * response proved the original session gone.
6
+ */
7
+ export declare function withCheckRelogon<TConnection extends CreatedConnection>(connection: TConnection,
8
+ /**
9
+ * A new connection for the replay. `quiet`: the call is a `noLog` one, so
10
+ * the connection writes no line — its logon, the replay and its logoff
11
+ * included; the call's one quiet line is the original request's.
12
+ */
13
+ fresh: (quiet: boolean) => TConnection): TConnection;
14
+ //# sourceMappingURL=withCheckRelogon.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"withCheckRelogon.d.ts","sourceRoot":"","sources":["../../src/connection/withCheckRelogon.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAI/D;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,WAAW,SAAS,iBAAiB,EACpE,UAAU,EAAE,WAAW;AACvB;;;;GAIG;AACH,KAAK,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,WAAW,GACrC,WAAW,CA4Db"}
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.withCheckRelogon = withCheckRelogon;
4
+ const interfaces_adt_connection_1 = require("@mcp-abap-adt/interfaces-adt-connection");
5
+ const AbstractAbapConnection_js_1 = require("./AbstractAbapConnection.js");
6
+ const IAdtTransport_js_1 = require("./IAdtTransport.js");
7
+ const statefulRequest_js_1 = require("./statefulRequest.js");
8
+ /**
9
+ * The syntax-check POST is a read. A 403 explicitly rejecting its CSRF token
10
+ * means SAP did not run it. Retry on a separate session only when that same
11
+ * response proved the original session gone.
12
+ */
13
+ function withCheckRelogon(connection,
14
+ /**
15
+ * A new connection for the replay. `quiet`: the call is a `noLog` one, so
16
+ * the connection writes no line — its logon, the replay and its logoff
17
+ * included; the call's one quiet line is the original request's.
18
+ */
19
+ fresh) {
20
+ const send = connection.makeAdtRequest.bind(connection);
21
+ const disconnect = connection.disconnect.bind(connection);
22
+ const pending = new Set();
23
+ let closing = false;
24
+ connection.disconnect = async () => {
25
+ closing = true;
26
+ await Promise.all([disconnect(), ...Array.from(pending, (stop) => stop())]);
27
+ };
28
+ connection.makeAdtRequest = async (request) => {
29
+ const eligible =
30
+ // `noResend`: the refusal is the caller's answer, never replayed.
31
+ !request.noResend &&
32
+ request.method.toUpperCase() === 'POST' &&
33
+ request.url.split('?', 1)[0] === '/sap/bc/adt/checkruns' &&
34
+ typeof request.data === 'string' &&
35
+ connection.getSessionMode() === 'stateless' &&
36
+ !connection.isInCriticalSection() &&
37
+ !(0, statefulRequest_js_1.isStatefulRequest)({
38
+ url: request.url,
39
+ method: request.method,
40
+ headers: request.headers,
41
+ });
42
+ try {
43
+ return await send(request);
44
+ }
45
+ catch (error) {
46
+ if (!eligible || closing || !(0, AbstractAbapConnection_js_1.carriesSessionLost)(error))
47
+ throw error;
48
+ const refusal = (0, IAdtTransport_js_1.refusalOf)(error);
49
+ if (refusal?.status !== 403 ||
50
+ typeof refusal.data !== 'string' ||
51
+ !refusal.data.includes('CSRF token validation failed'))
52
+ throw error;
53
+ const replacement = fresh(request.noLog === true);
54
+ let stopped;
55
+ const stop = () => (stopped ??= replacement.disconnect());
56
+ pending.add(stop);
57
+ try {
58
+ await replacement.connect();
59
+ if (closing)
60
+ throw error;
61
+ const result = await replacement.makeAdtRequest(request);
62
+ return { ...result, sessionLost: interfaces_adt_connection_1.ADT_SESSION_ERROR.SESSION_REPLACED };
63
+ }
64
+ finally {
65
+ // Disconnect waits until the logoff is written, as for any session.
66
+ // A caller closing during recovery shares this same disconnect.
67
+ try {
68
+ await stop();
69
+ }
70
+ finally {
71
+ pending.delete(stop);
72
+ }
73
+ }
74
+ }
75
+ };
76
+ return connection;
77
+ }
@@ -169,7 +169,11 @@ Every ADT request issued through `makeAdtRequest` automatically:
169
169
  3. Updates stored cookies if SAP returns `set-cookie`.
170
170
  4. Retries once when CSRF token is invalid/expired.
171
171
 
172
- This logic is transparent to callers (Builders, handlers, CLI scripts).
172
+ This logic is transparent to callers (Builders, handlers, CLI scripts). A
173
+ request sent with `noResend: true` (25.1.0) skips step 4 and every other
174
+ resend: its token fetch (step 1) goes once, and the first failure — of the
175
+ fetch or of the request — is the caller's answer. See the README's
176
+ [Request options](../README.md#request-options-noresend-nolog-stateless).
173
177
 
174
178
  A 401 that survives the wire's own CSRF recovery (in the same session) is put to the provider (`rejected`). On Ok the
175
179
  request is sent once more, always under the same lease and generation as the attempt it repeats,
package/docs/USAGE.md CHANGED
@@ -710,9 +710,8 @@ on whether it is stateful (since 23.0.0):
710
710
  rejects as itself — with `sessionLost: 'ADT_SESSION_REPLACED'` on that
711
711
  response or on that error (`IAdtWireResponse`, `IAbapConnectionError`,
712
712
  `@mcp-abap-adt/interfaces-adt-connection`, since 4.2.0). Only the request whose own
713
- answer showed the replacement carries it. Nothing is sent again for it: no
714
- CSRF retry, no credential renewal. The session is lost before the answer
715
- reaches you.
713
+ answer showed the replacement carries it. The original instance is spent
714
+ before the answer reaches you.
716
715
 
717
716
  The connection's own exchanges inside a request (a CSRF fetch, a logon) have no
718
717
  answer of yours to keep: a replacement seen on one is `ADT_SESSION_REPLACED`.
@@ -730,11 +729,11 @@ nothing here suggests a working session to continue on — the one we had is
730
729
  confirmed dead, and re-establishing silently is what let the old connector
731
730
  carry on over a session the caller never opened.
732
731
 
733
- In both cases the connector does **not** retry the request internally. Retrying
734
- blindly is what produced further orphaned locks in the field. That decision is
735
- yours. (The one resend the HTTP wire makes is of a request that never reached
736
- an answer on a dead pooled socket — see "Kept-alive connections, and the one
737
- resend" under the transports; it never resends a request the server answered.)
732
+ The factory can complete a [stateless syntax check](#syntax-check-recovery-on-http)
733
+ after SAP explicitly rejects its CSRF token and the response confirms session
734
+ replacement. Recovery for other requests is the caller's decision. At the HTTP
735
+ wire level, the pooled-socket resend still applies only when no answer byte
736
+ arrived (see "Kept-alive connections, and the one resend" under the transports).
738
737
 
739
738
  **A lost session spends the instance** (since 16.0.0), as `disconnect()` does.
740
739
  After either of these — a stateless answer marked `sessionLost` included —
@@ -838,6 +837,44 @@ await connection.makeAdtRequest({ method: 'POST', url: '...' , timeout: 0 });
838
837
  connection.setSessionType('stateless');
839
838
  ```
840
839
 
840
+ ### Syntax check recovery on HTTP
841
+
842
+ On a modern HTTP connection built by `createConnection`, an ADT syntax check
843
+ (`POST /sap/bc/adt/checkruns` with an XML string body) is retried once during
844
+ the same call if SAP rejects the CSRF token and the response also shows that
845
+ the original session was replaced. The retry logs on with the same destination
846
+ and provider, then sends the check on a temporary session. A failure to log on
847
+ is the call's error. The temporary session is logged off and the original
848
+ connection remains spent. A successful retry still carries
849
+ `sessionLost: ADT_SESSION_REPLACED` so the caller can discard state tied to the
850
+ original session.
851
+
852
+ This applies to stateless checks outside a critical section. Stateful requests
853
+ and operations under an existing lock retain their session-bound behavior. A
854
+ check sent with `noResend: true` is not retried: the refusal is its answer.
855
+
856
+ ### Request options: `noResend`, `noLog`, `stateless`
857
+
858
+ Three optional flags of `IAbapRequestOptions` (25.1.0) change one request and
859
+ nothing else; a request without them behaves as before. In short:
860
+
861
+ - `noResend: true` — sent once: no CSRF recovery, no resend after the
862
+ credential's renewal (the provider is not asked), no resend on a dead pooled
863
+ socket, no syntax-check replay; the CSRF token fetch before a mutation still
864
+ happens, once — no fallback endpoint, a token only from a `2xx` (its failure
865
+ is the answer and the request is not sent); a
866
+ `3xx` from SAP's endpoint is the answer, not followed.
867
+ - `noLog: true` — the connection writes exactly one line about the request,
868
+ `METHOD /path → STATUS (N bytes)` (or `→ no answer`), the path without its
869
+ query, host or user; the wires write none of their own, two fixed-word lines
870
+ aside (`CSRF token obtained`, and the cloud wire's
871
+ `The server published no logoff resource; sending the platform logoff`); the thrown error keeps SAP's answer.
872
+ - `stateless: true` — stateless on HTTP and RFC whatever the session mode, over
873
+ the caller's own `x-sap-adt-sessiontype` header in any letter case; the header
874
+ and the `sap-contextid` cookie are stripped; the session mode stays.
875
+
876
+ The whole contract, and why `noResend` exists: [README — Request options](../README.md#request-options-noresend-nolog-stateless).
877
+
841
878
  ### Starting Over
842
879
 
843
880
  There is no local-only reset. Discarding the cookie does not end the ABAP
@@ -1158,7 +1195,9 @@ carries it; the wire never reads `config.username` or `config.password`.
1158
1195
  the provider.
1159
1196
 
1160
1197
  Nothing else is sent again: a failed request — the CSRF token fetch included
1161
- (21.0.0) — is its own error.
1198
+ (21.0.0) — is its own error. A request with `noResend: true` (25.1.0) gets none
1199
+ of these three resends, and follows no redirect: see
1200
+ [Request options](#request-options-noresend-nolog-stateless).
1162
1201
  - RFC takes logon parameters (a password, or SNC settings) on every conversation
1163
1202
  it opens, the session's own and each per-call one. RFC refuses TLS material,
1164
1203
  and a provider that writes no logon parameters (a bearer token) cannot log on
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mcp-abap-adt/connection",
3
- "version": "25.0.0",
3
+ "version": "25.1.0",
4
4
  "description": "ABAP connection layer for MCP ABAP ADT server",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -52,7 +52,7 @@
52
52
  },
53
53
  "dependencies": {
54
54
  "@mcp-abap-adt/auth-errors": "^2.2.1",
55
- "@mcp-abap-adt/interfaces-adt-connection": "^5.0.0",
55
+ "@mcp-abap-adt/interfaces-adt-connection": "^5.2.0",
56
56
  "@mcp-abap-adt/interfaces-auth": "^7.5.0",
57
57
  "@mcp-abap-adt/interfaces-auth-sap": "^4.0.0",
58
58
  "@mcp-abap-adt/interfaces-network": "^2.0.1",