@apifuse/provider-sdk 2.1.0-beta.9 → 2.2.0-beta.2

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 (138) hide show
  1. package/AUTHORING.md +134 -0
  2. package/CHANGELOG.md +61 -0
  3. package/README.md +21 -9
  4. package/SUBMISSION.md +10 -11
  5. package/bin/apifuse-pack-check.ts +22 -0
  6. package/bin/apifuse-submit-check.ts +1747 -388
  7. package/dist/auth-turn/auth-turn.v1.schema.json +89 -0
  8. package/dist/auth-turn/fixtures/invalid/empty-kind.json +4 -0
  9. package/dist/auth-turn/fixtures/invalid/expires-at-not-string.json +5 -0
  10. package/dist/auth-turn/fixtures/invalid/missing-kind.json +3 -0
  11. package/dist/auth-turn/fixtures/invalid/missing-turn-id.json +3 -0
  12. package/dist/auth-turn/fixtures/invalid/timing-unknown-field.json +7 -0
  13. package/dist/auth-turn/fixtures/invalid/turn-id-snake-case.json +4 -0
  14. package/dist/auth-turn/fixtures/invalid/unknown-top-level-field.json +5 -0
  15. package/dist/auth-turn/fixtures/valid/abort.json +8 -0
  16. package/dist/auth-turn/fixtures/valid/challenge.json +17 -0
  17. package/dist/auth-turn/fixtures/valid/complete.json +13 -0
  18. package/dist/auth-turn/fixtures/valid/form.json +14 -0
  19. package/dist/auth-turn/fixtures/valid/message.json +13 -0
  20. package/dist/auth-turn/fixtures/valid/multi_choice.json +15 -0
  21. package/dist/auth-turn/fixtures/valid/pending.json +5 -0
  22. package/dist/auth-turn/fixtures/valid/poll.json +9 -0
  23. package/dist/auth-turn/fixtures/valid/redirect.json +16 -0
  24. package/dist/auth-turn/fixtures/valid/retry.json +8 -0
  25. package/dist/auth-turn/fixtures/valid/unknown-kind.json +7 -0
  26. package/dist/auth-turn/index.d.ts +195 -0
  27. package/dist/auth-turn/index.js +133 -0
  28. package/dist/auth.d.ts +76 -0
  29. package/dist/auth.js +436 -0
  30. package/dist/ceremonies/index.js +7 -31
  31. package/dist/cli/create.js +45 -30
  32. package/dist/cli/templates/provider/.dockerignore.tpl +22 -0
  33. package/dist/cli/templates/provider/.gitignore.tpl +22 -0
  34. package/dist/cli/templates/provider/AGENTS.md.tpl +87 -0
  35. package/dist/cli/templates/provider/CLAUDE.md.tpl +1 -0
  36. package/dist/cli/templates/provider/Dockerfile.tpl +7 -0
  37. package/dist/cli/templates/provider/README.md.tpl +163 -0
  38. package/dist/cli/templates/provider/dev.ts.tpl +5 -0
  39. package/dist/cli/templates/provider/domain/README.md.tpl +3 -0
  40. package/dist/cli/templates/provider/index.test.ts.tpl +13 -0
  41. package/dist/cli/templates/provider/index.ts.tpl +15 -0
  42. package/dist/cli/templates/provider/mappers/README.md.tpl +3 -0
  43. package/dist/cli/templates/provider/meta.ts.tpl +7 -0
  44. package/dist/cli/templates/provider/operations/index.ts.tpl +5 -0
  45. package/dist/cli/templates/provider/operations/ping.ts.tpl +24 -0
  46. package/dist/cli/templates/provider/schemas/ping.ts.tpl +24 -0
  47. package/dist/cli/templates/provider/skills/fixtures-and-recording/SKILL.md.tpl +58 -0
  48. package/dist/cli/templates/provider/skills/health-checks-and-fail-closed/SKILL.md.tpl +65 -0
  49. package/dist/cli/templates/provider/skills/normalization-standards/SKILL.md.tpl +57 -0
  50. package/dist/cli/templates/provider/skills/pagination-and-counts/SKILL.md.tpl +52 -0
  51. package/dist/cli/templates/provider/skills/upstream-contract-verification/SKILL.md.tpl +45 -0
  52. package/dist/cli/templates/provider/skills/upstream-notes/README.md.tpl +13 -0
  53. package/dist/cli/templates/provider/start.ts.tpl +5 -0
  54. package/dist/cli/templates/provider/upstream/README.md.tpl +3 -0
  55. package/dist/contract.js +1 -0
  56. package/dist/define.d.ts +6 -1
  57. package/dist/define.js +140 -70
  58. package/dist/index.d.ts +3 -2
  59. package/dist/index.js +2 -1
  60. package/dist/lint.d.ts +1 -0
  61. package/dist/lint.js +27 -0
  62. package/dist/provider.d.ts +4 -2
  63. package/dist/provider.js +2 -1
  64. package/dist/runtime/auth-flow.js +2 -0
  65. package/dist/runtime/browser.js +203 -0
  66. package/dist/runtime/http.js +46 -336
  67. package/dist/runtime/proxy-retry-policy.d.ts +40 -0
  68. package/dist/runtime/proxy-retry-policy.js +326 -0
  69. package/dist/runtime/stealth.d.ts +5 -2
  70. package/dist/runtime/stealth.js +206 -206
  71. package/dist/server/index.d.ts +4 -0
  72. package/dist/server/index.js +4 -0
  73. package/dist/server/self-test-input-tokens.d.ts +1 -0
  74. package/dist/server/self-test-input-tokens.js +37 -0
  75. package/dist/server/self-test-redaction.d.ts +20 -0
  76. package/dist/server/self-test-redaction.js +70 -0
  77. package/dist/server/self-test-token.d.ts +30 -0
  78. package/dist/server/self-test-token.js +50 -0
  79. package/dist/server/self-test.d.ts +98 -0
  80. package/dist/server/self-test.js +555 -0
  81. package/dist/server/serve.d.ts +6 -0
  82. package/dist/server/serve.js +38 -10
  83. package/dist/server/types.d.ts +1 -0
  84. package/dist/server/types.js +1 -0
  85. package/dist/testing/run.js +5 -1
  86. package/dist/types.d.ts +150 -0
  87. package/package.json +9 -2
  88. package/src/auth-turn/auth-turn.v1.schema.json +89 -0
  89. package/src/auth-turn/fixtures/invalid/empty-kind.json +4 -0
  90. package/src/auth-turn/fixtures/invalid/expires-at-not-string.json +5 -0
  91. package/src/auth-turn/fixtures/invalid/missing-kind.json +3 -0
  92. package/src/auth-turn/fixtures/invalid/missing-turn-id.json +3 -0
  93. package/src/auth-turn/fixtures/invalid/timing-unknown-field.json +7 -0
  94. package/src/auth-turn/fixtures/invalid/turn-id-snake-case.json +4 -0
  95. package/src/auth-turn/fixtures/invalid/unknown-top-level-field.json +5 -0
  96. package/src/auth-turn/fixtures/valid/abort.json +8 -0
  97. package/src/auth-turn/fixtures/valid/challenge.json +17 -0
  98. package/src/auth-turn/fixtures/valid/complete.json +13 -0
  99. package/src/auth-turn/fixtures/valid/form.json +14 -0
  100. package/src/auth-turn/fixtures/valid/message.json +13 -0
  101. package/src/auth-turn/fixtures/valid/multi_choice.json +15 -0
  102. package/src/auth-turn/fixtures/valid/pending.json +5 -0
  103. package/src/auth-turn/fixtures/valid/poll.json +9 -0
  104. package/src/auth-turn/fixtures/valid/redirect.json +16 -0
  105. package/src/auth-turn/fixtures/valid/retry.json +8 -0
  106. package/src/auth-turn/fixtures/valid/unknown-kind.json +7 -0
  107. package/src/auth-turn/index.ts +177 -0
  108. package/src/auth.ts +786 -0
  109. package/src/ceremonies/index.ts +9 -43
  110. package/src/cli/create.ts +60 -97
  111. package/src/cli/templates/provider/AGENTS.md.tpl +87 -0
  112. package/src/cli/templates/provider/CLAUDE.md.tpl +1 -0
  113. package/src/cli/templates/provider/README.md.tpl +7 -4
  114. package/src/cli/templates/provider/skills/fixtures-and-recording/SKILL.md.tpl +58 -0
  115. package/src/cli/templates/provider/skills/health-checks-and-fail-closed/SKILL.md.tpl +65 -0
  116. package/src/cli/templates/provider/skills/normalization-standards/SKILL.md.tpl +57 -0
  117. package/src/cli/templates/provider/skills/pagination-and-counts/SKILL.md.tpl +52 -0
  118. package/src/cli/templates/provider/skills/upstream-contract-verification/SKILL.md.tpl +45 -0
  119. package/src/cli/templates/provider/skills/upstream-notes/README.md.tpl +13 -0
  120. package/src/contract.ts +1 -0
  121. package/src/define.ts +198 -71
  122. package/src/index.ts +16 -0
  123. package/src/lint.ts +33 -0
  124. package/src/provider.ts +27 -0
  125. package/src/runtime/auth-flow.ts +2 -0
  126. package/src/runtime/browser.ts +293 -1
  127. package/src/runtime/http.ts +84 -530
  128. package/src/runtime/proxy-retry-policy.ts +469 -0
  129. package/src/runtime/stealth.ts +269 -353
  130. package/src/server/index.ts +36 -0
  131. package/src/server/self-test-input-tokens.ts +46 -0
  132. package/src/server/self-test-redaction.ts +97 -0
  133. package/src/server/self-test-token.ts +70 -0
  134. package/src/server/self-test.ts +725 -0
  135. package/src/server/serve.ts +75 -6
  136. package/src/server/types.ts +1 -0
  137. package/src/testing/run.ts +9 -1
  138. package/src/types.ts +186 -0
