webpack-dev-service 0.5.1 → 0.5.3

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 (41) hide show
  1. package/README.md +5 -2
  2. package/client/cjs/client.cjs +111 -109
  3. package/client/cjs/events.cjs +33 -32
  4. package/client/cjs/hot.cjs +61 -60
  5. package/client/cjs/index.cjs +10 -8
  6. package/client/cjs/main.cjs +63 -60
  7. package/client/cjs/ui/overlay.cjs +64 -63
  8. package/client/cjs/ui/progress.cjs +35 -35
  9. package/client/cjs/ui/utils/ansi/index.cjs +439 -413
  10. package/client/cjs/ui/utils/ansi/regx.cjs +8 -8
  11. package/client/cjs/ui/utils/ansi/utils.cjs +14 -14
  12. package/client/cjs/ui/utils/index.cjs +99 -99
  13. package/client/esm/client.js +111 -109
  14. package/client/esm/events.js +33 -32
  15. package/client/esm/hot.js +61 -60
  16. package/client/esm/index.js +8 -8
  17. package/client/esm/main.js +63 -60
  18. package/client/esm/ui/overlay.js +64 -63
  19. package/client/esm/ui/progress.js +35 -35
  20. package/client/esm/ui/utils/ansi/index.js +439 -413
  21. package/client/esm/ui/utils/ansi/regx.js +8 -8
  22. package/client/esm/ui/utils/ansi/utils.js +14 -14
  23. package/client/esm/ui/utils/index.js +99 -99
  24. package/package.json +15 -19
  25. package/server/cjs/dev.cjs +30 -36
  26. package/server/cjs/hot.cjs +174 -177
  27. package/server/cjs/index.cjs +16 -17
  28. package/server/esm/dev.js +28 -32
  29. package/server/esm/hot.js +171 -172
  30. package/server/esm/index.js +14 -13
  31. package/types/client/client.d.ts +7 -7
  32. package/types/client/events.d.ts +10 -10
  33. package/types/client/message.d.ts +25 -25
  34. package/types/client/ui/overlay.d.ts +12 -12
  35. package/types/client/ui/progress.d.ts +8 -8
  36. package/types/client/ui/utils/ansi/enum.d.ts +7 -7
  37. package/types/client/ui/utils/ansi/index.d.ts +11 -11
  38. package/types/client/ui/utils/ansi/interface.d.ts +46 -42
  39. package/types/server/dev.d.ts +4 -4
  40. package/types/server/hot.d.ts +5 -5
  41. package/types/server/index.d.ts +2 -2
@@ -1,12 +1,12 @@
1
1
  /**
2
- * @package webpack-dev-service
3
- * @license MIT
4
- * @version 0.5.1
5
- * @author nuintun <nuintun@qq.com>
6
- * @description A koa 2 middleware for webpack development and hot reloading.
7
- * @see https://github.com/nuintun/webpack-dev-service#readme
8
- */
9
-
2
+ * @package webpack-dev-service
3
+ * @license MIT
4
+ * @version 0.5.3
5
+ * @author nuintun <nuintun@qq.com>
6
+ * @description A koa 2 middleware for webpack development and hot reloading.
7
+ * @see https://github.com/nuintun/webpack-dev-service#readme
8
+ */
9
+
10
10
  'use strict';
11
11
 
12
12
  const utils = require('./utils.cjs');
@@ -16,428 +16,454 @@ const regx = require('./regx.cjs');
16
16
  * @module index
17
17
  */
