From 7e4d735532e20ca288dfff02b671278289052a4d Mon Sep 17 00:00:00 2001 From: Oscar Hinton Date: Mon, 7 Oct 2024 18:00:07 +0200 Subject: [PATCH] [PM-11290] Fix safari not starting with sdk (#11438) We ran into some inconsistencies between how manifest v2 and v3 are handled in browsers. To resolve this we have a different flow for manifest v2 which dynamically imports, while v3 uses the sync importScripts. --- .../sdk/browser-sdk-client-factory.ts | 40 +++++++++++++++---- .../safari/desktop.xcodeproj/project.pbxproj | 4 ++ apps/browser/webpack.config.js | 2 + 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/apps/browser/src/platform/services/sdk/browser-sdk-client-factory.ts b/apps/browser/src/platform/services/sdk/browser-sdk-client-factory.ts index aa8bfe61c2..d3b8f36a54 100644 --- a/apps/browser/src/platform/services/sdk/browser-sdk-client-factory.ts +++ b/apps/browser/src/platform/services/sdk/browser-sdk-client-factory.ts @@ -1,6 +1,8 @@ import { SdkClientFactory } from "@bitwarden/common/platform/abstractions/sdk/sdk-client-factory"; import type { BitwardenClient } from "@bitwarden/sdk-internal"; +import { BrowserApi } from "../../browser/browser-api"; + // https://stackoverflow.com/a/47880734 const supported = (() => { try { @@ -18,14 +20,34 @@ const supported = (() => { return false; })(); -if (supported) { - // eslint-disable-next-line no-console - console.debug("WebAssembly is supported in this environment"); - import("./wasm"); -} else { - // eslint-disable-next-line no-console - console.debug("WebAssembly is not supported in this environment"); - import("./fallback"); +// Manifest v3 does not support dynamic imports in the service worker. +if (BrowserApi.isManifestVersion(3)) { + if (supported) { + // eslint-disable-next-line no-console + console.debug("WebAssembly is supported in this environment"); + import("./wasm"); + } else { + // eslint-disable-next-line no-console + console.debug("WebAssembly is not supported in this environment"); + import("./fallback"); + } +} + +// Manifest v2 expects dynamic imports to prevent timing issues. +async function load() { + if (BrowserApi.isManifestVersion(3)) { + return; + } + + if (supported) { + // eslint-disable-next-line no-console + console.debug("WebAssembly is supported in this environment"); + await import("./wasm"); + } else { + // eslint-disable-next-line no-console + console.debug("WebAssembly is not supported in this environment"); + await import("./fallback"); + } } /** @@ -37,6 +59,8 @@ export class BrowserSdkClientFactory implements SdkClientFactory { async createSdkClient( ...args: ConstructorParameters ): Promise { + await load(); + return Promise.resolve((globalThis as any).init_sdk(...args)); } } diff --git a/apps/browser/src/safari/desktop.xcodeproj/project.pbxproj b/apps/browser/src/safari/desktop.xcodeproj/project.pbxproj index 6e5bc6b66c..7642e7d185 100644 --- a/apps/browser/src/safari/desktop.xcodeproj/project.pbxproj +++ b/apps/browser/src/safari/desktop.xcodeproj/project.pbxproj @@ -8,6 +8,7 @@ /* Begin PBXBuildFile section */ 03100CAF291891F4008E14EF /* encrypt-worker.js in Resources */ = {isa = PBXBuildFile; fileRef = 03100CAE291891F4008E14EF /* encrypt-worker.js */; }; + 55BC93932CB4268A008CA4C6 /* assets in Resources */ = {isa = PBXBuildFile; fileRef = 55BC93922CB4268A008CA4C6 /* assets */; }; 55E0374D2577FA6B00979016 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55E0374C2577FA6B00979016 /* AppDelegate.swift */; }; 55E037502577FA6B00979016 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 55E0374E2577FA6B00979016 /* Main.storyboard */; }; 55E037522577FA6B00979016 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55E037512577FA6B00979016 /* ViewController.swift */; }; @@ -54,6 +55,7 @@ /* Begin PBXFileReference section */ 03100CAE291891F4008E14EF /* encrypt-worker.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; name = "encrypt-worker.js"; path = "../../../build/encrypt-worker.js"; sourceTree = ""; }; 5508DD7926051B5900A85C58 /* libswiftAppKit.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libswiftAppKit.tbd; path = usr/lib/swift/libswiftAppKit.tbd; sourceTree = SDKROOT; }; + 55BC93922CB4268A008CA4C6 /* assets */ = {isa = PBXFileReference; lastKnownFileType = folder; name = assets; path = ../../../build/assets; sourceTree = ""; }; 55E037482577FA6B00979016 /* desktop.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = desktop.app; sourceTree = BUILT_PRODUCTS_DIR; }; 55E0374B2577FA6B00979016 /* desktop.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = desktop.entitlements; sourceTree = ""; }; 55E0374C2577FA6B00979016 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; @@ -152,6 +154,7 @@ 55E0376F2577FA6F00979016 /* Resources */ = { isa = PBXGroup; children = ( + 55BC93922CB4268A008CA4C6 /* assets */, 03100CAE291891F4008E14EF /* encrypt-worker.js */, 55E037702577FA6F00979016 /* popup */, 55E037712577FA6F00979016 /* background.js */, @@ -270,6 +273,7 @@ 55E0377A2577FA6F00979016 /* background.js in Resources */, 55E037792577FA6F00979016 /* popup in Resources */, 03100CAF291891F4008E14EF /* encrypt-worker.js in Resources */, + 55BC93932CB4268A008CA4C6 /* assets in Resources */, 55E0377C2577FA6F00979016 /* notification in Resources */, 55E0377E2577FA6F00979016 /* vendor.js in Resources */, 55E0377D2577FA6F00979016 /* content in Resources */, diff --git a/apps/browser/webpack.config.js b/apps/browser/webpack.config.js index 4309defd3a..5436e51011 100644 --- a/apps/browser/webpack.config.js +++ b/apps/browser/webpack.config.js @@ -316,6 +316,8 @@ const mainConfig = { }, output: { filename: "[name].js", + chunkFilename: "assets/[name].js", + webassemblyModuleFilename: "assets/[modulehash].wasm", path: path.resolve(__dirname, "build"), clean: true, },