From 880c340f332bf801321bcf927c60ebbb7f384d31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6lsche?= Date: Sun, 29 Sep 2019 16:53:58 +0200 Subject: [PATCH 1/2] Add canonicalize function, add ipv6 option to host() (now returns v4 by default), add tests --- index.js | 33 ++++++++++++++++++++++----------- package.json | 3 +++ test.js | 25 +++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 test.js diff --git a/index.js b/index.js index 8aa3504..5ff36d9 100644 --- a/index.js +++ b/index.js @@ -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 diff --git a/package.json b/package.json index 703761f..c9ffc6b 100644 --- a/package.json +++ b/package.json @@ -11,5 +11,8 @@ "license": "MIT", "dependencies": { "non-private-ip": "^1.4.4" + }, + "devDependencies": { + "tape": "^4.11.0" } } diff --git a/test.js b/test.js new file mode 100644 index 0000000..19d2970 --- /dev/null +++ b/test.js @@ -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() +}) From 0243f1ed9a0fba46f674aa5ac9521326de208379 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6lsche?= Date: Sun, 29 Sep 2019 17:11:43 +0200 Subject: [PATCH 2/2] readme --- README.md | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ff98cda..8fb1fec 100644 --- a/README.md +++ b/README.md @@ -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 ...)