-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathembedder.js
269 lines (222 loc) · 8.54 KB
/
embedder.js
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
const yauzl = require("yauzl");
let CLIENT_VERSION, REPO, PROOF_KEYS, NOTIFIER, PROMPTER, FILES, LOCAL_TESTING, SIGNATURE_CHECKER, SHA256, PROGRESS_EVENT;
let ETAGS = {};
const FILE_SERVICE_FNS = ['getFilesForDirectory', 'getDefaultPath', 'saveFile', 'openFile', 'existsOrMkdir', 'exists'];
const MAX_TRIES = 5;
const ERR_TITLE = 'Scatter Check Failure';
const API_ERR = `Scatter could not check GitHub for new releases.`
const HASH_ERR = `The signature used to validate this zip file does not match the hash of the zip file.`
const saveSource = async (filename, file) => {
let sourcePath = `${await FILES.getDefaultPath()}/cached_sources`;
if(filename.indexOf('/') > -1){
const parts = filename.split('/');
filename = parts.pop();
sourcePath += '/' + parts.join('/');
}
await FILES.existsOrMkdir(sourcePath);
return FILES.saveFile(sourcePath, filename, file);
};
const checkSignature = async (hashed, signed) => {
const recovered = await SIGNATURE_CHECKER(hashed, signed);
let proven = false;
for(let i = 0; i < PROOF_KEYS.length; i++){
try {
if(recovered === PROOF_KEYS[i]) {
proven = true;
break;
}
} catch(e){}
}
return proven;
}
const filterFiles = x => (x.indexOf('.js') > -1 || x.indexOf('.html') > -1 || x.indexOf('.css') > -1) && x.indexOf('.etags') === -1 && x.indexOf('.version') === -1;
const sendProgress = (msg) => {
if(PROGRESS_EVENT) PROGRESS_EVENT(msg);
else console.log('PROGRESS_EVENT', msg);
};
const getReleaseInfo = async (lastModified) => {
// If fetching from host fails, trying to fetch directly from github releases.
return fetch(`https://api.github.com/repos/GetScatter/${REPO}/releases/latest`, {
headers:{ "If-Modified-Since":lastModified ? lastModified.trim() : null }
}).then(async x => {
if(x.status === 304) return {notModified:true};
if(x.status !== 200) return null;
return {
newLastModified:x.headers.get('last-modified'),
json:await x.json(),
notModified:false,
}
}).catch(err => {
console.error(err);
return null;
});
}
class Embedder {
static init(
clientVersion,
repo = 'Bridge',
proofKeys,
fileService,
sha256er,
notifier = (title, text) => console.log('Notifier: ', title, text),
prompter = (title, text) => console.log('Prompt: ', title, text),
signatureChecker = (hashed, signed) => console.log('Signature Checker: ', hashed, signed),
progressEvent = null,
localTesting = false,
) {
CLIENT_VERSION = clientVersion;
REPO = repo;
PROOF_KEYS = proofKeys;
FILES = fileService;
SHA256 = sha256er;
NOTIFIER = notifier;
PROMPTER = prompter;
SIGNATURE_CHECKER = signatureChecker;
// Optionals
PROGRESS_EVENT = progressEvent;
LOCAL_TESTING = localTesting;
if(!PROOF_KEYS.length) throw new Error('You must include Proofing Keys');
if(!FILE_SERVICE_FNS.every(prop => typeof FILES[prop] === 'function')) throw new Error(`fileService must have the following methods: ${FILE_SERVICE_FNS}`);
if(!SHA256 || typeof SHA256 !== 'function') throw new Error('Sha256 must be a function.');
}
// Removes old files.
static async removeOldFiles(){
const localFiles = await FILES.getFilesForDirectory(`${await FILES.getDefaultPath()}/cached_sources`).catch(() => []);
await Promise.all(localFiles.map(async filename => {
return FILES.removeFile(`${await FILES.getDefaultPath()}/cached_sources/${filename}`);
}));
}
// Checks if the user has a timestamp file locally at all,
// which is always the last file that is cached.
static async hasLocalVersion(){
if(LOCAL_TESTING) return false;
return FILES.openFile(`${await FILES.getDefaultPath()}/cached_sources/release.tag`).then(x => !!x).catch(() => null)
}
static async lastModified(){
if(LOCAL_TESTING) return false;
return FILES.openFile(`${await FILES.getDefaultPath()}/cached_sources/modified.tag`).catch(() => null)
}
static async getZip(info){
return new Promise(async (resolve, reject) => {
const asset = info.assets.find(x => x.name.indexOf('.zip') > -1);
if(!asset){
NOTIFIER(ERR_TITLE, `It looks like the new version does not yet have an asset file. Please try again soon.`);
return resolve(null);
}
sendProgress(`Downloading user-interface <br> <p>This may take a few minutes.</p>`);
const downloadUrl = asset.browser_download_url;
const [repoTag, tagName, signature, ext] = downloadUrl.split('/')[downloadUrl.split('/').length-1].split('.');
const buf = await fetch(downloadUrl, { headers:{ 'Content-type':'application/zip' } })
.then(x => x.buffer()).catch(err => console.error(err));
if(!buf) return resolve(null);
const hash = SHA256(buf);
if(!await checkSignature(hash, signature)) {
NOTIFIER(ERR_TITLE, HASH_ERR);
return resolve(null);
}
sendProgress('Finished downloading new version!');
return resolve(buf);
})
}
static async unzip(buf){
return new Promise(async (resolve, reject) => {
yauzl.fromBuffer(buf, {lazyEntries:true}, (err, zipfile) => {
if (err) return resolve(console.error(err));
zipfile.readEntry();
zipfile.on("entry", (entry) => {
try {
// DIR
if (/\/$/.test(entry.fileName)) zipfile.readEntry();
// FILE
else zipfile.openReadStream(entry, (err, stream) => {
if (err) return resolve(console.error(err));
let bufs = [];
stream.on('data',data => bufs.push(data));
stream.on("end", async () => {
const buf = Buffer.concat(bufs);
await saveSource(entry.fileName, buf);
sendProgress(`Unpacking ${zipfile.entriesRead} of ${zipfile.entryCount}`);
if(zipfile.entriesRead === zipfile.entryCount) return resolve(true);
else zipfile.readEntry();
});
});
} catch(e){
console.error(e);
}
});
});
})
}
static async loadManualZipFile(zipBuffer, signature, releaseTag){
if(!zipBuffer) return false;
sendProgress('Checking zip file.');
const hash = SHA256(zipBuffer);
if(!await checkSignature(hash, signature)) {
NOTIFIER(ERR_TITLE, HASH_ERR);
return false;
}
sendProgress('Loading zip file.');
await Embedder.removeOldFiles();
await Embedder.unzip(zipBuffer);
await saveSource('release.tag', releaseTag);
const date = new Date();
const DAYS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
const hour = date.getUTCHours(),
minute = date.getUTCMinutes(),
second = date.getUTCSeconds(),
hourFormatted = hour < 12 ? "0" + hour % 12 || 12 : hour % 12 || 12,
minuteFormatted = minute < 10 ? "0" + minute : minute,
morning = hour < 12 ? "am" : "pm";
// Just faking a last modification header.
const lastModified = `${DAYS[date.getDay()]}, ${date.getDate()} ${MONTHS[date.getMonth()]} ${date.getFullYear()} ${hourFormatted}:${minuteFormatted}:${second} GMT`;
await saveSource('modified.tag', lastModified);
sendProgress('Zip file loaded successfully!');
return true;
}
static async check(){
let hasEmbed = false;
const hasLocalVersion = await Embedder.hasLocalVersion();
sendProgress('Checking for new versions.');
const lastModified = hasLocalVersion ? await Embedder.lastModified() : null;
const releaseInfo = await getReleaseInfo(lastModified);
if(!releaseInfo){
NOTIFIER(ERR_TITLE, API_ERR);
return false;
}
const {json:latestRelease, newLastModified, notModified} = releaseInfo;
if(notModified && hasLocalVersion) return true;
const updateLocalFiles = async () => {
const zipBuffer = await Embedder.getZip(latestRelease);
if(!zipBuffer){
hasEmbed = await PROMPTER(
'There was an issue getting the latest embed version.',
'Would you like to keep using your locally cached version of Scatter which has already been verified previously?'
);
} else {
await Embedder.removeOldFiles();
await Embedder.unzip(zipBuffer);
await saveSource('release.tag', latestRelease.tag_name);
await saveSource('modified.tag', newLastModified);
hasEmbed = true;
}
};
const versionAvailable = async () => {
if(LOCAL_TESTING) return true;
const localReleaseTag = await FILES.openFile(`${await FILES.getDefaultPath()}/cached_sources/release.tag`).catch(() => null);
return latestRelease.tag_name.trim() !== localReleaseTag.trim();
};
if(!hasLocalVersion) await updateLocalFiles();
else {
if (await versionAvailable()) {
if (await PROMPTER(
'An updated Scatter is available.',
'There is an updated version of Scatter available. Do you want to update automatically?'
)) await updateLocalFiles();
else hasEmbed = true;
} else hasEmbed = true;
}
return hasEmbed;
}
}
module.exports = Embedder;