diff --git a/package.json b/package.json index f1bfe21..b988ea1 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@internxt/inxt-js", "author": "Internxt ", - "version": "3.2.0", + "version": "3.2.1", "description": "", "main": "build/index.js", "types": "build/index.d.ts", diff --git a/src/index.ts b/src/index.ts index 20297d2..8f7e66e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,9 +28,6 @@ type GetBucketsCallback = (err: Error | null, result: any) => void; type GetBucketIdCallback = (err: Error | null, result: any) => void; -// eslint-disable-next-line @typescript-eslint/no-unused-vars -type DeleteBucketCallback = (err: Error | null, result: any) => void; - type ListFilesCallback = (err: Error | null, result: any) => void; type DeleteFileCallback = (err: Error | null, result: any) => void; @@ -146,10 +143,6 @@ export class Environment { cb(Error('Not implemented yet'), null); } - setEncryptionKey(newEncryptionKey: string): void { - this.config.encryptionKey = newEncryptionKey; - } - upload: UploadStrategyFunction = async (bucketId: string, opts: UploadOptions) => { if (!this.config.encryptionKey) { throw Error('Mnemonic was not provided, please, provide a mnemonic'); @@ -267,10 +260,6 @@ export class Environment { state.stop(); } - uploadCancel(state: ActionState): void { - state.stop(); - } - renameFile(bucketId: string, fileId: string, newPlainName: string): Promise { const mnemonic: string | undefined = this.config.encryptionKey; diff --git a/src/lib/INXTRequest.ts b/src/lib/INXTRequest.ts index 21498c5..f118559 100644 --- a/src/lib/INXTRequest.ts +++ b/src/lib/INXTRequest.ts @@ -1,10 +1,8 @@ import { ClientRequest } from 'http'; import { EventEmitter } from 'events'; -import { Readable } from 'stream'; -import axios, { AxiosRequestConfig, AxiosResponse, Canceler } from 'axios'; +import axios, { AxiosRequestConfig } from 'axios'; -import { request, streamRequest } from '../services/request'; -import { ProxyManager, getProxy } from '../services/proxy'; +import { request } from '../services/request'; import { EnvironmentConfig } from '../api'; export enum Methods { @@ -18,19 +16,12 @@ export enum Methods { export class INXTRequest extends EventEmitter { private req: Promise | ClientRequest | undefined; private config: EnvironmentConfig; - private cancel: Canceler; private useProxy: boolean; - private streaming = false; method: Methods; targetUrl: string; params: AxiosRequestConfig; - static Events = { - UploadProgress: 'upload-progress', - DownloadProgress: 'download-progress', - }; - constructor( config: EnvironmentConfig, method: Methods, @@ -45,14 +36,11 @@ export class INXTRequest extends EventEmitter { this.targetUrl = targetUrl; this.useProxy = useProxy ?? false; this.params = params; - - this.cancel = () => null; } start(): Promise { // TODO: Abstract from axios const source = axios.CancelToken.source(); - this.cancel = source.cancel; const cancelToken = source.token; @@ -66,67 +54,4 @@ export class INXTRequest extends EventEmitter { return this.req; } - - async stream(content: Readable, size: number): Promise>; - // eslint-disable-next-line @typescript-eslint/no-unused-vars - async stream(): Promise; - async stream(content?: any, size?: number): Promise { - if (size) { - return this.postStream(content, size); - } - - return this.getStream(); - } - - private async getStream(): Promise { - this.streaming = true; - - let proxy: ProxyManager | undefined; - - if (this.useProxy) { - proxy = await getProxy(); - } - - const targetUrl = `${proxy && proxy.url ? proxy.url + '/' : ''}${this.targetUrl}`; - - return streamRequest(targetUrl); - } - - private async postStream(content: Readable, size: number): Promise { - this.streaming = true; - - let proxy: ProxyManager | undefined; - - if (this.useProxy) { - proxy = await getProxy(); - } - - const targetUrl = `${proxy && proxy.url ? proxy.url + '/' : ''}${this.targetUrl}`; - - return axios - .post(targetUrl, content, { - maxContentLength: Infinity, - headers: { - 'Content-Type': 'application/octet-stream', - 'Content-Length': size.toString(), - }, - }) - .then((res) => { - proxy?.free(); - - return res as unknown as K; - }); - } - - abort() { - if (this.streaming && this.req instanceof ClientRequest) { - return this.req.destroy(); - } - - this.cancel(); - } - - isCancelled(err: Error): boolean { - return axios.isCancel(err); - } } diff --git a/src/lib/core/upload/multipart.ts b/src/lib/core/upload/multipart.ts index fcda916..582c5ea 100644 --- a/src/lib/core/upload/multipart.ts +++ b/src/lib/core/upload/multipart.ts @@ -46,19 +46,14 @@ export async function uploadParts( let partChunks: Buffer[] = []; let uploadPartError: Error | null = null; - const uploadQueue = queue(async (part: PartUpload, callback) => { + const uploadQueue = queue(async (part: PartUpload) => { logger.debug('Uploading part %s of %s => %s bytes', part.source.index, partUrls.length, part.source.size); - try { - const etag = await uploadPart(part.url, part.source, signal); - - if (!etag) { - throw new Error('ETag header was not returned'); - } - parts.push({ PartNumber: part.source.index, ETag: etag }); - callback(); - } catch (err) { - callback(err as Error); + const etag = await uploadPart(part.url, part.source, signal); + + if (!etag) { + throw new Error('ETag header was not returned'); } + parts.push({ PartNumber: part.source.index, ETag: etag }); }, concurrency); uploadQueue.error((err) => { diff --git a/src/services/request.ts b/src/services/request.ts index 03d6660..c3da1ad 100644 --- a/src/services/request.ts +++ b/src/services/request.ts @@ -1,8 +1,6 @@ -import * as url from 'url'; import * as https from 'https'; import * as http from 'http'; import { Readable } from 'stream'; -import { ClientRequest, IncomingMessage } from 'http'; import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'; import { EnvironmentConfig } from '../api'; @@ -45,61 +43,6 @@ export async function request( }); } -export function streamRequest(targetUrl: string, timeoutSeconds?: number): Readable { - const uriParts = url.parse(targetUrl); - let downloader: ClientRequest | null = null; - - function _createDownloadStream(): ClientRequest { - const requestOpts = { - protocol: uriParts.protocol, - hostname: uriParts.hostname, - port: uriParts.port, - path: uriParts.path, - headers: { - Accept: 'application/octet-stream', - }, - }; - - return uriParts.protocol === 'http:' ? http.get(requestOpts) : https.get(requestOpts); - } - - return new Readable({ - read() { - if (!downloader) { - downloader = _createDownloadStream(); - - if (timeoutSeconds) { - downloader.setTimeout(timeoutSeconds * 1000, () => { - downloader?.destroy(Error(`Request timeouted after ${timeoutSeconds} seconds`)); - }); - } - - this.once('signal', (message: string) => { - if (message === 'Destroy request') { - downloader?.destroy(); - } - - this.destroy(); - }); - - downloader - .on('response', (res: IncomingMessage) => { - res - .on('data', this.push.bind(this)) - .on('error', this.emit.bind(this, 'error')) - .on('end', () => { - this.push.bind(this, null); - this.emit('end'); - }) - .on('close', this.emit.bind(this, 'close')); - }) - .on('error', this.emit.bind(this, 'error')) - .on('timeout', () => this.emit('error', Error('Request timeout'))); - } - }, - }); -} - export async function getStream(url: string, config = { useProxy: false }): Promise { let targetUrl = url; let free: undefined | (() => void); diff --git a/vitest.config.mjs b/vitest.config.mjs index 7a0eae0..7d47704 100644 --- a/vitest.config.mjs +++ b/vitest.config.mjs @@ -12,7 +12,4 @@ export default defineConfig({ }, restoreMocks: true, }, - oxc: { - target: 'es2015', - }, });