Skip to content

Commit

Permalink
Merge pull request #10694 from CesiumGS/mjs
Browse files Browse the repository at this point in the history
Fix for NodeJS with ESM modules
  • Loading branch information
sanjeetsuhag authored Aug 22, 2022
2 parents 4b333bc + 89fe501 commit ec783fe
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 51 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ script:
- node -e "const Cesium = require('./');"
- NODE_ENV=development node Specs/test.cjs
- NODE_ENV=production node Specs/test.cjs
- node Specs/test.mjs

- npm --silent run cloc
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
##### Fixes :wrench:

- Fixed bug with `Viewer.flyTo` where camera could go underground when target is an `Entity` with `ModelGraphics` with `HeightReference.CLAMP_TO_GROUND` or `HeightReference.RELATIVE_TO_GROUND`. [#10631](https://github.com/CesiumGS/cesium/pull/10631)
- Fixed issues running CesiumJS under Node.js when using ESM modules. [#10684](https://github.com/CesiumGS/cesium/issues/10684)
- Fixed the incorrect lighting of instanced models. [#10690](https://github.com/CesiumGS/cesium/pull/10690)
- Fixed developer error with `Camera.flyTo` with an `orientation` and a `Rectangle` value for `destination`. [#10704](https://github.com/CesiumGS/cesium/issues/10704)

Expand Down
102 changes: 54 additions & 48 deletions Source/Core/Resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -2059,59 +2059,65 @@ function loadWithHttpRequest(
overrideMimeType
) {
// Note: only the 'json' and 'text' responseTypes transforms the loaded buffer
/* eslint-disable no-undef */
const URL = require("url").parse(url);
const http = URL.protocol === "https:" ? require("https") : require("http");
const zlib = require("zlib");
/* eslint-enable no-undef */

const 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;
}
let URL;
let zlib;
Promise.all([import("url"), import("zlib")])
.then(([urlImport, zlibImport]) => {
URL = urlImport.parse(url);
zlib = zlibImport;

return URL.protocol === "https:" ? import("https") : import("http");
})
.then((http) => {
const 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;
}

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

res.on("end", function () {
// eslint-disable-next-line no-undef
const result = Buffer.concat(chunkArray);
if (res.headers["content-encoding"] === "gzip") {
zlib.gunzip(result, function (error, resultUnzipped) {
if (error) {
deferred.reject(
new RuntimeError("Error decompressing response.")
);
res.on("end", function () {
// eslint-disable-next-line no-undef
const result = Buffer.concat(chunkArray);
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(resultUnzipped, responseType));
deferred.resolve(decodeResponse(result, responseType));
}
});
} else {
deferred.resolve(decodeResponse(result, responseType));
}
});
})
.on("error", function (e) {
deferred.reject(new RequestErrorEvent());
})
.end();
})
.on("error", function (e) {
deferred.reject(new RequestErrorEvent());
})
.end();
});
}

const noXMLHttpRequest = typeof XMLHttpRequest === "undefined";
Expand Down
2 changes: 1 addition & 1 deletion Source/Scene/GltfJsonLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import ForEach from "./GltfPipeline/ForEach.js";
import parseGlb from "./GltfPipeline/parseGlb.js";
import removePipelineExtras from "./GltfPipeline/removePipelineExtras.js";
import updateVersion from "./GltfPipeline/updateVersion.js";
import usesExtension from "./GltfPipeline/usesExtension";
import usesExtension from "./GltfPipeline/usesExtension.js";
import ResourceLoader from "./ResourceLoader.js";
import ResourceLoaderState from "./ResourceLoaderState.js";

Expand Down
17 changes: 17 additions & 0 deletions Specs/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Cartographic, CesiumTerrainProvider, sampleTerrain } from "cesium";
import assert from "node:assert";

// NodeJS smoke screen test

const provider = new CesiumTerrainProvider({
url: "https://s3.amazonaws.com/cesiumjs/smallTerrain",
});
sampleTerrain(provider, 11, [
Cartographic.fromDegrees(86.925145, 27.988257),
Cartographic.fromDegrees(87.0, 28.0),
]).then((results) => {
assert(results[0].height > 5000);
assert(results[0].height < 10000);
assert(results[1].height > 5000);
assert(results[1].height < 10000);
});
2 changes: 1 addition & 1 deletion build.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ async function buildWorkers(options) {
// 1) They can be built as AMD style modules
// 2) They can be built using code-splitting, resulting in smaller modules
const files = await globby(["Source/WorkersES6/*.js"]);
const plugins = [rollupResolve(), rollupCommonjs()];
const plugins = [rollupResolve({ preferBuiltins: true }), rollupCommonjs()];

if (options.removePragmas) {
plugins.push(
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"type": "module",
"dependencies": {
"@tweenjs/tween.js": "^18.6.4",
"@zip.js/zip.js": "^2.3.12",
"@zip.js/zip.js": "2.4.x",
"autolinker": "^3.14.3",
"bitmap-sdf": "^1.0.3",
"dompurify": "^2.2.2",
Expand Down

0 comments on commit ec783fe

Please sign in to comment.