Skip to content

Commit

Permalink
feat(Standalone): Support China mirror for binary downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Apr 6, 2020
1 parent 8d4ed6d commit 8e85fe6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
10 changes: 6 additions & 4 deletions lib/plugins/executable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const streamPromise = require('stream-promise');
const fse = BbPromise.promisifyAll(require('fs-extra'));
const isStandaloneExecutable =
require('../../utils/isStandaloneExecutable') && process.platform !== 'win32';
const isInChina = require('../../utils/isInChina');
const currentVersion = require('../../../package').version;
const fetch = require('node-fetch');

Expand Down Expand Up @@ -78,10 +79,11 @@ module.exports = class Executable {
}
})();
this.serverless.cli.log('Downloading new version...');
return fetch(
`https://github.com/serverless/serverless/releases/download/${tagName}/` +
`serverless-${platform}-${arch}`
)
const executableUrl = isInChina
? `https://binary.serverlesscloud.cn/${tagName}/serverless-${platform}-${arch}`
: `https://github.com/serverless/serverless/releases/download/${tagName}/` +
`serverless-${platform}-${arch}`;
return fetch(executableUrl)
.then(response => {
if (!response.ok) {
throw new this.serverless.classes.Error(
Expand Down
10 changes: 10 additions & 0 deletions lib/utils/isInChina.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

if (process.env.SLS_GEO_LOCATION === 'cn') {
module.exports = true;
return;
}

module.exports = new Intl.DateTimeFormat('en', { timeZoneName: 'long' })
.format()
.includes('China Standard Time');
17 changes: 17 additions & 0 deletions lib/utils/isInChina.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const { expect } = require('chai');
const requireUncached = require('ncjsm/require-uncached');

describe('isInChina', () => {
it('should return boolean', () => {
expect(typeof require('./isInChina')).to.equal('boolean');
});

it('should support SLS_GEO_LOCATION', () => {
process.env.SLS_GEO_LOCATION = 'cn';
expect(requireUncached(require.resolve('./isInChina'), () => require('./isInChina'))).to.equal(
true
);
});
});
12 changes: 11 additions & 1 deletion scripts/pkg/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,17 @@ BINARIES_DIR_PATH=$HOME/.serverless/bin
BINARY_PATH=$BINARIES_DIR_PATH/serverless
mkdir -p $BINARIES_DIR_PATH
printf "$yellow Downloading binary...$reset\n"
curl -L -o $BINARY_PATH https://github.com/serverless/serverless/releases/download/$LATEST_TAG/serverless-$PLATFORM-$ARCH

TIMEZONE_OFFSET=`date +"%Z %z"`
if [[ $TIMEZONE_OFFSET == "CST +0800" ]]
then
# In China download from location in China (Github API download is slow and times out)
BINARY_URL=https://binary.serverlesscloud.cn/$LATEST_TAG/serverless-$PLATFORM-$ARCH
else
BINARY_URL=https://github.com/serverless/serverless/releases/download/$LATEST_TAG/serverless-$PLATFORM-$ARCH
fi

curl -L -o $BINARY_PATH $BINARY_URL
chmod +x $BINARY_PATH

# Ensure aliases
Expand Down

0 comments on commit 8e85fe6

Please sign in to comment.