dreaction-client-core 1.2.2 → 1.4.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/lib/core.d.ts +30 -0
- package/lib/core.d.ts.map +1 -0
- package/lib/core.js +317 -0
- package/lib/index.d.ts +16 -195
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +22 -461
- package/lib/plugins/api-response.d.ts +15 -4
- package/lib/plugins/api-response.d.ts.map +1 -1
- package/lib/plugins/api-response.js +16 -4
- package/lib/plugins/benchmark.d.ts +1 -1
- package/lib/plugins/benchmark.d.ts.map +1 -1
- package/lib/plugins/clear.d.ts +1 -1
- package/lib/plugins/clear.d.ts.map +1 -1
- package/lib/plugins/image.d.ts +1 -1
- package/lib/plugins/image.d.ts.map +1 -1
- package/lib/plugins/index.d.ts +52 -0
- package/lib/plugins/index.d.ts.map +1 -0
- package/lib/plugins/index.js +36 -0
- package/lib/plugins/issue.d.ts +19 -0
- package/lib/plugins/issue.d.ts.map +1 -0
- package/lib/plugins/issue.js +20 -0
- package/lib/plugins/logger.d.ts +20 -5
- package/lib/plugins/logger.d.ts.map +1 -1
- package/lib/plugins/logger.js +8 -26
- package/lib/plugins/repl.d.ts +1 -1
- package/lib/plugins/repl.d.ts.map +1 -1
- package/lib/plugins/state-responses.d.ts +17 -5
- package/lib/plugins/state-responses.d.ts.map +1 -1
- package/lib/plugins/state-responses.js +10 -19
- package/lib/types.d.ts +73 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +3 -0
- package/lib/utils/plugin-guard.d.ts +12 -0
- package/lib/utils/plugin-guard.d.ts.map +1 -0
- package/lib/utils/plugin-guard.js +19 -0
- package/package.json +2 -2
- package/src/core.ts +428 -0
- package/src/index.ts +38 -720
- package/src/plugins/api-response.ts +39 -6
- package/src/plugins/benchmark.ts +1 -1
- package/src/plugins/clear.ts +1 -1
- package/src/plugins/image.ts +1 -1
- package/src/plugins/index.ts +26 -0
- package/src/plugins/issue.ts +25 -0
- package/src/plugins/logger.ts +17 -39
- package/src/plugins/state-responses.ts +19 -29
- package/src/types.ts +127 -0
- package/src/utils/plugin-guard.ts +31 -0
- package/src/plugins/repl.ts +0 -63
package/lib/index.js
CHANGED
|
@@ -1,465 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
Object.defineProperty(exports, "
|
|
20
|
-
var
|
|
21
|
-
Object.defineProperty(exports, "
|
|
22
|
-
Object.defineProperty(exports, "
|
|
3
|
+
exports.createPluginGuard = exports.runFPSMeter = exports.generateRequestId = exports.hasIssuePlugin = exports.assertHasIssuePlugin = exports.hasStateResponsePlugin = exports.assertHasStateResponsePlugin = exports.hasLoggerPlugin = exports.assertHasLoggerPlugin = exports.corePlugins = exports.createClient = exports.DReactionImpl = void 0;
|
|
4
|
+
// Core
|
|
5
|
+
var core_1 = require("./core");
|
|
6
|
+
Object.defineProperty(exports, "DReactionImpl", { enumerable: true, get: function () { return core_1.DReactionImpl; } });
|
|
7
|
+
Object.defineProperty(exports, "createClient", { enumerable: true, get: function () { return core_1.createClient; } });
|
|
8
|
+
// Plugins
|
|
9
|
+
var plugins_1 = require("./plugins");
|
|
10
|
+
Object.defineProperty(exports, "corePlugins", { enumerable: true, get: function () { return plugins_1.corePlugins; } });
|
|
11
|
+
var logger_1 = require("./plugins/logger");
|
|
12
|
+
Object.defineProperty(exports, "assertHasLoggerPlugin", { enumerable: true, get: function () { return logger_1.assertHasLoggerPlugin; } });
|
|
13
|
+
Object.defineProperty(exports, "hasLoggerPlugin", { enumerable: true, get: function () { return logger_1.hasLoggerPlugin; } });
|
|
14
|
+
var state_responses_1 = require("./plugins/state-responses");
|
|
15
|
+
Object.defineProperty(exports, "assertHasStateResponsePlugin", { enumerable: true, get: function () { return state_responses_1.assertHasStateResponsePlugin; } });
|
|
16
|
+
Object.defineProperty(exports, "hasStateResponsePlugin", { enumerable: true, get: function () { return state_responses_1.hasStateResponsePlugin; } });
|
|
17
|
+
var issue_1 = require("./plugins/issue");
|
|
18
|
+
Object.defineProperty(exports, "assertHasIssuePlugin", { enumerable: true, get: function () { return issue_1.assertHasIssuePlugin; } });
|
|
19
|
+
Object.defineProperty(exports, "hasIssuePlugin", { enumerable: true, get: function () { return issue_1.hasIssuePlugin; } });
|
|
20
|
+
var api_response_1 = require("./plugins/api-response");
|
|
21
|
+
Object.defineProperty(exports, "generateRequestId", { enumerable: true, get: function () { return api_response_1.generateRequestId; } });
|
|
22
|
+
// Utils
|
|
23
23
|
var fps_1 = require("./utils/fps");
|
|
24
24
|
Object.defineProperty(exports, "runFPSMeter", { enumerable: true, get: function () { return fps_1.runFPSMeter; } });
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
(0, logger_1.default)(),
|
|
28
|
-
(0, benchmark_1.default)(),
|
|
29
|
-
(0, state_responses_1.default)(),
|
|
30
|
-
(0, api_response_1.default)(),
|
|
31
|
-
(0, clear_1.default)(),
|
|
32
|
-
(0, repl_1.default)(),
|
|
33
|
-
];
|
|
34
|
-
// these are not for you.
|
|
35
|
-
const reservedFeatures = [
|
|
36
|
-
'configure',
|
|
37
|
-
'connect',
|
|
38
|
-
'connected',
|
|
39
|
-
'options',
|
|
40
|
-
'plugins',
|
|
41
|
-
'send',
|
|
42
|
-
'socket',
|
|
43
|
-
'startTimer',
|
|
44
|
-
'use',
|
|
45
|
-
];
|
|
46
|
-
const isReservedFeature = (value) => reservedFeatures.some((res) => res === value);
|
|
47
|
-
function emptyPromise() {
|
|
48
|
-
return Promise.resolve('');
|
|
49
|
-
}
|
|
50
|
-
class DReactionImpl {
|
|
51
|
-
// the configuration options
|
|
52
|
-
options;
|
|
53
|
-
/**
|
|
54
|
-
* Are we connected to a server?
|
|
55
|
-
*/
|
|
56
|
-
connected = false;
|
|
57
|
-
/**
|
|
58
|
-
* The socket we're using.
|
|
59
|
-
*/
|
|
60
|
-
socket = null;
|
|
61
|
-
/**
|
|
62
|
-
* Available plugins.
|
|
63
|
-
*/
|
|
64
|
-
plugins = [];
|
|
65
|
-
/**
|
|
66
|
-
* Messages that need to be sent.
|
|
67
|
-
*/
|
|
68
|
-
sendQueue = [];
|
|
69
|
-
/**
|
|
70
|
-
* Are we ready to start communicating?
|
|
71
|
-
*/
|
|
72
|
-
isReady = false;
|
|
73
|
-
/**
|
|
74
|
-
* The last time we sent a message.
|
|
75
|
-
*/
|
|
76
|
-
lastMessageDate = new Date();
|
|
77
|
-
/**
|
|
78
|
-
* The registered custom commands
|
|
79
|
-
*/
|
|
80
|
-
customCommands = [];
|
|
81
|
-
/**
|
|
82
|
-
* The current ID for custom commands
|
|
83
|
-
*/
|
|
84
|
-
customCommandLatestId = 1;
|
|
85
|
-
/**
|
|
86
|
-
* Promise resolvers for connection wait
|
|
87
|
-
*/
|
|
88
|
-
connectPromiseResolve = null;
|
|
89
|
-
connectPromiseReject = null;
|
|
90
|
-
connectPromise = null;
|
|
91
|
-
/**
|
|
92
|
-
* Starts a timer and returns a function you can call to stop it and return the elapsed time.
|
|
93
|
-
*/
|
|
94
|
-
startTimer = () => (0, stopwatch_1.start)();
|
|
95
|
-
/**
|
|
96
|
-
* Set the configuration options.
|
|
97
|
-
*/
|
|
98
|
-
configure(options = {}) {
|
|
99
|
-
// options get merged & validated before getting set
|
|
100
|
-
const newOptions = {
|
|
101
|
-
createSocket: null,
|
|
102
|
-
host: 'localhost',
|
|
103
|
-
port: 9600,
|
|
104
|
-
name: 'dreaction-core-client',
|
|
105
|
-
secure: false,
|
|
106
|
-
plugins: exports.corePlugins,
|
|
107
|
-
safeRecursion: true,
|
|
108
|
-
onCommand: () => null,
|
|
109
|
-
onConnect: () => null,
|
|
110
|
-
onDisconnect: () => null,
|
|
111
|
-
...this.options,
|
|
112
|
-
...options,
|
|
113
|
-
};
|
|
114
|
-
(0, validate_1.default)(newOptions);
|
|
115
|
-
this.options = newOptions;
|
|
116
|
-
// if we have plugins, let's add them here
|
|
117
|
-
if (Array.isArray(this.options.plugins)) {
|
|
118
|
-
this.options.plugins.forEach((p) => this.use(p));
|
|
119
|
-
}
|
|
120
|
-
return this;
|
|
121
|
-
}
|
|
122
|
-
close() {
|
|
123
|
-
this.connected = false;
|
|
124
|
-
this.socket && this.socket.close && this.socket.close();
|
|
125
|
-
// Reject any pending connection promise
|
|
126
|
-
if (this.connectPromiseReject) {
|
|
127
|
-
this.connectPromiseReject(new Error('Connection closed'));
|
|
128
|
-
this.connectPromiseResolve = null;
|
|
129
|
-
this.connectPromiseReject = null;
|
|
130
|
-
}
|
|
131
|
-
return this;
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* Connect to the DReaction server.
|
|
135
|
-
*/
|
|
136
|
-
connect() {
|
|
137
|
-
this.connected = true;
|
|
138
|
-
// Create a new promise for this connection attempt
|
|
139
|
-
this.connectPromise = new Promise((resolve, reject) => {
|
|
140
|
-
this.connectPromiseResolve = resolve;
|
|
141
|
-
this.connectPromiseReject = reject;
|
|
142
|
-
});
|
|
143
|
-
const { createSocket, secure, host, environment, port, name, client = {}, info = {}, getClientId, } = this.options;
|
|
144
|
-
const { onCommand, onConnect, onDisconnect } = this.options;
|
|
145
|
-
if (!host) {
|
|
146
|
-
console.log('host is not config, skip connect.');
|
|
147
|
-
if (this.connectPromiseReject) {
|
|
148
|
-
this.connectPromiseReject(new Error('Host is not configured'));
|
|
149
|
-
this.connectPromiseResolve = null;
|
|
150
|
-
this.connectPromiseReject = null;
|
|
151
|
-
}
|
|
152
|
-
return this;
|
|
153
|
-
}
|
|
154
|
-
// establish a connection to the server
|
|
155
|
-
const protocol = secure ? 'wss' : 'ws';
|
|
156
|
-
const socket = createSocket(`${protocol}://${host}:${port}`);
|
|
157
|
-
// fires when we talk to the server
|
|
158
|
-
const onOpen = () => {
|
|
159
|
-
// fire our optional onConnect handler
|
|
160
|
-
onConnect && onConnect();
|
|
161
|
-
// trigger our plugins onConnect
|
|
162
|
-
this.plugins.forEach((p) => p.onConnect && p.onConnect());
|
|
163
|
-
// Resolve the connection promise
|
|
164
|
-
if (this.connectPromiseResolve) {
|
|
165
|
-
this.connectPromiseResolve();
|
|
166
|
-
this.connectPromiseResolve = null;
|
|
167
|
-
this.connectPromiseReject = null;
|
|
168
|
-
}
|
|
169
|
-
const getClientIdPromise = getClientId || emptyPromise;
|
|
170
|
-
getClientIdPromise(name).then((clientId) => {
|
|
171
|
-
this.isReady = true;
|
|
172
|
-
// introduce ourselves
|
|
173
|
-
this.send('client.intro', {
|
|
174
|
-
environment,
|
|
175
|
-
...client,
|
|
176
|
-
...info,
|
|
177
|
-
name,
|
|
178
|
-
clientId,
|
|
179
|
-
dreactionCoreClientVersion: 'DREACTION_CORE_CLIENT_VERSION',
|
|
180
|
-
});
|
|
181
|
-
// flush the send queue
|
|
182
|
-
while (this.sendQueue.length > 0) {
|
|
183
|
-
const h = this.sendQueue[0];
|
|
184
|
-
this.sendQueue = this.sendQueue.slice(1);
|
|
185
|
-
this.socket.send(h);
|
|
186
|
-
}
|
|
187
|
-
});
|
|
188
|
-
};
|
|
189
|
-
// fires when we disconnect
|
|
190
|
-
const onClose = () => {
|
|
191
|
-
this.isReady = false;
|
|
192
|
-
// Reject connection promise if still pending
|
|
193
|
-
if (this.connectPromiseReject) {
|
|
194
|
-
this.connectPromiseReject(new Error('Connection failed or closed'));
|
|
195
|
-
this.connectPromiseResolve = null;
|
|
196
|
-
this.connectPromiseReject = null;
|
|
197
|
-
}
|
|
198
|
-
// trigger our disconnect handler
|
|
199
|
-
onDisconnect && onDisconnect();
|
|
200
|
-
// as well as the plugin's onDisconnect
|
|
201
|
-
this.plugins.forEach((p) => p.onDisconnect && p.onDisconnect());
|
|
202
|
-
};
|
|
203
|
-
const decodeCommandData = (data) => {
|
|
204
|
-
if (typeof data === 'string') {
|
|
205
|
-
return JSON.parse(data);
|
|
206
|
-
}
|
|
207
|
-
if (Buffer.isBuffer(data)) {
|
|
208
|
-
return JSON.parse(data.toString());
|
|
209
|
-
}
|
|
210
|
-
return data;
|
|
211
|
-
};
|
|
212
|
-
// fires when we receive a command, just forward it off
|
|
213
|
-
const onMessage = (data) => {
|
|
214
|
-
const command = decodeCommandData(data);
|
|
215
|
-
// trigger our own command handler
|
|
216
|
-
onCommand && onCommand(command);
|
|
217
|
-
// trigger our plugins onCommand
|
|
218
|
-
this.plugins.forEach((p) => p.onCommand && p.onCommand(command));
|
|
219
|
-
// trigger our registered custom commands
|
|
220
|
-
if (command.type === 'custom') {
|
|
221
|
-
this.customCommands
|
|
222
|
-
.filter((cc) => {
|
|
223
|
-
if (typeof command.payload === 'string') {
|
|
224
|
-
return cc.command === command.payload;
|
|
225
|
-
}
|
|
226
|
-
return cc.command === command.payload.command;
|
|
227
|
-
})
|
|
228
|
-
.forEach(async (cc) => {
|
|
229
|
-
const res = await cc.handler(typeof command.payload === 'object' ? command.payload.args : {});
|
|
230
|
-
if (res) {
|
|
231
|
-
// has return value
|
|
232
|
-
this.send('customCommand.response', {
|
|
233
|
-
command: cc.command,
|
|
234
|
-
payload: res,
|
|
235
|
-
});
|
|
236
|
-
}
|
|
237
|
-
});
|
|
238
|
-
}
|
|
239
|
-
else if (command.type === 'setClientId') {
|
|
240
|
-
this.options.setClientId && this.options.setClientId(command.payload);
|
|
241
|
-
}
|
|
242
|
-
};
|
|
243
|
-
// this is ws style from require('ws') on node js
|
|
244
|
-
if ('on' in socket && socket.on) {
|
|
245
|
-
const nodeWebSocket = socket;
|
|
246
|
-
nodeWebSocket.on('open', onOpen);
|
|
247
|
-
nodeWebSocket.on('close', onClose);
|
|
248
|
-
nodeWebSocket.on('message', onMessage);
|
|
249
|
-
// assign the socket to the instance
|
|
250
|
-
this.socket = socket;
|
|
251
|
-
}
|
|
252
|
-
else {
|
|
253
|
-
// this is a browser
|
|
254
|
-
const browserWebSocket = socket;
|
|
255
|
-
socket.onopen = onOpen;
|
|
256
|
-
socket.onclose = onClose;
|
|
257
|
-
socket.onmessage = (evt) => onMessage(evt.data);
|
|
258
|
-
// assign the socket to the instance
|
|
259
|
-
this.socket = browserWebSocket;
|
|
260
|
-
}
|
|
261
|
-
return this;
|
|
262
|
-
}
|
|
263
|
-
/**
|
|
264
|
-
* Sends a command to the server
|
|
265
|
-
*/
|
|
266
|
-
send = (type, payload, important) => {
|
|
267
|
-
// set the timing info
|
|
268
|
-
const date = new Date();
|
|
269
|
-
let deltaTime = date.getTime() - this.lastMessageDate.getTime();
|
|
270
|
-
// glitches in the matrix
|
|
271
|
-
if (deltaTime < 0) {
|
|
272
|
-
deltaTime = 0;
|
|
273
|
-
}
|
|
274
|
-
this.lastMessageDate = date;
|
|
275
|
-
const fullMessage = {
|
|
276
|
-
type,
|
|
277
|
-
payload,
|
|
278
|
-
important: !!important,
|
|
279
|
-
date: date.toISOString(),
|
|
280
|
-
deltaTime,
|
|
281
|
-
};
|
|
282
|
-
const serializedMessage = (0, serialize_1.default)(fullMessage, this.options.proxyHack);
|
|
283
|
-
if (this.isReady) {
|
|
284
|
-
// send this command
|
|
285
|
-
try {
|
|
286
|
-
this.socket.send(serializedMessage);
|
|
287
|
-
}
|
|
288
|
-
catch {
|
|
289
|
-
this.isReady = false;
|
|
290
|
-
console.log('An error occurred communicating with dreaction. Please reload your app');
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
else {
|
|
294
|
-
// queue it up until we can connect
|
|
295
|
-
this.sendQueue.push(serializedMessage);
|
|
296
|
-
}
|
|
297
|
-
};
|
|
298
|
-
/**
|
|
299
|
-
* Sends a custom command to the server to displays nicely.
|
|
300
|
-
*/
|
|
301
|
-
display(config) {
|
|
302
|
-
const { name, value, preview, image: img, important = false } = config;
|
|
303
|
-
const payload = {
|
|
304
|
-
name,
|
|
305
|
-
value: value || null,
|
|
306
|
-
preview: preview || null,
|
|
307
|
-
image: img || null,
|
|
308
|
-
};
|
|
309
|
-
this.send('display', payload, important);
|
|
310
|
-
}
|
|
311
|
-
/**
|
|
312
|
-
* Client libraries can hijack this to report errors.
|
|
313
|
-
*/
|
|
314
|
-
reportError(error) {
|
|
315
|
-
this.error(error);
|
|
316
|
-
}
|
|
317
|
-
/**
|
|
318
|
-
* Adds a plugin to the system
|
|
319
|
-
*/
|
|
320
|
-
use(pluginCreator) {
|
|
321
|
-
// we're supposed to be given a function
|
|
322
|
-
if (typeof pluginCreator !== 'function') {
|
|
323
|
-
throw new Error('plugins must be a function');
|
|
324
|
-
}
|
|
325
|
-
// execute it immediately passing the send function
|
|
326
|
-
const plugin = pluginCreator.bind(this)(this);
|
|
327
|
-
// ensure we get an Object-like creature back
|
|
328
|
-
if (typeof plugin !== 'object') {
|
|
329
|
-
throw new Error('plugins must return an object');
|
|
330
|
-
}
|
|
331
|
-
// do we have features to mixin?
|
|
332
|
-
if (plugin.features) {
|
|
333
|
-
// validate
|
|
334
|
-
if (typeof plugin.features !== 'object') {
|
|
335
|
-
throw new Error('features must be an object');
|
|
336
|
-
}
|
|
337
|
-
// here's how we're going to inject these in
|
|
338
|
-
const inject = (key) => {
|
|
339
|
-
// grab the function
|
|
340
|
-
const featureFunction = plugin.features[key];
|
|
341
|
-
// only functions may pass
|
|
342
|
-
if (typeof featureFunction !== 'function') {
|
|
343
|
-
throw new Error(`feature ${key} is not a function`);
|
|
344
|
-
}
|
|
345
|
-
// ditch reserved names
|
|
346
|
-
if (isReservedFeature(key)) {
|
|
347
|
-
throw new Error(`feature ${key} is a reserved name`);
|
|
348
|
-
}
|
|
349
|
-
// ok, let's glue it up... and lose all respect from elite JS champions.
|
|
350
|
-
this[key] = featureFunction;
|
|
351
|
-
};
|
|
352
|
-
// let's inject
|
|
353
|
-
Object.keys(plugin.features).forEach((key) => inject(key));
|
|
354
|
-
}
|
|
355
|
-
// add it to the list
|
|
356
|
-
this.plugins.push(plugin);
|
|
357
|
-
// call the plugins onPlugin
|
|
358
|
-
plugin.onPlugin &&
|
|
359
|
-
typeof plugin.onPlugin === 'function' &&
|
|
360
|
-
plugin.onPlugin.bind(this)(this);
|
|
361
|
-
// chain-friendly
|
|
362
|
-
return this;
|
|
363
|
-
}
|
|
364
|
-
registerCustomCommand(config, optHandler) {
|
|
365
|
-
let command;
|
|
366
|
-
let handler;
|
|
367
|
-
let title;
|
|
368
|
-
let description;
|
|
369
|
-
let args;
|
|
370
|
-
if (typeof config === 'string') {
|
|
371
|
-
command = config;
|
|
372
|
-
handler = optHandler;
|
|
373
|
-
}
|
|
374
|
-
else {
|
|
375
|
-
command = config.command;
|
|
376
|
-
handler = config.handler;
|
|
377
|
-
title = config.title;
|
|
378
|
-
description = config.description;
|
|
379
|
-
args = config.args;
|
|
380
|
-
}
|
|
381
|
-
// Validations
|
|
382
|
-
// Make sure there is a command
|
|
383
|
-
if (!command) {
|
|
384
|
-
throw new Error('A command is required');
|
|
385
|
-
}
|
|
386
|
-
// Make sure there is a handler
|
|
387
|
-
if (!handler) {
|
|
388
|
-
throw new Error(`A handler is required for command "${command}"`);
|
|
389
|
-
}
|
|
390
|
-
// Make sure the command doesn't already exist
|
|
391
|
-
const existingCommands = this.customCommands.filter((cc) => cc.command === command);
|
|
392
|
-
if (existingCommands.length > 0) {
|
|
393
|
-
existingCommands.forEach((command) => {
|
|
394
|
-
this.customCommands = this.customCommands.filter((cc) => cc.id !== command.id);
|
|
395
|
-
this.send('customCommand.unregister', {
|
|
396
|
-
id: command.id,
|
|
397
|
-
command: command.command,
|
|
398
|
-
});
|
|
399
|
-
});
|
|
400
|
-
}
|
|
401
|
-
if (args) {
|
|
402
|
-
const argNames = [];
|
|
403
|
-
args.forEach((arg) => {
|
|
404
|
-
if (!arg.name) {
|
|
405
|
-
throw new Error(`A arg on the command "${command}" is missing a name`);
|
|
406
|
-
}
|
|
407
|
-
if (argNames.indexOf(arg.name) > -1) {
|
|
408
|
-
throw new Error(`A arg with the name "${arg.name}" already exists in the command "${command}"`);
|
|
409
|
-
}
|
|
410
|
-
argNames.push(arg.name);
|
|
411
|
-
});
|
|
412
|
-
}
|
|
413
|
-
// Create this command handlers object
|
|
414
|
-
const customHandler = {
|
|
415
|
-
id: this.customCommandLatestId,
|
|
416
|
-
command,
|
|
417
|
-
handler,
|
|
418
|
-
title,
|
|
419
|
-
description,
|
|
420
|
-
args,
|
|
421
|
-
responseViewType: config.responseViewType,
|
|
422
|
-
};
|
|
423
|
-
// Increment our id counter
|
|
424
|
-
this.customCommandLatestId += 1;
|
|
425
|
-
// Add it to our array
|
|
426
|
-
this.customCommands.push(customHandler);
|
|
427
|
-
this.send('customCommand.register', {
|
|
428
|
-
id: customHandler.id,
|
|
429
|
-
command: customHandler.command,
|
|
430
|
-
title: customHandler.title,
|
|
431
|
-
description: customHandler.description,
|
|
432
|
-
args: customHandler.args,
|
|
433
|
-
responseViewType: customHandler.responseViewType,
|
|
434
|
-
});
|
|
435
|
-
return () => {
|
|
436
|
-
this.customCommands = this.customCommands.filter((cc) => cc.id !== customHandler.id);
|
|
437
|
-
this.send('customCommand.unregister', {
|
|
438
|
-
id: customHandler.id,
|
|
439
|
-
command: customHandler.command,
|
|
440
|
-
});
|
|
441
|
-
};
|
|
442
|
-
}
|
|
443
|
-
/**
|
|
444
|
-
* Wait for connection to be established.
|
|
445
|
-
* Returns a promise that resolves when the connection is ready.
|
|
446
|
-
*/
|
|
447
|
-
waitForConnect() {
|
|
448
|
-
if (this.isReady) {
|
|
449
|
-
// Already connected, resolve immediately
|
|
450
|
-
return Promise.resolve();
|
|
451
|
-
}
|
|
452
|
-
if (this.connectPromise) {
|
|
453
|
-
// Return existing promise
|
|
454
|
-
return this.connectPromise;
|
|
455
|
-
}
|
|
456
|
-
// No connection attempt in progress
|
|
457
|
-
return Promise.reject(new Error('Not connected. Call connect() first.'));
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
exports.DReactionImpl = DReactionImpl;
|
|
461
|
-
// convenience factory function
|
|
462
|
-
function createClient(options) {
|
|
463
|
-
const client = new DReactionImpl();
|
|
464
|
-
return client.configure(options);
|
|
465
|
-
}
|
|
25
|
+
var plugin_guard_1 = require("./utils/plugin-guard");
|
|
26
|
+
Object.defineProperty(exports, "createPluginGuard", { enumerable: true, get: function () { return plugin_guard_1.createPluginGuard; } });
|
|
@@ -1,12 +1,23 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { NetworkRequest, NetworkResponse } from 'dreaction-protocol';
|
|
2
|
+
import type { DReactionCore, Plugin } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* Generates a unique request ID for pairing api.request and api.response events.
|
|
5
|
+
*/
|
|
6
|
+
export declare function generateRequestId(): string;
|
|
7
|
+
export interface ApiResponseFeatures {
|
|
8
|
+
apiRequest: (requestId: string, request: NetworkRequest) => void;
|
|
9
|
+
apiResponse: (requestId: string, request: NetworkRequest, response: NetworkResponse, duration: number) => void;
|
|
10
|
+
}
|
|
11
|
+
export type ApiResponsePlugin = Plugin<DReactionCore> & {
|
|
12
|
+
features: ApiResponseFeatures;
|
|
13
|
+
};
|
|
2
14
|
/**
|
|
3
15
|
* Sends API request/response information.
|
|
4
16
|
*/
|
|
5
17
|
declare const apiResponse: () => (dreaction: DReactionCore) => {
|
|
6
18
|
features: {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}, response: any, duration: number) => void;
|
|
19
|
+
apiRequest: (requestId: string, request: NetworkRequest) => void;
|
|
20
|
+
apiResponse: (requestId: string, request: NetworkRequest, response: NetworkResponse, duration: number) => void;
|
|
10
21
|
};
|
|
11
22
|
};
|
|
12
23
|
export default apiResponse;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api-response.d.ts","sourceRoot":"","sources":["../../src/plugins/api-response.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,
|
|
1
|
+
{"version":3,"file":"api-response.d.ts","sourceRoot":"","sources":["../../src/plugins/api-response.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EACd,eAAe,EAChB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAItD;;GAEG;AACH,wBAAgB,iBAAiB,IAAI,MAAM,CAK1C;AAED,MAAM,WAAW,mBAAmB;IAClC,UAAU,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,KAAK,IAAI,CAAC;IACjE,WAAW,EAAE,CACX,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,cAAc,EACvB,QAAQ,EAAE,eAAe,EACzB,QAAQ,EAAE,MAAM,KACb,IAAI,CAAC;CACX;AAED,MAAM,MAAM,iBAAiB,GAAG,MAAM,CAAC,aAAa,CAAC,GAAG;IACtD,QAAQ,EAAE,mBAAmB,CAAC;CAC/B,CAAC;AAEF;;GAEG;AACH,QAAA,MAAM,WAAW,oBAAqB,aAAa;;gCAGrB,MAAM,WAAW,cAAc;iCAI1C,MAAM,WACR,cAAc,YACb,eAAe,YACf,MAAM;;CAiBvB,CAAC;AAEF,eAAe,WAAW,CAAC"}
|
|
@@ -1,21 +1,33 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateRequestId = generateRequestId;
|
|
4
|
+
let requestCounter = 0;
|
|
5
|
+
/**
|
|
6
|
+
* Generates a unique request ID for pairing api.request and api.response events.
|
|
7
|
+
*/
|
|
8
|
+
function generateRequestId() {
|
|
9
|
+
requestCounter++;
|
|
10
|
+
const timestamp = Date.now();
|
|
11
|
+
const random = Math.random().toString(36).substring(2, 9);
|
|
12
|
+
return `${timestamp}-${requestCounter}-${random}`;
|
|
13
|
+
}
|
|
3
14
|
/**
|
|
4
15
|
* Sends API request/response information.
|
|
5
16
|
*/
|
|
6
17
|
const apiResponse = () => (dreaction) => {
|
|
7
18
|
return {
|
|
8
19
|
features: {
|
|
9
|
-
|
|
20
|
+
apiRequest: (requestId, request) => {
|
|
21
|
+
dreaction.send('api.request', { requestId, request });
|
|
22
|
+
},
|
|
23
|
+
apiResponse: (requestId, request, response, duration) => {
|
|
10
24
|
const ok = response &&
|
|
11
25
|
response.status &&
|
|
12
26
|
typeof response.status === 'number' &&
|
|
13
27
|
response.status >= 200 &&
|
|
14
28
|
response.status <= 299;
|
|
15
29
|
const important = !ok;
|
|
16
|
-
dreaction.send('api.response',
|
|
17
|
-
// @ts-ignore
|
|
18
|
-
{ request, response, duration }, important);
|
|
30
|
+
dreaction.send('api.response', { requestId, request, response, duration }, important);
|
|
19
31
|
},
|
|
20
32
|
},
|
|
21
33
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"benchmark.d.ts","sourceRoot":"","sources":["../../src/plugins/benchmark.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAU,MAAM,
|
|
1
|
+
{"version":3,"file":"benchmark.d.ts","sourceRoot":"","sources":["../../src/plugins/benchmark.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAU,MAAM,UAAU,CAAC;AAEtD;;GAEG;AACH,QAAA,MAAM,SAAS,oBAAqB,aAAa;;2BAGrB,MAAM;8BAGL,MAAM;8BAWN,MAAM;8BAAN,MAAM;;;CAUlC,CAAC;AAEF,eAAe,SAAS,CAAC"}
|
package/lib/plugins/clear.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"clear.d.ts","sourceRoot":"","sources":["../../src/plugins/clear.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAU,MAAM,
|
|
1
|
+
{"version":3,"file":"clear.d.ts","sourceRoot":"","sources":["../../src/plugins/clear.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAU,MAAM,UAAU,CAAC;AAEtD;;GAEG;AACH,QAAA,MAAM,KAAK,oBAAqB,aAAa;;;;CAM5C,CAAC;AAEF,eAAe,KAAK,CAAC"}
|
package/lib/plugins/image.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"image.d.ts","sourceRoot":"","sources":["../../src/plugins/image.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAU,MAAM,
|
|
1
|
+
{"version":3,"file":"image.d.ts","sourceRoot":"","sources":["../../src/plugins/image.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAU,MAAM,UAAU,CAAC;AAEtD,MAAM,WAAW,YAAY;IAC3B,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,QAAA,MAAM,KAAK,oBAAqB,aAAa;;yBAItB,YAAY;;CAalC,CAAC;AAEF,eAAe,KAAK,CAAC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { DReactionCore } from '../types';
|
|
2
|
+
export { default as logger } from './logger';
|
|
3
|
+
export { default as image } from './image';
|
|
4
|
+
export { default as benchmark } from './benchmark';
|
|
5
|
+
export { default as stateResponses } from './state-responses';
|
|
6
|
+
export { default as apiResponse } from './api-response';
|
|
7
|
+
export { default as clear } from './clear';
|
|
8
|
+
export { default as issue } from './issue';
|
|
9
|
+
export declare const corePlugins: (((dreaction: DReactionCore) => {
|
|
10
|
+
features: {
|
|
11
|
+
log: (...args: any[]) => void;
|
|
12
|
+
info: (...args: any[]) => void;
|
|
13
|
+
logImportant: (...args: any[]) => void;
|
|
14
|
+
debug: (message: any, important?: any) => void;
|
|
15
|
+
warn: (message: any) => void;
|
|
16
|
+
error: (message: any, stack?: any) => void;
|
|
17
|
+
};
|
|
18
|
+
}) | ((dreaction: DReactionCore) => {
|
|
19
|
+
features: {
|
|
20
|
+
image: (payload: import("./image").ImagePayload) => void;
|
|
21
|
+
};
|
|
22
|
+
}) | ((dreaction: DReactionCore) => {
|
|
23
|
+
features: {
|
|
24
|
+
benchmark: (title: string) => {
|
|
25
|
+
step: (stepTitle: string) => void;
|
|
26
|
+
stop: (stopTitle: string) => void;
|
|
27
|
+
last: (stopTitle: string) => void;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
}) | ((dreaction: DReactionCore) => {
|
|
31
|
+
features: {
|
|
32
|
+
stateActionComplete: (name: import("dreaction-protocol").StateActionCompletePayload["name"], action: import("dreaction-protocol").StateActionCompletePayload["action"], important?: any) => void;
|
|
33
|
+
stateValuesResponse: (path: import("dreaction-protocol").StateValuesResponsePayload["path"], value: import("dreaction-protocol").StateValuesResponsePayload["value"], valid?: import("dreaction-protocol").StateValuesResponsePayload["value"]) => void;
|
|
34
|
+
stateKeysResponse: (path: import("dreaction-protocol").StateKeysResponsePayload["path"], keys: import("dreaction-protocol").StateKeysResponsePayload["keys"], valid?: import("dreaction-protocol").StateKeysResponsePayload["valid"]) => void;
|
|
35
|
+
stateValuesChange: (changes: import("dreaction-protocol").StateValuesChangePayload["changes"]) => false | void;
|
|
36
|
+
stateBackupResponse: (state: import("dreaction-protocol").StateBackupResponsePayload["state"]) => void;
|
|
37
|
+
};
|
|
38
|
+
}) | ((dreaction: DReactionCore) => {
|
|
39
|
+
features: {
|
|
40
|
+
apiRequest: (requestId: string, request: import("dreaction-protocol").NetworkRequest) => void;
|
|
41
|
+
apiResponse: (requestId: string, request: import("dreaction-protocol").NetworkRequest, response: import("dreaction-protocol").NetworkResponse, duration: number) => void;
|
|
42
|
+
};
|
|
43
|
+
}) | ((dreaction: DReactionCore) => {
|
|
44
|
+
features: {
|
|
45
|
+
clear: () => void;
|
|
46
|
+
};
|
|
47
|
+
}) | ((dreaction: DReactionCore) => {
|
|
48
|
+
features: {
|
|
49
|
+
reportIssue: (id: string, name?: string, description?: string) => void;
|
|
50
|
+
};
|
|
51
|
+
}))[];
|
|
52
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/plugins/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAiB,MAAM,UAAU,CAAC;AAS7D,OAAO,EAAE,OAAO,IAAI,MAAM,EAAE,MAAM,UAAU,CAAC;AAC7C,OAAO,EAAE,OAAO,IAAI,KAAK,EAAE,MAAM,SAAS,CAAC;AAC3C,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAC9D,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,EAAE,OAAO,IAAI,KAAK,EAAE,MAAM,SAAS,CAAC;AAC3C,OAAO,EAAE,OAAO,IAAI,KAAK,EAAE,MAAM,SAAS,CAAC;AAE3C,eAAO,MAAM,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KAQkB,CAAC"}
|