|
| 1 | +/* |
| 2 | + * OS.js - JavaScript Cloud/Web Desktop Platform |
| 3 | + * |
| 4 | + * Copyright (c) 2011-2020, Anders Evenrud <[email protected]> |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, this |
| 11 | + * list of conditions and the following disclaimer |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 20 | + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + * |
| 27 | + * @author Anders Evenrud <[email protected]> |
| 28 | + * @licence Simplified BSD License |
| 29 | + */ |
| 30 | + |
| 31 | +const bent = require('bent'); |
| 32 | +const tar = require('tar'); |
| 33 | +const path = require('path'); |
| 34 | +const fs = require('fs-extra'); |
| 35 | + |
| 36 | +const relative = filename => filename |
| 37 | + .replace(process.cwd(), ''); |
| 38 | + |
| 39 | +const archiveName = url => path |
| 40 | + .basename(url.split('?')[0]) |
| 41 | + .replace(/\.[^/.]+$/, ''); |
| 42 | + |
| 43 | +const fetchSteam = (url, options) => bent()(url, null, { |
| 44 | + headers: options.headers || {} |
| 45 | +}); |
| 46 | + |
| 47 | +const readOrDefault = filename => fs.existsSync(filename) |
| 48 | + ? fs.readJsonSync(filename) |
| 49 | + : []; |
| 50 | + |
| 51 | +const extract = (stream, target) => new Promise((resolve, reject) => { |
| 52 | + stream.once('end', () => resolve()); |
| 53 | + stream.once('error', error => reject(error)); |
| 54 | + stream.pipe(tar.extract({C: target})); |
| 55 | +}); |
| 56 | + |
| 57 | +module.exports = { |
| 58 | + relative, |
| 59 | + archiveName, |
| 60 | + fetchSteam, |
| 61 | + readOrDefault, |
| 62 | + extract |
| 63 | +}; |
0 commit comments