Skip to content

Commit e45fcdd

Browse files
committed
feat: force database update
1 parent 3460020 commit e45fcdd

File tree

2 files changed

+138
-115
lines changed

2 files changed

+138
-115
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ A file's extension has no meaning on the web. In order for the client to interpr
1010
```typescript
1111
// Constructor
1212
constructor(
13-
updateInterval?: number = 86400000 // The time, in milliseconds. if less than zero, periodic update will be disabled.
13+
updateInterval?: number = 86400000 // Periodic database update in milliseconds. if less than zero, will be disabled
1414
)
1515
```
1616

@@ -37,6 +37,8 @@ append(
3737
get(
3838
path: string // https://nodejs.org/api/path.html#pathparsepath
3939
): string[] // MIME type list
40+
41+
update(): void // database update
4042
```
4143

4244
```typescript

src/MimeTypes.js

Lines changed: 135 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,18 @@ class MimeTypes extends EventEmitter {
5353
}
5454

5555

56-
this.#update();
56+
try {
5757

58-
this.#updateInterval = updateInterval;
58+
this.update();
59+
60+
} catch (err) {
61+
62+
console.error(err);
63+
64+
}
65+
66+
67+
this.updateInterval = updateInterval;
5968

6069
}
6170

@@ -225,183 +234,175 @@ class MimeTypes extends EventEmitter {
225234

226235
}
227236

