Skip to content

Commit 92715b3

Browse files
committed
build: use got module to fetch http resources
1 parent 8aeef59 commit 92715b3

File tree

5 files changed

+26
-36
lines changed

5 files changed

+26
-36
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ jobs:
170170
171171
- name: Remove non-test npm modules
172172
run: |
173-
echo co cogent csv-parse gnode raw-body stream-to-array | \
173+
echo co csv-parse gnode got stream-to-array | \
174174
xargs -n1 npm rm --silent --save-dev
175175
176176
- name: Remove npm module(s) ${{ matrix.npm-rm }}

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"repository": "jshttp/mime-db",
2121
"devDependencies": {
2222
"co": "4.6.0",
23-
"cogent": "1.0.1",
2423
"csv-parse": "4.16.3",
2524
"eslint": "8.32.0",
2625
"eslint-config-standard": "15.0.1",
@@ -30,10 +29,10 @@
3029
"eslint-plugin-promise": "6.1.1",
3130
"eslint-plugin-standard": "4.1.0",
3231
"gnode": "0.1.2",
32+
"got": "11.8.6",
3333
"media-typer": "1.1.0",
3434
"mocha": "10.2.0",
3535
"nyc": "15.1.0",
36-
"raw-body": "2.5.1",
3736
"stream-to-array": "2.3.0"
3837
},
3938
"files": [

scripts/fetch-apache.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
* mime-db
33
* Copyright(c) 2014 Jonathan Ong
4-
* Copyright(c) 2015-2022 Douglas Christopher Wilson
4+
* Copyright(c) 2015-2023 Douglas Christopher Wilson
55
* MIT Licensed
66
*/
77

@@ -11,8 +11,7 @@
1111
* Convert these text files to JSON for browser usage.
1212
*/
1313

14-
var getBody = require('raw-body')
15-
var https = require('https')
14+
var got = require('got')
1615
var writedb = require('./lib/write-db')
1716

1817
/**
@@ -90,14 +89,12 @@ function appendExtensions (obj, extensions) {
9089
}
9190

9291
/**
93-
* Get HTTPS resource.
92+
* Get HTTP resource body.
9493
*/
9594
function get (url, callback) {
96-
https.get(url, function onResponse (res) {
97-
if (res.statusCode !== 200) {
98-
callback(new Error('got status code ' + res.statusCode + ' from ' + URL))
99-
} else {
100-
getBody(res, true, callback)
101-
}
95+
got(url).then(function onResponse (res) {
96+
callback(null, res.body)
97+
}).catch(function onError (err) {
98+
callback(err)
10299
})
103100
}

scripts/fetch-iana.js

+10-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
* mime-db
33
* Copyright(c) 2014 Jonathan Ong
4-
* Copyright(c) 2015-2022 Douglas Christopher Wilson
4+
* Copyright(c) 2015-2023 Douglas Christopher Wilson
55
* MIT Licensed
66
*/
77

@@ -10,8 +10,7 @@
1010
*/
1111

1212
var co = require('co')
13-
var getRawBody = require('raw-body')
14-
var cogent = require('cogent')
13+
var got = require('got')
1514
var parser = require('csv-parse')
1615
var toArray = require('stream-to-array')
1716
var typer = require('media-typer')
@@ -109,14 +108,14 @@ function addTemplateData (data, options) {
109108
}
110109

111110
return function * get () {
112-
var res = yield * cogent('https://www.iana.org/assignments/media-types/' + data.template, { retries: 3 })
111+
var res = yield got('https://www.iana.org/assignments/media-types/' + data.template)
113112
var ref = data.type + '/' + data.name
114113
var rfc = getRfcReferences(data.reference)[0]
115114

116115
if (res.statusCode === 404 && data.template !== ref) {
117116
console.log('template ' + data.template + ' not found, retry as ' + ref)
118117
data.template = ref
119-
res = yield * cogent('https://www.iana.org/assignments/media-types/' + ref, { retries: 3 })
118+
res = yield got('https://www.iana.org/assignments/media-types/' + ref)
120119

121120
// replace the guessed mime
122121
if (res.statusCode === 200) {
@@ -126,7 +125,7 @@ function addTemplateData (data, options) {
126125

127126
if (res.statusCode === 404 && rfc !== undefined) {
128127
console.log('template ' + data.template + ' not found, fetch ' + rfc)
129-
res = yield * cogent('https://tools.ietf.org/rfc/' + rfc.toLowerCase() + '.txt')
128+
res = yield got('https://tools.ietf.org/rfc/' + rfc.toLowerCase() + '.txt')
130129
}
131130

132131
if (res.statusCode === 404) {
@@ -138,12 +137,11 @@ function addTemplateData (data, options) {
138137
throw new Error('got status code ' + res.statusCode + ' from template ' + data.template)
139138
}
140139

141-
var body = yield getTemplateBody(res)
142-
var href = res.urls[0].href
140+
var body = getTemplateBody(res.body)
143141
var mime = extractTemplateMime(body)
144142

145143
// add the template as a source
146-
addSource(data, href)
144+
addSource(data, res.url)
147145

148146
if (mimeEql(mime, data.mime)) {
149147
// use extracted mime
@@ -232,13 +230,13 @@ function extractTemplateExtensions (body) {
232230
}
233231

234232
function * get (type, options) {
235-
var res = yield * cogent('https://www.iana.org/assignments/media-types/' + encodeURIComponent(type) + '.csv', { retries: 3 })
233+
var res = yield got('https://www.iana.org/assignments/media-types/' + encodeURIComponent(type) + '.csv')
236234

237235
if (res.statusCode !== 200) {
238236
throw new Error('got status code ' + res.statusCode + ' from ' + type)
239237
}
240238

241-
var mimes = yield toArray(res.pipe(parser()))
239+
var mimes = yield toArray(parser(res.body))
242240
var headers = mimes.shift().map(normalizeHeader)
243241
var reduceRows = generateRowMapper(headers)
244242
var templates = Object.create(null)
@@ -276,8 +274,7 @@ function * get (type, options) {
276274
})
277275
}
278276

279-
function * getTemplateBody (res) {
280-
var body = yield getRawBody(res, { encoding: 'ascii' })
277+
function getTemplateBody (body) {
281278
var lines = body.split(/\r?\n/)
282279
var slurp = false
283280

scripts/fetch-nginx.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
* mime-db
33
* Copyright(c) 2014 Jonathan Ong
4-
* Copyright(c) 2015-2022 Douglas Christopher Wilson
4+
* Copyright(c) 2015-2023 Douglas Christopher Wilson
55
* MIT Licensed
66
*/
77

@@ -11,8 +11,7 @@
1111
* Convert these text files to JSON for browser usage.
1212
*/
1313

14-
var getBody = require('raw-body')
15-
var https = require('https')
14+
var got = require('got')
1615
var writedb = require('./lib/write-db')
1716

1817
/**
@@ -84,14 +83,12 @@ function appendExtensions (obj, extensions) {
8483
}
8584

8685
/**
87-
* Get HTTPS resource.
86+
* Get HTTP resource body.
8887
*/
8988
function get (url, callback) {
90-
https.get(url, function onResponse (res) {
91-
if (res.statusCode !== 200) {
92-
callback(new Error('got status code ' + res.statusCode + ' from ' + URL))
93-
} else {
94-
getBody(res, true, callback)
95-
}
89+
got(url).then(function onResponse (res) {
90+
callback(null, res.body)
91+
}).catch(function onError (err) {
92+
callback(err)
9693
})
9794
}

0 commit comments

Comments
 (0)