|
| 1 | +/** |
| 2 | + * @file upload.js |
| 3 | + * @author zttonly <[email protected]> |
| 4 | + */ |
| 5 | + |
| 6 | +const md5 = require('md5'); |
| 7 | +const upload = require('./index'); |
| 8 | +const fsrUpload = require('./fsr'); |
| 9 | + |
| 10 | +/* |
| 11 | + * node使用, 上传 |
| 12 | + * |
| 13 | + * 参数说明: |
| 14 | + * disableFsr: 默认启用fsr 默认false |
| 15 | + * host: fsr上传 host |
| 16 | + * receiver: fsr上传 receiver |
| 17 | + * to: fsr上传 to |
| 18 | + * files: 文件对象{[filenam]: [sourceCode]} |
| 19 | + * replace: 替换内容 [{from:'', to:''}] |
| 20 | + * |
| 21 | + * **/ |
| 22 | + |
| 23 | +class Upload { |
| 24 | + constructor(options = {}) { |
| 25 | + this.options = options; |
| 26 | + this.uploadOptions = { |
| 27 | + host: options.host, |
| 28 | + receiver: options.receiver, |
| 29 | + retry: 2, |
| 30 | + aborted: false |
| 31 | + }; |
| 32 | + this.throttle = options.throttle || 200; |
| 33 | + this.files = options.files; |
| 34 | + this.to = options.to; |
| 35 | + this.replace = options.replace; |
| 36 | + |
| 37 | + this._running = false; |
| 38 | + this._deployFiles = {}; |
| 39 | + } |
| 40 | + |
| 41 | + run(cb) { |
| 42 | + if (this.running) { |
| 43 | + this.uploadOptions.aborted = true; |
| 44 | + clearTimeout(this.running); |
| 45 | + this.running = null; |
| 46 | + } |
| 47 | + const options = this.options; |
| 48 | + // 过滤掉已经上传成功的文件 |
| 49 | + const targetFiles = this.filterFiles(this.files); |
| 50 | + const uploadTargets = Object.keys(targetFiles).map(filename => { |
| 51 | + return { |
| 52 | + host: options.host, |
| 53 | + receiver: options.receiver, |
| 54 | + content: this.getContent(filename, targetFiles[filename]), |
| 55 | + to: options.to, |
| 56 | + subpath: filename.replace(/\?.*$/, '') |
| 57 | + }; |
| 58 | + }); |
| 59 | + |
| 60 | + // 是否FIS安全部署服务 |
| 61 | + const uploadHandler = options.disableFsr ? upload : fsrUpload; |
| 62 | + const startTime = Date.now(); |
| 63 | + this.running = setTimeout(() => { |
| 64 | + this.uploadOptions.aborted = false; |
| 65 | + uploadHandler(uploadTargets, this.uploadOptions, () => { |
| 66 | + // 对于存在hash的文件,使用 1 作为flag |
| 67 | + // 对于 tpl、html 这种没有 hash 的文件,使用内容的 md5 作为flag |
| 68 | + Object.keys(targetFiles).forEach(filename => { |
| 69 | + if (targetFiles[filename]) { |
| 70 | + this._deployFiles[filename] = md5(targetFiles[filename]); |
| 71 | + } |
| 72 | + }); |
| 73 | + if (cb) { |
| 74 | + return cb(); |
| 75 | + } |
| 76 | + console.log('\n'); |
| 77 | + console.log('Upload completed in ' + (Date.now() - startTime) + 'ms.'); |
| 78 | + }); |
| 79 | + }, this.throttle); |
| 80 | + } |
| 81 | + |
| 82 | + filterFiles(files) { |
| 83 | + const targetFiles = {}; |
| 84 | + // 过滤掉已经上传成功的文件 |
| 85 | + Object.keys(files).forEach(filename => { |
| 86 | + if (this._deployFiles[filename] && ( |
| 87 | + this._deployFiles[filename] === md5(files[filename]) |
| 88 | + )) { |
| 89 | + return; |
| 90 | + } |
| 91 | + targetFiles[filename] = files[filename]; |
| 92 | + }); |
| 93 | + return targetFiles; |
| 94 | + } |
| 95 | + |
| 96 | + getContent(filename, source) { |
| 97 | + const isContainCdn = /\.(css|js|html|tpl)$/.test(filename); |
| 98 | + if (isContainCdn) { |
| 99 | + source = source.toString(); |
| 100 | + this.replace.forEach(re => { |
| 101 | + const reg = typeof re.from === 'string' ? new RegExp(re.from, 'ig') : re.from; |
| 102 | + source = source.replace(reg, re.to); |
| 103 | + }); |
| 104 | + } |
| 105 | + return source; |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +module.exports = Upload; |
0 commit comments