@apifuse/provider-sdk 2.2.0-beta.59 → 2.2.0-beta.61

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.
Files changed (124) hide show
  1. package/AUTHORING.md +33 -16
  2. package/CHANGELOG.md +8 -0
  3. package/bin/apifuse-dev.ts +7 -8
  4. package/bin/apifuse-pack-types.ts +22 -0
  5. package/bin/apifuse-perf.ts +45 -10
  6. package/bin/apifuse-record.ts +6 -8
  7. package/bin/apifuse-submit-check.ts +2 -2
  8. package/dist/config/loader.js +13 -12
  9. package/dist/define.d.ts +3 -2
  10. package/dist/define.js +1 -1
  11. package/dist/dev.js +3 -1
  12. package/dist/engine.d.ts +7 -6
  13. package/dist/engine.js +6 -6
  14. package/dist/error-resolution.js +21 -4
  15. package/dist/errors.d.ts +3 -0
  16. package/dist/errors.js +9 -0
  17. package/dist/handle-meta.d.ts +39 -0
  18. package/dist/handle-meta.js +29 -0
  19. package/dist/handle.d.ts +187 -0
  20. package/dist/handle.js +254 -0
  21. package/dist/i18n/catalog.js +21 -1
  22. package/dist/index.d.ts +6 -4
  23. package/dist/index.js +4 -3
  24. package/dist/lint.d.ts +3 -0
  25. package/dist/lint.js +388 -62
  26. package/dist/provider.d.ts +4 -4
  27. package/dist/provider.js +3 -3
  28. package/dist/runtime/browser.js +2 -1
  29. package/dist/runtime/cache.js +3 -2
  30. package/dist/runtime/diagnostic-env-values.d.ts +5 -0
  31. package/dist/runtime/diagnostic-env-values.js +57 -0
  32. package/dist/runtime/diagnostic-env.d.ts +11 -0
  33. package/dist/runtime/diagnostic-env.js +35 -0
  34. package/dist/runtime/diagnostic-redactor.d.ts +49 -0
  35. package/dist/runtime/diagnostic-redactor.js +569 -0
  36. package/dist/runtime/egress-lease.d.ts +2 -0
  37. package/dist/runtime/egress-lease.js +4 -1
  38. package/dist/runtime/env.js +2 -1
  39. package/dist/runtime/handle-wordlist.d.ts +43 -0
  40. package/dist/runtime/{choice-wordlist.js → handle-wordlist.js} +127 -9
  41. package/dist/runtime/handle.d.ts +71 -0
  42. package/dist/runtime/handle.js +895 -0
  43. package/dist/runtime/instrumentation.js +3 -0
  44. package/dist/runtime/native-network.d.ts +10 -1
  45. package/dist/runtime/native-network.js +230 -29
  46. package/dist/runtime/native-telemetry.d.ts +142 -0
  47. package/dist/runtime/native-telemetry.js +231 -0
  48. package/dist/runtime/ocr.js +2 -1
  49. package/dist/runtime/otlp.js +5 -4
  50. package/dist/runtime/proxy-nodemaven.js +3 -2
  51. package/dist/runtime/request-options.d.ts +7 -0
  52. package/dist/runtime/request-options.js +62 -22
  53. package/dist/runtime/resolver-vendors/browser.js +0 -1
  54. package/dist/runtime/resolver.js +3 -2
  55. package/dist/runtime/state.d.ts +4 -1
  56. package/dist/runtime/state.js +32 -17
  57. package/dist/runtime/stt.js +3 -2
  58. package/dist/runtime/trace.d.ts +5 -0
  59. package/dist/runtime/trace.js +115 -20
  60. package/dist/schema.d.ts +11 -0
  61. package/dist/schema.js +56 -2
  62. package/dist/server/index.d.ts +1 -0
  63. package/dist/server/index.js +1 -0
  64. package/dist/server/self-test-redaction.js +8 -33
  65. package/dist/server/sensitive-values.d.ts +26 -0
  66. package/dist/server/sensitive-values.js +128 -0
  67. package/dist/server/serve-implementation.d.ts +4 -11
  68. package/dist/server/serve-implementation.js +305 -96
  69. package/dist/server/trace-output.d.ts +2 -1
  70. package/dist/server/trace-output.js +46 -14
  71. package/dist/testing/run.js +10 -7
  72. package/dist/trace-sanitization.d.ts +4 -3
  73. package/dist/trace-sanitization.js +45 -22
  74. package/dist/types.d.ts +4 -107
  75. package/package.json +2 -1
  76. package/src/config/loader.ts +13 -12
  77. package/src/define.ts +4 -3
  78. package/src/dev.ts +3 -1
  79. package/src/engine.ts +8 -8
  80. package/src/error-resolution.ts +21 -4
  81. package/src/errors.ts +13 -0
  82. package/src/handle-meta.ts +58 -0
  83. package/src/handle.ts +529 -0
  84. package/src/i18n/catalog.ts +23 -1
  85. package/src/index.ts +28 -13
  86. package/src/lint.ts +456 -71
  87. package/src/provider.ts +10 -20
  88. package/src/runtime/browser.ts +2 -1
  89. package/src/runtime/cache.ts +3 -2
  90. package/src/runtime/diagnostic-env-values.ts +56 -0
  91. package/src/runtime/diagnostic-env.ts +55 -0
  92. package/src/runtime/diagnostic-redactor.ts +662 -0
  93. package/src/runtime/egress-lease.ts +6 -1
  94. package/src/runtime/env.ts +2 -1
  95. package/src/runtime/{choice-wordlist.ts → handle-wordlist.ts} +130 -9
  96. package/src/runtime/handle.ts +1162 -0
  97. package/src/runtime/instrumentation.ts +3 -0
  98. package/src/runtime/native-network.ts +281 -33
  99. package/src/runtime/native-telemetry.ts +366 -0
  100. package/src/runtime/ocr.ts +2 -1
  101. package/src/runtime/otlp.ts +5 -4
  102. package/src/runtime/proxy-nodemaven.ts +3 -2
  103. package/src/runtime/request-options.ts +76 -28
  104. package/src/runtime/resolver-vendors/browser.ts +0 -1
  105. package/src/runtime/resolver.ts +3 -2
  106. package/src/runtime/state.ts +44 -16
  107. package/src/runtime/stt.ts +3 -2
  108. package/src/runtime/trace.ts +168 -26
  109. package/src/schema.ts +63 -2
  110. package/src/server/index.ts +17 -0
  111. package/src/server/self-test-redaction.ts +10 -40
  112. package/src/server/sensitive-values.ts +197 -0
  113. package/src/server/serve-implementation.ts +388 -128
  114. package/src/server/trace-output.ts +77 -17
  115. package/src/testing/run.ts +11 -7
  116. package/src/trace-sanitization.ts +70 -23
  117. package/src/types.ts +4 -137
  118. package/dist/choice-token.d.ts +0 -24
  119. package/dist/choice-token.js +0 -74
  120. package/dist/runtime/choice-wordlist.d.ts +0 -9
  121. package/dist/runtime/choice.d.ts +0 -27
  122. package/dist/runtime/choice.js +0 -801
  123. package/src/choice-token.ts +0 -165
  124. package/src/runtime/choice.ts +0 -1197
