Skip to content

Commit

Permalink
save request instead of protocol, host
Browse files Browse the repository at this point in the history
  • Loading branch information
xg-wang committed Oct 26, 2019
1 parent 4292dcb commit 8fdf660
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 31 deletions.
13 changes: 3 additions & 10 deletions fastboot/instance-initializers/setup-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,12 @@ import { setupFastboot } from 'fetch';

/**
* To allow relative URLs for Fastboot mode, we need the per request information
* from the fastboot service. Then we set the protocol and host to fetch module.
* from the fastboot service. Then we save the request from fastboot info.
* On each fetch with relative url we get host and protocol from it.
*/
function patchFetchForRelativeURLs(instance) {
const fastboot = instance.lookup('service:fastboot');
const request = fastboot.get('request');
// Prember is not sending protocol
const protocol = request.protocol === 'undefined:' ? 'http:' : request.protocol;
try {
// host is cp
setupFastboot(protocol, request.get('host'));
} catch (_e) {
// Do not throw error until request with a relative url.
}
setupFastboot(fastboot.get('request'));
}

export default {
Expand Down
38 changes: 17 additions & 21 deletions public/fetch-fastboot.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,42 @@ 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'
);
function parseRequest(request) {
if (request === null) {
throw new Error('Trying to fetch with relative url but ember-fetch hasn\'t finished loading FastBootInfo, see details at https://github.com/ember-cli/ember-fetch#relative-url');
}
// Old Prember version is not sending protocol
const protocol = request.protocol === 'undefined:' ? 'http:' : request.protocol;
return [request.get('host'), protocol];
}

/**
* Build the absolute url if it's not, can handle:
* - protocol-relative URL (//can-be-http-or-https.com/)
* - path-relative URL (/file/under/root)
*
* @param {string} url
* @param {string} protocol
* @param {string} host
* @returns {string}
*/
function buildAbsoluteUrl(url, protocol, host) {
function buildAbsoluteUrl(url) {
if (protocolRelativeRegex.test(url)) {
_checkHost(host);
let [host,] = parseRequest(REQUEST);
url = host + url;
} else if (!httpRegex.test(url)) {
_checkHost(host);
let [host, protocol] = parseRequest(REQUEST);
url = protocol + '//' + host + url;
}
return url;
}

var FastbootHost = null;
var FastbootProtocol = null;
var REQUEST = null;

class FastBootRequest extends nodeFetch.Request {
constructor(input, init) {
if (typeof input === 'string') {
input = buildAbsoluteUrl(input, FastbootProtocol, FastbootHost);
input = buildAbsoluteUrl(input);
} else if (input && input.href) {
// WHATWG URL or Node.js Url Object
input = buildAbsoluteUrl(input.href, FastbootProtocol, FastbootHost);
input = buildAbsoluteUrl(input.href);
}
super(input, init);
}
Expand All @@ -66,19 +63,18 @@ define('fetch', ['exports'], function(exports) {
*/
exports.default = function fetch(input, options) {
if (input && input.href) {
input.url = buildAbsoluteUrl(input.href, FastbootProtocol, FastbootHost);
input.url = buildAbsoluteUrl(input.href);
} else if (typeof input === 'string') {
input = buildAbsoluteUrl(input, FastbootProtocol, FastbootHost);
input = buildAbsoluteUrl(input);
}
return nodeFetch(input, options);
};
/**
* Assign the local protocol and host being used for building absolute URLs
* Assign the local REQUEST object for building absolute URLs
* @private
*/
exports.setupFastboot = function setupFastboot(protocol, host) {
FastbootProtocol = protocol;
FastbootHost = host;
exports.setupFastboot = function setupFastboot(fastBootRequest) {
REQUEST = fastBootRequest;
}
exports.Request = FastBootRequest;
exports.Headers = nodeFetch.Headers;
Expand Down

0 comments on commit 8fdf660

Please sign in to comment.