@flatfile/listener 0.3.19 → 0.4.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/dist/index.browser.cjs +1355 -0
- package/dist/index.browser.mjs +1343 -0
- package/dist/index.cjs +81479 -7077
- package/dist/index.d.ts +292 -7
- package/dist/index.js +689 -27
- package/dist/index.mjs +81488 -7081
- package/dist/index.mjs.map +1 -0
- package/package.json +14 -6
- package/dist/event-drivers/Browser.d.ts +0 -18
- package/dist/event-drivers/Browser.d.ts.map +0 -1
- package/dist/event-drivers/FlatfileVirtualMachine.d.ts +0 -18
- package/dist/event-drivers/FlatfileVirtualMachine.d.ts.map +0 -1
- package/dist/event-drivers/Webhook.d.ts +0 -1
- package/dist/event-drivers/Webhook.d.ts.map +0 -1
- package/dist/event-drivers/_EventDriver.d.ts +0 -18
- package/dist/event-drivers/_EventDriver.d.ts.map +0 -1
- package/dist/event-drivers/index.d.ts +0 -4
- package/dist/event-drivers/index.d.ts.map +0 -1
- package/dist/events/authenticated.client.d.ts +0 -15
- package/dist/events/authenticated.client.d.ts.map +0 -1
- package/dist/events/cache.d.ts +0 -8
- package/dist/events/cache.d.ts.map +0 -1
- package/dist/events/event.handler.d.ts +0 -91
- package/dist/events/event.handler.d.ts.map +0 -1
- package/dist/events/event.handler.spec.d.ts +0 -2
- package/dist/events/event.handler.spec.d.ts.map +0 -1
- package/dist/events/flatfile.event.d.ts +0 -69
- package/dist/events/flatfile.event.d.ts.map +0 -1
- package/dist/events/glob.match.d.ts +0 -19
- package/dist/events/glob.match.d.ts.map +0 -1
- package/dist/events/glob.match.spec.d.ts +0 -2
- package/dist/events/glob.match.spec.d.ts.map +0 -1
- package/dist/events/index.d.ts +0 -4
- package/dist/events/index.d.ts.map +0 -1
- package/dist/flatfile.listener.d.ts +0 -63
- package/dist/flatfile.listener.d.ts.map +0 -1
- package/dist/flatfile.listener.spec.d.ts +0 -2
- package/dist/flatfile.listener.spec.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/plugins/index.d.ts +0 -1
- package/dist/plugins/index.d.ts.map +0 -1
- package/dist/plugins/useConfigure.d.ts +0 -1
- package/dist/plugins/useConfigure.d.ts.map +0 -1
- package/dist/utils/helpers.d.ts +0 -2
- package/dist/utils/helpers.d.ts.map +0 -1
|
@@ -0,0 +1,1355 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var pako = require('pako');
|
|
6
|
+
var wildMatch = require('wildcard-match');
|
|
7
|
+
var flat = require('flat');
|
|
8
|
+
|
|
9
|
+
// src/index.ts
|
|
10
|
+
var CrossEnvConfig = class {
|
|
11
|
+
/**
|
|
12
|
+
* Get a config value from either the environment or any registry overrides
|
|
13
|
+
* @param prop
|
|
14
|
+
*/
|
|
15
|
+
static get(prop) {
|
|
16
|
+
return this.safeEnvLookup(prop);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Set a value explicitly
|
|
20
|
+
*
|
|
21
|
+
* @param key
|
|
22
|
+
* @param value
|
|
23
|
+
*/
|
|
24
|
+
static set(key, value) {
|
|
25
|
+
return this._overrides.set(key, value);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Alias a key to another key if helpful. This is useful if you have different naming
|
|
29
|
+
* constructs for different environments.
|
|
30
|
+
*
|
|
31
|
+
* @param from
|
|
32
|
+
* @param to
|
|
33
|
+
*/
|
|
34
|
+
static alias(from, to) {
|
|
35
|
+
return this._aliases.set(from, to);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Helpful if you've decided to store settings in another object and want to
|
|
39
|
+
* make that available here. For example in client-side implementations you may reserve
|
|
40
|
+
* a window.FLATFILE_CONFIG object to store settings.
|
|
41
|
+
*
|
|
42
|
+
* @param obj
|
|
43
|
+
*/
|
|
44
|
+
static attachConfigRegistry(obj) {
|
|
45
|
+
this._registry = obj;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Use this to provide an override getter for config values. This is useful
|
|
49
|
+
* if you need to dynamically look up values. Overrides will still take precedence.
|
|
50
|
+
*
|
|
51
|
+
* @param cb
|
|
52
|
+
*/
|
|
53
|
+
static attachConfigFactory(cb) {
|
|
54
|
+
this._factory = cb;
|
|
55
|
+
}
|
|
56
|
+
static reset() {
|
|
57
|
+
this._overrides = /* @__PURE__ */ new Map();
|
|
58
|
+
this._registry = void 0;
|
|
59
|
+
this._factory = void 0;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Internal function for traversing the possible environment sources for a value
|
|
63
|
+
*
|
|
64
|
+
* @param prop
|
|
65
|
+
* @private
|
|
66
|
+
*/
|
|
67
|
+
static safeEnvLookup(prop) {
|
|
68
|
+
let values = [];
|
|
69
|
+
if (this._overrides.get(prop)) {
|
|
70
|
+
values.push(this._overrides.get(prop));
|
|
71
|
+
}
|
|
72
|
+
if (typeof this._registry === "object") {
|
|
73
|
+
values.push(this._registry[prop]);
|
|
74
|
+
}
|
|
75
|
+
if (typeof this._factory === "function") {
|
|
76
|
+
values.push(this._factory(prop));
|
|
77
|
+
}
|
|
78
|
+
if (typeof process === "object" && typeof process.env === "object") {
|
|
79
|
+
values.push(process.env[prop]);
|
|
80
|
+
}
|
|
81
|
+
values.push(...this.checkForBrowserVariables(prop));
|
|
82
|
+
const foundValue = values.find((v) => v !== void 0);
|
|
83
|
+
if (foundValue !== void 0) {
|
|
84
|
+
return foundValue;
|
|
85
|
+
}
|
|
86
|
+
const alias = this._aliases.get(prop);
|
|
87
|
+
if (alias) {
|
|
88
|
+
return this.safeEnvLookup(alias);
|
|
89
|
+
}
|
|
90
|
+
return void 0;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Internal function for checking for stored variables in a browser-like environment
|
|
94
|
+
*
|
|
95
|
+
* @param prop
|
|
96
|
+
* @private
|
|
97
|
+
*/
|
|
98
|
+
static checkForBrowserVariables(prop) {
|
|
99
|
+
let values = [];
|
|
100
|
+
if (typeof window === "object") {
|
|
101
|
+
const windowValue = window[`CROSSENV_${prop}`];
|
|
102
|
+
if (windowValue !== void 0) {
|
|
103
|
+
values.push(windowValue);
|
|
104
|
+
}
|
|
105
|
+
if (typeof sessionStorage === "object") {
|
|
106
|
+
const storedValue = sessionStorage.getItem(`CROSSENV_${prop}`);
|
|
107
|
+
if (storedValue !== null) {
|
|
108
|
+
values.push(storedValue);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return values;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
CrossEnvConfig._overrides = /* @__PURE__ */ new Map();
|
|
116
|
+
/**
|
|
117
|
+
* A map of aliases that can be used to look up config values.
|
|
118
|
+
*
|
|
119
|
+
* @private
|
|
120
|
+
*/
|
|
121
|
+
CrossEnvConfig._aliases = /* @__PURE__ */ new Map();
|
|
122
|
+
|
|
123
|
+
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
|
124
|
+
|
|
125
|
+
function getDefaultExportFromCjs (x) {
|
|
126
|
+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
var browserPonyfill = {exports: {}};
|
|
130
|
+
|
|
131
|
+
(function (module, exports) {
|
|
132
|
+
// Save global object in a variable
|
|
133
|
+
var __global__ =
|
|
134
|
+
(typeof globalThis !== 'undefined' && globalThis) ||
|
|
135
|
+
(typeof self !== 'undefined' && self) ||
|
|
136
|
+
(typeof commonjsGlobal !== 'undefined' && commonjsGlobal);
|
|
137
|
+
// Create an object that extends from __global__ without the fetch function
|
|
138
|
+
var __globalThis__ = (function () {
|
|
139
|
+
function F() {
|
|
140
|
+
this.fetch = false;
|
|
141
|
+
this.DOMException = __global__.DOMException;
|
|
142
|
+
}
|
|
143
|
+
F.prototype = __global__; // Needed for feature detection on whatwg-fetch's code
|
|
144
|
+
return new F();
|
|
145
|
+
})();
|
|
146
|
+
// Wraps whatwg-fetch with a function scope to hijack the global object
|
|
147
|
+
// "globalThis" that's going to be patched
|
|
148
|
+
(function(globalThis) {
|
|
149
|
+
|
|
150
|
+
((function (exports) {
|
|
151
|
+
|
|
152
|
+
var global =
|
|
153
|
+
(typeof globalThis !== 'undefined' && globalThis) ||
|
|
154
|
+
(typeof self !== 'undefined' && self) ||
|
|
155
|
+
(typeof global !== 'undefined' && global);
|
|
156
|
+
|
|
157
|
+
var support = {
|
|
158
|
+
searchParams: 'URLSearchParams' in global,
|
|
159
|
+
iterable: 'Symbol' in global && 'iterator' in Symbol,
|
|
160
|
+
blob:
|
|
161
|
+
'FileReader' in global &&
|
|
162
|
+
'Blob' in global &&
|
|
163
|
+
(function() {
|
|
164
|
+
try {
|
|
165
|
+
new Blob();
|
|
166
|
+
return true
|
|
167
|
+
} catch (e) {
|
|
168
|
+
return false
|
|
169
|
+
}
|
|
170
|
+
})(),
|
|
171
|
+
formData: 'FormData' in global,
|
|
172
|
+
arrayBuffer: 'ArrayBuffer' in global
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
function isDataView(obj) {
|
|
176
|
+
return obj && DataView.prototype.isPrototypeOf(obj)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (support.arrayBuffer) {
|
|
180
|
+
var viewClasses = [
|
|
181
|
+
'[object Int8Array]',
|
|
182
|
+
'[object Uint8Array]',
|
|
183
|
+
'[object Uint8ClampedArray]',
|
|
184
|
+
'[object Int16Array]',
|
|
185
|
+
'[object Uint16Array]',
|
|
186
|
+
'[object Int32Array]',
|
|
187
|
+
'[object Uint32Array]',
|
|
188
|
+
'[object Float32Array]',
|
|
189
|
+
'[object Float64Array]'
|
|
190
|
+
];
|
|
191
|
+
|
|
192
|
+
var isArrayBufferView =
|
|
193
|
+
ArrayBuffer.isView ||
|
|
194
|
+
function(obj) {
|
|
195
|
+
return obj && viewClasses.indexOf(Object.prototype.toString.call(obj)) > -1
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function normalizeName(name) {
|
|
200
|
+
if (typeof name !== 'string') {
|
|
201
|
+
name = String(name);
|
|
202
|
+
}
|
|
203
|
+
if (/[^a-z0-9\-#$%&'*+.^_`|~!]/i.test(name) || name === '') {
|
|
204
|
+
throw new TypeError('Invalid character in header field name: "' + name + '"')
|
|
205
|
+
}
|
|
206
|
+
return name.toLowerCase()
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function normalizeValue(value) {
|
|
210
|
+
if (typeof value !== 'string') {
|
|
211
|
+
value = String(value);
|
|
212
|
+
}
|
|
213
|
+
return value
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Build a destructive iterator for the value list
|
|
217
|
+
function iteratorFor(items) {
|
|
218
|
+
var iterator = {
|
|
219
|
+
next: function() {
|
|
220
|
+
var value = items.shift();
|
|
221
|
+
return {done: value === undefined, value: value}
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
if (support.iterable) {
|
|
226
|
+
iterator[Symbol.iterator] = function() {
|
|
227
|
+
return iterator
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return iterator
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function Headers(headers) {
|
|
235
|
+
this.map = {};
|
|
236
|
+
|
|
237
|
+
if (headers instanceof Headers) {
|
|
238
|
+
headers.forEach(function(value, name) {
|
|
239
|
+
this.append(name, value);
|
|
240
|
+
}, this);
|
|
241
|
+
} else if (Array.isArray(headers)) {
|
|
242
|
+
headers.forEach(function(header) {
|
|
243
|
+
this.append(header[0], header[1]);
|
|
244
|
+
}, this);
|
|
245
|
+
} else if (headers) {
|
|
246
|
+
Object.getOwnPropertyNames(headers).forEach(function(name) {
|
|
247
|
+
this.append(name, headers[name]);
|
|
248
|
+
}, this);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
Headers.prototype.append = function(name, value) {
|
|
253
|
+
name = normalizeName(name);
|
|
254
|
+
value = normalizeValue(value);
|
|
255
|
+
var oldValue = this.map[name];
|
|
256
|
+
this.map[name] = oldValue ? oldValue + ', ' + value : value;
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
Headers.prototype['delete'] = function(name) {
|
|
260
|
+
delete this.map[normalizeName(name)];
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
Headers.prototype.get = function(name) {
|
|
264
|
+
name = normalizeName(name);
|
|
265
|
+
return this.has(name) ? this.map[name] : null
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
Headers.prototype.has = function(name) {
|
|
269
|
+
return this.map.hasOwnProperty(normalizeName(name))
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
Headers.prototype.set = function(name, value) {
|
|
273
|
+
this.map[normalizeName(name)] = normalizeValue(value);
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
Headers.prototype.forEach = function(callback, thisArg) {
|
|
277
|
+
for (var name in this.map) {
|
|
278
|
+
if (this.map.hasOwnProperty(name)) {
|
|
279
|
+
callback.call(thisArg, this.map[name], name, this);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
Headers.prototype.keys = function() {
|
|
285
|
+
var items = [];
|
|
286
|
+
this.forEach(function(value, name) {
|
|
287
|
+
items.push(name);
|
|
288
|
+
});
|
|
289
|
+
return iteratorFor(items)
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
Headers.prototype.values = function() {
|
|
293
|
+
var items = [];
|
|
294
|
+
this.forEach(function(value) {
|
|
295
|
+
items.push(value);
|
|
296
|
+
});
|
|
297
|
+
return iteratorFor(items)
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
Headers.prototype.entries = function() {
|
|
301
|
+
var items = [];
|
|
302
|
+
this.forEach(function(value, name) {
|
|
303
|
+
items.push([name, value]);
|
|
304
|
+
});
|
|
305
|
+
return iteratorFor(items)
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
if (support.iterable) {
|
|
309
|
+
Headers.prototype[Symbol.iterator] = Headers.prototype.entries;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function consumed(body) {
|
|
313
|
+
if (body.bodyUsed) {
|
|
314
|
+
return Promise.reject(new TypeError('Already read'))
|
|
315
|
+
}
|
|
316
|
+
body.bodyUsed = true;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function fileReaderReady(reader) {
|
|
320
|
+
return new Promise(function(resolve, reject) {
|
|
321
|
+
reader.onload = function() {
|
|
322
|
+
resolve(reader.result);
|
|
323
|
+
};
|
|
324
|
+
reader.onerror = function() {
|
|
325
|
+
reject(reader.error);
|
|
326
|
+
};
|
|
327
|
+
})
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
function readBlobAsArrayBuffer(blob) {
|
|
331
|
+
var reader = new FileReader();
|
|
332
|
+
var promise = fileReaderReady(reader);
|
|
333
|
+
reader.readAsArrayBuffer(blob);
|
|
334
|
+
return promise
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function readBlobAsText(blob) {
|
|
338
|
+
var reader = new FileReader();
|
|
339
|
+
var promise = fileReaderReady(reader);
|
|
340
|
+
reader.readAsText(blob);
|
|
341
|
+
return promise
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function readArrayBufferAsText(buf) {
|
|
345
|
+
var view = new Uint8Array(buf);
|
|
346
|
+
var chars = new Array(view.length);
|
|
347
|
+
|
|
348
|
+
for (var i = 0; i < view.length; i++) {
|
|
349
|
+
chars[i] = String.fromCharCode(view[i]);
|
|
350
|
+
}
|
|
351
|
+
return chars.join('')
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function bufferClone(buf) {
|
|
355
|
+
if (buf.slice) {
|
|
356
|
+
return buf.slice(0)
|
|
357
|
+
} else {
|
|
358
|
+
var view = new Uint8Array(buf.byteLength);
|
|
359
|
+
view.set(new Uint8Array(buf));
|
|
360
|
+
return view.buffer
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function Body() {
|
|
365
|
+
this.bodyUsed = false;
|
|
366
|
+
|
|
367
|
+
this._initBody = function(body) {
|
|
368
|
+
/*
|
|
369
|
+
fetch-mock wraps the Response object in an ES6 Proxy to
|
|
370
|
+
provide useful test harness features such as flush. However, on
|
|
371
|
+
ES5 browsers without fetch or Proxy support pollyfills must be used;
|
|
372
|
+
the proxy-pollyfill is unable to proxy an attribute unless it exists
|
|
373
|
+
on the object before the Proxy is created. This change ensures
|
|
374
|
+
Response.bodyUsed exists on the instance, while maintaining the
|
|
375
|
+
semantic of setting Request.bodyUsed in the constructor before
|
|
376
|
+
_initBody is called.
|
|
377
|
+
*/
|
|
378
|
+
this.bodyUsed = this.bodyUsed;
|
|
379
|
+
this._bodyInit = body;
|
|
380
|
+
if (!body) {
|
|
381
|
+
this._bodyText = '';
|
|
382
|
+
} else if (typeof body === 'string') {
|
|
383
|
+
this._bodyText = body;
|
|
384
|
+
} else if (support.blob && Blob.prototype.isPrototypeOf(body)) {
|
|
385
|
+
this._bodyBlob = body;
|
|
386
|
+
} else if (support.formData && FormData.prototype.isPrototypeOf(body)) {
|
|
387
|
+
this._bodyFormData = body;
|
|
388
|
+
} else if (support.searchParams && URLSearchParams.prototype.isPrototypeOf(body)) {
|
|
389
|
+
this._bodyText = body.toString();
|
|
390
|
+
} else if (support.arrayBuffer && support.blob && isDataView(body)) {
|
|
391
|
+
this._bodyArrayBuffer = bufferClone(body.buffer);
|
|
392
|
+
// IE 10-11 can't handle a DataView body.
|
|
393
|
+
this._bodyInit = new Blob([this._bodyArrayBuffer]);
|
|
394
|
+
} else if (support.arrayBuffer && (ArrayBuffer.prototype.isPrototypeOf(body) || isArrayBufferView(body))) {
|
|
395
|
+
this._bodyArrayBuffer = bufferClone(body);
|
|
396
|
+
} else {
|
|
397
|
+
this._bodyText = body = Object.prototype.toString.call(body);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
if (!this.headers.get('content-type')) {
|
|
401
|
+
if (typeof body === 'string') {
|
|
402
|
+
this.headers.set('content-type', 'text/plain;charset=UTF-8');
|
|
403
|
+
} else if (this._bodyBlob && this._bodyBlob.type) {
|
|
404
|
+
this.headers.set('content-type', this._bodyBlob.type);
|
|
405
|
+
} else if (support.searchParams && URLSearchParams.prototype.isPrototypeOf(body)) {
|
|
406
|
+
this.headers.set('content-type', 'application/x-www-form-urlencoded;charset=UTF-8');
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
if (support.blob) {
|
|
412
|
+
this.blob = function() {
|
|
413
|
+
var rejected = consumed(this);
|
|
414
|
+
if (rejected) {
|
|
415
|
+
return rejected
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
if (this._bodyBlob) {
|
|
419
|
+
return Promise.resolve(this._bodyBlob)
|
|
420
|
+
} else if (this._bodyArrayBuffer) {
|
|
421
|
+
return Promise.resolve(new Blob([this._bodyArrayBuffer]))
|
|
422
|
+
} else if (this._bodyFormData) {
|
|
423
|
+
throw new Error('could not read FormData body as blob')
|
|
424
|
+
} else {
|
|
425
|
+
return Promise.resolve(new Blob([this._bodyText]))
|
|
426
|
+
}
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
this.arrayBuffer = function() {
|
|
430
|
+
if (this._bodyArrayBuffer) {
|
|
431
|
+
var isConsumed = consumed(this);
|
|
432
|
+
if (isConsumed) {
|
|
433
|
+
return isConsumed
|
|
434
|
+
}
|
|
435
|
+
if (ArrayBuffer.isView(this._bodyArrayBuffer)) {
|
|
436
|
+
return Promise.resolve(
|
|
437
|
+
this._bodyArrayBuffer.buffer.slice(
|
|
438
|
+
this._bodyArrayBuffer.byteOffset,
|
|
439
|
+
this._bodyArrayBuffer.byteOffset + this._bodyArrayBuffer.byteLength
|
|
440
|
+
)
|
|
441
|
+
)
|
|
442
|
+
} else {
|
|
443
|
+
return Promise.resolve(this._bodyArrayBuffer)
|
|
444
|
+
}
|
|
445
|
+
} else {
|
|
446
|
+
return this.blob().then(readBlobAsArrayBuffer)
|
|
447
|
+
}
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
this.text = function() {
|
|
452
|
+
var rejected = consumed(this);
|
|
453
|
+
if (rejected) {
|
|
454
|
+
return rejected
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
if (this._bodyBlob) {
|
|
458
|
+
return readBlobAsText(this._bodyBlob)
|
|
459
|
+
} else if (this._bodyArrayBuffer) {
|
|
460
|
+
return Promise.resolve(readArrayBufferAsText(this._bodyArrayBuffer))
|
|
461
|
+
} else if (this._bodyFormData) {
|
|
462
|
+
throw new Error('could not read FormData body as text')
|
|
463
|
+
} else {
|
|
464
|
+
return Promise.resolve(this._bodyText)
|
|
465
|
+
}
|
|
466
|
+
};
|
|
467
|
+
|
|
468
|
+
if (support.formData) {
|
|
469
|
+
this.formData = function() {
|
|
470
|
+
return this.text().then(decode)
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
this.json = function() {
|
|
475
|
+
return this.text().then(JSON.parse)
|
|
476
|
+
};
|
|
477
|
+
|
|
478
|
+
return this
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
// HTTP methods whose capitalization should be normalized
|
|
482
|
+
var methods = ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'POST', 'PUT'];
|
|
483
|
+
|
|
484
|
+
function normalizeMethod(method) {
|
|
485
|
+
var upcased = method.toUpperCase();
|
|
486
|
+
return methods.indexOf(upcased) > -1 ? upcased : method
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
function Request(input, options) {
|
|
490
|
+
if (!(this instanceof Request)) {
|
|
491
|
+
throw new TypeError('Please use the "new" operator, this DOM object constructor cannot be called as a function.')
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
options = options || {};
|
|
495
|
+
var body = options.body;
|
|
496
|
+
|
|
497
|
+
if (input instanceof Request) {
|
|
498
|
+
if (input.bodyUsed) {
|
|
499
|
+
throw new TypeError('Already read')
|
|
500
|
+
}
|
|
501
|
+
this.url = input.url;
|
|
502
|
+
this.credentials = input.credentials;
|
|
503
|
+
if (!options.headers) {
|
|
504
|
+
this.headers = new Headers(input.headers);
|
|
505
|
+
}
|
|
506
|
+
this.method = input.method;
|
|
507
|
+
this.mode = input.mode;
|
|
508
|
+
this.signal = input.signal;
|
|
509
|
+
if (!body && input._bodyInit != null) {
|
|
510
|
+
body = input._bodyInit;
|
|
511
|
+
input.bodyUsed = true;
|
|
512
|
+
}
|
|
513
|
+
} else {
|
|
514
|
+
this.url = String(input);
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
this.credentials = options.credentials || this.credentials || 'same-origin';
|
|
518
|
+
if (options.headers || !this.headers) {
|
|
519
|
+
this.headers = new Headers(options.headers);
|
|
520
|
+
}
|
|
521
|
+
this.method = normalizeMethod(options.method || this.method || 'GET');
|
|
522
|
+
this.mode = options.mode || this.mode || null;
|
|
523
|
+
this.signal = options.signal || this.signal;
|
|
524
|
+
this.referrer = null;
|
|
525
|
+
|
|
526
|
+
if ((this.method === 'GET' || this.method === 'HEAD') && body) {
|
|
527
|
+
throw new TypeError('Body not allowed for GET or HEAD requests')
|
|
528
|
+
}
|
|
529
|
+
this._initBody(body);
|
|
530
|
+
|
|
531
|
+
if (this.method === 'GET' || this.method === 'HEAD') {
|
|
532
|
+
if (options.cache === 'no-store' || options.cache === 'no-cache') {
|
|
533
|
+
// Search for a '_' parameter in the query string
|
|
534
|
+
var reParamSearch = /([?&])_=[^&]*/;
|
|
535
|
+
if (reParamSearch.test(this.url)) {
|
|
536
|
+
// If it already exists then set the value with the current time
|
|
537
|
+
this.url = this.url.replace(reParamSearch, '$1_=' + new Date().getTime());
|
|
538
|
+
} else {
|
|
539
|
+
// Otherwise add a new '_' parameter to the end with the current time
|
|
540
|
+
var reQueryString = /\?/;
|
|
541
|
+
this.url += (reQueryString.test(this.url) ? '&' : '?') + '_=' + new Date().getTime();
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
Request.prototype.clone = function() {
|
|
548
|
+
return new Request(this, {body: this._bodyInit})
|
|
549
|
+
};
|
|
550
|
+
|
|
551
|
+
function decode(body) {
|
|
552
|
+
var form = new FormData();
|
|
553
|
+
body
|
|
554
|
+
.trim()
|
|
555
|
+
.split('&')
|
|
556
|
+
.forEach(function(bytes) {
|
|
557
|
+
if (bytes) {
|
|
558
|
+
var split = bytes.split('=');
|
|
559
|
+
var name = split.shift().replace(/\+/g, ' ');
|
|
560
|
+
var value = split.join('=').replace(/\+/g, ' ');
|
|
561
|
+
form.append(decodeURIComponent(name), decodeURIComponent(value));
|
|
562
|
+
}
|
|
563
|
+
});
|
|
564
|
+
return form
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
function parseHeaders(rawHeaders) {
|
|
568
|
+
var headers = new Headers();
|
|
569
|
+
// Replace instances of \r\n and \n followed by at least one space or horizontal tab with a space
|
|
570
|
+
// https://tools.ietf.org/html/rfc7230#section-3.2
|
|
571
|
+
var preProcessedHeaders = rawHeaders.replace(/\r?\n[\t ]+/g, ' ');
|
|
572
|
+
// Avoiding split via regex to work around a common IE11 bug with the core-js 3.6.0 regex polyfill
|
|
573
|
+
// https://github.com/github/fetch/issues/748
|
|
574
|
+
// https://github.com/zloirock/core-js/issues/751
|
|
575
|
+
preProcessedHeaders
|
|
576
|
+
.split('\r')
|
|
577
|
+
.map(function(header) {
|
|
578
|
+
return header.indexOf('\n') === 0 ? header.substr(1, header.length) : header
|
|
579
|
+
})
|
|
580
|
+
.forEach(function(line) {
|
|
581
|
+
var parts = line.split(':');
|
|
582
|
+
var key = parts.shift().trim();
|
|
583
|
+
if (key) {
|
|
584
|
+
var value = parts.join(':').trim();
|
|
585
|
+
headers.append(key, value);
|
|
586
|
+
}
|
|
587
|
+
});
|
|
588
|
+
return headers
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
Body.call(Request.prototype);
|
|
592
|
+
|
|
593
|
+
function Response(bodyInit, options) {
|
|
594
|
+
if (!(this instanceof Response)) {
|
|
595
|
+
throw new TypeError('Please use the "new" operator, this DOM object constructor cannot be called as a function.')
|
|
596
|
+
}
|
|
597
|
+
if (!options) {
|
|
598
|
+
options = {};
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
this.type = 'default';
|
|
602
|
+
this.status = options.status === undefined ? 200 : options.status;
|
|
603
|
+
this.ok = this.status >= 200 && this.status < 300;
|
|
604
|
+
this.statusText = options.statusText === undefined ? '' : '' + options.statusText;
|
|
605
|
+
this.headers = new Headers(options.headers);
|
|
606
|
+
this.url = options.url || '';
|
|
607
|
+
this._initBody(bodyInit);
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
Body.call(Response.prototype);
|
|
611
|
+
|
|
612
|
+
Response.prototype.clone = function() {
|
|
613
|
+
return new Response(this._bodyInit, {
|
|
614
|
+
status: this.status,
|
|
615
|
+
statusText: this.statusText,
|
|
616
|
+
headers: new Headers(this.headers),
|
|
617
|
+
url: this.url
|
|
618
|
+
})
|
|
619
|
+
};
|
|
620
|
+
|
|
621
|
+
Response.error = function() {
|
|
622
|
+
var response = new Response(null, {status: 0, statusText: ''});
|
|
623
|
+
response.type = 'error';
|
|
624
|
+
return response
|
|
625
|
+
};
|
|
626
|
+
|
|
627
|
+
var redirectStatuses = [301, 302, 303, 307, 308];
|
|
628
|
+
|
|
629
|
+
Response.redirect = function(url, status) {
|
|
630
|
+
if (redirectStatuses.indexOf(status) === -1) {
|
|
631
|
+
throw new RangeError('Invalid status code')
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
return new Response(null, {status: status, headers: {location: url}})
|
|
635
|
+
};
|
|
636
|
+
|
|
637
|
+
exports.DOMException = global.DOMException;
|
|
638
|
+
try {
|
|
639
|
+
new exports.DOMException();
|
|
640
|
+
} catch (err) {
|
|
641
|
+
exports.DOMException = function(message, name) {
|
|
642
|
+
this.message = message;
|
|
643
|
+
this.name = name;
|
|
644
|
+
var error = Error(message);
|
|
645
|
+
this.stack = error.stack;
|
|
646
|
+
};
|
|
647
|
+
exports.DOMException.prototype = Object.create(Error.prototype);
|
|
648
|
+
exports.DOMException.prototype.constructor = exports.DOMException;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
function fetch(input, init) {
|
|
652
|
+
return new Promise(function(resolve, reject) {
|
|
653
|
+
var request = new Request(input, init);
|
|
654
|
+
|
|
655
|
+
if (request.signal && request.signal.aborted) {
|
|
656
|
+
return reject(new exports.DOMException('Aborted', 'AbortError'))
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
var xhr = new XMLHttpRequest();
|
|
660
|
+
|
|
661
|
+
function abortXhr() {
|
|
662
|
+
xhr.abort();
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
xhr.onload = function() {
|
|
666
|
+
var options = {
|
|
667
|
+
status: xhr.status,
|
|
668
|
+
statusText: xhr.statusText,
|
|
669
|
+
headers: parseHeaders(xhr.getAllResponseHeaders() || '')
|
|
670
|
+
};
|
|
671
|
+
options.url = 'responseURL' in xhr ? xhr.responseURL : options.headers.get('X-Request-URL');
|
|
672
|
+
var body = 'response' in xhr ? xhr.response : xhr.responseText;
|
|
673
|
+
setTimeout(function() {
|
|
674
|
+
resolve(new Response(body, options));
|
|
675
|
+
}, 0);
|
|
676
|
+
};
|
|
677
|
+
|
|
678
|
+
xhr.onerror = function() {
|
|
679
|
+
setTimeout(function() {
|
|
680
|
+
reject(new TypeError('Network request failed'));
|
|
681
|
+
}, 0);
|
|
682
|
+
};
|
|
683
|
+
|
|
684
|
+
xhr.ontimeout = function() {
|
|
685
|
+
setTimeout(function() {
|
|
686
|
+
reject(new TypeError('Network request failed'));
|
|
687
|
+
}, 0);
|
|
688
|
+
};
|
|
689
|
+
|
|
690
|
+
xhr.onabort = function() {
|
|
691
|
+
setTimeout(function() {
|
|
692
|
+
reject(new exports.DOMException('Aborted', 'AbortError'));
|
|
693
|
+
}, 0);
|
|
694
|
+
};
|
|
695
|
+
|
|
696
|
+
function fixUrl(url) {
|
|
697
|
+
try {
|
|
698
|
+
return url === '' && global.location.href ? global.location.href : url
|
|
699
|
+
} catch (e) {
|
|
700
|
+
return url
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
xhr.open(request.method, fixUrl(request.url), true);
|
|
705
|
+
|
|
706
|
+
if (request.credentials === 'include') {
|
|
707
|
+
xhr.withCredentials = true;
|
|
708
|
+
} else if (request.credentials === 'omit') {
|
|
709
|
+
xhr.withCredentials = false;
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
if ('responseType' in xhr) {
|
|
713
|
+
if (support.blob) {
|
|
714
|
+
xhr.responseType = 'blob';
|
|
715
|
+
} else if (
|
|
716
|
+
support.arrayBuffer &&
|
|
717
|
+
request.headers.get('Content-Type') &&
|
|
718
|
+
request.headers.get('Content-Type').indexOf('application/octet-stream') !== -1
|
|
719
|
+
) {
|
|
720
|
+
xhr.responseType = 'arraybuffer';
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
if (init && typeof init.headers === 'object' && !(init.headers instanceof Headers)) {
|
|
725
|
+
Object.getOwnPropertyNames(init.headers).forEach(function(name) {
|
|
726
|
+
xhr.setRequestHeader(name, normalizeValue(init.headers[name]));
|
|
727
|
+
});
|
|
728
|
+
} else {
|
|
729
|
+
request.headers.forEach(function(value, name) {
|
|
730
|
+
xhr.setRequestHeader(name, value);
|
|
731
|
+
});
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
if (request.signal) {
|
|
735
|
+
request.signal.addEventListener('abort', abortXhr);
|
|
736
|
+
|
|
737
|
+
xhr.onreadystatechange = function() {
|
|
738
|
+
// DONE (success or failure)
|
|
739
|
+
if (xhr.readyState === 4) {
|
|
740
|
+
request.signal.removeEventListener('abort', abortXhr);
|
|
741
|
+
}
|
|
742
|
+
};
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
xhr.send(typeof request._bodyInit === 'undefined' ? null : request._bodyInit);
|
|
746
|
+
})
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
fetch.polyfill = true;
|
|
750
|
+
|
|
751
|
+
if (!global.fetch) {
|
|
752
|
+
global.fetch = fetch;
|
|
753
|
+
global.Headers = Headers;
|
|
754
|
+
global.Request = Request;
|
|
755
|
+
global.Response = Response;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
exports.Headers = Headers;
|
|
759
|
+
exports.Request = Request;
|
|
760
|
+
exports.Response = Response;
|
|
761
|
+
exports.fetch = fetch;
|
|
762
|
+
|
|
763
|
+
return exports;
|
|
764
|
+
|
|
765
|
+
}))({});
|
|
766
|
+
})(__globalThis__);
|
|
767
|
+
// This is a ponyfill, so...
|
|
768
|
+
__globalThis__.fetch.ponyfill = true;
|
|
769
|
+
delete __globalThis__.fetch.polyfill;
|
|
770
|
+
// Choose between native implementation (__global__) or custom implementation (__globalThis__)
|
|
771
|
+
var ctx = __global__.fetch ? __global__ : __globalThis__;
|
|
772
|
+
exports = ctx.fetch; // To enable: import fetch from 'cross-fetch'
|
|
773
|
+
exports.default = ctx.fetch; // For TypeScript consumers without esModuleInterop.
|
|
774
|
+
exports.fetch = ctx.fetch; // To enable: import {fetch} from 'cross-fetch'
|
|
775
|
+
exports.Headers = ctx.Headers;
|
|
776
|
+
exports.Request = ctx.Request;
|
|
777
|
+
exports.Response = ctx.Response;
|
|
778
|
+
module.exports = exports;
|
|
779
|
+
} (browserPonyfill, browserPonyfill.exports));
|
|
780
|
+
|
|
781
|
+
var browserPonyfillExports = browserPonyfill.exports;
|
|
782
|
+
var fetch = /*@__PURE__*/getDefaultExportFromCjs(browserPonyfillExports);
|
|
783
|
+
|
|
784
|
+
const ensureSingleTrailingSlash = (url) => {
|
|
785
|
+
// Remove all trailing slashes
|
|
786
|
+
while (url.endsWith('/')) {
|
|
787
|
+
url = url.slice(0, -1);
|
|
788
|
+
}
|
|
789
|
+
// Append one slash
|
|
790
|
+
return url + '/';
|
|
791
|
+
};
|
|
792
|
+
|
|
793
|
+
class AuthenticatedClient {
|
|
794
|
+
constructor(accessToken, apiUrl) {
|
|
795
|
+
const FLATFILE_API_URL = CrossEnvConfig.get('AGENT_INTERNAL_URL') || 'http://localhost:3000';
|
|
796
|
+
const bearerToken = CrossEnvConfig.get('FLATFILE_BEARER_TOKEN');
|
|
797
|
+
this._accessToken = accessToken || bearerToken || '...';
|
|
798
|
+
this._apiUrl =
|
|
799
|
+
apiUrl || FLATFILE_API_URL
|
|
800
|
+
? ensureSingleTrailingSlash(apiUrl || FLATFILE_API_URL)
|
|
801
|
+
: undefined;
|
|
802
|
+
}
|
|
803
|
+
async fetch(url, options) {
|
|
804
|
+
const headers = {
|
|
805
|
+
Authorization: `Bearer ${this._accessToken}`,
|
|
806
|
+
'x-disable-hooks': 'true',
|
|
807
|
+
'Content-Type': 'application/json',
|
|
808
|
+
...options?.headers,
|
|
809
|
+
};
|
|
810
|
+
const fetchUrl = this._apiUrl + url;
|
|
811
|
+
const fetchOptions = {
|
|
812
|
+
method: options?.method || 'GET',
|
|
813
|
+
headers,
|
|
814
|
+
body: options?.data,
|
|
815
|
+
};
|
|
816
|
+
try {
|
|
817
|
+
const response = await fetch(fetchUrl, fetchOptions);
|
|
818
|
+
if (response.status >= 200 && response.status <= 399) {
|
|
819
|
+
const contentType = response.headers.get('content-type');
|
|
820
|
+
if (contentType && contentType.includes('application/json')) {
|
|
821
|
+
const responseData = (await response.json());
|
|
822
|
+
return responseData.data;
|
|
823
|
+
}
|
|
824
|
+
else {
|
|
825
|
+
const responseData = await response.text();
|
|
826
|
+
return responseData;
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
else {
|
|
830
|
+
throw new Error(`HTTP error! Status: ${response.status}`);
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
catch (err) {
|
|
834
|
+
console.log('event.fetch error: ', err);
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
/**
|
|
838
|
+
*
|
|
839
|
+
* @deprecated use @flatfile/cross-env-config instead
|
|
840
|
+
*/
|
|
841
|
+
setVariables({ accessToken, apiUrl, }) {
|
|
842
|
+
this._accessToken = accessToken;
|
|
843
|
+
this._apiUrl = apiUrl;
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
class EventCache {
|
|
848
|
+
constructor() {
|
|
849
|
+
this.eventCache = new Map();
|
|
850
|
+
}
|
|
851
|
+
async init(key, callback) {
|
|
852
|
+
if (this.eventCache.get(key)) {
|
|
853
|
+
return this.eventCache.get(key);
|
|
854
|
+
}
|
|
855
|
+
else {
|
|
856
|
+
const result = await callback();
|
|
857
|
+
this.eventCache.set(key, result);
|
|
858
|
+
return result;
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
async set(key, callback) {
|
|
862
|
+
if (this.eventCache.get(key)) {
|
|
863
|
+
const result = await callback();
|
|
864
|
+
this.eventCache.set(key, result);
|
|
865
|
+
return result;
|
|
866
|
+
}
|
|
867
|
+
else {
|
|
868
|
+
throw new Error('Cache key not found');
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
get(key) {
|
|
872
|
+
if (this.eventCache.get(key)) {
|
|
873
|
+
return this.eventCache.get(key);
|
|
874
|
+
}
|
|
875
|
+
else {
|
|
876
|
+
throw new Error('Cache key not found');
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
delete(key) {
|
|
880
|
+
if (!key) {
|
|
881
|
+
this.eventCache.clear();
|
|
882
|
+
}
|
|
883
|
+
else if (this.eventCache.get(key)) {
|
|
884
|
+
if (Array.isArray(key)) {
|
|
885
|
+
key.forEach((k) => this.eventCache.delete(k));
|
|
886
|
+
}
|
|
887
|
+
else {
|
|
888
|
+
this.eventCache.delete(key);
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
else {
|
|
892
|
+
throw new Error('Cache key not found');
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
class FlatfileEvent extends AuthenticatedClient {
|
|
898
|
+
constructor(src, accessToken, apiUrl) {
|
|
899
|
+
super(accessToken, apiUrl);
|
|
900
|
+
this.src = src;
|
|
901
|
+
this.afterAllCallbacks = new Map();
|
|
902
|
+
this.cache = new EventCache();
|
|
903
|
+
this.domain = src.domain;
|
|
904
|
+
this.topic = src.topic;
|
|
905
|
+
this.context = src.context; // -> [us0_acc_ihjh8943h9w, space_id, workbook_id]
|
|
906
|
+
this.payload = src.payload;
|
|
907
|
+
this.target = src.target || '';
|
|
908
|
+
this.origin = src.origin || {};
|
|
909
|
+
this.action = src.context?.actionName || '';
|
|
910
|
+
this.namespace = src.namespaces || [];
|
|
911
|
+
this.createdAt = src.createdAt || undefined;
|
|
912
|
+
const data = async (options) => this.fetchData(options);
|
|
913
|
+
data.then = (onfulfilled, onrejected) => {
|
|
914
|
+
return this.data().then(onfulfilled, onrejected);
|
|
915
|
+
};
|
|
916
|
+
this.data = data;
|
|
917
|
+
}
|
|
918
|
+
/**
|
|
919
|
+
* Should return either event body if expanded already or fetch data from the
|
|
920
|
+
* signed dataURL
|
|
921
|
+
*/
|
|
922
|
+
async fetchData(options) {
|
|
923
|
+
const dataUrl = new URLSearchParams(this.src.dataUrl);
|
|
924
|
+
if (options) {
|
|
925
|
+
for (const [key, values] of Object.entries(options)) {
|
|
926
|
+
if (Array.isArray(values)) {
|
|
927
|
+
for (const value of values) {
|
|
928
|
+
dataUrl.append(key, value);
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
else {
|
|
932
|
+
dataUrl.append(key, values);
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
const decodedURL = decodeURIComponent(dataUrl.toString());
|
|
937
|
+
if (decodedURL) {
|
|
938
|
+
return this.fetch(decodedURL);
|
|
939
|
+
}
|
|
940
|
+
else {
|
|
941
|
+
return this.payload;
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
afterAll(callback, cacheKey) {
|
|
945
|
+
const key = cacheKey || callback.toString();
|
|
946
|
+
if (!this.afterAllCallbacks.get(key)) {
|
|
947
|
+
this.afterAllCallbacks.set(key, callback);
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
async update(records, compressRequestBody = true) {
|
|
951
|
+
if (!this.src.dataUrl) {
|
|
952
|
+
throw new Error('Cannot set data on an event without a dataUrl');
|
|
953
|
+
}
|
|
954
|
+
// TODO: do we need to remove source from the messages array?
|
|
955
|
+
records.map((record) => {
|
|
956
|
+
record.messages?.map((message) => {
|
|
957
|
+
delete message.source;
|
|
958
|
+
});
|
|
959
|
+
});
|
|
960
|
+
const data = compressRequestBody
|
|
961
|
+
? pako.gzip(JSON.stringify(records))
|
|
962
|
+
: records;
|
|
963
|
+
const headers = compressRequestBody
|
|
964
|
+
? { 'Content-Encoding': 'gzip', 'Content-Length': data.length.toString() }
|
|
965
|
+
: {};
|
|
966
|
+
await this.fetch(this.src.dataUrl, {
|
|
967
|
+
method: 'PUT',
|
|
968
|
+
headers: headers,
|
|
969
|
+
data,
|
|
970
|
+
});
|
|
971
|
+
}
|
|
972
|
+
/**
|
|
973
|
+
* Fetch the Secrets as indicated by this event context
|
|
974
|
+
*
|
|
975
|
+
* @param key - The name of the secret to fetch
|
|
976
|
+
* @param options - (Optional) environmentId and spaceId to override event context
|
|
977
|
+
*
|
|
978
|
+
* @returns The value of the secret (usually a credential or token)
|
|
979
|
+
*/
|
|
980
|
+
async secrets(key, options) {
|
|
981
|
+
// Allow options overrides, then take from context, else are absent
|
|
982
|
+
const environmentId = options?.environmentId || this.context.environmentId || '';
|
|
983
|
+
const spaceId = options?.spaceId || this.context.spaceId || '';
|
|
984
|
+
if (!environmentId) {
|
|
985
|
+
throw new Error('environmentId is required to fetch secrets');
|
|
986
|
+
}
|
|
987
|
+
let getSecrets = `v1/secrets?environmentId=${environmentId}`;
|
|
988
|
+
if (spaceId) {
|
|
989
|
+
getSecrets += `&spaceId=${spaceId}`;
|
|
990
|
+
}
|
|
991
|
+
const secretCacheKey = `secrets:${environmentId}${spaceId && `:${spaceId}`}`;
|
|
992
|
+
const secrets = await this.cache.init(secretCacheKey, async () => {
|
|
993
|
+
const secretsResponse = await this.fetch(getSecrets);
|
|
994
|
+
const SecretMap = new Map();
|
|
995
|
+
secretsResponse?.forEach((secret) => {
|
|
996
|
+
SecretMap.set(secret.name, secret.value);
|
|
997
|
+
});
|
|
998
|
+
return SecretMap;
|
|
999
|
+
});
|
|
1000
|
+
const value = secrets.get(key);
|
|
1001
|
+
if (!value) {
|
|
1002
|
+
throw new Error(`Secret ${key} not found`);
|
|
1003
|
+
}
|
|
1004
|
+
return value;
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
/**
|
|
1009
|
+
* Glob style matching of a value
|
|
1010
|
+
*
|
|
1011
|
+
* @param val
|
|
1012
|
+
* @param filter
|
|
1013
|
+
*/
|
|
1014
|
+
function glob(val, filter) {
|
|
1015
|
+
if (!val || typeof val !== 'string') {
|
|
1016
|
+
return false;
|
|
1017
|
+
}
|
|
1018
|
+
return wildMatch(filter || '**', ':')(val);
|
|
1019
|
+
}
|
|
1020
|
+
/**
|
|
1021
|
+
* Glob style matching of values in an object
|
|
1022
|
+
*
|
|
1023
|
+
* @param object
|
|
1024
|
+
* @param filterObject
|
|
1025
|
+
*/
|
|
1026
|
+
function objectMatches(object, filterObject) {
|
|
1027
|
+
const cleanFilter = !filterObject || typeof filterObject !== 'object'
|
|
1028
|
+
? { '**': filterObject }
|
|
1029
|
+
: filterObject;
|
|
1030
|
+
if (typeof object !== 'object') {
|
|
1031
|
+
throw new Error('You cannot filter a non-object');
|
|
1032
|
+
}
|
|
1033
|
+
let denied = false;
|
|
1034
|
+
const filter = flat(cleanFilter, { safe: true });
|
|
1035
|
+
const flattened = flat(object, { safe: true });
|
|
1036
|
+
// all filters MUST resolve true
|
|
1037
|
+
for (const keyPattern in filter) {
|
|
1038
|
+
const keys = filterKeys(flattened, keyPattern);
|
|
1039
|
+
const valuePattern = (Array.isArray(filter[keyPattern])
|
|
1040
|
+
? filter[keyPattern]
|
|
1041
|
+
: [filter[keyPattern]]);
|
|
1042
|
+
// only one filter must match
|
|
1043
|
+
denied || (denied = !keys.some((key) => {
|
|
1044
|
+
const value = flattened[key];
|
|
1045
|
+
return valuePattern.some((match) => globOrMatch(value, match));
|
|
1046
|
+
}));
|
|
1047
|
+
}
|
|
1048
|
+
return !denied;
|
|
1049
|
+
}
|
|
1050
|
+
/**
|
|
1051
|
+
* Glob keys of an object and return the narrowed set
|
|
1052
|
+
*
|
|
1053
|
+
* @param object
|
|
1054
|
+
* @param glob
|
|
1055
|
+
*/
|
|
1056
|
+
function filterKeys(object, glob) {
|
|
1057
|
+
glob = glob.includes('*') || glob.includes('.') ? glob : `**.${glob}`;
|
|
1058
|
+
const matcher = wildMatch(glob, '.');
|
|
1059
|
+
return Object.keys(object).filter((key) => matcher(key));
|
|
1060
|
+
}
|
|
1061
|
+
function globOrMatch(val, filter) {
|
|
1062
|
+
if (val === undefined || val === null) {
|
|
1063
|
+
return filter === null;
|
|
1064
|
+
}
|
|
1065
|
+
if (Array.isArray(val)) {
|
|
1066
|
+
return val.some((v) => globOrMatch(v, filter));
|
|
1067
|
+
}
|
|
1068
|
+
if (typeof filter === 'string') {
|
|
1069
|
+
return glob(val.toString(), filter);
|
|
1070
|
+
}
|
|
1071
|
+
// otherwise do a simple comparison
|
|
1072
|
+
return val === filter;
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
/**
|
|
1076
|
+
* EventHandler is a Flatfile flavored implementation of EventTarget
|
|
1077
|
+
*/
|
|
1078
|
+
class EventHandler extends AuthenticatedClient {
|
|
1079
|
+
constructor(filter, accessToken, apiUrl) {
|
|
1080
|
+
super(accessToken, apiUrl);
|
|
1081
|
+
/**
|
|
1082
|
+
* Cache of registered listeners on this instance
|
|
1083
|
+
* @private
|
|
1084
|
+
*/
|
|
1085
|
+
this.listeners = [];
|
|
1086
|
+
/**
|
|
1087
|
+
* Cache of registered child nodes for this listener. These nodes will
|
|
1088
|
+
* only receive events that pass the parent filter.
|
|
1089
|
+
*
|
|
1090
|
+
* @private
|
|
1091
|
+
*/
|
|
1092
|
+
this.nodes = [];
|
|
1093
|
+
if (filter) {
|
|
1094
|
+
this.filterQuery = filter;
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
on(query, ...rest) {
|
|
1098
|
+
let filter = {};
|
|
1099
|
+
const callback = rest.pop();
|
|
1100
|
+
if (rest.length) {
|
|
1101
|
+
filter = rest.shift();
|
|
1102
|
+
}
|
|
1103
|
+
this.listeners.push([query, filter, callback]);
|
|
1104
|
+
return this;
|
|
1105
|
+
}
|
|
1106
|
+
/**
|
|
1107
|
+
* Add child nodes to send this event to as well
|
|
1108
|
+
*
|
|
1109
|
+
* @param node
|
|
1110
|
+
*/
|
|
1111
|
+
addNode(node) {
|
|
1112
|
+
this.nodes.push(node);
|
|
1113
|
+
return this;
|
|
1114
|
+
}
|
|
1115
|
+
/**
|
|
1116
|
+
* Dispatch an event and resolve the promise once it has completed (or
|
|
1117
|
+
* errored
|
|
1118
|
+
*
|
|
1119
|
+
* @todo - is there a right order in which to resolve event listeners?
|
|
1120
|
+
* Should it matter?
|
|
1121
|
+
*
|
|
1122
|
+
* @param event
|
|
1123
|
+
*/
|
|
1124
|
+
async dispatchEvent(event) {
|
|
1125
|
+
if (!event)
|
|
1126
|
+
return;
|
|
1127
|
+
const eventPayload = event.src ? event.src : event;
|
|
1128
|
+
event = new FlatfileEvent(eventPayload, this._accessToken, this._apiUrl);
|
|
1129
|
+
await this.trigger(event, true);
|
|
1130
|
+
for (const [_key, cb] of event.afterAllCallbacks) {
|
|
1131
|
+
await cb(event);
|
|
1132
|
+
}
|
|
1133
|
+
event.cache.delete();
|
|
1134
|
+
}
|
|
1135
|
+
/**
|
|
1136
|
+
* @deprecated legacy shim for receiving events from the VM layer
|
|
1137
|
+
* @alias dispatchEvent
|
|
1138
|
+
* @param event
|
|
1139
|
+
*/
|
|
1140
|
+
async routeEvent(event) {
|
|
1141
|
+
return this.dispatchEvent(event);
|
|
1142
|
+
}
|
|
1143
|
+
/**
|
|
1144
|
+
* Actually trigger the event listeners on this particular target
|
|
1145
|
+
*
|
|
1146
|
+
* @note It is safer for now to run this in series to avoid IO locks and
|
|
1147
|
+
* potential race conditions and uncaught errors
|
|
1148
|
+
*
|
|
1149
|
+
* @param event
|
|
1150
|
+
* @param recursive
|
|
1151
|
+
*/
|
|
1152
|
+
async trigger(event, recursive = false) {
|
|
1153
|
+
const listeners = this.getListeners(event, recursive);
|
|
1154
|
+
for (const cb of listeners) {
|
|
1155
|
+
await cb.callback(event);
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
/**
|
|
1159
|
+
* Get any listeners from this target subscribing to this event
|
|
1160
|
+
*
|
|
1161
|
+
* @param event
|
|
1162
|
+
* @param recursive
|
|
1163
|
+
*/
|
|
1164
|
+
getListeners(event, recursive = false) {
|
|
1165
|
+
// never return any listeners if the event doesn't match the filter
|
|
1166
|
+
// event recursion should not occur either
|
|
1167
|
+
if (!this.matchEvent(event, this.filterQuery)) {
|
|
1168
|
+
return [];
|
|
1169
|
+
}
|
|
1170
|
+
// look at listeners registered here
|
|
1171
|
+
const listeners = this.listeners
|
|
1172
|
+
.filter(([query, filter]) => {
|
|
1173
|
+
const globbed = glob(event.topic, query);
|
|
1174
|
+
const matched = this.matchEvent(event, filter);
|
|
1175
|
+
return globbed && matched;
|
|
1176
|
+
})
|
|
1177
|
+
.map(([query, filter, callback]) => ({ query, filter, callback }));
|
|
1178
|
+
return !recursive
|
|
1179
|
+
? listeners
|
|
1180
|
+
: [
|
|
1181
|
+
...listeners,
|
|
1182
|
+
...this.nodes.flatMap((n) => n.getListeners(event, true)),
|
|
1183
|
+
];
|
|
1184
|
+
}
|
|
1185
|
+
/**
|
|
1186
|
+
* Attach more event listeners using a callback function. Used most
|
|
1187
|
+
* frequently for plugins.
|
|
1188
|
+
*
|
|
1189
|
+
* @param fn
|
|
1190
|
+
*/
|
|
1191
|
+
use(fn) {
|
|
1192
|
+
fn(this);
|
|
1193
|
+
return this;
|
|
1194
|
+
}
|
|
1195
|
+
/**
|
|
1196
|
+
* Filter an event out based on glob filter object
|
|
1197
|
+
*
|
|
1198
|
+
* @param event
|
|
1199
|
+
* @param filter
|
|
1200
|
+
*/
|
|
1201
|
+
matchEvent(event, filter) {
|
|
1202
|
+
return filter ? objectMatches(event, filter) : true;
|
|
1203
|
+
}
|
|
1204
|
+
}
|
|
1205
|
+
|
|
1206
|
+
/**
|
|
1207
|
+
* The Flatfile Listener
|
|
1208
|
+
*
|
|
1209
|
+
* The Flatfile PubSub Client is just a simple event subscriber. It can
|
|
1210
|
+
* receive events from any PubSub driver. The default drivers are:
|
|
1211
|
+
*
|
|
1212
|
+
* - Webhook (for simply processing events sent to URL)
|
|
1213
|
+
* - Websocket (for subscribing real time on an HTTP2 connection)
|
|
1214
|
+
* - Serverless (for stateless invocations via AWS Lambda or similar)
|
|
1215
|
+
*
|
|
1216
|
+
* Once an event is received, it is routed to any awaiting listeners which
|
|
1217
|
+
* are added with `addEventListener()` or its alias `on()`.
|
|
1218
|
+
*
|
|
1219
|
+
* Flatfile events follow a standard structure and event listeners can use
|
|
1220
|
+
* any of the following syntaxes to react to events within Flatfile.
|
|
1221
|
+
*
|
|
1222
|
+
* // listen to an event
|
|
1223
|
+
* addEventListener('entity:topic')
|
|
1224
|
+
*
|
|
1225
|
+
* // listen to an event on a specific namespace
|
|
1226
|
+
* addEventListener('entity:topic@namespace')
|
|
1227
|
+
*
|
|
1228
|
+
* // listen to a specific context on a namespace
|
|
1229
|
+
* addEventListener('entity:topic@namespace?context=us_sp_89234oihsdo')
|
|
1230
|
+
*
|
|
1231
|
+
* // filter by any
|
|
1232
|
+
* addEventListener('entity:topic@namespace?')
|
|
1233
|
+
*
|
|
1234
|
+
*/
|
|
1235
|
+
class FlatfileListener extends EventHandler {
|
|
1236
|
+
/**
|
|
1237
|
+
* Subscribe to events only within a certain namespace.
|
|
1238
|
+
*
|
|
1239
|
+
* @param namespace
|
|
1240
|
+
* @param cb
|
|
1241
|
+
*/
|
|
1242
|
+
namespace(namespaces, cb) {
|
|
1243
|
+
return this.filter({ namespaces }, cb);
|
|
1244
|
+
}
|
|
1245
|
+
/**
|
|
1246
|
+
* Filter by namespace
|
|
1247
|
+
*
|
|
1248
|
+
* @param filter
|
|
1249
|
+
* @param cb
|
|
1250
|
+
*/
|
|
1251
|
+
filter(filter, cb) {
|
|
1252
|
+
const client = new this.constructor(filter);
|
|
1253
|
+
this.addNode(client);
|
|
1254
|
+
cb?.(client);
|
|
1255
|
+
return client;
|
|
1256
|
+
}
|
|
1257
|
+
/**
|
|
1258
|
+
* Start subscribing to events
|
|
1259
|
+
*
|
|
1260
|
+
* @param cb
|
|
1261
|
+
*/
|
|
1262
|
+
static create(cb) {
|
|
1263
|
+
const client = new this();
|
|
1264
|
+
cb(client);
|
|
1265
|
+
return client;
|
|
1266
|
+
}
|
|
1267
|
+
/**
|
|
1268
|
+
* Mount this client using an acceptable Event Driver
|
|
1269
|
+
*/
|
|
1270
|
+
mount(driver) {
|
|
1271
|
+
driver.mountEventHandler(this);
|
|
1272
|
+
return this;
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
class EventDriver {
|
|
1277
|
+
get handler() {
|
|
1278
|
+
if (!this._handler) {
|
|
1279
|
+
throw new Error('handler not registered yet');
|
|
1280
|
+
}
|
|
1281
|
+
return this._handler;
|
|
1282
|
+
}
|
|
1283
|
+
/**
|
|
1284
|
+
* Mount an event handler
|
|
1285
|
+
*
|
|
1286
|
+
* @param handler
|
|
1287
|
+
*/
|
|
1288
|
+
mountEventHandler(handler) {
|
|
1289
|
+
this._handler = handler;
|
|
1290
|
+
return this;
|
|
1291
|
+
}
|
|
1292
|
+
/**
|
|
1293
|
+
* Dispatch an event
|
|
1294
|
+
*
|
|
1295
|
+
* @param e
|
|
1296
|
+
*/
|
|
1297
|
+
dispatchEvent(e) {
|
|
1298
|
+
this.handler.dispatchEvent(e);
|
|
1299
|
+
return this;
|
|
1300
|
+
}
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
class Browser extends EventDriver {
|
|
1304
|
+
constructor({ apiUrl, accessToken, environmentId, }) {
|
|
1305
|
+
super();
|
|
1306
|
+
this._apiUrl = apiUrl;
|
|
1307
|
+
this._accessToken = accessToken;
|
|
1308
|
+
this._environmentId = environmentId || '';
|
|
1309
|
+
}
|
|
1310
|
+
mountEventHandler(handler) {
|
|
1311
|
+
handler.setVariables({
|
|
1312
|
+
accessToken: this._accessToken,
|
|
1313
|
+
apiUrl: this._apiUrl,
|
|
1314
|
+
});
|
|
1315
|
+
this._handler = handler;
|
|
1316
|
+
return this;
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
1319
|
+
|
|
1320
|
+
/**
|
|
1321
|
+
* Flatfile's Virtual Machine is stateless / serverless. So when a new event
|
|
1322
|
+
* is handled, it will just call `handle(event)`.
|
|
1323
|
+
*/
|
|
1324
|
+
class FlatfileVirtualMachine extends EventDriver {
|
|
1325
|
+
/**
|
|
1326
|
+
* This method is triggered from within the Flatfile Core VM Runner. This
|
|
1327
|
+
* EventDriver does not have to listen for events because this method will
|
|
1328
|
+
* be invoked as necessary.
|
|
1329
|
+
*
|
|
1330
|
+
* @param event
|
|
1331
|
+
*/
|
|
1332
|
+
handle(event) {
|
|
1333
|
+
this.dispatchEvent(event);
|
|
1334
|
+
}
|
|
1335
|
+
mountEventHandler(handler) {
|
|
1336
|
+
this._handler = handler;
|
|
1337
|
+
return this;
|
|
1338
|
+
}
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
/**
|
|
1342
|
+
* Backwards compatibility
|
|
1343
|
+
*/
|
|
1344
|
+
class Client extends FlatfileListener {
|
|
1345
|
+
}
|
|
1346
|
+
|
|
1347
|
+
exports.AuthenticatedClient = AuthenticatedClient;
|
|
1348
|
+
exports.Browser = Browser;
|
|
1349
|
+
exports.Client = Client;
|
|
1350
|
+
exports.EventDriver = EventDriver;
|
|
1351
|
+
exports.EventHandler = EventHandler;
|
|
1352
|
+
exports.FlatfileEvent = FlatfileEvent;
|
|
1353
|
+
exports.FlatfileListener = FlatfileListener;
|
|
1354
|
+
exports.FlatfileVirtualMachine = FlatfileVirtualMachine;
|
|
1355
|
+
exports.default = FlatfileListener;
|