This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 970
/
Copy pathdataFile.js
153 lines (137 loc) · 5.77 KB
/
dataFile.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict'
const request = require('../js/lib/request')
const fs = require('fs')
const path = require('path')
const urlParse = require('./common/urlParse')
const app = require('electron').app
const appConfig = require('../js/constants/appConfig')
const appActions = require('../js/actions/appActions')
const cachedDataFiles = {}
const storagePath = (url) =>
path.join(app.getPath('userData'), path.basename(urlParse(url).pathname))
const downloadPath = (url) => `${storagePath(url)}.temp`
function downloadSingleFile (resourceName, url, version, force, resolve, reject) {
// console.log('downloading file for: ', resourceName, url)
let headers = {
'Cache-Control': 'no-cache, no-store, must-revalidate',
Pragma: 'no-cache',
Expires: '0'
}
const AppStore = require('../js/stores/appStore')
const etag = AppStore.getState().getIn([resourceName, 'etag'])
if (!force && etag) {
headers['If-None-Match'] = etag
}
const tmpPath = downloadPath(url)
request.requestDataFile(url, headers, tmpPath, reject, (newEtag) => {
fs.rename(tmpPath, storagePath(url), (err) => {
if (err) {
reject('could not rename downloaded file')
} else {
appActions.setResourceETag(resourceName, newEtag)
// console.log('set resource last check: ', resourceName, version, new Date().getTime())
appActions.setResourceLastCheck(resourceName, version, new Date().getTime())
resolve()
}
})
})
}
module.exports.downloadDataFile = (resourceName, url, version, force) => {
return new Promise((resolve, reject) => {
downloadSingleFile(resourceName, url, version, force, resolve, reject)
})
}
module.exports.readDataFile = (resourceName, url) => {
return new Promise((resolve, reject) => {
fs.readFile(storagePath(url), (err, data) => {
if (err || !data || data.length === 0) {
// console.log('rejecting for read for resource:', resourceName)
reject(new Error('unable to read data file'))
} else {
// console.log('resolving for read for resource:', resourceName)
resolve(data)
}
})
})
}
module.exports.shouldRedownloadFirst = (resourceName, version) => {
const AppStore = require('../js/stores/appStore')
const lastCheckDate = AppStore.getState().getIn([resourceName, 'lastCheckDate'])
const lastCheckVersion = AppStore.getState().getIn([resourceName, 'lastCheckVersion'])
return lastCheckVersion !== version ||
(lastCheckDate && (new Date().getTime() - lastCheckDate) > appConfig[resourceName].msBetweenRechecks)
}
/**
* @param {string} resourceName Name of the "extension".
* @param {function(BrowserWindow)} startExtension Function that starts the
* extension listeners.
* @param {function(Buffer|string)} onInitDone function to call when data is downloaded.
* @param {boolean} forceDownload Whether to force the data file to be downloaded. Defaults to false.
* Takes either the data itself as an argument or the pathname on disk of the
* directory where the data was downloaded.
*/
module.exports.init = (resourceName, version, startExtension, onInitDone, forceDownload) => {
version = version || appConfig[resourceName].version
let versionFolder = version
const hasStagedDatFile = [appConfig.resourceNames.ADBLOCK, appConfig.resourceNames.SAFE_BROWSING].includes(resourceName)
if (process.env.NODE_ENV === 'development' && hasStagedDatFile) {
versionFolder = `test/${versionFolder}`
}
const url = appConfig[resourceName].url.replace('{version}', versionFolder)
if (!appConfig[resourceName].enabled) {
return
}
const doneInit = (data) => {
// Make sure we keep a reference to the data since
// it's used directly
// console.log('done init:', resourceName)
cachedDataFiles[resourceName] = data
if (onInitDone(data)) {
startExtension()
} else {
console.error(`Failed to deserialize data file for resource: ${resourceName}`)
fs.unlink(storagePath(url), (err) => {
if (err) {
console.error(`Could not remove unserializable data file for resource: ${resourceName}`)
}
})
}
}
const loadProcess = (resourceName, version) =>
module.exports.readDataFile(resourceName, url)
.then(doneInit)
.catch(() => {
module.exports.downloadDataFile(resourceName, url, version, true)
.then(module.exports.readDataFile.bind(null, resourceName, url))
.then(doneInit)
.catch((err) => {
console.error(`Could not init ${resourceName}`, err || '')
})
})
// console.log('should redownload first? ', resourceName, version, module.exports.shouldRedownloadFirst(resourceName, version))
// If the last check version changes we always want to force a download, otherwise we always don't want to force
const AppStore = require('../js/stores/appStore')
const lastCheckVersion = AppStore.getState().getIn([resourceName, 'lastCheckVersion'])
// console.log('lastCheckVersion, version: ', lastCheckVersion, version, lastCheckVersion !== version)
if (forceDownload || module.exports.shouldRedownloadFirst(resourceName, version)) {
module.exports.downloadDataFile(resourceName, url, version, lastCheckVersion !== version)
.then(loadProcess.bind(null, resourceName, version))
.catch(loadProcess.bind(null, resourceName, version))
} else {
loadProcess(resourceName, version)
}
}
module.exports.debug = (resourceName, details, shouldBlock) => {
/*
if (!shouldBlock) {
}
console.log('-----')
console.log(`${resourceName} should block: `, shouldBlock)
console.log(details.url)
console.log(details.firstPartyUrl)
console.log(details.resourceType)
*/
}