package/src/provider.ts CHANGED
@@ -18,14 +18,6 @@ export type {
18
18
  DefinedCredentialsAuth,
19
19
  } from "./auth.js";
20
20
  export { createFormCeremony } from "./ceremonies/index.js";
21
- export {
22
- assertFreshProviderChoiceIssuedAt,
23
- createProviderChoiceToken,
24
- ProviderChoiceTokenError,
25
- type ProviderChoiceTokenErrorReason,
26
- type ProviderChoiceTokenPayload,
27
- parseProviderChoiceToken,
28
- } from "./choice-token.js";
29
21
  export {
30
22
  centered,
31
23
  delayed,
@@ -161,12 +153,16 @@ export {
161
153
  providerLocaleKey,
162
154
  qualifyProviderLocaleKey,
163
155
  } from "./i18n/index.js";
156
+ export * from "./handle.js";
164
157
  export {
165
- type CreateProviderChoiceContextOptions,
166
- createProviderChoiceContext,
167
- createTestProviderChoiceContext,
168
- PROVIDER_RUNTIME_CHOICE_TOKEN_MASTER_SECRET_ENV,
169
- } from "./runtime/choice.js";
158
+ type CreateHandleContextOptions,
159
+ type CreateTestHandleContextOptions,
160
+ createHandleContext,
161
+ createTestHandleContext,
162
+ type HandleNormalizationClass,
163
+ type NormalizedHandle,
164
+ normalizeHandle,
165
+ } from "./runtime/handle.js";
170
166
  export {
171
167
  APIFUSE_CONTENT_PROVENANCE_META_KEY,
172
168
  APIFUSE_CONTENT_TRUST_META_KEY,
@@ -179,6 +175,7 @@ export {
179
175
  field,
180
176
  fields,
181
177
  isSensitiveSchema,
178
+ publicField,
182
179
  redactPayload,
183
180
  type SensitiveFieldKind,
184
181
  type SensitiveFieldOptions,
@@ -244,13 +241,6 @@ export type {
244
241
  OperationSensitivePath,
245
242
  OperationTransport,
246
243
  ProviderAccessVisibility,
247
- ProviderChoiceBindingOptions,
248
- ProviderChoiceConsumeMode,
249
- ProviderChoiceConsumeResult,
250
- ProviderChoiceContext,
251
- ProviderChoiceExplicitParseResult,
252
- ProviderChoiceIssueOptions,
253
- ProviderChoiceParseOptions,
254
244
  ProviderContext,
255
245
  ProviderContextFor,
256
246
  ProviderDefinition,
@@ -1,3 +1,4 @@
1
+ import { readDiagnosticEnv } from "./diagnostic-env.js";
1
2
  import { createRequire } from "node:module";
2
3
  import type { CDPSession, Frame, LaunchOptions, Locator, Page, Request, Route } from "playwright";
3
4
 
@@ -495,7 +496,7 @@ type SupportedBrowserClient = {
495
496
  };
496
497
 
497
498
  function getDefaultCdpPoolUrl(env = process.env): string | undefined {
498
- return env.APIFUSE__CDP_POOL__URL;
499
+ return readDiagnosticEnv("APIFUSE__CDP_POOL__URL", env);
499
500
  }
500
501
 
501
502
  async function importOptionalModule<T extends object>(moduleName: string): Promise<T> {
@@ -1,3 +1,4 @@
1
+ import { readDiagnosticEnv } from "./diagnostic-env.js";
1
2
  import { createHash, createHmac } from "node:crypto";
2
3
 
3
4
  import { providerCacheRedisUrlFromEnv } from "../config/loader.js";
@@ -362,7 +363,7 @@ async function withRedisFallback<T>(operation: () => Promise<T>): Promise<T | un
362
363
 
363
364
  export function createProviderCache(options: ProviderCacheOptions): ProviderCache {
364
365
  const redisUrl = options.redisUrl ?? providerCacheRedisUrlFromEnv();
365
- const configuredPepper = process.env[APIFUSE__CACHE__KEY_PEPPER_ENV];
366
+ const configuredPepper = readDiagnosticEnv(APIFUSE__CACHE__KEY_PEPPER_ENV);
366
367
  const pepper = configuredPepper && configuredPepper.length > 0 ? configuredPepper : undefined;
367
368
  const backend = getSharedBackend(redisUrl);
368
369
  const memoryMaxEntries = Math.max(1, options.memoryMaxEntries ?? DEFAULT_MEMORY_MAX_ENTRIES);
@@ -599,7 +600,7 @@ export function isProviderCacheBypassed(cache: ProviderCache): boolean {
599
600
  export function createBypassProviderCache(
600
601
  options: Pick<ProviderCacheOptions, "providerId">,
601
602
  ): ProviderCache {
602
- const configuredPepper = process.env[APIFUSE__CACHE__KEY_PEPPER_ENV];
603
+ const configuredPepper = readDiagnosticEnv(APIFUSE__CACHE__KEY_PEPPER_ENV);
603
604
  const pepper = configuredPepper && configuredPepper.length > 0 ? configuredPepper : undefined;
604
605
  const events: ProviderCacheLookupMeta[] = [];
605
606
  const secretScopedKeys = new Set<string>();
@@ -0,0 +1,56 @@
1
+ export function resolvedStringVariants(value: string | undefined): string[] {
2
+ if (value === undefined) return [];
3
+ return [...new Set([value, value.trim()])].filter((candidate) => candidate.length > 0);
4
+ }
5
+
6
+ function decodedComponent(value: string): string {
7
+ try {
8
+ return decodeURIComponent(value);
9
+ } catch {
10
+ return value;
11
+ }
12
+ }
13
+
14
+ export function urlCredentialComponents(
15
+ value: string,
16
+ options: { includePathAndQuery: boolean },
17
+ ): string[] {
18
+ let url: URL;
19
+ try {
20
+ url = new URL(value.trim());
21
+ } catch {
22
+ return [];
23
+ }
24
+ const username = decodedComponent(url.username);
25
+ const password = decodedComponent(url.password);
26
+ const components = [username, password, username && password ? `${username}:${password}` : ""];
27
+ if (options.includePathAndQuery) {
28
+ for (const segment of url.pathname.split("/")) {
29
+ if (segment) components.push(decodedComponent(segment));
30
+ }
31
+ for (const [key, entryValue] of url.searchParams) {
32
+ if (key) components.push(key);
33
+ if (entryValue) {
34
+ components.push(entryValue, `${key}=${entryValue}`);
35
+ }
36
+ }
37
+ }
38
+ return [...new Set(components.flatMap((component) => resolvedStringVariants(component)))];
39
+ }
40
+
41
+ export function otlpHeaderCredentialValues(value: string): string[] {
42
+ const variants = resolvedStringVariants(value);
43
+ for (const member of value.split(",")) {
44
+ const separator = member.indexOf("=");
45
+ if (separator <= 0) continue;
46
+ const headerValue = decodedComponent(member.slice(separator + 1).trim());
47
+ variants.push(...resolvedStringVariants(headerValue));
48
+ const headerName = decodedComponent(member.slice(0, separator).trim()).toLowerCase();
49
+ if (headerName === "authorization" || headerName === "proxy-authorization") {
50
+ // A transport may echo just the credential without its HTTP auth scheme.
51
+ const credential = /^(?:Bearer|Basic)\s+(.+)$/i.exec(headerValue)?.[1];
52
+ if (credential) variants.push(...resolvedStringVariants(credential));
53
+ }
54
+ }
55
+ return [...new Set(variants)];
56
+ }
@@ -0,0 +1,55 @@
1
+ import { AsyncLocalStorage } from "node:async_hooks";
2
+
3
+ import {
4
+ resolvedStringVariants,
5
+ urlCredentialComponents,
6
+ otlpHeaderCredentialValues,
7
+ } from "./diagnostic-env-values.js";
8
+ import {
9
+ type CompiledDiagnosticSensitiveValues,
10
+ registerProcessDiagnosticValues,
11
+ suppressProcessDiagnosticValues,
12
+ withAppDiagnosticFallback,
13
+ } from "./diagnostic-redactor.js";
14
+
15
+ type EnvObserver = (name: string, value: string) => void;
16
+ export interface DiagnosticEnvScope {
17
+ observe: EnvObserver;
18
+ finished: boolean;
19
+ staticValues?: CompiledDiagnosticSensitiveValues;
20
+ }
21
+ const observers = new AsyncLocalStorage<DiagnosticEnvScope>();
22
+
23
+ /** Resolve and register atomically, before any consumer can use a live env credential. */
24
+ export function readDiagnosticEnv(
25
+ name: string,
26
+ env: Readonly<Record<string, string | undefined>> = process.env,
27
+ ): string | undefined {
28
+ const value = env[name];
29
+ if (value !== undefined) {
30
+ const scope = observers.getStore();
31
+ if (scope && !scope.finished) scope.observe(name, value);
32
+ else {
33
+ // ALS survives the response. Finished requests register into the app's
34
+ // inherited fallback lifetime, never a dead request's private inventory.
35
+ if (Buffer.byteLength(value) > 64 * 1024) suppressProcessDiagnosticValues();
36
+ else
37
+ registerProcessDiagnosticValues([
38
+ ...resolvedStringVariants(value),
39
+ ...urlCredentialComponents(value, {
40
+ includePathAndQuery: name === "APIFUSE__CDP_POOL__URL",
41
+ }),
42
+ ...(name === "OTEL_EXPORTER_OTLP_HEADERS" || name === "OTEL_EXPORTER_OTLP_TRACES_HEADERS"
43
+ ? otlpHeaderCredentialValues(value)
44
+ : []),
45
+ ]);
46
+ }
47
+ }
48
+ return value;
49
+ }
50
+
51
+ export function withDiagnosticEnv<T>(observer: EnvObserver | DiagnosticEnvScope, fn: () => T): T {
52
+ const scope = typeof observer === "function" ? { observe: observer, finished: false } : observer;
53
+ const run = () => observers.run(scope, fn);
54
+ return scope.staticValues ? withAppDiagnosticFallback(scope.staticValues, run) : run();
55
+ }