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 makeRange() helper method #28

Merged
merged 27 commits into from Apr 5, 2019
Merged

Conversation

stroncium
Copy link
Contributor

@stroncium stroncium commented Mar 24, 2019

Fixes #24

  • added makeRange helper (inclusive ranges)
  • reworked to use iterables
  • errors not concerning us are rethrowed
  • prepared for registered ports deprioritization
  • bumped node requirement to v8
  • removed travis for nodejs v6

Copy link
Contributor

@acostalima acostalima left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

README update is missing.

Copy link
Contributor

@acostalima acostalima left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

README update is missing.

index.js Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
test.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
index.js Show resolved Hide resolved
index.test-d.ts Outdated Show resolved Hide resolved
readme.md Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
@stroncium
Copy link
Contributor Author

stroncium commented Mar 24, 2019

@sindresorhus While we're at it, should we also filter out well known ports like 1080, 8080, 8081. I've had a rewrite of this project for internal use, and it was quite annoying problem sometimes, and adding a filter helped me a lot. (I work from console a lot and starting a couple adhoc web-servers here and there is normal for me.)

@stroncium
Copy link
Contributor Author

stroncium commented Mar 24, 2019

To develop the concept further, I've parsed registered ranges from https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.txt and propose filter out ports in those ranges and deprioritize them(move to the end of array).

for future reference:

