Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | 22x 22x 22x 24x 24x 24x 24x 18x 18x 18x 18x 18x 18x 1x 1x 1x 17x 17x 16x 2x 2x 14x 14x 14x 14x 18x 18x 3x 3x 1x 18x 18x 18x 18x 18x 2x 2x 2x 2x 4x 3x 3x 1x 1x 1x 1x 32x 32x 4x 28x 6x 6x 6x 2x 26x 3x 3x 2x 23x 1x 3x 3x 3x 3x 2x 2x 3x 2x 3x 3x 5x 5x 15x 1x 1x 1x 1x 11x | import {
IEngineLoader,
IEngineSourceConfig,
EngineErrorCode,
IFileStorage,
} from "../types.js";
import { EngineError } from "../errors/EngineError.js";
import { SecurityAdvisor } from "../capabilities/SecurityAdvisor.js";
import { createI18nKey } from "../protocol/ProtocolValidator.js";
/**
* 2026 Zenith Tier: エンジンリソース(WASM, JS, Assets)の物理的ロードと SRI 検証を管理。
*/
export class EngineLoader implements IEngineLoader {
private activeBlobs = new Map<string, string>();
private inflight = new Map<string, Promise<string>>();
constructor(private readonly storage?: IFileStorage) {}
/**
* 単一のリソースをロードします。
*/
async loadResource(
engineId: string,
config: IEngineSourceConfig,
): Promise<string> {
this.validateResourceUrl(config, engineId);
const safeId = engineId.replace(/[^a-zA-Z0-9]/g, "_");
const cacheKey = `${safeId}-${encodeURIComponent(config.url)}`;
Iif (this.activeBlobs.has(cacheKey)) {
return this.activeBlobs.get(cacheKey)!;
}
Iif (this.inflight.has(cacheKey)) {
return this.inflight.get(cacheKey)!;
}
const performLoad = async (): Promise<string> => {
try {
Eif (this.storage) {
const cached = await this.storage.get(cacheKey);
if (cached) {
const url = URL.createObjectURL(
new Blob([cached], {
type: this.getMimeType(config.type || "worker-js"),
}),
);
this.activeBlobs.set(cacheKey, url);
return url;
}
}
const fetchOptions = SecurityAdvisor.getSafeFetchOptions(config.sri);
// Protocol safety (HTTPS enforcement) is handled inside SecurityAdvisor.safeFetch().
const response = await SecurityAdvisor.safeFetch(config.url, {
...fetchOptions,
signal: AbortSignal.timeout(30000),
});
if (!response.ok) {
this.inflight.delete(cacheKey);
throw new EngineError({
code: EngineErrorCode.NETWORK_ERROR,
message: `Failed to download engine resource: ${config.url} (${response.status})`,
engineId,
});
}
const buffer = await response.arrayBuffer();
Eif (this.storage) {
void this.storage.set(cacheKey, buffer);
}
const url = URL.createObjectURL(
new Blob([buffer], {
type: this.getMimeType(config.type || "worker-js"),
}),
);
this.activeBlobs.set(cacheKey, url);
return url;
} catch (err) {
this.inflight.delete(cacheKey);
if (err instanceof EngineError) throw err;
throw new EngineError({
code: EngineErrorCode.NETWORK_ERROR,
message: `Failed to fetch engine resource: ${config.url}`,
engineId,
// originalError は IEngineError に定義されていないため message に含めるかキャスト
});
} finally {
this.inflight.delete(cacheKey);
}
};
const loadPromise = performLoad();
this.inflight.set(cacheKey, loadPromise);
return loadPromise.finally(() => {
this.inflight.delete(cacheKey);
});
}
/**
* 複数のリソースをロードします。
*/
async loadResources(
engineId: string,
sources: Record<string, IEngineSourceConfig>,
): Promise<Record<string, string>> {
const results: Record<string, string> = {};
const localNewUrls = new Set<string>();
try {
for (const [key, config] of Object.entries(sources)) {
const url = await this.loadResource(engineId, config);
results[key] = url;
localNewUrls.add(url);
}
return results;
} catch (err) {
for (const url of localNewUrls) {
this.revoke(url);
}
throw err;
}
}
private validateResourceUrl(
config: IEngineSourceConfig,
engineId: string,
forceProduction?: boolean,
): void {
const url = config.url;
if (!/^[a-zA-Z0-9_-]+$/.test(engineId)) {
throw new EngineError({
code: EngineErrorCode.SECURITY_ERROR,
message: `Invalid engine ID: ${engineId}`,
engineId,
i18nKey: createI18nKey("engine.errors.invalidEngineId"),
});
}
if (url.toLowerCase().startsWith("http:")) {
let parsedUrl: URL;
try {
parsedUrl = new URL(url);
} catch {
throw new EngineError({
code: EngineErrorCode.SECURITY_ERROR,
message: `Invalid URL format: ${url}`,
engineId,
i18nKey: createI18nKey("engine.errors.insecureConnection"),
});
}
if (!SecurityAdvisor.isLoopbackHost(parsedUrl.hostname)) {
throw new EngineError({
code: EngineErrorCode.SECURITY_ERROR,
message: `Insecure connection (HTTP) is not allowed: ${url}`,
engineId,
i18nKey: createI18nKey("engine.errors.insecureConnection"),
});
}
}
if (config.__unsafeNoSRI) {
const isProd =
forceProduction ??
((typeof process !== "undefined" &&
process.env["NODE_ENV"] === "production") ||
(globalThis as Record<string, unknown>).NODE_ENV === "production");
if (isProd) {
throw new EngineError({
code: EngineErrorCode.SECURITY_ERROR,
message: "SRI bypass (__unsafeNoSRI) is not allowed in production.",
engineId,
i18nKey: createI18nKey("engine.errors.sriBypassNotAllowed"),
});
}
} else if (!config.sri) {
throw new EngineError({
code: EngineErrorCode.SECURITY_ERROR,
message: `SRI hash is required for resource: ${url}`,
engineId,
i18nKey: createI18nKey("engine.errors.securityError"),
});
}
}
/**
* 特定のエンジンのリソースを物理的に解放します。
*/
revokeByEngineId(engineId: string): void {
const safeId = engineId.replace(/[^a-zA-Z0-9]/g, "_");
const prefix = `${safeId}-`;
for (const [key, url] of this.activeBlobs.entries()) {
if (key.startsWith(prefix)) {
this.revoke(url);
this.activeBlobs.delete(key);
}
}
}
/**
* 全てのリソースを物理的に解放します。
*/
revokeAll(): void {
for (const url of this.activeBlobs.values()) {
this.revoke(url);
}
this.activeBlobs.clear();
this.inflight.clear();
}
/**
* 指定された URL を物理的に解放します。
*/
public revoke(url: string): void {
try {
URL.revokeObjectURL(url);
} catch {
/* ignore */
}
}
private getMimeType(type: string): string {
switch (type) {
case "worker-js":
return "application/javascript";
case "wasm":
return "application/wasm";
case "json":
return "application/json";
case "text":
return "text/plain";
default:
return "application/octet-stream";
}
}
}
|