]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/request/node_modules/bl/bl.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / node_modules / request / node_modules / bl / bl.js
1 var DuplexStream = require('readable-stream/duplex')
2   , util         = require('util')
3
4
5 function BufferList (callback) {
6   if (!(this instanceof BufferList))
7     return new BufferList(callback)
8
9   this._bufs  = []
10   this.length = 0
11
12   if (typeof callback == 'function') {
13     this._callback = callback
14
15     var piper = function piper (err) {
16       if (this._callback) {
17         this._callback(err)
18         this._callback = null
19       }
20     }.bind(this)
21
22     this.on('pipe', function onPipe (src) {
23       src.on('error', piper)
24     })
25     this.on('unpipe', function onUnpipe (src) {
26       src.removeListener('error', piper)
27     })
28   } else {
29     this.append(callback)
30   }
31
32   DuplexStream.call(this)
33 }
34
35
36 util.inherits(BufferList, DuplexStream)
37
38
39 BufferList.prototype._offset = function _offset (offset) {
40   var tot = 0, i = 0, _t
41   for (; i < this._bufs.length; i++) {
42     _t = tot + this._bufs[i].length
43     if (offset < _t)
44       return [ i, offset - tot ]
45     tot = _t
46   }
47 }
48
49
50 BufferList.prototype.append = function append (buf) {
51   var i = 0
52     , newBuf
53
54   if (Array.isArray(buf)) {
55     for (; i < buf.length; i++)
56       this.append(buf[i])
57   } else if (buf instanceof BufferList) {
58     // unwrap argument into individual BufferLists
59     for (; i < buf._bufs.length; i++)
60       this.append(buf._bufs[i])
61   } else if (buf != null) {
62     // coerce number arguments to strings, since Buffer(number) does
63     // uninitialized memory allocation
64     if (typeof buf == 'number')
65       buf = buf.toString()
66
67     newBuf = Buffer.isBuffer(buf) ? buf : new Buffer(buf)
68     this._bufs.push(newBuf)
69     this.length += newBuf.length
70   }
71
72   return this
73 }
74
75
76 BufferList.prototype._write = function _write (buf, encoding, callback) {
77   this.append(buf)
78
79   if (typeof callback == 'function')
80     callback()
81 }
82
83
84 BufferList.prototype._read = function _read (size) {
85   if (!this.length)
86     return this.push(null)
87
88   size = Math.min(size, this.length)
89   this.push(this.slice(0, size))
90   this.consume(size)
91 }
92
93
94 BufferList.prototype.end = function end (chunk) {
95   DuplexStream.prototype.end.call(this, chunk)
96
97   if (this._callback) {
98     this._callback(null, this.slice())
99     this._callback = null
100   }
101 }
102
103
104 BufferList.prototype.get = function get (index) {
105   return this.slice(index, index + 1)[0]
106 }
107
108
109 BufferList.prototype.slice = function slice (start, end) {
110   return this.copy(null, 0, start, end)
111 }
112
113
114 BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) {
115   if (typeof srcStart != 'number' || srcStart < 0)
116     srcStart = 0
117   if (typeof srcEnd != 'number' || srcEnd > this.length)
118     srcEnd = this.length
119   if (srcStart >= this.length)
120     return dst || new Buffer(0)
121   if (srcEnd <= 0)
122     return dst || new Buffer(0)
123
124   var copy   = !!dst
125     , off    = this._offset(srcStart)
126     , len    = srcEnd - srcStart
127     , bytes  = len
128     , bufoff = (copy && dstStart) || 0
129     , start  = off[1]
130     , l
131     , i
132
133   // copy/slice everything
134   if (srcStart === 0 && srcEnd == this.length) {
135     if (!copy) // slice, just return a full concat
136       return Buffer.concat(this._bufs)
137
138     // copy, need to copy individual buffers
139     for (i = 0; i < this._bufs.length; i++) {
140       this._bufs[i].copy(dst, bufoff)
141       bufoff += this._bufs[i].length
142     }
143
144     return dst
145   }
146
147   // easy, cheap case where it's a subset of one of the buffers
148   if (bytes <= this._bufs[off[0]].length - start) {
149     return copy
150       ? this._bufs[off[0]].copy(dst, dstStart, start, start + bytes)
151       : this._bufs[off[0]].slice(start, start + bytes)
152   }
153
154   if (!copy) // a slice, we need something to copy in to
155     dst = new Buffer(len)
156
157   for (i = off[0]; i < this._bufs.length; i++) {
158     l = this._bufs[i].length - start
159
160     if (bytes > l) {
161       this._bufs[i].copy(dst, bufoff, start)
162     } else {
163       this._bufs[i].copy(dst, bufoff, start, start + bytes)
164       break
165     }
166
167     bufoff += l
168     bytes -= l
169
170     if (start)
171       start = 0
172   }
173
174   return dst
175 }
176
177 BufferList.prototype.toString = function toString (encoding, start, end) {
178   return this.slice(start, end).toString(encoding)
179 }
180
181 BufferList.prototype.consume = function consume (bytes) {
182   while (this._bufs.length) {
183     if (bytes >= this._bufs[0].length) {
184       bytes -= this._bufs[0].length
185       this.length -= this._bufs[0].length
186       this._bufs.shift()
187     } else {
188       this._bufs[0] = this._bufs[0].slice(bytes)
189       this.length -= bytes
190       break
191     }
192   }
193   return this
194 }
195
196
197 BufferList.prototype.duplicate = function duplicate () {
198   var i = 0
199     , copy = new BufferList()
200
201   for (; i < this._bufs.length; i++)
202     copy.append(this._bufs[i])
203
204   return copy
205 }
206
207
208 BufferList.prototype.destroy = function destroy () {
209   this._bufs.length = 0
210   this.length = 0
211   this.push(null)
212 }
213
214
215 ;(function () {
216   var methods = {
217       'readDoubleBE' : 8
218     , 'readDoubleLE' : 8
219     , 'readFloatBE'  : 4
220     , 'readFloatLE'  : 4
221     , 'readInt32BE'  : 4
222     , 'readInt32LE'  : 4
223     , 'readUInt32BE' : 4
224     , 'readUInt32LE' : 4
225     , 'readInt16BE'  : 2
226     , 'readInt16LE'  : 2
227     , 'readUInt16BE' : 2
228     , 'readUInt16LE' : 2
229     , 'readInt8'     : 1
230     , 'readUInt8'    : 1
231   }
232
233   for (var m in methods) {
234     (function (m) {
235       BufferList.prototype[m] = function (offset) {
236         return this.slice(offset, offset + methods[m])[m](0)
237       }
238     }(m))
239   }
240 }())
241
242
243 module.exports = BufferList