postalworker-es 4.0.8 → 5.0.0
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 +152 -35
- package/dist/PostalSharedWorker.js +305 -513
- package/dist/PostalSharedWorker.js.map +7 -0
- package/dist/PostalWorker.min.js +2 -171
- package/dist/PostalWorker.min.js.map +7 -0
- package/dist/compat.d.ts +53 -0
- package/dist/core.d.ts +104 -0
- package/dist/cross.d.ts +32 -0
- package/dist/emitter.d.ts +15 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +860 -0
- package/dist/index.js.map +7 -0
- package/dist/protocol.d.ts +125 -0
- package/dist/types.d.ts +77 -0
- package/dist/worker-script.d.ts +44 -0
- package/dist/worker-script.js +8 -0
- package/dist/worker-script.js.map +7 -0
- package/package.json +47 -71
- package/dist/PolyfillSharedWorker.js +0 -525
- package/dist/PostalWorker.es.js +0 -408
|
@@ -1,525 +0,0 @@
|
|
|
1
|
-
(function () {
|
|
2
|
-
'use strict';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* PostalWorker String Constants
|
|
6
|
-
* @type {string}
|
|
7
|
-
*/
|
|
8
|
-
const POSTAL_WORKER = "PostalWorker";
|
|
9
|
-
const POSTAL_SHARED_WORKER = "PostalSharedWorker";
|
|
10
|
-
const FIRE = "FIRE";
|
|
11
|
-
const WORKERREGISTER = "WORKERREGISTER";
|
|
12
|
-
const REGISTERID = "REGISTERID";
|
|
13
|
-
const UNREGISTERID = "UNREGISTERID";
|
|
14
|
-
const LOAD = "LOAD";
|
|
15
|
-
const POBOX = "POBOX";
|
|
16
|
-
const POST = "POST";
|
|
17
|
-
const COLLECT = "COLLECT";
|
|
18
|
-
const CLOSEBOX = "CLOSEBOX";
|
|
19
|
-
const BOXCLOSED = "BOXCLOSED";
|
|
20
|
-
const PACKAGE = "PACKAGE";
|
|
21
|
-
const PACK = "PACK";
|
|
22
|
-
const DELIVERY = "DELIVERY";
|
|
23
|
-
|
|
24
|
-
var hasProp = Object.prototype.hasOwnProperty;
|
|
25
|
-
|
|
26
|
-
function throwsMessage(err) {
|
|
27
|
-
return '[Throws: ' + (err ? err.message : '?') + ']';
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function safeGetValueFromPropertyOnObject(obj, property) {
|
|
31
|
-
if (hasProp.call(obj, property)) {
|
|
32
|
-
try {
|
|
33
|
-
return obj[property];
|
|
34
|
-
}
|
|
35
|
-
catch (err) {
|
|
36
|
-
return throwsMessage(err);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return obj[property];
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function ensureProperties(obj) {
|
|
44
|
-
var seen = [ ]; // store references to objects we have seen before
|
|
45
|
-
|
|
46
|
-
function visit(obj) {
|
|
47
|
-
if (obj === null || typeof obj !== 'object') {
|
|
48
|
-
return obj;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (seen.indexOf(obj) !== -1) {
|
|
52
|
-
return '[Circular]';
|
|
53
|
-
}
|
|
54
|
-
seen.push(obj);
|
|
55
|
-
|
|
56
|
-
if (typeof obj.toJSON === 'function') {
|
|
57
|
-
try {
|
|
58
|
-
var fResult = visit(obj.toJSON());
|
|
59
|
-
seen.pop();
|
|
60
|
-
return fResult;
|
|
61
|
-
} catch(err) {
|
|
62
|
-
return throwsMessage(err);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (Array.isArray(obj)) {
|
|
67
|
-
var aResult = obj.map(visit);
|
|
68
|
-
seen.pop();
|
|
69
|
-
return aResult;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
var result = Object.keys(obj).reduce(function(result, prop) {
|
|
73
|
-
// prevent faulty defined getter properties
|
|
74
|
-
result[prop] = visit(safeGetValueFromPropertyOnObject(obj, prop));
|
|
75
|
-
return result;
|
|
76
|
-
}, {});
|
|
77
|
-
seen.pop();
|
|
78
|
-
return result;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return visit(obj);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
var index = function(data, replacer, space) {
|
|
85
|
-
return JSON.stringify(ensureProperties(data), replacer, space);
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
var ensureProperties_1 = ensureProperties;
|
|
89
|
-
|
|
90
|
-
index.ensureProperties = ensureProperties_1;
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Postal Worker - Shared Worker Thread Engine
|
|
94
|
-
* @Description: Use web worker threading to fork threads and use postMessage to pass serialized data between threads and windows
|
|
95
|
-
* @Author: Russ Stratfull
|
|
96
|
-
* @Contributors: Sakshi Dheer, & Francois Wauquier
|
|
97
|
-
*/
|
|
98
|
-
|
|
99
|
-
const _env = {
|
|
100
|
-
_id: "SharedWorker",
|
|
101
|
-
_members: {},
|
|
102
|
-
_stringify: index,
|
|
103
|
-
_events: new Map(),
|
|
104
|
-
_scripts: new Map(),
|
|
105
|
-
_channel: new BroadcastChannel(POSTAL_WORKER),
|
|
106
|
-
_subscriptions: new Map(),
|
|
107
|
-
_postBoxes: {},
|
|
108
|
-
_packages: {},
|
|
109
|
-
PostalWorker: {}
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Load a library (script) into the worker
|
|
114
|
-
* @param {*} msg
|
|
115
|
-
*/
|
|
116
|
-
function _loadLibrary(msg) {
|
|
117
|
-
let scrpt = _env._scripts.get(msg.data);
|
|
118
|
-
// if script already exists, don't reload it
|
|
119
|
-
if (scrpt) {
|
|
120
|
-
// just add window id to it
|
|
121
|
-
if (!scrpt.includes(msg.id)) scrpt.push(msg.id);
|
|
122
|
-
} else {
|
|
123
|
-
try {
|
|
124
|
-
// Thank you chatGpt for this one
|
|
125
|
-
// Create a closure and importScripts in the closure
|
|
126
|
-
// this allows binding the imported script to the current scope
|
|
127
|
-
// and PostalWorker can be passed securely to it, a little like
|
|
128
|
-
// import in es modules
|
|
129
|
-
const closure = (() => {
|
|
130
|
-
importScripts(msg.data);
|
|
131
|
-
return () => {
|
|
132
|
-
return {
|
|
133
|
-
usePrivate: init
|
|
134
|
-
};
|
|
135
|
-
};
|
|
136
|
-
})();
|
|
137
|
-
const importedScript = closure();
|
|
138
|
-
// This is the magic
|
|
139
|
-
importedScript.usePrivate(PostalWorker);
|
|
140
|
-
|
|
141
|
-
_env._scripts.set(msg.data, [msg.id]);
|
|
142
|
-
} catch (e) {
|
|
143
|
-
console.error(e);
|
|
144
|
-
_env._channel.postMessage(
|
|
145
|
-
_env._stringify({
|
|
146
|
-
postal: true,
|
|
147
|
-
type: "ERROR",
|
|
148
|
-
data: `PostalWorkerWorker Error: Unable to load file ${msg.data}`
|
|
149
|
-
})
|
|
150
|
-
);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Add a node to the registry of members
|
|
157
|
-
* @param {*} msg
|
|
158
|
-
*/
|
|
159
|
-
function _addNode(msg) {
|
|
160
|
-
const { id, nodeType } = msg;
|
|
161
|
-
_env._members[id] = nodeType;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Remove a node from the registry of members
|
|
166
|
-
* @param {*} msg
|
|
167
|
-
*/
|
|
168
|
-
function _removeNode(msg) {
|
|
169
|
-
const { id } = msg;
|
|
170
|
-
delete _env._members[id];
|
|
171
|
-
Object.entries(_env._postBoxes).forEach(box => {
|
|
172
|
-
const [, node] = box;
|
|
173
|
-
if (node.members.includes(id))
|
|
174
|
-
node.members.splice(node.members.indexOf(id), 1);
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
*
|
|
180
|
-
* @param {*} msg
|
|
181
|
-
* @param {Function} callback - optional callback
|
|
182
|
-
*/
|
|
183
|
-
function _registerPObox(msg, callback) {
|
|
184
|
-
const { id, address } = msg;
|
|
185
|
-
let addr = _env._postBoxes[address];
|
|
186
|
-
if (addr) {
|
|
187
|
-
if (!addr.members.includes(id)) addr.members.push(id);
|
|
188
|
-
if (!addr.handlers.length) addr.handlers.push(callback);
|
|
189
|
-
} else {
|
|
190
|
-
_env._postBoxes[address] = {
|
|
191
|
-
members: [id],
|
|
192
|
-
handlers: callback ? [callback] : [],
|
|
193
|
-
value: null
|
|
194
|
-
};
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
const box = Object.entries(_env._postBoxes).reduce((store, current) => {
|
|
198
|
-
store[current[0]] = current[1].value;
|
|
199
|
-
return store;
|
|
200
|
-
}, {});
|
|
201
|
-
|
|
202
|
-
PostalWorker().fire(COLLECT, {
|
|
203
|
-
address: address,
|
|
204
|
-
value: _env._postBoxes[address].value,
|
|
205
|
-
box
|
|
206
|
-
});
|
|
207
|
-
// Is there a worker handler for this item?
|
|
208
|
-
for (const [key, bx] of Object.entries(_env._postBoxes)) {
|
|
209
|
-
if (key === address && bx.handlers.length) {
|
|
210
|
-
bx.handlers.forEach(handler => {
|
|
211
|
-
if (handler) {
|
|
212
|
-
handler({ address, value: _env._postBoxes[address].value, box });
|
|
213
|
-
}
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
*
|
|
221
|
-
* @param {*} msg
|
|
222
|
-
*/
|
|
223
|
-
function _postBox(msg) {
|
|
224
|
-
const { address, value } = msg;
|
|
225
|
-
let addr = _env._postBoxes[address];
|
|
226
|
-
if (addr) addr.value = value;
|
|
227
|
-
// todo: box not created case?
|
|
228
|
-
const box = Object.entries(_env._postBoxes).reduce((store, current) => {
|
|
229
|
-
store[current[0]] = current[1].value;
|
|
230
|
-
return store;
|
|
231
|
-
}, {});
|
|
232
|
-
PostalWorker().fire(COLLECT, { address, value, box });
|
|
233
|
-
// Is there a worker handler for this item?
|
|
234
|
-
for (const [key, bx] of Object.entries(_env._postBoxes)) {
|
|
235
|
-
if (key === address && bx.handlers.length) {
|
|
236
|
-
bx.handlers.forEach(handler => {
|
|
237
|
-
if (handler) handler({ address, value, box });
|
|
238
|
-
});
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
*
|
|
245
|
-
* @param {*} msg
|
|
246
|
-
*/
|
|
247
|
-
function _closeBox(msg) {
|
|
248
|
-
const { id, address } = msg;
|
|
249
|
-
let addr = _env._postBoxes[address];
|
|
250
|
-
if (addr) {
|
|
251
|
-
addr.members = addr.members.filter(m => m !== id);
|
|
252
|
-
if (id === _env._id) addr.handlers = [];
|
|
253
|
-
if (!addr.members.length) {
|
|
254
|
-
delete _env._postBoxes[address];
|
|
255
|
-
}
|
|
256
|
-
const value = null;
|
|
257
|
-
const box = Object.entries(_env._postBoxes).reduce((store, current) => {
|
|
258
|
-
store[current[0]] = current[1].value;
|
|
259
|
-
return store;
|
|
260
|
-
}, {});
|
|
261
|
-
PostalWorker().fire(BOXCLOSED, { id, address, value, box });
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
*
|
|
267
|
-
* @param {*} msg
|
|
268
|
-
* @param {*} callback
|
|
269
|
-
*/
|
|
270
|
-
function _preparePackage(msg, callback) {
|
|
271
|
-
const { id, address } = msg;
|
|
272
|
-
let pack = _env._packages[address];
|
|
273
|
-
if (pack) {
|
|
274
|
-
if (!pack.members.includes(id)) pack.members.push(id);
|
|
275
|
-
if (!pack.handlers.length) pack.handlers.push(callback);
|
|
276
|
-
} else
|
|
277
|
-
_env._packages[address] = {
|
|
278
|
-
members: [id],
|
|
279
|
-
handlers: callback ? [callback] : [],
|
|
280
|
-
content: null
|
|
281
|
-
};
|
|
282
|
-
|
|
283
|
-
if (_env._packages[address].content)
|
|
284
|
-
PostalWorker().fire(DELIVERY, {
|
|
285
|
-
address: address,
|
|
286
|
-
content: _env._packages[address].content
|
|
287
|
-
});
|
|
288
|
-
else
|
|
289
|
-
PostalWorker().fire(DELIVERY, {
|
|
290
|
-
address: address,
|
|
291
|
-
content: null
|
|
292
|
-
});
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
/**
|
|
296
|
-
*
|
|
297
|
-
* @param {*} msg
|
|
298
|
-
*/
|
|
299
|
-
function _pack(msg) {
|
|
300
|
-
const { address, content } = msg;
|
|
301
|
-
let addr = _env._packages[address];
|
|
302
|
-
if (addr) addr.content = content;
|
|
303
|
-
|
|
304
|
-
PostalWorker().fire(DELIVERY, { address, content });
|
|
305
|
-
|
|
306
|
-
// for (const [key, bx] of Object.entries(_env._postBoxes)) {
|
|
307
|
-
// if (key === address && bx.handlers.length) {
|
|
308
|
-
// bx.handlers.forEach(handler => {
|
|
309
|
-
// if (handler) {
|
|
310
|
-
// handler({ address, value: _env._postBoxes[address].value, box });
|
|
311
|
-
// }
|
|
312
|
-
// });
|
|
313
|
-
// }
|
|
314
|
-
// }
|
|
315
|
-
|
|
316
|
-
for (const [key, pack] of Object.entries(_env._packages)) {
|
|
317
|
-
if (key === address && pack.handlers.length) {
|
|
318
|
-
pack.handlers.forEach(handler => {
|
|
319
|
-
if (handler) {
|
|
320
|
-
handler({ address, content: content });
|
|
321
|
-
}
|
|
322
|
-
});
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
/**
|
|
328
|
-
*
|
|
329
|
-
* @param {*} msg
|
|
330
|
-
*/
|
|
331
|
-
function _processChannelMessage(msg) {
|
|
332
|
-
// console.info(msg);
|
|
333
|
-
if (!msg.postal) return;
|
|
334
|
-
// console.info(msg);
|
|
335
|
-
switch (msg.type) {
|
|
336
|
-
case FIRE:
|
|
337
|
-
if (_env._events.has(msg.data.msgClass)) {
|
|
338
|
-
_env._events.get(msg.data.msgClass)(msg.data.message, msg.id);
|
|
339
|
-
}
|
|
340
|
-
break;
|
|
341
|
-
case LOAD:
|
|
342
|
-
_loadLibrary(msg);
|
|
343
|
-
break;
|
|
344
|
-
case REGISTERID:
|
|
345
|
-
_addNode(msg);
|
|
346
|
-
break;
|
|
347
|
-
case UNREGISTERID:
|
|
348
|
-
_removeNode(msg);
|
|
349
|
-
break;
|
|
350
|
-
case POBOX:
|
|
351
|
-
_registerPObox(msg);
|
|
352
|
-
break;
|
|
353
|
-
case POST:
|
|
354
|
-
_postBox(msg);
|
|
355
|
-
break;
|
|
356
|
-
case CLOSEBOX:
|
|
357
|
-
_closeBox(msg);
|
|
358
|
-
break;
|
|
359
|
-
case PACKAGE:
|
|
360
|
-
_preparePackage(msg);
|
|
361
|
-
break;
|
|
362
|
-
case PACK:
|
|
363
|
-
_pack(msg);
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
const PostalWorker = () => ({
|
|
368
|
-
/**
|
|
369
|
-
*
|
|
370
|
-
* @param msgClass
|
|
371
|
-
* @param action
|
|
372
|
-
*/
|
|
373
|
-
on: (msgClass, action) => {
|
|
374
|
-
_env._events.set(msgClass, action);
|
|
375
|
-
},
|
|
376
|
-
|
|
377
|
-
/**
|
|
378
|
-
*
|
|
379
|
-
* @param msgClass
|
|
380
|
-
*/
|
|
381
|
-
un: msgClass => {
|
|
382
|
-
_env._events.delete(msgClass);
|
|
383
|
-
},
|
|
384
|
-
|
|
385
|
-
/**
|
|
386
|
-
*
|
|
387
|
-
* @param msgClass
|
|
388
|
-
* @param msg
|
|
389
|
-
*/
|
|
390
|
-
fire: (msgClass, msg) => {
|
|
391
|
-
_env._channel.postMessage(
|
|
392
|
-
_env._stringify({
|
|
393
|
-
postal: true,
|
|
394
|
-
type: FIRE,
|
|
395
|
-
id: POSTAL_SHARED_WORKER,
|
|
396
|
-
data: {
|
|
397
|
-
msgClass: msgClass,
|
|
398
|
-
message: msg
|
|
399
|
-
}
|
|
400
|
-
})
|
|
401
|
-
);
|
|
402
|
-
// deprecated
|
|
403
|
-
// PostalWorker._postMessenger(
|
|
404
|
-
// S.FIRE,
|
|
405
|
-
// audience,
|
|
406
|
-
// {
|
|
407
|
-
// msgClass: msgClass,
|
|
408
|
-
// message: msg,
|
|
409
|
-
// audience: audience
|
|
410
|
-
// },
|
|
411
|
-
// target
|
|
412
|
-
// );
|
|
413
|
-
},
|
|
414
|
-
|
|
415
|
-
/**
|
|
416
|
-
*
|
|
417
|
-
* @param {*} library
|
|
418
|
-
* @returns
|
|
419
|
-
*/
|
|
420
|
-
load: library => _loadLibrary(library),
|
|
421
|
-
|
|
422
|
-
/**
|
|
423
|
-
* Subscribe to Server Sent Events and create event source
|
|
424
|
-
* @param {*} endpoint
|
|
425
|
-
* @param {*} callback
|
|
426
|
-
* @param {*} errorCallback
|
|
427
|
-
*/
|
|
428
|
-
subscribe: (endpoint, callback, errorCallback) => {
|
|
429
|
-
if (!_env._subscriptions.get(endpoint)) {
|
|
430
|
-
_env._subscriptions.set(endpoint, new EventSource(endpoint));
|
|
431
|
-
_env._subscriptions.get(endpoint).onmessage = msg => {
|
|
432
|
-
callback(msg);
|
|
433
|
-
};
|
|
434
|
-
if (errorCallback) {
|
|
435
|
-
_env._subscriptions.get(endpoint).onerror = error => {
|
|
436
|
-
errorCallback(error);
|
|
437
|
-
};
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
},
|
|
441
|
-
|
|
442
|
-
postBox: (address, callback) => {
|
|
443
|
-
_registerPObox(
|
|
444
|
-
{
|
|
445
|
-
id: _env._id,
|
|
446
|
-
address
|
|
447
|
-
},
|
|
448
|
-
callback
|
|
449
|
-
);
|
|
450
|
-
},
|
|
451
|
-
|
|
452
|
-
/**
|
|
453
|
-
*
|
|
454
|
-
* @param {*} address
|
|
455
|
-
* @param {*} value
|
|
456
|
-
*/
|
|
457
|
-
post: (address, value) => {
|
|
458
|
-
_postBox({ address, value });
|
|
459
|
-
},
|
|
460
|
-
|
|
461
|
-
/**
|
|
462
|
-
*
|
|
463
|
-
* @param {*} address
|
|
464
|
-
*/
|
|
465
|
-
closeBox: address => {
|
|
466
|
-
_closeBox({ address, id: _env._id });
|
|
467
|
-
},
|
|
468
|
-
|
|
469
|
-
/**
|
|
470
|
-
*
|
|
471
|
-
* @param {*} address
|
|
472
|
-
* @param {*} handling
|
|
473
|
-
*/
|
|
474
|
-
package: (address, handling) => {
|
|
475
|
-
// id, address
|
|
476
|
-
_preparePackage(
|
|
477
|
-
{
|
|
478
|
-
id: _env._id,
|
|
479
|
-
address: address
|
|
480
|
-
},
|
|
481
|
-
handling
|
|
482
|
-
);
|
|
483
|
-
},
|
|
484
|
-
|
|
485
|
-
/**
|
|
486
|
-
*
|
|
487
|
-
* @param {*} address
|
|
488
|
-
* @param {*} content
|
|
489
|
-
*/
|
|
490
|
-
pack: (address, content) => {
|
|
491
|
-
let xhRequest = new XMLHttpRequest();
|
|
492
|
-
xhRequest.onload = function () {
|
|
493
|
-
let reader = new FileReader();
|
|
494
|
-
reader.onloadend = function () {
|
|
495
|
-
_pack({ address, content: reader.result });
|
|
496
|
-
};
|
|
497
|
-
reader.readAsDataURL(xhRequest.response);
|
|
498
|
-
};
|
|
499
|
-
xhRequest.open("GET", content);
|
|
500
|
-
xhRequest.responseType = "blob";
|
|
501
|
-
xhRequest.send();
|
|
502
|
-
},
|
|
503
|
-
|
|
504
|
-
// temp
|
|
505
|
-
env: () => _env
|
|
506
|
-
});
|
|
507
|
-
|
|
508
|
-
(function () {
|
|
509
|
-
// New messaging method using broadcast channel
|
|
510
|
-
if (!_env._channel.onmessage)
|
|
511
|
-
_env._channel.onmessage = event => {
|
|
512
|
-
const data = JSON.parse(event.data);
|
|
513
|
-
_processChannelMessage(data);
|
|
514
|
-
};
|
|
515
|
-
|
|
516
|
-
// Announce alive to broadcast channel
|
|
517
|
-
_env._channel.postMessage(
|
|
518
|
-
_env._stringify({
|
|
519
|
-
postal: true,
|
|
520
|
-
type: WORKERREGISTER
|
|
521
|
-
})
|
|
522
|
-
);
|
|
523
|
-
})();
|
|
524
|
-
|
|
525
|
-
}());
|