@xterm/xterm 5.4.0-beta.1

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.
Files changed (108) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +235 -0
  3. package/css/xterm.css +209 -0
  4. package/lib/xterm.js +2 -0
  5. package/lib/xterm.js.map +1 -0
  6. package/package.json +101 -0
  7. package/src/browser/AccessibilityManager.ts +278 -0
  8. package/src/browser/Clipboard.ts +93 -0
  9. package/src/browser/ColorContrastCache.ts +34 -0
  10. package/src/browser/Lifecycle.ts +33 -0
  11. package/src/browser/Linkifier2.ts +416 -0
  12. package/src/browser/LocalizableStrings.ts +12 -0
  13. package/src/browser/OscLinkProvider.ts +128 -0
  14. package/src/browser/RenderDebouncer.ts +83 -0
  15. package/src/browser/Terminal.ts +1317 -0
  16. package/src/browser/TimeBasedDebouncer.ts +86 -0
  17. package/src/browser/Types.d.ts +181 -0
  18. package/src/browser/Viewport.ts +401 -0
  19. package/src/browser/decorations/BufferDecorationRenderer.ts +134 -0
  20. package/src/browser/decorations/ColorZoneStore.ts +117 -0
  21. package/src/browser/decorations/OverviewRulerRenderer.ts +218 -0
  22. package/src/browser/input/CompositionHelper.ts +246 -0
  23. package/src/browser/input/Mouse.ts +54 -0
  24. package/src/browser/input/MoveToCell.ts +249 -0
  25. package/src/browser/public/Terminal.ts +260 -0
  26. package/src/browser/renderer/dom/DomRenderer.ts +509 -0
  27. package/src/browser/renderer/dom/DomRendererRowFactory.ts +526 -0
  28. package/src/browser/renderer/dom/WidthCache.ts +160 -0
  29. package/src/browser/renderer/shared/CellColorResolver.ts +137 -0
  30. package/src/browser/renderer/shared/CharAtlasCache.ts +96 -0
  31. package/src/browser/renderer/shared/CharAtlasUtils.ts +75 -0
  32. package/src/browser/renderer/shared/Constants.ts +14 -0
  33. package/src/browser/renderer/shared/CursorBlinkStateManager.ts +146 -0
  34. package/src/browser/renderer/shared/CustomGlyphs.ts +687 -0
  35. package/src/browser/renderer/shared/DevicePixelObserver.ts +41 -0
  36. package/src/browser/renderer/shared/README.md +1 -0
  37. package/src/browser/renderer/shared/RendererUtils.ts +58 -0
  38. package/src/browser/renderer/shared/SelectionRenderModel.ts +91 -0
  39. package/src/browser/renderer/shared/TextureAtlas.ts +1082 -0
  40. package/src/browser/renderer/shared/Types.d.ts +173 -0
  41. package/src/browser/selection/SelectionModel.ts +144 -0
  42. package/src/browser/selection/Types.d.ts +15 -0
  43. package/src/browser/services/CharSizeService.ts +102 -0
  44. package/src/browser/services/CharacterJoinerService.ts +339 -0
  45. package/src/browser/services/CoreBrowserService.ts +137 -0
  46. package/src/browser/services/MouseService.ts +46 -0
  47. package/src/browser/services/RenderService.ts +279 -0
  48. package/src/browser/services/SelectionService.ts +1031 -0
  49. package/src/browser/services/Services.ts +147 -0
  50. package/src/browser/services/ThemeService.ts +237 -0
  51. package/src/common/CircularList.ts +241 -0
  52. package/src/common/Clone.ts +23 -0
  53. package/src/common/Color.ts +357 -0
  54. package/src/common/CoreTerminal.ts +284 -0
  55. package/src/common/EventEmitter.ts +78 -0
  56. package/src/common/InputHandler.ts +3461 -0
  57. package/src/common/Lifecycle.ts +108 -0
  58. package/src/common/MultiKeyMap.ts +42 -0
  59. package/src/common/Platform.ts +44 -0
  60. package/src/common/SortedList.ts +118 -0
  61. package/src/common/TaskQueue.ts +166 -0
  62. package/src/common/TypedArrayUtils.ts +17 -0
  63. package/src/common/Types.d.ts +553 -0
  64. package/src/common/WindowsMode.ts +27 -0
  65. package/src/common/buffer/AttributeData.ts +196 -0
  66. package/src/common/buffer/Buffer.ts +654 -0
  67. package/src/common/buffer/BufferLine.ts +524 -0
  68. package/src/common/buffer/BufferRange.ts +13 -0
  69. package/src/common/buffer/BufferReflow.ts +223 -0
  70. package/src/common/buffer/BufferSet.ts +134 -0
  71. package/src/common/buffer/CellData.ts +94 -0
  72. package/src/common/buffer/Constants.ts +149 -0
  73. package/src/common/buffer/Marker.ts +43 -0
  74. package/src/common/buffer/Types.d.ts +52 -0
  75. package/src/common/data/Charsets.ts +256 -0
  76. package/src/common/data/EscapeSequences.ts +153 -0
  77. package/src/common/input/Keyboard.ts +398 -0
  78. package/src/common/input/TextDecoder.ts +346 -0
  79. package/src/common/input/UnicodeV6.ts +145 -0
  80. package/src/common/input/WriteBuffer.ts +246 -0
  81. package/src/common/input/XParseColor.ts +80 -0
  82. package/src/common/parser/Constants.ts +58 -0
  83. package/src/common/parser/DcsParser.ts +192 -0
  84. package/src/common/parser/EscapeSequenceParser.ts +792 -0
  85. package/src/common/parser/OscParser.ts +238 -0
  86. package/src/common/parser/Params.ts +229 -0
  87. package/src/common/parser/Types.d.ts +275 -0
  88. package/src/common/public/AddonManager.ts +53 -0
  89. package/src/common/public/BufferApiView.ts +35 -0
  90. package/src/common/public/BufferLineApiView.ts +29 -0
  91. package/src/common/public/BufferNamespaceApi.ts +36 -0
  92. package/src/common/public/ParserApi.ts +37 -0
  93. package/src/common/public/UnicodeApi.ts +27 -0
  94. package/src/common/services/BufferService.ts +151 -0
  95. package/src/common/services/CharsetService.ts +34 -0
  96. package/src/common/services/CoreMouseService.ts +318 -0
  97. package/src/common/services/CoreService.ts +87 -0
  98. package/src/common/services/DecorationService.ts +140 -0
  99. package/src/common/services/InstantiationService.ts +85 -0
  100. package/src/common/services/LogService.ts +124 -0
  101. package/src/common/services/OptionsService.ts +202 -0
  102. package/src/common/services/OscLinkService.ts +115 -0
  103. package/src/common/services/ServiceRegistry.ts +49 -0
  104. package/src/common/services/Services.ts +373 -0
  105. package/src/common/services/UnicodeService.ts +111 -0
  106. package/src/headless/Terminal.ts +136 -0
  107. package/src/headless/public/Terminal.ts +195 -0
  108. package/typings/xterm.d.ts +1857 -0
