@module-federation/treeshake-server 2.6.0 → 2.8.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.
Files changed (34) hide show
  1. package/dist/876.mjs +940 -0
  2. package/dist/app.d.ts +1 -1
  3. package/dist/frontend/adapter/index.js +12 -8
  4. package/dist/frontend/index.html +1 -1
  5. package/dist/frontend/static/css/index.513418ff5d.css +1 -0
  6. package/dist/frontend/static/js/637.b47e0403af.js +2 -0
  7. package/dist/frontend/static/js/async/427.dbb8b74259.js +2 -0
  8. package/dist/frontend/static/js/async/503.7bd056737e.js +12 -0
  9. package/dist/frontend/static/js/async/515.1b7f26d7ec.js +2 -0
  10. package/dist/frontend/static/js/index.ace3fa3d0e.js +88 -0
  11. package/dist/frontend/static/js/lib-react.7c43a84261.js +2 -0
  12. package/dist/frontend/static/js/{lib-router.949393cc.js → lib-router.5077a93d5a.js} +3 -3
  13. package/dist/http/routes/build.d.ts +1 -1
  14. package/dist/http/routes/index.d.ts +1 -1
  15. package/dist/http/routes/maintenance.d.ts +1 -1
  16. package/dist/http/routes/static.d.ts +1 -1
  17. package/dist/index.js +29 -27
  18. package/dist/index.mjs +1 -942
  19. package/dist/nodeServer.d.ts +1 -1
  20. package/dist/server.js +23 -21
  21. package/dist/server.mjs +11 -949
  22. package/package.json +5 -5
  23. package/dist/frontend/static/css/index.16175e0f.css +0 -1
  24. package/dist/frontend/static/js/465.8ad65aff.js +0 -2
  25. package/dist/frontend/static/js/async/538.91f59a63.js +0 -12
  26. package/dist/frontend/static/js/async/873.03a2ef6b.js +0 -2
  27. package/dist/frontend/static/js/async/987.89efa6cc.js +0 -2
  28. package/dist/frontend/static/js/index.3cfd4422.js +0 -88
  29. package/dist/frontend/static/js/lib-react.c59642e3.js +0 -2
  30. /package/dist/frontend/static/js/{465.8ad65aff.js.LICENSE.txt → 637.b47e0403af.js.LICENSE.txt} +0 -0
  31. /package/dist/frontend/static/js/async/{538.91f59a63.js.LICENSE.txt → 427.dbb8b74259.js.LICENSE.txt} +0 -0
  32. /package/dist/frontend/static/js/async/{987.89efa6cc.js.LICENSE.txt → 503.7bd056737e.js.LICENSE.txt} +0 -0
  33. /package/dist/frontend/static/js/{lib-react.c59642e3.js.LICENSE.txt → lib-react.7c43a84261.js.LICENSE.txt} +0 -0
  34. /package/dist/frontend/static/js/{lib-router.949393cc.js.LICENSE.txt → lib-router.5077a93d5a.js.LICENSE.txt} +0 -0
package/dist/876.mjs ADDED
@@ -0,0 +1,940 @@
1
+ import { Hono } from "hono";
2
+ import { cors } from "hono/cors";
3
+ import pino from "pino";
4
+ import node_os from "node:os";
5
+ import { zValidator } from "@hono/zod-validator";
6
+ import { nanoid } from "nanoid";
7
+ import p_limit from "p-limit";
8
+ import { z } from "zod";
9
+ import { createHash } from "node:crypto";
10
+ import promises from "node:fs/promises";
11
+ import node_path from "node:path";
12
+ import { spawn } from "node:child_process";
13
+ import json_stable_stringify from "json-stable-stringify";
14
+ import node_fs from "node:fs";
15
+ import { timeout } from "hono/timeout";
16
+ import { serve } from "@hono/node-server";
17
+ function createDiMiddleware(deps) {
18
+ return async (c, next)=>{
19
+ c.set('objectStore', deps.objectStore);
20
+ if (deps.projectPublisher) c.set('projectPublisher', deps.projectPublisher);
21
+ await next();
22
+ };
23
+ }
24
+ const createLogger = (opts)=>pino({
25
+ level: opts?.level ?? 'info',
26
+ formatters: {
27
+ level (label) {
28
+ return {
29
+ level: label
30
+ };
31
+ }
32
+ }
33
+ });
34
+ let logger_logger = createLogger();
35
+ const setLogger = (next)=>{
36
+ logger_logger = next;
37
+ };
38
+ const loggerMiddleware = async (c, next)=>{
39
+ const category = c.req.path.split('/')[1] || 'root';
40
+ const child = logger_logger.child({
41
+ category,
42
+ path: c.req.path,
43
+ method: c.req.method
44
+ });
45
+ c.set('logger', child);
46
+ await next();
47
+ };
48
+ const parseNormalizedKey = (key)=>{
49
+ const res = key.split('@');
50
+ return {
51
+ name: res.slice(0, -1).join('@'),
52
+ version: res[res.length - 1]
53
+ };
54
+ };
55
+ const normalizedKey = (name, v)=>`${name}@${v}`;
56
+ function normalizeConfig(config) {
57
+ const { shared, plugins, target, libraryType, hostName, uploadOptions } = config;
58
+ const commonNormalizedConfig = {
59
+ plugins: plugins?.sort(([a], [b])=>a.localeCompare(b)) ?? [],
60
+ target: Array.isArray(target) ? [
61
+ ...target
62
+ ].sort() : [
63
+ target
64
+ ],
65
+ uploadOptions,
66
+ libraryType,
67
+ hostName
68
+ };
69
+ const normalizedConfig = {};
70
+ shared.forEach(([sharedName, version, usedExports], index)=>{
71
+ const key = normalizedKey(sharedName, version);
72
+ normalizedConfig[key] = {
73
+ ...commonNormalizedConfig,
74
+ shared: shared.slice(0, index).concat(shared.slice(index + 1)).sort(([s, v, u], [b, v2, u2])=>`${s}${v}${u.sort().join('')}`.localeCompare(`${b}${v2}${u2.sort().join('')}`)),
75
+ usedExports
76
+ };
77
+ });
78
+ return normalizedConfig;
79
+ }
80
+ function extractBuildConfig(config, type) {
81
+ const { shared, plugins, target, libraryType, usedExports, hostName } = config;
82
+ return {
83
+ shared,
84
+ plugins,
85
+ target,
86
+ libraryType,
87
+ usedExports,
88
+ type,
89
+ hostName
90
+ };
91
+ }
92
+ const UploadOptionsSchema = z.object({
93
+ scmName: z.string(),
94
+ bucketName: z.string(),
95
+ publicFilePath: z.string(),
96
+ publicPath: z.string(),
97
+ cdnRegion: z.string()
98
+ });
99
+ const BasicSchema = z.object({
100
+ shared: z.array(z.tuple([
101
+ z.string(),
102
+ z.string(),
103
+ z.array(z.string())
104
+ ])).describe('List of plugins: [name, version, usedExports]'),
105
+ plugins: z.array(z.tuple([
106
+ z.string(),
107
+ z.string().optional()
108
+ ])).describe('Specify extra build plugin names, support specify version in second item').optional(),
109
+ target: z.union([
110
+ z.string(),
111
+ z.array(z.string())
112
+ ]).describe('Used to configure the target environment of Rspack output and the ECMAScript version of Rspack runtime code, the same with rspack#target'),
113
+ libraryType: z.string(),
114
+ hostName: z.string().describe('The name of the host app / mf')
115
+ });
116
+ const ConfigSchema = BasicSchema.extend({
117
+ uploadOptions: UploadOptionsSchema.optional()
118
+ });
119
+ const CheckTreeShakingSchema = ConfigSchema.extend({
120
+ uploadOptions: UploadOptionsSchema.optional()
121
+ });
122
+ const STATS_NAME = 'mf-stats.json';
123
+ let runtimeEnv = {};
124
+ const setRuntimeEnv = (env)=>{
125
+ runtimeEnv = env;
126
+ };
127
+ const getRuntimeEnv = ()=>runtimeEnv;
128
+ function _define_property(obj, key, value) {
129
+ if (key in obj) Object.defineProperty(obj, key, {
130
+ value: value,
131
+ enumerable: true,
132
+ configurable: true,
133
+ writable: true
134
+ });
135
+ else obj[key] = value;
136
+ return obj;
137
+ }
138
+ class CommandExecutionError extends Error {
139
+ constructor(command, exitCode, stdout, stderr){
140
+ super(`Command "${command}" exited with code ${exitCode}`), _define_property(this, "command", void 0), _define_property(this, "exitCode", void 0), _define_property(this, "stdout", void 0), _define_property(this, "stderr", void 0);
141
+ this.name = 'CommandExecutionError';
142
+ this.command = command;
143
+ this.exitCode = exitCode;
144
+ this.stdout = stdout;
145
+ this.stderr = stderr;
146
+ }
147
+ }
148
+ const runCommand = (command, options = {})=>new Promise((resolve, reject)=>{
149
+ const stdoutChunks = [];
150
+ const stderrChunks = [];
151
+ const child = spawn(command, {
152
+ cwd: options.cwd,
153
+ env: {
154
+ ...getRuntimeEnv(),
155
+ ...options.env
156
+ },
157
+ shell: true,
158
+ stdio: [
159
+ 'ignore',
160
+ 'pipe',
161
+ 'pipe'
162
+ ]
163
+ });
164
+ child.stdout.on('data', (chunk)=>{
165
+ stdoutChunks.push(chunk.toString());
166
+ });
167
+ child.stderr.on('data', (chunk)=>{
168
+ stderrChunks.push(chunk.toString());
169
+ });
170
+ child.on('error', (error)=>{
171
+ reject(error);
172
+ });
173
+ child.on('close', (exitCode)=>{
174
+ const stdout = stdoutChunks.join('');
175
+ const stderr = stderrChunks.join('');
176
+ if (0 === exitCode) return void resolve({
177
+ stdout,
178
+ stderr,
179
+ exitCode
180
+ });
181
+ reject(new CommandExecutionError(command, exitCode ?? -1, stdout, stderr));
182
+ });
183
+ });
184
+ let installs = 0;
185
+ let pruneScheduled = false;
186
+ let pruning = false;
187
+ const DEFAULT_INTERVAL = 3600000;
188
+ const markInstallStart = ()=>{
189
+ installs++;
190
+ };
191
+ const markInstallEnd = ()=>{
192
+ installs = Math.max(0, installs - 1);
193
+ schedulePruneSoon();
194
+ };
195
+ const schedulePruneSoon = (delay = 30000)=>{
196
+ if (pruneScheduled) return;
197
+ pruneScheduled = true;
198
+ setTimeout(()=>{
199
+ pruneScheduled = false;
200
+ maybePrune();
201
+ }, delay);
202
+ };
203
+ const maybePrune = async ()=>{
204
+ if (pruning || installs > 0) return void schedulePruneSoon(60000);
205
+ pruning = true;
206
+ try {
207
+ await runCommand('pnpm store prune');
208
+ } catch {} finally{
209
+ pruning = false;
210
+ }
211
+ };
212
+ const startPeriodicPrune = (intervalMs = DEFAULT_INTERVAL)=>{
213
+ if (intervalMs <= 0) return;
214
+ setInterval(()=>{
215
+ maybePrune();
216
+ }, intervalMs);
217
+ };
218
+ const createUniqueTempDirByKey = async (key)=>{
219
+ const base = node_path.join(node_os.tmpdir(), `re-shake-share-${key}`);
220
+ let candidate = base;
221
+ for(;;)try {
222
+ await promises.mkdir(candidate, {
223
+ recursive: false
224
+ });
225
+ return candidate;
226
+ } catch {
227
+ const rand = Math.floor(1e9 * Math.random());
228
+ candidate = `${base}-${rand}`;
229
+ }
230
+ };
231
+ const prepareProject = async (config, excludeShared)=>{
232
+ const key = createHash('sha256').update(JSON.stringify(config)).digest('hex');
233
+ const dir = await createUniqueTempDirByKey(key);
234
+ const templateDir = node_path.join(__dirname, '.', 'template', 're-shake-share');
235
+ await promises.cp(templateDir, dir, {
236
+ recursive: true
237
+ });
238
+ const pkgPath = node_path.join(dir, 'package.json');
239
+ const indexPath = node_path.join(dir, 'index.ts');
240
+ const rspackConfigPath = node_path.join(dir, 'rspack.config.ts');
241
+ const [pkgContent, indexContent, rspackConfigContent] = await Promise.all([
242
+ promises.readFile(pkgPath, 'utf-8'),
243
+ promises.readFile(indexPath, 'utf-8'),
244
+ promises.readFile(rspackConfigPath, 'utf-8')
245
+ ]);
246
+ const pkg = JSON.parse(pkgContent);
247
+ const deps = {
248
+ ...pkg.dependencies || {}
249
+ };
250
+ const shared = {};
251
+ const mfConfig = {
252
+ name: 're_shake',
253
+ library: {
254
+ type: 'global'
255
+ },
256
+ manifest: true,
257
+ shared
258
+ };
259
+ let pluginImportStr = '';
260
+ let pluginOptionStr = '[\n';
261
+ let sharedImport = '';
262
+ Object.entries(config).forEach(([key, { plugins = [], libraryType, usedExports, hostName }], index)=>{
263
+ const { name, version } = parseNormalizedKey(key);
264
+ deps[name] = version;
265
+ if (!index) {
266
+ plugins.forEach(([pluginName, pluginVersion], pIndex)=>{
267
+ deps[pluginName] = pluginVersion ?? 'latest';
268
+ const pluginImportName = `plugin_${pIndex}`;
269
+ pluginImportStr += `import ${pluginImportName} from '${pluginName}';\n`;
270
+ pluginOptionStr += `new ${pluginImportName}(),`;
271
+ });
272
+ mfConfig.library.type = libraryType;
273
+ mfConfig.name = hostName;
274
+ }
275
+ shared[name] = {
276
+ requiredVersion: version,
277
+ version,
278
+ treeShaking: excludeShared.some(([n, v])=>n === name && v === version) ? void 0 : {
279
+ usedExports,
280
+ mode: 'server-calc'
281
+ }
282
+ };
283
+ sharedImport += `import shared_${index} from '${name}';\n`;
284
+ });
285
+ pluginOptionStr += '\n]';
286
+ pkg.dependencies = deps;
287
+ const newPkgContent = JSON.stringify(pkg, null, 2);
288
+ const sharedImportPlaceholder = "${SHARED_IMPORT}";
289
+ const newIndexContent = indexContent.replace(sharedImportPlaceholder, sharedImport);
290
+ const pluginsPlaceholder = "${ PLUGINS }";
291
+ const mfConfigPlaceholder = "${ MF_CONFIG }";
292
+ let cfg = rspackConfigContent;
293
+ cfg += pluginImportStr;
294
+ cfg = cfg.replace(pluginsPlaceholder, pluginOptionStr);
295
+ cfg = cfg.replace(mfConfigPlaceholder, JSON.stringify(mfConfig, null, 2));
296
+ await Promise.all([
297
+ promises.writeFile(pkgPath, newPkgContent),
298
+ promises.writeFile(indexPath, newIndexContent),
299
+ promises.writeFile(rspackConfigPath, cfg)
300
+ ]);
301
+ return dir;
302
+ };
303
+ const installDependencies = async (cwd)=>{
304
+ markInstallStart();
305
+ try {
306
+ await runCommand("pnpm i --ignore-scripts --prefer-offline --reporter=silent ", {
307
+ cwd,
308
+ env: {
309
+ npm_config_registry: process.env.MF_NPM_REGISTRY || 'https://registry.npmjs.org/'
310
+ }
311
+ });
312
+ } finally{
313
+ markInstallEnd();
314
+ }
315
+ };
316
+ const buildProject = async (cwd, type)=>{
317
+ const scripts = [];
318
+ if ('re-shake' === type) scripts.push('pnpm build');
319
+ else scripts.push('pnpm build:full');
320
+ await Promise.all(scripts.map((script)=>runCommand(script, {
321
+ cwd
322
+ })));
323
+ };
324
+ const retrieveSharedFilepaths = async (projectDir, type)=>{
325
+ const sharedFilepaths = [];
326
+ const collectSharedFilepaths = async (t)=>{
327
+ const dir = 'full' === t ? 'full-shared' : 'dist';
328
+ const distDir = node_path.join(projectDir, dir);
329
+ const statsContent = await promises.readFile(node_path.join(distDir, STATS_NAME), 'utf-8');
330
+ const stats = JSON.parse(statsContent);
331
+ stats.shared.forEach((s)=>{
332
+ const { name, version, fallback, fallbackName } = s;
333
+ if (fallback && fallbackName) {
334
+ const filepath = node_path.join(distDir, fallback);
335
+ sharedFilepaths.push({
336
+ name,
337
+ version,
338
+ filepath,
339
+ globalName: fallbackName,
340
+ type: t,
341
+ modules: s.usedExports,
342
+ canTreeShaking: s.canTreeShaking ?? s.usedExports?.length !== 0
343
+ });
344
+ }
345
+ });
346
+ };
347
+ await collectSharedFilepaths(type);
348
+ return sharedFilepaths;
349
+ };
350
+ const runBuild = async (normalizedConfig, excludeShared, type)=>{
351
+ const tStart = Date.now();
352
+ const tmpDir = await prepareProject(normalizedConfig, excludeShared);
353
+ const tPrepare = Date.now();
354
+ logger_logger.info(`prepareProject took ${tPrepare - tStart}ms`);
355
+ await installDependencies(tmpDir);
356
+ const tInstall = Date.now();
357
+ logger_logger.info(`installDependencies took ${tInstall - tPrepare}ms`);
358
+ await buildProject(tmpDir, type);
359
+ const tBuild = Date.now();
360
+ logger_logger.info(`buildProject took ${tBuild - tInstall}ms`);
361
+ const sharedFilePaths = await retrieveSharedFilepaths(tmpDir, type);
362
+ const tRetrieve = Date.now();
363
+ logger_logger.info(`retrieveSharedFilepaths took ${tRetrieve - tBuild}ms`);
364
+ return {
365
+ sharedFilePaths,
366
+ dir: tmpDir
367
+ };
368
+ };
369
+ async function cleanUp(tmpDir) {
370
+ if (!tmpDir) return;
371
+ await promises.rm(tmpDir, {
372
+ recursive: true,
373
+ force: true
374
+ });
375
+ }
376
+ const encodeName = function(name, prefix = '', withExt = false) {
377
+ const ext = withExt ? '.js' : '';
378
+ return `${prefix}${name.replace(/@/g, 'scope_').replace(/-/g, '_').replace(/\//g, '__').replace(/\./g, '')}${ext}`;
379
+ };
380
+ function retrieveGlobalName(mfName, sharedName, version) {
381
+ return encodeName(`${mfName}_${sharedName}_${version}`);
382
+ }
383
+ const SERVER_VERSION = 'v0-0205';
384
+ const UPLOADED_DIR = '_shared-tree-shaking';
385
+ function createCacheHash(config, type) {
386
+ const relevant = extractBuildConfig({
387
+ ...config,
388
+ usedExports: 'full' === type ? [] : config.usedExports
389
+ }, type);
390
+ const json = json_stable_stringify(relevant);
391
+ if (!json) throw new Error('Can not stringify build config!');
392
+ return createHash('sha256').update(json).digest('hex');
393
+ }
394
+ function retrieveCDNPath({ config, sharedKey, type }) {
395
+ const configHash = createCacheHash(config, type);
396
+ return `tree-shaking-shared/${SERVER_VERSION}/${sharedKey}/${configHash}.js`;
397
+ }
398
+ async function cacheService_hitCache(sharedKey, config, type, store) {
399
+ const cdnPath = retrieveCDNPath({
400
+ config,
401
+ sharedKey,
402
+ type
403
+ });
404
+ const exists = await store.exists(cdnPath);
405
+ return exists ? store.publicUrl(cdnPath) : null;
406
+ }
407
+ const retrieveCacheItems = async (normalizedConfig, type, store)=>{
408
+ const cacheItems = [];
409
+ const restConfig = {};
410
+ const excludeShared = [];
411
+ for (const [sharedKey, config] of Object.entries(normalizedConfig)){
412
+ let cache = false;
413
+ const { name, version } = parseNormalizedKey(sharedKey);
414
+ const cdnUrl = await cacheService_hitCache(sharedKey, config, type, store);
415
+ if (cdnUrl) {
416
+ cache = true;
417
+ cacheItems.push({
418
+ type,
419
+ name,
420
+ version,
421
+ cdnUrl,
422
+ globalName: retrieveGlobalName(config.hostName, name, version)
423
+ });
424
+ }
425
+ if (cache) excludeShared.push([
426
+ name,
427
+ version
428
+ ]);
429
+ else if (config.usedExports.length || 're-shake' !== type) restConfig[sharedKey] = config;
430
+ else excludeShared.push([
431
+ name,
432
+ version
433
+ ]);
434
+ }
435
+ return {
436
+ cacheItems,
437
+ excludeShared,
438
+ restConfig
439
+ };
440
+ };
441
+ async function uploadToCacheStore(sharedFilePaths, normalizedConfig, store) {
442
+ const results = [];
443
+ for (const file of sharedFilePaths){
444
+ const { name, version, filepath, globalName, type, modules, canTreeShaking } = file;
445
+ try {
446
+ const sharedKey = normalizedKey(name, version);
447
+ logger_logger.info(`Uploading ${sharedKey} to CDN`);
448
+ const config = normalizedConfig[sharedKey];
449
+ const cdnPath = retrieveCDNPath({
450
+ config,
451
+ sharedKey,
452
+ type
453
+ });
454
+ const t0 = Date.now();
455
+ await store.uploadFile(filepath, cdnPath);
456
+ const tUpload = Date.now() - t0;
457
+ const cdnUrl = store.publicUrl(cdnPath);
458
+ const res = {
459
+ name: name,
460
+ version: version,
461
+ globalName: globalName,
462
+ cdnUrl,
463
+ type,
464
+ modules,
465
+ canTreeShaking
466
+ };
467
+ try {
468
+ const jsonFilePath = filepath.replace(/\.js$/, '.json');
469
+ const jsonFile = JSON.stringify(res);
470
+ const jsonCdnUrl = cdnPath.replace(/\.js$/, '.json');
471
+ await promises.writeFile(jsonFilePath, jsonFile);
472
+ await store.uploadFile(jsonFilePath, jsonCdnUrl);
473
+ } catch (error) {
474
+ logger_logger.error(`Failed to upload ${name}@${version} json file: ${error}`);
475
+ }
476
+ results.push(res);
477
+ logger_logger.info(`Successfully uploaded ${name}@${version} to ${cdnUrl} in ${tUpload}ms`);
478
+ } catch (error) {
479
+ logger_logger.error(`Failed to upload ${name}@${version}: ${error}`);
480
+ throw error;
481
+ }
482
+ }
483
+ return results;
484
+ }
485
+ const downloadToFile = async (url, destPath)=>{
486
+ const res = await fetch(url);
487
+ if (!res.ok) throw new Error(`Download failed: ${res.status} ${res.statusText} - ${url}`);
488
+ const buf = Buffer.from(await res.arrayBuffer());
489
+ await promises.mkdir(node_path.dirname(destPath), {
490
+ recursive: true
491
+ });
492
+ await promises.writeFile(destPath, buf);
493
+ return destPath;
494
+ };
495
+ async function uploadProject(uploadResults, sharedFilePaths, normalizedConfig, publisher, store) {
496
+ const tmpDir = await createUniqueTempDirByKey(`upload-project${Date.now().toString()}`);
497
+ const uploaded = [];
498
+ try {
499
+ for (const item of uploadResults)try {
500
+ const sharedKey = normalizedKey(item.name, item.version);
501
+ const config = normalizedConfig[sharedKey];
502
+ if (!config) {
503
+ logger_logger.error(`No config found for ${item.name}`);
504
+ continue;
505
+ }
506
+ const { uploadOptions } = config;
507
+ if (!uploadOptions) throw new Error(`No uploadOptions found for ${item.name}`);
508
+ const filename = node_path.basename(new URL(item.cdnUrl, 'http://dummy.com').pathname);
509
+ const localPath = node_path.join(tmpDir, filename);
510
+ const hash = createCacheHash({
511
+ ...config,
512
+ plugins: config.plugins || []
513
+ }, item.type);
514
+ const cdnUrl = publisher.publicUrl({
515
+ sharedName: item.name,
516
+ hash,
517
+ fileName: filename,
518
+ options: uploadOptions
519
+ });
520
+ const hitCache = await publisher.exists(cdnUrl);
521
+ if (hitCache) {
522
+ logger_logger.info(`Hit cache for ${item.name}@${item.version} -> ${cdnUrl}`);
523
+ uploaded.push({
524
+ ...item,
525
+ cdnUrl
526
+ });
527
+ continue;
528
+ }
529
+ logger_logger.info(`Downloading ${item.name}@${item.version} -> ${localPath}`);
530
+ const t0 = Date.now();
531
+ await downloadToFile(item.cdnUrl, localPath);
532
+ const tDownload = Date.now() - t0;
533
+ logger_logger.info(`Downloaded ${item.name}@${item.version} in ${tDownload}ms`);
534
+ const newCdnUrl = await publisher.publishFile({
535
+ localPath,
536
+ sharedName: item.name,
537
+ hash,
538
+ options: uploadOptions
539
+ });
540
+ uploaded.push({
541
+ ...item,
542
+ cdnUrl: newCdnUrl
543
+ });
544
+ } catch (error) {
545
+ logger_logger.error(`Failed to upload ${item.name}@${item.version}: ${error}`);
546
+ }
547
+ for (const s of sharedFilePaths)try {
548
+ const config = normalizedConfig[normalizedKey(s.name, s.version)];
549
+ if (!config) {
550
+ logger_logger.error(`No config found for ${s.name}`);
551
+ continue;
552
+ }
553
+ const { uploadOptions } = config;
554
+ if (!uploadOptions) throw new Error(`No uploadOptions found for ${s.name}`);
555
+ const hash = createCacheHash({
556
+ ...config,
557
+ plugins: config.plugins || []
558
+ }, s.type);
559
+ const cdnUrl = await publisher.publishFile({
560
+ localPath: s.filepath,
561
+ sharedName: s.name,
562
+ hash,
563
+ options: uploadOptions
564
+ });
565
+ uploaded.push({
566
+ name: s.name,
567
+ version: s.version,
568
+ globalName: s.globalName,
569
+ cdnUrl,
570
+ type: s.type,
571
+ modules: s.modules,
572
+ canTreeShaking: s.canTreeShaking
573
+ });
574
+ } catch (error) {
575
+ logger_logger.error(`Failed to upload ${s.name}@${s.version}: ${error}`);
576
+ }
577
+ return uploaded;
578
+ } finally{
579
+ promises.rm(tmpDir, {
580
+ recursive: true,
581
+ force: true
582
+ }).catch((err)=>{
583
+ logger_logger.error(`Failed to cleanup dir ${tmpDir}: ${err}`);
584
+ });
585
+ }
586
+ }
587
+ async function upload(sharedFilePaths, uploadResults, normalizedConfig, uploadOptions, store, publisher) {
588
+ const cacheUploaded = await uploadToCacheStore(sharedFilePaths, normalizedConfig, store);
589
+ if (!uploadOptions) {
590
+ const hydrated = await Promise.all(uploadResults.map(async (item)=>{
591
+ if ('full' !== item.type) return item;
592
+ const tmpDir = await createUniqueTempDirByKey(`download-full-json${Date.now().toString()}`);
593
+ const jsonPath = node_path.join(tmpDir, `${item.name}-${item.version}.json`);
594
+ try {
595
+ const tJson0 = Date.now();
596
+ await downloadToFile(item.cdnUrl.replace('.js', '.json'), jsonPath);
597
+ const tJson = Date.now() - tJson0;
598
+ logger_logger.info(`Downloaded ${item.name}@${item.version} json in ${tJson}ms`);
599
+ const jsonContent = JSON.parse(await promises.readFile(jsonPath, 'utf8'));
600
+ return {
601
+ ...item,
602
+ canTreeShaking: jsonContent.canTreeShaking,
603
+ modules: jsonContent.modules
604
+ };
605
+ } catch (jsonError) {
606
+ logger_logger.error(`Failed to download ${item.name}@${item.version} json: ${jsonError}`);
607
+ return {
608
+ ...item,
609
+ canTreeShaking: item.canTreeShaking ?? true
610
+ };
611
+ } finally{
612
+ await promises.rm(tmpDir, {
613
+ recursive: true,
614
+ force: true
615
+ });
616
+ }
617
+ }));
618
+ return [
619
+ ...cacheUploaded,
620
+ ...hydrated
621
+ ];
622
+ }
623
+ if (!publisher) throw new Error('uploadOptions provided but no projectPublisher configured (configure the selected adapter to enable it or omit uploadOptions)');
624
+ const projectUploadResults = await uploadProject(uploadResults, sharedFilePaths, normalizedConfig, publisher, store);
625
+ return projectUploadResults;
626
+ }
627
+ const buildLimit = p_limit(Math.max(1, node_os.cpus().length));
628
+ const buildRoute = new Hono();
629
+ buildRoute.post('/', zValidator('json', ConfigSchema), async (c)=>{
630
+ const logger = c.get('logger');
631
+ const startTime = Date.now();
632
+ const jobId = nanoid();
633
+ logger.info(jobId);
634
+ const body = c.req.valid('json');
635
+ logger.info(JSON.stringify(body));
636
+ const normalizedConfig = normalizeConfig(body);
637
+ const store = c.get('objectStore');
638
+ const publisher = c.get('projectPublisher');
639
+ try {
640
+ const t0 = Date.now();
641
+ const { cacheItems, excludeShared, restConfig } = await retrieveCacheItems(normalizedConfig, 're-shake', store);
642
+ const tRetrieveCache = Date.now();
643
+ logger.info(`retrieveCacheItems took ${tRetrieveCache - t0}ms`);
644
+ let sharedFilePaths = [];
645
+ let dir;
646
+ if (Object.keys(restConfig).length > 0) {
647
+ const buildResult = await buildLimit(()=>runBuild(normalizedConfig, excludeShared, 're-shake'));
648
+ sharedFilePaths = buildResult.sharedFilePaths;
649
+ dir = buildResult.dir;
650
+ }
651
+ const tBuild = Date.now();
652
+ logger.info(`runBuild took ${tBuild - tRetrieveCache}ms`);
653
+ const uploadResults = await upload(sharedFilePaths, cacheItems, normalizedConfig, body.uploadOptions, store, publisher);
654
+ const tUpload = Date.now();
655
+ logger.info(`upload took ${tUpload - tBuild}ms`);
656
+ cleanUp(dir).catch((err)=>{
657
+ logger.error(`Failed to cleanup dir ${dir}: ${err}`);
658
+ });
659
+ const tCleanUp = Date.now();
660
+ logger.info(`cleanUp scheduled (non-blocking) took ${tCleanUp - tUpload}ms`);
661
+ return c.json({
662
+ jobId,
663
+ status: 'success',
664
+ data: uploadResults,
665
+ cached: cacheItems,
666
+ duration: Date.now() - startTime
667
+ });
668
+ } catch (error) {
669
+ if (error instanceof CommandExecutionError) {
670
+ if (137 === error.exitCode) maybePrune();
671
+ return c.json({
672
+ jobId,
673
+ status: 'failed',
674
+ error: error.message,
675
+ command: error.command,
676
+ exitCode: error.exitCode,
677
+ stdout: error.stdout,
678
+ stderr: error.stderr
679
+ });
680
+ }
681
+ return c.json({
682
+ jobId,
683
+ status: 'failed',
684
+ error: error instanceof Error ? error.message : 'Unknown error'
685
+ });
686
+ }
687
+ });
688
+ async function handleCheckTreeshake(c, body) {
689
+ const logger = c.get('logger');
690
+ const startTime = Date.now();
691
+ const jobId = nanoid();
692
+ logger.info(jobId);
693
+ logger.info(JSON.stringify(body));
694
+ const normalizedConfig = normalizeConfig(body);
695
+ const store = c.get('objectStore');
696
+ const publisher = c.get('projectPublisher');
697
+ try {
698
+ const t0 = Date.now();
699
+ const { cacheItems, excludeShared, restConfig } = await retrieveCacheItems(normalizedConfig, 'full', store);
700
+ const tRetrieveCache = Date.now();
701
+ logger.info(`retrieveCacheItems took ${tRetrieveCache - t0}ms`);
702
+ let sharedFilePaths = [];
703
+ let dir;
704
+ if (Object.keys(restConfig).length > 0) {
705
+ const buildResult = await buildLimit(()=>runBuild(normalizedConfig, excludeShared, 'full'));
706
+ sharedFilePaths = buildResult.sharedFilePaths;
707
+ dir = buildResult.dir;
708
+ }
709
+ const tBuild = Date.now();
710
+ logger.info(`runBuild took ${tBuild - tRetrieveCache}ms`);
711
+ const uploadResults = await upload(sharedFilePaths, cacheItems, normalizedConfig, body.uploadOptions, store, publisher);
712
+ const tUpload = Date.now();
713
+ logger.info(`upload took ${tUpload - tBuild}ms`);
714
+ cleanUp(dir).catch((err)=>{
715
+ logger.error(`Failed to cleanup dir ${dir}: ${err}`);
716
+ });
717
+ const tCleanUp = Date.now();
718
+ logger.info(`cleanUp scheduled (non-blocking) took ${tCleanUp - tUpload}ms`);
719
+ return c.json({
720
+ jobId,
721
+ status: 'success',
722
+ data: uploadResults,
723
+ cached: cacheItems,
724
+ duration: Date.now() - startTime
725
+ });
726
+ } catch (error) {
727
+ if (error instanceof CommandExecutionError) {
728
+ if (137 === error.exitCode) maybePrune();
729
+ return c.json({
730
+ jobId,
731
+ status: 'failed',
732
+ error: error.message,
733
+ command: error.command,
734
+ exitCode: error.exitCode,
735
+ stdout: error.stdout,
736
+ stderr: error.stderr
737
+ });
738
+ }
739
+ return c.json({
740
+ jobId,
741
+ status: 'failed',
742
+ error: error instanceof Error ? error.message : 'Unknown error'
743
+ });
744
+ }
745
+ }
746
+ buildRoute.post('/check-tree-shaking', zValidator('json', CheckTreeShakingSchema), async (c)=>handleCheckTreeshake(c, c.req.valid('json')));
747
+ const maintenanceRoute = new Hono();
748
+ maintenanceRoute.post('/', async (c)=>{
749
+ const logger = c.get('logger');
750
+ const jobId = nanoid();
751
+ const startTime = Date.now();
752
+ logger.info(jobId);
753
+ await maybePrune();
754
+ return c.json({
755
+ jobId,
756
+ status: 'success',
757
+ duration: Date.now() - startTime
758
+ });
759
+ });
760
+ const routes = new Hono();
761
+ routes.route('/build', buildRoute);
762
+ routes.route('/clean-cache', maintenanceRoute);
763
+ function contentTypeByExt(filePath) {
764
+ if (filePath.endsWith('.js')) return "application/javascript";
765
+ if (filePath.endsWith('.json')) return 'application/json';
766
+ return 'application/octet-stream';
767
+ }
768
+ const DEFAULT_STATIC_ROOT = node_path.join(process.cwd(), 'log', 'static');
769
+ async function serveLocalFile(c, rootDir) {
770
+ let requestPath = c.req.path;
771
+ try {
772
+ requestPath = decodeURIComponent(requestPath);
773
+ } catch {
774
+ return c.text('Not Found', 404);
775
+ }
776
+ const relPath = requestPath.replace(/^\/+/, '');
777
+ const rootResolved = node_path.resolve(rootDir);
778
+ const filePath = node_path.resolve(rootResolved, relPath);
779
+ if (filePath !== rootResolved && !filePath.startsWith(`${rootResolved}${node_path.sep}`)) return c.text('Not Found', 404);
780
+ try {
781
+ const buf = await node_fs.promises.readFile(filePath);
782
+ return new Response(new Uint8Array(buf), {
783
+ status: 200,
784
+ headers: {
785
+ 'Content-Type': contentTypeByExt(filePath)
786
+ }
787
+ });
788
+ } catch {
789
+ return c.text('Not Found', 404);
790
+ }
791
+ }
792
+ function createStaticRoute(opts) {
793
+ const staticRoute = new Hono();
794
+ const rootDir = opts?.rootDir ?? DEFAULT_STATIC_ROOT;
795
+ staticRoute.get('/tree-shaking-shared/*', async (c)=>serveLocalFile(c, rootDir));
796
+ return staticRoute;
797
+ }
798
+ function createApp(deps, opts) {
799
+ if (opts?.logger) setLogger(opts.logger);
800
+ setRuntimeEnv(opts?.runtimeEnv ?? process.env);
801
+ const app = new Hono();
802
+ const corsOrigin = opts?.corsOrigin ?? '*';
803
+ const staticRootDir = opts?.staticRootDir ?? DEFAULT_STATIC_ROOT;
804
+ app.use('*', cors({
805
+ origin: corsOrigin,
806
+ allowMethods: [
807
+ 'GET',
808
+ 'POST',
809
+ 'OPTIONS'
810
+ ],
811
+ allowHeaders: [
812
+ 'Content-Type',
813
+ 'Authorization'
814
+ ]
815
+ }));
816
+ app.use('*', loggerMiddleware);
817
+ app.use('*', createDiMiddleware(deps));
818
+ app.use('*', timeout(60000));
819
+ if (opts?.appExtensions?.length) for (const extend of opts.appExtensions)extend(app);
820
+ app.get('/tree-shaking-shared/healthz', (c)=>c.json({
821
+ status: 'ok',
822
+ timestamp: new Date().toISOString()
823
+ }));
824
+ app.route('/tree-shaking-shared', routes);
825
+ app.route('/', createStaticRoute({
826
+ rootDir: staticRootDir
827
+ }));
828
+ if (opts?.frontendAdapters?.length) for (const adapter of opts.frontendAdapters)adapter.register(app);
829
+ startPeriodicPrune(opts?.pruneIntervalMs);
830
+ return app;
831
+ }
832
+ function createServer(opts) {
833
+ const port = opts.port ?? 3000;
834
+ const hostname = opts.hostname ?? '0.0.0.0';
835
+ return serve({
836
+ fetch: opts.app.fetch,
837
+ port,
838
+ hostname,
839
+ overrideGlobalObjects: false
840
+ });
841
+ }
842
+ async function createAdapterDeps(params) {
843
+ const adapterId = params.adapterId;
844
+ const adapter = params.registry.getAdapterById(adapterId);
845
+ return adapter.create(params.adapterConfig ?? {}, {
846
+ logger: params.logger ?? logger_logger,
847
+ uploadedDir: params.uploadedDir ?? UPLOADED_DIR
848
+ });
849
+ }
850
+ function localObjectStore_define_property(obj, key, value) {
851
+ if (key in obj) Object.defineProperty(obj, key, {
852
+ value: value,
853
+ enumerable: true,
854
+ configurable: true,
855
+ writable: true
856
+ });
857
+ else obj[key] = value;
858
+ return obj;
859
+ }
860
+ class LocalObjectStore {
861
+ async exists(key) {
862
+ const filePath = node_path.join(this.rootDir, key.replace(/^\//, ''));
863
+ try {
864
+ const stat = await node_fs.promises.stat(filePath);
865
+ return stat.isFile();
866
+ } catch {
867
+ return false;
868
+ }
869
+ }
870
+ async uploadFile(localPath, key) {
871
+ const rel = key.replace(/^\//, '');
872
+ const dest = node_path.join(this.rootDir, rel);
873
+ await node_fs.promises.mkdir(node_path.dirname(dest), {
874
+ recursive: true
875
+ });
876
+ await node_fs.promises.copyFile(localPath, dest);
877
+ }
878
+ publicUrl(key) {
879
+ return `${this.publicBaseUrl}${key.replace(/^\//, '')}`;
880
+ }
881
+ constructor(opts){
882
+ localObjectStore_define_property(this, "rootDir", void 0);
883
+ localObjectStore_define_property(this, "publicBaseUrl", void 0);
884
+ this.rootDir = opts?.rootDir ?? node_path.join(process.cwd(), 'log', 'static');
885
+ const port = process.env.PORT || 3000;
886
+ const base = opts?.publicBaseUrl === '/' ? `http://localhost:${port}/` : opts?.publicBaseUrl ?? '/';
887
+ this.publicBaseUrl = base.endsWith('/') ? base : `${base}/`;
888
+ }
889
+ }
890
+ function adapter_define_property(obj, key, value) {
891
+ if (key in obj) Object.defineProperty(obj, key, {
892
+ value: value,
893
+ enumerable: true,
894
+ configurable: true,
895
+ writable: true
896
+ });
897
+ else obj[key] = value;
898
+ return obj;
899
+ }
900
+ function envStr(env, name, fallback) {
901
+ const v = env[name];
902
+ if (void 0 === v || '' === v) return fallback;
903
+ return v;
904
+ }
905
+ class LocalAdapter {
906
+ fromEnv(env) {
907
+ return {
908
+ rootDir: envStr(env, 'LOCAL_STORE_DIR', node_path.join(process.cwd(), 'log', 'static')),
909
+ publicBaseUrl: envStr(env, 'LOCAL_STORE_BASE_URL', '/')
910
+ };
911
+ }
912
+ async create(config, _context) {
913
+ const objectStore = new LocalObjectStore({
914
+ rootDir: config.rootDir,
915
+ publicBaseUrl: config.publicBaseUrl
916
+ });
917
+ return {
918
+ objectStore
919
+ };
920
+ }
921
+ constructor(){
922
+ adapter_define_property(this, "id", 'local');
923
+ }
924
+ }
925
+ function createAdapterRegistry(adapters) {
926
+ return {
927
+ adapters,
928
+ getAdapterById: (id)=>{
929
+ const found = adapters.find((a)=>a.id === id);
930
+ if (!found) throw new Error(`Unknown ADAPTER_ID: ${id}`);
931
+ return found;
932
+ }
933
+ };
934
+ }
935
+ function createOssAdapterRegistry() {
936
+ return createAdapterRegistry([
937
+ new LocalAdapter()
938
+ ]);
939
+ }
940
+ export { DEFAULT_STATIC_ROOT, LocalAdapter, LocalObjectStore, createAdapterDeps, createAdapterRegistry, createApp, createLogger, createOssAdapterRegistry, createServer };