Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address Issue174 #200

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/sandbox/external-resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ function addCSS(resource: string) {
head.appendChild(link);
}

function getContentType(resource: string) {
return fetch(resource, {
method: 'HEAD',
mode: 'cors',
}).then(response => response.headers.get('Content-Type'));
}

function addJS(resource: string) {
const script = document.createElement('script');
script.setAttribute('src', resource);
Expand All @@ -32,13 +39,28 @@ function addJS(resource: string) {
document.head.appendChild(script);
}

function addResource(resource: string) {
export function addResource(
resource: string,
addCSS = addCSS,
addJS = addJS,
getContentType = getContentType
) {
const match = resource.match(/\.([^.]*)$/);

if (match && match[1] === 'css') {
addCSS(resource);
} else {
} else if (match && match[1] === 'js') {
addJS(resource);
} else {
return getContentType(resource).then(contentType => {
if (contentType.indexOf('text/css') === 0) {
addCSS(resource);
} else if (contentType.indexOf('application/javascript') === 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

application/javascript is not the only content type for javascript source code, see here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to my research, application/javascript is official. We could also add application/ecmascript, since it seems like it's in use. The text/ ones are deprecated. Do you think it's OK to leave them out? The rest seem obscure.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at the 'Content-Type' header for the scripts in a bunch of random websites, and the results were all over the place, from application/javascript, to text/javascript, to application/x-javascript etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. In that case, we can probably just go ahead and add the whole list.

addJS(resource);
} else {
throw new Error(`Unsupported Content-Type: ${contentType}`);
}
});
}
}

Expand All @@ -48,7 +70,9 @@ export default function handleExternalResources(externalResources) {
const extResString = getExternalResourcesConcatenation(externalResources);
if (extResString !== cachedExternalResources) {
clearExternalResources();
externalResources.forEach(addResource);
externalResources.forEach(resource => {
addResource(resource, addCSS, addJS, getContentType);
});
cachedExternalResources = extResString;
}
}
54 changes: 54 additions & 0 deletions src/sandbox/external-resources.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { addResource } from './external-resources';

describe('addResource', () => {
it('adds URLs that end with "css" as CSS', () => {
const addCSS = jest.fn();
const resource = 'resource.css';
addResource(resource, addCSS);
expect(addCSS).toHaveBeenCalled();
});

it('adds URLs that end with "js" as JS', () => {
const addJS = jest.fn();
const resource = 'resource.js';
addResource(resource, jest.fn(), addJS);
expect(addJS).toHaveBeenCalled();
});

it('checks content type if the file has an unknown ending', () => {
const getContentType = jest.fn();
getContentType.mockReturnValue(Promise.resolve('text/css'));
const resource = 'resource';
addResource(resource, jest.fn(), jest.fn(), getContentType);
expect(getContentType).toHaveBeenCalled();
});

it('throws an error for unknown content types', () => {
const getContentType = jest.fn();
getContentType.mockReturnValue(Promise.resolve('foo'));
const resource = 'resource';
addResource(resource, jest.fn(), jest.fn(), getContentType).catch(e => {
expect(e).toBeInstanceOf(Error);
});
});

it('adds "text/css" as CSS', () => {
const addCSS = jest.fn();
const getContentType = jest.fn();
getContentType.mockReturnValue(Promise.resolve('text/css'));
const resource = 'resource';
addResource(resource, addCSS, jest.fn(), getContentType).then(() => {
expect(addCSS).toHaveBeenCalled();
});
});

it('adds "application/javascript" as JS', () => {
const addJS = jest.fn();
const getContentType = jest.fn();
getContentType.mockReturnValue(Promise.resolve('application/javascript'));
const resource = 'resource';
addResource(resource, jest.fn(), addJS, getContentType).then(() => {
expect(addJS).toHaveBeenCalled();
});
});
});