const registeredRangesSorted = [[1024,4],[1033,76],[1110,381],[1492,291],[1784,410],[2197,62],[2260,109],[2370,8],[2379,303],[2683,111],[2795,30],[2826,47],[2874,51],[2926,166],[3093,33],[3127,174],[3302,20],[3326,41],[3372,31],[3405,141],[3547,147],[3695,299],[3995,53],[4049,95],[4145,49],[4300,15],[4320,4],[4325,7],[4333,4],[4340,23],[4368,12],[4389,8],[4400,24],[4425,9],[4441,18],[4484,5],[4534,5],[4545,15],[4566,5],[4590,16],[4658,35],[4700,5],[4725,9],[4737,11],[4749,8],[4784,8],[4800,5],[4837,15],[4867,5],[4876,10],[4888,2],[4899,4],[4912,4],[4936,2],[4940,3],[4949,5],[4969,3],[4984,8],[4999,17],[5020,15],[5042,34],[5078,10],[5092,3],[5099,9],[5111,2],[5114,4],[5133,5],[5145,2],[5150,8],[5161,8],[5190,8],[5200,4],[5221,17],[5245,10],[5264,2],[5269,4],[5280,3],[5298,13],[5312,7],[5320,2],[5343,2],[5349,16],[5397,41],[5453,4],[5461,5],[5470,6],[5500,8],[5553,5],[5565,5],[5573,3],[5579,8],[5597,9],[5627,13],[5670,15],[5687,3],[5713,18],[5741,8],[5766,6],[5780,8],[5793,2],[5813,2],[5841,2],[5910,4],[5968,2],[5984,10],[5999,68],[6068,10],[6080,9],[6099,20],[6121,4],[6140,10],[6159,5],[6200,2],[6241,4],[6251,3],[6267,3],[6300,2],[6315,3],[6320,3],[6324,3],[6343,2],[6346,2],[6389,2],[6417,5],[6442,5],[6455,2],[6480,10],[6500,4],[6505,7],[6513,3],[6543,2],[6547,5],[6579,5],[6600,3],[6619,11],[6632,5],[6655,3],[6665,9],[6678,2],[6687,4],[6696,2],[6701,6],[6714,3],[6767,5],[6777,2],[6784,8],[6841,2],[6900,2],[6935,2],[6961,6],[6969,2],[6997,30],[7030,2],[7070,4],[7099,3],[7128,2],[7161,14],[7200,3],[7215,2],[7227,3],[7235,3],[7272,12],[7391,5],[7400,3],[7410,2],[7420,2],[7426,6],[7473,2],[7500,2],[7508,4],[7542,10],[7569,2],[7626,6],[7672,6],[7700,2],[7707,2],[7724,5],[7741,4],[7777,3],[7786,2],[7797,6],[7845,3],[7869,4],[7900,4],[7932,2],[7979,4],[7997,13],[8015,2],[8019,5],[8025,2],[8032,3],[8040,5],[8051,10],[8066,2],[8080,4],[8086,3],[8090,2],[8100,3],[8115,4],[8121,2],[8128,5],[8148,2],[8160,3],[8181,4],[8190,3],[8194,2],[8199,4],[8204,5],[8230,3],[8292,3],[8300,2],[8320,3],[8376,5],[8383,2],[8400,6],[8415,3],[8442,4],[8470,5],[8500,4],[8554,2],[8609,7],[8665,2],[8732,2],[8763,4],[8786,2],[8804,2],[8807,2],[8880,2],[8888,7],[8899,3],[8910,4],[8953,2],[8980,2],[8989,3],[8997,6],[9007,5],[9020,7],[9050,2],[9080,2],[9083,11],[9100,8],[9122,2],[9160,5],[9200,18],[9277,11],[9292,4],[9343,4],[9387,4],[9396,2],[9400,3],[9443,3],[9535,2],[9592,9],[9616,3],[9628,5],[9666,3],[9694,2],[9800,3],[9875,2],[9888,2],[9898,3],[9950,7],[9978,2],[9987,2],[9990,21],[10050,2],[10080,2],[10100,5],[10110,2],[10113,5],[10128,2],[10160,3],[10200,2],[10252,2],[10260,2],[10540,5],[10809,2],[11000,2],[11103,4],[11108,5],[11161,5],[11171,5],[11201,2],[11319,3],[11876,2],[12000,11],[12012,2],[12321,2],[13216,3],[13223,2],[13720,3],[13782,2],[13785,2],[13818,6],[13929,2],[14000,3],[14033,2],[14141,3],[14149,2],[14936,2],[15998,6],[16020,2],[16161,2],[16309,3],[16360,2],[16367,2],[16384,2],[16665,2],[16991,5],[17184,2],[17219,7],[17234,2],[17754,3],[18181,7],[18241,3],[18634,2],[19410,3],[19539,3],[19998,6],[20012,3],[20048,2],[20999,2],[21553,2],[21845,5],[22000,6],[22350,2],[23000,6],[23400,3],[23456,2],[24000,7],[24321,3],[24676,3],[25000,10],[25900,4],[25954,2],[26260,4],[26486,2],[27999,3],[29167,2],[29999,6],[31948,2],[32635,2],[32767,11],[33333,2],[33434,2],[34378,2],[34962,3],[35000,7],[35354,4],[36411,2],[38000,3],[38201,3],[40841,3],[41794,4],[42508,3],[43188,4],[43438,4],[44321,3],[45000,3],[45824,2],[46998,4],[47808,2],[48000,6],[48048,3],[48128,2],[49000,2]];
const registeredSinglePorts = new Set([1029,4197,4199,4316,4366,4500,4563,4573,4621,4711,4774,4827,4894,4980,5120,5172,5209,5215,5443,5445,5450,5550,5618,5646,5666,5693,5696,5700,5705,5750,5755,5757,5777,5859,5863,5868,5883,5900,5963,6130,6133,6209,6222,6306,6350,6355,6360,6363,6370,6379,6382,6432,6464,6471,6558,6566,6568,6640,6653,6801,6817,6831,6850,6868,6888,6946,6951,7040,7080,7088,7095,7107,7117,7121,7181,7244,7262,7365,7397,7437,7443,7471,7478,7491,7560,7563,7566,7574,7588,7606,7624,7633,7648,7663,7680,7683,7687,7689,7697,7720,7734,7738,7747,7775,7781,7784,7789,7794,7810,7878,7880,7887,7913,7962,7967,8070,8074,8077,8097,8140,8153,8211,8243,8266,8270,8276,8280,8282,8313,8351,8423,8450,8457,8567,8600,8675,8686,8688,8699,8711,8750,8770,8778,8793,8800,8873,8883,8937,9005,9060,9111,9119,9131,9191,9222,9255,9300,9306,9312,9318,9321,9374,9380,9418,9450,9500,9522,9555,9612,9614,9640,9700,9747,9750,9753,9762,9878,9903,9909,9911,9925,9966,9981,10020,10023,10055,10107,10125,10288,10321,10439,10500,10548,10631,10800,10805,10860,10880,10933,10990,11095,11208,11211,11367,11371,11430,11489,11600,11623,11720,11723,11751,11796,11967,12109,12121,12168,12172,12300,12302,12345,12753,12865,13160,13400,13724,13894,14145,14154,14250,14414,14500,15000,15002,15118,15345,15363,15555,15660,15740,16619,16789,16900,16950,17007,17500,17555,17729,17777,18000,18104,18136,18262,18463,18668,18769,18881,18888,19000,19007,19020,19191,19194,19220,19283,19315,19398,19788,20005,20034,20046,20057,20167,20202,20222,20480,20670,21010,21212,21221,21590,21800,22125,22128,22222,22273,22305,22335,22343,22347,22537,22555,22763,22800,22951,23053,23272,23294,23333,23546,24242,24249,24386,24465,24554,24577,24666,24680,24754,24850,24922,25576,25604,25793,26000,26133,26208,26257,26489,27010,27017,27345,27442,27504,27782,27876,28119,28200,28240,28589,29118,30100,30260,30400,30832,30999,31016,31020,31029,31400,31416,31457,31620,31685,31765,32034,32249,32400,32483,32801,32811,32896,33060,33123,33331,33656,34249,34567,34980,35100,36001,36422,36462,36524,36602,36700,36865,37475,37483,37601,37654,38412,38422,38462,38472,38800,38865,39681,40000,40023,40404,40853,41111,41121,41230,43000,43210,44123,44444,44544,44553,44600,44818,44900,45045,45054,45514,45678,45966,46336,47100,47557,47624,47806,48556,48619,48653,49150]);

