From 3925860a69f1a15475d39db936f9656c9a545730 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Thu, 18 Apr 2024 16:25:25 -0600 Subject: [PATCH 01/10] Force service worker-safe crypto when operating under a service worker --- src/crypto/crypto.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/crypto/crypto.ts b/src/crypto/crypto.ts index c78970708f2..191ec5f2647 100644 --- a/src/crypto/crypto.ts +++ b/src/crypto/crypto.ts @@ -40,6 +40,13 @@ if (!TextEncoder) { } /* eslint-enable @typescript-eslint/no-var-requires */ +// @ts-expect-error - we don't have service worker types. See 'fetch' listener in Element Web service worker. +if (typeof self["skipWaiting"] === "function") { + crypto = self.crypto; + subtleCrypto = self.crypto.subtle ?? self.crypto.webkitSubtle; + TextEncoder = self.TextEncoder; +} + export function setCrypto(_crypto: Crypto): void { crypto = _crypto; subtleCrypto = _crypto.subtle ?? _crypto.webkitSubtle; From 529fcc369a6b6f6ab77f004b2952c35f5f2cd1cf Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Thu, 18 Apr 2024 16:29:26 -0600 Subject: [PATCH 02/10] Fix tests/unsafe execution --- src/crypto/crypto.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crypto/crypto.ts b/src/crypto/crypto.ts index 191ec5f2647..ee1142f2e48 100644 --- a/src/crypto/crypto.ts +++ b/src/crypto/crypto.ts @@ -41,7 +41,7 @@ if (!TextEncoder) { /* eslint-enable @typescript-eslint/no-var-requires */ // @ts-expect-error - we don't have service worker types. See 'fetch' listener in Element Web service worker. -if (typeof self["skipWaiting"] === "function") { +if (typeof self?.["skipWaiting"] === "function") { crypto = self.crypto; subtleCrypto = self.crypto.subtle ?? self.crypto.webkitSubtle; TextEncoder = self.TextEncoder; From 895901e677ddb7cf44fef1ce4d045567d6898bb9 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Thu, 18 Apr 2024 16:50:21 -0600 Subject: [PATCH 03/10] Further fix tests? --- src/crypto/crypto.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/crypto/crypto.ts b/src/crypto/crypto.ts index ee1142f2e48..3bc483fb0dd 100644 --- a/src/crypto/crypto.ts +++ b/src/crypto/crypto.ts @@ -41,10 +41,10 @@ if (!TextEncoder) { /* eslint-enable @typescript-eslint/no-var-requires */ // @ts-expect-error - we don't have service worker types. See 'fetch' listener in Element Web service worker. -if (typeof self?.["skipWaiting"] === "function") { - crypto = self.crypto; - subtleCrypto = self.crypto.subtle ?? self.crypto.webkitSubtle; - TextEncoder = self.TextEncoder; +if (typeof globalThis?.["skipWaiting"] === "function") { + crypto = globalThis.crypto; + subtleCrypto = globalThis.crypto.subtle ?? globalThis.crypto.webkitSubtle; + TextEncoder = globalThis.TextEncoder; } export function setCrypto(_crypto: Crypto): void { From 0a5492579860ea218842f0b6c435ddc7d64142e2 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Thu, 18 Apr 2024 17:04:09 -0600 Subject: [PATCH 04/10] Docs would probably be good --- src/crypto/crypto.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/crypto/crypto.ts b/src/crypto/crypto.ts index 3bc483fb0dd..118ccf29b5a 100644 --- a/src/crypto/crypto.ts +++ b/src/crypto/crypto.ts @@ -40,6 +40,11 @@ if (!TextEncoder) { } /* eslint-enable @typescript-eslint/no-var-requires */ +// Service workers don't have a useful `window` instance to pull these details off of, and in at least Chrome when +// `window.$thing` is accessed, `$thing` will be non-null but complain about being undefined when accessed further. +// To avoid spurious errors, we just override the crypto bits explicitly. +// +// Note: `skipWaiting` is a function exclusive to service workers. // @ts-expect-error - we don't have service worker types. See 'fetch' listener in Element Web service worker. if (typeof globalThis?.["skipWaiting"] === "function") { crypto = globalThis.crypto; From dbc297a46c37453df8c772de065da22eabb22b02 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Thu, 18 Apr 2024 17:14:04 -0600 Subject: [PATCH 05/10] Define a type guard function https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards --- src/crypto/crypto.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/crypto/crypto.ts b/src/crypto/crypto.ts index 118ccf29b5a..a90144de081 100644 --- a/src/crypto/crypto.ts +++ b/src/crypto/crypto.ts @@ -43,15 +43,18 @@ if (!TextEncoder) { // Service workers don't have a useful `window` instance to pull these details off of, and in at least Chrome when // `window.$thing` is accessed, `$thing` will be non-null but complain about being undefined when accessed further. // To avoid spurious errors, we just override the crypto bits explicitly. -// -// Note: `skipWaiting` is a function exclusive to service workers. -// @ts-expect-error - we don't have service worker types. See 'fetch' listener in Element Web service worker. -if (typeof globalThis?.["skipWaiting"] === "function") { +if (isServiceWorker(globalThis)) { crypto = globalThis.crypto; subtleCrypto = globalThis.crypto.subtle ?? globalThis.crypto.webkitSubtle; TextEncoder = globalThis.TextEncoder; } +// Window is a close enough approximation for what we need it for. +function isServiceWorker(globalThis: any): globalThis is typeof Window { + // Note: `skipWaiting` is a function exclusive to service workers. + return typeof globalThis["skipWaiting"] === "function"; +} + export function setCrypto(_crypto: Crypto): void { crypto = _crypto; subtleCrypto = _crypto.subtle ?? _crypto.webkitSubtle; From db46bcf1db4b94fbc7e0c97a20d5d800fcb2768b Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Thu, 18 Apr 2024 17:17:14 -0600 Subject: [PATCH 06/10] Use `@types` repo --- package.json | 9 +++++---- src/crypto/crypto.ts | 2 +- tsconfig.json | 3 ++- yarn.lock | 36 ++++++++---------------------------- 4 files changed, 16 insertions(+), 34 deletions(-) diff --git a/package.json b/package.json index 7d263f4ade5..d108b9dbe65 100644 --- a/package.json +++ b/package.json @@ -83,6 +83,7 @@ "@babel/preset-typescript": "^7.12.7", "@casualbot/jest-sonar-reporter": "2.2.7", "@matrix-org/olm": "3.2.15", + "@peculiar/webcrypto": "^1.4.5", "@types/bs58": "^4.0.1", "@types/content-type": "^1.1.5", "@types/debug": "^4.1.7", @@ -90,6 +91,7 @@ "@types/jest": "^29.0.0", "@types/node": "18", "@types/sdp-transform": "^2.4.5", + "@types/serviceworker": "^0.0.85", "@types/uuid": "9", "@typescript-eslint/eslint-plugin": "^7.0.0", "@typescript-eslint/parser": "^7.0.0", @@ -114,8 +116,10 @@ "jest-environment-jsdom": "^29.0.0", "jest-localstorage-mock": "^2.4.6", "jest-mock": "^29.0.0", + "knip": "^5.0.0", "lint-staged": "^15.0.2", "matrix-mock-request": "^2.5.0", + "node-fetch": "^2.7.0", "prettier": "3.2.5", "rimraf": "^5.0.0", "ts-node": "^10.9.2", @@ -123,10 +127,7 @@ "typedoc-plugin-coverage": "^3.0.0", "typedoc-plugin-mdn-links": "^3.0.3", "typedoc-plugin-missing-exports": "^2.0.0", - "typescript": "^5.3.3", - "node-fetch": "^2.7.0", - "knip": "^5.0.0", - "@peculiar/webcrypto": "^1.4.5" + "typescript": "^5.3.3" }, "@casualbot/jest-sonar-reporter": { "outputDirectory": "coverage", diff --git a/src/crypto/crypto.ts b/src/crypto/crypto.ts index a90144de081..6bc4cf374b2 100644 --- a/src/crypto/crypto.ts +++ b/src/crypto/crypto.ts @@ -50,7 +50,7 @@ if (isServiceWorker(globalThis)) { } // Window is a close enough approximation for what we need it for. -function isServiceWorker(globalThis: any): globalThis is typeof Window { +function isServiceWorker(globalThis: any): globalThis is ServiceWorker { // Note: `skipWaiting` is a function exclusive to service workers. return typeof globalThis["skipWaiting"] === "function"; } diff --git a/tsconfig.json b/tsconfig.json index c1c9d48b8e9..07c039f8c45 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,7 +8,8 @@ "noUnusedLocals": true, "noEmit": true, "declaration": true, - "strict": true + "strict": true, + "types": ["serviceworker"] }, "include": ["./src/**/*.ts", "./spec/**/*.ts"] } diff --git a/yarn.lock b/yarn.lock index 5a3095277c6..f9450ef754b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2234,6 +2234,11 @@ resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.5.8.tgz#8268a8c57a3e4abd25c165ecd36237db7948a55e" integrity sha512-I8EUhyrgfLrcTkzV3TSsGyl1tSuPrEDzr0yd5m90UgNxQkyDXULk3b6MlQqTCpZpNtWe1K0hzclnZkTcLBe2UQ== +"@types/serviceworker@^0.0.85": + version "0.0.85" + resolved "https://registry.yarnpkg.com/@types/serviceworker/-/serviceworker-0.0.85.tgz#b26fac0043a80c2792cd396d47463c2156496aea" + integrity sha512-jBhFui72jyO0Tpsq/8AvL7GRJKiuUxdHPD9rrSRhf2SSElCn61yTyU2G133IlBVvBumCH4T5FDjmjbAG7MU9tg== + "@types/stack-utils@^2.0.0": version "2.0.3" resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.3.tgz#6209321eb2c1712a7e7466422b8cb1fc0d9dd5d8" @@ -6543,16 +6548,7 @@ string-length@^4.0.1: char-regex "^1.0.2" strip-ansi "^6.0.0" -"string-width-cjs@npm:string-width@^4.2.0": - version "4.2.3" - resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" - integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== - dependencies: - emoji-regex "^8.0.0" - is-fullwidth-code-point "^3.0.0" - strip-ansi "^6.0.1" - -string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3: +"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3: version "4.2.3" resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== @@ -6618,14 +6614,7 @@ string_decoder@^1.1.1: dependencies: safe-buffer "~5.2.0" -"strip-ansi-cjs@npm:strip-ansi@^6.0.1": - version "6.0.1" - resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" - integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== - dependencies: - ansi-regex "^5.0.1" - -strip-ansi@^6.0.0, strip-ansi@^6.0.1: +"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1: version "6.0.1" resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== @@ -7231,16 +7220,7 @@ which@^4.0.0: dependencies: isexe "^3.1.1" -"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0": - version "7.0.0" - resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" - integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q== - dependencies: - ansi-styles "^4.0.0" - string-width "^4.1.0" - strip-ansi "^6.0.0" - -wrap-ansi@^7.0.0: +"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0: version "7.0.0" resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q== From 0cc6fbbc48c6a23c7ed17400471d8e90ceda2621 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Thu, 18 Apr 2024 17:21:14 -0600 Subject: [PATCH 07/10] Maybe don't modify tsconfig, I guess --- tsconfig.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tsconfig.json b/tsconfig.json index 07c039f8c45..c1c9d48b8e9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,8 +8,7 @@ "noUnusedLocals": true, "noEmit": true, "declaration": true, - "strict": true, - "types": ["serviceworker"] + "strict": true }, "include": ["./src/**/*.ts", "./spec/**/*.ts"] } From 361544e4eb6008cc35774305890b9ff03d84be3d Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Thu, 18 Apr 2024 17:22:34 -0600 Subject: [PATCH 08/10] Revert "Use `@types` repo" This reverts commit db46bcf1db4b94fbc7e0c97a20d5d800fcb2768b. --- package.json | 9 ++++----- src/crypto/crypto.ts | 2 +- yarn.lock | 36 ++++++++++++++++++++++++++++-------- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index d108b9dbe65..7d263f4ade5 100644 --- a/package.json +++ b/package.json @@ -83,7 +83,6 @@ "@babel/preset-typescript": "^7.12.7", "@casualbot/jest-sonar-reporter": "2.2.7", "@matrix-org/olm": "3.2.15", - "@peculiar/webcrypto": "^1.4.5", "@types/bs58": "^4.0.1", "@types/content-type": "^1.1.5", "@types/debug": "^4.1.7", @@ -91,7 +90,6 @@ "@types/jest": "^29.0.0", "@types/node": "18", "@types/sdp-transform": "^2.4.5", - "@types/serviceworker": "^0.0.85", "@types/uuid": "9", "@typescript-eslint/eslint-plugin": "^7.0.0", "@typescript-eslint/parser": "^7.0.0", @@ -116,10 +114,8 @@ "jest-environment-jsdom": "^29.0.0", "jest-localstorage-mock": "^2.4.6", "jest-mock": "^29.0.0", - "knip": "^5.0.0", "lint-staged": "^15.0.2", "matrix-mock-request": "^2.5.0", - "node-fetch": "^2.7.0", "prettier": "3.2.5", "rimraf": "^5.0.0", "ts-node": "^10.9.2", @@ -127,7 +123,10 @@ "typedoc-plugin-coverage": "^3.0.0", "typedoc-plugin-mdn-links": "^3.0.3", "typedoc-plugin-missing-exports": "^2.0.0", - "typescript": "^5.3.3" + "typescript": "^5.3.3", + "node-fetch": "^2.7.0", + "knip": "^5.0.0", + "@peculiar/webcrypto": "^1.4.5" }, "@casualbot/jest-sonar-reporter": { "outputDirectory": "coverage", diff --git a/src/crypto/crypto.ts b/src/crypto/crypto.ts index 6bc4cf374b2..a90144de081 100644 --- a/src/crypto/crypto.ts +++ b/src/crypto/crypto.ts @@ -50,7 +50,7 @@ if (isServiceWorker(globalThis)) { } // Window is a close enough approximation for what we need it for. -function isServiceWorker(globalThis: any): globalThis is ServiceWorker { +function isServiceWorker(globalThis: any): globalThis is typeof Window { // Note: `skipWaiting` is a function exclusive to service workers. return typeof globalThis["skipWaiting"] === "function"; } diff --git a/yarn.lock b/yarn.lock index f9450ef754b..5a3095277c6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2234,11 +2234,6 @@ resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.5.8.tgz#8268a8c57a3e4abd25c165ecd36237db7948a55e" integrity sha512-I8EUhyrgfLrcTkzV3TSsGyl1tSuPrEDzr0yd5m90UgNxQkyDXULk3b6MlQqTCpZpNtWe1K0hzclnZkTcLBe2UQ== -"@types/serviceworker@^0.0.85": - version "0.0.85" - resolved "https://registry.yarnpkg.com/@types/serviceworker/-/serviceworker-0.0.85.tgz#b26fac0043a80c2792cd396d47463c2156496aea" - integrity sha512-jBhFui72jyO0Tpsq/8AvL7GRJKiuUxdHPD9rrSRhf2SSElCn61yTyU2G133IlBVvBumCH4T5FDjmjbAG7MU9tg== - "@types/stack-utils@^2.0.0": version "2.0.3" resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.3.tgz#6209321eb2c1712a7e7466422b8cb1fc0d9dd5d8" @@ -6548,7 +6543,16 @@ string-length@^4.0.1: char-regex "^1.0.2" strip-ansi "^6.0.0" -"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3: +"string-width-cjs@npm:string-width@^4.2.0": + version "4.2.3" + resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" + integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== + dependencies: + emoji-regex "^8.0.0" + is-fullwidth-code-point "^3.0.0" + strip-ansi "^6.0.1" + +string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3: version "4.2.3" resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== @@ -6614,7 +6618,14 @@ string_decoder@^1.1.1: dependencies: safe-buffer "~5.2.0" -"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1: +"strip-ansi-cjs@npm:strip-ansi@^6.0.1": + version "6.0.1" + resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" + integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== + dependencies: + ansi-regex "^5.0.1" + +strip-ansi@^6.0.0, strip-ansi@^6.0.1: version "6.0.1" resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== @@ -7220,7 +7231,16 @@ which@^4.0.0: dependencies: isexe "^3.1.1" -"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0: +"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0": + version "7.0.0" + resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" + integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q== + dependencies: + ansi-styles "^4.0.0" + string-width "^4.1.0" + strip-ansi "^6.0.0" + +wrap-ansi@^7.0.0: version "7.0.0" resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q== From 007bb54cb7f28cff59a3aaad30a3642d422af28d Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Thu, 18 Apr 2024 17:22:58 -0600 Subject: [PATCH 09/10] Use a different type for Window --- src/crypto/crypto.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crypto/crypto.ts b/src/crypto/crypto.ts index a90144de081..0e615746b4e 100644 --- a/src/crypto/crypto.ts +++ b/src/crypto/crypto.ts @@ -50,7 +50,7 @@ if (isServiceWorker(globalThis)) { } // Window is a close enough approximation for what we need it for. -function isServiceWorker(globalThis: any): globalThis is typeof Window { +function isServiceWorker(globalThis: any): globalThis is Window { // Note: `skipWaiting` is a function exclusive to service workers. return typeof globalThis["skipWaiting"] === "function"; } From 2cfadba7b1dd8e37d9dc1f91745f70db27ef25e5 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Mon, 22 Apr 2024 12:05:25 -0600 Subject: [PATCH 10/10] Simplify the crypto accessors --- src/crypto/crypto.ts | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/src/crypto/crypto.ts b/src/crypto/crypto.ts index 0e615746b4e..170dabb0937 100644 --- a/src/crypto/crypto.ts +++ b/src/crypto/crypto.ts @@ -16,9 +16,9 @@ limitations under the License. import { logger } from "../logger"; -export let crypto = globalThis.window?.crypto; -export let subtleCrypto = globalThis.window?.crypto?.subtle ?? global.window?.crypto?.webkitSubtle; -export let TextEncoder = globalThis.window?.TextEncoder; +export let crypto = globalThis.crypto; +export let subtleCrypto = crypto?.subtle ?? crypto?.webkitSubtle; // TODO: Stop using webkitSubtle fallback +export let TextEncoder = globalThis.TextEncoder; /* eslint-disable @typescript-eslint/no-var-requires */ if (!crypto) { @@ -40,21 +40,6 @@ if (!TextEncoder) { } /* eslint-enable @typescript-eslint/no-var-requires */ -// Service workers don't have a useful `window` instance to pull these details off of, and in at least Chrome when -// `window.$thing` is accessed, `$thing` will be non-null but complain about being undefined when accessed further. -// To avoid spurious errors, we just override the crypto bits explicitly. -if (isServiceWorker(globalThis)) { - crypto = globalThis.crypto; - subtleCrypto = globalThis.crypto.subtle ?? globalThis.crypto.webkitSubtle; - TextEncoder = globalThis.TextEncoder; -} - -// Window is a close enough approximation for what we need it for. -function isServiceWorker(globalThis: any): globalThis is Window { - // Note: `skipWaiting` is a function exclusive to service workers. - return typeof globalThis["skipWaiting"] === "function"; -} - export function setCrypto(_crypto: Crypto): void { crypto = _crypto; subtleCrypto = _crypto.subtle ?? _crypto.webkitSubtle;