@ohos-ports/serwist-window 9.5.12-beta.1 → 9.5.12-beta.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.
- package/dist/ohos-node-shim.mjs +27 -12
- package/package.json +1 -1
- package/src/utils/ohosNodeShim.ts +20 -9
package/dist/ohos-node-shim.mjs
CHANGED
|
@@ -84,11 +84,13 @@ if (isNodeRuntime && !hasRealServiceWorkerContainer) {
|
|
|
84
84
|
//#region ServiceWorker
|
|
85
85
|
/**
|
|
86
86
|
* A ServiceWorker handle whose message channel is a real
|
|
87
|
-
* `worker_threads` MessagePort pair.
|
|
88
|
-
*
|
|
89
|
-
* posted
|
|
90
|
-
*
|
|
91
|
-
*
|
|
87
|
+
* `worker_threads` MessagePort pair. The page side of the channel is
|
|
88
|
+
* `port2`, the emulated SW side is `port1`: `postMessage()` by the page
|
|
89
|
+
* is posted on `port2`, so it emerges on the `port1` listener installed
|
|
90
|
+
* by `register()` and surfaces as a container-level `message` event
|
|
91
|
+
* (with any transferred reply ports intact). Messages posted by the
|
|
92
|
+
* emulated SW on the transferred reply port emerge on the page's own
|
|
93
|
+
* channel, matching the browser contract.
|
|
92
94
|
*/
|
|
93
95
|
class ShimServiceWorker extends ShimEventTarget {
|
|
94
96
|
_channel;
|
|
@@ -104,8 +106,13 @@ if (isNodeRuntime && !hasRealServiceWorkerContainer) {
|
|
|
104
106
|
}
|
|
105
107
|
|
|
106
108
|
postMessage(data, transfer) {
|
|
107
|
-
// Sent from the page, delivered on the worker side.
|
|
108
|
-
|
|
109
|
+
// Sent from the page, delivered on the worker (SW) side. In Node.js
|
|
110
|
+
// `worker_threads` semantics, `port2.postMessage()` delivers to the
|
|
111
|
+
// entangled peer `port1` — the port the registration listens on.
|
|
112
|
+
// (Posting on `port1` would deliver to `port2`, which has no
|
|
113
|
+
// listener, silently dropping the message and any transferred
|
|
114
|
+
// reply ports.)
|
|
115
|
+
this._channel.port2.postMessage(data, transfer ?? []);
|
|
109
116
|
}
|
|
110
117
|
|
|
111
118
|
_setState(state) {
|
|
@@ -172,9 +179,13 @@ if (isNodeRuntime && !hasRealServiceWorkerContainer) {
|
|
|
172
179
|
|
|
173
180
|
const channel = new MessageChannel();
|
|
174
181
|
const sw = new ShimServiceWorker(resolvedURL, channel);
|
|
175
|
-
//
|
|
176
|
-
//
|
|
177
|
-
//
|
|
182
|
+
// The page posts messages on `port2` (see ShimServiceWorker
|
|
183
|
+
// .postMessage), so they emerge here on `port1` and surface as
|
|
184
|
+
// container-level `message` events with `source` set to the
|
|
185
|
+
// worker. Any transferred reply ports arrive via `event.ports`,
|
|
186
|
+
// so SW-side code (tests, message handlers) can reply through
|
|
187
|
+
// `event.ports[0].postMessage()` and reach the page's own
|
|
188
|
+
// `messageSW()` channel — matching the browser contract.
|
|
178
189
|
channel.port1.onmessage = (event) => {
|
|
179
190
|
this._dispatch("message", {
|
|
180
191
|
data: event.data,
|
|
@@ -199,6 +210,10 @@ if (isNodeRuntime && !hasRealServiceWorkerContainer) {
|
|
|
199
210
|
registration.installing = null;
|
|
200
211
|
sw._setState("installed");
|
|
201
212
|
|
|
213
|
+
// Use a small observable delay between lifecycle phases so
|
|
214
|
+
// `registration.waiting` (like a real browser's waiting worker)
|
|
215
|
+
// stays observable to page-side polling before activation
|
|
216
|
+
// proceeds.
|
|
202
217
|
setTimeout(() => {
|
|
203
218
|
registration.waiting = null;
|
|
204
219
|
registration.active = sw;
|
|
@@ -208,8 +223,8 @@ if (isNodeRuntime && !hasRealServiceWorkerContainer) {
|
|
|
208
223
|
this.#controller = sw;
|
|
209
224
|
sw._setState("activated");
|
|
210
225
|
this._dispatch("controllerchange", { target: this });
|
|
211
|
-
},
|
|
212
|
-
},
|
|
226
|
+
}, 25);
|
|
227
|
+
}, 25);
|
|
213
228
|
}, 0);
|
|
214
229
|
|
|
215
230
|
return registration;
|
package/package.json
CHANGED
|
@@ -82,11 +82,13 @@ if (isNodeRuntime && !hasRealServiceWorkerContainer) {
|
|
|
82
82
|
//#region ServiceWorker
|
|
83
83
|
/**
|
|
84
84
|
* A ServiceWorker handle whose message channel is a real
|
|
85
|
-
* `worker_threads` MessagePort pair.
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
85
|
+
* `worker_threads` MessagePort pair. The page side of the channel is
|
|
86
|
+
* `port2`, the emulated SW side is `port1`: `postMessage()` by the page
|
|
87
|
+
* is posted on `port2`, so it emerges on the `port1` listener installed
|
|
88
|
+
* by `register()` and surfaces as a container-level `message` event
|
|
89
|
+
* (with any transferred reply ports intact). Messages posted by the
|
|
90
|
+
* emulated SW on the transferred reply port emerge on the page's own
|
|
91
|
+
* channel, matching the browser contract.
|
|
90
92
|
*/
|
|
91
93
|
class ShimServiceWorker extends ShimEventTarget {
|
|
92
94
|
scriptURL: string;
|
|
@@ -103,8 +105,13 @@ if (isNodeRuntime && !hasRealServiceWorkerContainer) {
|
|
|
103
105
|
}
|
|
104
106
|
|
|
105
107
|
postMessage(data: any, transfer?: any[]): void {
|
|
106
|
-
// Sent from the page, delivered on the worker side.
|
|
107
|
-
|
|
108
|
+
// Sent from the page, delivered on the worker (SW) side. In Node.js
|
|
109
|
+
// `worker_threads` semantics, `port2.postMessage()` delivers to the
|
|
110
|
+
// entangled peer `port1` — the port the registration listens on.
|
|
111
|
+
// (Posting on `port1` would deliver to `port2`, which has no
|
|
112
|
+
// listener, silently dropping the message and any transferred
|
|
113
|
+
// reply ports.)
|
|
114
|
+
this._channel.port2.postMessage(data, transfer ?? []);
|
|
108
115
|
}
|
|
109
116
|
|
|
110
117
|
_setState(state: string): void {
|
|
@@ -201,6 +208,10 @@ if (isNodeRuntime && !hasRealServiceWorkerContainer) {
|
|
|
201
208
|
registration.installing = null;
|
|
202
209
|
sw._setState("installed");
|
|
203
210
|
|
|
211
|
+
// Use a small observable delay between lifecycle phases so
|
|
212
|
+
// `registration.waiting` (like a real browser's waiting worker)
|
|
213
|
+
// stays observable to page-side polling before activation
|
|
214
|
+
// proceeds.
|
|
204
215
|
setTimeout(() => {
|
|
205
216
|
registration.waiting = null;
|
|
206
217
|
registration.active = sw;
|
|
@@ -210,8 +221,8 @@ if (isNodeRuntime && !hasRealServiceWorkerContainer) {
|
|
|
210
221
|
this.#controller = sw;
|
|
211
222
|
sw._setState("activated");
|
|
212
223
|
this._dispatch("controllerchange", { target: this });
|
|
213
|
-
},
|
|
214
|
-
},
|
|
224
|
+
}, 25);
|
|
225
|
+
}, 25);
|
|
215
226
|
}, 0);
|
|
216
227
|
|
|
217
228
|
return registration;
|