pezza_action_push_web 0.1.1 → 0.1.2
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.
- checksums.yaml +4 -4
- data/README.md +0 -1
- data/app/assets/javascripts/action_push_web.js +13 -303
- data/app/assets/javascripts/components/granted.js +10 -10
- data/app/assets/javascripts/components/request.js +2 -12
- data/app/helpers/action_push_web/application_helper.rb +3 -7
- data/lib/action_push_web/version.rb +1 -1
- data/lib/generators/action_push_web/install/install_generator.rb +0 -8
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eb9c4ebdcbd17365efb10ed10e722ddc65e1313c46904a2fefcab41e2a77d7b3
|
4
|
+
data.tar.gz: b470819fb3149a2a9b48d98ccdfce99aafa86bf7f7f153eb0019c33b15a2e9ac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3b550df8977bdd96661dd9cb96993697d409b0f6555a22e235429971b2e7cbd68a5800205526217b622a101774f1482b65fc60ef4a6ecd99467d8bc28cfb353b
|
7
|
+
data.tar.gz: 644ee1e71e8d4065e87fe9334078725b801ff3a9c2a7ea6f81de719e3e53ec139c4642c4d19852fbed0b798eb051854901a79f3f561189d2419971dd9b5eadef
|
data/README.md
CHANGED
@@ -26,7 +26,6 @@ The installation will create:
|
|
26
26
|
- `app/views/pwa/service_worker.js`
|
27
27
|
- mount the subscriptions controllers
|
28
28
|
- import action_push_web.js in your application.js
|
29
|
-
- Add `<%= action_push_web_key_tag %>` to the <head> of your application's layout if it can find it. Otherwise, you'll need to add it manually.
|
30
29
|
|
31
30
|
|
32
31
|
`app/models/application_push_web_notification.rb`:
|
@@ -10,11 +10,6 @@ class Request extends HTMLElement {
|
|
10
10
|
this.addEventListener("click", this.onClick);
|
11
11
|
}
|
12
12
|
#setState() {
|
13
|
-
if (this.isEnabled) {
|
14
|
-
this.removeAttribute("disabled");
|
15
|
-
} else {
|
16
|
-
this.setAttribute("disabled", true);
|
17
|
-
}
|
18
13
|
this.hidden = !this.isEnabled;
|
19
14
|
}
|
20
15
|
disconnectedCallback() {
|
@@ -24,15 +19,12 @@ class Request extends HTMLElement {
|
|
24
19
|
this.#setState();
|
25
20
|
}
|
26
21
|
get isEnabled() {
|
27
|
-
return navigator.serviceWorker && window.Notification && Notification.permission == "default"
|
28
|
-
}
|
29
|
-
get #isDisabled() {
|
30
|
-
return this.hasAttribute("disabled");
|
22
|
+
return navigator.serviceWorker && window.Notification && Notification.permission == "default";
|
31
23
|
}
|
32
24
|
async onClick(event) {
|
33
25
|
event.preventDefault();
|
34
26
|
event.stopPropagation();
|
35
|
-
if (this.isEnabled
|
27
|
+
if (this.isEnabled) {
|
36
28
|
const permission = await Notification.requestPermission();
|
37
29
|
if (permission === "granted") {
|
38
30
|
document.dispatchEvent(new CustomEvent("action-push-web:granted", {}));
|
@@ -66,292 +58,6 @@ class Denied extends HTMLElement {
|
|
66
58
|
}
|
67
59
|
}
|
68
60
|
|
69
|
-
// node_modules/@rails/request.js/src/fetch_response.js
|
70
|
-
class FetchResponse {
|
71
|
-
constructor(response) {
|
72
|
-
this.response = response;
|
73
|
-
}
|
74
|
-
get statusCode() {
|
75
|
-
return this.response.status;
|
76
|
-
}
|
77
|
-
get redirected() {
|
78
|
-
return this.response.redirected;
|
79
|
-
}
|
80
|
-
get ok() {
|
81
|
-
return this.response.ok;
|
82
|
-
}
|
83
|
-
get unauthenticated() {
|
84
|
-
return this.statusCode === 401;
|
85
|
-
}
|
86
|
-
get unprocessableEntity() {
|
87
|
-
return this.statusCode === 422;
|
88
|
-
}
|
89
|
-
get authenticationURL() {
|
90
|
-
return this.response.headers.get("WWW-Authenticate");
|
91
|
-
}
|
92
|
-
get contentType() {
|
93
|
-
const contentType = this.response.headers.get("Content-Type") || "";
|
94
|
-
return contentType.replace(/;.*$/, "");
|
95
|
-
}
|
96
|
-
get headers() {
|
97
|
-
return this.response.headers;
|
98
|
-
}
|
99
|
-
get html() {
|
100
|
-
if (this.contentType.match(/^(application|text)\/(html|xhtml\+xml)$/)) {
|
101
|
-
return this.text;
|
102
|
-
}
|
103
|
-
return Promise.reject(new Error(`Expected an HTML response but got "${this.contentType}" instead`));
|
104
|
-
}
|
105
|
-
get json() {
|
106
|
-
if (this.contentType.match(/^application\/.*json$/)) {
|
107
|
-
return this.responseJson || (this.responseJson = this.response.json());
|
108
|
-
}
|
109
|
-
return Promise.reject(new Error(`Expected a JSON response but got "${this.contentType}" instead`));
|
110
|
-
}
|
111
|
-
get text() {
|
112
|
-
return this.responseText || (this.responseText = this.response.text());
|
113
|
-
}
|
114
|
-
get isTurboStream() {
|
115
|
-
return this.contentType.match(/^text\/vnd\.turbo-stream\.html/);
|
116
|
-
}
|
117
|
-
get isScript() {
|
118
|
-
return this.contentType.match(/\b(?:java|ecma)script\b/);
|
119
|
-
}
|
120
|
-
async renderTurboStream() {
|
121
|
-
if (this.isTurboStream) {
|
122
|
-
if (window.Turbo) {
|
123
|
-
await window.Turbo.renderStreamMessage(await this.text);
|
124
|
-
} else {
|
125
|
-
console.warn("You must set `window.Turbo = Turbo` to automatically process Turbo Stream events with request.js");
|
126
|
-
}
|
127
|
-
} else {
|
128
|
-
return Promise.reject(new Error(`Expected a Turbo Stream response but got "${this.contentType}" instead`));
|
129
|
-
}
|
130
|
-
}
|
131
|
-
async activeScript() {
|
132
|
-
if (this.isScript) {
|
133
|
-
const script = document.createElement("script");
|
134
|
-
const metaTag = document.querySelector("meta[name=csp-nonce]");
|
135
|
-
if (metaTag) {
|
136
|
-
const nonce = metaTag.nonce === "" ? metaTag.content : metaTag.nonce;
|
137
|
-
if (nonce) {
|
138
|
-
script.setAttribute("nonce", nonce);
|
139
|
-
}
|
140
|
-
}
|
141
|
-
script.innerHTML = await this.text;
|
142
|
-
document.body.appendChild(script);
|
143
|
-
} else {
|
144
|
-
return Promise.reject(new Error(`Expected a Script response but got "${this.contentType}" instead`));
|
145
|
-
}
|
146
|
-
}
|
147
|
-
}
|
148
|
-
|
149
|
-
// node_modules/@rails/request.js/src/request_interceptor.js
|
150
|
-
class RequestInterceptor {
|
151
|
-
static register(interceptor) {
|
152
|
-
this.interceptor = interceptor;
|
153
|
-
}
|
154
|
-
static get() {
|
155
|
-
return this.interceptor;
|
156
|
-
}
|
157
|
-
static reset() {
|
158
|
-
this.interceptor = undefined;
|
159
|
-
}
|
160
|
-
}
|
161
|
-
|
162
|
-
// node_modules/@rails/request.js/src/lib/utils.js
|
163
|
-
function getCookie(name) {
|
164
|
-
const cookies = document.cookie ? document.cookie.split("; ") : [];
|
165
|
-
const prefix = `${encodeURIComponent(name)}=`;
|
166
|
-
const cookie = cookies.find((cookie2) => cookie2.startsWith(prefix));
|
167
|
-
if (cookie) {
|
168
|
-
const value = cookie.split("=").slice(1).join("=");
|
169
|
-
if (value) {
|
170
|
-
return decodeURIComponent(value);
|
171
|
-
}
|
172
|
-
}
|
173
|
-
}
|
174
|
-
function compact(object) {
|
175
|
-
const result = {};
|
176
|
-
for (const key in object) {
|
177
|
-
const value = object[key];
|
178
|
-
if (value !== undefined) {
|
179
|
-
result[key] = value;
|
180
|
-
}
|
181
|
-
}
|
182
|
-
return result;
|
183
|
-
}
|
184
|
-
function metaContent(name) {
|
185
|
-
const element = document.head.querySelector(`meta[name="${name}"]`);
|
186
|
-
return element && element.content;
|
187
|
-
}
|
188
|
-
function stringEntriesFromFormData(formData) {
|
189
|
-
return [...formData].reduce((entries, [name, value]) => {
|
190
|
-
return entries.concat(typeof value === "string" ? [[name, value]] : []);
|
191
|
-
}, []);
|
192
|
-
}
|
193
|
-
function mergeEntries(searchParams, entries) {
|
194
|
-
for (const [name, value] of entries) {
|
195
|
-
if (value instanceof window.File)
|
196
|
-
continue;
|
197
|
-
if (searchParams.has(name) && !name.includes("[]")) {
|
198
|
-
searchParams.delete(name);
|
199
|
-
searchParams.set(name, value);
|
200
|
-
} else {
|
201
|
-
searchParams.append(name, value);
|
202
|
-
}
|
203
|
-
}
|
204
|
-
}
|
205
|
-
|
206
|
-
// node_modules/@rails/request.js/src/fetch_request.js
|
207
|
-
class FetchRequest {
|
208
|
-
constructor(method, url, options = {}) {
|
209
|
-
this.method = method;
|
210
|
-
this.options = options;
|
211
|
-
this.originalUrl = url.toString();
|
212
|
-
}
|
213
|
-
async perform() {
|
214
|
-
try {
|
215
|
-
const requestInterceptor = RequestInterceptor.get();
|
216
|
-
if (requestInterceptor) {
|
217
|
-
await requestInterceptor(this);
|
218
|
-
}
|
219
|
-
} catch (error) {
|
220
|
-
console.error(error);
|
221
|
-
}
|
222
|
-
const fetch = this.responseKind === "turbo-stream" && window.Turbo ? window.Turbo.fetch : window.fetch;
|
223
|
-
const response = new FetchResponse(await fetch(this.url, this.fetchOptions));
|
224
|
-
if (response.unauthenticated && response.authenticationURL) {
|
225
|
-
return Promise.reject(window.location.href = response.authenticationURL);
|
226
|
-
}
|
227
|
-
if (response.isScript) {
|
228
|
-
await response.activeScript();
|
229
|
-
}
|
230
|
-
const responseStatusIsTurboStreamable = response.ok || response.unprocessableEntity;
|
231
|
-
if (responseStatusIsTurboStreamable && response.isTurboStream) {
|
232
|
-
await response.renderTurboStream();
|
233
|
-
}
|
234
|
-
return response;
|
235
|
-
}
|
236
|
-
addHeader(key, value) {
|
237
|
-
const headers = this.additionalHeaders;
|
238
|
-
headers[key] = value;
|
239
|
-
this.options.headers = headers;
|
240
|
-
}
|
241
|
-
sameHostname() {
|
242
|
-
if (!this.originalUrl.startsWith("http:") && !this.originalUrl.startsWith("https:")) {
|
243
|
-
return true;
|
244
|
-
}
|
245
|
-
try {
|
246
|
-
return new URL(this.originalUrl).hostname === window.location.hostname;
|
247
|
-
} catch (_) {
|
248
|
-
return true;
|
249
|
-
}
|
250
|
-
}
|
251
|
-
get fetchOptions() {
|
252
|
-
return {
|
253
|
-
method: this.method.toUpperCase(),
|
254
|
-
headers: this.headers,
|
255
|
-
body: this.formattedBody,
|
256
|
-
signal: this.signal,
|
257
|
-
credentials: this.credentials,
|
258
|
-
redirect: this.redirect,
|
259
|
-
keepalive: this.keepalive
|
260
|
-
};
|
261
|
-
}
|
262
|
-
get headers() {
|
263
|
-
const baseHeaders = {
|
264
|
-
"X-Requested-With": "XMLHttpRequest",
|
265
|
-
"Content-Type": this.contentType,
|
266
|
-
Accept: this.accept
|
267
|
-
};
|
268
|
-
if (this.sameHostname()) {
|
269
|
-
baseHeaders["X-CSRF-Token"] = this.csrfToken;
|
270
|
-
}
|
271
|
-
return compact(Object.assign(baseHeaders, this.additionalHeaders));
|
272
|
-
}
|
273
|
-
get csrfToken() {
|
274
|
-
return getCookie(metaContent("csrf-param")) || metaContent("csrf-token");
|
275
|
-
}
|
276
|
-
get contentType() {
|
277
|
-
if (this.options.contentType) {
|
278
|
-
return this.options.contentType;
|
279
|
-
} else if (this.body == null || this.body instanceof window.FormData) {
|
280
|
-
return;
|
281
|
-
} else if (this.body instanceof window.File) {
|
282
|
-
return this.body.type;
|
283
|
-
}
|
284
|
-
return "application/json";
|
285
|
-
}
|
286
|
-
get accept() {
|
287
|
-
switch (this.responseKind) {
|
288
|
-
case "html":
|
289
|
-
return "text/html, application/xhtml+xml";
|
290
|
-
case "turbo-stream":
|
291
|
-
return "text/vnd.turbo-stream.html, text/html, application/xhtml+xml";
|
292
|
-
case "json":
|
293
|
-
return "application/json, application/vnd.api+json";
|
294
|
-
case "script":
|
295
|
-
return "text/javascript, application/javascript";
|
296
|
-
default:
|
297
|
-
return "*/*";
|
298
|
-
}
|
299
|
-
}
|
300
|
-
get body() {
|
301
|
-
return this.options.body;
|
302
|
-
}
|
303
|
-
get query() {
|
304
|
-
const originalQuery = (this.originalUrl.split("?")[1] || "").split("#")[0];
|
305
|
-
const params = new URLSearchParams(originalQuery);
|
306
|
-
let requestQuery = this.options.query;
|
307
|
-
if (requestQuery instanceof window.FormData) {
|
308
|
-
requestQuery = stringEntriesFromFormData(requestQuery);
|
309
|
-
} else if (requestQuery instanceof window.URLSearchParams) {
|
310
|
-
requestQuery = requestQuery.entries();
|
311
|
-
} else {
|
312
|
-
requestQuery = Object.entries(requestQuery || {});
|
313
|
-
}
|
314
|
-
mergeEntries(params, requestQuery);
|
315
|
-
const query = params.toString();
|
316
|
-
return query.length > 0 ? `?${query}` : "";
|
317
|
-
}
|
318
|
-
get url() {
|
319
|
-
return this.originalUrl.split("?")[0].split("#")[0] + this.query;
|
320
|
-
}
|
321
|
-
get responseKind() {
|
322
|
-
return this.options.responseKind || "html";
|
323
|
-
}
|
324
|
-
get signal() {
|
325
|
-
return this.options.signal;
|
326
|
-
}
|
327
|
-
get redirect() {
|
328
|
-
return this.options.redirect || "follow";
|
329
|
-
}
|
330
|
-
get credentials() {
|
331
|
-
return this.options.credentials || "same-origin";
|
332
|
-
}
|
333
|
-
get keepalive() {
|
334
|
-
return this.options.keepalive || false;
|
335
|
-
}
|
336
|
-
get additionalHeaders() {
|
337
|
-
return this.options.headers || {};
|
338
|
-
}
|
339
|
-
get formattedBody() {
|
340
|
-
const bodyIsAString = Object.prototype.toString.call(this.body) === "[object String]";
|
341
|
-
const contentTypeIsJson = this.headers["Content-Type"] === "application/json";
|
342
|
-
if (contentTypeIsJson && !bodyIsAString) {
|
343
|
-
return JSON.stringify(this.body);
|
344
|
-
}
|
345
|
-
return this.body;
|
346
|
-
}
|
347
|
-
}
|
348
|
-
|
349
|
-
// node_modules/@rails/request.js/src/verbs.js
|
350
|
-
async function post(url, options) {
|
351
|
-
const request = new FetchRequest("post", url, options);
|
352
|
-
return request.perform();
|
353
|
-
}
|
354
|
-
|
355
61
|
// app/assets/javascripts/components/granted.js
|
356
62
|
class Granted extends HTMLElement {
|
357
63
|
constructor() {
|
@@ -376,22 +82,26 @@ class Granted extends HTMLElement {
|
|
376
82
|
});
|
377
83
|
}
|
378
84
|
get #isEnabled() {
|
379
|
-
return !!navigator.serviceWorker && !!window.Notification && Notification.permission == "granted";
|
85
|
+
return !!navigator.serviceWorker && !!window.Notification && Notification.permission == "granted" && this.getAttribute("href") && this.getAttribute("public-key");
|
380
86
|
}
|
381
87
|
get #serviceWorkerRegistration() {
|
382
88
|
return navigator.serviceWorker.getRegistration();
|
383
89
|
}
|
384
90
|
get #vapidPublicKey() {
|
385
|
-
|
386
|
-
return this.#urlBase64ToUint8Array(encodedVapidPublicKey);
|
91
|
+
return this.#urlBase64ToUint8Array(this.getAttribute("public-key"));
|
387
92
|
}
|
388
93
|
async#syncPushSubscription(subscription) {
|
389
|
-
const response = await
|
390
|
-
|
94
|
+
const response = await fetch(this.getAttribute("href"), {
|
95
|
+
method: "POST",
|
96
|
+
body: this.#extractJsonPayloadAsString(subscription),
|
97
|
+
headers: {
|
98
|
+
Accept: "text/vnd.turbo-stream.html, text/html, application/xhtml+xml",
|
99
|
+
"Content-Type": "application/json",
|
100
|
+
"X-CSRF-Token": document.querySelector("meta[name=csrf-token]").content
|
101
|
+
}
|
391
102
|
});
|
392
|
-
if (!response.ok)
|
103
|
+
if (!response.ok)
|
393
104
|
subscription.unsubscribe();
|
394
|
-
}
|
395
105
|
}
|
396
106
|
#extractJsonPayloadAsString(subscription) {
|
397
107
|
const { endpoint, keys: { p256dh, auth } } = subscription.toJSON();
|
@@ -1,5 +1,3 @@
|
|
1
|
-
import { post } from "@rails/request.js"
|
2
|
-
|
3
1
|
export default class Granted extends HTMLElement {
|
4
2
|
constructor() {
|
5
3
|
super();
|
@@ -30,7 +28,7 @@ export default class Granted extends HTMLElement {
|
|
30
28
|
}
|
31
29
|
|
32
30
|
get #isEnabled() {
|
33
|
-
return !!navigator.serviceWorker && !!window.Notification && Notification.permission == "granted"
|
31
|
+
return !!navigator.serviceWorker && !!window.Notification && Notification.permission == "granted" && this.getAttribute('href') && this.getAttribute('public-key')
|
34
32
|
}
|
35
33
|
|
36
34
|
get #serviceWorkerRegistration() {
|
@@ -38,18 +36,20 @@ export default class Granted extends HTMLElement {
|
|
38
36
|
}
|
39
37
|
|
40
38
|
get #vapidPublicKey() {
|
41
|
-
|
42
|
-
return this.#urlBase64ToUint8Array(encodedVapidPublicKey)
|
39
|
+
return this.#urlBase64ToUint8Array(this.getAttribute('public-key'))
|
43
40
|
}
|
44
41
|
|
45
42
|
async #syncPushSubscription(subscription) {
|
46
|
-
const response = await
|
43
|
+
const response = await fetch(this.getAttribute('href'), {
|
44
|
+
method: "POST",
|
47
45
|
body: this.#extractJsonPayloadAsString(subscription),
|
46
|
+
headers: {
|
47
|
+
Accept: "text/vnd.turbo-stream.html, text/html, application/xhtml+xml",
|
48
|
+
"Content-Type": "application/json",
|
49
|
+
"X-CSRF-Token": document.querySelector("meta[name=csrf-token]").content
|
50
|
+
}
|
48
51
|
})
|
49
|
-
|
50
|
-
if (!response.ok) {
|
51
|
-
subscription.unsubscribe()
|
52
|
-
}
|
52
|
+
if (!response.ok) subscription.unsubscribe()
|
53
53
|
}
|
54
54
|
|
55
55
|
#extractJsonPayloadAsString(subscription) {
|
@@ -13,12 +13,6 @@ export default class Request extends HTMLElement {
|
|
13
13
|
}
|
14
14
|
|
15
15
|
#setState() {
|
16
|
-
if (this.isEnabled) {
|
17
|
-
this.removeAttribute('disabled')
|
18
|
-
} else {
|
19
|
-
this.setAttribute('disabled', true)
|
20
|
-
}
|
21
|
-
|
22
16
|
this.hidden = !this.isEnabled;
|
23
17
|
}
|
24
18
|
|
@@ -31,18 +25,14 @@ export default class Request extends HTMLElement {
|
|
31
25
|
}
|
32
26
|
|
33
27
|
get isEnabled() {
|
34
|
-
return navigator.serviceWorker && window.Notification && Notification.permission == "default"
|
35
|
-
}
|
36
|
-
|
37
|
-
get #isDisabled() {
|
38
|
-
return this.hasAttribute('disabled')
|
28
|
+
return navigator.serviceWorker && window.Notification && Notification.permission == "default"
|
39
29
|
}
|
40
30
|
|
41
31
|
async onClick(event) {
|
42
32
|
event.preventDefault();
|
43
33
|
event.stopPropagation();
|
44
34
|
|
45
|
-
if (this.isEnabled
|
35
|
+
if (this.isEnabled) {
|
46
36
|
const permission = await Notification.requestPermission()
|
47
37
|
if (permission === "granted") {
|
48
38
|
document.dispatchEvent(new CustomEvent('action-push-web:granted', {}))
|
@@ -1,18 +1,14 @@
|
|
1
1
|
module ActionPushWeb
|
2
2
|
module ApplicationHelper
|
3
|
-
def action_push_web_key_tag(application: nil)
|
4
|
-
tag.meta name: "action-push-web-public-key",
|
5
|
-
content: ActionPushWeb.config_for(application)[:public_key]
|
6
|
-
end
|
7
|
-
|
8
3
|
def when_web_notifications_disabled(**attrs, &block)
|
9
4
|
content_tag("action-push-web-denied", capture(&block), **attrs)
|
10
5
|
end
|
11
6
|
|
12
7
|
def when_web_notifications_allowed(href: action_push_web.subscriptions_path,
|
13
|
-
service_worker_url: pwa_service_worker_path(format: :js), **attrs, &block)
|
8
|
+
service_worker_url: pwa_service_worker_path(format: :js), application: nil, **attrs, &block)
|
14
9
|
content_tag("action-push-web-granted", capture(&block),
|
15
|
-
href:, service_worker_url
|
10
|
+
href:, "service-worker-url" => service_worker_url,
|
11
|
+
"public-key" => ActionPushWeb.config_for(application)[:public_key], **attrs)
|
16
12
|
end
|
17
13
|
|
18
14
|
def ask_for_web_notifications(**attrs, &block)
|
@@ -26,14 +26,6 @@ class ActionPushWeb::InstallGenerator < Rails::Generators::Base
|
|
26
26
|
# run "yarn add action_push_web"
|
27
27
|
end
|
28
28
|
|
29
|
-
if APPLICATION_LAYOUT_PATH.exist?
|
30
|
-
say "Add action push web meta tag in application layout"
|
31
|
-
insert_into_file APPLICATION_LAYOUT_PATH.to_s, "\n <%= action_push_web_key_tag %>", before: /\s*<\/head>/
|
32
|
-
else
|
33
|
-
say "Default application.html.erb is missing!", :red
|
34
|
-
say " Add <%= action_push_web_key_tag %> within the <head> tag in your custom layout."
|
35
|
-
end
|
36
|
-
|
37
29
|
rails_command "railties:install:migrations FROM=action_push_web",
|
38
30
|
inline: true
|
39
31
|
|