Skip to content

Commit 2778718

Browse files
committed
feat: require serverURLs option for CN apps
1 parent 7f22bf8 commit 2778718

File tree

5 files changed

+42
-68
lines changed

5 files changed

+42
-68
lines changed

demo/leaderboard/leaderboard.js

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
let configs = {};
1+
let configs;
22
try {
3-
configs = JSON.parse(localStorage.getItem('js-sdk-demo/configs')) || {};
3+
configs = JSON.parse(localStorage.getItem('js-sdk-demo/configs')) || {
4+
appId: 'FNHw86LIu6lnFToIEDemKCQl-gzGzoHsz',
5+
appKey: 'DyvpOorH5HK1CVLDqDhb4gNT',
6+
serverURLs: 'https://fnhw86li.lc-cn-n1-shared.com',
7+
};
48
} catch (e) {}
59

6-
const {
7-
appId = 'FNHw86LIu6lnFToIEDemKCQl-gzGzoHsz',
8-
appKey = 'DyvpOorH5HK1CVLDqDhb4gNT',
9-
} = configs;
10-
11-
AV.init({
12-
appId,
13-
appKey,
14-
});
10+
AV.init(configs);
1511

1612
const MAX_RESULTS_COUNT = 12;
1713

@@ -127,14 +123,13 @@ var app = new Vue({
127123
limit: MAX_RESULTS_COUNT,
128124
selectUserKeys: ['username'],
129125
}),
130-
Promise.resolve(AV.User.current()).then(
131-
currentUser =>
132-
currentUser
133-
? leaderboard.getResultsAroundUser(currentUser, {
134-
limit: 3,
135-
selectUserKeys: ['username'],
136-
})
137-
: []
126+
Promise.resolve(AV.User.current()).then(currentUser =>
127+
currentUser
128+
? leaderboard.getResultsAroundUser(currentUser, {
129+
limit: 3,
130+
selectUserKeys: ['username'],
131+
})
132+
: []
138133
),
139134
])
140135
.then(([topRankings, [beforeUserRankings, userRanking]]) => {

demo/live-query/app.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
let configs = {};
1+
let configs;
22
try {
3-
configs = JSON.parse(localStorage.getItem('js-sdk-demo/configs')) || {};
3+
configs = JSON.parse(localStorage.getItem('js-sdk-demo/configs')) || {
4+
appId: 'FNHw86LIu6lnFToIEDemKCQl-gzGzoHsz',
5+
appKey: 'DyvpOorH5HK1CVLDqDhb4gNT',
6+
serverURLs: 'https://fnhw86li.lc-cn-n1-shared.com',
7+
};
48
} catch (e) {}
59

6-
const {
7-
appId = 'FNHw86LIu6lnFToIEDemKCQl-gzGzoHsz',
8-
appKey = 'DyvpOorH5HK1CVLDqDhb4gNT',
9-
} = configs;
10-
11-
AV.init({
12-
appId,
13-
appKey,
14-
});
10+
AV.init(configs);
1511

1612
var Todo = AV.Object.extend('Todo');
1713

@@ -40,8 +36,8 @@ var bind = (subscription, initialStats, onChange) => {
4036
};
4137
const upsert = value => {
4238
let existed = false;
43-
stats = stats.map(
44-
target => (target.id === value.id ? ((existed = true), value) : target)
39+
stats = stats.map(target =>
40+
target.id === value.id ? ((existed = true), value) : target
4541
);
4642
if (!existed) stats = [value, ...stats];
4743
return onChange(stats);

demo/settings/index.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
<div class="pure-control-group">
1313
<label>APP_KEY: </label><input v-model="configs.appKey"></label>
1414
</div>
15+
<div class="pure-control-group">
16+
<label>SERVER_URL: </label><input v-model="configs.serverURLs"></label>
17+
</div>
1518
<div class="pure-controls"><input type="submit" class="pure-button pure-button-primary"></div>
1619
Presets:
1720
<div class="pure-button-group">
@@ -30,10 +33,12 @@
3033
"cn-n1(default)": {
3134
appId: 'FNHw86LIu6lnFToIEDemKCQl-gzGzoHsz',
3235
appKey: 'DyvpOorH5HK1CVLDqDhb4gNT',
36+
serverURLs: 'https://fnhw86li.lc-cn-n1-shared.com',
3337
},
3438
"cn-e1": {
3539
appId: 'Ugf2ahNIhknfzoirYUx0POpQ-9Nh9j0Va',
3640
appKey: 'BlBownlFSWkWGQIkHLrbvmw8',
41+
serverURLs: 'https://ugf2ahni.lc-cn-e1-shared.com',
3742
},
3843
"us-la1": {
3944
appId: 'xCoMNb82GqwCQlEJNEItW5VD-MdYXbMMI',

src/init.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const AppRouter = require('./app-router');
33
const { isNullOrUndefined } = require('./utils');
44
const { extend, isObject } = require('underscore');
55

6+
const isCNApp = appId => appId.slice(-9) !== '-MdYXbMMI';
7+
68
const fillServerURLs = url => ({
79
push: url,
810
stats: url,
@@ -12,20 +14,11 @@ const fillServerURLs = url => ({
1214
});
1315

1416
function getDefaultServerURLs(appId) {
15-
let domain;
16-
const id = appId.slice(0, 8).toLowerCase();
17-
switch (appId.slice(-9)) {
18-
case '-9Nh9j0Va':
19-
// TAB
20-
domain = 'lncldapi.com';
21-
break;
22-
case '-MdYXbMMI':
23-
// US
24-
domain = 'lncldglobal.com';
25-
break;
26-
default:
27-
domain = 'lncld.net';
17+
if (isCNApp(appId)) {
18+
return {};
2819
}
20+
const id = appId.slice(0, 8).toLowerCase();
21+
const domain = 'lncldglobal.com';
2922
return {
3023
push: `https://${id}.push.${domain}`,
3124
stats: `https://${id}.stats.${domain}`,
@@ -82,6 +75,13 @@ AV.init = function init(options, ...params) {
8275
if (!appKey) throw new TypeError('appKey must be a string');
8376
if (process.env.CLIENT_PLATFORM && masterKey)
8477
console.warn('MasterKey is not supposed to be used in browser.');
78+
if (isCNApp(appId)) {
79+
if (!serverURLs) {
80+
throw new TypeError(
81+
`serverURLs option is required for apps from CN region`
82+
);
83+
}
84+
}
8585
AV._config.applicationId = appId;
8686
AV._config.applicationKey = appKey;
8787
AV._config.masterKey = masterKey;

test/test.js

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,11 @@ if (typeof require !== 'undefined') {
1313
// serverURLs: 'https://cn-stg1.leancloud.cn',
1414
// });
1515

16-
function serverURLsWithSuffix(appId, suffix) {
17-
const id = appId.slice(0, 8).toLowerCase();
18-
19-
return {
20-
push: `https://${id}.push.${suffix}`,
21-
stats: `https://${id}.stats.${suffix}`,
22-
engine: `https://${id}.engine.${suffix}`,
23-
api: `https://${id}.api.${suffix}`,
24-
rtm: `https://${id}.rtm.${suffix}`,
25-
};
26-
}
27-
28-
function serverUrls() {
29-
if (process.env.SERVER_URL) {
30-
return process.env.SERVER_URL;
31-
} else if (process.env.SERVER_URL_SUFFIX) {
32-
return serverURLsWithSuffix(process.env.SERVER_URL_SUFFIX);
33-
} else {
34-
return undefined;
35-
}
36-
}
37-
3816
AV.init({
3917
appId: process.env.APPID || '95TNUaOSUd8IpKNW0RSqSEOm-9Nh9j0Va',
4018
appKey: process.env.APPKEY || 'gNAE1iHowdQvV7cqpfCMGaGN',
4119
masterKey: process.env.MASTERKEY || 'ue9M9nqwD4MQNXD3oiN5rAOv',
4220
hookKey: process.env.HOOKKEY || '2iCbUZDgEF0siKxmCn2kVQXV',
43-
serverURLs: serverUrls(),
21+
serverURLs: process.env.SERVER_URL || 'https://95tnuaos.lc-cn-e1-shared.com',
4422
});
4523
AV.setProduction(true);

0 commit comments

Comments
 (0)