@leoqlin/openclaw-qqbot 1.6.7-alpha1
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/LICENSE +22 -0
- package/README.md +484 -0
- package/README.zh.md +479 -0
- package/bin/qqbot-cli.js +243 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +26 -0
- package/dist/src/admin-resolver.d.ts +33 -0
- package/dist/src/admin-resolver.js +157 -0
- package/dist/src/api.d.ts +301 -0
- package/dist/src/api.js +890 -0
- package/dist/src/channel.d.ts +29 -0
- package/dist/src/channel.js +452 -0
- package/dist/src/config.d.ts +56 -0
- package/dist/src/config.js +278 -0
- package/dist/src/credential-backup.d.ts +31 -0
- package/dist/src/credential-backup.js +66 -0
- package/dist/src/deliver-debounce.d.ts +74 -0
- package/dist/src/deliver-debounce.js +174 -0
- package/dist/src/gateway.d.ts +18 -0
- package/dist/src/gateway.js +2005 -0
- package/dist/src/group-history.d.ts +136 -0
- package/dist/src/group-history.js +226 -0
- package/dist/src/image-server.d.ts +87 -0
- package/dist/src/image-server.js +570 -0
- package/dist/src/inbound-attachments.d.ts +60 -0
- package/dist/src/inbound-attachments.js +248 -0
- package/dist/src/known-users.d.ts +100 -0
- package/dist/src/known-users.js +263 -0
- package/dist/src/message-gating.d.ts +53 -0
- package/dist/src/message-gating.js +107 -0
- package/dist/src/message-queue.d.ts +89 -0
- package/dist/src/message-queue.js +257 -0
- package/dist/src/onboarding.d.ts +10 -0
- package/dist/src/onboarding.js +203 -0
- package/dist/src/outbound-deliver.d.ts +48 -0
- package/dist/src/outbound-deliver.js +392 -0
- package/dist/src/outbound.d.ts +205 -0
- package/dist/src/outbound.js +938 -0
- package/dist/src/proactive.d.ts +170 -0
- package/dist/src/proactive.js +399 -0
- package/dist/src/ref-index-store.d.ts +101 -0
- package/dist/src/ref-index-store.js +298 -0
- package/dist/src/reply-dispatcher.d.ts +35 -0
- package/dist/src/reply-dispatcher.js +311 -0
- package/dist/src/request-context.d.ts +25 -0
- package/dist/src/request-context.js +37 -0
- package/dist/src/runtime.d.ts +3 -0
- package/dist/src/runtime.js +10 -0
- package/dist/src/session-store.d.ts +52 -0
- package/dist/src/session-store.js +254 -0
- package/dist/src/slash-commands.d.ts +77 -0
- package/dist/src/slash-commands.js +1866 -0
- package/dist/src/startup-greeting.d.ts +30 -0
- package/dist/src/startup-greeting.js +97 -0
- package/dist/src/streaming.d.ts +247 -0
- package/dist/src/streaming.js +899 -0
- package/dist/src/stt.d.ts +21 -0
- package/dist/src/stt.js +70 -0
- package/dist/src/tools/channel.d.ts +16 -0
- package/dist/src/tools/channel.js +234 -0
- package/dist/src/tools/remind.d.ts +2 -0
- package/dist/src/tools/remind.js +256 -0
- package/dist/src/types.d.ts +367 -0
- package/dist/src/types.js +17 -0
- package/dist/src/typing-keepalive.d.ts +27 -0
- package/dist/src/typing-keepalive.js +64 -0
- package/dist/src/update-checker.d.ts +36 -0
- package/dist/src/update-checker.js +171 -0
- package/dist/src/utils/audio-convert.d.ts +98 -0
- package/dist/src/utils/audio-convert.js +755 -0
- package/dist/src/utils/chunked-upload.d.ts +68 -0
- package/dist/src/utils/chunked-upload.js +341 -0
- package/dist/src/utils/file-utils.d.ts +61 -0
- package/dist/src/utils/file-utils.js +172 -0
- package/dist/src/utils/image-size.d.ts +51 -0
- package/dist/src/utils/image-size.js +234 -0
- package/dist/src/utils/media-send.d.ts +158 -0
- package/dist/src/utils/media-send.js +499 -0
- package/dist/src/utils/media-tags.d.ts +14 -0
- package/dist/src/utils/media-tags.js +165 -0
- package/dist/src/utils/payload.d.ts +112 -0
- package/dist/src/utils/payload.js +186 -0
- package/dist/src/utils/pkg-version.d.ts +5 -0
- package/dist/src/utils/pkg-version.js +61 -0
- package/dist/src/utils/platform.d.ts +137 -0
- package/dist/src/utils/platform.js +390 -0
- package/dist/src/utils/ssrf-guard.d.ts +25 -0
- package/dist/src/utils/ssrf-guard.js +91 -0
- package/dist/src/utils/text-parsing.d.ts +36 -0
- package/dist/src/utils/text-parsing.js +75 -0
- package/dist/src/utils/upload-cache.d.ts +34 -0
- package/dist/src/utils/upload-cache.js +93 -0
- package/index.ts +31 -0
- package/node_modules/@eshaz/web-worker/LICENSE +201 -0
- package/node_modules/@eshaz/web-worker/README.md +134 -0
- package/node_modules/@eshaz/web-worker/browser.js +17 -0
- package/node_modules/@eshaz/web-worker/cjs/browser.js +16 -0
- package/node_modules/@eshaz/web-worker/cjs/node.js +219 -0
- package/node_modules/@eshaz/web-worker/index.d.ts +4 -0
- package/node_modules/@eshaz/web-worker/node.js +223 -0
- package/node_modules/@eshaz/web-worker/package.json +54 -0
- package/node_modules/@wasm-audio-decoders/common/index.js +5 -0
- package/node_modules/@wasm-audio-decoders/common/package.json +36 -0
- package/node_modules/@wasm-audio-decoders/common/src/WASMAudioDecoderCommon.js +231 -0
- package/node_modules/@wasm-audio-decoders/common/src/WASMAudioDecoderWorker.js +129 -0
- package/node_modules/@wasm-audio-decoders/common/src/puff/README +67 -0
- package/node_modules/@wasm-audio-decoders/common/src/puff/build_puff.js +31 -0
- package/node_modules/@wasm-audio-decoders/common/src/puff/puff.c +863 -0
- package/node_modules/@wasm-audio-decoders/common/src/puff/puff.h +35 -0
- package/node_modules/@wasm-audio-decoders/common/src/utilities.js +3 -0
- package/node_modules/@wasm-audio-decoders/common/types.d.ts +7 -0
- package/node_modules/mpg123-decoder/README.md +265 -0
- package/node_modules/mpg123-decoder/dist/mpg123-decoder.min.js +185 -0
- package/node_modules/mpg123-decoder/dist/mpg123-decoder.min.js.map +1 -0
- package/node_modules/mpg123-decoder/index.js +8 -0
- package/node_modules/mpg123-decoder/package.json +58 -0
- package/node_modules/mpg123-decoder/src/EmscriptenWasm.js +464 -0
- package/node_modules/mpg123-decoder/src/MPEGDecoder.js +200 -0
- package/node_modules/mpg123-decoder/src/MPEGDecoderWebWorker.js +21 -0
- package/node_modules/mpg123-decoder/types.d.ts +30 -0
- package/node_modules/silk-wasm/LICENSE +21 -0
- package/node_modules/silk-wasm/README.md +85 -0
- package/node_modules/silk-wasm/lib/index.cjs +16 -0
- package/node_modules/silk-wasm/lib/index.d.ts +70 -0
- package/node_modules/silk-wasm/lib/index.mjs +16 -0
- package/node_modules/silk-wasm/lib/silk.wasm +0 -0
- package/node_modules/silk-wasm/lib/utils.d.ts +4 -0
- package/node_modules/silk-wasm/package.json +39 -0
- package/node_modules/simple-yenc/.github/FUNDING.yml +1 -0
- package/node_modules/simple-yenc/.prettierignore +1 -0
- package/node_modules/simple-yenc/LICENSE +7 -0
- package/node_modules/simple-yenc/README.md +163 -0
- package/node_modules/simple-yenc/dist/esm.js +1 -0
- package/node_modules/simple-yenc/dist/index.js +1 -0
- package/node_modules/simple-yenc/package.json +50 -0
- package/node_modules/simple-yenc/rollup.config.js +27 -0
- package/node_modules/simple-yenc/src/simple-yenc.js +302 -0
- package/node_modules/ws/LICENSE +20 -0
- package/node_modules/ws/README.md +548 -0
- package/node_modules/ws/browser.js +8 -0
- package/node_modules/ws/index.js +22 -0
- package/node_modules/ws/lib/buffer-util.js +131 -0
- package/node_modules/ws/lib/constants.js +19 -0
- package/node_modules/ws/lib/event-target.js +292 -0
- package/node_modules/ws/lib/extension.js +203 -0
- package/node_modules/ws/lib/limiter.js +55 -0
- package/node_modules/ws/lib/permessage-deflate.js +528 -0
- package/node_modules/ws/lib/receiver.js +706 -0
- package/node_modules/ws/lib/sender.js +602 -0
- package/node_modules/ws/lib/stream.js +161 -0
- package/node_modules/ws/lib/subprotocol.js +62 -0
- package/node_modules/ws/lib/validation.js +152 -0
- package/node_modules/ws/lib/websocket-server.js +554 -0
- package/node_modules/ws/lib/websocket.js +1393 -0
- package/node_modules/ws/package.json +70 -0
- package/node_modules/ws/wrapper.mjs +21 -0
- package/openclaw.plugin.json +17 -0
- package/package.json +70 -0
- package/preload.cjs +33 -0
- package/scripts/cleanup-legacy-plugins.sh +124 -0
- package/scripts/link-sdk-core.cjs +185 -0
- package/scripts/postinstall-link-sdk.js +126 -0
- package/scripts/proactive-api-server.ts +369 -0
- package/scripts/send-proactive.ts +293 -0
- package/scripts/set-markdown.sh +156 -0
- package/scripts/test-sendmedia.ts +116 -0
- package/scripts/upgrade-via-npm.ps1 +460 -0
- package/scripts/upgrade-via-npm.sh +652 -0
- package/scripts/upgrade-via-source.sh +1026 -0
- package/skills/qqbot-channel/SKILL.md +263 -0
- package/skills/qqbot-channel/references/api_references.md +521 -0
- package/skills/qqbot-media/SKILL.md +60 -0
- package/skills/qqbot-remind/SKILL.md +159 -0
- package/src/admin-resolver.ts +181 -0
- package/src/api.ts +1284 -0
- package/src/channel.ts +477 -0
- package/src/config.ts +347 -0
- package/src/credential-backup.ts +72 -0
- package/src/deliver-debounce.ts +229 -0
- package/src/gateway.ts +2245 -0
- package/src/group-history.ts +328 -0
- package/src/image-server.ts +675 -0
- package/src/inbound-attachments.ts +321 -0
- package/src/known-users.ts +353 -0
- package/src/message-gating.ts +190 -0
- package/src/message-queue.ts +352 -0
- package/src/onboarding.ts +274 -0
- package/src/openclaw-plugin-sdk.d.ts +587 -0
- package/src/outbound-deliver.ts +473 -0
- package/src/outbound.ts +1131 -0
- package/src/proactive.ts +530 -0
- package/src/ref-index-store.ts +412 -0
- package/src/reply-dispatcher.ts +334 -0
- package/src/request-context.ts +49 -0
- package/src/runtime.ts +14 -0
- package/src/session-store.ts +303 -0
- package/src/slash-commands.ts +2030 -0
- package/src/startup-greeting.ts +120 -0
- package/src/streaming.ts +1077 -0
- package/src/stt.ts +86 -0
- package/src/tools/channel.ts +281 -0
- package/src/tools/remind.ts +308 -0
- package/src/types.ts +391 -0
- package/src/typing-keepalive.ts +59 -0
- package/src/update-checker.ts +186 -0
- package/src/utils/audio-convert.ts +859 -0
- package/src/utils/chunked-upload.ts +483 -0
- package/src/utils/file-utils.ts +193 -0
- package/src/utils/image-size.ts +266 -0
- package/src/utils/media-send.ts +631 -0
- package/src/utils/media-tags.ts +183 -0
- package/src/utils/payload.ts +265 -0
- package/src/utils/pkg-version.ts +64 -0
- package/src/utils/platform.ts +435 -0
- package/src/utils/ssrf-guard.ts +102 -0
- package/src/utils/text-parsing.ts +85 -0
- package/src/utils/upload-cache.ts +128 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2020 Google LLC
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
const URL = require('url');
|
|
17
|
+
const VM = require('vm');
|
|
18
|
+
const threads = require('worker_threads');
|
|
19
|
+
const WORKER = Symbol.for('worker');
|
|
20
|
+
const EVENTS = Symbol.for('events');
|
|
21
|
+
class EventTarget {
|
|
22
|
+
constructor() {
|
|
23
|
+
Object.defineProperty(this, EVENTS, {
|
|
24
|
+
value: new Map()
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
dispatchEvent(event) {
|
|
28
|
+
event.target = event.currentTarget = this;
|
|
29
|
+
if (this['on' + event.type]) {
|
|
30
|
+
try {
|
|
31
|
+
this['on' + event.type](event);
|
|
32
|
+
} catch (err) {
|
|
33
|
+
console.error(err);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const list = this[EVENTS].get(event.type);
|
|
37
|
+
if (list == null) return;
|
|
38
|
+
list.forEach(handler => {
|
|
39
|
+
try {
|
|
40
|
+
handler.call(this, event);
|
|
41
|
+
} catch (err) {
|
|
42
|
+
console.error(err);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
addEventListener(type, fn) {
|
|
47
|
+
let events = this[EVENTS].get(type);
|
|
48
|
+
if (!events) this[EVENTS].set(type, events = []);
|
|
49
|
+
events.push(fn);
|
|
50
|
+
}
|
|
51
|
+
removeEventListener(type, fn) {
|
|
52
|
+
let events = this[EVENTS].get(type);
|
|
53
|
+
if (events) {
|
|
54
|
+
const index = events.indexOf(fn);
|
|
55
|
+
if (index !== -1) events.splice(index, 1);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function Event(type, target) {
|
|
60
|
+
this.type = type;
|
|
61
|
+
this.timeStamp = Date.now();
|
|
62
|
+
this.target = this.currentTarget = this.data = null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// this module is used self-referentially on both sides of the
|
|
66
|
+
// thread boundary, but behaves differently in each context.
|
|
67
|
+
module.exports = threads.isMainThread ? mainThread() : workerThread();
|
|
68
|
+
const baseUrl = URL.pathToFileURL(process.cwd() + '/');
|
|
69
|
+
function mainThread() {
|
|
70
|
+
/**
|
|
71
|
+
* A web-compatible Worker implementation atop Node's worker_threads.
|
|
72
|
+
* - uses DOM-style events (Event.data, Event.type, etc)
|
|
73
|
+
* - supports event handler properties (worker.onmessage)
|
|
74
|
+
* - Worker() constructor accepts a module URL
|
|
75
|
+
* - accepts the {type:'module'} option
|
|
76
|
+
* - emulates WorkerGlobalScope within the worker
|
|
77
|
+
* @param {string} url The URL or module specifier to load
|
|
78
|
+
* @param {object} [options] Worker construction options
|
|
79
|
+
* @param {string} [options.name] Available as `self.name` within the Worker
|
|
80
|
+
* @param {string} [options.type="classic"] Pass "module" to create a Module Worker.
|
|
81
|
+
*/
|
|
82
|
+
class Worker extends EventTarget {
|
|
83
|
+
constructor(url, options) {
|
|
84
|
+
super();
|
|
85
|
+
const {
|
|
86
|
+
name,
|
|
87
|
+
type
|
|
88
|
+
} = options || {};
|
|
89
|
+
url += '';
|
|
90
|
+
let mod;
|
|
91
|
+
if (/^data:/.test(url)) {
|
|
92
|
+
mod = url;
|
|
93
|
+
} else {
|
|
94
|
+
mod = URL.fileURLToPath(new URL.URL(url, baseUrl));
|
|
95
|
+
}
|
|
96
|
+
const worker = new threads.Worker(__filename, {
|
|
97
|
+
workerData: {
|
|
98
|
+
mod,
|
|
99
|
+
name,
|
|
100
|
+
type
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
Object.defineProperty(this, WORKER, {
|
|
104
|
+
value: worker
|
|
105
|
+
});
|
|
106
|
+
worker.on('message', data => {
|
|
107
|
+
const event = new Event('message');
|
|
108
|
+
event.data = data;
|
|
109
|
+
this.dispatchEvent(event);
|
|
110
|
+
});
|
|
111
|
+
worker.on('error', error => {
|
|
112
|
+
error.type = 'error';
|
|
113
|
+
this.dispatchEvent(error);
|
|
114
|
+
});
|
|
115
|
+
worker.on('exit', () => {
|
|
116
|
+
this.dispatchEvent(new Event('close'));
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
postMessage(data, transferList) {
|
|
120
|
+
this[WORKER].postMessage(data, transferList);
|
|
121
|
+
}
|
|
122
|
+
terminate() {
|
|
123
|
+
this[WORKER].terminate();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
Worker.prototype.onmessage = Worker.prototype.onerror = Worker.prototype.onclose = null;
|
|
127
|
+
return Worker;
|
|
128
|
+
}
|
|
129
|
+
function workerThread() {
|
|
130
|
+
let {
|
|
131
|
+
mod,
|
|
132
|
+
name,
|
|
133
|
+
type
|
|
134
|
+
} = threads.workerData || {};
|
|
135
|
+
if (!mod) return mainThread();
|
|
136
|
+
|
|
137
|
+
// turn global into a mock WorkerGlobalScope
|
|
138
|
+
const self = global.self = global;
|
|
139
|
+
|
|
140
|
+
// enqueue messages to dispatch after modules are loaded
|
|
141
|
+
let q = [];
|
|
142
|
+
function flush() {
|
|
143
|
+
const buffered = q;
|
|
144
|
+
q = null;
|
|
145
|
+
buffered.forEach(event => {
|
|
146
|
+
self.dispatchEvent(event);
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
threads.parentPort.on('message', data => {
|
|
150
|
+
const event = new Event('message');
|
|
151
|
+
event.data = data;
|
|
152
|
+
if (q == null) self.dispatchEvent(event);else q.push(event);
|
|
153
|
+
});
|
|
154
|
+
threads.parentPort.on('error', err => {
|
|
155
|
+
err.type = 'Error';
|
|
156
|
+
self.dispatchEvent(err);
|
|
157
|
+
});
|
|
158
|
+
class WorkerGlobalScope extends EventTarget {
|
|
159
|
+
postMessage(data, transferList) {
|
|
160
|
+
threads.parentPort.postMessage(data, transferList);
|
|
161
|
+
}
|
|
162
|
+
// Emulates https://developer.mozilla.org/en-US/docs/Web/API/DedicatedWorkerGlobalScope/close
|
|
163
|
+
close() {
|
|
164
|
+
process.exit();
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
let proto = Object.getPrototypeOf(global);
|
|
168
|
+
delete proto.constructor;
|
|
169
|
+
Object.defineProperties(WorkerGlobalScope.prototype, proto);
|
|
170
|
+
proto = Object.setPrototypeOf(global, new WorkerGlobalScope());
|
|
171
|
+
['postMessage', 'addEventListener', 'removeEventListener', 'dispatchEvent'].forEach(fn => {
|
|
172
|
+
proto[fn] = proto[fn].bind(global);
|
|
173
|
+
});
|
|
174
|
+
global.name = name;
|
|
175
|
+
const isDataUrl = /^data:/.test(mod);
|
|
176
|
+
if (type === 'module') {
|
|
177
|
+
import(mod).catch(err => {
|
|
178
|
+
if (isDataUrl && err.message === 'Not supported') {
|
|
179
|
+
console.warn('Worker(): Importing data: URLs requires Node 12.10+. Falling back to classic worker.');
|
|
180
|
+
return evaluateDataUrl(mod, name);
|
|
181
|
+
}
|
|
182
|
+
console.error(err);
|
|
183
|
+
}).then(flush);
|
|
184
|
+
} else {
|
|
185
|
+
try {
|
|
186
|
+
if (/^data:/.test(mod)) {
|
|
187
|
+
evaluateDataUrl(mod, name);
|
|
188
|
+
} else {
|
|
189
|
+
require(mod);
|
|
190
|
+
}
|
|
191
|
+
} catch (err) {
|
|
192
|
+
console.error(err);
|
|
193
|
+
}
|
|
194
|
+
Promise.resolve().then(flush);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
function evaluateDataUrl(url, name) {
|
|
198
|
+
const {
|
|
199
|
+
data
|
|
200
|
+
} = parseDataUrl(url);
|
|
201
|
+
return VM.runInThisContext(data, {
|
|
202
|
+
filename: 'worker.<' + (name || 'data:') + '>'
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
function parseDataUrl(url) {
|
|
206
|
+
let [m, type, encoding, data] = url.match(/^data: *([^;,]*)(?: *; *([^,]*))? *,(.*)$/) || [];
|
|
207
|
+
if (!m) throw Error('Invalid Data URL.');
|
|
208
|
+
if (encoding) switch (encoding.toLowerCase()) {
|
|
209
|
+
case 'base64':
|
|
210
|
+
data = Buffer.from(data, 'base64').toString();
|
|
211
|
+
break;
|
|
212
|
+
default:
|
|
213
|
+
throw Error('Unknown Data URL encoding "' + encoding + '"');
|
|
214
|
+
}
|
|
215
|
+
return {
|
|
216
|
+
type,
|
|
217
|
+
data
|
|
218
|
+
};
|
|
219
|
+
}
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2020 Google LLC
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import URL from 'url';
|
|
18
|
+
import VM from 'vm';
|
|
19
|
+
import threads from 'worker_threads';
|
|
20
|
+
|
|
21
|
+
const WORKER = Symbol.for('worker');
|
|
22
|
+
const EVENTS = Symbol.for('events');
|
|
23
|
+
|
|
24
|
+
class EventTarget {
|
|
25
|
+
constructor() {
|
|
26
|
+
Object.defineProperty(this, EVENTS, {
|
|
27
|
+
value: new Map()
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
dispatchEvent(event) {
|
|
31
|
+
event.target = event.currentTarget = this;
|
|
32
|
+
if (this['on'+event.type]) {
|
|
33
|
+
try {
|
|
34
|
+
this['on'+event.type](event);
|
|
35
|
+
}
|
|
36
|
+
catch (err) {
|
|
37
|
+
console.error(err);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const list = this[EVENTS].get(event.type);
|
|
41
|
+
if (list == null) return;
|
|
42
|
+
list.forEach(handler => {
|
|
43
|
+
try {
|
|
44
|
+
handler.call(this, event);
|
|
45
|
+
}
|
|
46
|
+
catch (err) {
|
|
47
|
+
console.error(err);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
addEventListener(type, fn) {
|
|
52
|
+
let events = this[EVENTS].get(type);
|
|
53
|
+
if (!events) this[EVENTS].set(type, events = []);
|
|
54
|
+
events.push(fn);
|
|
55
|
+
}
|
|
56
|
+
removeEventListener(type, fn) {
|
|
57
|
+
let events = this[EVENTS].get(type);
|
|
58
|
+
if (events) {
|
|
59
|
+
const index = events.indexOf(fn);
|
|
60
|
+
if (index !== -1) events.splice(index, 1);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function Event(type, target) {
|
|
66
|
+
this.type = type;
|
|
67
|
+
this.timeStamp = Date.now();
|
|
68
|
+
this.target = this.currentTarget = this.data = null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// this module is used self-referentially on both sides of the
|
|
72
|
+
// thread boundary, but behaves differently in each context.
|
|
73
|
+
export default threads.isMainThread ? mainThread() : workerThread();
|
|
74
|
+
|
|
75
|
+
const baseUrl = URL.pathToFileURL(process.cwd() + '/');
|
|
76
|
+
|
|
77
|
+
function mainThread() {
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* A web-compatible Worker implementation atop Node's worker_threads.
|
|
81
|
+
* - uses DOM-style events (Event.data, Event.type, etc)
|
|
82
|
+
* - supports event handler properties (worker.onmessage)
|
|
83
|
+
* - Worker() constructor accepts a module URL
|
|
84
|
+
* - accepts the {type:'module'} option
|
|
85
|
+
* - emulates WorkerGlobalScope within the worker
|
|
86
|
+
* @param {string} url The URL or module specifier to load
|
|
87
|
+
* @param {object} [options] Worker construction options
|
|
88
|
+
* @param {string} [options.name] Available as `self.name` within the Worker
|
|
89
|
+
* @param {string} [options.type="classic"] Pass "module" to create a Module Worker.
|
|
90
|
+
*/
|
|
91
|
+
class Worker extends EventTarget {
|
|
92
|
+
constructor(url, options) {
|
|
93
|
+
super();
|
|
94
|
+
const { name, type } = options || {};
|
|
95
|
+
url += '';
|
|
96
|
+
let mod;
|
|
97
|
+
if (/^data:/.test(url)) {
|
|
98
|
+
mod = url;
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
mod = URL.fileURLToPath(new URL.URL(url, baseUrl));
|
|
102
|
+
}
|
|
103
|
+
const worker = new threads.Worker(
|
|
104
|
+
__filename,
|
|
105
|
+
{ workerData: { mod, name, type } }
|
|
106
|
+
);
|
|
107
|
+
Object.defineProperty(this, WORKER, {
|
|
108
|
+
value: worker
|
|
109
|
+
});
|
|
110
|
+
worker.on('message', data => {
|
|
111
|
+
const event = new Event('message');
|
|
112
|
+
event.data = data;
|
|
113
|
+
this.dispatchEvent(event);
|
|
114
|
+
});
|
|
115
|
+
worker.on('error', error => {
|
|
116
|
+
error.type = 'error';
|
|
117
|
+
this.dispatchEvent(error);
|
|
118
|
+
});
|
|
119
|
+
worker.on('exit', () => {
|
|
120
|
+
this.dispatchEvent(new Event('close'));
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
postMessage(data, transferList) {
|
|
124
|
+
this[WORKER].postMessage(data, transferList);
|
|
125
|
+
}
|
|
126
|
+
terminate() {
|
|
127
|
+
this[WORKER].terminate();
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
Worker.prototype.onmessage = Worker.prototype.onerror = Worker.prototype.onclose = null;
|
|
131
|
+
return Worker;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function workerThread() {
|
|
135
|
+
let { mod, name, type } = threads.workerData || {};
|
|
136
|
+
if (!mod) return mainThread();
|
|
137
|
+
|
|
138
|
+
// turn global into a mock WorkerGlobalScope
|
|
139
|
+
const self = global.self = global;
|
|
140
|
+
|
|
141
|
+
// enqueue messages to dispatch after modules are loaded
|
|
142
|
+
let q = [];
|
|
143
|
+
function flush() {
|
|
144
|
+
const buffered = q;
|
|
145
|
+
q = null;
|
|
146
|
+
buffered.forEach(event => { self.dispatchEvent(event); });
|
|
147
|
+
}
|
|
148
|
+
threads.parentPort.on('message', data => {
|
|
149
|
+
const event = new Event('message');
|
|
150
|
+
event.data = data;
|
|
151
|
+
if (q == null) self.dispatchEvent(event);
|
|
152
|
+
else q.push(event);
|
|
153
|
+
});
|
|
154
|
+
threads.parentPort.on('error', err => {
|
|
155
|
+
err.type = 'Error';
|
|
156
|
+
self.dispatchEvent(err);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
class WorkerGlobalScope extends EventTarget {
|
|
160
|
+
postMessage(data, transferList) {
|
|
161
|
+
threads.parentPort.postMessage(data, transferList);
|
|
162
|
+
}
|
|
163
|
+
// Emulates https://developer.mozilla.org/en-US/docs/Web/API/DedicatedWorkerGlobalScope/close
|
|
164
|
+
close() {
|
|
165
|
+
process.exit();
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
let proto = Object.getPrototypeOf(global);
|
|
169
|
+
delete proto.constructor;
|
|
170
|
+
Object.defineProperties(WorkerGlobalScope.prototype, proto);
|
|
171
|
+
proto = Object.setPrototypeOf(global, new WorkerGlobalScope());
|
|
172
|
+
['postMessage', 'addEventListener', 'removeEventListener', 'dispatchEvent'].forEach(fn => {
|
|
173
|
+
proto[fn] = proto[fn].bind(global);
|
|
174
|
+
});
|
|
175
|
+
global.name = name;
|
|
176
|
+
|
|
177
|
+
const isDataUrl = /^data:/.test(mod);
|
|
178
|
+
if (type === 'module') {
|
|
179
|
+
import(mod)
|
|
180
|
+
.catch(err => {
|
|
181
|
+
if (isDataUrl && err.message === 'Not supported') {
|
|
182
|
+
console.warn('Worker(): Importing data: URLs requires Node 12.10+. Falling back to classic worker.');
|
|
183
|
+
return evaluateDataUrl(mod, name);
|
|
184
|
+
}
|
|
185
|
+
console.error(err);
|
|
186
|
+
})
|
|
187
|
+
.then(flush);
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
try {
|
|
191
|
+
if (/^data:/.test(mod)) {
|
|
192
|
+
evaluateDataUrl(mod, name);
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
require(mod);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
catch (err) {
|
|
199
|
+
console.error(err);
|
|
200
|
+
}
|
|
201
|
+
Promise.resolve().then(flush);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function evaluateDataUrl(url, name) {
|
|
206
|
+
const { data } = parseDataUrl(url);
|
|
207
|
+
return VM.runInThisContext(data, {
|
|
208
|
+
filename: 'worker.<'+(name || 'data:')+'>'
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function parseDataUrl(url) {
|
|
213
|
+
let [m, type, encoding, data] = url.match(/^data: *([^;,]*)(?: *; *([^,]*))? *,(.*)$/) || [];
|
|
214
|
+
if (!m) throw Error('Invalid Data URL.');
|
|
215
|
+
if (encoding) switch (encoding.toLowerCase()) {
|
|
216
|
+
case 'base64':
|
|
217
|
+
data = Buffer.from(data, 'base64').toString();
|
|
218
|
+
break;
|
|
219
|
+
default:
|
|
220
|
+
throw Error('Unknown Data URL encoding "' + encoding + '"');
|
|
221
|
+
}
|
|
222
|
+
return { type, data };
|
|
223
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@eshaz/web-worker",
|
|
3
|
+
"version": "1.2.2",
|
|
4
|
+
"description": "Consistent Web Workers in browser and Node.",
|
|
5
|
+
"main": "./cjs/node.js",
|
|
6
|
+
"browser": "./cjs/browser.js",
|
|
7
|
+
"types": "index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"cjs",
|
|
10
|
+
"browser.js",
|
|
11
|
+
"node.js",
|
|
12
|
+
"index.d.ts"
|
|
13
|
+
],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"prepare": "babel node.js browser.js -d cjs",
|
|
16
|
+
"test": "eslint '*.js' test && node --experimental-modules ./node_modules/.bin/ava"
|
|
17
|
+
},
|
|
18
|
+
"babel": {
|
|
19
|
+
"plugins": [
|
|
20
|
+
"@babel/plugin-syntax-dynamic-import",
|
|
21
|
+
"babel-plugin-commonjs-lite"
|
|
22
|
+
]
|
|
23
|
+
},
|
|
24
|
+
"repository": "https://github.com/eshaz/web-worker",
|
|
25
|
+
"keywords": [
|
|
26
|
+
"worker",
|
|
27
|
+
"worker_threads",
|
|
28
|
+
"webworker",
|
|
29
|
+
"web worker",
|
|
30
|
+
"web-worker",
|
|
31
|
+
"threads"
|
|
32
|
+
],
|
|
33
|
+
"authors": [],
|
|
34
|
+
"license": "Apache-2.0",
|
|
35
|
+
"homepage": "https://github.com/eshaz/web-worker",
|
|
36
|
+
"eslintConfig": {
|
|
37
|
+
"extends": "developit",
|
|
38
|
+
"rules": {
|
|
39
|
+
"no-console": 0
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@babel/cli": "^7.7.7",
|
|
44
|
+
"@babel/core": "^7.7.7",
|
|
45
|
+
"@babel/plugin-syntax-dynamic-import": "^7.7.4",
|
|
46
|
+
"@babel/plugin-transform-modules-commonjs": "^7.7.5",
|
|
47
|
+
"@babel/preset-env": "^7.7.7",
|
|
48
|
+
"ava": "^2.4.0",
|
|
49
|
+
"babel-plugin-commonjs-lite": "gist:3e38560d5bed35da1fd708d635e4acb3",
|
|
50
|
+
"eslint": "^6.8.0",
|
|
51
|
+
"eslint-config-developit": "^1.1.1",
|
|
52
|
+
"eslint-plugin-compat": "^4.1.4"
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wasm-audio-decoders/common",
|
|
3
|
+
"version": "9.0.7",
|
|
4
|
+
"description": "Web Assembly Audio Decoders Common",
|
|
5
|
+
"module": "index.js",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
"types": "./types.d.ts",
|
|
9
|
+
"default": "./index.js"
|
|
10
|
+
},
|
|
11
|
+
"types": "types.d.ts",
|
|
12
|
+
"sideEffects": false,
|
|
13
|
+
"files": [
|
|
14
|
+
"index.js",
|
|
15
|
+
"types.d.ts",
|
|
16
|
+
"src/*"
|
|
17
|
+
],
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/eshaz/wasm-audio-decoders.git"
|
|
21
|
+
},
|
|
22
|
+
"type": "module",
|
|
23
|
+
"author": {
|
|
24
|
+
"name": "Ethan Halsall",
|
|
25
|
+
"email": "ethanhalsall@pm.me"
|
|
26
|
+
},
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"bugs": {
|
|
29
|
+
"url": "https://github.com/eshaz/wasm-audio-decoders/issues"
|
|
30
|
+
},
|
|
31
|
+
"homepage": "https://github.com/eshaz/wasm-audio-decoders/tree/master/src/common",
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@eshaz/web-worker": "1.2.2",
|
|
34
|
+
"simple-yenc": "^1.0.4"
|
|
35
|
+
}
|
|
36
|
+
}
|