const portIsRegistered = port => {
  if (registeredSinglePorts.has(port)) {
    return true;
  }
  for (const [start, length] of registeredRangesSorted) {
    if (port < start) {
      break;
    }
    if (port < start + length) {
      return true;
    }
  }
  return false;
}

@stroncium
Copy link
Contributor Author

stroncium commented Mar 25, 2019

or faster and more compact

const nonregisteredSinglePortsBelow4096 = new Set([1028,1030,1031,1032,1109,1491,1783,2194,2195,2196,2259,2369,2378,2682,2794,2825,2873,2925,3092,3126,3301,3322,3323,3324,3325,3367,3368,3369,3370,3371,3403,3404,3546,3694,3994,4048]);
const registeredRangesAbove4096Sorted = [[4096,48],[4145,49],[4300,15],[4320,4],[4325,7],[4333,4],[4340,23],[4368,12],[4389,8],[4400,24],[4425,9],[4441,18],[4484,5],[4534,5],[4545,15],[4566,5],[4590,16],[4658,35],[4700,5],[4725,9],[4737,11],[4749,8],[4784,8],[4800,5],[4837,15],[4867,5],[4876,10],[4899,4],[4912,4],[4940,3],[4949,5],[4969,3],[4984,8],[4999,17],[5020,15],[5042,34],[5078,10],[5092,3],[5099,9],[5114,4],[5133,5],[5150,8],[5161,8],[5190,8],[5200,4],[5221,17],[5245,10],[5269,4],[5280,3],[5298,13],[5312,7],[5349,16],[5397,41],[5453,4],[5461,5],[5470,6],[5500,8],[5553,5],[5565,5],[5573,3],[5579,8],[5597,9],[5627,13],[5670,15],[5687,3],[5713,18],[5741,8],[5766,6],[5780,8],[5910,4],[5984,10],[5999,68],[6068,10],[6080,9],[6099,20],[6121,4],[6140,10],[6159,5],[6241,4],[6251,3],[6267,3],[6315,3],[6320,3],[6324,3],[6417,5],[6442,5],[6480,10],[6500,4],[6505,7],[6513,3],[6547,5],[6579,5],[6600,3],[6619,11],[6632,5],[6655,3],[6665,9],[6687,4],[6701,6],[6714,3],[6767,5],[6784,8],[6961,6],[6997,30],[7070,4],[7099,3],[7161,14],[7200,3],[7227,3],[7235,3],[7272,12],[7391,5],[7400,3],[7426,6],[7508,4],[7542,10],[7626,6],[7672,6],[7724,5],[7741,4],[7777,3],[7797,6],[7845,3],[7869,4],[7900,4],[7979,4],[7997,13],[8019,5],[8032,3],[8040,5],[8051,10],[8080,4],[8086,3],[8100,3],[8115,4],[8128,5],[8160,3],[8181,4],[8190,3],[8199,4],[8204,5],[8230,3],[8292,3],[8320,3],[8376,5],[8400,6],[8415,3],[8442,4],[8470,5],[8500,4],[8609,7],[8763,4],[8888,7],[8899,3],[8910,4],[8989,3],[8997,6],[9007,5],[9020,7],[9083,11],[9100,8],[9160,5],[9200,18],[9277,11],[9292,4],[9343,4],[9387,4],[9400,3],[9443,3],[9592,9],[9616,3],[9628,5],[9666,3],[9800,3],[9898,3],[9950,7],[9990,21],[10100,5],[10113,5],[10160,3],[10540,5],[11103,4],[11108,5],[11161,5],[11171,5],[11319,3],[12000,11],[13216,3],[13720,3],[13818,6],[14000,3],[14141,3],[15998,6],[16309,3],[16991,5],[17219,7],[17754,3],[18181,7],[18241,3],[19410,3],[19539,3],[19998,6],[20012,3],[21845,5],[22000,6],[23000,6],[23400,3],[24000,7],[24321,3],[24676,3],[25000,10],[25900,4],[26260,4],[27999,3],[29999,6],[32767,11],[34962,3],[35000,7],[35354,4],[38000,3],[38201,3],[40841,3],[41794,4],[42508,3],[43188,4],[43438,4],[44321,3],[45000,3],[46998,4],[48000,6],[48048,3]];
const registeredSinglePortsAbove4096 = new Set([4197,4199,4316,4366,4500,4563,4573,4621,4711,4774,4827,4888,4889,4894,4936,4937,4980,5111,5112,5120,5145,5146,5172,5209,5215,5264,5265,5320,5321,5343,5344,5443,5445,5450,5550,5618,5646,5666,5693,5696,5700,5705,5750,5755,5757,5777,5793,5794,5813,5814,5841,5842,5859,5863,5868,5883,5900,5963,5968,5969,6130,6133,6200,6201,6209,6222,6300,6301,6306,6343,6344,6346,6347,6350,6355,6360,6363,6370,6379,6382,6389,6390,6432,6455,6456,6464,6471,6543,6544,6558,6566,6568,6640,6653,6678,6679,6696,6697,6777,6778,6801,6817,6831,6841,6842,6850,6868,6888,6900,6901,6935,6936,6946,6951,6969,6970,7030,7031,7040,7080,7088,7095,7107,7117,7121,7128,7129,7181,7215,7216,7244,7262,7365,7397,7410,7411,7420,7421,7437,7443,7471,7473,7474,7478,7491,7500,7501,7560,7563,7566,7569,7570,7574,7588,7606,7624,7633,7648,7663,7680,7683,7687,7689,7697,7700,7701,7707,7708,7720,7734,7738,7747,7775,7781,7784,7786,7787,7789,7794,7810,7878,7880,7887,7913,7932,7933,7962,7967,8015,8016,8025,8026,8066,8067,8070,8074,8077,8090,8091,8097,8121,8122,8140,8148,8149,8153,8194,8195,8211,8243,8266,8270,8276,8280,8282,8300,8301,8313,8351,8383,8384,8423,8450,8457,8554,8555,8567,8600,8665,8666,8675,8686,8688,8699,8711,8732,8733,8750,8770,8778,8786,8787,8793,8800,8804,8805,8807,8808,8873,8880,8881,8883,8937,8953,8954,8980,8981,9005,9050,9051,9060,9080,9081,9111,9119,9122,9123,9131,9191,9222,9255,9300,9306,9312,9318,9321,9374,9380,9396,9397,9418,9450,9500,9522,9535,9536,9555,9612,9614,9640,9694,9695,9700,9747,9750,9753,9762,9875,9876,9878,9888,9889,9903,9909,9911,9925,9966,9978,9979,9981,9987,9988,10020,10023,10050,10051,10055,10080,10081,10107,10110,10111,10125,10128,10129,10200,10201,10252,10253,10260,10261,10288,10321,10439,10500,10548,10631,10800,10805,10809,10810,10860,10880,10933,10990,11000,11001,11095,11201,11202,11208,11211,11367,11371,11430,11489,11600,11623,11720,11723,11751,11796,11876,11877,11967,12012,12013,12109,12121,12168,12172,12300,12302,12321,12322,12345,12753,12865,13160,13223,13224,13400,13724,13782,13783,13785,13786,13894,13929,13930,14033,14034,14145,14149,14150,14154,14250,14414,14500,14936,14937,15000,15002,15118,15345,15363,15555,15660,15740,16020,16021,16161,16162,16360,16361,16367,16368,16384,16385,16619,16665,16666,16789,16900,16950,17007,17184,17185,17234,17235,17500,17555,17729,17777,18000,18104,18136,18262,18463,18634,18635,18668,18769,18881,18888,19000,19007,19020,19191,19194,19220,19283,19315,19398,19788,20005,20034,20046,20048,20049,20057,20167,20202,20222,20480,20670,20999,21000,21010,21212,21221,21553,21554,21590,21800,22125,22128,22222,22273,22305,22335,22343,22347,22350,22351,22537,22555,22763,22800,22951,23053,23272,23294,23333,23456,23457,23546,24242,24249,24386,24465,24554,24577,24666,24680,24754,24850,24922,25576,25604,25793,25954,25955,26000,26133,26208,26257,26486,26487,26489,27010,27017,27345,27442,27504,27782,27876,28119,28200,28240,28589,29118,29167,29168,30100,30260,30400,30832,30999,31016,31020,31029,31400,31416,31457,31620,31685,31765,31948,31949,32034,32249,32400,32483,32635,32636,32801,32811,32896,33060,33123,33331,33333,33334,33434,33435,33656,34249,34378,34379,34567,34980,35100,36001,36411,36412,36422,36462,36524,36602,36700,36865,37475,37483,37601,37654,38412,38422,38462,38472,38800,38865,39681,40000,40023,40404,40853,41111,41121,41230,43000,43210,44123,44444,44544,44553,44600,44818,44900,45045,45054,45514,45678,45824,45825,45966,46336,47100,47557,47624,47806,47808,47809,48128,48129,48556,48619,48653,49000,49001,49150]);

