Skip to content

Commit

Permalink
fix: throwing w/ fresh ember-cli-fastboot serve
Browse files Browse the repository at this point in the history
ember-cli-fastboot requires configuring host whitelist before accessing
host from request. In ember-fetch, the error should be lazily thrown.
  • Loading branch information
xg-wang committed Oct 25, 2019
1 parent 50d54ec commit 4292dcb
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 14 deletions.
8 changes: 6 additions & 2 deletions fastboot/instance-initializers/setup-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ function patchFetchForRelativeURLs(instance) {
const request = fastboot.get('request');
// Prember is not sending protocol
const protocol = request.protocol === 'undefined:' ? 'http:' : request.protocol;
// host is cp
setupFastboot(protocol, request.get('host'));
try {
// host is cp
setupFastboot(protocol, request.get('host'));
} catch (_e) {
// Do not throw error until request with a relative url.
}
}

export default {
Expand Down
18 changes: 12 additions & 6 deletions public/fetch-fastboot.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ define('fetch', ['exports'], function(exports) {
);
var nodeFetch = FastBoot.require('node-fetch');

function _checkHost(host) {
if (host === null) {
throw new Error(
'You are using using fetch with a relative URL, but host is missing from Fastboot request. Please set the hostWhitelist property in your environment.js. https://github.com/ember-fastboot/ember-cli-fastboot#host'
);
}
}

/**
* Build the absolute url if it's not, can handle:
* - protocol-relative URL (//can-be-http-or-https.com/)
Expand All @@ -20,19 +28,17 @@ define('fetch', ['exports'], function(exports) {
*/
function buildAbsoluteUrl(url, protocol, host) {
if (protocolRelativeRegex.test(url)) {
_checkHost(host);
url = host + url;
} else if (!httpRegex.test(url)) {
if (!host) {
throw new Error(
'You are using using fetch with a path-relative URL, but host is missing from Fastboot request. Please set the hostWhitelist property in your environment.js.'
);
}
_checkHost(host);
url = protocol + '//' + host + url;
}
return url;
}

var FastbootHost, FastbootProtocol;
var FastbootHost = null;
var FastbootProtocol = null;

class FastBootRequest extends nodeFetch.Request {
constructor(input, init) {
Expand Down
55 changes: 55 additions & 0 deletions test/fastboot-fetch-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';
const request = require('request');
const get = require('rsvp').denodeify(request);
const chai = require('chai');
const expect = chai.expect;
chai.use(require('chai-fs'));

const AddonTestApp = require('ember-cli-addon-tests').AddonTestApp;

describe('renders in fastboot build', function() {
this.timeout(300000);

let app;

beforeEach(function() {
app = new AddonTestApp();

return app
.create('dummy', { skipNpm: true })
.then(app =>
app.editPackageJSON(pkg => {
pkg.devDependencies['ember-cli-fastboot'] = '*';
// [email protected] has ember-fetch as dependency, we want to test
pkg.devDependencies['ember-fetch-adapter'] = '0.4.0';
// These 2 are in ember-fetch's package.json, symlinking to dummy won't help resolve
pkg.devDependencies['abortcontroller-polyfill'] = '*';
pkg.devDependencies['node-fetch'] = '*';
})
)
.then(function() {
return app.run('npm', 'install');
})
.then(function() {
return app.startServer({
command: 'serve'
});
});
});

afterEach(function() {
return app.stopServer();
});

it('fetches in fastboot mode', function() {
return get({
url: 'http://localhost:49741/',
headers: {
Accept: 'text/html'
}
}).then(function(response) {
expect(response.body).to.contain('Hello World! fetch');
expect(response.body).to.contain('Hello World! fetch (Request)');
});
});
});
11 changes: 5 additions & 6 deletions test/fastboot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ chai.use(require('chai-fs'));

const AddonTestApp = require('ember-cli-addon-tests').AddonTestApp;

describe('renders in fastboot build', function() {
describe('renders in fastboot build without calling fetch', function() {
this.timeout(300000);

let app;
Expand All @@ -16,7 +16,7 @@ describe('renders in fastboot build', function() {
app = new AddonTestApp();

return app
.create('dummy', { skipNpm: true })
.create('fresh', { skipNpm: true, noFixtures: true })
.then(app =>
app.editPackageJSON(pkg => {
pkg.devDependencies['ember-cli-fastboot'] = '*';
Expand Down Expand Up @@ -44,18 +44,17 @@ describe('renders in fastboot build', function() {
it('builds into dist/ember-fetch/fetch-fastboot.js ignoring sub dependency version conflict', function() {
expect(app.filePath('dist/index.html')).to.be.a.file();
expect(app.filePath('dist/ember-fetch/fetch-fastboot.js')).to.be.a.file();
expect(app.filePath('dist/assets/dummy-fastboot.js')).to.be.a.file();
expect(app.filePath('dist/assets/fresh-fastboot.js')).to.be.a.file();
});

it('fetches in fastboot mode', function() {
it('fresh serve works', function() {
return get({
url: 'http://localhost:49741/',
headers: {
Accept: 'text/html'
}
}).then(function(response) {
expect(response.body).to.contain('Hello World! fetch');
expect(response.body).to.contain('Hello World! fetch (Request)');
expect(response.body).to.contain('Congratulations, you made it!');
});
});
});

0 comments on commit 4292dcb

Please sign in to comment.