encoding-tools 0.0.4 → 0.0.7

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.
package/libbin2txt CHANGED
@@ -27,144 +27,341 @@
27
27
  * If not, see <https://www.gnu.org/licenses/>.
28
28
  */
29
29
 
30
+ /* eslint-disable
31
+ no-unused-vars */
32
+
30
33
  const
31
34
  _libcrash =
32
35
  require(
33
36
  "crash-js");
37
+ /* global
38
+ _cat */
39
+ const
40
+ _cat =
41
+ _libcrash._cat;
42
+ /* global
43
+ _dirname */
34
44
  const
35
45
  _dirname =
36
46
  _libcrash._dirname;
47
+ /* global
48
+ _error_display */
37
49
  const
38
50
  _error_display =
39
51
  _libcrash._error_display;
52
+ /* global
53
+ _ext_rm */
40
54
  const
41
55
  _ext_rm =
42
56
  _libcrash._ext_rm;
57
+ /* global
58
+ _file_exists */
43
59
  const
44
60
  _file_exists =
45
61
  _libcrash._file_exists;
62
+ /* global
63
+ _file_read */
46
64
  const
47
65
  _file_read =
48
66
  _libcrash._file_read;
67
+ /* global
68
+ _file_write */
49
69
  const
50
70
  _file_write =
51
71
  _libcrash._file_write;
72
+ /* global
73
+ _fs_worker_start */
52
74
  const
53
75
  _fs_worker_start =
54
76
  _libcrash._fs_worker_start;
77
+ /* global
78
+ _json_read */
55
79
  const
56
80
  _json_read =
57
81
  _libcrash._json_read;
82
+ /* global
83
+ _ls */
58
84
  const
59
85
  _ls =
60
86
  _libcrash._ls;
87
+ /* global
88
+ _mkdir */
61
89
  const
62
90
  _mkdir =
63
91
  _libcrash._mkdir;
92
+ /* global
93
+ _msg_info */
64
94
  const
65
95
  _msg_info =
66
96
  _libcrash._msg_info;
97
+ /* global
98
+ _msg_error */
67
99
  const
68
100
  _msg_error =
69
101
  _libcrash._msg_error;
102
+ /* global
103
+ _printf */
104
+ const
105
+ _printf =
106
+ _libcrash._printf;
107
+ /* global
108
+ _rm */
109
+ const
110
+ _rm =
111
+ _libcrash._rm;
112
+ /* global
113
+ _seq */
114
+ const
115
+ _seq =
116
+ _libcrash._seq;
117
+ /* global
118
+ _stat */
70
119
  const
71
120
  _stat =
72
121
  _libcrash._stat;
73
122
  const
74
123
  _tmcsplit =
75
- require(
76
- "tmcsplit");
124
+ require(
125
+ "tmcsplit");
126
+ /* global
127
+ _split */
77
128
  const
78
129
  _split =
79
130
  _tmcsplit._split;
80
131
 
81
132
  async function
