@juit/capacitor-updater 0.0.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/LICENSE.md +211 -0
- package/NOTICE.md +13 -0
- package/README.md +77 -0
- package/dist/index.d.ts +57 -0
- package/dist/index.js +109 -0
- package/dist/index.js.map +6 -0
- package/package.json +56 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
# Apache License
|
|
2
|
+
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
|
|
5
|
+
[http://www.apache.org/licenses/](http://www.apache.org/licenses/)
|
|
6
|
+
|
|
7
|
+
##### TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
8
|
+
|
|
9
|
+
### 1. Definitions.
|
|
10
|
+
|
|
11
|
+
"**License**" shall mean the terms and conditions for use, reproduction,
|
|
12
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
13
|
+
|
|
14
|
+
"**Licensor**" shall mean the copyright owner or entity authorized by
|
|
15
|
+
the copyright owner that is granting the License.
|
|
16
|
+
|
|
17
|
+
"**Legal Entity**" shall mean the union of the acting entity and all
|
|
18
|
+
other entities that control, are controlled by, or are under common
|
|
19
|
+
control with that entity. For the purposes of this definition,
|
|
20
|
+
"**control**" means **(i)** the power, direct or indirect, to cause the
|
|
21
|
+
direction or management of such entity, whether by contract or
|
|
22
|
+
otherwise, _or_ **(ii)** ownership of fifty percent (50%) or more of
|
|
23
|
+
the outstanding shares, _or_ **(iii)** beneficial ownership of such
|
|
24
|
+
entity.
|
|
25
|
+
|
|
26
|
+
"**You**" (or "**Your**") shall mean an individual or Legal Entity
|
|
27
|
+
exercising permissions granted by this License.
|
|
28
|
+
|
|
29
|
+
"**Source**" form shall mean the preferred form for making
|
|
30
|
+
modifications, including but not limited to software source code,
|
|
31
|
+
documentation source, and configuration files.
|
|
32
|
+
|
|
33
|
+
"**Object**" form shall mean any form resulting from mechanical
|
|
34
|
+
transformation or translation of a Source form, including but not
|
|
35
|
+
limited to compiled object code, generated documentation, and
|
|
36
|
+
conversions to other media types.
|
|
37
|
+
|
|
38
|
+
"**Work**" shall mean the work of authorship, whether in Source or
|
|
39
|
+
Object form, made available under the License, as indicated by a
|
|
40
|
+
copyright notice that is included in or attached to the work (an example
|
|
41
|
+
is provided in the Appendix below).
|
|
42
|
+
|
|
43
|
+
"**Derivative Works**" shall mean any work, whether in Source or Object
|
|
44
|
+
form, that is based on (or derived from) the Work and for which the
|
|
45
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
46
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
47
|
+
of this License, Derivative Works shall not include works that remain
|
|
48
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
49
|
+
the Work and Derivative Works thereof.
|
|
50
|
+
|
|
51
|
+
"**Contribution**" shall mean any work of authorship, including the
|
|
52
|
+
original version of the Work and any modifications or additions to that
|
|
53
|
+
Work or Derivative Works thereof, that is intentionally submitted to
|
|
54
|
+
Licensor for inclusion in the Work by the copyright owner or by an
|
|
55
|
+
individual or Legal Entity authorized to submit on behalf of the
|
|
56
|
+
copyright owner. For the purposes of this definition, "**submitted**"
|
|
57
|
+
means any form of electronic, verbal, or written communication sent to
|
|
58
|
+
the Licensor or its representatives, including but not limited to
|
|
59
|
+
communication on electronic mailing lists, source code control systems,
|
|
60
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
61
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
62
|
+
excluding communication that is conspicuously marked or otherwise
|
|
63
|
+
designated in writing by the copyright owner as "**Not a
|
|
64
|
+
Contribution**".
|
|
65
|
+
|
|
66
|
+
"**Contributor**" shall mean Licensor and any individual or Legal Entity
|
|
67
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
68
|
+
subsequently incorporated within the Work.
|
|
69
|
+
|
|
70
|
+
### 2. Grant of Copyright License.
|
|
71
|
+
|
|
72
|
+
Subject to the terms and conditions of this License, each Contributor
|
|
73
|
+
hereby grants to You a perpetual, worldwide, non-exclusive, no-charge,
|
|
74
|
+
royalty-free, irrevocable copyright license to reproduce, prepare
|
|
75
|
+
Derivative Works of, publicly display, publicly perform, sublicense,
|
|
76
|
+
and distribute the Work and such Derivative Works in Source or Object
|
|
77
|
+
form.
|
|
78
|
+
|
|
79
|
+
### 3. Grant of Patent License.
|
|
80
|
+
|
|
81
|
+
Subject to the terms and conditions of this License, each Contributor
|
|
82
|
+
hereby grants to You a perpetual, worldwide, non-exclusive, no-charge,
|
|
83
|
+
royalty-free, irrevocable (except as stated in this section) patent
|
|
84
|
+
license to make, have made, use, offer to sell, sell, import, and
|
|
85
|
+
otherwise transfer the Work, where such license applies only to those
|
|
86
|
+
patent claims licensable by such Contributor that are necessarily
|
|
87
|
+
infringed by their Contribution(s) alone or by combination of their
|
|
88
|
+
Contribution(s) with the Work to which such Contribution(s) was
|
|
89
|
+
submitted. If You institute patent litigation against any entity
|
|
90
|
+
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
|
91
|
+
the Work or a Contribution incorporated within the Work constitutes
|
|
92
|
+
direct or contributory patent infringement, then any patent licenses
|
|
93
|
+
granted to You under this License for that Work shall terminate as of
|
|
94
|
+
the date such litigation is filed.
|
|
95
|
+
|
|
96
|
+
### 4. Redistribution.
|
|
97
|
+
|
|
98
|
+
You may reproduce and distribute copies of the Work or Derivative Works
|
|
99
|
+
thereof in any medium, with or without modifications, and in Source or
|
|
100
|
+
Object form, provided that You meet the following conditions:
|
|
101
|
+
|
|
102
|
+
<ol type="a">
|
|
103
|
+
<li>You must give any other recipients of the Work or Derivative Works
|
|
104
|
+
a copy of this License; and
|
|
105
|
+
<li>You must cause any modified files to carry prominent notices
|
|
106
|
+
stating that You changed the files; and
|
|
107
|
+
<li>You must retain, in the Source form of any Derivative Works that
|
|
108
|
+
You distribute, all copyright, patent, trademark, and attribution
|
|
109
|
+
notices from the Source form of the Work, excluding those notices
|
|
110
|
+
that do not pertain to any part of the Derivative Works; and
|
|
111
|
+
<li>If the Work includes a "NOTICE" text file as part of its
|
|
112
|
+
distribution, then any Derivative Works that You distribute must
|
|
113
|
+
include a readable copy of the attribution notices contained within
|
|
114
|
+
such NOTICE file, excluding those notices that do not pertain to any
|
|
115
|
+
part of the Derivative Works, in at least one of the following
|
|
116
|
+
places: within a NOTICE text file distributed as part of the
|
|
117
|
+
Derivative Works; within the Source form or documentation, if
|
|
118
|
+
provided along with the Derivative Works; or, within a display
|
|
119
|
+
generated by the Derivative Works, if and wherever such third-party
|
|
120
|
+
notices normally appear. The contents of the NOTICE file are for
|
|
121
|
+
informational purposes only and do not modify the License. You may
|
|
122
|
+
add Your own attribution notices within Derivative Works that You
|
|
123
|
+
distribute, alongside or as an addendum to the NOTICE text from the
|
|
124
|
+
Work, provided that such additional attribution notices cannot be
|
|
125
|
+
construed as modifying the License.
|
|
126
|
+
</ol>
|
|
127
|
+
|
|
128
|
+
You may add Your own copyright statement to Your modifications and may
|
|
129
|
+
provide additional or different license terms and conditions for use,
|
|
130
|
+
reproduction, or distribution of Your modifications, or for any such
|
|
131
|
+
Derivative Works as a whole, provided Your use, reproduction, and
|
|
132
|
+
distribution of the Work otherwise complies with the conditions stated
|
|
133
|
+
in this License.
|
|
134
|
+
|
|
135
|
+
### 5. Submission of Contributions.
|
|
136
|
+
|
|
137
|
+
Unless You explicitly state otherwise, any Contribution intentionally
|
|
138
|
+
submitted for inclusion in the Work by You to the Licensor shall be
|
|
139
|
+
under the terms and conditions of this License, without any additional
|
|
140
|
+
terms or conditions. Notwithstanding the above, nothing herein shall
|
|
141
|
+
supersede or modify the terms of any separate license agreement you may
|
|
142
|
+
have executed with Licensor regarding such Contributions.
|
|
143
|
+
|
|
144
|
+
### 6. Trademarks.
|
|
145
|
+
|
|
146
|
+
This License does not grant permission to use the trade names,
|
|
147
|
+
trademarks, service marks, or product names of the Licensor, except as
|
|
148
|
+
required for reasonable and customary use in describing the origin of
|
|
149
|
+
the Work and reproducing the content of the NOTICE file.
|
|
150
|
+
|
|
151
|
+
### 7. Disclaimer of Warranty.
|
|
152
|
+
|
|
153
|
+
Unless required by applicable law or agreed to in writing, Licensor
|
|
154
|
+
provides the Work (and each Contributor provides its Contributions) on
|
|
155
|
+
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
|
|
156
|
+
express or implied, including, without limitation, any warranties or
|
|
157
|
+
conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR
|
|
158
|
+
A PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
159
|
+
appropriateness of using or redistributing the Work and assume any risks
|
|
160
|
+
associated with Your exercise of permissions under this License.
|
|
161
|
+
|
|
162
|
+
### 8. Limitation of Liability.
|
|
163
|
+
|
|
164
|
+
In no event and under no legal theory, whether in tort (including
|
|
165
|
+
negligence), contract, or otherwise, unless required by applicable law
|
|
166
|
+
(such as deliberate and grossly negligent acts) or agreed to in writing,
|
|
167
|
+
shall any Contributor be liable to You for damages, including any
|
|
168
|
+
direct, indirect, special, incidental, or consequential damages of any
|
|
169
|
+
character arising as a result of this License or out of the use or
|
|
170
|
+
inability to use the Work (including but not limited to damages for loss
|
|
171
|
+
of goodwill, work stoppage, computer failure or malfunction, or any and
|
|
172
|
+
all other commercial damages or losses), even if such Contributor has
|
|
173
|
+
been advised of the possibility of such damages.
|
|
174
|
+
|
|
175
|
+
### 9. Accepting Warranty or Additional Liability.
|
|
176
|
+
|
|
177
|
+
While redistributing the Work or Derivative Works thereof, You may
|
|
178
|
+
choose to offer, and charge a fee for, acceptance of support, warranty,
|
|
179
|
+
indemnity, or other liability obligations and/or rights consistent with
|
|
180
|
+
this License. However, in accepting such obligations, You may act only
|
|
181
|
+
on Your own behalf and on Your sole responsibility, not on behalf of any
|
|
182
|
+
other Contributor, and only if You agree to indemnify, defend, and hold
|
|
183
|
+
each Contributor harmless for any liability incurred by, or claims
|
|
184
|
+
asserted against, such Contributor by reason of your accepting any
|
|
185
|
+
such warranty or additional liability.
|
|
186
|
+
|
|
187
|
+
##### END OF TERMS AND CONDITIONS
|
|
188
|
+
|
|
189
|
+
## APPENDIX: How to apply the Apache License to your work.
|
|
190
|
+
|
|
191
|
+
To apply the Apache License to your work, attach the following
|
|
192
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
193
|
+
replaced with your own identifying information. (Don't include the
|
|
194
|
+
brackets!) The text should be enclosed in the appropriate comment
|
|
195
|
+
syntax for the file format. We also recommend that a file or class name
|
|
196
|
+
and description of purpose be included on the same "printed page" as the
|
|
197
|
+
copyright notice for easier identification within third-party archives.
|
|
198
|
+
|
|
199
|
+
> #### Copyright [yyyy] [name of copyright owner]
|
|
200
|
+
>
|
|
201
|
+
> Licensed under the Apache License, Version 2.0 (the "License");
|
|
202
|
+
> you may not use this file except in compliance with the License.
|
|
203
|
+
> You may obtain a copy of the License at
|
|
204
|
+
>
|
|
205
|
+
> [http://www.apache.org/licenses/](http://www.apache.org/licenses/)
|
|
206
|
+
>
|
|
207
|
+
> Unless required by applicable law or agreed to in writing, software
|
|
208
|
+
> distributed under the License is distributed on an "AS IS" BASIS,
|
|
209
|
+
> WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
210
|
+
> See the License for the specific language governing permissions and
|
|
211
|
+
> limitations under the License.
|
package/NOTICE.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Copyright 2024-2025 Juit GmbH
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
[http://www.apache.org/licenses/](http://www.apache.org/licenses/)
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# Juit Capacitor Updater
|
|
2
|
+
|
|
3
|
+
This library demonstrates how to perform [Capacitor](https://capacitorjs.com/)
|
|
4
|
+
live updates, which update the contents of the web application without requiring
|
|
5
|
+
an update from the app store.
|
|
6
|
+
|
|
7
|
+
Please note that this is not intended to be production quality, but rather a
|
|
8
|
+
simple showcase of the interaction between different Capacitor APIs that can
|
|
9
|
+
be used to perform live updates.
|
|
10
|
+
|
|
11
|
+
* [Outline](#outline)
|
|
12
|
+
* [Functions](#functions)
|
|
13
|
+
* [`updateCapacitor(...)`](#updatecapacitor)
|
|
14
|
+
* [`persistUpdates()`](#persistupdates)
|
|
15
|
+
* [License](LICENSE.md)
|
|
16
|
+
* [Copyright Notice](NOTICE.md)
|
|
17
|
+
|
|
18
|
+
### Outline
|
|
19
|
+
|
|
20
|
+
When Capacitor starts, it serves the web application bundled within the binary
|
|
21
|
+
itself (the `.ipa` for iOS or `.apk` for Android).
|
|
22
|
+
|
|
23
|
+
The [`updateCapacitor(...)`](#updatecapacitor) function performs the following
|
|
24
|
+
steps:
|
|
25
|
+
|
|
26
|
+
1. Downloads a ZIP file from the specified URL.
|
|
27
|
+
2. Uses [JSZip](https://stuk.github.io/jszip) to extract the downloaded ZIP file
|
|
28
|
+
in the [`Library`](https://capacitorjs.com/docs/apis/filesystem#directory)
|
|
29
|
+
directory of the app, following Capacitor's updates naming scheme.
|
|
30
|
+
3. Provides a callback to reload the current web view, pointing the root to the
|
|
31
|
+
extracted ZIP file.
|
|
32
|
+
|
|
33
|
+
Upon restart, the [`persistUpdates()`](#persistupdates) function can be called
|
|
34
|
+
to persist the server base path (where the ZIP file was extracted) and use this
|
|
35
|
+
for all subsequent uses of the app (e.g., when forcefully restarted).
|
|
36
|
+
|
|
37
|
+
### Functions
|
|
38
|
+
|
|
39
|
+
This library exposes two functions:
|
|
40
|
+
|
|
41
|
+
#### `updateCapacitor(...)`
|
|
42
|
+
|
|
43
|
+
The `updateCapacitor(...)` async function downloads the specified URL, extracts
|
|
44
|
+
it on the local device, and provides a callback to reload the web view with the
|
|
45
|
+
updated content. It accepts the following arguments:
|
|
46
|
+
|
|
47
|
+
* `url` _(required)_: A `string` or `URL` from which the ZIP file will be
|
|
48
|
+
downloaded.
|
|
49
|
+
* `progress` _(optional)_: A callback that will be invoked with a number between
|
|
50
|
+
0 and 1 indicating progress. Between 0.5, the
|
|
51
|
+
progress indicates download; between 0.5 and 1, it
|
|
52
|
+
indicates unzipping progress.
|
|
53
|
+
* `verbose` _(optional)_: When `true`, the update process will be logged using
|
|
54
|
+
`console.log(...)`.
|
|
55
|
+
|
|
56
|
+
When successful, this function will asynchronously return a callback
|
|
57
|
+
`() => Promise<void>` that needs to be invoked to reload the web view with the
|
|
58
|
+
updated contents.
|
|
59
|
+
|
|
60
|
+
If this function asynchronously returns `undefined`, it means that the update
|
|
61
|
+
could not be performed (either due to live-reload in Capacitor or when on the
|
|
62
|
+
web).
|
|
63
|
+
|
|
64
|
+
In case of errors during downloading or unzipping, this function will throw an
|
|
65
|
+
error asynchronously (returning a rejected promise).
|
|
66
|
+
|
|
67
|
+
#### `persistUpdates()`
|
|
68
|
+
|
|
69
|
+
Upon initialization of your app, always call `persistUpdates()` to ensure that
|
|
70
|
+
app reloads will point to the latest updated code.
|
|
71
|
+
|
|
72
|
+
This function is safe to call anytime and on any platform, as it will only
|
|
73
|
+
persist the path when the current server base path is indeed a directory
|
|
74
|
+
created during an update with [`updateCapacitor(...)`](#updatecapacitor).
|
|
75
|
+
|
|
76
|
+
This function is also asynchronous and will return a promise to a `boolean`
|
|
77
|
+
indicating `true` when the path has been persisted.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Update Capactitor contents by downloading and unzipping the specified URL.
|
|
3
|
+
*
|
|
4
|
+
* @param url - The URL to download the update from.
|
|
5
|
+
* @returns A function that will reload the WebView with the new contents or
|
|
6
|
+
* `undefined` if updates are not supported (on web, or live reload).
|
|
7
|
+
* @throws If the update fails to download, read, extract, or write.
|
|
8
|
+
*/
|
|
9
|
+
export declare function updateCapacitor(url: string | URL): Promise<(() => Promise<void>) | undefined>;
|
|
10
|
+
/**
|
|
11
|
+
* Update Capactitor contents by downloading and unzipping the specified URL.
|
|
12
|
+
*
|
|
13
|
+
* @param url - The URL to download the update from.
|
|
14
|
+
* @param verbose - Log the progress of the update (default: `false`).
|
|
15
|
+
* @returns A function that will reload the WebView with the new contents or
|
|
16
|
+
* `undefined` if updates are not supported (on web, or live reload).
|
|
17
|
+
* @throws If the update fails to download, read, extract, or write.
|
|
18
|
+
*/
|
|
19
|
+
export declare function updateCapacitor(url: string | URL, verbose: boolean): Promise<(() => Promise<void>) | undefined>;
|
|
20
|
+
/**
|
|
21
|
+
* Update Capactitor contents by downloading and unzipping the specified URL.
|
|
22
|
+
*
|
|
23
|
+
* @param url - The URL to download the update from.
|
|
24
|
+
* @param progress - A function to call with the progress of the update: the
|
|
25
|
+
* function will receive a number between 0 and 1; when this
|
|
26
|
+
* number between 0 and 0.5, the update is downloading, and
|
|
27
|
+
* when it's between 0.5 and 1, the update is extracting.
|
|
28
|
+
* @returns A function that will reload the WebView with the new contents or
|
|
29
|
+
* `undefined` if updates are not supported (on web, or live reload).
|
|
30
|
+
* @throws If the update fails to download, read, extract, or write.
|
|
31
|
+
*/
|
|
32
|
+
export declare function updateCapacitor(url: string | URL, progress: (progress: number) => any): Promise<(() => Promise<void>) | undefined>;
|
|
33
|
+
/**
|
|
34
|
+
* Update Capactitor contents by downloading and unzipping the specified URL.
|
|
35
|
+
*
|
|
36
|
+
* @param url - The URL to download the update from.
|
|
37
|
+
* @param progress - A function to call with the progress of the update: the
|
|
38
|
+
* function will receive a number between 0 and 1; when this
|
|
39
|
+
* number between 0 and 0.5, the update is downloading, and
|
|
40
|
+
* when it's between 0.5 and 1, the update is extracting.
|
|
41
|
+
* @param verbose - Log the progress of the update (default: `false`).
|
|
42
|
+
* @returns A function that will reload the WebView with the new contents or
|
|
43
|
+
* `undefined` if updates are not supported (on web, or live reload).
|
|
44
|
+
* @throws If the update fails to download, read, extract, or write.
|
|
45
|
+
*/
|
|
46
|
+
export declare function updateCapacitor(url: string | URL, progress: (progress: number) => any, verbose: boolean): Promise<(() => Promise<void>) | undefined>;
|
|
47
|
+
/**
|
|
48
|
+
* Persist the current server base path if and only if it is an update path.
|
|
49
|
+
*
|
|
50
|
+
* This function should be called after the appication is loaded successfully,
|
|
51
|
+
* and will update the server base path to the current path if it is an update.
|
|
52
|
+
*
|
|
53
|
+
* In this way, Capacitor will use the update path as the server base path.
|
|
54
|
+
*
|
|
55
|
+
* @returns A boolean indicating whether the update path was persisted or not.
|
|
56
|
+
*/
|
|
57
|
+
export declare function persistUpdates(): Promise<boolean>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// index.ts
|
|
2
|
+
import { Capacitor, WebView } from "@capacitor/core";
|
|
3
|
+
import { Directory, Filesystem } from "@capacitor/filesystem";
|
|
4
|
+
import JSZip from "jszip";
|
|
5
|
+
var UPDATES_DIRECTORY = "NoCloud/ionic_built_snapshots";
|
|
6
|
+
async function updateCapacitor(url, progressOrVerbose, maybeVerbose) {
|
|
7
|
+
const progress = typeof progressOrVerbose === "function" ? progressOrVerbose : () => {
|
|
8
|
+
};
|
|
9
|
+
const verbose = typeof progressOrVerbose === "boolean" ? progressOrVerbose : !!maybeVerbose;
|
|
10
|
+
const log = verbose ? console.log : () => {
|
|
11
|
+
};
|
|
12
|
+
if (!Capacitor.isNativePlatform()) {
|
|
13
|
+
console.warn("Update is only supported on native platforms");
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
if (window.location.protocol !== "capacitor:") {
|
|
17
|
+
console.warn("Update is not supported in Capacitor LiveReload");
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const uuid = crypto.randomUUID();
|
|
21
|
+
const zipFileName = `update-${uuid}.zip`;
|
|
22
|
+
try {
|
|
23
|
+
await Filesystem.addListener("progress", (event) => {
|
|
24
|
+
if (event.url !== url.toString()) return;
|
|
25
|
+
if (!event.bytes) return progress(0);
|
|
26
|
+
progress((event.contentLength || event.bytes) / event.bytes / 2);
|
|
27
|
+
});
|
|
28
|
+
log(`Downloading update from "${url}" as "${zipFileName}"`);
|
|
29
|
+
await Filesystem.downloadFile({
|
|
30
|
+
url: url.toString(),
|
|
31
|
+
path: zipFileName,
|
|
32
|
+
directory: Directory.Cache,
|
|
33
|
+
progress: true
|
|
34
|
+
});
|
|
35
|
+
log(`Reading downloaded ZIP file "${zipFileName}"`);
|
|
36
|
+
const { data } = await Filesystem.readFile({
|
|
37
|
+
path: zipFileName,
|
|
38
|
+
directory: Directory.Cache
|
|
39
|
+
// no encoding, we'll get the BASE64 data
|
|
40
|
+
});
|
|
41
|
+
log(`Reading ZIP file contents from "${zipFileName}"`);
|
|
42
|
+
const zipFile = await new JSZip().loadAsync(data, {
|
|
43
|
+
base64: true,
|
|
44
|
+
// this is what Capacitor gives us
|
|
45
|
+
checkCRC32: true
|
|
46
|
+
});
|
|
47
|
+
const entriesCount = Object.keys(zipFile.files).length;
|
|
48
|
+
let entryIndex = 0;
|
|
49
|
+
for (const zipEntry of Object.values(zipFile.files)) {
|
|
50
|
+
progress(entryIndex++ / entriesCount / 2 + 0.5);
|
|
51
|
+
if (zipEntry.dir) continue;
|
|
52
|
+
const data2 = await zipEntry.async("base64");
|
|
53
|
+
log(`Writing ZIP entry "${zipEntry.name}"`);
|
|
54
|
+
await Filesystem.writeFile({
|
|
55
|
+
path: `${UPDATES_DIRECTORY}/${uuid}/${zipEntry.name}`,
|
|
56
|
+
directory: Directory.Library,
|
|
57
|
+
recursive: true,
|
|
58
|
+
data: data2
|
|
59
|
+
// no encoding, let Capacitor handle the BASE64 data
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
const { uri } = await Filesystem.getUri({
|
|
63
|
+
path: `${UPDATES_DIRECTORY}/${uuid}`,
|
|
64
|
+
directory: Directory.Library
|
|
65
|
+
});
|
|
66
|
+
const serverBasePath = new URL(uri, "file:///").pathname;
|
|
67
|
+
log(`Update extracted to "${serverBasePath}"`);
|
|
68
|
+
progress(1);
|
|
69
|
+
return () => {
|
|
70
|
+
log(`Setting capacitor base path to "${serverBasePath}"`);
|
|
71
|
+
return WebView.setServerBasePath({ path: serverBasePath });
|
|
72
|
+
};
|
|
73
|
+
} finally {
|
|
74
|
+
log(`Deleting ZIP file "${zipFileName}"`);
|
|
75
|
+
await Filesystem.deleteFile({
|
|
76
|
+
path: zipFileName,
|
|
77
|
+
directory: Directory.Cache
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
async function persistUpdates() {
|
|
82
|
+
if (!Capacitor.isNativePlatform()) {
|
|
83
|
+
console.warn("Update is only supported on native platforms");
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
if (window.location.protocol !== "capacitor:") {
|
|
87
|
+
console.warn("Update is not supported in Capacitor LiveReload");
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
const { uri } = await Filesystem.getUri({
|
|
91
|
+
directory: Directory.Library,
|
|
92
|
+
path: UPDATES_DIRECTORY
|
|
93
|
+
});
|
|
94
|
+
const serverBasePathPrefix = new URL(uri, "file:///").pathname;
|
|
95
|
+
const { path } = await WebView.getServerBasePath();
|
|
96
|
+
if (path.startsWith(serverBasePathPrefix)) {
|
|
97
|
+
console.log(`Persisting Capacitor updates from "${path}"`);
|
|
98
|
+
await WebView.persistServerBasePath();
|
|
99
|
+
return true;
|
|
100
|
+
} else {
|
|
101
|
+
console.warn(`Cannot persist updates directory "${path}"`);
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
export {
|
|
106
|
+
persistUpdates,
|
|
107
|
+
updateCapacitor
|
|
108
|
+
};
|
|
109
|
+
//# sourceMappingURL=index.js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AAK,iDAAiD;AAC9D,WAAO;AAAA,EACT;AAGA,QAAM,EAAE,IAAI,IAAI,MAAM,WAAW,OAAO;AAAA,IACtC,WAAW,UAAU;AAAA,IACrB,MAAM;AAAA,EACR,CAAC;AAGD,QAAM,uBAAuB,IAAI,IAAI,KAAK,UAAU,EAAE;AAGtD,QAAM,EAAE,KAAK,IAAI,MAAM,QAAQ,kBAAkB;AAEjD,MAAI,KAAK,WAAW,oBAAoB,GAAG;AACzC,YAAQ,IAAI,sCAAsC,IAAI,GAAG;AACzD,UAAM,QAAQ,sBAAsB;AACpC,WAAO;AAAA,EACT,OAAO;AACL,YAAQ,KAAK,qCAAqC,IAAI,GAAG;AACzD,WAAO;AAAA,EACT;AACF;",
|
|
5
|
+
"names": ["data"]
|
|
6
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@juit/capacitor-updater",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Capacitor utility to update an app from a ZIP file",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "plug",
|
|
18
|
+
"coverage": "plug coverage",
|
|
19
|
+
"dev": "plug coverage -w src -w test",
|
|
20
|
+
"lint": "plug lint",
|
|
21
|
+
"test": "plug test",
|
|
22
|
+
"transpile": "plug transpile"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"jszip": "^3.10.1"
|
|
26
|
+
},
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"@capacitor/core": "^7.1.0",
|
|
29
|
+
"@capacitor/filesystem": "^7.0.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@plugjs/build": "^0.6.33"
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"*.md",
|
|
36
|
+
"dist/"
|
|
37
|
+
],
|
|
38
|
+
"directories": {
|
|
39
|
+
"test": "test"
|
|
40
|
+
},
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "git+ssh://git@github.com/juitnow/juit-capacitor-updater.git"
|
|
44
|
+
},
|
|
45
|
+
"keywords": [
|
|
46
|
+
"capacitor",
|
|
47
|
+
"updater",
|
|
48
|
+
"plugin"
|
|
49
|
+
],
|
|
50
|
+
"author": "Juit Developers <developers@juit.com>",
|
|
51
|
+
"license": "Apache-2.0",
|
|
52
|
+
"bugs": {
|
|
53
|
+
"url": "https://github.com/juitnow/juit-capacitor-updater/issues"
|
|
54
|
+
},
|
|
55
|
+
"homepage": "https://github.com/juitnow/juit-capacitor-updater#readme"
|
|
56
|
+
}
|