Send files directly between devices over the internet - no cloud storage, no size limits.
TypeScript cross-platformdata-transferdecentralizedfile-sharingfile-transferhole-punchingopen-sourcep2ppeer-to-peerprivacyprivacy-tools
fix: resolve hanging transfers from Android to Windows (#18)
André Sichelerofe97479a45e1
diff --git a/apps/mobile/src/transfer/send/views/SelectFilesView.tsx b/apps/mobile/src/transfer/send/views/SelectFilesView.tsxindex 0b88b71..f4524ec 100644--- a/apps/mobile/src/transfer/send/views/SelectFilesView.tsx+++ b/apps/mobile/src/transfer/send/views/SelectFilesView.tsx@@ -31,7 +31,7 @@ export function SelectFilesView() {const result = await DocumentPicker.getDocumentAsync({multiple: true,type: '*/*',- copyToCacheDirectory: false+ copyToCacheDirectory: true})if (result.canceled) return@@ -39,7 +39,8 @@ export function SelectFilesView() {const normalizedFiles: SelectedFile[] = result.assets.map((asset) => ({name: asset.name,path: uriToFilePath(asset.uri),- size: asset.size+ size: asset.size,+ isTemporary: true}))if (normalizedFiles.length > 0) {@@ -66,7 +67,8 @@ export function SelectFilesView() {const normalizedFiles: SelectedFile[] = result.assets.map((asset) => ({name: asset.fileName ?? asset.uri.split('/').pop() ?? 'photo',path: uriToFilePath(asset.uri),- size: asset.fileSize+ size: asset.fileSize,+ isTemporary: true}))if (normalizedFiles.length > 0) {diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex 41b60eb..7473f98 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -20,7 +20,8 @@ export type {DownloadFilesReply,DisconnectReply,DownloadFileRequest,- DownloadFileResult+ DownloadFileResult,+ ShareFileRequest} from './worklet/rpc/protocol'export type { TransferStatus } from './worklet/rpc/events'diff --git a/packages/core/src/worklet/rpc/protocol.ts b/packages/core/src/worklet/rpc/protocol.tsindex 181195c..43b3180 100644--- a/packages/core/src/worklet/rpc/protocol.ts+++ b/packages/core/src/worklet/rpc/protocol.ts@@ -26,6 +26,11 @@ export interface JoinReply {state: 'joined'}+export interface ShareFileRequest {+ path: string+ isTemporary?: boolean+}+export interface ShareFilesReply {acceptedFiles: number}@@ -71,7 +76,7 @@ export type IncomingFileOffer = FileOfferexport interface TransferRPC {host(): Promise<HostReply>join(topic: string): Promise<JoinReply>- shareFiles(paths: string[]): Promise<ShareFilesReply>+ shareFiles(files: ShareFileRequest[]): Promise<ShareFilesReply>downloadFiles(files: DownloadFileRequest[]): Promise<DownloadFilesReply>disconnect(): Promise<DisconnectReply>closePeers(): Promise<void>diff --git a/packages/core/src/worklet/transfer/orchestrator.ts b/packages/core/src/worklet/transfer/orchestrator.tsindex deacb2d..9eabe75 100644--- a/packages/core/src/worklet/transfer/orchestrator.ts+++ b/packages/core/src/worklet/transfer/orchestrator.ts@@ -18,6 +18,7 @@ import type {DownloadFilesReply,HostReply,JoinReply,+ ShareFileRequest,ShareFilesReply,TransferRPC} from '../rpc/protocol'@@ -293,8 +294,8 @@ export class TransferOrchestrator implements TransferRPC {return { state: 'joined' }}- async shareFiles(paths: string[]): Promise<ShareFilesReply> {- if (!Array.isArray(paths) || paths.length === 0) {+ async shareFiles(requests: ShareFileRequest[]): Promise<ShareFilesReply> {+ if (!Array.isArray(requests) || requests.length === 0) {throw new BadRequestError('Missing files to share')}if (this.suspended) {@@ -308,7 +309,7 @@ export class TransferOrchestrator implements TransferRPC {await this.readyPromisetry {- const { files, totalBytes, errors } = await this.stager.scanFiles(paths)+ const { files, totalBytes, errors } = await this.stager.scanFiles(requests)for (const error of errors) this.sendError(error)if (files.length === 0) {diff --git a/packages/core/src/worklet/transfer/sender.ts b/packages/core/src/worklet/transfer/sender.tsindex 2bb8e71..9bce3a5 100644--- a/packages/core/src/worklet/transfer/sender.ts+++ b/packages/core/src/worklet/transfer/sender.ts@@ -1,4 +1,5 @@import crypto from 'hypercore-crypto'+import fs from 'bare-fs'import b4a from 'b4a'import Localdrive from 'localdrive'import MirrorDrive from 'mirror-drive'@@ -12,6 +13,8 @@ export interface ScannedFile {sourcePath: stringsourceDrive: Localdrivesize: number+ isTemporary?: boolean+ alreadyStaged?: boolean}export interface ScanResult {@@ -47,14 +50,15 @@ export class TransferSender {return b4a.toString(this.drive.key, 'hex')}- async scanFiles(paths: string[]): Promise<ScanResult> {+ async scanFiles(requests: { path: string; isTemporary?: boolean }[]): Promise<ScanResult> {const files: ScannedFile[] = []const errors: string[] = []let totalBytes = 0const driveByDir = new Map<string, Localdrive>()- for (const path of paths) {+ for (const req of requests) {+ const path = req.pathconst fileName = getFileName(path)const sourcePath = `/${fileName}`const dir = getDirname(path)@@ -68,13 +72,35 @@ export class TransferSender {const entry = await sourceDrive.entry(sourcePath)if (!entry?.value?.blob) {+ const existingEntry = await this.drive.entry(sourcePath)+ if (existingEntry?.value?.blob) {+ const size = existingEntry.value.blob.byteLength+ totalBytes += size+ files.push({+ fileName,+ inputPath: path,+ sourcePath,+ sourceDrive,+ size,+ isTemporary: req.isTemporary,+ alreadyStaged: true+ })+ continue+ }errors.push(`Could not read file: ${fileName}`)continue}const size = entry.value.blob.byteLengthtotalBytes += size- files.push({ fileName, inputPath: path, sourcePath, sourceDrive, size })+ files.push({+ fileName,+ inputPath: path,+ sourcePath,+ sourceDrive,+ size,+ isTemporary: req.isTemporary+ })}return { files, totalBytes, errors }@@ -104,7 +130,12 @@ export class TransferSender {// Per-file granularity — MirrorDrive in flight is not abortable.if (signal?.aborted) throw new AbortError()onStaging(file)- await this.importToDrive(file.sourceDrive, file.sourcePath)+ if (!file.alreadyStaged) {+ await this.importToDrive(file.sourceDrive, file.sourcePath)+ if (file.isTemporary) {+ await this.tryDeleteFile(file.inputPath)+ }+ }offers.push({id: createFileId(),transferId,@@ -125,4 +156,12 @@ export class TransferSender {})await mirror.done()}++ private async tryDeleteFile(filePath: string): Promise<void> {+ try {+ await fs.promises.unlink(filePath)+ } catch (err) {+ console.warn(`TransferSender: failed to delete temporary file ${filePath}`, err)+ }+ }}diff --git a/packages/domain/src/send/draftTypes.ts b/packages/domain/src/send/draftTypes.tsindex a281f97..363b5b4 100644--- a/packages/domain/src/send/draftTypes.ts+++ b/packages/domain/src/send/draftTypes.ts@@ -2,6 +2,7 @@ export interface SelectedFile {name: stringpath: stringsize?: number+ isTemporary?: boolean}export type SenderUploadStatus = 'waiting' | 'uploading' | 'completed'diff --git a/packages/domain/src/transfer/commands.ts b/packages/domain/src/transfer/commands.tsindex 20b900e..32b02dc 100644--- a/packages/domain/src/transfer/commands.ts+++ b/packages/domain/src/transfer/commands.ts@@ -6,6 +6,7 @@ import type {DownloadFileRequest,DownloadFilesReply,JoinReply,+ ShareFileRequest,ShareFilesReply} from '@altersend/core'@@ -48,10 +49,10 @@ export const joinSession = async (topic: string): Promise<JoinReply> => {}}-export const shareFiles = async (paths: string[]): Promise<ShareFilesReply> => {+export const shareFiles = async (files: ShareFileRequest[]): Promise<ShareFilesReply> => {dispatchToTransferStore({ type: 'share_requested' })try {- return await getTransferApi().worker.shareFiles(paths)+ return await getTransferApi().worker.shareFiles(files)} catch (error) {reportError('shareFiles', error)dispatchToTransferStore({ type: 'role_changed', role: null })@@ -99,7 +100,10 @@ export const clearSenderFlow = (): void => {export const continueShare = async (files: SelectedFile[]): Promise<void> => {if (files.length === 0) return- const filePaths = files.map((file) => file.path)+ const fileRequests: ShareFileRequest[] = files.map((file) => ({+ path: file.path,+ isTemporary: file.isTemporary+ }))dispatchToTransferStore({type: 'init_upload_items',items: createInitialUploadItems(files)@@ -108,7 +112,7 @@ export const continueShare = async (files: SelectedFile[]): Promise<void> => {try {await startSendSession()- await shareFiles(filePaths)+ await shareFiles(fileRequests)dispatchToTransferStore({ type: 'complete_all_uploads' })dispatchToTransferStore({ type: 'set_draft_phase', phase: 'ready' })} catch (error) {