Skip to content

Commit c3433bc

Browse files
alan-agius4clydin
authored andcommitted
refactor(@angular-devkit/core): update stringToFileBuffer and fileBufferToString to use TextDecoder and TextEncoder
`TextDecoder` and `TextEncoder` can now be used on Node.js (cherry picked from commit a70e7a4)
1 parent 5a35970 commit c3433bc

File tree

3 files changed

+29
-81
lines changed

3 files changed

+29
-81
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ export class FileAlreadyExistException extends BaseException {
237237
// @public (undocumented)
238238
type FileBuffer = ArrayBuffer;
239239

240-
// @public (undocumented)
240+
// @public @deprecated (undocumented)
241241
const fileBuffer: TemplateTag<FileBuffer>;
242242

243243
// @public (undocumented)

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

Lines changed: 9 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,75 +6,23 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import { TextDecoder, TextEncoder } from 'node:util';
910
import { TemplateTag } from '../../utils/literals';
1011
import { FileBuffer } from './interface';
1112

12-
declare const TextEncoder: {
13-
new (encoding: string): {
14-
encode(str: string): Uint8Array;
15-
};
16-
};
17-
18-
declare const TextDecoder: {
19-
new (encoding: string): {
20-
decode(bytes: Uint8Array): string;
21-
};
22-
};
23-
2413
export function stringToFileBuffer(str: string): FileBuffer {
25-
// If we're in Node...
26-
if (typeof Buffer !== 'undefined' && typeof Buffer.from === 'function') {
27-
const buf = Buffer.from(str);
28-
const ab = new ArrayBuffer(buf.length);
29-
const view = new Uint8Array(ab);
30-
for (let i = 0; i < buf.length; ++i) {
31-
view[i] = buf[i];
32-
}
33-
34-
return ab;
35-
} else if (typeof TextEncoder !== 'undefined') {
36-
// Modern browsers implement TextEncode.
37-
return new TextEncoder('utf-8').encode(str).buffer;
38-
} else {
39-
// Slowest method but sure to be compatible with every platform.
40-
const buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char
41-
const bufView = new Uint16Array(buf);
42-
for (let i = 0, strLen = str.length; i < strLen; i++) {
43-
bufView[i] = str.charCodeAt(i);
44-
}
14+
return new TextEncoder().encode(str).buffer;
15+
}
4516

46-
return buf;
17+
export function fileBufferToString(fileBuffer: FileBuffer): string {
18+
if (fileBuffer.toString.length === 1) {
19+
return (fileBuffer.toString as (enc: string) => string)('utf-8');
4720
}
21+
22+
return new TextDecoder('utf-8').decode(new Uint8Array(fileBuffer));
4823
}
4924

25+
/** @deprecated use `stringToFileBuffer` instead. */
5026
export const fileBuffer: TemplateTag<FileBuffer> = (strings, ...values) => {
5127
return stringToFileBuffer(String.raw(strings, ...values));
5228
};
53-
54-
export function fileBufferToString(fileBuffer: FileBuffer): string {
55-
if (fileBuffer.toString.length == 1) {
56-
return (fileBuffer.toString as (enc: string) => string)('utf-8');
57-
} else if (typeof Buffer !== 'undefined') {
58-
return Buffer.from(fileBuffer).toString('utf-8');
59-
} else if (typeof TextDecoder !== 'undefined') {
60-
// Modern browsers implement TextEncode.
61-
return new TextDecoder('utf-8').decode(new Uint8Array(fileBuffer));
62-
} else {
63-
// Slowest method but sure to be compatible with every platform.
64-
const bufView = new Uint8Array(fileBuffer);
65-
const bufLength = bufView.length;
66-
let result = '';
67-
let chunkLength = Math.pow(2, 16) - 1;
68-
69-
// We have to chunk it because String.fromCharCode.apply will throw
70-
// `Maximum call stack size exceeded` on big inputs.
71-
for (let i = 0; i < bufLength; i += chunkLength) {
72-
if (i + chunkLength > bufLength) {
73-
chunkLength = bufLength - i;
74-
}
75-
result += String.fromCharCode.apply(null, [...bufView.subarray(i, i + chunkLength)]);
76-
}
77-
78-
return result;
79-
}
80-
}

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

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

99
/* eslint-disable @typescript-eslint/no-explicit-any */
1010
import { path } from '../path';
11-
import { fileBuffer } from './buffer';
11+
import { stringToFileBuffer } from './buffer';
1212
import { CordHost } from './record';
1313
import { test } from './test';
1414

@@ -22,7 +22,7 @@ describe('CordHost', () => {
2222
});
2323

2424
const host = new CordHost(base);
25-
host.write(path`/blue`, fileBuffer`hi`).subscribe(undefined, done.fail);
25+
host.write(path`/blue`, stringToFileBuffer(`hi`)).subscribe(undefined, done.fail);
2626

2727
const target = new TestHost();
2828
host.commit(target).subscribe(undefined, done.fail);
@@ -42,8 +42,8 @@ describe('CordHost', () => {
4242
});
4343

4444
const host = new CordHost(base);
45-
host.write(path`/blue`, fileBuffer`hi`).subscribe(undefined, done.fail);
46-
host.write(path`/blue`, fileBuffer`hi again`).subscribe(undefined, done.fail);
45+
host.write(path`/blue`, stringToFileBuffer(`hi`)).subscribe(undefined, done.fail);
46+
host.write(path`/blue`, stringToFileBuffer(`hi again`)).subscribe(undefined, done.fail);
4747

4848
const target = new TestHost();
4949
host.commit(target).subscribe(undefined, done.fail);
@@ -64,7 +64,7 @@ describe('CordHost', () => {
6464
});
6565

6666
const host = new CordHost(base);
67-
host.write(path`/blue`, fileBuffer`hi`).subscribe(undefined, done.fail);
67+
host.write(path`/blue`, stringToFileBuffer(`hi`)).subscribe(undefined, done.fail);
6868
host.delete(path`/blue`).subscribe(undefined, done.fail);
6969

7070
const target = new TestHost();
@@ -83,7 +83,7 @@ describe('CordHost', () => {
8383
});
8484

8585
const host = new CordHost(base);
86-
host.write(path`/blue`, fileBuffer`hi`).subscribe(undefined, done.fail);
86+
host.write(path`/blue`, stringToFileBuffer(`hi`)).subscribe(undefined, done.fail);
8787
host.rename(path`/blue`, path`/red`).subscribe(undefined, done.fail);
8888

8989
const target = new TestHost();
@@ -107,7 +107,7 @@ describe('CordHost', () => {
107107
});
108108

109109
const host = new CordHost(base);
110-
host.write(path`/blue`, fileBuffer`hi`).subscribe(undefined, done.fail);
110+
host.write(path`/blue`, stringToFileBuffer(`hi`)).subscribe(undefined, done.fail);
111111
host.rename(path`/blue`, path`/blue`).subscribe(undefined, done.fail);
112112

113113
const target = new TestHost();
@@ -130,7 +130,7 @@ describe('CordHost', () => {
130130
});
131131

132132
const host = new CordHost(base);
133-
host.write(path`/blue`, fileBuffer`hi`).subscribe(undefined, done.fail);
133+
host.write(path`/blue`, stringToFileBuffer(`hi`)).subscribe(undefined, done.fail);
134134
host.rename(path`/blue`, path`/red`).subscribe(undefined, done.fail);
135135
host.rename(path`/red`, path`/yellow`).subscribe(undefined, done.fail);
136136

@@ -203,7 +203,7 @@ describe('CordHost', () => {
203203

204204
const host = new CordHost(base);
205205
host.rename(path`/hello`, path`/blue`).subscribe(undefined, done.fail);
206-
host.write(path`/hello`, fileBuffer`beautiful world`).subscribe(undefined, done.fail);
206+
host.write(path`/hello`, stringToFileBuffer(`beautiful world`)).subscribe(undefined, done.fail);
207207

208208
const target = base.clone();
209209
host.commit(target).subscribe(undefined, done.fail);
@@ -226,7 +226,7 @@ describe('CordHost', () => {
226226
});
227227

228228
const host = new CordHost(base);
229-
host.write(path`/hello`, fileBuffer`beautiful world`).subscribe(undefined, done.fail);
229+
host.write(path`/hello`, stringToFileBuffer(`beautiful world`)).subscribe(undefined, done.fail);
230230

231231
const target = base.clone();
232232
host.commit(target).subscribe(undefined, done.fail);
@@ -248,8 +248,8 @@ describe('CordHost', () => {
248248
});
249249

250250
const host = new CordHost(base);
251-
host.write(path`/hello`, fileBuffer`beautiful world`).subscribe(undefined, done.fail);
252-
host.write(path`/hello`, fileBuffer`again`).subscribe(undefined, done.fail);
251+
host.write(path`/hello`, stringToFileBuffer(`beautiful world`)).subscribe(undefined, done.fail);
252+
host.write(path`/hello`, stringToFileBuffer(`again`)).subscribe(undefined, done.fail);
253253

254254
const target = base.clone();
255255
host.commit(target).subscribe(undefined, done.fail);
@@ -271,7 +271,7 @@ describe('CordHost', () => {
271271
});
272272

273273
const host = new CordHost(base);
274-
host.write(path`/hello`, fileBuffer`beautiful world`).subscribe(undefined, done.fail);
274+
host.write(path`/hello`, stringToFileBuffer(`beautiful world`)).subscribe(undefined, done.fail);
275275
host.rename(path`/hello`, path`/blue`).subscribe(undefined, done.fail);
276276

277277
const target = base.clone();
@@ -295,7 +295,7 @@ describe('CordHost', () => {
295295
});
296296

297297
const host = new CordHost(base);
298-
host.write(path`/hello`, fileBuffer`beautiful world`).subscribe(undefined, done.fail);
298+
host.write(path`/hello`, stringToFileBuffer(`beautiful world`)).subscribe(undefined, done.fail);
299299
host.delete(path`/hello`).subscribe(undefined, done.fail);
300300

301301
const target = base.clone();
@@ -316,7 +316,7 @@ describe('CordHost', () => {
316316

317317
const host = new CordHost(base);
318318
host.rename(path`/hello`, path`/blue`).subscribe(undefined, done.fail);
319-
host.write(path`/blue`, fileBuffer`beautiful world`).subscribe(undefined, done.fail);
319+
host.write(path`/blue`, stringToFileBuffer(`beautiful world`)).subscribe(undefined, done.fail);
320320

321321
const target = base.clone();
322322
host.commit(target).subscribe(undefined, done.fail);
@@ -359,7 +359,7 @@ describe('CordHost', () => {
359359

360360
const host = new CordHost(base);
361361
host.delete(path`/hello`).subscribe(undefined, done.fail);
362-
host.write(path`/hello`, fileBuffer`beautiful world`).subscribe(undefined, done.fail);
362+
host.write(path`/hello`, stringToFileBuffer(`beautiful world`)).subscribe(undefined, done.fail);
363363

364364
const target = base.clone();
365365
host.commit(target).subscribe(undefined, done.fail);
@@ -421,7 +421,7 @@ describe('CordHost', () => {
421421
});
422422

423423
const host = new CordHost(base);
424-
host.write(path`/blue`, fileBuffer`hi`).subscribe();
424+
host.write(path`/blue`, stringToFileBuffer(`hi`)).subscribe();
425425

426426
const target = new TestHost({
427427
'/blue': 'test',
@@ -442,7 +442,7 @@ describe('CordHost', () => {
442442
});
443443

444444
const host = new CordHost(base);
445-
host.write(path`/hello`, fileBuffer`hi`).subscribe();
445+
host.write(path`/hello`, stringToFileBuffer(`hi`)).subscribe();
446446

447447
const target = new TestHost({});
448448

@@ -502,7 +502,7 @@ describe('CordHost', () => {
502502

503503
const host = new CordHost(base);
504504
let error = false;
505-
host.write(path`/dir`, fileBuffer`beautiful world`).subscribe(
505+
host.write(path`/dir`, stringToFileBuffer(`beautiful world`)).subscribe(
506506
undefined,
507507
() => (error = true),
508508
() => (error = false),

0 commit comments

Comments
 (0)