@bleedingdev/modern-js-server-runtime-extensions 3.5.0-ultramodern.30 → 3.5.0-ultramodern.32
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/dist/cjs/contract-gate-snapshot-store/file-store.js +102 -0
- package/dist/cjs/contract-gate-snapshot-store/http-store.js +142 -0
- package/dist/cjs/contract-gate-snapshot-store/index.js +58 -0
- package/dist/cjs/contract-gate-snapshot-store/resolve.js +122 -0
- package/dist/cjs/contract-gate-snapshot-store/types.js +44 -0
- package/dist/cjs/contractGateAutopilot.js +27 -6
- package/dist/cjs/index.js +13 -13
- package/dist/cjs/module-federation-css/collector.js +79 -0
- package/dist/cjs/module-federation-css/index.js +55 -0
- package/dist/cjs/module-federation-css/manifest.js +144 -0
- package/dist/cjs/module-federation-css/plugin.js +71 -0
- package/dist/cjs/module-federation-css/remote.js +133 -0
- package/dist/cjs/runtime-fallback-signal/auth.js +87 -0
- package/dist/cjs/runtime-fallback-signal/constants.js +72 -0
- package/dist/cjs/runtime-fallback-signal/contract-gate.js +64 -0
- package/dist/cjs/runtime-fallback-signal/endpoint.js +50 -0
- package/dist/cjs/runtime-fallback-signal/errors.js +44 -0
- package/dist/cjs/runtime-fallback-signal/index.js +99 -0
- package/dist/cjs/runtime-fallback-signal/payload.js +72 -0
- package/dist/cjs/runtime-fallback-signal/source.js +66 -0
- package/dist/cjs/runtime-fallback-signal/state.js +57 -0
- package/dist/cjs/runtime-fallback-signal/status.js +47 -0
- package/dist/cjs/runtime-fallback-signal/trust-policy.js +108 -0
- package/dist/cjs/runtime-fallback-signal/types.js +18 -0
- package/dist/cjs/runtimeFallbackSignal.js +32 -281
- package/dist/cjs/telemetry/canary.js +180 -0
- package/dist/cjs/telemetry/canaryEvaluation.js +79 -0
- package/dist/cjs/telemetry/canarySetup.js +107 -0
- package/dist/cjs/telemetry/envelope.js +148 -0
- package/dist/cjs/telemetry/exporters.js +148 -0
- package/dist/cjs/telemetry/lifecycle.js +98 -0
- package/dist/cjs/telemetry/metrics.js +115 -0
- package/dist/cjs/telemetry/plugin.js +185 -0
- package/dist/cjs/telemetry/registry.js +348 -0
- package/dist/cjs/telemetry/registryTypes.js +46 -0
- package/dist/cjs/telemetry/runtimeEndpoints.js +135 -0
- package/dist/cjs/telemetry.js +32 -343
- package/dist/cjs/telemetryCore.js +14 -767
- package/dist/esm/contract-gate-snapshot-store/file-store.mjs +51 -0
- package/dist/esm/contract-gate-snapshot-store/http-store.mjs +92 -0
- package/dist/esm/contract-gate-snapshot-store/index.mjs +4 -0
- package/dist/esm/contract-gate-snapshot-store/resolve.mjs +68 -0
- package/dist/esm/contract-gate-snapshot-store/types.mjs +3 -0
- package/dist/esm/contractGateAutopilot.mjs +26 -5
- package/dist/esm/index.mjs +2 -2
- package/dist/esm/module-federation-css/collector.mjs +38 -0
- package/dist/esm/module-federation-css/index.mjs +4 -0
- package/dist/esm/module-federation-css/manifest.mjs +69 -0
- package/dist/esm/module-federation-css/plugin.mjs +33 -0
- package/dist/esm/module-federation-css/remote.mjs +92 -0
- package/dist/esm/runtime-fallback-signal/auth.mjs +40 -0
- package/dist/esm/runtime-fallback-signal/constants.mjs +10 -0
- package/dist/esm/runtime-fallback-signal/contract-gate.mjs +26 -0
- package/dist/esm/runtime-fallback-signal/endpoint.mjs +12 -0
- package/dist/esm/runtime-fallback-signal/errors.mjs +6 -0
- package/dist/esm/runtime-fallback-signal/index.mjs +9 -0
- package/dist/esm/runtime-fallback-signal/payload.mjs +31 -0
- package/dist/esm/runtime-fallback-signal/source.mjs +22 -0
- package/dist/esm/runtime-fallback-signal/state.mjs +16 -0
- package/dist/esm/runtime-fallback-signal/status.mjs +9 -0
- package/dist/esm/runtime-fallback-signal/trust-policy.mjs +67 -0
- package/dist/esm/runtime-fallback-signal/types.mjs +0 -0
- package/dist/esm/runtimeFallbackSignal.mjs +1 -222
- package/dist/esm/telemetry/canary.mjs +142 -0
- package/dist/esm/telemetry/canaryEvaluation.mjs +41 -0
- package/dist/esm/telemetry/canarySetup.mjs +69 -0
- package/dist/esm/telemetry/envelope.mjs +101 -0
- package/dist/esm/telemetry/exporters.mjs +104 -0
- package/dist/esm/telemetry/lifecycle.mjs +60 -0
- package/dist/esm/telemetry/metrics.mjs +77 -0
- package/dist/esm/telemetry/plugin.mjs +87 -0
- package/dist/esm/telemetry/registry.mjs +310 -0
- package/dist/esm/telemetry/registryTypes.mjs +8 -0
- package/dist/esm/telemetry/runtimeEndpoints.mjs +94 -0
- package/dist/esm/telemetry.mjs +1 -275
- package/dist/esm/telemetryCore.mjs +6 -759
- package/dist/esm-node/contract-gate-snapshot-store/file-store.mjs +52 -0
- package/dist/esm-node/contract-gate-snapshot-store/http-store.mjs +93 -0
- package/dist/esm-node/contract-gate-snapshot-store/index.mjs +5 -0
- package/dist/esm-node/contract-gate-snapshot-store/resolve.mjs +70 -0
- package/dist/esm-node/contract-gate-snapshot-store/types.mjs +4 -0
- package/dist/esm-node/contractGateAutopilot.mjs +26 -5
- package/dist/esm-node/index.mjs +2 -2
- package/dist/esm-node/module-federation-css/collector.mjs +39 -0
- package/dist/esm-node/module-federation-css/index.mjs +5 -0
- package/dist/esm-node/module-federation-css/manifest.mjs +70 -0
- package/dist/esm-node/module-federation-css/plugin.mjs +34 -0
- package/dist/esm-node/module-federation-css/remote.mjs +93 -0
- package/dist/esm-node/runtime-fallback-signal/auth.mjs +41 -0
- package/dist/esm-node/runtime-fallback-signal/constants.mjs +11 -0
- package/dist/esm-node/runtime-fallback-signal/contract-gate.mjs +27 -0
- package/dist/esm-node/runtime-fallback-signal/endpoint.mjs +13 -0
- package/dist/esm-node/runtime-fallback-signal/errors.mjs +7 -0
- package/dist/esm-node/runtime-fallback-signal/index.mjs +10 -0
- package/dist/esm-node/runtime-fallback-signal/payload.mjs +32 -0
- package/dist/esm-node/runtime-fallback-signal/source.mjs +23 -0
- package/dist/esm-node/runtime-fallback-signal/state.mjs +17 -0
- package/dist/esm-node/runtime-fallback-signal/status.mjs +10 -0
- package/dist/esm-node/runtime-fallback-signal/trust-policy.mjs +68 -0
- package/dist/esm-node/runtime-fallback-signal/types.mjs +1 -0
- package/dist/esm-node/runtimeFallbackSignal.mjs +1 -222
- package/dist/esm-node/telemetry/canary.mjs +143 -0
- package/dist/esm-node/telemetry/canaryEvaluation.mjs +42 -0
- package/dist/esm-node/telemetry/canarySetup.mjs +70 -0
- package/dist/esm-node/telemetry/envelope.mjs +102 -0
- package/dist/esm-node/telemetry/exporters.mjs +105 -0
- package/dist/esm-node/telemetry/lifecycle.mjs +61 -0
- package/dist/esm-node/telemetry/metrics.mjs +78 -0
- package/dist/esm-node/telemetry/plugin.mjs +88 -0
- package/dist/esm-node/telemetry/registry.mjs +311 -0
- package/dist/esm-node/telemetry/registryTypes.mjs +9 -0
- package/dist/esm-node/telemetry/runtimeEndpoints.mjs +95 -0
- package/dist/esm-node/telemetry.mjs +1 -275
- package/dist/esm-node/telemetryCore.mjs +6 -759
- package/dist/types/contract-gate-snapshot-store/file-store.d.ts +3 -0
- package/dist/types/contract-gate-snapshot-store/http-store.d.ts +11 -0
- package/dist/types/contract-gate-snapshot-store/index.d.ts +5 -0
- package/dist/types/contract-gate-snapshot-store/resolve.d.ts +9 -0
- package/dist/types/{contractGateSnapshotStore.d.ts → contract-gate-snapshot-store/types.d.ts} +1 -11
- package/dist/types/contractGateAutopilot.d.ts +4 -2
- package/dist/types/index.d.ts +2 -2
- package/dist/types/module-federation-css/collector.d.ts +25 -0
- package/dist/types/module-federation-css/index.d.ts +8 -0
- package/dist/types/module-federation-css/manifest.d.ts +33 -0
- package/dist/types/module-federation-css/plugin.d.ts +23 -0
- package/dist/types/module-federation-css/remote.d.ts +20 -0
- package/dist/types/runtime-fallback-signal/auth.d.ts +18 -0
- package/dist/types/runtime-fallback-signal/constants.d.ts +9 -0
- package/dist/types/runtime-fallback-signal/contract-gate.d.ts +2 -0
- package/dist/types/runtime-fallback-signal/endpoint.d.ts +1 -0
- package/dist/types/runtime-fallback-signal/errors.d.ts +2 -0
- package/dist/types/runtime-fallback-signal/index.d.ts +10 -0
- package/dist/types/runtime-fallback-signal/payload.d.ts +6 -0
- package/dist/types/runtime-fallback-signal/source.d.ts +3 -0
- package/dist/types/runtime-fallback-signal/state.d.ts +3 -0
- package/dist/types/runtime-fallback-signal/status.d.ts +2 -0
- package/dist/types/runtime-fallback-signal/trust-policy.d.ts +13 -0
- package/dist/types/runtime-fallback-signal/types.d.ts +50 -0
- package/dist/types/runtimeFallbackSignal.d.ts +1 -94
- package/dist/types/telemetry/canary.d.ts +85 -0
- package/dist/types/telemetry/canaryEvaluation.d.ts +19 -0
- package/dist/types/telemetry/canarySetup.d.ts +17 -0
- package/dist/types/telemetry/envelope.d.ts +45 -0
- package/dist/types/telemetry/exporters.d.ts +12 -0
- package/dist/types/telemetry/lifecycle.d.ts +18 -0
- package/dist/types/telemetry/metrics.d.ts +3 -0
- package/dist/types/telemetry/plugin.d.ts +12 -0
- package/dist/types/telemetry/registry.d.ts +72 -0
- package/dist/types/telemetry/registryTypes.d.ts +50 -0
- package/dist/types/telemetry/runtimeEndpoints.d.ts +35 -0
- package/dist/types/telemetry.d.ts +1 -12
- package/dist/types/telemetryCore.d.ts +6 -257
- package/package.json +7 -7
- package/src/contract-gate-snapshot-store/file-store.ts +85 -0
- package/src/contract-gate-snapshot-store/http-store.ts +159 -0
- package/src/contract-gate-snapshot-store/index.ts +20 -0
- package/src/contract-gate-snapshot-store/resolve.ts +175 -0
- package/src/contract-gate-snapshot-store/types.ts +64 -0
- package/src/contractGateAutopilot.ts +45 -9
- package/src/index.ts +6 -6
- package/src/module-federation-css/collector.ts +82 -0
- package/src/module-federation-css/index.ts +14 -0
- package/src/module-federation-css/manifest.ts +166 -0
- package/src/module-federation-css/plugin.ts +86 -0
- package/src/module-federation-css/remote.ts +165 -0
- package/src/runtime-fallback-signal/auth.ts +117 -0
- package/src/runtime-fallback-signal/constants.ts +19 -0
- package/src/runtime-fallback-signal/contract-gate.ts +43 -0
- package/src/runtime-fallback-signal/endpoint.ts +22 -0
- package/src/runtime-fallback-signal/errors.ts +10 -0
- package/src/runtime-fallback-signal/index.ts +36 -0
- package/src/runtime-fallback-signal/payload.ts +79 -0
- package/src/runtime-fallback-signal/source.ts +49 -0
- package/src/runtime-fallback-signal/state.ts +34 -0
- package/src/runtime-fallback-signal/status.ts +22 -0
- package/src/runtime-fallback-signal/trust-policy.ts +179 -0
- package/src/runtime-fallback-signal/types.ts +66 -0
- package/src/runtimeFallbackSignal.ts +1 -584
- package/src/telemetry/canary.ts +291 -0
- package/src/telemetry/canaryEvaluation.ts +83 -0
- package/src/telemetry/canarySetup.ts +160 -0
- package/src/telemetry/envelope.ts +189 -0
- package/src/telemetry/exporters.ts +165 -0
- package/src/telemetry/lifecycle.ts +138 -0
- package/src/telemetry/metrics.ts +141 -0
- package/src/telemetry/plugin.ts +197 -0
- package/src/telemetry/registry.ts +467 -0
- package/src/telemetry/registryTypes.ts +63 -0
- package/src/telemetry/runtimeEndpoints.ts +172 -0
- package/src/telemetry.ts +1 -554
- package/src/telemetryCore.ts +34 -1332
- package/tests/contractGateAutopilot.test.ts +205 -0
- package/tests/contractGateSnapshotStore.test.ts +54 -1
- package/tests/helpers.ts +1 -0
- package/tests/moduleFederationCss.test.ts +1 -1
- package/tests/registration.test.ts +1 -1
- package/tests/telemetry.test.ts +40 -0
- package/tests/telemetryLifecycle.test.ts +253 -2
- package/dist/cjs/contractGateSnapshotStore.js +0 -253
- package/dist/cjs/moduleFederationCss.js +0 -285
- package/dist/esm/contractGateSnapshotStore.mjs +0 -190
- package/dist/esm/moduleFederationCss.mjs +0 -225
- package/dist/esm-node/contractGateSnapshotStore.mjs +0 -192
- package/dist/esm-node/moduleFederationCss.mjs +0 -226
- package/dist/types/moduleFederationCss.d.ts +0 -87
- package/src/contractGateSnapshotStore.ts +0 -420
- package/src/moduleFederationCss.ts +0 -473
|
@@ -1,420 +0,0 @@
|
|
|
1
|
-
import { createRequire } from 'node:module';
|
|
2
|
-
import { fs } from '@modern-js/utils';
|
|
3
|
-
import { promises as nodeFs } from 'fs';
|
|
4
|
-
import path from 'path';
|
|
5
|
-
import { parseServerRuntimeExtensionsEnv } from './env';
|
|
6
|
-
|
|
7
|
-
export const CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION = 1;
|
|
8
|
-
export const DEFAULT_CONTRACT_GATE_SNAPSHOT_PATH =
|
|
9
|
-
'.modern/contract-gates.json';
|
|
10
|
-
|
|
11
|
-
export type GateSnapshotGateValue =
|
|
12
|
-
| boolean
|
|
13
|
-
| {
|
|
14
|
-
passed?: boolean;
|
|
15
|
-
reason?: string;
|
|
16
|
-
updatedAt?: number;
|
|
17
|
-
expiresAt?: number;
|
|
18
|
-
[key: string]: unknown;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export type GateSnapshot = {
|
|
22
|
-
schemaVersion?: number;
|
|
23
|
-
updatedAt?: number;
|
|
24
|
-
gates?: Record<string, GateSnapshotGateValue>;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
type LoggerLike = {
|
|
28
|
-
info?: (message: string) => void;
|
|
29
|
-
warn?: (message: string) => void;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export type ContractGateSnapshotStore = {
|
|
33
|
-
name: string;
|
|
34
|
-
readSnapshot: () => Promise<GateSnapshot | undefined>;
|
|
35
|
-
writeSnapshot: (snapshot: GateSnapshot) => Promise<void>;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
export type ContractGateSnapshotStoreFactoryContext = {
|
|
39
|
-
appDirectory: string;
|
|
40
|
-
gateSnapshotPath: string;
|
|
41
|
-
options?: Record<string, unknown>;
|
|
42
|
-
logger?: LoggerLike;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
export type ContractGateSnapshotStoreFactory = (
|
|
46
|
-
context: ContractGateSnapshotStoreFactoryContext,
|
|
47
|
-
) => Promise<ContractGateSnapshotStore> | ContractGateSnapshotStore;
|
|
48
|
-
|
|
49
|
-
export type ContractGateSnapshotStoreModule = {
|
|
50
|
-
createContractGateSnapshotStore?: ContractGateSnapshotStoreFactory;
|
|
51
|
-
default?:
|
|
52
|
-
| ContractGateSnapshotStoreFactory
|
|
53
|
-
| {
|
|
54
|
-
createContractGateSnapshotStore?: ContractGateSnapshotStoreFactory;
|
|
55
|
-
};
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
export type ContractGateSnapshotStoreUserConfig = {
|
|
59
|
-
module: string;
|
|
60
|
-
options?: Record<string, unknown>;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
export type ContractGateSnapshotHttpStoreOptions = {
|
|
64
|
-
endpoint: string;
|
|
65
|
-
readMethod?: string;
|
|
66
|
-
writeMethod?: string;
|
|
67
|
-
headers?: Record<string, string>;
|
|
68
|
-
timeoutMs?: number;
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
const DEFAULT_HTTP_STORE_TIMEOUT_MS = 5_000;
|
|
72
|
-
const BUILTIN_HTTP_STATE_STORE_MODULES = new Set([
|
|
73
|
-
'http',
|
|
74
|
-
// historical aliases kept for config compatibility with the era when these
|
|
75
|
-
// modules lived inside @modern-js/server-core.
|
|
76
|
-
'@modern-js/server-core/http',
|
|
77
|
-
'@modern-js/server-core/contract-gate-http-store',
|
|
78
|
-
'@modern-js/server-runtime-extensions/http',
|
|
79
|
-
'@modern-js/server-runtime-extensions/contract-gate-http-store',
|
|
80
|
-
]);
|
|
81
|
-
|
|
82
|
-
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
83
|
-
typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
84
|
-
|
|
85
|
-
const normalizeSnapshot = (snapshot: unknown): GateSnapshot | undefined => {
|
|
86
|
-
if (!isRecord(snapshot)) {
|
|
87
|
-
return undefined;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const schemaVersion =
|
|
91
|
-
typeof snapshot.schemaVersion === 'number'
|
|
92
|
-
? snapshot.schemaVersion
|
|
93
|
-
: CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION;
|
|
94
|
-
const updatedAt =
|
|
95
|
-
typeof snapshot.updatedAt === 'number' ? snapshot.updatedAt : Date.now();
|
|
96
|
-
const gates = isRecord(snapshot.gates)
|
|
97
|
-
? (snapshot.gates as Record<string, GateSnapshotGateValue>)
|
|
98
|
-
: {};
|
|
99
|
-
|
|
100
|
-
return {
|
|
101
|
-
schemaVersion,
|
|
102
|
-
updatedAt,
|
|
103
|
-
gates,
|
|
104
|
-
};
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
const normalizeHttpStoreOptions = (
|
|
108
|
-
options: Record<string, unknown> | undefined,
|
|
109
|
-
): ContractGateSnapshotHttpStoreOptions => {
|
|
110
|
-
const endpoint =
|
|
111
|
-
typeof options?.endpoint === 'string' ? options.endpoint.trim() : '';
|
|
112
|
-
if (!endpoint) {
|
|
113
|
-
throw new Error(
|
|
114
|
-
'[telemetry.canary.autopilot] HTTP stateStore requires options.endpoint',
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const readMethod =
|
|
119
|
-
typeof options?.readMethod === 'string' && options.readMethod.trim()
|
|
120
|
-
? options.readMethod.trim().toUpperCase()
|
|
121
|
-
: 'GET';
|
|
122
|
-
const writeMethod =
|
|
123
|
-
typeof options?.writeMethod === 'string' && options.writeMethod.trim()
|
|
124
|
-
? options.writeMethod.trim().toUpperCase()
|
|
125
|
-
: 'PUT';
|
|
126
|
-
|
|
127
|
-
const timeoutMsRaw = Number(options?.timeoutMs);
|
|
128
|
-
const timeoutMs =
|
|
129
|
-
Number.isFinite(timeoutMsRaw) && timeoutMsRaw > 0
|
|
130
|
-
? Math.floor(timeoutMsRaw)
|
|
131
|
-
: DEFAULT_HTTP_STORE_TIMEOUT_MS;
|
|
132
|
-
|
|
133
|
-
const headersRaw = options?.headers;
|
|
134
|
-
const headers: Record<string, string> = {};
|
|
135
|
-
if (
|
|
136
|
-
headersRaw &&
|
|
137
|
-
typeof headersRaw === 'object' &&
|
|
138
|
-
!Array.isArray(headersRaw)
|
|
139
|
-
) {
|
|
140
|
-
Object.entries(headersRaw).forEach(([key, value]) => {
|
|
141
|
-
if (typeof key === 'string' && key.trim().length > 0 && value != null) {
|
|
142
|
-
headers[key] = String(value);
|
|
143
|
-
}
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
return {
|
|
148
|
-
endpoint,
|
|
149
|
-
readMethod,
|
|
150
|
-
writeMethod,
|
|
151
|
-
headers,
|
|
152
|
-
timeoutMs,
|
|
153
|
-
};
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
const withTimeoutAbort = (timeoutMs: number) => {
|
|
157
|
-
const controller = new AbortController();
|
|
158
|
-
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
159
|
-
return {
|
|
160
|
-
signal: controller.signal,
|
|
161
|
-
clear: () => clearTimeout(timer),
|
|
162
|
-
};
|
|
163
|
-
};
|
|
164
|
-
|
|
165
|
-
export const createHttpContractGateSnapshotStore = (
|
|
166
|
-
options: ContractGateSnapshotHttpStoreOptions,
|
|
167
|
-
): ContractGateSnapshotStore => {
|
|
168
|
-
const normalized = normalizeHttpStoreOptions(
|
|
169
|
-
options as unknown as Record<string, unknown>,
|
|
170
|
-
);
|
|
171
|
-
const endpoint = normalized.endpoint;
|
|
172
|
-
|
|
173
|
-
return {
|
|
174
|
-
name: `http:${endpoint}`,
|
|
175
|
-
async readSnapshot() {
|
|
176
|
-
const { signal, clear } = withTimeoutAbort(normalized.timeoutMs || 5_000);
|
|
177
|
-
try {
|
|
178
|
-
const response = await fetch(endpoint, {
|
|
179
|
-
method: normalized.readMethod || 'GET',
|
|
180
|
-
headers: {
|
|
181
|
-
accept: 'application/json',
|
|
182
|
-
...(normalized.headers || {}),
|
|
183
|
-
},
|
|
184
|
-
signal,
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
if (response.status === 404) {
|
|
188
|
-
return undefined;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
if (!response.ok) {
|
|
192
|
-
throw new Error(
|
|
193
|
-
`HTTP stateStore read failed with status ${String(response.status)}`,
|
|
194
|
-
);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
const payload = await response.json();
|
|
198
|
-
return normalizeSnapshot(payload);
|
|
199
|
-
} finally {
|
|
200
|
-
clear();
|
|
201
|
-
}
|
|
202
|
-
},
|
|
203
|
-
async writeSnapshot(snapshot) {
|
|
204
|
-
const body = JSON.stringify(
|
|
205
|
-
normalizeSnapshot(snapshot) || {
|
|
206
|
-
schemaVersion: CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION,
|
|
207
|
-
updatedAt: Date.now(),
|
|
208
|
-
gates: {},
|
|
209
|
-
},
|
|
210
|
-
);
|
|
211
|
-
const { signal, clear } = withTimeoutAbort(normalized.timeoutMs || 5_000);
|
|
212
|
-
try {
|
|
213
|
-
const response = await fetch(endpoint, {
|
|
214
|
-
method: normalized.writeMethod || 'PUT',
|
|
215
|
-
headers: {
|
|
216
|
-
'content-type': 'application/json',
|
|
217
|
-
...(normalized.headers || {}),
|
|
218
|
-
},
|
|
219
|
-
body,
|
|
220
|
-
signal,
|
|
221
|
-
});
|
|
222
|
-
if (!response.ok) {
|
|
223
|
-
throw new Error(
|
|
224
|
-
`HTTP stateStore write failed with status ${String(response.status)}`,
|
|
225
|
-
);
|
|
226
|
-
}
|
|
227
|
-
} finally {
|
|
228
|
-
clear();
|
|
229
|
-
}
|
|
230
|
-
},
|
|
231
|
-
};
|
|
232
|
-
};
|
|
233
|
-
|
|
234
|
-
const tryResolveBuiltinSnapshotStore = (input: {
|
|
235
|
-
stateStore: ContractGateSnapshotStoreUserConfig;
|
|
236
|
-
}): ContractGateSnapshotStore | undefined => {
|
|
237
|
-
const moduleName = input.stateStore.module.trim();
|
|
238
|
-
if (!BUILTIN_HTTP_STATE_STORE_MODULES.has(moduleName)) {
|
|
239
|
-
return undefined;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
return createHttpContractGateSnapshotStore(
|
|
243
|
-
(input.stateStore.options || {}) as ContractGateSnapshotHttpStoreOptions,
|
|
244
|
-
);
|
|
245
|
-
};
|
|
246
|
-
|
|
247
|
-
const pickStoreFactory = (
|
|
248
|
-
mod: ContractGateSnapshotStoreModule,
|
|
249
|
-
): ContractGateSnapshotStoreFactory | undefined => {
|
|
250
|
-
if (typeof mod === 'function') {
|
|
251
|
-
return mod as unknown as ContractGateSnapshotStoreFactory;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
if (typeof mod.createContractGateSnapshotStore === 'function') {
|
|
255
|
-
return mod.createContractGateSnapshotStore;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
if (typeof mod.default === 'function') {
|
|
259
|
-
return mod.default;
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
if (
|
|
263
|
-
mod.default &&
|
|
264
|
-
typeof mod.default === 'object' &&
|
|
265
|
-
typeof mod.default.createContractGateSnapshotStore === 'function'
|
|
266
|
-
) {
|
|
267
|
-
return mod.default.createContractGateSnapshotStore;
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
return undefined;
|
|
271
|
-
};
|
|
272
|
-
|
|
273
|
-
const ensureStoreShape = (
|
|
274
|
-
store: ContractGateSnapshotStore,
|
|
275
|
-
modulePath: string,
|
|
276
|
-
) => {
|
|
277
|
-
if (
|
|
278
|
-
!store ||
|
|
279
|
-
typeof store !== 'object' ||
|
|
280
|
-
typeof store.readSnapshot !== 'function' ||
|
|
281
|
-
typeof store.writeSnapshot !== 'function'
|
|
282
|
-
) {
|
|
283
|
-
throw new Error(
|
|
284
|
-
`Invalid contract gate snapshot store from "${modulePath}". Expected { readSnapshot(), writeSnapshot() }.`,
|
|
285
|
-
);
|
|
286
|
-
}
|
|
287
|
-
};
|
|
288
|
-
|
|
289
|
-
/**
|
|
290
|
-
* Resolves a user-configured stateStore module specifier from the app, not
|
|
291
|
-
* from this framework package: relative paths resolve against appDirectory,
|
|
292
|
-
* and bare package specifiers resolve through the app's own module graph
|
|
293
|
-
* (`createRequire` anchored at the app package.json). Resolving from the
|
|
294
|
-
* framework package breaks app-installed stores under pnpm's strict
|
|
295
|
-
* node_modules layout. Bare specifiers fall back to framework-local
|
|
296
|
-
* resolution for stores installed alongside the framework.
|
|
297
|
-
*/
|
|
298
|
-
const resolveStoreModulePath = (appDirectory: string, modulePath: string) => {
|
|
299
|
-
const normalized = modulePath.trim();
|
|
300
|
-
if (!normalized) {
|
|
301
|
-
throw new Error(
|
|
302
|
-
'Contract gate snapshot stateStore.module must be non-empty',
|
|
303
|
-
);
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
if (path.isAbsolute(normalized)) {
|
|
307
|
-
return normalized;
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
if (normalized.startsWith('.')) {
|
|
311
|
-
return path.resolve(appDirectory, normalized);
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
const appRequire = createRequire(path.join(appDirectory, 'package.json'));
|
|
315
|
-
try {
|
|
316
|
-
return appRequire.resolve(normalized);
|
|
317
|
-
} catch (_error) {
|
|
318
|
-
// Fall back to this package's own resolution so stores installed next to
|
|
319
|
-
// the framework keep working.
|
|
320
|
-
return normalized;
|
|
321
|
-
}
|
|
322
|
-
};
|
|
323
|
-
|
|
324
|
-
export const resolveContractGateSnapshotPath = (
|
|
325
|
-
appDirectory: string,
|
|
326
|
-
configuredPath: string | undefined,
|
|
327
|
-
) => {
|
|
328
|
-
const rawPath =
|
|
329
|
-
configuredPath ||
|
|
330
|
-
parseServerRuntimeExtensionsEnv().contractGatesFile ||
|
|
331
|
-
DEFAULT_CONTRACT_GATE_SNAPSHOT_PATH;
|
|
332
|
-
if (path.isAbsolute(rawPath)) {
|
|
333
|
-
return rawPath;
|
|
334
|
-
}
|
|
335
|
-
return path.resolve(appDirectory, rawPath);
|
|
336
|
-
};
|
|
337
|
-
|
|
338
|
-
export const createFileContractGateSnapshotStore = (
|
|
339
|
-
gateSnapshotPath: string,
|
|
340
|
-
): ContractGateSnapshotStore => {
|
|
341
|
-
const resolvedPath = path.resolve(gateSnapshotPath);
|
|
342
|
-
return {
|
|
343
|
-
name: `file:${resolvedPath}`,
|
|
344
|
-
async readSnapshot() {
|
|
345
|
-
if (!(await fs.pathExists(resolvedPath))) {
|
|
346
|
-
return undefined;
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
try {
|
|
350
|
-
const raw = await nodeFs.readFile(resolvedPath, 'utf8');
|
|
351
|
-
return normalizeSnapshot(JSON.parse(raw));
|
|
352
|
-
} catch (_error) {
|
|
353
|
-
return undefined;
|
|
354
|
-
}
|
|
355
|
-
},
|
|
356
|
-
async writeSnapshot(snapshot) {
|
|
357
|
-
const normalized = normalizeSnapshot(snapshot) || {
|
|
358
|
-
schemaVersion: CONTRACT_GATE_SNAPSHOT_SCHEMA_VERSION,
|
|
359
|
-
updatedAt: Date.now(),
|
|
360
|
-
gates: {},
|
|
361
|
-
};
|
|
362
|
-
await nodeFs.mkdir(path.dirname(resolvedPath), { recursive: true });
|
|
363
|
-
await nodeFs.writeFile(
|
|
364
|
-
resolvedPath,
|
|
365
|
-
`${JSON.stringify(normalized, null, 2)}\n`,
|
|
366
|
-
);
|
|
367
|
-
},
|
|
368
|
-
};
|
|
369
|
-
};
|
|
370
|
-
|
|
371
|
-
export const resolveContractGateSnapshotStore = async (input: {
|
|
372
|
-
appDirectory: string;
|
|
373
|
-
gateSnapshotPath: string;
|
|
374
|
-
stateStore?: ContractGateSnapshotStoreUserConfig;
|
|
375
|
-
logger?: LoggerLike;
|
|
376
|
-
}): Promise<ContractGateSnapshotStore> => {
|
|
377
|
-
const { appDirectory, gateSnapshotPath, stateStore, logger } = input;
|
|
378
|
-
|
|
379
|
-
if (!stateStore?.module) {
|
|
380
|
-
return createFileContractGateSnapshotStore(gateSnapshotPath);
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
const builtinStore = tryResolveBuiltinSnapshotStore({ stateStore });
|
|
384
|
-
if (builtinStore) {
|
|
385
|
-
logger?.info?.(
|
|
386
|
-
`[telemetry.canary.autopilot] using built-in contract gate snapshot store "${builtinStore.name}"`,
|
|
387
|
-
);
|
|
388
|
-
return builtinStore;
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
const modulePath = resolveStoreModulePath(appDirectory, stateStore.module);
|
|
392
|
-
let mod: ContractGateSnapshotStoreModule;
|
|
393
|
-
try {
|
|
394
|
-
// eslint-disable-next-line import/no-dynamic-require,global-require
|
|
395
|
-
mod = require(modulePath) as ContractGateSnapshotStoreModule;
|
|
396
|
-
} catch (error) {
|
|
397
|
-
throw new Error(
|
|
398
|
-
`[telemetry.canary.autopilot] Failed to load stateStore.module "${stateStore.module}" (${modulePath}): ${error instanceof Error ? error.message : String(error)}`,
|
|
399
|
-
);
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
const factory = pickStoreFactory(mod);
|
|
403
|
-
if (!factory) {
|
|
404
|
-
throw new Error(
|
|
405
|
-
`[telemetry.canary.autopilot] stateStore.module "${stateStore.module}" does not export createContractGateSnapshotStore()`,
|
|
406
|
-
);
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
const store = await factory({
|
|
410
|
-
appDirectory,
|
|
411
|
-
gateSnapshotPath,
|
|
412
|
-
options: stateStore.options,
|
|
413
|
-
logger,
|
|
414
|
-
});
|
|
415
|
-
ensureStoreShape(store, modulePath);
|
|
416
|
-
logger?.info?.(
|
|
417
|
-
`[telemetry.canary.autopilot] using contract gate snapshot store "${store.name || modulePath}"`,
|
|
418
|
-
);
|
|
419
|
-
return store;
|
|
420
|
-
};
|