82
- _blocks_find(
83
- _pattern) {
133
+ _buffer_prepare(
134
+ _in,
135
+ _out,
136
+ _buffer_size_bytes) {
84
137
  let
85
- _dir,
86
- _dir_content,
87
138
  _msg;
139
+ const
140
+ _size_max =
141
+ 0;
142
+ const
143
+ _in_size_bytes =
144
+ _stat(
145
+ _in).size;
88
146
  _msg =
89
- "Looking for files " +
90
- `respecting pattern '^${_pattern}*.`;
147
+ `File '${_in}' has size ` +
148
+ `'${_in_size_bytes}' bytes.`;
91
149
  _msg_info(
92
150
  _msg);
93
- _dir =
94
- _dirname(
95
- _pattern);
96
- console.log(_dir);
97
- _dir_content =
98
- _ls(
99
- _dir);
100
- for ( _file of _dir_content ) {
101
- if ( _file.startsWith(
102
- _pattern)) {
103
- }
151
+ const
152
+ _blocks_total =
153
+ Math.ceil(
154
+ _in_size_bytes /
155
+ _buffer_size_bytes);
156
+ _msg =
157
+ `Splitting file '${_in}' in ` +
158
+ `'${_blocks_total}' blocks of ` +
159
+ `'${_buffer_size_bytes}' bytes.`;
160
+ _msg_info(
161
+ _msg);
162
+ const
163
+ _split_opts = [
164
+ _in,
165
+ `${_out}.block.`,
166
+ _blocks_total,
167
+ _size_max
168
+ ];
169
+ const
170
+ _output_files =
171
+ await _split.apply(
172
+ null,
173
+ _split_opts);
174
+ _msg =
175
+ "Done.";
176
+ _msg_info(
177
+ _msg);
178
+ return _output_files;
179
+ }
180
+
181
+ function
182
+ _base64_block_encode(
183
+ _file) {
184
+ let
185
+ _string;
186
+ const
187
+ _content =
188
+ _cat(
189
+ _file);
190
+ if ( typeof window === 'undefined' &&
191
+ ( typeof global !== 'undefined' &&
192
+ global.global === global ) &&
193
+ typeof __webpack_require__ !== 'function' ) {
194
+ const
195
+ _buffer =
196
+ Buffer.from(
197
+ _content);
198
+ _string =
199
+ _buffer.toString(
200
+ "base64");
201
+ }
202
+ else {
203
+ _string =
204
+ btoa(
205
+ _string);
104
206
  }
105
- console.log(
106
- typeof(_dir_content));
107
- console.log(
108
- _dir_content);
207
+ return _string;
109
208
  }
110
209
 
111
210
  async function
112
- _buffer_prepare(
211
+ _chunk_split(
113
212
  _in,
213
+ _chunk_current,
214
+ _length,
215
+ _prefix,
216
+ _block_last) {
217
+ let
218
+ _chunk,
219
+ _chunk_start,
220
+ _content,
221
+ _cut,
222
+ _msg,
223
+ _out;
224
+ const
225
+ _total =
226
+ _in.length;
227
+ const
228
+ _chunks =
229
+ Math.ceil(
230
+ _chunk_current +
231
+ _total /
232
+ _length);
233
+ if ( _chunks < 2 ) {
234
+ _out =
235
+ _prefix;
236
+ _file_write(
237
+ _out,
238
+ _in);
239
+ return 0;
240
+ }
241
+ _msg =
242
+ `Chunking '${_total}' ` +
243
+ `letters into '${_chunks}' ` +
244
+ `chunks of length '${_length}'.`;
245
+ _msg_info(
246
+ _msg);
247
+ for ( _chunk of _seq(
248
+ _chunk_current + 1,
249
+ _chunks) ) {
250
+ _chunk_start =
251
+ ( ( _chunk -
252
+ _chunk_current ) *
253
+ _length ) -
254
+ _length;
255
+ _cut =
256
+ _in.substring(
257
+ _chunk_start,
258
+ _chunk_start + _length);
259
+ _out =
260
+ `${_prefix}.${_chunk}`;
261
+ _msg =
262
+ `Writing chunk ${_chunk} in file '${_out}' ` +
263
+ `of length ${_cut.length} out of ${_chunks}.`;
264
+ _msg_info(
265
+ _msg);
266
+ if ( _chunk < _chunks ) {
267
+ _printf(
268
+ _out,
269
+ _cut);
270
+ }
271
+ else if ( _chunk == _chunks ) {
272
+ if ( _block_last == "true" ) {
273
+ _file_write(
274
+ _out,
275
+ _cut);
276
+ }
277
+ else if ( _block_last == "false" ) {
278
+ _msg =
279
+ "Last chunk of non-last block, " +
280
+ "adding a newline character at the end (nope).";
281
+ _msg_info(
282
+ _msg);
283
+ _file_write(
284
+ _out,
285
+ _cut);
286
+ }
287
+ }
288
+ }
289
+ _chunk_current =
290
+ _chunks;
291
+ _msg=
292
+ "Finished encoding block.";
293
+ _msg_info(
294
+ _msg);
295
+ return _chunk_current;
296
+ }
297
+
298
+ async function
299
+ _base64_encode(
300
+ _block,
301
+ _chunk_current,
302
+ _length,
114
303
  _out,
115
- _buffer_size_bytes) {
304
+ _block_last,
305
+ _amount_only) {
116
306
  let
117
- _in_size_bytes,
118
- _size_max,
119
- _split_opts,
120
307
  _msg;
121
- _size_max =
122
- 0;
123
- if ( ! _file_exists(
124
- _in) ) {
308
+ const
309
+ _txt =
310
+ _base64_block_encode(
311
+ _block);
312
+ _msg =
313
+ `Block '${_block}' encoded ` +
314
+ `length: ${_txt.length}.`;
315
+ _msg_info(
316
+ _msg);
317
+ if ( _amount_only === "false" ) {
318
+ _chunk_current =
319
+ await _chunk_split(
320
+ _txt,
321
+ _chunk_current,
322
+ _length,
323
+ _out,
324
+ _block_last);
325
+ }
326
+ else if ( _amount_only === "true" ) {
327
+ const
328
+ _chunks_total =
329
+ Math.ceil(
330
+ _chunks_total +
331
+ _txt.length /
332
+ _length);
333
+ _chunk_current =
334
+ _chunks_total;
335
+ }
336
+ else {
125
337
  _msg =
126
- `File '${_in}' does ` +
127
- 'not exist.';
338
+ `Value of the 'amount_only' can ` +
339
+ `be true or false. Current: '${_amount_only}'.`;
128
340
  _msg_error(
129
341
  _msg,
130
342
  1);
131
343
  }
132
- // console.log(
133
- // _in);
134
- _in_size_bytes =
135
- _stat(
136
- _in).size;
137
344
  _msg =
138
- `File '${_in}' has size ` +
139
- `'${_in_size_bytes}' bytes.`;
345
+ "Finished encoding block " +
346
+ `'${_block}'.`;
140
347
  _msg_info(
141
348
  _msg);
142
- console.log(
143
- typeof(
144
- _in_size_bytes));
145
- _blocks_total =
146
- Math.ceil(
147
- _in_size_bytes /
148
- _buffer_size_bytes);
149
- console.log(_in_size_bytes);
150
- console.log(_buffer_size_bytes);
151
- console.log(_blocks_total);
152
- _split_opts = [
153
- _in,
154
- `${_out}.block.`,
155
- _blocks_total,
156
- _size_max
157
- ];
158
- await _split.apply(
159
- null,
160
- _split_opts);
161
- _blocks_find(
162
- `${_out}.sf-part`);
163
- // console.log(
164
- // _stat(
165
- // _in).size / 1000);
166
- // console.log(
167
- // _out);
349
+ return _chunk_current;
350
+ }
351
+
352
+ async function
353
+ _input_data_check(
354
+ _in) {
355
+ if ( ! _file_exists(
356
+ _in) ) {
357
+ const
358
+ _msg =
359
+ `File '${_in}' does ` +
360
+ 'not exist.';
361
+ _msg_error(
362
+ _msg,
363
+ 1);
364
+ }
168
365
  }
169
366
 
170
367
  async function
@@ -176,63 +373,102 @@ async function
176
373
  _buffer_size_bytes,
177
374
  _amount_only) {
178
375
  let
179
- _blocks_total,
180
- _chunks_total,
376
+ _block,
181
377
  _chunk_current,
378
+ _chunks_total,
182
379
  _block_current,
183
380
  _block_last,
184
- _dir,
185
- _file,
186
381
  _msg,
382
+ _rm_args,
187
383
  _worker;
188
- _dir =
189
- "/test_dir";
190
- _file =
191
- `${_dir}/test_file`;
192
- _content =
193
- `${_in} ${_out}`;
194
384
  if ( typeof window !== 'undefined' ) {
195
- await _fs_worker_start(
196
- "fs-worker.js");
385
+ const
386
+ _worker =
387
+ await _fs_worker_start(
388
+ "fs-worker.js");
389
+ _msg =
390
+ `OPFS worker '${_worker}' started.`;
391
+ _msg_info(
392
+ _msg);
393
+ }
394
+ _input_data_check(
395
+ _in);
396
+ const
397
+ _blocks =
398
+ await _buffer_prepare(
399
+ _in,
400
+ _out,
401
+ _buffer_size_bytes);
402
+ const
403
+ _blocks_total =
404
+ _blocks.length;
405
+ _chunk_current =
406
+ 0;
407
+ _block_current =
408
+ 1;
409
+ for ( _block of _blocks ) {
410
+ _msg =
411
+ `Reading block '${_block}' ` +
412
+ `('${_block_current}' ` +
413
+ `of '${_blocks_total}').`;
414
+ _msg_info(
415
+ _msg);
416
+ if ( _block_current < _blocks_total ) {
417
+ _block_last =
418
+ "false";
419
+ }
420
+ else if ( _block_current == _blocks_total ) {
421
+ _block_last =
422
+ "true";
423
+ }
424
+ if ( _format === "base64" ) {
425
+ _chunk_current =
426
+ _base64_encode(
427
+ _block,
428
+ _chunk_current,
429
+ _length,
430
+ _out,
431
+ _block_last,
432
+ _amount_only);
433
+ _msg_info(
434
+ `Current chunk: '${_chunk_current}'.`);
435
+ _block_current =
436
+ _block_current + 1;
437
+ }
438
+ else {
439
+ _msg =
440
+ "Unknown encoding format " +
441
+ `${_format}.`;
442
+ _msg_error(
443
+ _msg,
444
+ 1);
445
+ }
446
+ }
447
+ for ( _block of _blocks ) {
448
+ _rm_args = [
449
+ _block
450
+ ];
451
+ _rm(
452
+ _rm_args);
453
+ }
454
+ if ( _amount_only === "true" ) {
455
+ _chunks_total =
456
+ _chunk_current;
457
+ _msg =
458
+ `File '${_in}' is base64 encoded and ` +
459
+ `split in '${_chunks_total}' ` +
460
+ `files of length '${_length}'.`;
461
+ _msg_info(
462
+ _msg);
463
+ return _chunks_total;
464
+ }
465
+ else if ( _amount_only === "false" ) {
466
+ _msg =
467
+ `Finished '${_format}' encoding ` +
468
+ `input file '${_in}'.`;
469
+ _msg_info(
470
+ _msg);
197
471
  }
198
- _buffer_prepare(
199
- _in,
200
- _out,
201
- _buffer_size_bytes);
202
- // console.log(
203
- // _split
204
- // );
205
- // _msg =
206
- // `Creating directory '${_dir}':`;
207
- // _msg_info(
208
- // _msg);
209
- // _mkdir(
210
- // _dir);
211
- // _msg =
212
- // `Writing content '${_content}' to file '${_file}':`;
213
- // _msg_info(
214
- // _msg);
215
- // _file_write(
216
- // _file,
217
- // _content);
218
- // _msg =
219
- // `Content of directory '/':`;
220
- // _msg_info(
221
- // _msg);
222
- // _dir_content =
223
- // _ls(
224
- // "/").unwrap();
225
- // console.log(
226
- // _dir_content);
227
- // _msg =
228
- // `Content of directory '${_dir}':`;
229
- // _msg_info(
230
- // _msg);
231
- // _dir_content =
232
- // _ls(
233
- // _dir).unwrap();
234
- // console.log(
235
- // _dir_content);
236
472
  }
237
473
 
238
474
  module.exports = {