RetriableReadableStream class
NODE.JS 런타임에서만 사용할 수 있습니다. Node.js ReadableStream은 내부 ReadableStream 예기치 않은 종료 시 내부적으로 다시 시도합니다.
- Extends
-
Readable
생성자
Retriable |
RetriableReadableStream의 인스턴스를 만듭니다. |
속성
destroyed |
|
readable |
|
readable |
|
readable |
|
readable |
지정된 |
readable |
|
readable |
이 속성은 |
readable |
이 |
readable |
이 속성은 읽을 준비가 된 큐의 바이트 수(또는 개체)를 포함합니다. 이 값은 |
readable |
지정된 |
메서드
add |
이벤트 내보내기 다음을 비롯한 문서에 대해 정의된 이벤트입니다.
|
add |
|
add |
|
add |
|
add |
|
add |
|
add |
|
add |
|
destroy(Error) | 스트림을 삭제합니다. 필요에 따라 구현자는 이 메서드를 재정의하지 말고 대신 |
emit("close") | |
emit("data", any) | |
emit("end") | |
emit("error", Error) | |
emit("pause") | |
emit("readable") | |
emit("resume") | |
emit(string | symbol, any[]) | |
event |
이미터가 수신기를 등록한 이벤트를 나열하는 배열을 반환합니다. 배열의 값은 문자열 또는
|
from(Iterable<any> | Async |
반복기에서 읽기 가능한 스트림을 만들기 위한 유틸리티 메서드입니다. |
get |
|
is |
스트림을 읽었는지 취소했는지 여부를 반환합니다. |
is |
|
listener |
|
listeners(string | symbol) |
|
off(string | symbol, (args: any[]) => void) |
|
on("close", () => void) | |
on("data", (chunk: any) => void) | |
on("end", () => void) | |
on("error", (err: Error) => void) | |
on("pause", () => void) | |
on("readable", () => void) | |
on("resume", () => void) | |
on(string | symbol, (args: any[]) => void) | |
once("close", () => void) | |
once("data", (chunk: any) => void) | |
once("end", () => void) | |
once("error", (err: Error) => void) | |
once("pause", () => void) | |
once("readable", () => void) | |
once("resume", () => void) | |
once(string | symbol, (args: any[]) => void) | |
pause() |
|
pipe<T>(T, undefined | Object) | |
prepend |
|
prepend |
|
prepend |
|
prepend |
|
prepend |
|
prepend |
|
prepend |
|
prepend |
|
prepend |
|
prepend |
|
prepend |
|
prepend |
|
prepend |
|
prepend |
|
prepend |
|
prepend |
|
push(any, Buffer |
|
raw |
래퍼(예:
|
read(undefined | number) |
따라서
개체 모드의
|
remove |
모든 수신기 또는 지정된 호출을 연결할 수 있도록 |
remove |
|
remove |
|
remove |
|
remove |
|
remove |
|
remove |
|
remove |
|
remove |
|
resume() |
|
set |
|
set |
기본적으로 |
unpipe(NodeJS. |
|
unshift(any, Buffer |
<xref:push>달리 |
wrap(Readable |
Node.js 0.10 이전에는 스트림이 현재 정의된 대로 전체
|
생성자 세부 정보
RetriableReadableStream(Aborter, ReadableStream, ReadableStreamGetter, number, number, IRetriableReadableStreamOptions)
RetriableReadableStream의 인스턴스를 만듭니다.
new RetriableReadableStream(aborter: Aborter, source: ReadableStream, getter: ReadableStreamGetter, offset: number, count: number, options?: IRetriableReadableStreamOptions)
매개 변수
- aborter
- Aborter
Aborter.none 또는 Aborter.timeout()을 사용하여 새 Aborter 인스턴스를 만듭니다. 요청 취소에 대한 자세한 예제를 보려면 Aborter의 goto 문서
- source
-
ReadableStream
getter에서 반환된 현재 ReadableStream
- getter
- ReadableStreamGetter
지정된 오프셋에서 새 ReadableStream을 반환하는 다운로드 요청을 호출하는 메서드
- offset
-
number
읽을 원래 데이터 원본의 오프셋 위치
- count
-
number
읽을 원본 데이터 원본의 데이터 양
- options
- IRetriableReadableStreamOptions
속성 세부 정보
destroyed
readable.destroy()
호출된 후 true
.
destroyed: boolean
속성 값
boolean
readable
readable.read()
호출하는 것이 안전한지 true
. 즉, 스트림이 제거되거나 'error'
내보내지 않았거나 'end'
.
readable: boolean
속성 값
boolean
readableAborted
'end'
내보내기 전에 스트림이 제거되었는지 또는 오류가 발생했는지 여부를 반환합니다.
readableAborted: boolean
속성 값
boolean
readableDidRead
'data'
내보내는지 여부를 반환합니다.
readableDidRead: boolean
속성 값
boolean
readableEncoding
지정된 Readable
스트림의 속성 encoding
대한 Getter입니다.
encoding
속성은 readable.setEncoding()
메서드를 사용하여 설정할 수 있습니다.
readableEncoding: BufferEncoding | null
속성 값
BufferEncoding | null
readableEnded
'end'
이벤트가 내보내지면 true
됩니다.
readableEnded: boolean
속성 값
boolean
readableFlowing
이 속성은 Three states
섹션에 설명된 대로 Readable
스트림의 현재 상태를 반영합니다.
readableFlowing: boolean | null
속성 값
boolean | null
readableHighWaterMark
이 Readable
만들 때 전달된 highWaterMark
값을 반환합니다.
readableHighWaterMark: number
속성 값
number
readableLength
이 속성은 읽을 준비가 된 큐의 바이트 수(또는 개체)를 포함합니다. 이 값은 highWaterMark
상태에 대한 내성 데이터를 제공합니다.
readableLength: number
속성 값
number
readableObjectMode
지정된 Readable
스트림의 속성 objectMode
대한 Getter입니다.
readableObjectMode: boolean
속성 값
boolean
메서드 세부 정보
addListener("close", () => void)
이벤트 내보내기 다음을 비롯한 문서에 대해 정의된 이벤트입니다.
- 닫다
- 데이터
- 끝
- 오류
- 일시 중지
- 읽을
- 이력서
function addListener(event: "close", listener: () => void)
매개 변수
- event
-
"close"
- listener
-
() => void
반환
this
addListener("data", (chunk: any) => void)
function addListener(event: "data", listener: (chunk: any) => void)
매개 변수
- event
-
"data"
- listener
-
(chunk: any) => void
반환
this
addListener("end", () => void)
function addListener(event: "end", listener: () => void)
매개 변수
- event
-
"end"
- listener
-
() => void
반환
this
addListener("error", (err: Error) => void)
function addListener(event: "error", listener: (err: Error) => void)
매개 변수
- event
-
"error"
- listener
-
(err: Error) => void
반환
this
addListener("pause", () => void)
function addListener(event: "pause", listener: () => void)
매개 변수
- event
-
"pause"
- listener
-
() => void
반환
this
addListener("readable", () => void)
function addListener(event: "readable", listener: () => void)
매개 변수
- event
-
"readable"
- listener
-
() => void
반환
this
addListener("resume", () => void)
function addListener(event: "resume", listener: () => void)
매개 변수
- event
-
"resume"
- listener
-
() => void
반환
this
addListener(string | symbol, (args: any[]) => void)
function addListener(event: string | symbol, listener: (args: any[]) => void)
매개 변수
- event
-
string | symbol
- listener
-
(args: any[]) => void
반환
this
destroy(Error)
스트림을 삭제합니다. 필요에 따라 'error'
이벤트를 내보내고 'close'
이벤트를 내보낸다(emitClose
false
설정되지 않은 경우). 이 호출 후 읽기 가능한 스트림은 내부 리소스를 해제하고 push()
대한 후속 호출은 무시됩니다.
destroy()
호출되면 추가 호출은 no-op 되고 _destroy()
제외한 추가 오류는 'error'
내보냅니다.
구현자는 이 메서드를 재정의하지 말고 대신 readable._destroy()
구현합니다.
function destroy(error?: Error)
매개 변수
- error
-
Error
'error'
이벤트에서 페이로드로 전달되는 오류
emit("close")
function emit(event: "close")
매개 변수
- event
-
"close"
반환
boolean
emit("data", any)
function emit(event: "data", chunk: any)
매개 변수
- event
-
"data"
- chunk
-
any
반환
boolean
emit("end")
function emit(event: "end")
매개 변수
- event
-
"end"
반환
boolean
emit("error", Error)
function emit(event: "error", err: Error)
매개 변수
- event
-
"error"
- err
-
Error
반환
boolean
emit("pause")
function emit(event: "pause")
매개 변수
- event
-
"pause"
반환
boolean
emit("readable")
function emit(event: "readable")
매개 변수
- event
-
"readable"
반환
boolean
emit("resume")
function emit(event: "resume")
매개 변수
- event
-
"resume"
반환
boolean
emit(string | symbol, any[])
function emit(event: string | symbol, args: any[])
매개 변수
- event
-
string | symbol
- args
-
any[]
반환
boolean
eventNames()
이미터가 수신기를 등록한 이벤트를 나열하는 배열을 반환합니다. 배열의 값은 문자열 또는 Symbol
.
const EventEmitter = require('events');
const myEE = new EventEmitter();
myEE.on('foo', () => {});
myEE.on('bar', () => {});
const sym = Symbol('symbol');
myEE.on(sym, () => {});
console.log(myEE.eventNames());
// Prints: [ 'foo', 'bar', Symbol(symbol) ]
function eventNames()
반환
Array<string | symbol>
from(Iterable<any> | AsyncIterable<any>, ReadableOptions)
반복기에서 읽기 가능한 스트림을 만들기 위한 유틸리티 메서드입니다.
static function from(iterable: Iterable<any> | AsyncIterable<any>, options?: ReadableOptions)
매개 변수
- iterable
-
Iterable<any> | AsyncIterable<any>
- options
-
ReadableOptions
반환
Readable
getMaxListeners()
emitter.setMaxListeners(n)
설정되거나 <xref:defaultMaxListeners>기본값인 EventEmitter
대한 현재 최대 수신기 값을 반환합니다.
function getMaxListeners()
반환
number
isDisturbed(Readable | ReadableStream)
스트림을 읽었는지 취소했는지 여부를 반환합니다.
static function isDisturbed(stream: Readable | ReadableStream)
매개 변수
- stream
-
Readable | ReadableStream
반환
boolean
isPaused()
readable.isPaused()
메서드는Readable
현재 작동 상태를 반환합니다. 이는 주로readable.pipe()
메서드의 기반이 되는 메커니즘에서 사용됩니다. 대부분의 일반적인 경우 이 메서드를 직접 사용할 이유가 없습니다.
const readable = new stream.Readable();
readable.isPaused(); // === false
readable.pause();
readable.isPaused(); // === true
readable.resume();
readable.isPaused(); // === false
function isPaused()
반환
boolean
listenerCount(string | symbol)
eventName
이벤트를 수신 대기하는 수신기 수를 반환합니다.
function listenerCount(eventName: string | symbol)
매개 변수
- eventName
-
string | symbol
수신 대기 중인 이벤트의 이름입니다.
반환
number
listeners(string | symbol)
eventName
이벤트에 대한 수신기 배열의 복사본을 반환합니다.
server.on('connection', (stream) => {
console.log('someone connected!');
});
console.log(util.inspect(server.listeners('connection')));
// Prints: [ [Function] ]
function listeners(eventName: string | symbol)
매개 변수
- eventName
-
string | symbol
반환
Function[]
off(string | symbol, (args: any[]) => void)
emitter.removeListener()
별칭입니다.
function off(eventName: string | symbol, listener: (args: any[]) => void)
매개 변수
- eventName
-
string | symbol
- listener
-
(args: any[]) => void
반환
this
on("close", () => void)
function on(event: "close", listener: () => void)
매개 변수
- event
-
"close"
- listener
-
() => void
반환
this
on("data", (chunk: any) => void)
function on(event: "data", listener: (chunk: any) => void)
매개 변수
- event
-
"data"
- listener
-
(chunk: any) => void
반환
this
on("end", () => void)
function on(event: "end", listener: () => void)
매개 변수
- event
-
"end"
- listener
-
() => void
반환
this
on("error", (err: Error) => void)
function on(event: "error", listener: (err: Error) => void)
매개 변수
- event
-
"error"
- listener
-
(err: Error) => void
반환
this
on("pause", () => void)
function on(event: "pause", listener: () => void)
매개 변수
- event
-
"pause"
- listener
-
() => void
반환
this
on("readable", () => void)
function on(event: "readable", listener: () => void)
매개 변수
- event
-
"readable"
- listener
-
() => void
반환
this
on("resume", () => void)
function on(event: "resume", listener: () => void)
매개 변수
- event
-
"resume"
- listener
-
() => void
반환
this
on(string | symbol, (args: any[]) => void)
function on(event: string | symbol, listener: (args: any[]) => void)
매개 변수
- event
-
string | symbol
- listener
-
(args: any[]) => void
반환
this
once("close", () => void)
function once(event: "close", listener: () => void)
매개 변수
- event
-
"close"
- listener
-
() => void
반환
this
once("data", (chunk: any) => void)
function once(event: "data", listener: (chunk: any) => void)
매개 변수
- event
-
"data"
- listener
-
(chunk: any) => void
반환
this
once("end", () => void)
function once(event: "end", listener: () => void)
매개 변수
- event
-
"end"
- listener
-
() => void
반환
this
once("error", (err: Error) => void)
function once(event: "error", listener: (err: Error) => void)
매개 변수
- event
-
"error"
- listener
-
(err: Error) => void
반환
this
once("pause", () => void)
function once(event: "pause", listener: () => void)
매개 변수
- event
-
"pause"
- listener
-
() => void
반환
this
once("readable", () => void)
function once(event: "readable", listener: () => void)
매개 변수
- event
-
"readable"
- listener
-
() => void
반환
this
once("resume", () => void)
function once(event: "resume", listener: () => void)
매개 변수
- event
-
"resume"
- listener
-
() => void
반환
this
once(string | symbol, (args: any[]) => void)
function once(event: string | symbol, listener: (args: any[]) => void)
매개 변수
- event
-
string | symbol
- listener
-
(args: any[]) => void
반환
this
pause()
readable.pause()
메서드를 사용하면 흐름 모드의 스트림이 'data'
이벤트 내보내기를 중지하고 흐름 모드에서 전환됩니다. 사용 가능한 모든 데이터는 내부 버퍼에 유지됩니다.
const readable = getReadableStreamSomehow();
readable.on('data', (chunk) => {
console.log(`Received ${chunk.length} bytes of data.`);
readable.pause();
console.log('There will be no additional data for 1 second.');
setTimeout(() => {
console.log('Now data will start flowing again.');
readable.resume();
}, 1000);
});
'readable'
이벤트 수신기가 있는 경우 readable.pause()
메서드는 영향을 주지 않습니다.
function pause()
반환
this
pipe<T>(T, undefined | Object)
function pipe<T>(destination: T, options?: undefined | Object)
매개 변수
- destination
-
T
- options
-
undefined | Object
반환
T
prependListener("close", () => void)
function prependListener(event: "close", listener: () => void)
매개 변수
- event
-
"close"
- listener
-
() => void
반환
this
prependListener("data", (chunk: any) => void)
function prependListener(event: "data", listener: (chunk: any) => void)
매개 변수
- event
-
"data"
- listener
-
(chunk: any) => void
반환
this
prependListener("end", () => void)
function prependListener(event: "end", listener: () => void)
매개 변수
- event
-
"end"
- listener
-
() => void
반환
this
prependListener("error", (err: Error) => void)
function prependListener(event: "error", listener: (err: Error) => void)
매개 변수
- event
-
"error"
- listener
-
(err: Error) => void
반환
this
prependListener("pause", () => void)
function prependListener(event: "pause", listener: () => void)
매개 변수
- event
-
"pause"
- listener
-
() => void
반환
this
prependListener("readable", () => void)
function prependListener(event: "readable", listener: () => void)
매개 변수
- event
-
"readable"
- listener
-
() => void
반환
this
prependListener("resume", () => void)
function prependListener(event: "resume", listener: () => void)
매개 변수
- event
-
"resume"
- listener
-
() => void
반환
this
prependListener(string | symbol, (args: any[]) => void)
function prependListener(event: string | symbol, listener: (args: any[]) => void)
매개 변수
- event
-
string | symbol
- listener
-
(args: any[]) => void
반환
this
prependOnceListener("close", () => void)
function prependOnceListener(event: "close", listener: () => void)
매개 변수
- event
-
"close"
- listener
-
() => void
반환
this
prependOnceListener("data", (chunk: any) => void)
function prependOnceListener(event: "data", listener: (chunk: any) => void)
매개 변수
- event
-
"data"
- listener
-
(chunk: any) => void
반환
this
prependOnceListener("end", () => void)
function prependOnceListener(event: "end", listener: () => void)
매개 변수
- event
-
"end"
- listener
-
() => void
반환
this
prependOnceListener("error", (err: Error) => void)
function prependOnceListener(event: "error", listener: (err: Error) => void)
매개 변수
- event
-
"error"
- listener
-
(err: Error) => void
반환
this
prependOnceListener("pause", () => void)
function prependOnceListener(event: "pause", listener: () => void)
매개 변수
- event
-
"pause"
- listener
-
() => void
반환
this
prependOnceListener("readable", () => void)
function prependOnceListener(event: "readable", listener: () => void)
매개 변수
- event
-
"readable"
- listener
-
() => void
반환
this
prependOnceListener("resume", () => void)
function prependOnceListener(event: "resume", listener: () => void)
매개 변수
- event
-
"resume"
- listener
-
() => void
반환
this
prependOnceListener(string | symbol, (args: any[]) => void)
function prependOnceListener(event: string | symbol, listener: (args: any[]) => void)
매개 변수
- event
-
string | symbol
- listener
-
(args: any[]) => void
반환
this
push(any, BufferEncoding)
function push(chunk: any, encoding?: BufferEncoding)
매개 변수
- chunk
-
any
- encoding
-
BufferEncoding
반환
boolean
rawListeners(string | symbol)
래퍼(예: .once()
만든 래퍼)를 포함하여 eventName
이벤트에 대한 수신기 배열의 복사본을 반환합니다.
const emitter = new EventEmitter();
emitter.once('log', () => console.log('log once'));
// Returns a new Array with a function `onceWrapper` which has a property
// `listener` which contains the original listener bound above
const listeners = emitter.rawListeners('log');
const logFnWrapper = listeners[0];
// Logs "log once" to the console and does not unbind the `once` event
logFnWrapper.listener();
// Logs "log once" to the console and removes the listener
logFnWrapper();
emitter.on('log', () => console.log('log persistently'));
// Will return a new Array with a single function bound by `.on()` above
const newListeners = emitter.rawListeners('log');
// Logs "log persistently" twice
newListeners[0]();
emitter.emit('log');
function rawListeners(eventName: string | symbol)
매개 변수
- eventName
-
string | symbol
반환
Function[]
read(undefined | number)
readable.read()
메서드는 내부 버퍼에서 일부 데이터를 가져와서 반환합니다. 읽을 수 있는 데이터가 없으면 null
반환됩니다. 기본적으로 readable.setEncoding()
메서드를 사용하여 인코딩을 지정하거나 스트림이 개체 모드에서 작동하지 않는 한 데이터는 Buffer
개체로 반환됩니다.
선택적 size
인수는 읽을 특정 바이트 수를 지정합니다.
size
바이트를 읽을 수 없는 경우 스트림이 종료된 _unless_the null
반환되며, 이 경우 내부 버퍼에 남아 있는 모든 데이터가 반환됩니다.
size
인수를 지정하지 않으면 내부 버퍼에 포함된 모든 데이터가 반환됩니다.
size
인수는 1GiB보다 작거나 같아야 합니다.
readable.read()
메서드는 일시 중지된 모드에서 작동하는 Readable
스트림에서만 호출되어야 합니다. 흐름 모드에서는 내부 버퍼가 완전히 드레이닝될 때까지 readable.read()
자동으로 호출됩니다.
const readable = getReadableStreamSomehow();
// 'readable' may be triggered multiple times as data is buffered in
readable.on('readable', () => {
let chunk;
console.log('Stream is readable (new data received in buffer)');
// Use a loop to make sure we read all currently available data
while (null !== (chunk = readable.read())) {
console.log(`Read ${chunk.length} bytes of data...`);
}
});
// 'end' will be triggered once when there is no more data available
readable.on('end', () => {
console.log('Reached end of stream.');
});
readable.read()
대한 각 호출은 데이터 청크 또는 null
반환합니다. 청크는 연결되지 않습니다. 현재 버퍼에 있는 모든 데이터를 사용하려면 while
루프가 필요합니다. 큰 파일을 읽을 때 .read()
지금까지 버퍼링된 모든 콘텐츠를 사용한 null
반환할 수 있지만 아직 버퍼링되지 않은 데이터가 더 많이 있습니다. 이 경우 버퍼에 데이터가 더 있을 때 새 'readable'
이벤트가 내보내집니다. 마지막으로 'end'
이벤트는 더 이상 올 데이터가 없을 때 내보내집니다.
따라서 readable
파일의 전체 내용을 읽으려면 여러 'readable'
이벤트에서 청크를 수집해야 합니다.
const chunks = [];
readable.on('readable', () => {
let chunk;
while (null !== (chunk = readable.read())) {
chunks.push(chunk);
}
});
readable.on('end', () => {
const content = chunks.join('');
});
개체 모드의 Readable
스트림은size
인수의 값에 관계없이 항상 readable.read(size)
호출에서 단일 항목을 반환합니다.
readable.read()
메서드가 데이터 청크를 반환하는 경우 'data'
이벤트도 내보내됩니다.
'end'
이벤트가 내보내진 후 <xref:read> 호출하면 null
반환됩니다. 런타임 오류가 발생하지 않습니다.
function read(size?: undefined | number)
매개 변수
- size
-
undefined | number
읽을 데이터의 양을 지정하는 선택적 인수입니다.
반환
any
removeAllListeners(string | symbol)
모든 수신기 또는 지정된 eventName
제거합니다.
특히 다른 구성 요소 또는 모듈(예: 소켓 또는 파일 스트림)에서 EventEmitter
인스턴스를 만든 경우 코드의 다른 위치에 추가된 수신기를 제거하는 것은 잘못된 방법입니다.
호출을 연결할 수 있도록 EventEmitter
대한 참조를 반환합니다.
function removeAllListeners(event?: string | symbol)
매개 변수
- event
-
string | symbol
반환
this
removeListener("close", () => void)
function removeListener(event: "close", listener: () => void)
매개 변수
- event
-
"close"
- listener
-
() => void
반환
this
removeListener("data", (chunk: any) => void)
function removeListener(event: "data", listener: (chunk: any) => void)
매개 변수
- event
-
"data"
- listener
-
(chunk: any) => void
반환
this
removeListener("end", () => void)
function removeListener(event: "end", listener: () => void)
매개 변수
- event
-
"end"
- listener
-
() => void
반환
this
removeListener("error", (err: Error) => void)
function removeListener(event: "error", listener: (err: Error) => void)
매개 변수
- event
-
"error"
- listener
-
(err: Error) => void
반환
this
removeListener("pause", () => void)
function removeListener(event: "pause", listener: () => void)
매개 변수
- event
-
"pause"
- listener
-
() => void
반환
this
removeListener("readable", () => void)
function removeListener(event: "readable", listener: () => void)
매개 변수
- event
-
"readable"
- listener
-
() => void
반환
this
removeListener("resume", () => void)
function removeListener(event: "resume", listener: () => void)
매개 변수
- event
-
"resume"
- listener
-
() => void
반환
this
removeListener(string | symbol, (args: any[]) => void)
function removeListener(event: string | symbol, listener: (args: any[]) => void)
매개 변수
- event
-
string | symbol
- listener
-
(args: any[]) => void
반환
this
resume()
readable.resume()
메서드를 사용하면 명시적으로 일시 중지된 Readable
스트림이 'data'
이벤트 내보내기를 다시 시작하여 스트림을 흐름 모드로 전환합니다.
readable.resume()
메서드는 실제로 해당 데이터를 처리하지 않고 스트림의 데이터를 완전히 사용하는 데 사용할 수 있습니다.
getReadableStreamSomehow()
.resume()
.on('end', () => {
console.log('Reached the end, but did not read anything.');
});
'readable'
이벤트 수신기가 있는 경우 readable.resume()
메서드는 영향을 주지 않습니다.
function resume()
반환
this
setEncoding(BufferEncoding)
readable.setEncoding()
메서드는 Readable
스트림에서 읽은 데이터에 대한 문자 인코딩을 설정합니다.
기본적으로 인코딩은 할당되지 않으며 스트림 데이터는Buffer
개체로 반환됩니다. 인코딩을 설정하면 스트림 데이터가 Buffer
개체가 아닌 지정된 인코딩의 문자열로 반환됩니다. 예를 들어 readable.setEncoding('utf8')
호출하면 출력 데이터가 UTF-8 데이터로 해석되고 문자열로 전달됩니다.
readable.setEncoding('hex')
호출하면 데이터가 16진수 문자열 형식으로 인코딩됩니다.
Readable
스트림은 스트림을 통해 전달되는 멀티바이트 문자를 제대로 처리합니다. 그렇지 않으면 스트림에서 단순히 Buffer
개체로 끌어온 경우 부적절하게 디코딩됩니다.
const readable = getReadableStreamSomehow();
readable.setEncoding('utf8');
readable.on('data', (chunk) => {
assert.equal(typeof chunk, 'string');
console.log('Got %d characters of string data:', chunk.length);
});
function setEncoding(encoding: BufferEncoding)
매개 변수
- encoding
-
BufferEncoding
사용할 인코딩입니다.
반환
this
setMaxListeners(number)
기본적으로 EventEmitter
특정 이벤트에 대해 10
개 이상의 수신기가 추가되면 경고를 출력합니다. 이는 메모리 누수 찾기에 도움이 되는 유용한 기본값입니다.
emitter.setMaxListeners()
메서드를 사용하면 이 특정 EventEmitter
인스턴스에 대한 제한을 수정할 수 있습니다. 값을Infinity
(또는 0
)로 설정하여 무제한 수신기 수를 나타낼 수 있습니다.
호출을 연결할 수 있도록 EventEmitter
대한 참조를 반환합니다.
function setMaxListeners(n: number)
매개 변수
- n
-
number
반환
this
unpipe(NodeJS.WritableStream)
readable.unpipe()
메서드는 <xref:pipe> 메서드를 사용하여 이전에 연결된 Writable
스트림을 분리합니다.
destination
지정하지 않으면 모든 파이프가 분리됩니다.
destination
지정되었지만 파이프가 설정되지 않은 경우 메서드는 아무 작업도 수행하지 않습니다.
const fs = require('fs');
const readable = getReadableStreamSomehow();
const writable = fs.createWriteStream('file.txt');
// All the data from readable goes into 'file.txt',
// but only for the first second.
readable.pipe(writable);
setTimeout(() => {
console.log('Stop writing to file.txt.');
readable.unpipe(writable);
console.log('Manually close the file stream.');
writable.end();
}, 1000);
function unpipe(destination?: NodeJS.WritableStream)
매개 변수
- destination
- NodeJS.WritableStream
피프 해제할 선택적 특정 스트림
반환
this
unshift(any, BufferEncoding)
null
chunk
전달하면 EOF(스트림의 끝)가 신호로 표시되고 readable.push(null)
동일하게 동작하며, 그 후에는 더 이상 데이터를 쓸 수 없습니다. EOF 신호는 버퍼의 끝에 배치되고 버퍼링된 데이터는 계속 플러시됩니다.
readable.unshift()
메서드는 데이터 청크를 다시 내부 버퍼로 푸시합니다. 이는 스트림이 원본에서 낙관적으로 끌어온 일부 양의 데이터를 "사용 취소"해야 하는 코드에서 스트림을 사용하는 경우에 유용하므로 데이터가 다른 당사자에게 전달될 수 있습니다.
'end'
이벤트가 내보내지거나 런타임 오류가 throw된 후에는 stream.unshift(chunk)
메서드를 호출할 수 없습니다.
stream.unshift()
사용하는 개발자는 대신 Transform
스트림을 사용하도록 전환하는 것을 고려해야 합니다. 자세한 내용은 API for stream implementers
섹션을 참조하세요.
// Pull off a header delimited by \n\n.
// Use unshift() if we get too much.
// Call the callback with (error, header, stream).
const { StringDecoder } = require('string_decoder');
function parseHeader(stream, callback) {
stream.on('error', callback);
stream.on('readable', onReadable);
const decoder = new StringDecoder('utf8');
let header = '';
function onReadable() {
let chunk;
while (null !== (chunk = stream.read())) {
const str = decoder.write(chunk);
if (str.match(/\n\n/)) {
// Found the header boundary.
const split = str.split(/\n\n/);
header += split.shift();
const remaining = split.join('\n\n');
const buf = Buffer.from(remaining, 'utf8');
stream.removeListener('error', callback);
// Remove the 'readable' listener before unshifting.
stream.removeListener('readable', onReadable);
if (buf.length)
stream.unshift(buf);
// Now the body of the message can be read from the stream.
callback(null, header, stream);
} else {
// Still reading the header.
header += str;
}
}
}
}
<xref:push>달리 stream.unshift(chunk)
스트림의 내부 읽기 상태를 다시 설정하여 읽기 프로세스를 종료하지 않습니다.
이렇게 하면 읽기 중에 readable.unshift()
호출되는 경우(즉, 사용자 지정 스트림의 <xref:_read> 구현 내에서) 예기치 않은 결과가 발생할 수 있습니다. 즉각적인 <xref:push>readable.unshift()
호출하면 읽기 상태가 적절하게 다시 설정되지만 읽기를 수행하는 동안 readable.unshift()
호출하지 않는 것이 가장 좋습니다.
function unshift(chunk: any, encoding?: BufferEncoding)
매개 변수
- chunk
-
any
읽기 큐로 이동하지 않는 데이터 청크입니다. 개체 모드에서 작동하지 않는 스트림의 경우 chunk
문자열, Buffer
, Uint8Array
또는 null
합니다. 개체 모드 스트림의 경우 chunk
JavaScript 값일 수 있습니다.
- encoding
-
BufferEncoding
문자열 청크의 인코딩입니다.
'utf8'
또는 'ascii'
같은 유효한 Buffer
인코딩이어야 합니다.
wrap(ReadableStream)
Node.js 0.10 이전에는 스트림이 현재 정의된 대로 전체 stream
모듈 API를 구현하지 않았습니다. 자세한 내용은 Compatibility
참조하세요. 'data'
이벤트를 내보내고 권고 전용인 <xref:pause> 메서드가 있는 이전 Node.js 라이브러리를 사용하는 경우readable.wrap()
메서드를 사용하여 이전 스트림을 데이터 원본으로 사용하는 Readable
스트림을 만들 수 있습니다.
readable.wrap()
사용할 필요는 거의 없지만 이전 Node.js 애플리케이션 및 라이브러리와 상호 작용하기 위한 편의를 위해 메서드가 제공되었습니다.
const { OldReader } = require('./old-api-module.js');
const { Readable } = require('stream');
const oreader = new OldReader();
const myReader = new Readable().wrap(oreader);
myReader.on('readable', () => {
myReader.read(); // etc.
});
function wrap(stream: ReadableStream)
매개 변수
- stream
-
ReadableStream
읽을 수 있는 "이전 스타일" 스트림
반환
this