@jskit-ai/push-core 0.1.11 → 0.1.12
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/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
- `web-push` is loaded at the first web push send, not as the server starts, where it added about 110–130 ms to every start of an app, sent or not.
|
|
5
6
|
- `push.sendEach(notifications)` sends several notifications, each to its own people with its own fields, in the same statements as one `push.send`, and answers their outcomes summed. A notification's `hiddenBody` is shown in place of `body` to a person who hides text (it was empty). `GET /api/push`'s devices carry their `tokenHash`, so a client can tell which is this one.
|
|
6
7
|
- Its migrations declare `"after": ["@jskit-ai/users-core"]`: they create a key to `users` with their tables, and ran before users-core created it on a new database (path order), which MySQL and PostgreSQL refuse.
|
|
7
8
|
- Quiet hours with one end are refused with the empty end in `details.fieldErrors` (`quietFrom` or `quietTo`, codes `push.settings.quietFromRequired` and `push.settings.quietToRequired`), so the field shows it. The refusals of a browser subscription, an unknown push service and an unknown time zone now say what the person can do.
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@ import { AppError } from "@jskit-ai/kernel/server/runtime/errors";
|
|
|
3
3
|
import { logSideEffect } from "@jskit-ai/kernel/server/runtime/appEnvironment";
|
|
4
4
|
import { PUSH_DEVICES_PER_USER, PUSH_KIND_PATTERN } from "../shared/pushKinds.js";
|
|
5
5
|
import { inQuietHours } from "./pushSettings.js";
|
|
6
|
-
import { isPushServiceEndpoint } from "./senders/
|
|
6
|
+
import { isPushServiceEndpoint } from "./senders/pushServiceEndpoint.js";
|
|
7
7
|
|
|
8
8
|
// Devices sent to at once, and the consecutive failures after which a device is forgotten.
|
|
9
9
|
const SEND_CONCURRENCY = 10;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Which web push endpoints the server may POST to; apart from the sender, so checking one loads no web-push.
|
|
2
|
+
|
|
3
|
+
// The push services browsers subscribe with. The server POSTs to a subscription's endpoint, so it reaches only these,
|
|
4
|
+
// over HTTPS on the default port, with no credentials in the address: never a host the person chose.
|
|
5
|
+
const PUSH_SERVICE_HOSTS = Object.freeze([
|
|
6
|
+
/^fcm\.googleapis\.com$/u,
|
|
7
|
+
/^updates\.push\.services\.mozilla\.com$/u,
|
|
8
|
+
/^(?:[a-z0-9-]+\.)*push\.apple\.com$/u,
|
|
9
|
+
/^[a-z0-9-]+\.notify\.windows\.com$/u
|
|
10
|
+
]);
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Whether a web push endpoint is a known push service's, so the server may POST to it.
|
|
14
|
+
* @param {string} endpoint
|
|
15
|
+
*/
|
|
16
|
+
function isPushServiceEndpoint(endpoint) {
|
|
17
|
+
let url;
|
|
18
|
+
try {
|
|
19
|
+
url = new URL(String(endpoint || ""));
|
|
20
|
+
} catch {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
return url.protocol === "https:" && url.port === "" && !url.username && !url.password &&
|
|
24
|
+
PUSH_SERVICE_HOSTS.some((host) => host.test(url.hostname));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { isPushServiceEndpoint };
|
|
@@ -1,29 +1,8 @@
|
|
|
1
|
-
import { generateRequestDetails } from "web-push";
|
|
2
1
|
import { GONE, SENT, failed, throttled } from "./outcomes.js";
|
|
2
|
+
import { isPushServiceEndpoint } from "./pushServiceEndpoint.js";
|
|
3
3
|
|
|
4
|
-
//
|
|
5
|
-
|
|
6
|
-
const PUSH_SERVICE_HOSTS = Object.freeze([
|
|
7
|
-
/^fcm\.googleapis\.com$/u,
|
|
8
|
-
/^updates\.push\.services\.mozilla\.com$/u,
|
|
9
|
-
/^(?:[a-z0-9-]+\.)*push\.apple\.com$/u,
|
|
10
|
-
/^[a-z0-9-]+\.notify\.windows\.com$/u
|
|
11
|
-
]);
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Whether a web push endpoint is a known push service's, so the server may POST to it.
|
|
15
|
-
* @param {string} endpoint
|
|
16
|
-
*/
|
|
17
|
-
function isPushServiceEndpoint(endpoint) {
|
|
18
|
-
let url;
|
|
19
|
-
try {
|
|
20
|
-
url = new URL(String(endpoint || ""));
|
|
21
|
-
} catch {
|
|
22
|
-
return false;
|
|
23
|
-
}
|
|
24
|
-
return url.protocol === "https:" && url.port === "" && !url.username && !url.password &&
|
|
25
|
-
PUSH_SERVICE_HOSTS.some((host) => host.test(url.hostname));
|
|
26
|
-
}
|
|
4
|
+
// web-push, loaded at the first send: loading it as the server starts cost every start 110–130 ms, sent or not.
|
|
5
|
+
let webPush = null;
|
|
27
6
|
|
|
28
7
|
/**
|
|
29
8
|
* Sends web push messages, encrypted for each subscription and signed with the server's VAPID key.
|
|
@@ -41,6 +20,8 @@ function createWebPushSender({ vapid, fetchImpl, now }) {
|
|
|
41
20
|
if (!isPushServiceEndpoint(device.token) || !device.p256dh || !device.auth) {
|
|
42
21
|
return GONE;
|
|
43
22
|
}
|
|
23
|
+
webPush ??= import("web-push");
|
|
24
|
+
const { generateRequestDetails } = await webPush;
|
|
44
25
|
const request = generateRequestDetails(
|
|
45
26
|
{ endpoint: device.token, keys: { p256dh: device.p256dh, auth: device.auth } },
|
|
46
27
|
JSON.stringify(message),
|
|
@@ -61,4 +42,4 @@ function topicOf(group) {
|
|
|
61
42
|
return String(group).replace(/[^A-Za-z0-9_-]/gu, "-").slice(0, 32);
|
|
62
43
|
}
|
|
63
44
|
|
|
64
|
-
export { createWebPushSender
|
|
45
|
+
export { createWebPushSender };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { execFile } from "node:child_process";
|
|
3
|
+
import { promisify } from "node:util";
|
|
4
|
+
import test from "node:test";
|
|
5
|
+
import { testCredentials } from "./support/credentials.js";
|
|
6
|
+
|
|
7
|
+
// web-push is loaded at the first web push send, never as the server starts: loading it added 110–130 ms to every
|
|
8
|
+
// start of an app that may never send one. A fresh process imports the push service and senders, then sends once,
|
|
9
|
+
// noting each time whether web-push has been resolved.
|
|
10
|
+
const SCRIPT = `
|
|
11
|
+
import { registerHooks } from "node:module";
|
|
12
|
+
let loaded = false;
|
|
13
|
+
registerHooks({ resolve(specifier, context, next) { if (specifier === "web-push") loaded = true; return next(specifier, context); } });
|
|
14
|
+
await import(new URL("../src/server/pushService.js", process.env.TEST_BASE).href);
|
|
15
|
+
const { createPushSenders } = await import(new URL("../src/server/pushSenders.js", process.env.TEST_BASE).href);
|
|
16
|
+
const { createWebPushSender } = await import(new URL("../src/server/senders/webPush.js", process.env.TEST_BASE).href);
|
|
17
|
+
const atStart = loaded;
|
|
18
|
+
const sender = createWebPushSender({ vapid: JSON.parse(process.env.TEST_VAPID), now: Date.now, fetchImpl: async () => new Response(null, { status: 201 }) });
|
|
19
|
+
const outcome = await sender.send({ token: "https://fcm.googleapis.com/fcm/send/x", p256dh: process.env.TEST_P256DH, auth: process.env.TEST_AUTH }, { title: "Hi" });
|
|
20
|
+
console.log(JSON.stringify({ atStart, afterSend: loaded, outcome, senders: typeof createPushSenders }));
|
|
21
|
+
`;
|
|
22
|
+
|
|
23
|
+
test("web-push is not loaded by importing the push service and senders, only by the first web push send", async () => {
|
|
24
|
+
const { vapid } = testCredentials();
|
|
25
|
+
const { stdout } = await promisify(execFile)(process.execPath, ["--input-type=module", "-e", SCRIPT], {
|
|
26
|
+
env: {
|
|
27
|
+
...process.env,
|
|
28
|
+
TEST_BASE: import.meta.url,
|
|
29
|
+
TEST_VAPID: JSON.stringify(vapid),
|
|
30
|
+
// RFC 8291's example subscription keys.
|
|
31
|
+
TEST_P256DH: "BCVxsr7N_eNgVRqvHtD0zTZsEc6-VV-JvLexhqUzORcxaOzi6-AYWXvTBHm4bjyPjs7Vd8pZGH6SRpkNtoIAiw4",
|
|
32
|
+
TEST_AUTH: "BTBZMqHH6r4Tts7J_aSIgg"
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
const result = JSON.parse(stdout);
|
|
36
|
+
assert.equal(result.atStart, false, "importing them loads no web-push");
|
|
37
|
+
assert.equal(result.afterSend, true);
|
|
38
|
+
assert.deepEqual(result.outcome, { status: "sent" });
|
|
39
|
+
assert.equal(result.senders, "function");
|
|
40
|
+
});
|
package/test/senders.test.js
CHANGED
|
@@ -3,7 +3,8 @@ import { createPublicKey, verify } from "node:crypto";
|
|
|
3
3
|
import test from "node:test";
|
|
4
4
|
import { createApnsSender } from "../src/server/senders/apns.js";
|
|
5
5
|
import { createFcmSender } from "../src/server/senders/fcm.js";
|
|
6
|
-
import {
|
|
6
|
+
import { isPushServiceEndpoint } from "../src/server/senders/pushServiceEndpoint.js";
|
|
7
|
+
import { createWebPushSender } from "../src/server/senders/webPush.js";
|
|
7
8
|
import { answer, testCredentials } from "./support/credentials.js";
|
|
8
9
|
|
|
9
10
|
const { vapid, apnsKey, serviceAccount } = testCredentials();
|