@@ -1,4 +1,40 @@
1
1
  export { createServerApp, type ServeOptions, serve } from "./serve";
2
+ export {
3
+ computeSelfTestPlanDigest,
4
+ createSelfTestApp,
5
+ createSelfTestInvoke,
6
+ DEFAULT_SELF_TEST_REQUEST_BUDGET_MS,
7
+ isSelfTestReadOnlyOperation,
8
+ PROVIDER_RUNTIME_SELF_TEST_REQUEST_BUDGET_MS_ENV,
9
+ resolveSelfTestPort,
10
+ SELF_TEST_HEALTHZ_PATH,
11
+ SELF_TEST_PATH,
12
+ SELF_TEST_SCHEMA_VERSION,
13
+ type SelfTestAppOptions,
14
+ type SelfTestCaseResult,
15
+ type SelfTestCaseStatus,
16
+ type SelfTestOperationInvoke,
17
+ type SelfTestRequest,
18
+ SelfTestRequestSchema,
19
+ type SelfTestResponse,
20
+ } from "./self-test";
21
+ export { resolveHealthCheckInputDateTokens } from "./self-test-input-tokens";
22
+ export {
23
+ collectSelfTestSensitiveValues,
24
+ redactSelfTestText,
25
+ SELF_TEST_MAX_TEXT_LENGTH,
26
+ SELF_TEST_REDACTED_PLACEHOLDER,
27
+ } from "./self-test-redaction";
28
+ export {
29
+ DEFAULT_SELF_TEST_PORT,
30
+ deriveSelfTestToken,
31
+ PROVIDER_RUNTIME_SELF_TEST_MASTER_SECRET_ENV,
32
+ PROVIDER_RUNTIME_SELF_TEST_MASTER_SECRET_PREVIOUS_ENV,
33
+ PROVIDER_RUNTIME_SELF_TEST_PORT_ENV,
34
+ resolveSelfTestMasterSecrets,
35
+ type SelfTestMasterSecrets,
36
+ verifySelfTestAuthorization,
37
+ } from "./self-test-token";
2
38
  export type {
3
39
  AuthFlowRequest,
4
40
  AuthFlowResponse,
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Relative-KST date token resolution for health-check case inputs, ported from
3
+ * the monorepo health-monitor (`apps/health-monitor/src/lib/health-check-input.ts`)
4
+ * so provider self-tests resolve durable probe inputs identically.
5
+ *
6
+ * Supported token: `+<days>d` or `+<days>d:YYYYMMDD` (1..365 days ahead, KST).
7
+ */
8
+ const RELATIVE_KST_DATE_TOKEN = /^\+(\d{1,3})d(?::(YYYYMMDD))?$/i;
9
+
10
+ function dateFromKstDaysAhead(
11
+ daysAhead: number,
12
+ now = new Date(),
13
+ format: "YYYY-MM-DD" | "YYYYMMDD" = "YYYY-MM-DD",
14
+ ): string {
15
+ const kstNow = new Date(now.getTime() + 9 * 60 * 60 * 1000);
16
+ const date = new Date(
17
+ Date.UTC(kstNow.getUTCFullYear(), kstNow.getUTCMonth(), kstNow.getUTCDate() + daysAhead),
18
+ );
19
+ const isoDate = date.toISOString().slice(0, 10);
20
+ return format === "YYYYMMDD" ? isoDate.replace(/-/g, "") : isoDate;
21
+ }
22
+
23
+ export function resolveHealthCheckInputDateTokens(value: unknown, now = new Date()): unknown {
24
+ if (typeof value === "string") {
25
+ const relative = value.match(RELATIVE_KST_DATE_TOKEN);
26
+ if (!relative) return value;
27
+ const daysAhead = Number(relative[1]);
28
+ if (!Number.isInteger(daysAhead) || daysAhead < 1 || daysAhead > 365) {
29
+ return value;
30
+ }
31
+ const format = relative[2]?.toUpperCase() === "YYYYMMDD" ? "YYYYMMDD" : "YYYY-MM-DD";
32
+ return dateFromKstDaysAhead(daysAhead, now, format);
33
+ }
34
+ if (Array.isArray(value)) {
35
+ return value.map((entry) => resolveHealthCheckInputDateTokens(entry, now));
36
+ }
37
+ if (value && typeof value === "object") {
38
+ return Object.fromEntries(
39
+ Object.entries(value).map(([key, entry]) => [
40
+ key,
41
+ resolveHealthCheckInputDateTokens(entry, now),
42
+ ]),
43
+ );
44
+ }
45
+ return value;
46
+ }
@@ -0,0 +1,97 @@
1
+ import type { ProviderDefinition } from "../types";
2
+ import {
3
+ PROVIDER_RUNTIME_SELF_TEST_MASTER_SECRET_ENV,
4
+ PROVIDER_RUNTIME_SELF_TEST_MASTER_SECRET_PREVIOUS_ENV,
5
+ } from "./self-test-token";
6
+
7
+ export const SELF_TEST_REDACTED_PLACEHOLDER = "[REDACTED]";
8
+
9
+ /** Maximum length of any string echoed in a self-test response. */
10
+ export const SELF_TEST_MAX_TEXT_LENGTH = 300;
11
+
12
+ /** Values shorter than this are too generic to be treated as secrets. */
13
+ const MIN_SENSITIVE_VALUE_LENGTH = 4;
14
+
15
+ const HEADER_SHAPED_SECRETS: readonly RegExp[] = [
16
+ /(authorization\s*[:=]\s*)[^\s;,]+/gi,
17
+ /(bearer\s+)[A-Za-z0-9._~+/=-]{8,}/gi,
18
+ /((?:set-)?cookie\s*[:=]\s*)[^;\n]+/gi,
19
+ ];
20
+
21
+ /**
22
+ * Collects every secret-shaped value known to the self-test runtime so probe
23
+ * results can be scrubbed before they leave the pod: master secrets, declared
24
+ * provider env secrets, health-probe credential env values, and request-supplied
25
+ * credential inputs.
26
+ */
27
+ export function collectSelfTestSensitiveValues(
28
+ provider: ProviderDefinition,
29
+ options: {
30
+ env?: Readonly<Record<string, string | undefined>>;
31
+ credentialInputs?: Readonly<Record<string, string>>;
32
+ } = {},
33
+ ): string[] {
34
+ const env = options.env ?? process.env;
35
+ const envNames = new Set<string>([
36
+ PROVIDER_RUNTIME_SELF_TEST_MASTER_SECRET_ENV,
37
+ PROVIDER_RUNTIME_SELF_TEST_MASTER_SECRET_PREVIOUS_ENV,
38
+ ]);
39
+ for (const secret of provider.secrets ?? []) {
40
+ envNames.add(secret.name);
41
+ }
42
+ const healthProbe = provider.healthProbe ?? provider.healthMonitor;
43
+ for (const name of healthProbe?.requiredSecrets ?? []) {
44
+ envNames.add(name);
45
+ }
46
+ for (const name of Object.values(healthProbe?.credentialInputs ?? {})) {
47
+ envNames.add(name);
48
+ }
49
+
50
+ const values = new Set<string>();
51
+ for (const name of envNames) {
52
+ const value = env[name];
53
+ if (typeof value === "string" && value.length >= MIN_SENSITIVE_VALUE_LENGTH) {
54
+ values.add(value);
55
+ }
56
+ }
57
+ for (const value of Object.values(options.credentialInputs ?? {})) {
58
+ if (value.length >= MIN_SENSITIVE_VALUE_LENGTH) {
59
+ values.add(value);
60
+ }
61
+ }
62
+ return [...values];
63
+ }
64
+
65
+ function escapeRegExp(value: string): string {
66
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
67
+ }
68
+
69
+ /**
70
+ * Scrubs known secret values and header-shaped credentials from a string and
71
+ * caps its length. Every string embedded in a SelfTestResponse MUST pass
72
+ * through this before serialization.
73
+ */
74
+ export function redactSelfTestText(
75
+ text: string,
76
+ sensitiveValues: readonly string[],
77
+ maxLength: number = SELF_TEST_MAX_TEXT_LENGTH,
78
+ ): string {
79
+ let redacted = text;
80
+ for (const value of sensitiveValues) {
81
+ if (value.length < MIN_SENSITIVE_VALUE_LENGTH) continue;
82
+ redacted = redacted.replace(
83
+ new RegExp(escapeRegExp(value), "g"),
84
+ SELF_TEST_REDACTED_PLACEHOLDER,
85
+ );
86
+ }
87
+ for (const pattern of HEADER_SHAPED_SECRETS) {
88
+ redacted = redacted.replace(
89
+ pattern,
90
+ (_match, prefix: string) => `${prefix}${SELF_TEST_REDACTED_PLACEHOLDER}`,
91
+ );
92
+ }
93
+ if (redacted.length > maxLength) {
94
+ redacted = `${redacted.slice(0, maxLength)}… [truncated]`;
95
+ }
96
+ return redacted;
97
+ }
@@ -0,0 +1,70 @@
1
+ import { createHash, createHmac, timingSafeEqual } from "node:crypto";
2
+
3
+ /**
4
+ * Shared master secret injected into every provider pod. The per-provider
5
+ * bearer token is derived from it, so compromise of one pod's env cannot
6
+ * unlock another provider's self-test endpoint.
7
+ */
8
+ export const PROVIDER_RUNTIME_SELF_TEST_MASTER_SECRET_ENV =
9
+ "APIFUSE__PROVIDER_RUNTIME__SELF_TEST_MASTER_SECRET";
10
+
11
+ /**
12
+ * Previous master secret accepted during a rotation window so fleet-wide
13
+ * rotation does not need to be atomic.
14
+ */
15
+ export const PROVIDER_RUNTIME_SELF_TEST_MASTER_SECRET_PREVIOUS_ENV =
16
+ "APIFUSE__PROVIDER_RUNTIME__SELF_TEST_MASTER_SECRET_PREVIOUS";
17
+
18
+ /** Internal self-test listener port (never the tenant-facing serve() port). */
19
+ export const PROVIDER_RUNTIME_SELF_TEST_PORT_ENV = "APIFUSE__PROVIDER_RUNTIME__SELF_TEST_PORT";
20
+
21
+ export const DEFAULT_SELF_TEST_PORT = 3001;
22
+
23
+ export interface SelfTestMasterSecrets {
24
+ readonly current: string;
25
+ readonly previous?: string;
26
+ }
27
+
28
+ /**
29
+ * Per-provider self-test bearer token: HMAC-SHA256(masterSecret, providerId),
30
+ * hex-encoded. Computed identically by the provider pod and the scheduler.
31
+ */
32
+ export function deriveSelfTestToken(masterSecret: string, providerId: string): string {
33
+ return createHmac("sha256", masterSecret).update(providerId).digest("hex");
34
+ }
35
+
36
+ export function resolveSelfTestMasterSecrets(
37
+ env: Readonly<Record<string, string | undefined>> = process.env,
38
+ ): SelfTestMasterSecrets | undefined {
39
+ const current = env[PROVIDER_RUNTIME_SELF_TEST_MASTER_SECRET_ENV]?.trim();
40
+ if (!current) return undefined;
41
+ const previous = env[PROVIDER_RUNTIME_SELF_TEST_MASTER_SECRET_PREVIOUS_ENV]?.trim();
42
+ return previous ? { current, previous } : { current };
43
+ }
44
+
45
+ function digestsMatch(presented: string, expected: string): boolean {
46
+ // Hash both sides before comparison so timingSafeEqual sees equal-length
47
+ // buffers regardless of what the caller presented (constant-time path).
48
+ const presentedDigest = createHash("sha256").update(presented).digest();
49
+ const expectedDigest = createHash("sha256").update(expected).digest();
50
+ return timingSafeEqual(presentedDigest, expectedDigest);
51
+ }
52
+
53
+ /**
54
+ * Verifies an Authorization header against the current-or-previous derived
55
+ * token (dual acceptance for one rotation window). Constant-time comparison;
56
+ * returns only a boolean so callers cannot leak which check failed.
57
+ */
58
+ export function verifySelfTestAuthorization(
59
+ authorizationHeader: string | undefined,
60
+ providerId: string,
61
+ secrets: SelfTestMasterSecrets,
62
+ ): boolean {
63
+ const match = /^Bearer\s+(\S+)$/i.exec(authorizationHeader ?? "");
64
+ const presented = match?.[1] ?? "";
65
+ const currentOk = digestsMatch(presented, deriveSelfTestToken(secrets.current, providerId));
66
+ const previousOk = secrets.previous
67
+ ? digestsMatch(presented, deriveSelfTestToken(secrets.previous, providerId))
68
+ : false;
69
+ return currentOk || previousOk;
70
+ }