@rei-standard/amsg-sw 2.1.0-next.2 → 2.1.0-next.3

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 CHANGED
@@ -78,6 +78,11 @@ installReiSW(self, {
78
78
  maxTotalBytes: 256_000,
79
79
  maxChunks: 128,
80
80
  cleanupIntervalMs: 15 * 60_000
81
+ },
82
+ // (新增于 2.1.0-next.3)离线持久化等业务拦截钩子:
83
+ onBusinessPayload: async (payload) => {
84
+ // 收到完整 payload 时触发,由于内置在 event.waitUntil 中,能够确保离线写库完毕再允许 SW 休眠
85
+ // await db.saveIncomingMessage(payload);
81
86
  }
82
87
  });
83
88
  ```
@@ -125,7 +130,10 @@ import { installReiSW } from '@rei-standard/amsg-sw';
125
130
  installReiSW(self, {
126
131
  defaultIcon: '/icon-192x192.png',
127
132
  defaultBadge: '/badge-72x72.png',
128
- multipart: { enabled: true }
133
+ multipart: { enabled: true },
134
+ onBusinessPayload: async (payload) => {
135
+ // 这里可安全地进行应用级别的离线数据库存储
136
+ }
129
137
  });
130
138
 
131
139
  // 业务侧自行实现点击跳转
package/dist/index.cjs CHANGED
@@ -68,6 +68,7 @@ function installReiSW(sw, opts = {}) {
68
68
  defaultBadge,
69
69
  defaultIcon,
70
70
  multipart,
71
+ onBusinessPayload: opts.onBusinessPayload,
71
72
  getLastMultipartCleanupAt: () => lastMultipartCleanupAt,
72
73
  setLastMultipartCleanupAt: (value) => {
73
74
  lastMultipartCleanupAt = value;
@@ -103,7 +104,8 @@ async function handlePushPayload(sw, payload, ctx) {
103
104
  }
104
105
  await dispatchBusinessPayload(sw, payload, {
105
106
  defaultIcon: ctx.defaultIcon,
106
- defaultBadge: ctx.defaultBadge
107
+ defaultBadge: ctx.defaultBadge,
108
+ onBusinessPayload: ctx.onBusinessPayload
107
109
  });
108
110
  }
109
111
  async function dispatchBusinessPayload(sw, payload, defaults) {
@@ -118,6 +120,18 @@ async function dispatchBusinessPayload(sw, payload, defaults) {
118
120
  );
119
121
  }
120
122
  }
123
+ if (typeof defaults.onBusinessPayload === "function") {
124
+ try {
125
+ const result = defaults.onBusinessPayload(payload);
126
+ if (result instanceof Promise) {
127
+ work.push(result.catch((error) => {
128
+ console.error("[rei-standard-amsg-sw] onBusinessPayload promise rejected:", error);
129
+ }));
130
+ }
131
+ } catch (error) {
132
+ console.error("[rei-standard-amsg-sw] onBusinessPayload error:", error);
133
+ }
134
+ }
121
135
  await Promise.all(work);
122
136
  }
123
137
  function resolveEventName(payload) {
package/dist/index.d.cts CHANGED
@@ -111,6 +111,7 @@ const REI_SW_MESSAGE_TYPE = Object.freeze({
111
111
  * @property {number} [multipart.maxTotalBytes=256000]
112
112
  * @property {number} [multipart.maxChunks=128]
113
113
  * @property {number} [multipart.cleanupIntervalMs=900000]
114
+ * @property {(payload: any) => void | Promise<void>} [onBusinessPayload]
114
115
  */
115
116
 
116
117
  /**
@@ -133,6 +134,7 @@ function installReiSW(sw, opts = {}) {
133
134
  defaultBadge,
134
135
  defaultIcon,
135
136
  multipart,
137
+ onBusinessPayload: opts.onBusinessPayload,
136
138
  getLastMultipartCleanupAt: () => lastMultipartCleanupAt,
137
139
  setLastMultipartCleanupAt: (value) => { lastMultipartCleanupAt = value; },
138
140
  }));
@@ -174,6 +176,7 @@ async function handlePushPayload(sw, payload, ctx) {
174
176
  await dispatchBusinessPayload(sw, payload, {
175
177
  defaultIcon: ctx.defaultIcon,
176
178
  defaultBadge: ctx.defaultBadge,
179
+ onBusinessPayload: ctx.onBusinessPayload,
177
180
  });
178
181
  }
179
182
 
@@ -193,6 +196,19 @@ async function dispatchBusinessPayload(sw, payload, defaults) {
193
196
  }
194
197
  }
195
198
 
199
+ if (typeof defaults.onBusinessPayload === 'function') {
200
+ try {
201
+ const result = defaults.onBusinessPayload(payload);
202
+ if (result instanceof Promise) {
203
+ work.push(result.catch(error => {
204
+ console.error('[rei-standard-amsg-sw] onBusinessPayload promise rejected:', error);
205
+ }));
206
+ }
207
+ } catch (error) {
208
+ console.error('[rei-standard-amsg-sw] onBusinessPayload error:', error);
209
+ }
210
+ }
211
+
196
212
  await Promise.all(work);
197
213
  }
198
214
 
package/dist/index.d.ts CHANGED
@@ -111,6 +111,7 @@ const REI_SW_MESSAGE_TYPE = Object.freeze({
111
111
  * @property {number} [multipart.maxTotalBytes=256000]
112
112
  * @property {number} [multipart.maxChunks=128]
113
113
  * @property {number} [multipart.cleanupIntervalMs=900000]
114
+ * @property {(payload: any) => void | Promise<void>} [onBusinessPayload]
114
115
  */
115
116
 
116
117
  /**
@@ -133,6 +134,7 @@ function installReiSW(sw, opts = {}) {
133
134
  defaultBadge,
134
135
  defaultIcon,
135
136
  multipart,
137
+ onBusinessPayload: opts.onBusinessPayload,
136
138
  getLastMultipartCleanupAt: () => lastMultipartCleanupAt,
137
139
  setLastMultipartCleanupAt: (value) => { lastMultipartCleanupAt = value; },
138
140
  }));
@@ -174,6 +176,7 @@ async function handlePushPayload(sw, payload, ctx) {
174
176
  await dispatchBusinessPayload(sw, payload, {
175
177
  defaultIcon: ctx.defaultIcon,
176
178
  defaultBadge: ctx.defaultBadge,
179
+ onBusinessPayload: ctx.onBusinessPayload,
177
180
  });
178
181
  }
179
182
 
@@ -193,6 +196,19 @@ async function dispatchBusinessPayload(sw, payload, defaults) {
193
196
  }
194
197
  }
195
198
 
199
+ if (typeof defaults.onBusinessPayload === 'function') {
200
+ try {
201
+ const result = defaults.onBusinessPayload(payload);
202
+ if (result instanceof Promise) {
203
+ work.push(result.catch(error => {
204
+ console.error('[rei-standard-amsg-sw] onBusinessPayload promise rejected:', error);
205
+ }));
206
+ }
207
+ } catch (error) {
208
+ console.error('[rei-standard-amsg-sw] onBusinessPayload error:', error);
209
+ }
210
+ }
211
+
196
212
  await Promise.all(work);
197
213
  }
198
214
 
package/dist/index.mjs CHANGED
@@ -42,6 +42,7 @@ function installReiSW(sw, opts = {}) {
42
42
  defaultBadge,
43
43
  defaultIcon,
44
44
  multipart,
45
+ onBusinessPayload: opts.onBusinessPayload,
45
46
  getLastMultipartCleanupAt: () => lastMultipartCleanupAt,
46
47
  setLastMultipartCleanupAt: (value) => {
47
48
  lastMultipartCleanupAt = value;
@@ -77,7 +78,8 @@ async function handlePushPayload(sw, payload, ctx) {
77
78
  }
78
79
  await dispatchBusinessPayload(sw, payload, {
79
80
  defaultIcon: ctx.defaultIcon,
80
- defaultBadge: ctx.defaultBadge
81
+ defaultBadge: ctx.defaultBadge,
82
+ onBusinessPayload: ctx.onBusinessPayload
81
83
  });
82
84
  }
83
85
  async function dispatchBusinessPayload(sw, payload, defaults) {
@@ -92,6 +94,18 @@ async function dispatchBusinessPayload(sw, payload, defaults) {
92
94
  );
93
95
  }
94
96
  }
97
+ if (typeof defaults.onBusinessPayload === "function") {
98
+ try {
99
+ const result = defaults.onBusinessPayload(payload);
100
+ if (result instanceof Promise) {
101
+ work.push(result.catch((error) => {
102
+ console.error("[rei-standard-amsg-sw] onBusinessPayload promise rejected:", error);
103
+ }));
104
+ }
105
+ } catch (error) {
106
+ console.error("[rei-standard-amsg-sw] onBusinessPayload error:", error);
107
+ }
108
+ }
95
109
  await Promise.all(work);
96
110
  }
97
111
  function resolveEventName(payload) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rei-standard/amsg-sw",
3
- "version": "2.1.0-next.2",
3
+ "version": "2.1.0-next.3",
4
4
  "description": "ReiStandard Active Messaging service worker SDK — three-axis push schema (content / reasoning / tool_request / error) with per-kind client postMessage events",
5
5
  "repository": {
6
6
  "type": "git",