({chrome_port:port, mode} = {})
| 214 | |
| 215 | // main |
| 216 | async function collect({chrome_port:port, mode} = {}) { |
| 217 | try { |
| 218 | console.log('Starting collect'); |
| 219 | const {library_path} = args; |
| 220 | const exitHandlers = []; |
| 221 | process.on('beforeExit', runHandlers); |
| 222 | process.on('SIGUSR2', code => runHandlers(code, 'SIGUSR2', {exit: true})); |
| 223 | process.on('exit', code => runHandlers(code, 'exit', {exit: true})); |
| 224 | State.connection = State.connection || await connect({port}); |
| 225 | console.log('Connection established'); |
| 226 | State.onExit = { |
| 227 | addHandler(h) { |
| 228 | exitHandlers.push(h); |
| 229 | } |
| 230 | }; |
| 231 | const {send, on, close} = State.connection; |
| 232 | //const DELAY = 100; // 500 ? |
| 233 | Close = close; |
| 234 | |
| 235 | let requestStage; |
| 236 | |
| 237 | console.log('Loading files...'); |
| 238 | await loadFiles(); |
| 239 | |
| 240 | clearSavers(); |
| 241 | |
| 242 | Mode = mode; |
| 243 | console.log({Mode}); |
| 244 | if ( Mode == 'save' || Mode == 'select' ) { |
| 245 | requestStage = "Response"; |
| 246 | // in case we get a updateBasePath call before an interval |
| 247 | // and we don't clear it in time, leading us to erroneously save the old |
| 248 | // cache to the new path, we always used our saved copy |
| 249 | State.saver = setInterval(() => saveCache(State.SavedCacheFilePath), 17000); |
| 250 | // we use timeout because we can trigger this ourself |
| 251 | // so in order to not get a race condition (overlapping calls) we ensure |
| 252 | // only 1 call at 1 time |
| 253 | State.indexSaver = setTimeout(() => saveIndex(State.SavedIndexFilePath), 11001); |
| 254 | State.ftsIndexSaver = setTimeout(() => saveFTS(State.SavedFTSIndexDirPath), 31001); |
| 255 | } else if ( Mode == 'serve' ) { |
| 256 | requestStage = "Request"; |
| 257 | clearSavers(); |
| 258 | } else { |
| 259 | throw new TypeError(`Must specify mode, and must be one of: save, serve, select`); |
| 260 | } |
| 261 | |
| 262 | on("Target.targetInfoChanged", attachToTarget); |
| 263 | on("Target.targetInfoChanged", updateTargetInfo); |
| 264 | on("Target.targetInfoChanged", indexURL); |
| 265 | on("Target.attachedToTarget", installForSession); |
| 266 | on("Page.loadEventFired", reloadIfNotLive); |
| 267 | on("Fetch.requestPaused", cacheRequest); |
| 268 | on("Runtime.consoleAPICalled", handleMessage); |
| 269 | |
| 270 | await send("Target.setDiscoverTargets", {discover:true}); |
| 271 | await send("Target.setAutoAttach", {autoAttach:true, waitForDebuggerOnStart:false, flatten: true}); |
| 272 | await send("Security.setIgnoreCertificateErrors", {ignore:true}); |
| 273 | await send("Fetch.enable", { |
no test coverage detected