228-
#update = () => {
229-
230-
try {
237+
update = () => {
231238

232-
Promise.allSettled([
233-
fetch('https://raw.githubusercontent.com/apache/httpd/trunk/docs/conf/mime.types', { // https://github.com/apache/httpd/blob/trunk/docs/conf/mime.types
234-
method: 'HEAD',
235-
headers: {
236-
'Accept-Encoding': 'identity'
237-
}
238-
}).then(res => {
239-
240-
if (res.status == 200 && res.headers.get('etag') != this.#versions.apache) {
241-
242-
return fetch('https://raw.githubusercontent.com/apache/httpd/trunk/docs/conf/mime.types', {
243-
headers: {
244-
'Accept-Encoding': 'identity'
245-
}
246-
});
247-
248-
}
249-
250-
}),
251-
fetch('https://salsa.debian.org/debian/media-types/-/raw/master/mime.types', { // https://salsa.debian.org/debian/media-types/-/blob/master/mime.types
252-
method: 'HEAD',
253-
headers: {
254-
'Accept-Encoding': 'identity'
255-
}
256-
}).then(res => {
239+
return Promise.allSettled([
240+
fetch('https://raw.githubusercontent.com/apache/httpd/trunk/docs/conf/mime.types', { // https://github.com/apache/httpd/blob/trunk/docs/conf/mime.types
241+
method: 'HEAD',
242+
headers: {
243+
'Accept-Encoding': 'identity'
244+
}
245+
}).then(res => {
257246

258-
if (res.status == 200 && res.headers.get('etag') != this.#versions.debian) {
247+
if (res.status == 200 && res.headers.get('etag') != this.#versions.apache) {
259248

260-
return fetch('https://salsa.debian.org/debian/media-types/-/raw/master/mime.types', {
261-
headers: {
262-
'Accept-Encoding': 'identity'
263-
}
264-
});
249+
return fetch('https://raw.githubusercontent.com/apache/httpd/trunk/docs/conf/mime.types', {
250+
headers: {
251+
'Accept-Encoding': 'identity'
252+
}
253+
});
265254

266-
}
255+
}
267256

268-
}),
269-
fetch('https://raw.githubusercontent.com/nginx/nginx/master/conf/mime.types', { // https://github.com/nginx/nginx/blob/master/conf/mime.types
270-
method: 'HEAD',
271-
headers: {
272-
'Accept-Encoding': 'identity'
273-
}
274-
}).then(res => {
257+
}),
258+
fetch('https://salsa.debian.org/debian/media-types/-/raw/master/mime.types', { // https://salsa.debian.org/debian/media-types/-/blob/master/mime.types
259+
method: 'HEAD',
260+
headers: {
261+
'Accept-Encoding': 'identity'
262+
}
263+
}).then(res => {
275264

276-
if (res.status == 200 && res.headers.get('etag') != this.#versions.nginx) {
265+
if (res.status == 200 && res.headers.get('etag') != this.#versions.debian) {
277266

278-
return fetch('https://raw.githubusercontent.com/nginx/nginx/master/conf/mime.types', {
279-
headers: {
280-
'Accept-Encoding': 'identity'
281-
}
282-
});
267+
return fetch('https://salsa.debian.org/debian/media-types/-/raw/master/mime.types', {
268+
headers: {
269+
'Accept-Encoding': 'identity'
270+
}
271+
});
283272

284-
}
273+
}
285274

286-
})
287-
].concat([ // https://www.iana.org/assignments/media-types/media-types.xhtml
288-
'https://www.iana.org/assignments/media-types/application.csv',
289-
'https://www.iana.org/assignments/media-types/audio.csv',
290-
'https://www.iana.org/assignments/media-types/font.csv',
291-
'https://www.iana.org/assignments/media-types/image.csv',
292-
'https://www.iana.org/assignments/media-types/message.csv',
293-
'https://www.iana.org/assignments/media-types/model.csv',
294-
'https://www.iana.org/assignments/media-types/multipart.csv',
295-
'https://www.iana.org/assignments/media-types/text.csv',
296-
'https://www.iana.org/assignments/media-types/video.csv'
297-
].map(url => fetch(url, {
275+
}),
276+
fetch('https://raw.githubusercontent.com/nginx/nginx/master/conf/mime.types', { // https://github.com/nginx/nginx/blob/master/conf/mime.types
298277
method: 'HEAD',
299278
headers: {
300-
'Accept-Encoding': 'identity',
301-
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'
279+
'Accept-Encoding': 'identity'
302280
}
303281
}).then(res => {
304282

305-
let type = url.split('/').pop().replace('.csv', '');
306-
307-
if (res.status == 200 && res.headers.get('last-modified') != this.#versions.iana?.[type]) {
283+
if (res.status == 200 && res.headers.get('etag') != this.#versions.nginx) {
308284

309-
return fetch(url, {
285+
return fetch('https://raw.githubusercontent.com/nginx/nginx/master/conf/mime.types', {
310286
headers: {
311-
'Accept-Encoding': 'identity',
312-
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'
287+
'Accept-Encoding': 'identity'
313288
}
314289
});
315290

316291
}
317292

318-
})))).then(async results => {
293+
})
294+
].concat([ // https://www.iana.org/assignments/media-types/media-types.xhtml
295+
'https://www.iana.org/assignments/media-types/application.csv',
296+
'https://www.iana.org/assignments/media-types/audio.csv',
297+
'https://www.iana.org/assignments/media-types/font.csv',
298+
'https://www.iana.org/assignments/media-types/image.csv',
299+
'https://www.iana.org/assignments/media-types/message.csv',
300+
'https://www.iana.org/assignments/media-types/model.csv',
301+
'https://www.iana.org/assignments/media-types/multipart.csv',
302+
'https://www.iana.org/assignments/media-types/text.csv',
303+
'https://www.iana.org/assignments/media-types/video.csv'
304+
].map(url => fetch(url, {
305+
method: 'HEAD',
306+
headers: {
307+
'Accept-Encoding': 'identity',
308+
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'
309+
}
310+
}).then(res => {
319311

320-
let updated = false;
312+
let type = url.split('/').pop().replace('.csv', '');
321313

322-
if (results[0].status == 'fulfilled' && results[0].value) {
314+
if (res.status == 200 && res.headers.get('last-modified') != this.#versions.iana?.[type]) {
323315

324-
let load = await this.#loadApache(results[0].value);
316+
return fetch(url, {
317+
headers: {
318+
'Accept-Encoding': 'identity',
319+
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'
320+
}
321+
});
325322

326-
if (load) {
323+
}
327324

328-
this.#versions.apache = load.version;
325+
})))).then(async results => {
329326

330-
this.#updateList(load.content);
327+
let updated = false;
331328

332-
updated = true;
329+
if (results[0].status == 'fulfilled' && results[0].value) {
333330

334-
}
331+
let load = await this.#loadApache(results[0].value);
332+
333+
if (load) {
334+
335+
this.#versions.apache = load.version;
336+
337+
this.#updateList(load.content);
338+
339+
updated = true;
335340

336341
}
337342

338-
if (results[1].status == 'fulfilled' && results[1].value) {
343+
}
339344

340-
let load = await this.#loadDebian(results[1].value);
345+
if (results[1].status == 'fulfilled' && results[1].value) {
341346

342-
if (load) {
347+
let load = await this.#loadDebian(results[1].value);
343348

344-
this.#versions.debian = load.version;
349+
if (load) {
345350

346-
this.#updateList(load.content);
351+
this.#versions.debian = load.version;
347352

348-
updated = true;
353+
this.#updateList(load.content);
349354

350-
}
355+
updated = true;
351356

352357
}
353358

354-
if (results[2].status == 'fulfilled' && results[2].value) {
359+
}
355360

356-
let load = await this.#loadNGINX(results[2].value);
361+
if (results[2].status == 'fulfilled' && results[2].value) {
357362

358-
if (load) {
363+
let load = await this.#loadNGINX(results[2].value);
359364

360-
this.#versions.nginx = load.version;
365+
if (load) {
361366

362-
this.#updateList(load.content);
367+
this.#versions.nginx = load.version;
363368

364-
updated = true;
369+
this.#updateList(load.content);
365370

366-
}
371+
updated = true;
367372

368373
}
369374

370-
for (let res of results.slice(3)) {
375+
}
371376

372-
if (res.status == 'fulfilled' && res.value) {
377+
for (let res of results.slice(3)) {
373378

374-
let load = await this.#loadIANA(res.value);
379+
if (res.status == 'fulfilled' && res.value) {
375380

376-
if (load) {
381+
let load = await this.#loadIANA(res.value);
377382

378-
this.#versions.iana[res.value.url.split('/').pop().replace('.csv', '')] = load.version;
383+
if (load) {
379384

380-
this.#updateList(load.content);
385+
this.#versions.iana[res.value.url.split('/').pop().replace('.csv', '')] = load.version;
381386

382-
updated = true;
387+
this.#updateList(load.content);
383388

384-
}
389+
updated = true;
385390

386391
}
387392

388393
}
389394

395+
}
390396

391-
if (updated) {
392-
393-
fs.writeFileSync('mimetypes.json', JSON.stringify(this.#mimeTypes));
394-
fs.writeFileSync('versions.json', JSON.stringify(this.#versions));
395397

396-
}
398+
if (updated) {
397399

398-
});
400+
fs.writeFileSync('mimetypes.json', JSON.stringify(this.#mimeTypes));
401+
fs.writeFileSync('versions.json', JSON.stringify(this.#versions));
399402

400-
} catch (err) {
401-
402-
console.error(err);
403+
}
403404

404-
}
405+
});
405406

406407
}
407408

@@ -412,13 +413,33 @@ class MimeTypes extends EventEmitter {
412413

413414
set updateInterval(updateInterval = 86400000) {
414415

416+
if (
417+
typeof updateInterval != 'number'
418+
|| !Number.isFinite(updateInterval)
419+
|| Number.isNaN(updateInterval)
420+
) {
421+
throw new TypeError('Invalid updateInterval')
422+
}
423+
415424
this.#updateInterval = updateInterval;
416425

417426
clearInterval(this.#updateLoop);
418427

419-
if (this.#updateInterval >= 0) {
428+
if (updateInterval >= 0) {
429+
430+
this.#updateLoop = setInterval(async () => {
431+
432+
try {
433+
434+
await this.update();
435+
436+
} catch (err) {
437+
438+
console.error(err);
439+
440+
}
420441

421-
this.#updateLoop = setInterval(this.#update, this.#updateInterval);
442+
}, this.#updateInterval);
422443

423444
}
424445

0 commit comments

Comments
 (0)