n3.js-messages 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/browser.d.ts ADDED
@@ -0,0 +1,26 @@
1
+ import type * as RDF from '@rdfjs/types';
2
+ import type { MessageParserOptions, MessageCallback, MessageParseCallbacks, MessageWriterOptions } from './common.js';
3
+ export * from './common.js';
4
+
5
+ export class MessageParser<Q extends RDF.BaseQuad = RDF.Quad> {
6
+ constructor(options?: MessageParserOptions<Q>);
7
+ parse(input: string): Q[][];
8
+ parse(input: string, callback: MessageCallback<Q>): void;
9
+ parse(input: string, callbacks: MessageParseCallbacks<Q>): void;
10
+ }
11
+ export function parseMessages<Q extends RDF.BaseQuad = RDF.Quad>(input: string, options?: MessageParserOptions<Q>): Q[][];
12
+
13
+ export type MessageChunk = string | ArrayBuffer | ArrayBufferView;
14
+ export interface BrowserParserOptions<Q extends RDF.BaseQuad = RDF.Quad> extends MessageParserOptions<Q> {
15
+ onPrefix?: (prefix: string, term: RDF.NamedNode) => void;
16
+ onVersion?: (version: string) => void;
17
+ }
18
+ /** Use source.pipeThrough(new MessageStreamParser(options)). */
19
+ export class MessageStreamParser<Q extends RDF.BaseQuad = RDF.Quad> extends TransformStream<MessageChunk, Q[]> {
20
+ constructor(options?: BrowserParserOptions<Q>);
21
+ }
22
+ export type MessageWriterInput = RDF.BaseQuad[] | { quad: RDF.BaseQuad; messageIndex?: number };
23
+ /** Emits strings; pipe through TextEncoderStream to obtain UTF-8 bytes. */
24
+ export class MessageStreamWriter extends TransformStream<MessageWriterInput, string> {
25
+ constructor(options?: Omit<MessageWriterOptions, 'end'>);
26
+ }
package/common.d.ts ADDED
@@ -0,0 +1,89 @@
1
+ import type * as RDF from '@rdfjs/types';
2
+
3
+ export type MessageFormat =
4
+ | 'Turtle' | 'TriG' | 'N-Triples' | 'N-Quads'
5
+ | (string & {});
6
+
7
+ /** One RDF message: the quads between two message delimiters. */
8
+ export type Message<Q extends RDF.BaseQuad = RDF.Quad> = Q[];
9
+
10
+ /**
11
+ * The part of an RDF/JS DataFactory that is used for parsing; any `RDF.DataFactory<Q>` fits.
12
+ * The quads that the factory creates determine the quads that are parsed.
13
+ */
14
+ export interface MessageQuadFactory<Q extends RDF.BaseQuad = RDF.Quad>
15
+ extends Pick<RDF.DataFactory<RDF.Quad, any>, 'namedNode' | 'blankNode' | 'literal' | 'variable' | 'defaultGraph'> {
16
+ quad(subject: Q['subject'], predicate: Q['predicate'], object: Q['object'], graph?: Q['graph']): Q;
17
+ }
18
+
19
+ export interface MessageParserOptions<Q extends RDF.BaseQuad = RDF.Quad> {
20
+ /** Syntax name or media type, case-insensitive. Defaults to `TriG`. */
21
+ format?: MessageFormat;
22
+ baseIRI?: string;
23
+ /** Data factory used to create terms and quads. */
24
+ factory?: MessageQuadFactory<Q>;
25
+ /** Prefix for generated blank node labels, e.g. `b0_`. */
26
+ blankNodePrefix?: string;
27
+ /** RDF version label, e.g. `1.2-messages`; a `-messages` suffix forces message mode. */
28
+ version?: string;
29
+ /** Force message mode even without a `-messages` version declaration. */
30
+ messages?: boolean;
31
+ }
32
+
33
+ export type MessageCallback<Q extends RDF.BaseQuad = RDF.Quad> = (
34
+ error: Error | null,
35
+ message: Q[] | null,
36
+ prefixes?: { [prefix: string]: RDF.NamedNode },
37
+ index?: number,
38
+ ) => void;
39
+
40
+ export interface MessageParseCallbacks<Q extends RDF.BaseQuad = RDF.Quad> {
41
+ onMessage?: MessageCallback<Q>;
42
+ onQuad?: (error: Error | null, quad?: Q, messageIndex?: number) => void;
43
+ onPrefix?: (prefix: string, term: RDF.NamedNode) => void;
44
+ onVersion?: (version: string) => void;
45
+ }
46
+
47
+ export interface MessageWriterOptions {
48
+ /** Syntax name or media type, case-insensitive. Defaults to `TriG`. */
49
+ format?: MessageFormat;
50
+ /** Prefixes that are declared up front. */
51
+ prefixes?: { [prefix: string]: RDF.NamedNode | string };
52
+ baseIRI?: string;
53
+ writeBase?: boolean;
54
+ lists?: { [listHead: string]: RDF.Term[] };
55
+ /** RDF version label announced in the output. Defaults to `1.2-messages`. */
56
+ version?: string;
57
+ /** Whether to end the output stream on `end()`. Defaults to `true`. */
58
+ end?: boolean;
59
+ }
60
+
61
+ export interface MessageOutputStream {
62
+ write(chunk: string, encoding?: string, callback?: (error?: Error | null) => void): unknown;
63
+ end(callback?: (error?: Error | null) => void): unknown;
64
+ }
65
+
66
+ export class MessageWriter {
67
+ constructor(options?: MessageWriterOptions);
68
+ constructor(outputStream?: MessageOutputStream | null, options?: MessageWriterOptions);
69
+ addMessage(quads: RDF.BaseQuad[], done: (error: Error | null) => void): void;
70
+ addMessage(quads: RDF.BaseQuad[]): Promise<void>;
71
+ /** Opens a message; optional, as `addQuad` opens one implicitly. */
72
+ beginMessage(done: (error: Error | null) => void): void;
73
+ beginMessage(): Promise<void>;
74
+ /** Writes a quad right away. With a 0-based `messageIndex`, a change of index ends the message and writes empty messages for gaps; a lower index is an error. */
75
+ addQuad(quad: RDF.BaseQuad, messageIndex: number | undefined, done: (error: Error | null) => void): void;
76
+ addQuad(quad: RDF.BaseQuad, done: (error: Error | null) => void): void;
77
+ addQuad(quad: RDF.BaseQuad, messageIndex?: number): Promise<void>;
78
+ /** Ends the open message (writes an empty message if none is open). */
79
+ endMessage(done: (error: Error | null) => void): void;
80
+ endMessage(): Promise<void>;
81
+ addPrefix(prefix: string, iri: RDF.NamedNode | string, done: (error: Error | null) => void): void;
82
+ addPrefix(prefix: string, iri: RDF.NamedNode | string): Promise<void>;
83
+ /** Without an output stream, the result is the written string. */
84
+ end(done: (error: Error | null, result?: string) => void): void;
85
+ end(): Promise<string>;
86
+ }
87
+
88
+ export function writeMessages(messages: Iterable<RDF.BaseQuad[]>, options?: MessageWriterOptions): string;
89
+
package/index.d.ts CHANGED
@@ -2,49 +2,8 @@
2
2
  import * as RDF from '@rdfjs/types';
