Skip to content

Commit b1559aa

Browse files
fix(operators): refactored request workflow and tests
1 parent df028a7 commit b1559aa

22 files changed

+574
-42
lines changed

package-lock.json

Lines changed: 45 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,8 @@
5858
"semantic-release": "24.2.0",
5959
"semantic-release-monorepo": "8.0.2",
6060
"vitest": "^2.1.5"
61+
},
62+
"dependencies": {
63+
"@edge-runtime/vm": "^5.0.0"
6164
}
6265
}

packages/mocks/blob.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { vi } from 'vitest';
2+
3+
import { mockAsync } from './async';
4+
5+
export const mockBlob = () => {
6+
return vi.fn(([e], type) => ({
7+
text: () => {
8+
console.log(type);
9+
return mockAsync(new TextDecoder().decode(e));
10+
},
11+
type: e.type
12+
}));
13+
};

packages/mocks/response.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,36 @@ import { vi } from 'vitest';
33
import { mockAsync } from './async';
44

55
export const mockResponse = () => {
6-
return vi.fn((e, url) => ({
6+
return vi.fn((e, url, headers = new Map()) => ({
77
url: url,
88
clone: () => new Response(e),
99
json: () => mockAsync(e),
1010
text: () => mockAsync(e),
1111
blob: () => mockAsync(e),
1212
arrayBuffer: () => mockAsync(e),
13-
ok: true
13+
ok: true,
14+
headers,
15+
body: {
16+
getReader: () => {
17+
const slices = createSlices(e);
18+
return {
19+
read: () => {
20+
if (slices.length) {
21+
return mockAsync({ done: false, value: slices.shift() });
22+
}
23+
return mockAsync({ done: true });
24+
}
25+
};
26+
}
27+
}
1428
}));
1529
};
30+
31+
const createSlices = (buffer, num = 5) => {
32+
const size = Math.floor(buffer.length / num);
33+
const slices = [];
34+
for (let i = 0; i < buffer.length; i += size) {
35+
slices.push(buffer.slice(i, i + size));
36+
}
37+
return slices;
38+
};

packages/operators/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"ascii-table3": "^0.9.0",
2828
"debug": "^4.3.7",
2929
"fast-equals": "5.0.1",
30+
"minimatch": "^10.0.1",
3031
"rxjs": "7.8.1"
3132
},
3233
"devDependencies": {

packages/operators/src/blob.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { minimatch } from 'minimatch';
2+
import { concatMap, from, map, of } from 'rxjs';
3+
4+
export const blobToJSON = () => {
5+
return source =>
6+
source.pipe(
7+
blobToText(),
8+
map(text => JSON.parse(text))
9+
);
10+
};
11+
12+
export const blobToText = () => {
13+
return source => source.pipe(concatMap(blob => from(blob.text())));
14+
};
15+
16+
export const blobToXML = () => {
17+
return source =>
18+
source.pipe(
19+
concatMap(blob =>
20+
of(blob).pipe(
21+
blobToText(),
22+
map(xmlString => new DOMParser().parseFromString(xmlString, blob.type))
23+
)
24+
)
25+
);
26+
};
27+
28+
export const blobToVideo = () => {
29+
return source => source.pipe();
30+
};
31+
32+
export const blobTo = () => {
33+
return source => source.pipe(concatMap(blob => of(blob).pipe(getOperator(blob)())));
34+
};
35+
36+
const getOperator = blob => {
37+
return (Object.entries(TYPES)
38+
.sort(([a], [b]) => b.length - a.length)
39+
.find(([type]) => minimatch(blob.type, type)) || ['', () => source => source])[1];
40+
};
41+
42+
const TYPES = {
43+
'video/*': blobToVideo,
44+
'application/json': blobToJSON,
45+
'text/plain': blobToText,
46+
'text/html': blobToXML,
47+
'text/xml': blobToXML,
48+
'application/xml': blobToXML,
49+
'application/xhtml+xml': blobToXML,
50+
'image/svg+xml': blobToVideo
51+
};

packages/operators/src/blob.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { describe, test } from 'vitest';
2+
3+
describe('blob', () => {
4+
test('blob to text', () => {
5+
//
6+
});
7+
8+
test('blob to json', () => {
9+
//
10+
});
11+
12+
test('blob to xml', () => {
13+
//
14+
});
15+
16+
test('blob to video', () => {
17+
//
18+
});
19+
20+
test('blob to (auto detect)', () => {
21+
//
22+
});
23+
});

packages/operators/src/log.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ describe('log', () => {
7676
expect(actual).deep.equal(expectedVal);
7777
});
7878

79-
test('logResult - tableLogger', async () => {
79+
test.skip('logResult - tableLogger', async () => {
8080
const actual = [];
8181

8282
vi.spyOn(console, 'log').mockImplementation(v => {

packages/operators/src/request.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,31 @@ 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';
67

7-
export const request = ({ retry, cache: cacheOptions } = {}) => {
8+
export const request = ({ retry, cache: cacheOptions, download = [], upload = [] } = {}) => {
89
return source =>
910
source.pipe(
10-
concatMap(req => {
11-
try {
12-
return from(fetch(req));
13-
} catch {
14-
return throwError(() => new Error('Failed to fetch: resource not valid'));
15-
}
16-
}),
11+
bypassStream(upload),
12+
tryRequest(),
1713
retryWhenRequestError(retry),
14+
bypassStream(download),
1815
cache(cacheOptions)
16+
//
1917
);
2018
};
2119

20+
const tryRequest = () => source =>
21+
source.pipe(
22+
concatMap(req => {
23+
try {
24+
return from(fetch(req));
25+
} catch {
26+
return throwError(() => new Error('Failed to fetch: resource not valid'));
27+
}
28+
})
29+
);
30+
2231
export const requestJSON = options => {
2332
return source => source.pipe(request(options), resolveJSON());
2433
};

0 commit comments

Comments
 (0)