deno.land / std@0.180.0 / streams / delimiter_stream.ts

delimiter_stream.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { BytesList } from "../bytes/bytes_list.ts";import { createLPS } from "./_common.ts";
/** Disposition of the delimiter. */export type DelimiterDisposition = /** Include delimiter in the found chunk. */ | "suffix" /** Include delimiter in the subsequent chunk. */ | "prefix" /** Discard the delimiter. */ | "discard" // delimiter discarded;
export interface DelimiterStreamOptions { /** Disposition of the delimiter. */ disposition?: DelimiterDisposition;}
/** * Divide a stream into chunks delimited by a given byte sequence. * * @example * Divide a CSV stream by commas, discarding the commas: * ```ts * import { DelimiterStream } from "https://deno.land/std@$STD_VERSION/streams/delimiter_stream.ts"; * const res = await fetch("https://example.com/data.csv"); * const parts = res.body! * .pipeThrough(new DelimiterStream(new TextEncoder().encode(","))) * .pipeThrough(new TextDecoderStream()); * ``` * * @example * Divide a stream after semi-colons, keeping the semi-colons in the output: * ```ts * import { DelimiterStream } from "https://deno.land/std@$STD_VERSION/streams/delimiter_stream.ts"; * const res = await fetch("https://example.com/file.js"); * const parts = res.body! * .pipeThrough( * new DelimiterStream( * new TextEncoder().encode(";"), * { disposition: "suffix" }, * ) * ) * .pipeThrough(new TextDecoderStream()); * ``` * * @param delimiter Delimiter byte sequence * @param options Options for the transform stream * @returns Transform stream */export class DelimiterStream extends TransformStream<Uint8Array, Uint8Array> { #bufs = new BytesList(); #delimiter: Uint8Array; #inspectIndex = 0; #matchIndex = 0; #delimLen: number; #delimLPS: Uint8Array; #disp?: DelimiterDisposition;
constructor( delimiter: Uint8Array, options?: DelimiterStreamOptions, ) { super({ transform: (chunk, controller) => { this.#handle(chunk, controller); }, flush: (controller) => { controller.enqueue(this.#bufs.concat()); }, });
this.#delimiter = delimiter; this.#delimLen = delimiter.length; this.#delimLPS = createLPS(delimiter); this.#disp = options?.disposition ?? "discard"; }
#handle( chunk: Uint8Array, controller: TransformStreamDefaultController<Uint8Array>, ) { this.#bufs.add(chunk); let localIndex = 0; while (this.#inspectIndex < this.#bufs.size()) { if (chunk[localIndex] === this.#delimiter[this.#matchIndex]) { this.#inspectIndex++; localIndex++; this.#matchIndex++; if (this.#matchIndex === this.#delimLen) { // Full match const start = this.#inspectIndex - this.#delimLen; const end = this.#disp == "suffix" ? this.#inspectIndex : start; const copy = this.#bufs.slice(0, end); controller.enqueue(copy); const shift = this.#disp == "prefix" ? start : this.#inspectIndex; this.#bufs.shift(shift); this.#inspectIndex = this.#disp == "prefix" ? this.#delimLen : 0; this.#matchIndex = 0; } } else { if (this.#matchIndex === 0) { this.#inspectIndex++; localIndex++; } else { this.#matchIndex = this.#delimLPS[this.#matchIndex - 1]; } } } }}
std

Version Info

Tagged at
a year ago