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