Skip to content

Commit 0ae5a9c

Browse files
committed
refactor(@angular-devkit/core): deprecate stringToFileBuffer and fileBufferToString
DEPRECATED: `stringToFileBuffer` and `fileBufferToString` are deprecated. Use standard Web APIs (`TextEncoder` and `TextDecoder`) instead. Internal usages within the repository have been removed and replaced with standard Web APIs. The public API golden file for `@angular-devkit/core` has been updated to reflect the deprecations.
1 parent db3c5d8 commit 0ae5a9c

30 files changed

Lines changed: 168 additions & 186 deletions

File tree

goldens/public-api/angular_devkit/core/index.api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ type FileBuffer = ArrayBuffer;
243243
// @public (undocumented)
244244
type FileBufferLike = ArrayBufferLike;
245245

246-
// @public (undocumented)
246+
// @public @deprecated (undocumented)
247247
function fileBufferToString(fileBuffer: FileBuffer): string;
248248

249249
// @public (undocumented)
@@ -1012,7 +1012,7 @@ declare namespace strings {
10121012
}
10131013
export { strings }
10141014

1015-
// @public (undocumented)
1015+
// @public @deprecated (undocumented)
10161016
function stringToFileBuffer(str: string): FileBuffer;
10171017

10181018
// @public (undocumented)

