Skip to content

Commit

Permalink
fix(unpkg): only perform GENERATION rewriting for old versions that s…
Browse files Browse the repository at this point in the history
…till have generation support
  • Loading branch information
trieloff committed Oct 2, 2023
1 parent d9ec94b commit fea4cab
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
12 changes: 8 additions & 4 deletions src/unpkg.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,14 @@ async function transformBody(resp, responseUrl, req) {
if (resp.ok
&& resp.status === 200
&& url.pathname.indexOf('@adobe/helix-rum-js') >= 0) {
const generation = url.searchParams.get('generation') || respURL.pathname.split(/[@\\/]/).slice(2, 5).join('-');
const text = await resp.text();
const body = text.replace(/__HELIX_RUM_JS_VERSION__/, generation.replace(/[^a-z0-9_.-]/ig, ''));
return new Response(body, { headers: resp.headers });
const urlversion = respURL.pathname.split(/[@\\/]/).slice(2, 5).pop();
if (urlversion === '1.0.0' || urlversion === '1.0.1') {
// only rewrite for 1.0.0 and 1.0.1 – newer versions don't need this
const generation = url.searchParams.get('generation') || respURL.pathname.split(/[@\\/]/).slice(2, 5).join('-');
const text = await resp.text();
const body = text.replace(/__HELIX_RUM_JS_VERSION__/, generation.replace(/[^a-z0-9_.-]/ig, ''));
return new Response(body, { headers: resp.headers });
}
}
return resp;
}
Expand Down
15 changes: 13 additions & 2 deletions test/unpkg.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,26 @@ describe('Test unpkg handler', () => {

it('handles @adobe/helix-rum request', async () => {
const req = {};
req.url = 'http://foo.bar.org/.rum/@adobe/helix-rum-js?generation=42';
req.url = 'http://foo.bar.org/.rum/@adobe/helix-rum-js@^1?generation=42';

const storedFetch = global.fetch;

try {
// Mock the global fetch function
global.fetch = (v, opts) => {
assert.equal('unpkg.com', opts.backend);
if (v.url === 'https://unpkg.com/@adobe/helix-rum-js') {
if (v.url === 'https://unpkg.com/@adobe/helix-rum-js@^1') {
const resp = {
ok: true,
url: v.url,
status: 302,
};
resp.headers = new Headers();
resp.headers.append('location', 'https://unpkg.com/@adobe/[email protected]');
resp.headers.append('foo-header', 'bar');
return resp;
}
if (v.url === 'https://unpkg.com/@adobe/[email protected]') {
const resp = {
ok: true,
url: v.url,
Expand Down

0 comments on commit fea4cab

Please sign in to comment.