capacitor-background-transfer 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CapacitorBackgroundTransfer.podspec +19 -0
- package/LICENSE +21 -0
- package/Package.swift +31 -0
- package/README.md +552 -2
- package/android/build.gradle +71 -0
- package/android/src/main/AndroidManifest.xml +20 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/AzureBlockUploader.java +148 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/BackgroundTransferPlugin.java +279 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/Downloader.java +244 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/Http.java +238 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/JobRuns.java +40 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/Notifications.java +54 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/Parts.java +25 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/PartsXml.java +199 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/Pause.java +28 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/Progress.java +13 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/PutUploader.java +47 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/Runner.java +369 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/S3MultipartUploader.java +143 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/Scheduler.java +144 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/TransferException.java +65 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/TransferJobService.java +115 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/TransferPaths.java +129 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/TransferRecord.java +163 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/TransferStore.java +121 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/TransferWorker.java +85 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/UploadAdapter.java +11 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/UploadAdapters.java +89 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/UploadOptionsCheck.java +104 -0
- package/android/src/main/java/io/github/sandman92/backgroundtransfer/Uploader.java +225 -0
- package/android/src/main/res/.gitkeep +0 -0
- package/dist/docs.json +527 -0
- package/dist/esm/definitions.d.ts +277 -0
- package/dist/esm/definitions.js +24 -0
- package/dist/esm/definitions.js.map +1 -0
- package/dist/esm/index.d.ts +10 -0
- package/dist/esm/index.js +26 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/validate.d.ts +18 -0
- package/dist/esm/validate.js +113 -0
- package/dist/esm/validate.js.map +1 -0
- package/dist/esm/web.d.ts +17 -0
- package/dist/esm/web.js +27 -0
- package/dist/esm/web.js.map +1 -0
- package/dist/plugin.cjs.js +200 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/plugin.js +203 -0
- package/dist/plugin.js.map +1 -0
- package/ios/Sources/BackgroundTransferCore/Files.swift +166 -0
- package/ios/Sources/BackgroundTransferCore/PartsUpload.swift +327 -0
- package/ios/Sources/BackgroundTransferCore/PartsXML.swift +137 -0
- package/ios/Sources/BackgroundTransferCore/TransferPaths.swift +103 -0
- package/ios/Sources/BackgroundTransferCore/TransferRecord.swift +141 -0
- package/ios/Sources/BackgroundTransferCore/TransferStore.swift +69 -0
- package/ios/Sources/BackgroundTransferCore/UploadOptionsCheck.swift +90 -0
- package/ios/Sources/BackgroundTransferCore/UploadOutcome.swift +96 -0
- package/ios/Sources/BackgroundTransferCore/UploadProtocol.swift +30 -0
- package/ios/Sources/BackgroundTransferPlugin/BackgroundTransferPlugin.swift +168 -0
- package/ios/Sources/BackgroundTransferPlugin/TransferManager.swift +1074 -0
- package/package.json +97 -4
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = 'CapacitorBackgroundTransfer'
|
|
7
|
+
# The same module name as the Swift package's target, so `import BackgroundTransferPlugin` works with either.
|
|
8
|
+
s.module_name = 'BackgroundTransferPlugin'
|
|
9
|
+
s.version = package['version']
|
|
10
|
+
s.summary = package['description']
|
|
11
|
+
s.license = package['license']
|
|
12
|
+
s.homepage = package['repository']['url']
|
|
13
|
+
s.author = package['author']
|
|
14
|
+
s.source = { :git => package['repository']['url'], :tag => s.version.to_s }
|
|
15
|
+
s.source_files = 'ios/Sources/**/*.{swift,h,m,c,cc,mm,cpp}'
|
|
16
|
+
s.ios.deployment_target = '15.0'
|
|
17
|
+
s.dependency 'Capacitor'
|
|
18
|
+
s.swift_version = '5.1'
|
|
19
|
+
end
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 sandman-92
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/Package.swift
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// swift-tools-version: 5.9
|
|
2
|
+
import PackageDescription
|
|
3
|
+
|
|
4
|
+
let package = Package(
|
|
5
|
+
name: "CapacitorBackgroundTransfer",
|
|
6
|
+
platforms: [.iOS(.v15)],
|
|
7
|
+
products: [
|
|
8
|
+
.library(
|
|
9
|
+
name: "CapacitorBackgroundTransfer",
|
|
10
|
+
targets: ["BackgroundTransferPlugin"])
|
|
11
|
+
],
|
|
12
|
+
dependencies: [
|
|
13
|
+
.package(url: "https://github.com/ionic-team/capacitor-swift-pm.git", "7.0.0"..<"9.0.0")
|
|
14
|
+
],
|
|
15
|
+
targets: [
|
|
16
|
+
// Foundation only: records, the store, the remainder copy and the download's part file.
|
|
17
|
+
.target(
|
|
18
|
+
name: "BackgroundTransferCore",
|
|
19
|
+
path: "ios/Sources/BackgroundTransferCore"),
|
|
20
|
+
.target(
|
|
21
|
+
name: "BackgroundTransferPlugin",
|
|
22
|
+
dependencies: [
|
|
23
|
+
"BackgroundTransferCore",
|
|
24
|
+
.product(name: "Capacitor", package: "capacitor-swift-pm"),
|
|
25
|
+
.product(name: "Cordova", package: "capacitor-swift-pm")
|
|
26
|
+
],
|
|
27
|
+
path: "ios/Sources/BackgroundTransferPlugin")
|
|
28
|
+
// The core's tests are in ios/CoreTests, a package of their own that builds on a Mac
|
|
29
|
+
// without Capacitor's iOS-only binaries: `npm run test:ios`.
|
|
30
|
+
]
|
|
31
|
+
)
|
package/README.md
CHANGED
|
@@ -1,3 +1,553 @@
|
|
|
1
|
-
#
|
|
1
|
+
# capacitor-background-transfer
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**Uploads and downloads that keep going when your Capacitor app doesn't.**
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/capacitor-background-transfer)
|
|
6
|
+
[](https://github.com/Outface-World/capacitor-background-transfer/actions/workflows/ci.yml)
|
|
7
|
+
[](LICENSE)
|
|
8
|
+

|
|
9
|
+

|
|
10
|
+

|
|
11
|
+
|
|
12
|
+
[Install](#install) · [npm](https://www.npmjs.com/package/capacitor-background-transfer) ·
|
|
13
|
+
[Quick start](#quick-start) · [Upload protocols](docs/upload-protocols.md) · [How it works](docs/how-it-works.md) · [API](#api) · [Changelog](CHANGELOG.md)
|
|
14
|
+
|
|
15
|
+
A phone suspends an app soon after it leaves the screen, and an upload made with `fetch` or XHR
|
|
16
|
+
from the web view is suspended with it. For a large file on a mobile network, the user has to keep
|
|
17
|
+
the app open and the screen on until the last byte arrives.
|
|
18
|
+
|
|
19
|
+
This plugin hands the transfer to the operating system instead: a user-initiated data transfer job
|
|
20
|
+
on Android 14 and later, a foreground service below that, and a background `URLSession` on iOS.
|
|
21
|
+
The user can lock the phone or switch apps, the app can be closed, the connection can drop, and
|
|
22
|
+
the file still arrives. After an interruption it picks up from where it stopped instead of
|
|
23
|
+
starting again.
|
|
24
|
+
|
|
25
|
+
## Highlights
|
|
26
|
+
|
|
27
|
+
- **Keeps going in the background.** It carries on with the phone locked, the app in the
|
|
28
|
+
background or closed, and through Doze. On iOS the system's transfer daemon keeps it running
|
|
29
|
+
after the app's process has ended.
|
|
30
|
+
- **Resumes instead of restarting.** An upload asks the server how much it already holds and
|
|
31
|
+
sends the rest. A download resumes with a `Range` request.
|
|
32
|
+
- **Four upload protocols.** Firebase Storage resumable uploads, Amazon S3 multipart uploads
|
|
33
|
+
(and S3-compatible services), Azure Blob block blobs, and a plain `PUT` to any presigned or
|
|
34
|
+
SAS URL. Downloads work with any URL.
|
|
35
|
+
- **No cloud keys in the plugin.** Your app or server starts the upload and hands over its URL,
|
|
36
|
+
presigned where the protocol needs it. The plugin never signs a request.
|
|
37
|
+
- **Truthful outcomes.** A transfer is reported finished only once every byte is confirmed, and
|
|
38
|
+
a download appears at its path only when it is complete. Every stopped transfer has a `reason`.
|
|
39
|
+
After a relaunch, `list()` tells you what finished or failed while the app was closed.
|
|
40
|
+
- **A small API.** `upload`, `download`, `cancel`, `list`, `forget` and two events, fully typed.
|
|
41
|
+
On the web it reports itself unavailable, so your app can fall back to its own code there.
|
|
42
|
+
|
|
43
|
+
## Status
|
|
44
|
+
|
|
45
|
+
Early but complete: version 0.x, so the API may still change before 1.0. The JVM and Swift tests
|
|
46
|
+
run in CI, including against local MinIO (S3) and Azurite (Azure) servers. CI also builds the
|
|
47
|
+
plugin into new Capacitor 7 and Capacitor 8 apps for Android and iOS on every pull request, with
|
|
48
|
+
the first and the newest release of each. The upload protocols have also run end to end on the
|
|
49
|
+
Android emulator and the iOS simulator, in a Capacitor 7 app, with the app killed, the network cut
|
|
50
|
+
and URLs left to expire. Real AWS and Azure accounts, a locked iPhone and makers' own
|
|
51
|
+
battery managers haven't been tested yet (see [what's checked](docs/upload-protocols.md#checked-here-and-not)).
|
|
52
|
+
Reports from real apps are very welcome.
|
|
53
|
+
|
|
54
|
+
## Install
|
|
55
|
+
|
|
56
|
+
The plugin is published on npm as
|
|
57
|
+
[`capacitor-background-transfer`](https://www.npmjs.com/package/capacitor-background-transfer).
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
npm install capacitor-background-transfer
|
|
61
|
+
npx cap sync
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
| Platform | Supported |
|
|
65
|
+
|---|---|
|
|
66
|
+
| Capacitor | 7 and 8 |
|
|
67
|
+
| Android | Capacitor 7: 6.0 (API 23) and later. Capacitor 8: 7.0 (API 24) and later |
|
|
68
|
+
| iOS | 15 and later, with Swift Package Manager or CocoaPods |
|
|
69
|
+
| Web | No: `isAvailable()` returns `{ available: false }` |
|
|
70
|
+
|
|
71
|
+
Each platform needs one small change to your app:
|
|
72
|
+
|
|
73
|
+
### Android: one manifest entry
|
|
74
|
+
|
|
75
|
+
The plugin's manifest adds the `INTERNET`, `ACCESS_NETWORK_STATE`, `RUN_USER_INITIATED_JOBS`,
|
|
76
|
+
`RECEIVE_BOOT_COMPLETED`, `FOREGROUND_SERVICE`, `FOREGROUND_SERVICE_DATA_SYNC` and
|
|
77
|
+
`POST_NOTIFICATIONS` permissions and its job service. **Your app declares WorkManager's
|
|
78
|
+
foreground service with the `dataSync` type** (the plugin doesn't, so it can't clash with an app
|
|
79
|
+
that declares the service with other types as well). In `android/app/src/main/AndroidManifest.xml`,
|
|
80
|
+
inside `<application>` (with `xmlns:tools` on `<manifest>`):
|
|
81
|
+
|
|
82
|
+
```xml
|
|
83
|
+
<service
|
|
84
|
+
android:name="androidx.work.impl.foreground.SystemForegroundService"
|
|
85
|
+
android:foregroundServiceType="dataSync"
|
|
86
|
+
tools:node="merge" />
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Without it, transfers below Android 14 stop when the app leaves the screen, and `isAvailable()`
|
|
90
|
+
carries a `warning` saying so. Ask for the notification permission on Android 13+ if you want the
|
|
91
|
+
progress notification shown; transfers run without it. The WorkManager version follows
|
|
92
|
+
`workManagerVersion` in your `variables.gradle` (default 2.10.0).
|
|
93
|
+
|
|
94
|
+
### iOS: one `AppDelegate` method
|
|
95
|
+
|
|
96
|
+
A new Capacitor 7 app targets iOS 14: raise its deployment target to 15 (in the Xcode project, and
|
|
97
|
+
in the `Podfile` with CocoaPods), then run `npx cap sync` again, which gives the app's Swift package
|
|
98
|
+
the new target or runs `pod install` with it. A Capacitor 8 app already targets 15.
|
|
99
|
+
|
|
100
|
+
Hand the system's background session events to the plugin in `AppDelegate`, so transfers that
|
|
101
|
+
end while the app is closed are recorded straight away (otherwise on the next launch):
|
|
102
|
+
|
|
103
|
+
```swift
|
|
104
|
+
import BackgroundTransferPlugin
|
|
105
|
+
|
|
106
|
+
func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String,
|
|
107
|
+
completionHandler: @escaping () -> Void) {
|
|
108
|
+
if BackgroundTransferPlugin.handleEvents(forBackgroundURLSession: identifier, completionHandler: completionHandler) { return }
|
|
109
|
+
completionHandler()
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
The module is `BackgroundTransferPlugin` with both Swift Package Manager and CocoaPods. Files to
|
|
114
|
+
upload must be readable while the phone is locked: the default data protection (until the first
|
|
115
|
+
unlock after a restart) is; complete protection isn't.
|
|
116
|
+
|
|
117
|
+
### Where files may be
|
|
118
|
+
|
|
119
|
+
A transfer's `path` must be inside the app's own storage, and the native side checks this after
|
|
120
|
+
resolving symbolic links, whatever called it: on Android the app's files, no-backup files and
|
|
121
|
+
cache directories, internal and external (`Directory.Data`, `Directory.Cache` and
|
|
122
|
+
`Directory.External` of the Filesystem plugin); on iOS Documents, Library and tmp (every
|
|
123
|
+
Filesystem directory). Paths with `..`, paths into the plugin's own storage, and a download onto a
|
|
124
|
+
symbolic link are refused. An app that keeps files elsewhere (an app group container, say) lists
|
|
125
|
+
the extra roots in the plugin's configuration, never from JavaScript:
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
// capacitor.config.ts
|
|
129
|
+
plugins: {
|
|
130
|
+
BackgroundTransfer: {
|
|
131
|
+
allowedRoots: ['/private/var/mobile/Containers/Shared/AppGroup/…/Videos'],
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Quick start
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
import { BackgroundTransfer } from 'capacitor-background-transfer';
|
|
140
|
+
|
|
141
|
+
const { available } = await BackgroundTransfer.isAvailable();
|
|
142
|
+
|
|
143
|
+
await BackgroundTransfer.addListener('progress', ({ transfer }) => {
|
|
144
|
+
console.log(`${transfer.id}: ${transfer.bytes} of ${transfer.total} bytes`);
|
|
145
|
+
});
|
|
146
|
+
await BackgroundTransfer.addListener('stateChange', ({ transfer }) => {
|
|
147
|
+
// queued, running, interrupted, finished, failed or cancelled
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
// Upload: your server (or the Firebase SDK) starts the upload, then the plugin sends the file.
|
|
151
|
+
await BackgroundTransfer.upload({
|
|
152
|
+
id: 'clip-42',
|
|
153
|
+
path: fileUri, // an absolute path or file:// URI, e.g. from Filesystem.getUri()
|
|
154
|
+
url: sessionUrl, // here, the X-Goog-Upload-URL of a Firebase Storage resumable upload
|
|
155
|
+
notification: { title: 'Uploading', text: 'clip-42.mp4' }, // Android only
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// Download any URL to a file.
|
|
159
|
+
await BackgroundTransfer.download({ id: 'clip-42', url: downloadUrl, path: destinationUri });
|
|
160
|
+
|
|
161
|
+
// At launch: what happened while the app was closed.
|
|
162
|
+
const { transfers } = await BackgroundTransfer.list();
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
Two rules to know:
|
|
166
|
+
|
|
167
|
+
- **Start transfers while the app is on screen.** Android only lets a user-initiated job be
|
|
168
|
+
scheduled then, and iOS may defer work started from the background.
|
|
169
|
+
- **The `id` is how you resume.** Calling `upload()` or `download()` again with the `id` of an
|
|
170
|
+
`interrupted`, `failed` or `cancelled` transfer carries on from where it stopped. See
|
|
171
|
+
[Starting a transfer again](docs/how-it-works.md#starting-a-transfer-again).
|
|
172
|
+
|
|
173
|
+
## Upload protocols
|
|
174
|
+
|
|
175
|
+
Pick one with the `protocol` option. Every protocol behaves the same way in the background and
|
|
176
|
+
reports through the same events.
|
|
177
|
+
|
|
178
|
+
| `protocol` | Uploads to | An interrupted upload resumes from |
|
|
179
|
+
|---|---|---|
|
|
180
|
+
| `firebase-resumable` (default) | A Firebase Storage resumable upload session | What the session holds |
|
|
181
|
+
| `s3-multipart` | An Amazon S3 multipart upload with presigned part URLs. S3-compatible services are expected to work; only MinIO is tested | The parts S3 holds, or the plugin's own record of them |
|
|
182
|
+
| `azure-blocks` | An Azure Blob Storage block blob through one SAS URL | The blocks Azure holds |
|
|
183
|
+
| `put` | Any URL that takes the whole file in one `PUT`, e.g. an S3 presigned `PutObject` URL or an Azure blob SAS URL | The start: there is nothing to resume from |
|
|
184
|
+
|
|
185
|
+
[docs/upload-protocols.md](docs/upload-protocols.md) has the server-side setup, the options, URL
|
|
186
|
+
lifetimes and a code example for each one.
|
|
187
|
+
|
|
188
|
+
## How it works
|
|
189
|
+
|
|
190
|
+
| | Android 14+ | Android 6 to 13 | iOS |
|
|
191
|
+
|---|---|---|---|
|
|
192
|
+
| Runs as | A user-initiated data transfer job | A WorkManager worker in a `dataSync` foreground service | A background `URLSession` |
|
|
193
|
+
| Locked, in the background or removed from Recents | Carries on | Carries on | Carries on, also after the app's process ends |
|
|
194
|
+
| Network drops while the app is off screen | Waits, then resumes by itself | Reported `interrupted`, so start it again at the next launch | The system often retries; otherwise reported `interrupted` |
|
|
195
|
+
| Notification | Shows progress | Shows progress (the service's notification) | None |
|
|
196
|
+
|
|
197
|
+
One transfer runs per direction at a time, and the rest wait in a queue.
|
|
198
|
+
[docs/how-it-works.md](docs/how-it-works.md) covers each platform in detail, including
|
|
199
|
+
system stops, ordering, iOS resends, what is stored on the device, and every failure `reason`.
|
|
200
|
+
|
|
201
|
+
## FAQ
|
|
202
|
+
|
|
203
|
+
**Why not just use `fetch` from the web view?** The web view's requests run in your app's process,
|
|
204
|
+
which the system suspends soon after the app leaves the screen. This plugin hands the transfer to
|
|
205
|
+
a part of the system that keeps running.
|
|
206
|
+
|
|
207
|
+
**What happens if the user swipes the app away or force-stops it?** The system ends the
|
|
208
|
+
transfer on purpose. It is reported `interrupted` (on iOS at the next launch), and starting it
|
|
209
|
+
again with the same `id` carries on from where it stopped.
|
|
210
|
+
|
|
211
|
+
**Are there limits?** Below Android 14, the system allows a `dataSync` foreground service six
|
|
212
|
+
hours a day. iOS may defer transfers started while the app is in the background. On iOS, resuming
|
|
213
|
+
an upload part-way needs temporary disk space for a copy of the bytes still to send.
|
|
214
|
+
|
|
215
|
+
**Where do my headers go?** To the host in the URL, and nowhere else: a redirect to another host
|
|
216
|
+
is refused rather than followed (on iOS, where the system follows it by itself, the transfer is
|
|
217
|
+
reported `session-refused` when it ends). While a transfer is queued or running its record,
|
|
218
|
+
`headers` included, is kept in the app's sandbox, in storage that is left out of device backups;
|
|
219
|
+
the headers are dropped the moment it finishes, fails, is cancelled or is interrupted (a restart
|
|
220
|
+
brings its own). The URL stays in the record until you call `forget()`, so presigned URLs with a
|
|
221
|
+
short life are the better route for credentials.
|
|
222
|
+
|
|
223
|
+
## Feedback and contributing
|
|
224
|
+
|
|
225
|
+
Found a bug or want a feature? [Open an issue](https://github.com/Outface-World/capacitor-background-transfer/issues).
|
|
226
|
+
Pull requests are welcome too. [CONTRIBUTING.md](CONTRIBUTING.md) explains how to build the plugin,
|
|
227
|
+
run its tests (including the local S3 and Azure servers) and try a change on a device with the
|
|
228
|
+
[example app](example-app/). To report a security problem privately, see [SECURITY.md](SECURITY.md).
|
|
229
|
+
|
|
230
|
+
## API
|
|
231
|
+
|
|
232
|
+
<docgen-index>
|
|
233
|
+
|
|
234
|
+
* [`isAvailable()`](#isavailable)
|
|
235
|
+
* [`upload(...)`](#upload)
|
|
236
|
+
* [`download(...)`](#download)
|
|
237
|
+
* [`cancel(...)`](#cancel)
|
|
238
|
+
* [`list()`](#list)
|
|
239
|
+
* [`forget(...)`](#forget)
|
|
240
|
+
* [`addListener('progress', ...)`](#addlistenerprogress-)
|
|
241
|
+
* [`addListener('stateChange', ...)`](#addlistenerstatechange-)
|
|
242
|
+
* [`removeAllListeners()`](#removealllisteners)
|
|
243
|
+
* [Interfaces](#interfaces)
|
|
244
|
+
* [Type Aliases](#type-aliases)
|
|
245
|
+
|
|
246
|
+
</docgen-index>
|
|
247
|
+
|
|
248
|
+
<docgen-api>
|
|
249
|
+
<!--Update the source file JSDoc comments and rerun docgen to update the docs below-->
|
|
250
|
+
|
|
251
|
+
### isAvailable()
|
|
252
|
+
|
|
253
|
+
```typescript
|
|
254
|
+
isAvailable() => Promise<AvailableResult>
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
Whether native background transfers work here. The web says no.
|
|
258
|
+
|
|
259
|
+
**Returns:** <code>Promise<<a href="#availableresult">AvailableResult</a>></code>
|
|
260
|
+
|
|
261
|
+
--------------------
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
### upload(...)
|
|
265
|
+
|
|
266
|
+
```typescript
|
|
267
|
+
upload(options: UploadOptions) => Promise<TransferEvent>
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
Queue an upload to an upload you have set up on the server (see
|
|
271
|
+
`UploadProtocol`). Refused at once if its options are missing or don't
|
|
272
|
+
fit together (on Android and iOS also when `partUrls` doesn't match the
|
|
273
|
+
file's size). Call it while the app
|
|
274
|
+
is on screen: Android only lets a user-initiated job be scheduled then,
|
|
275
|
+
and iPhone may defer tasks started from the background.
|
|
276
|
+
|
|
277
|
+
| Param | Type |
|
|
278
|
+
| ------------- | ------------------------------------------------------- |
|
|
279
|
+
| **`options`** | <code><a href="#uploadoptions">UploadOptions</a></code> |
|
|
280
|
+
|
|
281
|
+
**Returns:** <code>Promise<<a href="#transferevent">TransferEvent</a>></code>
|
|
282
|
+
|
|
283
|
+
--------------------
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
### download(...)
|
|
287
|
+
|
|
288
|
+
```typescript
|
|
289
|
+
download(options: DownloadOptions) => Promise<TransferEvent>
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
Queue a download. Same rules as `upload`.
|
|
293
|
+
|
|
294
|
+
| Param | Type |
|
|
295
|
+
| ------------- | ----------------------------------------------------------- |
|
|
296
|
+
| **`options`** | <code><a href="#downloadoptions">DownloadOptions</a></code> |
|
|
297
|
+
|
|
298
|
+
**Returns:** <code>Promise<<a href="#transferevent">TransferEvent</a>></code>
|
|
299
|
+
|
|
300
|
+
--------------------
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
### cancel(...)
|
|
304
|
+
|
|
305
|
+
```typescript
|
|
306
|
+
cancel(options: { id: string; direction: TransferDirection; }) => Promise<void>
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
Cancel a transfer; its temporary files are removed. Unknown ids are ignored.
|
|
310
|
+
|
|
311
|
+
| Param | Type |
|
|
312
|
+
| ------------- | ------------------------------------------------------------------------------------------- |
|
|
313
|
+
| **`options`** | <code>{ id: string; direction: <a href="#transferdirection">TransferDirection</a>; }</code> |
|
|
314
|
+
|
|
315
|
+
--------------------
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
### list()
|
|
319
|
+
|
|
320
|
+
```typescript
|
|
321
|
+
list() => Promise<{ transfers: TransferRecord[]; }>
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
Every transfer the plugin remembers, including ones that ran, finished
|
|
325
|
+
or failed while the app was closed. Ask on launch.
|
|
326
|
+
|
|
327
|
+
**Returns:** <code>Promise<{ transfers: TransferRecord[]; }></code>
|
|
328
|
+
|
|
329
|
+
--------------------
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
### forget(...)
|
|
333
|
+
|
|
334
|
+
```typescript
|
|
335
|
+
forget(options?: { ids?: { id: string; direction: TransferDirection; }[] | undefined; } | undefined) => Promise<void>
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
Forget finished, failed and cancelled transfers (all of them, or the
|
|
339
|
+
given ids). Queued, running and interrupted ones are kept.
|
|
340
|
+
|
|
341
|
+
| Param | Type |
|
|
342
|
+
| ------------- | -------------------------------------------------------------------------------------------------------- |
|
|
343
|
+
| **`options`** | <code>{ ids?: { id: string; direction: <a href="#transferdirection">TransferDirection</a>; }[]; }</code> |
|
|
344
|
+
|
|
345
|
+
--------------------
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
### addListener('progress', ...)
|
|
349
|
+
|
|
350
|
+
```typescript
|
|
351
|
+
addListener(eventName: 'progress', listener: (event: TransferEvent) => void) => Promise<PluginListenerHandle>
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
Bytes moved. At most a few events a second per transfer.
|
|
355
|
+
|
|
356
|
+
| Param | Type |
|
|
357
|
+
| --------------- | --------------------------------------------------------------------------- |
|
|
358
|
+
| **`eventName`** | <code>'progress'</code> |
|
|
359
|
+
| **`listener`** | <code>(event: <a href="#transferevent">TransferEvent</a>) => void</code> |
|
|
360
|
+
|
|
361
|
+
**Returns:** <code>Promise<<a href="#pluginlistenerhandle">PluginListenerHandle</a>></code>
|
|
362
|
+
|
|
363
|
+
--------------------
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
### addListener('stateChange', ...)
|
|
367
|
+
|
|
368
|
+
```typescript
|
|
369
|
+
addListener(eventName: 'stateChange', listener: (event: TransferEvent) => void) => Promise<PluginListenerHandle>
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
Any change of `state` (queued, running, interrupted, finished, failed,
|
|
373
|
+
cancelled).
|
|
374
|
+
|
|
375
|
+
| Param | Type |
|
|
376
|
+
| --------------- | --------------------------------------------------------------------------- |
|
|
377
|
+
| **`eventName`** | <code>'stateChange'</code> |
|
|
378
|
+
| **`listener`** | <code>(event: <a href="#transferevent">TransferEvent</a>) => void</code> |
|
|
379
|
+
|
|
380
|
+
**Returns:** <code>Promise<<a href="#pluginlistenerhandle">PluginListenerHandle</a>></code>
|
|
381
|
+
|
|
382
|
+
--------------------
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
### removeAllListeners()
|
|
386
|
+
|
|
387
|
+
```typescript
|
|
388
|
+
removeAllListeners() => Promise<void>
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
--------------------
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
### Interfaces
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
#### AvailableResult
|
|
398
|
+
|
|
399
|
+
| Prop | Type | Description |
|
|
400
|
+
| --------------- | -------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
401
|
+
| **`available`** | <code>boolean</code> | |
|
|
402
|
+
| **`mechanism`** | <code>'job' \| 'worker' \| 'urlsession' \| 'none'</code> | How transfers run here: `job` (Android 14+ user-initiated data transfer job), `worker` (WorkManager `dataSync` foreground service below Android 14), `urlsession` (iPhone background `URLSession`) or `none` (web). |
|
|
403
|
+
| **`warning`** | <code>string</code> | Set when the app is set up so that transfers can't run as they should, e.g. below Android 14 when the manifest doesn't declare WorkManager's foreground service with the `dataSync` type (the worker then stops when the app leaves the screen). For logs, not for people. |
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
#### TransferEvent
|
|
407
|
+
|
|
408
|
+
| Prop | Type |
|
|
409
|
+
| -------------- | --------------------------------------------------------- |
|
|
410
|
+
| **`transfer`** | <code><a href="#transferrecord">TransferRecord</a></code> |
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
#### TransferRecord
|
|
414
|
+
|
|
415
|
+
What the plugin knows about one transfer.
|
|
416
|
+
|
|
417
|
+
| Prop | Type | Description |
|
|
418
|
+
| ---------------- | ----------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
419
|
+
| **`id`** | <code>string</code> | |
|
|
420
|
+
| **`direction`** | <code><a href="#transferdirection">TransferDirection</a></code> | |
|
|
421
|
+
| **`state`** | <code><a href="#transferstate">TransferState</a></code> | |
|
|
422
|
+
| **`url`** | <code>string</code> | |
|
|
423
|
+
| **`path`** | <code>string</code> | |
|
|
424
|
+
| **`protocol`** | <code><a href="#uploadprotocol">UploadProtocol</a></code> | Uploads: the protocol the session speaks. |
|
|
425
|
+
| **`bytes`** | <code>number</code> | Bytes the server holds (upload) or that have landed in `.part` (download). For `s3-multipart` and `azure-blocks`, the parts or blocks stored plus the bytes of the one being sent (so it goes back to the stored parts' end when a part cut off part-way is sent again); for `put`, the bytes sent so far in this attempt. Below `total` until the server has confirmed the whole object. |
|
|
426
|
+
| **`total`** | <code>number</code> | Total size, or -1 until it is known. |
|
|
427
|
+
| **`reason`** | <code><a href="#transferfailurereason">TransferFailureReason</a></code> | Set when `state` is `failed`, `cancelled` or `interrupted`. |
|
|
428
|
+
| **`message`** | <code>string</code> | Detail for logs; not for showing to people. |
|
|
429
|
+
| **`httpStatus`** | <code>number</code> | HTTP status of the response that stopped it, when there was one. |
|
|
430
|
+
| **`updatedAt`** | <code>number</code> | Milliseconds since the epoch. |
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
#### UploadOptions
|
|
434
|
+
|
|
435
|
+
| Prop | Type | Description |
|
|
436
|
+
| ------------------ | --------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
437
|
+
| **`url`** | <code>string</code> | Where the upload goes, which depends on `protocol`: - `firebase-resumable`: the session URL of an upload you have already started (the `X-Goog-Upload-URL` from its `start`). The plugin asks it how many bytes it holds, sends the rest and finalizes. - `put`: the URL to `PUT` the whole file to. - `s3-multipart`: the presigned `CompleteMultipartUpload` URL (`POST`, with the upload's `uploadId`). - `azure-blocks`: the blob's SAS URL (with its `sig` parameter). The blob's properties go in `headers` as `x-ms-blob-content-type` and the other `x-ms-blob-*` headers: they are sent with the commit only. |
|
|
438
|
+
| **`protocol`** | <code><a href="#uploadprotocol">UploadProtocol</a></code> | The protocol the upload speaks. Default `firebase-resumable`. An unknown value is refused. |
|
|
439
|
+
| **`partUrls`** | <code>string[]</code> | `s3-multipart` only, and required there: the presigned `UploadPart` URL of every part, part 1 first. There must be exactly as many as the file needs in `partSize` parts (at most 10,000). They must stay valid until the last part is sent. |
|
|
440
|
+
| **`partSize`** | <code>number</code> | `s3-multipart` only: the size of every part but the last, in bytes. Default 8 MiB. At least 5 MiB when there is more than one part, at most 5 GiB. |
|
|
441
|
+
| **`listPartsUrl`** | <code>string</code> | `s3-multipart` only, optional: the presigned `ListParts` URL (`GET`, with the `uploadId`). When it is given, a resumed upload asks the server which parts it holds instead of trusting the plugin's own record, so a part the server lost is sent again and fresh URLs for the same upload carry on where the old ones stopped. |
|
|
442
|
+
| **`blockSize`** | <code>number</code> | `azure-blocks` only: the size of every block but the last, in bytes. Default 8 MiB, at most 4000 MiB, and at most 50,000 blocks. |
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
#### DownloadOptions
|
|
446
|
+
|
|
447
|
+
| Prop | Type | Description |
|
|
448
|
+
| --------- | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
449
|
+
| **`url`** | <code>string</code> | The URL to fetch, already resolved (a Storage download URL carries its own token). Bytes go to `<path>.part` and the file is renamed to `path` only once every byte has landed. |
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
#### PluginListenerHandle
|
|
453
|
+
|
|
454
|
+
| Prop | Type |
|
|
455
|
+
| ------------ | ----------------------------------------- |
|
|
456
|
+
| **`remove`** | <code>() => Promise<void></code> |
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
### Type Aliases
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
#### TransferDirection
|
|
463
|
+
|
|
464
|
+
Which way a transfer moves bytes.
|
|
465
|
+
|
|
466
|
+
<code>'upload' | 'download'</code>
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
#### TransferState
|
|
470
|
+
|
|
471
|
+
Where a transfer is.
|
|
472
|
+
|
|
473
|
+
- `queued`: waiting its turn (one transfer runs per direction), or waiting
|
|
474
|
+
for the network on Android 14+, where the system restarts it.
|
|
475
|
+
- `running`: bytes are moving.
|
|
476
|
+
- `interrupted`: stopped by a network drop, the app being swiped away or
|
|
477
|
+
the system, and won't restart by itself. Start it again with the same
|
|
478
|
+
`id` and it carries on from where it stopped.
|
|
479
|
+
- `finished`: every byte landed. For a download the file is at `path`.
|
|
480
|
+
- `failed`: it can't finish as asked; `reason` says why. Its temporary
|
|
481
|
+
files are gone.
|
|
482
|
+
- `cancelled`: `cancel()` was called. Its temporary files are gone.
|
|
483
|
+
|
|
484
|
+
<code>'queued' | 'running' | 'interrupted' | 'finished' | 'failed' | 'cancelled'</code>
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
#### UploadProtocol
|
|
488
|
+
|
|
489
|
+
The upload protocol a transfer speaks. The plugin never signs anything: you
|
|
490
|
+
create the upload on the server and hand the plugin URLs that carry their
|
|
491
|
+
own authorisation (a session URL, presigned URLs, a SAS URL).
|
|
492
|
+
|
|
493
|
+
- `firebase-resumable`: a Firebase Storage resumable upload (the
|
|
494
|
+
`X-Goog-Upload-*` protocol: `POST` with `X-Goog-Upload-Command` `query`,
|
|
495
|
+
`upload` and `upload, finalize`). `url` is the session URL.
|
|
496
|
+
- `put`: the whole file in one `PUT` to `url`, e.g. an S3 presigned `PUT`
|
|
497
|
+
URL or an Azure blob SAS URL (pass `x-ms-blob-type: BlockBlob` in
|
|
498
|
+
`headers` for Azure). An interrupted upload starts again from the first
|
|
499
|
+
byte. S3 takes at most 5 GB in one `PUT`.
|
|
500
|
+
- `s3-multipart`: an S3 (or S3-compatible) multipart upload you have
|
|
501
|
+
created. `partUrls` are the presigned `UploadPart` URLs, part 1 first;
|
|
502
|
+
`url` is the presigned `CompleteMultipartUpload` URL. The plugin sends the
|
|
503
|
+
parts one at a time, keeps each `ETag`, skips the parts already stored when
|
|
504
|
+
it resumes, then completes the upload listing every part.
|
|
505
|
+
- `azure-blocks`: an Azure block blob. `url` is the blob's SAS URL with
|
|
506
|
+
write permission (`sp` including `w`, or `c` and `w`). The plugin sends
|
|
507
|
+
blocks with stable ids (`Put Block`), skips the ones in the blob's
|
|
508
|
+
uncommitted block list when it resumes, then commits them
|
|
509
|
+
(`Put Block List`).
|
|
510
|
+
|
|
511
|
+
<code>'firebase-resumable' | 'put' | 's3-multipart' | 'azure-blocks'</code>
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
#### TransferFailureReason
|
|
515
|
+
|
|
516
|
+
Why a transfer stopped without finishing.
|
|
517
|
+
|
|
518
|
+
- `file-missing`: the upload's file isn't there (or is a directory).
|
|
519
|
+
- `disk-full`: no space for a download, or for the remainder copy an
|
|
520
|
+
iPhone upload needs to resume part-way.
|
|
521
|
+
- `session-refused`: the server refused the request (HTTP 4xx other than
|
|
522
|
+
the two below), e.g. Firebase Storage security rules refused the upload at finalize (403), or
|
|
523
|
+
a presigned URL or SAS token expired or lacks a permission (403). `message` names the
|
|
524
|
+
request that was refused (a part, a block, the `PUT`, or the commit).
|
|
525
|
+
- `session-expired`: the upload session or download URL is gone (HTTP 404
|
|
526
|
+
or 410), e.g. an S3 multipart upload that was aborted, found out while
|
|
527
|
+
sending a part or asking ListParts. Start a new session. (An S3 upload
|
|
528
|
+
found gone only at the completion reports `session-closed`: it may have
|
|
529
|
+
been completed.)
|
|
530
|
+
- `session-closed`: the upload can't be confirmed from here. For `firebase-resumable`, the
|
|
531
|
+
session was already closed when the upload started; that isn't proof the object was accepted
|
|
532
|
+
(the Firebase Storage emulator closes a session whose finalize its security rules refused).
|
|
533
|
+
For `s3-multipart` and `azure-blocks`, the request that completes the object was sent in an
|
|
534
|
+
earlier run (or earlier in this one) and its answer never came, so it may or may not have
|
|
535
|
+
been applied. Either way, check the object on the server, and start a new upload if it
|
|
536
|
+
isn't there or is the wrong size.
|
|
537
|
+
- `size-mismatch`: the bytes that landed don't add up to the size the
|
|
538
|
+
server or file said.
|
|
539
|
+
- `network`: the connection dropped. Used with `interrupted`.
|
|
540
|
+
- `stopped-by-system`: the system ended the work (the app was swiped away
|
|
541
|
+
on iPhone, the work was stopped below Android 14). Used with
|
|
542
|
+
`interrupted`.
|
|
543
|
+
- `cancelled`: `cancel()` was called.
|
|
544
|
+
- `server-error`: the server kept answering 5xx.
|
|
545
|
+
- `unknown`: anything else; `message` has the detail.
|
|
546
|
+
|
|
547
|
+
<code>'file-missing' | 'disk-full' | 'session-refused' | 'session-expired' | 'session-closed' | 'size-mismatch' | 'network' | 'stopped-by-system' | 'cancelled' | 'server-error' | 'unknown'</code>
|
|
548
|
+
|
|
549
|
+
</docgen-api>
|
|
550
|
+
|
|
551
|
+
## License
|
|
552
|
+
|
|
553
|
+
[MIT](LICENSE). Made by [sandman-92](https://github.com/sandman-92).
|