完成世界书、骰子、apiconfig页面处理

This commit is contained in:
2026-04-30 01:35:10 +08:00
parent a3e3711b2b
commit ba9b925c32
4602 changed files with 785225 additions and 23 deletions

16
frontend/node_modules/micromark/dev/lib/compile.d.ts generated vendored Normal file
View File

@@ -0,0 +1,16 @@
/**
* @param {CompileOptions | null | undefined} [options]
* @returns {Compile}
*/
export function compile(options?: CompileOptions | null | undefined): Compile;
export type Media = {
image?: boolean | undefined;
labelId?: string | undefined;
label?: string | undefined;
referenceId?: string | undefined;
destination?: string | undefined;
title?: string | undefined;
};
import type { CompileOptions } from 'micromark-util-types';
import type { Compile } from 'micromark-util-types';
//# sourceMappingURL=compile.d.ts.map

1152
frontend/node_modules/micromark/dev/lib/compile.js generated vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1 @@
{"version":3,"file":"constructs.d.ts","sourceRoot":"","sources":["constructs.js"],"names":[],"mappings":"AA6BA,yCAAyC;AACzC;;;;;;;;;;;;;;;EAeC;AAED,+CAA+C;AAC/C;;EAEC;AAED,4CAA4C;AAC5C;;;;EAIC;AAED,qCAAqC;AACrC;;;;;;;;;EASC;AAED,uCAAuC;AACvC;;;EAGC;AAED,qCAAqC;AACrC;;;;;;;;;;;;;EAaC"}

View File

@@ -0,0 +1,4 @@
/** @type {InitialConstruct} */
export const flow: InitialConstruct;
import type { InitialConstruct } from 'micromark-util-types';
//# sourceMappingURL=flow.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"flow.d.ts","sourceRoot":"","sources":["flow.js"],"names":[],"mappings":"AAeA,+BAA+B;AAC/B,mBADW,gBAAgB,CACmB;sCAVpC,sBAAsB"}

View File

@@ -0,0 +1,244 @@
/**
* @import {
* Code,
* InitialConstruct,
* Initializer,
* Resolver,
* State,
* TokenizeContext
* } from 'micromark-util-types'
*/
import {ok as assert} from 'devlop'
import {codes, constants, types} from 'micromark-util-symbol'
export const resolver = {resolveAll: createResolver()}
export const string = initializeFactory('string')
export const text = initializeFactory('text')
/**
* @param {'string' | 'text'} field
* Field.
* @returns {InitialConstruct}
* Construct.
*/
function initializeFactory(field) {
return {
resolveAll: createResolver(
field === 'text' ? resolveAllLineSuffixes : undefined
),
tokenize: initializeText
}
/**
* @this {TokenizeContext}
* Context.
* @type {Initializer}
*/
function initializeText(effects) {
const self = this
const constructs = this.parser.constructs[field]
const text = effects.attempt(constructs, start, notText)
return start
/** @type {State} */
function start(code) {
return atBreak(code) ? text(code) : notText(code)
}
/** @type {State} */
function notText(code) {
if (code === codes.eof) {
effects.consume(code)
return
}
effects.enter(types.data)
effects.consume(code)
return data
}
/** @type {State} */
function data(code) {
if (atBreak(code)) {
effects.exit(types.data)
return text(code)
}
// Data.
effects.consume(code)
return data
}
/**
* @param {Code} code
* Code.
* @returns {boolean}
* Whether the code is a break.
*/
function atBreak(code) {
if (code === codes.eof) {
return true
}
const list = constructs[code]
let index = -1
if (list) {
// Always populated by defaults.
assert(Array.isArray(list), 'expected `disable.null` to be populated')
while (++index < list.length) {
const item = list[index]
if (!item.previous || item.previous.call(self, self.previous)) {
return true
}
}
}
return false
}
}
}
/**
* @param {Resolver | undefined} [extraResolver]
* Resolver.
* @returns {Resolver}
* Resolver.
*/
function createResolver(extraResolver) {
return resolveAllText
/** @type {Resolver} */
function resolveAllText(events, context) {
let index = -1
/** @type {number | undefined} */
let enter
// A rather boring computation (to merge adjacent `data` events) which
// improves mm performance by 29%.
while (++index <= events.length) {
if (enter === undefined) {
if (events[index] && events[index][1].type === types.data) {
enter = index
index++
}
} else if (!events[index] || events[index][1].type !== types.data) {
// Dont do anything if there is one data token.
if (index !== enter + 2) {
events[enter][1].end = events[index - 1][1].end
events.splice(enter + 2, index - enter - 2)
index = enter + 2
}
enter = undefined
}
}
return extraResolver ? extraResolver(events, context) : events
}
}
/**
* A rather ugly set of instructions which again looks at chunks in the input
* stream.
* The reason to do this here is that it is *much* faster to parse in reverse.
* And that we cant hook into `null` to split the line suffix before an EOF.
* To do: figure out if we can make this into a clean utility, or even in core.
* As it will be useful for GFMs literal autolink extension (and maybe even
* tables?)
*
* @type {Resolver}
*/
function resolveAllLineSuffixes(events, context) {
let eventIndex = 0 // Skip first.
while (++eventIndex <= events.length) {
if (
(eventIndex === events.length ||
events[eventIndex][1].type === types.lineEnding) &&
events[eventIndex - 1][1].type === types.data
) {
const data = events[eventIndex - 1][1]
const chunks = context.sliceStream(data)
let index = chunks.length
let bufferIndex = -1
let size = 0
/** @type {boolean | undefined} */
let tabs
while (index--) {
const chunk = chunks[index]
if (typeof chunk === 'string') {
bufferIndex = chunk.length
while (chunk.charCodeAt(bufferIndex - 1) === codes.space) {
size++
bufferIndex--
}
if (bufferIndex) break
bufferIndex = -1
}
// Number
else if (chunk === codes.horizontalTab) {
tabs = true
size++
} else if (chunk === codes.virtualSpace) {
// Empty
} else {
// Replacement character, exit.
index++
break
}
}
// Allow final trailing whitespace.
if (context._contentTypeTextTrailing && eventIndex === events.length) {
size = 0
}
if (size) {
const token = {
type:
eventIndex === events.length ||
tabs ||
size < constants.hardBreakPrefixSizeMin
? types.lineSuffix
: types.hardBreakTrailing,
start: {
_bufferIndex: index
? bufferIndex
: data.start._bufferIndex + bufferIndex,
_index: data.start._index + index,
line: data.end.line,
column: data.end.column - size,
offset: data.end.offset - size
},
end: {...data.end}
}
data.end = {...token.start}
if (data.start.offset === data.end.offset) {
Object.assign(data, token)
} else {
events.splice(
eventIndex,
0,
['enter', token, context],
['exit', token, context]
)
eventIndex += 2
}
}
eventIndex++
}
}
return events
}

View File

