index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. module.exports = (function() {
  2. var __MODS__ = {};
  3. var __DEFINE__ = function(modId, func, req) { var m = { exports: {}, _tempexports: {} }; __MODS__[modId] = { status: 0, func: func, req: req, m: m }; };
  4. var __REQUIRE__ = function(modId, source) { if(!__MODS__[modId]) return require(source); if(!__MODS__[modId].status) { var m = __MODS__[modId].m; m._exports = m._tempexports; var desp = Object.getOwnPropertyDescriptor(m, "exports"); if (desp && desp.configurable) Object.defineProperty(m, "exports", { set: function (val) { if(typeof val === "object" && val !== m._exports) { m._exports.__proto__ = val.__proto__; Object.keys(val).forEach(function (k) { m._exports[k] = val[k]; }); } m._tempexports = val }, get: function () { return m._tempexports; } }); __MODS__[modId].status = 1; __MODS__[modId].func(__MODS__[modId].req, m, m.exports); } return __MODS__[modId].m.exports; };
  5. var __REQUIRE_WILDCARD__ = function(obj) { if(obj && obj.__esModule) { return obj; } else { var newObj = {}; if(obj != null) { for(var k in obj) { if (Object.prototype.hasOwnProperty.call(obj, k)) newObj[k] = obj[k]; } } newObj.default = obj; return newObj; } };
  6. var __REQUIRE_DEFAULT__ = function(obj) { return obj && obj.__esModule ? obj.default : obj; };
  7. __DEFINE__(1682324647456, function(require, module, exports) {
  8. const DuplexStream = require('readable-stream').Duplex
  9. const inherits = require('inherits')
  10. const BufferList = require('./BufferList')
  11. function BufferListStream (callback) {
  12. if (!(this instanceof BufferListStream)) {
  13. return new BufferListStream(callback)
  14. }
  15. if (typeof callback === 'function') {
  16. this._callback = callback
  17. const piper = function piper (err) {
  18. if (this._callback) {
  19. this._callback(err)
  20. this._callback = null
  21. }
  22. }.bind(this)
  23. this.on('pipe', function onPipe (src) {
  24. src.on('error', piper)
  25. })
  26. this.on('unpipe', function onUnpipe (src) {
  27. src.removeListener('error', piper)
  28. })
  29. callback = null
  30. }
  31. BufferList._init.call(this, callback)
  32. DuplexStream.call(this)
  33. }
  34. inherits(BufferListStream, DuplexStream)
  35. Object.assign(BufferListStream.prototype, BufferList.prototype)
  36. BufferListStream.prototype._new = function _new (callback) {
  37. return new BufferListStream(callback)
  38. }
  39. BufferListStream.prototype._write = function _write (buf, encoding, callback) {
  40. this._appendBuffer(buf)
  41. if (typeof callback === 'function') {
  42. callback()
  43. }
  44. }
  45. BufferListStream.prototype._read = function _read (size) {
  46. if (!this.length) {
  47. return this.push(null)
  48. }
  49. size = Math.min(size, this.length)
  50. this.push(this.slice(0, size))
  51. this.consume(size)
  52. }
  53. BufferListStream.prototype.end = function end (chunk) {
  54. DuplexStream.prototype.end.call(this, chunk)
  55. if (this._callback) {
  56. this._callback(null, this.slice())
  57. this._callback = null
  58. }
  59. }
  60. BufferListStream.prototype._destroy = function _destroy (err, cb) {
  61. this._bufs.length = 0
  62. this.length = 0
  63. cb(err)
  64. }
  65. BufferListStream.prototype._isBufferList = function _isBufferList (b) {
  66. return b instanceof BufferListStream || b instanceof BufferList || BufferListStream.isBufferList(b)
  67. }
  68. BufferListStream.isBufferList = BufferList.isBufferList
  69. module.exports = BufferListStream
  70. module.exports.BufferListStream = BufferListStream
  71. module.exports.BufferList = BufferList
  72. }, function(modId) {var map = {"./BufferList":1682324647457}; return __REQUIRE__(map[modId], modId); })
  73. __DEFINE__(1682324647457, function(require, module, exports) {
  74. const { Buffer } = require('buffer')
  75. const symbol = Symbol.for('BufferList')
  76. function BufferList (buf) {
  77. if (!(this instanceof BufferList)) {
  78. return new BufferList(buf)
  79. }
  80. BufferList._init.call(this, buf)
  81. }
  82. BufferList._init = function _init (buf) {
  83. Object.defineProperty(this, symbol, { value: true })
  84. this._bufs = []
  85. this.length = 0
  86. if (buf) {
  87. this.append(buf)
  88. }
  89. }
  90. BufferList.prototype._new = function _new (buf) {
  91. return new BufferList(buf)
  92. }
  93. BufferList.prototype._offset = function _offset (offset) {
  94. if (offset === 0) {
  95. return [0, 0]
  96. }
  97. let tot = 0
  98. for (let i = 0; i < this._bufs.length; i++) {
  99. const _t = tot + this._bufs[i].length
  100. if (offset < _t || i === this._bufs.length - 1) {
  101. return [i, offset - tot]
  102. }
  103. tot = _t
  104. }
  105. }
  106. BufferList.prototype._reverseOffset = function (blOffset) {
  107. const bufferId = blOffset[0]
  108. let offset = blOffset[1]
  109. for (let i = 0; i < bufferId; i++) {
  110. offset += this._bufs[i].length
  111. }
  112. return offset
  113. }
  114. BufferList.prototype.get = function get (index) {
  115. if (index > this.length || index < 0) {
  116. return undefined
  117. }
  118. const offset = this._offset(index)
  119. return this._bufs[offset[0]][offset[1]]
  120. }
  121. BufferList.prototype.slice = function slice (start, end) {
  122. if (typeof start === 'number' && start < 0) {
  123. start += this.length
  124. }
  125. if (typeof end === 'number' && end < 0) {
  126. end += this.length
  127. }
  128. return this.copy(null, 0, start, end)
  129. }
  130. BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) {
  131. if (typeof srcStart !== 'number' || srcStart < 0) {
  132. srcStart = 0
  133. }
  134. if (typeof srcEnd !== 'number' || srcEnd > this.length) {
  135. srcEnd = this.length
  136. }
  137. if (srcStart >= this.length) {
  138. return dst || Buffer.alloc(0)
  139. }
  140. if (srcEnd <= 0) {
  141. return dst || Buffer.alloc(0)
  142. }
  143. const copy = !!dst
  144. const off = this._offset(srcStart)
  145. const len = srcEnd - srcStart
  146. let bytes = len
  147. let bufoff = (copy && dstStart) || 0
  148. let start = off[1]
  149. // copy/slice everything
  150. if (srcStart === 0 && srcEnd === this.length) {
  151. if (!copy) {
  152. // slice, but full concat if multiple buffers
  153. return this._bufs.length === 1
  154. ? this._bufs[0]
  155. : Buffer.concat(this._bufs, this.length)
  156. }
  157. // copy, need to copy individual buffers
  158. for (let i = 0; i < this._bufs.length; i++) {
  159. this._bufs[i].copy(dst, bufoff)
  160. bufoff += this._bufs[i].length
  161. }
  162. return dst
  163. }
  164. // easy, cheap case where it's a subset of one of the buffers
  165. if (bytes <= this._bufs[off[0]].length - start) {
  166. return copy
  167. ? this._bufs[off[0]].copy(dst, dstStart, start, start + bytes)
  168. : this._bufs[off[0]].slice(start, start + bytes)
  169. }
  170. if (!copy) {
  171. // a slice, we need something to copy in to
  172. dst = Buffer.allocUnsafe(len)
  173. }
  174. for (let i = off[0]; i < this._bufs.length; i++) {
  175. const l = this._bufs[i].length - start
  176. if (bytes > l) {
  177. this._bufs[i].copy(dst, bufoff, start)
  178. bufoff += l
  179. } else {
  180. this._bufs[i].copy(dst, bufoff, start, start + bytes)
  181. bufoff += l
  182. break
  183. }
  184. bytes -= l
  185. if (start) {
  186. start = 0
  187. }
  188. }
  189. // safeguard so that we don't return uninitialized memory
  190. if (dst.length > bufoff) return dst.slice(0, bufoff)
  191. return dst
  192. }
  193. BufferList.prototype.shallowSlice = function shallowSlice (start, end) {
  194. start = start || 0
  195. end = typeof end !== 'number' ? this.length : end
  196. if (start < 0) {
  197. start += this.length
  198. }
  199. if (end < 0) {
  200. end += this.length
  201. }
  202. if (start === end) {
  203. return this._new()
  204. }
  205. const startOffset = this._offset(start)
  206. const endOffset = this._offset(end)
  207. const buffers = this._bufs.slice(startOffset[0], endOffset[0] + 1)
  208. if (endOffset[1] === 0) {
  209. buffers.pop()
  210. } else {
  211. buffers[buffers.length - 1] = buffers[buffers.length - 1].slice(0, endOffset[1])
  212. }
  213. if (startOffset[1] !== 0) {
  214. buffers[0] = buffers[0].slice(startOffset[1])
  215. }
  216. return this._new(buffers)
  217. }
  218. BufferList.prototype.toString = function toString (encoding, start, end) {
  219. return this.slice(start, end).toString(encoding)
  220. }
  221. BufferList.prototype.consume = function consume (bytes) {
  222. // first, normalize the argument, in accordance with how Buffer does it
  223. bytes = Math.trunc(bytes)
  224. // do nothing if not a positive number
  225. if (Number.isNaN(bytes) || bytes <= 0) return this
  226. while (this._bufs.length) {
  227. if (bytes >= this._bufs[0].length) {
  228. bytes -= this._bufs[0].length
  229. this.length -= this._bufs[0].length
  230. this._bufs.shift()
  231. } else {
  232. this._bufs[0] = this._bufs[0].slice(bytes)
  233. this.length -= bytes
  234. break
  235. }
  236. }
  237. return this
  238. }
  239. BufferList.prototype.duplicate = function duplicate () {
  240. const copy = this._new()
  241. for (let i = 0; i < this._bufs.length; i++) {
  242. copy.append(this._bufs[i])
  243. }
  244. return copy
  245. }
  246. BufferList.prototype.append = function append (buf) {
  247. if (buf == null) {
  248. return this
  249. }
  250. if (buf.buffer) {
  251. // append a view of the underlying ArrayBuffer
  252. this._appendBuffer(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength))
  253. } else if (Array.isArray(buf)) {
  254. for (let i = 0; i < buf.length; i++) {
  255. this.append(buf[i])
  256. }
  257. } else if (this._isBufferList(buf)) {
  258. // unwrap argument into individual BufferLists
  259. for (let i = 0; i < buf._bufs.length; i++) {
  260. this.append(buf._bufs[i])
  261. }
  262. } else {
  263. // coerce number arguments to strings, since Buffer(number) does
  264. // uninitialized memory allocation
  265. if (typeof buf === 'number') {
  266. buf = buf.toString()
  267. }
  268. this._appendBuffer(Buffer.from(buf))
  269. }
  270. return this
  271. }
  272. BufferList.prototype._appendBuffer = function appendBuffer (buf) {
  273. this._bufs.push(buf)
  274. this.length += buf.length
  275. }
  276. BufferList.prototype.indexOf = function (search, offset, encoding) {
  277. if (encoding === undefined && typeof offset === 'string') {
  278. encoding = offset
  279. offset = undefined
  280. }
  281. if (typeof search === 'function' || Array.isArray(search)) {
  282. throw new TypeError('The "value" argument must be one of type string, Buffer, BufferList, or Uint8Array.')
  283. } else if (typeof search === 'number') {
  284. search = Buffer.from([search])
  285. } else if (typeof search === 'string') {
  286. search = Buffer.from(search, encoding)
  287. } else if (this._isBufferList(search)) {
  288. search = search.slice()
  289. } else if (Array.isArray(search.buffer)) {
  290. search = Buffer.from(search.buffer, search.byteOffset, search.byteLength)
  291. } else if (!Buffer.isBuffer(search)) {
  292. search = Buffer.from(search)
  293. }
  294. offset = Number(offset || 0)
  295. if (isNaN(offset)) {
  296. offset = 0
  297. }
  298. if (offset < 0) {
  299. offset = this.length + offset
  300. }
  301. if (offset < 0) {
  302. offset = 0
  303. }
  304. if (search.length === 0) {
  305. return offset > this.length ? this.length : offset
  306. }
  307. const blOffset = this._offset(offset)
  308. let blIndex = blOffset[0] // index of which internal buffer we're working on
  309. let buffOffset = blOffset[1] // offset of the internal buffer we're working on
  310. // scan over each buffer
  311. for (; blIndex < this._bufs.length; blIndex++) {
  312. const buff = this._bufs[blIndex]
  313. while (buffOffset < buff.length) {
  314. const availableWindow = buff.length - buffOffset
  315. if (availableWindow >= search.length) {
  316. const nativeSearchResult = buff.indexOf(search, buffOffset)
  317. if (nativeSearchResult !== -1) {
  318. return this._reverseOffset([blIndex, nativeSearchResult])
  319. }
  320. buffOffset = buff.length - search.length + 1 // end of native search window
  321. } else {
  322. const revOffset = this._reverseOffset([blIndex, buffOffset])
  323. if (this._match(revOffset, search)) {
  324. return revOffset
  325. }
  326. buffOffset++
  327. }
  328. }
  329. buffOffset = 0
  330. }
  331. return -1
  332. }
  333. BufferList.prototype._match = function (offset, search) {
  334. if (this.length - offset < search.length) {
  335. return false
  336. }
  337. for (let searchOffset = 0; searchOffset < search.length; searchOffset++) {
  338. if (this.get(offset + searchOffset) !== search[searchOffset]) {
  339. return false
  340. }
  341. }
  342. return true
  343. }
  344. ;(function () {
  345. const methods = {
  346. readDoubleBE: 8,
  347. readDoubleLE: 8,
  348. readFloatBE: 4,
  349. readFloatLE: 4,
  350. readInt32BE: 4,
  351. readInt32LE: 4,
  352. readUInt32BE: 4,
  353. readUInt32LE: 4,
  354. readInt16BE: 2,
  355. readInt16LE: 2,
  356. readUInt16BE: 2,
  357. readUInt16LE: 2,
  358. readInt8: 1,
  359. readUInt8: 1,
  360. readIntBE: null,
  361. readIntLE: null,
  362. readUIntBE: null,
  363. readUIntLE: null
  364. }
  365. for (const m in methods) {
  366. (function (m) {
  367. if (methods[m] === null) {
  368. BufferList.prototype[m] = function (offset, byteLength) {
  369. return this.slice(offset, offset + byteLength)[m](0, byteLength)
  370. }
  371. } else {
  372. BufferList.prototype[m] = function (offset = 0) {
  373. return this.slice(offset, offset + methods[m])[m](0)
  374. }
  375. }
  376. }(m))
  377. }
  378. }())
  379. // Used internally by the class and also as an indicator of this object being
  380. // a `BufferList`. It's not possible to use `instanceof BufferList` in a browser
  381. // environment because there could be multiple different copies of the
  382. // BufferList class and some `BufferList`s might be `BufferList`s.
  383. BufferList.prototype._isBufferList = function _isBufferList (b) {
  384. return b instanceof BufferList || BufferList.isBufferList(b)
  385. }
  386. BufferList.isBufferList = function isBufferList (b) {
  387. return b != null && b[symbol]
  388. }
  389. module.exports = BufferList
  390. }, function(modId) { var map = {}; return __REQUIRE__(map[modId], modId); })
  391. return __REQUIRE__(1682324647456);
  392. })()
  393. //miniprogram-npm-outsideDeps=["readable-stream","inherits","buffer"]
  394. //# sourceMappingURL=index.js.map