@loaders.gl/tile-converter 4.0.0-alpha.23 → 4.0.0-alpha.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/dist/3d-tiles-converter/helpers/load-i3s.d.ts.map +1 -1
  2. package/dist/converter.min.js +97 -97
  3. package/dist/dist.min.js +237 -182
  4. package/dist/es5/3d-tiles-converter/helpers/load-i3s.js.map +1 -1
  5. package/dist/es5/deps-installer/deps-installer.js +1 -1
  6. package/dist/es5/i3s-converter/helpers/feature-attributes.js +6 -18
  7. package/dist/es5/i3s-converter/helpers/feature-attributes.js.map +1 -1
  8. package/dist/es5/i3s-converter/helpers/geometry-attributes.js +83 -44
  9. package/dist/es5/i3s-converter/helpers/geometry-attributes.js.map +1 -1
  10. package/dist/es5/i3s-converter/helpers/geometry-converter.js +9 -7
  11. package/dist/es5/i3s-converter/helpers/geometry-converter.js.map +1 -1
  12. package/dist/es5/i3s-converter/types.js.map +1 -1
  13. package/dist/es5/pgm-loader.js +11 -3
  14. package/dist/es5/pgm-loader.js.map +1 -1
  15. package/dist/esm/3d-tiles-converter/helpers/load-i3s.js.map +1 -1
  16. package/dist/esm/deps-installer/deps-installer.js +1 -1
  17. package/dist/esm/i3s-converter/helpers/feature-attributes.js +5 -5
  18. package/dist/esm/i3s-converter/helpers/feature-attributes.js.map +1 -1
  19. package/dist/esm/i3s-converter/helpers/geometry-attributes.js +76 -34
  20. package/dist/esm/i3s-converter/helpers/geometry-attributes.js.map +1 -1
  21. package/dist/esm/i3s-converter/helpers/geometry-converter.js +7 -5
  22. package/dist/esm/i3s-converter/helpers/geometry-converter.js.map +1 -1
  23. package/dist/esm/i3s-converter/types.js.map +1 -1
  24. package/dist/esm/pgm-loader.js +7 -4
  25. package/dist/esm/pgm-loader.js.map +1 -1
  26. package/dist/i3s-converter/helpers/feature-attributes.d.ts +6 -6
  27. package/dist/i3s-converter/helpers/feature-attributes.d.ts.map +1 -1
  28. package/dist/i3s-converter/helpers/geometry-attributes.d.ts.map +1 -1
  29. package/dist/i3s-converter/helpers/geometry-converter.d.ts.map +1 -1
  30. package/dist/i3s-converter/types.d.ts +7 -4
  31. package/dist/i3s-converter/types.d.ts.map +1 -1
  32. package/dist/pgm-loader.d.ts +9 -2
  33. package/dist/pgm-loader.d.ts.map +1 -1
  34. package/package.json +14 -14
  35. package/src/3d-tiles-converter/helpers/load-i3s.ts +1 -0
  36. package/src/i3s-converter/helpers/feature-attributes.ts +14 -11
  37. package/src/i3s-converter/helpers/geometry-attributes.ts +80 -50
  38. package/src/i3s-converter/helpers/geometry-converter.ts +18 -9
  39. package/src/i3s-converter/types.ts +8 -4
  40. package/src/pgm-loader.ts +15 -7
  41. package/dist/3d-tiles-converter/3d-tiles-converter.js +0 -279
  42. package/dist/3d-tiles-converter/helpers/b3dm-converter.js +0 -271
  43. package/dist/3d-tiles-converter/helpers/i3s-obb-to-3d-tiles-obb.js +0 -23
  44. package/dist/3d-tiles-converter/helpers/load-i3s.js +0 -42
  45. package/dist/3d-tiles-converter/helpers/texture-atlas.js +0 -54
  46. package/dist/3d-tiles-converter/json-templates/tileset.js +0 -43
  47. package/dist/bundle.js +0 -5
  48. package/dist/constants.js +0 -4
  49. package/dist/converter-cli.js +0 -222
  50. package/dist/deps-installer/deps-installer.js +0 -89
  51. package/dist/i3s-converter/helpers/batch-ids-extensions.js +0 -179
  52. package/dist/i3s-converter/helpers/coordinate-converter.js +0 -122
  53. package/dist/i3s-converter/helpers/create-scene-server-path.js +0 -28
  54. package/dist/i3s-converter/helpers/feature-attributes.js +0 -218
  55. package/dist/i3s-converter/helpers/geometry-attributes.js +0 -203
  56. package/dist/i3s-converter/helpers/geometry-converter.js +0 -1321
  57. package/dist/i3s-converter/helpers/gltf-attributes.js +0 -129
  58. package/dist/i3s-converter/helpers/load-3d-tiles.js +0 -99
  59. package/dist/i3s-converter/helpers/node-debug.js +0 -120
  60. package/dist/i3s-converter/helpers/node-index-document.js +0 -271
  61. package/dist/i3s-converter/helpers/node-pages.js +0 -316
  62. package/dist/i3s-converter/helpers/preprocess-3d-tiles.js +0 -100
  63. package/dist/i3s-converter/helpers/tileset-traversal.js +0 -29
  64. package/dist/i3s-converter/i3s-converter.js +0 -964
  65. package/dist/i3s-converter/json-templates/geometry-definitions.js +0 -87
  66. package/dist/i3s-converter/json-templates/layers.js +0 -139
  67. package/dist/i3s-converter/json-templates/metadata.js +0 -25
  68. package/dist/i3s-converter/json-templates/node.js +0 -89
  69. package/dist/i3s-converter/json-templates/scene-server.js +0 -31
  70. package/dist/i3s-converter/json-templates/shared-resources.js +0 -129
  71. package/dist/i3s-converter/json-templates/store.js +0 -103
  72. package/dist/i3s-converter/types.js +0 -17
  73. package/dist/i3s-server/app.js +0 -29
  74. package/dist/i3s-server/bin/www.js +0 -37
  75. package/dist/i3s-server/controllers/index-controller.js +0 -31
  76. package/dist/i3s-server/controllers/slpk-controller.js +0 -33
  77. package/dist/i3s-server/routes/index.js +0 -20
  78. package/dist/i3s-server/routes/slpk-router.js +0 -34
  79. package/dist/i3s-server/utils/create-scene-server.js +0 -22
  80. package/dist/i3s-server/utils/server-utils.js +0 -66
  81. package/dist/index.js +0 -10
  82. package/dist/lib/utils/cli-utils.js +0 -82
  83. package/dist/lib/utils/compress-util.js +0 -257
  84. package/dist/lib/utils/file-utils.js +0 -139
  85. package/dist/lib/utils/geometry-utils.js +0 -18
  86. package/dist/lib/utils/lod-conversion-utils.js +0 -76
  87. package/dist/lib/utils/queue.js +0 -18
  88. package/dist/lib/utils/statistic-utills.js +0 -64
  89. package/dist/lib/utils/write-queue.js +0 -80
  90. package/dist/pgm-loader.js +0 -24
  91. package/dist/slpk-extractor/slpk-extractor.js +0 -75
  92. package/dist/slpk-extractor-cli.js +0 -102
