File indexing completed on 2024-11-17 04:55:14

0001 /* This Source Code Form is subject to the terms of the Mozilla Public
0002  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
0003  * You can obtain one at http://mozilla.org/MPL/2.0/. */
0004 
0005 const fs = require('fs')
0006 const s3 = require('s3-client')
0007 const commander = require('commander')
0008 const path = require('path')
0009 const dataFileVersion = 4
0010 
0011 const client = s3.createClient({
0012   maxAsyncS3: 20,
0013   s3RetryCount: 3,
0014   s3RetryDelay: 1000,
0015   multipartUploadThreshold: 20971520,
0016   multipartUploadSize: 15728640,
0017   // See: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html#constructor-property
0018   s3Options: {}
0019 })
0020 
0021 const uploadFile = (key, filePath, filename) => {
0022   return new Promise((resolve, reject) => {
0023     var params = {
0024       localFile: filePath,
0025       // See: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property
0026       s3Params: {
0027         Bucket: 'adblock-data',
0028         Key: `${key}/${filename}`,
0029         ACL: 'public-read'
0030       }
0031     }
0032     var uploader = client.uploadFile(params)
0033     process.stdout.write(`Started uploading to: ${params.s3Params.Key}... `)
0034     uploader.on('error', function (err) {
0035       reject(new Error(`Unable to upload, do you have ~/.aws/credentials filled out? ${err}`))
0036     })
0037     uploader.on('end', function (params) {
0038       console.log('completed')
0039       resolve()
0040     })
0041   })
0042 }
0043 
0044 commander
0045   .option('-d, --dat [dat]', 'file path of the adblock .dat file to upload')
0046   .option('-p, --prod', 'whether the upload is for prod, if not specified uploads to the test location')
0047   .parse(process.argv)
0048 
0049 // Queue up all the uploads one at a time to easily spot errors
0050 let p = Promise.resolve()
0051 const date = new Date().toISOString().split('.')[0]
0052 
0053 if (commander.dat) {
0054   if (commander.prod) {
0055     p = p.then(uploadFile.bind(null, dataFileVersion, commander.dat, path.basename(commander.dat)))
0056     p = p.then(uploadFile.bind(null, `backups/${date}`, commander.dat, path.basename(commander.dat)))
0057   } else {
0058     p = p.then(uploadFile.bind(null, 'test', commander.dat, path.basename(commander.dat)))
0059   }
0060 } else {
0061   const dataFilenames = fs.readdirSync('out')
0062   dataFilenames.forEach((filename) => {
0063     if (commander.prod) {
0064       p = p.then(uploadFile.bind(null, dataFileVersion, `out/${filename}`, filename))
0065       p = p.then(uploadFile.bind(null, `backups/${date}`, `out/${filename}`, filename))
0066     } else {
0067       p = p.then(uploadFile.bind(null, `test/${dataFileVersion}`, `out/${filename}`, filename))
0068     }
0069   })
0070 }
0071 p = p.catch((e) => {
0072   console.error('A problem was encounterd', e)
0073   process.exit(1)
0074 })
0075 
0076 process.on('uncaughtException', (err) => {
0077   console.error('Caught exception:', err)
0078   process.exit(1)
0079 })
0080 
0081 process.on('unhandledRejection', (err) => {
0082   console.error('Unhandled rejection:', err)
0083   process.exit(1)
0084 })