feat(utils/streamToBuffer): rebase on get-stream and use everywhere (#295)
This commit is contained in:
parent
bb75d42ede
commit
5b8608c186
@ -1,5 +1,4 @@
|
|||||||
import assign from 'lodash.assign'
|
import assign from 'lodash.assign'
|
||||||
import getStream from 'get-stream'
|
|
||||||
import startsWith from 'lodash.startswith'
|
import startsWith from 'lodash.startswith'
|
||||||
import { parse as parseUrl } from 'url'
|
import { parse as parseUrl } from 'url'
|
||||||
import { request as httpRequest } from 'http'
|
import { request as httpRequest } from 'http'
|
||||||
@ -7,7 +6,8 @@ import { request as httpsRequest } from 'https'
|
|||||||
import { stringify as formatQueryString } from 'querystring'
|
import { stringify as formatQueryString } from 'querystring'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
isString
|
isString,
|
||||||
|
streamToBuffer
|
||||||
} from './utils'
|
} from './utils'
|
||||||
|
|
||||||
// -------------------------------------------------------------------
|
// -------------------------------------------------------------------
|
||||||
@ -90,7 +90,7 @@ export default (...args) => {
|
|||||||
response.cancel = () => {
|
response.cancel = () => {
|
||||||
req.abort()
|
req.abort()
|
||||||
}
|
}
|
||||||
response.readAll = () => getStream(response)
|
response.readAll = () => streamToBuffer(response)
|
||||||
|
|
||||||
const length = response.headers['content-length']
|
const length = response.headers['content-length']
|
||||||
if (length) {
|
if (length) {
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import eventToPromise from 'event-to-promise'
|
import eventToPromise from 'event-to-promise'
|
||||||
import getStream from 'get-stream'
|
|
||||||
import through2 from 'through2'
|
import through2 from 'through2'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@ -10,6 +9,7 @@ import {
|
|||||||
addChecksumToReadStream,
|
addChecksumToReadStream,
|
||||||
noop,
|
noop,
|
||||||
pCatch,
|
pCatch,
|
||||||
|
streamToBuffer,
|
||||||
validChecksumOfReadStream
|
validChecksumOfReadStream
|
||||||
} from '../utils'
|
} from '../utils'
|
||||||
|
|
||||||
@ -62,8 +62,8 @@ export default class RemoteHandlerAbstract {
|
|||||||
return this._readFile(file, options)
|
return this._readFile(file, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
async _readFile (file, options) {
|
_readFile (file, options) {
|
||||||
return getStream(await this.createReadStream(file, options))
|
return this.createReadStream(file, options).then(streamToBuffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
async rename (oldPath, newPath) {
|
async rename (oldPath, newPath) {
|
||||||
|
13
src/utils.js
13
src/utils.js
@ -1,6 +1,7 @@
|
|||||||
import base64url from 'base64url'
|
import base64url from 'base64url'
|
||||||
import eventToPromise from 'event-to-promise'
|
import eventToPromise from 'event-to-promise'
|
||||||
import forEach from 'lodash.foreach'
|
import forEach from 'lodash.foreach'
|
||||||
|
import getStream from 'get-stream'
|
||||||
import has from 'lodash.has'
|
import has from 'lodash.has'
|
||||||
import highland from 'highland'
|
import highland from 'highland'
|
||||||
import humanFormat from 'human-format'
|
import humanFormat from 'human-format'
|
||||||
@ -45,17 +46,7 @@ export function bufferToStream (buf) {
|
|||||||
return stream
|
return stream
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function streamToBuffer (stream) {
|
export const streamToBuffer = getStream.buffer
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const bufs = []
|
|
||||||
|
|
||||||
stream.on('error', reject)
|
|
||||||
stream.on('data', data => {
|
|
||||||
bufs.push(data)
|
|
||||||
})
|
|
||||||
stream.on('end', () => resolve(Buffer.concat(bufs)))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// -------------------------------------------------------------------
|
// -------------------------------------------------------------------
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user