7
7
*/
8
8
9
9
import { BaseException } from '@angular-devkit/core' ;
10
+ import MagicString from 'magic-string' ;
11
+ import { updateBufferV2Enabled } from './environment-options' ;
10
12
import { LinkedList } from './linked-list' ;
11
13
12
14
export class IndexOutOfBoundException extends BaseException {
13
15
constructor ( index : number , min : number , max = Infinity ) {
14
16
super ( `Index ${ index } outside of range [${ min } , ${ max } ].` ) ;
15
17
}
16
18
}
19
+ /** @deprecated Since v13.0 */
17
20
export class ContentCannotBeRemovedException extends BaseException {
18
21
constructor ( ) {
19
22
super ( `User tried to remove content that was marked essential.` ) ;
@@ -26,6 +29,7 @@ export class ContentCannotBeRemovedException extends BaseException {
26
29
* it means the content itself was deleted.
27
30
*
28
31
* @see UpdateBuffer
32
+ * @deprecated Since v13.0
29
33
*/
30
34
export class Chunk {
31
35
private _content : Buffer | null ;
@@ -176,6 +180,37 @@ export class Chunk {
176
180
}
177
181
}
178
182
183
+ /**
184
+ * Base class for an update buffer implementation that allows buffers to be inserted to the _right
185
+ * or _left, or deleted, while keeping indices to the original buffer.
186
+ */
187
+ export abstract class UpdateBufferBase {
188
+ constructor ( protected _originalContent : Buffer ) { }
189
+ abstract get length ( ) : number ;
190
+ abstract get original ( ) : Buffer ;
191
+ abstract toString ( encoding ?: string ) : string ;
192
+ abstract generate ( ) : Buffer ;
193
+ abstract insertLeft ( index : number , content : Buffer , assert ?: boolean ) : void ;
194
+ abstract insertRight ( index : number , content : Buffer , assert ?: boolean ) : void ;
195
+ abstract remove ( index : number , length : number ) : void ;
196
+
197
+ /**
198
+ * Creates an UpdateBufferBase instance. Depending on the NG_UPDATE_BUFFER_V2
199
+ * environment variable, will either create an UpdateBuffer or an UpdateBuffer2
200
+ * instance.
201
+ *
202
+ * See: https://github.com/angular/angular-cli/issues/21110
203
+ *
204
+ * @param originalContent The original content of the update buffer instance.
205
+ * @returns An UpdateBufferBase instance.
206
+ */
207
+ static create ( originalContent : Buffer ) : UpdateBufferBase {
208
+ return updateBufferV2Enabled
209
+ ? new UpdateBuffer2 ( originalContent )
210
+ : new UpdateBuffer ( originalContent ) ;
211
+ }
212
+ }
213
+
179
214
/**
180
215
* An utility class that allows buffers to be inserted to the _right or _left, or deleted, while
181
216
* keeping indices to the original buffer.
@@ -185,12 +220,15 @@ export class Chunk {
185
220
*
186
221
* Since the Node Buffer structure is non-destructive when slicing, we try to use slicing to create
187
222
* new chunks, and always keep chunks pointing to the original content.
223
+ *
224
+ * @deprecated Since v13.0
188
225
*/
189
- export class UpdateBuffer {
226
+ export class UpdateBuffer extends UpdateBufferBase {
190
227
protected _linkedList : LinkedList < Chunk > ;
191
228
192
- constructor ( protected _originalContent : Buffer ) {
193
- this . _linkedList = new LinkedList ( new Chunk ( 0 , _originalContent . length , _originalContent ) ) ;
229
+ constructor ( originalContent : Buffer ) {
230
+ super ( originalContent ) ;
231
+ this . _linkedList = new LinkedList ( new Chunk ( 0 , originalContent . length , originalContent ) ) ;
194
232
}
195
233
196
234
protected _assertIndex ( index : number ) {
@@ -274,3 +312,47 @@ export class UpdateBuffer {
274
312
}
275
313
}
276
314
}
315
+
316
+ /**
317
+ * An utility class that allows buffers to be inserted to the _right or _left, or deleted, while
318
+ * keeping indices to the original buffer.
319
+ */
320
+ export class UpdateBuffer2 extends UpdateBufferBase {
321
+ protected _mutatableContent : MagicString = new MagicString ( this . _originalContent . toString ( ) ) ;
322
+
323
+ protected _assertIndex ( index : number ) {
324
+ if ( index < 0 || index > this . _originalContent . length ) {
325
+ throw new IndexOutOfBoundException ( index , 0 , this . _originalContent . length ) ;
326
+ }
327
+ }
328
+
329
+ get length ( ) : number {
330
+ return this . _mutatableContent . length ( ) ;
331
+ }
332
+ get original ( ) : Buffer {
333
+ return this . _originalContent ;
334
+ }
335
+
336
+ toString ( ) : string {
337
+ return this . _mutatableContent . toString ( ) ;
338
+ }
339
+
340
+ generate ( ) : Buffer {
341
+ return Buffer . from ( this . toString ( ) ) ;
342
+ }
343
+
344
+ insertLeft ( index : number , content : Buffer ) : void {
345
+ this . _assertIndex ( index ) ;
346
+ this . _mutatableContent . appendLeft ( index , content . toString ( ) ) ;
347
+ }
348
+
349
+ insertRight ( index : number , content : Buffer ) : void {
350
+ this . _assertIndex ( index ) ;
351
+ this . _mutatableContent . appendRight ( index , content . toString ( ) ) ;
352
+ }
353
+
354
+ remove ( index : number , length : number ) {
355
+ this . _assertIndex ( index ) ;
356
+ this . _mutatableContent . remove ( index , index + length ) ;
357
+ }
358
+ }
0 commit comments