fix(core): update

This commit is contained in:
2022-06-07 17:11:13 +02:00
parent 277a3fff41
commit 9e14d881ba
9 changed files with 999 additions and 109 deletions

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@pushrocks/smartarchive',
version: '3.0.4',
version: '3.0.5',
description: 'work with archives'
}

View File

@ -1,6 +1,5 @@
import * as plugins from './smartarchive.plugins.js';
import * as paths from './smartarchive.paths.js';
import { extract } from 'tar';
export class SmartArchive {
public archiveDirectory: string;
@ -23,28 +22,54 @@ export class SmartArchive {
/**
* extracts an archive from a given filePath on disk
* @param filePathArg
* @param targetDir
* @param targetDirArg
*/
public async extractArchiveFromFilePathToFs(filePathArg: string, targetDir: string) {
const parsedPath = plugins.path.parse(filePathArg);
switch (parsedPath.ext) {
case '.tgz':
console.log(`detected a .tgz archive`);
await plugins.tar.extract({
file: filePathArg,
cwd: targetDir,
});
break;
}
public async extractArchiveFromFilePathToFs(filePathArg: string, targetDirArg: string) {
console.log(`extracting ${filePathArg}`);
const done = plugins.smartpromise.defer();
filePathArg = plugins.smartpath.transform.makeAbsolute(filePathArg);
targetDirArg = plugins.smartpath.transform.makeAbsolute(targetDirArg);
const readableStream = plugins.smartfile.fsStream.createReadStream(filePathArg);
const extractPipeStop = plugins.tarStream.extract();
extractPipeStop.on('entry', async (header, stream, next) => {
const targetFilePath = plugins.path.join(targetDirArg, header.name);
const parsedPath = plugins.path.parse(targetFilePath);
await plugins.smartfile.fs.ensureDir(parsedPath.dir);
const writeStream = plugins.smartfile.fsStream.createWriteStream(targetFilePath);
stream.pipe(writeStream);
stream.on('end', () => {
console.log(`extracted ${header.name}`);
next();
})
stream.resume();
});
extractPipeStop.on('finish', () => {
console.log(`Sucessfully extracted ${filePathArg}!`);
done.resolve();
});
// lets run the stream
readableStream
.pipe(plugins.gunzipMaybe())
.pipe(extractPipeStop);
await done.promise;
}
/**
* extracts to Observable
* where the Observable is emitting smartfiles
*/
public async extractArchiveFromBufferToObservable(
bufferArg: Buffer
): Promise<plugins.smartrx.rxjs.ReplaySubject<plugins.smartfile.Smartfile>> {
const intake = new plugins.streamfunction.Intake();
const { intake, replaySubject } = this.extractArchiveWithIntakeAndReplaySubject();
intake.pushData(bufferArg);
intake.signalEnd();
return replaySubject;
}
extractArchiveWithIntakeAndReplaySubject() {
const intake = new plugins.smartstream.StreamIntake<Buffer>();
const replaySubject = new plugins.smartrx.rxjs.ReplaySubject<plugins.smartfile.Smartfile>();
const readableStream = intake.getReadableStream();
const extractPipeStop = plugins.tarStream.extract();
@ -76,9 +101,10 @@ export class SmartArchive {
readableStream
.pipe(plugins.gunzipMaybe())
.pipe(extractPipeStop);
intake.pushData(bufferArg);
intake.signalEnd();
return replaySubject;
return {
intake,
replaySubject
};
}
/**
@ -93,10 +119,12 @@ export class SmartArchive {
}
// TODO
public async extractArchiveFromUrlToStream() {}
public async extractArchiveFromUrlToStream() {
}
// TODO
public async extractArchiveFromFsToStream() {}
public async extractArchiveFromFilePathToStream() {}
// TODO
public async extractArchiveFromStreamToStream() {}
@ -105,8 +133,8 @@ export class SmartArchive {
public async packFromStreamToStream() {}
// TODO
public async packFromFsToStream() {}
public async packFromDirPathToStream() {}
// TODO
public async packFromFsToFs() {}
public async packFromDirPathToFs() {}
}

View File

@ -6,12 +6,13 @@ export { path };
// @pushrocks scope
import * as smartfile from '@pushrocks/smartfile';
import * as smartpath from '@pushrocks/smartpath';
import * as smartpromise from '@pushrocks/smartpromise';
import * as smartrequest from '@pushrocks/smartrequest';
import * as smartunique from '@pushrocks/smartunique';
import * as streamfunction from '@pushrocks/streamfunction';
import * as smartstream from '@pushrocks/smartstream';
import * as smartrx from '@pushrocks/smartrx';
export { smartfile, smartpath, smartrequest, smartunique, streamfunction, smartrx };
export { smartfile, smartpath, smartpromise, smartrequest, smartunique, smartstream, smartrx };
// third party scope
import gunzipMaybe from 'gunzip-maybe';