@salesforce/lds-runtime-webruntime 0.1.0-dev1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.txt +82 -0
- package/dist/ldsWebruntimeOneStoreInit.js +4459 -0
- package/dist/types/__mocks__/aura.d.ts +3 -0
- package/dist/types/__mocks__/o11y/activity.d.ts +12 -0
- package/dist/types/__mocks__/o11y/client.d.ts +11 -0
- package/dist/types/__mocks__/o11y/idleDetector.d.ts +18 -0
- package/dist/types/__mocks__/o11y/instrumentation.d.ts +15 -0
- package/dist/types/__mocks__/o11y_schema/sf_lds.d.ts +1 -0
- package/dist/types/interceptors.d.ts +22 -0
- package/dist/types/jwt-authorized-fetch-service.d.ts +10 -0
- package/dist/types/main.d.ts +2 -0
- package/dist/types/network-sfap.d.ts +9 -0
- package/package.json +81 -0
|
@@ -0,0 +1,4459 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
3
|
+
* All rights reserved.
|
|
4
|
+
* For full license text, see the LICENSE.txt file
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/*
|
|
8
|
+
* ATTENTION!
|
|
9
|
+
* THIS IS A GENERATED FILE FROM https://github.com/salesforce-experience-platform-emu/lds-lightning-platform
|
|
10
|
+
* If you would like to contribute to LDS, please follow the steps outlined in the git repo.
|
|
11
|
+
* Any changes made to this file in p4 will be automatically overwritten.
|
|
12
|
+
* *******************************************************************************************
|
|
13
|
+
*/
|
|
14
|
+
/* proxy-compat-disable */
|
|
15
|
+
import { getInstrumentation } from 'o11y/client';
|
|
16
|
+
import 'force/luvioTypeNormalization1';
|
|
17
|
+
import { setServices } from 'force/luvioServiceProvisioner1';
|
|
18
|
+
export { default, resolve, setServices } from 'force/luvioServiceProvisioner1';
|
|
19
|
+
import { getSfapJwt } from 'force/clwrSfapExchange';
|
|
20
|
+
import language from '@salesforce/i18n/lang';
|
|
21
|
+
import isDesignMode from '@app/isDesignMode';
|
|
22
|
+
import isPreviewMode from '@app/isPreviewMode';
|
|
23
|
+
import authenticationCookieName from '@app/authenticationCookieName';
|
|
24
|
+
import apiBasePath from '@app/apiBasePath';
|
|
25
|
+
|
|
26
|
+
/*!
|
|
27
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
28
|
+
* All rights reserved.
|
|
29
|
+
* For full license text, see the LICENSE.txt file
|
|
30
|
+
*/
|
|
31
|
+
const { create, freeze, keys, entries } = Object;
|
|
32
|
+
const { isArray: isArray$1 } = Array;
|
|
33
|
+
const { stringify: stringify$1, parse: parse$1 } = JSON;
|
|
34
|
+
const LogLevelMap$1 = {
|
|
35
|
+
TRACE: 4,
|
|
36
|
+
DEBUG: 3,
|
|
37
|
+
INFO: 2,
|
|
38
|
+
WARN: 1,
|
|
39
|
+
ERROR: 0
|
|
40
|
+
};
|
|
41
|
+
let ConsoleLogger$1 = class ConsoleLogger {
|
|
42
|
+
constructor(level = "WARN", printer = console.log, formatter = (level2, message) => `${level2}: ${message}`) {
|
|
43
|
+
this.level = level;
|
|
44
|
+
this.printer = printer;
|
|
45
|
+
this.formatter = formatter;
|
|
46
|
+
this.messages = [];
|
|
47
|
+
}
|
|
48
|
+
trace(message) {
|
|
49
|
+
this.log("TRACE", message);
|
|
50
|
+
}
|
|
51
|
+
debug(message) {
|
|
52
|
+
this.log("DEBUG", message);
|
|
53
|
+
}
|
|
54
|
+
info(message) {
|
|
55
|
+
this.log("INFO", message);
|
|
56
|
+
}
|
|
57
|
+
warn(message) {
|
|
58
|
+
this.log("WARN", message);
|
|
59
|
+
}
|
|
60
|
+
error(message) {
|
|
61
|
+
this.log("ERROR", message);
|
|
62
|
+
}
|
|
63
|
+
log(level, message) {
|
|
64
|
+
if (LogLevelMap$1[level] > LogLevelMap$1[this.level]) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
this.printer(this.formatter(level, message));
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
let Ok$2 = class Ok {
|
|
71
|
+
constructor(value) {
|
|
72
|
+
this.value = value;
|
|
73
|
+
}
|
|
74
|
+
isOk() {
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
isErr() {
|
|
78
|
+
return !this.isOk();
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
let Err$1 = class Err {
|
|
82
|
+
constructor(error) {
|
|
83
|
+
this.error = error;
|
|
84
|
+
}
|
|
85
|
+
isOk() {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
isErr() {
|
|
89
|
+
return !this.isOk();
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
const ok$2 = (value) => new Ok$2(value);
|
|
93
|
+
const err$1 = (err2) => new Err$1(err2);
|
|
94
|
+
function isResult(value) {
|
|
95
|
+
return value != null && typeof value === "object" && "isOk" in value && "isErr" in value && typeof value.isOk === "function" && typeof value.isErr === "function" && (value.isOk() === true && value.isErr() === false && "value" in value || value.isOk() === false && value.isErr() === true && "error" in value);
|
|
96
|
+
}
|
|
97
|
+
function isSubscribable(obj) {
|
|
98
|
+
return typeof obj === "object" && obj !== null && "subscribe" in obj && typeof obj.subscribe === "function" && "refresh" in obj && typeof obj.refresh === "function";
|
|
99
|
+
}
|
|
100
|
+
function isSubscribableResult(x) {
|
|
101
|
+
if (!isResult(x)) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
return isSubscribable(x.isOk() ? x.value : x.error);
|
|
105
|
+
}
|
|
106
|
+
function buildSubscribableResult$1(result, subscribe, refresh) {
|
|
107
|
+
if (result.isOk()) {
|
|
108
|
+
return ok$2({ data: result.value, subscribe, refresh });
|
|
109
|
+
} else {
|
|
110
|
+
return err$1({ failure: result.error, subscribe, refresh });
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function resolvedPromiseLike$3(result) {
|
|
114
|
+
if (isPromiseLike$3(result)) {
|
|
115
|
+
return result.then((nextResult) => nextResult);
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
then: (onFulfilled, _onRejected) => {
|
|
119
|
+
try {
|
|
120
|
+
return resolvedPromiseLike$3(onFulfilled(result));
|
|
121
|
+
} catch (e) {
|
|
122
|
+
if (onFulfilled === void 0) {
|
|
123
|
+
return resolvedPromiseLike$3(result);
|
|
124
|
+
}
|
|
125
|
+
return rejectedPromiseLike$3(e);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
function rejectedPromiseLike$3(reason) {
|
|
131
|
+
if (isPromiseLike$3(reason)) {
|
|
132
|
+
return reason.then((nextResult) => nextResult);
|
|
133
|
+
}
|
|
134
|
+
return {
|
|
135
|
+
then: (_onFulfilled, onRejected) => {
|
|
136
|
+
if (typeof onRejected === "function") {
|
|
137
|
+
try {
|
|
138
|
+
return resolvedPromiseLike$3(onRejected(reason));
|
|
139
|
+
} catch (e) {
|
|
140
|
+
return rejectedPromiseLike$3(e);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return rejectedPromiseLike$3(reason);
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
function isPromiseLike$3(x) {
|
|
148
|
+
return typeof (x == null ? void 0 : x.then) === "function";
|
|
149
|
+
}
|
|
150
|
+
function stableJSONStringify(node) {
|
|
151
|
+
if (node && node.toJSON && typeof node.toJSON === "function") {
|
|
152
|
+
node = node.toJSON();
|
|
153
|
+
}
|
|
154
|
+
if (node === void 0) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (typeof node === "number") {
|
|
158
|
+
return isFinite(node) ? "" + node : "null";
|
|
159
|
+
}
|
|
160
|
+
if (typeof node !== "object") {
|
|
161
|
+
return stringify$1(node);
|
|
162
|
+
}
|
|
163
|
+
let i;
|
|
164
|
+
let out;
|
|
165
|
+
if (isArray$1(node)) {
|
|
166
|
+
out = "[";
|
|
167
|
+
for (i = 0; i < node.length; i++) {
|
|
168
|
+
if (i) {
|
|
169
|
+
out += ",";
|
|
170
|
+
}
|
|
171
|
+
out += stableJSONStringify(node[i]) || "null";
|
|
172
|
+
}
|
|
173
|
+
return out + "]";
|
|
174
|
+
}
|
|
175
|
+
if (node === null) {
|
|
176
|
+
return "null";
|
|
177
|
+
}
|
|
178
|
+
const objKeys = keys(node).sort();
|
|
179
|
+
out = "";
|
|
180
|
+
for (i = 0; i < objKeys.length; i++) {
|
|
181
|
+
const key = objKeys[i];
|
|
182
|
+
const value = stableJSONStringify(node[key]);
|
|
183
|
+
if (!value) {
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
if (out) {
|
|
187
|
+
out += ",";
|
|
188
|
+
}
|
|
189
|
+
out += stringify$1(key) + ":" + value;
|
|
190
|
+
}
|
|
191
|
+
return "{" + out + "}";
|
|
192
|
+
}
|
|
193
|
+
function toError(x) {
|
|
194
|
+
if (x instanceof Error) {
|
|
195
|
+
return x;
|
|
196
|
+
}
|
|
197
|
+
return new Error(typeof x === "string" ? x : JSON.stringify(x));
|
|
198
|
+
}
|
|
199
|
+
var HttpStatusCode = /* @__PURE__ */ ((HttpStatusCode2) => {
|
|
200
|
+
HttpStatusCode2[HttpStatusCode2["Ok"] = 200] = "Ok";
|
|
201
|
+
HttpStatusCode2[HttpStatusCode2["Created"] = 201] = "Created";
|
|
202
|
+
HttpStatusCode2[HttpStatusCode2["NoContent"] = 204] = "NoContent";
|
|
203
|
+
HttpStatusCode2[HttpStatusCode2["NotModified"] = 304] = "NotModified";
|
|
204
|
+
HttpStatusCode2[HttpStatusCode2["BadRequest"] = 400] = "BadRequest";
|
|
205
|
+
HttpStatusCode2[HttpStatusCode2["Unauthorized"] = 401] = "Unauthorized";
|
|
206
|
+
HttpStatusCode2[HttpStatusCode2["Forbidden"] = 403] = "Forbidden";
|
|
207
|
+
HttpStatusCode2[HttpStatusCode2["NotFound"] = 404] = "NotFound";
|
|
208
|
+
HttpStatusCode2[HttpStatusCode2["ServerError"] = 500] = "ServerError";
|
|
209
|
+
HttpStatusCode2[HttpStatusCode2["GatewayTimeout"] = 504] = "GatewayTimeout";
|
|
210
|
+
return HttpStatusCode2;
|
|
211
|
+
})(HttpStatusCode || {});
|
|
212
|
+
function getStatusText(status) {
|
|
213
|
+
switch (status) {
|
|
214
|
+
case 200:
|
|
215
|
+
return "OK";
|
|
216
|
+
case 201:
|
|
217
|
+
return "Created";
|
|
218
|
+
case 304:
|
|
219
|
+
return "Not Modified";
|
|
220
|
+
case 400:
|
|
221
|
+
return "Bad Request";
|
|
222
|
+
case 404:
|
|
223
|
+
return "Not Found";
|
|
224
|
+
case 500:
|
|
225
|
+
return "Server Error";
|
|
226
|
+
default:
|
|
227
|
+
return `Unexpected HTTP Status Code: ${status}`;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
class FetchResponse extends Error {
|
|
231
|
+
constructor(status, body, headers) {
|
|
232
|
+
super();
|
|
233
|
+
this.status = status;
|
|
234
|
+
this.body = body;
|
|
235
|
+
this.headers = headers || {};
|
|
236
|
+
this.ok = status >= 200 && this.status <= 299;
|
|
237
|
+
this.statusText = getStatusText(status);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
class InternalError extends Error {
|
|
241
|
+
constructor(data) {
|
|
242
|
+
super();
|
|
243
|
+
this.data = data;
|
|
244
|
+
this.type = "internal";
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
class UserVisibleError extends Error {
|
|
248
|
+
constructor(data) {
|
|
249
|
+
super();
|
|
250
|
+
this.data = data;
|
|
251
|
+
this.type = "user-visible";
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/*!
|
|
256
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
257
|
+
* All rights reserved.
|
|
258
|
+
* For full license text, see the LICENSE.txt file
|
|
259
|
+
*/
|
|
260
|
+
class BaseCommand {
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/*!
|
|
264
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
265
|
+
* All rights reserved.
|
|
266
|
+
* For full license text, see the LICENSE.txt file
|
|
267
|
+
*/
|
|
268
|
+
let NetworkCommand$1 = class NetworkCommand extends BaseCommand {
|
|
269
|
+
constructor(services) {
|
|
270
|
+
super();
|
|
271
|
+
this.services = services;
|
|
272
|
+
this.subscriptions = [];
|
|
273
|
+
this.exposeSubscribeAndRefresh = false;
|
|
274
|
+
}
|
|
275
|
+
execute() {
|
|
276
|
+
const result = this.fetch();
|
|
277
|
+
if (this.exposeSubscribeAndRefresh) {
|
|
278
|
+
return this.fetchSubscribableResult(result);
|
|
279
|
+
}
|
|
280
|
+
return result;
|
|
281
|
+
}
|
|
282
|
+
fetchSubscribableResult(res) {
|
|
283
|
+
return res.then((networkResult) => {
|
|
284
|
+
return buildSubscribableResult$1(
|
|
285
|
+
networkResult,
|
|
286
|
+
(cb) => {
|
|
287
|
+
this.subscriptions.push(cb);
|
|
288
|
+
return () => {
|
|
289
|
+
this.subscriptions = this.subscriptions.filter((cb2) => cb2 !== cb);
|
|
290
|
+
};
|
|
291
|
+
},
|
|
292
|
+
() => this.refresh()
|
|
293
|
+
);
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
refresh() {
|
|
297
|
+
return this.execute().then((newResult) => {
|
|
298
|
+
if (isSubscribableResult(newResult)) {
|
|
299
|
+
const value = newResult.isOk() ? ok$2(newResult.value.data) : err$1(newResult.error.failure);
|
|
300
|
+
this.subscriptions.forEach((cb) => {
|
|
301
|
+
cb(value);
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
return ok$2(void 0);
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
async afterRequestHooks(_options) {
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
function buildServiceDescriptor$j() {
|
|
311
|
+
return {
|
|
312
|
+
type: "networkCommandBaseClass",
|
|
313
|
+
version: "1.0",
|
|
314
|
+
service: NetworkCommand$1
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/*!
|
|
319
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
320
|
+
* All rights reserved.
|
|
321
|
+
* For full license text, see the LICENSE.txt file
|
|
322
|
+
*/
|
|
323
|
+
class AuraNetworkCommand extends NetworkCommand$1 {
|
|
324
|
+
constructor(services) {
|
|
325
|
+
super(services);
|
|
326
|
+
this.services = services;
|
|
327
|
+
this.actionConfig = {
|
|
328
|
+
background: false,
|
|
329
|
+
hotspot: true,
|
|
330
|
+
longRunning: false,
|
|
331
|
+
storable: false
|
|
332
|
+
};
|
|
333
|
+
this.networkPreference = "aura";
|
|
334
|
+
}
|
|
335
|
+
get fetchParams() {
|
|
336
|
+
throw new Error(
|
|
337
|
+
"Fetch parameters must be specified when using HTTP transport on an Aura adapter"
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
coerceAuraErrors(auraErrors) {
|
|
341
|
+
return toError(auraErrors[0]);
|
|
342
|
+
}
|
|
343
|
+
async coerceFetchErrors(errorResponse) {
|
|
344
|
+
return toError(errorResponse.statusText);
|
|
345
|
+
}
|
|
346
|
+
convertAuraResponseToData(responsePromise, coerceError) {
|
|
347
|
+
return responsePromise.then((response) => {
|
|
348
|
+
const auraReturnValue = response.getReturnValue();
|
|
349
|
+
try {
|
|
350
|
+
this.afterRequestHooks({ statusCode: 200 });
|
|
351
|
+
} catch (e) {
|
|
352
|
+
}
|
|
353
|
+
return ok$2(auraReturnValue);
|
|
354
|
+
}).catch((error) => {
|
|
355
|
+
if (!error || !error.getError) {
|
|
356
|
+
return err$1(toError("Failed to get error from response"));
|
|
357
|
+
} else {
|
|
358
|
+
const actionErrors = error.getError();
|
|
359
|
+
if (actionErrors.length > 0) {
|
|
360
|
+
return err$1(coerceError(actionErrors));
|
|
361
|
+
} else {
|
|
362
|
+
return err$1(toError("Error fetching component"));
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
convertFetchResponseToData(response) {
|
|
368
|
+
return response.then(
|
|
369
|
+
(response2) => {
|
|
370
|
+
if (response2.ok) {
|
|
371
|
+
return response2.json().then(
|
|
372
|
+
(json) => ok$2(json),
|
|
373
|
+
(reason) => err$1(toError(reason))
|
|
374
|
+
).finally(() => {
|
|
375
|
+
try {
|
|
376
|
+
this.afterRequestHooks({ statusCode: response2.status });
|
|
377
|
+
} catch (e) {
|
|
378
|
+
}
|
|
379
|
+
});
|
|
380
|
+
} else {
|
|
381
|
+
return this.coerceFetchErrors(response2).then((coercedError) => {
|
|
382
|
+
return err$1(coercedError);
|
|
383
|
+
}).finally(() => {
|
|
384
|
+
try {
|
|
385
|
+
this.afterRequestHooks({ statusCode: response2.status });
|
|
386
|
+
} catch (e) {
|
|
387
|
+
}
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
},
|
|
391
|
+
(reason) => err$1(toError(reason))
|
|
392
|
+
);
|
|
393
|
+
}
|
|
394
|
+
shouldUseAuraNetwork() {
|
|
395
|
+
return this.services.auraNetwork !== void 0 && (this.networkPreference === "aura" || !this.services.fetch);
|
|
396
|
+
}
|
|
397
|
+
shouldUseFetch() {
|
|
398
|
+
return this.services.fetch !== void 0 && (this.networkPreference === "http" || !this.services.auraNetwork);
|
|
399
|
+
}
|
|
400
|
+
fetch() {
|
|
401
|
+
if (this.shouldUseAuraNetwork()) {
|
|
402
|
+
return this.convertAuraResponseToData(
|
|
403
|
+
this.services.auraNetwork(this.endpoint, this.auraParams, this.actionConfig),
|
|
404
|
+
this.coerceAuraErrors
|
|
405
|
+
);
|
|
406
|
+
} else if (this.shouldUseFetch()) {
|
|
407
|
+
return this.convertFetchResponseToData(this.services.fetch(...this.fetchParams));
|
|
408
|
+
}
|
|
409
|
+
return resolvedPromiseLike$3(err$1(toError("Aura/Fetch network services not found")));
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
function buildServiceDescriptor$i() {
|
|
413
|
+
return {
|
|
414
|
+
type: "auraNetworkCommandBaseClass",
|
|
415
|
+
version: "1.0",
|
|
416
|
+
service: AuraNetworkCommand
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/*!
|
|
421
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
422
|
+
* All rights reserved.
|
|
423
|
+
* For full license text, see the LICENSE.txt file
|
|
424
|
+
*/
|
|
425
|
+
/*!
|
|
426
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
427
|
+
* All rights reserved.
|
|
428
|
+
* For full license text, see the LICENSE.txt file
|
|
429
|
+
*/
|
|
430
|
+
const { isArray } = Array;
|
|
431
|
+
let Ok$1 = class Ok {
|
|
432
|
+
constructor(value) {
|
|
433
|
+
this.value = value;
|
|
434
|
+
}
|
|
435
|
+
isOk() {
|
|
436
|
+
return true;
|
|
437
|
+
}
|
|
438
|
+
isErr() {
|
|
439
|
+
return !this.isOk();
|
|
440
|
+
}
|
|
441
|
+
};
|
|
442
|
+
class Err {
|
|
443
|
+
constructor(error) {
|
|
444
|
+
this.error = error;
|
|
445
|
+
}
|
|
446
|
+
isOk() {
|
|
447
|
+
return false;
|
|
448
|
+
}
|
|
449
|
+
isErr() {
|
|
450
|
+
return !this.isOk();
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
const ok$1 = (value) => new Ok$1(value);
|
|
454
|
+
const err = (err2) => new Err(err2);
|
|
455
|
+
function buildSubscribableResult(result, subscribe, refresh) {
|
|
456
|
+
if (result.isOk()) {
|
|
457
|
+
return ok$1({ data: result.value, subscribe, refresh });
|
|
458
|
+
} else {
|
|
459
|
+
return err({ failure: result.error, subscribe, refresh });
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
function resolvedPromiseLike$2(result) {
|
|
463
|
+
if (isPromiseLike$2(result)) {
|
|
464
|
+
return result.then((nextResult) => nextResult);
|
|
465
|
+
}
|
|
466
|
+
return {
|
|
467
|
+
then: (onFulfilled, _onRejected) => {
|
|
468
|
+
try {
|
|
469
|
+
return resolvedPromiseLike$2(onFulfilled(result));
|
|
470
|
+
} catch (e) {
|
|
471
|
+
if (onFulfilled === void 0) {
|
|
472
|
+
return resolvedPromiseLike$2(result);
|
|
473
|
+
}
|
|
474
|
+
return rejectedPromiseLike$2(e);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
function rejectedPromiseLike$2(reason) {
|
|
480
|
+
if (isPromiseLike$2(reason)) {
|
|
481
|
+
return reason.then((nextResult) => nextResult);
|
|
482
|
+
}
|
|
483
|
+
return {
|
|
484
|
+
then: (_onFulfilled, onRejected) => {
|
|
485
|
+
if (typeof onRejected === "function") {
|
|
486
|
+
try {
|
|
487
|
+
return resolvedPromiseLike$2(onRejected(reason));
|
|
488
|
+
} catch (e) {
|
|
489
|
+
return rejectedPromiseLike$2(e);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
return rejectedPromiseLike$2(reason);
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
function isPromiseLike$2(x) {
|
|
497
|
+
return typeof (x == null ? void 0 : x.then) === "function";
|
|
498
|
+
}
|
|
499
|
+
function deepEquals(x, y) {
|
|
500
|
+
if (x === void 0) {
|
|
501
|
+
return y === void 0;
|
|
502
|
+
} else if (x === null) {
|
|
503
|
+
return y === null;
|
|
504
|
+
} else if (y === null) {
|
|
505
|
+
return x === null;
|
|
506
|
+
} else if (isArray(x)) {
|
|
507
|
+
if (!isArray(y) || x.length !== y.length) {
|
|
508
|
+
return false;
|
|
509
|
+
}
|
|
510
|
+
for (let i = 0; i < x.length; ++i) {
|
|
511
|
+
if (!deepEquals(x[i], y[i])) {
|
|
512
|
+
return false;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
return true;
|
|
516
|
+
} else if (typeof x === "object") {
|
|
517
|
+
if (typeof y !== "object") {
|
|
518
|
+
return false;
|
|
519
|
+
}
|
|
520
|
+
const xkeys = Object.keys(x);
|
|
521
|
+
const ykeys = Object.keys(y);
|
|
522
|
+
if (xkeys.length !== ykeys.length) {
|
|
523
|
+
return false;
|
|
524
|
+
}
|
|
525
|
+
for (let i = 0; i < xkeys.length; ++i) {
|
|
526
|
+
const key = xkeys[i];
|
|
527
|
+
if (!deepEquals(x[key], y[key])) {
|
|
528
|
+
return false;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
return true;
|
|
532
|
+
}
|
|
533
|
+
return x === y;
|
|
534
|
+
}
|
|
535
|
+
function setOverlaps(setA, setB) {
|
|
536
|
+
if (setA.size > setB.size) {
|
|
537
|
+
for (const key of setB.keys()) {
|
|
538
|
+
if (setA.has(key)) {
|
|
539
|
+
return true;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
} else {
|
|
543
|
+
for (const key of setA) {
|
|
544
|
+
if (setB.has(key)) {
|
|
545
|
+
return true;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
return false;
|
|
550
|
+
}
|
|
551
|
+
class CacheControlRequestRunner {
|
|
552
|
+
constructor(readFromCache, requestFromNetwork, writeToCache) {
|
|
553
|
+
this.readFromCacheInternal = readFromCache;
|
|
554
|
+
this.requestFromNetworkInternal = requestFromNetwork;
|
|
555
|
+
this.writeToCacheInternal = writeToCache;
|
|
556
|
+
}
|
|
557
|
+
readFromCache(cache) {
|
|
558
|
+
const resultPromise = this.readFromCacheInternal(cache);
|
|
559
|
+
return resultPromise.then((result) => {
|
|
560
|
+
if (result.isErr()) {
|
|
561
|
+
return err(result.error.failure);
|
|
562
|
+
}
|
|
563
|
+
this.returnData = result;
|
|
564
|
+
return ok$1(void 0);
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
requestFromNetwork() {
|
|
568
|
+
const that = this;
|
|
569
|
+
return (async function* () {
|
|
570
|
+
const result = await that.requestFromNetworkInternal();
|
|
571
|
+
if (result.isErr()) {
|
|
572
|
+
that.networkError = result;
|
|
573
|
+
} else {
|
|
574
|
+
that.networkData = result;
|
|
575
|
+
}
|
|
576
|
+
yield result;
|
|
577
|
+
})();
|
|
578
|
+
}
|
|
579
|
+
writeToCache(cache, networkResult) {
|
|
580
|
+
return this.writeToCacheInternal(cache, networkResult);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
class CacheControlCommand extends BaseCommand {
|
|
584
|
+
constructor(services) {
|
|
585
|
+
super();
|
|
586
|
+
this.services = services;
|
|
587
|
+
this.keysUsed = /* @__PURE__ */ new Set();
|
|
588
|
+
this.keysUpdated = void 0;
|
|
589
|
+
this.operationType = "query";
|
|
590
|
+
this.lastResult = void 0;
|
|
591
|
+
this.unsubscribeFromKeysImpl = () => void 0;
|
|
592
|
+
this.subscriptions = [];
|
|
593
|
+
this.instantiationTime = Date.now() / 1e3;
|
|
594
|
+
}
|
|
595
|
+
execute(overrides) {
|
|
596
|
+
this.keysUpdated = void 0;
|
|
597
|
+
this.unsubscribeFromKeys();
|
|
598
|
+
const mergedCacheControlConfig = mergeCacheControlConfigs(
|
|
599
|
+
this.cacheControlStrategyConfig,
|
|
600
|
+
overrides
|
|
601
|
+
);
|
|
602
|
+
const requestRunner = this.buildRequestRunner();
|
|
603
|
+
const resultPromise = this.services.cacheController.execute(mergedCacheControlConfig, requestRunner, {
|
|
604
|
+
instrumentationAttributes: this.instrumentationAttributes
|
|
605
|
+
});
|
|
606
|
+
return resultPromise.then((result) => {
|
|
607
|
+
return this.handleCacheControllerResult(result, requestRunner).then((result2) => {
|
|
608
|
+
if (this.lastResult === void 0) {
|
|
609
|
+
if (result2.isErr()) {
|
|
610
|
+
this.lastResult = { type: "error", error: result2.error.failure };
|
|
611
|
+
} else {
|
|
612
|
+
this.lastResult = { type: "data", data: result2.value.data };
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
return result2;
|
|
616
|
+
});
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
handleCacheControllerResult(result, requestRunner) {
|
|
620
|
+
const { networkError, networkData, returnData } = requestRunner;
|
|
621
|
+
return this.publishUpdatedKeys().then(() => {
|
|
622
|
+
if (networkError) {
|
|
623
|
+
return buildSubscribableResult(
|
|
624
|
+
networkError,
|
|
625
|
+
this.buildSubscribe(),
|
|
626
|
+
() => this.refresh()
|
|
627
|
+
);
|
|
628
|
+
}
|
|
629
|
+
if (result.isErr()) {
|
|
630
|
+
if (networkData) {
|
|
631
|
+
return buildSubscribableResult(
|
|
632
|
+
networkData,
|
|
633
|
+
this.buildSubscribe(),
|
|
634
|
+
() => this.refresh()
|
|
635
|
+
);
|
|
636
|
+
}
|
|
637
|
+
return buildSubscribableResult(result, this.buildSubscribe(), () => this.refresh());
|
|
638
|
+
}
|
|
639
|
+
if (this.subscriptions.length > 0) {
|
|
640
|
+
this.subscribeToKeysUsed();
|
|
641
|
+
}
|
|
642
|
+
if (returnData === void 0) {
|
|
643
|
+
if (networkData) {
|
|
644
|
+
return buildSubscribableResult(
|
|
645
|
+
networkData,
|
|
646
|
+
this.buildSubscribe(),
|
|
647
|
+
() => this.refresh()
|
|
648
|
+
);
|
|
649
|
+
}
|
|
650
|
+
return buildSubscribableResult(
|
|
651
|
+
err(new Error("Cache miss after fetching from network")),
|
|
652
|
+
this.buildSubscribe(),
|
|
653
|
+
() => this.refresh()
|
|
654
|
+
);
|
|
655
|
+
}
|
|
656
|
+
return returnData;
|
|
657
|
+
});
|
|
658
|
+
}
|
|
659
|
+
buildRequestRunner() {
|
|
660
|
+
return new CacheControlRequestRunner(
|
|
661
|
+
(cache) => this.buildResultWithSubscribe(cache),
|
|
662
|
+
() => this.requestFromNetwork(),
|
|
663
|
+
(cache, networkResult) => this.writeToCacheAndRecordKeys(cache, networkResult)
|
|
664
|
+
);
|
|
665
|
+
}
|
|
666
|
+
publishUpdatedKeys() {
|
|
667
|
+
if (this.services.pubSub) {
|
|
668
|
+
if (this.keysUpdated !== void 0 && this.keysUpdated.size > 0) {
|
|
669
|
+
return this.services.pubSub.publish({
|
|
670
|
+
type: "cacheUpdate",
|
|
671
|
+
data: this.keysUpdated
|
|
672
|
+
});
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
return resolvedPromiseLike$2(void 0);
|
|
676
|
+
}
|
|
677
|
+
subscribeToKeysUsed() {
|
|
678
|
+
this.unsubscribeFromKeys();
|
|
679
|
+
const { pubSub } = this.services;
|
|
680
|
+
if (!pubSub) {
|
|
681
|
+
this.unsubscribeFromKeysImpl = () => void 0;
|
|
682
|
+
return;
|
|
683
|
+
}
|
|
684
|
+
const rebuildUnsubscribe = pubSub.subscribe({
|
|
685
|
+
type: "cacheUpdate",
|
|
686
|
+
predicate: (event) => setOverlaps(event.data, this.keysUsed),
|
|
687
|
+
callback: () => this.rerun({ now: this.instantiationTime }).then(() => void 0),
|
|
688
|
+
keys: this.keysUsed
|
|
689
|
+
});
|
|
690
|
+
const refreshUnsubscribe = pubSub.subscribe({
|
|
691
|
+
type: "cacheInvalidation",
|
|
692
|
+
predicate: (event) => setOverlaps(event.data, this.keysUsed),
|
|
693
|
+
callback: () => this.rerun().then(() => void 0),
|
|
694
|
+
keys: this.keysUsed
|
|
695
|
+
});
|
|
696
|
+
this.unsubscribeFromKeysImpl = () => {
|
|
697
|
+
rebuildUnsubscribe();
|
|
698
|
+
refreshUnsubscribe();
|
|
699
|
+
};
|
|
700
|
+
return;
|
|
701
|
+
}
|
|
702
|
+
unsubscribeFromKeys() {
|
|
703
|
+
this.unsubscribeFromKeysImpl();
|
|
704
|
+
}
|
|
705
|
+
// TODO: This should likely be abstract in v2. For v1, provide default comparison logic.
|
|
706
|
+
equals(result1, result2) {
|
|
707
|
+
return deepEquals(result1, result2);
|
|
708
|
+
}
|
|
709
|
+
async afterRequestHooks(_options) {
|
|
710
|
+
}
|
|
711
|
+
refresh() {
|
|
712
|
+
return this.rerun({ cacheControlConfig: { type: "no-cache" } }).then((result) => {
|
|
713
|
+
if (result.isErr()) {
|
|
714
|
+
return err(result.error.failure);
|
|
715
|
+
}
|
|
716
|
+
return ok$1(void 0);
|
|
717
|
+
});
|
|
718
|
+
}
|
|
719
|
+
writeToCacheAndRecordKeys(cache, networkResult) {
|
|
720
|
+
const recordableCache = cache.record();
|
|
721
|
+
return this.writeToCache(recordableCache, networkResult).then((result) => {
|
|
722
|
+
this.instantiationTime = Date.now() / 1e3;
|
|
723
|
+
this.keysUpdated = recordableCache.keysUpdated;
|
|
724
|
+
return ok$1(result);
|
|
725
|
+
});
|
|
726
|
+
}
|
|
727
|
+
buildResultWithSubscribe(cache) {
|
|
728
|
+
const recordableCache = cache.record();
|
|
729
|
+
const result = this.readFromCache(recordableCache);
|
|
730
|
+
return result.then((readResult) => {
|
|
731
|
+
if (readResult.isErr()) {
|
|
732
|
+
return buildSubscribableResult(
|
|
733
|
+
readResult,
|
|
734
|
+
this.buildSubscribe(),
|
|
735
|
+
() => this.refresh()
|
|
736
|
+
);
|
|
737
|
+
} else {
|
|
738
|
+
const data = readResult.value;
|
|
739
|
+
this.keysUsed = recordableCache.keysRead;
|
|
740
|
+
return buildSubscribableResult(
|
|
741
|
+
ok$1(data),
|
|
742
|
+
this.buildSubscribe(),
|
|
743
|
+
() => this.refresh()
|
|
744
|
+
);
|
|
745
|
+
}
|
|
746
|
+
});
|
|
747
|
+
}
|
|
748
|
+
/**
|
|
749
|
+
* Builds a function that subscribes to cache changes via the pubsub service. Whenever
|
|
750
|
+
* relevant cache updates occur, it re-reads the data and compares it against
|
|
751
|
+
* the last known value. If a change is detected, the provided
|
|
752
|
+
* callback is invoked.
|
|
753
|
+
*
|
|
754
|
+
* @param keysRead - keys of interest that were read during readFromCache
|
|
755
|
+
* @returns an unsubscribe function to stop watching for updates
|
|
756
|
+
*/
|
|
757
|
+
buildSubscribe() {
|
|
758
|
+
return (consumerCallback) => {
|
|
759
|
+
if (this.subscriptions.length === 0 && (this.operationType === "query" || this.operationType === "graphql")) {
|
|
760
|
+
this.subscribeToKeysUsed();
|
|
761
|
+
}
|
|
762
|
+
this.subscriptions.push(consumerCallback);
|
|
763
|
+
return () => {
|
|
764
|
+
this.subscriptions = this.subscriptions.filter((cb) => cb !== consumerCallback);
|
|
765
|
+
if (this.subscriptions.length === 0) {
|
|
766
|
+
this.unsubscribeFromKeys();
|
|
767
|
+
}
|
|
768
|
+
};
|
|
769
|
+
};
|
|
770
|
+
}
|
|
771
|
+
rerun(overrides) {
|
|
772
|
+
return this.execute(overrides).then((result) => {
|
|
773
|
+
if (result.isErr()) {
|
|
774
|
+
this.lastResult = { type: "error", error: result.error.failure };
|
|
775
|
+
this.invokeConsumerCallbacks(err(result.error.failure));
|
|
776
|
+
return result;
|
|
777
|
+
}
|
|
778
|
+
if (this.lastResult === void 0 || this.lastResult.type === "error" || !this.equals(this.lastResult.data, result.value.data)) {
|
|
779
|
+
this.lastResult = { type: "data", data: result.value.data };
|
|
780
|
+
this.invokeConsumerCallbacks(ok$1(result.value.data));
|
|
781
|
+
}
|
|
782
|
+
return result;
|
|
783
|
+
});
|
|
784
|
+
}
|
|
785
|
+
invokeConsumerCallbacks(data) {
|
|
786
|
+
this.subscriptions.forEach((cb) => {
|
|
787
|
+
try {
|
|
788
|
+
cb(data);
|
|
789
|
+
} catch (error) {
|
|
790
|
+
}
|
|
791
|
+
});
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
function mergeCacheControlConfigs(baseConfig, overrides) {
|
|
795
|
+
if (!overrides) {
|
|
796
|
+
return baseConfig;
|
|
797
|
+
}
|
|
798
|
+
const now = overrides.now ?? baseConfig.now;
|
|
799
|
+
if (!overrides.cacheControlConfig) {
|
|
800
|
+
return {
|
|
801
|
+
...baseConfig,
|
|
802
|
+
now
|
|
803
|
+
};
|
|
804
|
+
}
|
|
805
|
+
return {
|
|
806
|
+
...overrides.cacheControlConfig,
|
|
807
|
+
now
|
|
808
|
+
};
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
/*!
|
|
812
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
813
|
+
* All rights reserved.
|
|
814
|
+
* For full license text, see the LICENSE.txt file
|
|
815
|
+
*/
|
|
816
|
+
/*!
|
|
817
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
818
|
+
* All rights reserved.
|
|
819
|
+
* For full license text, see the LICENSE.txt file
|
|
820
|
+
*/
|
|
821
|
+
let AuraCacheControlCommand$1 = class AuraCacheControlCommand extends CacheControlCommand {
|
|
822
|
+
constructor(services) {
|
|
823
|
+
super(services);
|
|
824
|
+
this.services = services;
|
|
825
|
+
this.actionConfig = {
|
|
826
|
+
background: false,
|
|
827
|
+
hotspot: true,
|
|
828
|
+
longRunning: false,
|
|
829
|
+
storable: false
|
|
830
|
+
};
|
|
831
|
+
this.networkPreference = "aura";
|
|
832
|
+
}
|
|
833
|
+
get fetchParams() {
|
|
834
|
+
throw new Error(
|
|
835
|
+
"Fetch parameters must be specified when using HTTP transport on an Aura adapter"
|
|
836
|
+
);
|
|
837
|
+
}
|
|
838
|
+
shouldUseAuraNetwork() {
|
|
839
|
+
return this.services.auraNetwork !== void 0 && (this.networkPreference === "aura" || !this.services.fetch);
|
|
840
|
+
}
|
|
841
|
+
shouldUseFetch() {
|
|
842
|
+
return this.services.fetch !== void 0 && (this.networkPreference === "http" || !this.services.auraNetwork);
|
|
843
|
+
}
|
|
844
|
+
requestFromNetwork() {
|
|
845
|
+
if (this.shouldUseAuraNetwork()) {
|
|
846
|
+
return this.convertAuraResponseToData(
|
|
847
|
+
this.services.auraNetwork(this.endpoint, this.auraParams, this.actionConfig),
|
|
848
|
+
(errs) => this.coerceError(errs)
|
|
849
|
+
);
|
|
850
|
+
} else if (this.shouldUseFetch()) {
|
|
851
|
+
return this.convertFetchResponseToData(this.services.fetch(...this.fetchParams));
|
|
852
|
+
}
|
|
853
|
+
return resolvedPromiseLike$3(err$1(toError("Aura/Fetch network services not found")));
|
|
854
|
+
}
|
|
855
|
+
coerceError(auraErrors) {
|
|
856
|
+
return toError(auraErrors[0]);
|
|
857
|
+
}
|
|
858
|
+
async coerceFetchError(errorResponse) {
|
|
859
|
+
return toError(errorResponse.statusText);
|
|
860
|
+
}
|
|
861
|
+
processAuraReturnValue(auraReturnValue) {
|
|
862
|
+
return ok$2(auraReturnValue);
|
|
863
|
+
}
|
|
864
|
+
processFetchReturnValue(json) {
|
|
865
|
+
return ok$2(json);
|
|
866
|
+
}
|
|
867
|
+
convertAuraResponseToData(responsePromise, coerceError) {
|
|
868
|
+
return responsePromise.then((response) => {
|
|
869
|
+
return this.processAuraReturnValue(response.getReturnValue());
|
|
870
|
+
}).finally(() => {
|
|
871
|
+
try {
|
|
872
|
+
this.afterRequestHooks({ statusCode: 200 });
|
|
873
|
+
} catch (e) {
|
|
874
|
+
}
|
|
875
|
+
}).catch((error) => {
|
|
876
|
+
if (!error || !error.getError) {
|
|
877
|
+
return err$1(toError("Failed to get error from response"));
|
|
878
|
+
} else {
|
|
879
|
+
const actionErrors = error.getError();
|
|
880
|
+
if (actionErrors.length > 0) {
|
|
881
|
+
return err$1(coerceError(actionErrors));
|
|
882
|
+
} else {
|
|
883
|
+
return err$1(toError("Error fetching component"));
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
});
|
|
887
|
+
}
|
|
888
|
+
convertFetchResponseToData(response) {
|
|
889
|
+
return response.then(
|
|
890
|
+
(response2) => {
|
|
891
|
+
if (response2.ok) {
|
|
892
|
+
return response2.json().then(
|
|
893
|
+
(json) => {
|
|
894
|
+
return this.processFetchReturnValue(json);
|
|
895
|
+
},
|
|
896
|
+
(reason) => err$1(toError(reason))
|
|
897
|
+
).finally(() => {
|
|
898
|
+
try {
|
|
899
|
+
this.afterRequestHooks({ statusCode: response2.status });
|
|
900
|
+
} catch (e) {
|
|
901
|
+
}
|
|
902
|
+
});
|
|
903
|
+
} else {
|
|
904
|
+
return this.coerceFetchError(response2).then((coercedError) => {
|
|
905
|
+
return err$1(coercedError);
|
|
906
|
+
}).finally(() => {
|
|
907
|
+
try {
|
|
908
|
+
this.afterRequestHooks({ statusCode: response2.status });
|
|
909
|
+
} catch (e) {
|
|
910
|
+
}
|
|
911
|
+
});
|
|
912
|
+
}
|
|
913
|
+
},
|
|
914
|
+
(reason) => err$1(toError(reason))
|
|
915
|
+
);
|
|
916
|
+
}
|
|
917
|
+
};
|
|
918
|
+
class AuraResourceCacheControlCommand extends AuraCacheControlCommand$1 {
|
|
919
|
+
constructor(services) {
|
|
920
|
+
super(services);
|
|
921
|
+
this.services = services;
|
|
922
|
+
}
|
|
923
|
+
readFromCache(cache) {
|
|
924
|
+
var _a;
|
|
925
|
+
const data = (_a = cache.get(this.buildKey())) == null ? void 0 : _a.value;
|
|
926
|
+
if (data === void 0) {
|
|
927
|
+
return resolvedPromiseLike$3(err$1(new Error("Failed to find data in cache")));
|
|
928
|
+
}
|
|
929
|
+
return resolvedPromiseLike$3(ok$2(data));
|
|
930
|
+
}
|
|
931
|
+
writeToCache(cache, networkResult) {
|
|
932
|
+
if (networkResult.isOk()) {
|
|
933
|
+
cache.set(this.buildKey(), {
|
|
934
|
+
value: networkResult.value,
|
|
935
|
+
metadata: {
|
|
936
|
+
cacheControl: this.buildCacheControlMetadata(networkResult.value)
|
|
937
|
+
}
|
|
938
|
+
});
|
|
939
|
+
}
|
|
940
|
+
return resolvedPromiseLike$3(void 0);
|
|
941
|
+
}
|
|
942
|
+
buildKey() {
|
|
943
|
+
return `{"endpoint":${this.endpoint},"params":${stableJSONStringify(this.auraParams)}}`;
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
function buildServiceDescriptor$h() {
|
|
947
|
+
return {
|
|
948
|
+
type: "auraResourceCacheControlCommand",
|
|
949
|
+
version: "1.0",
|
|
950
|
+
service: AuraResourceCacheControlCommand
|
|
951
|
+
};
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
/*!
|
|
955
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
956
|
+
* All rights reserved.
|
|
957
|
+
* For full license text, see the LICENSE.txt file
|
|
958
|
+
*/
|
|
959
|
+
/*!
|
|
960
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
961
|
+
* All rights reserved.
|
|
962
|
+
* For full license text, see the LICENSE.txt file
|
|
963
|
+
*/
|
|
964
|
+
class AuraCacheControlCommand extends CacheControlCommand {
|
|
965
|
+
constructor(services) {
|
|
966
|
+
super(services);
|
|
967
|
+
this.services = services;
|
|
968
|
+
this.actionConfig = {
|
|
969
|
+
background: false,
|
|
970
|
+
hotspot: true,
|
|
971
|
+
longRunning: false,
|
|
972
|
+
storable: false
|
|
973
|
+
};
|
|
974
|
+
this.networkPreference = "aura";
|
|
975
|
+
}
|
|
976
|
+
get fetchParams() {
|
|
977
|
+
throw new Error(
|
|
978
|
+
"Fetch parameters must be specified when using HTTP transport on an Aura adapter"
|
|
979
|
+
);
|
|
980
|
+
}
|
|
981
|
+
shouldUseAuraNetwork() {
|
|
982
|
+
return this.services.auraNetwork !== void 0 && (this.networkPreference === "aura" || !this.services.fetch);
|
|
983
|
+
}
|
|
984
|
+
shouldUseFetch() {
|
|
985
|
+
return this.services.fetch !== void 0 && (this.networkPreference === "http" || !this.services.auraNetwork);
|
|
986
|
+
}
|
|
987
|
+
requestFromNetwork() {
|
|
988
|
+
if (this.shouldUseAuraNetwork()) {
|
|
989
|
+
return this.convertAuraResponseToData(
|
|
990
|
+
this.services.auraNetwork(this.endpoint, this.auraParams, this.actionConfig),
|
|
991
|
+
(errs) => this.coerceError(errs)
|
|
992
|
+
);
|
|
993
|
+
} else if (this.shouldUseFetch()) {
|
|
994
|
+
return this.convertFetchResponseToData(this.services.fetch(...this.fetchParams));
|
|
995
|
+
}
|
|
996
|
+
return resolvedPromiseLike$3(err$1(toError("Aura/Fetch network services not found")));
|
|
997
|
+
}
|
|
998
|
+
coerceError(auraErrors) {
|
|
999
|
+
return toError(auraErrors[0]);
|
|
1000
|
+
}
|
|
1001
|
+
async coerceFetchError(errorResponse) {
|
|
1002
|
+
return toError(errorResponse.statusText);
|
|
1003
|
+
}
|
|
1004
|
+
processAuraReturnValue(auraReturnValue) {
|
|
1005
|
+
return ok$2(auraReturnValue);
|
|
1006
|
+
}
|
|
1007
|
+
processFetchReturnValue(json) {
|
|
1008
|
+
return ok$2(json);
|
|
1009
|
+
}
|
|
1010
|
+
convertAuraResponseToData(responsePromise, coerceError) {
|
|
1011
|
+
return responsePromise.then((response) => {
|
|
1012
|
+
return this.processAuraReturnValue(response.getReturnValue());
|
|
1013
|
+
}).finally(() => {
|
|
1014
|
+
try {
|
|
1015
|
+
this.afterRequestHooks({ statusCode: 200 });
|
|
1016
|
+
} catch (e) {
|
|
1017
|
+
}
|
|
1018
|
+
}).catch((error) => {
|
|
1019
|
+
if (!error || !error.getError) {
|
|
1020
|
+
return err$1(toError("Failed to get error from response"));
|
|
1021
|
+
} else {
|
|
1022
|
+
const actionErrors = error.getError();
|
|
1023
|
+
if (actionErrors.length > 0) {
|
|
1024
|
+
return err$1(coerceError(actionErrors));
|
|
1025
|
+
} else {
|
|
1026
|
+
return err$1(toError("Error fetching component"));
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
});
|
|
1030
|
+
}
|
|
1031
|
+
convertFetchResponseToData(response) {
|
|
1032
|
+
return response.then(
|
|
1033
|
+
(response2) => {
|
|
1034
|
+
if (response2.ok) {
|
|
1035
|
+
return response2.json().then(
|
|
1036
|
+
(json) => {
|
|
1037
|
+
return this.processFetchReturnValue(json);
|
|
1038
|
+
},
|
|
1039
|
+
(reason) => err$1(toError(reason))
|
|
1040
|
+
).finally(() => {
|
|
1041
|
+
try {
|
|
1042
|
+
this.afterRequestHooks({ statusCode: response2.status });
|
|
1043
|
+
} catch (e) {
|
|
1044
|
+
}
|
|
1045
|
+
});
|
|
1046
|
+
} else {
|
|
1047
|
+
return this.coerceFetchError(response2).then((coercedError) => {
|
|
1048
|
+
return err$1(coercedError);
|
|
1049
|
+
}).finally(() => {
|
|
1050
|
+
try {
|
|
1051
|
+
this.afterRequestHooks({ statusCode: response2.status });
|
|
1052
|
+
} catch (e) {
|
|
1053
|
+
}
|
|
1054
|
+
});
|
|
1055
|
+
}
|
|
1056
|
+
},
|
|
1057
|
+
(reason) => err$1(toError(reason))
|
|
1058
|
+
);
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
class AuraNormalizedCacheControlCommand extends AuraCacheControlCommand {
|
|
1062
|
+
constructor(services) {
|
|
1063
|
+
super(services);
|
|
1064
|
+
this.services = services;
|
|
1065
|
+
}
|
|
1066
|
+
readFromCache(cache) {
|
|
1067
|
+
const data = this.buildResultType().query(cache, this.buildQuery());
|
|
1068
|
+
if (data.isErr()) {
|
|
1069
|
+
return resolvedPromiseLike$3(
|
|
1070
|
+
err$1(new Error(`Failed to build data from type: ${stringify$1(data.error)}`))
|
|
1071
|
+
);
|
|
1072
|
+
}
|
|
1073
|
+
return resolvedPromiseLike$3(ok$2(data.value));
|
|
1074
|
+
}
|
|
1075
|
+
writeToCache(cache, networkResult) {
|
|
1076
|
+
if (networkResult.isOk()) {
|
|
1077
|
+
this.buildResultType().write(
|
|
1078
|
+
cache.buildFixedTimeWritableCache(Date.now() / 1e3),
|
|
1079
|
+
this.buildWriteInput(networkResult.value)
|
|
1080
|
+
);
|
|
1081
|
+
}
|
|
1082
|
+
return resolvedPromiseLike$3(void 0);
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
function buildServiceDescriptor$g() {
|
|
1086
|
+
return {
|
|
1087
|
+
type: "auraNormalizedCacheControlCommand",
|
|
1088
|
+
version: "1.0",
|
|
1089
|
+
service: AuraNormalizedCacheControlCommand
|
|
1090
|
+
};
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
/*!
|
|
1094
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
1095
|
+
* All rights reserved.
|
|
1096
|
+
* For full license text, see the LICENSE.txt file
|
|
1097
|
+
*/
|
|
1098
|
+
/*!
|
|
1099
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
1100
|
+
* All rights reserved.
|
|
1101
|
+
* For full license text, see the LICENSE.txt file
|
|
1102
|
+
*/
|
|
1103
|
+
class HttpCacheControlCommand extends CacheControlCommand {
|
|
1104
|
+
constructor(services) {
|
|
1105
|
+
super(services);
|
|
1106
|
+
this.services = services;
|
|
1107
|
+
}
|
|
1108
|
+
requestFromNetwork() {
|
|
1109
|
+
return this.fetch();
|
|
1110
|
+
}
|
|
1111
|
+
fetch() {
|
|
1112
|
+
return this.convertFetchResponseToData(this.services.fetch(...this.fetchParams));
|
|
1113
|
+
}
|
|
1114
|
+
async coerceError(errorResponse) {
|
|
1115
|
+
return toError(errorResponse.statusText);
|
|
1116
|
+
}
|
|
1117
|
+
processFetchReturnValue(json) {
|
|
1118
|
+
return ok$2(json);
|
|
1119
|
+
}
|
|
1120
|
+
convertFetchResponseToData(response) {
|
|
1121
|
+
return response.then(
|
|
1122
|
+
(response2) => {
|
|
1123
|
+
if (response2.ok) {
|
|
1124
|
+
return response2.json().then(
|
|
1125
|
+
(json) => {
|
|
1126
|
+
return this.processFetchReturnValue(json);
|
|
1127
|
+
},
|
|
1128
|
+
(reason) => err$1(toError(reason))
|
|
1129
|
+
).finally(() => {
|
|
1130
|
+
try {
|
|
1131
|
+
this.afterRequestHooks({ statusCode: response2.status });
|
|
1132
|
+
} catch (e) {
|
|
1133
|
+
}
|
|
1134
|
+
});
|
|
1135
|
+
} else {
|
|
1136
|
+
return this.coerceError(response2).then((coercedError) => {
|
|
1137
|
+
return err$1(coercedError);
|
|
1138
|
+
}).finally(() => {
|
|
1139
|
+
try {
|
|
1140
|
+
this.afterRequestHooks({ statusCode: response2.status });
|
|
1141
|
+
} catch (e) {
|
|
1142
|
+
}
|
|
1143
|
+
});
|
|
1144
|
+
}
|
|
1145
|
+
},
|
|
1146
|
+
(reason) => err$1(toError(reason))
|
|
1147
|
+
);
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
class HttpNormalizedCacheControlCommand extends HttpCacheControlCommand {
|
|
1151
|
+
constructor(services) {
|
|
1152
|
+
super(services);
|
|
1153
|
+
this.services = services;
|
|
1154
|
+
}
|
|
1155
|
+
readFromCache(cache) {
|
|
1156
|
+
const data = this.buildResultType().query(cache, this.buildQuery());
|
|
1157
|
+
if (data.isErr()) {
|
|
1158
|
+
return resolvedPromiseLike$3(
|
|
1159
|
+
err$1(new Error(`Failed to build data from type: ${stringify$1(data.error)}`))
|
|
1160
|
+
);
|
|
1161
|
+
}
|
|
1162
|
+
return resolvedPromiseLike$3(ok$2(data.value));
|
|
1163
|
+
}
|
|
1164
|
+
writeToCache(cache, networkResult) {
|
|
1165
|
+
if (networkResult.isOk()) {
|
|
1166
|
+
this.buildResultType().write(
|
|
1167
|
+
cache.buildFixedTimeWritableCache(Date.now() / 1e3),
|
|
1168
|
+
this.buildWriteInput(networkResult.value)
|
|
1169
|
+
);
|
|
1170
|
+
}
|
|
1171
|
+
return resolvedPromiseLike$3(void 0);
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
function buildServiceDescriptor$f() {
|
|
1175
|
+
return {
|
|
1176
|
+
type: "httpNormalizedCacheControlCommand",
|
|
1177
|
+
version: "1.0",
|
|
1178
|
+
service: HttpNormalizedCacheControlCommand
|
|
1179
|
+
};
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
/*!
|
|
1183
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
1184
|
+
* All rights reserved.
|
|
1185
|
+
* For full license text, see the LICENSE.txt file
|
|
1186
|
+
*/
|
|
1187
|
+
/*!
|
|
1188
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
1189
|
+
* All rights reserved.
|
|
1190
|
+
* For full license text, see the LICENSE.txt file
|
|
1191
|
+
*/
|
|
1192
|
+
class NetworkCommand extends BaseCommand {
|
|
1193
|
+
constructor(services) {
|
|
1194
|
+
super();
|
|
1195
|
+
this.services = services;
|
|
1196
|
+
this.subscriptions = [];
|
|
1197
|
+
this.exposeSubscribeAndRefresh = false;
|
|
1198
|
+
}
|
|
1199
|
+
execute() {
|
|
1200
|
+
const result = this.fetch();
|
|
1201
|
+
if (this.exposeSubscribeAndRefresh) {
|
|
1202
|
+
return this.fetchSubscribableResult(result);
|
|
1203
|
+
}
|
|
1204
|
+
return result;
|
|
1205
|
+
}
|
|
1206
|
+
fetchSubscribableResult(res) {
|
|
1207
|
+
return res.then((networkResult) => {
|
|
1208
|
+
return buildSubscribableResult$1(
|
|
1209
|
+
networkResult,
|
|
1210
|
+
(cb) => {
|
|
1211
|
+
this.subscriptions.push(cb);
|
|
1212
|
+
return () => {
|
|
1213
|
+
this.subscriptions = this.subscriptions.filter((cb2) => cb2 !== cb);
|
|
1214
|
+
};
|
|
1215
|
+
},
|
|
1216
|
+
() => this.refresh()
|
|
1217
|
+
);
|
|
1218
|
+
});
|
|
1219
|
+
}
|
|
1220
|
+
refresh() {
|
|
1221
|
+
return this.execute().then((newResult) => {
|
|
1222
|
+
if (isSubscribableResult(newResult)) {
|
|
1223
|
+
const value = newResult.isOk() ? ok$2(newResult.value.data) : err$1(newResult.error.failure);
|
|
1224
|
+
this.subscriptions.forEach((cb) => {
|
|
1225
|
+
cb(value);
|
|
1226
|
+
});
|
|
1227
|
+
}
|
|
1228
|
+
return ok$2(void 0);
|
|
1229
|
+
});
|
|
1230
|
+
}
|
|
1231
|
+
async afterRequestHooks(_options) {
|
|
1232
|
+
}
|
|
1233
|
+
}
|
|
1234
|
+
class FetchNetworkCommand extends NetworkCommand {
|
|
1235
|
+
constructor(services) {
|
|
1236
|
+
super(services);
|
|
1237
|
+
this.services = services;
|
|
1238
|
+
}
|
|
1239
|
+
fetch() {
|
|
1240
|
+
return this.convertFetchResponseToData(this.services.fetch(...this.fetchParams));
|
|
1241
|
+
}
|
|
1242
|
+
async coerceError(errorResponse) {
|
|
1243
|
+
return toError(errorResponse.statusText);
|
|
1244
|
+
}
|
|
1245
|
+
convertFetchResponseToData(response) {
|
|
1246
|
+
return response.then(
|
|
1247
|
+
(response2) => {
|
|
1248
|
+
if (response2.ok) {
|
|
1249
|
+
return response2.json().then(
|
|
1250
|
+
(json) => ok$2(json),
|
|
1251
|
+
(reason) => err$1(toError(reason))
|
|
1252
|
+
).finally(() => {
|
|
1253
|
+
try {
|
|
1254
|
+
this.afterRequestHooks({ statusCode: response2.status });
|
|
1255
|
+
} catch (e) {
|
|
1256
|
+
}
|
|
1257
|
+
});
|
|
1258
|
+
} else {
|
|
1259
|
+
return this.coerceError(response2).then((coercedError) => {
|
|
1260
|
+
return err$1(coercedError);
|
|
1261
|
+
}).finally(() => {
|
|
1262
|
+
try {
|
|
1263
|
+
this.afterRequestHooks({ statusCode: response2.status });
|
|
1264
|
+
} catch (e) {
|
|
1265
|
+
}
|
|
1266
|
+
});
|
|
1267
|
+
}
|
|
1268
|
+
},
|
|
1269
|
+
(reason) => err$1(toError(reason))
|
|
1270
|
+
);
|
|
1271
|
+
}
|
|
1272
|
+
}
|
|
1273
|
+
function buildServiceDescriptor$e() {
|
|
1274
|
+
return {
|
|
1275
|
+
type: "fetchNetworkCommandBaseClass",
|
|
1276
|
+
version: "1.0",
|
|
1277
|
+
service: FetchNetworkCommand
|
|
1278
|
+
};
|
|
1279
|
+
}
|
|
1280
|
+
|
|
1281
|
+
/*!
|
|
1282
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
1283
|
+
* All rights reserved.
|
|
1284
|
+
* For full license text, see the LICENSE.txt file
|
|
1285
|
+
*/
|
|
1286
|
+
class StreamingCommand extends BaseCommand {
|
|
1287
|
+
constructor(services) {
|
|
1288
|
+
super();
|
|
1289
|
+
this.services = services;
|
|
1290
|
+
}
|
|
1291
|
+
execute() {
|
|
1292
|
+
return this.convertFetchStreamResponseToData(this.services.fetch(...this.fetchParams));
|
|
1293
|
+
}
|
|
1294
|
+
convertFetchStreamResponseToData(response) {
|
|
1295
|
+
return response.then(
|
|
1296
|
+
(response2) => {
|
|
1297
|
+
if (response2.ok && response2.body) {
|
|
1298
|
+
return ok$2(this.decodeByteStream(response2.body));
|
|
1299
|
+
} else {
|
|
1300
|
+
return err$1(
|
|
1301
|
+
toError(response2.ok ? "Response body not present" : response2.statusText)
|
|
1302
|
+
);
|
|
1303
|
+
}
|
|
1304
|
+
},
|
|
1305
|
+
(reason) => err$1(toError(reason))
|
|
1306
|
+
);
|
|
1307
|
+
}
|
|
1308
|
+
}
|
|
1309
|
+
function buildServiceDescriptor$d() {
|
|
1310
|
+
return {
|
|
1311
|
+
type: "streamingCommandBaseClass",
|
|
1312
|
+
version: "1.0",
|
|
1313
|
+
service: StreamingCommand
|
|
1314
|
+
};
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1317
|
+
/*!
|
|
1318
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
1319
|
+
* All rights reserved.
|
|
1320
|
+
* For full license text, see the LICENSE.txt file
|
|
1321
|
+
*/
|
|
1322
|
+
class SSECommand extends StreamingCommand {
|
|
1323
|
+
constructor(services) {
|
|
1324
|
+
super(services);
|
|
1325
|
+
this.services = services;
|
|
1326
|
+
}
|
|
1327
|
+
/**
|
|
1328
|
+
* Decodes the bytes returned by fetch into the correct shape.
|
|
1329
|
+
*
|
|
1330
|
+
* @param byteStream ReadableStream<Uint8Array> returned by fetch
|
|
1331
|
+
* @returns ReadableStream<StreamedData> that will be the result of the Command
|
|
1332
|
+
*/
|
|
1333
|
+
decodeByteStream(byteStream) {
|
|
1334
|
+
return this.decodeSSEStream(
|
|
1335
|
+
byteStream.pipeThrough(new TextDecoderStream()).pipeThrough(new SSEParsingStream())
|
|
1336
|
+
);
|
|
1337
|
+
}
|
|
1338
|
+
}
|
|
1339
|
+
const sseRegex = /^(?<field>[^:]*?)(: ?(?<value>.*?))?(?:\r\n?|\n)/;
|
|
1340
|
+
class SSEParsingStream extends TransformStream {
|
|
1341
|
+
constructor() {
|
|
1342
|
+
let ignoreLeadingLF = false, partialLine = "", data = "", event = "", id = "", retry;
|
|
1343
|
+
super({
|
|
1344
|
+
transform(chunk, controller) {
|
|
1345
|
+
if (ignoreLeadingLF && chunk.startsWith("\n")) {
|
|
1346
|
+
chunk = chunk.slice(1);
|
|
1347
|
+
}
|
|
1348
|
+
ignoreLeadingLF = chunk.endsWith("\r");
|
|
1349
|
+
let text = partialLine + chunk;
|
|
1350
|
+
let match;
|
|
1351
|
+
while (match = text.match(sseRegex)) {
|
|
1352
|
+
text = text.slice(match[0].length);
|
|
1353
|
+
if (match.groups.field === "" && match.groups.value === void 0) {
|
|
1354
|
+
if (data === "") {
|
|
1355
|
+
event = "";
|
|
1356
|
+
continue;
|
|
1357
|
+
}
|
|
1358
|
+
if (data.endsWith("\n")) {
|
|
1359
|
+
data = data.slice(0, -1);
|
|
1360
|
+
}
|
|
1361
|
+
const sseMessage = {
|
|
1362
|
+
data,
|
|
1363
|
+
id,
|
|
1364
|
+
// default event value is "message"
|
|
1365
|
+
event: event || "message"
|
|
1366
|
+
};
|
|
1367
|
+
if (retry !== void 0) {
|
|
1368
|
+
sseMessage.retry = retry;
|
|
1369
|
+
}
|
|
1370
|
+
controller.enqueue(sseMessage);
|
|
1371
|
+
data = event = "";
|
|
1372
|
+
} else if (match.groups.field === "data") {
|
|
1373
|
+
data += (match.groups.value || "") + "\n";
|
|
1374
|
+
} else if (match.groups.field === "event") {
|
|
1375
|
+
event = match.groups.value || "";
|
|
1376
|
+
} else if (match.groups.field === "id") {
|
|
1377
|
+
const idValue = match.groups.value || "";
|
|
1378
|
+
if (!idValue.includes("\0")) {
|
|
1379
|
+
id = idValue;
|
|
1380
|
+
}
|
|
1381
|
+
} else if (match.groups.field === "retry") {
|
|
1382
|
+
if (/^\d+$/.exec(match.groups.value)) {
|
|
1383
|
+
retry = parseInt(match.groups.value);
|
|
1384
|
+
}
|
|
1385
|
+
} else ;
|
|
1386
|
+
}
|
|
1387
|
+
partialLine = text;
|
|
1388
|
+
}
|
|
1389
|
+
});
|
|
1390
|
+
}
|
|
1391
|
+
}
|
|
1392
|
+
function buildServiceDescriptor$c() {
|
|
1393
|
+
return {
|
|
1394
|
+
type: "SSECommandBaseClass",
|
|
1395
|
+
version: "1.0",
|
|
1396
|
+
service: SSECommand
|
|
1397
|
+
};
|
|
1398
|
+
}
|
|
1399
|
+
|
|
1400
|
+
/*!
|
|
1401
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
1402
|
+
* All rights reserved.
|
|
1403
|
+
* For full license text, see the LICENSE.txt file
|
|
1404
|
+
*/
|
|
1405
|
+
function buildInstrumentCommand(services) {
|
|
1406
|
+
const meter = services.instrumentation.metrics.getMeter("onestore");
|
|
1407
|
+
return function instrumentCommand(commandClass, commandName) {
|
|
1408
|
+
const invocationCounter = meter.createCounter(`${commandName}.command.invocation.count`);
|
|
1409
|
+
const errorCounter = meter.createCounter(`${commandName}.command.error.count`);
|
|
1410
|
+
const durationHistogram = meter.createHistogram(`${commandName}.command.duration`);
|
|
1411
|
+
return class extends commandClass {
|
|
1412
|
+
execute(...args) {
|
|
1413
|
+
invocationCounter.add(1);
|
|
1414
|
+
let result;
|
|
1415
|
+
const start = services.instrumentation.currentTimeMs();
|
|
1416
|
+
function recordDuration() {
|
|
1417
|
+
const end = services.instrumentation.currentTimeMs();
|
|
1418
|
+
durationHistogram.record(end - start);
|
|
1419
|
+
}
|
|
1420
|
+
try {
|
|
1421
|
+
result = super.execute(...args);
|
|
1422
|
+
} catch (e) {
|
|
1423
|
+
errorCounter.add(1);
|
|
1424
|
+
throw e;
|
|
1425
|
+
}
|
|
1426
|
+
if (typeof result === "object" && result !== null && "then" in result) {
|
|
1427
|
+
result.then(recordDuration, () => {
|
|
1428
|
+
errorCounter.add(1);
|
|
1429
|
+
});
|
|
1430
|
+
} else {
|
|
1431
|
+
recordDuration();
|
|
1432
|
+
}
|
|
1433
|
+
return result;
|
|
1434
|
+
}
|
|
1435
|
+
};
|
|
1436
|
+
};
|
|
1437
|
+
}
|
|
1438
|
+
function buildServiceDescriptor$b(instrumentation) {
|
|
1439
|
+
return {
|
|
1440
|
+
type: "instrumentCommand",
|
|
1441
|
+
version: "1.0",
|
|
1442
|
+
service: buildInstrumentCommand({ instrumentation })
|
|
1443
|
+
};
|
|
1444
|
+
}
|
|
1445
|
+
|
|
1446
|
+
/*!
|
|
1447
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
1448
|
+
* All rights reserved.
|
|
1449
|
+
* For full license text, see the LICENSE.txt file
|
|
1450
|
+
*/
|
|
1451
|
+
/*!
|
|
1452
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
1453
|
+
* All rights reserved.
|
|
1454
|
+
* For full license text, see the LICENSE.txt file
|
|
1455
|
+
*/
|
|
1456
|
+
class O11yOTelTraceAPI {
|
|
1457
|
+
constructor(services) {
|
|
1458
|
+
this.services = services;
|
|
1459
|
+
}
|
|
1460
|
+
getTracer(name, _version, _options) {
|
|
1461
|
+
const o11yInstrumentation = getInstrumentation(name);
|
|
1462
|
+
return new O11yTracer(o11yInstrumentation, this.services.logger);
|
|
1463
|
+
}
|
|
1464
|
+
}
|
|
1465
|
+
class O11yTracer {
|
|
1466
|
+
constructor(o11yInstrumentation, logger) {
|
|
1467
|
+
this.o11yInstrumentation = o11yInstrumentation;
|
|
1468
|
+
this.logger = logger;
|
|
1469
|
+
}
|
|
1470
|
+
startSpan(name, _options, context) {
|
|
1471
|
+
const traceId = context == null ? void 0 : context.getValue(Symbol.for("traceId"));
|
|
1472
|
+
const spanId = context == null ? void 0 : context.getValue(Symbol.for("spanId"));
|
|
1473
|
+
const traceFlags = context == null ? void 0 : context.getValue(Symbol.for("traceFlags"));
|
|
1474
|
+
let spanContext = void 0;
|
|
1475
|
+
if (traceId !== void 0 && spanId !== void 0 && traceFlags !== void 0) {
|
|
1476
|
+
spanContext = {
|
|
1477
|
+
traceId,
|
|
1478
|
+
spanId,
|
|
1479
|
+
traceFlags
|
|
1480
|
+
};
|
|
1481
|
+
}
|
|
1482
|
+
const activityOptions = spanContext === void 0 ? void 0 : {
|
|
1483
|
+
instrumentationContext: {
|
|
1484
|
+
rootId: spanContext.traceId,
|
|
1485
|
+
isRootActivitySampled: spanContext.traceFlags === 1,
|
|
1486
|
+
parentId: spanId
|
|
1487
|
+
}
|
|
1488
|
+
};
|
|
1489
|
+
const activity = this.o11yInstrumentation.startActivity(name, activityOptions);
|
|
1490
|
+
return new O11ySpan(activity, this.logger);
|
|
1491
|
+
}
|
|
1492
|
+
startActiveSpan(name, options, context, fn) {
|
|
1493
|
+
let opts;
|
|
1494
|
+
let ctx;
|
|
1495
|
+
let fun;
|
|
1496
|
+
if (typeof options === "function") {
|
|
1497
|
+
fun = options;
|
|
1498
|
+
} else {
|
|
1499
|
+
opts = options;
|
|
1500
|
+
if (typeof context === "function") {
|
|
1501
|
+
fun = context;
|
|
1502
|
+
} else {
|
|
1503
|
+
ctx = context;
|
|
1504
|
+
fun = fn;
|
|
1505
|
+
}
|
|
1506
|
+
}
|
|
1507
|
+
const span = this.startSpan(name, opts, ctx);
|
|
1508
|
+
return fun(span);
|
|
1509
|
+
}
|
|
1510
|
+
}
|
|
1511
|
+
class O11ySpan {
|
|
1512
|
+
constructor(activity, logger) {
|
|
1513
|
+
this.activity = activity;
|
|
1514
|
+
this.logger = logger;
|
|
1515
|
+
this._isRecording = true;
|
|
1516
|
+
this.attributes = {};
|
|
1517
|
+
}
|
|
1518
|
+
spanContext() {
|
|
1519
|
+
return {
|
|
1520
|
+
traceId: this.activity.getRootId(),
|
|
1521
|
+
spanId: this.activity.getId(),
|
|
1522
|
+
traceFlags: this.activity.getIsSampled() ? 1 : 0
|
|
1523
|
+
};
|
|
1524
|
+
}
|
|
1525
|
+
setAttribute(key, value) {
|
|
1526
|
+
this.attributes[key] = value;
|
|
1527
|
+
return this;
|
|
1528
|
+
}
|
|
1529
|
+
setAttributes(attributes) {
|
|
1530
|
+
this.attributes = { ...this.attributes, ...attributes };
|
|
1531
|
+
return this;
|
|
1532
|
+
}
|
|
1533
|
+
addEvent(_name, _attributesOrStartTime, _startTime) {
|
|
1534
|
+
this.logger.warn("O11ySpan does not support addEvents.");
|
|
1535
|
+
return this;
|
|
1536
|
+
}
|
|
1537
|
+
setStatus(_status) {
|
|
1538
|
+
this.logger.warn("O11ySpan does not support setStatus.");
|
|
1539
|
+
return this;
|
|
1540
|
+
}
|
|
1541
|
+
updateName(_name) {
|
|
1542
|
+
this.logger.warn("O11ySpan does not support updateName.");
|
|
1543
|
+
return this;
|
|
1544
|
+
}
|
|
1545
|
+
end(endTime) {
|
|
1546
|
+
let endTimeInternal = endTime;
|
|
1547
|
+
try {
|
|
1548
|
+
if (typeof endTimeInternal !== "number") {
|
|
1549
|
+
this.logger.warn("O11ySpan does not support non number endTime override.");
|
|
1550
|
+
endTimeInternal = void 0;
|
|
1551
|
+
}
|
|
1552
|
+
this.activity.stop(
|
|
1553
|
+
void 0,
|
|
1554
|
+
void 0,
|
|
1555
|
+
endTimeInternal ? { perfStopOverride: endTimeInternal } : void 0
|
|
1556
|
+
);
|
|
1557
|
+
} finally {
|
|
1558
|
+
this._isRecording = false;
|
|
1559
|
+
}
|
|
1560
|
+
return;
|
|
1561
|
+
}
|
|
1562
|
+
isRecording() {
|
|
1563
|
+
return this._isRecording;
|
|
1564
|
+
}
|
|
1565
|
+
recordException(exception, _time) {
|
|
1566
|
+
this.activity.error(exception);
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
class O11yOTelMetricsAPI {
|
|
1570
|
+
constructor(services) {
|
|
1571
|
+
this.services = services;
|
|
1572
|
+
}
|
|
1573
|
+
getMeter(name, _version, __options) {
|
|
1574
|
+
const o11yInstrumentation = getInstrumentation(name);
|
|
1575
|
+
return new O11yMeter(o11yInstrumentation, this.services.logger);
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
class O11yMeter {
|
|
1579
|
+
constructor(o11yInstrumentation, logger) {
|
|
1580
|
+
this.o11yInstrumentation = o11yInstrumentation;
|
|
1581
|
+
this.logger = logger;
|
|
1582
|
+
}
|
|
1583
|
+
createHistogram(name, _options) {
|
|
1584
|
+
return new O11yHistogram(name, this.o11yInstrumentation, this.logger);
|
|
1585
|
+
}
|
|
1586
|
+
createCounter(name, options) {
|
|
1587
|
+
if (options) {
|
|
1588
|
+
this.logger.warn("counter options not supported in O11y instrumentation");
|
|
1589
|
+
}
|
|
1590
|
+
return new O11yCounter(name, this.o11yInstrumentation, this.logger);
|
|
1591
|
+
}
|
|
1592
|
+
createUpDownCounter(_name, _options) {
|
|
1593
|
+
return new O11yUpDownCounter(this.logger);
|
|
1594
|
+
}
|
|
1595
|
+
createObservableGauge(_name, _options) {
|
|
1596
|
+
return new O11yObservableGuage(this.logger);
|
|
1597
|
+
}
|
|
1598
|
+
createObservableCounter(_name, _options) {
|
|
1599
|
+
return new O11yObservableCounter(this.logger);
|
|
1600
|
+
}
|
|
1601
|
+
createObservableUpDownCounter(_name, _options) {
|
|
1602
|
+
return new O11yObservableUpDownCounter(this.logger);
|
|
1603
|
+
}
|
|
1604
|
+
addBatchObservableCallback(_callback, _observables) {
|
|
1605
|
+
this.logger.warn("addBatchObservableCallback not supported yet");
|
|
1606
|
+
}
|
|
1607
|
+
removeBatchObservableCallback(_callback, _observables) {
|
|
1608
|
+
this.logger.warn("removeBatchObservableCallback not supported yet");
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
class O11yCounter {
|
|
1612
|
+
constructor(name, o11yInstrumentation, logger) {
|
|
1613
|
+
this.name = name;
|
|
1614
|
+
this.o11yInstrumentation = o11yInstrumentation;
|
|
1615
|
+
this.logger = logger;
|
|
1616
|
+
}
|
|
1617
|
+
add(value, attributes, _context) {
|
|
1618
|
+
if (value < 0) {
|
|
1619
|
+
this.logger.warn(`Counter values must be non-negative. Got ${value}.`);
|
|
1620
|
+
return;
|
|
1621
|
+
}
|
|
1622
|
+
this.o11yInstrumentation.incrementCounter(
|
|
1623
|
+
this.name,
|
|
1624
|
+
value,
|
|
1625
|
+
void 0,
|
|
1626
|
+
sanitizeAttributes(attributes)
|
|
1627
|
+
);
|
|
1628
|
+
}
|
|
1629
|
+
}
|
|
1630
|
+
class O11yHistogram {
|
|
1631
|
+
constructor(name, o11yInstrumentation, logger) {
|
|
1632
|
+
this.name = name;
|
|
1633
|
+
this.o11yInstrumentation = o11yInstrumentation;
|
|
1634
|
+
this.logger = logger;
|
|
1635
|
+
}
|
|
1636
|
+
record(value, attributes, _context) {
|
|
1637
|
+
if (value < 0) {
|
|
1638
|
+
this.logger.warn(`Histogram values must be non-negative. Got ${value}.`);
|
|
1639
|
+
return;
|
|
1640
|
+
}
|
|
1641
|
+
this.o11yInstrumentation.trackValue(
|
|
1642
|
+
this.name,
|
|
1643
|
+
value,
|
|
1644
|
+
void 0,
|
|
1645
|
+
sanitizeAttributes(attributes)
|
|
1646
|
+
);
|
|
1647
|
+
}
|
|
1648
|
+
}
|
|
1649
|
+
class O11yUpDownCounter {
|
|
1650
|
+
constructor(logger) {
|
|
1651
|
+
this.logger = logger;
|
|
1652
|
+
}
|
|
1653
|
+
add(_value, _attributes, _context) {
|
|
1654
|
+
this.logger.warn("O11yUpDownCounter not supported yet.");
|
|
1655
|
+
}
|
|
1656
|
+
}
|
|
1657
|
+
class O11yObservableCounter {
|
|
1658
|
+
constructor(logger) {
|
|
1659
|
+
this.logger = logger;
|
|
1660
|
+
}
|
|
1661
|
+
addCallback(_callback) {
|
|
1662
|
+
this.logger.warn("O11yObservableCounter not supported yet. Defaulting to noop.");
|
|
1663
|
+
}
|
|
1664
|
+
removeCallback(_callback) {
|
|
1665
|
+
this.logger.warn("O11yObservableCounter not supported yet. Defaulting to noop.");
|
|
1666
|
+
}
|
|
1667
|
+
}
|
|
1668
|
+
class O11yObservableGuage {
|
|
1669
|
+
constructor(logger) {
|
|
1670
|
+
this.logger = logger;
|
|
1671
|
+
}
|
|
1672
|
+
addCallback(_callback) {
|
|
1673
|
+
this.logger.warn("O11yObservableGuage not supported yet. Defaulting to noop.");
|
|
1674
|
+
}
|
|
1675
|
+
removeCallback(_callback) {
|
|
1676
|
+
this.logger.warn("O11yObservableGuage not supported yet. Defaulting to noop.");
|
|
1677
|
+
}
|
|
1678
|
+
}
|
|
1679
|
+
class O11yObservableUpDownCounter {
|
|
1680
|
+
constructor(logger) {
|
|
1681
|
+
this.logger = logger;
|
|
1682
|
+
}
|
|
1683
|
+
addCallback(_callback) {
|
|
1684
|
+
this.logger.warn("O11yObservableUpDownCounter not supported yet. Defaulting to noop.");
|
|
1685
|
+
}
|
|
1686
|
+
removeCallback(_callback) {
|
|
1687
|
+
this.logger.warn("O11yObservableUpDownCounter not supported yet. Defaulting to noop.");
|
|
1688
|
+
}
|
|
1689
|
+
}
|
|
1690
|
+
function sanitizeAttributes(attributes) {
|
|
1691
|
+
if (!attributes) return;
|
|
1692
|
+
const metricTags = {};
|
|
1693
|
+
Object.entries(attributes).forEach(([key, value]) => {
|
|
1694
|
+
if (value !== void 0 && !Array.isArray(value)) {
|
|
1695
|
+
metricTags[key] = value;
|
|
1696
|
+
}
|
|
1697
|
+
});
|
|
1698
|
+
return metricTags;
|
|
1699
|
+
}
|
|
1700
|
+
class O11yInstrumentation {
|
|
1701
|
+
constructor(services) {
|
|
1702
|
+
this.services = services;
|
|
1703
|
+
this.currentTimeMs = () => performance.now();
|
|
1704
|
+
this.trace = new O11yOTelTraceAPI(this.services);
|
|
1705
|
+
this.metrics = new O11yOTelMetricsAPI(this.services);
|
|
1706
|
+
}
|
|
1707
|
+
}
|
|
1708
|
+
function buildServiceDescriptor$a(logger) {
|
|
1709
|
+
return {
|
|
1710
|
+
type: "instrumentation",
|
|
1711
|
+
version: "1.0",
|
|
1712
|
+
service: new O11yInstrumentation({ logger })
|
|
1713
|
+
};
|
|
1714
|
+
}
|
|
1715
|
+
|
|
1716
|
+
/*!
|
|
1717
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
1718
|
+
* All rights reserved.
|
|
1719
|
+
* For full license text, see the LICENSE.txt file
|
|
1720
|
+
*/
|
|
1721
|
+
/*!
|
|
1722
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
1723
|
+
* All rights reserved.
|
|
1724
|
+
* For full license text, see the LICENSE.txt file
|
|
1725
|
+
*/
|
|
1726
|
+
const { stringify, parse } = JSON;
|
|
1727
|
+
function deepCopy(x) {
|
|
1728
|
+
const stringified = stringify(x);
|
|
1729
|
+
return stringified ? parse(stringified) : void 0;
|
|
1730
|
+
}
|
|
1731
|
+
class DefaultRecordableCache {
|
|
1732
|
+
constructor(baseCache) {
|
|
1733
|
+
this.baseCache = baseCache;
|
|
1734
|
+
this.keysRead = /* @__PURE__ */ new Set();
|
|
1735
|
+
this.missingKeysRead = /* @__PURE__ */ new Set();
|
|
1736
|
+
this.keysUpdated = /* @__PURE__ */ new Set();
|
|
1737
|
+
this.metadataKeysUpdated = /* @__PURE__ */ new Set();
|
|
1738
|
+
}
|
|
1739
|
+
delete(key) {
|
|
1740
|
+
this.keysUpdated.add(key);
|
|
1741
|
+
this.baseCache.delete(key);
|
|
1742
|
+
}
|
|
1743
|
+
get(key, options) {
|
|
1744
|
+
this.keysRead.add(key);
|
|
1745
|
+
const value = this.baseCache.get(key);
|
|
1746
|
+
if (value === void 0) {
|
|
1747
|
+
this.missingKeysRead.add(key);
|
|
1748
|
+
}
|
|
1749
|
+
if (options == null ? void 0 : options.copy) {
|
|
1750
|
+
return deepCopy(value);
|
|
1751
|
+
}
|
|
1752
|
+
return value;
|
|
1753
|
+
}
|
|
1754
|
+
set(key, value) {
|
|
1755
|
+
this.keysUpdated.add(key);
|
|
1756
|
+
this.metadataKeysUpdated.add(key);
|
|
1757
|
+
this.baseCache.set(key, value);
|
|
1758
|
+
}
|
|
1759
|
+
setMetadata(key, cacheControlMetadata) {
|
|
1760
|
+
this.metadataKeysUpdated.add(key);
|
|
1761
|
+
this.baseCache.setMetadata(key, cacheControlMetadata);
|
|
1762
|
+
}
|
|
1763
|
+
length() {
|
|
1764
|
+
return this.baseCache.length();
|
|
1765
|
+
}
|
|
1766
|
+
keys() {
|
|
1767
|
+
return this.baseCache.keys();
|
|
1768
|
+
}
|
|
1769
|
+
entries() {
|
|
1770
|
+
return this.baseCache.entries();
|
|
1771
|
+
}
|
|
1772
|
+
record() {
|
|
1773
|
+
return new DefaultRecordableCache(this);
|
|
1774
|
+
}
|
|
1775
|
+
filter(predicate) {
|
|
1776
|
+
return new DefaultFilteredCache(this, predicate);
|
|
1777
|
+
}
|
|
1778
|
+
buildFixedTimeWritableCache(generatedTime) {
|
|
1779
|
+
return new FixedTimeWritableCache(this, generatedTime);
|
|
1780
|
+
}
|
|
1781
|
+
}
|
|
1782
|
+
class DefaultFilteredCache {
|
|
1783
|
+
constructor(baseCache, predicate) {
|
|
1784
|
+
this.baseCache = baseCache;
|
|
1785
|
+
this.predicate = predicate;
|
|
1786
|
+
}
|
|
1787
|
+
delete(key) {
|
|
1788
|
+
this.baseCache.delete(key);
|
|
1789
|
+
}
|
|
1790
|
+
get(key, options) {
|
|
1791
|
+
const result = this.baseCache.get(key);
|
|
1792
|
+
if (result && this.predicate(key, result)) {
|
|
1793
|
+
if (options == null ? void 0 : options.copy) {
|
|
1794
|
+
return deepCopy(result);
|
|
1795
|
+
}
|
|
1796
|
+
return result;
|
|
1797
|
+
}
|
|
1798
|
+
return void 0;
|
|
1799
|
+
}
|
|
1800
|
+
set(key, value) {
|
|
1801
|
+
this.baseCache.set(key, value);
|
|
1802
|
+
}
|
|
1803
|
+
setMetadata(key, cacheControlMetadata) {
|
|
1804
|
+
this.baseCache.setMetadata(key, cacheControlMetadata);
|
|
1805
|
+
}
|
|
1806
|
+
length() {
|
|
1807
|
+
return this.getFilteredKeys().size;
|
|
1808
|
+
}
|
|
1809
|
+
keys() {
|
|
1810
|
+
return this.getFilteredKeys();
|
|
1811
|
+
}
|
|
1812
|
+
entries() {
|
|
1813
|
+
return this.getFilteredEntries();
|
|
1814
|
+
}
|
|
1815
|
+
record() {
|
|
1816
|
+
return new DefaultRecordableCache(this);
|
|
1817
|
+
}
|
|
1818
|
+
filter(predicate) {
|
|
1819
|
+
return new DefaultFilteredCache(this, predicate);
|
|
1820
|
+
}
|
|
1821
|
+
getFilteredEntries() {
|
|
1822
|
+
return this.baseCache.entries().filter(([key, _value]) => {
|
|
1823
|
+
return this.get(key);
|
|
1824
|
+
});
|
|
1825
|
+
}
|
|
1826
|
+
getFilteredKeys() {
|
|
1827
|
+
const filteredKeySet = /* @__PURE__ */ new Set();
|
|
1828
|
+
this.baseCache.keys().forEach((key) => {
|
|
1829
|
+
if (this.get(key)) {
|
|
1830
|
+
filteredKeySet.add(key);
|
|
1831
|
+
}
|
|
1832
|
+
});
|
|
1833
|
+
return filteredKeySet;
|
|
1834
|
+
}
|
|
1835
|
+
buildFixedTimeWritableCache(generatedTime) {
|
|
1836
|
+
return new FixedTimeWritableCache(this, generatedTime);
|
|
1837
|
+
}
|
|
1838
|
+
}
|
|
1839
|
+
class FixedTimeWritableCache {
|
|
1840
|
+
constructor(baseCache, generatedTime) {
|
|
1841
|
+
this.baseCache = baseCache;
|
|
1842
|
+
this.generatedTime = generatedTime;
|
|
1843
|
+
}
|
|
1844
|
+
delete(key) {
|
|
1845
|
+
this.baseCache.delete(key);
|
|
1846
|
+
}
|
|
1847
|
+
get(key, options) {
|
|
1848
|
+
return this.baseCache.get(key, options);
|
|
1849
|
+
}
|
|
1850
|
+
set(key, value) {
|
|
1851
|
+
this.baseCache.set(key, {
|
|
1852
|
+
...value,
|
|
1853
|
+
metadata: {
|
|
1854
|
+
...value.metadata,
|
|
1855
|
+
cacheControl: { ...value.metadata.cacheControl, generatedTime: this.generatedTime }
|
|
1856
|
+
}
|
|
1857
|
+
});
|
|
1858
|
+
}
|
|
1859
|
+
setMetadata(key, cacheControlMetadata) {
|
|
1860
|
+
this.baseCache.setMetadata(key, {
|
|
1861
|
+
...cacheControlMetadata,
|
|
1862
|
+
generatedTime: this.generatedTime
|
|
1863
|
+
});
|
|
1864
|
+
}
|
|
1865
|
+
length() {
|
|
1866
|
+
return this.baseCache.length();
|
|
1867
|
+
}
|
|
1868
|
+
keys() {
|
|
1869
|
+
return this.baseCache.keys();
|
|
1870
|
+
}
|
|
1871
|
+
entries() {
|
|
1872
|
+
return this.baseCache.entries();
|
|
1873
|
+
}
|
|
1874
|
+
record() {
|
|
1875
|
+
return new DefaultRecordableCache(this);
|
|
1876
|
+
}
|
|
1877
|
+
filter(predicate) {
|
|
1878
|
+
return new DefaultFilteredCache(this, predicate);
|
|
1879
|
+
}
|
|
1880
|
+
buildFixedTimeWritableCache(generatedTime) {
|
|
1881
|
+
return new FixedTimeWritableCache(this, generatedTime);
|
|
1882
|
+
}
|
|
1883
|
+
}
|
|
1884
|
+
class DefaultCache {
|
|
1885
|
+
constructor() {
|
|
1886
|
+
this.data = {};
|
|
1887
|
+
}
|
|
1888
|
+
get(key, options) {
|
|
1889
|
+
if (options == null ? void 0 : options.copy) {
|
|
1890
|
+
return deepCopy(this.data[key]);
|
|
1891
|
+
}
|
|
1892
|
+
return this.data[key];
|
|
1893
|
+
}
|
|
1894
|
+
/**
|
|
1895
|
+
* Adds the specified key/value to the cache.
|
|
1896
|
+
*
|
|
1897
|
+
* @param key key at which to store value
|
|
1898
|
+
* @param entry value to be stored
|
|
1899
|
+
*/
|
|
1900
|
+
set(key, entry) {
|
|
1901
|
+
if (entry.metadata.cacheControl.type === "no-store") {
|
|
1902
|
+
return;
|
|
1903
|
+
}
|
|
1904
|
+
this.data[key] = {
|
|
1905
|
+
...entry,
|
|
1906
|
+
metadata: {
|
|
1907
|
+
...entry.metadata,
|
|
1908
|
+
type: entry.metadata.type || {
|
|
1909
|
+
namespace: "OneStore:Internal",
|
|
1910
|
+
name: "UnknownType"
|
|
1911
|
+
},
|
|
1912
|
+
cacheControl: {
|
|
1913
|
+
generatedTime: Date.now() / 1e3,
|
|
1914
|
+
...entry.metadata.cacheControl
|
|
1915
|
+
}
|
|
1916
|
+
}
|
|
1917
|
+
};
|
|
1918
|
+
}
|
|
1919
|
+
/**
|
|
1920
|
+
* Removes the cache entry associated with the specified key.
|
|
1921
|
+
*
|
|
1922
|
+
* @param key key to be removed from the store
|
|
1923
|
+
*/
|
|
1924
|
+
delete(key) {
|
|
1925
|
+
delete this.data[key];
|
|
1926
|
+
}
|
|
1927
|
+
/**
|
|
1928
|
+
* Sets the metadata for the specified key if the key is in cache.
|
|
1929
|
+
* If the key doesn't exist, it does nothing.
|
|
1930
|
+
*
|
|
1931
|
+
* @param key key at which to store metadata
|
|
1932
|
+
* @param cacheControlMetadata metadata to be stored
|
|
1933
|
+
*/
|
|
1934
|
+
setMetadata(key, cacheControlMetadata) {
|
|
1935
|
+
if (key in this.data) {
|
|
1936
|
+
this.data[key].metadata.cacheControl = {
|
|
1937
|
+
generatedTime: Date.now() / 1e3,
|
|
1938
|
+
...cacheControlMetadata
|
|
1939
|
+
};
|
|
1940
|
+
}
|
|
1941
|
+
}
|
|
1942
|
+
length() {
|
|
1943
|
+
return this.keys().size;
|
|
1944
|
+
}
|
|
1945
|
+
keys() {
|
|
1946
|
+
return new Set(Object.keys(this.data));
|
|
1947
|
+
}
|
|
1948
|
+
entries() {
|
|
1949
|
+
return Object.entries(this.data);
|
|
1950
|
+
}
|
|
1951
|
+
record() {
|
|
1952
|
+
return new DefaultRecordableCache(this);
|
|
1953
|
+
}
|
|
1954
|
+
filter(predicate) {
|
|
1955
|
+
return new DefaultFilteredCache(this, predicate);
|
|
1956
|
+
}
|
|
1957
|
+
buildFixedTimeWritableCache(generatedTime) {
|
|
1958
|
+
return new FixedTimeWritableCache(this, generatedTime);
|
|
1959
|
+
}
|
|
1960
|
+
}
|
|
1961
|
+
function buildServiceDescriptor$9() {
|
|
1962
|
+
return {
|
|
1963
|
+
type: "cache",
|
|
1964
|
+
version: "1.0",
|
|
1965
|
+
service: new DefaultCache()
|
|
1966
|
+
};
|
|
1967
|
+
}
|
|
1968
|
+
|
|
1969
|
+
/*!
|
|
1970
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
1971
|
+
* All rights reserved.
|
|
1972
|
+
* For full license text, see the LICENSE.txt file
|
|
1973
|
+
*/
|
|
1974
|
+
class CacheControlStrategy {
|
|
1975
|
+
constructor(services, config, requestRunner) {
|
|
1976
|
+
this.services = services;
|
|
1977
|
+
this.config = config;
|
|
1978
|
+
this.requestRunner = requestRunner;
|
|
1979
|
+
this.filteredCache = this.services.cache.filter((_, entry) => {
|
|
1980
|
+
const { cacheControl } = entry.metadata;
|
|
1981
|
+
return !this.expiredChecks.some((check) => check(cacheControl));
|
|
1982
|
+
});
|
|
1983
|
+
}
|
|
1984
|
+
get expiredChecks() {
|
|
1985
|
+
return [
|
|
1986
|
+
(cacheControlMetadata) => cacheControlMetadata.type === "max-age" && this.config.now > cacheControlMetadata.generatedTime + cacheControlMetadata.maxAge,
|
|
1987
|
+
(cacheControlMetadata) => cacheControlMetadata.type === "max-age" && cacheControlMetadata.maxAge <= 0,
|
|
1988
|
+
(cacheControlMetadata) => cacheControlMetadata.type === "no-store",
|
|
1989
|
+
(cacheControlMetadata) => cacheControlMetadata.type === "no-cache" && cacheControlMetadata.generatedTime < this.config.now
|
|
1990
|
+
];
|
|
1991
|
+
}
|
|
1992
|
+
}
|
|
1993
|
+
class NoCacheCacheControlStrategy extends CacheControlStrategy {
|
|
1994
|
+
execute() {
|
|
1995
|
+
const tempCache = this.filteredCache;
|
|
1996
|
+
return new Promise(async (resolve, reject) => {
|
|
1997
|
+
try {
|
|
1998
|
+
let readResult = ok$2(void 0);
|
|
1999
|
+
for await (const rfnResult of this.requestRunner.requestFromNetwork()) {
|
|
2000
|
+
if (rfnResult) {
|
|
2001
|
+
const result = await this.services.cacheInclusionPolicy.write({
|
|
2002
|
+
l1: tempCache,
|
|
2003
|
+
writeToL1: (l1) => this.requestRunner.writeToCache(l1, rfnResult)
|
|
2004
|
+
});
|
|
2005
|
+
if (result.isErr()) {
|
|
2006
|
+
return resolve(result);
|
|
2007
|
+
}
|
|
2008
|
+
}
|
|
2009
|
+
readResult = await this.services.cacheInclusionPolicy.read({
|
|
2010
|
+
l1: tempCache,
|
|
2011
|
+
readFromL1: (l1) => this.requestRunner.readFromCache(l1)
|
|
2012
|
+
});
|
|
2013
|
+
if (readResult.isOk()) {
|
|
2014
|
+
resolve(readResult);
|
|
2015
|
+
}
|
|
2016
|
+
}
|
|
2017
|
+
return resolve(readResult);
|
|
2018
|
+
} catch (error) {
|
|
2019
|
+
return reject(error);
|
|
2020
|
+
}
|
|
2021
|
+
});
|
|
2022
|
+
}
|
|
2023
|
+
}
|
|
2024
|
+
class MaxAgeCacheControlStrategy extends CacheControlStrategy {
|
|
2025
|
+
execute(options) {
|
|
2026
|
+
const startTime = this.services.instrumentation ? this.services.instrumentation.currentTimeMs() : 0;
|
|
2027
|
+
return this.services.cacheInclusionPolicy.read({
|
|
2028
|
+
l1: this.filteredCache,
|
|
2029
|
+
readFromL1: (l1) => this.requestRunner.readFromCache(l1)
|
|
2030
|
+
}).then((value) => {
|
|
2031
|
+
if (value.isOk()) {
|
|
2032
|
+
this.collectCacheHitInstrumentation(
|
|
2033
|
+
startTime,
|
|
2034
|
+
options == null ? void 0 : options.instrumentationAttributes
|
|
2035
|
+
);
|
|
2036
|
+
return ok$2(void 0);
|
|
2037
|
+
}
|
|
2038
|
+
this.collectCacheMissInstrumentation(startTime, options == null ? void 0 : options.instrumentationAttributes);
|
|
2039
|
+
const tempCache = this.filteredCache;
|
|
2040
|
+
return new Promise(async (resolve, reject) => {
|
|
2041
|
+
try {
|
|
2042
|
+
let readResult = ok$2(void 0);
|
|
2043
|
+
for await (const rfnResult of this.requestRunner.requestFromNetwork()) {
|
|
2044
|
+
if (rfnResult) {
|
|
2045
|
+
const result = await this.services.cacheInclusionPolicy.write({
|
|
2046
|
+
l1: tempCache,
|
|
2047
|
+
writeToL1: (l1) => this.requestRunner.writeToCache(l1, rfnResult)
|
|
2048
|
+
});
|
|
2049
|
+
if (result.isErr()) {
|
|
2050
|
+
return resolve(err$1(result.error));
|
|
2051
|
+
}
|
|
2052
|
+
}
|
|
2053
|
+
readResult = await this.services.cacheInclusionPolicy.read({
|
|
2054
|
+
l1: tempCache,
|
|
2055
|
+
readFromL1: (l1) => this.requestRunner.readFromCache(l1)
|
|
2056
|
+
});
|
|
2057
|
+
if (readResult.isOk()) {
|
|
2058
|
+
resolve(ok$2(void 0));
|
|
2059
|
+
}
|
|
2060
|
+
}
|
|
2061
|
+
return resolve(readResult);
|
|
2062
|
+
} catch (e) {
|
|
2063
|
+
return reject(e);
|
|
2064
|
+
}
|
|
2065
|
+
});
|
|
2066
|
+
});
|
|
2067
|
+
}
|
|
2068
|
+
collectCacheHitInstrumentation(startTime, instrumentationAttributes) {
|
|
2069
|
+
if (this.services.instrumentation) {
|
|
2070
|
+
const meter = this.services.instrumentation.metrics.getMeter("onestore");
|
|
2071
|
+
meter.createCounter(`command.max-age.cache-hit.count`).add(1, instrumentationAttributes);
|
|
2072
|
+
meter.createHistogram(`command.max-age.cache-hit.duration`).record(
|
|
2073
|
+
this.services.instrumentation.currentTimeMs() - startTime,
|
|
2074
|
+
instrumentationAttributes
|
|
2075
|
+
);
|
|
2076
|
+
}
|
|
2077
|
+
}
|
|
2078
|
+
collectCacheMissInstrumentation(startTime, instrumentationAttributes) {
|
|
2079
|
+
if (this.services.instrumentation) {
|
|
2080
|
+
const meter = this.services.instrumentation.metrics.getMeter("onestore");
|
|
2081
|
+
meter.createCounter(`command.max-age.cache-miss.count`).add(1, instrumentationAttributes);
|
|
2082
|
+
meter.createHistogram(`command.max-age.cache-miss.duration`).record(
|
|
2083
|
+
this.services.instrumentation.currentTimeMs() - startTime,
|
|
2084
|
+
instrumentationAttributes
|
|
2085
|
+
);
|
|
2086
|
+
}
|
|
2087
|
+
}
|
|
2088
|
+
get expiredChecks() {
|
|
2089
|
+
const config = this.config;
|
|
2090
|
+
return [
|
|
2091
|
+
...super.expiredChecks,
|
|
2092
|
+
(cacheControlMetadata) => {
|
|
2093
|
+
return cacheControlMetadata.generatedTime + config.requestMaxAge < config.now;
|
|
2094
|
+
}
|
|
2095
|
+
];
|
|
2096
|
+
}
|
|
2097
|
+
}
|
|
2098
|
+
class OnlyIfCachedCacheControlStrategy extends CacheControlStrategy {
|
|
2099
|
+
execute(options) {
|
|
2100
|
+
const startTime = this.services.instrumentation ? this.services.instrumentation.currentTimeMs() : 0;
|
|
2101
|
+
return this.services.cacheInclusionPolicy.read({
|
|
2102
|
+
l1: this.filteredCache,
|
|
2103
|
+
readFromL1: (l1) => this.requestRunner.readFromCache(l1)
|
|
2104
|
+
}).then((result) => {
|
|
2105
|
+
if (result.isOk()) {
|
|
2106
|
+
this.collectCacheHitInstrumentation(
|
|
2107
|
+
startTime,
|
|
2108
|
+
options == null ? void 0 : options.instrumentationAttributes
|
|
2109
|
+
);
|
|
2110
|
+
return ok$2(void 0);
|
|
2111
|
+
}
|
|
2112
|
+
this.collectCacheMissInstrumentation(startTime, options == null ? void 0 : options.instrumentationAttributes);
|
|
2113
|
+
const error = new UserVisibleError(
|
|
2114
|
+
new FetchResponse(HttpStatusCode.GatewayTimeout, {
|
|
2115
|
+
error: "Cache miss for only-if-cached request"
|
|
2116
|
+
})
|
|
2117
|
+
);
|
|
2118
|
+
return err$1(error);
|
|
2119
|
+
});
|
|
2120
|
+
}
|
|
2121
|
+
get expiredChecks() {
|
|
2122
|
+
return [
|
|
2123
|
+
(cacheControlMetadata) => cacheControlMetadata.type === "no-store"
|
|
2124
|
+
];
|
|
2125
|
+
}
|
|
2126
|
+
collectCacheHitInstrumentation(startTime, instrumentationAttributes) {
|
|
2127
|
+
if (this.services.instrumentation) {
|
|
2128
|
+
const meter = this.services.instrumentation.metrics.getMeter("onestore");
|
|
2129
|
+
meter.createCounter(`command.only-if-cached.cache-hit.count`).add(1, instrumentationAttributes);
|
|
2130
|
+
meter.createHistogram(
|
|
2131
|
+
`command.only-if-cached.cache-hit.duration`
|
|
2132
|
+
).record(
|
|
2133
|
+
this.services.instrumentation.currentTimeMs() - startTime,
|
|
2134
|
+
instrumentationAttributes
|
|
2135
|
+
);
|
|
2136
|
+
}
|
|
2137
|
+
}
|
|
2138
|
+
collectCacheMissInstrumentation(startTime, instrumentationAttributes) {
|
|
2139
|
+
if (this.services.instrumentation) {
|
|
2140
|
+
const meter = this.services.instrumentation.metrics.getMeter("onestore");
|
|
2141
|
+
meter.createCounter(`command.only-if-cached.cache-miss.count`).add(1, instrumentationAttributes);
|
|
2142
|
+
meter.createHistogram(
|
|
2143
|
+
`command.only-if-cached.cache-miss.duration`
|
|
2144
|
+
).record(
|
|
2145
|
+
this.services.instrumentation.currentTimeMs() - startTime,
|
|
2146
|
+
instrumentationAttributes
|
|
2147
|
+
);
|
|
2148
|
+
}
|
|
2149
|
+
}
|
|
2150
|
+
}
|
|
2151
|
+
class CacheController {
|
|
2152
|
+
constructor(services) {
|
|
2153
|
+
this.services = services;
|
|
2154
|
+
}
|
|
2155
|
+
execute(config, requestRunner, options) {
|
|
2156
|
+
const strategy = this.getCacheControlStrategy(config, requestRunner);
|
|
2157
|
+
return strategy.execute(options);
|
|
2158
|
+
}
|
|
2159
|
+
getCacheControlStrategy(config, requestRunner) {
|
|
2160
|
+
if (config.type === "max-age") {
|
|
2161
|
+
return new MaxAgeCacheControlStrategy(this.services, config, requestRunner);
|
|
2162
|
+
} else if (config.type === "no-cache") {
|
|
2163
|
+
return new NoCacheCacheControlStrategy(this.services, config, requestRunner);
|
|
2164
|
+
} else if (config.type === "only-if-cached") {
|
|
2165
|
+
return new OnlyIfCachedCacheControlStrategy(this.services, config, requestRunner);
|
|
2166
|
+
}
|
|
2167
|
+
throw new Error(`Unknown cache control strategy ${config.type}`);
|
|
2168
|
+
}
|
|
2169
|
+
/**
|
|
2170
|
+
* Finds cache entries that match the given query.
|
|
2171
|
+
* Returns an async generator that yields `[key, entry]`.
|
|
2172
|
+
*/
|
|
2173
|
+
async *find(query) {
|
|
2174
|
+
yield* this.services.cacheInclusionPolicy.find(query);
|
|
2175
|
+
}
|
|
2176
|
+
/**
|
|
2177
|
+
* Finds and modifies cache entries that match the given query.
|
|
2178
|
+
* Extends `find(query)` and returns an async generator of modified keys.
|
|
2179
|
+
*/
|
|
2180
|
+
async *findAndModify(query, cacheUpdate) {
|
|
2181
|
+
yield* this.services.cacheInclusionPolicy.findAndModify(query, cacheUpdate);
|
|
2182
|
+
}
|
|
2183
|
+
}
|
|
2184
|
+
function buildServiceDescriptor$8(cache, cacheInclusionPolicy) {
|
|
2185
|
+
return {
|
|
2186
|
+
type: "cacheController",
|
|
2187
|
+
version: "1.0",
|
|
2188
|
+
service: new CacheController({ cache, cacheInclusionPolicy })
|
|
2189
|
+
};
|
|
2190
|
+
}
|
|
2191
|
+
|
|
2192
|
+
/*!
|
|
2193
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
2194
|
+
* All rights reserved.
|
|
2195
|
+
* For full license text, see the LICENSE.txt file
|
|
2196
|
+
*/
|
|
2197
|
+
/*!
|
|
2198
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
2199
|
+
* All rights reserved.
|
|
2200
|
+
* For full license text, see the LICENSE.txt file
|
|
2201
|
+
*/
|
|
2202
|
+
function resolvedPromiseLike$1(result) {
|
|
2203
|
+
if (isPromiseLike$1(result)) {
|
|
2204
|
+
return result.then((nextResult) => nextResult);
|
|
2205
|
+
}
|
|
2206
|
+
return {
|
|
2207
|
+
then: (onFulfilled, _onRejected) => {
|
|
2208
|
+
try {
|
|
2209
|
+
return resolvedPromiseLike$1(onFulfilled(result));
|
|
2210
|
+
} catch (e) {
|
|
2211
|
+
if (onFulfilled === void 0) {
|
|
2212
|
+
return resolvedPromiseLike$1(result);
|
|
2213
|
+
}
|
|
2214
|
+
return rejectedPromiseLike$1(e);
|
|
2215
|
+
}
|
|
2216
|
+
}
|
|
2217
|
+
};
|
|
2218
|
+
}
|
|
2219
|
+
function rejectedPromiseLike$1(reason) {
|
|
2220
|
+
if (isPromiseLike$1(reason)) {
|
|
2221
|
+
return reason.then((nextResult) => nextResult);
|
|
2222
|
+
}
|
|
2223
|
+
return {
|
|
2224
|
+
then: (_onFulfilled, onRejected) => {
|
|
2225
|
+
if (typeof onRejected === "function") {
|
|
2226
|
+
try {
|
|
2227
|
+
return resolvedPromiseLike$1(onRejected(reason));
|
|
2228
|
+
} catch (e) {
|
|
2229
|
+
return rejectedPromiseLike$1(e);
|
|
2230
|
+
}
|
|
2231
|
+
}
|
|
2232
|
+
return rejectedPromiseLike$1(reason);
|
|
2233
|
+
}
|
|
2234
|
+
};
|
|
2235
|
+
}
|
|
2236
|
+
function isPromiseLike$1(x) {
|
|
2237
|
+
return typeof (x == null ? void 0 : x.then) === "function";
|
|
2238
|
+
}
|
|
2239
|
+
const EventTypeWildcard = Symbol("EventTypeWildcard");
|
|
2240
|
+
class DefaultPubSubService {
|
|
2241
|
+
constructor() {
|
|
2242
|
+
this.subscriptions = /* @__PURE__ */ new Map();
|
|
2243
|
+
}
|
|
2244
|
+
subscribe(subscription) {
|
|
2245
|
+
let eventTypeSubscriptions = this.subscriptions.get(subscription.type);
|
|
2246
|
+
if (eventTypeSubscriptions === void 0) {
|
|
2247
|
+
eventTypeSubscriptions = [];
|
|
2248
|
+
this.subscriptions.set(subscription.type, eventTypeSubscriptions);
|
|
2249
|
+
}
|
|
2250
|
+
eventTypeSubscriptions.push(subscription);
|
|
2251
|
+
return () => {
|
|
2252
|
+
this.subscriptions.set(
|
|
2253
|
+
subscription.type,
|
|
2254
|
+
this.subscriptions.get(subscription.type).filter((value) => value !== subscription)
|
|
2255
|
+
);
|
|
2256
|
+
};
|
|
2257
|
+
}
|
|
2258
|
+
publish(event) {
|
|
2259
|
+
const promises = [];
|
|
2260
|
+
const subscriptions = this.getSubscriptions(event);
|
|
2261
|
+
subscriptions.forEach((subscription) => {
|
|
2262
|
+
if (!this.getSubscriptions(event).includes(subscription)) {
|
|
2263
|
+
return;
|
|
2264
|
+
}
|
|
2265
|
+
const returnVal = subscription.callback.call(subscription, event);
|
|
2266
|
+
if (isPromiseLike$1(returnVal)) {
|
|
2267
|
+
promises.push(returnVal);
|
|
2268
|
+
}
|
|
2269
|
+
});
|
|
2270
|
+
if (promises.length > 0) {
|
|
2271
|
+
return Promise.all(promises).then(() => void 0);
|
|
2272
|
+
}
|
|
2273
|
+
return resolvedPromiseLike$1(void 0);
|
|
2274
|
+
}
|
|
2275
|
+
getSubscriptions(event) {
|
|
2276
|
+
const eventTypeSubscriptions = this.subscriptions.get(event.type);
|
|
2277
|
+
const wildcardSubscriptions = this.subscriptions.get(EventTypeWildcard);
|
|
2278
|
+
if (eventTypeSubscriptions === void 0 && wildcardSubscriptions === void 0) {
|
|
2279
|
+
return [];
|
|
2280
|
+
}
|
|
2281
|
+
let matchingSubscriptions = [];
|
|
2282
|
+
if (eventTypeSubscriptions !== void 0) {
|
|
2283
|
+
matchingSubscriptions = eventTypeSubscriptions.filter((subscription) => {
|
|
2284
|
+
if (subscription.predicate) {
|
|
2285
|
+
return subscription.predicate(event);
|
|
2286
|
+
}
|
|
2287
|
+
return true;
|
|
2288
|
+
});
|
|
2289
|
+
}
|
|
2290
|
+
matchingSubscriptions = [...matchingSubscriptions, ...wildcardSubscriptions || []];
|
|
2291
|
+
return matchingSubscriptions;
|
|
2292
|
+
}
|
|
2293
|
+
}
|
|
2294
|
+
function buildServiceDescriptor$7() {
|
|
2295
|
+
return {
|
|
2296
|
+
type: "pubSub",
|
|
2297
|
+
version: "1.0",
|
|
2298
|
+
service: new DefaultPubSubService()
|
|
2299
|
+
};
|
|
2300
|
+
}
|
|
2301
|
+
|
|
2302
|
+
/*!
|
|
2303
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
2304
|
+
* All rights reserved.
|
|
2305
|
+
* For full license text, see the LICENSE.txt file
|
|
2306
|
+
*/
|
|
2307
|
+
class CacheInclusionPolicyService {
|
|
2308
|
+
}
|
|
2309
|
+
/*!
|
|
2310
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
2311
|
+
* All rights reserved.
|
|
2312
|
+
* For full license text, see the LICENSE.txt file
|
|
2313
|
+
*/
|
|
2314
|
+
class Ok {
|
|
2315
|
+
constructor(value) {
|
|
2316
|
+
this.value = value;
|
|
2317
|
+
}
|
|
2318
|
+
isOk() {
|
|
2319
|
+
return true;
|
|
2320
|
+
}
|
|
2321
|
+
isErr() {
|
|
2322
|
+
return !this.isOk();
|
|
2323
|
+
}
|
|
2324
|
+
}
|
|
2325
|
+
const ok = (value) => new Ok(value);
|
|
2326
|
+
function resolvedPromiseLike(result) {
|
|
2327
|
+
if (isPromiseLike(result)) {
|
|
2328
|
+
return result.then((nextResult) => nextResult);
|
|
2329
|
+
}
|
|
2330
|
+
return {
|
|
2331
|
+
then: (onFulfilled, _onRejected) => {
|
|
2332
|
+
try {
|
|
2333
|
+
return resolvedPromiseLike(onFulfilled(result));
|
|
2334
|
+
} catch (e) {
|
|
2335
|
+
if (onFulfilled === void 0) {
|
|
2336
|
+
return resolvedPromiseLike(result);
|
|
2337
|
+
}
|
|
2338
|
+
return rejectedPromiseLike(e);
|
|
2339
|
+
}
|
|
2340
|
+
}
|
|
2341
|
+
};
|
|
2342
|
+
}
|
|
2343
|
+
function rejectedPromiseLike(reason) {
|
|
2344
|
+
if (isPromiseLike(reason)) {
|
|
2345
|
+
return reason.then((nextResult) => nextResult);
|
|
2346
|
+
}
|
|
2347
|
+
return {
|
|
2348
|
+
then: (_onFulfilled, onRejected) => {
|
|
2349
|
+
if (typeof onRejected === "function") {
|
|
2350
|
+
try {
|
|
2351
|
+
return resolvedPromiseLike(onRejected(reason));
|
|
2352
|
+
} catch (e) {
|
|
2353
|
+
return rejectedPromiseLike(e);
|
|
2354
|
+
}
|
|
2355
|
+
}
|
|
2356
|
+
return rejectedPromiseLike(reason);
|
|
2357
|
+
}
|
|
2358
|
+
};
|
|
2359
|
+
}
|
|
2360
|
+
function isPromiseLike(x) {
|
|
2361
|
+
return typeof (x == null ? void 0 : x.then) === "function";
|
|
2362
|
+
}
|
|
2363
|
+
const isAndQuery = (query) => "$and" in query;
|
|
2364
|
+
const isOrQuery = (query) => "$or" in query;
|
|
2365
|
+
const isNotQuery = (query) => "$not" in query;
|
|
2366
|
+
const matchesMetadata = (metadataQuery, cacheControl) => {
|
|
2367
|
+
if ("cacheControlType" in metadataQuery && cacheControl.type !== metadataQuery.cacheControlType.$eq) {
|
|
2368
|
+
return false;
|
|
2369
|
+
}
|
|
2370
|
+
if ("maxAge" in metadataQuery && cacheControl.type === "max-age") {
|
|
2371
|
+
const maxAge = cacheControl.maxAge ?? 0;
|
|
2372
|
+
if (metadataQuery.maxAge.$gte !== void 0 && maxAge < metadataQuery.maxAge.$gte || metadataQuery.maxAge.$lte !== void 0 && maxAge > metadataQuery.maxAge.$lte) {
|
|
2373
|
+
return false;
|
|
2374
|
+
}
|
|
2375
|
+
}
|
|
2376
|
+
return true;
|
|
2377
|
+
};
|
|
2378
|
+
function queryToPredicate(query) {
|
|
2379
|
+
return (key, entry) => {
|
|
2380
|
+
if (!query) return false;
|
|
2381
|
+
if (isAndQuery(query))
|
|
2382
|
+
return query.$and.every((subQuery) => queryToPredicate(subQuery)(key, entry));
|
|
2383
|
+
if (isOrQuery(query))
|
|
2384
|
+
return query.$or.some((subQuery) => queryToPredicate(subQuery)(key, entry));
|
|
2385
|
+
if (isNotQuery(query)) return !queryToPredicate(query.$not)(key, entry);
|
|
2386
|
+
if ("key" in query) return matchesKey(query.key, key);
|
|
2387
|
+
if ("metadata" in query)
|
|
2388
|
+
return matchesMetadata(query.metadata, entry.metadata.cacheControl);
|
|
2389
|
+
if ("value" in query) return false;
|
|
2390
|
+
throw new Error("Unknown Query Operation");
|
|
2391
|
+
};
|
|
2392
|
+
}
|
|
2393
|
+
function matchesKey(keyQuery, key) {
|
|
2394
|
+
if ("$regex" in keyQuery) {
|
|
2395
|
+
return keyQuery.$regex.test(key);
|
|
2396
|
+
}
|
|
2397
|
+
return false;
|
|
2398
|
+
}
|
|
2399
|
+
function buildUpdate(update, existing) {
|
|
2400
|
+
switch (update.type) {
|
|
2401
|
+
case "invalidate":
|
|
2402
|
+
const updatedCacheControl = buildInvalidatedCacheControl(
|
|
2403
|
+
existing.metadata.cacheControl
|
|
2404
|
+
);
|
|
2405
|
+
return updatedCacheControl !== void 0 ? { type: "metadata", metadata: updatedCacheControl } : { type: "no-op" };
|
|
2406
|
+
default:
|
|
2407
|
+
throw new Error(`Invalid update operation: ${update.type}`);
|
|
2408
|
+
}
|
|
2409
|
+
}
|
|
2410
|
+
function buildInvalidatedCacheControl(existingCacheControl) {
|
|
2411
|
+
switch (existingCacheControl.type) {
|
|
2412
|
+
case "max-age":
|
|
2413
|
+
case "stale-while-revalidate":
|
|
2414
|
+
if (existingCacheControl.maxAge !== 0) {
|
|
2415
|
+
return {
|
|
2416
|
+
...existingCacheControl,
|
|
2417
|
+
maxAge: 0
|
|
2418
|
+
};
|
|
2419
|
+
}
|
|
2420
|
+
}
|
|
2421
|
+
return void 0;
|
|
2422
|
+
}
|
|
2423
|
+
class InMemoryCacheInclusionPolicy extends CacheInclusionPolicyService {
|
|
2424
|
+
constructor(services) {
|
|
2425
|
+
super();
|
|
2426
|
+
this.services = services;
|
|
2427
|
+
}
|
|
2428
|
+
/**
|
|
2429
|
+
* Reads data out of a single level in memory store.
|
|
2430
|
+
*/
|
|
2431
|
+
read(options) {
|
|
2432
|
+
const { l1, readFromL1 } = options;
|
|
2433
|
+
return readFromL1(l1);
|
|
2434
|
+
}
|
|
2435
|
+
/**
|
|
2436
|
+
* Writes data to a single level in memory store.
|
|
2437
|
+
*/
|
|
2438
|
+
write(options) {
|
|
2439
|
+
const { l1, writeToL1 } = options;
|
|
2440
|
+
return writeToL1(l1);
|
|
2441
|
+
}
|
|
2442
|
+
/**
|
|
2443
|
+
* Finds cache entries that match the given query.
|
|
2444
|
+
* Returns an async generator that yields `[key, entry]`.
|
|
2445
|
+
*/
|
|
2446
|
+
async *find(query) {
|
|
2447
|
+
const cache = this.services.cache;
|
|
2448
|
+
const predicate = queryToPredicate(query);
|
|
2449
|
+
const filteredEntries = cache.filter(predicate).entries();
|
|
2450
|
+
for (const entry of filteredEntries) {
|
|
2451
|
+
yield entry;
|
|
2452
|
+
}
|
|
2453
|
+
}
|
|
2454
|
+
/**
|
|
2455
|
+
* Finds and modifies cache entries that match the given query.
|
|
2456
|
+
* Extends `find(query)` and returns an async generator of modified keys.
|
|
2457
|
+
*/
|
|
2458
|
+
async *findAndModify(query, cacheUpdate) {
|
|
2459
|
+
const cache = this.services.cache;
|
|
2460
|
+
for await (const [key, value] of this.find(query)) {
|
|
2461
|
+
const update = buildUpdate(cacheUpdate, value);
|
|
2462
|
+
switch (update.type) {
|
|
2463
|
+
case "entry":
|
|
2464
|
+
this.write({
|
|
2465
|
+
l1: cache,
|
|
2466
|
+
writeToL1: (l1) => resolvedPromiseLike(ok(l1.set(key, update.entry)))
|
|
2467
|
+
});
|
|
2468
|
+
yield key;
|
|
2469
|
+
break;
|
|
2470
|
+
case "metadata":
|
|
2471
|
+
this.write({
|
|
2472
|
+
l1: cache,
|
|
2473
|
+
writeToL1: (l1) => resolvedPromiseLike(ok(l1.setMetadata(key, update.metadata)))
|
|
2474
|
+
});
|
|
2475
|
+
yield key;
|
|
2476
|
+
break;
|
|
2477
|
+
}
|
|
2478
|
+
}
|
|
2479
|
+
}
|
|
2480
|
+
}
|
|
2481
|
+
function buildInMemoryCacheInclusionPolicyService(cache) {
|
|
2482
|
+
return {
|
|
2483
|
+
service: new InMemoryCacheInclusionPolicy({ cache }),
|
|
2484
|
+
type: "cacheInclusionPolicy",
|
|
2485
|
+
version: "1.0"
|
|
2486
|
+
};
|
|
2487
|
+
}
|
|
2488
|
+
|
|
2489
|
+
/*!
|
|
2490
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
2491
|
+
* All rights reserved.
|
|
2492
|
+
* For full license text, see the LICENSE.txt file
|
|
2493
|
+
*/
|
|
2494
|
+
class FeatureFlagsService {
|
|
2495
|
+
constructor() {
|
|
2496
|
+
this.flags = /* @__PURE__ */ new Map();
|
|
2497
|
+
}
|
|
2498
|
+
set(flagName, value) {
|
|
2499
|
+
this.flags.set(flagName, value);
|
|
2500
|
+
}
|
|
2501
|
+
get(flagName, defaultValue = false) {
|
|
2502
|
+
return this.flags.get(flagName) || defaultValue;
|
|
2503
|
+
}
|
|
2504
|
+
}
|
|
2505
|
+
function buildServiceDescriptor$6() {
|
|
2506
|
+
return {
|
|
2507
|
+
version: "1.0",
|
|
2508
|
+
service: new FeatureFlagsService(),
|
|
2509
|
+
type: "featureFlags"
|
|
2510
|
+
};
|
|
2511
|
+
}
|
|
2512
|
+
|
|
2513
|
+
/*!
|
|
2514
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
2515
|
+
* All rights reserved.
|
|
2516
|
+
* For full license text, see the LICENSE.txt file
|
|
2517
|
+
*/
|
|
2518
|
+
function devAssert(condition, message) {
|
|
2519
|
+
const booleanCondition = Boolean(condition);
|
|
2520
|
+
if (!booleanCondition) {
|
|
2521
|
+
throw new Error(message);
|
|
2522
|
+
}
|
|
2523
|
+
}
|
|
2524
|
+
const MAX_ARRAY_LENGTH = 10;
|
|
2525
|
+
const MAX_RECURSIVE_DEPTH = 2;
|
|
2526
|
+
function inspect(value) {
|
|
2527
|
+
return formatValue(value, []);
|
|
2528
|
+
}
|
|
2529
|
+
function formatValue(value, seenValues) {
|
|
2530
|
+
switch (typeof value) {
|
|
2531
|
+
case "string":
|
|
2532
|
+
return JSON.stringify(value);
|
|
2533
|
+
case "function":
|
|
2534
|
+
return value.name ? `[function ${value.name}]` : "[function]";
|
|
2535
|
+
case "object":
|
|
2536
|
+
return formatObjectValue(value, seenValues);
|
|
2537
|
+
default:
|
|
2538
|
+
return String(value);
|
|
2539
|
+
}
|
|
2540
|
+
}
|
|
2541
|
+
function formatObjectValue(value, previouslySeenValues) {
|
|
2542
|
+
if (value === null) {
|
|
2543
|
+
return "null";
|
|
2544
|
+
}
|
|
2545
|
+
if (previouslySeenValues.includes(value)) {
|
|
2546
|
+
return "[Circular]";
|
|
2547
|
+
}
|
|
2548
|
+
const seenValues = [...previouslySeenValues, value];
|
|
2549
|
+
if (isJSONable(value)) {
|
|
2550
|
+
const jsonValue = value.toJSON();
|
|
2551
|
+
if (jsonValue !== value) {
|
|
2552
|
+
return typeof jsonValue === "string" ? jsonValue : formatValue(jsonValue, seenValues);
|
|
2553
|
+
}
|
|
2554
|
+
} else if (Array.isArray(value)) {
|
|
2555
|
+
return formatArray(value, seenValues);
|
|
2556
|
+
}
|
|
2557
|
+
return formatObject(value, seenValues);
|
|
2558
|
+
}
|
|
2559
|
+
function isJSONable(value) {
|
|
2560
|
+
return typeof value.toJSON === "function";
|
|
2561
|
+
}
|
|
2562
|
+
function formatObject(object, seenValues) {
|
|
2563
|
+
const entries = Object.entries(object);
|
|
2564
|
+
if (entries.length === 0) {
|
|
2565
|
+
return "{}";
|
|
2566
|
+
}
|
|
2567
|
+
if (seenValues.length > MAX_RECURSIVE_DEPTH) {
|
|
2568
|
+
return "[" + getObjectTag(object) + "]";
|
|
2569
|
+
}
|
|
2570
|
+
const properties = entries.map(
|
|
2571
|
+
([key, value]) => key + ": " + formatValue(value, seenValues)
|
|
2572
|
+
);
|
|
2573
|
+
return "{ " + properties.join(", ") + " }";
|
|
2574
|
+
}
|
|
2575
|
+
function formatArray(array, seenValues) {
|
|
2576
|
+
if (array.length === 0) {
|
|
2577
|
+
return "[]";
|
|
2578
|
+
}
|
|
2579
|
+
if (seenValues.length > MAX_RECURSIVE_DEPTH) {
|
|
2580
|
+
return "[Array]";
|
|
2581
|
+
}
|
|
2582
|
+
const len = Math.min(MAX_ARRAY_LENGTH, array.length);
|
|
2583
|
+
const remaining = array.length - len;
|
|
2584
|
+
const items = [];
|
|
2585
|
+
for (let i = 0; i < len; ++i) {
|
|
2586
|
+
items.push(formatValue(array[i], seenValues));
|
|
2587
|
+
}
|
|
2588
|
+
if (remaining === 1) {
|
|
2589
|
+
items.push("... 1 more item");
|
|
2590
|
+
} else if (remaining > 1) {
|
|
2591
|
+
items.push(`... ${remaining} more items`);
|
|
2592
|
+
}
|
|
2593
|
+
return "[" + items.join(", ") + "]";
|
|
2594
|
+
}
|
|
2595
|
+
function getObjectTag(object) {
|
|
2596
|
+
const tag = Object.prototype.toString.call(object).replace(/^\[object /, "").replace(/]$/, "");
|
|
2597
|
+
if (tag === "Object" && typeof object.constructor === "function") {
|
|
2598
|
+
const name = object.constructor.name;
|
|
2599
|
+
if (typeof name === "string" && name !== "") {
|
|
2600
|
+
return name;
|
|
2601
|
+
}
|
|
2602
|
+
}
|
|
2603
|
+
return tag;
|
|
2604
|
+
}
|
|
2605
|
+
globalThis.process && // eslint-disable-next-line no-undef
|
|
2606
|
+
process.env.NODE_ENV === "production";
|
|
2607
|
+
var Kind;
|
|
2608
|
+
(function(Kind2) {
|
|
2609
|
+
Kind2["NAME"] = "Name";
|
|
2610
|
+
Kind2["DOCUMENT"] = "Document";
|
|
2611
|
+
Kind2["OPERATION_DEFINITION"] = "OperationDefinition";
|
|
2612
|
+
Kind2["VARIABLE_DEFINITION"] = "VariableDefinition";
|
|
2613
|
+
Kind2["SELECTION_SET"] = "SelectionSet";
|
|
2614
|
+
Kind2["FIELD"] = "Field";
|
|
2615
|
+
Kind2["ARGUMENT"] = "Argument";
|
|
2616
|
+
Kind2["FRAGMENT_SPREAD"] = "FragmentSpread";
|
|
2617
|
+
Kind2["INLINE_FRAGMENT"] = "InlineFragment";
|
|
2618
|
+
Kind2["FRAGMENT_DEFINITION"] = "FragmentDefinition";
|
|
2619
|
+
Kind2["VARIABLE"] = "Variable";
|
|
2620
|
+
Kind2["INT"] = "IntValue";
|
|
2621
|
+
Kind2["FLOAT"] = "FloatValue";
|
|
2622
|
+
Kind2["STRING"] = "StringValue";
|
|
2623
|
+
Kind2["BOOLEAN"] = "BooleanValue";
|
|
2624
|
+
Kind2["NULL"] = "NullValue";
|
|
2625
|
+
Kind2["ENUM"] = "EnumValue";
|
|
2626
|
+
Kind2["LIST"] = "ListValue";
|
|
2627
|
+
Kind2["OBJECT"] = "ObjectValue";
|
|
2628
|
+
Kind2["OBJECT_FIELD"] = "ObjectField";
|
|
2629
|
+
Kind2["DIRECTIVE"] = "Directive";
|
|
2630
|
+
Kind2["NAMED_TYPE"] = "NamedType";
|
|
2631
|
+
Kind2["LIST_TYPE"] = "ListType";
|
|
2632
|
+
Kind2["NON_NULL_TYPE"] = "NonNullType";
|
|
2633
|
+
Kind2["SCHEMA_DEFINITION"] = "SchemaDefinition";
|
|
2634
|
+
Kind2["OPERATION_TYPE_DEFINITION"] = "OperationTypeDefinition";
|
|
2635
|
+
Kind2["SCALAR_TYPE_DEFINITION"] = "ScalarTypeDefinition";
|
|
2636
|
+
Kind2["OBJECT_TYPE_DEFINITION"] = "ObjectTypeDefinition";
|
|
2637
|
+
Kind2["FIELD_DEFINITION"] = "FieldDefinition";
|
|
2638
|
+
Kind2["INPUT_VALUE_DEFINITION"] = "InputValueDefinition";
|
|
2639
|
+
Kind2["INTERFACE_TYPE_DEFINITION"] = "InterfaceTypeDefinition";
|
|
2640
|
+
Kind2["UNION_TYPE_DEFINITION"] = "UnionTypeDefinition";
|
|
2641
|
+
Kind2["ENUM_TYPE_DEFINITION"] = "EnumTypeDefinition";
|
|
2642
|
+
Kind2["ENUM_VALUE_DEFINITION"] = "EnumValueDefinition";
|
|
2643
|
+
Kind2["INPUT_OBJECT_TYPE_DEFINITION"] = "InputObjectTypeDefinition";
|
|
2644
|
+
Kind2["DIRECTIVE_DEFINITION"] = "DirectiveDefinition";
|
|
2645
|
+
Kind2["SCHEMA_EXTENSION"] = "SchemaExtension";
|
|
2646
|
+
Kind2["SCALAR_TYPE_EXTENSION"] = "ScalarTypeExtension";
|
|
2647
|
+
Kind2["OBJECT_TYPE_EXTENSION"] = "ObjectTypeExtension";
|
|
2648
|
+
Kind2["INTERFACE_TYPE_EXTENSION"] = "InterfaceTypeExtension";
|
|
2649
|
+
Kind2["UNION_TYPE_EXTENSION"] = "UnionTypeExtension";
|
|
2650
|
+
Kind2["ENUM_TYPE_EXTENSION"] = "EnumTypeExtension";
|
|
2651
|
+
Kind2["INPUT_OBJECT_TYPE_EXTENSION"] = "InputObjectTypeExtension";
|
|
2652
|
+
})(Kind || (Kind = {}));
|
|
2653
|
+
var TokenKind;
|
|
2654
|
+
(function(TokenKind2) {
|
|
2655
|
+
TokenKind2["SOF"] = "<SOF>";
|
|
2656
|
+
TokenKind2["EOF"] = "<EOF>";
|
|
2657
|
+
TokenKind2["BANG"] = "!";
|
|
2658
|
+
TokenKind2["DOLLAR"] = "$";
|
|
2659
|
+
TokenKind2["AMP"] = "&";
|
|
2660
|
+
TokenKind2["PAREN_L"] = "(";
|
|
2661
|
+
TokenKind2["PAREN_R"] = ")";
|
|
2662
|
+
TokenKind2["SPREAD"] = "...";
|
|
2663
|
+
TokenKind2["COLON"] = ":";
|
|
2664
|
+
TokenKind2["EQUALS"] = "=";
|
|
2665
|
+
TokenKind2["AT"] = "@";
|
|
2666
|
+
TokenKind2["BRACKET_L"] = "[";
|
|
2667
|
+
TokenKind2["BRACKET_R"] = "]";
|
|
2668
|
+
TokenKind2["BRACE_L"] = "{";
|
|
2669
|
+
TokenKind2["PIPE"] = "|";
|
|
2670
|
+
TokenKind2["BRACE_R"] = "}";
|
|
2671
|
+
TokenKind2["NAME"] = "Name";
|
|
2672
|
+
TokenKind2["INT"] = "Int";
|
|
2673
|
+
TokenKind2["FLOAT"] = "Float";
|
|
2674
|
+
TokenKind2["STRING"] = "String";
|
|
2675
|
+
TokenKind2["BLOCK_STRING"] = "BlockString";
|
|
2676
|
+
TokenKind2["COMMENT"] = "Comment";
|
|
2677
|
+
})(TokenKind || (TokenKind = {}));
|
|
2678
|
+
const QueryDocumentKeys = {
|
|
2679
|
+
Name: [],
|
|
2680
|
+
Document: ["definitions"],
|
|
2681
|
+
OperationDefinition: [
|
|
2682
|
+
"name",
|
|
2683
|
+
"variableDefinitions",
|
|
2684
|
+
"directives",
|
|
2685
|
+
"selectionSet"
|
|
2686
|
+
],
|
|
2687
|
+
VariableDefinition: ["variable", "type", "defaultValue", "directives"],
|
|
2688
|
+
Variable: ["name"],
|
|
2689
|
+
SelectionSet: ["selections"],
|
|
2690
|
+
Field: ["alias", "name", "arguments", "directives", "selectionSet"],
|
|
2691
|
+
Argument: ["name", "value"],
|
|
2692
|
+
FragmentSpread: ["name", "directives"],
|
|
2693
|
+
InlineFragment: ["typeCondition", "directives", "selectionSet"],
|
|
2694
|
+
FragmentDefinition: [
|
|
2695
|
+
"name",
|
|
2696
|
+
// Note: fragment variable definitions are deprecated and will removed in v17.0.0
|
|
2697
|
+
"variableDefinitions",
|
|
2698
|
+
"typeCondition",
|
|
2699
|
+
"directives",
|
|
2700
|
+
"selectionSet"
|
|
2701
|
+
],
|
|
2702
|
+
IntValue: [],
|
|
2703
|
+
FloatValue: [],
|
|
2704
|
+
StringValue: [],
|
|
2705
|
+
BooleanValue: [],
|
|
2706
|
+
NullValue: [],
|
|
2707
|
+
EnumValue: [],
|
|
2708
|
+
ListValue: ["values"],
|
|
2709
|
+
ObjectValue: ["fields"],
|
|
2710
|
+
ObjectField: ["name", "value"],
|
|
2711
|
+
Directive: ["name", "arguments"],
|
|
2712
|
+
NamedType: ["name"],
|
|
2713
|
+
ListType: ["type"],
|
|
2714
|
+
NonNullType: ["type"],
|
|
2715
|
+
SchemaDefinition: ["description", "directives", "operationTypes"],
|
|
2716
|
+
OperationTypeDefinition: ["type"],
|
|
2717
|
+
ScalarTypeDefinition: ["description", "name", "directives"],
|
|
2718
|
+
ObjectTypeDefinition: [
|
|
2719
|
+
"description",
|
|
2720
|
+
"name",
|
|
2721
|
+
"interfaces",
|
|
2722
|
+
"directives",
|
|
2723
|
+
"fields"
|
|
2724
|
+
],
|
|
2725
|
+
FieldDefinition: ["description", "name", "arguments", "type", "directives"],
|
|
2726
|
+
InputValueDefinition: [
|
|
2727
|
+
"description",
|
|
2728
|
+
"name",
|
|
2729
|
+
"type",
|
|
2730
|
+
"defaultValue",
|
|
2731
|
+
"directives"
|
|
2732
|
+
],
|
|
2733
|
+
InterfaceTypeDefinition: [
|
|
2734
|
+
"description",
|
|
2735
|
+
"name",
|
|
2736
|
+
"interfaces",
|
|
2737
|
+
"directives",
|
|
2738
|
+
"fields"
|
|
2739
|
+
],
|
|
2740
|
+
UnionTypeDefinition: ["description", "name", "directives", "types"],
|
|
2741
|
+
EnumTypeDefinition: ["description", "name", "directives", "values"],
|
|
2742
|
+
EnumValueDefinition: ["description", "name", "directives"],
|
|
2743
|
+
InputObjectTypeDefinition: ["description", "name", "directives", "fields"],
|
|
2744
|
+
DirectiveDefinition: ["description", "name", "arguments", "locations"],
|
|
2745
|
+
SchemaExtension: ["directives", "operationTypes"],
|
|
2746
|
+
ScalarTypeExtension: ["name", "directives"],
|
|
2747
|
+
ObjectTypeExtension: ["name", "interfaces", "directives", "fields"],
|
|
2748
|
+
InterfaceTypeExtension: ["name", "interfaces", "directives", "fields"],
|
|
2749
|
+
UnionTypeExtension: ["name", "directives", "types"],
|
|
2750
|
+
EnumTypeExtension: ["name", "directives", "values"],
|
|
2751
|
+
InputObjectTypeExtension: ["name", "directives", "fields"]
|
|
2752
|
+
};
|
|
2753
|
+
const kindValues = new Set(Object.keys(QueryDocumentKeys));
|
|
2754
|
+
function isNode(maybeNode) {
|
|
2755
|
+
const maybeKind = maybeNode === null || maybeNode === void 0 ? void 0 : maybeNode.kind;
|
|
2756
|
+
return typeof maybeKind === "string" && kindValues.has(maybeKind);
|
|
2757
|
+
}
|
|
2758
|
+
var OperationTypeNode;
|
|
2759
|
+
(function(OperationTypeNode2) {
|
|
2760
|
+
OperationTypeNode2["QUERY"] = "query";
|
|
2761
|
+
OperationTypeNode2["MUTATION"] = "mutation";
|
|
2762
|
+
OperationTypeNode2["SUBSCRIPTION"] = "subscription";
|
|
2763
|
+
})(OperationTypeNode || (OperationTypeNode = {}));
|
|
2764
|
+
function isWhiteSpace(code) {
|
|
2765
|
+
return code === 9 || code === 32;
|
|
2766
|
+
}
|
|
2767
|
+
function printBlockString(value, options) {
|
|
2768
|
+
const escapedValue = value.replace(/"""/g, '\\"""');
|
|
2769
|
+
const lines = escapedValue.split(/\r\n|[\n\r]/g);
|
|
2770
|
+
const isSingleLine = lines.length === 1;
|
|
2771
|
+
const forceLeadingNewLine = lines.length > 1 && lines.slice(1).every((line) => line.length === 0 || isWhiteSpace(line.charCodeAt(0)));
|
|
2772
|
+
const hasTrailingTripleQuotes = escapedValue.endsWith('\\"""');
|
|
2773
|
+
const hasTrailingQuote = value.endsWith('"') && !hasTrailingTripleQuotes;
|
|
2774
|
+
const hasTrailingSlash = value.endsWith("\\");
|
|
2775
|
+
const forceTrailingNewline = hasTrailingQuote || hasTrailingSlash;
|
|
2776
|
+
const printAsMultipleLines = !(options !== null && options !== void 0 && options.minimize) && // add leading and trailing new lines only if it improves readability
|
|
2777
|
+
(!isSingleLine || value.length > 70 || forceTrailingNewline || forceLeadingNewLine || hasTrailingTripleQuotes);
|
|
2778
|
+
let result = "";
|
|
2779
|
+
const skipLeadingNewLine = isSingleLine && isWhiteSpace(value.charCodeAt(0));
|
|
2780
|
+
if (printAsMultipleLines && !skipLeadingNewLine || forceLeadingNewLine) {
|
|
2781
|
+
result += "\n";
|
|
2782
|
+
}
|
|
2783
|
+
result += escapedValue;
|
|
2784
|
+
if (printAsMultipleLines || forceTrailingNewline) {
|
|
2785
|
+
result += "\n";
|
|
2786
|
+
}
|
|
2787
|
+
return '"""' + result + '"""';
|
|
2788
|
+
}
|
|
2789
|
+
var DirectiveLocation;
|
|
2790
|
+
(function(DirectiveLocation2) {
|
|
2791
|
+
DirectiveLocation2["QUERY"] = "QUERY";
|
|
2792
|
+
DirectiveLocation2["MUTATION"] = "MUTATION";
|
|
2793
|
+
DirectiveLocation2["SUBSCRIPTION"] = "SUBSCRIPTION";
|
|
2794
|
+
DirectiveLocation2["FIELD"] = "FIELD";
|
|
2795
|
+
DirectiveLocation2["FRAGMENT_DEFINITION"] = "FRAGMENT_DEFINITION";
|
|
2796
|
+
DirectiveLocation2["FRAGMENT_SPREAD"] = "FRAGMENT_SPREAD";
|
|
2797
|
+
DirectiveLocation2["INLINE_FRAGMENT"] = "INLINE_FRAGMENT";
|
|
2798
|
+
DirectiveLocation2["VARIABLE_DEFINITION"] = "VARIABLE_DEFINITION";
|
|
2799
|
+
DirectiveLocation2["SCHEMA"] = "SCHEMA";
|
|
2800
|
+
DirectiveLocation2["SCALAR"] = "SCALAR";
|
|
2801
|
+
DirectiveLocation2["OBJECT"] = "OBJECT";
|
|
2802
|
+
DirectiveLocation2["FIELD_DEFINITION"] = "FIELD_DEFINITION";
|
|
2803
|
+
DirectiveLocation2["ARGUMENT_DEFINITION"] = "ARGUMENT_DEFINITION";
|
|
2804
|
+
DirectiveLocation2["INTERFACE"] = "INTERFACE";
|
|
2805
|
+
DirectiveLocation2["UNION"] = "UNION";
|
|
2806
|
+
DirectiveLocation2["ENUM"] = "ENUM";
|
|
2807
|
+
DirectiveLocation2["ENUM_VALUE"] = "ENUM_VALUE";
|
|
2808
|
+
DirectiveLocation2["INPUT_OBJECT"] = "INPUT_OBJECT";
|
|
2809
|
+
DirectiveLocation2["INPUT_FIELD_DEFINITION"] = "INPUT_FIELD_DEFINITION";
|
|
2810
|
+
})(DirectiveLocation || (DirectiveLocation = {}));
|
|
2811
|
+
function printString(str) {
|
|
2812
|
+
return `"${str.replace(escapedRegExp, escapedReplacer)}"`;
|
|
2813
|
+
}
|
|
2814
|
+
const escapedRegExp = /[\x00-\x1f\x22\x5c\x7f-\x9f]/g;
|
|
2815
|
+
function escapedReplacer(str) {
|
|
2816
|
+
return escapeSequences[str.charCodeAt(0)];
|
|
2817
|
+
}
|
|
2818
|
+
const escapeSequences = [
|
|
2819
|
+
"\\u0000",
|
|
2820
|
+
"\\u0001",
|
|
2821
|
+
"\\u0002",
|
|
2822
|
+
"\\u0003",
|
|
2823
|
+
"\\u0004",
|
|
2824
|
+
"\\u0005",
|
|
2825
|
+
"\\u0006",
|
|
2826
|
+
"\\u0007",
|
|
2827
|
+
"\\b",
|
|
2828
|
+
"\\t",
|
|
2829
|
+
"\\n",
|
|
2830
|
+
"\\u000B",
|
|
2831
|
+
"\\f",
|
|
2832
|
+
"\\r",
|
|
2833
|
+
"\\u000E",
|
|
2834
|
+
"\\u000F",
|
|
2835
|
+
"\\u0010",
|
|
2836
|
+
"\\u0011",
|
|
2837
|
+
"\\u0012",
|
|
2838
|
+
"\\u0013",
|
|
2839
|
+
"\\u0014",
|
|
2840
|
+
"\\u0015",
|
|
2841
|
+
"\\u0016",
|
|
2842
|
+
"\\u0017",
|
|
2843
|
+
"\\u0018",
|
|
2844
|
+
"\\u0019",
|
|
2845
|
+
"\\u001A",
|
|
2846
|
+
"\\u001B",
|
|
2847
|
+
"\\u001C",
|
|
2848
|
+
"\\u001D",
|
|
2849
|
+
"\\u001E",
|
|
2850
|
+
"\\u001F",
|
|
2851
|
+
"",
|
|
2852
|
+
"",
|
|
2853
|
+
'\\"',
|
|
2854
|
+
"",
|
|
2855
|
+
"",
|
|
2856
|
+
"",
|
|
2857
|
+
"",
|
|
2858
|
+
"",
|
|
2859
|
+
"",
|
|
2860
|
+
"",
|
|
2861
|
+
"",
|
|
2862
|
+
"",
|
|
2863
|
+
"",
|
|
2864
|
+
"",
|
|
2865
|
+
"",
|
|
2866
|
+
"",
|
|
2867
|
+
// 2F
|
|
2868
|
+
"",
|
|
2869
|
+
"",
|
|
2870
|
+
"",
|
|
2871
|
+
"",
|
|
2872
|
+
"",
|
|
2873
|
+
"",
|
|
2874
|
+
"",
|
|
2875
|
+
"",
|
|
2876
|
+
"",
|
|
2877
|
+
"",
|
|
2878
|
+
"",
|
|
2879
|
+
"",
|
|
2880
|
+
"",
|
|
2881
|
+
"",
|
|
2882
|
+
"",
|
|
2883
|
+
"",
|
|
2884
|
+
// 3F
|
|
2885
|
+
"",
|
|
2886
|
+
"",
|
|
2887
|
+
"",
|
|
2888
|
+
"",
|
|
2889
|
+
"",
|
|
2890
|
+
"",
|
|
2891
|
+
"",
|
|
2892
|
+
"",
|
|
2893
|
+
"",
|
|
2894
|
+
"",
|
|
2895
|
+
"",
|
|
2896
|
+
"",
|
|
2897
|
+
"",
|
|
2898
|
+
"",
|
|
2899
|
+
"",
|
|
2900
|
+
"",
|
|
2901
|
+
// 4F
|
|
2902
|
+
"",
|
|
2903
|
+
"",
|
|
2904
|
+
"",
|
|
2905
|
+
"",
|
|
2906
|
+
"",
|
|
2907
|
+
"",
|
|
2908
|
+
"",
|
|
2909
|
+
"",
|
|
2910
|
+
"",
|
|
2911
|
+
"",
|
|
2912
|
+
"",
|
|
2913
|
+
"",
|
|
2914
|
+
"\\\\",
|
|
2915
|
+
"",
|
|
2916
|
+
"",
|
|
2917
|
+
"",
|
|
2918
|
+
// 5F
|
|
2919
|
+
"",
|
|
2920
|
+
"",
|
|
2921
|
+
"",
|
|
2922
|
+
"",
|
|
2923
|
+
"",
|
|
2924
|
+
"",
|
|
2925
|
+
"",
|
|
2926
|
+
"",
|
|
2927
|
+
"",
|
|
2928
|
+
"",
|
|
2929
|
+
"",
|
|
2930
|
+
"",
|
|
2931
|
+
"",
|
|
2932
|
+
"",
|
|
2933
|
+
"",
|
|
2934
|
+
"",
|
|
2935
|
+
// 6F
|
|
2936
|
+
"",
|
|
2937
|
+
"",
|
|
2938
|
+
"",
|
|
2939
|
+
"",
|
|
2940
|
+
"",
|
|
2941
|
+
"",
|
|
2942
|
+
"",
|
|
2943
|
+
"",
|
|
2944
|
+
"",
|
|
2945
|
+
"",
|
|
2946
|
+
"",
|
|
2947
|
+
"",
|
|
2948
|
+
"",
|
|
2949
|
+
"",
|
|
2950
|
+
"",
|
|
2951
|
+
"\\u007F",
|
|
2952
|
+
"\\u0080",
|
|
2953
|
+
"\\u0081",
|
|
2954
|
+
"\\u0082",
|
|
2955
|
+
"\\u0083",
|
|
2956
|
+
"\\u0084",
|
|
2957
|
+
"\\u0085",
|
|
2958
|
+
"\\u0086",
|
|
2959
|
+
"\\u0087",
|
|
2960
|
+
"\\u0088",
|
|
2961
|
+
"\\u0089",
|
|
2962
|
+
"\\u008A",
|
|
2963
|
+
"\\u008B",
|
|
2964
|
+
"\\u008C",
|
|
2965
|
+
"\\u008D",
|
|
2966
|
+
"\\u008E",
|
|
2967
|
+
"\\u008F",
|
|
2968
|
+
"\\u0090",
|
|
2969
|
+
"\\u0091",
|
|
2970
|
+
"\\u0092",
|
|
2971
|
+
"\\u0093",
|
|
2972
|
+
"\\u0094",
|
|
2973
|
+
"\\u0095",
|
|
2974
|
+
"\\u0096",
|
|
2975
|
+
"\\u0097",
|
|
2976
|
+
"\\u0098",
|
|
2977
|
+
"\\u0099",
|
|
2978
|
+
"\\u009A",
|
|
2979
|
+
"\\u009B",
|
|
2980
|
+
"\\u009C",
|
|
2981
|
+
"\\u009D",
|
|
2982
|
+
"\\u009E",
|
|
2983
|
+
"\\u009F"
|
|
2984
|
+
];
|
|
2985
|
+
const BREAK = Object.freeze({});
|
|
2986
|
+
function visit(root, visitor, visitorKeys = QueryDocumentKeys) {
|
|
2987
|
+
const enterLeaveMap = /* @__PURE__ */ new Map();
|
|
2988
|
+
for (const kind of Object.values(Kind)) {
|
|
2989
|
+
enterLeaveMap.set(kind, getEnterLeaveForKind(visitor, kind));
|
|
2990
|
+
}
|
|
2991
|
+
let stack = void 0;
|
|
2992
|
+
let inArray = Array.isArray(root);
|
|
2993
|
+
let keys = [root];
|
|
2994
|
+
let index = -1;
|
|
2995
|
+
let edits = [];
|
|
2996
|
+
let node = root;
|
|
2997
|
+
let key = void 0;
|
|
2998
|
+
let parent = void 0;
|
|
2999
|
+
const path = [];
|
|
3000
|
+
const ancestors = [];
|
|
3001
|
+
do {
|
|
3002
|
+
index++;
|
|
3003
|
+
const isLeaving = index === keys.length;
|
|
3004
|
+
const isEdited = isLeaving && edits.length !== 0;
|
|
3005
|
+
if (isLeaving) {
|
|
3006
|
+
key = ancestors.length === 0 ? void 0 : path[path.length - 1];
|
|
3007
|
+
node = parent;
|
|
3008
|
+
parent = ancestors.pop();
|
|
3009
|
+
if (isEdited) {
|
|
3010
|
+
if (inArray) {
|
|
3011
|
+
node = node.slice();
|
|
3012
|
+
let editOffset = 0;
|
|
3013
|
+
for (const [editKey, editValue] of edits) {
|
|
3014
|
+
const arrayKey = editKey - editOffset;
|
|
3015
|
+
if (editValue === null) {
|
|
3016
|
+
node.splice(arrayKey, 1);
|
|
3017
|
+
editOffset++;
|
|
3018
|
+
} else {
|
|
3019
|
+
node[arrayKey] = editValue;
|
|
3020
|
+
}
|
|
3021
|
+
}
|
|
3022
|
+
} else {
|
|
3023
|
+
node = { ...node };
|
|
3024
|
+
for (const [editKey, editValue] of edits) {
|
|
3025
|
+
node[editKey] = editValue;
|
|
3026
|
+
}
|
|
3027
|
+
}
|
|
3028
|
+
}
|
|
3029
|
+
index = stack.index;
|
|
3030
|
+
keys = stack.keys;
|
|
3031
|
+
edits = stack.edits;
|
|
3032
|
+
inArray = stack.inArray;
|
|
3033
|
+
stack = stack.prev;
|
|
3034
|
+
} else if (parent) {
|
|
3035
|
+
key = inArray ? index : keys[index];
|
|
3036
|
+
node = parent[key];
|
|
3037
|
+
if (node === null || node === void 0) {
|
|
3038
|
+
continue;
|
|
3039
|
+
}
|
|
3040
|
+
path.push(key);
|
|
3041
|
+
}
|
|
3042
|
+
let result;
|
|
3043
|
+
if (!Array.isArray(node)) {
|
|
3044
|
+
var _enterLeaveMap$get, _enterLeaveMap$get2;
|
|
3045
|
+
isNode(node) || devAssert(false, `Invalid AST Node: ${inspect(node)}.`);
|
|
3046
|
+
const visitFn = isLeaving ? (_enterLeaveMap$get = enterLeaveMap.get(node.kind)) === null || _enterLeaveMap$get === void 0 ? void 0 : _enterLeaveMap$get.leave : (_enterLeaveMap$get2 = enterLeaveMap.get(node.kind)) === null || _enterLeaveMap$get2 === void 0 ? void 0 : _enterLeaveMap$get2.enter;
|
|
3047
|
+
result = visitFn === null || visitFn === void 0 ? void 0 : visitFn.call(visitor, node, key, parent, path, ancestors);
|
|
3048
|
+
if (result === BREAK) {
|
|
3049
|
+
break;
|
|
3050
|
+
}
|
|
3051
|
+
if (result === false) {
|
|
3052
|
+
if (!isLeaving) {
|
|
3053
|
+
path.pop();
|
|
3054
|
+
continue;
|
|
3055
|
+
}
|
|
3056
|
+
} else if (result !== void 0) {
|
|
3057
|
+
edits.push([key, result]);
|
|
3058
|
+
if (!isLeaving) {
|
|
3059
|
+
if (isNode(result)) {
|
|
3060
|
+
node = result;
|
|
3061
|
+
} else {
|
|
3062
|
+
path.pop();
|
|
3063
|
+
continue;
|
|
3064
|
+
}
|
|
3065
|
+
}
|
|
3066
|
+
}
|
|
3067
|
+
}
|
|
3068
|
+
if (result === void 0 && isEdited) {
|
|
3069
|
+
edits.push([key, node]);
|
|
3070
|
+
}
|
|
3071
|
+
if (isLeaving) {
|
|
3072
|
+
path.pop();
|
|
3073
|
+
} else {
|
|
3074
|
+
var _node$kind;
|
|
3075
|
+
stack = {
|
|
3076
|
+
inArray,
|
|
3077
|
+
index,
|
|
3078
|
+
keys,
|
|
3079
|
+
edits,
|
|
3080
|
+
prev: stack
|
|
3081
|
+
};
|
|
3082
|
+
inArray = Array.isArray(node);
|
|
3083
|
+
keys = inArray ? node : (_node$kind = visitorKeys[node.kind]) !== null && _node$kind !== void 0 ? _node$kind : [];
|
|
3084
|
+
index = -1;
|
|
3085
|
+
edits = [];
|
|
3086
|
+
if (parent) {
|
|
3087
|
+
ancestors.push(parent);
|
|
3088
|
+
}
|
|
3089
|
+
parent = node;
|
|
3090
|
+
}
|
|
3091
|
+
} while (stack !== void 0);
|
|
3092
|
+
if (edits.length !== 0) {
|
|
3093
|
+
return edits[edits.length - 1][1];
|
|
3094
|
+
}
|
|
3095
|
+
return root;
|
|
3096
|
+
}
|
|
3097
|
+
function getEnterLeaveForKind(visitor, kind) {
|
|
3098
|
+
const kindVisitor = visitor[kind];
|
|
3099
|
+
if (typeof kindVisitor === "object") {
|
|
3100
|
+
return kindVisitor;
|
|
3101
|
+
} else if (typeof kindVisitor === "function") {
|
|
3102
|
+
return {
|
|
3103
|
+
enter: kindVisitor,
|
|
3104
|
+
leave: void 0
|
|
3105
|
+
};
|
|
3106
|
+
}
|
|
3107
|
+
return {
|
|
3108
|
+
enter: visitor.enter,
|
|
3109
|
+
leave: visitor.leave
|
|
3110
|
+
};
|
|
3111
|
+
}
|
|
3112
|
+
function print(ast) {
|
|
3113
|
+
return visit(ast, printDocASTReducer);
|
|
3114
|
+
}
|
|
3115
|
+
const MAX_LINE_LENGTH = 80;
|
|
3116
|
+
const printDocASTReducer = {
|
|
3117
|
+
Name: {
|
|
3118
|
+
leave: (node) => node.value
|
|
3119
|
+
},
|
|
3120
|
+
Variable: {
|
|
3121
|
+
leave: (node) => "$" + node.name
|
|
3122
|
+
},
|
|
3123
|
+
// Document
|
|
3124
|
+
Document: {
|
|
3125
|
+
leave: (node) => join(node.definitions, "\n\n")
|
|
3126
|
+
},
|
|
3127
|
+
OperationDefinition: {
|
|
3128
|
+
leave(node) {
|
|
3129
|
+
const varDefs = wrap("(", join(node.variableDefinitions, ", "), ")");
|
|
3130
|
+
const prefix = join(
|
|
3131
|
+
[
|
|
3132
|
+
node.operation,
|
|
3133
|
+
join([node.name, varDefs]),
|
|
3134
|
+
join(node.directives, " ")
|
|
3135
|
+
],
|
|
3136
|
+
" "
|
|
3137
|
+
);
|
|
3138
|
+
return (prefix === "query" ? "" : prefix + " ") + node.selectionSet;
|
|
3139
|
+
}
|
|
3140
|
+
},
|
|
3141
|
+
VariableDefinition: {
|
|
3142
|
+
leave: ({ variable, type, defaultValue, directives }) => variable + ": " + type + wrap(" = ", defaultValue) + wrap(" ", join(directives, " "))
|
|
3143
|
+
},
|
|
3144
|
+
SelectionSet: {
|
|
3145
|
+
leave: ({ selections }) => block(selections)
|
|
3146
|
+
},
|
|
3147
|
+
Field: {
|
|
3148
|
+
leave({ alias, name, arguments: args, directives, selectionSet }) {
|
|
3149
|
+
const prefix = wrap("", alias, ": ") + name;
|
|
3150
|
+
let argsLine = prefix + wrap("(", join(args, ", "), ")");
|
|
3151
|
+
if (argsLine.length > MAX_LINE_LENGTH) {
|
|
3152
|
+
argsLine = prefix + wrap("(\n", indent(join(args, "\n")), "\n)");
|
|
3153
|
+
}
|
|
3154
|
+
return join([argsLine, join(directives, " "), selectionSet], " ");
|
|
3155
|
+
}
|
|
3156
|
+
},
|
|
3157
|
+
Argument: {
|
|
3158
|
+
leave: ({ name, value }) => name + ": " + value
|
|
3159
|
+
},
|
|
3160
|
+
// Fragments
|
|
3161
|
+
FragmentSpread: {
|
|
3162
|
+
leave: ({ name, directives }) => "..." + name + wrap(" ", join(directives, " "))
|
|
3163
|
+
},
|
|
3164
|
+
InlineFragment: {
|
|
3165
|
+
leave: ({ typeCondition, directives, selectionSet }) => join(
|
|
3166
|
+
[
|
|
3167
|
+
"...",
|
|
3168
|
+
wrap("on ", typeCondition),
|
|
3169
|
+
join(directives, " "),
|
|
3170
|
+
selectionSet
|
|
3171
|
+
],
|
|
3172
|
+
" "
|
|
3173
|
+
)
|
|
3174
|
+
},
|
|
3175
|
+
FragmentDefinition: {
|
|
3176
|
+
leave: ({ name, typeCondition, variableDefinitions, directives, selectionSet }) => (
|
|
3177
|
+
// or removed in the future.
|
|
3178
|
+
`fragment ${name}${wrap("(", join(variableDefinitions, ", "), ")")} on ${typeCondition} ${wrap("", join(directives, " "), " ")}` + selectionSet
|
|
3179
|
+
)
|
|
3180
|
+
},
|
|
3181
|
+
// Value
|
|
3182
|
+
IntValue: {
|
|
3183
|
+
leave: ({ value }) => value
|
|
3184
|
+
},
|
|
3185
|
+
FloatValue: {
|
|
3186
|
+
leave: ({ value }) => value
|
|
3187
|
+
},
|
|
3188
|
+
StringValue: {
|
|
3189
|
+
leave: ({ value, block: isBlockString }) => isBlockString ? printBlockString(value) : printString(value)
|
|
3190
|
+
},
|
|
3191
|
+
BooleanValue: {
|
|
3192
|
+
leave: ({ value }) => value ? "true" : "false"
|
|
3193
|
+
},
|
|
3194
|
+
NullValue: {
|
|
3195
|
+
leave: () => "null"
|
|
3196
|
+
},
|
|
3197
|
+
EnumValue: {
|
|
3198
|
+
leave: ({ value }) => value
|
|
3199
|
+
},
|
|
3200
|
+
ListValue: {
|
|
3201
|
+
leave: ({ values }) => "[" + join(values, ", ") + "]"
|
|
3202
|
+
},
|
|
3203
|
+
ObjectValue: {
|
|
3204
|
+
leave: ({ fields }) => "{" + join(fields, ", ") + "}"
|
|
3205
|
+
},
|
|
3206
|
+
ObjectField: {
|
|
3207
|
+
leave: ({ name, value }) => name + ": " + value
|
|
3208
|
+
},
|
|
3209
|
+
// Directive
|
|
3210
|
+
Directive: {
|
|
3211
|
+
leave: ({ name, arguments: args }) => "@" + name + wrap("(", join(args, ", "), ")")
|
|
3212
|
+
},
|
|
3213
|
+
// Type
|
|
3214
|
+
NamedType: {
|
|
3215
|
+
leave: ({ name }) => name
|
|
3216
|
+
},
|
|
3217
|
+
ListType: {
|
|
3218
|
+
leave: ({ type }) => "[" + type + "]"
|
|
3219
|
+
},
|
|
3220
|
+
NonNullType: {
|
|
3221
|
+
leave: ({ type }) => type + "!"
|
|
3222
|
+
},
|
|
3223
|
+
// Type System Definitions
|
|
3224
|
+
SchemaDefinition: {
|
|
3225
|
+
leave: ({ description, directives, operationTypes }) => wrap("", description, "\n") + join(["schema", join(directives, " "), block(operationTypes)], " ")
|
|
3226
|
+
},
|
|
3227
|
+
OperationTypeDefinition: {
|
|
3228
|
+
leave: ({ operation, type }) => operation + ": " + type
|
|
3229
|
+
},
|
|
3230
|
+
ScalarTypeDefinition: {
|
|
3231
|
+
leave: ({ description, name, directives }) => wrap("", description, "\n") + join(["scalar", name, join(directives, " ")], " ")
|
|
3232
|
+
},
|
|
3233
|
+
ObjectTypeDefinition: {
|
|
3234
|
+
leave: ({ description, name, interfaces, directives, fields }) => wrap("", description, "\n") + join(
|
|
3235
|
+
[
|
|
3236
|
+
"type",
|
|
3237
|
+
name,
|
|
3238
|
+
wrap("implements ", join(interfaces, " & ")),
|
|
3239
|
+
join(directives, " "),
|
|
3240
|
+
block(fields)
|
|
3241
|
+
],
|
|
3242
|
+
" "
|
|
3243
|
+
)
|
|
3244
|
+
},
|
|
3245
|
+
FieldDefinition: {
|
|
3246
|
+
leave: ({ description, name, arguments: args, type, directives }) => wrap("", description, "\n") + name + (hasMultilineItems(args) ? wrap("(\n", indent(join(args, "\n")), "\n)") : wrap("(", join(args, ", "), ")")) + ": " + type + wrap(" ", join(directives, " "))
|
|
3247
|
+
},
|
|
3248
|
+
InputValueDefinition: {
|
|
3249
|
+
leave: ({ description, name, type, defaultValue, directives }) => wrap("", description, "\n") + join(
|
|
3250
|
+
[name + ": " + type, wrap("= ", defaultValue), join(directives, " ")],
|
|
3251
|
+
" "
|
|
3252
|
+
)
|
|
3253
|
+
},
|
|
3254
|
+
InterfaceTypeDefinition: {
|
|
3255
|
+
leave: ({ description, name, interfaces, directives, fields }) => wrap("", description, "\n") + join(
|
|
3256
|
+
[
|
|
3257
|
+
"interface",
|
|
3258
|
+
name,
|
|
3259
|
+
wrap("implements ", join(interfaces, " & ")),
|
|
3260
|
+
join(directives, " "),
|
|
3261
|
+
block(fields)
|
|
3262
|
+
],
|
|
3263
|
+
" "
|
|
3264
|
+
)
|
|
3265
|
+
},
|
|
3266
|
+
UnionTypeDefinition: {
|
|
3267
|
+
leave: ({ description, name, directives, types }) => wrap("", description, "\n") + join(
|
|
3268
|
+
["union", name, join(directives, " "), wrap("= ", join(types, " | "))],
|
|
3269
|
+
" "
|
|
3270
|
+
)
|
|
3271
|
+
},
|
|
3272
|
+
EnumTypeDefinition: {
|
|
3273
|
+
leave: ({ description, name, directives, values }) => wrap("", description, "\n") + join(["enum", name, join(directives, " "), block(values)], " ")
|
|
3274
|
+
},
|
|
3275
|
+
EnumValueDefinition: {
|
|
3276
|
+
leave: ({ description, name, directives }) => wrap("", description, "\n") + join([name, join(directives, " ")], " ")
|
|
3277
|
+
},
|
|
3278
|
+
InputObjectTypeDefinition: {
|
|
3279
|
+
leave: ({ description, name, directives, fields }) => wrap("", description, "\n") + join(["input", name, join(directives, " "), block(fields)], " ")
|
|
3280
|
+
},
|
|
3281
|
+
DirectiveDefinition: {
|
|
3282
|
+
leave: ({ description, name, arguments: args, repeatable, locations }) => wrap("", description, "\n") + "directive @" + name + (hasMultilineItems(args) ? wrap("(\n", indent(join(args, "\n")), "\n)") : wrap("(", join(args, ", "), ")")) + (repeatable ? " repeatable" : "") + " on " + join(locations, " | ")
|
|
3283
|
+
},
|
|
3284
|
+
SchemaExtension: {
|
|
3285
|
+
leave: ({ directives, operationTypes }) => join(
|
|
3286
|
+
["extend schema", join(directives, " "), block(operationTypes)],
|
|
3287
|
+
" "
|
|
3288
|
+
)
|
|
3289
|
+
},
|
|
3290
|
+
ScalarTypeExtension: {
|
|
3291
|
+
leave: ({ name, directives }) => join(["extend scalar", name, join(directives, " ")], " ")
|
|
3292
|
+
},
|
|
3293
|
+
ObjectTypeExtension: {
|
|
3294
|
+
leave: ({ name, interfaces, directives, fields }) => join(
|
|
3295
|
+
[
|
|
3296
|
+
"extend type",
|
|
3297
|
+
name,
|
|
3298
|
+
wrap("implements ", join(interfaces, " & ")),
|
|
3299
|
+
join(directives, " "),
|
|
3300
|
+
block(fields)
|
|
3301
|
+
],
|
|
3302
|
+
" "
|
|
3303
|
+
)
|
|
3304
|
+
},
|
|
3305
|
+
InterfaceTypeExtension: {
|
|
3306
|
+
leave: ({ name, interfaces, directives, fields }) => join(
|
|
3307
|
+
[
|
|
3308
|
+
"extend interface",
|
|
3309
|
+
name,
|
|
3310
|
+
wrap("implements ", join(interfaces, " & ")),
|
|
3311
|
+
join(directives, " "),
|
|
3312
|
+
block(fields)
|
|
3313
|
+
],
|
|
3314
|
+
" "
|
|
3315
|
+
)
|
|
3316
|
+
},
|
|
3317
|
+
UnionTypeExtension: {
|
|
3318
|
+
leave: ({ name, directives, types }) => join(
|
|
3319
|
+
[
|
|
3320
|
+
"extend union",
|
|
3321
|
+
name,
|
|
3322
|
+
join(directives, " "),
|
|
3323
|
+
wrap("= ", join(types, " | "))
|
|
3324
|
+
],
|
|
3325
|
+
" "
|
|
3326
|
+
)
|
|
3327
|
+
},
|
|
3328
|
+
EnumTypeExtension: {
|
|
3329
|
+
leave: ({ name, directives, values }) => join(["extend enum", name, join(directives, " "), block(values)], " ")
|
|
3330
|
+
},
|
|
3331
|
+
InputObjectTypeExtension: {
|
|
3332
|
+
leave: ({ name, directives, fields }) => join(["extend input", name, join(directives, " "), block(fields)], " ")
|
|
3333
|
+
}
|
|
3334
|
+
};
|
|
3335
|
+
function join(maybeArray, separator = "") {
|
|
3336
|
+
var _maybeArray$filter$jo;
|
|
3337
|
+
return (_maybeArray$filter$jo = maybeArray === null || maybeArray === void 0 ? void 0 : maybeArray.filter((x) => x).join(separator)) !== null && _maybeArray$filter$jo !== void 0 ? _maybeArray$filter$jo : "";
|
|
3338
|
+
}
|
|
3339
|
+
function block(array) {
|
|
3340
|
+
return wrap("{\n", indent(join(array, "\n")), "\n}");
|
|
3341
|
+
}
|
|
3342
|
+
function wrap(start, maybeString, end = "") {
|
|
3343
|
+
return maybeString != null && maybeString !== "" ? start + maybeString + end : "";
|
|
3344
|
+
}
|
|
3345
|
+
function indent(str) {
|
|
3346
|
+
return wrap(" ", str.replace(/\n/g, "\n "));
|
|
3347
|
+
}
|
|
3348
|
+
function hasMultilineItems(maybeArray) {
|
|
3349
|
+
var _maybeArray$some;
|
|
3350
|
+
return (_maybeArray$some = maybeArray === null || maybeArray === void 0 ? void 0 : maybeArray.some((str) => str.includes("\n"))) !== null && _maybeArray$some !== void 0 ? _maybeArray$some : false;
|
|
3351
|
+
}
|
|
3352
|
+
|
|
3353
|
+
/*!
|
|
3354
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
3355
|
+
* All rights reserved.
|
|
3356
|
+
* For full license text, see the LICENSE.txt file
|
|
3357
|
+
*/
|
|
3358
|
+
function findExecutableOperation(input) {
|
|
3359
|
+
const operations = input.query.definitions.filter(
|
|
3360
|
+
(def) => def.kind === Kind.OPERATION_DEFINITION
|
|
3361
|
+
);
|
|
3362
|
+
if (operations.length === 0) {
|
|
3363
|
+
return err$1(new Error("No operations found in query"));
|
|
3364
|
+
}
|
|
3365
|
+
if (operations.length === 1 && !input.operationName) {
|
|
3366
|
+
return ok$2(operations[0]);
|
|
3367
|
+
}
|
|
3368
|
+
if (input.operationName) {
|
|
3369
|
+
const specifiedOperation = operations.find((op) => {
|
|
3370
|
+
var _a;
|
|
3371
|
+
return ((_a = op.name) == null ? void 0 : _a.value) === input.operationName;
|
|
3372
|
+
});
|
|
3373
|
+
if (specifiedOperation) {
|
|
3374
|
+
return ok$2(specifiedOperation);
|
|
3375
|
+
}
|
|
3376
|
+
return err$1(new Error(`Operation "${input.operationName}" not found in query`));
|
|
3377
|
+
}
|
|
3378
|
+
return err$1(new Error("Multiple operations found in query, and no operation name provided"));
|
|
3379
|
+
}
|
|
3380
|
+
function buildGraphQLInputExtension(input) {
|
|
3381
|
+
const operationResult = findExecutableOperation(input);
|
|
3382
|
+
if (operationResult.isErr()) {
|
|
3383
|
+
return err$1(operationResult.error);
|
|
3384
|
+
}
|
|
3385
|
+
const operation = operationResult.value;
|
|
3386
|
+
const selections = operation.selectionSet.selections;
|
|
3387
|
+
const variableDefinitions = operation.variableDefinitions;
|
|
3388
|
+
const variables = (variableDefinitions == null ? void 0 : variableDefinitions.reduce((prev, def) => {
|
|
3389
|
+
var _a;
|
|
3390
|
+
prev[def.variable.name.value] = {
|
|
3391
|
+
definition: def,
|
|
3392
|
+
value: (_a = input.variables) == null ? void 0 : _a[def.variable.name.value]
|
|
3393
|
+
};
|
|
3394
|
+
return prev;
|
|
3395
|
+
}, {})) || {};
|
|
3396
|
+
const fragments = input.query.definitions.filter(isFragmentDefinition).reduce((prev, fragment) => {
|
|
3397
|
+
prev[fragment.name.value] = fragment;
|
|
3398
|
+
return prev;
|
|
3399
|
+
}, {});
|
|
3400
|
+
return ok$2({
|
|
3401
|
+
selections,
|
|
3402
|
+
request: { definitions: { variables, fragments } },
|
|
3403
|
+
parentFieldSelection: void 0
|
|
3404
|
+
});
|
|
3405
|
+
}
|
|
3406
|
+
const TYPENAME_FIELD = {
|
|
3407
|
+
kind: Kind.FIELD,
|
|
3408
|
+
name: {
|
|
3409
|
+
kind: Kind.NAME,
|
|
3410
|
+
value: "__typename"
|
|
3411
|
+
}
|
|
3412
|
+
};
|
|
3413
|
+
function addTypenameToDocument(doc) {
|
|
3414
|
+
return visit(doc, {
|
|
3415
|
+
SelectionSet: {
|
|
3416
|
+
enter(node, _key, parent) {
|
|
3417
|
+
if (isOperationDefinition(parent)) {
|
|
3418
|
+
return;
|
|
3419
|
+
}
|
|
3420
|
+
const { selections } = node;
|
|
3421
|
+
if (!selections || selections.length === 0) {
|
|
3422
|
+
return;
|
|
3423
|
+
}
|
|
3424
|
+
const skip = selections.some((selection) => {
|
|
3425
|
+
return isField(selection) && (selection.name.value === "__typename" || selection.name.value.lastIndexOf("__", 0) === 0);
|
|
3426
|
+
});
|
|
3427
|
+
if (skip) {
|
|
3428
|
+
return;
|
|
3429
|
+
}
|
|
3430
|
+
return {
|
|
3431
|
+
...node,
|
|
3432
|
+
selections: [...selections, TYPENAME_FIELD]
|
|
3433
|
+
};
|
|
3434
|
+
}
|
|
3435
|
+
}
|
|
3436
|
+
});
|
|
3437
|
+
}
|
|
3438
|
+
function isOperationDefinition(parent) {
|
|
3439
|
+
return !!parent && !Array.isArray(parent) && parent.kind === Kind.OPERATION_DEFINITION;
|
|
3440
|
+
}
|
|
3441
|
+
function isField(node) {
|
|
3442
|
+
return node.kind === Kind.FIELD;
|
|
3443
|
+
}
|
|
3444
|
+
function isFragmentDefinition(node) {
|
|
3445
|
+
return node.kind === Kind.FRAGMENT_DEFINITION;
|
|
3446
|
+
}
|
|
3447
|
+
|
|
3448
|
+
/*!
|
|
3449
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
3450
|
+
* All rights reserved.
|
|
3451
|
+
* For full license text, see the LICENSE.txt file
|
|
3452
|
+
*/
|
|
3453
|
+
class AuraGraphQLNormalizedCacheControlCommand extends AuraNormalizedCacheControlCommand {
|
|
3454
|
+
constructor(config, documentRootType, services) {
|
|
3455
|
+
super(services);
|
|
3456
|
+
this.config = config;
|
|
3457
|
+
this.documentRootType = documentRootType;
|
|
3458
|
+
this.services = services;
|
|
3459
|
+
}
|
|
3460
|
+
buildResultType() {
|
|
3461
|
+
return this.documentRootType;
|
|
3462
|
+
}
|
|
3463
|
+
buildQuery() {
|
|
3464
|
+
const extensionResult = buildGraphQLInputExtension(this.config);
|
|
3465
|
+
if (extensionResult.isErr()) {
|
|
3466
|
+
throw extensionResult.error;
|
|
3467
|
+
}
|
|
3468
|
+
return {
|
|
3469
|
+
type: "keyParams",
|
|
3470
|
+
keyParams: null,
|
|
3471
|
+
...extensionResult.value
|
|
3472
|
+
};
|
|
3473
|
+
}
|
|
3474
|
+
responseHasErrors(data) {
|
|
3475
|
+
return data.hasOwnProperty("errors") && data.errors && data.errors.length > 0;
|
|
3476
|
+
}
|
|
3477
|
+
processAuraReturnValue(auraReturnValue) {
|
|
3478
|
+
if (this.responseHasErrors(auraReturnValue)) {
|
|
3479
|
+
return err$1(new UserVisibleError(auraReturnValue));
|
|
3480
|
+
}
|
|
3481
|
+
return ok$2(auraReturnValue);
|
|
3482
|
+
}
|
|
3483
|
+
processFetchReturnValue(json) {
|
|
3484
|
+
if (this.responseHasErrors(json)) {
|
|
3485
|
+
return err$1(new UserVisibleError(json));
|
|
3486
|
+
}
|
|
3487
|
+
return ok$2(json);
|
|
3488
|
+
}
|
|
3489
|
+
/**
|
|
3490
|
+
* Optional body wrapper name for aura requests.
|
|
3491
|
+
* If defined, the aura params will be wrapped in an object with this property name.
|
|
3492
|
+
* Override this method in subclasses if a body wrapper is needed.
|
|
3493
|
+
*/
|
|
3494
|
+
get auraBodyWrapperName() {
|
|
3495
|
+
return void 0;
|
|
3496
|
+
}
|
|
3497
|
+
get auraParams() {
|
|
3498
|
+
const params = { ...this.config, query: print(this.buildRequestQuery()) };
|
|
3499
|
+
if (this.auraBodyWrapperName) {
|
|
3500
|
+
return { [this.auraBodyWrapperName]: params };
|
|
3501
|
+
}
|
|
3502
|
+
return params;
|
|
3503
|
+
}
|
|
3504
|
+
get fetchParams() {
|
|
3505
|
+
const body = { ...this.config, query: print(this.buildRequestQuery()) };
|
|
3506
|
+
const headers = {
|
|
3507
|
+
"Content-Type": "application/json"
|
|
3508
|
+
};
|
|
3509
|
+
const params = {
|
|
3510
|
+
method: "POST",
|
|
3511
|
+
cache: "no-cache",
|
|
3512
|
+
headers,
|
|
3513
|
+
body: JSON.stringify(body)
|
|
3514
|
+
};
|
|
3515
|
+
return [this.url, params];
|
|
3516
|
+
}
|
|
3517
|
+
get originalAuraParams() {
|
|
3518
|
+
const params = { ...this.config, query: print(this.config.query) };
|
|
3519
|
+
if (this.auraBodyWrapperName) {
|
|
3520
|
+
return { [this.auraBodyWrapperName]: params };
|
|
3521
|
+
}
|
|
3522
|
+
return params;
|
|
3523
|
+
}
|
|
3524
|
+
get originalFetchParams() {
|
|
3525
|
+
const body = { ...this.config, query: print(this.config.query) };
|
|
3526
|
+
const headers = {
|
|
3527
|
+
"Content-Type": "application/json"
|
|
3528
|
+
};
|
|
3529
|
+
const params = {
|
|
3530
|
+
method: "POST",
|
|
3531
|
+
cache: "no-cache",
|
|
3532
|
+
headers,
|
|
3533
|
+
body: JSON.stringify(body)
|
|
3534
|
+
};
|
|
3535
|
+
return [this.url, params];
|
|
3536
|
+
}
|
|
3537
|
+
// TODO: We should respect the cache control strategy in this method. We don't support only-if-cached strategies at the moment, but if we did add support, we would technically violate that cache control strategy if the data wasn't in the cache here.
|
|
3538
|
+
runOriginalRequest() {
|
|
3539
|
+
if (this.shouldUseAuraNetwork()) {
|
|
3540
|
+
return this.convertAuraResponseToData(
|
|
3541
|
+
this.services.auraNetwork(
|
|
3542
|
+
this.endpoint,
|
|
3543
|
+
this.originalAuraParams,
|
|
3544
|
+
this.actionConfig
|
|
3545
|
+
),
|
|
3546
|
+
(errs) => this.coerceError(errs)
|
|
3547
|
+
).then((result) => {
|
|
3548
|
+
return buildSubscribableResult$1(result, this.buildSubscribe(), () => this.refresh());
|
|
3549
|
+
});
|
|
3550
|
+
} else if (this.shouldUseFetch()) {
|
|
3551
|
+
return this.convertFetchResponseToData(
|
|
3552
|
+
this.services.fetch(...this.originalFetchParams)
|
|
3553
|
+
).then((result) => {
|
|
3554
|
+
return buildSubscribableResult$1(result, this.buildSubscribe(), () => this.refresh());
|
|
3555
|
+
});
|
|
3556
|
+
}
|
|
3557
|
+
return resolvedPromiseLike$3(
|
|
3558
|
+
buildSubscribableResult$1(
|
|
3559
|
+
err$1(toError("Aura/Fetch network services not found")),
|
|
3560
|
+
this.buildSubscribe(),
|
|
3561
|
+
() => this.refresh()
|
|
3562
|
+
)
|
|
3563
|
+
);
|
|
3564
|
+
}
|
|
3565
|
+
buildRequestQuery() {
|
|
3566
|
+
const augmentedQueryResult = this.documentRootType.buildAugmentedQuery(this.config);
|
|
3567
|
+
if (augmentedQueryResult.isErr()) {
|
|
3568
|
+
throw augmentedQueryResult.error;
|
|
3569
|
+
}
|
|
3570
|
+
return addTypenameToDocument(augmentedQueryResult.value);
|
|
3571
|
+
}
|
|
3572
|
+
buildWriteInput(data) {
|
|
3573
|
+
const extensionResult = buildGraphQLInputExtension({
|
|
3574
|
+
...this.config,
|
|
3575
|
+
query: this.buildRequestQuery()
|
|
3576
|
+
});
|
|
3577
|
+
if (extensionResult.isErr()) {
|
|
3578
|
+
throw new InternalError(extensionResult.error);
|
|
3579
|
+
}
|
|
3580
|
+
return { data: data.data, ...extensionResult.value };
|
|
3581
|
+
}
|
|
3582
|
+
// Any changes to this method should most likely be duplicated in the https command as well
|
|
3583
|
+
handleCacheControllerResult(result, requestRunner) {
|
|
3584
|
+
const { networkError, returnData } = requestRunner;
|
|
3585
|
+
return this.publishUpdatedKeys().then(() => {
|
|
3586
|
+
if (networkError || returnData === void 0 || returnData.isErr() || result.isErr()) {
|
|
3587
|
+
return this.runOriginalRequest();
|
|
3588
|
+
}
|
|
3589
|
+
if (this.subscriptions.length > 0) {
|
|
3590
|
+
this.subscribeToKeysUsed();
|
|
3591
|
+
}
|
|
3592
|
+
return returnData;
|
|
3593
|
+
});
|
|
3594
|
+
}
|
|
3595
|
+
}
|
|
3596
|
+
function buildServiceDescriptor$5() {
|
|
3597
|
+
return {
|
|
3598
|
+
type: "auraGraphQLNormalizedCacheControlCommand",
|
|
3599
|
+
version: "1.0",
|
|
3600
|
+
service: AuraGraphQLNormalizedCacheControlCommand
|
|
3601
|
+
};
|
|
3602
|
+
}
|
|
3603
|
+
|
|
3604
|
+
/*!
|
|
3605
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
3606
|
+
* All rights reserved.
|
|
3607
|
+
* For full license text, see the LICENSE.txt file
|
|
3608
|
+
*/
|
|
3609
|
+
class HttpGraphQLNormalizedCacheControlCommand extends HttpNormalizedCacheControlCommand {
|
|
3610
|
+
constructor(config, documentRootType, services) {
|
|
3611
|
+
super(services);
|
|
3612
|
+
this.config = config;
|
|
3613
|
+
this.documentRootType = documentRootType;
|
|
3614
|
+
this.services = services;
|
|
3615
|
+
}
|
|
3616
|
+
buildResultType() {
|
|
3617
|
+
return this.documentRootType;
|
|
3618
|
+
}
|
|
3619
|
+
buildQuery() {
|
|
3620
|
+
const extensionResult = buildGraphQLInputExtension(this.config);
|
|
3621
|
+
if (extensionResult.isErr()) {
|
|
3622
|
+
throw extensionResult.error;
|
|
3623
|
+
}
|
|
3624
|
+
return {
|
|
3625
|
+
type: "keyParams",
|
|
3626
|
+
keyParams: null,
|
|
3627
|
+
...extensionResult.value
|
|
3628
|
+
};
|
|
3629
|
+
}
|
|
3630
|
+
get fetchParams() {
|
|
3631
|
+
const body = { ...this.config, query: print(this.buildRequestQuery()) };
|
|
3632
|
+
const headers = {
|
|
3633
|
+
"Content-Type": "application/json"
|
|
3634
|
+
};
|
|
3635
|
+
const params = {
|
|
3636
|
+
method: "POST",
|
|
3637
|
+
cache: "no-cache",
|
|
3638
|
+
headers,
|
|
3639
|
+
body: JSON.stringify(body)
|
|
3640
|
+
};
|
|
3641
|
+
return [this.url, params];
|
|
3642
|
+
}
|
|
3643
|
+
get originalFetchParams() {
|
|
3644
|
+
const body = { ...this.config, query: print(this.config.query) };
|
|
3645
|
+
const headers = {
|
|
3646
|
+
"Content-Type": "application/json"
|
|
3647
|
+
};
|
|
3648
|
+
const params = {
|
|
3649
|
+
method: "POST",
|
|
3650
|
+
cache: "no-cache",
|
|
3651
|
+
headers,
|
|
3652
|
+
body: JSON.stringify(body)
|
|
3653
|
+
};
|
|
3654
|
+
return [this.url, params];
|
|
3655
|
+
}
|
|
3656
|
+
buildRequestQuery() {
|
|
3657
|
+
const augmentedQueryResult = this.documentRootType.buildAugmentedQuery(this.config);
|
|
3658
|
+
if (augmentedQueryResult.isErr()) {
|
|
3659
|
+
throw augmentedQueryResult.error;
|
|
3660
|
+
}
|
|
3661
|
+
return addTypenameToDocument(augmentedQueryResult.value);
|
|
3662
|
+
}
|
|
3663
|
+
buildWriteInput(data) {
|
|
3664
|
+
const extensionResult = buildGraphQLInputExtension({
|
|
3665
|
+
...this.config,
|
|
3666
|
+
query: this.buildRequestQuery()
|
|
3667
|
+
});
|
|
3668
|
+
if (extensionResult.isErr()) {
|
|
3669
|
+
throw new InternalError(extensionResult.error);
|
|
3670
|
+
}
|
|
3671
|
+
return { data: data.data, ...extensionResult.value };
|
|
3672
|
+
}
|
|
3673
|
+
processFetchReturnValue(json) {
|
|
3674
|
+
if (json.hasOwnProperty("errors") && json.errors && json.errors.length > 0) {
|
|
3675
|
+
return err$1(new UserVisibleError(json));
|
|
3676
|
+
}
|
|
3677
|
+
return ok$2(json);
|
|
3678
|
+
}
|
|
3679
|
+
runOriginalRequest() {
|
|
3680
|
+
return this.convertFetchResponseToData(
|
|
3681
|
+
this.services.fetch(...this.originalFetchParams)
|
|
3682
|
+
).then((result) => {
|
|
3683
|
+
return buildSubscribableResult$1(result, this.buildSubscribe(), () => this.refresh());
|
|
3684
|
+
});
|
|
3685
|
+
}
|
|
3686
|
+
// Any changes to this method should most likely be duplicated in the aura command as well
|
|
3687
|
+
handleCacheControllerResult(result, requestRunner) {
|
|
3688
|
+
const { networkError, returnData } = requestRunner;
|
|
3689
|
+
return this.publishUpdatedKeys().then(() => {
|
|
3690
|
+
if (networkError || returnData === void 0 || returnData.isErr() || result.isErr()) {
|
|
3691
|
+
return this.runOriginalRequest();
|
|
3692
|
+
}
|
|
3693
|
+
if (this.subscriptions.length > 0) {
|
|
3694
|
+
this.subscribeToKeysUsed();
|
|
3695
|
+
}
|
|
3696
|
+
return returnData;
|
|
3697
|
+
});
|
|
3698
|
+
}
|
|
3699
|
+
}
|
|
3700
|
+
function buildServiceDescriptor$4() {
|
|
3701
|
+
return {
|
|
3702
|
+
type: "httpGraphQLNormalizedCacheControlCommand",
|
|
3703
|
+
version: "1.0",
|
|
3704
|
+
service: HttpGraphQLNormalizedCacheControlCommand
|
|
3705
|
+
};
|
|
3706
|
+
}
|
|
3707
|
+
|
|
3708
|
+
/*!
|
|
3709
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
3710
|
+
* All rights reserved.
|
|
3711
|
+
* For full license text, see the LICENSE.txt file
|
|
3712
|
+
*/
|
|
3713
|
+
class NDJSONCommand extends StreamingCommand {
|
|
3714
|
+
constructor(services) {
|
|
3715
|
+
super(services);
|
|
3716
|
+
this.services = services;
|
|
3717
|
+
}
|
|
3718
|
+
/**
|
|
3719
|
+
* Decodes the bytes returned by fetch into the correct shape.
|
|
3720
|
+
*
|
|
3721
|
+
* @param byteStream ReadableStream<Uint8Array> returned by fetch
|
|
3722
|
+
* @returns ReadableStream<T> that will be the result of the Command
|
|
3723
|
+
*/
|
|
3724
|
+
decodeByteStream(byteStream) {
|
|
3725
|
+
return this.decodeNDJSONStream(
|
|
3726
|
+
byteStream.pipeThrough(new TextDecoderStream()).pipeThrough(new NDJSONParsingStream())
|
|
3727
|
+
);
|
|
3728
|
+
}
|
|
3729
|
+
}
|
|
3730
|
+
class NDJSONParsingStream extends TransformStream {
|
|
3731
|
+
constructor() {
|
|
3732
|
+
let partialLine = "";
|
|
3733
|
+
super({
|
|
3734
|
+
transform(chunk, controller) {
|
|
3735
|
+
const text = partialLine + chunk;
|
|
3736
|
+
const lines = text.split(/\r?\n/);
|
|
3737
|
+
partialLine = lines.pop() || "";
|
|
3738
|
+
for (const line of lines) {
|
|
3739
|
+
const trimmed = line.trim();
|
|
3740
|
+
if (trimmed === "") continue;
|
|
3741
|
+
try {
|
|
3742
|
+
const parsed = JSON.parse(trimmed);
|
|
3743
|
+
controller.enqueue(parsed);
|
|
3744
|
+
} catch (e) {
|
|
3745
|
+
throw new Error(`Invalid NDJSON line: ${line}`);
|
|
3746
|
+
}
|
|
3747
|
+
}
|
|
3748
|
+
},
|
|
3749
|
+
flush(controller) {
|
|
3750
|
+
const trimmed = partialLine.trim();
|
|
3751
|
+
if (trimmed !== "") {
|
|
3752
|
+
try {
|
|
3753
|
+
const parsed = JSON.parse(trimmed);
|
|
3754
|
+
controller.enqueue(parsed);
|
|
3755
|
+
} catch (e) {
|
|
3756
|
+
throw new Error(`Invalid NDJSON final line: ${partialLine}`);
|
|
3757
|
+
}
|
|
3758
|
+
}
|
|
3759
|
+
}
|
|
3760
|
+
});
|
|
3761
|
+
}
|
|
3762
|
+
}
|
|
3763
|
+
function buildServiceDescriptor$3() {
|
|
3764
|
+
return {
|
|
3765
|
+
type: "NDJSONCommandBaseClass",
|
|
3766
|
+
version: "1.0",
|
|
3767
|
+
service: NDJSONCommand
|
|
3768
|
+
};
|
|
3769
|
+
}
|
|
3770
|
+
|
|
3771
|
+
/**
|
|
3772
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
3773
|
+
* All rights reserved.
|
|
3774
|
+
* For full license text, see the LICENSE.txt file
|
|
3775
|
+
*/
|
|
3776
|
+
|
|
3777
|
+
/*
|
|
3778
|
+
* ATTENTION!
|
|
3779
|
+
* THIS IS A GENERATED FILE FROM https://github.com/salesforce-experience-platform-emu/lds-lightning-platform
|
|
3780
|
+
* If you would like to contribute to LDS, please follow the steps outlined in the git repo.
|
|
3781
|
+
* Any changes made to this file in p4 will be automatically overwritten.
|
|
3782
|
+
* *******************************************************************************************
|
|
3783
|
+
*/
|
|
3784
|
+
/* proxy-compat-disable */
|
|
3785
|
+
/**
|
|
3786
|
+
* Creates a LuvioService descriptor with the provided Luvio instance.
|
|
3787
|
+
* This function follows the standard service builder pattern used
|
|
3788
|
+
* throughout the Luvio service ecosystem.
|
|
3789
|
+
*
|
|
3790
|
+
* @param luvio The Luvio engine instance to wrap in the service
|
|
3791
|
+
* @returns A service descriptor that can be used in dependency injection
|
|
3792
|
+
*
|
|
3793
|
+
* @example
|
|
3794
|
+
* ```typescript
|
|
3795
|
+
* const luvioService = buildLuvioService(myLuvioInstance);
|
|
3796
|
+
*
|
|
3797
|
+
* // Use in a services object
|
|
3798
|
+
* const services = {
|
|
3799
|
+
* ...otherServices,
|
|
3800
|
+
* luvio: luvioService.service,
|
|
3801
|
+
* };
|
|
3802
|
+
* ```
|
|
3803
|
+
*/
|
|
3804
|
+
function buildServiceDescriptor$2(luvio) {
|
|
3805
|
+
return {
|
|
3806
|
+
type: 'luvio',
|
|
3807
|
+
version: '1.0',
|
|
3808
|
+
service: {
|
|
3809
|
+
luvio,
|
|
3810
|
+
},
|
|
3811
|
+
};
|
|
3812
|
+
}
|
|
3813
|
+
// version: 0.1.0-dev1-c978a7b010
|
|
3814
|
+
|
|
3815
|
+
/**
|
|
3816
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
3817
|
+
* All rights reserved.
|
|
3818
|
+
* For full license text, see the LICENSE.txt file
|
|
3819
|
+
*/
|
|
3820
|
+
|
|
3821
|
+
/*
|
|
3822
|
+
* ATTENTION!
|
|
3823
|
+
* THIS IS A GENERATED FILE FROM https://github.com/salesforce-experience-platform-emu/lds-lightning-platform
|
|
3824
|
+
* If you would like to contribute to LDS, please follow the steps outlined in the git repo.
|
|
3825
|
+
* Any changes made to this file in p4 will be automatically overwritten.
|
|
3826
|
+
* *******************************************************************************************
|
|
3827
|
+
*/
|
|
3828
|
+
/* proxy-compat-disable */
|
|
3829
|
+
function buildServiceDescriptor$1(notifyRecordUpdateAvailable, getNormalizedLuvioRecord) {
|
|
3830
|
+
return {
|
|
3831
|
+
type: 'luvioUiapiRecords',
|
|
3832
|
+
version: '1.0',
|
|
3833
|
+
service: {
|
|
3834
|
+
notifyRecordUpdateAvailable,
|
|
3835
|
+
getNormalizedLuvioRecord,
|
|
3836
|
+
},
|
|
3837
|
+
};
|
|
3838
|
+
}
|
|
3839
|
+
// version: 0.1.0-dev1-c978a7b010
|
|
3840
|
+
|
|
3841
|
+
/*!
|
|
3842
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
3843
|
+
* All rights reserved.
|
|
3844
|
+
* For full license text, see the LICENSE.txt file
|
|
3845
|
+
*/
|
|
3846
|
+
function e(e2) {
|
|
3847
|
+
this.message = e2;
|
|
3848
|
+
}
|
|
3849
|
+
e.prototype = new Error(), e.prototype.name = "InvalidCharacterError";
|
|
3850
|
+
var r = "undefined" != typeof window && window.atob && window.atob.bind(window) || function(r2) {
|
|
3851
|
+
var t2 = String(r2).replace(/=+$/, "");
|
|
3852
|
+
if (t2.length % 4 == 1) throw new e("'atob' failed: The string to be decoded is not correctly encoded.");
|
|
3853
|
+
for (var n2, o2, a = 0, i = 0, c = ""; o2 = t2.charAt(i++); ~o2 && (n2 = a % 4 ? 64 * n2 + o2 : o2, a++ % 4) ? c += String.fromCharCode(255 & n2 >> (-2 * a & 6)) : 0) o2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".indexOf(o2);
|
|
3854
|
+
return c;
|
|
3855
|
+
};
|
|
3856
|
+
function t(e2) {
|
|
3857
|
+
var t2 = e2.replace(/-/g, "+").replace(/_/g, "/");
|
|
3858
|
+
switch (t2.length % 4) {
|
|
3859
|
+
case 0:
|
|
3860
|
+
break;
|
|
3861
|
+
case 2:
|
|
3862
|
+
t2 += "==";
|
|
3863
|
+
break;
|
|
3864
|
+
case 3:
|
|
3865
|
+
t2 += "=";
|
|
3866
|
+
break;
|
|
3867
|
+
default:
|
|
3868
|
+
throw "Illegal base64url string!";
|
|
3869
|
+
}
|
|
3870
|
+
try {
|
|
3871
|
+
return (function(e3) {
|
|
3872
|
+
return decodeURIComponent(r(e3).replace(/(.)/g, (function(e4, r2) {
|
|
3873
|
+
var t3 = r2.charCodeAt(0).toString(16).toUpperCase();
|
|
3874
|
+
return t3.length < 2 && (t3 = "0" + t3), "%" + t3;
|
|
3875
|
+
})));
|
|
3876
|
+
})(t2);
|
|
3877
|
+
} catch (e3) {
|
|
3878
|
+
return r(t2);
|
|
3879
|
+
}
|
|
3880
|
+
}
|
|
3881
|
+
function n(e2) {
|
|
3882
|
+
this.message = e2;
|
|
3883
|
+
}
|
|
3884
|
+
function o(e2, r2) {
|
|
3885
|
+
if ("string" != typeof e2) throw new n("Invalid token specified");
|
|
3886
|
+
var o2 = true === (r2 = r2 || {}).header ? 0 : 1;
|
|
3887
|
+
try {
|
|
3888
|
+
return JSON.parse(t(e2.split(".")[o2]));
|
|
3889
|
+
} catch (e3) {
|
|
3890
|
+
throw new n("Invalid token specified: " + e3.message);
|
|
3891
|
+
}
|
|
3892
|
+
}
|
|
3893
|
+
n.prototype = new Error(), n.prototype.name = "InvalidTokenError";
|
|
3894
|
+
/*!
|
|
3895
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
3896
|
+
* All rights reserved.
|
|
3897
|
+
* For full license text, see the LICENSE.txt file
|
|
3898
|
+
*/
|
|
3899
|
+
const LogLevelMap = {
|
|
3900
|
+
TRACE: 4,
|
|
3901
|
+
DEBUG: 3,
|
|
3902
|
+
INFO: 2,
|
|
3903
|
+
WARN: 1,
|
|
3904
|
+
ERROR: 0
|
|
3905
|
+
};
|
|
3906
|
+
class ConsoleLogger {
|
|
3907
|
+
constructor(level = "WARN", printer = console.log, formatter = (level2, message) => `${level2}: ${message}`) {
|
|
3908
|
+
this.level = level;
|
|
3909
|
+
this.printer = printer;
|
|
3910
|
+
this.formatter = formatter;
|
|
3911
|
+
this.messages = [];
|
|
3912
|
+
}
|
|
3913
|
+
trace(message) {
|
|
3914
|
+
this.log("TRACE", message);
|
|
3915
|
+
}
|
|
3916
|
+
debug(message) {
|
|
3917
|
+
this.log("DEBUG", message);
|
|
3918
|
+
}
|
|
3919
|
+
info(message) {
|
|
3920
|
+
this.log("INFO", message);
|
|
3921
|
+
}
|
|
3922
|
+
warn(message) {
|
|
3923
|
+
this.log("WARN", message);
|
|
3924
|
+
}
|
|
3925
|
+
error(message) {
|
|
3926
|
+
this.log("ERROR", message);
|
|
3927
|
+
}
|
|
3928
|
+
log(level, message) {
|
|
3929
|
+
if (LogLevelMap[level] > LogLevelMap[this.level]) {
|
|
3930
|
+
return;
|
|
3931
|
+
}
|
|
3932
|
+
this.printer(this.formatter(level, message));
|
|
3933
|
+
}
|
|
3934
|
+
}
|
|
3935
|
+
function loggerService$1(level, printer, formatter) {
|
|
3936
|
+
return new ConsoleLogger(level, printer, formatter);
|
|
3937
|
+
}
|
|
3938
|
+
class JwtToken {
|
|
3939
|
+
/**
|
|
3940
|
+
* Create a new JwtToken.
|
|
3941
|
+
*
|
|
3942
|
+
* @param _token - The JWT string.
|
|
3943
|
+
* @param _decodedInfo - The decoded information from the JWT.
|
|
3944
|
+
* @param _extraInfo - Any additional information associated with the JWT.
|
|
3945
|
+
*/
|
|
3946
|
+
constructor(_token, _decodedInfo, _extraInfo) {
|
|
3947
|
+
this._token = _token;
|
|
3948
|
+
this._decodedInfo = _decodedInfo;
|
|
3949
|
+
this._extraInfo = _extraInfo;
|
|
3950
|
+
}
|
|
3951
|
+
/**
|
|
3952
|
+
* Get the JWT string.
|
|
3953
|
+
*
|
|
3954
|
+
* @returns The JWT string.
|
|
3955
|
+
*/
|
|
3956
|
+
get token() {
|
|
3957
|
+
return this._token;
|
|
3958
|
+
}
|
|
3959
|
+
/**
|
|
3960
|
+
* Get the additional information associated with the JWT.
|
|
3961
|
+
*
|
|
3962
|
+
* @returns The additional information.
|
|
3963
|
+
*/
|
|
3964
|
+
get extraInfo() {
|
|
3965
|
+
return this._extraInfo;
|
|
3966
|
+
}
|
|
3967
|
+
/**
|
|
3968
|
+
* Get the decoded information from the JWT.
|
|
3969
|
+
*
|
|
3970
|
+
* @returns The decoded information.
|
|
3971
|
+
*/
|
|
3972
|
+
get decodedInfo() {
|
|
3973
|
+
return this._decodedInfo;
|
|
3974
|
+
}
|
|
3975
|
+
/**
|
|
3976
|
+
* Get the remaining time in seconds until the JWT expires.
|
|
3977
|
+
*
|
|
3978
|
+
* @returns The remaining time in seconds.
|
|
3979
|
+
*/
|
|
3980
|
+
get tokenRemainingSeconds() {
|
|
3981
|
+
return this.decodedInfo.exp - Date.now() / 1e3;
|
|
3982
|
+
}
|
|
3983
|
+
/**
|
|
3984
|
+
* Check if the JWT is expired.
|
|
3985
|
+
*
|
|
3986
|
+
* @returns True if the JWT is expired, false otherwise.
|
|
3987
|
+
*/
|
|
3988
|
+
get isExpired() {
|
|
3989
|
+
return this.tokenRemainingSeconds <= 0;
|
|
3990
|
+
}
|
|
3991
|
+
}
|
|
3992
|
+
let defaultLogger = {
|
|
3993
|
+
trace: () => {
|
|
3994
|
+
},
|
|
3995
|
+
debug: () => {
|
|
3996
|
+
},
|
|
3997
|
+
info: () => {
|
|
3998
|
+
},
|
|
3999
|
+
warn: () => {
|
|
4000
|
+
},
|
|
4001
|
+
error: () => {
|
|
4002
|
+
}
|
|
4003
|
+
};
|
|
4004
|
+
if (process.env.NODE_ENV !== "production") {
|
|
4005
|
+
defaultLogger = loggerService$1();
|
|
4006
|
+
}
|
|
4007
|
+
function computeDecodedInfo(token, defaultTokenTTLInSeconds, logger) {
|
|
4008
|
+
const decodedInfo = o(token);
|
|
4009
|
+
if (decodedInfo.exp === void 0) {
|
|
4010
|
+
logger.warn(`"exp" claim is not present in the provided token.`);
|
|
4011
|
+
decodedInfo.exp = Date.now() / 1e3 + defaultTokenTTLInSeconds;
|
|
4012
|
+
}
|
|
4013
|
+
return decodedInfo;
|
|
4014
|
+
}
|
|
4015
|
+
class JwtRepository {
|
|
4016
|
+
/**
|
|
4017
|
+
* @param limitInSeconds - Time in seconds before the token's expiry to notify observers.
|
|
4018
|
+
* @param defaultTokenTTLInSeconds - Default token expiry time in seconds if "exp" claim is not present in token.
|
|
4019
|
+
* @param logger - Logger for logging warnings and errors.
|
|
4020
|
+
*/
|
|
4021
|
+
constructor(limitInSeconds = 5, defaultTokenTTLInSeconds = 120, logger = defaultLogger) {
|
|
4022
|
+
this.limitInSeconds = limitInSeconds;
|
|
4023
|
+
this.defaultTokenTTLInSeconds = defaultTokenTTLInSeconds;
|
|
4024
|
+
this.logger = logger;
|
|
4025
|
+
this.observers = [];
|
|
4026
|
+
}
|
|
4027
|
+
/**
|
|
4028
|
+
* Get the current token.
|
|
4029
|
+
*/
|
|
4030
|
+
get token() {
|
|
4031
|
+
return this._token;
|
|
4032
|
+
}
|
|
4033
|
+
/**
|
|
4034
|
+
* Set the current token.
|
|
4035
|
+
*
|
|
4036
|
+
* @param token - JWT token as a string.
|
|
4037
|
+
* @param extraInfo - Optional extra information.
|
|
4038
|
+
*/
|
|
4039
|
+
setToken(token, extraInfo) {
|
|
4040
|
+
const decodedInfo = computeDecodedInfo(
|
|
4041
|
+
token,
|
|
4042
|
+
this.defaultTokenTTLInSeconds,
|
|
4043
|
+
this.logger
|
|
4044
|
+
);
|
|
4045
|
+
this._token = new JwtToken(token, decodedInfo, extraInfo);
|
|
4046
|
+
this.observeTokenExpiration();
|
|
4047
|
+
return this._token;
|
|
4048
|
+
}
|
|
4049
|
+
/**
|
|
4050
|
+
* Remove the current token.
|
|
4051
|
+
*/
|
|
4052
|
+
removeToken() {
|
|
4053
|
+
this._token = void 0;
|
|
4054
|
+
this.clearTimeoutHandler();
|
|
4055
|
+
}
|
|
4056
|
+
/**
|
|
4057
|
+
* Subscribe to the token nearing its expiration.
|
|
4058
|
+
*
|
|
4059
|
+
* @param cb - Callback function to execute when token is nearing expiration.
|
|
4060
|
+
*/
|
|
4061
|
+
subscribeToTokenNearExpiration(cb) {
|
|
4062
|
+
this.observers.push(cb);
|
|
4063
|
+
this.observeTokenExpiration();
|
|
4064
|
+
return () => {
|
|
4065
|
+
this.observers = this.observers.filter((observer) => observer !== cb);
|
|
4066
|
+
};
|
|
4067
|
+
}
|
|
4068
|
+
/**
|
|
4069
|
+
* Clear the timeout handler.
|
|
4070
|
+
*/
|
|
4071
|
+
clearTimeoutHandler() {
|
|
4072
|
+
if (this.timeoutHandler !== void 0) {
|
|
4073
|
+
clearTimeout(this.timeoutHandler);
|
|
4074
|
+
}
|
|
4075
|
+
}
|
|
4076
|
+
/**
|
|
4077
|
+
* Observe and handle token expiration.
|
|
4078
|
+
*/
|
|
4079
|
+
observeTokenExpiration() {
|
|
4080
|
+
this.clearTimeoutHandler();
|
|
4081
|
+
if (this.observers.length === 0 || this.token === void 0) {
|
|
4082
|
+
return;
|
|
4083
|
+
}
|
|
4084
|
+
this.timeoutHandler = setTimeout(
|
|
4085
|
+
() => this.notifyTokenIsExpiring(),
|
|
4086
|
+
this.computeTimeoutTimeInMs()
|
|
4087
|
+
);
|
|
4088
|
+
}
|
|
4089
|
+
/**
|
|
4090
|
+
* Compute the timeout time in milliseconds.
|
|
4091
|
+
*/
|
|
4092
|
+
computeTimeoutTimeInMs() {
|
|
4093
|
+
const remainingSeconds = this.token.tokenRemainingSeconds;
|
|
4094
|
+
let timeoutTimeInSeconds = remainingSeconds - this.limitInSeconds;
|
|
4095
|
+
return timeoutTimeInSeconds < 0 ? 0 : timeoutTimeInSeconds * 1e3;
|
|
4096
|
+
}
|
|
4097
|
+
/**
|
|
4098
|
+
* Notify all observers that the token is expiring.
|
|
4099
|
+
*/
|
|
4100
|
+
notifyTokenIsExpiring() {
|
|
4101
|
+
this.observers.forEach((cb) => {
|
|
4102
|
+
try {
|
|
4103
|
+
cb.call(void 0, this.token);
|
|
4104
|
+
} catch (e2) {
|
|
4105
|
+
this.logger.error(e2.message);
|
|
4106
|
+
}
|
|
4107
|
+
});
|
|
4108
|
+
}
|
|
4109
|
+
}
|
|
4110
|
+
class JwtManager {
|
|
4111
|
+
/**
|
|
4112
|
+
* Constructor for JwtManager class.
|
|
4113
|
+
*
|
|
4114
|
+
* @param {JwtRepository<T, ExtraInfo>} jwtRepository JwtRepository instance used for token management.
|
|
4115
|
+
* @param {JwtResolver<ExtraInfo>} resolver JwtResolver instance used for token retrieval.
|
|
4116
|
+
* @param {JwtManagerOptions} options JwtManagerOptions bag to customize behavior.
|
|
4117
|
+
*/
|
|
4118
|
+
constructor(jwtRepository, resolver, options) {
|
|
4119
|
+
this.jwtRepository = jwtRepository;
|
|
4120
|
+
this.resolver = resolver;
|
|
4121
|
+
if (options == null ? void 0 : options.keepTokenUpdated) {
|
|
4122
|
+
jwtRepository.subscribeToTokenNearExpiration(() => this.refreshToken());
|
|
4123
|
+
}
|
|
4124
|
+
}
|
|
4125
|
+
/**
|
|
4126
|
+
* Method to get a JWT token.
|
|
4127
|
+
* If there's a token request in progress, it will return the Promise of this request.
|
|
4128
|
+
* If the current token is undefined or expired, it will initiate a token refresh.
|
|
4129
|
+
* Otherwise, it will return the current token.
|
|
4130
|
+
*
|
|
4131
|
+
* @returns {JwtToken<T, ExtraInfo> | Promise<JwtToken<T, ExtraInfo>>} The current token or the Promise of a token request.
|
|
4132
|
+
*/
|
|
4133
|
+
getJwt() {
|
|
4134
|
+
if (this.inflightPromise) {
|
|
4135
|
+
return this.inflightPromise;
|
|
4136
|
+
}
|
|
4137
|
+
const token = this.jwtRepository.token;
|
|
4138
|
+
if (token === void 0 || token.isExpired) {
|
|
4139
|
+
return this.refreshToken();
|
|
4140
|
+
}
|
|
4141
|
+
return token;
|
|
4142
|
+
}
|
|
4143
|
+
/**
|
|
4144
|
+
* Method to refresh a JWT token.
|
|
4145
|
+
* If a refresh request is already in progress, it will return the Promise of this request.
|
|
4146
|
+
* Otherwise, it will start a new refresh request and return its Promise.
|
|
4147
|
+
*
|
|
4148
|
+
* @returns {Promise<JwtToken<T, ExtraInfo>>} Promise of the refreshed token.
|
|
4149
|
+
*/
|
|
4150
|
+
refreshToken() {
|
|
4151
|
+
if (this.inflightPromise === void 0) {
|
|
4152
|
+
this.inflightPromise = new Promise((resolve, reject) => {
|
|
4153
|
+
this.resolver.getJwt().then(({ jwt, extraInfo }) => {
|
|
4154
|
+
this.inflightPromise = void 0;
|
|
4155
|
+
const token = this.jwtRepository.setToken(jwt, extraInfo);
|
|
4156
|
+
resolve(token);
|
|
4157
|
+
}).catch((reason) => {
|
|
4158
|
+
this.inflightPromise = void 0;
|
|
4159
|
+
reject(reason);
|
|
4160
|
+
});
|
|
4161
|
+
});
|
|
4162
|
+
}
|
|
4163
|
+
return this.inflightPromise;
|
|
4164
|
+
}
|
|
4165
|
+
/**
|
|
4166
|
+
* Method to check if a token refresh is in progress.
|
|
4167
|
+
*
|
|
4168
|
+
* @returns {boolean} true if a token refresh is in progress, false otherwise.
|
|
4169
|
+
*/
|
|
4170
|
+
get isRefreshingToken() {
|
|
4171
|
+
return this.inflightPromise !== void 0;
|
|
4172
|
+
}
|
|
4173
|
+
}
|
|
4174
|
+
|
|
4175
|
+
/*!
|
|
4176
|
+
* Copyright (c) 2022, Salesforce, Inc.,
|
|
4177
|
+
* All rights reserved.
|
|
4178
|
+
* For full license text, see the LICENSE.txt file
|
|
4179
|
+
*/
|
|
4180
|
+
function buildServiceDescriptor(interceptors = { request: [], response: [] }) {
|
|
4181
|
+
return {
|
|
4182
|
+
type: "fetch",
|
|
4183
|
+
version: "1.0",
|
|
4184
|
+
service: function(...args) {
|
|
4185
|
+
const { request: requestInterceptors = [], response: responseInterceptors = [] } = interceptors;
|
|
4186
|
+
const pending = requestInterceptors.reduce(
|
|
4187
|
+
(previousPromise, interceptor) => previousPromise.then(interceptor),
|
|
4188
|
+
resolvedPromiseLike$3(args)
|
|
4189
|
+
);
|
|
4190
|
+
return pending.then((args2) => fetch(...args2)).then((response) => {
|
|
4191
|
+
return responseInterceptors.reduce(
|
|
4192
|
+
(previousPromise, interceptor) => previousPromise.then(interceptor),
|
|
4193
|
+
resolvedPromiseLike$3(response)
|
|
4194
|
+
);
|
|
4195
|
+
});
|
|
4196
|
+
}
|
|
4197
|
+
};
|
|
4198
|
+
}
|
|
4199
|
+
const UNEXPECTED_AUTHORIZATION_HEADER_MESSAGE = "Unexpected Authorization header encountered. To specify a custom Authorization header, use a Fetch service that is not configured with JwtRequestHeaderInterceptor";
|
|
4200
|
+
function setHeaderAuthorization({ token }, [resource, options = {}]) {
|
|
4201
|
+
let hasAuthorizationHeaderBeenSet = false;
|
|
4202
|
+
const Authorization = `Bearer ${token}`;
|
|
4203
|
+
if (resource instanceof Request && !(options == null ? void 0 : options.headers)) {
|
|
4204
|
+
if (resource.headers.has("Authorization")) {
|
|
4205
|
+
throw new Error(UNEXPECTED_AUTHORIZATION_HEADER_MESSAGE);
|
|
4206
|
+
}
|
|
4207
|
+
resource.headers.set("Authorization", Authorization);
|
|
4208
|
+
hasAuthorizationHeaderBeenSet = true;
|
|
4209
|
+
}
|
|
4210
|
+
if ((options == null ? void 0 : options.headers) instanceof Headers) {
|
|
4211
|
+
if (options == null ? void 0 : options.headers.has("Authorization")) {
|
|
4212
|
+
throw new Error(UNEXPECTED_AUTHORIZATION_HEADER_MESSAGE);
|
|
4213
|
+
}
|
|
4214
|
+
options == null ? void 0 : options.headers.set("Authorization", Authorization);
|
|
4215
|
+
} else {
|
|
4216
|
+
if ((options == null ? void 0 : options.headers) && Reflect.has(options == null ? void 0 : options.headers, "Authorization")) {
|
|
4217
|
+
throw new Error(UNEXPECTED_AUTHORIZATION_HEADER_MESSAGE);
|
|
4218
|
+
}
|
|
4219
|
+
if (!hasAuthorizationHeaderBeenSet) {
|
|
4220
|
+
options.headers = {
|
|
4221
|
+
...options == null ? void 0 : options.headers,
|
|
4222
|
+
Authorization
|
|
4223
|
+
};
|
|
4224
|
+
}
|
|
4225
|
+
}
|
|
4226
|
+
return [resource, options];
|
|
4227
|
+
}
|
|
4228
|
+
function buildJwtRequestHeaderInterceptor(jwtManager, jwtRequestModifier = (_e, fetchArgs) => fetchArgs) {
|
|
4229
|
+
return (args) => {
|
|
4230
|
+
return resolvedPromiseLike$3(jwtManager.getJwt()).then((token) => {
|
|
4231
|
+
const fetchArgsWithRequestHeaderAuthorization = setHeaderAuthorization(token, args);
|
|
4232
|
+
return token.extraInfo ? jwtRequestModifier(token.extraInfo, fetchArgsWithRequestHeaderAuthorization) : fetchArgsWithRequestHeaderAuthorization;
|
|
4233
|
+
});
|
|
4234
|
+
};
|
|
4235
|
+
}
|
|
4236
|
+
|
|
4237
|
+
/**
|
|
4238
|
+
* Resolves Jwt token for SFAP by calling Aura action
|
|
4239
|
+
* {@link JwtResolver} for platform SFAP
|
|
4240
|
+
*/
|
|
4241
|
+
const platformSfapJwtResolver = {
|
|
4242
|
+
getJwt() {
|
|
4243
|
+
return new Promise((resolve) => {
|
|
4244
|
+
getSfapJwt().then((response) => {
|
|
4245
|
+
resolve({
|
|
4246
|
+
jwt: response.jwt,
|
|
4247
|
+
extraInfo: {
|
|
4248
|
+
baseUri: response.baseUri,
|
|
4249
|
+
},
|
|
4250
|
+
});
|
|
4251
|
+
});
|
|
4252
|
+
});
|
|
4253
|
+
},
|
|
4254
|
+
};
|
|
4255
|
+
|
|
4256
|
+
const METHODS_WITH_CSRF = ['POST', 'PATCH', 'PUT', 'DELETE'];
|
|
4257
|
+
/**
|
|
4258
|
+
* Webruntime request interceptor that modifies outgoing HTTP requests with standard
|
|
4259
|
+
* query parameters and security headers. This ensures the api calls for onestore adapters
|
|
4260
|
+
* follow the same pattern as the equivalent luvio api calls for all CLWR endpoints.
|
|
4261
|
+
*
|
|
4262
|
+
* This interceptor:
|
|
4263
|
+
* - Adds language query parameter from i18n settings
|
|
4264
|
+
* - Adds asGuest parameter based on design/preview mode and SID cookie presence
|
|
4265
|
+
* - Adds htmlEncode=false query parameter (since this is an api call by definition)
|
|
4266
|
+
* - For POST/PATCH/PUT/DELETE requests, adds CSRF token to headers
|
|
4267
|
+
*
|
|
4268
|
+
* @param {FetchParameters} args - The fetch parameters array containing [url, requestInit]
|
|
4269
|
+
* @returns Promise-like resolved with the modified fetch parameters
|
|
4270
|
+
*
|
|
4271
|
+
* @example
|
|
4272
|
+
* ```typescript
|
|
4273
|
+
* const modifiedArgs = await webruntimeRequestInterceptor(['/api/data', { method: 'GET' }]);
|
|
4274
|
+
* // URL will have ?language=en_US&asGuest=false&htmlEncode=false appended
|
|
4275
|
+
* ```
|
|
4276
|
+
*/
|
|
4277
|
+
async function webruntimeRequestInterceptor(args) {
|
|
4278
|
+
const [url, requestInit] = args;
|
|
4279
|
+
if (!(typeof url === 'string' || url instanceof URL)) {
|
|
4280
|
+
return resolvedPromiseLike$3(args);
|
|
4281
|
+
}
|
|
4282
|
+
const urlObj = buildApiBaseUrl(url, apiBasePath);
|
|
4283
|
+
urlObj.searchParams.append('language', language);
|
|
4284
|
+
urlObj.searchParams.append('asGuest', getAsGuestParamValue().toString());
|
|
4285
|
+
urlObj.searchParams.append('htmlEncode', 'false');
|
|
4286
|
+
// Update the args array with the modified URL string
|
|
4287
|
+
args[0] = urlObj;
|
|
4288
|
+
if (requestInit?.method && METHODS_WITH_CSRF.includes(requestInit.method)) {
|
|
4289
|
+
await addCSRFToken(requestInit);
|
|
4290
|
+
}
|
|
4291
|
+
return resolvedPromiseLike$3(args);
|
|
4292
|
+
}
|
|
4293
|
+
function buildApiBaseUrl(url, apiBasePath) {
|
|
4294
|
+
// The URL constructor's second argument provides a base if the first argument is a relative URL.
|
|
4295
|
+
const urlObj = new URL(url, location?.href);
|
|
4296
|
+
// If a base path is provided, ensure the URL starts with it, otherwiswe prepend it to the path.
|
|
4297
|
+
if (apiBasePath && !urlObj.pathname.startsWith(apiBasePath)) {
|
|
4298
|
+
urlObj.pathname = apiBasePath + urlObj.pathname;
|
|
4299
|
+
}
|
|
4300
|
+
return urlObj;
|
|
4301
|
+
}
|
|
4302
|
+
function hasSidCookie() {
|
|
4303
|
+
const cookies = document.cookie.split(';');
|
|
4304
|
+
return cookies.some((cookie) => cookie.trim().startsWith(`${authenticationCookieName}=`));
|
|
4305
|
+
}
|
|
4306
|
+
function getAsGuestParamValue() {
|
|
4307
|
+
// It's a guest user if not in design/preview mode AND the '__Secure-has-sid' cookie is missing.
|
|
4308
|
+
// Note: `authenticationCookieName` (__Secure-has-sid) is *NOT* set in design/preview mode.
|
|
4309
|
+
return !(isDesignMode || isPreviewMode) && !hasSidCookie();
|
|
4310
|
+
}
|
|
4311
|
+
async function addCSRFToken(params) {
|
|
4312
|
+
const { csrfToken } = await getUser();
|
|
4313
|
+
if (!params.headers) {
|
|
4314
|
+
params.headers = {};
|
|
4315
|
+
}
|
|
4316
|
+
if (csrfToken) {
|
|
4317
|
+
params.headers['CSRF-Token'] = csrfToken;
|
|
4318
|
+
}
|
|
4319
|
+
}
|
|
4320
|
+
async function getUser() {
|
|
4321
|
+
if (typeof window === 'undefined') {
|
|
4322
|
+
// always make API calls as guest on the server
|
|
4323
|
+
return { isGuest: true, id: null, csrfToken: null };
|
|
4324
|
+
}
|
|
4325
|
+
const { default: user } = await import('@app/user');
|
|
4326
|
+
return user;
|
|
4327
|
+
}
|
|
4328
|
+
|
|
4329
|
+
const SFAP_BASE_URL = 'api.salesforce.com';
|
|
4330
|
+
const sfapJwtRepository = new JwtRepository();
|
|
4331
|
+
const sfapJwtManager = new JwtManager(sfapJwtRepository, platformSfapJwtResolver);
|
|
4332
|
+
function buildJwtAuthorizedSfapFetchServiceDescriptor(logger) {
|
|
4333
|
+
const jwtAuthorizedFetchService = buildServiceDescriptor({
|
|
4334
|
+
request: [buildJwtRequestInterceptor(logger)],
|
|
4335
|
+
});
|
|
4336
|
+
return {
|
|
4337
|
+
...jwtAuthorizedFetchService,
|
|
4338
|
+
tags: { authenticationScopes: 'sfap_api' },
|
|
4339
|
+
};
|
|
4340
|
+
}
|
|
4341
|
+
/**
|
|
4342
|
+
* Returns a service descriptor for a fetch service that includes one-off copilot
|
|
4343
|
+
* hacks. This fetch service is not intended for use by anything other than
|
|
4344
|
+
* copilot commands.
|
|
4345
|
+
*/
|
|
4346
|
+
function buildCopilotFetchServiceDescriptor(logger) {
|
|
4347
|
+
return {
|
|
4348
|
+
// Note that this layers the Interceptor below directly on top of fetch(). WHen
|
|
4349
|
+
// we switch to JWT authentication this will need to change to incorporate the
|
|
4350
|
+
// Interceptor here with the logic in buildJwtAuthorizedSfapFetchServiceDescriptor()
|
|
4351
|
+
// above.
|
|
4352
|
+
...buildServiceDescriptor({
|
|
4353
|
+
request: [
|
|
4354
|
+
// Note that this function is VERY closely tied to the fetchParams generated
|
|
4355
|
+
// by copilotStartSessionCommand. Any changes to those parameters will require
|
|
4356
|
+
// corresponding updates to the logic below.
|
|
4357
|
+
(args) => {
|
|
4358
|
+
const [url, requestInit] = args;
|
|
4359
|
+
// ignore anything other than a start session request
|
|
4360
|
+
if (typeof url !== 'string' ||
|
|
4361
|
+
!url.endsWith('/sessions') ||
|
|
4362
|
+
!requestInit ||
|
|
4363
|
+
requestInit.method !== 'POST' ||
|
|
4364
|
+
!requestInit.body ||
|
|
4365
|
+
typeof requestInit.body !== 'string') {
|
|
4366
|
+
return resolvedPromiseLike$3(args);
|
|
4367
|
+
}
|
|
4368
|
+
return resolvedPromiseLike$3(sfapJwtManager.getJwt()).then((token) => {
|
|
4369
|
+
// replace the body's instanceConfig.endpoint with the JWT's iss value
|
|
4370
|
+
const body = JSON.parse(requestInit.body);
|
|
4371
|
+
if (!body || !token.decodedInfo || !token.decodedInfo.iss) {
|
|
4372
|
+
logger.warn('skipping injection of endpoint into start session request');
|
|
4373
|
+
}
|
|
4374
|
+
else {
|
|
4375
|
+
if (!body.instanceConfig) {
|
|
4376
|
+
body.instanceConfig = {};
|
|
4377
|
+
}
|
|
4378
|
+
body.instanceConfig.endpoint = token.decodedInfo.iss;
|
|
4379
|
+
}
|
|
4380
|
+
return [
|
|
4381
|
+
args[0],
|
|
4382
|
+
{
|
|
4383
|
+
...args[1],
|
|
4384
|
+
body: JSON.stringify(body),
|
|
4385
|
+
},
|
|
4386
|
+
];
|
|
4387
|
+
});
|
|
4388
|
+
},
|
|
4389
|
+
buildJwtRequestInterceptor(logger),
|
|
4390
|
+
],
|
|
4391
|
+
}),
|
|
4392
|
+
tags: { specialHacksFor: 'copilot' },
|
|
4393
|
+
};
|
|
4394
|
+
}
|
|
4395
|
+
function buildDefaultFetchServiceDescriptor() {
|
|
4396
|
+
const fetchService = buildServiceDescriptor({ request: [webruntimeRequestInterceptor] });
|
|
4397
|
+
return {
|
|
4398
|
+
...fetchService,
|
|
4399
|
+
tags: { authenticationScopes: '' },
|
|
4400
|
+
};
|
|
4401
|
+
}
|
|
4402
|
+
function buildJwtRequestInterceptor(logger) {
|
|
4403
|
+
const jwtRequestModifier = ({ baseUri }, [resource, request]) => {
|
|
4404
|
+
if (typeof resource !== 'string' && !(resource instanceof URL)) {
|
|
4405
|
+
// istanbul ignore else: this will not be tested in NODE_ENV = production for test coverage
|
|
4406
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
4407
|
+
throw new Error('SFAP fetch service expects a string or URL resource');
|
|
4408
|
+
}
|
|
4409
|
+
return [resource, request];
|
|
4410
|
+
}
|
|
4411
|
+
const overrideUrl = new URL(baseUri);
|
|
4412
|
+
const url = typeof resource === 'string' ? new URL(resource) : new URL(resource.toString());
|
|
4413
|
+
if (!(url.host === SFAP_BASE_URL)) {
|
|
4414
|
+
logger.warn(`SFAP fetch service requires that the host of the resource is ${SFAP_BASE_URL}`);
|
|
4415
|
+
return [resource, request];
|
|
4416
|
+
}
|
|
4417
|
+
url.host = overrideUrl.host;
|
|
4418
|
+
url.protocol = overrideUrl.protocol;
|
|
4419
|
+
return [url, request];
|
|
4420
|
+
};
|
|
4421
|
+
const jwtRequestHeaderInterceptor = buildJwtRequestHeaderInterceptor(sfapJwtManager, jwtRequestModifier);
|
|
4422
|
+
return jwtRequestHeaderInterceptor;
|
|
4423
|
+
}
|
|
4424
|
+
|
|
4425
|
+
const loggerService = new ConsoleLogger$1('ERROR');
|
|
4426
|
+
const cacheServiceDescriptor = buildServiceDescriptor$9();
|
|
4427
|
+
const instrumentationServiceDescriptor = buildServiceDescriptor$a(loggerService);
|
|
4428
|
+
const inMemoryCacheInclusionPolicyServiceDescriptor = buildInMemoryCacheInclusionPolicyService(cacheServiceDescriptor.service);
|
|
4429
|
+
const featureFlagsServiceDescriptor = buildServiceDescriptor$6();
|
|
4430
|
+
const featureFlagsService = featureFlagsServiceDescriptor.service;
|
|
4431
|
+
// This disables the OneStore GraphQL in this runtime,
|
|
4432
|
+
// we will enable this in the future, but while we do rollout, we disable it here
|
|
4433
|
+
featureFlagsService.set('useOneStoreGraphQL', false);
|
|
4434
|
+
const services = [
|
|
4435
|
+
instrumentationServiceDescriptor,
|
|
4436
|
+
buildDefaultFetchServiceDescriptor(),
|
|
4437
|
+
buildJwtAuthorizedSfapFetchServiceDescriptor(loggerService),
|
|
4438
|
+
buildCopilotFetchServiceDescriptor(loggerService),
|
|
4439
|
+
buildServiceDescriptor$b(instrumentationServiceDescriptor.service),
|
|
4440
|
+
buildServiceDescriptor$8(cacheServiceDescriptor.service, inMemoryCacheInclusionPolicyServiceDescriptor.service),
|
|
4441
|
+
buildServiceDescriptor$i(),
|
|
4442
|
+
buildServiceDescriptor$3(),
|
|
4443
|
+
buildServiceDescriptor$e(),
|
|
4444
|
+
buildServiceDescriptor$j(),
|
|
4445
|
+
buildServiceDescriptor$d(),
|
|
4446
|
+
buildServiceDescriptor$c(),
|
|
4447
|
+
buildServiceDescriptor$h(),
|
|
4448
|
+
buildServiceDescriptor$g(),
|
|
4449
|
+
buildServiceDescriptor$f(),
|
|
4450
|
+
buildServiceDescriptor$7(),
|
|
4451
|
+
buildServiceDescriptor$5(),
|
|
4452
|
+
buildServiceDescriptor$4(),
|
|
4453
|
+
featureFlagsServiceDescriptor,
|
|
4454
|
+
// Luvio service won't be used since we set the useOneStoreGraphQL flag to false
|
|
4455
|
+
buildServiceDescriptor$2({}),
|
|
4456
|
+
buildServiceDescriptor$1({}, {}),
|
|
4457
|
+
];
|
|
4458
|
+
setServices(services);
|
|
4459
|
+
// version: 0.1.0-dev1-54c03dd38c
|