@@ -13,10 +13,14 @@ enum State {
13
13
BEFORE_CHUNK_END ,
14
14
}
15
15
16
- export function jsonSeqDelimiterTransformer ( options ?: {
16
+ export interface _JsonSeqDelimiterTransformerOptions {
17
17
strict ?: boolean ;
18
- } ) : Transformer < string , string > {
19
- const strict = options ?. strict === undefined ? true : options ?. strict ;
18
+ }
19
+
20
+ export function _jsonSeqDelimiterTransformer (
21
+ options ?: _JsonSeqDelimiterTransformerOptions ,
22
+ ) : Transformer < string , string > {
23
+ const strict = options ?. strict === undefined ? false : options ?. strict ;
20
24
let state : State = State . BEFORE_CHUNK_START ;
21
25
let unDelimitedChunks : string [ ] = [ ] ;
22
26
@@ -29,7 +33,7 @@ export function jsonSeqDelimiterTransformer(options?: {
29
33
throw new Error (
30
34
`leading content before chunk start: ${
31
35
start < 0 ? chunk : chunk . substring ( 0 , start )
32
- } `
36
+ } `,
33
37
) ;
34
38
}
35
39
if ( start < 0 ) {
@@ -63,25 +67,52 @@ export function jsonSeqDelimiterTransformer(options?: {
63
67
} ;
64
68
}
65
69
66
- export function stringToJSONTransformer ( ) : Transformer < string , unknown > {
70
+ export function _stringToJSONTransformer ( ) : Transformer < string , unknown > {
67
71
return {
68
72
transform ( chunk , controller ) {
69
73
controller . enqueue ( JSON . parse ( chunk ) ) ;
70
74
} ,
71
75
} ;
72
76
}
73
77
78
+ export interface JsonSequenceDecoderStreamOptions {
79
+ /** If `true`, raise errors instead of recovering when parsing malformed
80
+ * streams.
81
+ *
82
+ * The default is `false`, as the json-seq spec (RFC 7464) encourages decoders
83
+ * to automatically handle stream errors, such as truncated JSON texts.
84
+ *
85
+ * * When `true`, the decoder behaves as if the stream format **MUST** exactly
86
+ * match the [JSON Text Sequence Encoding] format.
87
+ * * When `false` the decoder follows the more permissive
88
+ * [JSON Text Sequence Parsing] format and other permissive behaviour
89
+ * described in the spec.
90
+ *
91
+ * [JSON Text Sequence Encoding]: https://datatracker.ietf.org/doc/html/rfc7464#section-2.2
92
+ * [JSON Text Sequence Parsing]: https://datatracker.ietf.org/doc/html/rfc7464#section-2.1
93
+ */
94
+ strict ?: boolean ;
95
+ }
96
+
97
+ /** A streaming decoder that decodes RFC 7464 JSON Sequences to JSON values.
98
+ *
99
+ * The stream consumes UTF-8-encoded bytes. The byte stream consists of zero or
100
+ * more JSON-encoded values, each of which is preceded by a `'\x1E'` character,
101
+ * and followed by a `'\n'` character.
102
+ *
103
+ * The stream produces the values resulting from parsing each individual JSON
104
+ * text in the stream.
105
+ */
74
106
export class JsonSequenceDecoderStream
75
- implements TransformStream < Uint8Array , unknown >
76
- {
107
+ implements TransformStream < Uint8Array , unknown > {
77
108
readonly readable : ReadableStream < unknown > ;
78
109
readonly writable : WritableStream < Uint8Array > ;
79
110
80
- constructor ( ) {
111
+ constructor ( options ?: JsonSequenceDecoderStreamOptions ) {
81
112
const decoder = new TextDecoderStream ( ) ;
82
113
this . readable = decoder . readable
83
- . pipeThrough ( new TransformStream ( jsonSeqDelimiterTransformer ( ) ) )
84
- . pipeThrough ( new TransformStream ( stringToJSONTransformer ( ) ) ) ;
114
+ . pipeThrough ( new TransformStream ( _jsonSeqDelimiterTransformer ( options ) ) )
115
+ . pipeThrough ( new TransformStream ( _stringToJSONTransformer ( ) ) ) ;
85
116
this . writable = decoder . writable ;
86
117
}
87
118
}
0 commit comments