Skip to content

Commit 4ba67ee

Browse files
hanslfilipesilva
authored andcommitted
refactor: remove usage of Buffer constructor
Its being deprecated in Node 10.4. The replacements are available in Node 8 so its all good.
1 parent a11dddf commit 4ba67ee

File tree

11 files changed

+77
-77
lines changed

11 files changed

+77
-77
lines changed

packages/angular_devkit/core/src/utils/template.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ function templateWithSourceMap(ast: TemplateAst, options?: TemplateOptions): str
334334

335335
return code.code
336336
+ '\n//# sourceMappingURL=data:application/json;base64,'
337-
+ new Buffer(code.map.toString()).toString('base64');
337+
+ Buffer.from(code.map.toString()).toString('base64');
338338
}
339339

340340

packages/angular_devkit/core/src/virtual-fs/host/buffer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export function fileBufferToString(fileBuffer: FileBuffer): string {
5656
if (fileBuffer.toString.length == 1) {
5757
return (fileBuffer.toString as (enc: string) => string)('utf-8');
5858
} else if (typeof Buffer !== 'undefined') {
59-
return new Buffer(fileBuffer).toString('utf-8');
59+
return Buffer.from(fileBuffer).toString('utf-8');
6060
} else if (typeof TextDecoder !== 'undefined') {
6161
// Modern browsers implement TextEncode.
6262
return new TextDecoder('utf-8').decode(new Uint8Array(fileBuffer));

packages/angular_devkit/schematics/src/rules/template.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function applyContentTemplate<T extends TemplateOptions>(options: T): Fil
4747

4848
return {
4949
path: path,
50-
content: new Buffer(templateImpl(content.toString('utf-8'), {})(options)),
50+
content: Buffer.from(templateImpl(content.toString('utf-8'), {})(options)),
5151
};
5252
};
5353
}

packages/angular_devkit/schematics/src/rules/template_spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function _entry(path?: string, content?: string): FileEntry {
2626

2727
return {
2828
path: normalize(path),
29-
content: new Buffer(content),
29+
content: Buffer.from(content),
3030
};
3131
}
3232

packages/angular_devkit/schematics/src/tree/action_spec.ts