@@ -0,0 +1,246 @@
1
+
2
+ /**
3
+ * Copyright (c) 2019 The xterm.js authors. All rights reserved.
4
+ * @license MIT
5
+ */
6
+
7
+ import { EventEmitter } from 'common/EventEmitter';
8
+ import { Disposable } from 'common/Lifecycle';
9
+
10
+ declare const setTimeout: (handler: () => void, timeout?: number) => void;
11
+
12
+ /**
13
+ * Safety watermark to avoid memory exhaustion and browser engine crash on fast data input.
14
+ * Enable flow control to avoid this limit and make sure that your backend correctly
15
+ * propagates this to the underlying pty. (see docs for further instructions)
16
+ * Since this limit is meant as a safety parachute to prevent browser crashs,
17
+ * it is set to a very high number. Typically xterm.js gets unresponsive with
18
+ * a 100 times lower number (>500 kB).
19
+ */
20
+ const DISCARD_WATERMARK = 50000000; // ~50 MB
21
+
22
+ /**
23
+ * The max number of ms to spend on writes before allowing the renderer to
24
+ * catch up with a 0ms setTimeout. A value of < 33 to keep us close to
25
+ * 30fps, and a value of < 16 to try to run at 60fps. Of course, the real FPS
26
+ * depends on the time it takes for the renderer to draw the frame.
27
+ */
28
+ const WRITE_TIMEOUT_MS = 12;
29
+
30
+ /**
31
+ * Threshold of max held chunks in the write buffer, that were already processed.
32
+ * This is a tradeoff between extensive write buffer shifts (bad runtime) and high
33
+ * memory consumption by data thats not used anymore.
34
+ */
35
+ const WRITE_BUFFER_LENGTH_THRESHOLD = 50;
36
+
37
+ export class WriteBuffer extends Disposable {
38
+ private _writeBuffer: (string | Uint8Array)[] = [];
39
+ private _callbacks: ((() => void) | undefined)[] = [];
40
+ private _pendingData = 0;
41
+ private _bufferOffset = 0;
42
+ private _isSyncWriting = false;
43
+ private _syncCalls = 0;
44
+ private _didUserInput = false;
45
+
46
+ private readonly _onWriteParsed = this.register(new EventEmitter<void>());
47
+ public readonly onWriteParsed = this._onWriteParsed.event;
48
+
49
+ constructor(private _action: (data: string | Uint8Array, promiseResult?: boolean) => void | Promise<boolean>) {
50
+ super();
51
+ }
52
+
53
+ public handleUserInput(): void {
54
+ this._didUserInput = true;
55
+ }
56
+
57
+ /**
58
+ * @deprecated Unreliable, to be removed soon.
59
+ */
60
+ public writeSync(data: string | Uint8Array, maxSubsequentCalls?: number): void {
61
+ // stop writeSync recursions with maxSubsequentCalls argument
62
+ // This is dangerous to use as it will lose the current data chunk
63
+ // and return immediately.
64
+ if (maxSubsequentCalls !== undefined && this._syncCalls > maxSubsequentCalls) {
65
+ // comment next line if a whole loop block should only contain x `writeSync` calls
66
+ // (total flat vs. deep nested limit)
67
+ this._syncCalls = 0;
68
+ return;
69
+ }
70
+ // append chunk to buffer
71
+ this._pendingData += data.length;
72
+ this._writeBuffer.push(data);
73
+ this._callbacks.push(undefined);
74
+
75
+ // increase recursion counter
76
+ this._syncCalls++;
77
+ // exit early if another writeSync loop is active
78
+ if (this._isSyncWriting) {
79
+ return;
80
+ }
81
+ this._isSyncWriting = true;
82
+
83
+ // force sync processing on pending data chunks to avoid in-band data scrambling
84
+ // does the same as innerWrite but without event loop
85
+ // we have to do it here as single loop steps to not corrupt loop subject
86
+ // by another writeSync call triggered from _action
87
+ let chunk: string | Uint8Array | undefined;
88
+ while (chunk = this._writeBuffer.shift()) {
89
+ this._action(chunk);
90
+ const cb = this._callbacks.shift();
91
+ if (cb) cb();
92
+ }
93
+ // reset to avoid reprocessing of chunks with scheduled innerWrite call
94
+ // stopping scheduled innerWrite by offset > length condition
95
+ this._pendingData = 0;
96
+ this._bufferOffset = 0x7FFFFFFF;
97
+
98
+ // allow another writeSync to loop
99
+ this._isSyncWriting = false;
100
+ this._syncCalls = 0;
101
+ }
102
+
103
+ public write(data: string | Uint8Array, callback?: () => void): void {
104
+ if (this._pendingData > DISCARD_WATERMARK) {
105
+ throw new Error('write data discarded, use flow control to avoid losing data');
106
+ }
107
+
108
+ // schedule chunk processing for next event loop run
109
+ if (!this._writeBuffer.length) {
110
+ this._bufferOffset = 0;
111
+
112
+ // If this is the first write call after the user has done some input,
113
+ // parse it immediately to minimize input latency,
114
+ // otherwise schedule for the next event
115
+ if (this._didUserInput) {
116
+ this._didUserInput = false;
117
+ this._pendingData += data.length;
118
+ this._writeBuffer.push(data);
119
+ this._callbacks.push(callback);
120
+ this._innerWrite();
121
+ return;
122
+ }
123
+
124
+ setTimeout(() => this._innerWrite());
125
+ }
126
+
127
+ this._pendingData += data.length;
128
+ this._writeBuffer.push(data);
129
+ this._callbacks.push(callback);
130
+ }
131
+
132
+ /**
133
+ * Inner write call, that enters the sliced chunk processing by timing.
134
+ *
135
+ * `lastTime` indicates, when the last _innerWrite call had started.
136
+ * It is used to aggregate async handler execution under a timeout constraint
137
+ * effectively lowering the redrawing needs, schematically:
138
+ *
139
+ * macroTask _innerWrite:
140
+ * if (Date.now() - (lastTime | 0) < WRITE_TIMEOUT_MS):
141
+ * schedule microTask _innerWrite(lastTime)
142
+ * else:
143
+ * schedule macroTask _innerWrite(0)
144
+ *
145
+ * overall execution order on task queues:
146
+ *
147
+ * macrotasks: [...] --> _innerWrite(0) --> [...] --> screenUpdate --> [...]
148
+ * m t: |
149
+ * i a: [...]
150
+ * c s: |
151
+ * r k: while < timeout:
152
+ * o s: _innerWrite(timeout)
153
+ *
154
+ * `promiseResult` depicts the promise resolve value of an async handler.
155
+ * This value gets carried forward through all saved stack states of the
156
+ * paused parser for proper continuation.
157
+ *
158
+ * Note, for pure sync code `lastTime` and `promiseResult` have no meaning.
159
+ */
160
+ protected _innerWrite(lastTime: number = 0, promiseResult: boolean = true): void {
161
+ const startTime = lastTime || Date.now();
162
+ while (this._writeBuffer.length > this._bufferOffset) {
163
+ const data = this._writeBuffer[this._bufferOffset];
164
+ const result = this._action(data, promiseResult);
165
+ if (result) {
166
+ /**
167
+ * If we get a promise as return value, we re-schedule the continuation
168
+ * as thenable on the promise and exit right away.
169
+ *
170
+ * The exit here means, that we block input processing at the current active chunk,
171
+ * the exact execution position within the chunk is preserved by the saved
172
+ * stack content in InputHandler and EscapeSequenceParser.
173
+ *
174
+ * Resuming happens automatically from that saved stack state.
175
+ * Also the resolved promise value is passed along the callstack to
176
+ * `EscapeSequenceParser.parse` to correctly resume the stopped handler loop.
177
+ *
178
+ * Exceptions on async handlers will be logged to console async, but do not interrupt
179
+ * the input processing (continues with next handler at the current input position).
180
+ */
181
+
182
+ /**
183
+ * If a promise takes long to resolve, we should schedule continuation behind setTimeout.
184
+ * This might already be too late, if our .then enters really late (executor + prev thens
185
+ * took very long). This cannot be solved here for the handler itself (it is the handlers
186
+ * responsibility to slice hard work), but we can at least schedule a screen update as we
187
+ * gain control.
188
+ */
189
+ const continuation: (r: boolean) => void = (r: boolean) => Date.now() - startTime >= WRITE_TIMEOUT_MS
190
+ ? setTimeout(() => this._innerWrite(0, r))
191
+ : this._innerWrite(startTime, r);
192
+
193
+ /**
194
+ * Optimization considerations:
195
+ * The continuation above favors FPS over throughput by eval'ing `startTime` on resolve.
196
+ * This might schedule too many screen updates with bad throughput drops (in case a slow
197
+ * resolving handler sliced its work properly behind setTimeout calls). We cannot spot
198
+ * this condition here, also the renderer has no way to spot nonsense updates either.
199
+ * FIXME: A proper fix for this would track the FPS at the renderer entry level separately.
200
+ *
201
+ * If favoring of FPS shows bad throughtput impact, use the following instead. It favors
202
+ * throughput by eval'ing `startTime` upfront pulling at least one more chunk into the
203
+ * current microtask queue (executed before setTimeout).
204
+ */
205
+ // const continuation: (r: boolean) => void = Date.now() - startTime >= WRITE_TIMEOUT_MS
206
+ // ? r => setTimeout(() => this._innerWrite(0, r))
207
+ // : r => this._innerWrite(startTime, r);
208
+
209
+ // Handle exceptions synchronously to current band position, idea:
210
+ // 1. spawn a single microtask which we allow to throw hard
211
+ // 2. spawn a promise immediately resolving to `true`
212
+ // (executed on the same queue, thus properly aligned before continuation happens)
213
+ result.catch(err => {
214
+ queueMicrotask(() => {throw err;});
215
+ return Promise.resolve(false);
216
+ }).then(continuation);
217
+ return;
218
+ }
219
+
220
+ const cb = this._callbacks[this._bufferOffset];
221
+ if (cb) cb();
222
+ this._bufferOffset++;
223
+ this._pendingData -= data.length;
224
+
225
+ if (Date.now() - startTime >= WRITE_TIMEOUT_MS) {
226
+ break;
227
+ }
228
+ }
229
+ if (this._writeBuffer.length > this._bufferOffset) {
230
+ // Allow renderer to catch up before processing the next batch
231
+ // trim already processed chunks if we are above threshold
232
+ if (this._bufferOffset > WRITE_BUFFER_LENGTH_THRESHOLD) {
233
+ this._writeBuffer = this._writeBuffer.slice(this._bufferOffset);
234
+ this._callbacks = this._callbacks.slice(this._bufferOffset);
235
+ this._bufferOffset = 0;
236
+ }
237
+ setTimeout(() => this._innerWrite());
238
+ } else {
239
+ this._writeBuffer.length = 0;
240
+ this._callbacks.length = 0;
241
+ this._pendingData = 0;
242
+ this._bufferOffset = 0;
243
+ }
244
+ this._onWriteParsed.fire();
245
+ }
246
+ }
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Copyright (c) 2021 The xterm.js authors. All rights reserved.
3
+ * @license MIT
4
+ */
5
+
6
+
7
+ // 'rgb:' rule - matching: r/g/b | rr/gg/bb | rrr/ggg/bbb | rrrr/gggg/bbbb (hex digits)
8
+ const RGB_REX = /^([\da-f])\/([\da-f])\/([\da-f])$|^([\da-f]{2})\/([\da-f]{2})\/([\da-f]{2})$|^([\da-f]{3})\/([\da-f]{3})\/([\da-f]{3})$|^([\da-f]{4})\/([\da-f]{4})\/([\da-f]{4})$/;
9
+ // '#...' rule - matching any hex digits
10
+ const HASH_REX = /^[\da-f]+$/;
11
+
12
+ /**
13
+ * Parse color spec to RGB values (8 bit per channel).
14
+ * See `man xparsecolor` for details about certain format specifications.
15
+ *
16
+ * Supported formats:
17
+ * - rgb:<red>/<green>/<blue> with <red>, <green>, <blue> in h | hh | hhh | hhhh
18
+ * - #RGB, #RRGGBB, #RRRGGGBBB, #RRRRGGGGBBBB
19
+ *
20
+ * All other formats like rgbi: or device-independent string specifications
21
+ * with float numbering are not supported.
22
+ */
23
+ export function parseColor(data: string): [number, number, number] | undefined {
24
+ if (!data) return;
25
+ // also handle uppercases
26
+ let low = data.toLowerCase();
27
+ if (low.indexOf('rgb:') === 0) {
28
+ // 'rgb:' specifier
29
+ low = low.slice(4);
30
+ const m = RGB_REX.exec(low);
31
+ if (m) {
32
+ const base = m[1] ? 15 : m[4] ? 255 : m[7] ? 4095 : 65535;
33
+ return [
34
+ Math.round(parseInt(m[1] || m[4] || m[7] || m[10], 16) / base * 255),
35
+ Math.round(parseInt(m[2] || m[5] || m[8] || m[11], 16) / base * 255),
36
+ Math.round(parseInt(m[3] || m[6] || m[9] || m[12], 16) / base * 255)
37
+ ];
38
+ }
39
+ } else if (low.indexOf('#') === 0) {
40
+ // '#' specifier
41
+ low = low.slice(1);
42
+ if (HASH_REX.exec(low) && [3, 6, 9, 12].includes(low.length)) {
43
+ const adv = low.length / 3;
44
+ const result: [number, number, number] = [0, 0, 0];
45
+ for (let i = 0; i < 3; ++i) {
46
+ const c = parseInt(low.slice(adv * i, adv * i + adv), 16);
47
+ result[i] = adv === 1 ? c << 4 : adv === 2 ? c : adv === 3 ? c >> 4 : c >> 8;
48
+ }
49
+ return result;
50
+ }
51
+ }
52
+
53
+ // Named colors are currently not supported due to the large addition to the xterm.js bundle size
54
+ // they would add. In order to support named colors, we would need some way of optionally loading
55
+ // additional payloads so startup/download time is not bloated (see #3530).
56
+ }
57
+
58
+ // pad hex output to requested bit width
59
+ function pad(n: number, bits: number): string {
60
+ const s = n.toString(16);
61
+ const s2 = s.length < 2 ? '0' + s : s;
62
+ switch (bits) {
63
+ case 4:
64
+ return s[0];
65
+ case 8:
66
+ return s2;
67
+ case 12:
68
+ return (s2 + s2).slice(0, 3);
69
+ default:
70
+ return s2 + s2;
71
+ }
72
+ }
73
+
74
+ /**
75
+ * Convert a given color to rgb:../../.. string of `bits` depth.
76
+ */
77
+ export function toRgbString(color: [number, number, number], bits: number = 16): string {
78
+ const [r, g, b] = color;
79
+ return `rgb:${pad(r, bits)}/${pad(g, bits)}/${pad(b, bits)}`;
80
+ }
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Copyright (c) 2017 The xterm.js authors. All rights reserved.
3
+ * @license MIT
4
+ */
5
+
6
+ /**
7
+ * Internal states of EscapeSequenceParser.
8
+ */
9
+ export const enum ParserState {
10
+ GROUND = 0,
11
+ ESCAPE = 1,
12
+ ESCAPE_INTERMEDIATE = 2,
13
+ CSI_ENTRY = 3,
14
+ CSI_PARAM = 4,
15
+ CSI_INTERMEDIATE = 5,
16
+ CSI_IGNORE = 6,
17
+ SOS_PM_APC_STRING = 7,
18
+ OSC_STRING = 8,
19
+ DCS_ENTRY = 9,
20
+ DCS_PARAM = 10,
21
+ DCS_IGNORE = 11,
22
+ DCS_INTERMEDIATE = 12,
23
+ DCS_PASSTHROUGH = 13
24
+ }
25
+
26
+ /**
27
+ * Internal actions of EscapeSequenceParser.
28
+ */
29
+ export const enum ParserAction {
30
+ IGNORE = 0,
31
+ ERROR = 1,
32
+ PRINT = 2,
33
+ EXECUTE = 3,
34
+ OSC_START = 4,
35
+ OSC_PUT = 5,
36
+ OSC_END = 6,
37
+ CSI_DISPATCH = 7,
38
+ PARAM = 8,
39
+ COLLECT = 9,
40
+ ESC_DISPATCH = 10,
41
+ CLEAR = 11,
42
+ DCS_HOOK = 12,
43
+ DCS_PUT = 13,
44
+ DCS_UNHOOK = 14
45
+ }
46
+
47
+ /**
48
+ * Internal states of OscParser.
49
+ */
50
+ export const enum OscState {
51
+ START = 0,
52
+ ID = 1,
53
+ PAYLOAD = 2,
54
+ ABORT = 3
55
+ }
56
+
57
+ // payload limit for OSC and DCS
58
+ export const PAYLOAD_LIMIT = 10000000;
@@ -0,0 +1,192 @@
1
+ /**
2
+ * Copyright (c) 2019 The xterm.js authors. All rights reserved.
3
+ * @license MIT
4
+ */
5
+
6
+ import { IDisposable } from 'common/Types';
7
+ import { IDcsHandler, IParams, IHandlerCollection, IDcsParser, DcsFallbackHandlerType, ISubParserStackState } from 'common/parser/Types';
8
+ import { utf32ToString } from 'common/input/TextDecoder';
9
+ import { Params } from 'common/parser/Params';
10
+ import { PAYLOAD_LIMIT } from 'common/parser/Constants';
11
+
12
+ const EMPTY_HANDLERS: IDcsHandler[] = [];
13
+
14
+ export class DcsParser implements IDcsParser {
15
+ private _handlers: IHandlerCollection<IDcsHandler> = Object.create(null);
16
+ private _active: IDcsHandler[] = EMPTY_HANDLERS;
17
+ private _ident: number = 0;
18
+ private _handlerFb: DcsFallbackHandlerType = () => { };
19
+ private _stack: ISubParserStackState = {
20
+ paused: false,
21
+ loopPosition: 0,
22
+ fallThrough: false
23
+ };
24
+
25
+ public dispose(): void {
26
+ this._handlers = Object.create(null);
27
+ this._handlerFb = () => { };
28
+ this._active = EMPTY_HANDLERS;
29
+ }
30
+
31
+ public registerHandler(ident: number, handler: IDcsHandler): IDisposable {
32
+ if (this._handlers[ident] === undefined) {
33
+ this._handlers[ident] = [];
34
+ }
35
+ const handlerList = this._handlers[ident];
36
+ handlerList.push(handler);
37
+ return {
38
+ dispose: () => {
39
+ const handlerIndex = handlerList.indexOf(handler);
40
+ if (handlerIndex !== -1) {
41
+ handlerList.splice(handlerIndex, 1);
42
+ }
43
+ }
44
+ };
45
+ }
46
+
47
+ public clearHandler(ident: number): void {
48
+ if (this._handlers[ident]) delete this._handlers[ident];
49
+ }
50
+
51
+ public setHandlerFallback(handler: DcsFallbackHandlerType): void {
52
+ this._handlerFb = handler;
53
+ }
54
+
55
+ public reset(): void {
56
+ // force cleanup leftover handlers
57
+ if (this._active.length) {
58
+ for (let j = this._stack.paused ? this._stack.loopPosition - 1 : this._active.length - 1; j >= 0; --j) {
59
+ this._active[j].unhook(false);
60
+ }
61
+ }
62
+ this._stack.paused = false;
63
+ this._active = EMPTY_HANDLERS;
64
+ this._ident = 0;
65
+ }
66
+
67
+ public hook(ident: number, params: IParams): void {
68
+ // always reset leftover handlers
69
+ this.reset();
70
+ this._ident = ident;
71
+ this._active = this._handlers[ident] || EMPTY_HANDLERS;
72
+ if (!this._active.length) {
73
+ this._handlerFb(this._ident, 'HOOK', params);
74
+ } else {
75
+ for (let j = this._active.length - 1; j >= 0; j--) {
76
+ this._active[j].hook(params);
77
+ }
78
+ }
79
+ }
80
+
81
+ public put(data: Uint32Array, start: number, end: number): void {
82
+ if (!this._active.length) {
83
+ this._handlerFb(this._ident, 'PUT', utf32ToString(data, start, end));
84
+ } else {
85
+ for (let j = this._active.length - 1; j >= 0; j--) {
86
+ this._active[j].put(data, start, end);
87
+ }
88
+ }
89
+ }
90
+
91
+ public unhook(success: boolean, promiseResult: boolean = true): void | Promise<boolean> {
92
+ if (!this._active.length) {
93
+ this._handlerFb(this._ident, 'UNHOOK', success);
94
+ } else {
95
+ let handlerResult: boolean | Promise<boolean> = false;
96
+ let j = this._active.length - 1;
97
+ let fallThrough = false;
98
+ if (this._stack.paused) {
99
+ j = this._stack.loopPosition - 1;
100
+ handlerResult = promiseResult;
101
+ fallThrough = this._stack.fallThrough;
102
+ this._stack.paused = false;
103
+ }
104
+ if (!fallThrough && handlerResult === false) {
105
+ for (; j >= 0; j--) {
106
+ handlerResult = this._active[j].unhook(success);
107
+ if (handlerResult === true) {
108
+ break;
109
+ } else if (handlerResult instanceof Promise) {
110
+ this._stack.paused = true;
111
+ this._stack.loopPosition = j;
112
+ this._stack.fallThrough = false;
113
+ return handlerResult;
114
+ }
115
+ }
116
+ j--;
117
+ }
118
+ // cleanup left over handlers (fallThrough for async)
119
+ for (; j >= 0; j--) {
120
+ handlerResult = this._active[j].unhook(false);
121
+ if (handlerResult instanceof Promise) {
122
+ this._stack.paused = true;
123
+ this._stack.loopPosition = j;
124
+ this._stack.fallThrough = true;
125
+ return handlerResult;
126
+ }
127
+ }
128
+ }
129
+ this._active = EMPTY_HANDLERS;
130
+ this._ident = 0;
131
+ }
132
+ }
133
+
134
+ // predefine empty params as [0] (ZDM)
135
+ const EMPTY_PARAMS = new Params();
136
+ EMPTY_PARAMS.addParam(0);
137
+
138
+ /**
139
+ * Convenient class to create a DCS handler from a single callback function.
140
+ * Note: The payload is currently limited to 50 MB (hardcoded).
141
+ */
142
+ export class DcsHandler implements IDcsHandler {
143
+ private _data = '';
144
+ private _params: IParams = EMPTY_PARAMS;
145
+ private _hitLimit: boolean = false;
146
+
147
+ constructor(private _handler: (data: string, params: IParams) => boolean | Promise<boolean>) { }
148
+
149
+ public hook(params: IParams): void {
150
+ // since we need to preserve params until `unhook`, we have to clone it
151
+ // (only borrowed from parser and spans multiple parser states)
152
+ // perf optimization:
153
+ // clone only, if we have non empty params, otherwise stick with default
154
+ this._params = (params.length > 1 || params.params[0]) ? params.clone() : EMPTY_PARAMS;
155
+ this._data = '';
156
+ this._hitLimit = false;
157
+ }
158
+
159
+ public put(data: Uint32Array, start: number, end: number): void {
160
+ if (this._hitLimit) {
161
+ return;
162
+ }
163
+ this._data += utf32ToString(data, start, end);
164
+ if (this._data.length > PAYLOAD_LIMIT) {
165
+ this._data = '';
166
+ this._hitLimit = true;
167
+ }
168
+ }
169
+
170
+ public unhook(success: boolean): boolean | Promise<boolean> {
171
+ let ret: boolean | Promise<boolean> = false;
172
+ if (this._hitLimit) {
173
+ ret = false;
174
+ } else if (success) {
175
+ ret = this._handler(this._data, this._params);
176
+ if (ret instanceof Promise) {
177
+ // need to hold data and params until `ret` got resolved
178
+ // dont care for errors, data will be freed anyway on next start
179
+ return ret.then(res => {
180
+ this._params = EMPTY_PARAMS;
181
+ this._data = '';
182
+ this._hitLimit = false;
183
+ return res;
184
+ });
185
+ }
186
+ }
187
+ this._params = EMPTY_PARAMS;
188
+ this._data = '';
189
+ this._hitLimit = false;
190
+ return ret;
191
+ }
192
+ }