@crowdstrike/foundry-js 0.2.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/LICENSE.md +21 -0
- package/README.md +17 -0
- package/dist/api.d.ts +16 -0
- package/dist/apis/available-apis.d.ts +15 -0
- package/dist/apis/incidents/bridge.d.ts +15 -0
- package/dist/apis/incidents/types.d.ts +33 -0
- package/dist/apis/public-api.d.ts +23 -0
- package/dist/apis/remote-response/bridge.d.ts +15 -0
- package/dist/apis/remote-response/types.d.ts +33 -0
- package/dist/apis/types.d.ts +2 -0
- package/dist/apis/version.d.ts +1 -0
- package/dist/bridge.d.ts +15 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +911 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +69 -0
- package/dist/utils.d.ts +4 -0
- package/package.json +68 -0
package/dist/index.js
ADDED
@@ -0,0 +1,911 @@
|
|
1
|
+
/******************************************************************************
|
2
|
+
Copyright (c) Microsoft Corporation.
|
3
|
+
|
4
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
5
|
+
purpose with or without fee is hereby granted.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
8
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
9
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
10
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
11
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
12
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
13
|
+
PERFORMANCE OF THIS SOFTWARE.
|
14
|
+
***************************************************************************** */
|
15
|
+
|
16
|
+
function __decorate(decorators, target, key, desc) {
|
17
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
18
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
19
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
20
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
21
|
+
}
|
22
|
+
|
23
|
+
function Memoize(args) {
|
24
|
+
let hashFunction;
|
25
|
+
let duration;
|
26
|
+
let tags;
|
27
|
+
if (typeof args === 'object') {
|
28
|
+
hashFunction = args.hashFunction;
|
29
|
+
duration = args.expiring;
|
30
|
+
tags = args.tags;
|
31
|
+
}
|
32
|
+
else {
|
33
|
+
hashFunction = args;
|
34
|
+
}
|
35
|
+
return (target, propertyKey, descriptor) => {
|
36
|
+
if (descriptor.value != null) {
|
37
|
+
descriptor.value = getNewFunction(descriptor.value, hashFunction, duration, tags);
|
38
|
+
}
|
39
|
+
else if (descriptor.get != null) {
|
40
|
+
descriptor.get = getNewFunction(descriptor.get, hashFunction, duration, tags);
|
41
|
+
}
|
42
|
+
else {
|
43
|
+
throw 'Only put a Memoize() decorator on a method or get accessor.';
|
44
|
+
}
|
45
|
+
};
|
46
|
+
}
|
47
|
+
const clearCacheTagsMap = new Map();
|
48
|
+
function getNewFunction(originalMethod, hashFunction, duration = 0, tags) {
|
49
|
+
const propMapName = Symbol(`__memoized_map__`);
|
50
|
+
return function (...args) {
|
51
|
+
let returnedValue;
|
52
|
+
if (!this.hasOwnProperty(propMapName)) {
|
53
|
+
Object.defineProperty(this, propMapName, {
|
54
|
+
configurable: false,
|
55
|
+
enumerable: false,
|
56
|
+
writable: false,
|
57
|
+
value: new Map()
|
58
|
+
});
|
59
|
+
}
|
60
|
+
let myMap = this[propMapName];
|
61
|
+
if (Array.isArray(tags)) {
|
62
|
+
for (const tag of tags) {
|
63
|
+
if (clearCacheTagsMap.has(tag)) {
|
64
|
+
clearCacheTagsMap.get(tag).push(myMap);
|
65
|
+
}
|
66
|
+
else {
|
67
|
+
clearCacheTagsMap.set(tag, [myMap]);
|
68
|
+
}
|
69
|
+
}
|
70
|
+
}
|
71
|
+
if (hashFunction || args.length > 0 || duration > 0) {
|
72
|
+
let hashKey;
|
73
|
+
if (hashFunction === true) {
|
74
|
+
hashKey = args.map(a => a.toString()).join('!');
|
75
|
+
}
|
76
|
+
else if (hashFunction) {
|
77
|
+
hashKey = hashFunction.apply(this, args);
|
78
|
+
}
|
79
|
+
else {
|
80
|
+
hashKey = args[0];
|
81
|
+
}
|
82
|
+
const timestampKey = `${hashKey}__timestamp`;
|
83
|
+
let isExpired = false;
|
84
|
+
if (duration > 0) {
|
85
|
+
if (!myMap.has(timestampKey)) {
|
86
|
+
isExpired = true;
|
87
|
+
}
|
88
|
+
else {
|
89
|
+
let timestamp = myMap.get(timestampKey);
|
90
|
+
isExpired = (Date.now() - timestamp) > duration;
|
91
|
+
}
|
92
|
+
}
|
93
|
+
if (myMap.has(hashKey) && !isExpired) {
|
94
|
+
returnedValue = myMap.get(hashKey);
|
95
|
+
}
|
96
|
+
else {
|
97
|
+
returnedValue = originalMethod.apply(this, args);
|
98
|
+
myMap.set(hashKey, returnedValue);
|
99
|
+
if (duration > 0) {
|
100
|
+
myMap.set(timestampKey, Date.now());
|
101
|
+
}
|
102
|
+
}
|
103
|
+
}
|
104
|
+
else {
|
105
|
+
const hashKey = this;
|
106
|
+
if (myMap.has(hashKey)) {
|
107
|
+
returnedValue = myMap.get(hashKey);
|
108
|
+
}
|
109
|
+
else {
|
110
|
+
returnedValue = originalMethod.apply(this, args);
|
111
|
+
myMap.set(hashKey, returnedValue);
|
112
|
+
}
|
113
|
+
}
|
114
|
+
return returnedValue;
|
115
|
+
};
|
116
|
+
}
|
117
|
+
|
118
|
+
/**
|
119
|
+
*
|
120
|
+
* This file is autogenerated.
|
121
|
+
*
|
122
|
+
* DO NOT EDIT DIRECTLY
|
123
|
+
*
|
124
|
+
**/
|
125
|
+
class IncidentsApiBridge {
|
126
|
+
bridge;
|
127
|
+
constructor(bridge) {
|
128
|
+
this.bridge = bridge;
|
129
|
+
}
|
130
|
+
async getIncidentIds(urlParams = {}) {
|
131
|
+
const message = {
|
132
|
+
type: 'api',
|
133
|
+
api: 'incidents',
|
134
|
+
method: 'getQueriesIncidentsV1',
|
135
|
+
payload: {
|
136
|
+
params: urlParams,
|
137
|
+
},
|
138
|
+
};
|
139
|
+
return this.bridge.postMessage(message);
|
140
|
+
}
|
141
|
+
async getIncidentEntities(postBody, urlParams = {}) {
|
142
|
+
const message = {
|
143
|
+
type: 'api',
|
144
|
+
api: 'incidents',
|
145
|
+
method: 'postEntitiesIncidentsGetV1',
|
146
|
+
payload: {
|
147
|
+
body: postBody,
|
148
|
+
params: urlParams,
|
149
|
+
},
|
150
|
+
};
|
151
|
+
return this.bridge.postMessage(message);
|
152
|
+
}
|
153
|
+
}
|
154
|
+
|
155
|
+
/**
|
156
|
+
*
|
157
|
+
* This file is autogenerated.
|
158
|
+
*
|
159
|
+
* DO NOT EDIT DIRECTLY
|
160
|
+
*
|
161
|
+
**/
|
162
|
+
class RemoteResponseApiBridge {
|
163
|
+
bridge;
|
164
|
+
constructor(bridge) {
|
165
|
+
this.bridge = bridge;
|
166
|
+
}
|
167
|
+
async getScriptIds(urlParams = {}) {
|
168
|
+
const message = {
|
169
|
+
type: 'api',
|
170
|
+
api: 'remoteResponse',
|
171
|
+
method: 'getQueriesScriptsV1',
|
172
|
+
payload: { params: urlParams },
|
173
|
+
};
|
174
|
+
return this.bridge.postMessage(message);
|
175
|
+
}
|
176
|
+
async getScriptEntities(postBody, urlParams = {}) {
|
177
|
+
const message = {
|
178
|
+
type: 'api',
|
179
|
+
api: 'remoteResponse',
|
180
|
+
method: 'postEntitiesScriptsGetV2',
|
181
|
+
payload: { body: postBody, params: urlParams },
|
182
|
+
};
|
183
|
+
return this.bridge.postMessage(message);
|
184
|
+
}
|
185
|
+
}
|
186
|
+
|
187
|
+
function assertConnection(falcon) {
|
188
|
+
if (!falcon.isConnected) {
|
189
|
+
throw new Error('You cannot call this API before having established a connection to the host!');
|
190
|
+
}
|
191
|
+
}
|
192
|
+
function isValidResponse(
|
193
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
194
|
+
event) {
|
195
|
+
return !!event?.data?.meta?.messageId;
|
196
|
+
}
|
197
|
+
|
198
|
+
/**
|
199
|
+
*
|
200
|
+
* This file is autogenerated from the available APIs for App Platform.
|
201
|
+
*
|
202
|
+
* DO NOT EDIT DIRECTLY
|
203
|
+
*
|
204
|
+
* If you need to change the contents of this file please edit the above configuration file and
|
205
|
+
* then run:
|
206
|
+
*
|
207
|
+
* ```
|
208
|
+
* yarn cs-gen platform-apis
|
209
|
+
* ```
|
210
|
+
*
|
211
|
+
**/
|
212
|
+
class FalconPublicApis {
|
213
|
+
isConnected = false;
|
214
|
+
get incidents() {
|
215
|
+
assertConnection(this);
|
216
|
+
return new IncidentsApiBridge(this.bridge);
|
217
|
+
}
|
218
|
+
get remoteResponse() {
|
219
|
+
assertConnection(this);
|
220
|
+
return new RemoteResponseApiBridge(this.bridge);
|
221
|
+
}
|
222
|
+
}
|
223
|
+
__decorate([
|
224
|
+
Memoize()
|
225
|
+
], FalconPublicApis.prototype, "incidents", null);
|
226
|
+
__decorate([
|
227
|
+
Memoize()
|
228
|
+
], FalconPublicApis.prototype, "remoteResponse", null);
|
229
|
+
|
230
|
+
const anyMap = new WeakMap();
|
231
|
+
const eventsMap = new WeakMap();
|
232
|
+
const producersMap = new WeakMap();
|
233
|
+
|
234
|
+
const anyProducer = Symbol('anyProducer');
|
235
|
+
const resolvedPromise = Promise.resolve();
|
236
|
+
|
237
|
+
// Define symbols for "meta" events.
|
238
|
+
const listenerAdded = Symbol('listenerAdded');
|
239
|
+
const listenerRemoved = Symbol('listenerRemoved');
|
240
|
+
|
241
|
+
let canEmitMetaEvents = false;
|
242
|
+
let isGlobalDebugEnabled = false;
|
243
|
+
|
244
|
+
function assertEventName(eventName) {
|
245
|
+
if (typeof eventName !== 'string' && typeof eventName !== 'symbol' && typeof eventName !== 'number') {
|
246
|
+
throw new TypeError('`eventName` must be a string, symbol, or number');
|
247
|
+
}
|
248
|
+
}
|
249
|
+
|
250
|
+
function assertListener(listener) {
|
251
|
+
if (typeof listener !== 'function') {
|
252
|
+
throw new TypeError('listener must be a function');
|
253
|
+
}
|
254
|
+
}
|
255
|
+
|
256
|
+
function getListeners(instance, eventName) {
|
257
|
+
const events = eventsMap.get(instance);
|
258
|
+
if (!events.has(eventName)) {
|
259
|
+
return;
|
260
|
+
}
|
261
|
+
|
262
|
+
return events.get(eventName);
|
263
|
+
}
|
264
|
+
|
265
|
+
function getEventProducers(instance, eventName) {
|
266
|
+
const key = typeof eventName === 'string' || typeof eventName === 'symbol' || typeof eventName === 'number' ? eventName : anyProducer;
|
267
|
+
const producers = producersMap.get(instance);
|
268
|
+
if (!producers.has(key)) {
|
269
|
+
return;
|
270
|
+
}
|
271
|
+
|
272
|
+
return producers.get(key);
|
273
|
+
}
|
274
|
+
|
275
|
+
function enqueueProducers(instance, eventName, eventData) {
|
276
|
+
const producers = producersMap.get(instance);
|
277
|
+
if (producers.has(eventName)) {
|
278
|
+
for (const producer of producers.get(eventName)) {
|
279
|
+
producer.enqueue(eventData);
|
280
|
+
}
|
281
|
+
}
|
282
|
+
|
283
|
+
if (producers.has(anyProducer)) {
|
284
|
+
const item = Promise.all([eventName, eventData]);
|
285
|
+
for (const producer of producers.get(anyProducer)) {
|
286
|
+
producer.enqueue(item);
|
287
|
+
}
|
288
|
+
}
|
289
|
+
}
|
290
|
+
|
291
|
+
function iterator(instance, eventNames) {
|
292
|
+
eventNames = Array.isArray(eventNames) ? eventNames : [eventNames];
|
293
|
+
|
294
|
+
let isFinished = false;
|
295
|
+
let flush = () => {};
|
296
|
+
let queue = [];
|
297
|
+
|
298
|
+
const producer = {
|
299
|
+
enqueue(item) {
|
300
|
+
queue.push(item);
|
301
|
+
flush();
|
302
|
+
},
|
303
|
+
finish() {
|
304
|
+
isFinished = true;
|
305
|
+
flush();
|
306
|
+
},
|
307
|
+
};
|
308
|
+
|
309
|
+
for (const eventName of eventNames) {
|
310
|
+
let set = getEventProducers(instance, eventName);
|
311
|
+
if (!set) {
|
312
|
+
set = new Set();
|
313
|
+
const producers = producersMap.get(instance);
|
314
|
+
producers.set(eventName, set);
|
315
|
+
}
|
316
|
+
|
317
|
+
set.add(producer);
|
318
|
+
}
|
319
|
+
|
320
|
+
return {
|
321
|
+
async next() {
|
322
|
+
if (!queue) {
|
323
|
+
return {done: true};
|
324
|
+
}
|
325
|
+
|
326
|
+
if (queue.length === 0) {
|
327
|
+
if (isFinished) {
|
328
|
+
queue = undefined;
|
329
|
+
return this.next();
|
330
|
+
}
|
331
|
+
|
332
|
+
await new Promise(resolve => {
|
333
|
+
flush = resolve;
|
334
|
+
});
|
335
|
+
|
336
|
+
return this.next();
|
337
|
+
}
|
338
|
+
|
339
|
+
return {
|
340
|
+
done: false,
|
341
|
+
value: await queue.shift(),
|
342
|
+
};
|
343
|
+
},
|
344
|
+
|
345
|
+
async return(value) {
|
346
|
+
queue = undefined;
|
347
|
+
|
348
|
+
for (const eventName of eventNames) {
|
349
|
+
const set = getEventProducers(instance, eventName);
|
350
|
+
if (set) {
|
351
|
+
set.delete(producer);
|
352
|
+
if (set.size === 0) {
|
353
|
+
const producers = producersMap.get(instance);
|
354
|
+
producers.delete(eventName);
|
355
|
+
}
|
356
|
+
}
|
357
|
+
}
|
358
|
+
|
359
|
+
flush();
|
360
|
+
|
361
|
+
return arguments.length > 0
|
362
|
+
? {done: true, value: await value}
|
363
|
+
: {done: true};
|
364
|
+
},
|
365
|
+
|
366
|
+
[Symbol.asyncIterator]() {
|
367
|
+
return this;
|
368
|
+
},
|
369
|
+
};
|
370
|
+
}
|
371
|
+
|
372
|
+
function defaultMethodNamesOrAssert(methodNames) {
|
373
|
+
if (methodNames === undefined) {
|
374
|
+
return allEmitteryMethods;
|
375
|
+
}
|
376
|
+
|
377
|
+
if (!Array.isArray(methodNames)) {
|
378
|
+
throw new TypeError('`methodNames` must be an array of strings');
|
379
|
+
}
|
380
|
+
|
381
|
+
for (const methodName of methodNames) {
|
382
|
+
if (!allEmitteryMethods.includes(methodName)) {
|
383
|
+
if (typeof methodName !== 'string') {
|
384
|
+
throw new TypeError('`methodNames` element must be a string');
|
385
|
+
}
|
386
|
+
|
387
|
+
throw new Error(`${methodName} is not Emittery method`);
|
388
|
+
}
|
389
|
+
}
|
390
|
+
|
391
|
+
return methodNames;
|
392
|
+
}
|
393
|
+
|
394
|
+
const isMetaEvent = eventName => eventName === listenerAdded || eventName === listenerRemoved;
|
395
|
+
|
396
|
+
function emitMetaEvent(emitter, eventName, eventData) {
|
397
|
+
if (isMetaEvent(eventName)) {
|
398
|
+
try {
|
399
|
+
canEmitMetaEvents = true;
|
400
|
+
emitter.emit(eventName, eventData);
|
401
|
+
} finally {
|
402
|
+
canEmitMetaEvents = false;
|
403
|
+
}
|
404
|
+
}
|
405
|
+
}
|
406
|
+
|
407
|
+
class Emittery {
|
408
|
+
static mixin(emitteryPropertyName, methodNames) {
|
409
|
+
methodNames = defaultMethodNamesOrAssert(methodNames);
|
410
|
+
return target => {
|
411
|
+
if (typeof target !== 'function') {
|
412
|
+
throw new TypeError('`target` must be function');
|
413
|
+
}
|
414
|
+
|
415
|
+
for (const methodName of methodNames) {
|
416
|
+
if (target.prototype[methodName] !== undefined) {
|
417
|
+
throw new Error(`The property \`${methodName}\` already exists on \`target\``);
|
418
|
+
}
|
419
|
+
}
|
420
|
+
|
421
|
+
function getEmitteryProperty() {
|
422
|
+
Object.defineProperty(this, emitteryPropertyName, {
|
423
|
+
enumerable: false,
|
424
|
+
value: new Emittery(),
|
425
|
+
});
|
426
|
+
return this[emitteryPropertyName];
|
427
|
+
}
|
428
|
+
|
429
|
+
Object.defineProperty(target.prototype, emitteryPropertyName, {
|
430
|
+
enumerable: false,
|
431
|
+
get: getEmitteryProperty,
|
432
|
+
});
|
433
|
+
|
434
|
+
const emitteryMethodCaller = methodName => function (...args) {
|
435
|
+
return this[emitteryPropertyName][methodName](...args);
|
436
|
+
};
|
437
|
+
|
438
|
+
for (const methodName of methodNames) {
|
439
|
+
Object.defineProperty(target.prototype, methodName, {
|
440
|
+
enumerable: false,
|
441
|
+
value: emitteryMethodCaller(methodName),
|
442
|
+
});
|
443
|
+
}
|
444
|
+
|
445
|
+
return target;
|
446
|
+
};
|
447
|
+
}
|
448
|
+
|
449
|
+
static get isDebugEnabled() {
|
450
|
+
// In a browser environment, `globalThis.process` can potentially reference a DOM Element with a `#process` ID,
|
451
|
+
// so instead of just type checking `globalThis.process`, we need to make sure that `globalThis.process.env` exists.
|
452
|
+
// eslint-disable-next-line n/prefer-global/process
|
453
|
+
if (typeof globalThis.process?.env !== 'object') {
|
454
|
+
return isGlobalDebugEnabled;
|
455
|
+
}
|
456
|
+
|
457
|
+
// eslint-disable-next-line n/prefer-global/process
|
458
|
+
const {env} = globalThis.process ?? {env: {}};
|
459
|
+
return env.DEBUG === 'emittery' || env.DEBUG === '*' || isGlobalDebugEnabled;
|
460
|
+
}
|
461
|
+
|
462
|
+
static set isDebugEnabled(newValue) {
|
463
|
+
isGlobalDebugEnabled = newValue;
|
464
|
+
}
|
465
|
+
|
466
|
+
constructor(options = {}) {
|
467
|
+
anyMap.set(this, new Set());
|
468
|
+
eventsMap.set(this, new Map());
|
469
|
+
producersMap.set(this, new Map());
|
470
|
+
|
471
|
+
producersMap.get(this).set(anyProducer, new Set());
|
472
|
+
|
473
|
+
this.debug = options.debug ?? {};
|
474
|
+
|
475
|
+
if (this.debug.enabled === undefined) {
|
476
|
+
this.debug.enabled = false;
|
477
|
+
}
|
478
|
+
|
479
|
+
if (!this.debug.logger) {
|
480
|
+
this.debug.logger = (type, debugName, eventName, eventData) => {
|
481
|
+
try {
|
482
|
+
// TODO: Use https://github.com/sindresorhus/safe-stringify when the package is more mature. Just copy-paste the code.
|
483
|
+
eventData = JSON.stringify(eventData);
|
484
|
+
} catch {
|
485
|
+
eventData = `Object with the following keys failed to stringify: ${Object.keys(eventData).join(',')}`;
|
486
|
+
}
|
487
|
+
|
488
|
+
if (typeof eventName === 'symbol' || typeof eventName === 'number') {
|
489
|
+
eventName = eventName.toString();
|
490
|
+
}
|
491
|
+
|
492
|
+
const currentTime = new Date();
|
493
|
+
const logTime = `${currentTime.getHours()}:${currentTime.getMinutes()}:${currentTime.getSeconds()}.${currentTime.getMilliseconds()}`;
|
494
|
+
console.log(`[${logTime}][emittery:${type}][${debugName}] Event Name: ${eventName}\n\tdata: ${eventData}`);
|
495
|
+
};
|
496
|
+
}
|
497
|
+
}
|
498
|
+
|
499
|
+
logIfDebugEnabled(type, eventName, eventData) {
|
500
|
+
if (Emittery.isDebugEnabled || this.debug.enabled) {
|
501
|
+
this.debug.logger(type, this.debug.name, eventName, eventData);
|
502
|
+
}
|
503
|
+
}
|
504
|
+
|
505
|
+
on(eventNames, listener) {
|
506
|
+
assertListener(listener);
|
507
|
+
|
508
|
+
eventNames = Array.isArray(eventNames) ? eventNames : [eventNames];
|
509
|
+
for (const eventName of eventNames) {
|
510
|
+
assertEventName(eventName);
|
511
|
+
let set = getListeners(this, eventName);
|
512
|
+
if (!set) {
|
513
|
+
set = new Set();
|
514
|
+
const events = eventsMap.get(this);
|
515
|
+
events.set(eventName, set);
|
516
|
+
}
|
517
|
+
|
518
|
+
set.add(listener);
|
519
|
+
|
520
|
+
this.logIfDebugEnabled('subscribe', eventName, undefined);
|
521
|
+
|
522
|
+
if (!isMetaEvent(eventName)) {
|
523
|
+
emitMetaEvent(this, listenerAdded, {eventName, listener});
|
524
|
+
}
|
525
|
+
}
|
526
|
+
|
527
|
+
return this.off.bind(this, eventNames, listener);
|
528
|
+
}
|
529
|
+
|
530
|
+
off(eventNames, listener) {
|
531
|
+
assertListener(listener);
|
532
|
+
|
533
|
+
eventNames = Array.isArray(eventNames) ? eventNames : [eventNames];
|
534
|
+
for (const eventName of eventNames) {
|
535
|
+
assertEventName(eventName);
|
536
|
+
const set = getListeners(this, eventName);
|
537
|
+
if (set) {
|
538
|
+
set.delete(listener);
|
539
|
+
if (set.size === 0) {
|
540
|
+
const events = eventsMap.get(this);
|
541
|
+
events.delete(eventName);
|
542
|
+
}
|
543
|
+
}
|
544
|
+
|
545
|
+
this.logIfDebugEnabled('unsubscribe', eventName, undefined);
|
546
|
+
|
547
|
+
if (!isMetaEvent(eventName)) {
|
548
|
+
emitMetaEvent(this, listenerRemoved, {eventName, listener});
|
549
|
+
}
|
550
|
+
}
|
551
|
+
}
|
552
|
+
|
553
|
+
once(eventNames) {
|
554
|
+
let off_;
|
555
|
+
|
556
|
+
const promise = new Promise(resolve => {
|
557
|
+
off_ = this.on(eventNames, data => {
|
558
|
+
off_();
|
559
|
+
resolve(data);
|
560
|
+
});
|
561
|
+
});
|
562
|
+
|
563
|
+
promise.off = off_;
|
564
|
+
return promise;
|
565
|
+
}
|
566
|
+
|
567
|
+
events(eventNames) {
|
568
|
+
eventNames = Array.isArray(eventNames) ? eventNames : [eventNames];
|
569
|
+
for (const eventName of eventNames) {
|
570
|
+
assertEventName(eventName);
|
571
|
+
}
|
572
|
+
|
573
|
+
return iterator(this, eventNames);
|
574
|
+
}
|
575
|
+
|
576
|
+
async emit(eventName, eventData) {
|
577
|
+
assertEventName(eventName);
|
578
|
+
|
579
|
+
if (isMetaEvent(eventName) && !canEmitMetaEvents) {
|
580
|
+
throw new TypeError('`eventName` cannot be meta event `listenerAdded` or `listenerRemoved`');
|
581
|
+
}
|
582
|
+
|
583
|
+
this.logIfDebugEnabled('emit', eventName, eventData);
|
584
|
+
|
585
|
+
enqueueProducers(this, eventName, eventData);
|
586
|
+
|
587
|
+
const listeners = getListeners(this, eventName) ?? new Set();
|
588
|
+
const anyListeners = anyMap.get(this);
|
589
|
+
const staticListeners = [...listeners];
|
590
|
+
const staticAnyListeners = isMetaEvent(eventName) ? [] : [...anyListeners];
|
591
|
+
|
592
|
+
await resolvedPromise;
|
593
|
+
await Promise.all([
|
594
|
+
...staticListeners.map(async listener => {
|
595
|
+
if (listeners.has(listener)) {
|
596
|
+
return listener(eventData);
|
597
|
+
}
|
598
|
+
}),
|
599
|
+
...staticAnyListeners.map(async listener => {
|
600
|
+
if (anyListeners.has(listener)) {
|
601
|
+
return listener(eventName, eventData);
|
602
|
+
}
|
603
|
+
}),
|
604
|
+
]);
|
605
|
+
}
|
606
|
+
|
607
|
+
async emitSerial(eventName, eventData) {
|
608
|
+
assertEventName(eventName);
|
609
|
+
|
610
|
+
if (isMetaEvent(eventName) && !canEmitMetaEvents) {
|
611
|
+
throw new TypeError('`eventName` cannot be meta event `listenerAdded` or `listenerRemoved`');
|
612
|
+
}
|
613
|
+
|
614
|
+
this.logIfDebugEnabled('emitSerial', eventName, eventData);
|
615
|
+
|
616
|
+
const listeners = getListeners(this, eventName) ?? new Set();
|
617
|
+
const anyListeners = anyMap.get(this);
|
618
|
+
const staticListeners = [...listeners];
|
619
|
+
const staticAnyListeners = [...anyListeners];
|
620
|
+
|
621
|
+
await resolvedPromise;
|
622
|
+
/* eslint-disable no-await-in-loop */
|
623
|
+
for (const listener of staticListeners) {
|
624
|
+
if (listeners.has(listener)) {
|
625
|
+
await listener(eventData);
|
626
|
+
}
|
627
|
+
}
|
628
|
+
|
629
|
+
for (const listener of staticAnyListeners) {
|
630
|
+
if (anyListeners.has(listener)) {
|
631
|
+
await listener(eventName, eventData);
|
632
|
+
}
|
633
|
+
}
|
634
|
+
/* eslint-enable no-await-in-loop */
|
635
|
+
}
|
636
|
+
|
637
|
+
onAny(listener) {
|
638
|
+
assertListener(listener);
|
639
|
+
|
640
|
+
this.logIfDebugEnabled('subscribeAny', undefined, undefined);
|
641
|
+
|
642
|
+
anyMap.get(this).add(listener);
|
643
|
+
emitMetaEvent(this, listenerAdded, {listener});
|
644
|
+
return this.offAny.bind(this, listener);
|
645
|
+
}
|
646
|
+
|
647
|
+
anyEvent() {
|
648
|
+
return iterator(this);
|
649
|
+
}
|
650
|
+
|
651
|
+
offAny(listener) {
|
652
|
+
assertListener(listener);
|
653
|
+
|
654
|
+
this.logIfDebugEnabled('unsubscribeAny', undefined, undefined);
|
655
|
+
|
656
|
+
emitMetaEvent(this, listenerRemoved, {listener});
|
657
|
+
anyMap.get(this).delete(listener);
|
658
|
+
}
|
659
|
+
|
660
|
+
clearListeners(eventNames) {
|
661
|
+
eventNames = Array.isArray(eventNames) ? eventNames : [eventNames];
|
662
|
+
|
663
|
+
for (const eventName of eventNames) {
|
664
|
+
this.logIfDebugEnabled('clear', eventName, undefined);
|
665
|
+
|
666
|
+
if (typeof eventName === 'string' || typeof eventName === 'symbol' || typeof eventName === 'number') {
|
667
|
+
const set = getListeners(this, eventName);
|
668
|
+
if (set) {
|
669
|
+
set.clear();
|
670
|
+
}
|
671
|
+
|
672
|
+
const producers = getEventProducers(this, eventName);
|
673
|
+
if (producers) {
|
674
|
+
for (const producer of producers) {
|
675
|
+
producer.finish();
|
676
|
+
}
|
677
|
+
|
678
|
+
producers.clear();
|
679
|
+
}
|
680
|
+
} else {
|
681
|
+
anyMap.get(this).clear();
|
682
|
+
|
683
|
+
for (const [eventName, listeners] of eventsMap.get(this).entries()) {
|
684
|
+
listeners.clear();
|
685
|
+
eventsMap.get(this).delete(eventName);
|
686
|
+
}
|
687
|
+
|
688
|
+
for (const [eventName, producers] of producersMap.get(this).entries()) {
|
689
|
+
for (const producer of producers) {
|
690
|
+
producer.finish();
|
691
|
+
}
|
692
|
+
|
693
|
+
producers.clear();
|
694
|
+
producersMap.get(this).delete(eventName);
|
695
|
+
}
|
696
|
+
}
|
697
|
+
}
|
698
|
+
}
|
699
|
+
|
700
|
+
listenerCount(eventNames) {
|
701
|
+
eventNames = Array.isArray(eventNames) ? eventNames : [eventNames];
|
702
|
+
let count = 0;
|
703
|
+
|
704
|
+
for (const eventName of eventNames) {
|
705
|
+
if (typeof eventName === 'string') {
|
706
|
+
count += anyMap.get(this).size
|
707
|
+
+ (getListeners(this, eventName)?.size ?? 0)
|
708
|
+
+ (getEventProducers(this, eventName)?.size ?? 0)
|
709
|
+
+ (getEventProducers(this)?.size ?? 0);
|
710
|
+
|
711
|
+
continue;
|
712
|
+
}
|
713
|
+
|
714
|
+
if (typeof eventName !== 'undefined') {
|
715
|
+
assertEventName(eventName);
|
716
|
+
}
|
717
|
+
|
718
|
+
count += anyMap.get(this).size;
|
719
|
+
|
720
|
+
for (const value of eventsMap.get(this).values()) {
|
721
|
+
count += value.size;
|
722
|
+
}
|
723
|
+
|
724
|
+
for (const value of producersMap.get(this).values()) {
|
725
|
+
count += value.size;
|
726
|
+
}
|
727
|
+
}
|
728
|
+
|
729
|
+
return count;
|
730
|
+
}
|
731
|
+
|
732
|
+
bindMethods(target, methodNames) {
|
733
|
+
if (typeof target !== 'object' || target === null) {
|
734
|
+
throw new TypeError('`target` must be an object');
|
735
|
+
}
|
736
|
+
|
737
|
+
methodNames = defaultMethodNamesOrAssert(methodNames);
|
738
|
+
|
739
|
+
for (const methodName of methodNames) {
|
740
|
+
if (target[methodName] !== undefined) {
|
741
|
+
throw new Error(`The property \`${methodName}\` already exists on \`target\``);
|
742
|
+
}
|
743
|
+
|
744
|
+
Object.defineProperty(target, methodName, {
|
745
|
+
enumerable: false,
|
746
|
+
value: this[methodName].bind(this),
|
747
|
+
});
|
748
|
+
}
|
749
|
+
}
|
750
|
+
}
|
751
|
+
|
752
|
+
const allEmitteryMethods = Object.getOwnPropertyNames(Emittery.prototype).filter(v => v !== 'constructor');
|
753
|
+
|
754
|
+
Object.defineProperty(Emittery, 'listenerAdded', {
|
755
|
+
value: listenerAdded,
|
756
|
+
writable: false,
|
757
|
+
enumerable: true,
|
758
|
+
configurable: false,
|
759
|
+
});
|
760
|
+
Object.defineProperty(Emittery, 'listenerRemoved', {
|
761
|
+
value: listenerRemoved,
|
762
|
+
writable: false,
|
763
|
+
enumerable: true,
|
764
|
+
configurable: false,
|
765
|
+
});
|
766
|
+
|
767
|
+
// Unique ID creation requires a high quality random # generator. In the browser we therefore
|
768
|
+
// require the crypto API and do not support built-in fallback to lower quality random number
|
769
|
+
// generators (like Math.random()).
|
770
|
+
let getRandomValues;
|
771
|
+
const rnds8 = new Uint8Array(16);
|
772
|
+
function rng() {
|
773
|
+
// lazy load so that environments that need to polyfill have a chance to do so
|
774
|
+
if (!getRandomValues) {
|
775
|
+
// getRandomValues needs to be invoked in a context where "this" is a Crypto implementation.
|
776
|
+
getRandomValues = typeof crypto !== 'undefined' && crypto.getRandomValues && crypto.getRandomValues.bind(crypto);
|
777
|
+
|
778
|
+
if (!getRandomValues) {
|
779
|
+
throw new Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');
|
780
|
+
}
|
781
|
+
}
|
782
|
+
|
783
|
+
return getRandomValues(rnds8);
|
784
|
+
}
|
785
|
+
|
786
|
+
/**
|
787
|
+
* Convert array of 16 byte values to UUID string format of the form:
|
788
|
+
* XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
|
789
|
+
*/
|
790
|
+
|
791
|
+
const byteToHex = [];
|
792
|
+
|
793
|
+
for (let i = 0; i < 256; ++i) {
|
794
|
+
byteToHex.push((i + 0x100).toString(16).slice(1));
|
795
|
+
}
|
796
|
+
|
797
|
+
function unsafeStringify(arr, offset = 0) {
|
798
|
+
// Note: Be careful editing this code! It's been tuned for performance
|
799
|
+
// and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434
|
800
|
+
return (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + '-' + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + '-' + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + '-' + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + '-' + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase();
|
801
|
+
}
|
802
|
+
|
803
|
+
const randomUUID = typeof crypto !== 'undefined' && crypto.randomUUID && crypto.randomUUID.bind(crypto);
|
804
|
+
var native = {
|
805
|
+
randomUUID
|
806
|
+
};
|
807
|
+
|
808
|
+
function v4(options, buf, offset) {
|
809
|
+
if (native.randomUUID && !buf && !options) {
|
810
|
+
return native.randomUUID();
|
811
|
+
}
|
812
|
+
|
813
|
+
options = options || {};
|
814
|
+
const rnds = options.random || (options.rng || rng)(); // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
|
815
|
+
|
816
|
+
rnds[6] = rnds[6] & 0x0f | 0x40;
|
817
|
+
rnds[8] = rnds[8] & 0x3f | 0x80; // Copy bytes to buffer, if provided
|
818
|
+
|
819
|
+
if (buf) {
|
820
|
+
offset = offset || 0;
|
821
|
+
|
822
|
+
for (let i = 0; i < 16; ++i) {
|
823
|
+
buf[offset + i] = rnds[i];
|
824
|
+
}
|
825
|
+
|
826
|
+
return buf;
|
827
|
+
}
|
828
|
+
|
829
|
+
return unsafeStringify(rnds);
|
830
|
+
}
|
831
|
+
|
832
|
+
const VERSION = 'current';
|
833
|
+
|
834
|
+
const CONNECTION_TIMEOUT = 5000;
|
835
|
+
class Bridge {
|
836
|
+
onDataUpdate;
|
837
|
+
pendingMessages = new Map();
|
838
|
+
targetOrigin = '*';
|
839
|
+
constructor({ onDataUpdate } = {}) {
|
840
|
+
this.onDataUpdate = onDataUpdate;
|
841
|
+
window.addEventListener('message', this.handleMessage);
|
842
|
+
}
|
843
|
+
destroy() {
|
844
|
+
window.removeEventListener('message', this.handleMessage);
|
845
|
+
}
|
846
|
+
setOrigin(origin) {
|
847
|
+
this.targetOrigin = origin;
|
848
|
+
}
|
849
|
+
async postMessage(message) {
|
850
|
+
return new Promise((resolve, reject) => {
|
851
|
+
const messageId = v4();
|
852
|
+
const timeoutTimer = setTimeout(() => {
|
853
|
+
reject(new Error(`Waiting for response from foundry host for "${message.type}" message (ID: ${messageId}) timed out after ${CONNECTION_TIMEOUT}ms`));
|
854
|
+
}, CONNECTION_TIMEOUT);
|
855
|
+
this.pendingMessages.set(messageId, (result) => {
|
856
|
+
clearTimeout(timeoutTimer);
|
857
|
+
resolve(result);
|
858
|
+
});
|
859
|
+
const eventData = {
|
860
|
+
message,
|
861
|
+
meta: {
|
862
|
+
messageId,
|
863
|
+
version: VERSION,
|
864
|
+
},
|
865
|
+
};
|
866
|
+
window.parent.postMessage(eventData, this.targetOrigin);
|
867
|
+
});
|
868
|
+
}
|
869
|
+
handleMessage = (event) => {
|
870
|
+
if (!isValidResponse(event)) {
|
871
|
+
return;
|
872
|
+
}
|
873
|
+
const message = event.data.message;
|
874
|
+
if (message.type === 'data') {
|
875
|
+
this.onDataUpdate?.(message);
|
876
|
+
// data update events are unidirectional and originated from the host, so there cannot be a callback waiting for this message
|
877
|
+
return;
|
878
|
+
}
|
879
|
+
const { messageId } = event.data.meta;
|
880
|
+
const callback = this.pendingMessages.get(messageId);
|
881
|
+
if (!callback) {
|
882
|
+
throw new Error(`Received unexpected message`);
|
883
|
+
}
|
884
|
+
this.pendingMessages.delete(messageId);
|
885
|
+
callback(message.payload);
|
886
|
+
};
|
887
|
+
}
|
888
|
+
|
889
|
+
class FalconApi extends FalconPublicApis {
|
890
|
+
events = new Emittery();
|
891
|
+
data;
|
892
|
+
bridge = new Bridge({
|
893
|
+
onDataUpdate: (data) => this.handleDataUpdate(data),
|
894
|
+
});
|
895
|
+
async connect() {
|
896
|
+
const { origin, data } = await this.bridge.postMessage({ type: 'connect' });
|
897
|
+
this.bridge.setOrigin(origin);
|
898
|
+
this.data = data;
|
899
|
+
this.isConnected = true;
|
900
|
+
}
|
901
|
+
handleDataUpdate(dataMessage) {
|
902
|
+
this.data = dataMessage.payload;
|
903
|
+
this.events.emit('data', this.data);
|
904
|
+
}
|
905
|
+
destroy() {
|
906
|
+
this.bridge.destroy();
|
907
|
+
}
|
908
|
+
}
|
909
|
+
|
910
|
+
export { FalconApi as default };
|
911
|
+
//# sourceMappingURL=index.js.map
|