diff --git a/index.js b/index.js index 172080c..1b54bfc 100644 --- a/index.js +++ b/index.js @@ -1,40 +1,63 @@ +/* QR-FILETRANSFER done in NodeJS + * + * Inspired from @claudiodangelis qr-filetransfer written in Go + * @claudiodangelis https://github.com/claudiodangelis + */ + +//Initialising dependencies var express = require('express'); var qrTerminal = require('qrcode-terminal'); var chalk = require('chalk'); var fs = require('fs'); -var localIpV4Address = require('local-ipv4-address'); +var localIpV4Address = require("local-ipv4-address"); + +//Calling local scripts var misc = require("./lib/misc.js"); var constants = require("./lib/constants.js"); +//Initialising express var app = express(); -const port = 3000 || process.env.PORT; +//Defining constants for path and port const pathInitial = __dirname + "/"; var pathTotal = pathInitial + process.argv[2]; -var ipv4 = "http://192.168.1.2:3000"; +//Using the constant values in lib/constants.js +//TODO: Constant values shouldn't be usued at all. It could break in any other device. +var ipv4 = constants.ipv4; +var fPort = constants.port; + +//Getting the localIP of the device localIpV4Address().then(function(ipAddress){ ipv4 = ipAddress; }); +//Defining a string in the form http://ip-address:port for the qr generation +const finalIP = ipv4 + ":" + fPort; +//Checking the arguments passed if(process.argv.length != 3) { console.log(chalk.red(":/ Something's missing in the arguments")); console.log(chalk.green("It should be something like node index.js i_am_so_great.pdf")); process.exit(0); } +//Checking if the file exists if(!fs.existsSync(pathTotal)) { console.log(chalk.red("The file does not exist, Please check the file path again")); } else { - app.listen(port, (req,res) => { - //console.log("Server is live"); + //Initialising the server + app.listen(fPort, (req,res) => { + console.log("Server is live at port" + fPort); misc.header(); - qrTerminal.generate(ipv4); + + //Generating the qrcode + qrTerminal.generate(finalIP); + console.log(finalIP); }); diff --git a/lib/constants.js b/lib/constants.js index cd5e75c..6223751 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -1,3 +1,7 @@ module.exports = { - owner: "Sagar" + owner: "Sagar", + port: "3000", + ipv4: "http://192.168.1.2" } + +//Incase localIPv4Address fails, Assign the local IP of your machine from here diff --git a/node_modules/freeport/.travis.yml b/node_modules/freeport/.travis.yml new file mode 100644 index 0000000..3a552a0 --- /dev/null +++ b/node_modules/freeport/.travis.yml @@ -0,0 +1,9 @@ +language: node_js +node_js: + - iojs + - 0.6 + - 0.8 + - 0.9 + - 0.10 + - 0.11 + - 0.12 diff --git a/node_modules/freeport/lib/freeport.js b/node_modules/freeport/lib/freeport.js new file mode 100644 index 0000000..008770e --- /dev/null +++ b/node_modules/freeport/lib/freeport.js @@ -0,0 +1,17 @@ +var net = require('net') + +module.exports = function(cb) { + var server = net.createServer() + , port = 0 + server.on('listening', function() { + port = server.address().port + server.close() + }) + server.on('close', function() { + cb(null, port) + }) + server.on('error', function(err) { + cb(err, null) + }) + server.listen(0, '127.0.0.1') +} diff --git a/node_modules/freeport/license b/node_modules/freeport/license new file mode 100644 index 0000000..b7d73f0 --- /dev/null +++ b/node_modules/freeport/license @@ -0,0 +1,13 @@ +Copyright 2015 Naitik Shah + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/node_modules/freeport/package.json b/node_modules/freeport/package.json new file mode 100644 index 0000000..00f06c2 --- /dev/null +++ b/node_modules/freeport/package.json @@ -0,0 +1,51 @@ +{ + "_from": "freeport", + "_id": "freeport@1.0.5", + "_inBundle": false, + "_integrity": "sha1-JV6KuEFwwzuoXZkOghrl9KGpvF0=", + "_location": "/freeport", + "_phantomChildren": {}, + "_requested": { + "type": "tag", + "registry": true, + "raw": "freeport", + "name": "freeport", + "escapedName": "freeport", + "rawSpec": "", + "saveSpec": null, + "fetchSpec": "latest" + }, + "_requiredBy": [ + "#USER", + "/" + ], + "_resolved": "https://registry.npmjs.org/freeport/-/freeport-1.0.5.tgz", + "_shasum": "255e8ab84170c33ba85d990e821ae5f4a1a9bc5d", + "_spec": "freeport", + "_where": "E:\\Github projects\\qr-transfer", + "author": { + "name": "Naitik Shah", + "email": "n@daaku.org" + }, + "bugs": { + "url": "https://github.com/daaku/nodejs-freeport/issues" + }, + "bundleDependencies": false, + "deprecated": false, + "description": "Find a free port.", + "devDependencies": { + "mocha": "0.12.x" + }, + "homepage": "https://github.com/daaku/nodejs-freeport", + "license": "Apache-2.0", + "main": "lib/freeport", + "name": "freeport", + "repository": { + "type": "git", + "url": "git+https://github.com/daaku/nodejs-freeport.git" + }, + "scripts": { + "test": "NODE_PATH=./lib mocha --ui exports" + }, + "version": "1.0.5" +} diff --git a/node_modules/freeport/readme.md b/node_modules/freeport/readme.md new file mode 100644 index 0000000..8d37d3e --- /dev/null +++ b/node_modules/freeport/readme.md @@ -0,0 +1,11 @@ +freeport [![Build Status](https://secure.travis-ci.org/daaku/nodejs-freeport.png)](http://travis-ci.org/daaku/nodejs-freeport) +======== + +Find a free port. + +```javascript +freeport(function(err, port) { + if (err) throw err + console.log(port) +}) +``` diff --git a/node_modules/get-port/index.js b/node_modules/get-port/index.js new file mode 100644 index 0000000..fbd74e6 --- /dev/null +++ b/node_modules/get-port/index.js @@ -0,0 +1,28 @@ +'use strict'; +const net = require('net'); + +const getPort = options => new Promise((resolve, reject) => { + // For backwards compatibility with number-only input + // TODO: Remove this in the next major version + if (typeof options === 'number') { + options = { + port: options + }; + } + + const server = net.createServer(); + + server.unref(); + server.on('error', reject); + + server.listen(options, () => { + const port = server.address().port; + server.close(() => { + resolve(port); + }); + }); +}); + +module.exports = options => options ? + getPort(options).catch(() => getPort(0)) : + getPort(0); diff --git a/node_modules/get-port/license b/node_modules/get-port/license new file mode 100644 index 0000000..e7af2f7 --- /dev/null +++ b/node_modules/get-port/license @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) Sindre Sorhus (sindresorhus.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/node_modules/get-port/package.json b/node_modules/get-port/package.json new file mode 100644 index 0000000..9caefe8 --- /dev/null +++ b/node_modules/get-port/package.json @@ -0,0 +1,77 @@ +{ + "_from": "get-port", + "_id": "get-port@3.2.0", + "_inBundle": false, + "_integrity": "sha1-3Xzn3hh8Bsi/NTeWrHHgmfCYDrw=", + "_location": "/get-port", + "_phantomChildren": {}, + "_requested": { + "type": "tag", + "registry": true, + "raw": "get-port", + "name": "get-port", + "escapedName": "get-port", + "rawSpec": "", + "saveSpec": null, + "fetchSpec": "latest" + }, + "_requiredBy": [ + "#USER", + "/" + ], + "_resolved": "https://registry.npmjs.org/get-port/-/get-port-3.2.0.tgz", + "_shasum": "dd7ce7de187c06c8bf353796ac71e099f0980ebc", + "_spec": "get-port", + "_where": "E:\\Github projects\\qr-transfer", + "author": { + "name": "Sindre Sorhus", + "email": "sindresorhus@gmail.com", + "url": "sindresorhus.com" + }, + "bugs": { + "url": "https://github.com/sindresorhus/get-port/issues" + }, + "bundleDependencies": false, + "deprecated": false, + "description": "Get an available port", + "devDependencies": { + "ava": "*", + "pify": "^3.0.0", + "xo": "*" + }, + "engines": { + "node": ">=4" + }, + "files": [ + "index.js" + ], + "homepage": "https://github.com/sindresorhus/get-port#readme", + "keywords": [ + "port", + "find", + "finder", + "portfinder", + "free", + "available", + "connection", + "connect", + "open", + "net", + "tcp", + "scan", + "rand", + "random", + "preferred", + "chosen" + ], + "license": "MIT", + "name": "get-port", + "repository": { + "type": "git", + "url": "git+https://github.com/sindresorhus/get-port.git" + }, + "scripts": { + "test": "xo && ava" + }, + "version": "3.2.0" +} diff --git a/node_modules/get-port/readme.md b/node_modules/get-port/readme.md new file mode 100644 index 0000000..72c1fbc --- /dev/null +++ b/node_modules/get-port/readme.md @@ -0,0 +1,64 @@ +# get-port [![Build Status](https://travis-ci.org/sindresorhus/get-port.svg?branch=master)](https://travis-ci.org/sindresorhus/get-port) + +> Get an available port + + +## Install + +``` +$ npm install get-port +``` + + +## Usage + +```js +const getPort = require('get-port'); + +getPort().then(port => { + console.log(port); + //=> 51402 +}); +``` + +Optionally, pass in a preferred port: + +```js +getPort({port: 3000}).then(port => { + console.log(port); + // Will use 3000 if available, otherwise fall back to a random port +}); +``` + + +## API + +### getPort([options]) + +Returns a `Promise` for a port number. + +#### options + +Type: `Object` + +##### port + +Type: `number` + +The preferred port to use. + +##### host + +Type: `string` + +The host on which port resolution should be performed. Can be either an IPv4 or IPv6 address. + + +## Related + +- [get-port-cli](https://github.com/sindresorhus/get-port-cli) - CLI for this module + + +## License + +MIT © [Sindre Sorhus](https://sindresorhus.com) diff --git a/package-lock.json b/package-lock.json index 864fc2a..4f5bd35 100644 --- a/package-lock.json +++ b/package-lock.json @@ -195,11 +195,21 @@ "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz", "integrity": "sha1-mMI9qxF1ZXuMBXPozszZGw/xjIQ=" }, + "freeport": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/freeport/-/freeport-1.0.5.tgz", + "integrity": "sha1-JV6KuEFwwzuoXZkOghrl9KGpvF0=" + }, "fresh": { "version": "0.5.2", "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=" }, + "get-port": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/get-port/-/get-port-3.2.0.tgz", + "integrity": "sha1-3Xzn3hh8Bsi/NTeWrHHgmfCYDrw=" + }, "has-flag": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", diff --git a/package.json b/package.json index e30303d..c671c97 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,8 @@ "chalk": "^2.3.2", "express": "^4.16.3", "figlet": "^1.2.0", + "freeport": "^1.0.5", + "get-port": "^3.2.0", "local-ipv4-address": "0.0.2", "qrcode-terminal": "^0.12.0" }