Skip to content

Commit ba876c2

Browse files
fix(request): refactored stream to transfer
1 parent 9c3668e commit ba876c2

14 files changed

+39
-39
lines changed

packages/operators/src/request.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import { concatMap, from, throwError } from 'rxjs';
33
import { cache } from './cache';
44
import { resolveBlob, resolveJSON, resolveText } from './response';
55
import { retryWhenRequestError } from './retry';
6-
import { bypassStream } from './stream/bypassStream';
6+
import { interceptTransfer } from './transfer/interceptTransfer';
77

88
export const request = ({ retry, cache: cacheOptions, stats } = {}) => {
99
return source =>
1010
source.pipe(
11-
bypassStream(stats?.upload),
11+
interceptTransfer(stats?.upload),
1212
tryRequest(),
1313
retryWhenRequestError(retry),
14-
bypassStream(stats?.download),
14+
interceptTransfer(stats?.download),
1515
cache(cacheOptions)
1616
//
1717
);

packages/operators/src/request.test.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import { test, describe, beforeEach, expect, vi, afterAll, beforeAll } from 'vit
99

1010
import { log, logResult } from './log.js';
1111
import { resolveBlob, resolveJSON } from './response.js';
12-
import ElapsedTime from './stream/stats/ElapsedTime.js';
13-
import EstimateTime from './stream/stats/EstimateTime.js';
14-
import Progress from './stream/stats/Progress.js';
15-
import TransferRate from './stream/stats/TransferRate.js';
16-
import { MBYTE, SECOND } from './stream/stats/utils.js';
12+
import Bandwidth from './transfer/stats/Bandwidth.js';
13+
import ElapsedTime from './transfer/stats/ElapsedTime.js';
14+
import Progress from './transfer/stats/Progress.js';
15+
import TimeEstimate from './transfer/stats/TimeEstimate.js';
16+
import { MBYTE, SECOND } from './transfer/stats/utils.js';
1717

1818
describe.skip('request', () => {
1919
let testScheduler;
@@ -209,11 +209,11 @@ describe('test', () => {
209209
const progress = Progress.create();
210210
progress.subscribe({ next: e => console.log('DOWNLOAD', e) });
211211

212-
const byteRate = TransferRate.create(MBYTE, SECOND);
213-
byteRate.subscribe({ next: e => console.log('RATE', e) });
212+
const bandwidth = Bandwidth.create(MBYTE, SECOND);
213+
bandwidth.subscribe({ next: e => console.log('RATE', e) });
214214

215-
const estimateTime = EstimateTime.create(SECOND);
216-
estimateTime.subscribe({ next: e => console.log('ESTIMATE', e) });
215+
const timeEstimate = TimeEstimate.create(SECOND);
216+
timeEstimate.subscribe({ next: e => console.log('ESTIMATE', e) });
217217

218218
const elapsedTime = ElapsedTime.create(SECOND);
219219
elapsedTime.subscribe({ next: e => console.log('ELAPSED', e) });
@@ -233,7 +233,7 @@ describe('test', () => {
233233

234234
const value = await lastValueFrom(
235235
of(req).pipe(
236-
request({ stats: { download: [progress, byteRate, estimateTime, elapsedTime] } }),
236+
request({ stats: { download: [progress, bandwidth, timeEstimate, elapsedTime] } }),
237237
resolveBlob()
238238
//
239239
)

packages/operators/src/stream/bypassStream.js renamed to packages/operators/src/transfer/interceptTransfer.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@ import { concatMap, from, map, of } from 'rxjs';
22

33
import { readBytes } from './utils';
44

5-
export const bypassStream = (reworkers = [], chunkSize = 60 * 1024) => {
5+
export const interceptTransfer = (reworkers = [], chunkSize = 60 * 1024) => {
66
return source =>
77
source.pipe(
88
concatMap(requestResponse => {
99
if (reworkers.length) {
1010
return of(requestResponse).pipe(
11-
objectToStream(),
11+
sourceToStream(),
1212
interceptStream(reworkers, chunkSize),
13-
streamToObject(requestResponse)
13+
streamToSource(requestResponse)
1414
);
1515
}
1616
return of(requestResponse);
1717
})
1818
);
1919
};
2020

21-
const objectToStream = () => {
21+
const sourceToStream = () => {
2222
return source =>
2323
source.pipe(concatMap(reqResp => objectToStreamMap.get(reqResp.constructor)(reqResp)));
2424
};
2525

26-
const streamToObject = reqResp => {
26+
const streamToSource = reqResp => {
2727
return source =>
2828
source.pipe(concatMap(stream => streamToObjectMap.get(reqResp.constructor)(stream, reqResp)));
2929
};

packages/operators/src/stream/bypassStream.test.js renamed to packages/operators/src/transfer/interceptTransfer.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { of, Subject } from 'rxjs';
55
import { TestScheduler } from 'rxjs/testing';
66
import { beforeAll, beforeEach, describe, expect, test, vi } from 'vitest';
77

8-
import { bypassStream } from './bypassStream';
8+
import { interceptTransfer } from './interceptTransfer';
99

10-
describe('bypass stream', () => {
10+
describe('intercept transfer', () => {
1111
let testScheduler;
1212

1313
beforeAll(() => {
@@ -19,21 +19,21 @@ describe('bypass stream', () => {
1919
testScheduler = new TestScheduler((actual, expected) => expect(actual).deep.equal(expected));
2020
});
2121

22-
test('bypass response stream', async () => {
22+
test('intercept response', async () => {
2323
const blob = new Blob(
2424
[fs.readFileSync(`${__dirname}/../../fixtures/videos/demo.mp4`)],
2525
'video/mp4'
2626
);
2727

28-
const bypass = new Subject();
28+
const intercept = new Subject();
2929

3030
const triggerVal = {
3131
a: new Response(blob, { status: 200 })
3232
};
3333

3434
// testScheduler.run(({ cold, expectObservable }) => {
3535
// expectObservable(bypass).toBe('a');
36-
// expectObservable(cold('a|', triggerVal).pipe(bypassStream([bypass])));
36+
// expectObservable(cold('a|', triggerVal).pipe(interceptTransfer([bypass])));
3737
// });
3838
});
3939
});

packages/operators/src/stream/stats/TransferRate.test.js renamed to packages/operators/src/transfer/stats/Bandwidth.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { tap } from 'rxjs';
22
import { TestScheduler } from 'rxjs/testing';
33
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
44

5-
import TransferRate from './TransferRate';
5+
import Bandwidth from './Bandwidth';
66
import { KBIT } from './utils';
77

8-
describe('TransferRate', () => {
8+
describe('Bandwidth', () => {
99
let testScheduler;
1010

1111
beforeEach(() => {
@@ -18,7 +18,7 @@ describe('TransferRate', () => {
1818
vi.restoreAllMocks();
1919
});
2020

21-
test('calc transfer rate', async () => {
21+
test('calc bandwidth', async () => {
2222
const time = Date.now();
2323

2424
const triggerVal = {
@@ -37,11 +37,11 @@ describe('TransferRate', () => {
3737
e: 15.625
3838
};
3939

40-
const transferRate = TransferRate.create(KBIT);
40+
const bandwidth = Bandwidth.create(KBIT);
4141

4242
testScheduler.run(({ cold, expectObservable }) => {
43-
expectObservable(transferRate).toBe('--a-b-c-d-e|', expectedVal);
44-
expectObservable(cold('--a-b-c-d-e|', triggerVal).pipe(tap(transferRate)));
43+
expectObservable(bandwidth).toBe('--a-b-c-d-e|', expectedVal);
44+
expectObservable(cold('--a-b-c-d-e|', triggerVal).pipe(tap(bandwidth)));
4545
});
4646
});
4747
});

packages/operators/src/stream/stats/EstimateTime.js renamed to packages/operators/src/transfer/stats/TimeEstimate.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default {
77
return new Subject().pipe(
88
calcReceivedStats(),
99
calcEstimatedTime(),
10-
convertEstimedTimeTo(timeUnit),
10+
convertEstimatedTimeTo(timeUnit),
1111
concatWith(of(0)),
1212
distinctUntilChanged()
1313
);
@@ -19,6 +19,6 @@ const calcEstimatedTime = () => {
1919
source.pipe(map(({ value, total, period }) => Math.ceil((total - value) * (period / value))));
2020
};
2121

22-
const convertEstimedTimeTo = timeRatio => {
22+
const convertEstimatedTimeTo = timeRatio => {
2323
return source => source.pipe(map(value => value / timeRatio));
2424
};

packages/operators/src/stream/stats/EstimateTime.test.js renamed to packages/operators/src/transfer/stats/TimeEstimate.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { tap } from 'rxjs';
22
import { TestScheduler } from 'rxjs/testing';
33
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
44

5-
import EstimateTime from './EstimateTime';
5+
import TimeEstimate from './TimeEstimate';
66
import { SECOND } from './utils';
77

8-
describe('EstimateTime', () => {
8+
describe('TimeEstimate', () => {
99
let testScheduler;
1010

1111
beforeEach(() => {
@@ -37,11 +37,11 @@ describe('EstimateTime', () => {
3737
e: 0
3838
};
3939

40-
const estimateTime = EstimateTime.create();
40+
const timeEstimate = TimeEstimate.create();
4141

4242
testScheduler.run(({ cold, expectObservable }) => {
43-
expectObservable(estimateTime).toBe('--a-b-c-d-e|', expectedVal);
44-
expectObservable(cold('--a-b-c-d-e|', triggerVal).pipe(tap(estimateTime)));
43+
expectObservable(timeEstimate).toBe('--a-b-c-d-e|', expectedVal);
44+
expectObservable(cold('--a-b-c-d-e|', triggerVal).pipe(tap(timeEstimate)));
4545
});
4646
});
4747

@@ -64,11 +64,11 @@ describe('EstimateTime', () => {
6464
e: 0
6565
};
6666

67-
const estimateTimeSecond = EstimateTime.create(SECOND);
67+
const timeEstimateSecond = TimeEstimate.create(SECOND);
6868

6969
testScheduler.run(({ cold, expectObservable }) => {
70-
expectObservable(estimateTimeSecond).toBe('--a-b-c-d-e|', expectedVal);
71-
expectObservable(cold('--a-b-c-d-e|', triggerVal).pipe(tap(estimateTimeSecond)));
70+
expectObservable(timeEstimateSecond).toBe('--a-b-c-d-e|', expectedVal);
71+
expectObservable(cold('--a-b-c-d-e|', triggerVal).pipe(tap(timeEstimateSecond)));
7272
});
7373
});
7474
});

0 commit comments

Comments
 (0)