Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add canonicalize function, add ipv6 option to host() (now returns v4 … #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,22 @@ This module should become the definition of what scopes mean in ssb.

## API

## `host(scope)`

returns default host for a given scope
## `canonicalize(scope)`

There are two names used in the scuttlebutt codebase for the LAN scope: `local` and `private`.
`canonocalize()` deals with this (and possible other aliases) by mapping both names to 'private'.

The complete mapping is:

```
- device -> device
- local -> private
- private -> private
- public -> public
```
## `host(scope, opts)`

returns default host for a given scope. Returns an IPv4 address unless opts.ipv6 is truthy.

- `device` -> 'localhost'
- `local` -> private/non-routable IP (e.g. 192.168 ...)
Expand Down
33 changes: 22 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
var ip = require('non-private-ip')

module.exports = {
host: function(scope) {
var f = {
device: function () {return 'localhost'},
local: ip.private,
private: ip.private,
public: ip
}[scope]
if (!f) throw new Error('invalid scope: ' + scope)
return f()
}
function canonicalize(scope) {
var canonicalized = {
device: 'device',
local: 'private',
private: 'private',
public: 'public'
}[scope]
if (!canonicalized) throw new Error('invalid scope: ' + scope)
return canonicalized
}

function host(scope, opts) {
opts = opts || {}
var family = opts.ipv6 ? 'v6' : 'v4'
return {
device: {v4: '127.0.0.1', v6: '::1'}[family],
private: ip.private[family],
public: ip[family]
}[canonicalize(scope)]
}

module.exports.canonicalize = canonicalize
module.exports.host = host
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
"license": "MIT",
"dependencies": {
"non-private-ip": "^1.4.4"
},
"devDependencies": {
"tape": "^4.11.0"
}
}
25 changes: 25 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var test = require('tape')
var scopes = require('.')

test('canonicalize', function(t) {
var allScopes = 'device local private public'.split(' ')
t.deepEqual(
allScopes.map(scopes.canonicalize),
'device private private public'.split(' ')
)
t.end()
})

test('throw if scope is invalid', function(t) {
t.throws(function() {
scopes.canonicalize('this')
})
t.end()
})

test('host("device") returns ipv4 or ipv6 representation of localhost', function(t) {
t.equal(scopes.host('device'), '127.0.0.1')
t.equal(scopes.host('device', {ipv6: true}), '::1')
t.equal(scopes.host('device', {ipv6: false}), '127.0.0.1')
t.end()
})