| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
|
If it is agreed that we should add this, I think it would best be done in C++ land for performance reasons. |
Sorry, something went wrong.
|
I think a benchmark would be useful before deciding to put this in C++ land. |
Sorry, something went wrong.
There was a problem hiding this comment.
why not do a map:
flatten(Object.values(interfaces))
.map((v) => ({v, mask: netmaskToCIDRSuffixMap.get(v.netmask)}))
.forEach(({v, mask}) => {
Sorry, something went wrong.
There was a problem hiding this comment.
Only because not all netmasks returned by networkInterfaces() might be in the map above. That's also the cause for the if condition.
What do you think of the following?
flatten(Object.values(interfaces))
.map((v) => {
assert.ok('cidr' in v, `"cidr" prop not found in ${inspect(v)}`);
return v;
})
.filter((v) => netmaskToCIDRSuffixMap.has(v.netmask))
.map((v) => [v.cidr, `${v.address}/${netmaskToCIDRSuffixMap.get(v.netmask)}`])
.forEach(([actual, expected]) => assert.strictEqual(actual, expected));
Sorry, something went wrong.
There was a problem hiding this comment.
I like the use of entries but it will exclude this from backporting to v6.x, and it's a nice feature.
So be ready to backport.
Sorry, something went wrong.
There was a problem hiding this comment.
So, should I make a more backport friendly version or leave this be till we decide to backport it?
Sorry, something went wrong.
There was a problem hiding this comment.
Ping @gibfahn
Sorry, something went wrong.
There was a problem hiding this comment.
style nit: assign getCIDRSuffix(v.netmask, protocol) to a const, and do this in one line.
Sorry, something went wrong.
|
Don't know why but Functional Programing makes me happy. |
Sorry, something went wrong.
AFAICT It's a very cold code-path. So maybe for reusability.
https://github.com/nodejs/node/blob/master/doc/guides/writing-and-running-benchmarks.md |
Sorry, something went wrong.
There was a problem hiding this comment.
Also add some tests for non-byte-aligned subnet masks, e.g. 255.255.224.0 -> 19 and ffff:ffff:ffff:ff80:: -> 57.
Sorry, something went wrong.
There was a problem hiding this comment.
This is hard as I rely on the output of networkInterfaces method and that gets data from the os binding. So, I don't really know where I can inject some mock data with a custom non-aligned netmask and have the js land networkInterfaces method consume that. Any ideas?
Sorry, something went wrong.
There was a problem hiding this comment.
Hmm, you could potential define the parsing method in a internal os module and test that separately.
Sorry, something went wrong.
There was a problem hiding this comment.
@silverwind So I moved the code out but requiring internal/os fails with a module not found. Do I need to register it somewhere? Found this registry node.gyp.
Secondly, where do tests for internal modules go? I see thetest folder in lib/internal only has tests for unicode.
Sorry, something went wrong.
There was a problem hiding this comment.
@zeusdeux In test, just like everything else. You would need to specify a // Flags: --expose-internals to make the testing harness expose internal modules for testing though.
Sorry, something went wrong.
There was a problem hiding this comment.
Moved the CIDR logic to internal/os and added tests for it @silverwind @TimothyGu.
Sorry, something went wrong.
There was a problem hiding this comment.
I think you can remove temp altogether and return .join('').length after then maps above.
Sorry, something went wrong.
There was a problem hiding this comment.
This won't work as temp.join('') could be a string of the form 11111111000 for 255.0.0.0 and called .length would return 11 which is wrong. Hence the parse and reduce.
Sorry, something went wrong.
There was a problem hiding this comment.
Right, here's a slight modification I came up with that includes validation for invalid masks:
function getCIDRSuffix(subnetMask, proto) {
const v6 = proto === 'ipv6';
const parts = subnetMask
.split(v6 ? ':' : '.')
.map((part) => parseInt(part || 0, v6 ? 16 : 10));
const isInvalidSubnetMask = parts.some(function(_, i) {
if (i > 1 && parts[i - 1] < parts[i]) {
return true;
}
});
if (isInvalidSubnetMask) {
return null;
}
return parts
.map((dec) => dec.toString(2))
.join('')
.split('')
.map((bin) => parseInt(bin, 10))
.reduce((acc, v) => acc += v, 0);
}There's probably a mathematical way to count the number of binary 1s in a number that eludes me right now.
Sorry, something went wrong.
|
We also got to consider invalid subnet masks, for example 255.0.0.255. I think returning null in that case might be reasonable (should be documented). |
Sorry, something went wrong.
I thought of adding this validation in, but I decided not to as that I believe should lie in C++ land. I just wanted this to be a js land "transform" on data coming in from the os binding. Nevertheless, I am open to a discussion on this. |
Sorry, something went wrong.
There was a problem hiding this comment.
You'll need to write a few words in the documentation
Sorry, something went wrong.
There was a problem hiding this comment.
You don't have to care that the map.get returns undefined. It's not pure FP, but it's susinct
flatten(Object.values(interfaces))
.map((v) => ({v, mask: netmaskToCIDRSuffixMap.get(v.netmask)}))
.forEach(({v, mask}) => {
assert.ok('cidr' in v, `"cidr" prop not found in ${inspect(v)}`);
if (mask)
assert.strictEqual(v.cidr, `${v.address}/${mask)}`)
});
Sorry, something went wrong.
|
Sorry, was away for a bit. Pulled in suggested changes :) |
Sorry, something went wrong.
|
Would you mind taking another look folks? @refack @silverwind @TimothyGu |
Sorry, something went wrong.
|
One more approval and I'll squash my commits. |
Sorry, something went wrong.
|
@zeusdeux no need to squash, the person landing the PR will squash. |
Sorry, something went wrong.
This patch adds support for CIDR notation to the output of the `networkInterfaces()` method PR-URL: nodejs#14307 Fixes: nodejs#14006 Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com>
|
Extra sanity on master: https://ci.nodejs.org/job/node-test-commit-linuxone/7965/ |
Sorry, something went wrong.
This patch adds support for CIDR notation to the output of the `networkInterfaces()` method PR-URL: #14307 Fixes: #14006 Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Notable Changes * build: * Snapshots are now re-enabled in V8 #14875 * console: * Implement minimal `console.group()`. #14910 * deps: * upgrade libuv to 1.14.1 #14866 * update nghttp2 to v1.25.0 #14955 * dns: * Add `verbatim` option to dns.lookup(). When true, results from the DNS resolver are passed on as-is, without the reshuffling that Node.js otherwise does that puts IPv4 addresses before IPv6 addresses. #14731 * fs: * add fs.copyFile and fs.copyFileSync which allows for more efficient copying of files. #15034 * inspector: * Enable async stack traces #13870 * module: * Add support for ESM. This is currently behind the `--experimental-modules` flag and requires the .mjs extension. `node --experimental-modules index.mjs` #14369 * napi: * implement promise #14365 * os: * Add support for CIDR notation to the output of the networkInterfaces() method. #14307 * perf_hooks: * An initial implementation of the Performance Timing API for Node.js. This is the same Performance Timing API implemented by modern browsers with a number of Node.js specific properties. The User Timing mark() and measure() APIs are implemented, as is a Node.js specific flavor of the Frame Timing for measuring event loop duration. #14680 * tls: * multiple PFX in createSecureContext [#14793](#14793) * Added new collaborators: * BridgeAR – Ruben Bridgewater PR-URL: #15308
Notable Changes * build: * Snapshots are now re-enabled in V8 #14875 * console: * Implement minimal `console.group()`. #14910 * deps: * upgrade libuv to 1.14.1 #14866 * update nghttp2 to v1.25.0 #14955 * dns: * Add `verbatim` option to dns.lookup(). When true, results from the DNS resolver are passed on as-is, without the reshuffling that Node.js otherwise does that puts IPv4 addresses before IPv6 addresses. #14731 * fs: * add fs.copyFile and fs.copyFileSync which allows for more efficient copying of files. #15034 * inspector: * Enable async stack traces #13870 * module: * Add support for ESM. This is currently behind the `--experimental-modules` flag and requires the .mjs extension. `node --experimental-modules index.mjs` #14369 * napi: * implement promise #14365 * os: * Add support for CIDR notation to the output of the networkInterfaces() method. #14307 * perf_hooks: * An initial implementation of the Performance Timing API for Node.js. This is the same Performance Timing API implemented by modern browsers with a number of Node.js specific properties. The User Timing mark() and measure() APIs are implemented, as is a Node.js specific flavor of the Frame Timing for measuring event loop duration. #14680 * tls: * multiple PFX in createSecureContext [#14793](#14793) * Added new collaborators: * BridgeAR – Ruben Bridgewater PR-URL: #15308
Notable Changes * build: * Snapshots are now re-enabled in V8 nodejs#14875 * console: * Implement minimal `console.group()`. nodejs#14910 * deps: * upgrade libuv to 1.14.1 nodejs#14866 * update nghttp2 to v1.25.0 nodejs#14955 * dns: * Add `verbatim` option to dns.lookup(). When true, results from the DNS resolver are passed on as-is, without the reshuffling that Node.js otherwise does that puts IPv4 addresses before IPv6 addresses. nodejs#14731 * fs: * add fs.copyFile and fs.copyFileSync which allows for more efficient copying of files. nodejs#15034 * inspector: * Enable async stack traces nodejs#13870 * module: * Add support for ESM. This is currently behind the `--experimental-modules` flag and requires the .mjs extension. `node --experimental-modules index.mjs` nodejs#14369 * napi: * implement promise nodejs#14365 * os: * Add support for CIDR notation to the output of the networkInterfaces() method. nodejs#14307 * perf_hooks: * An initial implementation of the Performance Timing API for Node.js. This is the same Performance Timing API implemented by modern browsers with a number of Node.js specific properties. The User Timing mark() and measure() APIs are implemented, as is a Node.js specific flavor of the Frame Timing for measuring event loop duration. nodejs#14680 * tls: * multiple PFX in createSecureContext [nodejs#14793](nodejs#14793) * Added new collaborators: * BridgeAR – Ruben Bridgewater PR-URL: nodejs#15308
|
Release team decided not to land on v6.x, if you disagree let us know. |
Sorry, something went wrong.
os.networkInterfaces() does support a new .cidr property since version 8.5.0 which is below the minimum 10.0.0 required by this module so use it. Ref: nodejs/node#14307
| Back | FazBrowse Home | New Git URL |
This patch adds support for CIDR notation to the output of the
networkInterfaces() method
Fixes: #14006
Checklist
Affected core subsystem(s)
os