packages/angular_devkit/architect/testing/test-project-host.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export class TestProjectHost extends NodeJsSyncHost {
117117
Object.keys(files).forEach((fileName) => {
118118
let content = files[fileName];
119119
if (typeof content == 'string') {
120-
content = virtualFs.stringToFileBuffer(content);
120+
content = new TextEncoder().encode(content).buffer;
121121
} else if (content instanceof Buffer) {
122122
content = content.buffer.slice(content.byteOffset, content.byteOffset + content.byteLength);
123123
}
@@ -127,16 +127,16 @@ export class TestProjectHost extends NodeJsSyncHost {
127127
}
128128

129129
replaceInFile(path: string, match: RegExp | string, replacement: string): void {
130-
const content = virtualFs.fileBufferToString(this.scopedSync().read(normalize(path)));
130+
const content = new TextDecoder().decode(this.scopedSync().read(normalize(path)));
131131
this.scopedSync().write(
132132
normalize(path),
133-
virtualFs.stringToFileBuffer(content.replace(match, replacement)),
133+
new TextEncoder().encode(content.replace(match, replacement)).buffer,
134134
);
135135
}
136136

137137
appendToFile(path: string, str: string): void {
138-
const content = virtualFs.fileBufferToString(this.scopedSync().read(normalize(path)));
139-
this.scopedSync().write(normalize(path), virtualFs.stringToFileBuffer(content.concat(str)));
138+
const content = new TextDecoder().decode(this.scopedSync().read(normalize(path)));
139+
this.scopedSync().write(normalize(path), new TextEncoder().encode(content.concat(str)).buffer);
140140
}
141141

142142
fileMatchExists(dir: string, regex: RegExp): PathFragment | undefined {

packages/angular_devkit/build_angular/src/builders/app-shell/app-shell_spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import { Architect } from '@angular-devkit/architect';
10-
import { normalize, virtualFs } from '@angular-devkit/core';
10+
import { normalize } from '@angular-devkit/core';
1111
import { createArchitect, host } from '../../testing/test-utils';
1212

1313
describe('AppShell Builder', () => {
@@ -133,7 +133,7 @@ describe('AppShell Builder', () => {
133133
expect(output.success).toBe(true);
134134

135135
const fileName = 'dist/index.html';
136-
const content = virtualFs.fileBufferToString(host.scopedSync().read(normalize(fileName)));
136+
const content = new TextDecoder().decode(host.scopedSync().read(normalize(fileName)));
137137
expect(content).toMatch('Welcome to app');
138138
});
139139

@@ -147,7 +147,7 @@ describe('AppShell Builder', () => {
147147

148148
expect(output.success).toBe(true);
149149
const fileName = 'dist/index.html';
150-
const content = virtualFs.fileBufferToString(host.scopedSync().read(normalize(fileName)));
150+
const content = new TextDecoder().decode(host.scopedSync().read(normalize(fileName)));
151151
expect(content).toContain('app-shell works!');
152152
});
153153

@@ -164,7 +164,7 @@ describe('AppShell Builder', () => {
164164

165165
expect(output.success).toBe(true);
166166
const fileName = 'dist/index.html';
167-
const content = virtualFs.fileBufferToString(host.scopedSync().read(normalize(fileName)));
167+
const content = new TextDecoder().decode(host.scopedSync().read(normalize(fileName)));
168168

169169
expect(content).toContain('app-shell works!');
170170
expect(content).toContain('p{color:#000}');
@@ -187,7 +187,7 @@ describe('AppShell Builder', () => {
187187

188188
expect(output.success).toBe(true);
189189
const fileName = 'dist/index.html';
190-
const content = virtualFs.fileBufferToString(host.scopedSync().read(normalize(fileName)));
190+
const content = new TextDecoder().decode(host.scopedSync().read(normalize(fileName)));
191191

192192
expect(content).toContain('app-shell works!');
193193
expect(content).toContain('<style nonce="{% nonce %}">p{color:#000}</style>');

packages/angular_devkit/build_angular/src/builders/browser/specs/allow-js_spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import { Architect } from '@angular-devkit/architect';
1010

11-
import { join, normalize, relative, virtualFs } from '@angular-devkit/core';
11+
import { join, normalize, relative } from '@angular-devkit/core';
1212
import { Observable, lastValueFrom, take, tap } from 'rxjs';
1313
import { createArchitect, host } from '../../../testing/test-utils';
1414
import { BrowserBuilderOutput } from '../index';
@@ -39,7 +39,7 @@ describe('Browser Builder allow js', () => {
3939
const output = await run.result;
4040
expect(output.success).toBe(true);
4141

42-
const content = virtualFs.fileBufferToString(
42+
const content = new TextDecoder().decode(
4343
await lastValueFrom(host.read(join(normalize(output.outputs[0].path), 'main.js'))),
4444
);
4545

@@ -66,7 +66,7 @@ describe('Browser Builder allow js', () => {
6666
const output = await run.result;
6767
expect(output.success).toBe(true);
6868

69-
const content = virtualFs.fileBufferToString(
69+
const content = new TextDecoder().decode(
7070
await lastValueFrom(host.read(join(normalize(output.outputs[0].path), 'main.js'))),
7171
);
7272

@@ -96,7 +96,7 @@ describe('Browser Builder allow js', () => {
9696
run.output.pipe(
9797
tap((output) => {
9898
const path = relative(host.root(), join(normalize(output.outputs[0].path), 'main.js'));
99-
const content = virtualFs.fileBufferToString(host.scopedSync().read(path));
99+
const content = new TextDecoder().decode(host.scopedSync().read(path));
100100

101101
switch (buildCount) {
102102
case 1:

packages/angular_devkit/build_angular/src/builders/browser/specs/base-href_spec.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import { Architect } from '@angular-devkit/architect';
10-
import { join, normalize, tags, virtualFs } from '@angular-devkit/core';
10+
import { join, normalize, tags } from '@angular-devkit/core';
1111
import { lastValueFrom } from 'rxjs';
1212
import { createArchitect, host } from '../../../testing/test-utils';
1313

@@ -39,7 +39,7 @@ describe('Browser Builder base href', () => {
3939

4040
expect(output.success).toBe(true);
4141
const fileName = join(normalize(output.outputs[0].path), 'index.html');
42-
const content = virtualFs.fileBufferToString(await lastValueFrom(host.read(fileName)));
42+
const content = new TextDecoder().decode(await lastValueFrom(host.read(fileName)));
4343
expect(content).toMatch(/<base href="\/myUrl">/);
4444

4545
await run.stop();
@@ -60,7 +60,7 @@ describe('Browser Builder base href', () => {
6060

6161
expect(output.success).toBeTrue();
6262
const fileName = join(normalize(output.outputs[0].path), 'index.html');
63-
const content = virtualFs.fileBufferToString(await lastValueFrom(host.read(fileName)));
63+
const content = new TextDecoder().decode(await lastValueFrom(host.read(fileName)));
6464
expect(content).toContain(`<base href=".">`);
6565

6666
await run.stop();
@@ -79,9 +79,7 @@ describe('Browser Builder base href', () => {
7979
const output = await run.result;
8080
expect(output.success).toBe(true);
8181
const fileName = join(normalize(output.outputs[0].path), 'index.html');
82-
const content = virtualFs.fileBufferToString(
83-
await lastValueFrom(host.read(normalize(fileName))),
84-
);
82+
const content = new TextDecoder().decode(await lastValueFrom(host.read(normalize(fileName))));
8583
expect(content).toContain('<head><base href="/myUrl"><meta charset="UTF-8">');
8684
await run.stop();
8785
});

packages/angular_devkit/build_angular/src/builders/browser/specs/cross-origin_spec.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import { Architect } from '@angular-devkit/architect';
10-
import { join, normalize, virtualFs } from '@angular-devkit/core';
10+
import { join, normalize } from '@angular-devkit/core';
1111
import { lastValueFrom } from 'rxjs';
1212
import { createArchitect, host } from '../../../testing/test-utils';
1313
import { BrowserBuilderOutput } from '../index';
@@ -37,9 +37,7 @@ describe('Browser Builder crossOrigin', () => {
3737
const output = await run.result;
3838
expect(output.success).toBe(true);
3939
const fileName = join(normalize(output.outputs[0].path), 'index.html');
40-
const content = virtualFs.fileBufferToString(
41-
await lastValueFrom(host.read(normalize(fileName))),
42-
);
40+
const content = new TextDecoder().decode(await lastValueFrom(host.read(normalize(fileName))));
4341
expect(content).toBe(
4442
`<html><head><base href="/"><link rel="stylesheet" href="styles.css" crossorigin="use-credentials"></head>` +
4543
`<body><app-root></app-root>` +
@@ -57,9 +55,7 @@ describe('Browser Builder crossOrigin', () => {
5755
const output = await run.result;
5856
expect(output.success).toBe(true);
5957
const fileName = join(normalize(output.outputs[0].path), 'index.html');
60-
const content = virtualFs.fileBufferToString(
61-
await lastValueFrom(host.read(normalize(fileName))),
62-
);
58+
const content = new TextDecoder().decode(await lastValueFrom(host.read(normalize(fileName))));
6359
expect(content).toBe(
6460
`<html><head><base href="/">` +
6561
`<link rel="stylesheet" href="styles.css" crossorigin="anonymous"></head>` +
@@ -78,9 +74,7 @@ describe('Browser Builder crossOrigin', () => {
7874
const output = await run.result;
7975
expect(output.success).toBe(true);
8076
const fileName = join(normalize(output.outputs[0].path), 'index.html');
81-
const content = virtualFs.fileBufferToString(
82-
await lastValueFrom(host.read(normalize(fileName))),
83-
);
77+
const content = new TextDecoder().decode(await lastValueFrom(host.read(normalize(fileName))));
8478
expect(content).toBe(
8579
`<html><head><base href="/">` +
8680
`<link rel="stylesheet" href="styles.css"></head>` +
@@ -105,9 +99,7 @@ describe('Browser Builder crossOrigin', () => {
10599
expect(output.success).toBe(true);
106100

107101
const fileName = join(normalize(output.outputs[0].path), 'runtime.js');
108-
const content = virtualFs.fileBufferToString(
109-
await lastValueFrom(host.read(normalize(fileName))),
110-
);
102+
const content = new TextDecoder().decode(await lastValueFrom(host.read(normalize(fileName))));
111103
expect(content).toContain('script.crossOrigin = "use-credentials"');
112104
await run.stop();
113105
});

packages/angular_devkit/build_angular/src/builders/browser/specs/deploy-url_spec.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import { Architect } from '@angular-devkit/architect';
10-
import { join, normalize, virtualFs } from '@angular-devkit/core';
10+
import { join, normalize } from '@angular-devkit/core';
1111
import { lastValueFrom } from 'rxjs';
1212
import { createArchitect, host } from '../../../testing/test-utils';
1313
import { BrowserBuilderOutput } from '../index';
@@ -41,11 +41,9 @@ describe('Browser Builder deploy url', () => {
4141

4242
const fileName = join(outputPath, 'index.html');
4343
const runtimeFileName = join(outputPath, 'runtime.js');
44-
const content = virtualFs.fileBufferToString(
45-
await lastValueFrom(host.read(normalize(fileName))),
46-
);
44+
const content = new TextDecoder().decode(await lastValueFrom(host.read(normalize(fileName))));
4745
expect(content).toContain('deployUrl/main.js');
48-
const runtimeContent = virtualFs.fileBufferToString(
46+
const runtimeContent = new TextDecoder().decode(
4947
await lastValueFrom(host.read(normalize(runtimeFileName))),
5048
);
5149
expect(runtimeContent).toContain('deployUrl/');
@@ -54,9 +52,7 @@ describe('Browser Builder deploy url', () => {
5452
const output2 = await run2.result;
5553
expect(output2.outputs[0].path).toEqual(outputPath); // These should be the same.
5654

57-
const content2 = virtualFs.fileBufferToString(
58-
await lastValueFrom(host.read(normalize(fileName))),
59-
);
55+
const content2 = new TextDecoder().decode(await lastValueFrom(host.read(normalize(fileName))));
6056
expect(content2).toContain('http://example.com/some/path/main.js');
6157

6258
await run.stop();

packages/angular_devkit/build_angular/src/builders/browser/specs/index_spec.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import { Architect } from '@angular-devkit/architect';
10-
import { join, normalize, tags, virtualFs, workspaces } from '@angular-devkit/core';
10+
import { join, normalize, tags, workspaces } from '@angular-devkit/core';
1111
import { lastValueFrom } from 'rxjs';
1212
import { createArchitect, host } from '../../../testing/test-utils';
1313
import { BrowserBuilderOutput } from '../index';
@@ -34,9 +34,7 @@ describe('Browser Builder index HTML processing', () => {
3434
const output = await run.result;
3535
expect(output.success).toBe(true);
3636
const fileName = join(normalize(output.outputs[0].path), 'index.html');
37-
const content = virtualFs.fileBufferToString(
38-
await lastValueFrom(host.read(normalize(fileName))),
39-
);
37+
const content = new TextDecoder().decode(await lastValueFrom(host.read(normalize(fileName))));
4038
expect(content).toBe(
4139
`<html><head><base href="/"><link rel="stylesheet" href="styles.css"></head>` +
4240
`<body><app-root></app-root><script src="runtime.js" type="module"></script>` +
@@ -59,9 +57,7 @@ describe('Browser Builder index HTML processing', () => {
5957
const output = await run.result;
6058
expect(output.success).toBe(true);
6159
const fileName = join(normalize(output.outputs[0].path), 'index.html');
62-
const content = virtualFs.fileBufferToString(
63-
await lastValueFrom(host.read(normalize(fileName))),
64-
);
60+
const content = new TextDecoder().decode(await lastValueFrom(host.read(normalize(fileName))));
6561
expect(content).toBe(
6662
`<html><head><base href="/"><link rel="stylesheet" href="styles.css"></head>` +
6763
`<body><app-root></app-root>` +
@@ -84,9 +80,7 @@ describe('Browser Builder index HTML processing', () => {
8480
const output = await run.result;
8581
expect(output.success).toBe(true);
8682
const fileName = join(normalize(output.outputs[0].path), 'index.html');
87-
const content = virtualFs.fileBufferToString(
88-
await lastValueFrom(host.read(normalize(fileName))),
89-
);
83+
const content = new TextDecoder().decode(await lastValueFrom(host.read(normalize(fileName))));
9084
expect(content).toBe(
9185
`<html><head><title>&iacute;</title><base href="/"><link rel="stylesheet" href="styles.css"></head> ` +
9286
`<body><app-root></app-root><script src="runtime.js" type="module"></script>` +
@@ -108,9 +102,7 @@ describe('Browser Builder index HTML processing', () => {
108102
const output = await run.result;
109103
expect(output.success).toBe(true);
110104
const fileName = join(normalize(output.outputs[0].path), 'index.html');
111-
const content = virtualFs.fileBufferToString(
112-
await lastValueFrom(host.read(normalize(fileName))),
113-
);
105+
const content = new TextDecoder().decode(await lastValueFrom(host.read(normalize(fileName))));
114106
expect(content).toBe(
115107
`<html><head><base href="/"><%= csrf_meta_tags %><link rel="stylesheet" href="styles.css"></head> ` +
116108
`<body><app-root></app-root><script src="runtime.js" type="module"></script>` +
@@ -159,7 +151,7 @@ describe('Browser Builder index HTML processing', () => {
159151

160152
const outputIndexPath = join(host.root(), 'dist', 'index.html');
161153
const content = await lastValueFrom(host.read(normalize(outputIndexPath)));
162-
expect(virtualFs.fileBufferToString(content)).toBe(
154+
expect(new TextDecoder().decode(content)).toBe(
163155
`<html><head><base href="/"><%= csrf_meta_tags %><link rel="stylesheet" href="styles.css"></head> ` +
164156
`<body><app-root></app-root><script src="runtime.js" type="module"></script>` +
165157
`<script src="polyfills.js" type="module"></script>` +
@@ -206,7 +198,7 @@ describe('Browser Builder index HTML processing', () => {
206198

207199
const outputIndexPath = join(host.root(), 'dist', 'main.html');
208200
const content = await lastValueFrom(host.read(normalize(outputIndexPath)));
209-
expect(virtualFs.fileBufferToString(content)).toBe(
201+
expect(new TextDecoder().decode(content)).toBe(
210202
`<html><head><base href="/"><link rel="stylesheet" href="styles.css"></head> ` +
211203
`<body><app-root></app-root><script src="runtime.js" type="module"></script>` +
212204
`<script src="polyfills.js" type="module"></script>` +
@@ -253,7 +245,7 @@ describe('Browser Builder index HTML processing', () => {
253245

254246
const outputIndexPath = join(host.root(), 'dist', 'extra', 'main.html');
255247
const content = await lastValueFrom(host.read(normalize(outputIndexPath)));
256-
expect(virtualFs.fileBufferToString(content)).toBe(
248+
expect(new TextDecoder().decode(content)).toBe(
257249
`<html><head><base href="/"><link rel="stylesheet" href="styles.css"></head> ` +
258250
`<body><app-root></app-root><script src="runtime.js" type="module"></script>` +
259251
`<script src="polyfills.js" type="module"></script>` +

packages/angular_devkit/build_angular/src/builders/browser/specs/output-path_spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import { Architect } from '@angular-devkit/architect';
10-
import { getSystemPath, join, virtualFs } from '@angular-devkit/core';
10+
import { getSystemPath, join } from '@angular-devkit/core';
1111
import * as fs from 'node:fs';
1212
import { browserBuild, createArchitect, host } from '../../../testing/test-utils';
1313

@@ -24,7 +24,7 @@ describe('Browser Builder output path', () => {
2424
it('deletes output path content', async () => {
2525
// Write a file to the output path to later verify it was deleted.
2626
await host
27-
.write(join(host.root(), 'dist/file.txt'), virtualFs.stringToFileBuffer('file'))
27+
.write(join(host.root(), 'dist/file.txt'), new TextEncoder().encode('file').buffer)
2828
.toPromise();
2929

3030
// Delete an app file to force a failed compilation.
@@ -42,7 +42,7 @@ describe('Browser Builder output path', () => {
4242
// Write a file to the output path to later verify it was deleted.
4343
host.writeMultipleFiles({
4444
'src-link/a.txt': '',
45-
'dist/file.txt': virtualFs.stringToFileBuffer('file'),
45+
'dist/file.txt': new TextEncoder().encode('file').buffer,
4646
});
4747

4848
const distLinked = join(host.root(), 'dist', 'linked');

0 commit comments

Comments
 (0)