Skip to content

Commit 0268e67

Browse files
committed
build: convert apache and nginx scripts to callbacks
1 parent 1610546 commit 0268e67

File tree

3 files changed

+43
-51
lines changed

3 files changed

+43
-51
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
},
4848
"scripts": {
4949
"build": "node scripts/build",
50-
"fetch": "gnode scripts/fetch-apache && gnode scripts/fetch-iana && gnode scripts/fetch-nginx",
50+
"fetch": "node scripts/fetch-apache && gnode scripts/fetch-iana && node scripts/fetch-nginx",
5151
"lint": "eslint .",
5252
"test": "mocha --reporter spec --bail --check-leaks test/",
5353
"test-cov": "nyc --reporter=html --reporter=text npm test",

scripts/fetch-apache.js

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
'use strict'
12

23
/**
34
* Convert these text files to JSON for browser usage.
45
*/
56

6-
global.Promise = global.Promise || loadBluebird()
7-
8-
var co = require('co')
9-
var cogent = require('cogent')
7+
var getBody = require('raw-body')
8+
var https = require('https')
109
var writedb = require('./lib/write-db')
1110

1211
/**
@@ -19,24 +18,22 @@ var writedb = require('./lib/write-db')
1918
* We could also just remove all lines that start with `#` if we want to make the JSON files smaller
2019
* and ignore all mime types without associated extensions.
2120
*/
22-
var typeLineRegExp = /^(?:# )?([\w-]+\/[\w+.-]+)((?:\s+[\w-]+)*)$/gm
21+
var TYPE_LINE_REGEXP = /^(?:# )?([\w-]+\/[\w+.-]+)((?:\s+[\w-]+)*)$/gm
2322

24-
co(function * () {
25-
var url = 'http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types'
26-
var res = yield * cogent(url, {
27-
string: true
28-
})
23+
/**
24+
* URL for the mime.types file in the Apache HTTPD project source.
25+
*/
26+
var URL = 'https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types'
2927

30-
if (res.statusCode !== 200) {
31-
throw new Error('got status code ' + res.statusCode + ' from ' + url)
32-
}
28+
get(URL, function onResponse (err, body) {
29+
if (err) throw err
3330

3431
var json = {}
3532
var match = null
3633

37-
typeLineRegExp.index = 0
34+
TYPE_LINE_REGEXP.index = 0
3835

39-
while ((match = typeLineRegExp.exec(res.text))) {
36+
while ((match = TYPE_LINE_REGEXP.exec(body))) {
4037
var mime = match[1]
4138

4239
if (mime.substr(-8) === '/example') {
@@ -54,7 +51,7 @@ co(function * () {
5451
}
5552

5653
writedb('src/apache-types.json', json)
57-
}).then()
54+
})
5855

5956
/**
6057
* Append an extension to an object.
@@ -86,15 +83,14 @@ function appendExtensions (obj, extensions) {
8683
}
8784

8885
/**
89-
* Load the Bluebird promise.
86+
* Get HTTPS resource.
9087
*/
91-
function loadBluebird () {
92-
var Promise = require('bluebird')
93-
94-
// Silence all warnings
95-
Promise.config({
96-
warnings: false
88+
function get (url, callback) {
89+
https.get(url, function onResponse (res) {
90+
if (res.statusCode !== 200) {
91+
callback(new Error('got status code ' + res.statusCode + ' from ' + URL))
92+
} else {
93+
getBody(res, true, callback)
94+
}
9795
})
98-
99-
return Promise
10096
}

scripts/fetch-nginx.js

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,34 @@
1+
'use strict'
12

23
/**
34
* Convert these text files to JSON for browser usage.
45
*/
56

6-
global.Promise = global.Promise || loadBluebird()
7-
8-
var co = require('co')
9-
var cogent = require('cogent')
7+
var getBody = require('raw-body')
8+
var https = require('https')
109
var writedb = require('./lib/write-db')
1110

1211
/**
1312
* Mime types and associated extensions are stored in the form:
1413
*
1514
* <type> <ext> <ext> <ext>;
1615
*/
17-
var typeLineRegExp = /^\s*([\w-]+\/[\w+.-]+)((?:\s+[\w-]+)*);\s*$/gm
16+
var TYPE_LINE_REGEXP = /^\s*([\w-]+\/[\w+.-]+)((?:\s+[\w-]+)*);\s*$/gm
1817

19-
co(function * () {
20-
var url = 'http://hg.nginx.org/nginx/raw-file/default/conf/mime.types'
21-
var res = yield * cogent(url, {
22-
string: true
23-
})
18+
/**
19+
* URL for the mime.types file in the NGINX project source.
20+
*/
21+
var URL = 'https://hg.nginx.org/nginx/raw-file/default/conf/mime.types'
2422

25-
if (res.statusCode !== 200) {
26-
throw new Error('got status code ' + res.statusCode + ' from ' + url)
27-
}
23+
get(URL, function onResponse (err, body) {
24+
if (err) throw err
2825

2926
var json = {}
3027
var match = null
3128

32-
typeLineRegExp.index = 0
29+
TYPE_LINE_REGEXP.index = 0
3330

34-
while ((match = typeLineRegExp.exec(res.text))) {
31+
while ((match = TYPE_LINE_REGEXP.exec(body))) {
3532
var mime = match[1]
3633

3734
// parse the extensions
@@ -45,7 +42,7 @@ co(function * () {
4542
}
4643

4744
writedb('src/nginx-types.json', json)
48-
}).then()
45+
})
4946

5047
/**
5148
* Append an extension to an object.
@@ -77,15 +74,14 @@ function appendExtensions (obj, extensions) {
7774
}
7875

7976
/**
80-
* Load the Bluebird promise.
77+
* Get HTTPS resource.
8178
*/
82-
function loadBluebird () {
83-
var Promise = require('bluebird')
84-
85-
// Silence all warnings
86-
Promise.config({
87-
warnings: false
79+
function get (url, callback) {
80+
https.get(url, function onResponse (res) {
81+
if (res.statusCode !== 200) {
82+
callback(new Error('got status code ' + res.statusCode + ' from ' + URL))
83+
} else {
84+
getBody(res, true, callback)
85+
}
8886
})
89-
90-
return Promise
9187
}

0 commit comments

Comments
 (0)