-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
176 lines (162 loc) · 5.5 KB
/
index.js
File metadata and controls
176 lines (162 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*!
* Main
* Copyright(c) 2009-2017 Marcus Ma
* E-mail:maji1991@sina.com
* GitHub : https://github.com/MarcusMa
* MIT Licensed
*/
'use strict';
/**
* Module dependencies.
*/
const express = require('express');
const bodyParser = require('body-parser');
/**
* Custom module dependencies.
*/
const Log = require('./src/utils/Log');
const path = require('path');
const BusinessManager = require('./src/service/BusinessManager');
const PatchManager = require('./src/service/PatchManager');
const BusinessInfo = require('./src/entity/BusinessInfo');
const PatchVersionInfo = require('./src/entity/PatchVersionInfo');
/**
* Constants
*/
const TAG = "RN_CODE_PUSH_SERVER";
const SERVER_PORT = 8888;
/**
* Services
*/
var businessManager = null;
var patchManger = null;
const responseJson = {
"success": 1,
"data": null,
"msg": null
};
/** Server Settings */
const app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json({
limit: '1mb'
}));
app.use(bodyParser.urlencoded({
extended: true
}));
/**
* For test the server is running successfully.
*/
app.get('/test', function (req, res) {
Log.i(TAG, "/test with params" + req.query);
res.send("Access successful");
});
/**
* For check there is a new patch can be used.
*/
app.post('/checkForUpdate', function (req, res) {
Log.i(TAG, "Call /checkForUpdate");
// Log.i(TAG,JSON.stringify(req.params));
Log.i(TAG, "Request body:");
Log.i(TAG, JSON.stringify(req.body));
var reqBody = req.body;
var clientInfoList = [];
clientInfoList = reqBody.localBusinessList;
var remoteBuinessList = [];
if (clientInfoList instanceof Array) {
clientInfoList.forEach(function (tmp) {
let businessInfo = businessManager.getBusinessInfoById(tmp.id);
if (null != businessInfo) {
let respData = {
id: tmp.id,
verifyHashCode: ""
};
// check the hashcode.
if (businessInfo.isPatchVersionExist(tmp.localPackageHashCode)) {
Log.i(TAG, "exitVersion : " + tmp.localPackageHashCode);
respData.verifyHashCode = tmp.localPackageHashCode;
} else {
Log.i(TAG, "No Such Version " + tmp.localPackageHashCode);
respData.verifyHashCode = "";
}
// check the new versions
let latestPatchInfo = businessInfo.getLatestPatchInfo();
if (latestPatchInfo.hashCode == tmp.localPackageHashCode) {
// no versions
} else {
respData.latestPatch = {
hashCode: latestPatchInfo.hashCode,
downloadUrl: latestPatchInfo.getDownloadUrl()
};
}
// add to the response
remoteBuinessList.push(respData);
}
});
}
// 处理未传入的businessId
businessManager.businessMap.forEach(function (tmp) {
let isExist = false;
if (clientInfoList instanceof Array) {
for (let j = 0; j < clientInfoList.length; j++) {
if (clientInfoList[j].businessId == tmp.businessId) {
isExist = true;
break;
}
}
}
if (!isExist) {
let respData = {
id: tmp.businessId,
verifyHashCode: ""
};
let latestPatchInfo = tmp.getLatestPatchInfo();
respData.latestPatch = {
hashCode: latestPatchInfo.hashCode,
downloadUrl: latestPatchInfo.getDownloadUrl()
};
remoteBuinessList.push(respData);
}
});
var respJson = responseJson;
respJson.data = remoteBuinessList;
respJson.msg = " Success ";
Log.i(TAG,"Response: " + JSON.stringify(respJson));
res.send(respJson);
});
/**
* Start the server
*/
app.listen(SERVER_PORT, () => {
onLogProjectInfo();
Log.i(TAG, 'Server Listening at port :' + SERVER_PORT);
onInit();
});
function onInit() {
Log.i(TAG, ".... Initilization ....");
businessManager = new BusinessManager;
patchManger = new PatchManager();
patchManger.init(businessManager);
/**
* NOTICE: Add your self BusinessInfo Object Like this:
* var bus = new BusinessInfo({businessId},{businessName},{businessTag})
*/
var mybuss = new BusinessInfo("AAF047B7-E816-2AE0-949A-D5FB4CE40245", "myBusiness", "tag1");
businessManager.add(mybuss);
/**
* NOTICE: PatchManager startPatch method must be called after your all BusinessInfos has been added to the BusinessManger Object.
*/
patchManger.startPatch();
}
function onLogProjectInfo() {
Log.i(TAG, "##################################################################################");
Log.i(TAG, '# Author : Marcus Ma');
Log.i(TAG, '# E-mail : maji1991@sina.com');
Log.i(TAG, "# GitHub : https://github.com/MarcusMa")
Log.i(TAG, "##################################################################################");
Log.i(TAG, "# Demo Server for React Native Hot Code Push");
Log.i(TAG, "# See: https://github.com/MarcusMa/simple-react-native-hot-code-push-server");
Log.i(TAG, "# For Client Demo, please");
Log.i(TAG, "# See: https://github.com/MarcusMa/simple-react-native-hot-code-push");
Log.i(TAG, "##################################################################################");
}