Skip to content

Commit

Permalink
use t.cn by in-gfw (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
gucong3000 authored Apr 8, 2018
1 parent 941d3b5 commit 9ba44de
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 31 deletions.
41 changes: 25 additions & 16 deletions lib/short-doc-url.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
'use strict';
const got = require('got');
const locale = require('./locale');
const isCI = require('ci-info').isCI;
const inGFW = locale === 'zh_CN' && (isCI || new Date().getTimezoneOffset() === -480);
const shorturlCache = require(inGFW ? './shorturl_cn.json' : './shorturl.json');
const ci = require('ci-info');
const googl = require('goo.gl');
const inGFW = require('in-gfw');

// Set a developer key (_required by Google_; see http://goo.gl/4DvFk for more info.)
googl.setKey('AIzaSyACqNSi3cybDvDfWMaPyXZEzQ6IeaPehLE');

const isInGFW = ci.isCI && ci.name ? Promise.resolve(locale === 'zh_CN') : inGFW.os();

const shorturlCache = isInGFW.then(inGFW => {
return require(inGFW ? './shorturl_cn.json' : './shorturl.json');
});

function shortDocUrl (error) {
if (!error.doc) {
return error;
Expand All @@ -23,20 +29,23 @@ function shortDocUrl (error) {
}

function shortUrl (url) {
const urlLowerCase = url.toLowerCase();
if (shorturlCache[urlLowerCase]) {
return Promise.resolve(shorturlCache[urlLowerCase]);
}

if (inGFW) {
return got(`http://api.t.sina.com.cn/short_url/shorten.json?source=3271760578&url_long=${encodeURIComponent(url)}`, {
json: true,
}).then(result => {
return result.body[0].url_short;
return shorturlCache.then(shorturlCache => {
const urlLowerCase = url.toLowerCase();
if (shorturlCache[urlLowerCase]) {
return shorturlCache[urlLowerCase];
}
return isInGFW.then(inGFW => {
if (inGFW) {
return got(`http://api.t.sina.com.cn/short_url/shorten.json?source=3271760578&url_long=${encodeURIComponent(url)}`, {
json: true,
}).then(result => {
return result.body[0].url_short;
});
} else {
return googl.shorten(url);
}
});
} else {
return googl.shorten(url);
}
});
}

module.exports = function (errors) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"fs-extra": "^5.0.0",
"goo.gl": "^0.1.4",
"got": "^8.3.0",
"in-gfw": "^1.0.0",
"is-windows": "^1.0.2",
"js-yaml": "^3.11.0",
"junit-report-builder": "^1.3.0",
Expand Down
1 change: 1 addition & 0 deletions scripts/shorturl.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ Promise.all([
'add',
'lib/*.json',
]);
process.exitCode = 127;
}
});

Expand Down
28 changes: 13 additions & 15 deletions test/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ describe('API', () => {
if (!errors[0].docShort) {
return;
}
const locale = require('../lib/locale');
if (locale !== 'zh_CN') {
assert.ok(/^https?:\/\/goo\.gl\//.test(errors[0].docShort));
} else {
assert.ok(/^https?:\/\/(goo\.gl|t\.cn)\//.test(errors[0].docShort));
}
assert.ok(/^https?:\/\/(goo\.gl|t\.cn)\//.test(errors[0].docShort));
});
});

Expand All @@ -44,28 +39,31 @@ describe('API', () => {
});
});

it('short-doc-url (default)', () => {
it('short-doc-url (in GFW)', () => {
const shortDocUrl = proxyquire('../lib/short-doc-url', {
'./locale': 'en_US',
'./locale': 'zh_CN',
'in-gfw': {
os: () => Promise.resolve(true),
},
});
return shortDocUrl([{
doc: 'https://stylelint.io/user-guide/rules/indentation/',
}]).then(errors => {
assert.equal(errors[0].docShort, 'https://goo.gl/NVQ9aa');
assert.equal(errors[0].docShort, 'http://t.cn/Ro8Mjw5');
});
});

it('short-doc-url (in GWF)', () => {
const getTimezoneOffset = Date.prototype.getTimezoneOffset;
it('short-doc-url (out GFW)', () => {
const shortDocUrl = proxyquire('../lib/short-doc-url', {
'./locale': 'zh_CN',
'./locale': 'en_US',
'in-gfw': {
os: () => Promise.resolve(false),
},
});
Date.prototype.getTimezoneOffset = () => (-480);
return shortDocUrl([{
doc: 'https://stylelint.io/user-guide/rules/indentation/',
}]).then(errors => {
Date.prototype.getTimezoneOffset = getTimezoneOffset;
assert.equal(errors[0].docShort, 'http://t.cn/Ro8Mjw5');
assert.equal(errors[0].docShort, 'https://goo.gl/NVQ9aa');
});
});

Expand Down

0 comments on commit 9ba44de

Please sign in to comment.