+19-19
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ describe('Action', () => {
1414
it('works with create', () => {
1515
const actions = new ActionList;
1616

17-
actions.create(normalize('/a/b'), new Buffer('1'));
18-
actions.create(normalize('/a/c'), new Buffer('2'));
19-
actions.create(normalize('/a/c'), new Buffer('3'));
17+
actions.create(normalize('/a/b'), Buffer.from('1'));
18+
actions.create(normalize('/a/c'), Buffer.from('2'));
19+
actions.create(normalize('/a/c'), Buffer.from('3'));
2020

2121
expect(actions.length).toBe(3);
2222
actions.optimize();
@@ -25,10 +25,10 @@ describe('Action', () => {
2525
it('works with overwrite', () => {
2626
const actions = new ActionList;
2727

28-
actions.create(normalize('/a/b'), new Buffer('1'));
29-
actions.create(normalize('/a/c'), new Buffer('2'));
30-
actions.overwrite(normalize('/a/c'), new Buffer('3'));
31-
actions.overwrite(normalize('/a/b'), new Buffer('4'));
28+
actions.create(normalize('/a/b'), Buffer.from('1'));
29+
actions.create(normalize('/a/c'), Buffer.from('2'));
30+
actions.overwrite(normalize('/a/c'), Buffer.from('3'));
31+
actions.overwrite(normalize('/a/b'), Buffer.from('4'));
3232

3333
expect(actions.length).toBe(4);
3434
actions.optimize();
@@ -38,11 +38,11 @@ describe('Action', () => {
3838
it('works with cloning a list', () => {
3939
const actions = new ActionList;
4040

41-
actions.create(normalize('/a/b'), new Buffer('1'));
42-
actions.create(normalize('/a/c'), new Buffer('2'));
43-
actions.overwrite(normalize('/a/c'), new Buffer('3'));
44-
actions.overwrite(normalize('/a/b'), new Buffer('4'));
45-
actions.create(normalize('/a/d'), new Buffer('5'));
41+
actions.create(normalize('/a/b'), Buffer.from('1'));
42+
actions.create(normalize('/a/c'), Buffer.from('2'));
43+
actions.overwrite(normalize('/a/c'), Buffer.from('3'));
44+
actions.overwrite(normalize('/a/b'), Buffer.from('4'));
45+
actions.create(normalize('/a/d'), Buffer.from('5'));
4646

4747
const actions2 = new ActionList;
4848
actions.forEach(x => actions2.push(x));
@@ -59,10 +59,10 @@ describe('Action', () => {
5959
it('handles edge cases (1)', () => {
6060
const actions = new ActionList;
6161

62-
actions.create(normalize('/test'), new Buffer('1'));
63-
actions.overwrite(normalize('/test'), new Buffer('3'));
64-
actions.overwrite(normalize('/hello'), new Buffer('2'));
65-
actions.overwrite(normalize('/test'), new Buffer('4'));
62+
actions.create(normalize('/test'), Buffer.from('1'));
63+
actions.overwrite(normalize('/test'), Buffer.from('3'));
64+
actions.overwrite(normalize('/hello'), Buffer.from('2'));
65+
actions.overwrite(normalize('/test'), Buffer.from('4'));
6666

6767
const actions2 = new ActionList;
6868
actions.forEach(x => actions2.push(x));
@@ -79,9 +79,9 @@ describe('Action', () => {
7979
it('handles edge cases (2)', () => {
8080
const actions = new ActionList;
8181

82-
actions.create(normalize('/test'), new Buffer('1'));
82+
actions.create(normalize('/test'), Buffer.from('1'));
8383
actions.rename(normalize('/test'), normalize('/test1'));
84-
actions.overwrite(normalize('/test1'), new Buffer('2'));
84+
actions.overwrite(normalize('/test1'), Buffer.from('2'));
8585
actions.rename(normalize('/test1'), normalize('/test2'));
8686

8787
actions.optimize();
@@ -95,7 +95,7 @@ describe('Action', () => {
9595
const actions = new ActionList;
9696

9797
actions.rename(normalize('/test'), normalize('/test1'));
98-
actions.overwrite(normalize('/test1'), new Buffer('2'));
98+
actions.overwrite(normalize('/test1'), Buffer.from('2'));
9999
actions.rename(normalize('/test1'), normalize('/test2'));
100100

101101
actions.optimize();

packages/angular_devkit/schematics/src/tree/filesystem.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class FileSystemTree extends VirtualTree {
102102
this._initialized = true;
103103
this._recursiveFileList().forEach(path => {
104104
this._tree.set(path, new LazyFileEntry(path, () => {
105-
return new Buffer(host.read(path));
105+
return Buffer.from(host.read(path));
106106
}));
107107
});
108108
}
@@ -121,7 +121,7 @@ export class FileSystemTree extends VirtualTree {
121121

122122
if (fileExists) {
123123
const host = this._host;
124-
entry = new LazyFileEntry(normalizedPath, () => new Buffer(host.read(systemPath)));
124+
entry = new LazyFileEntry(normalizedPath, () => Buffer.from(host.read(systemPath)));
125125
this._tree.set(normalizedPath, entry);
126126
}
127127
}
@@ -178,7 +178,7 @@ export class FileSystemCreateTree extends FileSystemTree {
178178
super(host);
179179

180180
this._recursiveFileList().forEach(path => {
181-
this.create(path, new Buffer(this._host.read(path)));
181+
this.create(path, Buffer.from(this._host.read(path)));
182182
});
183183
this._initialized = true;
184184
}

packages/angular_devkit/schematics/src/tree/host-tree.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ export class HostTree implements Tree {
287287
return null;
288288
}
289289

290-
return new LazyFileEntry(p, () => new Buffer(this._recordSync.read(p)));
290+
return new LazyFileEntry(p, () => Buffer.from(this._recordSync.read(p)));
291291
}
292292

293293
getDir(path: string): DirEntry {
@@ -324,7 +324,7 @@ export class HostTree implements Tree {
324324
if (!this._recordSync.exists(p)) {
325325
throw new FileDoesNotExistException(p);
326326
}
327-
const c = typeof content == 'string' ? new Buffer(content) : content;
327+
const c = typeof content == 'string' ? Buffer.from(content) : content;
328328
this._record.overwrite(p, c as {} as virtualFs.FileBuffer).subscribe();
329329
}
330330
beginUpdate(path: string): UpdateRecorder {
@@ -356,7 +356,7 @@ export class HostTree implements Tree {
356356
if (this._recordSync.exists(p)) {
357357
throw new FileAlreadyExistException(p);
358358
}
359-
const c = typeof content == 'string' ? new Buffer(content) : content;
359+
const c = typeof content == 'string' ? Buffer.from(content) : content;
360360
this._record.create(p, c as {} as virtualFs.FileBuffer).subscribe();
361361
}
362362
delete(path: string): void {
@@ -393,15 +393,15 @@ export class HostTree implements Tree {
393393
parent: 0,
394394
kind: 'c',
395395
path: record.path,
396-
content: new Buffer(record.content),
396+
content: Buffer.from(record.content),
397397
} as CreateFileAction;
398398
case 'overwrite':
399399
return {
400400
id: this._id,
401401
parent: 0,
402402
kind: 'o',
403403
path: record.path,
404-
content: new Buffer(record.content),
404+
content: Buffer.from(record.content),
405405
} as OverwriteFileAction;
406406
case 'rename':
407407
return {

packages/angular_devkit/schematics/src/tree/recorder.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class UpdateRecorderBase implements UpdateRecorder {
1616
protected _content: UpdateBuffer;
1717

1818
constructor(entry: FileEntry) {
19-
this._original = new Buffer(entry.content);
19+
this._original = Buffer.from(entry.content);
2020
this._content = new UpdateBuffer(entry.content);
2121
this._path = entry.path;
2222
}
@@ -42,13 +42,13 @@ export class UpdateRecorderBase implements UpdateRecorder {
4242

4343
// These just record changes.
4444
insertLeft(index: number, content: Buffer | string): UpdateRecorder {
45-
this._content.insertLeft(index, typeof content == 'string' ? new Buffer(content) : content);
45+
this._content.insertLeft(index, typeof content == 'string' ? Buffer.from(content) : content);
4646

4747
return this;
4848
}
4949

5050
insertRight(index: number, content: Buffer | string): UpdateRecorder {
51-
this._content.insertRight(index, typeof content == 'string' ? new Buffer(content) : content);
51+
this._content.insertRight(index, typeof content == 'string' ? Buffer.from(content) : content);
5252

5353
return this;
5454
}

packages/angular_devkit/schematics/src/tree/recorder_spec.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { UpdateRecorderBase, UpdateRecorderBom } from './recorder';
1111

1212
describe('UpdateRecorderBase', () => {
1313
it('works for simple files', () => {
14-
const buffer = new Buffer('Hello World');
14+
const buffer = Buffer.from('Hello World');
1515
const entry = new SimpleFileEntry(normalize('/some/path'), buffer);
1616

1717
const recorder = new UpdateRecorderBase(entry);
@@ -21,7 +21,7 @@ describe('UpdateRecorderBase', () => {
2121
});
2222

2323
it('works for simple files (2)', () => {
24-
const buffer = new Buffer('Hello World');
24+
const buffer = Buffer.from('Hello World');
2525
const entry = new SimpleFileEntry(normalize('/some/path'), buffer);
2626

2727
const recorder = new UpdateRecorderBase(entry);
@@ -31,31 +31,31 @@ describe('UpdateRecorderBase', () => {
3131
});
3232

3333
it('can create the proper recorder', () => {
34-
const e = new SimpleFileEntry(normalize('/some/path'), new Buffer('hello'));
34+
const e = new SimpleFileEntry(normalize('/some/path'), Buffer.from('hello'));
3535
expect(UpdateRecorderBase.createFromFileEntry(e) instanceof UpdateRecorderBase).toBe(true);
3636
expect(UpdateRecorderBase.createFromFileEntry(e) instanceof UpdateRecorderBom).toBe(false);
3737
});
3838

3939
it('can create the proper recorder (bom)', () => {
40-
const eBom = new SimpleFileEntry(normalize('/some/path'), new Buffer('\uFEFFhello'));
40+
const eBom = new SimpleFileEntry(normalize('/some/path'), Buffer.from('\uFEFFhello'));
4141
expect(UpdateRecorderBase.createFromFileEntry(eBom) instanceof UpdateRecorderBase).toBe(true);
4242
expect(UpdateRecorderBase.createFromFileEntry(eBom) instanceof UpdateRecorderBom).toBe(true);
4343
});
4444

4545
it('supports empty files', () => {
46-
const e = new SimpleFileEntry(normalize('/some/path'), new Buffer(''));
46+
const e = new SimpleFileEntry(normalize('/some/path'), Buffer.from(''));
4747
expect(UpdateRecorderBase.createFromFileEntry(e) instanceof UpdateRecorderBase).toBe(true);
4848
});
4949

5050
it('supports empty files (bom)', () => {
51-
const eBom = new SimpleFileEntry(normalize('/some/path'), new Buffer('\uFEFF'));
51+
const eBom = new SimpleFileEntry(normalize('/some/path'), Buffer.from('\uFEFF'));
5252
expect(UpdateRecorderBase.createFromFileEntry(eBom) instanceof UpdateRecorderBase).toBe(true);
5353
});
5454
});
5555

5656
describe('UpdateRecorderBom', () => {
5757
it('works for simple files', () => {
58-
const buffer = new Buffer('\uFEFFHello World');
58+
const buffer = Buffer.from('\uFEFFHello World');
5959
const entry = new SimpleFileEntry(normalize('/some/path'), buffer);
6060

6161
const recorder = new UpdateRecorderBom(entry);

packages/angular_devkit/schematics/src/tree/virtual.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export class VirtualTree implements Tree {
207207
overwrite(path: string, content: Buffer | string) {
208208
const normalizedTo = this._normalizePath(path);
209209
if (typeof content == 'string') {
210-
content = new Buffer(content, 'utf-8');
210+
content = Buffer.from(content, 'utf-8');
211211
}
212212
const maybeEntry = this.get(normalizedTo);
213213
if (maybeEntry && maybeEntry.content.equals(content)) {
@@ -218,7 +218,7 @@ export class VirtualTree implements Tree {
218218
create(path: string, content: Buffer | string): void {
219219
const normalizedTo = this._normalizePath(path);
220220
if (typeof content == 'string') {
221-
content = new Buffer(content);
221+
content = Buffer.from(content);
222222
}
223223
this._create(normalizedTo, content);
224224
}

0 commit comments

Comments
 (0)