Debian Codegraph @

node/@xterm/xterm/src/common/parser/DcsParser.ts

raw
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 }