@@ -25,7 +25,6 @@
|
|||||||
"@xen-orchestra/log": "^0.2.0",
|
"@xen-orchestra/log": "^0.2.0",
|
||||||
"async-iterator-to-stream": "^1.0.2",
|
"async-iterator-to-stream": "^1.0.2",
|
||||||
"core-js": "^3.0.0",
|
"core-js": "^3.0.0",
|
||||||
"from2": "^2.3.0",
|
|
||||||
"fs-extra": "^8.0.1",
|
"fs-extra": "^8.0.1",
|
||||||
"limit-concurrency-decorator": "^0.4.0",
|
"limit-concurrency-decorator": "^0.4.0",
|
||||||
"lodash": "^4.17.4",
|
"lodash": "^4.17.4",
|
||||||
|
|||||||
@@ -1,42 +0,0 @@
|
|||||||
import from2 from 'from2'
|
|
||||||
|
|
||||||
const constantStream = (data, n = 1) => {
|
|
||||||
if (!Buffer.isBuffer(data)) {
|
|
||||||
data = Buffer.from(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
const { length } = data
|
|
||||||
|
|
||||||
if (!length) {
|
|
||||||
throw new Error('data should not be empty')
|
|
||||||
}
|
|
||||||
|
|
||||||
n *= length
|
|
||||||
let currentLength = length
|
|
||||||
|
|
||||||
return from2((size, next) => {
|
|
||||||
if (n <= 0) {
|
|
||||||
return next(null, null)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (n < size) {
|
|
||||||
size = n
|
|
||||||
}
|
|
||||||
|
|
||||||
if (size < currentLength) {
|
|
||||||
const m = Math.floor(size / length) * length || length
|
|
||||||
n -= m
|
|
||||||
return next(null, data.slice(0, m))
|
|
||||||
}
|
|
||||||
|
|
||||||
// if more than twice the data length is requested, repeat the data
|
|
||||||
if (size > currentLength * 2) {
|
|
||||||
currentLength = Math.floor(size / length) * length
|
|
||||||
data = Buffer.alloc(currentLength, data)
|
|
||||||
}
|
|
||||||
|
|
||||||
n -= currentLength
|
|
||||||
return next(null, data)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
export { constantStream as default }
|
|
||||||
Reference in New Issue
Block a user