Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const Minipass = require('minipass')
  2. class SizeError extends Error {
  3. constructor (found, expect) {
  4. super(`Bad data size: expected ${expect} bytes, but got ${found}`)
  5. this.expect = expect
  6. this.found = found
  7. this.code = 'EBADSIZE'
  8. Error.captureStackTrace(this, this.constructor)
  9. }
  10. get name () {
  11. return 'SizeError'
  12. }
  13. }
  14. class MinipassSized extends Minipass {
  15. constructor (options = {}) {
  16. super(options)
  17. if (options.objectMode)
  18. throw new TypeError(`${
  19. this.constructor.name
  20. } streams only work with string and buffer data`)
  21. this.found = 0
  22. this.expect = options.size
  23. if (typeof this.expect !== 'number' ||
  24. this.expect > Number.MAX_SAFE_INTEGER ||
  25. isNaN(this.expect) ||
  26. this.expect < 0 ||
  27. !isFinite(this.expect) ||
  28. this.expect !== Math.floor(this.expect))
  29. throw new Error('invalid expected size: ' + this.expect)
  30. }
  31. write (chunk, encoding, cb) {
  32. const buffer = Buffer.isBuffer(chunk) ? chunk
  33. : typeof chunk === 'string' ?
  34. Buffer.from(chunk, typeof encoding === 'string' ? encoding : 'utf8')
  35. : chunk
  36. if (!Buffer.isBuffer(buffer)) {
  37. this.emit('error', new TypeError(`${
  38. this.constructor.name
  39. } streams only work with string and buffer data`))
  40. return false
  41. }
  42. this.found += buffer.length
  43. if (this.found > this.expect)
  44. this.emit('error', new SizeError(this.found, this.expect))
  45. return super.write(chunk, encoding, cb)
  46. }
  47. emit (ev, ...data) {
  48. if (ev === 'end') {
  49. if (this.found !== this.expect)
  50. this.emit('error', new SizeError(this.found, this.expect))
  51. }
  52. return super.emit(ev, ...data)
  53. }
  54. }
  55. MinipassSized.SizeError = SizeError
  56. module.exports = MinipassSized