Skip to content

Commit

Permalink
Support Prember, update node-fetch
Browse files Browse the repository at this point in the history
- Prember is not sending protocol, need to check "undefined:"
- node-fetch 2.3 has AbortController support
- Fix issue when fetch(request) instead of fetch(url, options)
  • Loading branch information
xg-wang committed Nov 14, 2018
1 parent 8629160 commit 9fdbb68
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
4 changes: 3 additions & 1 deletion fastboot/instance-initializers/setup-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import setupFetch from 'fetch/setup';
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;
// host is cp
setupFetch(request.protocol, request.get('host'))();
setupFetch(protocol, request.get('host'))();
}

export default {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"broccoli-stew": "^2.0.0",
"broccoli-templater": "^2.0.1",
"ember-cli-babel": "^6.8.2",
"node-fetch": "^2.0.0-alpha.9",
"node-fetch": "^2.3.0",
"rollup-plugin-babel": "^3.0.7",
"whatwg-fetch": "^3.0.0"
},
Expand Down
32 changes: 18 additions & 14 deletions public/fastboot-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,11 @@ define('fetch/setup', ['exports'], function(self) {
'abortcontroller-polyfill/dist/cjs-ponyfill'
);
var nodeFetch = FastBoot.require('node-fetch');
var abortableFetch = AbortControllerPolyfill.abortableFetch({
fetch: nodeFetch,
Request: nodeFetch.Request
});

self['default'] = function(protocol, host) {
return function() {
define('fetch', ['exports'], function(exports) {
/**
* Setup the exported fetch for a given origin so it can handle:
* - protocol-relative URL (//can-be-http-or-https.com/)
* - path-relative URL (/file/under/root)
* @param {String} url
* @param {Object} [options]
*/
exports['default'] = function fetch(url, options) {
function buildAbsoluteUrl(url, protocol, host) {
if (protocolRelativeRegex.test(url)) {
url = host + url;
} else if (!httpRegex.test(url)) {
Expand All @@ -33,9 +22,24 @@ define('fetch/setup', ['exports'], function(self) {
}
url = protocol + '//' + host + url;
}
return abortableFetch.fetch(url, options);
return url;
}
/**
* Setup the exported fetch for a given origin so it can handle:
* - protocol-relative URL (//can-be-http-or-https.com/)
* - path-relative URL (/file/under/root)
* @param {String|Object} input
* @param {Object} [options]
*/
exports['default'] = function fetch(input, options) {
if (typeof input === 'object') {
input.url = buildAbsoluteUrl(input.url, protocol, host);
} else {
input = buildAbsoluteUrl(input, protocol, host);
}
return nodeFetch(input, options);
};
exports['Request'] = abortableFetch.Request;
exports['Request'] = nodeFetch.Request;
exports['Headers'] = nodeFetch.Headers;
exports['Response'] = nodeFetch.Response;
exports['AbortController'] = AbortControllerPolyfill.AbortController;
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6504,10 +6504,10 @@ no-case@^2.2.0:
dependencies:
lower-case "^1.1.1"

node-fetch@^2.0.0-alpha.9:
version "2.2.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.2.0.tgz#4ee79bde909262f9775f731e3656d0db55ced5b5"
integrity sha512-OayFWziIxiHY8bCUyLX6sTpDH8Jsbp4FfYd1j1f7vZyfgkcOnAyM4oQR16f8a0s7Gl/viMGRey8eScYk4V4EZA==
node-fetch@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.3.0.tgz#1a1d940bbfb916a1d3e0219f037e89e71f8c5fa5"
integrity sha512-MOd8pV3fxENbryESLgVIeaGKrdl+uaYhCSSVkjeOb/31/njTpcis5aWfdqgNlHIrKOLRbMnfPINPOML2CIFeXA==

node-int64@^0.4.0:
version "0.4.0"
Expand Down

0 comments on commit 9fdbb68

Please sign in to comment.