-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcaching-queued-fetch.js
219 lines (182 loc) · 6.49 KB
/
caching-queued-fetch.js
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* Tests the caching fetch library.
*/
/* global describe, it */
const QueuedFetch = require("../lib/caching-queued-fetch");
const { queuedFetch } = QueuedFetch;
const { MockAgent, setGlobalDispatcher, getGlobalDispatcher } = require('undici');
const origInterval = QueuedFetch.DEFAULT_INTERVAL;
const assert = require("assert");
const origDispatcher = getGlobalDispatcher();
const agent = new MockAgent();
const mock = (url, response = 200, delay = 0, responseOptions = {}) => {
const u = new URL(url);
const interceptor = agent
.get(u.origin)
.intercept({path: u.pathname, method: "GET"})
.reply(response, null, responseOptions);
if (delay > 0) {
interceptor.delay(delay);
}
};
const testBaseUrl = 'https://example.com/';
const testBaseUrl2 = 'https://example.net/';
let counter = 0;
const newPath = () => {
return "" + (counter++);
};
describe('The HTTP request manager', function () {
this.timeout(5000);
before(() => {
setGlobalDispatcher(agent);
agent.disableNetConnect();
});
it('makes only one request to a given URL', async () => {
mock(testBaseUrl);
const requests = [];
for(let i = 0; i<10; i++) {
requests.push(queuedFetch(testBaseUrl));
}
await Promise.all(requests);
});
it('queues different request to a single server, respecting the default request interval', async () => {
const origInterval = QueuedFetch.DEFAULT_INTERVAL;
QueuedFetch.DEFAULT_INTERVAL = 1000;
const url1 = testBaseUrl + newPath();
const url2 = testBaseUrl + newPath();
mock(url1, 200, 500);
mock(url2, 304);
const requests = [];
const startTime = Date.now();
requests.push(queuedFetch(url1));
requests.push(queuedFetch(url2));
const response = await Promise.race(requests);
await Promise.all(requests);
const endTime = Date.now();
assert.equal(response.status, 200);
assert(endTime-startTime > QueuedFetch.DEFAULT_INTERVAL);
});
it('queues different request to a single server, respecting the specified request interval', async () => {
const url1 = testBaseUrl + newPath();
const url2 = testBaseUrl + newPath();
mock(url1, 200, 500);
mock(url2, 304);
const interval = 2000;
const requests = [];
const startTime = Date.now();
requests.push(queuedFetch(url1, {}, {interval}));
requests.push(queuedFetch(url2));
const response = await Promise.race(requests);
await Promise.all(requests);
const endTime = Date.now();
assert.equal(response.status, 200);
assert(endTime-startTime > interval);
});
it("reports an HTTP error but doesn't stop crawling upon it", async () => {
const url1 = testBaseUrl + newPath();
const url2 = testBaseUrl + newPath();
mock(url1, 404, 500);
mock(url2, 200);
const requests = [];
requests.push(queuedFetch(url1));
requests.push(queuedFetch(url2));
await Promise.all(requests)
.then(arr => {
assert.equal(arr[0], undefined);
assert.equal(arr[1].status, 200);
})
.catch(err =>
assert.match(err.message, /404/)
);
});
it("respects retry-after header", async () => {
const url1 = testBaseUrl + newPath();
const url2 = testBaseUrl + newPath();
mock(url1, 503, 0, { headers: {"Retry-After": 2}});
mock(url1, 200);
mock(url2, 200);
const requests = [];
requests.push(queuedFetch(url1));
requests.push(queuedFetch(url2));
const startTime = Date.now();
await Promise.all(requests)
.then(arr => {
assert.equal(arr[0].status, 200);
assert.equal(arr[1].status, 200);
})
.catch(err =>
assert.fail("Unexpected HTTP error:" + err)
);
const endTime = Date.now();
assert(endTime-startTime > 2000);
});
it("respects x-ratelimit-reset header", async () => {
const url1 = testBaseUrl + newPath();
const url2 = testBaseUrl + newPath();
mock(url1, 503, 0, { headers: {"X-Ratelimit-Reset": (Date.now() / 1000) + 2 }});
mock(url1, 200);
mock(url2, 200);
const requests = [];
requests.push(queuedFetch(url1));
requests.push(queuedFetch(url2));
const startTime = Date.now();
await Promise.all(requests)
.then(arr => {
assert.equal(arr[0].status, 200);
assert.equal(arr[1].status, 200);
})
.catch(err =>
assert.fail("Unexpected HTTP error:" + err)
);
const endTime = Date.now();
assert(endTime-startTime > 2000, endTime-startTime);
});
it('runs in parallel requests to distinct servers', async () => {
const url1 = testBaseUrl + newPath();
const url2 = testBaseUrl2 + newPath();
mock(url1, 200, 500);
mock(url2, 304);
const requests = [];
requests.push(queuedFetch(url1));
requests.push(queuedFetch(url2));
const response = await Promise.race(requests);
await Promise.all(requests);
assert.equal(response.status, 304);
});
it('loads a response from the FS cache when it has not expired', async () => {
const ims = "Fri, 20 Oct 2023 16:51:59 GMT";
const fsUrl = testBaseUrl + "fs";
const u = new URL(fsUrl);
const response = await queuedFetch(fsUrl, {headers: {authorization: "test"}}, {fsCachePath: "test/fs-cache", verbose: true});
assert.match(response.headers.get("cache-status"), /hit/);
});
it('handles 304 from the FS cache with the proper request header', async () => {
const ims = "Fri, 20 Oct 2023 16:51:59 GMT";
const fsUrl = testBaseUrl + "fs3";
const u = new URL(fsUrl);
const interceptor = agent
.get(u.origin)
.intercept({path: u.pathname, method: "GET", headers: {"if-modified-since": ims}})
.reply(304);
const response = await queuedFetch(fsUrl, {headers: {authorization: "test", "if-modified-since": ims}}, {fsCachePath: "test/fs-cache", verbose: true});
assert.equal(response.headers.get("cache-status"), null);
});
it('skips a response from the FS cache when it is stale', async () => {
const fsUrl = testBaseUrl + "fs2";
const u = new URL(fsUrl);
const interceptor = agent
.get(u.origin)
.intercept({path: u.pathname, method: "GET"})
.reply(200);
const response = await queuedFetch(fsUrl, {}, {fsCachePath: "test/fs-cache", verbose: true});
assert.equal(response.headers.get("cache-status"), null);
});
afterEach(() => {
QueuedFetch.INTERVAL = origInterval;
agent.assertNoPendingInterceptors();
});
after(async () => {
await agent.close();
setGlobalDispatcher(origDispatcher);
});
});