fix(ui): serve SPA bundle from root, add cache-busting query, expose files API and include default CSS manager styles
This commit is contained in:
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* autocreated commitinfo by @push.rocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@serve.zone/opencdn',
|
||||
version: '1.0.4',
|
||||
description: 'A CDN that serves files directly from npm packages'
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
@@ -133,51 +133,49 @@ export class UiPublicServer {
|
||||
}
|
||||
});
|
||||
|
||||
// API endpoint for file listing (dev mode)
|
||||
if (this.options.mode === 'dev') {
|
||||
expressApplication.get('/api/files/:org/:package', async (req, res) => {
|
||||
const packageName = `${req.params.org}/${req.params.package}`;
|
||||
const version = (req.query.version as string) || '';
|
||||
const distTag = (req.query.disttag as string) || '';
|
||||
// API endpoint for file listing
|
||||
expressApplication.get('/api/files/:org/:package', async (req, res) => {
|
||||
const packageName = `${req.params.org}/${req.params.package}`;
|
||||
const version = (req.query.version as string) || '';
|
||||
const distTag = (req.query.disttag as string) || '';
|
||||
|
||||
if (!this.options.allowedPackages.includes(packageName)) {
|
||||
res.status(403).json({ error: 'Package not allowed' });
|
||||
return;
|
||||
}
|
||||
if (!this.options.allowedPackages.includes(packageName)) {
|
||||
res.status(403).json({ error: 'Package not allowed' });
|
||||
return;
|
||||
}
|
||||
|
||||
const baseDirectory = this.getPackageBaseDirectory(packageName);
|
||||
const registry = this.getPackageRegistry(packageName);
|
||||
const baseDirectory = this.getPackageBaseDirectory(packageName);
|
||||
const registry = this.getPackageRegistry(packageName);
|
||||
|
||||
try {
|
||||
const result = await registry.getFilesFromPackage(
|
||||
packageName,
|
||||
plugins.path.join(baseDirectory),
|
||||
{ version, distTag }
|
||||
);
|
||||
try {
|
||||
const result = await registry.getFilesFromPackage(
|
||||
packageName,
|
||||
plugins.path.join(baseDirectory),
|
||||
{ version, distTag }
|
||||
);
|
||||
|
||||
const files = result.map(smartfile => {
|
||||
return smartfile.path.replace(plugins.path.join('package', baseDirectory), '');
|
||||
});
|
||||
const files = result.map(smartfile => {
|
||||
return smartfile.path.replace(plugins.path.join('package', baseDirectory), '');
|
||||
});
|
||||
|
||||
// Get package info for versions
|
||||
const packageInfo = await registry.getPackageInfo(packageName).catch(() => null);
|
||||
const versions = packageInfo?.allVersions?.slice(-20).reverse().map(v => v.version) || [];
|
||||
// Get package info for versions
|
||||
const packageInfo = await registry.getPackageInfo(packageName).catch(() => null);
|
||||
const versions = packageInfo?.allVersions?.slice(-20).reverse().map(v => v.version) || [];
|
||||
|
||||
res.json({ files, versions });
|
||||
} catch (err) {
|
||||
res.status(404).json({ error: 'Package not found' });
|
||||
}
|
||||
});
|
||||
res.json({ files, versions });
|
||||
} catch (err) {
|
||||
res.status(404).json({ error: 'Package not found' });
|
||||
}
|
||||
});
|
||||
|
||||
// Peek route serves the SPA
|
||||
expressApplication.get('/peek/*', async (req, res) => {
|
||||
await this.serveIndexHtml(req, res);
|
||||
});
|
||||
// Peek route serves the SPA
|
||||
expressApplication.get('/peek/*', async (req, res) => {
|
||||
await this.serveIndexHtml(req, res);
|
||||
});
|
||||
|
||||
expressApplication.get('/peek', async (req, res) => {
|
||||
await this.serveIndexHtml(req, res);
|
||||
});
|
||||
}
|
||||
expressApplication.get('/peek', async (req, res) => {
|
||||
await this.serveIndexHtml(req, res);
|
||||
});
|
||||
|
||||
// Main package serving route
|
||||
expressApplication.use('/', async (req, res) => {
|
||||
@@ -229,6 +227,9 @@ export class UiPublicServer {
|
||||
const configScript = `<script>window.__OPENCDN_CONFIG__ = ${JSON.stringify(config)};</script>`;
|
||||
html = html.replace('</head>', `${configScript}\n</head>`);
|
||||
|
||||
// Add version query parameter to bundle.js for cache busting
|
||||
html = html.replace('/bundle.js', `/bundle.js?v=${this.projectinfo.version}`);
|
||||
|
||||
res.setHeader('content-type', embeddedFile.contentType);
|
||||
res.setHeader('cache-control', 'no-cache');
|
||||
res.status(200).send(html);
|
||||
|
||||
Reference in New Issue
Block a user