@push.rocks/smartarchive 4.1.0 → 4.2.0

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.
@@ -5,7 +5,10 @@ export interface IAnalyzedResult {
5
5
  fileType: plugins.fileType.FileTypeResult;
6
6
  isArchive: boolean;
7
7
  resultStream: plugins.smartstream.SmartDuplex;
8
- decompressionStream: plugins.stream.Transform | plugins.stream.Duplex | plugins.tarStream.Extract;
8
+ decompressionStream:
9
+ | plugins.stream.Transform
10
+ | plugins.stream.Duplex
11
+ | plugins.tarStream.Extract;
9
12
  }
10
13
 
11
14
  export class ArchiveAnalyzer {
@@ -25,14 +28,15 @@ export class ArchiveAnalyzer {
25
28
  'application/x-bzip2',
26
29
  // Add other archive mime types here
27
30
  ]);
28
-
31
+
29
32
  return archiveMimeTypes.has(mimeType);
30
33
  }
31
34
 
32
-
33
35
  private async getDecompressionStream(
34
- mimeTypeArg: plugins.fileType.FileTypeResult['mime']
35
- ): Promise<plugins.stream.Transform | plugins.stream.Duplex | plugins.tarStream.Extract> {
36
+ mimeTypeArg: plugins.fileType.FileTypeResult['mime'],
37
+ ): Promise<
38
+ plugins.stream.Transform | plugins.stream.Duplex | plugins.tarStream.Extract
39
+ > {
36
40
  switch (mimeTypeArg) {
37
41
  case 'application/gzip':
38
42
  return this.smartArchiveRef.gzipTools.getDecompressionStream();
@@ -51,13 +55,18 @@ export class ArchiveAnalyzer {
51
55
  public getAnalyzedStream() {
52
56
  let firstRun = true;
53
57
  const resultStream = plugins.smartstream.createPassThrough();
54
- const analyzerstream = new plugins.smartstream.SmartDuplex<Buffer, IAnalyzedResult>({
58
+ const analyzerstream = new plugins.smartstream.SmartDuplex<
59
+ Buffer,
60
+ IAnalyzedResult
61
+ >({
55
62
  readableObjectMode: true,
56
63
  writeFunction: async (chunkArg: Buffer, streamtools) => {
57
64
  if (firstRun) {
58
65
  firstRun = false;
59
66
  const fileType = await plugins.fileType.fileTypeFromBuffer(chunkArg);
60
- const decompressionStream = await this.getDecompressionStream(fileType?.mime as any);
67
+ const decompressionStream = await this.getDecompressionStream(
68
+ fileType?.mime as any,
69
+ );
61
70
  /**
62
71
  * analyzed stream emits once with this object
63
72
  */
@@ -75,7 +84,7 @@ export class ArchiveAnalyzer {
75
84
  finalFunction: async (tools) => {
76
85
  resultStream.push(null);
77
86
  return null;
78
- }
87
+ },
79
88
  });
80
89
  return analyzerstream;
81
90
  }
@@ -13,4 +13,4 @@ export class Bzip2Tools {
13
13
  getDecompressionStream() {
14
14
  return unbzip2Stream();
15
15
  }
16
- }
16
+ }
@@ -1,5 +1,5 @@
1
1
  import type { SmartArchive } from './classes.smartarchive.js';
2
- import * as plugins from './plugins.js'
2
+ import * as plugins from './plugins.js';
3
3
 
4
4
  // This class wraps fflate's gunzip in a Node.js Transform stream
5
5
  export class CompressGunzipTransform extends plugins.stream.Transform {
@@ -7,7 +7,11 @@ export class CompressGunzipTransform extends plugins.stream.Transform {
7
7
  super();
8
8
  }
9
9
 
10
- _transform(chunk: Buffer, encoding: BufferEncoding, callback: plugins.stream.TransformCallback) {
10
+ _transform(
11
+ chunk: Buffer,
12
+ encoding: BufferEncoding,
13
+ callback: plugins.stream.TransformCallback,
14
+ ) {
11
15
  plugins.fflate.gunzip(chunk, (err, decompressed) => {
12
16
  if (err) {
13
17
  callback(err);
@@ -26,7 +30,11 @@ export class DecompressGunzipTransform extends plugins.stream.Transform {
26
30
  super();
27
31
  }
28
32
 
29
- _transform(chunk: Buffer, encoding: BufferEncoding, callback: plugins.stream.TransformCallback) {
33
+ _transform(
34
+ chunk: Buffer,
35
+ encoding: BufferEncoding,
36
+ callback: plugins.stream.TransformCallback,
37
+ ) {
30
38
  // Use fflate's gunzip function to decompress the chunk
31
39
  plugins.fflate.gunzip(chunk, (err, decompressed) => {
32
40
  if (err) {
@@ -41,10 +49,8 @@ export class DecompressGunzipTransform extends plugins.stream.Transform {
41
49
  }
42
50
  }
43
51
 
44
-
45
52
  export class GzipTools {
46
- constructor() {
47
- }
53
+ constructor() {}
48
54
 
49
55
  public getCompressionStream() {
50
56
  return new CompressGunzipTransform();
@@ -6,7 +6,10 @@ import { GzipTools } from './classes.gziptools.js';
6
6
  import { TarTools } from './classes.tartools.js';
7
7
  import { ZipTools } from './classes.ziptools.js';
8
8
 
9
- import { ArchiveAnalyzer, type IAnalyzedResult } from './classes.archiveanalyzer.js';
9
+ import {
10
+ ArchiveAnalyzer,
11
+ type IAnalyzedResult,
12
+ } from './classes.archiveanalyzer.js';
10
13
 
11
14
  import type { from } from '@push.rocks/smartrx/dist_ts/smartrx.plugins.rxjs.js';
12
15
 
@@ -18,14 +21,19 @@ export class SmartArchive {
18
21
  return smartArchiveInstance;
19
22
  }
20
23
 
21
- public static async fromArchiveFile(filePathArg: string): Promise<SmartArchive> {
24
+ public static async fromArchiveFile(
25
+ filePathArg: string,
26
+ ): Promise<SmartArchive> {
22
27
  const smartArchiveInstance = new SmartArchive();
23
28
  smartArchiveInstance.sourceFilePath = filePathArg;
24
29
  return smartArchiveInstance;
25
30
  }
26
31
 
27
32
  public static async fromArchiveStream(
28
- streamArg: plugins.stream.Readable | plugins.stream.Duplex | plugins.stream.Transform
33
+ streamArg:
34
+ | plugins.stream.Readable
35
+ | plugins.stream.Duplex
36
+ | plugins.stream.Transform,
29
37
  ): Promise<SmartArchive> {
30
38
  const smartArchiveInstance = new SmartArchive();
31
39
  smartArchiveInstance.sourceStream = streamArg;
@@ -41,13 +49,19 @@ export class SmartArchive {
41
49
 
42
50
  public sourceUrl: string;
43
51
  public sourceFilePath: string;
44
- public sourceStream: plugins.stream.Readable | plugins.stream.Duplex | plugins.stream.Transform;
52
+ public sourceStream:
53
+ | plugins.stream.Readable
54
+ | plugins.stream.Duplex
55
+ | plugins.stream.Transform;
45
56
 
46
57
  public archiveName: string;
47
58
  public singleFileMode: boolean = false;
48
59
 
49
60
  public addedDirectories: string[] = [];
50
- public addedFiles: (plugins.smartfile.SmartFile | plugins.smartfile.StreamFile)[] = [];
61
+ public addedFiles: (
62
+ | plugins.smartfile.SmartFile
63
+ | plugins.smartfile.StreamFile
64
+ )[] = [];
51
65
  public addedUrls: string[] = [];
52
66
 
53
67
  constructor() {}
@@ -81,24 +95,35 @@ export class SmartArchive {
81
95
  // return archiveStream;
82
96
  }
83
97
 
84
- public async exportToFs(targetDir: string, fileNameArg?: string): Promise<void> {
98
+ public async exportToFs(
99
+ targetDir: string,
100
+ fileNameArg?: string,
101
+ ): Promise<void> {
85
102
  const done = plugins.smartpromise.defer<void>();
86
103
  const streamFileStream = await this.exportToStreamOfStreamFiles();
87
104
  streamFileStream.pipe(
88
105
  new plugins.smartstream.SmartDuplex({
89
106
  objectMode: true,
90
- writeFunction: async (streamFileArg: plugins.smartfile.StreamFile, streamtools) => {
107
+ writeFunction: async (
108
+ streamFileArg: plugins.smartfile.StreamFile,
109
+ streamtools,
110
+ ) => {
91
111
  const done = plugins.smartpromise.defer<void>();
92
- console.log(streamFileArg.relativeFilePath ? streamFileArg.relativeFilePath : 'no relative path');
112
+ console.log(
113
+ streamFileArg.relativeFilePath
114
+ ? streamFileArg.relativeFilePath
115
+ : 'no relative path',
116
+ );
93
117
  const streamFile = streamFileArg;
94
118
  const readStream = await streamFile.createReadStream();
95
119
  await plugins.smartfile.fs.ensureDir(targetDir);
96
120
  const writePath = plugins.path.join(
97
121
  targetDir,
98
- streamFile.relativeFilePath || fileNameArg
122
+ streamFile.relativeFilePath || fileNameArg,
99
123
  );
100
124
  await plugins.smartfile.fs.ensureDir(plugins.path.dirname(writePath));
101
- const writeStream = plugins.smartfile.fsStream.createWriteStream(writePath);
125
+ const writeStream =
126
+ plugins.smartfile.fsStream.createWriteStream(writePath);
102
127
  readStream.pipe(writeStream);
103
128
  writeStream.on('finish', () => {
104
129
  done.resolve();
@@ -108,15 +133,16 @@ export class SmartArchive {
108
133
  finalFunction: async () => {
109
134
  done.resolve();
110
135
  },
111
- })
136
+ }),
112
137
  );
113
138
  return done.promise;
114
139
  }
115
140
 
116
141
  public async exportToStreamOfStreamFiles() {
117
- const streamFileIntake = new plugins.smartstream.StreamIntake<plugins.smartfile.StreamFile>({
118
- objectMode: true,
119
- });
142
+ const streamFileIntake =
143
+ new plugins.smartstream.StreamIntake<plugins.smartfile.StreamFile>({
144
+ objectMode: true,
145
+ });
120
146
  const archiveStream = await this.getArchiveStream();
121
147
  const createAnalyzedStream = () => this.archiveAnalyzer.getAnalyzedStream();
122
148
 
@@ -125,15 +151,21 @@ export class SmartArchive {
125
151
  plugins.smartstream.createTransformFunction<IAnalyzedResult, any>(
126
152
  async (analyzedResultChunk) => {
127
153
  if (analyzedResultChunk.fileType?.mime === 'application/x-tar') {
128
- const tarStream = analyzedResultChunk.decompressionStream as plugins.tarStream.Extract;
154
+ const tarStream =
155
+ analyzedResultChunk.decompressionStream as plugins.tarStream.Extract;
129
156
  tarStream.on('entry', async (header, stream, next) => {
130
157
  if (header.type === 'directory') {
131
- console.log(`tar stream directory: ${header.name} ... skipping!`);
158
+ console.log(
159
+ `tar stream directory: ${header.name} ... skipping!`,
160
+ );
132
161
  next();
133
162
  return;
134
163
  }
135
164
  console.log(`tar stream file: ${header.name}`);
136
- const streamfile = plugins.smartfile.StreamFile.fromStream(stream, header.name);
165
+ const streamfile = plugins.smartfile.StreamFile.fromStream(
166
+ stream,
167
+ header.name,
168
+ );
137
169
  streamFileIntake.push(streamfile);
138
170
  stream.on('end', function () {
139
171
  next(); // ready for next entry
@@ -143,20 +175,30 @@ export class SmartArchive {
143
175
  console.log('finished');
144
176
  streamFileIntake.signalEnd();
145
177
  });
146
- analyzedResultChunk.resultStream.pipe(analyzedResultChunk.decompressionStream);
178
+ analyzedResultChunk.resultStream.pipe(
179
+ analyzedResultChunk.decompressionStream,
180
+ );
147
181
  } else if (analyzedResultChunk.fileType?.mime === 'application/zip') {
148
182
  analyzedResultChunk.resultStream
149
183
  .pipe(analyzedResultChunk.decompressionStream)
150
- .pipe(new plugins.smartstream.SmartDuplex({
151
- objectMode: true,
152
- writeFunction: async (streamFileArg: plugins.smartfile.StreamFile, streamtools) => {
153
- streamFileIntake.push(streamFileArg);
154
- },
155
- finalFunction: async () => {
156
- streamFileIntake.signalEnd();
157
- }
158
- }));
159
- } else if (analyzedResultChunk.isArchive && analyzedResultChunk.decompressionStream) {
184
+ .pipe(
185
+ new plugins.smartstream.SmartDuplex({
186
+ objectMode: true,
187
+ writeFunction: async (
188
+ streamFileArg: plugins.smartfile.StreamFile,
189
+ streamtools,
190
+ ) => {
191
+ streamFileIntake.push(streamFileArg);
192
+ },
193
+ finalFunction: async () => {
194
+ streamFileIntake.signalEnd();
195
+ },
196
+ }),
197
+ );
198
+ } else if (
199
+ analyzedResultChunk.isArchive &&
200
+ analyzedResultChunk.decompressionStream
201
+ ) {
160
202
  analyzedResultChunk.resultStream
161
203
  .pipe(analyzedResultChunk.decompressionStream)
162
204
  .pipe(createAnalyzedStream())
@@ -164,7 +206,7 @@ export class SmartArchive {
164
206
  } else {
165
207
  const streamFile = plugins.smartfile.StreamFile.fromStream(
166
208
  analyzedResultChunk.resultStream,
167
- analyzedResultChunk.fileType?.ext
209
+ analyzedResultChunk.fileType?.ext,
168
210
  );
169
211
  streamFileIntake.push(streamFile);
170
212
  streamFileIntake.signalEnd();
@@ -172,7 +214,7 @@ export class SmartArchive {
172
214
  },
173
215
  {
174
216
  objectMode: true,
175
- }
217
+ },
176
218
  );
177
219
 
178
220
  archiveStream.pipe(createAnalyzedStream()).pipe(createUnpackStream());
@@ -18,7 +18,7 @@ export class TarTools {
18
18
  | plugins.smartfile.StreamFile;
19
19
  byteLength?: number;
20
20
  filePath?: string;
21
- }
21
+ },
22
22
  ): Promise<void> {
23
23
  return new Promise<void>(async (resolve, reject) => {
24
24
  let fileName: string | null = null;
@@ -28,7 +28,8 @@ export class TarTools {
28
28
  } else if (optionsArg.content instanceof plugins.smartfile.SmartFile) {
29
29
  fileName = (optionsArg.content as plugins.smartfile.SmartFile).relative;
30
30
  } else if (optionsArg.content instanceof plugins.smartfile.StreamFile) {
31
- fileName = (optionsArg.content as plugins.smartfile.StreamFile).relativeFilePath;
31
+ fileName = (optionsArg.content as plugins.smartfile.StreamFile)
32
+ .relativeFilePath;
32
33
  } else if (optionsArg.filePath) {
33
34
  fileName = optionsArg.filePath;
34
35
  }
@@ -47,9 +48,11 @@ export class TarTools {
47
48
  contentByteLength = await optionsArg.content.getSize(); // assuming SmartFile has getSize method
48
49
  } else if (optionsArg.content instanceof plugins.smartfile.StreamFile) {
49
50
  contentByteLength = await optionsArg.content.getSize(); // assuming StreamFile has getSize method
50
- } else if (optionsArg.content instanceof plugins.smartstream.stream.Readable) {
51
+ } else if (
52
+ optionsArg.content instanceof plugins.smartstream.stream.Readable
53
+ ) {
51
54
  console.warn(
52
- '@push.rocks/smartarchive: When streaming, it is recommended to provide byteLength, if known.'
55
+ '@push.rocks/smartarchive: When streaming, it is recommended to provide byteLength, if known.',
53
56
  );
54
57
  } else if (optionsArg.filePath) {
55
58
  const fileStat = await plugins.smartfile.fs.stat(optionsArg.filePath);
@@ -63,12 +66,18 @@ export class TarTools {
63
66
  if (Buffer.isBuffer(optionsArg.content)) {
64
67
  content = plugins.smartstream.stream.Readable.from(optionsArg.content);
65
68
  } else if (typeof optionsArg.content === 'string') {
66
- content = plugins.smartstream.stream.Readable.from(Buffer.from(optionsArg.content));
69
+ content = plugins.smartstream.stream.Readable.from(
70
+ Buffer.from(optionsArg.content),
71
+ );
67
72
  } else if (optionsArg.content instanceof plugins.smartfile.SmartFile) {
68
- content = plugins.smartstream.stream.Readable.from(optionsArg.content.contents);
73
+ content = plugins.smartstream.stream.Readable.from(
74
+ optionsArg.content.contents,
75
+ );
69
76
  } else if (optionsArg.content instanceof plugins.smartfile.StreamFile) {
70
77
  content = await optionsArg.content.createReadStream();
71
- } else if (optionsArg.content instanceof plugins.smartstream.stream.Readable) {
78
+ } else if (
79
+ optionsArg.content instanceof plugins.smartstream.stream.Readable
80
+ ) {
72
81
  content = optionsArg.content;
73
82
  }
74
83
 
@@ -87,7 +96,7 @@ export class TarTools {
87
96
  } else {
88
97
  resolve();
89
98
  }
90
- }
99
+ },
91
100
  );
92
101
 
93
102
  content.pipe(entry);
@@ -100,7 +109,10 @@ export class TarTools {
100
109
  * @param directoryPath
101
110
  */
102
111
  public async packDirectory(directoryPath: string) {
103
- const fileTree = await plugins.smartfile.fs.listFileTree(directoryPath, '**/*');
112
+ const fileTree = await plugins.smartfile.fs.listFileTree(
113
+ directoryPath,
114
+ '**/*',
115
+ );
104
116
  const pack = await this.getPackStream();
105
117
  for (const filePath of fileTree) {
106
118
  const absolutePath = plugins.path.join(directoryPath, filePath);
@@ -1,33 +1,39 @@
1
1
  import type { SmartArchive } from './classes.smartarchive.js';
2
2
  import * as plugins from './plugins.js';
3
3
 
4
- class DecompressZipTransform extends plugins.smartstream.SmartDuplex<ArrayBufferLike> {
4
+ class DecompressZipTransform extends plugins.smartstream
5
+ .SmartDuplex<ArrayBufferLike> {
5
6
  private streamtools: plugins.smartstream.IStreamTools;
6
7
  private unzipper = new plugins.fflate.Unzip(async (fileArg) => {
7
8
  let resultBuffer: Buffer;
8
9
  fileArg.ondata = async (flateError, dat, final) => {
9
- resultBuffer? resultBuffer = Buffer.concat([resultBuffer, Buffer.from(dat)])
10
- : resultBuffer = Buffer.from(dat);
10
+ resultBuffer
11
+ ? (resultBuffer = Buffer.concat([resultBuffer, Buffer.from(dat)]))
12
+ : (resultBuffer = Buffer.from(dat));
11
13
  if (final) {
12
- const streamFile = plugins.smartfile.StreamFile.fromBuffer(resultBuffer);
14
+ const streamFile =
15
+ plugins.smartfile.StreamFile.fromBuffer(resultBuffer);
13
16
  streamFile.relativeFilePath = fileArg.name;
14
17
  this.streamtools.push(streamFile);
15
18
  }
16
- }
19
+ };
17
20
  fileArg.start();
18
21
  });
19
22
  constructor() {
20
23
  super({
21
24
  objectMode: true,
22
25
  writeFunction: async (chunkArg, streamtoolsArg) => {
23
- this.streamtools? null : this.streamtools = streamtoolsArg;
24
- this.unzipper.push(Buffer.isBuffer(chunkArg) ? chunkArg : Buffer.from(chunkArg), false);
26
+ this.streamtools ? null : (this.streamtools = streamtoolsArg);
27
+ this.unzipper.push(
28
+ Buffer.isBuffer(chunkArg) ? chunkArg : Buffer.from(chunkArg),
29
+ false,
30
+ );
25
31
  },
26
32
  finalFunction: async () => {
27
33
  this.unzipper.push(Buffer.from(''), true);
28
34
  await plugins.smartdelay.delayFor(0);
29
35
  await this.streamtools.push(null);
30
- }
36
+ },
31
37
  });
32
38
  this.unzipper.register(plugins.fflate.UnzipInflate);
33
39
  }
@@ -42,7 +48,11 @@ export class CompressZipTransform extends plugins.stream.Transform {
42
48
  this.files = {};
43
49
  }
44
50
 
45
- _transform(chunk: Buffer, encoding: BufferEncoding, callback: plugins.stream.TransformCallback) {
51
+ _transform(
52
+ chunk: Buffer,
53
+ encoding: BufferEncoding,
54
+ callback: plugins.stream.TransformCallback,
55
+ ) {
46
56
  // Simple example: storing chunks in memory before finalizing ZIP in _flush
47
57
  this.files['file.txt'] = new Uint8Array(chunk);
48
58
  callback();
@@ -61,8 +71,7 @@ export class CompressZipTransform extends plugins.stream.Transform {
61
71
  }
62
72
 
63
73
  export class ZipTools {
64
- constructor() {
65
- }
74
+ constructor() {}
66
75
 
67
76
  public getCompressionStream() {
68
77
  return new CompressZipTransform();
package/ts/paths.ts CHANGED
@@ -2,6 +2,6 @@ import * as plugins from './plugins.js';
2
2
 
3
3
  export const packageDir = plugins.path.join(
4
4
  plugins.smartpath.get.dirnameFromImportMetaUrl(import.meta.url),
5
- '../'
5
+ '../',
6
6
  );
7
7
  export const nogitDir = plugins.path.join(packageDir, './.nogit');
package/ts/plugins.ts CHANGED
@@ -15,7 +15,17 @@ import * as smartstream from '@push.rocks/smartstream';
15
15
  import * as smartrx from '@push.rocks/smartrx';
16
16
  import * as smarturl from '@push.rocks/smarturl';
17
17
 
18
- export { smartfile, smartdelay, smartpath, smartpromise, smartrequest, smartunique, smartstream, smartrx, smarturl };
18
+ export {
19
+ smartfile,
20
+ smartdelay,
21
+ smartpath,
22
+ smartpromise,
23
+ smartrequest,
24
+ smartunique,
25
+ smartstream,
26
+ smartrx,
27
+ smarturl,
28
+ };
19
29
 
20
30
  // third party scope
21
31
  import * as fileType from 'file-type';