Skip to content

Commit

Permalink
fix: allow static html with selectors
Browse files Browse the repository at this point in the history
fixes #481
  • Loading branch information
tripodsan committed Jan 23, 2024
1 parent 11fc94e commit 33757b1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 60 deletions.
3 changes: 1 addition & 2 deletions src/html-pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import unwrapSoleImages from './steps/unwrap-sole-images.js';
import tohtml from './steps/stringify-response.js';
import { PipelineStatusError } from './PipelineStatusError.js';
import { PipelineResponse } from './PipelineResponse.js';
import { validatePathInfo } from './utils/path.js';
import { initAuthRoute } from './utils/auth.js';
import fetchMappedMetadata from './steps/fetch-mapped-metadata.js';

Expand Down Expand Up @@ -89,7 +88,7 @@ export async function htmlPipe(state, req) {
const { log } = state;
state.type = 'html';

if (!validatePathInfo(state.info)) {
if (!state.info) {
return new PipelineResponse('', {
status: 404,
headers: {
Expand Down
31 changes: 9 additions & 22 deletions src/utils/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,22 @@ export function getPathInfo(path) {
}
let resExt = info.extension;
if (info.selector) {
if (resExt === '.html') {
if (info.selector === 'plain' && resExt === '.html') {
// force .plain.html as markdown resources
resExt = '.md';
fileName = `${baseName}${resExt}`;
} else if (resExt === '.html') {
// allow selector for static html
fileName = `${baseName}.${info.selector}${resExt}`;
} else {
// remove selector for all others
fileName = `${baseName}${resExt}`;
}
segs.push(`${baseName}.${info.selector}${info.extension}`);
} else {
segs.push(`${baseName}${resExt}`);
fileName = `${baseName}${resExt}`;
}
fileName = `${baseName}${resExt}`;
}

info.path = `/${segs.join('/')}`;
Expand All @@ -84,26 +91,6 @@ export function getPathInfo(path) {
return info;
}

/**
* Validates the path info
* @param {PathInfo} info Info to valida
* @return {boolean} {@code true} if valid.
*/
export function validatePathInfo(info) {
if (!info) {
return false;
}

// only support empty selector or plain with html
if (info.selector) {
if (info.selector !== 'plain') {
return false;
}
return info.extension === '.html';
}
return true;
}

/**
* Returns the file extension (e.g. '.html') of the given parh
* @param {string} path the path to get the extension from
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/code/my-block.selector.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><body>static</body></html>
30 changes: 29 additions & 1 deletion test/html-pipe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { StaticS3Loader } from './StaticS3Loader.js';
describe('HTML Pipe Test', () => {
it('responds with 404 for invalid path', async () => {
const resp = await htmlPipe(
new PipelineState({ path: '/foo.hidden.html' }),
new PipelineState({ path: '/../etc/passwd' }),
new PipelineRequest(new URL('https://www.hlx.live/')),
);
assert.strictEqual(resp.status, 404);
Expand Down Expand Up @@ -275,4 +275,32 @@ describe('HTML Pipe Test', () => {
link: '</scripts/scripts.js>; rel=modulepreload; as=script; crossorigin=use-credentials',
});
});

it('renders static html with selector my-block.selector.html', async () => {
const s3Loader = new FileS3Loader();
const state = new PipelineState({
log: console,
s3Loader,
owner: 'adobe',
repo: 'helix-pages',
ref: 'super-test',
partition: 'live',
path: '/my-block.selector.html',
timer: {
update: () => { },
},
});
const resp = await htmlPipe(
state,
new PipelineRequest(new URL('https://www.hlx.live/')),
);
assert.strictEqual(resp.status, 200);
assert.strictEqual(resp.body, '<html><body>static</body></html>\n');
assert.deepStrictEqual(Object.fromEntries(resp.headers.entries()), {
'access-control-allow-origin': '*',
'content-type': 'text/html; charset=utf-8',
'last-modified': 'Fri, 30 Apr 2021 03:47:18 GMT',
'x-surrogate-key': 'Tl4ey1eS4kJ2iRMt kvcvppnfHtt5omSX foo-id_metadata super-test--helix-pages--adobe_head',
});
});
});
36 changes: 1 addition & 35 deletions test/utils/path.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@

import assert from 'assert';
import path from 'path';
import {
getPathInfo,
validatePathInfo,
getExtension,
} from '../../src/utils/path.js';
import { getPathInfo, getExtension } from '../../src/utils/path.js';

describe('Path Utils Test - getPathInfo', () => {
it('get path info populates correctly', async () => {
Expand Down Expand Up @@ -185,36 +181,6 @@ describe('Path Utils Test - getPathInfo', () => {
});
});

describe('Path Utils Test - validatePathInfo', () => {
it('rejects undefined path', async () => {
assert.strictEqual(validatePathInfo(), false);
});

it('validates paths correctly', async () => {
assert.strictEqual(validatePathInfo(getPathInfo('')), true);
});

it('validates path with html', async () => {
assert.strictEqual(validatePathInfo(getPathInfo('/blog.html')), true);
});

it('rejects path ending with /index', async () => {
assert.strictEqual(validatePathInfo(getPathInfo('/index')), true);
});

it('validates path with plain.html', async () => {
assert.strictEqual(validatePathInfo(getPathInfo('/blog.plain.html')), true);
});

it('rejects path with html.plain.html', async () => {
assert.strictEqual(validatePathInfo(getPathInfo('/blog.html.plain.html')), false);
});

it('rejects path with plain.json', async () => {
assert.strictEqual(validatePathInfo(getPathInfo('/blog.plain.json')), false);
});
});

describe('Path Utils Test - getExtension', () => {
it('mimics path.extname', async () => {
const paths = [
Expand Down

0 comments on commit 33757b1

Please sign in to comment.