From aef4d38e5c7b14badedf8be1cf8cb5d73e79c410 Mon Sep 17 00:00:00 2001 From: Gabby Getz Date: Tue, 16 Aug 2022 15:51:23 -0400 Subject: [PATCH 1/3] Fix for NodeJS with ESM modules --- .travis.yml | 1 + Source/Core/Resource.js | 102 +++++++++++++++++---------------- Source/Scene/GltfJsonLoader.js | 2 +- Specs/test.mjs | 17 ++++++ build.cjs | 2 +- package.json | 2 +- 6 files changed, 75 insertions(+), 51 deletions(-) create mode 100644 Specs/test.mjs diff --git a/.travis.yml b/.travis.yml index 93018db3c066..e2911e8e65db 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/Source/Core/Resource.js b/Source/Core/Resource.js index 3f8b894f3efb..489f42ee9021 100644 --- a/Source/Core/Resource.js +++ b/Source/Core/Resource.js @@ -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"; diff --git a/Source/Scene/GltfJsonLoader.js b/Source/Scene/GltfJsonLoader.js index 3c15ed23661a..fdf7acb60efd 100644 --- a/Source/Scene/GltfJsonLoader.js +++ b/Source/Scene/GltfJsonLoader.js @@ -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"; diff --git a/Specs/test.mjs b/Specs/test.mjs new file mode 100644 index 000000000000..38d649a8328d --- /dev/null +++ b/Specs/test.mjs @@ -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); + }); \ No newline at end of file diff --git a/build.cjs b/build.cjs index 0f1489f6a4da..5351f6cc7725 100644 --- a/build.cjs +++ b/build.cjs @@ -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( diff --git a/package.json b/package.json index 99344390743c..65690bdb3a89 100644 --- a/package.json +++ b/package.json @@ -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", From 338e82390265aae345d2ebb6adaf353134c0b333 Mon Sep 17 00:00:00 2001 From: Gabby Getz Date: Tue, 16 Aug 2022 16:10:19 -0400 Subject: [PATCH 2/3] Update CHANGES.md --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 2086598a7151..99c5d5e1cffe 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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) ### 1.96 - 2022-08-01 From 89fe5011683bddb6863b01d1051f6a1d74953223 Mon Sep 17 00:00:00 2001 From: Gabby Getz Date: Mon, 22 Aug 2022 10:36:02 -0400 Subject: [PATCH 3/3] import --- Specs/test.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Specs/test.mjs b/Specs/test.mjs index 38d649a8328d..24c2f66be0ed 100644 --- a/Specs/test.mjs +++ b/Specs/test.mjs @@ -1,4 +1,4 @@ -import { Cartographic, CesiumTerrainProvider, sampleTerrain } from "../Source/Cesium.js"; +import { Cartographic, CesiumTerrainProvider, sampleTerrain } from "cesium"; import assert from "node:assert"; // NodeJS smoke screen test