@inglorious/web 6.19.0 → 6.21.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/README.md +44 -0
- package/package.json +11 -3
- package/src/back-button/index.js +35 -0
- package/src/live-update/index.js +140 -0
- package/types/back-button.d.ts +24 -0
- package/types/live-update.d.ts +87 -0
package/README.md
CHANGED
|
@@ -1544,6 +1544,35 @@ A failed check keeps the last answer, so nobody is blocked by being offline. Ren
|
|
|
1544
1544
|
|
|
1545
1545
|
---
|
|
1546
1546
|
|
|
1547
|
+
## Live Updates
|
|
1548
|
+
|
|
1549
|
+
A store release is only needed for native changes. `@inglorious/web/live-update` replaces the web code inside a Capacitor app with a newer published bundle, through [`@capawesome/capacitor-live-update`](https://capawesome.io/plugins/live-update/), whose open source plugin downloads from any URL. `publishLiveUpdate` from `@inglorious/supabase/live-update` publishes one.
|
|
1550
|
+
|
|
1551
|
+
```javascript
|
|
1552
|
+
import { App } from "@capacitor/app"
|
|
1553
|
+
import { LiveUpdate } from "@capawesome/capacitor-live-update"
|
|
1554
|
+
import { createLiveUpdate, watchLiveUpdate } from "@inglorious/web/live-update"
|
|
1555
|
+
|
|
1556
|
+
const types = { LiveUpdate: createLiveUpdate({ plugin: LiveUpdate }) }
|
|
1557
|
+
const entities = { liveUpdate: { type: "LiveUpdate" } }
|
|
1558
|
+
|
|
1559
|
+
watchLiveUpdate({
|
|
1560
|
+
plugin: LiveUpdate,
|
|
1561
|
+
app: App,
|
|
1562
|
+
store,
|
|
1563
|
+
manifestUrl,
|
|
1564
|
+
release: Number(import.meta.env.VITE_LIVE_UPDATE_RELEASE),
|
|
1565
|
+
})
|
|
1566
|
+
```
|
|
1567
|
+
|
|
1568
|
+
`watchLiveUpdate` tells the plugin the running bundle works, then reads the manifest at launch, every hour (`checkEveryMs`) and on each resume. The manifest is `{ release, minBuild, url, checksum, signature }`. A bundle is wanted when its `release` is newer than the running one and the installed build is at least `minBuild` (`isLiveUpdateWanted`). It downloads in the background, is applied on the next pause, and is announced as `#liveUpdate:liveUpdateReady` meanwhile; `liveUpdateApply` moves over at once. It does nothing outside a native app, and returns a function that stops watching.
|
|
1569
|
+
|
|
1570
|
+
`release` must grow with every build, like the build's timestamp, and only release builds should have one: a development build without it must not watch, or it would turn into the published code.
|
|
1571
|
+
|
|
1572
|
+
Set `publicKey` in the plugin's Capacitor config so it refuses unsigned bundles, and `readyTimeout` so a bundle that doesn't start is rolled back; with `autoBlockRolledBackBundles`, `watchLiveUpdate` never downloads it again.
|
|
1573
|
+
|
|
1574
|
+
---
|
|
1575
|
+
|
|
1547
1576
|
## App Links
|
|
1548
1577
|
|
|
1549
1578
|
With Universal Links (iOS) and App Links (Android), a link to the app's domain opens the installed app instead of the browser. `@inglorious/web/app-links` sends the router to that link's path, both for the link that started the app and for links that arrive while it runs. It does nothing outside a native app.
|
|
@@ -1561,6 +1590,21 @@ The operating system opens the app only when the domain vouches for it: iOS read
|
|
|
1561
1590
|
|
|
1562
1591
|
---
|
|
1563
1592
|
|
|
1593
|
+
## Back Button
|
|
1594
|
+
|
|
1595
|
+
On Android, the back button and the back gesture go back in history. `@inglorious/web/back-button` closes the app instead on a root path, a top-level page like a tab bar's, and when there is no history left, as other Android apps do. It does nothing outside a native app, and iOS has no back button.
|
|
1596
|
+
|
|
1597
|
+
```javascript
|
|
1598
|
+
import { App } from "@capacitor/app"
|
|
1599
|
+
import { watchBackButton } from "@inglorious/web/back-button"
|
|
1600
|
+
|
|
1601
|
+
watchBackButton(App, { rootPaths: ["/", "/search", "/profile"] })
|
|
1602
|
+
```
|
|
1603
|
+
|
|
1604
|
+
`rootPaths` defaults to `["/"]`; it returns a function that stops listening. Navigate between root paths with `{ to, replace: true }`, so switching tabs does not pile up history.
|
|
1605
|
+
|
|
1606
|
+
---
|
|
1607
|
+
|
|
1564
1608
|
## Forms
|
|
1565
1609
|
|
|
1566
1610
|
`@inglorious/web` includes a small but powerful `form` type for managing form state inside your entity store. It offers:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/web",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.21.0",
|
|
4
4
|
"description": "A new web framework that leverages the power of the Inglorious Store combined with the performance and simplicity of lit-html.",
|
|
5
5
|
"author": "Matteo Antony Mistretta <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
|
|
6
6
|
"contributors": [
|
|
@@ -41,6 +41,10 @@
|
|
|
41
41
|
"types": "./types/app-links.d.ts",
|
|
42
42
|
"import": "./src/app-links/index.js"
|
|
43
43
|
},
|
|
44
|
+
"./back-button": {
|
|
45
|
+
"types": "./types/back-button.d.ts",
|
|
46
|
+
"import": "./src/back-button/index.js"
|
|
47
|
+
},
|
|
44
48
|
"./decorators/with-debounce": {
|
|
45
49
|
"types": "./types/decorators/with-debounce.d.ts",
|
|
46
50
|
"import": "./src/decorators/with-debounce/index.js"
|
|
@@ -66,6 +70,10 @@
|
|
|
66
70
|
"import": "./src/form/index.js"
|
|
67
71
|
},
|
|
68
72
|
"./hydrate": "./src/hydrate.js",
|
|
73
|
+
"./live-update": {
|
|
74
|
+
"types": "./types/live-update.d.ts",
|
|
75
|
+
"import": "./src/live-update/index.js"
|
|
76
|
+
},
|
|
69
77
|
"./mobile": {
|
|
70
78
|
"types": "./types/mobile.d.ts",
|
|
71
79
|
"import": "./src/mobile/index.js"
|
|
@@ -138,9 +146,9 @@
|
|
|
138
146
|
},
|
|
139
147
|
"devDependencies": {
|
|
140
148
|
"@inglorious/eslint-config": "1.2.0",
|
|
141
|
-
"prettier": "^3.9.
|
|
149
|
+
"prettier": "^3.9.10",
|
|
142
150
|
"typescript": "~7.0.2",
|
|
143
|
-
"vitest": "^5.
|
|
151
|
+
"vitest": "^5.1.0"
|
|
144
152
|
},
|
|
145
153
|
"engines": {
|
|
146
154
|
"node": ">= 26"
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { isNative } from "../mobile/index.js"
|
|
2
|
+
|
|
3
|
+
const DEFAULT_ROOT_PATHS = ["/"]
|
|
4
|
+
const BACK_BUTTON_EVENT = "backButton"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Android's back button and back gesture, as other Android apps have them:
|
|
8
|
+
* they go back in history, and close the app on a root path or when there is
|
|
9
|
+
* no history left. Root paths are the top-level pages, like a tab bar's: going
|
|
10
|
+
* back from one leaves the app at once. Does nothing outside a native app, and
|
|
11
|
+
* iOS sends no back button.
|
|
12
|
+
*
|
|
13
|
+
* @param {Object} app `App` from `@capacitor/app`.
|
|
14
|
+
* @param {{ rootPaths?: string[] }} [options] `["/"]` by default.
|
|
15
|
+
* @returns {() => Promise<void>} Stops listening.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* watchBackButton(App, { rootPaths: ["/", "/search", "/profile"] })
|
|
19
|
+
*/
|
|
20
|
+
export function watchBackButton(app, { rootPaths = DEFAULT_ROOT_PATHS } = {}) {
|
|
21
|
+
if (!isNative()) return async () => {}
|
|
22
|
+
|
|
23
|
+
const listener = app.addListener(BACK_BUTTON_EVENT, ({ canGoBack }) => {
|
|
24
|
+
if (canGoBack && !rootPaths.includes(window.location.pathname)) {
|
|
25
|
+
window.history.back()
|
|
26
|
+
} else {
|
|
27
|
+
app.exitApp()
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
return async function stop() {
|
|
32
|
+
const handle = await listener
|
|
33
|
+
await handle.remove()
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { isNative } from "../mobile/index.js"
|
|
2
|
+
|
|
3
|
+
const DEFAULT_ID = "liveUpdate"
|
|
4
|
+
const ONE_HOUR_MS = 3_600_000
|
|
5
|
+
const NO_MIN_BUILD = 0
|
|
6
|
+
const RESUME_EVENT = "resume"
|
|
7
|
+
const PAUSE_EVENT = "pause"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The type that offers a downloaded live update. A native app carries its web
|
|
11
|
+
* code inside the binary; a live update replaces that code without the
|
|
12
|
+
* stores, and `watchLiveUpdate` announces one that is ready as
|
|
13
|
+
* `#${id}:liveUpdateReady`. `liveUpdateApply` moves over to it at once.
|
|
14
|
+
*
|
|
15
|
+
* @param {{ plugin: { reload(): Promise<void> } }} config `LiveUpdate` from
|
|
16
|
+
* `@capawesome/capacitor-live-update`.
|
|
17
|
+
*/
|
|
18
|
+
export function createLiveUpdate({ plugin }) {
|
|
19
|
+
return {
|
|
20
|
+
create(entity) {
|
|
21
|
+
entity.isUpdateReady ??= false
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
liveUpdateReady(entity) {
|
|
25
|
+
entity.isUpdateReady = true
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
liveUpdateApply() {
|
|
29
|
+
plugin.reload()
|
|
30
|
+
},
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Whether a published bundle should replace the running one: it must be newer
|
|
36
|
+
* than what runs, and the installed build must be one it was made for.
|
|
37
|
+
*
|
|
38
|
+
* @param {{ release: number, minBuild?: number } | null} manifest
|
|
39
|
+
* @param {{ release: number, build: number }} installed
|
|
40
|
+
*/
|
|
41
|
+
export function isLiveUpdateWanted(manifest, { release, build }) {
|
|
42
|
+
return (
|
|
43
|
+
Number.isFinite(manifest?.release) &&
|
|
44
|
+
manifest.release > release &&
|
|
45
|
+
build >= (manifest.minBuild ?? NO_MIN_BUILD)
|
|
46
|
+
)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Keeps a native app on its latest published web code. Confirms at once that
|
|
51
|
+
* the running bundle works, so the plugin doesn't roll it back. Then, at launch,
|
|
52
|
+
* every `checkEveryMs` and on each return to the foreground, reads the
|
|
53
|
+
* manifest at `manifestUrl` and downloads a bundle that is wanted
|
|
54
|
+
* (`isLiveUpdateWanted`) and not blocked by an earlier rollback. A downloaded
|
|
55
|
+
* bundle is applied the next time the app goes to the background, and
|
|
56
|
+
* announced as `#${id}:liveUpdateReady` meanwhile. Does nothing outside a
|
|
57
|
+
* native app.
|
|
58
|
+
*
|
|
59
|
+
* @param {Object} options
|
|
60
|
+
* @param {Object} options.plugin `LiveUpdate` from
|
|
61
|
+
* `@capawesome/capacitor-live-update`.
|
|
62
|
+
* @param {Object} options.app `App` from `@capacitor/app`.
|
|
63
|
+
* @param {{ notify: (type: string, payload?: unknown) => void }} [options.store]
|
|
64
|
+
* @param {string} options.manifestUrl
|
|
65
|
+
* @param {number} options.release The release of the running web code.
|
|
66
|
+
* @param {string} [options.id]
|
|
67
|
+
* @param {number} [options.checkEveryMs]
|
|
68
|
+
* @returns {() => Promise<void>} Stops watching.
|
|
69
|
+
*/
|
|
70
|
+
export function watchLiveUpdate({
|
|
71
|
+
plugin,
|
|
72
|
+
app,
|
|
73
|
+
store,
|
|
74
|
+
manifestUrl,
|
|
75
|
+
release,
|
|
76
|
+
id = DEFAULT_ID,
|
|
77
|
+
checkEveryMs = ONE_HOUR_MS,
|
|
78
|
+
}) {
|
|
79
|
+
if (!isNative()) return async () => {}
|
|
80
|
+
|
|
81
|
+
let pending = null
|
|
82
|
+
let isChecking = false
|
|
83
|
+
|
|
84
|
+
plugin.ready().catch(() => {})
|
|
85
|
+
check()
|
|
86
|
+
const interval = setInterval(check, checkEveryMs)
|
|
87
|
+
const listeners = [
|
|
88
|
+
app.addListener(RESUME_EVENT, check),
|
|
89
|
+
app.addListener(PAUSE_EVENT, apply),
|
|
90
|
+
]
|
|
91
|
+
|
|
92
|
+
return async function stop() {
|
|
93
|
+
clearInterval(interval)
|
|
94
|
+
for (const listener of listeners) {
|
|
95
|
+
const handle = await listener
|
|
96
|
+
await handle.remove()
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function apply() {
|
|
101
|
+
if (pending !== null) plugin.reload().catch(() => {})
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async function check() {
|
|
105
|
+
if (isChecking) return
|
|
106
|
+
isChecking = true
|
|
107
|
+
try {
|
|
108
|
+
const manifest = await latest()
|
|
109
|
+
const { build } = await app.getInfo()
|
|
110
|
+
if (!isLiveUpdateWanted(manifest, { release, build: Number(build) })) {
|
|
111
|
+
return
|
|
112
|
+
}
|
|
113
|
+
if (pending === manifest.release) return
|
|
114
|
+
await prepare(manifest)
|
|
115
|
+
pending = manifest.release
|
|
116
|
+
store?.notify(`#${id}:liveUpdateReady`)
|
|
117
|
+
} catch {
|
|
118
|
+
// Offline, or a bundle that fails its checks: the running code stays.
|
|
119
|
+
} finally {
|
|
120
|
+
isChecking = false
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async function latest() {
|
|
125
|
+
const response = await fetch(manifestUrl, { cache: "no-store" })
|
|
126
|
+
if (!response.ok) throw new Error(`Manifest: ${response.status}`)
|
|
127
|
+
return response.json()
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async function prepare({ release: next, url, checksum, signature }) {
|
|
131
|
+
const bundleId = String(next)
|
|
132
|
+
const { bundleIds: blocked } = await plugin.getBlockedBundles()
|
|
133
|
+
if (blocked.includes(bundleId)) throw new Error("Rolled back before")
|
|
134
|
+
const { bundleIds: downloaded } = await plugin.getDownloadedBundles()
|
|
135
|
+
if (!downloaded.includes(bundleId)) {
|
|
136
|
+
await plugin.downloadBundle({ url, bundleId, checksum, signature })
|
|
137
|
+
}
|
|
138
|
+
await plugin.setNextBundle({ bundleId })
|
|
139
|
+
}
|
|
140
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** What `watchBackButton` needs of `App` from `@capacitor/app`. */
|
|
2
|
+
export interface BackButtonApp {
|
|
3
|
+
exitApp(): Promise<void>
|
|
4
|
+
addListener(
|
|
5
|
+
event: "backButton",
|
|
6
|
+
listener: (event: { canGoBack: boolean }) => void,
|
|
7
|
+
): Promise<{ remove(): Promise<void> }>
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface WatchBackButtonOptions {
|
|
11
|
+
/** The top-level paths, where back closes the app. Defaults to `["/"]`. */
|
|
12
|
+
rootPaths?: string[]
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Android's back button and back gesture: back in history, or closes the app
|
|
17
|
+
* on a root path or when there is no history left. Does nothing outside a
|
|
18
|
+
* native app.
|
|
19
|
+
* @returns A function that stops listening.
|
|
20
|
+
*/
|
|
21
|
+
export declare function watchBackButton(
|
|
22
|
+
app: BackButtonApp,
|
|
23
|
+
options?: WatchBackButtonOptions,
|
|
24
|
+
): () => Promise<void>
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { Api } from "./mount"
|
|
2
|
+
|
|
3
|
+
/** What a publisher puts at the manifest URL. */
|
|
4
|
+
export interface LiveUpdateManifest {
|
|
5
|
+
/** Grows with every bundle: newer than the running release means wanted. */
|
|
6
|
+
release: number
|
|
7
|
+
/** The oldest native build that can run this bundle. */
|
|
8
|
+
minBuild?: number
|
|
9
|
+
/** Where the bundle's zip lives. */
|
|
10
|
+
url: string
|
|
11
|
+
/** The zip's SHA-256, hex. */
|
|
12
|
+
checksum?: string
|
|
13
|
+
/** The zip's RSA-SHA256 signature, base64. */
|
|
14
|
+
signature?: string
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** What the live update types need of `LiveUpdate` from `@capawesome/capacitor-live-update`. */
|
|
18
|
+
export interface LiveUpdatePlugin {
|
|
19
|
+
ready(): Promise<unknown>
|
|
20
|
+
reload(): Promise<void>
|
|
21
|
+
getBlockedBundles(): Promise<{ bundleIds: string[] }>
|
|
22
|
+
getDownloadedBundles(): Promise<{ bundleIds: string[] }>
|
|
23
|
+
downloadBundle(options: {
|
|
24
|
+
url: string
|
|
25
|
+
bundleId: string
|
|
26
|
+
checksum?: string
|
|
27
|
+
signature?: string
|
|
28
|
+
}): Promise<void>
|
|
29
|
+
setNextBundle(options: { bundleId: string | null }): Promise<void>
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** What `watchLiveUpdate` needs of `App` from `@capacitor/app`. */
|
|
33
|
+
export interface LiveUpdateApp {
|
|
34
|
+
getInfo(): Promise<{ build: string }>
|
|
35
|
+
addListener(
|
|
36
|
+
event: "resume" | "pause",
|
|
37
|
+
listener: () => void,
|
|
38
|
+
): Promise<{ remove(): Promise<void> }>
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface LiveUpdateEntity {
|
|
42
|
+
id?: string
|
|
43
|
+
type: string
|
|
44
|
+
/** A bundle is downloaded and waits for the next trip to the background. */
|
|
45
|
+
isUpdateReady: boolean
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export type LiveUpdateType = {
|
|
49
|
+
create(entity: LiveUpdateEntity): void
|
|
50
|
+
liveUpdateReady(entity: LiveUpdateEntity): void
|
|
51
|
+
/** Moves over to the downloaded bundle now, reloading the app. */
|
|
52
|
+
liveUpdateApply(): void
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The type that offers a downloaded live update. */
|
|
56
|
+
export declare function createLiveUpdate(config: {
|
|
57
|
+
plugin: Pick<LiveUpdatePlugin, "reload">
|
|
58
|
+
}): LiveUpdateType
|
|
59
|
+
|
|
60
|
+
/** Whether a published bundle should replace the running one. */
|
|
61
|
+
export declare function isLiveUpdateWanted(
|
|
62
|
+
manifest: Pick<LiveUpdateManifest, "release" | "minBuild"> | null,
|
|
63
|
+
installed: { release: number; build: number },
|
|
64
|
+
): boolean
|
|
65
|
+
|
|
66
|
+
export interface WatchLiveUpdateOptions {
|
|
67
|
+
plugin: LiveUpdatePlugin
|
|
68
|
+
app: LiveUpdateApp
|
|
69
|
+
/** Where to announce a downloaded bundle. */
|
|
70
|
+
store?: Pick<Api, "notify">
|
|
71
|
+
manifestUrl: string
|
|
72
|
+
/** The release of the running web code. */
|
|
73
|
+
release: number
|
|
74
|
+
/** The LiveUpdate entity to announce to. Defaults to `liveUpdate`. */
|
|
75
|
+
id?: string
|
|
76
|
+
/** How often to check for a new bundle. Defaults to one hour. */
|
|
77
|
+
checkEveryMs?: number
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Keeps a native app on its latest published web code, applying a downloaded
|
|
82
|
+
* bundle on the next trip to the background. Does nothing outside a native app.
|
|
83
|
+
* @returns A function that stops watching.
|
|
84
|
+
*/
|
|
85
|
+
export declare function watchLiveUpdate(
|
|
86
|
+
options: WatchLiveUpdateOptions,
|
|
87
|
+
): () => Promise<void>
|