├── .gitignore ├── README.md ├── package.json ├── LICENSE └── server.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # xz-as-a-service 2 | 3 | xz compression as a service 4 | 5 | ``` 6 | npm install -g xz-as-a-service 7 | ``` 8 | 9 | ## Usage 10 | 11 | First spin up the service 12 | 13 | ```sh 14 | xz-as-a-service --port 9927 15 | ``` 16 | 17 | Then send a file to it you want to compress 18 | 19 | ```sh 20 | curl --upload-file my.tar http://localhost:9927/ > my.tar.xz 21 | ``` 22 | 23 | The service will also announce itself as `xz-as-a-service.local` so you can do 24 | 25 | ```sh 26 | curl --upload-file my.tar http://xz-as-a-service.local:9927 > my.tar.xz 27 | ``` 28 | 29 | If you want to change the name there is a `--name` flag 30 | 31 | ```sh 32 | xz-as-a-service --name another-name --port 9927 33 | ``` 34 | 35 | ## License 36 | 37 | MIT 38 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "xz-as-a-service", 3 | "version": "1.0.0", 4 | "description": "xz compression as a service", 5 | "main": "index.js", 6 | "dependencies": { 7 | "minimist": "^1.2.0", 8 | "multicast-dns": "^6.2.0", 9 | "pump": "^1.0.3" 10 | }, 11 | "devDependencies": { 12 | "standard": "^10.0.3" 13 | }, 14 | "scripts": { 15 | "test": "standard" 16 | }, 17 | "bin": { 18 | "xz-as-a-service": "./server.js" 19 | }, 20 | "repository": { 21 | "type": "git", 22 | "url": "https://github.com/mafintosh/xz-as-a-service.git" 23 | }, 24 | "author": "Mathias Buus (@mafintosh)", 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://github.com/mafintosh/xz-as-a-service/issues" 28 | }, 29 | "homepage": "https://github.com/mafintosh/xz-as-a-service" 30 | } 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var os = require('os') 4 | var http = require('http') 5 | var proc = require('child_process') 6 | var pump = require('pump') 7 | var mdns = require('multicast-dns') 8 | var argv = require('minimist')(process.argv, { 9 | alias: { 10 | port: 'p', 11 | name: 'n' 12 | }, 13 | default: { 14 | port: process.env.PORT || 9927, 15 | name: 'xz-as-a-service' 16 | } 17 | }) 18 | 19 | var server = http.createServer(function (req, res) { 20 | console.log('Compressing request and piping to response') 21 | var compress = proc.spawn('xz', ['-e', '-9', '--threads=10', '--stdout']) 22 | pump(req, compress.stdin) 23 | pump(compress.stdout, res) 24 | }) 25 | 26 | server.listen(argv.port, function () { 27 | console.log(`xz-as-a-service listening on ${server.address().port}`) 28 | 29 | if (argv.name === false) return 30 | 31 | var dns = mdns() 32 | var name = argv.name + '.local' 33 | 34 | dns.on('query', function (query) { 35 | var qs = query.questions 36 | for (var i = 0; i < qs.length; i++) { 37 | var q = qs[i] 38 | if (q.name === name && (q.type === 'A' || q.type === 'AAAA')) { 39 | dns.respond({ 40 | answers: answers() 41 | }) 42 | } 43 | } 44 | 45 | function answers () { 46 | var ans = [] 47 | var networks = os.networkInterfaces() 48 | 49 | Object.keys(networks).forEach(function (net) { 50 | var n = networks[net] 51 | n.forEach(function (addr) { 52 | if (!addr.internal) { 53 | ans.push({type: addr.family === 'IPv4' ? 'A' : 'AAAA', ttl: 120, name: name, data: addr.address}) 54 | } 55 | }) 56 | }) 57 | 58 | if (!ans.length) { 59 | ans.push({type: 'A', ttl: 120, name: name, data: '127.0.0.1'}) 60 | ans.push({type: 'AAAA', ttl: 120, name: name, data: '::1'}) 61 | } 62 | return ans 63 | } 64 | }) 65 | }) 66 | --------------------------------------------------------------------------------