@loaders.gl/tile-converter 4.1.0-alpha.10 → 4.1.0-alpha.11

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.
@@ -1,13 +1,5 @@
1
1
  import {createGzip} from 'zlib';
2
- import {join} from 'path';
3
- import {promises as fs, createReadStream, createWriteStream} from 'fs';
4
- import archiver from 'archiver';
5
- import {removeFile} from './file-utils';
6
- import {ChildProcessProxy} from '@loaders.gl/worker-utils';
7
- import JSZip from 'jszip';
8
- import {MD5Hash} from '@loaders.gl/crypto';
9
- import crypt from 'crypt';
10
- import {getAbsoluteFilePath} from './file-utils';
2
+ import {createReadStream, createWriteStream} from 'fs';
11
3
 
12
4
  /**
13
5
  * Compress file to gzip file
@@ -33,258 +25,3 @@ export function compressFileWithGzip(pathFile: string): Promise<string> {
33
25
  input.pipe(gzip).pipe(output);
34
26
  });
35
27
  }
36
-
37
- /**
38
- * Compress files from map into slpk file
39
- *
40
- * @param fileMap - map with file paths (key: output path, value: input path)
41
- * @param outputFile - output slpk file
42
- * @param level - compression level
43
- */
44
- export async function compressFilesWithZip(
45
- fileMap: {[key: string]: string},
46
- outputFile: string,
47
- level: number = 0
48
- ) {
49
- // Before creating a new file, we need to delete the old file
50
- try {
51
- await removeFile(outputFile);
52
- } catch (e) {
53
- // Do nothing if old file doesn't exist
54
- }
55
-
56
- const output = createWriteStream(outputFile);
57
- const archive = archiver('zip', {
58
- zlib: {level} // Sets the compression level.
59
- });
60
-
61
- return new Promise(async (resolve, reject) => {
62
- // listen for all archive data to be writte
63
- // 'close' event is fired only when a file descriptor is involved
64
- output.on('close', function () {
65
- console.log(`${outputFile} saved.`); // eslint-disable-line no-undef,no-console
66
- console.log(`${archive.pointer()} total bytes`); // eslint-disable-line no-undef,no-console
67
- resolve(null);
68
- });
69
-
70
- // This event is fired when the data source is drained no matter what was the data source.
71
- // It is not part of this library but rather from the NodeJS Stream API.
72
- // @see: https://nodejs.org/api/stream.html#stream_event_end
73
- output.on('end', function () {
74
- console.log('Data has been drained'); // eslint-disable-line no-undef,no-console
75
- resolve(null);
76
- });
77
-
78
- // good practice to catch warnings (ie stat failures and other non-blocking errors)
79
- archive.on('warning', function (err) {
80
- console.log(err); // eslint-disable-line no-undef,no-console
81
- reject(err);
82
- });
83
-
84
- // good practice to catch this error explicitly
85
- archive.on('error', function (err) {
86
- reject(err);
87
- });
88
-
89
- // pipe archive data to the file
90
- archive.pipe(output);
91
-
92
- for (const subFileName in fileMap) {
93
- const subFileData = fileMap[subFileName];
94
- await appendFileToArchive(archive, subFileName, subFileData);
95
- }
96
-
97
- // finalize the archive (ie we are done appending files but streams have to finish yet)
98
- archive.finalize();
99
- });
100
- }
101
-
102
- /**
103
- * Compress files using external tool 'zip'/'7z'
104
- *
105
- * @param inputFolder - folder to archive - for cwd option
106
- * @param outputFile - output slpk file
107
- * @param level - compression level
108
- * @param inputFiles - input files path to pass to the executable as option
109
- * @param sevenZipExe - path to 7z.exe executable
110
- */
111
- export async function compressWithChildProcess(
112
- inputFolder: string,
113
- outputFile: string,
114
- level: number,
115
- inputFiles: string,
116
- sevenZipExe: string
117
- ) {
118
- // eslint-disable-next-line no-undef
119
- if (process.platform === 'win32') {
120
- await compressWithChildProcessWindows(inputFolder, outputFile, level, inputFiles, sevenZipExe);
121
- } else {
122
- await compressWithChildProcessUnix(inputFolder, outputFile, level, inputFiles);
123
- }
124
- }
125
-
126
- /**
127
- * Compress files using external linux tool 'zip'
128
- *
129
- * @param inputFolder - folder to archive - for cwd option
130
- * @param outputFile - output slpk file
131
- * @param level - compression level
132
- * @param inputFiles - input files path to pass to the executable as option
133
- */
134
- async function compressWithChildProcessUnix(
135
- inputFolder: string,
136
- outputFile: string,
137
- level: number = 0,
138
- inputFiles: string = '.'
139
- ) {
140
- const fullOutputFile = getAbsoluteFilePath(outputFile);
141
- const args = [`-${level}`, '-r', fullOutputFile, inputFiles];
142
- const childProcess = new ChildProcessProxy();
143
- await childProcess.start({
144
- command: 'zip',
145
- arguments: args,
146
- spawn: {
147
- cwd: inputFolder
148
- },
149
- wait: 0
150
- });
151
- }
152
-
153
- /**
154
- * Compress files using windows external tool '7z'
155
- *
156
- * @param inputFolder - folder to archive - for cwd option
157
- * @param outputFile - output slpk file
158
- * @param level - compression level
159
- * @param inputFiles - input files path to pass to the executable as option
160
- * @param sevenZipExe - path to 7z.exe executable
161
- */
162
- async function compressWithChildProcessWindows(
163
- inputFolder: string,
164
- outputFile: string,
165
- level: number = 0,
166
- inputFiles: string = join('.', '*'),
167
- sevenZipExe: string
168
- ) {
169
- // Workaround for @listfile issue. In 7z.exe @-leading files are handled as listfiles
170
- // https://sevenzip.osdn.jp/chm/cmdline/syntax.htm
171
- if (inputFiles[0] === '@') {
172
- inputFiles = `*${inputFiles.substr(1)}`;
173
- }
174
-
175
- const fullOutputFile = getAbsoluteFilePath(outputFile);
176
- const args = ['a', '-tzip', `-mx=${level}`, fullOutputFile, inputFiles];
177
- const childProcess = new ChildProcessProxy();
178
- await childProcess.start({
179
- command: sevenZipExe,
180
- arguments: args,
181
- spawn: {
182
- cwd: `${inputFolder}`
183
- },
184
- wait: 0
185
- });
186
- }
187
-
188
- /**
189
- * Generate hash file from zip archive
190
- * https://github.com/Esri/i3s-spec/blob/master/docs/1.7/slpk_hashtable.cmn.md
191
- *
192
- * @param inputZipFile
193
- * @param outputFile
194
- */
195
- export async function generateHash128FromZip(inputZipFile: string, outputFile: string) {
196
- const input = await fs.readFile(inputZipFile);
197
- const zip = await JSZip.loadAsync(input);
198
- const hashTable: {key: string; value: string}[] = [];
199
- const zipFiles = zip.files;
200
- for (const relativePath in zipFiles) {
201
- const zipEntry = zipFiles[relativePath];
202
- // Had to use a workaround because the correct string is getting the wrong data
203
- // const content = await zipEntry.async('nodebuffer');
204
- // _data isn't described in the interface, so lint thought it was wrong
205
- const _data = '_data';
206
- const content = zipEntry[_data].compressedContent;
207
- if (zipEntry.dir) continue; // eslint-disable-line no-continue
208
- // eslint-disable-next-line no-undef
209
- const hash = await new MD5Hash().hash(Buffer.from(relativePath.toLowerCase()), 'base64');
210
- // eslint-disable-next-line no-undef
211
- hashTable.push({key: atob(hash), value: content.byteOffset});
212
- }
213
-
214
- hashTable.sort((prev, next) => {
215
- if (prev.key === next.key) {
216
- return prev.value < next.value ? -1 : 1;
217
- }
218
- return prev.key < next.key ? -1 : 1;
219
- });
220
-
221
- const output = createWriteStream(outputFile);
222
- return new Promise((resolve, reject) => {
223
- output.on('close', function () {
224
- console.log(`${outputFile} generated and saved`); // eslint-disable-line
225
- resolve(null);
226
- });
227
- output.on('error', function (err) {
228
- console.log(err); // eslint-disable-line
229
- reject(err);
230
- });
231
- for (const key in hashTable) {
232
- const item = hashTable[key];
233
- const value = longToByteArray(item.value);
234
- // TODO: perhaps you need to wait for the 'drain' event if the write returns 'false'
235
- // eslint-disable-next-line no-undef
236
- output.write(Buffer.from(crypt.hexToBytes(item.key).concat(value)));
237
- }
238
- output.close();
239
- });
240
- }
241
-
242
- /**
243
- * Encode 64 bit value to byte array
244
- *
245
- * @param long - stringified number
246
- * @returns
247
- */
248
- function longToByteArray(long: string): number[] {
249
- const buffer = new ArrayBuffer(8); // JS numbers are 8 bytes long, or 64 bits
250
- const longNum = new Float64Array(buffer); // so equivalent to Float64
251
- longNum[0] = parseInt(long);
252
- return Array.from(new Uint8Array(buffer)).reverse(); // reverse to get little endian
253
- }
254
-
255
- /**
256
- * Add file to zip archive
257
- *
258
- * @param inputFile
259
- * @param fileName
260
- * @param zipFile
261
- * @param sevenZipExe
262
- */
263
- export async function addFileToZip(
264
- inputFolder: string,
265
- fileName: string,
266
- zipFile: string,
267
- sevenZipExe: string
268
- ) {
269
- await compressWithChildProcess(inputFolder, zipFile, 0, fileName, sevenZipExe);
270
- console.log(`${fileName} added to ${zipFile}.`); // eslint-disable-line
271
- }
272
-
273
- /**
274
- *
275
- * @param archive zip archive instance
276
- * @param subFileName file path inside archive
277
- * @param subFileData source file path
278
- * @returns
279
- */
280
- function appendFileToArchive(archive: any, subFileName: string, subFileData: string) {
281
- return new Promise((resolve) => {
282
- const fileStream = createReadStream(subFileData);
283
- console.log(`Compression start: ${subFileName}`); // eslint-disable-line no-undef,no-console
284
- fileStream.on('close', () => {
285
- console.log(`Compression finish: ${subFileName}`); // eslint-disable-line no-undef,no-console
286
- resolve(null);
287
- });
288
- archive.append(fileStream, {name: subFileName});
289
- });
290
- }
@@ -20,7 +20,8 @@ export type ConversionDumpOptions = {
20
20
 
21
21
  type NodeDoneStatus = {
22
22
  nodeId: number;
23
- done: Record<string, boolean> | boolean;
23
+ done: boolean;
24
+ progress: Record<string, boolean>;
24
25
  };
25
26
 
26
27
  type TilesConverted = {
@@ -140,7 +141,7 @@ export class ConversionDump {
140
141
  */
141
142
  async addNode(filename: string, nodeId: number) {
142
143
  const {nodes} = this.getRecord(filename) || {nodes: []};
143
- nodes.push({nodeId, done: {}});
144
+ nodes.push({nodeId, done: false, progress: {}});
144
145
  if (nodes.length === 1) {
145
146
  this.setRecord(filename, {nodes});
146
147
  }
@@ -159,7 +160,10 @@ export class ConversionDump {
159
160
  (element) => element.nodeId === nodeId
160
161
  );
161
162
  if (nodeDump) {
162
- nodeDump.done[resourceType] = value;
163
+ nodeDump.progress[resourceType] = value;
164
+ if (!value) {
165
+ nodeDump.done = false;
166
+ }
163
167
  }
164
168
  }
165
169
 
@@ -177,18 +181,19 @@ export class ConversionDump {
177
181
  const {sourceId, resourceType, outputId} = changedRecords[i];
178
182
  if (!sourceId || !resourceType || !outputId) continue;
179
183
  for (const node of this.tilesConverted[sourceId].nodes) {
180
- if (typeof node.done !== 'boolean' && node.nodeId === outputId) {
181
- node.done[resourceType] = true;
182
- }
183
- if (typeof node.done !== 'boolean') {
184
+ if (node.nodeId === outputId) {
185
+ node.progress[resourceType] = true;
186
+
184
187
  let done = false;
185
- for (const key in node.done) {
186
- done = node.done[key];
188
+ for (const key in node.progress) {
189
+ done = node.progress[key];
187
190
  if (!done) break;
188
191
  }
189
- if (done) {
190
- node.done = true;
192
+ node.done = done;
193
+ if (node.done) {
194
+ node.progress = {};
191
195
  }
196
+ break;
192
197
  }
193
198
  }
194
199
  }