

























@@ -1,6 +1,5 @@
11import { createHash, randomUUID } from "node:crypto";
22import path from "node:path";
3-import webPush from "web-push";
43import { resolveStateDir } from "../config/paths.js";
54import { createAsyncLock, readJsonFile, writeJsonAtomic } from "./json-files.js";
65@@ -41,6 +40,18 @@ const DEFAULT_VAPID_SUBJECT = "mailto:openclaw@localhost";
41404241const withLock = createAsyncLock();
434243+type WebPushRuntime = typeof import("web-push");
44+type WebPushRuntimeModule = WebPushRuntime & { default?: WebPushRuntime };
45+46+let webPushRuntimePromise: Promise<WebPushRuntime> | undefined;
47+48+async function loadWebPushRuntime(): Promise<WebPushRuntime> {
49+webPushRuntimePromise ??= import("web-push").then(
50+(mod: WebPushRuntimeModule) => mod.default ?? mod,
51+);
52+return await webPushRuntimePromise;
53+}
54+4455// --- Helpers ---
45564657function resolveWebPushStatePath(baseDir?: string): string {
@@ -115,6 +126,7 @@ export async function resolveVapidKeys(baseDir?: string): Promise<VapidKeyPair>
115126};
116127}
117128129+const webPush = await loadWebPushRuntime();
118130const keys = webPush.generateVAPIDKeys();
119131const pair: VapidKeyPair = {
120132publicKey: keys.publicKey,
@@ -238,7 +250,7 @@ export type WebPushPayload = {
238250url?: string;
239251};
240252241-function applyVapidDetails(keys: VapidKeyPair): void {
253+function applyVapidDetails(webPush: WebPushRuntime, keys: VapidKeyPair): void {
242254webPush.setVapidDetails(keys.subject, keys.publicKey, keys.privateKey);
243255}
244256@@ -248,12 +260,14 @@ export async function sendWebPushNotification(
248260vapidKeys?: VapidKeyPair,
249261): Promise<WebPushSendResult> {
250262const keys = vapidKeys ?? (await resolveVapidKeys());
251-applyVapidDetails(keys);
263+const webPush = await loadWebPushRuntime();
264+applyVapidDetails(webPush, keys);
252265253-return sendPreparedWebPushNotification(subscription, payload);
266+return sendPreparedWebPushNotification(webPush, subscription, payload);
254267}
255268256269async function sendPreparedWebPushNotification(
270+webPush: WebPushRuntime,
257271subscription: WebPushSubscription,
258272payload: WebPushPayload,
259273): Promise<WebPushSendResult> {
@@ -300,12 +314,13 @@ export async function broadcastWebPush(
300314}
301315302316const vapidKeys = await resolveVapidKeys(baseDir);
317+const webPush = await loadWebPushRuntime();
303318304319// Set VAPID details once before fanning out concurrent sends.
305-applyVapidDetails(vapidKeys);
320+applyVapidDetails(webPush, vapidKeys);
306321307322const results = await Promise.allSettled(
308-subscriptions.map((sub) => sendPreparedWebPushNotification(sub, payload)),
323+subscriptions.map((sub) => sendPreparedWebPushNotification(webPush, sub, payload)),
309324);
310325311326const mapped = results.map((r, i) =>
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。