Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/

@file:Suppress("unused")

package com.wire.kalium.cryptography

@JsModule("fake-indexeddb/auto")
@JsNonModule
private external val fakeIndexedDbAuto: dynamic

// Force module side effects so js tests expose indexedDB in the Node-based runner.
private val indexedDbPolyfill = fakeIndexedDbAuto
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,31 @@
*/
package com.wire.kalium.cryptography

// CoreCrypto web persists its own state in IndexedDB. These filesystem helpers are only
// relevant for native file-based storage/migration paths and should stay inert on JS.
internal actual fun createDirectory(path: String): Boolean {
TODO("Not yet implemented")
logJsFilesystemShimUsageOnce("createDirectory", path)
return true
}

internal actual fun fileExists(path: String): Boolean {
TODO("Not yet implemented")
logJsFilesystemShimUsageOnce("fileExists", path)
return false
}

internal actual fun deleteFile(path: String): Boolean {
TODO("Not yet implemented")
logJsFilesystemShimUsageOnce("deleteFile", path)
return false
}

private var hasLoggedJsFilesystemShimUsage = false

private fun logJsFilesystemShimUsageOnce(operation: String, path: String) {
if (hasLoggedJsFilesystemShimUsage) return

hasLoggedJsFilesystemShimUsage = true
kaliumLogger.w(
"JS filesystem shim invoked via '$operation' for path '$path'. " +
"CoreCrypto web uses IndexedDB-backed persistence, so file-based helpers stay inert on JS."
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ actual object FileUtil {
return File(path).deleteRecursively()
}

actual suspend fun deletePersistentDirectory(path: String): Boolean = deleteDirectory(path)

actual fun isDirectoryNonEmpty(path: String): Boolean {
return File(path).listFiles()?.isNotEmpty() ?: false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ actual object FileUtil {
return NSFileManager.defaultManager.removeItemAtPath(path, error.ptr)
}

actual suspend fun deletePersistentDirectory(path: String): Boolean = deleteDirectory(path)

actual fun isDirectoryNonEmpty(path: String): Boolean = memScoped {
val error = alloc<ObjCObjectVar<NSError?>>()
NSFileManager.defaultManager.contentsOfDirectoryAtPath(path, error.ptr)?.isNotEmpty() ?: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ expect object FileUtil {

fun deleteDirectory(path: String): Boolean

suspend fun deletePersistentDirectory(path: String): Boolean

fun isDirectoryNonEmpty(path: String): Boolean
}
40 changes: 37 additions & 3 deletions core/util/src/jsMain/kotlin/com.wire.kalium.util/FileUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,50 @@

package com.wire.kalium.util

import kotlinx.coroutines.await
import kotlin.js.Promise

actual object FileUtil {
actual fun mkDirs(path: String): Boolean {
TODO("Not yet implemented")
// Web targets do not expose a host filesystem here; crypto persistence is handled elsewhere.
return path.isNotBlank()
}

actual fun deleteDirectory(path: String): Boolean {
TODO("Not yet implemented")
return path.isNotBlank()
}

actual suspend fun deletePersistentDirectory(path: String): Boolean {
val indexedDb = js("window.indexedDB")
return when {
path.isBlank() -> false
indexedDb == null -> false
js("typeof indexedDb.databases !== 'function'") as Boolean -> deleteIndexedDb(indexedDb, path).await()
else -> {
val databases = indexedDb.databases().unsafeCast<Promise<Array<dynamic>>>().await()
val matchingNames = databases
.mapNotNull { it?.name as? String }
.filter { name -> name == path || name.startsWith("$path/") }

matchingNames.isEmpty() || matchingNames.map { deleteIndexedDb(indexedDb, it).await() }.all { it }
}
}
}

actual fun isDirectoryNonEmpty(path: String): Boolean {
TODO("Not yet implemented")
return false
}
}

private fun deleteIndexedDb(indexedDb: dynamic, name: String): Promise<Boolean> = Promise { resolve, _ ->
val request = indexedDb.deleteDatabase(name)
request.onsuccess = {
resolve(true)
}
request.onerror = {
resolve(false)
}
request.onblocked = {
resolve(false)
}
}
2 changes: 2 additions & 0 deletions core/util/src/jvmMain/kotlin/com/wire/kalium/util/FileUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ actual object FileUtil {
return File(path).deleteRecursively()
}

actual suspend fun deletePersistentDirectory(path: String): Boolean = deleteDirectory(path)

actual fun isDirectoryNonEmpty(path: String): Boolean {
return File(path).listFiles()?.isNotEmpty() ?: false
}
Expand Down
Loading