Skip to content

Commit

Permalink
refactor(backup): replace ByteArray with Uint8Array for JS
Browse files Browse the repository at this point in the history
Ensure compatibility with JavaScript by replacing `ByteArray` with `Uint8Array` in key backup export/import functions. Updated type handling across related classes and methods to support the new standard, maintaining seamless integration with the web platform.
  • Loading branch information
vitorhugods committed Feb 7, 2025
1 parent 20791fd commit acb89e4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
*/
package com.wire.backup.dump

import org.khronos.webgl.Uint8Array

@JsExport
public sealed class BackupExportResult {
public class Success(public val bytes: ByteArray) : BackupExportResult()
public class Success(
public val bytes: Uint8Array
) : BackupExportResult()
public sealed class Failure(public val message: String) : BackupExportResult() {
/**
* Represents an I/O error that occurs during an export process.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public actual class MPBackupExporter(
when (val result = finalize(password, output)) {
is ExportResult.Failure.IOError -> BackupExportResult.Failure.IOError(result.message)
is ExportResult.Failure.ZipError -> BackupExportResult.Failure.ZipError(result.message)
ExportResult.Success -> BackupExportResult.Success(output.readByteArray())
ExportResult.Success -> BackupExportResult.Success(output.readByteArray().toUByteArray().toUInt8Array())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ import kotlin.js.Promise
public actual class MPBackupImporter : CommonMPBackupImporter() {
private val inMemoryUnencryptedBuffer = Buffer()

public fun peekFileData(data: ByteArray): Promise<BackupPeekResult> = GlobalScope.promise {
public fun peekFileData(data: Uint8Array): Promise<BackupPeekResult> = GlobalScope.promise {
val buffer = Buffer()
buffer.write(data)
buffer.write(data.toUByteArray().toByteArray())
peekBackup(buffer)
}

public fun importFromFileData(data: ByteArray, passphrase: String?): Promise<BackupImportResult> = GlobalScope.promise {
public fun importFromFileData(data: Uint8Array, passphrase: String?): Promise<BackupImportResult> = GlobalScope.promise {
val buffer = Buffer()
buffer.write(data)
buffer.write(data.toUByteArray().toByteArray())
importBackup(buffer, passphrase)
}

Expand Down
3 changes: 2 additions & 1 deletion samples/src/jsMain/kotlin/samples/backup/BackupSamplesJs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.wire.backup.ingest.BackupPeekResult
import com.wire.backup.ingest.MPBackupImporter
import com.wire.backup.ingest.isCreatedBySameUser
import kotlinx.coroutines.await
import org.khronos.webgl.Uint8Array

object BackupSamplesJs : BackupSamples() {

Expand Down Expand Up @@ -51,7 +52,7 @@ object BackupSamplesJs : BackupSamples() {
println("Backup created file. Raw binary data: $fileData")
}

suspend fun peekBackup(data: ByteArray) {
suspend fun peekBackup(data: Uint8Array) {
// Peek into backup file
val importer = MPBackupImporter()

Expand Down

0 comments on commit acb89e4

Please sign in to comment.