18
18
  class Ansi {
19
- buffer = '';
20
- style = {
21
- dim: false,
22
- bold: false,
23
- color: null,
24
- blink: false,
25
- hidden: false,
26
- italic: false,
27
- inverse: false,
28
- overline: false,
29
- background: null,
30
- underline: false,
31
- strikethrough: false
32
- };
33
- colors256;
34
- colors16;
35
- constructor(theme = {}) {
36
- const colors16 = [
37
- // Colors 16 bit
38
- [
39
- // Black
40
- utils.getThemeColor([0, 0, 0], theme.black),
41
- // Red
42
- utils.getThemeColor([187, 0, 0], theme.red),
43
- // Green
44
- utils.getThemeColor([0, 187, 0], theme.green),
45
- // Yellow
46
- utils.getThemeColor([187, 187, 0], theme.yellow),
47
- // Blue
48
- utils.getThemeColor([0, 0, 187], theme.blue),
49
- // Magenta
50
- utils.getThemeColor([187, 0, 187], theme.magenta),
51
- // Cyan
52
- utils.getThemeColor([0, 187, 187], theme.cyan),
53
- // White
54
- utils.getThemeColor([255, 255, 255], theme.white)
55
- ],
56
- // Bright colors
57
- [
58
- // Bright Black
59
- utils.getThemeColor([85, 85, 85], theme.brightBlack),
60
- // Bright Red
61
- utils.getThemeColor([255, 85, 85], theme.brightRed),
62
- // Bright Green
63
- utils.getThemeColor([0, 255, 0], theme.brightGreen),
64
- // Bright Yellow
65
- utils.getThemeColor([255, 255, 85], theme.brightYellow),
66
- // Bright Blue
67
- utils.getThemeColor([85, 85, 255], theme.brightBlue),
68
- // Bright Magenta
69
- utils.getThemeColor([255, 85, 255], theme.brightMagenta),
70
- // Bright Cyan
71
- utils.getThemeColor([85, 255, 255], theme.brightCyan),
72
- // Bright White
73
- utils.getThemeColor([255, 255, 255], theme.brightWhite)
74
- ]
75
- ];
76
- // Colors 256 bit
77
- const colors256 = [];
78
- // Index 0..15 : Ansi-Colors
79
- for (const palette of colors16) {
80
- for (const color of palette) {
81
- colors256.push(color);
82
- }
83
- }
84
- // Index 16..231 : RGB 6x6x6
85
- // https://gist.github.com/jasonm23/2868981#file-xterm-256color-yaml
86
- const levels = [0, 95, 135, 175, 215, 255];
87
- for (let r = 0; r < 6; ++r) {
88
- for (let g = 0; g < 6; ++g) {
89
- for (let b = 0; b < 6; ++b) {
90
- colors256.push([levels[r], levels[g], levels[b]]);
19
+ buffer = '';
20
+ style = {
21
+ dim: false,
22
+ bold: false,
23
+ color: null,
24
+ blink: false,
25
+ hidden: false,
26
+ italic: false,
27
+ inverse: false,
28
+ overline: false,
29
+ background: null,
30
+ underline: false,
31
+ strikethrough: false
32
+ };
33
+ colors256;
34
+ colors16;
35
+ constructor(theme = {}) {
36
+ const colors16 = [
37
+ // Colors 16 bit
38
+ [
39
+ // Black
40
+ utils.getThemeColor([0, 0, 0], theme.black),
41
+ // Red
42
+ utils.getThemeColor([187, 0, 0], theme.red),
43
+ // Green
44
+ utils.getThemeColor([0, 187, 0], theme.green),
45
+ // Yellow
46
+ utils.getThemeColor([187, 187, 0], theme.yellow),
47
+ // Blue
48
+ utils.getThemeColor([0, 0, 187], theme.blue),
49
+ // Magenta
50
+ utils.getThemeColor([187, 0, 187], theme.magenta),
51
+ // Cyan
52
+ utils.getThemeColor([0, 187, 187], theme.cyan),
53
+ // White
54
+ utils.getThemeColor([255, 255, 255], theme.white)
55
+ ],
56
+ // Bright colors
57
+ [
58
+ // Bright Black
59
+ utils.getThemeColor([85, 85, 85], theme.brightBlack),
60
+ // Bright Red
61
+ utils.getThemeColor([255, 85, 85], theme.brightRed),
62
+ // Bright Green
63
+ utils.getThemeColor([0, 255, 0], theme.brightGreen),
64
+ // Bright Yellow
65
+ utils.getThemeColor([255, 255, 85], theme.brightYellow),
66
+ // Bright Blue
67
+ utils.getThemeColor([85, 85, 255], theme.brightBlue),
68
+ // Bright Magenta
69
+ utils.getThemeColor([255, 85, 255], theme.brightMagenta),
70
+ // Bright Cyan
71
+ utils.getThemeColor([85, 255, 255], theme.brightCyan),
72
+ // Bright White
73
+ utils.getThemeColor([255, 255, 255], theme.brightWhite)
74
+ ]
75
+ ];
76
+ // Colors 256 bit
77
+ const colors256 = [];
78
+ // Index 0..15 : Ansi-Colors
79
+ for (const palette of colors16) {
80
+ for (const color of palette) {
81
+ colors256.push(color);
82
+ }
91
83
  }
92
- }
93
- }
94
- // Index 232..255 : Grayscale
95
- let grayscale = 8;
96
- for (let i = 0; i < 24; ++i, grayscale += 10) {
97
- colors256.push([grayscale, grayscale, grayscale]);
98
- }
99
- // Init ANSI colors
100
- this.colors16 = colors16;
101
- this.colors256 = colors256;
102
- }
103
- read() {
104
- const { buffer } = this;
105
- const { length } = buffer;
106
- const pos = buffer.indexOf('\x1B');
107
- // The most common case, no ESC codes
108
- if (pos < 0) {
109
- this.buffer = '';
110
- return {
111
- value: buffer,
112
- type: 4 /* TokenType.TEXT */
113
- };
114
- }
115
- if (pos > 0) {
116
- this.buffer = buffer.slice(pos);
117
- return {
118
- type: 4 /* TokenType.TEXT */,
119
- value: buffer.slice(0, pos)
120
- };
121
- }
122
- if (length === 1) {
123
- // Lone ESC in Buffer, We don't know yet
124
- return {
125
- type: 5 /* TokenType.INCESC */
126
- };
127
- }
128
- const peek = buffer.charAt(1);
129
- // We treat this as a single ESC
130
- // Which effecitvely shows
131
- if (peek !== '[' && peek !== ']') {
132
- this.buffer = buffer.slice(1);
133
- // DeMorgan
134
- return {
135
- type: 1 /* TokenType.ESC */
136
- };
137
- }
138
- // OK is this an SGR or OSC that we handle
139
- // SGR CHECK
140
- if (peek === '[') {
141
- // We do this regex initialization here so
142
- // we can keep the regex close to its use (Readability)
143
- // All ansi codes are typically in the following format.
144
- // We parse it and focus specifically on the
145
- // graphics commands (SGR)
146
- //
147
- // CONTROL-SEQUENCE-INTRODUCER CSI (ESC, '[')
148
- // PRIVATE-MODE-CHAR (!, <, >, ?)
149
- // Numeric parameters separated by semicolons ('0' - '9', ';')
150
- // Intermediate-modifiers (0x20 - 0x2f)
151
- // COMMAND-CHAR (0x40 - 0x7e)
152
- const match = buffer.match(regx.CSI_RE);
153
- // This match is guaranteed to terminate (even on
154
- // invalid input). The key is to match on legal and
155
- // illegal sequences.
156
- // The first alternate matches everything legal and
157
- // the second matches everything illegal.
158
- //
159
- // If it doesn't match, then we have not received
160
- // either the full sequence or an illegal sequence.
161
- // If it does match, the presence of field 4 tells
162
- // us whether it was legal or illegal.
163
- if (match === null) {
164
- return {
165
- type: 5 /* TokenType.INCESC */
166
- };
167
- }
168
- // match is an array
169
- // 0 - total match
170
- // 1 - private mode chars group
171
- // 2 - digits and semicolons group
172
- // 3 - command
173
- // 4 - illegal char
174
- if (match[4]) {
175
- this.buffer = buffer.slice(1);
176
- // Illegal sequence, just remove the ESC
177
- return {
178
- type: 1 /* TokenType.ESC */
179
- };
180
- }
181
- this.buffer = buffer.slice(match[0].length);
182
- // If not a valid SGR, we don't handle
183
- if (match[1] !== '' || match[3] !== 'm') {
184
- return {
185
- type: 6 /* TokenType.UNKNOWN */
186
- };
187
- } else {
188
- return {
189
- signal: match[2],
190
- type: 3 /* TokenType.SGR */
191
- };
192
- }
84
+ // Index 16..231 : RGB 6x6x6
85
+ // https://gist.github.com/jasonm23/2868981#file-xterm-256color-yaml
86
+ const levels = [0, 95, 135, 175, 215, 255];
87
+ for (let r = 0; r < 6; ++r) {
88
+ for (let g = 0; g < 6; ++g) {
89
+ for (let b = 0; b < 6; ++b) {
90
+ colors256.push([levels[r], levels[g], levels[b]]);
91
+ }
92
+ }
93
+ }
94
+ // Index 232..255 : Grayscale
95
+ let grayscale = 8;
96
+ for (let i = 0; i < 24; ++i, grayscale += 10) {
97
+ colors256.push([grayscale, grayscale, grayscale]);
98
+ }
99
+ // Init ANSI colors
100
+ this.colors16 = colors16;
101
+ this.colors256 = colors256;
193
102
  }
194
- // OSC CHECK
195
- if (peek === ']') {
196
- if (length < 4) {
197
- return {
198
- type: 5 /* TokenType.INCESC */
199
- };
200
- }
201
- if (buffer.charAt(2) !== '8' || buffer.charAt(3) !== ';') {
202
- this.buffer = buffer.slice(1);
203
- // This is not a match, so we'll just treat it as ESC
204
- return {
205
- type: 1 /* TokenType.ESC */
206
- };
207
- }
208
- // We do this regex initialization here so
209
- // we can keep the regex close to its use (Readability)
210
- // Matching a Hyperlink OSC with a regex is difficult
211
- // because Javascript's regex engine doesn't support
212
- // 'partial match' support.
213
- //
214
- // Therefore, we require the system to match the
215
- // string-terminator(ST) before attempting a match.
216
- // Once we find it, we attempt the Hyperlink-Begin
217
- // match.
218
- // If that goes ok, we scan forward for the next
219
- // ST.
220
- // Finally, we try to match it all and return
221
- // the sequence.
222
- // Also, it is important to note that we consider
223
- // certain control characters as an invalidation of
224
- // the entire sequence.
225
- // We do regex initializations here so
226
- // we can keep the regex close to its use (Readability)
227
- // STRING-TERMINATOR
228
- // This is likely to terminate in most scenarios
229
- // because it will terminate on a newline
230
- // VERY IMPORTANT
231
- // We do a stateful regex match with exec.
232
- // If the regex is global, and it used with 'exec',
233
- // then it will search starting at the 'lastIndex'
234
- // If it matches, the regex can be used again to
235
- // find the next match.
236
- regx.OSC_ST_RE.lastIndex = 0;
237
- // We might have the prefix and URI
238
- // Lets start our search for the ST twice
239
- for (let count = 0; count < 2; count++) {
240
- const match = regx.OSC_ST_RE.exec(buffer);
241
- if (match === null) {
242
- return {
243
- type: 5 /* TokenType.INCESC */
244
- };
103
+ read() {
104
+ const { buffer } = this;
105
+ const { length } = buffer;
106
+ const pos = buffer.indexOf('\x1B');
107
+ // The most common case, no ESC codes
108
+ if (pos < 0) {
109
+ this.buffer = '';
110
+ return {
111
+ value: buffer,
112
+ type: 4 /* TokenType.TEXT */
113
+ };
114
+ }
115
+ if (pos > 0) {
116
+ this.buffer = buffer.slice(pos);
117
+ return {
118
+ type: 4 /* TokenType.TEXT */,
119
+ value: buffer.slice(0, pos)
120
+ };
245
121
  }
246
- // If an illegal character was found, bail on the match
247
- if (match[3]) {
248
- this.buffer = buffer.slice(1);
249
- // Illegal sequence, just remove the ESC
250
- return {
251
- type: 1 /* TokenType.ESC */
252
- };
122
+ if (length === 1) {
123
+ // Lone ESC in Buffer, We don't know yet
124
+ return {
125
+ type: 5 /* TokenType.INCESC */
126
+ };
127
+ }
128
+ const peek = buffer.charAt(1);
129
+ // We treat this as a single ESC
130
+ // Which effecitvely shows
131
+ if (peek !== '[' && peek !== ']') {
132
+ this.buffer = buffer.slice(1);
133
+ // DeMorgan
134
+ return {
135
+ type: 1 /* TokenType.ESC */
136
+ };
137
+ }
138
+ // OK is this an SGR or OSC that we handle
139
+ // SGR CHECK
140
+ if (peek === '[') {
141
+ // We do this regex initialization here so
142
+ // we can keep the regex close to its use (Readability)
143
+ // All ansi codes are typically in the following format.
144
+ // We parse it and focus specifically on the
145
+ // graphics commands (SGR)
146
+ //
147
+ // CONTROL-SEQUENCE-INTRODUCER CSI (ESC, '[')
148
+ // PRIVATE-MODE-CHAR (!, <, >, ?)
149
+ // Numeric parameters separated by semicolons ('0' - '9', ';')
150
+ // Intermediate-modifiers (0x20 - 0x2f)
151
+ // COMMAND-CHAR (0x40 - 0x7e)
152
+ const match = buffer.match(regx.CSI_RE);
153
+ // This match is guaranteed to terminate (even on
154
+ // invalid input). The key is to match on legal and
155
+ // illegal sequences.
156
+ // The first alternate matches everything legal and
157
+ // the second matches everything illegal.
158
+ //
159
+ // If it doesn't match, then we have not received
160
+ // either the full sequence or an illegal sequence.
161
+ // If it does match, the presence of field 4 tells
162
+ // us whether it was legal or illegal.
163
+ if (match === null) {
164
+ return {
165
+ type: 5 /* TokenType.INCESC */
166
+ };
167
+ }
168
+ // match is an array
169
+ // 0 - total match
170
+ // 1 - private mode chars group
171
+ // 2 - digits and semicolons group
172
+ // 3 - command
173
+ // 4 - illegal char
174
+ if (match[4]) {
175
+ this.buffer = buffer.slice(1);
176
+ // Illegal sequence, just remove the ESC
177
+ return {
178
+ type: 1 /* TokenType.ESC */
179
+ };
180
+ }
181
+ this.buffer = buffer.slice(match[0].length);
182
+ // If not a valid SGR, we don't handle
183
+ if (match[1] !== '' || match[3] !== 'm') {
184
+ return {
185
+ type: 6 /* TokenType.UNKNOWN */
186
+ };
187
+ }
188
+ else {
189
+ return {
190
+ signal: match[2],
191
+ type: 3 /* TokenType.SGR */
192
+ };
193
+ }
194
+ }
195
+ // OSC CHECK
196
+ if (peek === ']') {
197
+ if (length < 4) {
198
+ return {
199
+ type: 5 /* TokenType.INCESC */
200
+ };
201
+ }
202
+ if (buffer.charAt(2) !== '8' || buffer.charAt(3) !== ';') {
203
+ this.buffer = buffer.slice(1);
204
+ // This is not a match, so we'll just treat it as ESC
205
+ return {
206
+ type: 1 /* TokenType.ESC */
207
+ };
208
+ }
209
+ // We do this regex initialization here so
210
+ // we can keep the regex close to its use (Readability)
211
+ // Matching a Hyperlink OSC with a regex is difficult
212
+ // because Javascript's regex engine doesn't support
213
+ // 'partial match' support.
214
+ //
215
+ // Therefore, we require the system to match the
216
+ // string-terminator(ST) before attempting a match.
217
+ // Once we find it, we attempt the Hyperlink-Begin
218
+ // match.
219
+ // If that goes ok, we scan forward for the next
220
+ // ST.
221
+ // Finally, we try to match it all and return
222
+ // the sequence.
223
+ // Also, it is important to note that we consider
224
+ // certain control characters as an invalidation of
225
+ // the entire sequence.
226
+ // We do regex initializations here so
227
+ // we can keep the regex close to its use (Readability)
228
+ // STRING-TERMINATOR
229
+ // This is likely to terminate in most scenarios
230
+ // because it will terminate on a newline
231
+ // VERY IMPORTANT
232
+ // We do a stateful regex match with exec.
233
+ // If the regex is global, and it used with 'exec',
234
+ // then it will search starting at the 'lastIndex'
235
+ // If it matches, the regex can be used again to
236
+ // find the next match.
237
+ regx.OSC_ST_RE.lastIndex = 0;
238
+ // We might have the prefix and URI
239
+ // Lets start our search for the ST twice
240
+ for (let count = 0; count < 2; count++) {
241
+ const match = regx.OSC_ST_RE.exec(buffer);
242
+ if (match === null) {
243
+ return {
244
+ type: 5 /* TokenType.INCESC */
245
+ };
246
+ }
247
+ // If an illegal character was found, bail on the match
248
+ if (match[3]) {
249
+ this.buffer = buffer.slice(1);
250
+ // Illegal sequence, just remove the ESC
251
+ return {
252
+ type: 1 /* TokenType.ESC */
253
+ };
254
+ }
255
+ }
256
+ // OK, at this point we should have a FULL match!
257
+ // Lets try to match that now
258
+ const match = buffer.match(regx.OSC_RE);
259
+ if (match === null) {
260
+ this.buffer = buffer.slice(1);
261
+ // Illegal sequence, just remove the ESC
262
+ return {
263
+ type: 1 /* TokenType.ESC */
264
+ };
265
+ }
266
+ this.buffer = buffer.slice(match[0].length);
267
+ // If a valid SGR
268
+ // match is an array
269
+ // 0 - total match
270
+ // 1 - URL
271
+ // 2 - Text
272
+ return {
273
+ url: match[1],
274
+ value: match[2],
275
+ type: 2 /* TokenType.OSC */
276
+ };
253
277
  }
254
- }
255
- // OK, at this point we should have a FULL match!
256
- // Lets try to match that now
257
- const match = buffer.match(regx.OSC_RE);
258
- if (match === null) {
259
- this.buffer = buffer.slice(1);
260
- // Illegal sequence, just remove the ESC
261
278
  return {
262
- type: 1 /* TokenType.ESC */
279
+ type: 0 /* TokenType.EOS */
263
280
  };
264
- }
265
- this.buffer = buffer.slice(match[0].length);
266
- // If a valid SGR
267
- // match is an array
268
- // 0 - total match
269
- // 1 - URL
270
- // 2 - Text
271
- return {
272
- url: match[1],
273
- value: match[2],
274
- type: 2 /* TokenType.OSC */
275
- };
276
281
  }
277
- return {
278
- type: 0 /* TokenType.EOS */
279
- };
280
- }
281
- reset() {
282
- const { style } = this;
283
- style.dim = false;
284
- style.bold = false;
285
- style.color = null;
286
- style.blink = false;
287
- style.hidden = false;
288
- style.italic = false;
289
- style.inverse = false;
290
- style.background = null;
291
- style.underline = false;
292
- style.strikethrough = false;
293
- }
294
- process(signal) {
295
- let index = 0;
296
- // Ok - we have a valid "SGR" (Select Graphic Rendition)
297
- const sequences = signal.split(';');
298
- const maxIndex = sequences.length - 1;
299
- // ANSI style and colors
300
- const { style, colors16, colors256 } = this;
301
- // Read cmd by index
302
- const read = () => parseInt(sequences[index++], 10);
303
- // Each of these params affects the SGR state
304
- // Why do we shift through the array instead of a forEach??
305
- // ... because some commands consume the params that follow !
306
- for (; index <= maxIndex; index++) {
307
- const code = read();
308
- if (code === 1) {
309
- style.bold = true;
310
- } else if (code === 2) {
311
- style.dim = true;
312
- } else if (code === 3) {
313
- style.italic = true;
314
- } else if (code === 4) {
315
- style.underline = true;
316
- } else if (code === 5) {
317
- style.blink = true;
318
- } else if (code === 7) {
319
- style.inverse = true;
320
- } else if (code === 8) {
321
- style.hidden = true;
322
- } else if (code === 9) {
323
- style.strikethrough = true;
324
- } else if (code === 53) {
325
- style.overline = true;
326
- } else if (code === 21) {
327
- style.bold = false;
328
- } else if (code === 22) {
282
+ reset() {
283
+ const { style } = this;
329
284
  style.dim = false;
330
285
  style.bold = false;
331
- } else if (code === 23) {
332
- style.italic = false;
333
- } else if (code === 24) {
334
- style.underline = false;
335
- } else if (code === 25) {
286
+ style.color = null;
336
287
  style.blink = false;
337
- } else if (code === 27) {
338
- style.inverse = false;
339
- } else if (code === 28) {
340
288
  style.hidden = false;
341
- } else if (code === 29) {
342
- style.strikethrough = false;
343
- } else if (code === 29) {
344
- style.strikethrough = false;
345
- } else if (code === 55) {
346
- style.overline = false;
347
- } else if (code === 49) {
289
+ style.italic = false;
290
+ style.inverse = false;
348
291
  style.background = null;
349
- } else if (code >= 30 && code < 38) {
350
- style.color = colors16[0][code - 30];
351
- } else if (code >= 40 && code < 48) {
352
- style.background = colors16[0][code - 40];
353
- } else if (code >= 90 && code < 98) {
354
- style.color = colors16[1][code - 90];
355
- } else if (code >= 100 && code < 108) {
356
- style.background = colors16[1][code - 100];
357
- } else if (code === 38 || code === 48) {
358
- // Extended set foreground/background color
359
- // validate that param exists
360
- if (index < maxIndex) {
361
- const mode = read();
362
- // Extend color (38=fg, 48=bg)
363
- const isForeground = code === 38;
364
- // MODE 5 - 256 color palette
365
- if (mode === 5 && index <= maxIndex) {
366
- const index = utils.toUint8(read());
367
- if (isForeground) {
368
- style.color = colors256[index];
369
- } else {
370
- style.background = colors256[index];
371
- }
372
- }
373
- // MODE 2 - True Color
374
- if (mode === 2 && index + 2 <= maxIndex) {
375
- const r = utils.toUint8(read());
376
- const g = utils.toUint8(read());
377
- const b = utils.toUint8(read());
378
- // True Color
379
- const color = [r, g, b];
380
- if (isForeground) {
381
- style.color = color;
382
- } else {
383
- style.background = color;
384
- }
385
- }
292
+ style.underline = false;
293
+ style.strikethrough = false;
294
+ }
295
+ process(signal) {
296
+ let index = 0;
297
+ // Ok - we have a valid "SGR" (Select Graphic Rendition)
298
+ const sequences = signal.split(';');
299
+ const maxIndex = sequences.length - 1;
300
+ // ANSI style and colors
301
+ const { style, colors16, colors256 } = this;
302
+ // Read cmd by index
303
+ const read = () => parseInt(sequences[index++], 10);
304
+ // Each of these params affects the SGR state
305
+ // Why do we shift through the array instead of a forEach??
306
+ // ... because some commands consume the params that follow !
307
+ for (; index <= maxIndex; index++) {
308
+ const code = read();
309
+ if (code === 1) {
310
+ style.bold = true;
311
+ }
312
+ else if (code === 2) {
313
+ style.dim = true;
314
+ }
315
+ else if (code === 3) {
316
+ style.italic = true;
317
+ }
318
+ else if (code === 4) {
319
+ style.underline = true;
320
+ }
321
+ else if (code === 5) {
322
+ style.blink = true;
323
+ }
324
+ else if (code === 7) {
325
+ style.inverse = true;
326
+ }
327
+ else if (code === 8) {
328
+ style.hidden = true;
329
+ }
330
+ else if (code === 9) {
331
+ style.strikethrough = true;
332
+ }
333
+ else if (code === 53) {
334
+ style.overline = true;
335
+ }
336
+ else if (code === 21) {
337
+ style.bold = false;
338
+ }
339
+ else if (code === 22) {
340
+ style.dim = false;
341
+ style.bold = false;
342
+ }
343
+ else if (code === 23) {
344
+ style.italic = false;
345
+ }
346
+ else if (code === 24) {
347
+ style.underline = false;
348
+ }
349
+ else if (code === 25) {
350
+ style.blink = false;
351
+ }
352
+ else if (code === 27) {
353
+ style.inverse = false;
354
+ }
355
+ else if (code === 28) {
356
+ style.hidden = false;
357
+ }
358
+ else if (code === 29) {
359
+ style.strikethrough = false;
360
+ }
361
+ else if (code === 29) {
362
+ style.strikethrough = false;
363
+ }
364
+ else if (code === 55) {
365
+ style.overline = false;
366
+ }
367
+ else if (code === 49) {
368
+ style.background = null;
369
+ }
370
+ else if (code >= 30 && code < 38) {
371
+ style.color = colors16[0][code - 30];
372
+ }
373
+ else if (code >= 40 && code < 48) {
374
+ style.background = colors16[0][code - 40];
375
+ }
376
+ else if (code >= 90 && code < 98) {
377
+ style.color = colors16[1][code - 90];
378
+ }
379
+ else if (code >= 100 && code < 108) {
380
+ style.background = colors16[1][code - 100];
381
+ }
382
+ else if (code === 38 || code === 48) {
383
+ // Extended set foreground/background color
384
+ // validate that param exists
385
+ if (index < maxIndex) {
386
+ const mode = read();
387
+ // Extend color (38=fg, 48=bg)
388
+ const isForeground = code === 38;
389
+ // MODE 5 - 256 color palette
390
+ if (mode === 5 && index <= maxIndex) {
391
+ const index = utils.toUint8(read());
392
+ if (isForeground) {
393
+ style.color = colors256[index];
394
+ }
395
+ else {
396
+ style.background = colors256[index];
397
+ }
398
+ }
399
+ // MODE 2 - True Color
400
+ if (mode === 2 && index + 2 <= maxIndex) {
401
+ const r = utils.toUint8(read());
402
+ const g = utils.toUint8(read());
403
+ const b = utils.toUint8(read());
404
+ // True Color
405
+ const color = [r, g, b];
406
+ if (isForeground) {
407
+ style.color = color;
408
+ }
409
+ else {
410
+ style.background = color;
411
+ }
412
+ }
413
+ }
414
+ }
415
+ else {
416
+ this.reset();
417
+ }
386
418
  }
387
- } else {
388
- this.reset();
389
- }
390
419
  }
391
- }
392
- block(token) {
393
- const block = {
394
- value: token.value,
395
- style: { ...this.style }
396
- };
397
- if ('url' in token) {
398
- block.url = token.url;
420
+ block(token) {
421
+ const block = {
422
+ value: token.value,
423
+ style: { ...this.style }
424
+ };
425
+ if ('url' in token) {
426
+ block.url = token.url;
427
+ }
428
+ return block;
399
429
  }
400
- return block;
401
- }
402
- write(text, callback) {
403
- this.buffer += text;
404
- while (this.buffer) {
405
- const token = this.read();
406
- switch (token.type) {
407
- case 0 /* TokenType.EOS */:
408
- case 5 /* TokenType.INCESC */:
409
- break;
410
- case 1 /* TokenType.ESC */:
411
- case 6 /* TokenType.UNKNOWN */:
412
- continue;
413
- case 3 /* TokenType.SGR */:
414
- this.process(token.signal);
415
- continue;
416
- case 2 /* TokenType.OSC */:
417
- case 4 /* TokenType.TEXT */:
418
- callback(this.block(token));
419
- continue;
420
- default:
421
- continue;
422
- }
430
+ write(text, callback) {
431
+ this.buffer += text;
432
+ while (this.buffer) {
433
+ const token = this.read();
434
+ switch (token.type) {
435
+ case 0 /* TokenType.EOS */:
436
+ case 5 /* TokenType.INCESC */:
437
+ break;
438
+ case 1 /* TokenType.ESC */:
439
+ case 6 /* TokenType.UNKNOWN */:
440
+ continue;
441
+ case 3 /* TokenType.SGR */:
442
+ this.process(token.signal);
443
+ continue;
444
+ case 2 /* TokenType.OSC */:
445
+ case 4 /* TokenType.TEXT */:
446
+ callback(this.block(token));
447
+ continue;
448
+ default:
449
+ continue;
450
+ }
451
+ }
423
452
  }
424
- }
425
- flush(callback) {
426
- const { buffer } = this;
427
- // Get flush block
428
- if (buffer !== '') {
429
- callback(
430
- this.block({
431
- value: buffer,
432
- type: 4 /* TokenType.TEXT */
433
- })
434
- );
453
+ flush(callback) {
454
+ const { buffer } = this;
455
+ // Get flush block
456
+ if (buffer !== '') {
457
+ callback(this.block({
458
+ value: buffer,
459
+ type: 4 /* TokenType.TEXT */
460
+ }));
461
+ }
462
+ // Reset
463
+ this.reset();
464
+ // Flush buffer
465
+ this.buffer = '';
435
466
  }
436
- // Reset
437
- this.reset();
438
- // Flush buffer
439
- this.buffer = '';
440
- }
441
467
  }
442
468
 
443
469
  module.exports = Ansi;