For AI agents: the complete documentation index is available at https://canary.gukhanmun.org/ko-Kore/llms.txt, the full documentation bundle is available at https://canary.gukhanmun.org/ko-Kore/llms-full.txt, and this page is available as Markdown at https://canary.gukhanmun.org/ko-Kore/guide/javascript/streaming.md.
  • 韓國語 (國漢文)
  • 스트리밍 API

    g.stream(format?)은 文字列 청크를 받는 TransformStream<string, string>을 返還합니다. 現在 WebAssembly와 Node-API 具顯은 모두 入力 全體를 버퍼링하고 쓰기 側이 닫힐 때 變換합니다. 이는 청크 인터페이스이며, 메모리 使用量을 制限하거나 出力을 미리 내보내는 스트리밍이 아닙니다.

    基本 使用法

    import { 
    function load(options?: GukhanmunLoadOptions): Promise<Gukhanmun>

    Creates a Gukhanmun converter with the given options.

    Initialises the WASM module on the first call (subsequent calls reuse the cached module). Compiled dictionaries supplied via GukhanmunOptions.dictionaries are fetched, while MapDictionarySource entry lists are serialised and built in memory as Rust MapDictionary values.

    Note: unlike the Rust ko-kr preset, the JavaScript preset never includes a bundled dictionary. Pass dictionaries: [await stdictFst()] to include the Standard Korean Language Dictionary.

    @paramoptions - Conversion options. All fields are optional; defaults match the ko-kr preset.@returnsA Gukhanmun instance.@throws{@linkGukhanmunError } on invalid options or dictionary load failure.
    load
    } from "@gukhanmun/wasm";
    import {
    function stdictFst(): Promise<FileDictionarySource>

    Loads the bundled Standard Korean Language Dictionary as a

    FileDictionarySource ready to pass to load({ dictionaries: [...] }).

    @returnsA FileDictionarySource with format: "fst" and role: "reference".
    stdictFst
    } from "@gukhanmun/stdict-fst";
    const
    const g: Gukhanmun
    g
    = await
    function load(options?: GukhanmunLoadOptions): Promise<Gukhanmun>

    Creates a Gukhanmun converter with the given options.

    Initialises the WASM module on the first call (subsequent calls reuse the cached module). Compiled dictionaries supplied via GukhanmunOptions.dictionaries are fetched, while MapDictionarySource entry lists are serialised and built in memory as Rust MapDictionary values.

    Note: unlike the Rust ko-kr preset, the JavaScript preset never includes a bundled dictionary. Pass dictionaries: [await stdictFst()] to include the Standard Korean Language Dictionary.

    @paramoptions - Conversion options. All fields are optional; defaults match the ko-kr preset.@returnsA Gukhanmun instance.@throws{@linkGukhanmunError } on invalid options or dictionary load failure.
    load
    ({
    dictionaries?: readonly (DictionarySource | LoadedDictionary)[] | undefined
    dictionaries
    : [await
    function stdictFst(): Promise<FileDictionarySource>

    Loads the bundled Standard Korean Language Dictionary as a

    FileDictionarySource ready to pass to load({ dictionaries: [...] }).

    @returnsA FileDictionarySource with format: "fst" and role: "reference".
    stdictFst
    ()] });
    const
    const stream: TransformStream<string, string>
    stream
    =
    const g: Gukhanmun
    g
    .
    Gukhanmun.stream(format?: MediaType): TransformStream<string, string> (+2 overloads)

    Returns a TransformStream<string, string> that converts chunks incrementally. Chunks are JavaScript strings; byte-level encoding is the caller's responsibility (TextDecoderStream / TextEncoderStream).

    The stream guarantees that the concatenated output equals the result of calling convert on the concatenated input, regardless of chunk boundaries. Document-wide middlewares (e.g., homophone marking with homophoneWindow: "per-document") buffer until the writable side is closed.

    @paramformat - Input/output media type. Defaults to "text/plain".@returnsA platform TransformStream<string, string>.@throws{@linkGukhanmunError } on initialisation failure (not on chunk errors; those are signalled via the stream's error channel).
    stream
    ("text/html");
    const
    const writer: WritableStreamDefaultWriter<string>
    writer
    =
    const stream: TransformStream<string, string>
    stream
    .
    TransformStream<string, string>.writable: WritableStream<string>

    The writable read-only property of the TransformStream interface returns the WritableStream instance controlled by this TransformStream. This stream accepts input data that will be transformed and emitted to the readable stream.

    MDN Reference

    writable
    .
    WritableStream<string>.getWriter(): WritableStreamDefaultWriter<string>

    The getWriter() method of the WritableStream interface returns a new instance of WritableStreamDefaultWriter and locks the stream to that instance. While the stream is locked, no other writer can be acquired until this one is released.

    MDN Reference

    getWriter
    ();
    const
    const reader: ReadableStreamDefaultReader<string>
    reader
    =
    const stream: TransformStream<string, string>
    stream
    .
    TransformStream<string, string>.readable: ReadableStream<string>

    The readable read-only property of the TransformStream interface returns the ReadableStream instance controlled by this TransformStream. This stream emits the transformed output data.

    MDN Reference

    readable
    .
    ReadableStream<string>.getReader(): ReadableStreamDefaultReader<string> (+2 overloads)

    The getReader() method of the ReadableStream interface creates a reader and locks the stream to it. While the stream is locked, no other reader can be acquired until this one is released.

    MDN Reference

    getReader
    ();
    // 쓰기가 막히지 않도록 쓰기 前에 읽기를 始作합니다. const
    const output: Promise<string>
    output
    = (async () => {
    const
    const parts: string[]
    parts
    : string[] = [];
    for (;;) { const {
    const done: boolean
    done
    ,
    const value: string | undefined
    value
    } = await
    const reader: ReadableStreamDefaultReader<string>
    reader
    .
    ReadableStreamDefaultReader<string>.read(): Promise<ReadableStreamReadResult<string>>

    The read() method of the ReadableStreamDefaultReader interface returns a Promise providing access to the next chunk in the stream's internal queue.

    MDN Reference

    read
    ();
    if (
    const done: boolean
    done
    ) return
    const parts: string[]
    parts
    .
    Array<string>.join(separator?: string): string

    Adds all the elements of an array into a string, separated by the specified separator string.

    @paramseparator A string used to separate one element of the array from the next in the resulting string. If omitted, the array elements are separated with a comma.
    join
    ("");
    const parts: string[]
    parts
    .
    Array<string>.push(...items: string[]): number

    Appends new elements to the end of an array, and returns the new length of the array.

    @paramitems New elements to add to the array.
    push
    (
    const value: string
    value
    );
    } })(); await
    const writer: WritableStreamDefaultWriter<string>
    writer
    .
    WritableStreamDefaultWriter<string>.write(chunk?: string | undefined): Promise<void>

    The write() method of the WritableStreamDefaultWriter interface writes a passed chunk of data to a WritableStream and its underlying sink, then returns a Promise that resolves to indicate the success or failure of the write operation.

    MDN Reference

    write
    ("<p>漢");
    await
    const writer: WritableStreamDefaultWriter<string>
    writer
    .
    WritableStreamDefaultWriter<string>.write(chunk?: string | undefined): Promise<void>

    The write() method of the WritableStreamDefaultWriter interface writes a passed chunk of data to a WritableStream and its underlying sink, then returns a Promise that resolves to indicate the success or failure of the write operation.

    MDN Reference

    write
    ("字</p>");
    await
    const writer: WritableStreamDefaultWriter<string>
    writer
    .
    WritableStreamDefaultWriter<string>.close(): Promise<void>

    The close() method of the WritableStreamDefaultWriter interface closes the associated writable stream.

    MDN Reference

    close
    ();
    var console: Console

    The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

    The module exports two specific components:

    • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
    • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

    Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

    Example using the global console:

    console.log('hello world');
    // Prints: hello world, to stdout
    console.log('hello %s', 'world');
    // Prints: hello world, to stdout
    console.error(new Error('Whoops, something bad happened'));
    // Prints error message and stack trace to stderr:
    //   Error: Whoops, something bad happened
    //     at [eval]:5:15
    //     at Script.runInThisContext (node:vm:132:18)
    //     at Object.runInThisContext (node:vm:309:38)
    //     at node:internal/process/execution:77:19
    //     at [eval]-wrapper:6:22
    //     at evalScript (node:internal/process/execution:76:60)
    //     at node:internal/main/eval_string:23:3
    
    const name = 'Will Robinson';
    console.warn(`Danger ${name}! Danger!`);
    // Prints: Danger Will Robinson! Danger!, to stderr

    Example using the Console class:

    const out = getStreamSomehow();
    const err = getStreamSomehow();
    const myConsole = new console.Console(out, err);
    
    myConsole.log('hello world');
    // Prints: hello world, to out
    myConsole.log('hello %s', 'world');
    // Prints: hello world, to out
    myConsole.error(new Error('Whoops, something bad happened'));
    // Prints: [Error: Whoops, something bad happened], to err
    
    const name = 'Will Robinson';
    myConsole.warn(`Danger ${name}! Danger!`);
    // Prints: Danger Will Robinson! Danger!, to err
    @seesource
    console
    .
    Console.log(message?: any, ...optionalParams: any[]): void (+1 overload)

    Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

    const count = 5;
    console.log('count: %d', count);
    // Prints: count: 5, to stdout
    console.log('count:', count);
    // Prints: count: 5, to stdout

    See util.format() for more information.

    @sincev0 .1.100
    log
    (await
    const output: Promise<string>
    output
    ); // <p>한자</p>

    形式 引數

    stream()의 形式 引數는 convert()와 같은 미디어 타입을 받습니다:

    const g: Gukhanmun
    g
    .
    Gukhanmun.stream(format?: MediaType): TransformStream<string, string> (+2 overloads)

    Returns a TransformStream<string, string> that converts chunks incrementally. Chunks are JavaScript strings; byte-level encoding is the caller's responsibility (TextDecoderStream / TextEncoderStream).

    The stream guarantees that the concatenated output equals the result of calling convert on the concatenated input, regardless of chunk boundaries. Document-wide middlewares (e.g., homophone marking with homophoneWindow: "per-document") buffer until the writable side is closed.

    @paramformat - Input/output media type. Defaults to "text/plain".@returnsA platform TransformStream<string, string>.@throws{@linkGukhanmunError } on initialisation failure (not on chunk errors; those are signalled via the stream's error channel).
    stream
    (); // 平文 텍스트 (基本)
    const g: Gukhanmun
    g
    .
    Gukhanmun.stream(format?: MediaType): TransformStream<string, string> (+2 overloads)

    Returns a TransformStream<string, string> that converts chunks incrementally. Chunks are JavaScript strings; byte-level encoding is the caller's responsibility (TextDecoderStream / TextEncoderStream).

    The stream guarantees that the concatenated output equals the result of calling convert on the concatenated input, regardless of chunk boundaries. Document-wide middlewares (e.g., homophone marking with homophoneWindow: "per-document") buffer until the writable side is closed.

    @paramformat - Input/output media type. Defaults to "text/plain".@returnsA platform TransformStream<string, string>.@throws{@linkGukhanmunError } on initialisation failure (not on chunk errors; those are signalled via the stream's error channel).
    stream
    ("text/html"); // HTML
    const g: Gukhanmun
    g
    .
    Gukhanmun.stream(format?: MediaType): TransformStream<string, string> (+2 overloads)

    Returns a TransformStream<string, string> that converts chunks incrementally. Chunks are JavaScript strings; byte-level encoding is the caller's responsibility (TextDecoderStream / TextEncoderStream).

    The stream guarantees that the concatenated output equals the result of calling convert on the concatenated input, regardless of chunk boundaries. Document-wide middlewares (e.g., homophone marking with homophoneWindow: "per-document") buffer until the writable side is closed.

    @paramformat - Input/output media type. Defaults to "text/plain".@returnsA platform TransformStream<string, string>.@throws{@linkGukhanmunError } on initialisation failure (not on chunk errors; those are signalled via the stream's error channel).
    stream
    ("text/markdown"); // CommonMark
    const g: Gukhanmun
    g
    .
    Gukhanmun.stream(format?: MediaType): TransformStream<string, string> (+2 overloads)

    Returns a TransformStream<string, string> that converts chunks incrementally. Chunks are JavaScript strings; byte-level encoding is the caller's responsibility (TextDecoderStream / TextEncoderStream).

    The stream guarantees that the concatenated output equals the result of calling convert on the concatenated input, regardless of chunk boundaries. Document-wide middlewares (e.g., homophone marking with homophoneWindow: "per-document") buffer until the writable side is closed.

    @paramformat - Input/output media type. Defaults to "text/plain".@returnsA platform TransformStream<string, string>.@throws{@linkGukhanmunError } on initialisation failure (not on chunk errors; those are signalled via the stream's error channel).
    stream
    ("text/markdown; variant=GFM"); // GFM

    WHATWG Streams API로 파이프하기

    TransformStream은 標準 Streams API와 統合됩니다:

    const 
    const response: Response
    response
    = await
    function fetch(input: string | URL | Request, init?: RequestInit): Promise<Response> (+1 overload)
    fetch
    ("/large-document.html");
    const
    const converted: ReadableStream<Uint8Array<ArrayBuffer>>
    converted
    =
    const response: Response
    response
    .
    Body.body: ReadableStream<Uint8Array<ArrayBuffer>> | null
    body
    !
    .
    ReadableStream<Uint8Array<ArrayBuffer>>.pipeThrough<string>(transform: ReadableWritablePair<string, Uint8Array<ArrayBuffer>>, options?: StreamPipeOptions): ReadableStream<string>

    The pipeThrough() method of the ReadableStream interface provides a chainable way of piping the current stream through a transform stream or any other writable/readable pair.

    MDN Reference

    pipeThrough
    (new
    var TextDecoderStream: new (label?: string, options?: TextDecoderOptions) => TextDecoderStream

    The TextDecoderStream interface of the Encoding API converts a stream of text in a binary encoding, such as UTF-8 etc., to a stream of strings. It is the streaming equivalent of TextDecoder. It implements the same shape as a TransformStream, allowing it to be used in ReadableStream.pipeThrough() and similar methods.

    MDN Reference

    TextDecoderStream class is a global reference for import { TextDecoderStream } from 'node:stream/web'. https://nodejs.org/api/globals.html#class-textdecoderstream

    @sincev18 .0.0
    TextDecoderStream
    ())
    .
    ReadableStream<string>.pipeThrough<string>(transform: ReadableWritablePair<string, string>, options?: StreamPipeOptions): ReadableStream<string>

    The pipeThrough() method of the ReadableStream interface provides a chainable way of piping the current stream through a transform stream or any other writable/readable pair.

    MDN Reference

    pipeThrough
    (
    const g: Gukhanmun
    g
    .
    Gukhanmun.stream(format?: MediaType): TransformStream<string, string> (+2 overloads)

    Returns a TransformStream<string, string> that converts chunks incrementally. Chunks are JavaScript strings; byte-level encoding is the caller's responsibility (TextDecoderStream / TextEncoderStream).

    The stream guarantees that the concatenated output equals the result of calling convert on the concatenated input, regardless of chunk boundaries. Document-wide middlewares (e.g., homophone marking with homophoneWindow: "per-document") buffer until the writable side is closed.

    @paramformat - Input/output media type. Defaults to "text/plain".@returnsA platform TransformStream<string, string>.@throws{@linkGukhanmunError } on initialisation failure (not on chunk errors; those are signalled via the stream's error channel).
    stream
    ("text/html"))
    .
    ReadableStream<string>.pipeThrough<Uint8Array<ArrayBuffer>>(transform: ReadableWritablePair<Uint8Array<ArrayBuffer>, string>, options?: StreamPipeOptions): ReadableStream<Uint8Array<ArrayBuffer>>

    The pipeThrough() method of the ReadableStream interface provides a chainable way of piping the current stream through a transform stream or any other writable/readable pair.

    MDN Reference

    pipeThrough
    (new
    var TextEncoderStream: new () => TextEncoderStream

    The TextEncoderStream interface of the Encoding API converts a stream of strings into bytes in the UTF-8 encoding. It is the streaming equivalent of TextEncoder. It implements the same shape as a TransformStream, allowing it to be used in ReadableStream.pipeThrough() and similar methods.

    MDN Reference

    TextEncoderStream class is a global reference for import { TextEncoderStream } from 'node:stream/web'. https://nodejs.org/api/globals.html#class-textencoderstream

    @sincev18 .0.0
    TextEncoderStream
    ());

    스트리밍 保證

    스트림의 쓰기 側에 쓴 모든 청크와 읽기 側에서 읽은 모든 청크를 이어 붙인 것은, 全體 連結 入力에 對해 g.convert()를 呼出한 것과 同等합니다:

    // 다음은 恒常 同等하다:
    const 
    const result1: string
    result1
    =
    const g: Gukhanmun
    g
    .
    Gukhanmun.convert(source: string, format?: MediaType): string (+2 overloads)

    Converts source to hangul in one shot. Buffers the entire input before returning.

    @paramsource - The text to convert.@paramformat - Input/output media type. Defaults to "text/plain".@returnsThe converted text.@throws{@linkGukhanmunError } on conversion failure.
    convert
    (
    const chunkA: string
    chunkA
    +
    const chunkB: string
    chunkB
    +
    const chunkC: string
    chunkC
    , "text/html");
    // 그리고 const
    const stream: TransformStream<string, string>
    stream
    =
    const g: Gukhanmun
    g
    .
    Gukhanmun.stream(format?: MediaType): TransformStream<string, string> (+2 overloads)

    Returns a TransformStream<string, string> that converts chunks incrementally. Chunks are JavaScript strings; byte-level encoding is the caller's responsibility (TextDecoderStream / TextEncoderStream).

    The stream guarantees that the concatenated output equals the result of calling convert on the concatenated input, regardless of chunk boundaries. Document-wide middlewares (e.g., homophone marking with homophoneWindow: "per-document") buffer until the writable side is closed.

    @paramformat - Input/output media type. Defaults to "text/plain".@returnsA platform TransformStream<string, string>.@throws{@linkGukhanmunError } on initialisation failure (not on chunk errors; those are signalled via the stream's error channel).
    stream
    ("text/html");
    const
    const output: Promise<string>
    output
    = new
    var Response: new (body?: BodyInit | null, init?: ResponseInit) => Response

    The Response interface of the Fetch API represents the response to a request.

    MDN Reference

    Response
    (
    const stream: TransformStream<string, string>
    stream
    .
    TransformStream<string, string>.readable: ReadableStream<string>

    The readable read-only property of the TransformStream interface returns the ReadableStream instance controlled by this TransformStream. This stream emits the transformed output data.

    MDN Reference

    readable
    .
    ReadableStream<string>.pipeThrough<Uint8Array<ArrayBuffer>>(transform: ReadableWritablePair<Uint8Array<ArrayBuffer>, string>, options?: StreamPipeOptions): ReadableStream<Uint8Array<ArrayBuffer>>

    The pipeThrough() method of the ReadableStream interface provides a chainable way of piping the current stream through a transform stream or any other writable/readable pair.

    MDN Reference

    pipeThrough
    (new
    var TextEncoderStream: new () => TextEncoderStream

    The TextEncoderStream interface of the Encoding API converts a stream of strings into bytes in the UTF-8 encoding. It is the streaming equivalent of TextEncoder. It implements the same shape as a TransformStream, allowing it to be used in ReadableStream.pipeThrough() and similar methods.

    MDN Reference

    TextEncoderStream class is a global reference for import { TextEncoderStream } from 'node:stream/web'. https://nodejs.org/api/globals.html#class-textencoderstream

    @sincev18 .0.0
    TextEncoderStream
    ()),
    ).
    Body.text(): Promise<string>
    text
    ();
    const
    const writer: WritableStreamDefaultWriter<string>
    writer
    =
    const stream: TransformStream<string, string>
    stream
    .
    TransformStream<string, string>.writable: WritableStream<string>

    The writable read-only property of the TransformStream interface returns the WritableStream instance controlled by this TransformStream. This stream accepts input data that will be transformed and emitted to the readable stream.

    MDN Reference

    writable
    .
    WritableStream<string>.getWriter(): WritableStreamDefaultWriter<string>

    The getWriter() method of the WritableStream interface returns a new instance of WritableStreamDefaultWriter and locks the stream to that instance. While the stream is locked, no other writer can be acquired until this one is released.

    MDN Reference

    getWriter
    ();
    await
    const writer: WritableStreamDefaultWriter<string>
    writer
    .
    WritableStreamDefaultWriter<string>.write(chunk?: string | undefined): Promise<void>

    The write() method of the WritableStreamDefaultWriter interface writes a passed chunk of data to a WritableStream and its underlying sink, then returns a Promise that resolves to indicate the success or failure of the write operation.

    MDN Reference

    write
    (
    const chunkA: string
    chunkA
    );
    await
    const writer: WritableStreamDefaultWriter<string>
    writer
    .
    WritableStreamDefaultWriter<string>.write(chunk?: string | undefined): Promise<void>

    The write() method of the WritableStreamDefaultWriter interface writes a passed chunk of data to a WritableStream and its underlying sink, then returns a Promise that resolves to indicate the success or failure of the write operation.

    MDN Reference

    write
    (
    const chunkB: string
    chunkB
    );
    await
    const writer: WritableStreamDefaultWriter<string>
    writer
    .
    WritableStreamDefaultWriter<string>.write(chunk?: string | undefined): Promise<void>

    The write() method of the WritableStreamDefaultWriter interface writes a passed chunk of data to a WritableStream and its underlying sink, then returns a Promise that resolves to indicate the success or failure of the write operation.

    MDN Reference

    write
    (
    const chunkC: string
    chunkC
    );
    await
    const writer: WritableStreamDefaultWriter<string>
    writer
    .
    WritableStreamDefaultWriter<string>.close(): Promise<void>

    The close() method of the WritableStreamDefaultWriter interface closes the associated writable stream.

    MDN Reference

    close
    ();
    const
    const result2: string
    result2
    = await
    const output: Promise<string>
    output
    ;
    import
    function assert(value: unknown, message?: string | Error): asserts value
    assert
    from "node:assert/strict";
    function assert(value: unknown, message?: string | Error): asserts value
    assert
    .
    strict.strictEqual<string>(actual: unknown, expected: string, message?: string | Error): asserts actual is string
    export strict.strictEqual

    Tests strict equality between the actual and expected parameters as determined by Object.is().

    import assert from 'node:assert/strict';
    
    assert.strictEqual(1, 2);
    // AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:
    //
    // 1 !== 2
    
    assert.strictEqual(1, 1);
    // OK
    
    assert.strictEqual('Hello foobar', 'Hello World!');
    // AssertionError [ERR_ASSERTION]: Expected inputs to be strictly equal:
    // + actual - expected
    //
    // + 'Hello foobar'
    // - 'Hello World!'
    //          ^
    
    const apples = 1;
    const oranges = 2;
    assert.strictEqual(apples, oranges, `apples ${apples} !== oranges ${oranges}`);
    // AssertionError [ERR_ASSERTION]: apples 1 !== oranges 2
    
    assert.strictEqual(1, '1', new TypeError('Inputs are not identical'));
    // TypeError: Inputs are not identical

    If the values are not strictly equal, an AssertionError is thrown with a message property set equal to the value of the message parameter. If the message parameter is undefined, a default error message is assigned. If the message parameter is an instance of an Error then it will be thrown instead of the AssertionError.

    @sincev0 .1.21
    strictEqual
    (
    const result1: string
    result1
    ,
    const result2: string
    result2
    );

    "per-block"을 使用하거나 同音異義 追跡을 꺼도 最終化 前에는 出力이 없습니다. 이 옵션들은 註解(annotation) 文脈을 制御하며, JavaScript 바인딩이 청크를 내보내는 時點을 바꾸지는 않습니다. 스트림 基盤 入出力과 統合해도 文書 全體를 담는 버퍼는 必要합니다.