-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.ts
141 lines (116 loc) · 3.46 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// @ts-nocheck
import { suite } from '@marais/bench';
import { equal } from 'uvu/assert';
import { makePart, mockResponseBrowser, mockResponseNode, preamble, tail, wrap } from '../test/mocks';
import fetchMultiPartGraphql from 'fetch-multipart-graphql';
import ItMultipart from 'it-multipart';
import { meros as merosBrowser } from '../src/browser';
import { meros as merosNode } from '../src/node';
const fmg = fetchMultiPartGraphql.default;
const parts = [
{ hello: 'world' },
[{ other: 'world' }, { another: 'world' }],
{
massive: {
nested: {
world: 'okay',
},
},
},
];
const results = parts.reduce((result, item) => {
if (Array.isArray(item)) {
return [...result, ...item];
}
return [...result, item];
}, [] as any[]);
const chunks = [
[preamble, wrap],
...parts.map((v, i) => {
if (Array.isArray(v)) {
return v.map(v2 => [makePart(v2), wrap]).flat()
}
if (i === parts.length - 1) {
return [makePart(v), tail];
}
return [makePart(v), wrap];
}),
];
const chunk_gen = (async function*() {
for (const value of chunks) {
yield value;
}
});
const do_node_call = mockResponseNode.bind(null, chunk_gen(), '-');
const do_browser_call = mockResponseBrowser.bind(null, chunk_gen(), '-');
global['fetch'] = async function(url, options) {
return do_browser_call();
};
const verify = (result) => {
equal(result, results, 'should match reference patch set');
return true;
};
console.log('Node');
await suite({
meros() {
return async () => {
const response = await do_node_call();
const parts = await merosNode(response);
const collection = [];
for await (let { body } of parts) {
collection.push(body);
}
return collection;
}
},
'it-multipart'() {
return async () => {
const response = await do_node_call();
const parts = await ItMultipart(response);
const collection = [];
for await (let part of parts) {
let data = '';
for await (const chunk of part.body) {
data += String(chunk);
}
collection.push(
!!~part.headers['content-type'].indexOf('application/json')
? JSON.parse(data)
: data,
);
}
return collection;
}
},
}, (run) => {
run(undefined, undefined, verify);
});
console.log('\nBrowser');
await suite({
meros() {
return async () => {
const response = await do_browser_call();
const parts = await merosBrowser(response);
const collection = [];
for await (let { body } of parts) {
collection.push(body);
}
return collection;
}
},
'fetch-multipart-graphql'() {
return async () => {
return new Promise((resolve, reject) => {
let collection: any[] = [];
fmg('test', {
onNext: (parts: any) =>
(collection = [...collection, ...parts]),
onError: (err: Error) => reject(err),
onComplete: () => resolve(collection),
});
});
}
},
}, (run) => {
run(undefined, undefined, verify);
});