Skip to content

Commit

Permalink
feat: force database update
Browse files Browse the repository at this point in the history
  • Loading branch information
JadsonLucena committed Mar 23, 2023
1 parent 3460020 commit e45fcdd
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 115 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ A file's extension has no meaning on the web. In order for the client to interpr
```typescript
// Constructor
constructor(
updateInterval?: number = 86400000 // The time, in milliseconds. if less than zero, periodic update will be disabled.
updateInterval?: number = 86400000 // Periodic database update in milliseconds. if less than zero, will be disabled
)
```

Expand All @@ -37,6 +37,8 @@ append(
get(
path: string // https://nodejs.org/api/path.html#pathparsepath
): string[] // MIME type list

update(): void // database update
```

```typescript
Expand Down
249 changes: 135 additions & 114 deletions src/MimeTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,18 @@ class MimeTypes extends EventEmitter {
}


this.#update();
try {

this.#updateInterval = updateInterval;
this.update();

} catch (err) {

console.error(err);

}


this.updateInterval = updateInterval;

}

Expand Down Expand Up @@ -225,183 +234,175 @@ class MimeTypes extends EventEmitter {

}

#update = () => {

try {
update = () => {

Promise.allSettled([
fetch('https://raw.githubusercontent.com/apache/httpd/trunk/docs/conf/mime.types', { // https://github.com/apache/httpd/blob/trunk/docs/conf/mime.types
method: 'HEAD',
headers: {
'Accept-Encoding': 'identity'
}
}).then(res => {

if (res.status == 200 && res.headers.get('etag') != this.#versions.apache) {

return fetch('https://raw.githubusercontent.com/apache/httpd/trunk/docs/conf/mime.types', {
headers: {
'Accept-Encoding': 'identity'
}
});

}

}),
fetch('https://salsa.debian.org/debian/media-types/-/raw/master/mime.types', { // https://salsa.debian.org/debian/media-types/-/blob/master/mime.types
method: 'HEAD',
headers: {
'Accept-Encoding': 'identity'
}
}).then(res => {
return Promise.allSettled([
fetch('https://raw.githubusercontent.com/apache/httpd/trunk/docs/conf/mime.types', { // https://github.com/apache/httpd/blob/trunk/docs/conf/mime.types
method: 'HEAD',
headers: {
'Accept-Encoding': 'identity'
}
}).then(res => {

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

return fetch('https://salsa.debian.org/debian/media-types/-/raw/master/mime.types', {
headers: {
'Accept-Encoding': 'identity'
}
});
return fetch('https://raw.githubusercontent.com/apache/httpd/trunk/docs/conf/mime.types', {
headers: {
'Accept-Encoding': 'identity'
}
});

}
}

}),
fetch('https://raw.githubusercontent.com/nginx/nginx/master/conf/mime.types', { // https://github.com/nginx/nginx/blob/master/conf/mime.types
method: 'HEAD',
headers: {
'Accept-Encoding': 'identity'
}
}).then(res => {
}),
fetch('https://salsa.debian.org/debian/media-types/-/raw/master/mime.types', { // https://salsa.debian.org/debian/media-types/-/blob/master/mime.types
method: 'HEAD',
headers: {
'Accept-Encoding': 'identity'
}
}).then(res => {

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

return fetch('https://raw.githubusercontent.com/nginx/nginx/master/conf/mime.types', {
headers: {
'Accept-Encoding': 'identity'
}
});
return fetch('https://salsa.debian.org/debian/media-types/-/raw/master/mime.types', {
headers: {
'Accept-Encoding': 'identity'
}
});

}
}

})
].concat([ // https://www.iana.org/assignments/media-types/media-types.xhtml
'https://www.iana.org/assignments/media-types/application.csv',
'https://www.iana.org/assignments/media-types/audio.csv',
'https://www.iana.org/assignments/media-types/font.csv',
'https://www.iana.org/assignments/media-types/image.csv',
'https://www.iana.org/assignments/media-types/message.csv',
'https://www.iana.org/assignments/media-types/model.csv',
'https://www.iana.org/assignments/media-types/multipart.csv',
'https://www.iana.org/assignments/media-types/text.csv',
'https://www.iana.org/assignments/media-types/video.csv'
].map(url => fetch(url, {
}),
fetch('https://raw.githubusercontent.com/nginx/nginx/master/conf/mime.types', { // https://github.com/nginx/nginx/blob/master/conf/mime.types
method: 'HEAD',
headers: {
'Accept-Encoding': 'identity',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'
'Accept-Encoding': 'identity'
}
}).then(res => {

let type = url.split('/').pop().replace('.csv', '');

if (res.status == 200 && res.headers.get('last-modified') != this.#versions.iana?.[type]) {
if (res.status == 200 && res.headers.get('etag') != this.#versions.nginx) {

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

}

})))).then(async results => {
})
].concat([ // https://www.iana.org/assignments/media-types/media-types.xhtml
'https://www.iana.org/assignments/media-types/application.csv',
'https://www.iana.org/assignments/media-types/audio.csv',
'https://www.iana.org/assignments/media-types/font.csv',
'https://www.iana.org/assignments/media-types/image.csv',
'https://www.iana.org/assignments/media-types/message.csv',
'https://www.iana.org/assignments/media-types/model.csv',
'https://www.iana.org/assignments/media-types/multipart.csv',
'https://www.iana.org/assignments/media-types/text.csv',
'https://www.iana.org/assignments/media-types/video.csv'
].map(url => fetch(url, {
method: 'HEAD',
headers: {
'Accept-Encoding': 'identity',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'
}
}).then(res => {

let updated = false;
let type = url.split('/').pop().replace('.csv', '');

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

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

if (load) {
}

this.#versions.apache = load.version;
})))).then(async results => {

this.#updateList(load.content);
let updated = false;

updated = true;
if (results[0].status == 'fulfilled' && results[0].value) {

}
let load = await this.#loadApache(results[0].value);

if (load) {

this.#versions.apache = load.version;

this.#updateList(load.content);

updated = true;

}

if (results[1].status == 'fulfilled' && results[1].value) {
}

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

if (load) {
let load = await this.#loadDebian(results[1].value);

this.#versions.debian = load.version;
if (load) {

this.#updateList(load.content);
this.#versions.debian = load.version;

updated = true;
this.#updateList(load.content);

}
updated = true;

}

if (results[2].status == 'fulfilled' && results[2].value) {
}

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

if (load) {
let load = await this.#loadNGINX(results[2].value);

this.#versions.nginx = load.version;
if (load) {

this.#updateList(load.content);
this.#versions.nginx = load.version;

updated = true;
this.#updateList(load.content);

}
updated = true;

}

for (let res of results.slice(3)) {
}

if (res.status == 'fulfilled' && res.value) {
for (let res of results.slice(3)) {

let load = await this.#loadIANA(res.value);
if (res.status == 'fulfilled' && res.value) {

if (load) {
let load = await this.#loadIANA(res.value);

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

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

updated = true;
this.#updateList(load.content);

}
updated = true;

}

}

}

if (updated) {

fs.writeFileSync('mimetypes.json', JSON.stringify(this.#mimeTypes));
fs.writeFileSync('versions.json', JSON.stringify(this.#versions));

}
if (updated) {

});
fs.writeFileSync('mimetypes.json', JSON.stringify(this.#mimeTypes));
fs.writeFileSync('versions.json', JSON.stringify(this.#versions));

} catch (err) {

console.error(err);
}

}
});

}

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

set updateInterval(updateInterval = 86400000) {

if (
typeof updateInterval != 'number'
|| !Number.isFinite(updateInterval)
|| Number.isNaN(updateInterval)
) {
throw new TypeError('Invalid updateInterval')
}

this.#updateInterval = updateInterval;

clearInterval(this.#updateLoop);

if (this.#updateInterval >= 0) {
if (updateInterval >= 0) {

this.#updateLoop = setInterval(async () => {

try {

await this.update();

} catch (err) {

console.error(err);

}

this.#updateLoop = setInterval(this.#update, this.#updateInterval);
}, this.#updateInterval);

}

Expand Down

0 comments on commit e45fcdd

Please sign in to comment.