Skip to content

Commit

Permalink
fix: return TextEncoder polyfill as it's not JS standard
Browse files Browse the repository at this point in the history
  • Loading branch information
stepan662 committed Nov 10, 2023
1 parent 1b4e402 commit 9ba4999
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 13 deletions.
1 change: 0 additions & 1 deletion packages/i18next/jest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export default {
preset: 'ts-jest',
testEnvironment: 'jsdom',
roots: ['src'],
setupFiles: ['../testing/setupJest.ts'],
unmockedModulePathPatterns: [
'<rootDir>/src/__testFixtures/*',
'/node_modules/*',
Expand Down
1 change: 0 additions & 1 deletion packages/ngx/projects/ngx-tolgee/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ module.exports = {
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {
prefix: '<rootDir>/../../',
}),
setupFiles: ['../../../testing/setupJest.ts'],
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
Expand Down
1 change: 0 additions & 1 deletion packages/react/jest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export default {
modulePathIgnorePatterns: ['cypress'],
transformIgnorePatterns: ['node_modules/(?!@tolgee/web)'],
roots: ['src'],
setupFiles: ['../testing/setupJest.ts'],
globals: {
'ts-jest': {
tsconfig: 'tsconfig.spec.json',
Expand Down
1 change: 0 additions & 1 deletion packages/svelte/jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ module.exports = {
'<rootDir>/.svelte-kit/build/runtime/app$1',
],
},
setupFiles: ['../testing/setupJest.ts'],
setupFilesAfterEnv: ['<rootDir>/jest-setup.ts'],
};
5 changes: 0 additions & 5 deletions packages/testing/setupJest.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/vue/jest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export default {
transform: {
'^.+\\.vue$': '@vue/vue3-jest',
},
setupFiles: ['../testing/setupJest.ts'],
testEnvironmentOptions: {
customExportConditions: ['node', 'node-addons'],
},
Expand Down
1 change: 0 additions & 1 deletion packages/web/jest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ export default {
moduleNameMapper: {
'@testFixtures/(.*)': '<rootDir>/src/__testFixtures/$1',
},
setupFiles: ['../testing/setupJest.ts'],
roots: ['src'],
};
96 changes: 96 additions & 0 deletions packages/web/src/observers/invisible/encoderPolyfill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// TextEncoder/TextDecoder polyfills for utf-8 - an implementation of TextEncoder/TextDecoder APIs
// Written in 2013 by Viktor Mukhachev <[email protected]>
// To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
// You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.

// Some important notes about the polyfill below:
// Native TextEncoder/TextDecoder implementation is overwritten
// String.prototype.codePointAt polyfill not included, as well as String.fromCodePoint
// TextEncoder.prototype.encode returns a regular array instead of Uint8Array
// No options (fatal of the TextDecoder constructor and stream of the TextDecoder.prototype.decode method) are supported.
// TextDecoder.prototype.decode does not valid byte sequences
// This is a demonstrative implementation not intended to have the best performance

// http://encoding.spec.whatwg.org/#textencoder

// http://encoding.spec.whatwg.org/#textencoder

function PTextEncoder() {}

PTextEncoder.prototype.encode = function (string: string) {
const octets = [];
const length = string.length;
let i = 0;
while (i < length) {
const codePoint = string.codePointAt(i)!;
let c = 0;
let bits = 0;
if (codePoint <= 0x0000007f) {
c = 0;
bits = 0x00;
} else if (codePoint <= 0x000007ff) {
c = 6;
bits = 0xc0;
} else if (codePoint <= 0x0000ffff) {
c = 12;
bits = 0xe0;
} else if (codePoint <= 0x001fffff) {
c = 18;
bits = 0xf0;
}
octets.push(bits | (codePoint >> c));
c -= 6;
while (c >= 0) {
octets.push(0x80 | ((codePoint >> c) & 0x3f));
c -= 6;
}
i += codePoint >= 0x10000 ? 2 : 1;
}
return octets;
};

function PTextDecoder() {}

PTextDecoder.prototype.decode = function (octets: any[]) {
let string = '';
let i = 0;
while (i < octets.length) {
let octet = octets[i];
let bytesNeeded = 0;
let codePoint = 0;
if (octet <= 0x7f) {
bytesNeeded = 0;
codePoint = octet & 0xff;
} else if (octet <= 0xdf) {
bytesNeeded = 1;
codePoint = octet & 0x1f;
} else if (octet <= 0xef) {
bytesNeeded = 2;
codePoint = octet & 0x0f;
} else if (octet <= 0xf4) {
bytesNeeded = 3;
codePoint = octet & 0x07;
}
if (octets.length - i - bytesNeeded > 0) {
let k = 0;
while (k < bytesNeeded) {
octet = octets[i + k + 1];
codePoint = (codePoint << 6) | (octet & 0x3f);
k += 1;
}
} else {
codePoint = 0xfffd;
bytesNeeded = octets.length - i;
}
string += String.fromCodePoint(codePoint);
i += bytesNeeded + 1;
}
return string;
};

export const Encoder = (typeof TextEncoder === 'undefined'
? PTextEncoder
: TextEncoder) as unknown as typeof TextEncoder;
export const Decoder = (typeof TextDecoder === 'undefined'
? PTextDecoder
: TextDecoder) as unknown as typeof TextDecoder;
6 changes: 4 additions & 2 deletions packages/web/src/observers/invisible/secret.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Encoder, Decoder } from './encoderPolyfill';

export const INVISIBLE_CHARACTERS = ['\u200C', '\u200D'];

export const INVISIBLE_REGEX = RegExp(
Expand All @@ -6,11 +8,11 @@ export const INVISIBLE_REGEX = RegExp(
);

function toBytes(text: string) {
return Array.from(new TextEncoder().encode(text));
return Array.from(new Encoder().encode(text));
}

function fromBytes(bytes: Iterable<number>) {
return new TextDecoder().decode(new Uint8Array(bytes));
return new Decoder().decode(new Uint8Array(bytes));
}

function padToWholeBytes(binary: string) {
Expand Down

0 comments on commit 9ba4999

Please sign in to comment.