Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

netrc-parser

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

netrc-parser - npm Package Compare versions

Comparing version 0.0.1 to 1.0.0

224

lib/netrc.js
'use strict';
var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');
var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

@@ -20,20 +24,70 @@

var path = require('path');
var Lexer = require('lex');
/**
* @typedef Machines
* @type Object
* @prop {string} host
* @prop {string} password
*/
function findIndex(arr, fn) {
for (var i = 0; i < arr.length; i++) {
if (fn(arr[i])) return i;
}
return -1;
}
function readFile(file) {
function decryptFile(file) {
var _require = require('child_process'),
spawnSync = _require.spawnSync;
/**
* @typedef Machines
* @type {Object.<string, Machine}
*/
var _spawnSync = spawnSync('gpg', ['--batch', '--quiet', '--decrypt', file], { stdio: [0, null, 2], encoding: 'utf8' }),
stdout = _spawnSync.stdout,
status = _spawnSync.status;
if (status !== 0) throw new Error('gpg exited with code ' + status);
return stdout;
}
if (path.extname(file) === '.gpg') return decryptFile(file);else return fs.readFileSync(file, 'utf8');
}
function lex(body) {
var tokens = [];
var lineIndex = 0;
var lexer = new Lexer(function (char) {
throw new Error('Unexpected character during netrc parsing at character ' + char + ':\n' + body);
});
lexer.addRule(/\s+/, function (content) {
tokens.push({ type: 'whitespace', content: content });
}, [0, 1]);
lexer.addRule(/#.*/, function (content) {
tokens.push({ type: 'comment', content: content });
}, [0, 1]);
lexer.addRule(/macdef/g, function (content) {
this.state = 3;
tokens.push({ type: 'macdef', content: content });
}, [0, 1, 3]);
lexer.addRule(/machine +(\S+)/, function (content, value) {
this.state = 1;
tokens.push({ type: 'machine', content: content, value: value });
}, [0, 1, 3]);
lexer.addRule(/[\s\S\n]/, function (content) {
tokens[tokens.length - 1].content += content;
}, [3]);
lexer.addRule(/([a-zA-Z]+) +(\S+)/, function (content, name, value) {
tokens.push({ type: 'prop', name: name, value: value });
}, [1]);
lexer.addRule(/default/, function (content) {
this.state = 1;
tokens.push({ type: 'default', content: content });
}, [0]);
lexer.setInput(body).lex();
return tokens;
}
function machineProxy(machine) {
var tokens = machine._tokens = machine._tokens || [];
var props = function props() {
return tokens.filter(function (t) {
return t.type === 'prop';
});
};
var _iteratorNormalCompletion = true;

@@ -44,14 +98,5 @@ var _didIteratorError = false;

try {
for (var _iterator = (0, _getIterator3.default)(body.split('\n')), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var _line = _step.value;
_line = _line.trim();
if (_line.includes(' ')) {
var idx = _line.indexOf(' ');
tokens.push({ token: _line.slice(0, idx).trim(), line: lineIndex });
tokens.push({ token: _line.slice(idx).trim(), line: lineIndex });
} else {
tokens.push({ token: _line, line: lineIndex });
}
lineIndex++;
for (var _iterator = (0, _getIterator3.default)(props()), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var prop = _step.value;
machine[prop.name] = prop.value;
}

@@ -73,24 +118,27 @@ } catch (err) {

return tokens;
return new Proxy(machine, {
set: function set(machine, name, value) {
machine[name] = value;
var prop = props().find(function (p) {
return p.name === name;
});
if (prop) prop.value = value;else {
var lastPropIdx = findIndex(tokens, function (t) {
return t.type === 'prop';
});
tokens.splice(lastPropIdx, 0, tokens[lastPropIdx - 1]); // insert whitespace
tokens.splice(lastPropIdx, 0, { type: 'prop', name: name, value: value });
}
return true;
}
});
}
function parse(body) {
var machines = {};
var tokens = lex(body);
var host = void 0;
var prop = void 0;
while (tokens.length > 0) {
prop = tokens.shift();
switch (prop.token) {
case 'machine':
host = tokens.shift().token;
machines[host] = { host: host };
break;
default:
if (!host) throw new Error('Invalid token ' + prop.token + ' on line ' + prop.line);
machines[host][prop.token] = tokens.shift().token;
function machinesProxy() {
return new Proxy({}, {
set: function set(machines, host, value) {
machines[host] = machineProxy(value);
return true;
}
}
console.dir(machines);
return machines;
});
}

@@ -108,7 +156,16 @@

* gets the machines on the default netrc file
* @example
* const netrc = require('netrc-parser')
* netrc.machines['api.heroku.com'].password // get auth token from ~/.netrc
*/
value: function machines() {
get: function get() {
return this.default.machines;
}
}, {
key: 'default',
get: function get() {
if (this._default) return this._default;
var f = os.platform() === 'win32' ? '_netrc' : '.netrc';
var netrc = new Netrc(path.join(os.homedir(), f));
return netrc.machines;
this._default = new Netrc(path.join(os.homedir(), f));
return this._default;
}

@@ -120,5 +177,80 @@ }]);

this.machines = parse(fs.readFileSync(file, 'utf8'));
this.file = file;
this.machines = machinesProxy();
this._parse();
}
(0, _createClass3.default)(Netrc, [{
key: 'save',
value: function save() {
var body = this._tokens.map(function (t) {
switch (t.type) {
case 'default':
case 'machine':
var tokens = t._tokens || [];
return tokens.map(function (t) {
switch (t.type) {
case 'prop':
return t.name + ' ' + t.value;
case 'machine':
case 'default':
case 'comment':
case 'whitespace':
return t.content;
}
}).join('');
case 'macdef':
case 'comment':
case 'whitespace':
return t.content;
}
}).join('');
fs.writeFileSync(this.file, body, { mode: 384 });
}
}, {
key: '_parse',
value: function _parse() {
var _this = this;
this._tokens = [];
var tokens = lex(readFile(this.file));
var _loop = function _loop(_i) {
var _tokens2;
var getMachineTokens = function getMachineTokens() {
var machineTokens = [];
while (1) {
machineTokens.push(tokens[_i]);
var next = tokens[_i + 1];
if (!next || ['machine', 'default', 'macdef'].includes(next.type) || next.type === 'default') break;
_i++;
}
return machineTokens;
};
switch (tokens[_i].type) {
case 'macdef':
(_tokens2 = _this._tokens).push.apply(_tokens2, (0, _toConsumableArray3.default)(getMachineTokens()));
break;
case 'default':
_this.default = machineProxy({ type: 'default', _tokens: getMachineTokens() });
_this._tokens.push(_this.default);
break;
case 'machine':
var _host = tokens[_i].value;
_this.machines[_host] = { type: 'machine', _tokens: getMachineTokens() };
_this._tokens.push(_this.machines[_host]);
break;
default:
_this._tokens.push(tokens[_i]);
}
i = _i;
};
for (var i = 0; i < tokens.length; i++) {
_loop(i);
}
}
}]);
return Netrc;

@@ -125,0 +257,0 @@ }();