3
3
  import { Transform } from 'stream';
4
4
 
5
- export type MessageFormat =
6
- | 'Turtle' | 'TriG' | 'N-Triples' | 'N-Quads'
7
- | (string & {});
8
-
9
- /** One RDF message: the quads between two message delimiters. */
10
- export type Message<Q extends RDF.BaseQuad = RDF.Quad> = Q[];
11
-
12
- /**
13
- * The part of an RDF/JS DataFactory that is used for parsing; any `RDF.DataFactory<Q>` fits.
14
- * The quads that the factory creates determine the quads that are parsed.
15
- */
16
- export interface MessageQuadFactory<Q extends RDF.BaseQuad = RDF.Quad>
17
- extends Pick<RDF.DataFactory<RDF.Quad, any>, 'namedNode' | 'blankNode' | 'literal' | 'variable' | 'defaultGraph'> {
18
- quad(subject: Q['subject'], predicate: Q['predicate'], object: Q['object'], graph?: Q['graph']): Q;
19
- }
20
-
21
- export interface MessageParserOptions<Q extends RDF.BaseQuad = RDF.Quad> {
22
- /** Syntax name or media type, case-insensitive. Defaults to `TriG`. */
23
- format?: MessageFormat;
24
- baseIRI?: string;
25
- /** Data factory used to create terms and quads. */
26
- factory?: MessageQuadFactory<Q>;
27
- /** Prefix for generated blank node labels, e.g. `b0_`. */
28
- blankNodePrefix?: string;
29
- /** RDF version label, e.g. `1.2-messages`; a `-messages` suffix forces message mode. */
30
- version?: string;
31
- /** Force message mode even without a `-messages` version declaration. */
32
- messages?: boolean;
33
- }
34
-
35
- export type MessageCallback<Q extends RDF.BaseQuad = RDF.Quad> = (
36
- error: Error | null,
37
- message: Q[] | null,
38
- prefixes?: { [prefix: string]: RDF.NamedNode },
39
- index?: number,
40
- ) => void;
41
-
42
- export interface MessageParseCallbacks<Q extends RDF.BaseQuad = RDF.Quad> {
43
- onMessage?: MessageCallback<Q>;
44
- onQuad?: (error: Error | null, quad?: Q, messageIndex?: number) => void;
45
- onPrefix?: (prefix: string, term: RDF.NamedNode) => void;
46
- onVersion?: (version: string) => void;
47
- }
5
+ export * from './common.js';
6
+ import type { MessageParserOptions, MessageCallback, MessageParseCallbacks, MessageWriterOptions } from './common.js';
48
7
 
49
8
  export class MessageParser<Q extends RDF.BaseQuad = RDF.Quad> {
50
9
  constructor(options?: MessageParserOptions<Q>);
@@ -77,39 +36,6 @@ export class MessageStreamParser<Q extends RDF.BaseQuad = RDF.Quad> extends Tran
77
36
  [Symbol.asyncIterator](): NodeJS.AsyncIterator<Q[]>;
78
37
  }
79
38
 
80
- export interface MessageWriterOptions {
81
- /** Syntax name or media type, case-insensitive. Defaults to `TriG`. */
82
- format?: MessageFormat;
83
- /** Prefixes that are declared up front. */
84
- prefixes?: { [prefix: string]: RDF.NamedNode | string };
85
- baseIRI?: string;
86
- writeBase?: boolean;
87
- lists?: { [listHead: string]: RDF.Term[] };
88
- /** RDF version label announced in the output. Defaults to `1.2-messages`. */
89
- version?: string;
90
- /** Whether to end the output stream on `end()`. Defaults to `true`. */
91
- end?: boolean;
92
- }
93
-
94
- export interface MessageOutputStream {
95
- write(chunk: string, encoding?: string, callback?: (error?: Error | null) => void): unknown;
96
- end(callback?: (error?: Error | null) => void): unknown;
97
- }
98
-
99
- export class MessageWriter {
100
- constructor(options?: MessageWriterOptions);
101
- constructor(outputStream?: MessageOutputStream | NodeJS.WritableStream | null, options?: MessageWriterOptions);
102
- addMessage(quads: RDF.BaseQuad[], done: (error: Error | null) => void): void;
103
- addMessage(quads: RDF.BaseQuad[]): Promise<void>;
104
- addPrefix(prefix: string, iri: RDF.NamedNode | string, done: (error: Error | null) => void): void;
105
- addPrefix(prefix: string, iri: RDF.NamedNode | string): Promise<void>;
106
- /** Without an output stream, the result is the written string. */
107
- end(done: (error: Error | null, result?: string) => void): void;
108
- end(): Promise<string>;
109
- }
110
-
111
- export function writeMessages(messages: Iterable<RDF.BaseQuad[]>, options?: MessageWriterOptions): string;
112
-
113
39
  export class MessageStreamWriter extends Transform {
114
40
  constructor(options?: Omit<MessageWriterOptions, 'end'>);
115
41
  }
