Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@internxt/inxt-js",
"author": "Internxt <hello@internxt.com>",
"version": "2.3.1",
"version": "3.0.0",
"description": "",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down Expand Up @@ -45,7 +45,7 @@
},
"dependencies": {
"@internxt/lib": "1.4.1",
"@internxt/sdk": "1.15.1",
"@internxt/sdk": "1.15.2",
"async": "3.2.6",
"axios": "1.13.5",
"bip39": "3.1.0",
Expand Down
89 changes: 8 additions & 81 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { Bridge, CreateFileTokenResponse, GetDownloadLinksResponse } from './ser
import { HashStream } from './lib/utils/streams';
import { downloadFileV2 } from './lib/core/download/downloadV2';
import { FileVersionOneError } from '@internxt/sdk/dist/network/download';
import { uploadFileMultipart, uploadFileV2 } from './lib/core/upload/uploadV2';
import { upload as uploadFileV2 } from './lib/core/upload/uploadV2';

type GetBucketsCallback = (err: Error | null, result: any) => void;

Expand Down Expand Up @@ -150,82 +150,20 @@ export class Environment {
this.config.encryptionKey = newEncryptionKey;
}

uploadMultipartFile(bucketId: string, opts: UploadOptions): ActionState {
const uploadState = new ActionState(ActionTypes.Upload);

upload: UploadStrategyFunction = async (bucketId: string, opts: UploadOptions) => {
if (!this.config.encryptionKey) {
opts.finishedCallback(Error('Mnemonic was not provided, please, provide a mnemonic'), null);

return uploadState;
throw Error('Mnemonic was not provided, please, provide a mnemonic');
}

if (!this.config.bridgeUrl) {
opts.finishedCallback(Error('Missing param "bridgeUrl"'), null);

return uploadState;
throw Error('Missing param "bridgeUrl"');
}

if (!bucketId) {
opts.finishedCallback(Error('Bucket id was not provided'), null);

return uploadState;
throw Error('Bucket id was not provided');
}

// if (!opts.parts || isNaN(opts.parts) || opts.parts < 2) {
// opts.finishedCallback(Error('Invalid "parts" parameter. Expected number > 1'), null);

// return uploadState;
// }

uploadFileMultipart(
opts.fileSize,
opts.source,
bucketId,
this.config.encryptionKey,
this.config.bridgeUrl,
{
user: this.config.bridgeUser,
pass: this.config.bridgePass,
},
opts.progressCallback,
uploadState,
this.config.appDetails,
)
.then((fileId) => {
opts.finishedCallback(null, fileId);
})
.catch((err) => {
opts.finishedCallback(
err.message === 'The operation was aborted' ? new Error('Process killed by user') : err,
null,
);
});

return uploadState;
}

upload: UploadStrategyFunction = (bucketId: string, opts: UploadOptions) => {
const uploadState = new ActionState(ActionTypes.Upload);

if (!this.config.encryptionKey) {
opts.finishedCallback(Error('Mnemonic was not provided, please, provide a mnemonic'), null);

return uploadState;
}

if (!this.config.bridgeUrl) {
opts.finishedCallback(Error('Missing param "bridgeUrl"'), null);

return uploadState;
}

if (!bucketId) {
opts.finishedCallback(Error('Bucket id was not provided'), null);

return uploadState;
}

uploadFileV2(
return await uploadFileV2(
opts.fileSize,
opts.source,
bucketId,
Expand All @@ -237,19 +175,8 @@ export class Environment {
},
this.config.appDetails,
opts.progressCallback,
uploadState,
)
.then((fileId) => {
opts.finishedCallback(null, fileId);
})
.catch((err) => {
opts.finishedCallback(
err.message === 'The operation was aborted' ? new Error('Process killed by user') : err,
null,
);
});

return uploadState;
opts.abortSignal,
);
};

download: DownloadStrategyFunction<any> = (
Expand Down
3 changes: 1 addition & 2 deletions src/lib/core/upload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ export type UploadProgressCallback = (
totalBytes: number | null,
) => void;
export type EncryptProgressCallback = (progress: number) => void;
export type UploadFinishCallback = (err: Error | null, response: string | null) => void;

export interface UploadOptions {
progressCallback: UploadProgressCallback;
finishedCallback: UploadFinishCallback;
encryptProgressCallback?: EncryptProgressCallback;
fileSize: number;
source: Readable;
abortSignal?: AbortSignal;
}

type FileId = string;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/core/upload/multipart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ interface PartUpload {
source: { size: number; stream: Buffer; index: number };
}

export async function uploadParts(partUrls: string[], stream: Readable, signal: AbortSignal): Promise<Part[]> {
export async function uploadParts(partUrls: string[], stream: Readable, signal?: AbortSignal): Promise<Part[]> {
const parts: Part[] = [];
const concurrency = 10;

Expand Down
4 changes: 2 additions & 2 deletions src/lib/core/upload/strategy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EventEmitter } from 'events';

import { UploadOneStreamStrategyObject, UploadOneShardStrategyObject, UploadOptions } from '.';
import { Abortable, ActionState, ContractMeta } from '../../../api';
import { Abortable, ContractMeta } from '../../../api';
import { ShardMeta } from '../../models';

export type NegotiateContract = (shardMeta: ShardMeta) => Promise<ContractMeta>;
Expand All @@ -14,7 +14,7 @@ export type UploadStrategyLabel = string;

export type UploadStrategyObject = UploadOneStreamStrategyObject | UploadOneShardStrategyObject;

export type UploadStrategyFunction = (bucketId: string, opts: UploadOptions) => ActionState;
export type UploadStrategyFunction = (bucketId: string, opts: UploadOptions) => Promise<string>;

export abstract class UploadStrategy extends EventEmitter implements Abortable {
fileEncryptionKey: Buffer<ArrayBufferLike> = Buffer.alloc(0);
Expand Down
Loading