@@ -0,0 +1 @@
{"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["parse.js"],"names":[],"mappings":"AAkBA;;;;;GAKG;AACH,gCALW,YAAY,GAAG,IAAI,GAAG,SAAS,GAE7B,YAAY,CAoCxB;kCAlDS,sBAAsB;kCAAtB,sBAAsB"}

58
frontend/node_modules/micromark/dev/lib/parse.js generated vendored Normal file
View File

@@ -0,0 +1,58 @@
/**
* @import {
* Create,
* FullNormalizedExtension,
* InitialConstruct,
* ParseContext,
* ParseOptions
* } from 'micromark-util-types'
*/
import {combineExtensions} from 'micromark-util-combine-extensions'
import {content} from './initialize/content.js'
import {document} from './initialize/document.js'
import {flow} from './initialize/flow.js'
import {string, text} from './initialize/text.js'
import * as defaultConstructs from './constructs.js'
import {createTokenizer} from './create-tokenizer.js'
/**
* @param {ParseOptions | null | undefined} [options]
* Configuration (optional).
* @returns {ParseContext}
* Parser.
*/
export function parse(options) {
const settings = options || {}
const constructs = /** @type {FullNormalizedExtension} */ (
combineExtensions([defaultConstructs, ...(settings.extensions || [])])
)
/** @type {ParseContext} */
const parser = {
constructs,
content: create(content),
defined: [],
document: create(document),
flow: create(flow),
lazy: {},
string: create(string),
text: create(text)
}
return parser
/**
* @param {InitialConstruct} initial
* Construct to start with.
* @returns {Create}
* Create a tokenizer.
*/
function create(initial) {
return creator
/** @type {Create} */
function creator(from) {
return createTokenizer(parser, initial, from)
}
}
}

35
frontend/node_modules/micromark/dev/stream.d.ts generated vendored Normal file
View File

@@ -0,0 +1,35 @@
/**
* Create a duplex (readable and writable) stream.
*
* Some of the work to parse markdown can be done streaming, but in the
* end buffering is required.
*
* micromark does not handle errors for you, so you must handle errors on whatever
* streams you pipe into it.
* As markdown does not know errors, `micromark` itself does not emit errors.
*
* @param {Options | null | undefined} [options]
* Configuration (optional).
* @returns {MinimalDuplex}
* Duplex stream.
*/
export function stream(options?: Options | null | undefined): MinimalDuplex;
export type Options = import("micromark-util-types").Options;
/**
* Function called when write was successful.
*/
export type Callback = () => undefined;
/**
* Configuration for piping.
*/
export type PipeOptions = {
/**
* Whether to end the destination stream when the source stream ends.
*/
end?: boolean | null | undefined;
};
/**
* Duplex stream.
*/
export type MinimalDuplex = Omit<NodeJS.ReadableStream & NodeJS.WritableStream, "isPaused" | "pause" | "read" | "resume" | "setEncoding" | "unpipe" | "unshift" | "wrap">;
//# sourceMappingURL=stream.d.ts.map

1
frontend/node_modules/micromark/dev/stream.d.ts.map generated vendored Normal file
View File

@@ -0,0 +1 @@
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["stream.js"],"names":[],"mappings":"AA6BA;;;;;;;;;;;;;;GAcG;AACH,iCALW,OAAO,GAAG,IAAI,GAAG,SAAS,GAExB,aAAa,CAoOzB;sBAxQY,OAAO,sBAAsB,EAAE,OAAO;;;;6BAMtC,SAAS;;;;;;;;UAKR,OAAO,GAAG,IAAI,GAAG,SAAS;;;;;4BAG3B,IAAI,CAAC,MAAM,CAAC,cAAc,GAAG,MAAM,CAAC,cAAc,EAAE,UAAU,GAAG,OAAO,GAAG,MAAM,GAAG,QAAQ,GAAG,aAAa,GAAG,QAAQ,GAAG,SAAS,GAAG,MAAM,CAAC"}

73
frontend/node_modules/micromark/lib/constructs.d.ts generated vendored Normal file
View File

@@ -0,0 +1,73 @@
/** @satisfies {Extension['document']} */
export const document: {
42: import("micromark-util-types").Construct;
43: import("micromark-util-types").Construct;
45: import("micromark-util-types").Construct;
48: import("micromark-util-types").Construct;
49: import("micromark-util-types").Construct;
50: import("micromark-util-types").Construct;
51: import("micromark-util-types").Construct;
52: import("micromark-util-types").Construct;
53: import("micromark-util-types").Construct;
54: import("micromark-util-types").Construct;
55: import("micromark-util-types").Construct;
56: import("micromark-util-types").Construct;
57: import("micromark-util-types").Construct;
62: import("micromark-util-types").Construct;
};
/** @satisfies {Extension['contentInitial']} */
export const contentInitial: {
91: import("micromark-util-types").Construct;
};
/** @satisfies {Extension['flowInitial']} */
export const flowInitial: {
[-2]: import("micromark-util-types").Construct;
[-1]: import("micromark-util-types").Construct;
32: import("micromark-util-types").Construct;
};
/** @satisfies {Extension['flow']} */
export const flow: {
35: import("micromark-util-types").Construct;
42: import("micromark-util-types").Construct;
45: import("micromark-util-types").Construct[];
60: import("micromark-util-types").Construct;
61: import("micromark-util-types").Construct;
95: import("micromark-util-types").Construct;
96: import("micromark-util-types").Construct;
126: import("micromark-util-types").Construct;
};
/** @satisfies {Extension['string']} */
export const string: {
38: import("micromark-util-types").Construct;
92: import("micromark-util-types").Construct;
};
/** @satisfies {Extension['text']} */
export const text: {
[-5]: import("micromark-util-types").Construct;
[-4]: import("micromark-util-types").Construct;
[-3]: import("micromark-util-types").Construct;
33: import("micromark-util-types").Construct;
38: import("micromark-util-types").Construct;
42: import("micromark-util-types").Construct;
60: import("micromark-util-types").Construct[];
91: import("micromark-util-types").Construct;
92: import("micromark-util-types").Construct[];
93: import("micromark-util-types").Construct;
95: import("micromark-util-types").Construct;
96: import("micromark-util-types").Construct;
};
export namespace insideSpan {
let _null: (import("micromark-util-types").Construct | {
resolveAll: import("micromark-util-types").Resolver;
})[];
export { _null as null };
}
export namespace attentionMarkers {
let _null_1: (42 | 95)[];
export { _null_1 as null };
}
export namespace disable {
let _null_2: never[];
export { _null_2 as null };
}
//# sourceMappingURL=constructs.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"document.d.ts","sourceRoot":"","sources":["document.js"],"names":[],"mappings":"AAyBA,+BAA+B;AAC/B,uBADW,gBAAgB,CAC2B;;;;wBAXzC,CAAC,SAAS,EAAE,cAAc,CAAC;sCAJ9B,sBAAsB;+BAAtB,sBAAsB;oCAAtB,sBAAsB"}