@@ -10,12 +10,18 @@ var _MessageParserCore = _interopRequireDefault(require("./MessageParserCore.js"
10
10
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
11
11
  // **MessageParser** parses RDF Message Logs (Turtle, TriG, N-Triples and N-Quads).
12
12
 
13
+ /** @template {import('@rdfjs/types').BaseQuad} [Q=import('@rdfjs/types').Quad] */
13
14
  class MessageParser {
15
+ /** @param {import('../common.js').MessageParserOptions<Q>} [options] */
14
16
  constructor(options) {
17
+ /** @type {import('../common.js').MessageParserOptions<Q>} */
15
18
  this._options = options || {};
16
19
  // Fail early on unsupported options
17
- this._createCore({});
20
+ // Construction only validates options; this core never receives input.
21
+ this._createCore(/** @type {import('./internal.js').ParserSink<Q>} */{});
18
22
  }
23
+
24
+ /** @param {import('./internal.js').ParserSink<Q>} sink */
19
25
  _createCore(sink) {
20
26
  return new _MessageParserCore.default(this._options, sink);
21
27
  }
@@ -28,23 +34,45 @@ class MessageParser {
28
34
  // callbacks `onMessage(error, message, prefixes)`, `onQuad(error, quad, messageIndex)`,
29
35
  // `onPrefix(prefix, term)` and `onVersion(label)`, messages are reported as soon
30
36
  // as they are complete. The final call has `null` as message.
37
+ /**
38
+ * @overload
39
+ * @param {string} input
40
+ * @returns {Q[][]}
41
+ */
42
+ /**
43
+ * @overload
44
+ * @param {string | NodeJS.ReadableStream} input
45
+ * @param {import('../common.js').MessageCallback<Q> | import('../common.js').MessageParseCallbacks<Q>} [callbacks]
46
+ * @returns {void}
47
+ */
48
+ /**
49
+ * @param {string | NodeJS.ReadableStream} input
50
+ * @param {import('../common.js').MessageCallback<Q> | import('../common.js').MessageParseCallbacks<Q>} [callbacks]
51
+ * @returns {Q[][] | undefined}
52
+ */
31
53
  parse(input, callbacks) {
32
54
  if (typeof callbacks === 'function') callbacks = {
33
55
  onMessage: callbacks
34
56
  };
57
+
58
+ /** @type {Q[][] | null} */
35
59
  const messages = callbacks ? null : [];
60
+
61
+ /** @type {Error | null} */
36
62
  let error = null;
37
63
  callbacks = callbacks || {};
64
+
65
+ /** @type {import('./internal.js').ParserSink<Q>} */
38
66
  const sink = {
39
67
  message: (quads, index) => callbacks.onMessage ? callbacks.onMessage(null, quads, undefined, index) : messages && messages.push(quads),
40
68
  end: prefixes => callbacks.onMessage && callbacks.onMessage(null, null, prefixes),
41
69
  error: e => {
42
70
  error = e;
43
- callbacks.onMessage && callbacks.onMessage(e);
71
+ callbacks.onMessage && callbacks.onMessage(e, null);
44
72
  callbacks.onQuad && callbacks.onQuad(e);
45
73
  }
46
74
  };
47
- if (callbacks.onQuad) sink.quad = (quad, index) => callbacks.onQuad(null, quad, index);
75
+ if (callbacks.onQuad) sink.quad = (quad, index) => callbacks.onQuad?.(null, quad, index);
48
76
  if (callbacks.onPrefix) sink.prefix = callbacks.onPrefix;
49
77
  if (callbacks.onVersion) sink.version = callbacks.onVersion;
50
78
  const core = this._createCore(sink);
@@ -69,8 +97,14 @@ class MessageParser {
69
97
  }
70
98
  }
71
99
 
100
+ /**
101
+ * @template {import('@rdfjs/types').BaseQuad} [Q=import('@rdfjs/types').Quad]
102
+ * @param {string} input
103
+ * @param {import('../common.js').MessageParserOptions<Q>} [options]
104
+ */
72
105
  // ### `parseMessages` synchronously parses a string into an array of messages
73
106
  exports.default = MessageParser;
74
107
  function parseMessages(input, options) {
75
108
  return new MessageParser(options).parse(input);
76
- }
109
+ }
110
+ //# sourceMappingURL=MessageParser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MessageParser.js","names":["MessageParserCore","StringDecoder"],"sources":["../src/MessageParser.js"],"sourcesContent":["// **MessageParser** parses RDF Message Logs (Turtle, TriG, N-Triples and N-Quads).\nimport { StringDecoder } from 'string_decoder';\nimport MessageParserCore from './MessageParserCore.js';\n\n/** @template {import('@rdfjs/types').BaseQuad} [Q=import('@rdfjs/types').Quad] */\nexport default class MessageParser {\n /** @param {import('../common.js').MessageParserOptions<Q>} [options] */\n constructor(options) {\n /** @type {import('../common.js').MessageParserOptions<Q>} */\n this._options = options || {};\n // Fail early on unsupported options\n // Construction only validates options; this core never receives input.\n this._createCore(/** @type {import('./internal.js').ParserSink<Q>} */ ({}));\n }\n\n /** @param {import('./internal.js').ParserSink<Q>} sink */\n _createCore(sink) {\n return new MessageParserCore(this._options, sink);\n }\n\n // ### `parse` parses a string or a readable stream of RDF Messages.\n //\n // Without callbacks, a string is parsed synchronously into an array of\n // messages (arrays of quads), and parse errors are thrown.\n // With a callback `(error, message, prefixes)`, or an object with the\n // callbacks `onMessage(error, message, prefixes)`, `onQuad(error, quad, messageIndex)`,\n // `onPrefix(prefix, term)` and `onVersion(label)`, messages are reported as soon\n // as they are complete. The final call has `null` as message.\n /**\n * @overload\n * @param {string} input\n * @returns {Q[][]}\n */\n /**\n * @overload\n * @param {string | NodeJS.ReadableStream} input\n * @param {import('../common.js').MessageCallback<Q> | import('../common.js').MessageParseCallbacks<Q>} [callbacks]\n * @returns {void}\n */\n /**\n * @param {string | NodeJS.ReadableStream} input\n * @param {import('../common.js').MessageCallback<Q> | import('../common.js').MessageParseCallbacks<Q>} [callbacks]\n * @returns {Q[][] | undefined}\n */\n parse(input, callbacks) {\n if (typeof callbacks === 'function')\n callbacks = { onMessage: callbacks };\n\n /** @type {Q[][] | null} */\n const messages = callbacks ? null : [];\n\n /** @type {Error | null} */\n let error = null;\n callbacks = callbacks || {};\n\n /** @type {import('./internal.js').ParserSink<Q>} */\n const sink = {\n message: (quads, index) => callbacks.onMessage ? callbacks.onMessage(null, quads, undefined, index) : messages && messages.push(quads),\n end: prefixes => callbacks.onMessage && callbacks.onMessage(null, null, prefixes),\n error: e => {\n error = e;\n callbacks.onMessage && callbacks.onMessage(e, null);\n callbacks.onQuad && callbacks.onQuad(e);\n },\n };\n if (callbacks.onQuad)\n sink.quad = (quad, index) => callbacks.onQuad?.(null, quad, index);\n if (callbacks.onPrefix)\n sink.prefix = callbacks.onPrefix;\n if (callbacks.onVersion)\n sink.version = callbacks.onVersion;\n const core = this._createCore(sink);\n\n // Parse a string at once\n if (typeof input === 'string') {\n core.write(input);\n core.end();\n if (error && messages)\n throw error;\n return messages || undefined;\n }\n\n // Parse a stream chunk by chunk\n const decoder = new StringDecoder('utf8');\n input.on('data', chunk => core.write(typeof chunk === 'string' ? chunk : decoder.write(chunk)));\n input.on('end', () => {\n core.write(decoder.end());\n core.end();\n });\n input.on('error', e => sink.error(e));\n return undefined;\n }\n}\n\n/**\n * @template {import('@rdfjs/types').BaseQuad} [Q=import('@rdfjs/types').Quad]\n * @param {string} input\n * @param {import('../common.js').MessageParserOptions<Q>} [options]\n */\n// ### `parseMessages` synchronously parses a string into an array of messages\nexport function parseMessages(input, options) {\n return new MessageParser(options).parse(input);\n}\n"],"mappings":";;;;;;;AACA;AACA;AAAuD;AAFvD;;AAIA;AACe,MAAM,aAAa,CAAC;EACjC;EACA,WAAW,CAAC,OAAO,EAAE;IACnB;IACA,IAAI,CAAC,QAAQ,GAAG,OAAO,IAAI,CAAC,CAAC;IAC7B;IACA;IACA,IAAI,CAAC,WAAW,CAAC,oDAAsD,CAAC,CAAE,CAAC;EAC7E;;EAEA;EACA,WAAW,CAAC,IAAI,EAAE;IAChB,OAAO,IAAIA,0BAAiB,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC;EACnD;;EAEA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;AACF;AACA;AACA;AACA;EACE;AACF;AACA;AACA;AACA;AACA;EACE;AACF;AACA;AACA;AACA;EACE,KAAK,CAAC,KAAK,EAAE,SAAS,EAAE;IACtB,IAAI,OAAO,SAAS,KAAK,UAAU,EACjC,SAAS,GAAG;MAAE,SAAS,EAAE;IAAU,CAAC;;IAEtC;IACA,MAAM,QAAQ,GAAG,SAAS,GAAG,IAAI,GAAG,EAAE;;IAEtC;IACA,IAAI,KAAK,GAAG,IAAI;IAChB,SAAS,GAAG,SAAS,IAAI,CAAC,CAAC;;IAE3B;IACA,MAAM,IAAI,GAAG;MACX,OAAO,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,SAAS,CAAC,SAAS,GAAG,SAAS,CAAC,SAAS,CAAC,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,KAAK,CAAC,GAAG,QAAQ,IAAI,QAAQ,CAAC,IAAI,CAA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,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC;MACzB,IAAI,CAAC,GAAG,CAAC,CAAC;IACZ,CAAC,CAAC;IACF,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IACrC,OAAO,SAAS;EAClB;AACF;;AAEA;AACA;AACA;AACA;AACA;AACA;AAAA;AACO,SAAS,aAAa,CAAC,KAAK,EAAE,OAAO,EAAE;EAC5C,OAAO,IAAI,aAAa,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC;AAChD","ignoreList":[]}
@@ -11,27 +11,41 @@ var _formats = require("./formats.js");
11
11
  // It feeds text to the N3.js parser, which recognizes the message delimiters
12
12
  // through its `directives` option, and groups the resulting quads into messages.
13
13
 
14
+ /** @typedef {import('@rdfjs/types').Term} BaseTerm */
15
+ /** @template {import('@rdfjs/types').BaseQuad} [Q=import('@rdfjs/types').Quad] */
14
16
  class MessageParserCore {
15
17
  // `sink` receives `message(quads, index)`, `end(prefixes)`, `error(error)`
16
18
  // and optionally `quad(quad, messageIndex)`, `prefix(prefix, term)`, `version(label)`
19
+ /**
20
+ * @param {import('../common.js').MessageParserOptions<Q> | undefined} options
21
+ * @param {import('./internal.js').ParserSink<Q>} sink
22
+ */
17
23
  constructor(options, sink) {
18
24
  options = options || {};
19
25
  const format = (0, _formats.resolveFormat)(options.format);
20
26
  this._sink = sink;
21
- this._factory = options.factory || _n.DataFactory;
27
+
28
+ /** @type {import('../common.js').MessageQuadFactory<Q>} */
29
+ this._factory = options.factory || /** @type {import('../common.js').MessageQuadFactory<Q>} */_n.DataFactory;
22
30
  this._blankNodePrefix = typeof options.blankNodePrefix === 'string' ? options.blankNodePrefix.replace(/^_:/, '') : undefined;
23
31
  this._blankNodeCounter = 0;
24
32
  this._supportedVersions = _n.Parser.SUPPORTED_VERSIONS;
25
33
  this._messages = options.messages === true || (0, _formats.isMessagesVersion)(options.version);
26
34
  this._index = 0;
35
+
36
+ /** @type {Q[] | null} */
27
37
  this._current = null;
38
+
39
+ /** @type {Map<string, import('@rdfjs/types').BlankNode> | null} */
28
40
  this._scope = null;
29
41
  this._seenQuad = false;
30
42
  this._failed = false;
31
43
  this._finished = false;
44
+
45
+ /** @type {import('n3').ParserOptions} */
32
46
  const n3Options = {
33
47
  format: format.name,
34
- factory: this._factory,
48
+ factory: (/** @type {import('@rdfjs/types').DataFactory} */ /** @type {unknown} */this._factory),
35
49
  // Version labels are validated here, because N3.js does not know `-messages` labels
36
50
  parseUnsupportedVersions: true,
37
51
  // N3.js reports `MESSAGE` and `@message .` between statements through `onDirective`,
@@ -49,7 +63,7 @@ class MessageParserCore {
49
63
  this._line = 0;
50
64
  this._delimiterLine = 0;
51
65
  new _n.Parser(n3Options).parse(this._input, {
52
- onQuad: (error, quad, prefixes) => this._onQuad(error, quad, prefixes),
66
+ onQuad: (error, quad, prefixes) => this._onQuad(error, /** @type {Q | null} */quad, prefixes),
53
67
  onPrefix: (prefix, term) => this._failed || sink.prefix && sink.prefix(prefix, term),
54
68
  onVersion: label => this._failed || this._announce(label),
55
69
  onDirective: () => this._failed || this._delimiter(),
@@ -58,6 +72,7 @@ class MessageParserCore {
58
72
  }
59
73
 
60
74
  // ### `write` accepts the next chunk of text
75
+ /** @param {string} text */
61
76
  write(text) {
62
77
  if (!this._failed && !this._finished && text.length > 0) this._input.emit('data', text);
63
78
  }
@@ -68,6 +83,7 @@ class MessageParserCore {
68
83
  }
69
84
 
70
85
  // Keeps track of lines; in N-Triples and N-Quads, a delimiter must be on its own line
86
+ /** @param {{ line: number; type: string }} token */
71
87
  _onToken(token) {
72
88
  const line = token.line,
73
89
  delimiter = token.type === 'MESSAGE';
@@ -75,6 +91,10 @@ class MessageParserCore {
75
91
  if (delimiter) this._delimiterLine = line;
76
92
  this._line = line;
77
93
  }
94
+
95
+ /** @param {Error | null} error
96
+ * @param {Q | null} quad
97
+ * @param {Record<string, import('@rdfjs/types').NamedNode>} [prefixes] */
78
98
  _onQuad(error, quad, prefixes) {
79
99
  if (this._failed || this._finished) return;
80
100
  if (error) this._fail(error);else if (quad) this._addQuad(quad);else {
@@ -83,6 +103,8 @@ class MessageParserCore {
83
103
  this._sink.end(prefixes);
84
104
  }
85
105
  }
106
+
107
+ /** @param {Q} quad */
86
108
  _addQuad(quad) {
87
109
  this._seenQuad = true;
88
110
  // Blank node labels are scoped to their message
@@ -91,6 +113,8 @@ class MessageParserCore {
91
113
  this._current.push(quad);
92
114
  if (this._sink.quad) this._sink.quad(quad, this._index);
93
115
  }
116
+
117
+ /** @param {string} label */
94
118
  _announce(label) {
95
119
  const error = this._checkVersion(label);
96
120
  if (error) return this._fail(error);
@@ -102,6 +126,8 @@ class MessageParserCore {
102
126
  }
103
127
 
104
128
  // Validates a version label, and returns an error for unsupported ones
129
+ /** @param {string} label
130
+ * @param {boolean} [throwing] */
105
131
  _checkVersion(label, throwing) {
106
132
  if ((0, _formats.isSupportedVersion)((0, _formats.baseVersion)(label), this._supportedVersions)) return null;
107
133
  const error = new Error(`Detected unsupported version: "${label}"`);
@@ -122,6 +148,8 @@ class MessageParserCore {
122
148
  this._scope = null;
123
149
  this._sink.message(quads, this._index++);
124
150
  }
151
+
152
+ /** @param {Error} error */
125
153
  _fail(error) {
126
154
  this._failed = true;
127
155
  this._current = null;
@@ -130,6 +158,8 @@ class MessageParserCore {
130
158
 
131
159
  // ## Blank node scoping
132
160
 
161
+ /** @param {Q} quad
162
+ * @returns {Q} */
133
163
  _scopeQuad(quad) {
134
164
  const subject = this._scopeTerm(quad.subject);
135
165
  const predicate = this._scopeTerm(quad.predicate);
@@ -137,6 +167,10 @@ class MessageParserCore {
137
167
  const graph = this._scopeTerm(quad.graph);
138
168
  return subject === quad.subject && predicate === quad.predicate && object === quad.object && graph === quad.graph ? quad : this._factory.quad(subject, predicate, object, graph);
139
169
  }
170
+
171
+ /** @template {BaseTerm} T
172
+ * @param {T} term
173
+ * @returns {T} */
140
174
  _scopeTerm(term) {
141
175
  if (term.termType === 'BlankNode') {
142
176
  const scope = this._scope || (this._scope = new Map());
@@ -145,9 +179,11 @@ class MessageParserCore {
145
179
  scoped = this._blankNodePrefix === undefined ? this._factory.blankNode() : this._factory.blankNode(`${this._blankNodePrefix}${this._blankNodeCounter++}`);
146
180
  scope.set(term.value, scoped);
147
181
  }
148
- return scoped;
182
+ return /** @type {T} */ /** @type {unknown} */scoped;
149
183
  }
150
- return term.termType === 'Quad' ? this._scopeQuad(term) : term;
184
+ return term.termType === 'Quad' ? (/** @type {T} */ /** @type {unknown} */
185
+ this._scopeQuad(/** @type {Q} */ /** @type {unknown} */term)) : term;
151
186
  }
152
187
  }
153
- exports.default = MessageParserCore;
188
+ exports.default = MessageParserCore;
189
+ //# sourceMappingURL=MessageParserCore.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MessageParserCore.js","names":["resolveFormat","DataFactory","Parser","isMessagesVersion","baseVersion","EventEmitter","isSupportedVersion"],"sources":["../src/MessageParserCore.js"],"sourcesContent":["// **MessageParserCore** is the push-based engine behind all message parsers.\n// It feeds text to the N3.js parser, which recognizes the message delimiters\n// through its `directives` option, and groups the resulting quads into messages.\nimport { EventEmitter } from 'events';\nimport { Parser, DataFactory } from 'n3';\nimport { resolveFormat, isMessagesVersion, baseVersion, isSupportedVersion } from './formats.js';\n\n/** @typedef {import('@rdfjs/types').Term} BaseTerm */\n/** @template {import('@rdfjs/types').BaseQuad} [Q=import('@rdfjs/types').Quad] */\nexport default class MessageParserCore {\n // `sink` receives `message(quads, index)`, `end(prefixes)`, `error(error)`\n // and optionally `quad(quad, messageIndex)`, `prefix(prefix, term)`, `version(label)`\n /**\n * @param {import('../common.js').MessageParserOptions<Q> | undefined} options\n * @param {import('./internal.js').ParserSink<Q>} sink\n */\n constructor(options, sink) {\n options = options || {};\n const format = resolveFormat(options.format);\n this._sink = sink;\n\n /** @type {import('../common.js').MessageQuadFactory<Q>} */\n this._factory = options.factory || /** @type {import('../common.js').MessageQuadFactory<Q>} */ (DataFactory);\n this._blankNodePrefix = typeof options.blankNodePrefix === 'string' ?\n options.blankNodePrefix.replace(/^_:/, '') : undefined;\n this._blankNodeCounter = 0;\n this._supportedVersions = Parser.SUPPORTED_VERSIONS;\n this._messages = options.messages === true || isMessagesVersion(options.version);\n this._index = 0;\n\n /** @type {Q[] | null} */\n this._current = null;\n\n /** @type {Map<string, import('@rdfjs/types').BlankNode> | null} */\n this._scope = null;\n this._seenQuad = false;\n this._failed = false;\n this._finished = false;\n\n /** @type {import('n3').ParserOptions} */\n const n3Options = {\n format: format.name,\n factory: /** @type {import('@rdfjs/types').DataFactory} */ (/** @type {unknown} */ (this._factory)),\n // Version labels are validated here, because N3.js does not know `-messages` labels\n parseUnsupportedVersions: true,\n // N3.js reports `MESSAGE` and `@message .` between statements through `onDirective`,\n // in order with the quads, and rejects them anywhere else\n directives: ['message'],\n };\n if (options.baseIRI)\n n3Options.baseIRI = options.baseIRI;\n if (options.version) {\n this._checkVersion(options.version, true);\n n3Options.version = baseVersion(options.version);\n }\n\n this._input = new EventEmitter();\n this._lineMode = format.lineMode;\n // The line of the current token, and of the last delimiter\n this._line = 0;\n this._delimiterLine = 0;\n new Parser(n3Options).parse(this._input, {\n onQuad: (error, quad, prefixes) => this._onQuad(error, /** @type {Q | null} */ (quad), prefixes),\n onPrefix: (prefix, term) => this._failed || sink.prefix && sink.prefix(prefix, term),\n onVersion: label => this._failed || this._announce(label),\n onDirective: () => this._failed || this._delimiter(),\n onToken: token => this._onToken(token),\n });\n }\n\n // ### `write` accepts the next chunk of text\n /** @param {string} text */\n write(text) {\n if (!this._failed && !this._finished && text.length > 0)\n this._input.emit('data', text);\n }\n\n // ### `end` signals the end of the input\n end() {\n if (!this._failed && !this._finished)\n this._input.emit('end');\n }\n\n // Keeps track of lines; in N-Triples and N-Quads, a delimiter must be on its own line\n /** @param {{ line: number; type: string }} token */\n _onToken(token) {\n const line = token.line, delimiter = token.type === 'MESSAGE';\n if (this._lineMode && !this._failed &&\n (delimiter ? line === this._line : line === this._delimiterLine && token.type !== 'eof'))\n this._fail(new Error(`Unexpected \"MESSAGE\" on line ${line}.`));\n if (delimiter)\n this._delimiterLine = line;\n this._line = line;\n }\n\n /** @param {Error | null} error\n * @param {Q | null} quad\n * @param {Record<string, import('@rdfjs/types').NamedNode>} [prefixes] */\n _onQuad(error, quad, prefixes) {\n if (this._failed || this._finished)\n return;\n if (error)\n this._fail(error);\n else if (quad)\n this._addQuad(quad);\n else {\n this._finished = true;\n if (this._current !== null)\n this._emitMessage();\n this._sink.end(prefixes);\n }\n }\n\n /** @param {Q} quad */\n _addQuad(quad) {\n this._seenQuad = true;\n // Blank node labels are scoped to their message\n if (this._messages)\n quad = this._scopeQuad(quad);\n if (this._current === null)\n this._current = [];\n this._current.push(quad);\n if (this._sink.quad)\n this._sink.quad(quad, this._index);\n }\n\n /** @param {string} label */\n _announce(label) {\n const error = this._checkVersion(label);\n if (error)\n return this._fail(error);\n if (isMessagesVersion(label) && !this._messages) {\n if (this._seenQuad)\n return this._fail(new Error(`Version \"${label}\" must be announced before the first statement.`));\n this._messages = true;\n }\n if (this._sink.version)\n this._sink.version(label);\n }\n\n // Validates a version label, and returns an error for unsupported ones\n /** @param {string} label\n * @param {boolean} [throwing] */\n _checkVersion(label, throwing) {\n if (isSupportedVersion(baseVersion(label), this._supportedVersions))\n return null;\n const error = new Error(`Detected unsupported version: \"${label}\"`);\n if (throwing)\n throw error;\n return error;\n }\n\n _delimiter() {\n if (!this._messages) {\n return this._fail(new Error(`Unexpected \"MESSAGE\" on line ${this._line}: the document did not announce ` +\n 'a version with the \"-messages\" suffix.'));\n }\n this._emitMessage();\n }\n\n // Finalizes the current message, which is empty if no quad was seen\n _emitMessage() {\n const quads = this._current || [];\n this._current = null;\n this._scope = null;\n this._sink.message(quads, this._index++);\n }\n\n /** @param {Error} error */\n _fail(error) {\n this._failed = true;\n this._current = null;\n this._sink.error(error);\n }\n\n // ## Blank node scoping\n\n /** @param {Q} quad\n * @returns {Q} */\n _scopeQuad(quad) {\n const subject = this._scopeTerm(quad.subject);\n const predicate = this._scopeTerm(quad.predicate);\n const object = this._scopeTerm(quad.object);\n const graph = this._scopeTerm(quad.graph);\n return subject === quad.subject && predicate === quad.predicate &&\n object === quad.object && graph === quad.graph ?\n quad : this._factory.quad(subject, predicate, object, graph);\n }\n\n /** @template {BaseTerm} T\n * @param {T} term\n * @returns {T} */\n _scopeTerm(term) {\n if (term.termType === 'BlankNode') {\n const scope = this._scope || (this._scope = new Map());\n let scoped = scope.get(term.value);\n if (!scoped) {\n scoped = this._blankNodePrefix === undefined ? this._factory.blankNode() :\n this._factory.blankNode(`${this._blankNodePrefix}${this._blankNodeCounter++}`);\n scope.set(term.value, scoped);\n }\n return /** @type {T} */ (/** @type {unknown} */ (scoped));\n }\n return term.termType === 'Quad' ? /** @type {T} */ (/** @type {unknown} */ (\n this._scopeQuad(/** @type {Q} */ (/** @type {unknown} */ (term))))) : term;\n }\n}\n"],"mappings":";;;;;;AAGA;AACA;AACA;AALA;AACA;AACA;;AAKA;AACA;AACe,MAAM,iBAAiB,CAAC;EACrC;EACA;EACA;AACF;AACA;AACA;EACE,WAAW,CAAC,OAAO,EAAE,IAAI,EAAE;IACzB,OAAO,GAAG,OAAO,IAAI,CAAC,CAAC;IACvB,MAAM,MAAM,GAAG,IAAAA,SAAA,aAAa,EAAC,OAAO,CAAC,MAAM,CAAC;IAC5C,IAAI,CAAC,KAAK,GAAG,IAAI;;IAEjB;IACA,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,OAAO,IAAI,2DAA6DC,GAAA,WAAY;IAC5G,IAAI,CAAC,gBAAgB,GAAG,OAAO,OAAO,CAAC,eAAe,KAAK,QAAQ,GACjE,OAAO,CAAC,eAAe,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,GAAG,SAAS;IACxD,IAAI,CAAC,iBAAiB,GAAG,CAAC;IAC1B,IAAI,CAAC,kBAAkB,GAAGC,G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BAAkB,sBAAwB,IAAM,CAAC,IAAK,IAAI;EAC9E;AACF;AAAC","ignoreList":[]}
@@ -13,13 +13,17 @@ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e
13
13
  // It consumes strings or buffers, and produces one array of quads per message,
14
14
  // as soon as that message is complete.
15
15
 
16
+ /** @template {import('@rdfjs/types').BaseQuad} [Q=import('@rdfjs/types').Quad] */
16
17
  class MessageStreamParser extends _stream.Transform {
18
+ /** @param {import('../common.js').MessageParserOptions<Q>} [options] */
17
19
  constructor(options) {
18
20
  super({
19
21
  decodeStrings: false,
20
22
  readableObjectMode: true
21
23
  });
22
24
  this._decoder = new _string_decoder.StringDecoder('utf8');
25
+
26
+ /** @type {Error | null} */
23
27
  this._error = null;
24
28
  this._core = new _MessageParserCore.default(options, {
25
29
  message: quads => this.push(quads),
@@ -31,14 +35,21 @@ class MessageStreamParser extends _stream.Transform {
31
35
  version: label => this.emit('version', label)
32
36
  });
33
37
  }
38
+
39
+ /** @param {string | Buffer} chunk
40
+ * @param {BufferEncoding} encoding
41
+ * @param {import('node:stream').TransformCallback} done */
34
42
  _transform(chunk, encoding, done) {
35
43
  this._core.write(typeof chunk === 'string' ? chunk : this._decoder.write(chunk));
36
44
  done(this._error);
37
45
  }
46
+
47
+ /** @param {import('node:stream').TransformCallback} done */
38
48
  _flush(done) {
39
49
  this._core.write(this._decoder.end());
40
50
  this._core.end();
41
51
  done(this._error);
42
52
  }
43
53
  }
44
- exports.default = MessageStreamParser;
54
+ exports.default = MessageStreamParser;
55
+ //# sourceMappingURL=MessageStreamParser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MessageStreamParser.js","names":["Transform","StringDecoder","MessageParserCore"],"sources":["../src/MessageStreamParser.js"],"sourcesContent":["// **MessageStreamParser** is a Node.js stream that turns text into RDF Messages.\n//\n// It consumes strings or buffers, and produces one array of quads per message,\n// as soon as that message is complete.\nimport { Transform } from 'stream';\nimport { StringDecoder } from 'string_decoder';\nimport MessageParserCore from './MessageParserCore.js';\n\n/** @template {import('@rdfjs/types').BaseQuad} [Q=import('@rdfjs/types').Quad] */\nexport default class MessageStreamParser extends Transform {\n /** @param {import('../common.js').MessageParserOptions<Q>} [options] */\n constructor(options) {\n super({ decodeStrings: false, readableObjectMode: true });\n this._decoder = new StringDecoder('utf8');\n\n /** @type {Error | null} */\n this._error = null;\n this._core = new MessageParserCore(options, {\n message: quads => this.push(quads),\n end: () => {},\n error: error => { this._error = this._error || error; },\n prefix: (prefix, term) => this.emit('prefix', prefix, term),\n version: label => this.emit('version', label),\n });\n }\n\n /** @param {string | Buffer} chunk\n * @param {BufferEncoding} encoding\n * @param {import('node:stream').TransformCallback} done */\n _transform(chunk, encoding, done) {\n this._core.write(typeof chunk === 'string' ? chunk : this._decoder.write(chunk));\n done(this._error);\n }\n\n /** @param {import('node:stream').TransformCallback} done */\n _flush(done) {\n this._core.write(this._decoder.end());\n this._core.end();\n done(this._error);\n }\n}\n"],"mappings":";;;;;;AAIA;AACA;AACA;AAAuD;AANvD;AACA;AACA;AACA;;AAKA;AACe,MAAM,mBAAmB,SAASA,QAAA,SAAS,CAAC;EACzD;EACA,WAAW,CAAC,OAAO,EAAE;IACnB,KAAK,CAAC;MAAE,aAAa,EAAE,KAAK;MAAE,kBAAkB,EAAE;IAAK,CAAC,CAAC;IACzD,IAAI,CAAC,QAAQ,GAAG,IAAIC,gBAAA,aAAa,CAAC,MAAM,CAAC;;IAEzC;IACA,IAAI,CAAC,MAAM,GAAG,IAAI;IAClB,IAAI,CAAC,KAAK,GAAG,IAAIC,0BAAiB,CAAC,OAAO,EAAE;MAC1C,OAAO,EAAE,KAAK,IAAI,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC;MAClC,GAAG,EAAE,MAAM,CAAC,CAAC;MACb,KAAK,EAAE,KAAK,IAAI;QAAE,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,IAAI,KAAK;MAAE,CAAC;MACvD,MAAM,EAAE,CAAC,MAAM,EAAE,IAAI,KAAK,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,CAAC;MAC3D,OAAO,EAAE,KAAK,IAAI,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,KAAK;IAC9C,CAAC,CAAC;EACJ;;EAEA;AACF;AACA;EACE,UAAU,CAAC,KAAK,EAAE,QAAQ,EAAE,IAAI,EAAE;IAChC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,KAAK,KAAK,QAAQ,GAAG,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAChF,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC;EACnB;;EAEA;EACA,MAAM,CAAC,IAAI,EAAE;IACX,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC;IACrC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAChB,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC;EACnB;AACF;AAAC","ignoreList":[]}
@@ -9,15 +9,17 @@ var _MessageWriter = _interopRequireDefault(require("./MessageWriter.js"));
9
9
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
10
  // **MessageStreamWriter** is a Node.js stream that serializes RDF Messages.
11
11
  //
12
- // It consumes messages (arrays of quads) and produces text.
12
+ // It consumes messages (arrays of quads), or `{ quad, messageIndex }` objects, and produces text.
13
13
 
14
14
  class MessageStreamWriter extends _stream.Transform {
15
+ /** @param {import('../common.js').MessageWriterOptions} [options] */
15
16
  constructor(options) {
16
17
  super({
17
18
  writableObjectMode: true,
18
19
  encoding: 'utf8'
19
20
  });
20
21
  // Writing into this stream never blocks, and output is pushed synchronously
22
+ /** @type {Pick<import('./internal.js').Output, 'write'>} */
21
23
  const output = {
22
24
  write: (chunk, encoding, done) => (this.push(chunk), done && done())
23
25
  };
@@ -25,11 +27,19 @@ class MessageStreamWriter extends _stream.Transform {
25
27
  end: false
26
28
  }));
27
29
  }
30
+
31
+ /** @param {import('../browser.js').MessageWriterInput} message
32
+ * @param {BufferEncoding} encoding
33
+ * @param {import('node:stream').TransformCallback} done */
28
34
  _transform(message, encoding, done) {
29
- this._writer.addMessage(message, done);
35
+ // A message (array of quads), or a quad of a message: `{ quad, messageIndex }`
36
+ if (Array.isArray(message)) this._writer.addMessage(message, done);else this._writer.addQuad(message.quad, message.messageIndex, done);
30
37
  }
38
+
39
+ /** @param {import('node:stream').TransformCallback} done */
31
40
  _flush(done) {
32
41
  this._writer.end(done);
33
42
  }
34
43
  }
35
- exports.default = MessageStreamWriter;
44
+ exports.default = MessageStreamWriter;
45
+ //# sourceMappingURL=MessageStreamWriter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MessageStreamWriter.js","names":["Transform","MessageWriter"],"sources":["../src/MessageStreamWriter.js"],"sourcesContent":["// **MessageStreamWriter** is a Node.js stream that serializes RDF Messages.\n//\n// It consumes messages (arrays of quads), or `{ quad, messageIndex }` objects, and produces text.\nimport { Transform } from 'stream';\nimport MessageWriter from './MessageWriter.js';\n\nexport default class MessageStreamWriter extends Transform {\n /** @param {import('../common.js').MessageWriterOptions} [options] */\n constructor(options) {\n super({ writableObjectMode: true, encoding: 'utf8' });\n // Writing into this stream never blocks, and output is pushed synchronously\n /** @type {Pick<import('./internal.js').Output, 'write'>} */\n const output = { write: (chunk, encoding, done) => (this.push(chunk), done && done()) };\n this._writer = new MessageWriter(output, Object.assign({}, options, { end: false }));\n }\n\n /** @param {import('../browser.js').MessageWriterInput} message\n * @param {BufferEncoding} encoding\n * @param {import('node:stream').TransformCallback} done */\n _transform(message, encoding, done) {\n // A message (array of quads), or a quad of a message: `{ quad, messageIndex }`\n if (Array.isArray(message)) this._writer.addMessage(message, done);\n else this._writer.addQuad(message.quad, message.messageIndex, done);\n }\n\n /** @param {import('node:stream').TransformCallback} done */\n _flush(done) {\n this._writer.end(done);\n }\n}\n"],"mappings":";;;;;;AAGA;AACA;AAA+C;AAJ/C;AACA;AACA;;AAIe,MAAM,mBAAmB,SAASA,QAAA,SAAS,CAAC;EACzD;EACA,WAAW,CAAC,OAAO,EAAE;IACnB,KAAK,CAAC;MAAE,kBAAkB,EAAE,IAAI;MAAE,QAAQ,EAAE;IAAO,CAAC,CAAC;IACrD;IACA;IACA,MAAM,MAAM,GAAG;MAAE,KAAK,EAAE,CAAC,KAAK,EAAE,QAAQ,EAAE,IAAI,MAAM,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,IAAI,IAAI,IAAI,CAAC,CAAC;IAAE,CAAC;IACvF,IAAI,CAAC,OAAO,GAAG,IAAIC,sBAAa,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE;MAAE,GAAG,EAAE;IAAM,CAAC,CAAC,CAAC;EACtF;;EAEA;AACF;AACA;EACE,UAAU,CAAC,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE;IAClC;IACA,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,KAC9D,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,YAAY,EAAE,IAAI,CAAC;EACrE;;EAEA;EACA,MAAM,CAAC,IAAI,EAAE;IACX,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC;EACxB;AACF;AAAC","ignoreList":[]}