refactor(compiler): use native UTF-8 text encoding when calculating i18n IDs (#48256)

Instead of relying on a custom implementation of a UTF-8 text encoder that needs to
be maintained by the Angular team, the i18n message digesting process now uses the
native `TextEncoder` class. This class is available for use in all browsers and Node.js
versions supported by the Angular framework (https://caniuse.com/textencoder). The
custom encoder is still used by the compiler's source map generator and cannot yet be
fully removed.

PR Close #48256
This commit is contained in:
Charles Lyding
2022-11-23 15:33:29 -05:00
committed by Andrew Kushnir
parent 27eaded62d
commit 0827091d49
+12 -5
View File
@@ -6,11 +6,16 @@
* found in the LICENSE file at https://angular.io/license
*/
import {Byte, newArray, utf8Encode} from '../util';
import {BigIntExponentiation} from './big_integer';
import {Byte} from '../util';
import {BigIntExponentiation} from './big_integer';
import * as i18n from './i18n_ast';
/**
* A lazily created TextEncoder instance for converting strings into UTF-8 bytes
*/
let textEncoder: TextEncoder|undefined;
/**
* Return the message id or compute it using the XLIFF1 digest.
*/
@@ -109,11 +114,12 @@ class _SerializerIgnoreIcuExpVisitor extends _SerializerVisitor {
* DO NOT USE IT IN A SECURITY SENSITIVE CONTEXT.
*/
export function sha1(str: string): string {
const utf8 = utf8Encode(str);
textEncoder ??= new TextEncoder();
const utf8 = [...textEncoder.encode(str)];
const words32 = bytesToWords32(utf8, Endian.Big);
const len = utf8.length * 8;
const w = newArray(80);
const w = new Uint32Array(80);
let a = 0x67452301, b = 0xefcdab89, c = 0x98badcfe, d = 0x10325476, e = 0xc3d2e1f0;
words32[len >> 5] |= 0x80 << (24 - len % 32);
@@ -174,7 +180,8 @@ function fk(index: number, b: number, c: number, d: number): [number, number] {
* https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/GoogleJsMessageIdGenerator.java
*/
export function fingerprint(str: string): [number, number] {
const utf8 = utf8Encode(str);
textEncoder ??= new TextEncoder();
const utf8 = [...textEncoder.encode(str)];
let hi = hash32(utf8, 0);
let lo = hash32(utf8, 102072);