@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
@@ -0,0 +1,197 @@
1
+ import {
2
+ resolvedStringVariants,
3
+ urlCredentialComponents,
4
+ otlpHeaderCredentialValues,
5
+ } from "../runtime/diagnostic-env-values.js";
6
+ import {
7
+ PROVIDER_CACHE_REDIS_URL_ENV,
8
+ PROVIDER_STATE_REDIS_URL_ENV,
9
+ REDIS_URL_ENV,
10
+ } from "../config/loader.js";
11
+ import {
12
+ ENGINE_OWNED_PROXY_CREDENTIAL_ENV_NAMES,
13
+ ENGINE_OWNED_RESOLVER_CREDENTIAL_ENV_NAMES,
14
+ } from "../engine.js";
15
+ import { APIFUSE__CACHE__KEY_PEPPER_ENV } from "../runtime/cache.js";
16
+ import { APIFUSE__ENGINE__CEREMONY_LEASE_KEY } from "../runtime/egress-lease.js";
17
+ import {
18
+ APIFUSE__OCR__API_KEY_ENV,
19
+ APIFUSE__OCR__CLOUDFLARE_API_TOKEN_ENV,
20
+ } from "../runtime/ocr.js";
21
+ import {
22
+ OTEL_EXPORTER_OTLP_HEADERS,
23
+ OTEL_EXPORTER_OTLP_TRACES_HEADERS,
24
+ OTEL_EXPORTER_OTLP_ENDPOINT,
25
+ OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
26
+ } from "../runtime/otlp.js";
27
+ import { APIFUSE__CDP_POOL__URL } from "../runtime/resolver-config.js";
28
+ import { APIFUSE__STT__CLOUDFLARE_API_TOKEN_ENV } from "../runtime/stt.js";
29
+ import type { ProviderDefinition } from "../types.js";
30
+ import {
31
+ PROVIDER_RUNTIME_SELF_TEST_MASTER_SECRET_ENV,
32
+ PROVIDER_RUNTIME_SELF_TEST_MASTER_SECRET_PREVIOUS_ENV,
33
+ } from "./self-test-token.js";
34
+
35
+ export const SELF_TEST_SENSITIVE_SOURCE_KINDS = [
36
+ "selfTestMasterSecrets",
37
+ "providerSecrets",
38
+ "healthProbeSecrets",
39
+ "requestCredentials",
40
+ ] as const;
41
+
42
+ /** Explicit source allowlist: no environment-name or payload-shape secret sweep. */
43
+ export const DIAGNOSTIC_SENSITIVE_SOURCE_KINDS = [
44
+ ...SELF_TEST_SENSITIVE_SOURCE_KINDS,
45
+ "engineProxyCredentials",
46
+ "engineSolverKeys",
47
+ "engineCeremonyLeaseCredentials",
48
+ "ocrCredentials",
49
+ "sttCredentials",
50
+ "cdpCredentials",
51
+ "statefulForwardingSecret",
52
+ "redisCredentials",
53
+ "otlpHeaders",
54
+ "cacheKeyPepper",
55
+ "sensitiveParams",
56
+ ] as const;
57
+ export type DiagnosticSensitiveSourceKind = (typeof DIAGNOSTIC_SENSITIVE_SOURCE_KINDS)[number];
58
+
59
+ /**
60
+ * Startup inventory; request-scoped readers additionally register live values on access.
61
+ */
62
+ export function collectStaticDiagnosticSensitiveValues(
63
+ provider: ProviderDefinition,
64
+ options: {
65
+ env?: Readonly<Record<string, string | undefined>>;
66
+ statefulForwardingSecret?: string;
67
+ } = {},
68
+ ): string[] {
69
+ return collectDiagnosticSensitiveValues(provider, {
70
+ ...options,
71
+ sourceKinds: DIAGNOSTIC_SENSITIVE_SOURCE_KINDS.filter(
72
+ (kind) => kind !== "requestCredentials" && kind !== "sensitiveParams",
73
+ ),
74
+ });
75
+ }
76
+
77
+ /** Generalised self-test collector, also used to seed and enrich ordinary request scopes. */
78
+ export function collectDiagnosticSensitiveValues(
79
+ provider: ProviderDefinition,
80
+ options: {
81
+ env?: Readonly<Record<string, string | undefined>>;
82
+ credentialInputs?: Readonly<Record<string, unknown>>;
83
+ connectionSecrets?: Readonly<Record<string, string | undefined>>;
84
+ sensitiveParams?: readonly string[];
85
+ statefulForwardingSecret?: string;
86
+ sourceKinds?: readonly DiagnosticSensitiveSourceKind[];
87
+ minimumLength?: number;
88
+ /** Preserve the legacy self-test response collector when false. */
89
+ includeResolvedEnvVariants?: boolean;
90
+ } = {},
91
+ ): string[] {
92
+ const env = options.env ?? process.env;
93
+ const envValues = (names: readonly string[]) => names.map((name) => env[name]);
94
+ const resolvedEnvValues = (names: readonly string[]) =>
95
+ envValues(names).flatMap((value) =>
96
+ value === undefined
97
+ ? []
98
+ : options.includeResolvedEnvVariants === false
99
+ ? [value]
100
+ : [value, value.trim()],
101
+ );
102
+ const healthProbe = provider.healthProbe ?? provider.healthMonitor;
103
+ const sources: Record<DiagnosticSensitiveSourceKind, () => readonly unknown[]> = {
104
+ selfTestMasterSecrets: () =>
105
+ resolvedEnvValues([
106
+ PROVIDER_RUNTIME_SELF_TEST_MASTER_SECRET_ENV,
107
+ PROVIDER_RUNTIME_SELF_TEST_MASTER_SECRET_PREVIOUS_ENV,
108
+ ]),
109
+ providerSecrets: () => resolvedEnvValues((provider.secrets ?? []).map((secret) => secret.name)),
110
+ healthProbeSecrets: () =>
111
+ resolvedEnvValues([
112
+ ...(healthProbe?.requiredSecrets ?? []),
113
+ ...Object.values(healthProbe?.credentialInputs ?? {}),
114
+ ]),
115
+ requestCredentials: () => [
116
+ ...Object.values(options.credentialInputs ?? {}),
117
+ ...Object.values(options.connectionSecrets ?? {}),
118
+ ],
119
+ engineProxyCredentials: () => resolvedEnvValues(ENGINE_OWNED_PROXY_CREDENTIAL_ENV_NAMES),
120
+ // These are the resolver-config API keys after #251's engine credential projection.
121
+ engineSolverKeys: () => resolvedEnvValues(ENGINE_OWNED_RESOLVER_CREDENTIAL_ENV_NAMES),
122
+ engineCeremonyLeaseCredentials: () => resolvedEnvValues([APIFUSE__ENGINE__CEREMONY_LEASE_KEY]),
123
+ ocrCredentials: () =>
124
+ resolvedEnvValues([APIFUSE__OCR__CLOUDFLARE_API_TOKEN_ENV, APIFUSE__OCR__API_KEY_ENV]),
125
+ sttCredentials: () => resolvedEnvValues([APIFUSE__STT__CLOUDFLARE_API_TOKEN_ENV]),
126
+ cdpCredentials: () =>
127
+ resolvedStringVariants(env[APIFUSE__CDP_POOL__URL]).flatMap((url) => [
128
+ url,
129
+ ...urlCredentialComponents(url, { includePathAndQuery: true }),
130
+ ]),
131
+ statefulForwardingSecret: () => resolvedStringVariants(options.statefulForwardingSecret),
132
+ redisCredentials: () =>
133
+ [PROVIDER_CACHE_REDIS_URL_ENV, PROVIDER_STATE_REDIS_URL_ENV, REDIS_URL_ENV]
134
+ .flatMap((name) => resolvedStringVariants(env[name]))
135
+ .flatMap((url) => urlCredentialComponents(url, { includePathAndQuery: false })),
136
+ otlpHeaders: () => [
137
+ ...[OTEL_EXPORTER_OTLP_TRACES_HEADERS, OTEL_EXPORTER_OTLP_HEADERS].flatMap((name) =>
138
+ env[name] === undefined ? [] : otlpHeaderCredentialValues(env[name]),
139
+ ),
140
+ ...[OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_TRACES_ENDPOINT].flatMap((name) =>
141
+ resolvedStringVariants(env[name]).flatMap((url) =>
142
+ urlCredentialComponents(url, { includePathAndQuery: false }),
143
+ ),
144
+ ),
145
+ ],
146
+ cacheKeyPepper: () => resolvedEnvValues([APIFUSE__CACHE__KEY_PEPPER_ENV]),
147
+ sensitiveParams: () => options.sensitiveParams ?? [],
148
+ };
149
+ const values = new Set<string>();
150
+ for (const kind of options.sourceKinds ?? DIAGNOSTIC_SENSITIVE_SOURCE_KINDS) {
151
+ for (const value of sources[kind]()) {
152
+ if (typeof value === "string" && value.length >= (options.minimumLength ?? 1))
153
+ values.add(value);
154
+ }
155
+ }
156
+ return [...values];
157
+ }
158
+
159
+ const diagnosticEnvNames = new WeakMap<ProviderDefinition, Set<string>>();
160
+
161
+ /** Derive the live-read allowlist from the same collector as the startup inventory. */
162
+ export function createDiagnosticEnvObserver(
163
+ provider: ProviderDefinition,
164
+ register: (values: readonly string[]) => void,
165
+ has?: (value: string) => boolean,
166
+ ): (name: string, value: string) => void {
167
+ let names = diagnosticEnvNames.get(provider);
168
+ if (!names) {
169
+ names = new Set<string>();
170
+ const captured = names;
171
+ collectStaticDiagnosticSensitiveValues(provider, {
172
+ env: new Proxy(
173
+ {},
174
+ {
175
+ get: (_target, key) => {
176
+ if (typeof key === "string") captured.add(key);
177
+ return undefined;
178
+ },
179
+ },
180
+ ),
181
+ });
182
+ diagnosticEnvNames.set(provider, names);
183
+ }
184
+ const seen = new Map<string, string>();
185
+ return (name, value) => {
186
+ if (!names.has(name) || seen.get(name) === value) return;
187
+ if (has?.(value) && has(value.trim())) return;
188
+ // Includes URL userinfo/CDP components and OTLP auth values, using the same rules.
189
+ register([
190
+ ...resolvedStringVariants(value),
191
+ ...collectStaticDiagnosticSensitiveValues(provider, {
192
+ env: { [name]: value },
193
+ }),
194
+ ]);
195
+ seen.set(name, value);
196
+ };
197
+ }