@elysra/tungsten-runtime 0.0.0-bootstrap.0 → 0.0.0-stage
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/README.md +3 -0
- package/package.json +6 -55
- package/LICENSE +0 -55
- package/dist/auth.d.ts +0 -50
- package/dist/auth.js +0 -264
- package/dist/classify.d.ts +0 -64
- package/dist/classify.js +0 -301
- package/dist/client.d.ts +0 -49
- package/dist/client.js +0 -2309
- package/dist/confirm.d.ts +0 -7
- package/dist/confirm.js +0 -32
- package/dist/define.d.ts +0 -13
- package/dist/define.js +0 -12
- package/dist/envelope.d.ts +0 -24
- package/dist/envelope.js +0 -152
- package/dist/errors.d.ts +0 -37
- package/dist/errors.js +0 -75
- package/dist/expr.d.ts +0 -24
- package/dist/expr.js +0 -198
- package/dist/idempotency.d.ts +0 -27
- package/dist/idempotency.js +0 -56
- package/dist/index.d.ts +0 -18
- package/dist/index.js +0 -19
- package/dist/node.d.ts +0 -17
- package/dist/node.js +0 -98
- package/dist/oauth.d.ts +0 -124
- package/dist/oauth.js +0 -367
- package/dist/serialize.d.ts +0 -55
- package/dist/serialize.js +0 -323
- package/dist/sse.d.ts +0 -42
- package/dist/sse.js +0 -166
- package/dist/streams.d.ts +0 -65
- package/dist/streams.js +0 -96
- package/dist/transport.d.ts +0 -88
- package/dist/transport.js +0 -245
- package/dist/types.d.ts +0 -667
- package/dist/types.js +0 -1
- package/dist/util.d.ts +0 -73
- package/dist/util.js +0 -317
- package/dist/version.d.ts +0 -3
- package/dist/version.js +0 -4
package/README.md
ADDED
package/package.json
CHANGED
|
@@ -1,55 +1,6 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@elysra/tungsten-runtime",
|
|
3
|
-
"version": "0.0.0-
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
"sdk",
|
|
8
|
-
"tungsten",
|
|
9
|
-
"agents",
|
|
10
|
-
"runtime",
|
|
11
|
-
"typescript"
|
|
12
|
-
],
|
|
13
|
-
"homepage": "https://github.com/pboachie/tungsten",
|
|
14
|
-
"repository": {
|
|
15
|
-
"type": "git",
|
|
16
|
-
"url": "git+https://github.com/pboachie/tungsten.git",
|
|
17
|
-
"directory": "runtimes/ts"
|
|
18
|
-
},
|
|
19
|
-
"bugs": {
|
|
20
|
-
"url": "https://github.com/pboachie/tungsten/issues"
|
|
21
|
-
},
|
|
22
|
-
"author": "Prince Boachie",
|
|
23
|
-
"license": "Apache-2.0",
|
|
24
|
-
"publishConfig": {
|
|
25
|
-
"access": "public"
|
|
26
|
-
},
|
|
27
|
-
"type": "module",
|
|
28
|
-
"sideEffects": false,
|
|
29
|
-
"main": "./dist/index.js",
|
|
30
|
-
"types": "./dist/index.d.ts",
|
|
31
|
-
"exports": {
|
|
32
|
-
".": {
|
|
33
|
-
"types": "./dist/index.d.ts",
|
|
34
|
-
"import": "./dist/index.js"
|
|
35
|
-
},
|
|
36
|
-
"./node": {
|
|
37
|
-
"types": "./dist/node.d.ts",
|
|
38
|
-
"import": "./dist/node.js"
|
|
39
|
-
}
|
|
40
|
-
},
|
|
41
|
-
"files": [
|
|
42
|
-
"dist",
|
|
43
|
-
"LICENSE"
|
|
44
|
-
],
|
|
45
|
-
"engines": {
|
|
46
|
-
"node": ">=22"
|
|
47
|
-
},
|
|
48
|
-
"scripts": {
|
|
49
|
-
"build": "tsc -p tsconfig.json"
|
|
50
|
-
},
|
|
51
|
-
"devDependencies": {
|
|
52
|
-
"@types/node": "26.6.4",
|
|
53
|
-
"typescript": "7.0.2"
|
|
54
|
-
}
|
|
55
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@elysra/tungsten-runtime",
|
|
3
|
+
"version": "0.0.0-stage",
|
|
4
|
+
"stub": true,
|
|
5
|
+
"description": "Temporary package placeholder for staged publishing"
|
|
6
|
+
}
|
package/LICENSE
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
Apache License
|
|
2
|
-
|
|
3
|
-
Version 2.0, January 2004
|
|
4
|
-
|
|
5
|
-
http://www.apache.org/licenses/
|
|
6
|
-
|
|
7
|
-
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
8
|
-
|
|
9
|
-
1. Definitions.
|
|
10
|
-
|
|
11
|
-
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
-
|
|
13
|
-
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
|
14
|
-
|
|
15
|
-
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
|
16
|
-
|
|
17
|
-
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
|
18
|
-
|
|
19
|
-
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
|
20
|
-
|
|
21
|
-
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
|
22
|
-
|
|
23
|
-
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
|
24
|
-
|
|
25
|
-
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
|
26
|
-
|
|
27
|
-
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
|
28
|
-
|
|
29
|
-
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
|
30
|
-
|
|
31
|
-
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
|
32
|
-
|
|
33
|
-
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
|
34
|
-
|
|
35
|
-
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
|
36
|
-
|
|
37
|
-
You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
|
38
|
-
|
|
39
|
-
You must cause any modified files to carry prominent notices stating that You changed the files; and
|
|
40
|
-
|
|
41
|
-
You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
|
42
|
-
|
|
43
|
-
If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
|
44
|
-
|
|
45
|
-
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
|
46
|
-
|
|
47
|
-
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
|
48
|
-
|
|
49
|
-
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
|
50
|
-
|
|
51
|
-
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
|
52
|
-
|
|
53
|
-
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
|
54
|
-
|
|
55
|
-
END OF TERMS AND CONDITIONS
|
package/dist/auth.d.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Auth resolution (planning/06 "Auth profiles"): an operation's security is
|
|
3
|
-
* an OR of AND-sets of scheme names; the first alternative that
|
|
4
|
-
* `ClientOptions.auth` fully satisfies is applied. A composite profile
|
|
5
|
-
* satisfies the scheme names in its `satisfies` list when every part it
|
|
6
|
-
* needs for this request is configured.
|
|
7
|
-
*/
|
|
8
|
-
import type { ApiDescriptor, AuthConfig, AuthSchemeDescriptor, HttpMethod, OperationDescriptor } from "./types.js";
|
|
9
|
-
type OAuth2 = Extract<AuthSchemeDescriptor, {
|
|
10
|
-
kind: "oauth2";
|
|
11
|
-
}>;
|
|
12
|
-
/** Credentials to apply to one request. */
|
|
13
|
-
export interface AuthPlan {
|
|
14
|
-
headers: Array<{
|
|
15
|
-
name: string;
|
|
16
|
-
value: string;
|
|
17
|
-
secret: boolean;
|
|
18
|
-
}>;
|
|
19
|
-
cookies: Array<{
|
|
20
|
-
name: string;
|
|
21
|
-
value: string;
|
|
22
|
-
}>;
|
|
23
|
-
query: Array<{
|
|
24
|
-
name: string;
|
|
25
|
-
value: string;
|
|
26
|
-
}>;
|
|
27
|
-
}
|
|
28
|
-
export type AuthResolution = {
|
|
29
|
-
ok: true;
|
|
30
|
-
plan: AuthPlan;
|
|
31
|
-
secrets: string[];
|
|
32
|
-
/** The authorization-code scheme whose stored token the plan carries, if any. */
|
|
33
|
-
oauth: string | null;
|
|
34
|
-
} | {
|
|
35
|
-
ok: false;
|
|
36
|
-
remediation: string;
|
|
37
|
-
};
|
|
38
|
-
/** Fetches (and caches) OAuth2 tokens: client credentials, or the stored
|
|
39
|
-
* token of an authorization-code scheme (`config.flow`). */
|
|
40
|
-
export type TokenSource = (scheme: OAuth2, config: Record<string, string>) => Promise<string>;
|
|
41
|
-
/** Thrown by a token source; the message is shown in the AUTH_FAILED envelope. */
|
|
42
|
-
export declare class TokenError extends Error {
|
|
43
|
-
/** The token endpoint refused the credential it was given (HTTP 4xx). */
|
|
44
|
-
rejected: boolean;
|
|
45
|
-
}
|
|
46
|
-
export declare function isSafeMethod(method: HttpMethod): boolean;
|
|
47
|
-
/** Resolve the credentials for `op`. Never throws; a token source failure
|
|
48
|
-
* becomes an `ok: false` resolution. */
|
|
49
|
-
export declare function resolveAuth(api: ApiDescriptor, op: OperationDescriptor, auth: AuthConfig, method: HttpMethod, tokens: TokenSource): Promise<AuthResolution>;
|
|
50
|
-
export {};
|
package/dist/auth.js
DELETED
|
@@ -1,264 +0,0 @@
|
|
|
1
|
-
import { bytesToBase64, isRecord, utf8 } from "./util.js";
|
|
2
|
-
/** Thrown by a token source; the message is shown in the AUTH_FAILED envelope. */
|
|
3
|
-
export class TokenError extends Error {
|
|
4
|
-
/** The token endpoint refused the credential it was given (HTTP 4xx). */
|
|
5
|
-
rejected = false;
|
|
6
|
-
}
|
|
7
|
-
const SAFE_METHODS = new Set(["GET", "HEAD", "OPTIONS", "TRACE"]);
|
|
8
|
-
export function isSafeMethod(method) {
|
|
9
|
-
return SAFE_METHODS.has(method);
|
|
10
|
-
}
|
|
11
|
-
function compositeConfigKey(part) {
|
|
12
|
-
switch (part.kind) {
|
|
13
|
-
case "cookie":
|
|
14
|
-
return part.name;
|
|
15
|
-
case "bearer":
|
|
16
|
-
return "bearer";
|
|
17
|
-
case "header":
|
|
18
|
-
return part.equalsCookie ?? part.fromConfig ?? part.name;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
function checkPrefix(token, prefix, what) {
|
|
22
|
-
return prefix && !token.startsWith(prefix) ? `The ${what} credential must start with "${prefix}"; check that the right kind of token is configured.` : null;
|
|
23
|
-
}
|
|
24
|
-
function tryComposite(c, config, method) {
|
|
25
|
-
if (!isRecord(config))
|
|
26
|
-
return { ok: false, missing: [`auth.${c.name} (an object for the composite profile ${c.name})`] };
|
|
27
|
-
const values = config;
|
|
28
|
-
const missing = [];
|
|
29
|
-
const mutation = !isSafeMethod(method);
|
|
30
|
-
for (const part of c.parts) {
|
|
31
|
-
if (part.kind === "header" && part.mutationOnly && !mutation)
|
|
32
|
-
continue;
|
|
33
|
-
const key = compositeConfigKey(part);
|
|
34
|
-
const value = values[key];
|
|
35
|
-
if (typeof value !== "string" || value === "") {
|
|
36
|
-
const role = part.kind === "cookie"
|
|
37
|
-
? `cookie ${part.name}`
|
|
38
|
-
: part.kind === "bearer"
|
|
39
|
-
? "bearer token"
|
|
40
|
-
: `header ${part.name}${part.equalsCookie ? ` (equal to cookie ${part.equalsCookie})` : ""}`;
|
|
41
|
-
missing.push(`auth.${c.name}[${JSON.stringify(key)}] (${role} of the composite profile ${c.name})`);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
if (missing.length > 0)
|
|
45
|
-
return { ok: false, missing };
|
|
46
|
-
return {
|
|
47
|
-
ok: true,
|
|
48
|
-
satisfier: {
|
|
49
|
-
key: `composite:${c.name}`,
|
|
50
|
-
apply: async (plan, secrets) => {
|
|
51
|
-
for (const part of c.parts) {
|
|
52
|
-
if (part.kind === "header" && part.mutationOnly && !mutation)
|
|
53
|
-
continue;
|
|
54
|
-
const value = values[compositeConfigKey(part)];
|
|
55
|
-
if (part.kind === "cookie") {
|
|
56
|
-
plan.cookies.push({ name: part.name, value });
|
|
57
|
-
secrets.push(value);
|
|
58
|
-
}
|
|
59
|
-
else if (part.kind === "bearer") {
|
|
60
|
-
const problem = checkPrefix(value, part.prefix, `${c.name} bearer`);
|
|
61
|
-
if (problem)
|
|
62
|
-
return problem;
|
|
63
|
-
plan.headers.push({ name: "Authorization", value: `Bearer ${value}`, secret: true });
|
|
64
|
-
secrets.push(value);
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
const secret = part.fromConfig === null;
|
|
68
|
-
plan.headers.push({ name: part.name, value, secret });
|
|
69
|
-
if (secret)
|
|
70
|
-
secrets.push(value);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
return null;
|
|
74
|
-
},
|
|
75
|
-
},
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
function tryDirect(scheme, config) {
|
|
79
|
-
const name = scheme.name;
|
|
80
|
-
if (scheme.kind === "oauth2" && isRecord(config)) {
|
|
81
|
-
const cfg = config;
|
|
82
|
-
if (cfg.flow === "authorizationCode") {
|
|
83
|
-
if (!scheme.authorizationCode) {
|
|
84
|
-
return { ok: false, missing: [`auth.${name} (sets flow "authorizationCode", but the API descriptor has no authorizationCode flow for ${name})`] };
|
|
85
|
-
}
|
|
86
|
-
if (typeof cfg.clientId !== "string" || cfg.clientId === "") {
|
|
87
|
-
return { ok: false, missing: [`auth.${name} (a clientId for the authorizationCode flow)`] };
|
|
88
|
-
}
|
|
89
|
-
const strings = { flow: "authorizationCode", clientId: cfg.clientId };
|
|
90
|
-
const secret = typeof cfg.clientSecret === "string" ? cfg.clientSecret : "";
|
|
91
|
-
return {
|
|
92
|
-
ok: true,
|
|
93
|
-
satisfier: {
|
|
94
|
-
key: `scheme:${name}`,
|
|
95
|
-
apply: async (plan, secrets, tokens, oauth) => {
|
|
96
|
-
if (secret !== "")
|
|
97
|
-
secrets.push(secret);
|
|
98
|
-
let token;
|
|
99
|
-
try {
|
|
100
|
-
token = await tokens(scheme, strings);
|
|
101
|
-
}
|
|
102
|
-
catch (error) {
|
|
103
|
-
return error instanceof TokenError ? error.message : `Could not obtain an OAuth2 token for ${name}.`;
|
|
104
|
-
}
|
|
105
|
-
secrets.push(token);
|
|
106
|
-
oauth.push(name);
|
|
107
|
-
plan.headers.push({ name: "Authorization", value: `Bearer ${token}`, secret: true });
|
|
108
|
-
return null;
|
|
109
|
-
},
|
|
110
|
-
},
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
if (typeof cfg.clientId === "string" && typeof cfg.clientSecret === "string" && scheme.tokenUrl) {
|
|
114
|
-
const strings = { clientId: cfg.clientId, clientSecret: cfg.clientSecret };
|
|
115
|
-
return {
|
|
116
|
-
ok: true,
|
|
117
|
-
satisfier: {
|
|
118
|
-
key: `scheme:${name}`,
|
|
119
|
-
apply: async (plan, secrets, tokens) => {
|
|
120
|
-
secrets.push(strings.clientSecret);
|
|
121
|
-
let token;
|
|
122
|
-
try {
|
|
123
|
-
token = await tokens(scheme, strings);
|
|
124
|
-
}
|
|
125
|
-
catch (error) {
|
|
126
|
-
return error instanceof TokenError ? error.message : `Could not obtain an OAuth2 token for ${name}.`;
|
|
127
|
-
}
|
|
128
|
-
secrets.push(token);
|
|
129
|
-
plan.headers.push({ name: "Authorization", value: `Bearer ${token}`, secret: true });
|
|
130
|
-
return null;
|
|
131
|
-
},
|
|
132
|
-
},
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
const code = scheme.authorizationCode ? `, or {flow: "authorizationCode", clientId, clientSecret?, redirectUri?}` : "";
|
|
136
|
-
return { ok: false, missing: [`auth.${name} (an access token, or {clientId, clientSecret} for the token URL${code})`] };
|
|
137
|
-
}
|
|
138
|
-
if (typeof config !== "string" || config === "") {
|
|
139
|
-
const what = scheme.kind === "http_basic"
|
|
140
|
-
? `"user:password" for HTTP Basic scheme ${name}`
|
|
141
|
-
: scheme.kind === "api_key"
|
|
142
|
-
? `API key for scheme ${name} (${scheme.in} ${scheme.wire})`
|
|
143
|
-
: `bearer token for scheme ${name}`;
|
|
144
|
-
return { ok: false, missing: [`auth.${name} (${what})`] };
|
|
145
|
-
}
|
|
146
|
-
const secret = config;
|
|
147
|
-
return {
|
|
148
|
-
ok: true,
|
|
149
|
-
satisfier: {
|
|
150
|
-
key: `scheme:${name}`,
|
|
151
|
-
apply: async (plan, secrets) => {
|
|
152
|
-
secrets.push(secret);
|
|
153
|
-
switch (scheme.kind) {
|
|
154
|
-
case "api_key":
|
|
155
|
-
if (scheme.in === "header")
|
|
156
|
-
plan.headers.push({ name: scheme.wire, value: secret, secret: true });
|
|
157
|
-
else if (scheme.in === "query")
|
|
158
|
-
plan.query.push({ name: scheme.wire, value: secret });
|
|
159
|
-
else
|
|
160
|
-
plan.cookies.push({ name: scheme.wire, value: secret });
|
|
161
|
-
return null;
|
|
162
|
-
case "http_bearer":
|
|
163
|
-
case "oauth2": {
|
|
164
|
-
const problem = scheme.kind === "http_bearer" ? checkPrefix(secret, scheme.prefix, name) : null;
|
|
165
|
-
if (problem)
|
|
166
|
-
return problem;
|
|
167
|
-
plan.headers.push({ name: "Authorization", value: `Bearer ${secret}`, secret: true });
|
|
168
|
-
return null;
|
|
169
|
-
}
|
|
170
|
-
case "http_basic": {
|
|
171
|
-
const encoded = bytesToBase64(utf8(secret));
|
|
172
|
-
secrets.push(encoded);
|
|
173
|
-
plan.headers.push({ name: "Authorization", value: `Basic ${encoded}`, secret: true });
|
|
174
|
-
return null;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
},
|
|
178
|
-
},
|
|
179
|
-
};
|
|
180
|
-
}
|
|
181
|
-
function trySchemeName(api, auth, name, method) {
|
|
182
|
-
const schemes = Array.isArray(api.auth) ? api.auth.filter(isRecord) : [];
|
|
183
|
-
const missing = [];
|
|
184
|
-
for (const scheme of schemes) {
|
|
185
|
-
if (scheme.kind === "composite" && Array.isArray(scheme.satisfies) && scheme.satisfies.includes(name) && Array.isArray(scheme.parts)) {
|
|
186
|
-
if (auth[scheme.name] === undefined) {
|
|
187
|
-
missing.push(`auth.${scheme.name} (composite profile ${scheme.name})`);
|
|
188
|
-
continue;
|
|
189
|
-
}
|
|
190
|
-
const attempt = tryComposite(scheme, auth[scheme.name], method);
|
|
191
|
-
if (attempt.ok)
|
|
192
|
-
return attempt;
|
|
193
|
-
missing.push(...attempt.missing);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
const direct = schemes.find((s) => s.name === name && s.kind !== "composite");
|
|
197
|
-
if (direct) {
|
|
198
|
-
const attempt = tryDirect(direct, auth[name]);
|
|
199
|
-
if (attempt.ok)
|
|
200
|
-
return attempt;
|
|
201
|
-
// When a composite profile covers the name, its missing parts explain
|
|
202
|
-
// the gap better than the raw spec scheme.
|
|
203
|
-
if (missing.length === 0)
|
|
204
|
-
missing.push(...attempt.missing);
|
|
205
|
-
}
|
|
206
|
-
else if (missing.length === 0) {
|
|
207
|
-
missing.push(`a scheme named ${name} (the API descriptor does not define it)`);
|
|
208
|
-
}
|
|
209
|
-
return { ok: false, missing };
|
|
210
|
-
}
|
|
211
|
-
/** Whether `auth` has an entry for the scheme `name` or a composite profile satisfying it. */
|
|
212
|
-
function configured(api, auth, name) {
|
|
213
|
-
if (auth[name] !== undefined)
|
|
214
|
-
return true;
|
|
215
|
-
const schemes = Array.isArray(api.auth) ? api.auth.filter(isRecord) : [];
|
|
216
|
-
return schemes.some((s) => s.kind === "composite" && Array.isArray(s.satisfies) && s.satisfies.includes(name) && typeof s.name === "string" && auth[s.name] !== undefined);
|
|
217
|
-
}
|
|
218
|
-
/** Resolve the credentials for `op`. Never throws; a token source failure
|
|
219
|
-
* becomes an `ok: false` resolution. */
|
|
220
|
-
export async function resolveAuth(api, op, auth, method, tokens) {
|
|
221
|
-
const security = Array.isArray(op.security) ? op.security.filter(Array.isArray) : [];
|
|
222
|
-
if (security.length === 0)
|
|
223
|
-
return { ok: true, plan: { headers: [], cookies: [], query: [] }, secrets: [], oauth: null };
|
|
224
|
-
let best = null;
|
|
225
|
-
// An empty alternative (anonymous access) is the fallback, never preferred
|
|
226
|
-
// over configured credentials.
|
|
227
|
-
const ordered = [...security.filter((alt) => alt.length > 0), ...security.filter((alt) => alt.length === 0)];
|
|
228
|
-
for (const alternative of ordered) {
|
|
229
|
-
const names = alternative.filter((n) => typeof n === "string");
|
|
230
|
-
const satisfiers = new Map();
|
|
231
|
-
const missing = [];
|
|
232
|
-
for (const name of names) {
|
|
233
|
-
const attempt = trySchemeName(api, auth, name, method);
|
|
234
|
-
if (attempt.ok)
|
|
235
|
-
satisfiers.set(attempt.satisfier.key, attempt.satisfier);
|
|
236
|
-
else
|
|
237
|
-
missing.push(...attempt.missing);
|
|
238
|
-
}
|
|
239
|
-
if (missing.length === 0) {
|
|
240
|
-
const plan = { headers: [], cookies: [], query: [] };
|
|
241
|
-
const secrets = [];
|
|
242
|
-
const oauth = [];
|
|
243
|
-
for (const satisfier of satisfiers.values()) {
|
|
244
|
-
const problem = await satisfier.apply(plan, secrets, tokens, oauth);
|
|
245
|
-
if (problem)
|
|
246
|
-
return { ok: false, remediation: `${problem} Operation ${op.id} was not sent.` };
|
|
247
|
-
}
|
|
248
|
-
return { ok: true, plan, secrets, oauth: oauth[0] ?? null };
|
|
249
|
-
}
|
|
250
|
-
// Report the alternative the caller started to configure, then the one
|
|
251
|
-
// with the fewest missing pieces.
|
|
252
|
-
const unique = [...new Set(missing)];
|
|
253
|
-
const touched = names.some((name) => configured(api, auth, name));
|
|
254
|
-
if (best === null || (touched && !best.touched) || (touched === best.touched && unique.length < best.missing.length)) {
|
|
255
|
-
best = { missing: unique, touched };
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
const alternatives = security.map((alt) => alt.join(" AND ") || "no credentials").join(" OR ");
|
|
259
|
-
const missingText = best ? best.missing.join("; ") : "credentials";
|
|
260
|
-
return {
|
|
261
|
-
ok: false,
|
|
262
|
-
remediation: `Operation ${op.id} needs ${alternatives}. Missing: ${missingText}. Configure it in ClientOptions.auth and call again; the request was not sent.`,
|
|
263
|
-
};
|
|
264
|
-
}
|
package/dist/classify.d.ts
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import type { ApiDescriptor, Diagnostic, OperationDescriptor, ResponseDescriptor } from "./types.js";
|
|
2
|
-
export declare function requestIdOf(headers: Record<string, string>): string | null;
|
|
3
|
-
/** The media type without parameters, lowercased; "" when absent. */
|
|
4
|
-
export declare function mediaTypeOf(headers: Record<string, string>): string;
|
|
5
|
-
export declare function isJsonMedia(media: string): boolean;
|
|
6
|
-
/** JSON Lines media types: one JSON value per line. */
|
|
7
|
-
export declare function isJsonlMedia(media: string): boolean;
|
|
8
|
-
/** The response descriptor matching a status: exact, then `NXX`, then `default`. */
|
|
9
|
-
export declare function matchResponse(responses: readonly ResponseDescriptor[], status: number): ResponseDescriptor | undefined;
|
|
10
|
-
/** A decoded response body. */
|
|
11
|
-
export interface DecodedBody {
|
|
12
|
-
/** Parsed JSON, text, bytes, or undefined for an empty body. */
|
|
13
|
-
value: unknown;
|
|
14
|
-
json: boolean;
|
|
15
|
-
/** JSON was announced but did not parse. */
|
|
16
|
-
invalidJson: boolean;
|
|
17
|
-
empty: boolean;
|
|
18
|
-
/** The body is JSON Lines: `value` is the array of the lines' values, or
|
|
19
|
-
* (when `invalidJson`) the text. */
|
|
20
|
-
jsonl?: boolean;
|
|
21
|
-
/** The first line (1-based) that did not parse. */
|
|
22
|
-
badLine?: number;
|
|
23
|
-
}
|
|
24
|
-
export declare function decodeBody(bytes: Uint8Array, headers: Record<string, string>, declared: string | null): DecodedBody;
|
|
25
|
-
/** Context shared by every classification of one call. */
|
|
26
|
-
export interface CallContext {
|
|
27
|
-
api: ApiDescriptor;
|
|
28
|
-
op: OperationDescriptor;
|
|
29
|
-
/** Key sent with the request, if any (never shown). */
|
|
30
|
-
key: string | null;
|
|
31
|
-
/** The key's header name, for remediation text. */
|
|
32
|
-
keyHeader: string;
|
|
33
|
-
attempts: number;
|
|
34
|
-
/** For a mutation without replay protection: the read that shows
|
|
35
|
-
* whether it took effect (see {@link OutcomeCheck}), or null. */
|
|
36
|
-
check?: OutcomeCheck | null;
|
|
37
|
-
}
|
|
38
|
-
/** How to find out whether an unprotected mutation took effect, in words:
|
|
39
|
-
* `call` names the read and its arguments (`public.listWebhookEndpoints`,
|
|
40
|
-
* `public.getAlphaMessageStatus with message_id "m1"`), `shows` what to
|
|
41
|
-
* look for in its answer (`whether endpoints contains {...}`). */
|
|
42
|
-
export interface OutcomeCheck {
|
|
43
|
-
call: string;
|
|
44
|
-
shows: string;
|
|
45
|
-
}
|
|
46
|
-
/** Whether the verification hook's arguments can be built without the
|
|
47
|
-
* call's response: after a lost or ambiguous answer there is no success
|
|
48
|
-
* body, so a hook whose args reference `$response` cannot be called. */
|
|
49
|
-
export declare function verifyCallable(verify: unknown): boolean;
|
|
50
|
-
/** The change an operation makes, in words, for remediation text: its
|
|
51
|
-
* summary when it has one (`the change "Rotate an endpoint's signing
|
|
52
|
-
* secret"`). */
|
|
53
|
-
export declare function changeOf(op: OperationDescriptor): string;
|
|
54
|
-
/** OUTCOME_UNKNOWN for a mutation whose effect cannot be known. Without
|
|
55
|
-
* replay protection (no key, no identity body) a repeat can apply the
|
|
56
|
-
* effect twice, so the envelope never offers one: `retryable` is
|
|
57
|
-
* `after_remediation` (check the state first), not `same_key_only`, and
|
|
58
|
-
* the remediation and `next_action` say what to check before repeating:
|
|
59
|
-
* `ctx.check` when the client found a read that shows it, else the
|
|
60
|
-
* operation's own change in words. */
|
|
61
|
-
export declare function outcomeUnknown(ctx: CallContext, cause: string, fields?: Partial<Diagnostic>): Diagnostic;
|
|
62
|
-
export declare function isMutation(op: OperationDescriptor): boolean;
|
|
63
|
-
/** The classification of an HTTP error status. */
|
|
64
|
-
export declare function classifyError(ctx: CallContext, status: number, headers: Record<string, string>, decoded: DecodedBody, retryAfterMs: number | null): Diagnostic;
|