forked from ember-cli/ember-fetch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix fetch public/fastboot-fetch.js module definition for Fastboot emb…
- Loading branch information
Showing
8 changed files
with
120 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* globals define FastBoot */ | ||
define('fetch', ['exports'], function(exports) { | ||
var httpRegex = /^https?:\/\//; | ||
var protocolRelativeRegex = /^\/\//; | ||
|
||
var AbortControllerPolyfill = FastBoot.require( | ||
'abortcontroller-polyfill/dist/cjs-ponyfill' | ||
); | ||
var nodeFetch = FastBoot.require('node-fetch'); | ||
|
||
/** | ||
* 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) { | ||
if (protocolRelativeRegex.test(url)) { | ||
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.' | ||
); | ||
} | ||
url = protocol + '//' + host + url; | ||
} | ||
return url; | ||
} | ||
|
||
var FastbootHost, FastbootProtocol; | ||
|
||
/** | ||
* Isomorphic `fetch` API for both browser and fastboot | ||
* | ||
* node-fetch doesn't allow relative URLs, we patch it with Fastboot runtime info. | ||
* Before instance-initializers Absolute URL is still not allowed, in this case | ||
* node-fetch will throw error. | ||
* `FastbootProtocol` and `FastbootHost` are re-set for every instance during its | ||
* initializers through calling `setupFastboot`. | ||
* | ||
* @param {String|Object} input | ||
* @param {Object} [options] | ||
*/ | ||
exports.default = function fetch(input, options) { | ||
if (typeof input === 'object') { | ||
input.url = buildAbsoluteUrl(input.url, FastbootProtocol, FastbootHost); | ||
} else { | ||
input = buildAbsoluteUrl(input, FastbootProtocol, FastbootHost); | ||
} | ||
return nodeFetch(input, options); | ||
}; | ||
/** | ||
* Assign the local protocol and host being used for building absolute URLs | ||
* @private | ||
*/ | ||
exports.setupFastboot = function setupFastboot(protocol, host) { | ||
FastbootProtocol = protocol; | ||
FastbootHost = host; | ||
} | ||
exports.Request = nodeFetch.Request; | ||
exports.Headers = nodeFetch.Headers; | ||
exports.Response = nodeFetch.Response; | ||
exports.AbortController = AbortControllerPolyfill.AbortController; | ||
}); | ||
|
||
define('fetch/ajax', ['exports'], function() { | ||
throw new Error( | ||
'You included `fetch/ajax` but it was renamed to `ember-fetch/ajax`' | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,8 @@ describe('renders in fastboot build', function() { | |
.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'] = '*'; | ||
|
@@ -39,6 +41,12 @@ describe('renders in fastboot build', function() { | |
return app.stopServer(); | ||
}); | ||
|
||
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(); | ||
}); | ||
|
||
it('fetches in fastboot mode', function() { | ||
return get({ | ||
url: 'http://localhost:49741/', | ||
|