UNPKG

4.13 kBJavaScriptView Raw
1
2exports.deepAssign = deepAssign
3exports.lineEmitter = lineEmitter
4exports.rand = rand
5exports.round = round
6exports.uuid4 = uuid4
7exports.wait = wait
8
9exports.urlRe = /^([-.\da-z]+:)?\/\/(([\da-z.]*)(?::(\d+))?)(\/.*?)(\?.*?)?(#.*)?$/
10exports.domainRe = /^(?:(?:xn-|[a-z\d]+)(?:-[a-z\d]+)*(?:\.(?=.)|$)){2,}/i
11exports.ipv4Re = /^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?=.)|$)){4}$/
12exports.ipv6Re = /^((?=.*::)(([1-9a-f][\da-f]{0,3}|0|)(:(?=[^:])|::(?!.*::)|$)){1,8}|([\da-f]{1,4}(:(?=[^:])|$)){8})$/i
13exports.buf2ip = buf2ip
14exports.int2ip = int2ip
15exports.ip2buf = ip2buf
16exports.ip2int = ip2int
17exports.ipInNet = ipInNet
18
19var hasOwn = {}.hasOwnProperty
20
21function deepAssign(to) {
22 for (var key, from, a = arguments, i = 1, len = a.length; i < len; ) {
23 if (from = a[i++]) for (key in from) if (hasOwn.call(from, key)) {
24 to[key] = (
25 from[key] && from[key].constructor === Object ?
26 deepAssign(to[key] && to[key].constructor === Object ? to[key] : {}, from[key]) :
27 from[key]
28 )
29 }
30 }
31 return to
32}
33
34// Usage:
35// var client = net.connect(soc)
36// .on("line", function(line) {})
37// lineEmitter(client)
38
39function lineEmitter(emitter, opts) {
40 opts = opts || {}
41
42 var leftover = ""
43 , separator = opts.separator || /\r?\n/
44 , emit = opts.emit || "line"
45 , listen = opts.listen || "data"
46 , end = opts.end || "end"
47
48 emitter
49 .on(listen, lineEmitterData)
50 .on(end, lineEmitterEnd)
51
52 function lineEmitterData(data){
53 var lines = (leftover + data).split(separator)
54
55 // keep the last partial line buffered
56 leftover = lines.pop()
57
58 for (var i = 0, len = lines.length; i < len; ) {
59 this.emit(emit, lines[i++], len)
60 }
61 }
62 function lineEmitterEnd() {
63 if (leftover) {
64 this.emit(emit, leftover)
65 }
66 leftover = ""
67 emitter
68 .removeListener(listen, lineEmitterData)
69 .removeListener(end, lineEmitterEnd)
70 }
71 return emitter
72}
73
74function uuid4(a, b) {
75 for (a = b = ""; a++ < 36; ) {
76 b += a*51&52 ? ( a^15 ? 8 ^ Math.random() * (a^20?16:4) : 4 ).toString(16) : "-";
77 }
78 return b
79}
80
81function rand(len) {
82 for (var out = ""; out.length < len; ) {
83 out += (Date.now() * Math.random()).toString(36).split(".")[0]
84 }
85 return out.slice(-len)
86}
87
88// Rounding Errors
89// Number((1.005).toFixed(2)); // 1 instead of 1.01
90// Math.round(1.005*100)/100; // 1 instead of 1.01
91// http://www.jacklmoore.com/notes/rounding-in-javascript/
92// The rounding problem can be avoided by using numbers represented in exponential notation:
93
94function round(value, decimals) {
95 return +(Math.round(value + "e" + decimals) + "e-" + decimals)
96}
97
98
99function wait(fn, _pending) {
100 var pending = _pending || 0
101 , result = [null]
102
103 if (typeof fn !== "function") throw TypeError("Not a function")
104
105 function resume() {
106 if (--pending === 0) {
107 fn.apply(this, result)
108 }
109 }
110 resume.wait = function(pos) {
111 ++pending
112 if (pos === void 0) pos = pending
113 return function(err, res) {
114 if (err) {
115 pending = 0
116 fn.call(this, err)
117 } else {
118 result[pos] = res
119 resume()
120 }
121 }
122 }
123 return resume
124}
125
126function ip2int(str) {
127 var t = str.split(".")
128 return ((t[0] << 24) | (t[1] << 16) | (t[2] << 8 ) | (t[3]))>>>0
129}
130
131function int2ip(i) {
132 return [i>>>24, (i>>>16)&255, (i>>>8)&255, i&255].join(".")
133}
134
135// var re = /^((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])(?:\.(?=.)|$)){4}$/
136function ipInNet(ip, cidr) {
137 var junks = cidr.split("/")
138 , bits = junks[1] || 8 * junks[0].split(".").length
139 , mask = bits < 31 ? (-1 << (32 - bits)) >>> 0 : ip2int(bits)
140 //, netInt = (ip2int(cidr) & mask) >>> 0
141 //, size = 1 << (32 - bits)
142 //, last = netInt + size - 1
143
144 return (ip2int(ip) & mask) >>> 0 === (ip2int(junks[0]) & mask) >>> 0
145}
146
147function ip2buf(str) {
148 var arr = str.split(":", 8)
149 , len = arr.length
150 if (arr[len - 1].length > 4) {
151 return Buffer.from(arr[len - 1].split(".", 4))
152 }
153 for (; len--; ) {
154 arr[len] = (
155 arr[len] !== "" ?
156 ("0000" + arr[len]).slice(-4) :
157 "00000000000000000000000000000000".slice(-4*(9 - arr.length))
158 )
159 }
160 return Buffer.from(arr.join(""), "hex")
161}
162
163function buf2ip(buf) {
164 if (buf.length === 4) {
165 return buf.join(".")
166 }
167 return buf.toString("hex").replace(/.{4}(?=.)/g, "$&:")
168}
169
170
\No newline at end of file