@module-federation/treeshake-server 0.0.1-alpha.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 (64) hide show
  1. package/README.md +71 -0
  2. package/bin/treeshake-server.js +35 -0
  3. package/dist/adapters/createAdapterDeps.d.ts +10 -0
  4. package/dist/adapters/local/adapter.d.ts +10 -0
  5. package/dist/adapters/local/index.d.ts +3 -0
  6. package/dist/adapters/local/localObjectStore.d.ts +13 -0
  7. package/dist/adapters/registry.d.ts +7 -0
  8. package/dist/adapters/types.d.ts +74 -0
  9. package/dist/app.d.ts +18 -0
  10. package/dist/cli/ossEnv.d.ts +18 -0
  11. package/dist/domain/build/constant.d.ts +1 -0
  12. package/dist/domain/build/normalize-config.d.ts +22 -0
  13. package/dist/domain/build/retrieve-global-name.d.ts +1 -0
  14. package/dist/domain/build/schema.d.ts +31 -0
  15. package/dist/domain/upload/constant.d.ts +2 -0
  16. package/dist/domain/upload/retrieve-cdn-path.d.ts +4 -0
  17. package/dist/frontend/adapter/index.d.ts +13 -0
  18. package/dist/frontend/adapter/index.js +128 -0
  19. package/dist/frontend/adapter/index.mjs +83 -0
  20. package/dist/frontend/favicon.ico +0 -0
  21. package/dist/frontend/index.html +1 -0
  22. package/dist/frontend/static/css/index.16175e0f.css +1 -0
  23. package/dist/frontend/static/js/296.084d1b43.js +2 -0
  24. package/dist/frontend/static/js/296.084d1b43.js.LICENSE.txt +16 -0
  25. package/dist/frontend/static/js/async/873.21368adc.js +2 -0
  26. package/dist/frontend/static/js/async/951.ec9191e2.js +12 -0
  27. package/dist/frontend/static/js/async/951.ec9191e2.js.LICENSE.txt +6 -0
  28. package/dist/frontend/static/js/async/987.86ff6794.js +2 -0
  29. package/dist/frontend/static/js/async/987.86ff6794.js.LICENSE.txt +6 -0
  30. package/dist/frontend/static/js/index.5488f626.js +88 -0
  31. package/dist/frontend/static/js/lib-react.c59642e3.js +2 -0
  32. package/dist/frontend/static/js/lib-react.c59642e3.js.LICENSE.txt +39 -0
  33. package/dist/frontend/static/js/lib-router.75e1e689.js +4 -0
  34. package/dist/frontend/static/js/lib-router.75e1e689.js.LICENSE.txt +10 -0
  35. package/dist/http/env.d.ts +10 -0
  36. package/dist/http/middlewares/di.middleware.d.ts +4 -0
  37. package/dist/http/middlewares/logger.middleware.d.ts +3 -0
  38. package/dist/http/routes/build.d.ts +3 -0
  39. package/dist/http/routes/index.d.ts +2 -0
  40. package/dist/http/routes/maintenance.d.ts +3 -0
  41. package/dist/http/routes/static.d.ts +5 -0
  42. package/dist/index.d.ts +10 -0
  43. package/dist/index.js +1028 -0
  44. package/dist/index.mjs +957 -0
  45. package/dist/infra/logger.d.ts +6 -0
  46. package/dist/nodeServer.d.ts +7 -0
  47. package/dist/ports/objectStore.d.ts +1 -0
  48. package/dist/ports/projectPublisher.d.ts +1 -0
  49. package/dist/server.d.ts +2 -0
  50. package/dist/server.js +1160 -0
  51. package/dist/server.mjs +1126 -0
  52. package/dist/services/buildService.d.ts +8 -0
  53. package/dist/services/cacheService.d.ts +15 -0
  54. package/dist/services/pnpmMaintenance.d.ts +4 -0
  55. package/dist/services/uploadService.d.ts +36 -0
  56. package/dist/template/re-shake-share/Collect.js +115 -0
  57. package/dist/template/re-shake-share/EmitManifest.js +49 -0
  58. package/dist/template/re-shake-share/index.ts +1 -0
  59. package/dist/template/re-shake-share/package.json +23 -0
  60. package/dist/template/re-shake-share/rspack.config.ts +33 -0
  61. package/dist/utils/runCommand.d.ts +20 -0
  62. package/dist/utils/runtimeEnv.d.ts +3 -0
  63. package/dist/utils/uploadSdk.d.ts +10 -0
  64. package/package.json +68 -0
package/dist/index.mjs ADDED
@@ -0,0 +1,957 @@
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 { serve } from "@hono/node-server";
16
+ function createDiMiddleware(deps) {
17
+ return async (c, next)=>{
18
+ c.set('objectStore', deps.objectStore);
19
+ if (deps.projectPublisher) c.set('projectPublisher', deps.projectPublisher);
20
+ await next();
21
+ };
22
+ }
23
+ const createLogger = (opts)=>pino({
24
+ level: (null == opts ? void 0 : opts.level) ?? 'info',
25
+ formatters: {
26
+ level (label) {
27
+ return {
28
+ level: label
29
+ };
30
+ }
31
+ }
32
+ });
33
+ let logger_logger = createLogger();
34
+ const setLogger = (next)=>{
35
+ logger_logger = next;
36
+ };
37
+ const loggerMiddleware = async (c, next)=>{
38
+ const category = c.req.path.split('/')[1] || 'root';
39
+ const child = logger_logger.child({
40
+ category,
41
+ path: c.req.path,
42
+ method: c.req.method
43
+ });
44
+ c.set('logger', child);
45
+ await next();
46
+ };
47
+ const parseNormalizedKey = (key)=>{
48
+ const res = key.split('@');
49
+ return {
50
+ name: res.slice(0, -1).join('@'),
51
+ version: res[res.length - 1]
52
+ };
53
+ };
54
+ const normalizedKey = (name, v)=>`${name}@${v}`;
55
+ function normalizeConfig(config) {
56
+ const { shared, plugins, target, libraryType, hostName, uploadOptions } = config;
57
+ const commonNormalizedConfig = {
58
+ plugins: (null == plugins ? void 0 : plugins.sort(([a], [b])=>a.localeCompare(b))) ?? [],
59
+ target: Array.isArray(target) ? [
60
+ ...target
61
+ ].sort() : [
62
+ target
63
+ ],
64
+ uploadOptions,
65
+ libraryType,
66
+ hostName
67
+ };
68
+ const normalizedConfig = {};
69
+ shared.forEach(([sharedName, version, usedExports], index)=>{
70
+ const key = normalizedKey(sharedName, version);
71
+ normalizedConfig[key] = {
72
+ ...commonNormalizedConfig,
73
+ 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('')}`)),
74
+ usedExports
75
+ };
76
+ });
77
+ return normalizedConfig;
78
+ }
79
+ function extractBuildConfig(config, type) {
80
+ const { shared, plugins, target, libraryType, usedExports, hostName } = config;
81
+ return {
82
+ shared,
83
+ plugins,
84
+ target,
85
+ libraryType,
86
+ usedExports,
87
+ type,
88
+ hostName
89
+ };
90
+ }
91
+ const UploadOptionsSchema = z.object({
92
+ scmName: z.string(),
93
+ bucketName: z.string(),
94
+ publicFilePath: z.string(),
95
+ publicPath: z.string(),
96
+ cdnRegion: z.string()
97
+ });
98
+ const BasicSchema = z.object({
99
+ shared: z.array(z.tuple([
100
+ z.string(),
101
+ z.string(),
102
+ z.array(z.string())
103
+ ])).describe('List of plugins: [name, version, usedExports]'),
104
+ plugins: z.array(z.tuple([
105
+ z.string(),
106
+ z.string().optional()
107
+ ])).describe('Specify extra build plugin names, support specify version in second item').optional(),
108
+ target: z.union([
109
+ z.string(),
110
+ z.array(z.string())
111
+ ]).describe('Used to configure the target environment of Rspack output and the ECMAScript version of Rspack runtime code, the same with rspack#target'),
112
+ libraryType: z.string(),
113
+ hostName: z.string().describe('The name of the host app / mf')
114
+ });
115
+ const ConfigSchema = BasicSchema.extend({
116
+ uploadOptions: UploadOptionsSchema.optional()
117
+ });
118
+ const CheckTreeShakingSchema = ConfigSchema.extend({
119
+ uploadOptions: UploadOptionsSchema.optional()
120
+ });
121
+ const STATS_NAME = 'mf-stats.json';
122
+ let runtimeEnv = {};
123
+ const setRuntimeEnv = (env)=>{
124
+ runtimeEnv = env;
125
+ };
126
+ const getRuntimeEnv = ()=>runtimeEnv;
127
+ function _define_property(obj, key, value) {
128
+ if (key in obj) Object.defineProperty(obj, key, {
129
+ value: value,
130
+ enumerable: true,
131
+ configurable: true,
132
+ writable: true
133
+ });
134
+ else obj[key] = value;
135
+ return obj;
136
+ }
137
+ class CommandExecutionError extends Error {
138
+ constructor(command, exitCode, stdout, stderr){
139
+ 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);
140
+ this.name = 'CommandExecutionError';
141
+ this.command = command;
142
+ this.exitCode = exitCode;
143
+ this.stdout = stdout;
144
+ this.stderr = stderr;
145
+ }
146
+ }
147
+ const runCommand = (command, options = {})=>new Promise((resolve, reject)=>{
148
+ const stdoutChunks = [];
149
+ const stderrChunks = [];
150
+ const child = spawn(command, {
151
+ cwd: options.cwd,
152
+ env: {
153
+ ...getRuntimeEnv(),
154
+ ...options.env
155
+ },
156
+ shell: true,
157
+ stdio: [
158
+ 'ignore',
159
+ 'pipe',
160
+ 'pipe'
161
+ ]
162
+ });
163
+ child.stdout.on('data', (chunk)=>{
164
+ stdoutChunks.push(chunk.toString());
165
+ });
166
+ child.stderr.on('data', (chunk)=>{
167
+ stderrChunks.push(chunk.toString());
168
+ });
169
+ child.on('error', (error)=>{
170
+ reject(error);
171
+ });
172
+ child.on('close', (exitCode)=>{
173
+ const stdout = stdoutChunks.join('');
174
+ const stderr = stderrChunks.join('');
175
+ if (0 === exitCode) return void resolve({
176
+ stdout,
177
+ stderr,
178
+ exitCode
179
+ });
180
+ reject(new CommandExecutionError(command, exitCode ?? -1, stdout, stderr));
181
+ });
182
+ });
183
+ let installs = 0;
184
+ let pruneScheduled = false;
185
+ let pruning = false;
186
+ const DEFAULT_INTERVAL = 3600000;
187
+ const markInstallStart = ()=>{
188
+ installs++;
189
+ };
190
+ const markInstallEnd = ()=>{
191
+ installs = Math.max(0, installs - 1);
192
+ schedulePruneSoon();
193
+ };
194
+ const schedulePruneSoon = (delay = 30000)=>{
195
+ if (pruneScheduled) return;
196
+ pruneScheduled = true;
197
+ setTimeout(()=>{
198
+ pruneScheduled = false;
199
+ maybePrune();
200
+ }, delay);
201
+ };
202
+ const maybePrune = async ()=>{
203
+ if (pruning || installs > 0) return void schedulePruneSoon(60000);
204
+ pruning = true;
205
+ try {
206
+ await runCommand('pnpm store prune');
207
+ } catch {} finally{
208
+ pruning = false;
209
+ }
210
+ };
211
+ const startPeriodicPrune = (intervalMs = DEFAULT_INTERVAL)=>{
212
+ if (intervalMs <= 0) return;
213
+ setInterval(()=>{
214
+ maybePrune();
215
+ }, intervalMs);
216
+ };
217
+ const createUniqueTempDirByKey = async (key)=>{
218
+ const base = node_path.join(node_os.tmpdir(), `re-shake-share-${key}`);
219
+ let candidate = base;
220
+ for(;;)try {
221
+ await promises.mkdir(candidate, {
222
+ recursive: false
223
+ });
224
+ return candidate;
225
+ } catch {
226
+ const rand = Math.floor(1e9 * Math.random());
227
+ candidate = `${base}-${rand}`;
228
+ }
229
+ };
230
+ const prepareProject = async (config, excludeShared)=>{
231
+ const key = createHash('sha256').update(JSON.stringify(config)).digest('hex');
232
+ const dir = await createUniqueTempDirByKey(key);
233
+ const templateDir = node_path.join(__dirname, '.', 'template', 're-shake-share');
234
+ await promises.cp(templateDir, dir, {
235
+ recursive: true
236
+ });
237
+ const pkgPath = node_path.join(dir, 'package.json');
238
+ const indexPath = node_path.join(dir, 'index.ts');
239
+ const rspackConfigPath = node_path.join(dir, 'rspack.config.ts');
240
+ const [pkgContent, indexContent, rspackConfigContent] = await Promise.all([
241
+ promises.readFile(pkgPath, 'utf-8'),
242
+ promises.readFile(indexPath, 'utf-8'),
243
+ promises.readFile(rspackConfigPath, 'utf-8')
244
+ ]);
245
+ const pkg = JSON.parse(pkgContent);
246
+ const deps = {
247
+ ...pkg.dependencies || {}
248
+ };
249
+ const shared = {};
250
+ const mfConfig = {
251
+ name: 're_shake',
252
+ library: {
253
+ type: 'global'
254
+ },
255
+ manifest: true,
256
+ shared
257
+ };
258
+ let pluginImportStr = '';
259
+ let pluginOptionStr = '[\n';
260
+ let sharedImport = '';
261
+ Object.entries(config).forEach(([key, { plugins = [], libraryType, usedExports, hostName }], index)=>{
262
+ const { name, version } = parseNormalizedKey(key);
263
+ deps[name] = version;
264
+ if (!index) {
265
+ plugins.forEach(([pluginName, pluginVersion], pIndex)=>{
266
+ deps[pluginName] = pluginVersion ?? 'latest';
267
+ const pluginImportName = `plugin_${pIndex}`;
268
+ pluginImportStr += `import ${pluginImportName} from '${pluginName}';\n`;
269
+ pluginOptionStr += `new ${pluginImportName}(),`;
270
+ });
271
+ mfConfig.library.type = libraryType;
272
+ mfConfig.name = hostName;
273
+ }
274
+ shared[name] = {
275
+ requiredVersion: version,
276
+ version,
277
+ treeShaking: excludeShared.some(([n, v])=>n === name && v === version) ? void 0 : {
278
+ usedExports,
279
+ mode: 'server-calc'
280
+ }
281
+ };
282
+ sharedImport += `import shared_${index} from '${name}';\n`;
283
+ });
284
+ pluginOptionStr += '\n]';
285
+ pkg.dependencies = deps;
286
+ const newPkgContent = JSON.stringify(pkg, null, 2);
287
+ const sharedImportPlaceholder = "${SHARED_IMPORT}";
288
+ const newIndexContent = indexContent.replace(sharedImportPlaceholder, sharedImport);
289
+ const pluginsPlaceholder = "${ PLUGINS }";
290
+ const mfConfigPlaceholder = "${ MF_CONFIG }";
291
+ let cfg = rspackConfigContent;
292
+ cfg += pluginImportStr;
293
+ cfg = cfg.replace(pluginsPlaceholder, pluginOptionStr);
294
+ cfg = cfg.replace(mfConfigPlaceholder, JSON.stringify(mfConfig, null, 2));
295
+ await Promise.all([
296
+ promises.writeFile(pkgPath, newPkgContent),
297
+ promises.writeFile(indexPath, newIndexContent),
298
+ promises.writeFile(rspackConfigPath, cfg)
299
+ ]);
300
+ return dir;
301
+ };
302
+ const installDependencies = async (cwd)=>{
303
+ markInstallStart();
304
+ try {
305
+ await runCommand("pnpm i --ignore-scripts --no-frozen-lockfile --prefer-offline --reporter=silent --shamefully-hoist", {
306
+ cwd,
307
+ env: {
308
+ npm_config_registry: 'https://registry.npmjs.org/'
309
+ }
310
+ });
311
+ } finally{
312
+ markInstallEnd();
313
+ }
314
+ };
315
+ const buildProject = async (cwd, type)=>{
316
+ const scripts = [];
317
+ if ('re-shake' === type) scripts.push('pnpm build');
318
+ else scripts.push('pnpm build:full');
319
+ await Promise.all(scripts.map((script)=>runCommand(script, {
320
+ cwd
321
+ })));
322
+ };
323
+ const retrieveSharedFilepaths = async (projectDir, type)=>{
324
+ const sharedFilepaths = [];
325
+ const collectSharedFilepaths = async (t)=>{
326
+ const dir = 'full' === t ? 'full-shared' : 'dist';
327
+ const distDir = node_path.join(projectDir, dir);
328
+ const statsContent = await promises.readFile(node_path.join(distDir, STATS_NAME), 'utf-8');
329
+ const stats = JSON.parse(statsContent);
330
+ stats.shared.forEach((s)=>{
331
+ const { name, version, fallback, fallbackName } = s;
332
+ if (fallback && fallbackName) {
333
+ var _s_usedExports;
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 ?? (null == (_s_usedExports = s.usedExports) ? void 0 : _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-011501';
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
+ const cdnPath = retrieveCDNPath({
532
+ config,
533
+ sharedKey,
534
+ type: item.type
535
+ });
536
+ await store.downloadFile(cdnPath, localPath);
537
+ const tDownload = Date.now() - t0;
538
+ logger_logger.info(`Downloaded ${item.name}@${item.version} in ${tDownload}ms`);
539
+ const newCdnUrl = await publisher.publishFile({
540
+ localPath,
541
+ sharedName: item.name,
542
+ hash,
543
+ options: uploadOptions
544
+ });
545
+ uploaded.push({
546
+ ...item,
547
+ cdnUrl: newCdnUrl
548
+ });
549
+ } catch (error) {
550
+ logger_logger.error(`Failed to upload ${item.name}@${item.version}: ${error}`);
551
+ }
552
+ for (const s of sharedFilePaths)try {
553
+ const config = normalizedConfig[normalizedKey(s.name, s.version)];
554
+ if (!config) {
555
+ logger_logger.error(`No config found for ${s.name}`);
556
+ continue;
557
+ }
558
+ const { uploadOptions } = config;
559
+ if (!uploadOptions) throw new Error(`No uploadOptions found for ${s.name}`);
560
+ const hash = createCacheHash({
561
+ ...config,
562
+ plugins: config.plugins || []
563
+ }, s.type);
564
+ const cdnUrl = await publisher.publishFile({
565
+ localPath: s.filepath,
566
+ sharedName: s.name,
567
+ hash,
568
+ options: uploadOptions
569
+ });
570
+ uploaded.push({
571
+ name: s.name,
572
+ version: s.version,
573
+ globalName: s.globalName,
574
+ cdnUrl,
575
+ type: s.type
576
+ });
577
+ } catch (error) {
578
+ logger_logger.error(`Failed to upload ${s.name}@${s.version}: ${error}`);
579
+ }
580
+ return uploaded;
581
+ } finally{
582
+ await promises.rm(tmpDir, {
583
+ recursive: true,
584
+ force: true
585
+ });
586
+ }
587
+ }
588
+ async function upload(sharedFilePaths, uploadResults, normalizedConfig, uploadOptions, store, publisher) {
589
+ const cacheUploaded = await uploadToCacheStore(sharedFilePaths, normalizedConfig, store);
590
+ if (!uploadOptions) {
591
+ const hydrated = await Promise.all(uploadResults.map(async (item)=>{
592
+ if ('full' !== item.type) return item;
593
+ const tmpDir = await createUniqueTempDirByKey(`download-full-json${Date.now().toString()}`);
594
+ const jsonPath = node_path.join(tmpDir, `${item.name}-${item.version}.json`);
595
+ try {
596
+ const tJson0 = Date.now();
597
+ const sharedKey = normalizedKey(item.name, item.version);
598
+ const config = normalizedConfig[sharedKey];
599
+ if (config) {
600
+ const cdnPath = retrieveCDNPath({
601
+ config,
602
+ sharedKey,
603
+ type: item.type
604
+ });
605
+ const jsonCdnPath = cdnPath.replace(/\.js$/, '.json');
606
+ await store.downloadFile(jsonCdnPath, jsonPath);
607
+ } else await downloadToFile(item.cdnUrl.replace('.js', '.json'), jsonPath);
608
+ const tJson = Date.now() - tJson0;
609
+ logger_logger.info(`Downloaded ${item.name}@${item.version} json in ${tJson}ms`);
610
+ const jsonContent = JSON.parse(await promises.readFile(jsonPath, 'utf8'));
611
+ return {
612
+ ...item,
613
+ canTreeShaking: jsonContent.canTreeShaking,
614
+ modules: jsonContent.modules
615
+ };
616
+ } catch (jsonError) {
617
+ logger_logger.error(`Failed to download ${item.name}@${item.version} json: ${jsonError}`);
618
+ return {
619
+ ...item,
620
+ canTreeShaking: item.canTreeShaking ?? true
621
+ };
622
+ } finally{
623
+ await promises.rm(tmpDir, {
624
+ recursive: true,
625
+ force: true
626
+ });
627
+ }
628
+ }));
629
+ return [
630
+ ...cacheUploaded,
631
+ ...hydrated
632
+ ];
633
+ }
634
+ if (!publisher) throw new Error('uploadOptions provided but no projectPublisher configured (configure the selected adapter to enable it or omit uploadOptions)');
635
+ const projectUploadResults = await uploadProject(uploadResults, sharedFilePaths, normalizedConfig, publisher, store);
636
+ return projectUploadResults;
637
+ }
638
+ const buildLimit = p_limit(Math.max(1, node_os.cpus().length));
639
+ const buildRoute = new Hono();
640
+ buildRoute.post('/', zValidator('json', ConfigSchema), async (c)=>{
641
+ const logger = c.get('logger');
642
+ const startTime = Date.now();
643
+ const jobId = nanoid();
644
+ logger.info(jobId);
645
+ const body = c.req.valid('json');
646
+ logger.info(JSON.stringify(body));
647
+ const normalizedConfig = normalizeConfig(body);
648
+ const store = c.get('objectStore');
649
+ const publisher = c.get('projectPublisher');
650
+ try {
651
+ const t0 = Date.now();
652
+ const { cacheItems, excludeShared, restConfig } = await retrieveCacheItems(normalizedConfig, 're-shake', store);
653
+ const tRetrieveCache = Date.now();
654
+ logger.info(`retrieveCacheItems took ${tRetrieveCache - t0}ms`);
655
+ let sharedFilePaths = [];
656
+ let dir;
657
+ if (Object.keys(restConfig).length > 0) {
658
+ const buildResult = await buildLimit(()=>runBuild(normalizedConfig, excludeShared, 're-shake'));
659
+ sharedFilePaths = buildResult.sharedFilePaths;
660
+ dir = buildResult.dir;
661
+ }
662
+ const tBuild = Date.now();
663
+ logger.info(`runBuild took ${tBuild - tRetrieveCache}ms`);
664
+ const uploadResults = await upload(sharedFilePaths, cacheItems, normalizedConfig, body.uploadOptions, store, publisher);
665
+ const tUpload = Date.now();
666
+ logger.info(`upload took ${tUpload - tBuild}ms`);
667
+ cleanUp(dir).catch((err)=>{
668
+ logger.error(`Failed to cleanup dir ${dir}: ${err}`);
669
+ });
670
+ const tCleanUp = Date.now();
671
+ logger.info(`cleanUp scheduled (non-blocking) took ${tCleanUp - tUpload}ms`);
672
+ return c.json({
673
+ jobId,
674
+ status: 'success',
675
+ data: uploadResults,
676
+ cached: cacheItems,
677
+ duration: Date.now() - startTime
678
+ });
679
+ } catch (error) {
680
+ if (error instanceof CommandExecutionError) {
681
+ if (137 === error.exitCode) maybePrune();
682
+ return c.json({
683
+ jobId,
684
+ status: 'failed',
685
+ error: error.message,
686
+ command: error.command,
687
+ exitCode: error.exitCode,
688
+ stdout: error.stdout,
689
+ stderr: error.stderr
690
+ });
691
+ }
692
+ return c.json({
693
+ jobId,
694
+ status: 'failed',
695
+ error: error instanceof Error ? error.message : 'Unknown error'
696
+ });
697
+ }
698
+ });
699
+ async function handleCheckTreeshake(c, body) {
700
+ const logger = c.get('logger');
701
+ const startTime = Date.now();
702
+ const jobId = nanoid();
703
+ logger.info(jobId);
704
+ logger.info(JSON.stringify(body));
705
+ const normalizedConfig = normalizeConfig(body);
706
+ const store = c.get('objectStore');
707
+ const publisher = c.get('projectPublisher');
708
+ try {
709
+ const t0 = Date.now();
710
+ const { cacheItems, excludeShared, restConfig } = await retrieveCacheItems(normalizedConfig, 'full', store);
711
+ const tRetrieveCache = Date.now();
712
+ logger.info(`retrieveCacheItems took ${tRetrieveCache - t0}ms`);
713
+ let sharedFilePaths = [];
714
+ let dir;
715
+ if (Object.keys(restConfig).length > 0) {
716
+ const buildResult = await buildLimit(()=>runBuild(normalizedConfig, excludeShared, 'full'));
717
+ sharedFilePaths = buildResult.sharedFilePaths;
718
+ dir = buildResult.dir;
719
+ }
720
+ const tBuild = Date.now();
721
+ logger.info(`runBuild took ${tBuild - tRetrieveCache}ms`);
722
+ const uploadResults = await upload(sharedFilePaths, cacheItems, normalizedConfig, body.uploadOptions, store, publisher);
723
+ const tUpload = Date.now();
724
+ logger.info(`upload took ${tUpload - tBuild}ms`);
725
+ cleanUp(dir).catch((err)=>{
726
+ logger.error(`Failed to cleanup dir ${dir}: ${err}`);
727
+ });
728
+ const tCleanUp = Date.now();
729
+ logger.info(`cleanUp scheduled (non-blocking) took ${tCleanUp - tUpload}ms`);
730
+ return c.json({
731
+ jobId,
732
+ status: 'success',
733
+ data: uploadResults,
734
+ cached: cacheItems,
735
+ duration: Date.now() - startTime
736
+ });
737
+ } catch (error) {
738
+ if (error instanceof CommandExecutionError) {
739
+ if (137 === error.exitCode) maybePrune();
740
+ return c.json({
741
+ jobId,
742
+ status: 'failed',
743
+ error: error.message,
744
+ command: error.command,
745
+ exitCode: error.exitCode,
746
+ stdout: error.stdout,
747
+ stderr: error.stderr
748
+ });
749
+ }
750
+ return c.json({
751
+ jobId,
752
+ status: 'failed',
753
+ error: error instanceof Error ? error.message : 'Unknown error'
754
+ });
755
+ }
756
+ }
757
+ buildRoute.post('/check-tree-shaking', zValidator('json', CheckTreeShakingSchema), async (c)=>handleCheckTreeshake(c, c.req.valid('json')));
758
+ const maintenanceRoute = new Hono();
759
+ maintenanceRoute.post('/', async (c)=>{
760
+ const logger = c.get('logger');
761
+ const jobId = nanoid();
762
+ const startTime = Date.now();
763
+ logger.info(jobId);
764
+ await maybePrune();
765
+ return c.json({
766
+ jobId,
767
+ status: 'success',
768
+ duration: Date.now() - startTime
769
+ });
770
+ });
771
+ const routes = new Hono();
772
+ routes.route('/build', buildRoute);
773
+ routes.route('/clean-cache', maintenanceRoute);
774
+ function contentTypeByExt(filePath) {
775
+ if (filePath.endsWith('.js')) return "application/javascript";
776
+ if (filePath.endsWith('.json')) return 'application/json';
777
+ return 'application/octet-stream';
778
+ }
779
+ const DEFAULT_STATIC_ROOT = node_path.join(process.cwd(), 'log', 'static');
780
+ async function serveLocalFile(c, rootDir) {
781
+ let requestPath = c.req.path;
782
+ try {
783
+ requestPath = decodeURIComponent(requestPath);
784
+ } catch {
785
+ return c.text('Not Found', 404);
786
+ }
787
+ const relPath = requestPath.replace(/^\/+/, '');
788
+ const rootResolved = node_path.resolve(rootDir);
789
+ const filePath = node_path.resolve(rootResolved, relPath);
790
+ if (filePath !== rootResolved && !filePath.startsWith(`${rootResolved}${node_path.sep}`)) return c.text('Not Found', 404);
791
+ try {
792
+ const buf = await node_fs.promises.readFile(filePath);
793
+ return new Response(buf, {
794
+ status: 200,
795
+ headers: {
796
+ 'Content-Type': contentTypeByExt(filePath)
797
+ }
798
+ });
799
+ } catch {
800
+ return c.text('Not Found', 404);
801
+ }
802
+ }
803
+ function createStaticRoute(opts) {
804
+ const staticRoute = new Hono();
805
+ const rootDir = (null == opts ? void 0 : opts.rootDir) ?? DEFAULT_STATIC_ROOT;
806
+ staticRoute.get('/tree-shaking-shared/*', async (c)=>serveLocalFile(c, rootDir));
807
+ return staticRoute;
808
+ }
809
+ function createApp(deps, opts) {
810
+ var _opts_appExtensions, _opts_frontendAdapters;
811
+ if (null == opts ? void 0 : opts.logger) setLogger(opts.logger);
812
+ setRuntimeEnv((null == opts ? void 0 : opts.runtimeEnv) ?? process.env);
813
+ const app = new Hono();
814
+ const corsOrigin = (null == opts ? void 0 : opts.corsOrigin) ?? '*';
815
+ const staticRootDir = (null == opts ? void 0 : opts.staticRootDir) ?? DEFAULT_STATIC_ROOT;
816
+ app.use('*', cors({
817
+ origin: corsOrigin,
818
+ allowMethods: [
819
+ 'GET',
820
+ 'POST',
821
+ 'OPTIONS'
822
+ ],
823
+ allowHeaders: [
824
+ 'Content-Type',
825
+ 'Authorization'
826
+ ]
827
+ }));
828
+ app.use('*', loggerMiddleware);
829
+ app.use('*', createDiMiddleware(deps));
830
+ if (null == opts ? void 0 : null == (_opts_appExtensions = opts.appExtensions) ? void 0 : _opts_appExtensions.length) for (const extend of opts.appExtensions)extend(app);
831
+ app.get('/tree-shaking-shared/healthz', (c)=>c.json({
832
+ status: 'ok',
833
+ timestamp: new Date().toISOString()
834
+ }));
835
+ app.route('/tree-shaking-shared', routes);
836
+ app.route('/', createStaticRoute({
837
+ rootDir: staticRootDir
838
+ }));
839
+ if (null == opts ? void 0 : null == (_opts_frontendAdapters = opts.frontendAdapters) ? void 0 : _opts_frontendAdapters.length) for (const adapter of opts.frontendAdapters)adapter.register(app);
840
+ startPeriodicPrune(null == opts ? void 0 : opts.pruneIntervalMs);
841
+ return app;
842
+ }
843
+ function createServer(opts) {
844
+ const port = opts.port ?? 3000;
845
+ const hostname = opts.hostname ?? '0.0.0.0';
846
+ return serve({
847
+ fetch: opts.app.fetch,
848
+ port,
849
+ hostname
850
+ });
851
+ }
852
+ async function createAdapterDeps(params) {
853
+ const adapterId = params.adapterId;
854
+ const adapter = params.registry.getAdapterById(adapterId);
855
+ return adapter.create(params.adapterConfig ?? {}, {
856
+ logger: params.logger ?? logger_logger,
857
+ uploadedDir: params.uploadedDir ?? UPLOADED_DIR
858
+ });
859
+ }
860
+ function localObjectStore_define_property(obj, key, value) {
861
+ if (key in obj) Object.defineProperty(obj, key, {
862
+ value: value,
863
+ enumerable: true,
864
+ configurable: true,
865
+ writable: true
866
+ });
867
+ else obj[key] = value;
868
+ return obj;
869
+ }
870
+ class LocalObjectStore {
871
+ async exists(key) {
872
+ const filePath = node_path.join(this.rootDir, key.replace(/^\//, ''));
873
+ try {
874
+ const stat = await node_fs.promises.stat(filePath);
875
+ return stat.isFile();
876
+ } catch {
877
+ return false;
878
+ }
879
+ }
880
+ async uploadFile(localPath, key) {
881
+ const rel = key.replace(/^\//, '');
882
+ const dest = node_path.join(this.rootDir, rel);
883
+ await node_fs.promises.mkdir(node_path.dirname(dest), {
884
+ recursive: true
885
+ });
886
+ await node_fs.promises.copyFile(localPath, dest);
887
+ }
888
+ async downloadFile(key, localPath) {
889
+ const rel = key.replace(/^\//, '');
890
+ const src = node_path.join(this.rootDir, rel);
891
+ await node_fs.promises.mkdir(node_path.dirname(localPath), {
892
+ recursive: true
893
+ });
894
+ await node_fs.promises.copyFile(src, localPath);
895
+ }
896
+ publicUrl(key) {
897
+ return `${this.publicBaseUrl}${key.replace(/^\//, '')}`;
898
+ }
899
+ constructor(opts){
900
+ localObjectStore_define_property(this, "rootDir", void 0);
901
+ localObjectStore_define_property(this, "publicBaseUrl", void 0);
902
+ this.rootDir = (null == opts ? void 0 : opts.rootDir) ?? node_path.join(process.cwd(), 'log', 'static');
903
+ const base = (null == opts ? void 0 : opts.publicBaseUrl) ?? '/';
904
+ this.publicBaseUrl = base.endsWith('/') ? base : `${base}/`;
905
+ }
906
+ }
907
+ function adapter_define_property(obj, key, value) {
908
+ if (key in obj) Object.defineProperty(obj, key, {
909
+ value: value,
910
+ enumerable: true,
911
+ configurable: true,
912
+ writable: true
913
+ });
914
+ else obj[key] = value;
915
+ return obj;
916
+ }
917
+ function envStr(env, name, fallback) {
918
+ const v = env[name];
919
+ if (void 0 === v || '' === v) return fallback;
920
+ return v;
921
+ }
922
+ class LocalAdapter {
923
+ fromEnv(env) {
924
+ return {
925
+ rootDir: envStr(env, 'LOCAL_STORE_DIR', node_path.join(process.cwd(), 'log', 'static')),
926
+ publicBaseUrl: envStr(env, 'LOCAL_STORE_BASE_URL', '/')
927
+ };
928
+ }
929
+ async create(config, _context) {
930
+ const objectStore = new LocalObjectStore({
931
+ rootDir: config.rootDir,
932
+ publicBaseUrl: config.publicBaseUrl
933
+ });
934
+ return {
935
+ objectStore
936
+ };
937
+ }
938
+ constructor(){
939
+ adapter_define_property(this, "id", 'local');
940
+ }
941
+ }
942
+ function createAdapterRegistry(adapters) {
943
+ return {
944
+ adapters,
945
+ getAdapterById: (id)=>{
946
+ const found = adapters.find((a)=>a.id === id);
947
+ if (!found) throw new Error(`Unknown ADAPTER_ID: ${id}`);
948
+ return found;
949
+ }
950
+ };
951
+ }
952
+ function createOssAdapterRegistry() {
953
+ return createAdapterRegistry([
954
+ new LocalAdapter()
955
+ ]);
956
+ }
957
+ export { LocalAdapter, LocalObjectStore, createAdapterDeps, createAdapterRegistry, createApp, createLogger, createOssAdapterRegistry, createServer };