@hamur/pwa 0.0.0-stage → 0.0.1
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 +22 -0
- package/README.md +14 -2
- package/dist/chunks/bun-index-33c14md9.js +11 -0
- package/dist/chunks/bun-index-5jwxzmvt.js +133 -0
- package/dist/src/adapters/html.js +28 -0
- package/dist/src/adapters/preact.js +23 -0
- package/dist/src/adapters/react.js +19 -0
- package/dist/src/adapters/solid.js +29 -0
- package/dist/src/adapters/svelte.js +32 -0
- package/dist/src/adapters/vue.js +31 -0
- package/dist/src/client/index.js +43 -0
- package/dist/src/index.js +178 -0
- package/dist/src/server/index.js +202 -0
- package/dist/types/src/adapters/bind.d.ts +4 -0
- package/dist/types/src/adapters/html.d.ts +3 -0
- package/dist/types/src/adapters/preact.d.ts +3 -0
- package/dist/types/src/adapters/react.d.ts +3 -0
- package/dist/types/src/adapters/solid.d.ts +6 -0
- package/dist/types/src/adapters/svelte.d.ts +3 -0
- package/dist/types/src/adapters/vue.d.ts +6 -0
- package/dist/types/src/build/icons.d.ts +7 -0
- package/dist/types/src/client/index.d.ts +3 -0
- package/dist/types/src/client/plugin.d.ts +4 -0
- package/dist/types/src/client/push.d.ts +4 -0
- package/dist/types/src/client/store.d.ts +34 -0
- package/dist/types/src/index.d.ts +8 -0
- package/dist/types/src/module.d.ts +3 -0
- package/dist/types/src/options.d.ts +13 -0
- package/dist/types/src/server/encrypt.d.ts +18 -0
- package/dist/types/src/server/index.d.ts +2 -0
- package/dist/types/src/server/push.d.ts +28 -0
- package/dist/types/src/server/vapid.d.ts +7 -0
- package/dist/types/src/types.d.ts +16 -0
- package/package.json +102 -4
- package/src/adapters/bind.ts +10 -0
- package/src/adapters/html.ts +24 -0
- package/src/adapters/preact.ts +16 -0
- package/src/adapters/react.ts +12 -0
- package/src/adapters/solid.ts +22 -0
- package/src/adapters/svelte.ts +28 -0
- package/src/adapters/vue.ts +24 -0
- package/src/build/icons.ts +60 -0
- package/src/client/index.ts +3 -0
- package/src/client/plugin.ts +11 -0
- package/src/client/push.ts +37 -0
- package/src/client/store.ts +174 -0
- package/src/env.d.ts +4 -0
- package/src/index.ts +12 -0
- package/src/module.ts +110 -0
- package/src/options.ts +55 -0
- package/src/server/encrypt.ts +84 -0
- package/src/server/index.ts +2 -0
- package/src/server/push.ts +146 -0
- package/src/server/vapid.ts +32 -0
- package/src/types.ts +17 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// packages/pwa/src/server/push.ts
|
|
3
|
+
import { fromBase64Url, privateAddress, toBase64Url as toBase64Url2 } from "hamur/server/internal";
|
|
4
|
+
|
|
5
|
+
// packages/pwa/src/server/encrypt.ts
|
|
6
|
+
var RECORD_SIZE = 4096;
|
|
7
|
+
var MAX_PAYLOAD = 3993;
|
|
8
|
+
var SALT_BYTES = 16;
|
|
9
|
+
var KEY_BYTES = 65;
|
|
10
|
+
var HEADER_BYTES = SALT_BYTES + 4 + 1 + KEY_BYTES;
|
|
11
|
+
var DELIMITER = 2;
|
|
12
|
+
var P256 = { name: "ECDH", namedCurve: "P-256" };
|
|
13
|
+
var encoder = new TextEncoder;
|
|
14
|
+
var KEY_INFO = encoder.encode("WebPush: info\x00");
|
|
15
|
+
var CEK_INFO = encoder.encode("Content-Encoding: aes128gcm\x00");
|
|
16
|
+
var NONCE_INFO = encoder.encode("Content-Encoding: nonce\x00");
|
|
17
|
+
function concat(...parts) {
|
|
18
|
+
const out = new Uint8Array(parts.reduce((total, part) => total + part.length, 0));
|
|
19
|
+
let offset = 0;
|
|
20
|
+
for (const part of parts) {
|
|
21
|
+
out.set(part, offset);
|
|
22
|
+
offset += part.length;
|
|
23
|
+
}
|
|
24
|
+
return out;
|
|
25
|
+
}
|
|
26
|
+
async function hkdf(salt, ikm, info, bytes) {
|
|
27
|
+
const key = await crypto.subtle.importKey("raw", ikm, "HKDF", false, ["deriveBits"]);
|
|
28
|
+
return new Uint8Array(await crypto.subtle.deriveBits({ name: "HKDF", hash: "SHA-256", salt, info }, key, bytes * 8));
|
|
29
|
+
}
|
|
30
|
+
async function importPublicKey(point) {
|
|
31
|
+
if (!point || point.length !== KEY_BYTES || point[0] !== 4)
|
|
32
|
+
return;
|
|
33
|
+
try {
|
|
34
|
+
return await crypto.subtle.importKey("raw", point, P256, false, []);
|
|
35
|
+
} catch {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async function senderKeys() {
|
|
40
|
+
const pair = await crypto.subtle.generateKey(P256, false, ["deriveBits"]);
|
|
41
|
+
return {
|
|
42
|
+
publicKey: new Uint8Array(await crypto.subtle.exportKey("raw", pair.publicKey)),
|
|
43
|
+
privateKey: pair.privateKey
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
async function encryptPayload(plaintext, receiver, sender, salt = crypto.getRandomValues(new Uint8Array(SALT_BYTES))) {
|
|
47
|
+
const own = sender ?? await senderKeys();
|
|
48
|
+
const secret = new Uint8Array(await crypto.subtle.deriveBits({ name: "ECDH", public: receiver.key }, own.privateKey, 256));
|
|
49
|
+
const ikm = await hkdf(receiver.auth, secret, concat(KEY_INFO, receiver.publicKey, own.publicKey), 32);
|
|
50
|
+
const cek = await crypto.subtle.importKey("raw", await hkdf(salt, ikm, CEK_INFO, 16), "AES-GCM", false, ["encrypt"]);
|
|
51
|
+
const nonce = await hkdf(salt, ikm, NONCE_INFO, 12);
|
|
52
|
+
const record = new Uint8Array(await crypto.subtle.encrypt({ name: "AES-GCM", iv: nonce }, cek, concat(plaintext, Uint8Array.of(DELIMITER))));
|
|
53
|
+
const header = new Uint8Array(HEADER_BYTES);
|
|
54
|
+
header.set(salt, 0);
|
|
55
|
+
new DataView(header.buffer).setUint32(SALT_BYTES, RECORD_SIZE);
|
|
56
|
+
header[SALT_BYTES + 4] = KEY_BYTES;
|
|
57
|
+
header.set(own.publicKey, SALT_BYTES + 5);
|
|
58
|
+
return concat(header, record);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// packages/pwa/src/server/vapid.ts
|
|
62
|
+
import { signJWT } from "hamur/server";
|
|
63
|
+
import { toBase64Url } from "hamur/server/internal";
|
|
64
|
+
var VAPID_LIFETIME = 12 * 60 * 60;
|
|
65
|
+
async function generateVapidKeys() {
|
|
66
|
+
const pair = await crypto.subtle.generateKey({ name: "ECDSA", namedCurve: "P-256" }, true, [
|
|
67
|
+
"sign",
|
|
68
|
+
"verify"
|
|
69
|
+
]);
|
|
70
|
+
const publicKey = new Uint8Array(await crypto.subtle.exportKey("raw", pair.publicKey));
|
|
71
|
+
const { d } = await crypto.subtle.exportKey("jwk", pair.privateKey);
|
|
72
|
+
return { publicKey: toBase64Url(publicKey), privateKey: d };
|
|
73
|
+
}
|
|
74
|
+
async function vapidAuthorization(endpoint, subject, publicKey, privateKey) {
|
|
75
|
+
const jwk = {
|
|
76
|
+
kty: "EC",
|
|
77
|
+
crv: "P-256",
|
|
78
|
+
x: toBase64Url(publicKey.subarray(1, 33)),
|
|
79
|
+
y: toBase64Url(publicKey.subarray(33, 65)),
|
|
80
|
+
d: privateKey
|
|
81
|
+
};
|
|
82
|
+
const token = await signJWT({ aud: endpoint.origin, sub: subject }, jwk, { alg: "ES256", expiresIn: VAPID_LIFETIME });
|
|
83
|
+
return `vapid t=${token}, k=${toBase64Url(publicKey)}`;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// packages/pwa/src/server/push.ts
|
|
87
|
+
var URGENCIES = ["very-low", "low", "normal", "high"];
|
|
88
|
+
var MAX_TTL = 2419200;
|
|
89
|
+
var TOPIC = /^[A-Za-z0-9_-]{1,32}$/;
|
|
90
|
+
var AUTH_BYTES = 16;
|
|
91
|
+
var PRIVATE_BYTES = 32;
|
|
92
|
+
var DEFAULT_TIMEOUT = 1e4;
|
|
93
|
+
var IPV4 = /^\d{1,3}(?:\.\d{1,3}){3}$/;
|
|
94
|
+
var encoder2 = new TextEncoder;
|
|
95
|
+
var shown = (value) => typeof value === "number" ? String(value) : JSON.stringify(value) ?? String(value);
|
|
96
|
+
var isObject = (value) => typeof value === "object" && value !== null;
|
|
97
|
+
var bytesOf = (value) => typeof value === "string" ? fromBase64Url(value) : undefined;
|
|
98
|
+
function fail(message) {
|
|
99
|
+
throw new TypeError(`sendPush: ${message}`);
|
|
100
|
+
}
|
|
101
|
+
function subjectOf(subject) {
|
|
102
|
+
const url = typeof subject === "string" && URL.canParse(subject) ? new URL(subject) : undefined;
|
|
103
|
+
if (!url || !(url.protocol === "mailto:" && url.pathname !== "" || url.protocol === "https:" && url.host !== ""))
|
|
104
|
+
fail(`vapid.subject must be a mailto: or https: URL (got ${shown(subject)})`);
|
|
105
|
+
return subject;
|
|
106
|
+
}
|
|
107
|
+
function localHost(hostname) {
|
|
108
|
+
if (hostname === "localhost" || hostname.endsWith(".localhost"))
|
|
109
|
+
return true;
|
|
110
|
+
return (hostname.startsWith("[") || IPV4.test(hostname)) && privateAddress(hostname);
|
|
111
|
+
}
|
|
112
|
+
function endpointOf(endpoint, insecure) {
|
|
113
|
+
const url = typeof endpoint === "string" && URL.canParse(endpoint) ? new URL(endpoint) : undefined;
|
|
114
|
+
if (insecure) {
|
|
115
|
+
if (!url || url.protocol !== "https:" && url.protocol !== "http:")
|
|
116
|
+
fail("subscription.endpoint must be an http: or https: URL");
|
|
117
|
+
return url;
|
|
118
|
+
}
|
|
119
|
+
if (!url || url.protocol !== "https:")
|
|
120
|
+
fail("subscription.endpoint must be an https: URL");
|
|
121
|
+
if (localHost(url.hostname))
|
|
122
|
+
fail(`subscription.endpoint must not be a local or private address (got host ${shown(url.hostname)})`);
|
|
123
|
+
return url;
|
|
124
|
+
}
|
|
125
|
+
var typeOf = (value) => value === null ? "null" : Array.isArray(value) ? "an array" : typeof value;
|
|
126
|
+
async function sendPush(subscription, payload, options) {
|
|
127
|
+
const {
|
|
128
|
+
vapid,
|
|
129
|
+
ttl = 86400,
|
|
130
|
+
urgency = "normal",
|
|
131
|
+
topic,
|
|
132
|
+
timeout = DEFAULT_TIMEOUT,
|
|
133
|
+
allowInsecureEndpoint = false
|
|
134
|
+
} = options ?? {};
|
|
135
|
+
const subject = subjectOf(isObject(vapid) ? vapid.subject : undefined);
|
|
136
|
+
const vapidPublic = bytesOf(vapid.publicKey);
|
|
137
|
+
if (!await importPublicKey(vapidPublic))
|
|
138
|
+
fail("vapid.publicKey must be a base64url P-256 public key");
|
|
139
|
+
if (bytesOf(vapid.privateKey)?.length !== PRIVATE_BYTES)
|
|
140
|
+
fail("vapid.privateKey must be a base64url P-256 private key");
|
|
141
|
+
const endpoint = endpointOf(isObject(subscription) ? subscription.endpoint : undefined, allowInsecureEndpoint === true);
|
|
142
|
+
const keys = isObject(subscription) && isObject(subscription.keys) ? subscription.keys : {};
|
|
143
|
+
const receiverPublic = bytesOf(keys.p256dh);
|
|
144
|
+
const receiverKey = await importPublicKey(receiverPublic);
|
|
145
|
+
if (!receiverKey)
|
|
146
|
+
fail("subscription.keys.p256dh must be a base64url P-256 public key");
|
|
147
|
+
const auth = bytesOf(keys.auth);
|
|
148
|
+
if (auth?.length !== AUTH_BYTES)
|
|
149
|
+
fail("subscription.keys.auth must be 16 bytes in base64url");
|
|
150
|
+
if (!Number.isInteger(ttl) || ttl < 0 || ttl > MAX_TTL)
|
|
151
|
+
fail(`ttl must be an integer from 0 to ${MAX_TTL} (got ${shown(ttl)})`);
|
|
152
|
+
if (!URGENCIES.includes(urgency))
|
|
153
|
+
fail(`urgency must be "very-low", "low", "normal" or "high" (got ${shown(urgency)})`);
|
|
154
|
+
if (topic !== undefined && !(typeof topic === "string" && TOPIC.test(topic)))
|
|
155
|
+
fail(`topic must be at most 32 characters of A\u2013Z, a\u2013z, 0\u20139, - and _ (got ${shown(topic)})`);
|
|
156
|
+
if (!Number.isInteger(timeout) || timeout <= 0)
|
|
157
|
+
fail(`timeout must be a positive integer of milliseconds (got ${shown(timeout)})`);
|
|
158
|
+
if (!(typeof payload === "string" || payload instanceof Uint8Array || payload === null))
|
|
159
|
+
fail(`payload must be a string, a Uint8Array or null (got ${typeOf(payload)})`);
|
|
160
|
+
const plaintext = typeof payload === "string" ? encoder2.encode(payload) : payload;
|
|
161
|
+
if (plaintext && plaintext.length > MAX_PAYLOAD)
|
|
162
|
+
throw new RangeError(`sendPush: the payload is ${plaintext.length} bytes; a push message holds at most ${MAX_PAYLOAD}`);
|
|
163
|
+
let authorization;
|
|
164
|
+
try {
|
|
165
|
+
authorization = await vapidAuthorization(endpoint, subject, vapidPublic, toBase64Url2(bytesOf(vapid.privateKey)));
|
|
166
|
+
} catch (error) {
|
|
167
|
+
if (error instanceof TypeError)
|
|
168
|
+
fail("vapid.privateKey must be a base64url P-256 private key");
|
|
169
|
+
throw error;
|
|
170
|
+
}
|
|
171
|
+
const headers = new Headers({ authorization, ttl: String(ttl), urgency });
|
|
172
|
+
if (topic !== undefined)
|
|
173
|
+
headers.set("topic", topic);
|
|
174
|
+
let body;
|
|
175
|
+
if (plaintext) {
|
|
176
|
+
body = await encryptPayload(plaintext, { publicKey: receiverPublic, key: receiverKey, auth });
|
|
177
|
+
headers.set("content-encoding", "aes128gcm");
|
|
178
|
+
headers.set("content-type", "application/octet-stream");
|
|
179
|
+
}
|
|
180
|
+
let response;
|
|
181
|
+
try {
|
|
182
|
+
response = await fetch(endpoint, {
|
|
183
|
+
method: "POST",
|
|
184
|
+
headers,
|
|
185
|
+
redirect: "manual",
|
|
186
|
+
signal: AbortSignal.timeout(timeout),
|
|
187
|
+
...body ? { body } : {}
|
|
188
|
+
});
|
|
189
|
+
} catch (error) {
|
|
190
|
+
if (error?.name === "TimeoutError")
|
|
191
|
+
throw new Error(`sendPush: the push service did not answer within ${timeout} ms`);
|
|
192
|
+
throw error;
|
|
193
|
+
}
|
|
194
|
+
await response.body?.cancel().catch(() => {
|
|
195
|
+
return;
|
|
196
|
+
});
|
|
197
|
+
return { status: response.status, gone: response.status === 404 || response.status === 410 };
|
|
198
|
+
}
|
|
199
|
+
export {
|
|
200
|
+
generateVapidKeys,
|
|
201
|
+
sendPush
|
|
202
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const APPLE_SIZE = 180;
|
|
2
|
+
type Key = "source" | "maskable";
|
|
3
|
+
/** Reads `file` and refuses it, in the words of the design, unless it is a square PNG, JPEG or WebP of at least `largest` px. */
|
|
4
|
+
export declare function readSource(key: Key, shown: string, file: string, largest: number): Promise<Uint8Array<ArrayBuffer>>;
|
|
5
|
+
/** `new Bun.Image(source).resize(n, n).png()`; a result is kept per file, modification time and size so `hamur dev` restarts do not decode again. */
|
|
6
|
+
export declare function resizeIcon(file: string, mtimeMs: number, bytes: Uint8Array, size: number): Promise<Uint8Array<ArrayBuffer>>;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare const NO_WORKER = "subscribePush needs a service worker (app/sw.ts or serviceWorker.generate)";
|
|
2
|
+
export declare function subscribeWith(container: ServiceWorkerContainer | undefined, publicKey: string, wait?: number): Promise<PushSubscriptionJSON>;
|
|
3
|
+
/** Subscribes the page's service worker registration to push with the app's VAPID public key. */
|
|
4
|
+
export declare function subscribePush(publicKey: string): Promise<PushSubscriptionJSON>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/** What `usePwa()` reads. */
|
|
2
|
+
export interface PwaState {
|
|
3
|
+
readonly needRefresh: boolean;
|
|
4
|
+
readonly offlineReady: boolean;
|
|
5
|
+
readonly installable: boolean;
|
|
6
|
+
}
|
|
7
|
+
export type PwaInstallOutcome = "accepted" | "dismissed" | "unavailable";
|
|
8
|
+
/** What `usePwa()` calls. */
|
|
9
|
+
export interface PwaActions {
|
|
10
|
+
update(): Promise<void>;
|
|
11
|
+
install(): Promise<PwaInstallOutcome>;
|
|
12
|
+
}
|
|
13
|
+
export interface PwaStartOptions {
|
|
14
|
+
registerType: "prompt" | "autoUpdate";
|
|
15
|
+
}
|
|
16
|
+
export interface PwaStore extends PwaActions {
|
|
17
|
+
get(): PwaState;
|
|
18
|
+
subscribe(listener: () => void): () => void;
|
|
19
|
+
/** Starts listening once; the client app plugin calls it before the page mounts. */
|
|
20
|
+
start(options: PwaStartOptions): void;
|
|
21
|
+
/** Asks the registration to look for a new `sw.js` (the app update found a new build); a failure is ignored. */
|
|
22
|
+
refresh(): Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
export interface PwaEnvironment {
|
|
25
|
+
window: {
|
|
26
|
+
addEventListener(type: string, listener: (event: Event) => void): void;
|
|
27
|
+
};
|
|
28
|
+
serviceWorker: ServiceWorkerContainer | undefined;
|
|
29
|
+
reload(): void;
|
|
30
|
+
}
|
|
31
|
+
export declare const PWA_OFF: PwaState;
|
|
32
|
+
export declare function createPwaStore(env: PwaEnvironment): PwaStore;
|
|
33
|
+
/** The page's store: one per document; on the server an inert one that reads all `false`. */
|
|
34
|
+
export declare function pwaStore(): PwaStore;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { PwaOptions } from "./types.ts";
|
|
2
|
+
export declare const DEFAULT_SIZES: number[];
|
|
3
|
+
export interface ResolvedPwaOptions {
|
|
4
|
+
registerType: "prompt" | "autoUpdate";
|
|
5
|
+
icons: false | {
|
|
6
|
+
source: string;
|
|
7
|
+
maskable?: string;
|
|
8
|
+
sizes: number[];
|
|
9
|
+
apple: boolean;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
/** Checks the `pwa` key and fills its defaults. */
|
|
13
|
+
export declare function resolveOptions(input: PwaOptions | undefined): ResolvedPwaOptions;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare const RECORD_SIZE = 4096;
|
|
2
|
+
export declare const MAX_PAYLOAD = 3993;
|
|
3
|
+
export interface SenderKeys {
|
|
4
|
+
publicKey: Uint8Array<ArrayBuffer>;
|
|
5
|
+
privateKey: CryptoKey;
|
|
6
|
+
}
|
|
7
|
+
/** A P-256 public key from its 65-byte uncompressed point, or `undefined` when the bytes are not a point of the curve. */
|
|
8
|
+
export declare function importPublicKey(point: Uint8Array<ArrayBuffer> | undefined): Promise<CryptoKey | undefined>;
|
|
9
|
+
export declare function senderKeys(): Promise<SenderKeys>;
|
|
10
|
+
/**
|
|
11
|
+
* RFC 8291 §3.4 over RFC 8188 `aes128gcm`: one record of `rs` 4096, the body `salt | rs | idlen | keyid | record` where `keyid`
|
|
12
|
+
* is the sender's public point. `sender` and `salt` are fresh per message unless a test fixes them.
|
|
13
|
+
*/
|
|
14
|
+
export declare function encryptPayload(plaintext: Uint8Array, receiver: {
|
|
15
|
+
publicKey: Uint8Array<ArrayBuffer>;
|
|
16
|
+
key: CryptoKey;
|
|
17
|
+
auth: Uint8Array<ArrayBuffer>;
|
|
18
|
+
}, sender?: SenderKeys, salt?: Uint8Array<ArrayBuffer>): Promise<Uint8Array<ArrayBuffer>>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
type Urgency = "very-low" | "low" | "normal" | "high";
|
|
2
|
+
interface PushSubscriptionData {
|
|
3
|
+
endpoint: string;
|
|
4
|
+
keys: {
|
|
5
|
+
p256dh: string;
|
|
6
|
+
auth: string;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
interface SendPushOptions {
|
|
10
|
+
vapid: {
|
|
11
|
+
subject: string;
|
|
12
|
+
publicKey: string;
|
|
13
|
+
privateKey: string;
|
|
14
|
+
};
|
|
15
|
+
ttl?: number;
|
|
16
|
+
urgency?: Urgency;
|
|
17
|
+
topic?: string;
|
|
18
|
+
/** Milliseconds the push service has to answer, default 10000. */
|
|
19
|
+
timeout?: number;
|
|
20
|
+
/** Accepts `http:` endpoints and local or private hosts, for a push service in tests. */
|
|
21
|
+
allowInsecureEndpoint?: boolean;
|
|
22
|
+
}
|
|
23
|
+
/** One push message, encrypted with RFC 8291 and signed with RFC 8292 VAPID, posted to the subscription's endpoint. */
|
|
24
|
+
export declare function sendPush(subscription: PushSubscriptionData, payload: string | Uint8Array | null, options: SendPushOptions): Promise<{
|
|
25
|
+
status: number;
|
|
26
|
+
gone: boolean;
|
|
27
|
+
}>;
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const VAPID_LIFETIME: number;
|
|
2
|
+
export declare function generateVapidKeys(): Promise<{
|
|
3
|
+
publicKey: string;
|
|
4
|
+
privateKey: string;
|
|
5
|
+
}>;
|
|
6
|
+
/** RFC 8292 §2 and §3: an ES256 JWT for the endpoint's origin, sent with the public key as `vapid t=<jwt>, k=<key>`. */
|
|
7
|
+
export declare function vapidAuthorization(endpoint: URL, subject: string, publicKey: Uint8Array<ArrayBuffer>, privateKey: string): Promise<string>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** Icons made from one source image. */
|
|
2
|
+
export interface PwaIcons {
|
|
3
|
+
/** A PNG, JPEG or WebP file from `rootDir`, square and at least as large as the largest size. */
|
|
4
|
+
source: string;
|
|
5
|
+
/** A second source drawn with the safe zone of a maskable icon; the plain source is never padded. */
|
|
6
|
+
maskable?: string;
|
|
7
|
+
/** Default `[192, 512]`. */
|
|
8
|
+
sizes?: number[];
|
|
9
|
+
/** Default `true`: a 180 px `apple-touch-icon`, unless `app/apple-icon.*` exists. */
|
|
10
|
+
apple?: boolean;
|
|
11
|
+
}
|
|
12
|
+
/** The `pwa` config key. */
|
|
13
|
+
export interface PwaOptions {
|
|
14
|
+
registerType?: "prompt" | "autoUpdate";
|
|
15
|
+
icons?: PwaIcons | false;
|
|
16
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,104 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hamur/pwa",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/productdevbook/hamur.git",
|
|
9
|
+
"directory": "packages/pwa"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/types/src/index.d.ts",
|
|
14
|
+
"default": "./dist/src/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./client": {
|
|
17
|
+
"types": "./dist/types/src/client/index.d.ts",
|
|
18
|
+
"default": "./dist/src/client/index.js"
|
|
19
|
+
},
|
|
20
|
+
"./server": {
|
|
21
|
+
"types": "./dist/types/src/server/index.d.ts",
|
|
22
|
+
"default": "./dist/src/server/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./react": {
|
|
25
|
+
"types": "./dist/types/src/adapters/react.d.ts",
|
|
26
|
+
"default": "./dist/src/adapters/react.js"
|
|
27
|
+
},
|
|
28
|
+
"./vue": {
|
|
29
|
+
"types": "./dist/types/src/adapters/vue.d.ts",
|
|
30
|
+
"default": "./dist/src/adapters/vue.js"
|
|
31
|
+
},
|
|
32
|
+
"./preact": {
|
|
33
|
+
"types": "./dist/types/src/adapters/preact.d.ts",
|
|
34
|
+
"default": "./dist/src/adapters/preact.js"
|
|
35
|
+
},
|
|
36
|
+
"./svelte": {
|
|
37
|
+
"types": "./dist/types/src/adapters/svelte.d.ts",
|
|
38
|
+
"default": "./dist/src/adapters/svelte.js"
|
|
39
|
+
},
|
|
40
|
+
"./solid": {
|
|
41
|
+
"types": "./dist/types/src/adapters/solid.d.ts",
|
|
42
|
+
"default": "./dist/src/adapters/solid.js"
|
|
43
|
+
},
|
|
44
|
+
"./html": {
|
|
45
|
+
"types": "./dist/types/src/adapters/html.d.ts",
|
|
46
|
+
"default": "./dist/src/adapters/html.js"
|
|
47
|
+
},
|
|
48
|
+
"./package.json": "./package.json"
|
|
49
|
+
},
|
|
50
|
+
"files": [
|
|
51
|
+
"dist",
|
|
52
|
+
"src"
|
|
53
|
+
],
|
|
54
|
+
"dependencies": {},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"hamur": "~0.0.1",
|
|
57
|
+
"@hamur/adapter-react": "^0.0.1",
|
|
58
|
+
"@hamur/adapter-vue": "^0.0.1",
|
|
59
|
+
"@hamur/adapter-preact": "^0.0.1",
|
|
60
|
+
"@hamur/adapter-svelte": "^0.0.1",
|
|
61
|
+
"@hamur/adapter-solid": "^0.0.1",
|
|
62
|
+
"@hamur/adapter-html": "^0.0.1",
|
|
63
|
+
"react": "^19.3.1",
|
|
64
|
+
"preact": "^11.0.2",
|
|
65
|
+
"vue": "^3.5.44",
|
|
66
|
+
"svelte": "^5.57.2",
|
|
67
|
+
"solid-js": "^2.0.4"
|
|
68
|
+
},
|
|
69
|
+
"peerDependenciesMeta": {
|
|
70
|
+
"@hamur/adapter-react": {
|
|
71
|
+
"optional": true
|
|
72
|
+
},
|
|
73
|
+
"@hamur/adapter-vue": {
|
|
74
|
+
"optional": true
|
|
75
|
+
},
|
|
76
|
+
"@hamur/adapter-preact": {
|
|
77
|
+
"optional": true
|
|
78
|
+
},
|
|
79
|
+
"@hamur/adapter-svelte": {
|
|
80
|
+
"optional": true
|
|
81
|
+
},
|
|
82
|
+
"@hamur/adapter-solid": {
|
|
83
|
+
"optional": true
|
|
84
|
+
},
|
|
85
|
+
"@hamur/adapter-html": {
|
|
86
|
+
"optional": true
|
|
87
|
+
},
|
|
88
|
+
"react": {
|
|
89
|
+
"optional": true
|
|
90
|
+
},
|
|
91
|
+
"preact": {
|
|
92
|
+
"optional": true
|
|
93
|
+
},
|
|
94
|
+
"vue": {
|
|
95
|
+
"optional": true
|
|
96
|
+
},
|
|
97
|
+
"svelte": {
|
|
98
|
+
"optional": true
|
|
99
|
+
},
|
|
100
|
+
"solid-js": {
|
|
101
|
+
"optional": true
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { AppContext } from "hamur/app"
|
|
2
|
+
import type { PwaStore } from "../client/store.ts"
|
|
3
|
+
|
|
4
|
+
export const NO_MODULE = "usePwa() needs the @hamur/pwa module in hamur.config.ts modules"
|
|
5
|
+
|
|
6
|
+
export function pwaOf(app: AppContext): PwaStore {
|
|
7
|
+
const store = (app as { pwa?: PwaStore }).pwa
|
|
8
|
+
if (!store) throw new Error(NO_MODULE)
|
|
9
|
+
return store
|
|
10
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { rerunOnHydrate, useApp } from "@hamur/adapter-html"
|
|
2
|
+
import type { PwaActions, PwaState, PwaStore } from "../client/store.ts"
|
|
3
|
+
import { pwaOf } from "./bind.ts"
|
|
4
|
+
|
|
5
|
+
export type { PwaActions, PwaState }
|
|
6
|
+
|
|
7
|
+
export function usePwa(): PwaState & PwaActions & Pick<PwaStore, "subscribe"> {
|
|
8
|
+
rerunOnHydrate()
|
|
9
|
+
const store = pwaOf(useApp())
|
|
10
|
+
return {
|
|
11
|
+
get needRefresh() {
|
|
12
|
+
return store.get().needRefresh
|
|
13
|
+
},
|
|
14
|
+
get offlineReady() {
|
|
15
|
+
return store.get().offlineReady
|
|
16
|
+
},
|
|
17
|
+
get installable() {
|
|
18
|
+
return store.get().installable
|
|
19
|
+
},
|
|
20
|
+
subscribe: store.subscribe,
|
|
21
|
+
update: store.update,
|
|
22
|
+
install: store.install,
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { useEffect, useState } from "preact/hooks"
|
|
2
|
+
import { useApp } from "@hamur/adapter-preact"
|
|
3
|
+
import { PWA_OFF, type PwaActions, type PwaState } from "../client/store.ts"
|
|
4
|
+
import { pwaOf } from "./bind.ts"
|
|
5
|
+
|
|
6
|
+
export type { PwaActions, PwaState }
|
|
7
|
+
|
|
8
|
+
export function usePwa(): PwaState & PwaActions {
|
|
9
|
+
const store = pwaOf(useApp())
|
|
10
|
+
const [state, setState] = useState<PwaState>(PWA_OFF)
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
setState(store.get())
|
|
13
|
+
return store.subscribe(() => setState(store.get()))
|
|
14
|
+
}, [store])
|
|
15
|
+
return { ...state, update: store.update, install: store.install }
|
|
16
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { useSyncExternalStore } from "react"
|
|
2
|
+
import { useApp } from "@hamur/adapter-react"
|
|
3
|
+
import { PWA_OFF, type PwaActions, type PwaState } from "../client/store.ts"
|
|
4
|
+
import { pwaOf } from "./bind.ts"
|
|
5
|
+
|
|
6
|
+
export type { PwaActions, PwaState }
|
|
7
|
+
|
|
8
|
+
export function usePwa(): PwaState & PwaActions {
|
|
9
|
+
const store = pwaOf(useApp())
|
|
10
|
+
const state = useSyncExternalStore(store.subscribe, store.get, () => PWA_OFF)
|
|
11
|
+
return { ...state, update: store.update, install: store.install }
|
|
12
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { createSignal, onSettled, type Accessor } from "solid-js"
|
|
2
|
+
import { useApp } from "@hamur/adapter-solid"
|
|
3
|
+
import { PWA_OFF, type PwaActions, type PwaState } from "../client/store.ts"
|
|
4
|
+
import { pwaOf } from "./bind.ts"
|
|
5
|
+
|
|
6
|
+
export type { PwaActions, PwaState }
|
|
7
|
+
|
|
8
|
+
export function usePwa(): { [K in keyof PwaState]: Accessor<PwaState[K]> } & PwaActions {
|
|
9
|
+
const store = pwaOf(useApp())
|
|
10
|
+
const [state, setState] = createSignal<PwaState>(PWA_OFF)
|
|
11
|
+
onSettled(() => {
|
|
12
|
+
setState(() => store.get())
|
|
13
|
+
return store.subscribe(() => setState(() => store.get()))
|
|
14
|
+
})
|
|
15
|
+
return {
|
|
16
|
+
needRefresh: () => state().needRefresh,
|
|
17
|
+
offlineReady: () => state().offlineReady,
|
|
18
|
+
installable: () => state().installable,
|
|
19
|
+
update: store.update,
|
|
20
|
+
install: store.install,
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { createSubscriber } from "svelte/reactivity"
|
|
2
|
+
import { useApp } from "@hamur/adapter-svelte"
|
|
3
|
+
import type { PwaActions, PwaState } from "../client/store.ts"
|
|
4
|
+
import { pwaOf } from "./bind.ts"
|
|
5
|
+
|
|
6
|
+
export type { PwaActions, PwaState }
|
|
7
|
+
|
|
8
|
+
export function usePwa(): PwaState & PwaActions {
|
|
9
|
+
const store = pwaOf(useApp())
|
|
10
|
+
const subscribe = createSubscriber((update) => store.subscribe(update))
|
|
11
|
+
const read = <K extends keyof PwaState>(key: K): PwaState[K] => {
|
|
12
|
+
subscribe()
|
|
13
|
+
return store.get()[key]
|
|
14
|
+
}
|
|
15
|
+
return {
|
|
16
|
+
get needRefresh() {
|
|
17
|
+
return read("needRefresh")
|
|
18
|
+
},
|
|
19
|
+
get offlineReady() {
|
|
20
|
+
return read("offlineReady")
|
|
21
|
+
},
|
|
22
|
+
get installable() {
|
|
23
|
+
return read("installable")
|
|
24
|
+
},
|
|
25
|
+
update: store.update,
|
|
26
|
+
install: store.install,
|
|
27
|
+
}
|
|
28
|
+
}
|