const portIsRegistered = port => {
  if (port < 4096) {
    return !nonregisteredSinglePortsBelow4096.has(port);
  }

  if (registeredSinglePortsAbove4096.has(port)) {
    return true;
  }
  for (const [start, length] of registeredRangesAbove4096Sorted) {
    if (port < start) {
      break;
    }
    if (port < start + length) {
      return true;
    }
  }
  return false;
}

On further thought, it is probably a good idea to just always deprioritize those ports when selecting from any range.

@sindresorhus
Copy link
Owner

and propose filter out ports in those ranges and deprioritize them(move to the end of array).

👍

@sindresorhus
Copy link
Owner

sindresorhus commented Mar 25, 2019

How about we make it a generator, so it will lazily create the ports when needed instead of potentially creating a very large array that will just be thrown away? Since a generator is also an iterable, the port option could just be typed to accept an iterable, which makes it flexible enough for both arrays and makeRange().

We could add https://github.com/sindresorhus/get-range as a dependency.

@sindresorhus
Copy link
Owner

Can you give the PR a proper (descriptive) title?

index.js Show resolved Hide resolved
index.d.ts Outdated Show resolved Hide resolved
readme.md Outdated Show resolved Hide resolved
@stroncium stroncium changed the title Fix24 Add makeRange helper Mar 25, 2019
@stroncium
Copy link
Contributor Author

@sindresorhus Eh, I actually migrated it to iterables and generators, but there is a tiny problem: Node.js v6 doesn't support them. What should we do about that?

@stroncium
Copy link
Contributor Author

stroncium commented Mar 25, 2019

@sindresorhus Ok, I changed it to iterables only, without generators and without async.
But it is ugly as hell, and nodejs v6 is going out of maintenance in a month(april 30). Should we maybe just stick to normal solution and wait with releasing this feature until there won't be reason to support v6 anymore? Or maybe even update engines right now, given it's only month till it goes out of use?

@sindresorhus
Copy link
Owner

You can target Node.js 8

index.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
index.js Show resolved Hide resolved
@acostalima
Copy link
Contributor

ping @sindresorhus

@sindresorhus sindresorhus changed the title Add makeRange helper Add makeRange() helper method Apr 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants