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

Fix for NodeJS with ESM modules #10694

Merged
merged 4 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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 .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
sanjeetsuhag marked this conversation as resolved.
Show resolved Hide resolved

- npm --silent run cloc
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,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)

### 1.96 - 2022-08-01
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")])
sanjeetsuhag marked this conversation as resolved.
Show resolved Hide resolved
.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 "../Source/Cesium.js";
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