@flatfile/listener 0.0.10 → 0.1.1
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/.eslintrc.js +1 -1
- package/.turbo/turbo-build.log +7 -7
- package/CHANGELOG.md +12 -0
- package/dist/index.d.ts +13 -9
- package/dist/index.js +81 -46
- package/dist/{index.cjs → index.mjs} +60 -66
- package/package.json +5 -3
- package/src/events/flatfile.event.ts +1 -1
- package/src/events/glob.match.spec.ts +154 -0
- package/src/events/glob.match.ts +72 -21
- package/src/examples/Local.ts +2 -2
- package/src/{Client.spec.ts → flatfile.listener.spec.ts} +10 -10
- package/src/{Client.ts → flatfile.listener.ts} +7 -13
- package/src/index.ts +7 -1
package/.eslintrc.js
CHANGED
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
> @flatfile/listener@0.
|
|
2
|
+
> @flatfile/listener@0.1.1 build
|
|
3
3
|
> tsup src/index.ts --format esm,cjs --dts
|
|
4
4
|
|
|
5
5
|
[34mCLI[39m Building entry: src/index.ts
|
|
@@ -8,10 +8,10 @@
|
|
|
8
8
|
[34mCLI[39m Target: es2020
|
|
9
9
|
[34mESM[39m Build start
|
|
10
10
|
[34mCJS[39m Build start
|
|
11
|
-
[32mESM[39m [1mdist/index.
|
|
12
|
-
[32mESM[39m ⚡️ Build success in
|
|
13
|
-
[32mCJS[39m [1mdist/index.
|
|
14
|
-
[32mCJS[39m ⚡️ Build success in
|
|
11
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m25.91 KB[39m
|
|
12
|
+
[32mESM[39m ⚡️ Build success in 180ms
|
|
13
|
+
[32mCJS[39m [1mdist/index.js [22m[32m26.77 KB[39m
|
|
14
|
+
[32mCJS[39m ⚡️ Build success in 188ms
|
|
15
15
|
[34mDTS[39m Build start
|
|
16
|
-
[32mDTS[39m ⚡️ Build success in
|
|
17
|
-
[32mDTS[39m [1mdist/index.d.ts [22m[32m7.
|
|
16
|
+
[32mDTS[39m ⚡️ Build success in 7000ms
|
|
17
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m7.48 KB[39m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @flatfile/listener
|
|
2
2
|
|
|
3
|
+
## 0.1.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 356c35d: Minor CI improvements and backwards compatible rename of Client to FlatfileListener
|
|
8
|
+
|
|
9
|
+
## 0.1.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- 4d88069: Adds a major improvement to event filtering, allowing you to grep the key path and defaulting to matching any key in the object
|
|
14
|
+
|
|
3
15
|
## 0.0.10
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
package/dist/index.d.ts
CHANGED
|
@@ -159,7 +159,7 @@ declare abstract class EventDriver {
|
|
|
159
159
|
}
|
|
160
160
|
|
|
161
161
|
/**
|
|
162
|
-
* The
|
|
162
|
+
* The Flatfile Listener
|
|
163
163
|
*
|
|
164
164
|
* The Flatfile PubSub Client is just a simple event subscriber. It can
|
|
165
165
|
* receive events from any PubSub driver. The default drivers are:
|
|
@@ -188,35 +188,33 @@ declare abstract class EventDriver {
|
|
|
188
188
|
*
|
|
189
189
|
*/
|
|
190
190
|
|
|
191
|
-
declare class
|
|
191
|
+
declare class FlatfileListener extends EventHandler {
|
|
192
192
|
/**
|
|
193
193
|
* Subscribe to events only within a certain namespace.
|
|
194
194
|
*
|
|
195
195
|
* @param namespace
|
|
196
196
|
* @param cb
|
|
197
197
|
*/
|
|
198
|
-
namespace(namespace: string | string[], cb?: SubFn):
|
|
198
|
+
namespace(namespace: string | string[], cb?: SubFn): FlatfileListener;
|
|
199
199
|
/**
|
|
200
200
|
* Filter by namespace
|
|
201
201
|
*
|
|
202
202
|
* @param filter
|
|
203
203
|
* @param cb
|
|
204
204
|
*/
|
|
205
|
-
filter(filter: EventFilter, cb?: SubFn):
|
|
206
|
-
setupFunctions: Array<(...args: any[]) => void>;
|
|
207
|
-
beforeMount(cb: SubFn): void;
|
|
205
|
+
filter(filter: EventFilter, cb?: SubFn): FlatfileListener;
|
|
208
206
|
/**
|
|
209
207
|
* Start subscribing to events
|
|
210
208
|
*
|
|
211
209
|
* @param cb
|
|
212
210
|
*/
|
|
213
|
-
static create(cb: SubFn):
|
|
211
|
+
static create(cb: SubFn): FlatfileListener;
|
|
214
212
|
/**
|
|
215
213
|
* Mount this client using an acceptable Event Driver
|
|
216
214
|
*/
|
|
217
215
|
mount(driver: EventDriver): this;
|
|
218
216
|
}
|
|
219
|
-
type SubFn = (client:
|
|
217
|
+
type SubFn = (client: FlatfileListener) => void;
|
|
220
218
|
|
|
221
219
|
declare class Browser extends EventDriver {
|
|
222
220
|
_accessToken?: string;
|
|
@@ -261,4 +259,10 @@ declare class FlatfileVirtualMachine extends EventDriver {
|
|
|
261
259
|
mountEventHandler(handler: EventHandler): this;
|
|
262
260
|
}
|
|
263
261
|
|
|
264
|
-
|
|
262
|
+
/**
|
|
263
|
+
* Backwards compatibility
|
|
264
|
+
*/
|
|
265
|
+
declare class Client extends FlatfileListener {
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
export { Arrayable, AuthenticatedClient, Browser, Client, EventCallback, EventFilter, EventHandler, FlatfileEvent, FlatfileListener, FlatfileVirtualMachine, PollingEventDriver };
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -7,6 +8,10 @@ var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
|
7
8
|
var __commonJS = (cb, mod) => function __require() {
|
|
8
9
|
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
9
10
|
};
|
|
11
|
+
var __export = (target, all) => {
|
|
12
|
+
for (var name in all)
|
|
13
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
14
|
+
};
|
|
10
15
|
var __copyProps = (to, from, except, desc) => {
|
|
11
16
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
17
|
for (let key of __getOwnPropNames(from))
|
|
@@ -23,10 +28,11 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
23
28
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
24
29
|
mod
|
|
25
30
|
));
|
|
31
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
26
32
|
|
|
27
33
|
// ../../node_modules/ansi-colors/symbols.js
|
|
28
34
|
var require_symbols = __commonJS({
|
|
29
|
-
"../../node_modules/ansi-colors/symbols.js"(exports,
|
|
35
|
+
"../../node_modules/ansi-colors/symbols.js"(exports, module2) {
|
|
30
36
|
"use strict";
|
|
31
37
|
var isHyper = typeof process !== "undefined" && process.env.TERM_PROGRAM === "Hyper";
|
|
32
38
|
var isWindows = typeof process !== "undefined" && process.platform === "win32";
|
|
@@ -87,16 +93,16 @@ var require_symbols = __commonJS({
|
|
|
87
93
|
radioOn: "\u25C9",
|
|
88
94
|
warning: "\u26A0"
|
|
89
95
|
});
|
|
90
|
-
|
|
91
|
-
Reflect.defineProperty(
|
|
92
|
-
Reflect.defineProperty(
|
|
93
|
-
Reflect.defineProperty(
|
|
96
|
+
module2.exports = isWindows && !isHyper ? windows : other;
|
|
97
|
+
Reflect.defineProperty(module2.exports, "common", { enumerable: false, value: common });
|
|
98
|
+
Reflect.defineProperty(module2.exports, "windows", { enumerable: false, value: windows });
|
|
99
|
+
Reflect.defineProperty(module2.exports, "other", { enumerable: false, value: other });
|
|
94
100
|
}
|
|
95
101
|
});
|
|
96
102
|
|
|
97
103
|
// ../../node_modules/ansi-colors/index.js
|
|
98
104
|
var require_ansi_colors = __commonJS({
|
|
99
|
-
"../../node_modules/ansi-colors/index.js"(exports,
|
|
105
|
+
"../../node_modules/ansi-colors/index.js"(exports, module2) {
|
|
100
106
|
"use strict";
|
|
101
107
|
var isObject = (val) => val !== null && typeof val === "object" && !Array.isArray(val);
|
|
102
108
|
var ANSI_REGEX = /[\u001b\u009b][[\]#;?()]*(?:(?:(?:[^\W_]*;?[^\W_]*)\u0007)|(?:(?:[0-9]{1,4}(;[0-9]{0,4})*)?[~0-9=<>cf-nqrtyA-PRZ]))/g;
|
|
@@ -256,14 +262,28 @@ var require_ansi_colors = __commonJS({
|
|
|
256
262
|
colors.define = define;
|
|
257
263
|
return colors;
|
|
258
264
|
};
|
|
259
|
-
|
|
260
|
-
|
|
265
|
+
module2.exports = create();
|
|
266
|
+
module2.exports.create = create;
|
|
261
267
|
}
|
|
262
268
|
});
|
|
263
269
|
|
|
270
|
+
// src/index.ts
|
|
271
|
+
var src_exports = {};
|
|
272
|
+
__export(src_exports, {
|
|
273
|
+
AuthenticatedClient: () => AuthenticatedClient,
|
|
274
|
+
Browser: () => Browser,
|
|
275
|
+
Client: () => Client,
|
|
276
|
+
EventHandler: () => EventHandler,
|
|
277
|
+
FlatfileEvent: () => FlatfileEvent,
|
|
278
|
+
FlatfileListener: () => FlatfileListener,
|
|
279
|
+
FlatfileVirtualMachine: () => FlatfileVirtualMachine,
|
|
280
|
+
PollingEventDriver: () => PollingEventDriver
|
|
281
|
+
});
|
|
282
|
+
module.exports = __toCommonJS(src_exports);
|
|
283
|
+
|
|
264
284
|
// src/events/authenticated.client.ts
|
|
265
|
-
|
|
266
|
-
|
|
285
|
+
var import_api = require("@flatfile/api");
|
|
286
|
+
var import_node_fetch = __toESM(require("node-fetch"));
|
|
267
287
|
var FLATFILE_API_URL = process.env.AGENT_INTERNAL_URL || "http://localhost:3000";
|
|
268
288
|
var AuthenticatedClient = class {
|
|
269
289
|
constructor(accessToken, apiUrl) {
|
|
@@ -276,16 +296,16 @@ var AuthenticatedClient = class {
|
|
|
276
296
|
}
|
|
277
297
|
const accessToken = this._accessToken;
|
|
278
298
|
const apiUrl = this._apiUrl;
|
|
279
|
-
const ClientConfig = new Configuration({
|
|
299
|
+
const ClientConfig = new import_api.Configuration({
|
|
280
300
|
basePath: `${apiUrl}/v1`,
|
|
281
|
-
fetchApi:
|
|
301
|
+
fetchApi: import_node_fetch.default,
|
|
282
302
|
accessToken,
|
|
283
303
|
headers: {
|
|
284
304
|
Authorization: `Bearer ${accessToken || "..."}`,
|
|
285
305
|
"x-disable-hooks": "true"
|
|
286
306
|
}
|
|
287
307
|
});
|
|
288
|
-
this._api = new DefaultApi(ClientConfig);
|
|
308
|
+
this._api = new import_api.DefaultApi(ClientConfig);
|
|
289
309
|
return this._api;
|
|
290
310
|
}
|
|
291
311
|
fetch(url) {
|
|
@@ -297,7 +317,7 @@ var AuthenticatedClient = class {
|
|
|
297
317
|
"x-disable-hooks": "true"
|
|
298
318
|
};
|
|
299
319
|
const fetchUrl = this._apiUrl + "/" + url;
|
|
300
|
-
this._fetchApi =
|
|
320
|
+
this._fetchApi = (0, import_node_fetch.default)(fetchUrl, {
|
|
301
321
|
headers
|
|
302
322
|
}).then((resp) => resp.json()).then((resp) => resp.data);
|
|
303
323
|
return this._fetchApi;
|
|
@@ -373,7 +393,7 @@ var FlatfileEvent = class extends AuthenticatedClient {
|
|
|
373
393
|
this.context = src.context;
|
|
374
394
|
this.payload = src.payload;
|
|
375
395
|
this.target = src.target || "";
|
|
376
|
-
this.action = src.context
|
|
396
|
+
this.action = src.context?.actionName || "";
|
|
377
397
|
}
|
|
378
398
|
/**
|
|
379
399
|
* Should return either event body if expanded already or fetch data from the
|
|
@@ -515,31 +535,48 @@ function wildcardMatch(pattern, options) {
|
|
|
515
535
|
var index_es_default = wildcardMatch;
|
|
516
536
|
|
|
517
537
|
// src/events/glob.match.ts
|
|
538
|
+
var import_flat = __toESM(require("flat"));
|
|
518
539
|
function glob(val, filter) {
|
|
519
540
|
if (!val || typeof val !== "string") {
|
|
520
541
|
return false;
|
|
521
542
|
}
|
|
522
543
|
return index_es_default(filter || "**", ":")(val);
|
|
523
544
|
}
|
|
524
|
-
function objectMatches(object,
|
|
545
|
+
function objectMatches(object, filterObject) {
|
|
546
|
+
const cleanFilter = !filterObject || typeof filterObject !== "object" ? { "**": filterObject } : filterObject;
|
|
547
|
+
if (typeof object !== "object") {
|
|
548
|
+
throw new Error("You cannot filter a non-object");
|
|
549
|
+
}
|
|
525
550
|
let denied = false;
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
}));
|
|
536
|
-
} else {
|
|
537
|
-
denied || (denied = !glob(prop, filter[x]));
|
|
538
|
-
}
|
|
539
|
-
}
|
|
551
|
+
const filter = (0, import_flat.default)(cleanFilter, { safe: true });
|
|
552
|
+
const flattened = (0, import_flat.default)(object, { safe: true });
|
|
553
|
+
for (const keyPattern in filter) {
|
|
554
|
+
const keys = filterKeys(flattened, keyPattern);
|
|
555
|
+
const valuePattern = Array.isArray(filter[keyPattern]) ? filter[keyPattern] : [filter[keyPattern]];
|
|
556
|
+
denied || (denied = !keys.some((key) => {
|
|
557
|
+
const value = flattened[key];
|
|
558
|
+
return valuePattern.some((match) => globOrMatch(value, match));
|
|
559
|
+
}));
|
|
540
560
|
}
|
|
541
561
|
return !denied;
|
|
542
562
|
}
|
|
563
|
+
function filterKeys(object, glob2) {
|
|
564
|
+
glob2 = glob2.includes("*") || glob2.includes(".") ? glob2 : `**.${glob2}`;
|
|
565
|
+
const matcher = index_es_default(glob2, ".");
|
|
566
|
+
return Object.keys(object).filter((key) => matcher(key));
|
|
567
|
+
}
|
|
568
|
+
function globOrMatch(val, filter) {
|
|
569
|
+
if (val === void 0 || val === null) {
|
|
570
|
+
return filter === null;
|
|
571
|
+
}
|
|
572
|
+
if (Array.isArray(val)) {
|
|
573
|
+
return val.some((v) => globOrMatch(v, filter));
|
|
574
|
+
}
|
|
575
|
+
if (typeof filter === "string") {
|
|
576
|
+
return glob(val.toString(), filter);
|
|
577
|
+
}
|
|
578
|
+
return val === filter;
|
|
579
|
+
}
|
|
543
580
|
|
|
544
581
|
// src/events/event.handler.ts
|
|
545
582
|
var EventHandler = class extends AuthenticatedClient {
|
|
@@ -663,12 +700,8 @@ var EventHandler = class extends AuthenticatedClient {
|
|
|
663
700
|
}
|
|
664
701
|
};
|
|
665
702
|
|
|
666
|
-
// src/
|
|
667
|
-
var
|
|
668
|
-
constructor() {
|
|
669
|
-
super(...arguments);
|
|
670
|
-
this.setupFunctions = [];
|
|
671
|
-
}
|
|
703
|
+
// src/flatfile.listener.ts
|
|
704
|
+
var FlatfileListener = class extends EventHandler {
|
|
672
705
|
/**
|
|
673
706
|
* Subscribe to events only within a certain namespace.
|
|
674
707
|
*
|
|
@@ -685,29 +718,25 @@ var Client = class extends EventHandler {
|
|
|
685
718
|
* @param cb
|
|
686
719
|
*/
|
|
687
720
|
filter(filter, cb) {
|
|
688
|
-
const client = new
|
|
721
|
+
const client = new FlatfileListener(filter);
|
|
689
722
|
this.addNode(client);
|
|
690
723
|
cb?.(client);
|
|
691
724
|
return client;
|
|
692
725
|
}
|
|
693
|
-
beforeMount(cb) {
|
|
694
|
-
this.setupFunctions.push(cb);
|
|
695
|
-
}
|
|
696
726
|
/**
|
|
697
727
|
* Start subscribing to events
|
|
698
728
|
*
|
|
699
729
|
* @param cb
|
|
700
730
|
*/
|
|
701
731
|
static create(cb) {
|
|
702
|
-
const client = new
|
|
703
|
-
|
|
732
|
+
const client = new FlatfileListener();
|
|
733
|
+
cb(client);
|
|
704
734
|
return client;
|
|
705
735
|
}
|
|
706
736
|
/**
|
|
707
737
|
* Mount this client using an acceptable Event Driver
|
|
708
738
|
*/
|
|
709
739
|
mount(driver) {
|
|
710
|
-
this.setupFunctions.forEach((f) => f());
|
|
711
740
|
driver.mountEventHandler(this);
|
|
712
741
|
return this;
|
|
713
742
|
}
|
|
@@ -767,7 +796,7 @@ var Browser = class extends EventDriver {
|
|
|
767
796
|
};
|
|
768
797
|
|
|
769
798
|
// src/event-drivers/Polling.ts
|
|
770
|
-
var import_ansi_colors = __toESM(require_ansi_colors()
|
|
799
|
+
var import_ansi_colors = __toESM(require_ansi_colors());
|
|
771
800
|
var events = /* @__PURE__ */ new Map();
|
|
772
801
|
var PollingEventDriver = class extends EventDriver {
|
|
773
802
|
constructor({
|
|
@@ -841,12 +870,18 @@ var FlatfileVirtualMachine = class extends EventDriver {
|
|
|
841
870
|
return this;
|
|
842
871
|
}
|
|
843
872
|
};
|
|
844
|
-
|
|
873
|
+
|
|
874
|
+
// src/index.ts
|
|
875
|
+
var Client = class extends FlatfileListener {
|
|
876
|
+
};
|
|
877
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
878
|
+
0 && (module.exports = {
|
|
845
879
|
AuthenticatedClient,
|
|
846
880
|
Browser,
|
|
847
881
|
Client,
|
|
848
882
|
EventHandler,
|
|
849
883
|
FlatfileEvent,
|
|
884
|
+
FlatfileListener,
|
|
850
885
|
FlatfileVirtualMachine,
|
|
851
886
|
PollingEventDriver
|
|
852
|
-
};
|
|
887
|
+
});
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -8,10 +7,6 @@ var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
|
8
7
|
var __commonJS = (cb, mod) => function __require() {
|
|
9
8
|
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
10
9
|
};
|
|
11
|
-
var __export = (target, all) => {
|
|
12
|
-
for (var name in all)
|
|
13
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
14
|
-
};
|
|
15
10
|
var __copyProps = (to, from, except, desc) => {
|
|
16
11
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
17
12
|
for (let key of __getOwnPropNames(from))
|
|
@@ -28,11 +23,10 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
28
23
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
29
24
|
mod
|
|
30
25
|
));
|
|
31
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
32
26
|
|
|
33
27
|
// ../../node_modules/ansi-colors/symbols.js
|
|
34
28
|
var require_symbols = __commonJS({
|
|
35
|
-
"../../node_modules/ansi-colors/symbols.js"(exports,
|
|
29
|
+
"../../node_modules/ansi-colors/symbols.js"(exports, module) {
|
|
36
30
|
"use strict";
|
|
37
31
|
var isHyper = typeof process !== "undefined" && process.env.TERM_PROGRAM === "Hyper";
|
|
38
32
|
var isWindows = typeof process !== "undefined" && process.platform === "win32";
|
|
@@ -93,16 +87,16 @@ var require_symbols = __commonJS({
|
|
|
93
87
|
radioOn: "\u25C9",
|
|
94
88
|
warning: "\u26A0"
|
|
95
89
|
});
|
|
96
|
-
|
|
97
|
-
Reflect.defineProperty(
|
|
98
|
-
Reflect.defineProperty(
|
|
99
|
-
Reflect.defineProperty(
|
|
90
|
+
module.exports = isWindows && !isHyper ? windows : other;
|
|
91
|
+
Reflect.defineProperty(module.exports, "common", { enumerable: false, value: common });
|
|
92
|
+
Reflect.defineProperty(module.exports, "windows", { enumerable: false, value: windows });
|
|
93
|
+
Reflect.defineProperty(module.exports, "other", { enumerable: false, value: other });
|
|
100
94
|
}
|
|
101
95
|
});
|
|
102
96
|
|
|
103
97
|
// ../../node_modules/ansi-colors/index.js
|
|
104
98
|
var require_ansi_colors = __commonJS({
|
|
105
|
-
"../../node_modules/ansi-colors/index.js"(exports,
|
|
99
|
+
"../../node_modules/ansi-colors/index.js"(exports, module) {
|
|
106
100
|
"use strict";
|
|
107
101
|
var isObject = (val) => val !== null && typeof val === "object" && !Array.isArray(val);
|
|
108
102
|
var ANSI_REGEX = /[\u001b\u009b][[\]#;?()]*(?:(?:(?:[^\W_]*;?[^\W_]*)\u0007)|(?:(?:[0-9]{1,4}(;[0-9]{0,4})*)?[~0-9=<>cf-nqrtyA-PRZ]))/g;
|
|
@@ -262,27 +256,14 @@ var require_ansi_colors = __commonJS({
|
|
|
262
256
|
colors.define = define;
|
|
263
257
|
return colors;
|
|
264
258
|
};
|
|
265
|
-
|
|
266
|
-
|
|
259
|
+
module.exports = create();
|
|
260
|
+
module.exports.create = create;
|
|
267
261
|
}
|
|
268
262
|
});
|
|
269
263
|
|
|
270
|
-
// src/index.ts
|
|
271
|
-
var src_exports = {};
|
|
272
|
-
__export(src_exports, {
|
|
273
|
-
AuthenticatedClient: () => AuthenticatedClient,
|
|
274
|
-
Browser: () => Browser,
|
|
275
|
-
Client: () => Client,
|
|
276
|
-
EventHandler: () => EventHandler,
|
|
277
|
-
FlatfileEvent: () => FlatfileEvent,
|
|
278
|
-
FlatfileVirtualMachine: () => FlatfileVirtualMachine,
|
|
279
|
-
PollingEventDriver: () => PollingEventDriver
|
|
280
|
-
});
|
|
281
|
-
module.exports = __toCommonJS(src_exports);
|
|
282
|
-
|
|
283
264
|
// src/events/authenticated.client.ts
|
|
284
|
-
|
|
285
|
-
|
|
265
|
+
import { Configuration, DefaultApi } from "@flatfile/api";
|
|
266
|
+
import fetch from "node-fetch";
|
|
286
267
|
var FLATFILE_API_URL = process.env.AGENT_INTERNAL_URL || "http://localhost:3000";
|
|
287
268
|
var AuthenticatedClient = class {
|
|
288
269
|
constructor(accessToken, apiUrl) {
|
|
@@ -295,16 +276,16 @@ var AuthenticatedClient = class {
|
|
|
295
276
|
}
|
|
296
277
|
const accessToken = this._accessToken;
|
|
297
278
|
const apiUrl = this._apiUrl;
|
|
298
|
-
const ClientConfig = new
|
|
279
|
+
const ClientConfig = new Configuration({
|
|
299
280
|
basePath: `${apiUrl}/v1`,
|
|
300
|
-
fetchApi:
|
|
281
|
+
fetchApi: fetch,
|
|
301
282
|
accessToken,
|
|
302
283
|
headers: {
|
|
303
284
|
Authorization: `Bearer ${accessToken || "..."}`,
|
|
304
285
|
"x-disable-hooks": "true"
|
|
305
286
|
}
|
|
306
287
|
});
|
|
307
|
-
this._api = new
|
|
288
|
+
this._api = new DefaultApi(ClientConfig);
|
|
308
289
|
return this._api;
|
|
309
290
|
}
|
|
310
291
|
fetch(url) {
|
|
@@ -316,7 +297,7 @@ var AuthenticatedClient = class {
|
|
|
316
297
|
"x-disable-hooks": "true"
|
|
317
298
|
};
|
|
318
299
|
const fetchUrl = this._apiUrl + "/" + url;
|
|
319
|
-
this._fetchApi = (
|
|
300
|
+
this._fetchApi = fetch(fetchUrl, {
|
|
320
301
|
headers
|
|
321
302
|
}).then((resp) => resp.json()).then((resp) => resp.data);
|
|
322
303
|
return this._fetchApi;
|
|
@@ -392,7 +373,7 @@ var FlatfileEvent = class extends AuthenticatedClient {
|
|
|
392
373
|
this.context = src.context;
|
|
393
374
|
this.payload = src.payload;
|
|
394
375
|
this.target = src.target || "";
|
|
395
|
-
this.action = src.context
|
|
376
|
+
this.action = src.context?.actionName || "";
|
|
396
377
|
}
|
|
397
378
|
/**
|
|
398
379
|
* Should return either event body if expanded already or fetch data from the
|
|
@@ -534,31 +515,48 @@ function wildcardMatch(pattern, options) {
|
|
|
534
515
|
var index_es_default = wildcardMatch;
|
|
535
516
|
|
|
536
517
|
// src/events/glob.match.ts
|
|
518
|
+
import flat from "flat";
|
|
537
519
|
function glob(val, filter) {
|
|
538
520
|
if (!val || typeof val !== "string") {
|
|
539
521
|
return false;
|
|
540
522
|
}
|
|
541
523
|
return index_es_default(filter || "**", ":")(val);
|
|
542
524
|
}
|
|
543
|
-
function objectMatches(object,
|
|
525
|
+
function objectMatches(object, filterObject) {
|
|
526
|
+
const cleanFilter = !filterObject || typeof filterObject !== "object" ? { "**": filterObject } : filterObject;
|
|
527
|
+
if (typeof object !== "object") {
|
|
528
|
+
throw new Error("You cannot filter a non-object");
|
|
529
|
+
}
|
|
544
530
|
let denied = false;
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
}));
|
|
555
|
-
} else {
|
|
556
|
-
denied || (denied = !glob(prop, filter[x]));
|
|
557
|
-
}
|
|
558
|
-
}
|
|
531
|
+
const filter = flat(cleanFilter, { safe: true });
|
|
532
|
+
const flattened = flat(object, { safe: true });
|
|
533
|
+
for (const keyPattern in filter) {
|
|
534
|
+
const keys = filterKeys(flattened, keyPattern);
|
|
535
|
+
const valuePattern = Array.isArray(filter[keyPattern]) ? filter[keyPattern] : [filter[keyPattern]];
|
|
536
|
+
denied || (denied = !keys.some((key) => {
|
|
537
|
+
const value = flattened[key];
|
|
538
|
+
return valuePattern.some((match) => globOrMatch(value, match));
|
|
539
|
+
}));
|
|
559
540
|
}
|
|
560
541
|
return !denied;
|
|
561
542
|
}
|
|
543
|
+
function filterKeys(object, glob2) {
|
|
544
|
+
glob2 = glob2.includes("*") || glob2.includes(".") ? glob2 : `**.${glob2}`;
|
|
545
|
+
const matcher = index_es_default(glob2, ".");
|
|
546
|
+
return Object.keys(object).filter((key) => matcher(key));
|
|
547
|
+
}
|
|
548
|
+
function globOrMatch(val, filter) {
|
|
549
|
+
if (val === void 0 || val === null) {
|
|
550
|
+
return filter === null;
|
|
551
|
+
}
|
|
552
|
+
if (Array.isArray(val)) {
|
|
553
|
+
return val.some((v) => globOrMatch(v, filter));
|
|
554
|
+
}
|
|
555
|
+
if (typeof filter === "string") {
|
|
556
|
+
return glob(val.toString(), filter);
|
|
557
|
+
}
|
|
558
|
+
return val === filter;
|
|
559
|
+
}
|
|
562
560
|
|
|
563
561
|
// src/events/event.handler.ts
|
|
564
562
|
var EventHandler = class extends AuthenticatedClient {
|
|
@@ -682,12 +680,8 @@ var EventHandler = class extends AuthenticatedClient {
|
|
|
682
680
|
}
|
|
683
681
|
};
|
|
684
682
|
|
|
685
|
-
// src/
|
|
686
|
-
var
|
|
687
|
-
constructor() {
|
|
688
|
-
super(...arguments);
|
|
689
|
-
this.setupFunctions = [];
|
|
690
|
-
}
|
|
683
|
+
// src/flatfile.listener.ts
|
|
684
|
+
var FlatfileListener = class extends EventHandler {
|
|
691
685
|
/**
|
|
692
686
|
* Subscribe to events only within a certain namespace.
|
|
693
687
|
*
|
|
@@ -704,29 +698,25 @@ var Client = class extends EventHandler {
|
|
|
704
698
|
* @param cb
|
|
705
699
|
*/
|
|
706
700
|
filter(filter, cb) {
|
|
707
|
-
const client = new
|
|
701
|
+
const client = new FlatfileListener(filter);
|
|
708
702
|
this.addNode(client);
|
|
709
703
|
cb?.(client);
|
|
710
704
|
return client;
|
|
711
705
|
}
|
|
712
|
-
beforeMount(cb) {
|
|
713
|
-
this.setupFunctions.push(cb);
|
|
714
|
-
}
|
|
715
706
|
/**
|
|
716
707
|
* Start subscribing to events
|
|
717
708
|
*
|
|
718
709
|
* @param cb
|
|
719
710
|
*/
|
|
720
711
|
static create(cb) {
|
|
721
|
-
const client = new
|
|
722
|
-
|
|
712
|
+
const client = new FlatfileListener();
|
|
713
|
+
cb(client);
|
|
723
714
|
return client;
|
|
724
715
|
}
|
|
725
716
|
/**
|
|
726
717
|
* Mount this client using an acceptable Event Driver
|
|
727
718
|
*/
|
|
728
719
|
mount(driver) {
|
|
729
|
-
this.setupFunctions.forEach((f) => f());
|
|
730
720
|
driver.mountEventHandler(this);
|
|
731
721
|
return this;
|
|
732
722
|
}
|
|
@@ -786,7 +776,7 @@ var Browser = class extends EventDriver {
|
|
|
786
776
|
};
|
|
787
777
|
|
|
788
778
|
// src/event-drivers/Polling.ts
|
|
789
|
-
var import_ansi_colors = __toESM(require_ansi_colors()
|
|
779
|
+
var import_ansi_colors = __toESM(require_ansi_colors());
|
|
790
780
|
var events = /* @__PURE__ */ new Map();
|
|
791
781
|
var PollingEventDriver = class extends EventDriver {
|
|
792
782
|
constructor({
|
|
@@ -860,13 +850,17 @@ var FlatfileVirtualMachine = class extends EventDriver {
|
|
|
860
850
|
return this;
|
|
861
851
|
}
|
|
862
852
|
};
|
|
863
|
-
|
|
864
|
-
|
|
853
|
+
|
|
854
|
+
// src/index.ts
|
|
855
|
+
var Client = class extends FlatfileListener {
|
|
856
|
+
};
|
|
857
|
+
export {
|
|
865
858
|
AuthenticatedClient,
|
|
866
859
|
Browser,
|
|
867
860
|
Client,
|
|
868
861
|
EventHandler,
|
|
869
862
|
FlatfileEvent,
|
|
863
|
+
FlatfileListener,
|
|
870
864
|
FlatfileVirtualMachine,
|
|
871
865
|
PollingEventDriver
|
|
872
|
-
}
|
|
866
|
+
};
|
package/package.json
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flatfile/listener",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "A PubSub Listener for configuring and using Flatfile",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
|
-
"type": "module",
|
|
9
8
|
"scripts": {
|
|
10
9
|
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
|
|
11
10
|
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
12
11
|
"dev": "tsup src/index.ts --format esm,cjs --dts --watch",
|
|
13
|
-
"poll": "tsup src/dev.ts --format esm,cjs --watch --dts --onSuccess \"node dist/dev.js\""
|
|
12
|
+
"poll": "tsup src/dev.ts --format esm,cjs --watch --dts --onSuccess \"node dist/dev.js\"",
|
|
13
|
+
"test": "jest --passWithNoTests"
|
|
14
14
|
},
|
|
15
15
|
"license": "MIT",
|
|
16
16
|
"devDependencies": {
|
|
17
17
|
"@flatfile/eslint-config-platform-sdk": "*",
|
|
18
18
|
"@flatfile/jest-preset-platform-sdk": "*",
|
|
19
19
|
"@flatfile/ts-config-platform-sdk": "*",
|
|
20
|
+
"@types/flat": "^5.0.2",
|
|
20
21
|
"@types/jest": "^29.5.1",
|
|
21
22
|
"eslint": "^8.19.0",
|
|
22
23
|
"jest": "^29.5.0",
|
|
@@ -27,6 +28,7 @@
|
|
|
27
28
|
},
|
|
28
29
|
"dependencies": {
|
|
29
30
|
"@flatfile/api": "^0.0.19",
|
|
31
|
+
"flat": "^5.0.2",
|
|
30
32
|
"node-fetch": "^2.6.7"
|
|
31
33
|
}
|
|
32
34
|
}
|
|
@@ -37,7 +37,7 @@ export class FlatfileEvent extends AuthenticatedClient {
|
|
|
37
37
|
this.context = src.context // -> [us0_acc_ihjh8943h9w, space_id, workbook_id]
|
|
38
38
|
this.payload = src.payload
|
|
39
39
|
this.target = src.target || ''
|
|
40
|
-
this.action = src.context
|
|
40
|
+
this.action = src.context?.actionName || ''
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
/**
|
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
import { objectMatches } from './glob.match'
|
|
2
2
|
|
|
3
3
|
describe('objectMatches', () => {
|
|
4
|
+
test('matches a primitive anywhere in the object', () => {
|
|
5
|
+
expect(objectMatches({ foo: 'bar' }, 'bar')).toBe(true)
|
|
6
|
+
expect(objectMatches({ foo: 11 }, 11)).toBe(true)
|
|
7
|
+
expect(objectMatches({ foo: 11 }, 13)).toBe(false)
|
|
8
|
+
expect(objectMatches({ foo: 11, bar: 13 }, 13)).toBe(true)
|
|
9
|
+
expect(objectMatches({ foo: { bar: 'blue' } }, 'blue')).toBe(true)
|
|
10
|
+
expect(objectMatches({ foo: ['bar', 'baz'] }, 'baz')).toBe(true)
|
|
11
|
+
expect(objectMatches({ foo: ['bar', 'baz'] }, 'qux')).toBe(false)
|
|
12
|
+
})
|
|
13
|
+
|
|
4
14
|
test('matches an object', () => {
|
|
5
15
|
expect(objectMatches({ foo: 'bar' }, { foo: 'bar' })).toBe(true)
|
|
6
16
|
expect(objectMatches({ foo: ['bar', 'baz'] }, { foo: 'bar' })).toBe(true)
|
|
@@ -74,4 +84,148 @@ describe('objectMatches', () => {
|
|
|
74
84
|
)
|
|
75
85
|
).toBe(false)
|
|
76
86
|
})
|
|
87
|
+
|
|
88
|
+
test('allows user to provide a nested match object as well', () => {
|
|
89
|
+
expect(
|
|
90
|
+
objectMatches({ one: { two: 'bar' } }, { one: { two: 'bar' } })
|
|
91
|
+
).toBe(true)
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
test('matches a nested key', () => {
|
|
95
|
+
expect(objectMatches({ one: { two: 'bar' } }, { 'one.two': 'bar' })).toBe(
|
|
96
|
+
true
|
|
97
|
+
)
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
test('does not use end with logic when a nested pattern is provided', () => {
|
|
101
|
+
expect(
|
|
102
|
+
objectMatches({ zero: { one: { two: 'bar' } } }, { 'one.two': 'bar' })
|
|
103
|
+
).toBe(false)
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
test('does not use end with logic when a glob pattern is provided', () => {
|
|
107
|
+
expect(
|
|
108
|
+
objectMatches({ zero: { one: { two: 'bar' } } }, { '*two': 'bar' })
|
|
109
|
+
).toBe(false)
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
test('matches a glob pattern', () => {
|
|
113
|
+
expect(objectMatches({ one: { two: 'bar' } }, { '*.two': 'bar' })).toBe(
|
|
114
|
+
true
|
|
115
|
+
)
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
test('defaults to an `endsWith` pattern', () => {
|
|
119
|
+
expect(objectMatches({ one: { two: 'bar' } }, { two: 'bar' })).toBe(true)
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
test('false if non-existent key', () => {
|
|
123
|
+
expect(objectMatches({ one: { two: 'bar' } }, { three: 'bar' })).toBe(false)
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
test('matches a deep glob pattern', () => {
|
|
127
|
+
expect(
|
|
128
|
+
objectMatches({ one: { two: { three: 'bar' } } }, { '**.three': 'bar' })
|
|
129
|
+
).toBe(true)
|
|
130
|
+
expect(
|
|
131
|
+
objectMatches({ one: { two: { three: 'bar' } } }, { '*.three': 'bar' })
|
|
132
|
+
).toBe(false)
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
describe('additional edge cases', () => {
|
|
136
|
+
// Empty Objects
|
|
137
|
+
test('handles empty objects', () => {
|
|
138
|
+
expect(objectMatches({}, {})).toBe(true)
|
|
139
|
+
expect(objectMatches({ foo: 'bar' }, {})).toBe(true)
|
|
140
|
+
expect(objectMatches({}, { foo: 'bar' })).toBe(false)
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
// Non-Object Inputs
|
|
144
|
+
test('handles non-object inputs', () => {
|
|
145
|
+
// @ts-ignore
|
|
146
|
+
expect(() => objectMatches('foo', { foo: 'bar' })).toThrow()
|
|
147
|
+
|
|
148
|
+
// @ts-ignore
|
|
149
|
+
expect(() => objectMatches(123, { foo: 'bar' })).toThrow()
|
|
150
|
+
|
|
151
|
+
// @ts-ignore
|
|
152
|
+
expect(() => objectMatches(null, { foo: 'bar' })).toThrow()
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
// Special Characters in Keys
|
|
156
|
+
test('handles special characters in keys', () => {
|
|
157
|
+
expect(objectMatches({ 'foo*': 'bar' }, { 'foo*': 'bar' })).toBe(true)
|
|
158
|
+
expect(objectMatches({ 'foo.bar': 'baz' }, { 'foo.bar': 'baz' })).toBe(
|
|
159
|
+
true
|
|
160
|
+
)
|
|
161
|
+
expect(objectMatches({ foo$bar: 'baz' }, { foo$bar: 'baz' })).toBe(true)
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
// Matching Null Values
|
|
165
|
+
test('matches null values', () => {
|
|
166
|
+
expect(objectMatches({ foo: null }, { foo: null })).toBe(true)
|
|
167
|
+
expect(objectMatches({ foo: 'bar' }, { foo: null })).toBe(false)
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
// Matching Undefined Values
|
|
171
|
+
test('matches undefined values', () => {
|
|
172
|
+
expect(objectMatches({ foo: undefined }, { foo: null })).toBe(true)
|
|
173
|
+
|
|
174
|
+
// @ts-ignore
|
|
175
|
+
expect(objectMatches({ foo: 'bar' }, { foo: undefined })).toBe(false)
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
// Nested Array Matches
|
|
179
|
+
// test('handles nested array matches', () => {
|
|
180
|
+
// expect(
|
|
181
|
+
// objectMatches({ foo: [{ bar: 'baz' }] }, { foo: [{ bar: 'baz' }] })
|
|
182
|
+
// ).toBe(true)
|
|
183
|
+
// expect(
|
|
184
|
+
// objectMatches({ foo: [{ bar: 'qux' }] }, { foo: [{ bar: 'baz' }] })
|
|
185
|
+
// ).toBe(false)
|
|
186
|
+
// })
|
|
187
|
+
|
|
188
|
+
// Nested Wildcard Matches
|
|
189
|
+
test('handles nested wildcard matches', () => {
|
|
190
|
+
expect(
|
|
191
|
+
objectMatches({ foo: { bar: 'baz' } }, { foo: { bar: '*' } })
|
|
192
|
+
).toBe(true)
|
|
193
|
+
expect(
|
|
194
|
+
objectMatches({ foo: { bar: 'qux' } }, { foo: { bar: 'baz*' } })
|
|
195
|
+
).toBe(false)
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
// Nested Glob Pattern Matches
|
|
199
|
+
test('handles nested glob pattern matches', () => {
|
|
200
|
+
expect(objectMatches({ foo: { bar: 'baz' } }, { 'foo.*': 'baz' })).toBe(
|
|
201
|
+
true
|
|
202
|
+
)
|
|
203
|
+
expect(objectMatches({ foo: { bar: 'qux' } }, { 'foo.*': 'baz' })).toBe(
|
|
204
|
+
false
|
|
205
|
+
)
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
// Type Coercion
|
|
209
|
+
test('it attempts to perform type coercion when a string match is provided', () => {
|
|
210
|
+
expect(objectMatches({ foo: 1 }, { foo: '1' })).toBe(true)
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
// Type Coercion
|
|
214
|
+
test('it will not coerce types if a non-string matcher is provided', () => {
|
|
215
|
+
expect(objectMatches({ foo: '1' }, { foo: 1 })).toBe(false)
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
// Case Sensitivity
|
|
219
|
+
test('is case sensitive', () => {
|
|
220
|
+
expect(objectMatches({ foo: 'Bar' }, { foo: 'bar' })).toBe(false)
|
|
221
|
+
expect(objectMatches({ foo: 'bar' }, { foo: 'Bar' })).toBe(false)
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
// Order of Keys
|
|
225
|
+
test('does not care about order of keys', () => {
|
|
226
|
+
expect(
|
|
227
|
+
objectMatches({ foo: 'bar', baz: 'qux' }, { baz: 'qux', foo: 'bar' })
|
|
228
|
+
).toBe(true)
|
|
229
|
+
})
|
|
230
|
+
})
|
|
77
231
|
})
|
package/src/events/glob.match.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Arrayable } from './event.handler'
|
|
2
2
|
import wildMatch from 'wildcard-match'
|
|
3
|
+
import flat from 'flat'
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Glob style matching of a value
|
|
@@ -18,32 +19,82 @@ export function glob(val: any, filter: string | string[]): boolean {
|
|
|
18
19
|
* Glob style matching of values in an object
|
|
19
20
|
*
|
|
20
21
|
* @param object
|
|
21
|
-
* @param
|
|
22
|
+
* @param filterObject
|
|
22
23
|
*/
|
|
23
24
|
export function objectMatches(
|
|
24
25
|
object: Record<string, any>,
|
|
25
|
-
|
|
26
|
+
filterObject: JSONPrimitive | FilterObj
|
|
26
27
|
): boolean {
|
|
28
|
+
const cleanFilter: FilterObj =
|
|
29
|
+
!filterObject || typeof filterObject !== 'object'
|
|
30
|
+
? { '**': filterObject }
|
|
31
|
+
: filterObject
|
|
32
|
+
|
|
33
|
+
if (typeof object !== 'object') {
|
|
34
|
+
throw new Error('You cannot filter a non-object')
|
|
35
|
+
}
|
|
27
36
|
let denied = false
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
37
|
+
const filter: FilterObj = flat(cleanFilter, { safe: true })
|
|
38
|
+
const flattened = flat(object, { safe: true }) as Record<
|
|
39
|
+
string,
|
|
40
|
+
JSONPrimitive
|
|
41
|
+
>
|
|
42
|
+
|
|
43
|
+
// all filters MUST resolve true
|
|
44
|
+
for (const keyPattern in filter) {
|
|
45
|
+
const keys = filterKeys(flattened, keyPattern)
|
|
46
|
+
|
|
47
|
+
const valuePattern = (
|
|
48
|
+
Array.isArray(filter[keyPattern])
|
|
49
|
+
? filter[keyPattern]
|
|
50
|
+
: [filter[keyPattern]]
|
|
51
|
+
) as JSONPrimitive[]
|
|
52
|
+
|
|
53
|
+
// only one filter must match
|
|
54
|
+
denied ||= !keys.some((key) => {
|
|
55
|
+
const value: JSONPrimitive = flattened[key]
|
|
56
|
+
return valuePattern.some((match) => globOrMatch(value, match))
|
|
57
|
+
})
|
|
47
58
|
}
|
|
48
59
|
return !denied
|
|
49
60
|
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Glob keys of an object and return the narrowed set
|
|
64
|
+
*
|
|
65
|
+
* @param object
|
|
66
|
+
* @param glob
|
|
67
|
+
*/
|
|
68
|
+
function filterKeys<T extends Record<string, any>>(
|
|
69
|
+
object: Record<string, any>,
|
|
70
|
+
glob: string
|
|
71
|
+
): Array<keyof T> {
|
|
72
|
+
glob = glob.includes('*') || glob.includes('.') ? glob : `**.${glob}`
|
|
73
|
+
const matcher = wildMatch(glob, '.')
|
|
74
|
+
return Object.keys(object).filter((key) => matcher(key))
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function globOrMatch(
|
|
78
|
+
val: Arrayable<JSONPrimitive>,
|
|
79
|
+
filter: JSONPrimitive
|
|
80
|
+
): boolean {
|
|
81
|
+
if (val === undefined || val === null) {
|
|
82
|
+
return filter === null
|
|
83
|
+
}
|
|
84
|
+
if (Array.isArray(val)) {
|
|
85
|
+
return val.some((v) => globOrMatch(v, filter))
|
|
86
|
+
}
|
|
87
|
+
if (typeof filter === 'string') {
|
|
88
|
+
return glob(val.toString(), filter)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// otherwise do a simple comparison
|
|
92
|
+
return val === filter
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
type JSONPrimitive = string | number | boolean | null
|
|
96
|
+
|
|
97
|
+
type FilterObj = Record<
|
|
98
|
+
string,
|
|
99
|
+
Arrayable<JSONPrimitive> | Record<string, Arrayable<JSONPrimitive>>
|
|
100
|
+
>
|
package/src/examples/Local.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { FlatfileListener, FlatfileVirtualMachine } from '../'
|
|
2
2
|
|
|
3
|
-
const example =
|
|
3
|
+
const example = FlatfileListener.create((client) => {
|
|
4
4
|
/**
|
|
5
5
|
* This is a basic hook on events with no sugar on top
|
|
6
6
|
*/
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { FlatfileListener } from './flatfile.listener'
|
|
2
2
|
|
|
3
3
|
describe('Client', () => {
|
|
4
4
|
let testFn: jest.Mock
|
|
@@ -8,16 +8,16 @@ describe('Client', () => {
|
|
|
8
8
|
})
|
|
9
9
|
|
|
10
10
|
describe('triggering', () => {
|
|
11
|
-
test('triggers basic event listeners', () => {
|
|
12
|
-
const client =
|
|
11
|
+
test('triggers basic event listeners', async () => {
|
|
12
|
+
const client = FlatfileListener.create((c) => {
|
|
13
13
|
c.on('foo', testFn)
|
|
14
14
|
})
|
|
15
|
-
client.dispatchEvent({ topic: 'foo' })
|
|
15
|
+
await client.dispatchEvent({ topic: 'foo' })
|
|
16
16
|
expect(testFn).toHaveBeenCalledTimes(1)
|
|
17
17
|
})
|
|
18
18
|
|
|
19
19
|
test('triggers namespaced event listeners', () => {
|
|
20
|
-
const client =
|
|
20
|
+
const client = FlatfileListener.create((c) => {
|
|
21
21
|
c.on('records:created', testFn)
|
|
22
22
|
})
|
|
23
23
|
client.dispatchEvent({ topic: 'records:created' })
|
|
@@ -25,7 +25,7 @@ describe('Client', () => {
|
|
|
25
25
|
})
|
|
26
26
|
|
|
27
27
|
test('triggers event listener for array of events', () => {
|
|
28
|
-
const client =
|
|
28
|
+
const client = FlatfileListener.create((c) => {
|
|
29
29
|
c.on(['records:created', 'records:updated'], testFn)
|
|
30
30
|
})
|
|
31
31
|
client.dispatchEvent({ topic: 'records:created' })
|
|
@@ -34,7 +34,7 @@ describe('Client', () => {
|
|
|
34
34
|
})
|
|
35
35
|
|
|
36
36
|
test('triggers event listener for wildcard query of events', () => {
|
|
37
|
-
const client =
|
|
37
|
+
const client = FlatfileListener.create((c) => {
|
|
38
38
|
c.on('records:*', testFn)
|
|
39
39
|
})
|
|
40
40
|
client.dispatchEvent({ topic: 'records:created' })
|
|
@@ -46,7 +46,7 @@ describe('Client', () => {
|
|
|
46
46
|
|
|
47
47
|
describe('filtering', () => {
|
|
48
48
|
test('trigger only when filter match - on listener', async () => {
|
|
49
|
-
const client =
|
|
49
|
+
const client = FlatfileListener.create((c) => {
|
|
50
50
|
c.on('records:created', { domain: 'foo' }, testFn)
|
|
51
51
|
})
|
|
52
52
|
await client.dispatchEvent({ topic: 'records:created' })
|
|
@@ -55,7 +55,7 @@ describe('Client', () => {
|
|
|
55
55
|
})
|
|
56
56
|
|
|
57
57
|
test('trigger only when filter match - on scope', async () => {
|
|
58
|
-
const client =
|
|
58
|
+
const client = FlatfileListener.create((c) => {
|
|
59
59
|
c.filter({ domain: 'foo' }, (f) => {
|
|
60
60
|
f.on('records:created', testFn)
|
|
61
61
|
})
|
|
@@ -66,7 +66,7 @@ describe('Client', () => {
|
|
|
66
66
|
})
|
|
67
67
|
|
|
68
68
|
test('multiple layers of filtering', async () => {
|
|
69
|
-
const client =
|
|
69
|
+
const client = FlatfileListener.create((c) => {
|
|
70
70
|
c.filter({ domain: 'foo:*' })
|
|
71
71
|
.filter({ domain: 'foo:bar' })
|
|
72
72
|
.use((f) => {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The
|
|
2
|
+
* The Flatfile Listener
|
|
3
3
|
*
|
|
4
4
|
* The Flatfile PubSub Client is just a simple event subscriber. It can
|
|
5
5
|
* receive events from any PubSub driver. The default drivers are:
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
import { EventFilter, EventHandler } from './events'
|
|
32
32
|
import { EventDriver } from './event-drivers/_EventDriver'
|
|
33
33
|
|
|
34
|
-
export class
|
|
34
|
+
export class FlatfileListener extends EventHandler {
|
|
35
35
|
/**
|
|
36
36
|
* Subscribe to events only within a certain namespace.
|
|
37
37
|
*
|
|
@@ -49,25 +49,20 @@ export class Client extends EventHandler {
|
|
|
49
49
|
* @param cb
|
|
50
50
|
*/
|
|
51
51
|
filter(filter: EventFilter, cb?: SubFn) {
|
|
52
|
-
const client = new
|
|
52
|
+
const client = new FlatfileListener(filter)
|
|
53
53
|
this.addNode(client)
|
|
54
54
|
cb?.(client)
|
|
55
55
|
return client
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
setupFunctions: Array<(...args: any[]) => void> = []
|
|
59
|
-
public beforeMount(cb: SubFn) {
|
|
60
|
-
this.setupFunctions.push(cb)
|
|
61
|
-
}
|
|
62
|
-
|
|
63
58
|
/**
|
|
64
59
|
* Start subscribing to events
|
|
65
60
|
*
|
|
66
61
|
* @param cb
|
|
67
62
|
*/
|
|
68
|
-
public static create(cb: SubFn):
|
|
69
|
-
const client = new
|
|
70
|
-
|
|
63
|
+
public static create(cb: SubFn): FlatfileListener {
|
|
64
|
+
const client = new FlatfileListener()
|
|
65
|
+
cb(client)
|
|
71
66
|
return client
|
|
72
67
|
}
|
|
73
68
|
|
|
@@ -75,10 +70,9 @@ export class Client extends EventHandler {
|
|
|
75
70
|
* Mount this client using an acceptable Event Driver
|
|
76
71
|
*/
|
|
77
72
|
mount(driver: EventDriver) {
|
|
78
|
-
this.setupFunctions.forEach((f) => f())
|
|
79
73
|
driver.mountEventHandler(this)
|
|
80
74
|
return this
|
|
81
75
|
}
|
|
82
76
|
}
|
|
83
77
|
|
|
84
|
-
type SubFn = (client:
|
|
78
|
+
type SubFn = (client: FlatfileListener) => void
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
import { FlatfileListener } from './flatfile.listener'
|
|
2
|
+
export * from './flatfile.listener'
|
|
2
3
|
export * from './event-drivers'
|
|
3
4
|
export * from './events'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Backwards compatibility
|
|
8
|
+
*/
|
|
9
|
+
export class Client extends FlatfileListener {}
|