{
"name": "netrc-parser",
"description": "netrc parser",
"version": "0.0.1",
"version": "1.0.0",
"author": "Jeff Dickey (@dickeyxxx)",
"bugs": "https://github.com/dickeyxxx/node-netrc-parser/issues",
"dependencies": {
"babel-runtime": "6.23.0"
"babel-runtime": "6.23.0",
"lex": "1.7.9"
},

@@ -16,6 +17,8 @@ "devDependencies": {

"documentation": "4.0.0-beta.18",
"flow-bin": "0.40.0",
"flow-bin": "0.41.0",
"fs-extra": "2.0.0",
"jest": "19.0.2",
"rimraf": "2.6.1"
"jest-junit": "1.3.0",
"rimraf": "2.6.1",
"standard": "9.0.1"
},

@@ -41,5 +44,6 @@ "homepage": "https://github.com/dickeyxxx/node-netrc-parser",

"ignore": [
"lib"
"lib",
"flow-typed"
]
}
}
# netrc-parser
[![CircleCI](https://circleci.com/gh/dickeyxxx/node-netrc-parser.svg?style=svg)](https://circleci.com/gh/dickeyxxx/node-netrc-parser)
[![codecov](https://codecov.io/gh/dickeyxxx/node-netrc-parser/branch/master/graph/badge.svg)](https://codecov.io/gh/dickeyxxx/node-netrc-parser)
# API

@@ -7,22 +10,5 @@

## Machine
[src/netrc.js:13-17](https://github.com/dickeyxxx/node-netrc-parser/blob/f8791670f1febd3dd5980657dc086326e1bc6492/src/netrc.js#L13-L17 "Source code on GitHub")
Type: {host: [string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String), password: [string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?}
**Properties**
- `host` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)**
- `password` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)**
## Machines
[src/netrc.js:23-23](https://github.com/dickeyxxx/node-netrc-parser/blob/f8791670f1febd3dd5980657dc086326e1bc6492/src/netrc.js#L23-L23 "Source code on GitHub")
Type: {}
## Netrc
[src/netrc.js:66-81](https://github.com/dickeyxxx/node-netrc-parser/blob/f8791670f1febd3dd5980657dc086326e1bc6492/src/netrc.js#L66-L81 "Source code on GitHub")
[src/netrc.js:142-233](https://github.com/dickeyxxx/node-netrc-parser/blob/844949ed3577f30c53a78c99c7407017573ebe3d/src/netrc.js#L142-L233 "Source code on GitHub")

@@ -33,6 +19,13 @@ parses a netrc file

[src/netrc.js:70-74](https://github.com/dickeyxxx/node-netrc-parser/blob/f8791670f1febd3dd5980657dc086326e1bc6492/src/netrc.js#L70-L74 "Source code on GitHub")
[src/netrc.js:149-151](https://github.com/dickeyxxx/node-netrc-parser/blob/844949ed3577f30c53a78c99c7407017573ebe3d/src/netrc.js#L149-L151 "Source code on GitHub")
gets the machines on the default netrc file
Returns **[Machines](#machines)**
**Examples**
```javascript
const netrc = require('netrc-parser')
netrc.machines['api.heroku.com'].password // get auth token from ~/.netrc
```
Returns **Machines**
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc