Skip to content

Commit 9ba44de

Browse files
authored
use t.cn by in-gfw (#235)
1 parent 941d3b5 commit 9ba44de

4 files changed

Lines changed: 40 additions & 31 deletions

File tree

lib/short-doc-url.js

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
'use strict';
22
const got = require('got');
33
const locale = require('./locale');
4-
const isCI = require('ci-info').isCI;
5-
const inGFW = locale === 'zh_CN' && (isCI || new Date().getTimezoneOffset() === -480);
6-
const shorturlCache = require(inGFW ? './shorturl_cn.json' : './shorturl.json');
4+
const ci = require('ci-info');
75
const googl = require('goo.gl');
6+
const inGFW = require('in-gfw');
7+
88
// Set a developer key (_required by Google_; see http://goo.gl/4DvFk for more info.)
99
googl.setKey('AIzaSyACqNSi3cybDvDfWMaPyXZEzQ6IeaPehLE');
1010

11+
const isInGFW = ci.isCI && ci.name ? Promise.resolve(locale === 'zh_CN') : inGFW.os();
12+
13+
const shorturlCache = isInGFW.then(inGFW => {
14+
return require(inGFW ? './shorturl_cn.json' : './shorturl.json');
15+
});
16+
1117
function shortDocUrl (error) {
1218
if (!error.doc) {
1319
return error;
@@ -23,20 +29,23 @@ function shortDocUrl (error) {
2329
}
2430

2531
function shortUrl (url) {
26-
const urlLowerCase = url.toLowerCase();
27-
if (shorturlCache[urlLowerCase]) {
28-
return Promise.resolve(shorturlCache[urlLowerCase]);
29-
}
30-
31-
if (inGFW) {
32-
return got(`http://api.t.sina.com.cn/short_url/shorten.json?source=3271760578&url_long=${encodeURIComponent(url)}`, {
33-
json: true,
34-
}).then(result => {
35-
return result.body[0].url_short;
32+
return shorturlCache.then(shorturlCache => {
33+
const urlLowerCase = url.toLowerCase();
34+
if (shorturlCache[urlLowerCase]) {
35+
return shorturlCache[urlLowerCase];
36+
}
37+
return isInGFW.then(inGFW => {
38+
if (inGFW) {
39+
return got(`http://api.t.sina.com.cn/short_url/shorten.json?source=3271760578&url_long=${encodeURIComponent(url)}`, {
40+
json: true,
41+
}).then(result => {
42+
return result.body[0].url_short;
43+
});
44+
} else {
45+
return googl.shorten(url);
46+
}
3647
});
37-
} else {
38-
return googl.shorten(url);
39-
}
48+
});
4049
}
4150

4251
module.exports = function (errors) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"fs-extra": "^5.0.0",
1717
"goo.gl": "^0.1.4",
1818
"got": "^8.3.0",
19+
"in-gfw": "^1.0.0",
1920
"is-windows": "^1.0.2",
2021
"js-yaml": "^3.11.0",
2122
"junit-report-builder": "^1.3.0",

scripts/shorturl.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ Promise.all([
337337
'add',
338338
'lib/*.json',
339339
]);
340+
process.exitCode = 127;
340341
}
341342
});
342343

test/api.test.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,7 @@ describe('API', () => {
2727
if (!errors[0].docShort) {
2828
return;
2929
}
30-
const locale = require('../lib/locale');
31-
if (locale !== 'zh_CN') {
32-
assert.ok(/^https?:\/\/goo\.gl\//.test(errors[0].docShort));
33-
} else {
34-
assert.ok(/^https?:\/\/(goo\.gl|t\.cn)\//.test(errors[0].docShort));
35-
}
30+
assert.ok(/^https?:\/\/(goo\.gl|t\.cn)\//.test(errors[0].docShort));
3631
});
3732
});
3833

@@ -44,28 +39,31 @@ describe('API', () => {
4439
});
4540
});
4641

47-
it('short-doc-url (default)', () => {
42+
it('short-doc-url (in GFW)', () => {
4843
const shortDocUrl = proxyquire('../lib/short-doc-url', {
49-
'./locale': 'en_US',
44+
'./locale': 'zh_CN',
45+
'in-gfw': {
46+
os: () => Promise.resolve(true),
47+
},
5048
});
5149
return shortDocUrl([{
5250
doc: 'https://stylelint.io/user-guide/rules/indentation/',
5351
}]).then(errors => {
54-
assert.equal(errors[0].docShort, 'https://goo.gl/NVQ9aa');
52+
assert.equal(errors[0].docShort, 'http://t.cn/Ro8Mjw5');
5553
});
5654
});
5755

58-
it('short-doc-url (in GWF)', () => {
59-
const getTimezoneOffset = Date.prototype.getTimezoneOffset;
56+
it('short-doc-url (out GFW)', () => {
6057
const shortDocUrl = proxyquire('../lib/short-doc-url', {
61-
'./locale': 'zh_CN',
58+
'./locale': 'en_US',
59+
'in-gfw': {
60+
os: () => Promise.resolve(false),
61+
},
6262
});
63-
Date.prototype.getTimezoneOffset = () => (-480);
6463
return shortDocUrl([{
6564
doc: 'https://stylelint.io/user-guide/rules/indentation/',
6665
}]).then(errors => {
67-
Date.prototype.getTimezoneOffset = getTimezoneOffset;
68-
assert.equal(errors[0].docShort, 'http://t.cn/Ro8Mjw5');
66+
assert.equal(errors[0].docShort, 'https://goo.gl/NVQ9aa');
6967
});
7068
});
7169

0 commit comments

Comments
 (0)