Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial XHR polyfull for Node.js #6463

Merged
merged 9 commits into from
Apr 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Change Log
* Fix flicker when adding, removing, or modifiying entities. [#3945](https://github.com/AnalyticalGraphicsInc/cesium/issues/3945)
##### Additions :tada:
* Improved `MapboxImageryProvider` performance by 300% via `tiles.mapbox.com` subdomain switching. [#6426](https://github.com/AnalyticalGraphicsInc/cesium/issues/6426)
* Added ability to invoke `sampleTerrain` from node.js to enable offline terrain sampling

### 1.44 - 2018-04-02

Expand Down
9 changes: 9 additions & 0 deletions Source/Core/FeatureDetection.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ define([
return isFirefox() && firefoxVersionResult;
}

var isNodeJsResult;
function isNodeJs() {
if (!defined(isNodeJsResult)) {
isNodeJsResult = typeof process === 'object' && Object.prototype.toString.call(process) === '[object process]'; // eslint-disable-line
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the eslint-disable-line for? Can we do eslint-disable-line some-eslint-rule instead of disabling everything for this line?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nevermind I guess haha

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

process is only a global in node and this is browser code so we need to ignore it. I couldn't figure out how to just allow a global for one line.

}
return isNodeJsResult;
}

var hasPointerEvents;
function supportsPointerEvents() {
if (!defined(hasPointerEvents)) {
Expand Down Expand Up @@ -230,6 +238,7 @@ define([
isFirefox : isFirefox,
firefoxVersion : firefoxVersion,
isWindows : isWindows,
isNodeJs: isNodeJs,
hardwareConcurrency : defaultValue(theNavigator.hardwareConcurrency, 3),
supportsPointerEvents : supportsPointerEvents,
supportsImageRenderingPixelated: supportsImageRenderingPixelated,
Expand Down
64 changes: 64 additions & 0 deletions Source/Core/Resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ define([
'./defineProperties',
'./deprecationWarning',
'./DeveloperError',
'./FeatureDetection',
'./freezeObject',
'./getAbsoluteUri',
'./getBaseUri',
Expand Down Expand Up @@ -37,6 +38,7 @@ define([
defineProperties,
deprecationWarning,
DeveloperError,
FeatureDetection,
freezeObject,
getAbsoluteUri,
getBaseUri,
Expand Down Expand Up @@ -1772,13 +1774,75 @@ define([
image.src = url;
};

function decodeResponse(loadWithHttpResponse, responseType) {
switch (responseType) {
case 'text':
return loadWithHttpResponse.toString('utf8');
case 'json':
return JSON.parse(loadWithHttpResponse.toString('utf8'));
default:
return new Uint8Array(loadWithHttpResponse).buffer;
}
}

function loadWithHttpRequest(url, responseType, method, data, headers, deferred, overrideMimeType) {
// Note: only the 'json' and 'text' responseTypes transforms the loaded buffer
var URL = require('url').parse(url);
var http = URL.protocol === 'https:' ? require('https') : require('http');
var zlib = require('zlib');
var options = {
protocol : URL.protocol,
hostname : URL.hostname,
port : URL.port,
path : URL.path,
query : URL.query,
method : method,
headers : headers
};

http.request(options)
.on('response', function(res) {
if (res.statusCode < 200 || res.statusCode >= 300) {
deferred.reject(new RequestErrorEvent(res.statusCode, res, res.headers));
return;
}

var chunkArray = [];
res.on('data', function(chunk) {
chunkArray.push(chunk);
});

res.on('end', function() {
var result = Buffer.concat(chunkArray); // eslint-disable-line
if (res.headers['content-encoding'] === 'gzip') {
zlib.gunzip(result, function(error, resultUnzipped) {
if (error) {
deferred.reject(new RuntimeError('Error decompressing response.'));
} else {
deferred.resolve(decodeResponse(resultUnzipped, responseType));
}
});
} else {
deferred.resolve(decodeResponse(result, responseType));
}
});
}).on('error', function(e) {
deferred.reject(new RequestErrorEvent());
}).end();
}

Resource._Implementations.loadWithXhr = function(url, responseType, method, data, headers, deferred, overrideMimeType) {
var dataUriRegexResult = dataUriRegex.exec(url);
if (dataUriRegexResult !== null) {
deferred.resolve(decodeDataUri(dataUriRegexResult, responseType));
return;
}

if (FeatureDetection.isNodeJs()) {
loadWithHttpRequest(url, responseType, method, data, headers, deferred, overrideMimeType);
return;
}

var xhr = new XMLHttpRequest();

if (TrustedServers.contains(url)) {
Expand Down
16 changes: 14 additions & 2 deletions Source/Core/getAbsoluteUri.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ define([
* var absoluteUri = Cesium.getAbsoluteUri('awesome.png', 'https://test.com');
*/
function getAbsoluteUri(relative, base) {
return getAbsoluteUri._implementation(relative, base, document);
var documentObject;
if (typeof document !== 'undefined') {
documentObject = document;
}

return getAbsoluteUri._implementation(relative, base, documentObject);
}

getAbsoluteUri._implementation = function(relative, base, documentObject) {
Expand All @@ -32,7 +37,14 @@ define([
throw new DeveloperError('relative uri is required.');
}
//>>includeEnd('debug');
base = defaultValue(base, defaultValue(documentObject.baseURI, documentObject.location.href));

if (!defined(base)) {
if (typeof documentObject === 'undefined') {
return relative;
}
base = defaultValue(documentObject.baseURI, documentObject.location.href);
}

var baseUri = new Uri(base);
var relativeUri = new Uri(relative);
return relativeUri.resolve(baseUri).toString();
Expand Down
4 changes: 4 additions & 0 deletions Specs/Core/FeatureDetectionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,8 @@ defineSuite([
expect(FeatureDetection.imageRenderingValue()).not.toBeDefined();
}
});

it('detects Node.js', function() {
expect(FeatureDetection.isNodeJs()).toBe(false);
});
});