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/CHANGES.md b/CHANGES.md index 19ed749bfb63..8efbe37cffd3 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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) diff --git a/Source/Core/Resource.js b/Source/Core/Resource.js index 7acb7b3dab4a..a02c303f4b32 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..24c2f66be0ed --- /dev/null +++ b/Specs/test.mjs @@ -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); + }); \ 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",