fix(core): update

This commit is contained in:
2024-06-03 21:35:08 +02:00
parent 91c04b2364
commit b9c384dd08
8 changed files with 175 additions and 59 deletions

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/smartbucket',
version: '3.0.9',
version: '3.0.10',
description: 'A TypeScript library offering simple and cloud-agnostic object storage with advanced features like bucket creation, file and directory management, and data streaming.'
}

View File

@ -107,10 +107,12 @@ export class Bucket {
/**
* get file
*/
public async fastGet(optionsArg: Parameters<typeof this.fastGetStream>[0]): Promise<Buffer> {
public async fastGet(optionsArg: {
path: string
}): Promise<Buffer> {
const done = plugins.smartpromise.defer();
let completeFile: Buffer;
const replaySubject = await this.fastGetStream(optionsArg);
const replaySubject = await this.fastGetReplaySubject(optionsArg);
const subscription = replaySubject.subscribe({
next: (chunk) => {
if (completeFile) {
@ -131,7 +133,13 @@ export class Bucket {
return completeFile;
}
public async fastGetStream(optionsArg: {
/**
* good when time to first byte is important
* and multiple subscribers are expected
* @param optionsArg
* @returns
*/
public async fastGetReplaySubject(optionsArg: {
path: string;
}): Promise<plugins.smartrx.rxjs.ReplaySubject<Buffer>> {
const fileStream = await this.smartbucketRef.minioClient
@ -161,12 +169,54 @@ export class Bucket {
return replaySubject;
}
public fastGetStream(optionsArg: {
path: string;
}, typeArg: 'webstream'): Promise<ReadableStream>
public async fastGetStream(optionsArg: {
path: string;
}, typeArg: 'nodestream'): Promise<plugins.stream.Readable>
/**
* fastGetStream
* @param optionsArg
* @returns
*/
public async fastGetStream(optionsArg: { path: string; }, typeArg: 'webstream' | 'nodestream' = 'nodestream'): Promise<ReadableStream | plugins.stream.Readable>{
const fileStream = await this.smartbucketRef.minioClient
.getObject(this.name, optionsArg.path)
.catch((e) => console.log(e));
const duplexStream = new plugins.smartstream.SmartDuplex<Buffer, Buffer>({
writeFunction: async (chunk) => {
return chunk;
},
finalFunction: async (cb) => {
return null;
}
});
if (!fileStream) {
return null;
}
const smartstream = new plugins.smartstream.StreamWrapper([
fileStream,
duplexStream,
]);
smartstream.run();
if (typeArg === 'nodestream') {
return duplexStream;
};
if (typeArg === 'webstream') {
return (await duplexStream.getWebStreams()).readable;
}
}
/**
* store file as stream
*/
public async fastPutStream(optionsArg: {
path: string;
dataStream: plugins.stream.Readable;
dataStream: plugins.stream.Readable | ReadableStream;
nativeMetadata?: { [key: string]: string };
overwrite?: boolean;
}): Promise<void> {
@ -182,12 +232,14 @@ export class Bucket {
} else {
console.log(`Creating new object at path '${optionsArg.path}' in bucket '${this.name}'.`);
}
const streamIntake = await plugins.smartstream.StreamIntake.fromStream<Uint8Array>(optionsArg.dataStream);
// Proceed with putting the object
await this.smartbucketRef.minioClient.putObject(
this.name,
optionsArg.path,
optionsArg.dataStream,
streamIntake,
null,
...(optionsArg.nativeMetadata
? (() => {
@ -313,6 +365,13 @@ export class Bucket {
}
}
/**
* deletes this bucket
*/
public async delete() {
await this.smartbucketRef.minioClient.removeBucket(this.name);
}
public async fastStat(pathDescriptor: interfaces.IPathDecriptor) {
let checkPath = await helpers.reducePathDescriptorToPath(pathDescriptor);
return this.smartbucketRef.minioClient.statObject(this.name, checkPath);

View File

@ -234,14 +234,30 @@ export class Directory {
return result;
}
public async fastGetStream(pathArg: string): Promise<plugins.smartrx.rxjs.ReplaySubject<Buffer>> {
const path = plugins.path.join(this.getBasePath(), pathArg);
public fastGetStream(optionsArg: {
path: string;
}, typeArg: 'webstream'): Promise<ReadableStream>
public async fastGetStream(optionsArg: {
path: string;
}, typeArg: 'nodestream'): Promise<plugins.stream.Readable>
/**
* fastGetStream
* @param optionsArg
* @returns
*/
public async fastGetStream(optionsArg: { path: string; }, typeArg: 'webstream' | 'nodestream'): Promise<ReadableStream | plugins.stream.Readable>{
const path = plugins.path.join(this.getBasePath(), optionsArg.path);
const result = await this.bucketRef.fastGetStream({
path,
});
path
}, typeArg as any);
return result;
}
/**
* removes a file within the directory
* @param optionsArg
*/
public async fastRemove(optionsArg: { path: string }) {
const path = plugins.path.join(this.getBasePath(), optionsArg.path);
await this.bucketRef.fastRemove({

View File

@ -4,7 +4,6 @@ import * as interfaces from './interfaces.js';
import { Directory } from './classes.directory.js';
import { MetaData } from './classes.metadata.js';
/**
* represents a file in a directory
*/
@ -33,7 +32,8 @@ export class File {
directoryRefArg: optionsArg.directory,
fileName: optionsArg.name,
});
if (contents instanceof plugins.stream.Readable) {} else {
if (contents instanceof plugins.stream.Readable) {
} else {
await optionsArg.directory.fastPut({
path: optionsArg.name,
contents: contents,
@ -48,7 +48,7 @@ export class File {
public getBasePath(): string {
return plugins.path.join(this.parentDirectoryRef.getBasePath(), this.name);
};
}
constructor(optionsArg: { directoryRefArg: Directory; fileName: string }) {
this.parentDirectoryRef = optionsArg.directoryRefArg;
@ -67,35 +67,61 @@ export class File {
return resultBuffer;
}
public async getReadStream() {
const readStream = this.parentDirectoryRef.bucketRef.fastGetStream({
path: this.getBasePath(),
});
public async getReadStream(typeArg: 'webstream'): Promise<ReadableStream>;
public async getReadStream(typeArg: 'nodestream'): Promise<plugins.stream.Readable>;
public async getReadStream(
typeArg: 'nodestream' | 'webstream'
): Promise<ReadableStream | plugins.stream.Readable> {
const readStream = this.parentDirectoryRef.bucketRef.fastGetStream(
{
path: this.getBasePath(),
},
typeArg as any
);
return readStream;
}
/**
* removes this file
* for using recycling mechanics use .delete()
* deletes this file
*/
public async remove() {
await this.parentDirectoryRef.bucketRef.fastRemove({
path: this.getBasePath(),
});
if (!this.name.endsWith('.metadata')) {
public async delete(optionsArg?: {
mode: 'trash' | 'permanent';
}) {
optionsArg = {
... {
mode: 'permanent',
},
...optionsArg,
}
if (optionsArg.mode === 'permanent') {
await this.parentDirectoryRef.bucketRef.fastRemove({
path: this.getBasePath() + '.metadata',
path: this.getBasePath(),
});
if (!this.name.endsWith('.metadata')) {
const metadata = await this.getMetaData();
await metadata.metadataFile.delete(optionsArg);
}
} else if (optionsArg.mode === 'trash') {
const metadata = await this.getMetaData();
await metadata.storeCustomMetaData({
key: 'recycle',
value: {
deletedAt: Date.now(),
originalPath: this.getBasePath(),
},
});
const trashName = plugins.smartunique.uuid4();
await this.move({
directory: await this.parentDirectoryRef.bucketRef.getBaseDirectory(),
path: plugins.path.join('trash', trashName),
});
}
await this.parentDirectoryRef.listFiles();
}
/**
* deletes the file with recycling mechanics
*/
public async delete() {
await this.remove();
}
/**
* allows locking the file
* @param optionsArg
@ -125,10 +151,13 @@ export class File {
}
public async updateWithContents(optionsArg: {
contents: Buffer | string | plugins.stream.Readable;
contents: Buffer | string | plugins.stream.Readable | ReadableStream;
encoding?: 'utf8' | 'binary';
}) {
if (optionsArg.contents instanceof plugins.stream.Readable) {
if (
optionsArg.contents instanceof plugins.stream.Readable ||
optionsArg.contents instanceof ReadableStream
) {
await this.parentDirectoryRef.bucketRef.fastPutStream({
path: this.getBasePath(),
dataStream: optionsArg.contents,

View File

@ -10,8 +10,9 @@ import * as smartpath from '@push.rocks/smartpath';
import * as smartpromise from '@push.rocks/smartpromise';
import * as smartrx from '@push.rocks/smartrx';
import * as smartstream from '@push.rocks/smartstream';
import * as smartunique from '@push.rocks/smartunique';
export { smartmime, smartpath, smartpromise, smartrx, smartstream };
export { smartmime, smartpath, smartpromise, smartrx, smartstream, smartunique };
// @tsclass
import * as tsclass from '@tsclass/tsclass';