11cb0ef41Sopenharmony_ci'use strict'
21cb0ef41Sopenharmony_ciconst { Minipass } = require('minipass')
31cb0ef41Sopenharmony_ciconst TYPE = Symbol('type')
41cb0ef41Sopenharmony_ciconst BUFFER = Symbol('buffer')
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciclass Blob {
71cb0ef41Sopenharmony_ci  constructor (blobParts, options) {
81cb0ef41Sopenharmony_ci    this[TYPE] = ''
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci    const buffers = []
111cb0ef41Sopenharmony_ci    let size = 0
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci    if (blobParts) {
141cb0ef41Sopenharmony_ci      const a = blobParts
151cb0ef41Sopenharmony_ci      const length = Number(a.length)
161cb0ef41Sopenharmony_ci      for (let i = 0; i < length; i++) {
171cb0ef41Sopenharmony_ci        const element = a[i]
181cb0ef41Sopenharmony_ci        const buffer = element instanceof Buffer ? element
191cb0ef41Sopenharmony_ci          : ArrayBuffer.isView(element)
201cb0ef41Sopenharmony_ci            ? Buffer.from(element.buffer, element.byteOffset, element.byteLength)
211cb0ef41Sopenharmony_ci            : element instanceof ArrayBuffer ? Buffer.from(element)
221cb0ef41Sopenharmony_ci            : element instanceof Blob ? element[BUFFER]
231cb0ef41Sopenharmony_ci            : typeof element === 'string' ? Buffer.from(element)
241cb0ef41Sopenharmony_ci            : Buffer.from(String(element))
251cb0ef41Sopenharmony_ci        size += buffer.length
261cb0ef41Sopenharmony_ci        buffers.push(buffer)
271cb0ef41Sopenharmony_ci      }
281cb0ef41Sopenharmony_ci    }
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci    this[BUFFER] = Buffer.concat(buffers, size)
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci    const type = options && options.type !== undefined
331cb0ef41Sopenharmony_ci      && String(options.type).toLowerCase()
341cb0ef41Sopenharmony_ci    if (type && !/[^\u0020-\u007E]/.test(type)) {
351cb0ef41Sopenharmony_ci      this[TYPE] = type
361cb0ef41Sopenharmony_ci    }
371cb0ef41Sopenharmony_ci  }
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  get size () {
401cb0ef41Sopenharmony_ci    return this[BUFFER].length
411cb0ef41Sopenharmony_ci  }
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  get type () {
441cb0ef41Sopenharmony_ci    return this[TYPE]
451cb0ef41Sopenharmony_ci  }
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  text () {
481cb0ef41Sopenharmony_ci    return Promise.resolve(this[BUFFER].toString())
491cb0ef41Sopenharmony_ci  }
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci  arrayBuffer () {
521cb0ef41Sopenharmony_ci    const buf = this[BUFFER]
531cb0ef41Sopenharmony_ci    const off = buf.byteOffset
541cb0ef41Sopenharmony_ci    const len = buf.byteLength
551cb0ef41Sopenharmony_ci    const ab = buf.buffer.slice(off, off + len)
561cb0ef41Sopenharmony_ci    return Promise.resolve(ab)
571cb0ef41Sopenharmony_ci  }
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  stream () {
601cb0ef41Sopenharmony_ci    return new Minipass().end(this[BUFFER])
611cb0ef41Sopenharmony_ci  }
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci  slice (start, end, type) {
641cb0ef41Sopenharmony_ci    const size = this.size
651cb0ef41Sopenharmony_ci    const relativeStart = start === undefined ? 0
661cb0ef41Sopenharmony_ci      : start < 0 ? Math.max(size + start, 0)
671cb0ef41Sopenharmony_ci      : Math.min(start, size)
681cb0ef41Sopenharmony_ci    const relativeEnd = end === undefined ? size
691cb0ef41Sopenharmony_ci      : end < 0 ? Math.max(size + end, 0)
701cb0ef41Sopenharmony_ci      : Math.min(end, size)
711cb0ef41Sopenharmony_ci    const span = Math.max(relativeEnd - relativeStart, 0)
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci    const buffer = this[BUFFER]
741cb0ef41Sopenharmony_ci    const slicedBuffer = buffer.slice(
751cb0ef41Sopenharmony_ci      relativeStart,
761cb0ef41Sopenharmony_ci      relativeStart + span
771cb0ef41Sopenharmony_ci    )
781cb0ef41Sopenharmony_ci    const blob = new Blob([], { type })
791cb0ef41Sopenharmony_ci    blob[BUFFER] = slicedBuffer
801cb0ef41Sopenharmony_ci    return blob
811cb0ef41Sopenharmony_ci  }
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci  get [Symbol.toStringTag] () {
841cb0ef41Sopenharmony_ci    return 'Blob'
851cb0ef41Sopenharmony_ci  }
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci  static get BUFFER () {
881cb0ef41Sopenharmony_ci    return BUFFER
891cb0ef41Sopenharmony_ci  }
901cb0ef41Sopenharmony_ci}
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ciObject.defineProperties(Blob.prototype, {
931cb0ef41Sopenharmony_ci  size: { enumerable: true },
941cb0ef41Sopenharmony_ci  type: { enumerable: true },
951cb0ef41Sopenharmony_ci})
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_cimodule.exports = Blob
98