@satilian/plugin-mf 0.0.0-stage → 0.0.0
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 +522 -2
- package/dist/build/bootstrap.d.ts +9 -0
- package/dist/build/bootstrap.js +51 -0
- package/dist/build/build-expose.d.ts +11 -0
- package/dist/build/build-expose.js +142 -0
- package/dist/build/build-release.d.ts +12 -0
- package/dist/build/build-release.js +61 -0
- package/dist/build/build-types.d.ts +7 -0
- package/dist/build/build-types.js +88 -0
- package/dist/build/config.d.ts +14 -0
- package/dist/build/config.js +1 -0
- package/dist/build/format-stats.d.ts +8 -0
- package/dist/build/format-stats.js +35 -0
- package/dist/build/plugin.d.ts +3 -0
- package/dist/build/plugin.js +87 -0
- package/dist/build/resolver.d.ts +1 -0
- package/dist/build/resolver.js +13 -0
- package/dist/build/shared-dependencies.d.ts +2 -0
- package/dist/build/shared-dependencies.js +56 -0
- package/dist/build/sync-types.d.ts +7 -0
- package/dist/build/sync-types.js +202 -0
- package/dist/build/type-cache.d.ts +13 -0
- package/dist/build/type-cache.js +160 -0
- package/dist/build/types-cli.d.ts +2 -0
- package/dist/build/types-cli.js +99 -0
- package/dist/build/types-options.d.ts +11 -0
- package/dist/build/types-options.js +10 -0
- package/dist/client.d.ts +12 -0
- package/dist/client.js +6 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +2 -0
- package/dist/protocol/asset-path.d.ts +1 -0
- package/dist/protocol/asset-path.js +10 -0
- package/dist/protocol/load-error.d.ts +29 -0
- package/dist/protocol/load-error.js +17 -0
- package/dist/protocol/module.d.ts +12 -0
- package/dist/protocol/module.js +1 -0
- package/dist/protocol/network-policy.d.ts +5 -0
- package/dist/protocol/network-policy.js +1 -0
- package/dist/protocol/observability.d.ts +26 -0
- package/dist/protocol/observability.js +1 -0
- package/dist/protocol/preload.d.ts +16 -0
- package/dist/protocol/preload.js +1 -0
- package/dist/protocol/registry.d.ts +17 -0
- package/dist/protocol/registry.js +75 -0
- package/dist/protocol/release-manifest.d.ts +17 -0
- package/dist/protocol/release-manifest.js +29 -0
- package/dist/protocol/remote-manifest.d.ts +13 -0
- package/dist/protocol/remote-manifest.js +27 -0
- package/dist/protocol/shared-dependencies.d.ts +19 -0
- package/dist/protocol/shared-dependencies.js +20 -0
- package/dist/protocol/ssr-releases.d.ts +4 -0
- package/dist/protocol/ssr-releases.js +22 -0
- package/dist/protocol/type-manifest.d.ts +9 -0
- package/dist/protocol/type-manifest.js +19 -0
- package/dist/protocol/types-lock.d.ts +11 -0
- package/dist/protocol/types-lock.js +33 -0
- package/dist/release-manifest.test.d.ts +1 -0
- package/dist/release-manifest.test.js +119 -0
- package/dist/runtime/client/runtime.d.ts +12 -0
- package/dist/runtime/client/runtime.js +115 -0
- package/dist/runtime/client/runtime.js.LICENSE.txt +1 -0
- package/dist/runtime/core/manifest-loader.d.ts +6 -0
- package/dist/runtime/core/manifest-loader.js +29 -0
- package/dist/runtime/core/preload.d.ts +8 -0
- package/dist/runtime/core/preload.js +45 -0
- package/dist/runtime/core/remote-source.d.ts +32 -0
- package/dist/runtime/core/remote-source.js +159 -0
- package/dist/runtime/react/remote.d.ts +8 -0
- package/dist/runtime/react/remote.js +42 -0
- package/dist/runtime/server/cache/config.d.ts +11 -0
- package/dist/runtime/server/cache/config.js +1 -0
- package/dist/runtime/server/cache/disk-cache.d.ts +10 -0
- package/dist/runtime/server/cache/disk-cache.js +172 -0
- package/dist/runtime/server/cache/entry.d.ts +13 -0
- package/dist/runtime/server/cache/entry.js +47 -0
- package/dist/runtime/server/cache/lock.d.ts +3 -0
- package/dist/runtime/server/cache/lock.js +115 -0
- package/dist/runtime/server/cache/paths.d.ts +2 -0
- package/dist/runtime/server/cache/paths.js +17 -0
- package/dist/runtime/server/cache/prune.d.ts +4 -0
- package/dist/runtime/server/cache/prune.js +88 -0
- package/dist/runtime/server/loader.d.ts +11 -0
- package/dist/runtime/server/loader.js +49 -0
- package/dist/runtime/server/runtime.d.ts +18 -0
- package/dist/runtime/server/runtime.js +106 -0
- package/dist/server.d.ts +13 -0
- package/dist/server.js +6 -0
- package/dist/shared.d.ts +2 -0
- package/dist/shared.js +1 -0
- package/dist/sharing/registry.d.ts +13 -0
- package/dist/sharing/registry.js +69 -0
- package/dist/transport/loading.d.ts +16 -0
- package/dist/transport/loading.js +186 -0
- package/dist/transport/network-policy.d.ts +4 -0
- package/dist/transport/network-policy.js +30 -0
- package/dist/transport/observability.d.ts +9 -0
- package/dist/transport/observability.js +111 -0
- package/dist/transport/registry.d.ts +14 -0
- package/dist/transport/registry.js +96 -0
- package/dist/transport/release-loader.d.ts +4 -0
- package/dist/transport/release-loader.js +12 -0
- package/package.json +74 -6
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { RemoteLoadError } from "../protocol/load-error.js";
|
|
2
|
+
import { assertRemoteURL } from "./network-policy.js";
|
|
3
|
+
import { emitLoadingEvent, observeLoading } from "./observability.js";
|
|
4
|
+
const key = Symbol.for("plugin-mf.loading");
|
|
5
|
+
const shared = globalThis;
|
|
6
|
+
const state = shared[key] ??= {
|
|
7
|
+
options: {
|
|
8
|
+
manifestTimeoutMs: 5000,
|
|
9
|
+
moduleTimeoutMs: 15000,
|
|
10
|
+
typesTimeoutMs: 15000,
|
|
11
|
+
retries: 2,
|
|
12
|
+
retryDelayMs: 100,
|
|
13
|
+
circuitBreaker: {
|
|
14
|
+
failureThreshold: 3,
|
|
15
|
+
cooldownMs: 10000
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
circuits: new Map()
|
|
19
|
+
};
|
|
20
|
+
function configureLoading(options) {
|
|
21
|
+
const next = {
|
|
22
|
+
...state.options,
|
|
23
|
+
...options,
|
|
24
|
+
circuitBreaker: {
|
|
25
|
+
...state.options.circuitBreaker,
|
|
26
|
+
...options.circuitBreaker
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
for (const [name, value] of Object.entries({
|
|
30
|
+
...next,
|
|
31
|
+
...next.circuitBreaker
|
|
32
|
+
}))if ("circuitBreaker" !== name) {
|
|
33
|
+
if ("number" != typeof value || !Number.isSafeInteger(value) || value > ("retries" === name ? 10 : 2147483647) || value < ("retries" === name || "retryDelayMs" === name ? 0 : 1)) throw new Error(`[plugin-mf] Invalid loading option: ${name}`);
|
|
34
|
+
}
|
|
35
|
+
state.options = next;
|
|
36
|
+
}
|
|
37
|
+
function loadingPolicy() {
|
|
38
|
+
return state.options;
|
|
39
|
+
}
|
|
40
|
+
function loadError(error, context) {
|
|
41
|
+
return error instanceof RemoteLoadError ? error : new RemoteLoadError(context, "LOAD_FAILED", "Loading failed", {
|
|
42
|
+
cause: error
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
async function withDeadline(context, timeoutMs, callback, parent) {
|
|
46
|
+
const controller = new AbortController();
|
|
47
|
+
const abort = ()=>controller.abort(new RemoteLoadError(context, "ABORTED", "Loading aborted", {
|
|
48
|
+
cause: parent?.reason
|
|
49
|
+
}));
|
|
50
|
+
parent?.addEventListener("abort", abort, {
|
|
51
|
+
once: true
|
|
52
|
+
});
|
|
53
|
+
if (parent?.aborted) abort();
|
|
54
|
+
const timer = setTimeout(()=>controller.abort(new RemoteLoadError(context, "TIMEOUT", `Deadline exceeded (${timeoutMs} ms)`)), timeoutMs);
|
|
55
|
+
let onAbort = ()=>{};
|
|
56
|
+
try {
|
|
57
|
+
const cancelled = new Promise((_, reject)=>{
|
|
58
|
+
onAbort = ()=>reject(controller.signal.reason);
|
|
59
|
+
controller.signal.addEventListener("abort", onAbort, {
|
|
60
|
+
once: true
|
|
61
|
+
});
|
|
62
|
+
if (controller.signal.aborted) onAbort();
|
|
63
|
+
});
|
|
64
|
+
return await Promise.race([
|
|
65
|
+
cancelled,
|
|
66
|
+
Promise.resolve().then(()=>{
|
|
67
|
+
controller.signal.throwIfAborted();
|
|
68
|
+
return callback(controller.signal);
|
|
69
|
+
})
|
|
70
|
+
]);
|
|
71
|
+
} catch (error) {
|
|
72
|
+
throw loadError(error, context);
|
|
73
|
+
} finally{
|
|
74
|
+
clearTimeout(timer);
|
|
75
|
+
controller.signal.removeEventListener("abort", onAbort);
|
|
76
|
+
parent?.removeEventListener("abort", abort);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function loading_transient(error) {
|
|
80
|
+
return error instanceof TypeError || error instanceof RemoteLoadError && ("TIMEOUT" === error.code || "HTTP_ERROR" === error.code && [
|
|
81
|
+
408,
|
|
82
|
+
429,
|
|
83
|
+
500,
|
|
84
|
+
502,
|
|
85
|
+
503,
|
|
86
|
+
504
|
|
87
|
+
].includes(error.status ?? 0));
|
|
88
|
+
}
|
|
89
|
+
async function pause(ms, signal) {
|
|
90
|
+
signal.throwIfAborted();
|
|
91
|
+
await new Promise((resolve, reject)=>{
|
|
92
|
+
const abort = ()=>{
|
|
93
|
+
clearTimeout(timer);
|
|
94
|
+
reject(signal.reason);
|
|
95
|
+
};
|
|
96
|
+
const timer = setTimeout(()=>{
|
|
97
|
+
signal.removeEventListener("abort", abort);
|
|
98
|
+
resolve();
|
|
99
|
+
}, ms);
|
|
100
|
+
signal.addEventListener("abort", abort, {
|
|
101
|
+
once: true
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
async function retryNetwork(callback, signal, retryable = loading_transient, context) {
|
|
106
|
+
const policy = loadingPolicy();
|
|
107
|
+
for(let attempt = 0;; attempt++){
|
|
108
|
+
signal.throwIfAborted();
|
|
109
|
+
try {
|
|
110
|
+
return await callback(attempt);
|
|
111
|
+
} catch (error) {
|
|
112
|
+
if (signal.aborted) throw signal.reason;
|
|
113
|
+
if (attempt >= policy.retries || !retryable(error)) throw error;
|
|
114
|
+
if (context) emitLoadingEvent({
|
|
115
|
+
...context,
|
|
116
|
+
type: "retry",
|
|
117
|
+
operation: "fetch",
|
|
118
|
+
attempt: attempt + 1
|
|
119
|
+
});
|
|
120
|
+
await pause(Math.min(policy.retryDelayMs * 2 ** attempt, 2147483647), signal);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
async function fetchResource(url, context, read, signal, allow404 = false) {
|
|
125
|
+
const policy = loadingPolicy();
|
|
126
|
+
const timeout = "types" === context.stage ? policy.typesTimeoutMs : "module" === context.stage ? policy.moduleTimeoutMs : policy.manifestTimeoutMs;
|
|
127
|
+
return observeLoading(context, "fetch", ()=>withDeadline(context, timeout, (active)=>retryNetwork(async ()=>{
|
|
128
|
+
let target = assertRemoteURL(url, context.remote);
|
|
129
|
+
let response;
|
|
130
|
+
for(let redirects = 0;; redirects++){
|
|
131
|
+
response = await fetch(target, {
|
|
132
|
+
signal: active,
|
|
133
|
+
redirect: "manual"
|
|
134
|
+
});
|
|
135
|
+
if ("opaqueredirect" === response.type) throw new RemoteLoadError(context, "LOAD_FAILED", "Opaque browser redirects are not allowed");
|
|
136
|
+
if (response.status >= 300 && response.status < 400) {
|
|
137
|
+
const location = response.headers.get("location");
|
|
138
|
+
await response.body?.cancel();
|
|
139
|
+
if (!location || redirects >= 5) throw new RemoteLoadError(context, "LOAD_FAILED", "Invalid redirect chain");
|
|
140
|
+
target = assertRemoteURL(new URL(location, target).href, context.remote);
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
assertRemoteURL(response.url || target, context.remote);
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
if (allow404 && 404 === response.status) return void await response.body?.cancel();
|
|
147
|
+
if (!response.ok) {
|
|
148
|
+
await response.body?.cancel();
|
|
149
|
+
throw new RemoteLoadError(context, "HTTP_ERROR", `HTTP ${response.status}`, {
|
|
150
|
+
status: response.status
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
return await read(response);
|
|
154
|
+
}, active, loading_transient, context), signal));
|
|
155
|
+
}
|
|
156
|
+
async function withProvider(context, callback) {
|
|
157
|
+
const policy = loadingPolicy().circuitBreaker;
|
|
158
|
+
const circuit = state.circuits.get(context.remote) ?? {
|
|
159
|
+
failures: 0,
|
|
160
|
+
probing: false
|
|
161
|
+
};
|
|
162
|
+
state.circuits.set(context.remote, circuit);
|
|
163
|
+
const openedAt = circuit.openedAt;
|
|
164
|
+
if (void 0 !== circuit.openedAt) {
|
|
165
|
+
if (circuit.probing || performance.now() - circuit.openedAt < policy.cooldownMs) throw new RemoteLoadError(context, "CIRCUIT_OPEN", "Provider circuit is open");
|
|
166
|
+
circuit.probing = true;
|
|
167
|
+
}
|
|
168
|
+
try {
|
|
169
|
+
const result = await callback();
|
|
170
|
+
if (circuit.openedAt === openedAt) {
|
|
171
|
+
circuit.failures = 0;
|
|
172
|
+
circuit.openedAt = void 0;
|
|
173
|
+
}
|
|
174
|
+
return result;
|
|
175
|
+
} catch (error) {
|
|
176
|
+
const failure = loadError(error, context);
|
|
177
|
+
if (circuit.openedAt === openedAt && "ABORTED" !== failure.code && "CIRCUIT_OPEN" !== failure.code) {
|
|
178
|
+
circuit.failures++;
|
|
179
|
+
if (circuit.failures >= policy.failureThreshold) circuit.openedAt = performance.now();
|
|
180
|
+
}
|
|
181
|
+
throw failure;
|
|
182
|
+
} finally{
|
|
183
|
+
if (void 0 !== openedAt) circuit.probing = false;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
export { configureLoading, fetchResource, loadError, loadingPolicy, retryNetwork, withDeadline, withProvider };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { NetworkOptions } from "../protocol/network-policy.ts";
|
|
2
|
+
export declare function configureNetwork(options: NetworkOptions): void;
|
|
3
|
+
export declare function requiresServerIntegrity(): boolean;
|
|
4
|
+
export declare function assertRemoteURL(value: string, provider: string): string;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const key = Symbol.for("plugin-mf.network-policy");
|
|
2
|
+
const shared = globalThis;
|
|
3
|
+
const policy = shared[key] ??= {};
|
|
4
|
+
function configureNetwork(options) {
|
|
5
|
+
if (options.allowedProtocols?.some((protocol)=>"http:" !== protocol && "https:" !== protocol)) throw new Error("[plugin-mf] Only HTTP(S) protocols may be allowed");
|
|
6
|
+
if (options.allowedOrigins) for (const origin of options.allowedOrigins){
|
|
7
|
+
const url = new URL(origin);
|
|
8
|
+
if (url.origin !== origin || ![
|
|
9
|
+
"http:",
|
|
10
|
+
"https:"
|
|
11
|
+
].includes(url.protocol)) throw new Error(`[plugin-mf] Invalid allowed origin: ${origin}`);
|
|
12
|
+
}
|
|
13
|
+
Object.assign(policy, options);
|
|
14
|
+
}
|
|
15
|
+
function requiresServerIntegrity() {
|
|
16
|
+
return policy.requireServerIntegrity ?? false;
|
|
17
|
+
}
|
|
18
|
+
function assertRemoteURL(value, provider) {
|
|
19
|
+
const url = new URL(value);
|
|
20
|
+
const origins = policy.allowedOrigins ?? [
|
|
21
|
+
new URL(provider).origin
|
|
22
|
+
];
|
|
23
|
+
const protocols = policy.allowedProtocols ?? [
|
|
24
|
+
"http:",
|
|
25
|
+
"https:"
|
|
26
|
+
];
|
|
27
|
+
if (url.username || url.password || !protocols.includes(url.protocol) || !origins.includes(url.origin)) throw new Error(`[plugin-mf] Remote URL is not allowed: ${url.origin}`);
|
|
28
|
+
return url.href;
|
|
29
|
+
}
|
|
30
|
+
export { assertRemoteURL, configureNetwork, requiresServerIntegrity };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type LoadContext } from "../protocol/load-error.ts";
|
|
2
|
+
import type { LoadingEvent, LoadingEventHandler, LoadingMetrics } from "../protocol/observability.ts";
|
|
3
|
+
export declare function configureObservability(handler?: LoadingEventHandler): void;
|
|
4
|
+
export declare function setObservationContext(context: () => {
|
|
5
|
+
requestId?: string;
|
|
6
|
+
}): void;
|
|
7
|
+
export declare function getLoadingMetrics(): LoadingMetrics;
|
|
8
|
+
export declare function emitLoadingEvent(event: LoadingEvent): void;
|
|
9
|
+
export declare function observeLoading<T>(context: LoadContext, operation: LoadingEvent["operation"], callback: () => Promise<T>): Promise<T>;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { RemoteLoadError } from "../protocol/load-error.js";
|
|
2
|
+
const key = Symbol.for("plugin-mf.observability");
|
|
3
|
+
const shared = globalThis;
|
|
4
|
+
const state = shared[key] ??= {
|
|
5
|
+
sequence: 0,
|
|
6
|
+
metrics: {
|
|
7
|
+
starts: 0,
|
|
8
|
+
successes: 0,
|
|
9
|
+
errors: 0,
|
|
10
|
+
cacheHits: 0,
|
|
11
|
+
retries: 0,
|
|
12
|
+
downloadedBytes: 0,
|
|
13
|
+
diskHits: 0,
|
|
14
|
+
diskDownloads: 0,
|
|
15
|
+
prunedBytes: 0
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
function configureObservability(handler) {
|
|
19
|
+
state.handler = handler;
|
|
20
|
+
}
|
|
21
|
+
function setObservationContext(context) {
|
|
22
|
+
state.context = context;
|
|
23
|
+
}
|
|
24
|
+
function getLoadingMetrics() {
|
|
25
|
+
return {
|
|
26
|
+
...state.metrics
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function emitLoadingEvent(event) {
|
|
30
|
+
const metrics = state.metrics;
|
|
31
|
+
switch(event.type){
|
|
32
|
+
case "start":
|
|
33
|
+
metrics.starts++;
|
|
34
|
+
break;
|
|
35
|
+
case "success":
|
|
36
|
+
metrics.successes++;
|
|
37
|
+
break;
|
|
38
|
+
case "error":
|
|
39
|
+
metrics.errors++;
|
|
40
|
+
break;
|
|
41
|
+
case "cache-hit":
|
|
42
|
+
metrics.cacheHits++;
|
|
43
|
+
if ("disk" === event.operation) metrics.diskHits++;
|
|
44
|
+
break;
|
|
45
|
+
case "retry":
|
|
46
|
+
metrics.retries++;
|
|
47
|
+
break;
|
|
48
|
+
case "download":
|
|
49
|
+
metrics.downloadedBytes += event.bytes ?? 0;
|
|
50
|
+
if ("disk" === event.operation) metrics.diskDownloads++;
|
|
51
|
+
break;
|
|
52
|
+
case "prune":
|
|
53
|
+
metrics.prunedBytes += event.bytes ?? 0;
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
if (!state.handler) return;
|
|
57
|
+
let remote = event.remote;
|
|
58
|
+
try {
|
|
59
|
+
const url = new URL(remote);
|
|
60
|
+
remote = ("null" === url.origin ? `${url.protocol}//${url.host}` : url.origin) + url.pathname;
|
|
61
|
+
} catch {
|
|
62
|
+
remote = "unresolved";
|
|
63
|
+
}
|
|
64
|
+
try {
|
|
65
|
+
const result = state.handler(Object.freeze({
|
|
66
|
+
...event,
|
|
67
|
+
remote,
|
|
68
|
+
...state.context?.()
|
|
69
|
+
}));
|
|
70
|
+
if (result) result.catch(()=>console.error("[plugin-mf] Async loading event handler failed"));
|
|
71
|
+
} catch {
|
|
72
|
+
console.error("[plugin-mf] Loading event handler failed");
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
async function observeLoading(context, operation, callback) {
|
|
76
|
+
const start = performance.now();
|
|
77
|
+
const loadId = ++state.sequence;
|
|
78
|
+
emitLoadingEvent({
|
|
79
|
+
...context,
|
|
80
|
+
type: "start",
|
|
81
|
+
operation,
|
|
82
|
+
loadId
|
|
83
|
+
});
|
|
84
|
+
try {
|
|
85
|
+
const result = await callback();
|
|
86
|
+
emitLoadingEvent({
|
|
87
|
+
...context,
|
|
88
|
+
type: "success",
|
|
89
|
+
operation,
|
|
90
|
+
loadId,
|
|
91
|
+
durationMs: performance.now() - start
|
|
92
|
+
});
|
|
93
|
+
return result;
|
|
94
|
+
} catch (error) {
|
|
95
|
+
emitLoadingEvent({
|
|
96
|
+
...context,
|
|
97
|
+
type: "error",
|
|
98
|
+
operation,
|
|
99
|
+
loadId,
|
|
100
|
+
durationMs: performance.now() - start,
|
|
101
|
+
error: error instanceof RemoteLoadError ? {
|
|
102
|
+
code: error.code,
|
|
103
|
+
status: error.status
|
|
104
|
+
} : {
|
|
105
|
+
code: "LOAD_FAILED"
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
throw error;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
export { configureObservability, emitLoadingEvent, getLoadingMetrics, observeLoading, setObservationContext };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type RegistryConfig, type RegistrySnapshot } from "../protocol/registry.ts";
|
|
2
|
+
export type RegistryOptions = {
|
|
3
|
+
remotes?: RegistryConfig;
|
|
4
|
+
url?: string;
|
|
5
|
+
maxAgeMs?: number;
|
|
6
|
+
selectionKey?: string;
|
|
7
|
+
};
|
|
8
|
+
export declare function registerStaticRemotes(remotes: Record<string, string>): void;
|
|
9
|
+
export declare function configureRegistry(options: RegistryOptions): void;
|
|
10
|
+
export declare function getRegistrySnapshot(selectionKey?: string): RegistrySnapshot;
|
|
11
|
+
export declare function getRegistryOverrides(selectionKey?: string): RegistrySnapshot;
|
|
12
|
+
export declare function refreshRegistry(options?: {
|
|
13
|
+
force?: boolean;
|
|
14
|
+
}): Promise<boolean>;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { parseRegistry, selectRegistry } from "../protocol/registry.js";
|
|
2
|
+
import { fetchResource } from "./loading.js";
|
|
3
|
+
import { assertRemoteURL } from "./network-policy.js";
|
|
4
|
+
const key = Symbol.for("plugin-mf.runtime-registry");
|
|
5
|
+
const shared = globalThis;
|
|
6
|
+
const state = shared[key] ??= {
|
|
7
|
+
static: {},
|
|
8
|
+
local: {},
|
|
9
|
+
remote: {},
|
|
10
|
+
options: {}
|
|
11
|
+
};
|
|
12
|
+
function registerStaticRemotes(remotes) {
|
|
13
|
+
state.static = {
|
|
14
|
+
...state.static,
|
|
15
|
+
...parseRegistry(Object.fromEntries(Object.entries(remotes).map(([name, url])=>[
|
|
16
|
+
name,
|
|
17
|
+
{
|
|
18
|
+
url
|
|
19
|
+
}
|
|
20
|
+
])))
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function configureRegistry(options) {
|
|
24
|
+
if (void 0 !== options.maxAgeMs && (!Number.isSafeInteger(options.maxAgeMs) || options.maxAgeMs < 0)) throw new Error("[plugin-mf] Invalid registry maxAgeMs");
|
|
25
|
+
if (options.url) assertRemoteURL(options.url, options.url);
|
|
26
|
+
if (options.remotes) state.local = parseRegistry(structuredClone(options.remotes));
|
|
27
|
+
if (void 0 !== options.url && options.url !== state.options.url) {
|
|
28
|
+
state.refreshed = void 0;
|
|
29
|
+
state.pending = void 0;
|
|
30
|
+
}
|
|
31
|
+
state.options = {
|
|
32
|
+
...state.options,
|
|
33
|
+
...options
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function snapshotFor(config, selectionKey) {
|
|
37
|
+
const snapshot = selectRegistry(config, selectionKey);
|
|
38
|
+
for (const entry of Object.values(snapshot)){
|
|
39
|
+
assertRemoteURL(entry.url, entry.url);
|
|
40
|
+
if (entry.release) assertRemoteURL(entry.release.baseUrl, entry.url);
|
|
41
|
+
}
|
|
42
|
+
return structuredClone(snapshot);
|
|
43
|
+
}
|
|
44
|
+
function getRegistrySnapshot(selectionKey = state.options.selectionKey ?? "") {
|
|
45
|
+
return snapshotFor({
|
|
46
|
+
...state.static,
|
|
47
|
+
...state.local,
|
|
48
|
+
...state.remote
|
|
49
|
+
}, selectionKey);
|
|
50
|
+
}
|
|
51
|
+
function getRegistryOverrides(selectionKey = state.options.selectionKey ?? "") {
|
|
52
|
+
return snapshotFor({
|
|
53
|
+
...state.local,
|
|
54
|
+
...state.remote
|
|
55
|
+
}, selectionKey);
|
|
56
|
+
}
|
|
57
|
+
async function refreshRegistry(options = {}) {
|
|
58
|
+
const url = state.options.url;
|
|
59
|
+
if (!url) return true;
|
|
60
|
+
if (state.pending) return state.pending;
|
|
61
|
+
if (!options.force && void 0 !== state.refreshed && performance.now() - state.refreshed < (state.options.maxAgeMs ?? 30000)) return state.lastResult ?? true;
|
|
62
|
+
const pending = (async ()=>{
|
|
63
|
+
try {
|
|
64
|
+
const config = await fetchResource(url, {
|
|
65
|
+
remote: url,
|
|
66
|
+
stage: "manifest"
|
|
67
|
+
}, async (response)=>parseRegistry(await response.json()));
|
|
68
|
+
for (const entry of Object.values(config)){
|
|
69
|
+
assertRemoteURL(entry.url, entry.url);
|
|
70
|
+
if (entry.release) assertRemoteURL(entry.release.baseUrl, entry.url);
|
|
71
|
+
if (entry.canary) {
|
|
72
|
+
assertRemoteURL(entry.canary.url, entry.canary.url);
|
|
73
|
+
if (entry.canary.release) assertRemoteURL(entry.canary.release.baseUrl, entry.canary.url);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (state.options.url !== url) return false;
|
|
77
|
+
state.remote = config;
|
|
78
|
+
state.refreshed = performance.now();
|
|
79
|
+
state.lastResult = true;
|
|
80
|
+
return true;
|
|
81
|
+
} catch {
|
|
82
|
+
if (state.options.url !== url) return false;
|
|
83
|
+
state.refreshed = performance.now();
|
|
84
|
+
state.lastResult = false;
|
|
85
|
+
console.warn("[plugin-mf] Registry refresh failed; retaining the last successful configuration");
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
})();
|
|
89
|
+
state.pending = pending;
|
|
90
|
+
try {
|
|
91
|
+
return await pending;
|
|
92
|
+
} finally{
|
|
93
|
+
if (state.pending === pending) state.pending = void 0;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
export { configureRegistry, getRegistryOverrides, getRegistrySnapshot, refreshRegistry, registerStaticRemotes };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type ResolvedRelease } from "../protocol/release-manifest.ts";
|
|
2
|
+
import type { LoadContext } from "../protocol/load-error.ts";
|
|
3
|
+
/** A missing release manifest is the only signal that a provider uses the legacy layout. */
|
|
4
|
+
export declare function loadReleaseManifest(url: string, context?: LoadContext, signal?: AbortSignal): Promise<ResolvedRelease | undefined>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { parseReleaseManifest } from "../protocol/release-manifest.js";
|
|
2
|
+
import { fetchResource } from "./loading.js";
|
|
3
|
+
async function loadReleaseManifest(url, context = {
|
|
4
|
+
remote: new URL(".", url).href,
|
|
5
|
+
stage: "release-manifest"
|
|
6
|
+
}, signal) {
|
|
7
|
+
return fetchResource(url, context, async (response)=>({
|
|
8
|
+
manifest: parseReleaseManifest(await response.json()),
|
|
9
|
+
baseUrl: new URL(".", response.url).href
|
|
10
|
+
}), signal, true);
|
|
11
|
+
}
|
|
12
|
+
export { loadReleaseManifest };
|
package/package.json
CHANGED
|
@@ -1,6 +1,74 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@satilian/plugin-mf",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@satilian/plugin-mf",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js"
|
|
11
|
+
},
|
|
12
|
+
"./client": {
|
|
13
|
+
"types": "./dist/client.d.ts",
|
|
14
|
+
"import": "./dist/client.js"
|
|
15
|
+
},
|
|
16
|
+
"./server": {
|
|
17
|
+
"types": "./dist/server.d.ts",
|
|
18
|
+
"import": "./dist/server.js"
|
|
19
|
+
},
|
|
20
|
+
"./shared": {
|
|
21
|
+
"types": "./dist/shared.d.ts",
|
|
22
|
+
"import": "./dist/shared.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"bin": {
|
|
27
|
+
"plugin-mf-types": "./dist/build/types-cli.js"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist"
|
|
31
|
+
],
|
|
32
|
+
"sideEffects": false,
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"@rsbuild/core": "^2.2.1",
|
|
38
|
+
"@types/react": "^19.2.18",
|
|
39
|
+
"react": "^19.2.8"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"semver": "^7.8.5",
|
|
43
|
+
"typescript": "^6.0.3"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "rslib",
|
|
47
|
+
"types:sync": "node dist/build/types-cli.js",
|
|
48
|
+
"benchmark:routes": "node scripts/benchmark-routes.mjs",
|
|
49
|
+
"benchmark:scale": "node --expose-gc scripts/scale-workload.mjs",
|
|
50
|
+
"build:shell": "pnpm --filter ./example/shell* run build",
|
|
51
|
+
"build:host": "pnpm --filter ./example/host* run build",
|
|
52
|
+
"build:subhost": "pnpm --filter ./example/sub-host* run build",
|
|
53
|
+
"dev": "rslib --watch",
|
|
54
|
+
"test": "node --experimental-transform-types --test \"src/**/*.test.ts\"",
|
|
55
|
+
"test:watch": "node --experimental-transform-types --test --watch \"src/**/*.test.ts\"",
|
|
56
|
+
"start:subhost": "pnpm build:subhost && pnpm --filter ./example/sub-host* --parallel --stream run server",
|
|
57
|
+
"start:host": "pnpm --filter shell exec wait-on --timeout 120000 --log http-get://localhost:4001/mf/manifest.json && pnpm build:host && pnpm --filter ./example/host* --parallel --stream run server",
|
|
58
|
+
"start:shell": "pnpm --filter shell exec wait-on --timeout 120000 --log http-get://localhost:4001/mf/manifest.json http-get://localhost:3001/mf/manifest.json http-get://localhost:3002/mf/manifest.json && pnpm build:shell && pnpm --filter ./example/shell* --parallel --stream run server",
|
|
59
|
+
"start": "pnpm build && concurrently --kill-others --names subhost,host,shell \"pnpm start:subhost\" \"pnpm start:host\" \"pnpm start:shell\"",
|
|
60
|
+
"typecheck": "tsc --noEmit",
|
|
61
|
+
"lint": "eslint src --max-warnings 0",
|
|
62
|
+
"check": "pnpm typecheck && pnpm lint && pnpm test && pnpm build"
|
|
63
|
+
},
|
|
64
|
+
"devDependencies": {
|
|
65
|
+
"@rsbuild/core": "^2.2.1",
|
|
66
|
+
"@rslib/core": "^0.23.2",
|
|
67
|
+
"@types/node": "^24.13.2",
|
|
68
|
+
"@types/semver": "^7.8.0",
|
|
69
|
+
"concurrently": "^10.0.6",
|
|
70
|
+
"eslint": "^10.13.0",
|
|
71
|
+
"typescript": "^6.0.3",
|
|
72
|
+
"typescript-eslint": "^8.72.1"
|
|
73
|
+
}
|
|
74
|
+
}
|