View File

@@ -0,0 +1,4 @@
/** @type {InitialConstruct} */
export const flow: InitialConstruct;
import type { InitialConstruct } from 'micromark-util-types';
//# sourceMappingURL=flow.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"flow.d.ts","sourceRoot":"","sources":["flow.js"],"names":[],"mappings":"AAeA,+BAA+B;AAC/B,mBADW,gBAAgB,CACmB;sCAVpC,sBAAsB"}

58
frontend/node_modules/micromark/lib/initialize/flow.js generated vendored Normal file
View File

@@ -0,0 +1,58 @@
/**
* @import {
* InitialConstruct,
* Initializer,
* State,
* TokenizeContext
* } from 'micromark-util-types'
*/
import { blankLine, content } from 'micromark-core-commonmark';
import { factorySpace } from 'micromark-factory-space';
import { markdownLineEnding } from 'micromark-util-character';
/** @type {InitialConstruct} */
export const flow = {
tokenize: initializeFlow
};
/**
* @this {TokenizeContext}
* Self.
* @type {Initializer}
* Initializer.
*/
function initializeFlow(effects) {
const self = this;
const initial = effects.attempt(
// Try to parse a blank line.
blankLine, atBlankEnding,
// Try to parse initial flow (essentially, only code).
effects.attempt(this.parser.constructs.flowInitial, afterConstruct, factorySpace(effects, effects.attempt(this.parser.constructs.flow, afterConstruct, effects.attempt(content, afterConstruct)), "linePrefix")));
return initial;
/** @type {State} */
function atBlankEnding(code) {
if (code === null) {
effects.consume(code);
return;
}
effects.enter("lineEndingBlank");
effects.consume(code);
effects.exit("lineEndingBlank");
self.currentConstruct = undefined;
return initial;
}
/** @type {State} */
function afterConstruct(code) {
if (code === null) {
effects.consume(code);
return;
}
effects.enter("lineEnding");
effects.consume(code);
effects.exit("lineEnding");
self.currentConstruct = undefined;
return initial;
}
}

1
frontend/node_modules/micromark/lib/parse.d.ts.map generated vendored Normal file
View File

