@sanity/migrate
Version:
Tooling for running data migrations on Sanity.io projects
50 lines (47 loc) • 1.33 kB
text/typescript
import {concatUint8Arrays} from '../uint8arrays/index.js'
export function peekInto(readable: ReadableStream, options: {size: number}) {
const {size} = options
return new Promise<[head: Uint8Array, ReadableStream]>((resolve, reject) => {
let totalBytesRead = 0
let streamCompleted = false
const chunks: Array<Uint8Array> = []
const reader = readable.getReader()
function settled() {
const head = concatUint8Arrays(chunks)
resolve([
head,
new ReadableStream<Uint8Array>({
async pull(controller) {
const {done, value} = await reader.read()
if (done) {
controller.close()
} else {
controller.enqueue(value)
}
},
start(controller) {
controller.enqueue(head)
if (streamCompleted) {
controller.close()
}
},
}),
])
}
;(async () => {
while (true) {
const {done, value: chunk} = await reader.read()
if (done) {
streamCompleted = true
break
} else {
totalBytesRead += chunk.byteLength
chunks.push(chunk)
if (totalBytesRead >= size) {
break
}
}
}
})().then(settled, reject)
})
}