Skip to content

Commit f392cf2

Browse files
Fix tsconfig target
1 parent 370b0ae commit f392cf2

5 files changed

Lines changed: 8 additions & 159 deletions

File tree

src/index.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ type GetBucketsCallback = (err: Error | null, result: any) => void;
2828

2929
type GetBucketIdCallback = (err: Error | null, result: any) => void;
3030

31-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
32-
type DeleteBucketCallback = (err: Error | null, result: any) => void;
33-
3431
type ListFilesCallback = (err: Error | null, result: any) => void;
3532

3633
type DeleteFileCallback = (err: Error | null, result: any) => void;
@@ -146,10 +143,6 @@ export class Environment {
146143
cb(Error('Not implemented yet'), null);
147144
}
148145

149-
setEncryptionKey(newEncryptionKey: string): void {
150-
this.config.encryptionKey = newEncryptionKey;
151-
}
152-
153146
upload: UploadStrategyFunction = async (bucketId: string, opts: UploadOptions) => {
154147
if (!this.config.encryptionKey) {
155148
throw Error('Mnemonic was not provided, please, provide a mnemonic');
@@ -267,10 +260,6 @@ export class Environment {
267260
state.stop();
268261
}
269262

270-
uploadCancel(state: ActionState): void {
271-
state.stop();
272-
}
273-
274263
renameFile(bucketId: string, fileId: string, newPlainName: string): Promise<void> {
275264
const mnemonic: string | undefined = this.config.encryptionKey;
276265

src/lib/INXTRequest.ts

Lines changed: 2 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { ClientRequest } from 'http';
22
import { EventEmitter } from 'events';
3-
import { Readable } from 'stream';
4-
import axios, { AxiosRequestConfig, AxiosResponse, Canceler } from 'axios';
3+
import axios, { AxiosRequestConfig } from 'axios';
54

6-
import { request, streamRequest } from '../services/request';
7-
import { ProxyManager, getProxy } from '../services/proxy';
5+
import { request } from '../services/request';
86
import { EnvironmentConfig } from '../api';
97

108
export enum Methods {
@@ -18,19 +16,12 @@ export enum Methods {
1816
export class INXTRequest extends EventEmitter {
1917
private req: Promise<any> | ClientRequest | undefined;
2018
private config: EnvironmentConfig;
21-
private cancel: Canceler;
2219
private useProxy: boolean;
23-
private streaming = false;
2420

2521
method: Methods;
2622
targetUrl: string;
2723
params: AxiosRequestConfig;
2824

29-
static Events = {
30-
UploadProgress: 'upload-progress',
31-
DownloadProgress: 'download-progress',
32-
};
33-
3425
constructor(
3526
config: EnvironmentConfig,
3627
method: Methods,
@@ -45,14 +36,11 @@ export class INXTRequest extends EventEmitter {
4536
this.targetUrl = targetUrl;
4637
this.useProxy = useProxy ?? false;
4738
this.params = params;
48-
49-
this.cancel = () => null;
5039
}
5140

5241
start<K>(): Promise<K> {
5342
// TODO: Abstract from axios
5443
const source = axios.CancelToken.source();
55-
this.cancel = source.cancel;
5644

5745
const cancelToken = source.token;
5846

@@ -66,67 +54,4 @@ export class INXTRequest extends EventEmitter {
6654

6755
return this.req;
6856
}
69-
70-
async stream<K>(content: Readable, size: number): Promise<AxiosResponse<K>>;
71-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
72-
async stream<K>(): Promise<Readable>;
73-
async stream<K>(content?: any, size?: number): Promise<any> {
74-
if (size) {
75-
return this.postStream<K>(content, size);
76-
}
77-
78-
return this.getStream();
79-
}
80-
81-
private async getStream(): Promise<Readable> {
82-
this.streaming = true;
83-
84-
let proxy: ProxyManager | undefined;
85-
86-
if (this.useProxy) {
87-
proxy = await getProxy();
88-
}
89-
90-
const targetUrl = `${proxy && proxy.url ? proxy.url + '/' : ''}${this.targetUrl}`;
91-
92-
return streamRequest(targetUrl);
93-
}
94-
95-
private async postStream<K>(content: Readable, size: number): Promise<K> {
96-
this.streaming = true;
97-
98-
let proxy: ProxyManager | undefined;
99-
100-
if (this.useProxy) {
101-
proxy = await getProxy();
102-
}
103-
104-
const targetUrl = `${proxy && proxy.url ? proxy.url + '/' : ''}${this.targetUrl}`;
105-
106-
return axios
107-
.post<K>(targetUrl, content, {
108-
maxContentLength: Infinity,
109-
headers: {
110-
'Content-Type': 'application/octet-stream',
111-
'Content-Length': size.toString(),
112-
},
113-
})
114-
.then((res) => {
115-
proxy?.free();
116-
117-
return res as unknown as K;
118-
});
119-
}
120-
121-
abort() {
122-
if (this.streaming && this.req instanceof ClientRequest) {
123-
return this.req.destroy();
124-
}
125-
126-
this.cancel();
127-
}
128-
129-
isCancelled(err: Error): boolean {
130-
return axios.isCancel(err);
131-
}
13257
}

src/lib/core/upload/multipart.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,14 @@ export async function uploadParts(
4646
let partChunks: Buffer[] = [];
4747
let uploadPartError: Error | null = null;
4848

49-
const uploadQueue = queue(async (part: PartUpload, callback) => {
49+
const uploadQueue = queue(async (part: PartUpload) => {
5050
logger.debug('Uploading part %s of %s => %s bytes', part.source.index, partUrls.length, part.source.size);
51-
try {
52-
const etag = await uploadPart(part.url, part.source, signal);
53-
54-
if (!etag) {
55-
throw new Error('ETag header was not returned');
56-
}
57-
parts.push({ PartNumber: part.source.index, ETag: etag });
58-
callback();
59-
} catch (err) {
60-
callback(err as Error);
51+
const etag = await uploadPart(part.url, part.source, signal);
52+
53+
if (!etag) {
54+
throw new Error('ETag header was not returned');
6155
}
56+
parts.push({ PartNumber: part.source.index, ETag: etag });
6257
}, concurrency);
6358

6459
uploadQueue.error((err) => {

src/services/request.ts

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import * as url from 'url';
21
import * as https from 'https';
32
import * as http from 'http';
43
import { Readable } from 'stream';
5-
import { ClientRequest, IncomingMessage } from 'http';
64
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
75

86
import { EnvironmentConfig } from '../api';
@@ -45,61 +43,6 @@ export async function request(
4543
});
4644
}
4745

48-
export function streamRequest(targetUrl: string, timeoutSeconds?: number): Readable {
49-
const uriParts = url.parse(targetUrl);
50-
let downloader: ClientRequest | null = null;
51-
52-
function _createDownloadStream(): ClientRequest {
53-
const requestOpts = {
54-
protocol: uriParts.protocol,
55-
hostname: uriParts.hostname,
56-
port: uriParts.port,
57-
path: uriParts.path,
58-
headers: {
59-
Accept: 'application/octet-stream',
60-
},
61-
};
62-
63-
return uriParts.protocol === 'http:' ? http.get(requestOpts) : https.get(requestOpts);
64-
}
65-
66-
return new Readable({
67-
read() {
68-
if (!downloader) {
69-
downloader = _createDownloadStream();
70-
71-
if (timeoutSeconds) {
72-
downloader.setTimeout(timeoutSeconds * 1000, () => {
73-
downloader?.destroy(Error(`Request timeouted after ${timeoutSeconds} seconds`));
74-
});
75-
}
76-
77-
this.once('signal', (message: string) => {
78-
if (message === 'Destroy request') {
79-
downloader?.destroy();
80-
}
81-
82-
this.destroy();
83-
});
84-
85-
downloader
86-
.on('response', (res: IncomingMessage) => {
87-
res
88-
.on('data', this.push.bind(this))
89-
.on('error', this.emit.bind(this, 'error'))
90-
.on('end', () => {
91-
this.push.bind(this, null);
92-
this.emit('end');
93-
})
94-
.on('close', this.emit.bind(this, 'close'));
95-
})
96-
.on('error', this.emit.bind(this, 'error'))
97-
.on('timeout', () => this.emit('error', Error('Request timeout')));
98-
}
99-
},
100-
});
101-
}
102-
10346
export async function getStream(url: string, config = { useProxy: false }): Promise<Readable> {
10447
let targetUrl = url;
10548
let free: undefined | (() => void);

vitest.config.mjs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,4 @@ export default defineConfig({
1212
},
1313
restoreMocks: true,
1414
},
15-
oxc: {
16-
target: 'es2015',
17-
},
1815
});

0 commit comments

Comments
 (0)