@@ -1,31 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getFileNameByUrl = void 0;
7
- const path_1 = __importDefault(require("path"));
8
- const fs_1 = __importDefault(require("fs"));
9
- const { promises } = fs_1.default;
10
- const I3S_LAYER_PATH = process.env.I3sLayerPath || '';
11
- /**
12
- * Get local file name by input HTTP URL
13
- * @param url - I3S HTTP service url
14
- * @returns - local file name
15
- */
16
- async function getFileNameByUrl(url) {
17
- const extensions = ['json', 'bin', 'jpg', 'jpeg', 'png', 'bin.dds', 'ktx2'];
18
- const FULL_LAYER_PATH = path_1.default.join(process.cwd(), I3S_LAYER_PATH);
19
- for (const ext of extensions) {
20
- const fileName = `${FULL_LAYER_PATH}${url}/index.${ext}`;
21
- try {
22
- await promises.access(fileName);
23
- return fileName;
24
- }
25
- catch {
26
- continue; // eslint-disable-line no-continue
27
- }
28
- }
29
- return null;
30
- }
31
- exports.getFileNameByUrl = getFileNameByUrl;
@@ -1,33 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getFileByUrl = exports.loadArchive = void 0;
4
- require("@loaders.gl/polyfills");
5
- const i3s_1 = require("@loaders.gl/i3s");
6
- const loader_utils_1 = require("@loaders.gl/loader-utils");
7
- let slpkArchive;
8
- /**
9
- * Open SLPK file for reading and load HASH file
10
- * @param fullLayerPath - full path to SLPK file
11
- */
12
- const loadArchive = async (fullLayerPath) => {
13
- slpkArchive = await (0, i3s_1.parseSLPK)(await loader_utils_1.FileHandleFile.from(fullLayerPath), (msg) => console.log(msg));
14
- console.log('The server is ready to use');
15
- };
16
- exports.loadArchive = loadArchive;
17
- /**
18
- * Get a file from SLPK
19
- * @param url - I3S HTTP URL
20
- * @returns - file content
21
- */
22
- async function getFileByUrl(url) {
23
- const trimmedPath = /^\/?(.*)\/?$/.exec(url);
24
- let uncompressedFile = null;
25
- if (trimmedPath) {
26
- try {
27
- uncompressedFile = Buffer.from(await slpkArchive.getFile(trimmedPath[1], 'http'));
28
- }
29
- catch (e) { }
30
- }
31
- return uncompressedFile;
32
- }
33
- exports.getFileByUrl = getFileByUrl;
@@ -1,20 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const express_1 = __importDefault(require("express"));
7
- const router = express_1.default.Router();
8
- const { getFileNameByUrl } = require('../controllers/index-controller');
9
- /* GET home page. */
10
- router.get('*', async function (req, res, next) {
11
- const fileName = await getFileNameByUrl(req.path);
12
- if (fileName) {
13
- res.sendFile(fileName);
14
- }
15
- else {
16
- res.status(404);
17
- res.send('File not found');
18
- }
19
- });
20
- module.exports = router;
@@ -1,34 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.router = exports.sceneServerRouter = void 0;
7
- const express_1 = __importDefault(require("express"));
8
- const slpk_controller_1 = require("../controllers/slpk-controller");
9
- const create_scene_server_1 = require("../utils/create-scene-server");
10
- exports.sceneServerRouter = express_1.default.Router();
11
- exports.sceneServerRouter.get('*', async function (req, res, next) {
12
- const file = await (0, slpk_controller_1.getFileByUrl)('/');
13
- if (file) {
14
- const layer = JSON.parse(file.toString());
15
- const sceneServerResponse = (0, create_scene_server_1.createSceneServer)(layer.name, layer);
16
- res.send(sceneServerResponse);
17
- }
18
- else {
19
- res.status(404);
20
- res.send('File not found');
21
- }
22
- });
23
- exports.router = express_1.default.Router();
24
- exports.router.get('*', async function (req, res, next) {
25
- console.log(req.path);
26
- const file = await (0, slpk_controller_1.getFileByUrl)(req.path);
27
- if (file) {
28
- res.send(file);
29
- }
30
- else {
31
- res.status(404);
32
- res.send('File not found');
33
- }
34
- });
@@ -1,22 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createSceneServer = void 0;
4
- const uuid_1 = require("uuid");
5
- /**
6
- * Create `/SceneServer` response
7
- * @param name - service name, custom user-friendly name of the service
8
- * @param layer - I3S layer JSON
9
- * @returns reponse JSON for `/SceneServer` route
10
- */
11
- const createSceneServer = (name, layer) => {
12
- return {
13
- serviceItemId: (0, uuid_1.v4)().replace(/-/gi, ''),
14
- serviceName: name,
15
- name,
16
- currentVersion: '10.7',
17
- serviceVersion: '1.8',
18
- supportedBindings: ['REST'],
19
- layers: [layer]
20
- };
21
- };
22
- exports.createSceneServer = createSceneServer;
@@ -1,66 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.formListeningHandler = exports.formErrorHandler = exports.normalizePort = void 0;
7
- const debug_1 = __importDefault(require("debug"));
8
- const debug = (0, debug_1.default)('i3s-server:server');
9
- /**
10
- * Normalize a port into a number, string, or false.
11
- * @param val - port value from env variables
12
- * @returns - `number` for port, `string` for a named pipe, or `false` if the port number is not correct
13
- */
14
- function normalizePort(val) {
15
- const chkPort = parseInt(val, 10);
16
- if (Number.isNaN(chkPort)) {
17
- // named pipe
18
- return val;
19
- }
20
- if (chkPort >= 0) {
21
- // port number
22
- return chkPort;
23
- }
24
- return false;
25
- }
26
- exports.normalizePort = normalizePort;
27
- /**
28
- * Event listener creator for HTTP/HTTPS server "error" event.
29
- * @param optionalPort - the port/named pipe the server is started on
30
- * @return callback to handle server errors
31
- */
32
- function formErrorHandler(optionalPort) {
33
- return function onError(error) {
34
- if (error.syscall !== 'listen') {
35
- throw error;
36
- }
37
- const bind = typeof optionalPort === 'string' ? `Pipe ${optionalPort}` : `Port ${optionalPort}`;
38
- // handle specific listen errors with friendly messages
39
- switch (error.code) {
40
- case 'EACCES':
41
- console.error(`${bind} requires elevated privileges`); // eslint-disable-line no-console, no-undef
42
- process.exit(1); // eslint-disable-line no-process-exit, no-undef
43
- break;
44
- case 'EADDRINUSE':
45
- console.error(`${bind} is already in use`); // eslint-disable-line no-console, no-undef
46
- process.exit(1); // eslint-disable-line no-process-exit, no-undef
47
- break;
48
- default:
49
- throw error;
50
- }
51
- };
52
- }
53
- exports.formErrorHandler = formErrorHandler;
54
- /**
55
- * Event listener for HTTP/HTTPS server "listening" event.
56
- * @param optionalServer - http or https NodeJS server
57
- * @return callback that is triggered when the server has started
58
- */
59
- function formListeningHandler(optionalServer) {
60
- return function onListening() {
61
- const addr = optionalServer.address();
62
- const bind = typeof addr === 'string' ? `pipe ${addr}` : `port ${addr?.port}`;
63
- debug(`Listening on ${bind}`);
64
- };
65
- }
66
- exports.formListeningHandler = formListeningHandler;
package/dist/index.js DELETED
@@ -1,10 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Tiles3DConverter = exports.I3SConverter = void 0;
7
- var i3s_converter_1 = require("./i3s-converter/i3s-converter");
8
- Object.defineProperty(exports, "I3SConverter", { enumerable: true, get: function () { return __importDefault(i3s_converter_1).default; } });
9
- var _3d_tiles_converter_1 = require("./3d-tiles-converter/3d-tiles-converter");
10
- Object.defineProperty(exports, "Tiles3DConverter", { enumerable: true, get: function () { return __importDefault(_3d_tiles_converter_1).default; } });
@@ -1,82 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getBooleanValue = exports.getIntegerValue = exports.validateOptionsWithEqual = exports.getURLValue = exports.getStringValue = void 0;
4
- /**
5
- * Get string option value from cli arguments
6
- * @param index - option's name index in the argument's array.
7
- * The value of the option should be next to name of the option.
8
- * @param args - cli arguments array
9
- * @returns - string value of the option
10
- */
11
- function getStringValue(index, args) {
12
- if (index + 1 >= args.length) {
13
- return '';
14
- }
15
- const value = args[index + 1];
16
- if (value.indexOf('--') === 0) {
17
- return '';
18
- }
19
- return value;
20
- }
21
- exports.getStringValue = getStringValue;
22
- /**
23
- * Modyfy URL path to be compatible with fetch
24
- * @param index - option's name index in the argument's array.
25
- * The value of the option should be next to name of the option.
26
- * @param args - cli arguments array
27
- * @returns - string value of the option
28
- */
29
- function getURLValue(index, args) {
30
- const value = getStringValue(index, args);
31
- console.log(`Input tileset value: ${value}`);
32
- console.log(`Modified tileset value: ${value.replace(/\\/g, '/')}`);
33
- return value.replace(/\\/g, '/');
34
- }
35
- exports.getURLValue = getURLValue;
36
- function validateOptionsWithEqual(args) {
37
- return args.reduce((acc, curr) => {
38
- const equalSignIndex = curr.indexOf('=');
39
- const beforeEqual = curr.slice(0, equalSignIndex);
40
- const afterEqual = curr.slice(equalSignIndex + 1, curr.length);
41
- const condition = curr.includes('=') && curr.startsWith('--') && afterEqual;
42
- if (condition) {
43
- return acc.concat(beforeEqual, afterEqual);
44
- }
45
- return acc.concat(curr);
46
- }, []);
47
- }
48
- exports.validateOptionsWithEqual = validateOptionsWithEqual;
49
- /**
50
- * Get integer option value from cli arguments
51
- * @param index - option's name index in the argument's array
52
- * The value of the option should be next to name of the option.
53
- * @param args - cli arguments array
54
- * @returns - number value of the option
55
- */
56
- function getIntegerValue(index, args) {
57
- const stringValue = getStringValue(index, args);
58
- const result = Number.parseInt(stringValue);
59
- if (isFinite(result)) {
60
- return result;
61
- }
62
- return NaN;
63
- }
64
- exports.getIntegerValue = getIntegerValue;
65
- /**
66
- * Get boolean option value from cli arguments
67
- * @param index - option's name index in the argument's array
68
- * The value of the option should be next to name of the option.
69
- * @param args - cli arguments array
70
- * @returns - boolean value of the option
71
- */
72
- function getBooleanValue(index, args) {
73
- const stringValue = getStringValue(index, args).toLowerCase().trim();
74
- if (['--no-draco', '--split-nodes'].includes(args[index]) && !stringValue) {
75
- return false;
76
- }
77
- if (!stringValue || stringValue === 'true') {
78
- return true;
79
- }
80
- return false;
81
- }
82
- exports.getBooleanValue = getBooleanValue;
@@ -1,257 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.addFileToZip = exports.generateHash128FromZip = exports.compressWithChildProcess = exports.compressFilesWithZip = exports.compressFileWithGzip = void 0;
7
- const zlib_1 = require("zlib");
8
- const path_1 = require("path");
9
- const fs_1 = require("fs");
10
- const archiver_1 = __importDefault(require("archiver"));
11
- const file_utils_1 = require("./file-utils");
12
- const worker_utils_1 = require("@loaders.gl/worker-utils");
13
- const jszip_1 = __importDefault(require("jszip"));
14
- const crypto_1 = require("@loaders.gl/crypto");
15
- const crypt_1 = __importDefault(require("crypt"));
16
- const file_utils_2 = require("./file-utils");
17
- /**
18
- * Compress file to gzip file
19
- *
20
- * @param pathFile - the path to the file
21
- * @return the path to the gzip file
22
- */
23
- function compressFileWithGzip(pathFile) {
24
- const compressedPathFile = `${pathFile}.gz`;
25
- const gzip = (0, zlib_1.createGzip)();
26
- const input = (0, fs_1.createReadStream)(pathFile);
27
- const output = (0, fs_1.createWriteStream)(compressedPathFile);
28
- return new Promise((resolve, reject) => {
29
- input.on('end', () => {
30
- console.log(`${compressedPathFile} compressed and saved.`); // eslint-disable-line no-undef,no-console
31
- resolve(compressedPathFile);
32
- });
33
- input.on('error', (error) => {
34
- console.log(`${compressedPathFile}: compression error!`); // eslint-disable-line no-undef,no-console
35
- reject(error);
36
- });
37
- input.pipe(gzip).pipe(output);
38
- });
39
- }
40
- exports.compressFileWithGzip = compressFileWithGzip;
41
- /**
42
- * Compress files from map into slpk file
43
- *
44
- * @param fileMap - map with file paths (key: output path, value: input path)
45
- * @param outputFile - output slpk file
46
- * @param level - compression level
47
- */
48
- async function compressFilesWithZip(fileMap, outputFile, level = 0) {
49
- // Before creating a new file, we need to delete the old file
50
- try {
51
- await (0, file_utils_1.removeFile)(outputFile);
52
- }
53
- catch (e) {
54
- // Do nothing if old file doesn't exist
55
- }
56
- const output = (0, fs_1.createWriteStream)(outputFile);
57
- const archive = (0, archiver_1.default)('zip', {
58
- zlib: { level } // Sets the compression level.
59
- });
60
- return new Promise(async (resolve, reject) => {
61
- // listen for all archive data to be writte
62
- // 'close' event is fired only when a file descriptor is involved
63
- output.on('close', function () {
64
- console.log(`${outputFile} saved.`); // eslint-disable-line no-undef,no-console
65
- console.log(`${archive.pointer()} total bytes`); // eslint-disable-line no-undef,no-console
66
- resolve(null);
67
- });
68
- // This event is fired when the data source is drained no matter what was the data source.
69
- // It is not part of this library but rather from the NodeJS Stream API.
70
- // @see: https://nodejs.org/api/stream.html#stream_event_end
71
- output.on('end', function () {
72
- console.log('Data has been drained'); // eslint-disable-line no-undef,no-console
73
- resolve(null);
74
- });
75
- // good practice to catch warnings (ie stat failures and other non-blocking errors)
76
- archive.on('warning', function (err) {
77
- console.log(err); // eslint-disable-line no-undef,no-console
78
- reject(err);
79
- });
80
- // good practice to catch this error explicitly
81
- archive.on('error', function (err) {
82
- reject(err);
83
- });
84
- // pipe archive data to the file
85
- archive.pipe(output);
86
- for (const subFileName in fileMap) {
87
- const subFileData = fileMap[subFileName];
88
- await appendFileToArchive(archive, subFileName, subFileData);
89
- }
90
- // finalize the archive (ie we are done appending files but streams have to finish yet)
91
- archive.finalize();
92
- });
93
- }
94
- exports.compressFilesWithZip = compressFilesWithZip;
95
- /**
96
- * Compress files using external tool 'zip'/'7z'
97
- *
98
- * @param inputFolder - folder to archive - for cwd option
99
- * @param outputFile - output slpk file
100
- * @param level - compression level
101
- * @param inputFiles - input files path to pass to the executable as option
102
- * @param sevenZipExe - path to 7z.exe executable
103
- */
104
- async function compressWithChildProcess(inputFolder, outputFile, level, inputFiles, sevenZipExe) {
105
- // eslint-disable-next-line no-undef
106
- if (process.platform === 'win32') {
107
- await compressWithChildProcessWindows(inputFolder, outputFile, level, inputFiles, sevenZipExe);
108
- }
109
- else {
110
- await compressWithChildProcessUnix(inputFolder, outputFile, level, inputFiles);
111
- }
112
- }
113
- exports.compressWithChildProcess = compressWithChildProcess;
114
- /**
115
- * Compress files using external linux tool 'zip'
116
- *
117
- * @param inputFolder - folder to archive - for cwd option
118
- * @param outputFile - output slpk file
119
- * @param level - compression level
120
- * @param inputFiles - input files path to pass to the executable as option
121
- */
122
- async function compressWithChildProcessUnix(inputFolder, outputFile, level = 0, inputFiles = '.') {
123
- const fullOutputFile = (0, file_utils_2.getAbsoluteFilePath)(outputFile);
124
- const args = [`-${level}`, '-r', fullOutputFile, inputFiles];
125
- const childProcess = new worker_utils_1.ChildProcessProxy();
126
- await childProcess.start({
127
- command: 'zip',
128
- arguments: args,
129
- spawn: {
130
- cwd: inputFolder
131
- },
132
- wait: 0
133
- });
134
- }
135
- /**
136
- * Compress files using windows external tool '7z'
137
- *
138
- * @param inputFolder - folder to archive - for cwd option
139
- * @param outputFile - output slpk file
140
- * @param level - compression level
141
- * @param inputFiles - input files path to pass to the executable as option
142
- * @param sevenZipExe - path to 7z.exe executable
143
- */
144
- async function compressWithChildProcessWindows(inputFolder, outputFile, level = 0, inputFiles = (0, path_1.join)('.', '*'), sevenZipExe) {
145
- // Workaround for @listfile issue. In 7z.exe @-leading files are handled as listfiles
146
- // https://sevenzip.osdn.jp/chm/cmdline/syntax.htm
147
- if (inputFiles[0] === '@') {
148
- inputFiles = `*${inputFiles.substr(1)}`;
149
- }
150
- const fullOutputFile = (0, file_utils_2.getAbsoluteFilePath)(outputFile);
151
- const args = ['a', '-tzip', `-mx=${level}`, fullOutputFile, inputFiles];
152
- const childProcess = new worker_utils_1.ChildProcessProxy();
153
- await childProcess.start({
154
- command: sevenZipExe,
155
- arguments: args,
156
- spawn: {
157
- cwd: `${inputFolder}`
158
- },
159
- wait: 0
160
- });
161
- }
162
- /**
163
- * Generate hash file from zip archive
164
- * https://github.com/Esri/i3s-spec/blob/master/docs/1.7/slpk_hashtable.cmn.md
165
- *
166
- * @param inputZipFile
167
- * @param outputFile
168
- */
169
- async function generateHash128FromZip(inputZipFile, outputFile) {
170
- const input = await fs_1.promises.readFile(inputZipFile);
171
- const zip = await jszip_1.default.loadAsync(input);
172
- const hashTable = [];
173
- const zipFiles = zip.files;
174
- for (const relativePath in zipFiles) {
175
- const zipEntry = zipFiles[relativePath];
176
- // Had to use a workaround because the correct string is getting the wrong data
177
- // const content = await zipEntry.async('nodebuffer');
178
- // _data isn't described in the interface, so lint thought it was wrong
179
- const _data = '_data';
180
- const content = zipEntry[_data].compressedContent;
181
- if (zipEntry.dir)
182
- continue; // eslint-disable-line no-continue
183
- // eslint-disable-next-line no-undef
184
- const hash = await new crypto_1.MD5Hash().hash(Buffer.from(relativePath.toLowerCase()));
185
- // eslint-disable-next-line no-undef
186
- hashTable.push({ key: atob(hash), value: content.byteOffset });
187
- }
188
- hashTable.sort((prev, next) => {
189
- if (prev.key === next.key) {
190
- return prev.value < next.value ? -1 : 1;
191
- }
192
- return prev.key < next.key ? -1 : 1;
193
- });
194
- const output = (0, fs_1.createWriteStream)(outputFile);
195
- return new Promise((resolve, reject) => {
196
- output.on('close', function () {
197
- console.log(`${outputFile} generated and saved`); // eslint-disable-line
198
- resolve(null);
199
- });
200
- output.on('error', function (err) {
201
- console.log(err); // eslint-disable-line
202
- reject(err);
203
- });
204
- for (const key in hashTable) {
205
- const item = hashTable[key];
206
- const value = longToByteArray(item.value);
207
- // TODO: perhaps you need to wait for the 'drain' event if the write returns 'false'
208
- // eslint-disable-next-line no-undef
209
- output.write(Buffer.from(crypt_1.default.hexToBytes(item.key).concat(value)));
210
- }
211
- output.close();
212
- });
213
- }
214
- exports.generateHash128FromZip = generateHash128FromZip;
215
- /**
216
- * Encode 64 bit value to byte array
217
- *
218
- * @param long - stringified number
219
- * @returns
220
- */
221
- function longToByteArray(long) {
222
- const buffer = new ArrayBuffer(8); // JS numbers are 8 bytes long, or 64 bits
223
- const longNum = new Float64Array(buffer); // so equivalent to Float64
224
- longNum[0] = parseInt(long);
225
- return Array.from(new Uint8Array(buffer)).reverse(); // reverse to get little endian
226
- }
227
- /**
228
- * Add file to zip archive
229
- *
230
- * @param inputFile
231
- * @param fileName
232
- * @param zipFile
233
- * @param sevenZipExe
234
- */
235
- async function addFileToZip(inputFolder, fileName, zipFile, sevenZipExe) {
236
- await compressWithChildProcess(inputFolder, zipFile, 0, fileName, sevenZipExe);
237
- console.log(`${fileName} added to ${zipFile}.`); // eslint-disable-line
238
- }
239
- exports.addFileToZip = addFileToZip;
240
- /**
241
- *
242
- * @param archive zip archive instance
243
- * @param subFileName file path inside archive
244
- * @param subFileData source file path
245
- * @returns
246
- */
247
- function appendFileToArchive(archive, subFileName, subFileData) {
248
- return new Promise((resolve) => {
249
- const fileStream = (0, fs_1.createReadStream)(subFileData);
250
- console.log(`Compression start: ${subFileName}`); // eslint-disable-line no-undef,no-console
251
- fileStream.on('close', () => {
252
- console.log(`Compression finish: ${subFileName}`); // eslint-disable-line no-undef,no-console
253
- resolve(null);
254
- });
255
- archive.append(fileStream, { name: subFileName });
256
- });
257
- }