Skip to content

Commit da0edef

Browse files
committed
Support Prember, update node-fetch
- 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)
1 parent 0c55e6b commit da0edef

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

fastboot/instance-initializers/setup-fetch.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import setupFetch from 'fetch/setup';
77
function patchFetchForRelativeURLs(instance) {
88
const fastboot = instance.lookup('service:fastboot');
99
const request = fastboot.get('request');
10+
// Prember is not sending protocol
11+
const protocol = request.protocol === 'undefined:' ? 'http:' : request.protocol;
1012
// host is cp
11-
setupFetch(request.protocol, request.get('host'))();
13+
setupFetch(protocol, request.get('host'))();
1214
}
1315

1416
export default {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"broccoli-stew": "^2.0.0",
3030
"broccoli-templater": "^2.0.1",
3131
"ember-cli-babel": "^6.8.2",
32-
"node-fetch": "^2.0.0-alpha.9",
32+
"node-fetch": "^2.3.0",
3333
"whatwg-fetch": "^3.0.0"
3434
},
3535
"devDependencies": {

public/fastboot-fetch.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,11 @@ define('fetch/setup', ['exports'], function(self) {
77
'abortcontroller-polyfill/dist/cjs-ponyfill'
88
);
99
var nodeFetch = FastBoot.require('node-fetch');
10-
var abortableFetch = AbortControllerPolyfill.abortableFetch({
11-
fetch: nodeFetch,
12-
Request: nodeFetch.Request
13-
});
1410

1511
self['default'] = function(protocol, host) {
1612
return function() {
1713
define('fetch', ['exports'], function(exports) {
18-
/**
19-
* Setup the exported fetch for a given origin so it can handle:
20-
* - protocol-relative URL (//can-be-http-or-https.com/)
21-
* - path-relative URL (/file/under/root)
22-
* @param {String} url
23-
* @param {Object} [options]
24-
*/
25-
exports['default'] = function fetch(url, options) {
14+
function buildAbsoluteUrl(url, protocol, host) {
2615
if (protocolRelativeRegex.test(url)) {
2716
url = host + url;
2817
} else if (!httpRegex.test(url)) {
@@ -33,9 +22,24 @@ define('fetch/setup', ['exports'], function(self) {
3322
}
3423
url = protocol + '//' + host + url;
3524
}
36-
return abortableFetch.fetch(url, options);
25+
return url;
26+
}
27+
/**
28+
* Setup the exported fetch for a given origin so it can handle:
29+
* - protocol-relative URL (//can-be-http-or-https.com/)
30+
* - path-relative URL (/file/under/root)
31+
* @param {String|Object} input
32+
* @param {Object} [options]
33+
*/
34+
exports['default'] = function fetch(input, options) {
35+
if (typeof input === 'object') {
36+
input.url = buildAbsoluteUrl(input.url, protocol, host);
37+
} else {
38+
input = buildAbsoluteUrl(input, protocol, host);
39+
}
40+
return nodeFetch(input, options);
3741
};
38-
exports['Request'] = abortableFetch.Request;
42+
exports['Request'] = nodeFetch.Request;
3943
exports['Headers'] = nodeFetch.Headers;
4044
exports['Response'] = nodeFetch.Response;
4145
exports['AbortController'] = AbortControllerPolyfill.AbortController;

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6967,7 +6967,7 @@ node-fetch-npm@^2.0.2:
69676967
json-parse-better-errors "^1.0.0"
69686968
safe-buffer "^5.1.1"
69696969

6970-
node-fetch@^2.0.0-alpha.9:
6970+
node-fetch@^2.3.0:
69716971
version "2.3.0"
69726972
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.3.0.tgz#1a1d940bbfb916a1d3e0219f037e89e71f8c5fa5"
69736973
integrity sha512-MOd8pV3fxENbryESLgVIeaGKrdl+uaYhCSSVkjeOb/31/njTpcis5aWfdqgNlHIrKOLRbMnfPINPOML2CIFeXA==

0 commit comments

Comments
 (0)