@@ -0,0 +1 @@
{"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["parse.js"],"names":[],"mappings":"AAkBA;;;;;GAKG;AACH,gCALW,YAAY,GAAG,IAAI,GAAG,SAAS,GAE7B,YAAY,CAoCxB;kCAlDS,sBAAsB;kCAAtB,sBAAsB"}

56
frontend/node_modules/micromark/lib/parse.js generated vendored Normal file
View File

@@ -0,0 +1,56 @@
/**
* @import {
* Create,
* FullNormalizedExtension,
* InitialConstruct,
* ParseContext,
* ParseOptions
* } from 'micromark-util-types'
*/
import { combineExtensions } from 'micromark-util-combine-extensions';
import { content } from './initialize/content.js';
import { document } from './initialize/document.js';
import { flow } from './initialize/flow.js';
import { string, text } from './initialize/text.js';
import * as defaultConstructs from './constructs.js';
import { createTokenizer } from './create-tokenizer.js';
/**
* @param {ParseOptions | null | undefined} [options]
* Configuration (optional).
* @returns {ParseContext}
* Parser.
*/
export function parse(options) {
const settings = options || {};
const constructs = /** @type {FullNormalizedExtension} */
combineExtensions([defaultConstructs, ...(settings.extensions || [])]);
/** @type {ParseContext} */
const parser = {
constructs,
content: create(content),
defined: [],
document: create(document),
flow: create(flow),
lazy: {},
string: create(string),
text: create(text)
};
return parser;
/**
* @param {InitialConstruct} initial
* Construct to start with.
* @returns {Create}
* Create a tokenizer.
*/
function create(initial) {
return creator;
/** @type {Create} */
function creator(from) {
return createTokenizer(parser, initial, from);
}
}
}

18
frontend/node_modules/micromark/lib/postprocess.js generated vendored Normal file
View File

@@ -0,0 +1,18 @@
/**
* @import {Event} from 'micromark-util-types'
*/
import { subtokenize } from 'micromark-util-subtokenize';
/**
* @param {Array<Event>} events
* Events.
* @returns {Array<Event>}
* Events.
*/
export function postprocess(events) {
while (!subtokenize(events)) {
// Empty
}
return events;
}

22
frontend/node_modules/micromark/license generated vendored Normal file
View File

@@ -0,0 +1,22 @@
(The MIT License)
Copyright (c) Titus Wormer <tituswormer@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

256
frontend/node_modules/micromark/stream.js generated vendored Normal file
View File

@@ -0,0 +1,256 @@
/**
* @import {Encoding, Value} from 'micromark-util-types'
*/
/**
* @typedef {import('micromark-util-types').Options} Options
*/
/**
* @callback Callback
* Function called when write was successful.
* @returns {undefined}
* Nothing.
*
* @typedef PipeOptions
* Configuration for piping.
* @property {boolean | null | undefined} [end]
* Whether to end the destination stream when the source stream ends.
*
* @typedef {Omit<NodeJS.ReadableStream & NodeJS.WritableStream, 'isPaused' | 'pause' | 'read' | 'resume' | 'setEncoding' | 'unpipe' | 'unshift' | 'wrap'>} MinimalDuplex
* Duplex stream.
*/
import { EventEmitter } from 'node:events';
import { compile } from './lib/compile.js';
import { parse } from './lib/parse.js';
import { postprocess } from './lib/postprocess.js';
import { preprocess } from './lib/preprocess.js';
/**
* Create a duplex (readable and writable) stream.
*
* Some of the work to parse markdown can be done streaming, but in the
* end buffering is required.
*
* micromark does not handle errors for you, so you must handle errors on whatever
* streams you pipe into it.
* As markdown does not know errors, `micromark` itself does not emit errors.
*
* @param {Options | null | undefined} [options]
* Configuration (optional).
* @returns {MinimalDuplex}
* Duplex stream.
*/
export function stream(options) {
const prep = preprocess();
const tokenize = parse(options).document().write;
const comp = compile(options);
/** @type {boolean} */
let ended;
const emitter = /** @type {MinimalDuplex} */new EventEmitter();
// @ts-expect-error: fine.
emitter.end = end;
emitter.pipe = pipe;
emitter.readable = true;
emitter.writable = true;
// @ts-expect-error: fine.
emitter.write = write;
return emitter;
/**
* Write a chunk into memory.
*
* @overload
* @param {Value | null | undefined} [chunk]
* Slice of markdown to parse (`string` or `Uint8Array`).
* @param {Encoding | null | undefined} [encoding]
* Character encoding to understand `chunk` as when its a `Uint8Array`
* (`string`, default: `'utf8'`).
* @param {Callback | null | undefined} [callback]
* Function called when write was successful.
* @returns {boolean}
* Whether write was successful.
*
* @overload
* @param {Value | null | undefined} [chunk]
* Slice of markdown to parse (`string` or `Uint8Array`).
* @param {Callback | null | undefined} [callback]
* Function called when write was successful.
* @returns {boolean}
* Whether write was successful.
*
* @param {Value | null | undefined} [chunk]
* Slice of markdown to parse (`string` or `Uint8Array`).
* @param {Callback | Encoding | null | undefined} [encoding]
* Character encoding to understand `chunk` as when its a `Uint8Array`
* (`string`, default: `'utf8'`).
* @param {Callback | null | undefined} [callback]
* Function called when write was successful.
* @returns {boolean}
* Whether write was successful.
*/
function write(chunk, encoding, callback) {
if (typeof encoding === 'function') {
callback = encoding;
encoding = undefined;
}
if (ended) {
throw new Error('Did not expect `write` after `end`');
}
tokenize(prep(chunk || '', encoding));
if (callback) {
callback();
}
// Signal successful write.
return true;
}
/**
* End the writing.
*
* Passes all arguments as a final `write`.
*
* @overload
* @param {Value | null | undefined} [chunk]
* Slice of markdown to parse (`string` or `Uint8Array`).
* @param {Encoding | null | undefined} [encoding]
* Character encoding to understand `chunk` as when its a `Uint8Array`
* (`string`, default: `'utf8'`).
* @param {Callback | null | undefined} [callback]
* Function called when write was successful.
* @returns {boolean}
* Whether write was successful.
*
* @overload
* @param {Value | null | undefined} [chunk]
* Slice of markdown to parse (`string` or `Uint8Array`).
* @param {Callback | null | undefined} [callback]
* Function called when write was successful.
* @returns {boolean}
* Whether write was successful.
*
* @overload
* @param {Callback | null | undefined} [callback]
* Function called when write was successful.
* @returns {boolean}
* Whether write was successful.
*
* @param {Callback | Value | null | undefined} [chunk]
* Slice of markdown to parse (`string` or `Uint8Array`).
* @param {Callback | Encoding | null | undefined} [encoding]
* Character encoding to understand `chunk` as when its a `Uint8Array`
* (`string`, default: `'utf8'`).
* @param {Callback | null | undefined} [callback]
* Function called when write was successful.
* @returns {boolean}
* Whether write was successful.
*/
function end(chunk, encoding, callback) {
if (typeof chunk === 'function') {
encoding = chunk;
chunk = undefined;
}
if (typeof encoding === 'function') {
callback = encoding;
encoding = undefined;
}
write(chunk, encoding, callback);
emitter.emit('data', comp(postprocess(tokenize(prep('', encoding, true)))));
emitter.emit('end');
ended = true;
return true;
}
/**
* Pipe the processor into a writable stream.
*
* Basically `Stream#pipe`, but inlined and simplified to keep the bundled
* size down.
* See: <https://github.com/nodejs/node/blob/43a5170/lib/internal/streams/legacy.js#L13>.
*
* @template {NodeJS.WritableStream} Stream
* Writable stream.
* @param {Stream} destination
* Stream to pipe into.
* @param {PipeOptions | null | undefined} [options]
* Configuration.
* @returns {Stream}
* Destination stream.
*/
function pipe(destination, options) {
emitter.on('data', ondata);
emitter.on('error', onerror);
emitter.on('end', cleanup);
emitter.on('close', cleanup);
// If the `end` option is not supplied, `destination.end()` will be
// called when the `end` or `close` events are received.
// @ts-expect-error `_isStdio` is available on `std{err,out}`
if (!destination._isStdio && (!options || options.end !== false)) {
emitter.on('end', onend);
}
destination.on('error', onerror);
destination.on('close', cleanup);
destination.emit('pipe', emitter);
return destination;
/**
* End destination stream.
*
* @returns {undefined}
* Nothing.
*/
function onend() {
if (destination.end) {
destination.end();
}
}
/**
* Handle data.
*
* @param {string} chunk
* Data.
* @returns {undefined}
* Nothing.
*/
function ondata(chunk) {
if (destination.writable) {
destination.write(chunk);
}
}
/**
* Clean listeners.
*
* @returns {undefined}
* Nothing.
*/
function cleanup() {
emitter.removeListener('data', ondata);
emitter.removeListener('end', onend);
emitter.removeListener('error', onerror);
emitter.removeListener('end', cleanup);
emitter.removeListener('close', cleanup);
destination.removeListener('error', onerror);
destination.removeListener('close', cleanup);
}
/**
* Close dangling pipes and handle unheard errors.
*
* @param {Error | null | undefined} [error]
* Error, if any.
* @returns {undefined}
* Nothing.
*/
function onerror(error) {
cleanup();
if (!emitter.listenerCount('error')) {
throw error; // Unhandled stream error in pipe.
}
}
}
}