@satilian/plugin-mf 0.0.0-stage → 1.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,45 @@
|
|
|
1
|
+
async function preload(targets, options = {}) {
|
|
2
|
+
const concurrency = options.concurrency ?? 4;
|
|
3
|
+
for (const [name, value] of Object.entries({
|
|
4
|
+
concurrency,
|
|
5
|
+
maxRemotes: options.maxRemotes,
|
|
6
|
+
maxJSBytes: options.maxJSBytes,
|
|
7
|
+
maxCSSBytes: options.maxCSSBytes
|
|
8
|
+
}))if (void 0 !== value && (!Number.isSafeInteger(value) || value < ("concurrency" === name ? 1 : 0))) throw new Error(`[plugin-mf] Invalid preload budget: ${name}`);
|
|
9
|
+
const sources = new Set(targets.map((target)=>target.source.provider));
|
|
10
|
+
if (void 0 !== options.maxRemotes && sources.size > options.maxRemotes) throw new Error("[plugin-mf] Route remote budget exceeded");
|
|
11
|
+
async function map(callback) {
|
|
12
|
+
const result = [];
|
|
13
|
+
let next = 0;
|
|
14
|
+
const settled = await Promise.allSettled(Array.from({
|
|
15
|
+
length: Math.min(concurrency, targets.length)
|
|
16
|
+
}, async ()=>{
|
|
17
|
+
while(next < targets.length){
|
|
18
|
+
options.signal?.throwIfAborted();
|
|
19
|
+
const index = next++;
|
|
20
|
+
result[index] = await callback(targets[index]);
|
|
21
|
+
}
|
|
22
|
+
}));
|
|
23
|
+
const failures = settled.filter((entry)=>"rejected" === entry.status);
|
|
24
|
+
if (failures.length) throw new AggregateError(failures.map((entry)=>entry.reason), "[plugin-mf] Route preload failed");
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
27
|
+
const inspected = await map((target)=>target.source.inspectExport(target.expose));
|
|
28
|
+
if ((void 0 !== options.maxJSBytes || void 0 !== options.maxCSSBytes) && inspected.some((entry)=>!entry.measured)) throw new Error("[plugin-mf] Route budgets require asset sizes in all provider manifests");
|
|
29
|
+
const assets = new Map(inspected.flatMap((entry)=>entry.assets.map((asset)=>[
|
|
30
|
+
asset.url,
|
|
31
|
+
asset
|
|
32
|
+
])));
|
|
33
|
+
const jsBytes = [
|
|
34
|
+
...assets.values()
|
|
35
|
+
].filter((asset)=>"js" === asset.type).reduce((sum, asset)=>sum + asset.bytes, 0);
|
|
36
|
+
const cssBytes = [
|
|
37
|
+
...assets.values()
|
|
38
|
+
].filter((asset)=>"css" === asset.type).reduce((sum, asset)=>sum + asset.bytes, 0);
|
|
39
|
+
if (void 0 !== options.maxJSBytes && jsBytes > options.maxJSBytes) throw new Error(`[plugin-mf] Route JS budget exceeded: ${jsBytes}`);
|
|
40
|
+
if (void 0 !== options.maxCSSBytes && cssBytes > options.maxCSSBytes) throw new Error(`[plugin-mf] Route CSS budget exceeded: ${cssBytes}`);
|
|
41
|
+
await map(async (target)=>{
|
|
42
|
+
await target.source.loadExport(target.expose);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
export { preload };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { RemoteManifest } from "../../protocol/remote-manifest.ts";
|
|
2
|
+
import type { ResolvedRelease } from "../../protocol/release-manifest.ts";
|
|
3
|
+
import type { ModuleLoader, RemoteScope } from "../../protocol/module.ts";
|
|
4
|
+
import { type LoadContext, type LoadStage } from "../../protocol/load-error.ts";
|
|
5
|
+
import type { RemoteAsset } from "../../protocol/preload.ts";
|
|
6
|
+
type RemoteExport = {
|
|
7
|
+
value: unknown;
|
|
8
|
+
stylesheets: string[];
|
|
9
|
+
};
|
|
10
|
+
export declare class RemoteSource {
|
|
11
|
+
#private;
|
|
12
|
+
private url;
|
|
13
|
+
private moduleLoader;
|
|
14
|
+
private scope;
|
|
15
|
+
private getPinnedRelease?;
|
|
16
|
+
private signal?;
|
|
17
|
+
manifest?: RemoteManifest;
|
|
18
|
+
baseUrl: string;
|
|
19
|
+
releaseId?: string;
|
|
20
|
+
constructor(url: string, moduleLoader: ModuleLoader, scope: RemoteScope, getPinnedRelease?: (() => ResolvedRelease | null | undefined) | undefined, signal?: AbortSignal | undefined);
|
|
21
|
+
protected context(stage: LoadStage): LoadContext;
|
|
22
|
+
get provider(): string;
|
|
23
|
+
inspectExport(name: string): Promise<{
|
|
24
|
+
measured: boolean;
|
|
25
|
+
assets: (RemoteAsset & {
|
|
26
|
+
url: string;
|
|
27
|
+
})[];
|
|
28
|
+
}>;
|
|
29
|
+
getSelectedRelease(): ResolvedRelease | null | undefined;
|
|
30
|
+
loadExport(name: string): Promise<RemoteExport>;
|
|
31
|
+
}
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { ManifestLoader } from "./manifest-loader.js";
|
|
2
|
+
import { loadReleaseManifest } from "../../transport/release-loader.js";
|
|
3
|
+
import { RemoteLoadError } from "../../protocol/load-error.js";
|
|
4
|
+
import { loadingPolicy, withDeadline, withProvider } from "../../transport/loading.js";
|
|
5
|
+
import { assertSharedRequirements } from "../../sharing/registry.js";
|
|
6
|
+
import { assertRemoteURL } from "../../transport/network-policy.js";
|
|
7
|
+
import { emitLoadingEvent, observeLoading } from "../../transport/observability.js";
|
|
8
|
+
class RemoteSource {
|
|
9
|
+
url;
|
|
10
|
+
moduleLoader;
|
|
11
|
+
scope;
|
|
12
|
+
getPinnedRelease;
|
|
13
|
+
signal;
|
|
14
|
+
#manifestLoader = new ManifestLoader();
|
|
15
|
+
manifest;
|
|
16
|
+
baseUrl = "";
|
|
17
|
+
releaseId;
|
|
18
|
+
#release;
|
|
19
|
+
#selectedRelease;
|
|
20
|
+
#exports = new Map();
|
|
21
|
+
constructor(url, moduleLoader, scope, getPinnedRelease, signal){
|
|
22
|
+
this.url = url;
|
|
23
|
+
this.moduleLoader = moduleLoader;
|
|
24
|
+
this.scope = scope;
|
|
25
|
+
this.getPinnedRelease = getPinnedRelease;
|
|
26
|
+
this.signal = signal;
|
|
27
|
+
}
|
|
28
|
+
context(stage) {
|
|
29
|
+
const context = {
|
|
30
|
+
remote: this.url.replace(/\/$/, ""),
|
|
31
|
+
stage
|
|
32
|
+
};
|
|
33
|
+
Object.defineProperty(context, "releaseId", {
|
|
34
|
+
enumerable: true,
|
|
35
|
+
get: ()=>this.releaseId
|
|
36
|
+
});
|
|
37
|
+
return context;
|
|
38
|
+
}
|
|
39
|
+
get provider() {
|
|
40
|
+
return this.url.replace(/\/$/, "");
|
|
41
|
+
}
|
|
42
|
+
async inspectExport(name) {
|
|
43
|
+
return withDeadline(this.context("manifest"), loadingPolicy().moduleTimeoutMs, async (signal)=>{
|
|
44
|
+
if (!this.manifest) await this.#load(signal);
|
|
45
|
+
const info = "client" === this.scope ? {
|
|
46
|
+
manifest: this.manifest,
|
|
47
|
+
__baseUrl: this.baseUrl
|
|
48
|
+
} : await this.#loadManifest("client", this.#selectedRelease, signal);
|
|
49
|
+
const entry = info.manifest?.[name];
|
|
50
|
+
if (!entry) throw new RemoteLoadError(this.context("manifest"), "LOAD_FAILED", `Remote "${name}" not found`);
|
|
51
|
+
return {
|
|
52
|
+
measured: "string" != typeof entry && void 0 !== entry.assets,
|
|
53
|
+
assets: "string" == typeof entry ? [] : (entry.assets ?? []).map((asset)=>({
|
|
54
|
+
...asset,
|
|
55
|
+
url: assertRemoteURL(new URL(asset.path, info.__baseUrl).href, this.url)
|
|
56
|
+
}))
|
|
57
|
+
};
|
|
58
|
+
}, this.signal);
|
|
59
|
+
}
|
|
60
|
+
getSelectedRelease() {
|
|
61
|
+
return this.manifest ? this.#selectedRelease ?? null : void 0;
|
|
62
|
+
}
|
|
63
|
+
async #getRelease(signal) {
|
|
64
|
+
if (!this.#release) {
|
|
65
|
+
const pinned = this.getPinnedRelease?.();
|
|
66
|
+
if (void 0 !== pinned) {
|
|
67
|
+
this.#release = Promise.resolve(pinned ?? void 0);
|
|
68
|
+
return this.#release;
|
|
69
|
+
}
|
|
70
|
+
this.#release = loadReleaseManifest(`${this.url.replace(/\/$/, "")}/manifest.json`, this.context("release-manifest"), signal).catch((error)=>{
|
|
71
|
+
this.#release = void 0;
|
|
72
|
+
throw error;
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
return this.#release;
|
|
76
|
+
}
|
|
77
|
+
async #loadManifest(scope, release, signal) {
|
|
78
|
+
const manifestPath = release?.manifest[scope];
|
|
79
|
+
if (release && !manifestPath) throw new RemoteLoadError(this.context("manifest"), "LOAD_FAILED", `Release does not contain ${scope} assets`);
|
|
80
|
+
const url = release && manifestPath ? new URL(manifestPath, release.baseUrl).href : `${this.url.replace(/\/$/, "")}/${scope}/manifest.json`;
|
|
81
|
+
return this.#manifestLoader.load(url, this.context("manifest"), signal);
|
|
82
|
+
}
|
|
83
|
+
async #load(signal) {
|
|
84
|
+
if ("server" !== this.scope && "client" !== this.scope) throw new Error(`Unsupported remote scope: ${this.scope}`);
|
|
85
|
+
const release = await this.#getRelease(signal);
|
|
86
|
+
this.releaseId = release?.manifest.releaseId;
|
|
87
|
+
this.#selectedRelease = release;
|
|
88
|
+
if (release?.manifest.shared) try {
|
|
89
|
+
assertSharedRequirements(release.manifest.shared[this.scope]);
|
|
90
|
+
} catch (error) {
|
|
91
|
+
throw new RemoteLoadError(this.context("module"), "LOAD_FAILED", error instanceof Error ? error.message : "Incompatible shared dependencies", {
|
|
92
|
+
cause: error
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
const { manifest, __baseUrl } = await this.#loadManifest(this.scope, release, signal);
|
|
96
|
+
this.manifest = manifest;
|
|
97
|
+
this.baseUrl = __baseUrl;
|
|
98
|
+
}
|
|
99
|
+
loadExport(name) {
|
|
100
|
+
const cached = this.#exports.get(name);
|
|
101
|
+
if (cached) {
|
|
102
|
+
emitLoadingEvent({
|
|
103
|
+
...this.context("module"),
|
|
104
|
+
type: "cache-hit",
|
|
105
|
+
operation: "remote"
|
|
106
|
+
});
|
|
107
|
+
return cached;
|
|
108
|
+
}
|
|
109
|
+
const context = this.context("module");
|
|
110
|
+
const promise = observeLoading(context, "remote", ()=>withProvider(context, ()=>withDeadline(context, loadingPolicy().moduleTimeoutMs, (signal)=>this.#loadExport(name, signal), this.signal))).catch((error)=>{
|
|
111
|
+
this.#exports.delete(name);
|
|
112
|
+
if (error instanceof RemoteLoadError && !error.releaseId && this.releaseId) throw new RemoteLoadError(this.context(error.stage), error.code, "Loading failed", {
|
|
113
|
+
cause: error,
|
|
114
|
+
status: error.status
|
|
115
|
+
});
|
|
116
|
+
throw error;
|
|
117
|
+
});
|
|
118
|
+
this.#exports.set(name, promise);
|
|
119
|
+
return promise;
|
|
120
|
+
}
|
|
121
|
+
async #loadExport(name, signal) {
|
|
122
|
+
if (!this.manifest) await this.#load(signal);
|
|
123
|
+
signal.throwIfAborted();
|
|
124
|
+
const path = this.manifest?.[name];
|
|
125
|
+
if (!path) throw new RemoteLoadError(this.context("manifest"), "LOAD_FAILED", `Remote "${name}" not found in manifest`);
|
|
126
|
+
const url = new URL("string" == typeof path ? path : path.js, this.baseUrl).href;
|
|
127
|
+
assertRemoteURL(url, this.url);
|
|
128
|
+
const module = await this.moduleLoader.load(url, {
|
|
129
|
+
provider: this.url.replace(/\/$/, ""),
|
|
130
|
+
releaseId: this.releaseId,
|
|
131
|
+
baseUrl: this.baseUrl,
|
|
132
|
+
files: "string" == typeof path ? [
|
|
133
|
+
path
|
|
134
|
+
] : path.files ?? [
|
|
135
|
+
path.js
|
|
136
|
+
],
|
|
137
|
+
integrity: "string" == typeof path ? void 0 : path.integrity
|
|
138
|
+
});
|
|
139
|
+
const component = module[name];
|
|
140
|
+
if (!component) throw new RemoteLoadError(this.context("module"), "LOAD_FAILED", `Export "${name}" not found`);
|
|
141
|
+
let css = "string" == typeof path ? [] : path.css;
|
|
142
|
+
let cssBaseUrl = this.baseUrl;
|
|
143
|
+
if ("server" === this.scope && "string" != typeof path) {
|
|
144
|
+
signal.throwIfAborted();
|
|
145
|
+
const client = await this.#loadManifest("client", this.#selectedRelease, signal);
|
|
146
|
+
const entry = client.manifest[name];
|
|
147
|
+
if (!entry) throw new RemoteLoadError(this.context("manifest"), "LOAD_FAILED", `Remote "${name}" not found in client manifest`);
|
|
148
|
+
css = "string" == typeof entry ? [] : entry.css;
|
|
149
|
+
cssBaseUrl = client.__baseUrl;
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
value: component,
|
|
153
|
+
stylesheets: [
|
|
154
|
+
...new Set(css.map((asset)=>assertRemoteURL(new URL(asset, cssBaseUrl).href, this.url)))
|
|
155
|
+
]
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
export { RemoteSource };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type ComponentType, type LazyExoticComponent } from "react";
|
|
2
|
+
import { RemoteSource } from "../core/remote-source.ts";
|
|
3
|
+
export declare class Remote extends RemoteSource {
|
|
4
|
+
#private;
|
|
5
|
+
retry(name: string): void;
|
|
6
|
+
component<Props extends object = Record<string, unknown>>(name: string): Promise<ComponentType<Props>>;
|
|
7
|
+
lazyComponent<Props extends object = Record<string, unknown>>(name: string): LazyExoticComponent<ComponentType<Props>>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Fragment, createElement, lazy } from "react";
|
|
2
|
+
import { RemoteSource } from "../core/remote-source.js";
|
|
3
|
+
import { RemoteLoadError } from "../../protocol/load-error.js";
|
|
4
|
+
class Remote extends RemoteSource {
|
|
5
|
+
#lazyComponents = new Map();
|
|
6
|
+
#failedComponents = new Set();
|
|
7
|
+
retry(name) {
|
|
8
|
+
if (!this.#failedComponents.delete(name)) return;
|
|
9
|
+
this.#lazyComponents.delete(name);
|
|
10
|
+
}
|
|
11
|
+
async component(name) {
|
|
12
|
+
const { value, stylesheets } = await this.loadExport(name);
|
|
13
|
+
if ("function" != typeof value && ("object" != typeof value || null === value || !("$$typeof" in value))) throw new RemoteLoadError(this.context("module"), "LOAD_FAILED", `Export "${name}" is not a React component`);
|
|
14
|
+
const Component = value;
|
|
15
|
+
if (!stylesheets.length) return Component;
|
|
16
|
+
return function(props) {
|
|
17
|
+
return createElement(Fragment, null, ...stylesheets.map((href)=>createElement("link", {
|
|
18
|
+
key: href,
|
|
19
|
+
rel: "stylesheet",
|
|
20
|
+
href,
|
|
21
|
+
precedence: "mf"
|
|
22
|
+
})), createElement(Component, props));
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
lazyComponent(name) {
|
|
26
|
+
const cached = this.#lazyComponents.get(name);
|
|
27
|
+
if (cached) return cached;
|
|
28
|
+
const component = lazy(async ()=>{
|
|
29
|
+
try {
|
|
30
|
+
return {
|
|
31
|
+
default: await this.component(name)
|
|
32
|
+
};
|
|
33
|
+
} catch (error) {
|
|
34
|
+
this.#failedComponents.add(name);
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
this.#lazyComponents.set(name, component);
|
|
39
|
+
return component;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export { Remote };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { CacheEntry } from "./entry.ts";
|
|
2
|
+
import type { ServerCacheOptions, CachePruneResult } from "./config.ts";
|
|
3
|
+
import type { LoadContext } from "../../../protocol/load-error.ts";
|
|
4
|
+
export declare class DiskCache {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(options?: string | ServerCacheOptions);
|
|
7
|
+
resolve(entry: CacheEntry, context: LoadContext, deadlineSignal: AbortSignal): Promise<string>;
|
|
8
|
+
pruneCache(): Promise<CachePruneResult>;
|
|
9
|
+
clearCache(): Promise<CachePruneResult>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { lstat, mkdir, mkdtemp, open as promises_open, readFile, realpath, rename, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import node_path from "node:path";
|
|
4
|
+
import { hasCode, withCacheLock } from "./lock.js";
|
|
5
|
+
import { inside, regularFile } from "./paths.js";
|
|
6
|
+
import { entrySize, isPinned, prune } from "./prune.js";
|
|
7
|
+
import { fetchResource } from "../../../transport/loading.js";
|
|
8
|
+
import { emitLoadingEvent } from "../../../transport/observability.js";
|
|
9
|
+
const DEFAULT_MAX_BYTES = 536870912;
|
|
10
|
+
class DiskCache {
|
|
11
|
+
#root;
|
|
12
|
+
#cacheDir;
|
|
13
|
+
#maxBytes;
|
|
14
|
+
#lockTimeoutMs;
|
|
15
|
+
constructor(options = {}){
|
|
16
|
+
const config = "string" == typeof options ? {
|
|
17
|
+
cacheDir: options
|
|
18
|
+
} : options;
|
|
19
|
+
this.#cacheDir = node_path.resolve(config.cacheDir ?? process.env.MF_CACHE_DIR ?? node_path.join(process.cwd(), "node_modules/.cache/mf"));
|
|
20
|
+
this.#maxBytes = config.maxBytes ?? (process.env.MF_CACHE_MAX_BYTES ? Number(process.env.MF_CACHE_MAX_BYTES) : DEFAULT_MAX_BYTES);
|
|
21
|
+
this.#lockTimeoutMs = config.lockTimeoutMs ?? 30000;
|
|
22
|
+
if (!Number.isSafeInteger(this.#maxBytes) || this.#maxBytes <= 0 || !Number.isSafeInteger(this.#lockTimeoutMs) || this.#lockTimeoutMs <= 0) throw new Error("[plugin-mf] Cache maxBytes and lockTimeoutMs must be positive safe integers");
|
|
23
|
+
}
|
|
24
|
+
async #initialize() {
|
|
25
|
+
await mkdir(this.#cacheDir, {
|
|
26
|
+
recursive: true
|
|
27
|
+
});
|
|
28
|
+
const root = await realpath(this.#cacheDir);
|
|
29
|
+
if (root === node_path.parse(root).root) throw new Error("[plugin-mf] Cache directory must not be a filesystem root");
|
|
30
|
+
const probe = node_path.join(root, `.probe-${randomUUID()}`);
|
|
31
|
+
try {
|
|
32
|
+
const handle = await promises_open(probe, "wx");
|
|
33
|
+
try {
|
|
34
|
+
await handle.writeFile("probe");
|
|
35
|
+
await handle.sync();
|
|
36
|
+
} finally{
|
|
37
|
+
await handle.close();
|
|
38
|
+
}
|
|
39
|
+
} catch (error) {
|
|
40
|
+
throw new Error(`[plugin-mf] Cache directory is not writable: ${root}`, {
|
|
41
|
+
cause: error
|
|
42
|
+
});
|
|
43
|
+
} finally{
|
|
44
|
+
await rm(probe, {
|
|
45
|
+
force: true
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
const entries = node_path.join(root, "entries");
|
|
49
|
+
await mkdir(entries, {
|
|
50
|
+
recursive: true
|
|
51
|
+
});
|
|
52
|
+
if ((await lstat(entries)).isSymbolicLink()) throw new Error("[plugin-mf] Cache entries directory must not be a symbolic link");
|
|
53
|
+
return root;
|
|
54
|
+
}
|
|
55
|
+
#getRoot() {
|
|
56
|
+
this.#root ??= this.#initialize().catch((error)=>{
|
|
57
|
+
this.#root = void 0;
|
|
58
|
+
throw error;
|
|
59
|
+
});
|
|
60
|
+
return this.#root;
|
|
61
|
+
}
|
|
62
|
+
async #download(root, entry, destination, context, deadlineSignal) {
|
|
63
|
+
const staging = await mkdtemp(node_path.join(root, ".download-"));
|
|
64
|
+
try {
|
|
65
|
+
let downloadedBytes = 0;
|
|
66
|
+
for(let offset = 0; offset < entry.files.length; offset += 8){
|
|
67
|
+
deadlineSignal.throwIfAborted();
|
|
68
|
+
const results = await Promise.allSettled(entry.files.slice(offset, offset + 8).map(async (file)=>{
|
|
69
|
+
const buffer = Buffer.from(await fetchResource(file.url, context, (response)=>response.arrayBuffer(), deadlineSignal));
|
|
70
|
+
if (file.integrity && createHash("sha256").update(buffer).digest("hex") !== file.integrity) throw new Error(`[plugin-mf] Server asset integrity mismatch: ${file.path}`);
|
|
71
|
+
deadlineSignal.throwIfAborted();
|
|
72
|
+
downloadedBytes += buffer.byteLength;
|
|
73
|
+
emitLoadingEvent({
|
|
74
|
+
...context,
|
|
75
|
+
type: "download",
|
|
76
|
+
operation: "disk",
|
|
77
|
+
bytes: buffer.byteLength
|
|
78
|
+
});
|
|
79
|
+
if (downloadedBytes > this.#maxBytes) throw new Error("[plugin-mf] Server release exceeds cache maxBytes");
|
|
80
|
+
const target = inside(staging, file.path);
|
|
81
|
+
await mkdir(node_path.dirname(target), {
|
|
82
|
+
recursive: true
|
|
83
|
+
});
|
|
84
|
+
const temporary = `${target}.${randomUUID()}.tmp`;
|
|
85
|
+
const handle = await promises_open(temporary, "wx");
|
|
86
|
+
try {
|
|
87
|
+
await handle.writeFile(buffer);
|
|
88
|
+
await handle.sync();
|
|
89
|
+
} finally{
|
|
90
|
+
await handle.close();
|
|
91
|
+
}
|
|
92
|
+
await rename(temporary, target);
|
|
93
|
+
}));
|
|
94
|
+
const failure = results.find((result)=>"rejected" === result.status);
|
|
95
|
+
if (failure?.status === "rejected") throw failure.reason;
|
|
96
|
+
}
|
|
97
|
+
await writeFile(node_path.join(staging, "package.json"), '{"type":"module"}', "utf8");
|
|
98
|
+
await writeFile(node_path.join(staging, ".ready.json"), JSON.stringify(entry.files), "utf8");
|
|
99
|
+
deadlineSignal.throwIfAborted();
|
|
100
|
+
const size = await entrySize(staging);
|
|
101
|
+
const pruned = await prune(root, Math.max(0, this.#maxBytes - size), void 0, staging);
|
|
102
|
+
if (size + pruned.bytes > this.#maxBytes) throw new Error("[plugin-mf] Server cache limit reached by active ESM releases; raise maxBytes or restart workers");
|
|
103
|
+
await rename(staging, destination);
|
|
104
|
+
} finally{
|
|
105
|
+
await rm(staging, {
|
|
106
|
+
recursive: true,
|
|
107
|
+
force: true
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
async resolve(entry, context, deadlineSignal) {
|
|
112
|
+
const root = await this.#getRoot();
|
|
113
|
+
const directory = inside(node_path.join(root, "entries"), entry.key);
|
|
114
|
+
await withCacheLock(root, this.#lockTimeoutMs, async ()=>{
|
|
115
|
+
deadlineSignal.throwIfAborted();
|
|
116
|
+
let ready = false;
|
|
117
|
+
try {
|
|
118
|
+
if ((await lstat(directory)).isSymbolicLink()) throw new Error("[plugin-mf] Cache entry must not be a symbolic link");
|
|
119
|
+
await regularFile(directory, ".ready.json");
|
|
120
|
+
await regularFile(directory, "package.json");
|
|
121
|
+
ready = await readFile(node_path.join(directory, ".ready.json"), "utf8") === JSON.stringify(entry.files);
|
|
122
|
+
if (ready) for (const file of entry.files){
|
|
123
|
+
await regularFile(directory, file.path);
|
|
124
|
+
if (file.integrity && createHash("sha256").update(await readFile(inside(directory, file.path))).digest("hex") !== file.integrity) throw new Error(`[plugin-mf] Cached server asset integrity mismatch: ${file.path}`);
|
|
125
|
+
}
|
|
126
|
+
} catch (error) {
|
|
127
|
+
if (!hasCode(error, "ENOENT")) throw error;
|
|
128
|
+
ready = false;
|
|
129
|
+
}
|
|
130
|
+
if (ready) emitLoadingEvent({
|
|
131
|
+
...context,
|
|
132
|
+
type: "cache-hit",
|
|
133
|
+
operation: "disk"
|
|
134
|
+
});
|
|
135
|
+
else {
|
|
136
|
+
if (await isPinned(directory)) throw new Error("[plugin-mf] Active server cache entry is incomplete");
|
|
137
|
+
await rm(directory, {
|
|
138
|
+
recursive: true,
|
|
139
|
+
force: true
|
|
140
|
+
});
|
|
141
|
+
await this.#download(root, entry, directory, context, deadlineSignal);
|
|
142
|
+
}
|
|
143
|
+
const pruned = await prune(root, this.#maxBytes, directory);
|
|
144
|
+
if (pruned.overBudgetBytes) throw new Error("[plugin-mf] Server cache limit reached by active ESM releases; raise maxBytes or restart workers");
|
|
145
|
+
const pins = node_path.join(directory, ".pins");
|
|
146
|
+
await mkdir(pins, {
|
|
147
|
+
recursive: true
|
|
148
|
+
});
|
|
149
|
+
if ((await lstat(pins)).isSymbolicLink()) throw new Error("[plugin-mf] Cache lease directory must not be a symbolic link");
|
|
150
|
+
const pin = node_path.join(pins, String(process.pid));
|
|
151
|
+
try {
|
|
152
|
+
await writeFile(pin, "", {
|
|
153
|
+
encoding: "utf8",
|
|
154
|
+
flag: "wx"
|
|
155
|
+
});
|
|
156
|
+
} catch (error) {
|
|
157
|
+
if (!hasCode(error, "EEXIST")) throw error;
|
|
158
|
+
await regularFile(pins, String(process.pid));
|
|
159
|
+
}
|
|
160
|
+
}, deadlineSignal);
|
|
161
|
+
return inside(directory, entry.entryPath);
|
|
162
|
+
}
|
|
163
|
+
async pruneCache() {
|
|
164
|
+
const root = await this.#getRoot();
|
|
165
|
+
return withCacheLock(root, this.#lockTimeoutMs, ()=>prune(root, this.#maxBytes));
|
|
166
|
+
}
|
|
167
|
+
async clearCache() {
|
|
168
|
+
const root = await this.#getRoot();
|
|
169
|
+
return withCacheLock(root, this.#lockTimeoutMs, ()=>prune(root, 0));
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
export { DiskCache };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ModuleLoadContext } from "../../../protocol/module.ts";
|
|
2
|
+
type CacheFile = {
|
|
3
|
+
path: string;
|
|
4
|
+
url: string;
|
|
5
|
+
integrity?: string;
|
|
6
|
+
};
|
|
7
|
+
export type CacheEntry = {
|
|
8
|
+
key: string;
|
|
9
|
+
files: CacheFile[];
|
|
10
|
+
entryPath: string;
|
|
11
|
+
};
|
|
12
|
+
export declare function describe(url: string, context?: ModuleLoadContext): CacheEntry;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { validateAssetPath } from "../../../protocol/asset-path.js";
|
|
3
|
+
import { assertRemoteURL, requiresServerIntegrity } from "../../../transport/network-policy.js";
|
|
4
|
+
function describe(url, context) {
|
|
5
|
+
const entry = new URL(url);
|
|
6
|
+
if ("http:" !== entry.protocol && "https:" !== entry.protocol) throw new Error("[plugin-mf] Unsupported module URL protocol");
|
|
7
|
+
const baseUrl = context ? new URL(context.baseUrl) : new URL(".", entry);
|
|
8
|
+
const provider = context?.provider ?? baseUrl.href;
|
|
9
|
+
assertRemoteURL(url, provider);
|
|
10
|
+
if (requiresServerIntegrity() && !context?.integrity) throw new Error("[plugin-mf] Server asset integrity is required");
|
|
11
|
+
const paths = context?.files ?? [
|
|
12
|
+
decodeURIComponent(entry.pathname.split("/").at(-1) || "remote.js")
|
|
13
|
+
];
|
|
14
|
+
const files = [
|
|
15
|
+
...new Set(paths)
|
|
16
|
+
].sort().map((file)=>{
|
|
17
|
+
validateAssetPath(file);
|
|
18
|
+
if ([
|
|
19
|
+
"package.json",
|
|
20
|
+
".ready.json"
|
|
21
|
+
].includes(file.toLowerCase())) throw new Error(`[plugin-mf] Reserved cache asset path: ${file}`);
|
|
22
|
+
const integrity = context?.integrity?.[file];
|
|
23
|
+
if ((context?.integrity || requiresServerIntegrity()) && (!integrity || !/^[a-f0-9]{64}$/.test(integrity))) throw new Error(`[plugin-mf] Missing or invalid integrity: ${file}`);
|
|
24
|
+
return {
|
|
25
|
+
path: file,
|
|
26
|
+
url: assertRemoteURL(context ? new URL(file, baseUrl).href : entry.href, provider),
|
|
27
|
+
...integrity ? {
|
|
28
|
+
integrity
|
|
29
|
+
} : {}
|
|
30
|
+
};
|
|
31
|
+
});
|
|
32
|
+
const selected = files.find((file)=>file.url === entry.href);
|
|
33
|
+
if (!selected) throw new Error("[plugin-mf] Module entry is missing from its asset list");
|
|
34
|
+
const localNames = files.map((file)=>"win32" === process.platform ? file.path.toLowerCase() : file.path);
|
|
35
|
+
if (new Set(localNames).size !== localNames.length) throw new Error("[plugin-mf] Cache asset paths collide");
|
|
36
|
+
const key = createHash("sha256").update(JSON.stringify({
|
|
37
|
+
provider: context?.provider ?? baseUrl.href,
|
|
38
|
+
releaseId: context?.releaseId ?? null,
|
|
39
|
+
files
|
|
40
|
+
})).digest("hex");
|
|
41
|
+
return {
|
|
42
|
+
key,
|
|
43
|
+
files,
|
|
44
|
+
entryPath: selected.path
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
export { describe };
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export declare function hasCode(error: unknown, code: string): boolean;
|
|
2
|
+
export declare function isProcessAlive(pid: number): boolean;
|
|
3
|
+
export declare function withCacheLock<T>(root: string, timeoutMs: number, callback: () => Promise<T>, deadlineSignal?: AbortSignal): Promise<T>;
|