-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.js
763 lines (629 loc) · 21.7 KB
/
lib.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
'use strict';
const VALID_HTTP_METHOD_LIST = [
'DELETE',
'GET',
'HEAD',
'OPTIONS',
'PATCH',
'POST',
'PUT',
];
// ref: https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
const HTTP_STATUS_CODE_DESCRIPTION = {
200: 'OK',
201: 'Created',
202: 'Accepted',
203: 'Non-Authoritative Information',
204: 'No Content',
205: 'Reset Content',
206: 'Partial Content',
300: 'Multiple Choices',
301: 'Moved Permanently',
302: 'Found',
303: 'See Other',
304: 'Not Modified',
305: 'Use Proxy',
// 306: reserved/unused
307: 'Temporary Redirect',
400: 'Bad Request',
401: 'Unauthorized',
402: 'Payment Required',
403: 'Forbidden',
404: 'Not Found',
405: 'Method Not Allowed',
406: 'Not Acceptable',
407: 'Proxy Authentication Required',
408: 'Request Timeout',
409: 'Conflict',
410: 'Gone',
411: 'Length Required',
412: 'Precondition Failed',
413: 'Request Entity Too Large',
414: 'Request-URI Too Long',
415: 'Unsupported Media Type',
416: 'Requested Range Not Satisfiable',
417: 'Expectation Failed',
500: 'Internal Server Error',
501: 'Not Implemented',
502: 'Bad Gateway',
503: 'Service Unavailable',
504: 'Gateway Timeout',
505: 'HTTP Version Not Supported',
};
const VALID_SSL_PROTOCOL_LIST = [
'SSLv3',
'TLSv1',
'TLSv1.1',
'TLSv1.2',
];
class EdgeEventBase {
// properties: config
setDistributionDomainName(name) {
cfEventData(this._event).config.distributionDomainName = name;
return this;
}
setDistributionId(id) {
cfEventData(this._event).config.distributionId = id;
return this;
}
setRequestId(id) {
cfEventData(this._event).config.requestId = id;
return this;
}
// properties: request
setClientIp(ipAddr) {
cfEventData(this._event).request.clientIp = ipAddr;
return this;
}
setRequestHttpHeader(key,value) {
setEdgeEventHttpHeaderKeyValue(
cfEventData(this._event).request.headers,
key,value
);
return this;
}
addRequestHttpHeader(key,value) {
addEdgeEventHttpHeaderKeyValue(
cfEventData(this._event).request.headers,
key,value
);
return this;
}
setHttpMethod(method) {
if (!VALID_HTTP_METHOD_LIST.includes(method)) {
throw new Error(`unexpected HTTP method of [${method}]`);
}
cfEventData(this._event).request.method = method;
return this;
}
setQuerystring(qs) {
// strip any leading question mark(s) and whitespace
qs = qs.trim().replace(/^[? ]+/,'');
cfEventData(this._event).request.querystring = qs;
return this;
}
setUri(uri) {
// `uri` must start with a single forward slash
uri = uri.trim().replace(/^[/ ]+/,'');
cfEventData(this._event).request.uri = `/${uri}`;
return this;
}
// Lambda@Edge function executor
async execute(handler) {
// create copy of CloudFront Lambda@Edge event and execute Lambda@Edge handler
const event = cfEventClone(this._event);
// execute handler and validate returned/mutated payload
const payload = await executeHandler(handler,event);
this._payloadVerify(payload);
return payload;
}
}
class EdgeEventRequestBase extends EdgeEventBase {
constructor(eventType,hasOrigin = false) {
super();
this._event = buildEventBase(eventType,hasOrigin,false);
}
// properties: request
setBody(data,isTruncated = false) {
// note: `data` will be base64 encoded into the `cf.request.body.data` property
cfEventData(this._event).request.body = {
action: 'read-only',
data: Buffer.from(data || '').toString('base64'),
encoding: 'base64',
inputTruncated: !!isTruncated,
};
return this;
}
}
class EdgeEventResponseBase extends EdgeEventBase {
constructor(eventType,hasOrigin = false) {
super();
this._event = buildEventBase(eventType,hasOrigin,true);
}
// properties: response
setResponseHttpHeader(key,value) {
setEdgeEventHttpHeaderKeyValue(
cfEventData(this._event).response.headers,
key,value
);
return this;
}
addResponseHttpHeader(key,value) {
addEdgeEventHttpHeaderKeyValue(
cfEventData(this._event).response.headers,
key,value
);
return this;
}
setResponseHttpStatusCode(code) {
setEdgeEventResponseHttpStatusCode(this._event,code);
return this;
}
}
function buildEventBase(eventType,hasOrigin,hasResponse) {
// common payload properties
const event = {
Records: [{
cf: {
config: {
distributionDomainName: undefined,
distributionId: undefined,
eventType: eventType,
requestId: undefined,
},
request: {
// note: skipping `body` property - will be handled/added via `EdgeEventRequestBase.setBody()`
clientIp: '127.0.0.1',
headers: {},
method: 'GET',
querystring: '',
uri: '/',
},
},
}],
};
if (hasOrigin) {
// additional origin (`origin-request` / `origin-response`) payload properties
cfEventData(event).request.origin = {};
}
if (hasResponse) {
// additional response payload properties
cfEventData(event).response = { headers: {} };
setEdgeEventResponseHttpStatusCode(event,200); // default to HTTP 200
}
return event;
}
function setEdgeEventOriginCustom(event,domainName,path) {
// verify custom origin path
path = (path || '');
if (!isValidOriginPath(path)) {
throw new Error(`custom origin path must be empty or begin, but not end with forward slash - got [${path}]`);
}
if (path.length > 255) {
throw new Error(`custom origin path length must not exceed 255 characters - got [${path}]`);
}
cfEventData(event).request.origin = {
custom: {
customHeaders: {},
domainName: domainName,
keepaliveTimeout: 1,
path: path,
port: 443,
protocol: 'https',
readTimeout: 4,
sslProtocols: [],
},
};
}
function setEdgeEventOriginKeepaliveTimeout(event,timeout) {
verifyEdgeEventOriginModeCustom(event);
cfEventData(event).request.origin.custom.keepaliveTimeout = intOrZero(timeout);
}
function setEdgeEventOriginPort(event,port) {
verifyEdgeEventOriginModeCustom(event);
cfEventData(event).request.origin.custom.port = intOrZero(port);
}
function setEdgeEventOriginHttps(event,isHttps) {
verifyEdgeEventOriginModeCustom(event);
cfEventData(event).request.origin.custom.protocol = (isHttps) ? 'https' : 'http';
}
function setEdgeEventOriginReadTimeout(event,timeout) {
verifyEdgeEventOriginModeCustom(event);
cfEventData(event).request.origin.custom.readTimeout = intOrZero(timeout);
}
function setEdgeEventOriginSslProtocolList(event,protocolList) {
verifyEdgeEventOriginModeCustom(event);
if (!Array.isArray(protocolList)) {
throw new Error('protocol list must be an array');
}
const resultList = [];
for (const item of VALID_SSL_PROTOCOL_LIST) {
if (protocolList.includes(item)) resultList.push(item);
}
cfEventData(event).request.origin.custom.sslProtocols = resultList;
}
function setEdgeEventOriginS3(event,domainName,region,path) {
// verify S3 origin path
path = (path || '');
if (!isValidOriginPath(path)) {
throw new Error(`S3 origin path must be empty or begin, but not end with forward slash - got [${path}]`);
}
cfEventData(event).request.origin = {
s3: {
authMethod: 'none',
customHeaders: {},
domainName: domainName,
path: path,
region: (region || ''),
},
};
}
function setEdgeEventOriginOAI(event,isOAI) {
verifyEdgeEventOriginModeS3(event);
cfEventData(event).request.origin.s3.authMethod = (isOAI) ? 'origin-access-identity' : 'none';
}
// (set|add)EdgeEventOriginHttpHeader() are the only origin methods shared by custom/S3 modes
function setEdgeEventOriginHttpHeader(event,key,value) {
const origin = cfEventData(event).request.origin;
if (origin.hasOwnProperty('custom')) {
setEdgeEventHttpHeaderKeyValue(origin.custom.customHeaders,key,value);
return;
}
if (origin.hasOwnProperty('s3')) {
setEdgeEventHttpHeaderKeyValue(origin.s3.customHeaders,key,value);
return;
}
throw new Error('an origin mode must be set via one of [setOriginCustom()/setOriginS3()]');
}
function addEdgeEventOriginHttpHeader(event,key,value) {
const origin = cfEventData(event).request.origin;
if (origin.hasOwnProperty('custom')) {
addEdgeEventHttpHeaderKeyValue(origin.custom.customHeaders,key,value);
return;
}
if (origin.hasOwnProperty('s3')) {
addEdgeEventHttpHeaderKeyValue(origin.s3.customHeaders,key,value);
return;
}
throw new Error('an origin mode must be set via one of [setOriginCustom()/setOriginS3()]');
}
function verifyEdgeEventOriginModeCustom(event) {
const origin = cfEventData(event).request.origin;
if ((origin === undefined) || !origin.hasOwnProperty('custom')) {
throw new Error('method only valid in custom origin [setOriginCustom()] mode');
}
}
function verifyEdgeEventOriginModeS3(event) {
const origin = cfEventData(event).request.origin;
if ((origin === undefined) || !origin.hasOwnProperty('s3')) {
throw new Error('method only valid in S3 origin [setOriginS3()] mode');
}
}
function setEdgeEventResponseHttpStatusCode(event,httpCode) {
const response = cfEventData(event).response;
response.status = '' + httpCode; // as string
response.statusDescription = HTTP_STATUS_CODE_DESCRIPTION[httpCode] || '';
}
function setEdgeEventHttpHeaderKeyValue(headerCollection,key,value) {
// trim whitespace/lowercase key
key = key.trim();
const keyLower = key.toLowerCase();
// if value not given, remove header entirely
if (value === undefined) {
delete headerCollection[keyLower];
return;
}
// (re)set HTTP header in collection
headerCollection[keyLower] = [{
key: key,
value: value.trim(),
}];
}
function addEdgeEventHttpHeaderKeyValue(headerCollection,key,value) {
// trim whitespace/lowercase key
key = key.trim();
const keyLower = key.toLowerCase();
// add HTTP header to collection
headerCollection[keyLower] = headerCollection[keyLower] ?? [];
headerCollection[keyLower].push({
key: key,
value: (value || '').trim(),
});
}
async function executeHandler(handler,event) {
const argLength = handler.length;
// execute Lambda@Edge handler based on type
// see: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html
if (handler.constructor.name === 'AsyncFunction') {
if (argLength < 1 || argLength > 2) {
throw new Error('unexpected async handler argument count - expecting either one or two arguments');
}
return await handler(event,{});
}
// callback handler
if (argLength !== 3) {
throw new Error('unexpected callback handler argument count - expecting exactly three arguments');
}
return new Promise(function(resolve,reject) {
handler(event,{},function(err,payload) {
if (err) {
// return error from Lambda@Edge function callback
return reject(err);
}
resolve(payload);
});
});
}
function payloadVerifyRequest(payload) {
// payload must be an object
if (typeof payload !== 'object') {
throw new Error('expected payload to be of type object');
}
// confirm expected properties exist
payloadPropertyExistsString(payload,'clientIp');
payloadVerifyPropertyHeaders(payload,'headers');
payloadPropertyExistsString(payload,'method');
payloadPropertyExistsString(payload,'querystring');
payloadPropertyExistsString(payload,'uri');
// ensure `payload.method` is a valid HTTP method
if (!VALID_HTTP_METHOD_LIST.includes(payload.method)) {
throw new Error(`unexpected payload HTTP [method] of [${payload.method}]`);
}
// ensure `payload.uri` begins with forward slash
if (payload.uri[0] !== '/') {
throw new Error(`payload value [uri] must begin with forward slash - got [${payload.uri}]`);
}
// if `payload.body` exists - validate properties within
if (payload.hasOwnProperty('body')) {
payloadPropertyExistsObject(payload,'body');
const body = payload.body;
// confirm expected `body` properties exist
payloadPropertyExistsString(body,'action','body');
payloadPropertyExistsString(body,'data','body');
payloadPropertyExistsString(body,'encoding','body');
payloadPropertyExists(body,'inputTruncated','body');
// verify `body.action` and `body.encoding` properties have allowed values
if (!['read-only','replace'].includes(body.action)) {
throw new Error(`payload value [body.action] must be 'read-only' or 'replace' - got [${body.action}]`);
}
if (!['base64','text'].includes(body.encoding)) {
throw new Error(`payload value [body.encoding] must be 'base64' or 'text' - got [${body.encoding}]`);
}
}
}
function payloadVerifyRequestOrigin(payload) {
payloadPropertyExistsObject(payload,'origin');
const origin = payload.origin;
// origin must contain a property of either `custom` or `s3` - but never both
if (origin.hasOwnProperty('custom') && origin.hasOwnProperty('s3')) {
throw new Error('payload property [origin] to contain child property of [custom] or [s3] - never both');
}
if (!origin.hasOwnProperty('custom') && !origin.hasOwnProperty('s3')) {
throw new Error('payload property [origin] to contain child property of [custom] or [s3]');
}
if (origin.hasOwnProperty('custom')) {
payloadPropertyExistsObject(origin,'custom','origin');
const custom = origin.custom;
// confirm expected properties exist
payloadVerifyPropertyHeaders(custom,'customHeaders','origin.custom');
payloadPropertyExistsString(custom,'domainName','origin.custom');
payloadPropertyExistsNumber(custom,'keepaliveTimeout','origin.custom');
payloadPropertyExistsString(custom,'path','origin.custom');
payloadPropertyExistsNumber(custom,'port','origin.custom');
payloadPropertyExistsString(custom,'protocol','origin.custom');
payloadPropertyExistsNumber(custom,'readTimeout','origin.custom');
payloadPropertyExists(custom,'sslProtocols','origin.custom');
// ensure `origin.custom.domainName` is non-empty
if (custom.domainName.trim() === '') {
throw new Error('payload property [origin.custom.domainName] must be non-empty');
}
// ensure `origin.custom.keepaliveTimeout` is within bounds
if ((custom.keepaliveTimeout < 1) || (custom.keepaliveTimeout > 60)) {
throw new Error(`payload property [origin.custom.keepaliveTimeout] must be between 1-60 seconds - got [${custom.keepaliveTimeout}]`);
}
// ensure `origin.custom.path` is valid
if (!isValidOriginPath(custom.path)) {
throw new Error(`payload property [origin.custom.path] must be empty or begin, but not end with forward slash - got [${custom.path}]`);
}
if (custom.path.length > 255) {
throw new Error(`payload property [origin.custom.path] length must not exceed 255 characters - got [${custom.path}]`);
}
// ensure `origin.custom.port` is within bounds
if (
(custom.port !== 80) &&
(custom.port !== 443) &&
((custom.port < 1024) || (custom.port > 65535))
) {
throw new Error(`payload property [origin.custom.port] must be a value of 80,443 or between 1024-65535 - got [${custom.port}]`);
}
// verify `origin.custom.protocol` is one of 'http' or 'https'
if (!['http','https'].includes(custom.protocol)) {
throw new Error(`payload value [origin.custom.protocol] must be 'http' or 'https' - got [${custom.protocol}]`);
}
// ensure `origin.custom.readTimeout` is within bounds
if ((custom.readTimeout < 4) || (custom.readTimeout > 60)) {
throw new Error(`payload property [origin.custom.readTimeout] must be between 4-60 seconds - got [${custom.readTimeout}]`);
}
// ensure `origin.custom.sslProtocols` is an array and contains valid protocols
if (!Array.isArray(custom.sslProtocols)) {
throw new Error('payload property [origin.custom.sslProtocols] must be an array');
}
for (const item of custom.sslProtocols) {
if (!VALID_SSL_PROTOCOL_LIST.includes(item)) {
throw new Error(`payload property [origin.custom.sslProtocols] contains an invalid protocol of [${item}]`);
}
}
} else if (origin.hasOwnProperty('s3')) {
payloadPropertyExistsObject(origin,'s3','origin');
const s3 = origin.s3;
// confirm expected properties exist
payloadPropertyExistsString(s3,'authMethod','origin.s3');
payloadVerifyPropertyHeaders(s3,'customHeaders','origin.s3');
payloadPropertyExistsString(s3,'domainName','origin.s3');
payloadPropertyExistsString(s3,'path','origin.s3');
payloadPropertyExistsString(s3,'region','origin.s3');
// verify `origin.s3.authMethod` is one of 'origin-access-identity' or 'none'
if (!['origin-access-identity','none'].includes(s3.authMethod)) {
throw new Error(`payload value [origin.s3.authMethod] must be 'origin-access-identity' or 'none' - got [${s3.authMethod}]`);
}
// ensure `origin.s3.domainName` is non-empty
if (s3.domainName.trim() === '') {
throw new Error('payload property [origin.s3.domainName] must be non-empty');
}
// ensure `origin.s3.path` is valid
if (!isValidOriginPath(s3.path)) {
throw new Error(`payload property [origin.s3.path] must be empty or begin, but not end with forward slash - got [${s3.path}]`);
}
}
}
function payloadVerifyResponse(payload) {
// payload must be an object
if (typeof payload !== 'object') {
throw new Error('expected payload to be of type object');
}
// confirm expected properties exist
payloadVerifyPropertyHeaders(payload,'headers');
payloadPropertyExistsString(payload,'status');
payloadPropertyExistsString(payload,'statusDescription');
// ensure `payload.status` is a valid/known HTTP status code
if (!HTTP_STATUS_CODE_DESCRIPTION.hasOwnProperty(payload.status)) {
throw new Error(`payload value [status] is an unknown HTTP status code - got [${payload.status}]`);
}
}
function payloadVerifyPropertyHeaders(payload,property,prefix) {
payloadPropertyExistsObject(payload,property,prefix);
const headerSet = payload[property];
for (const headerKey of Object.keys(headerSet)) {
// ensure header key is lowercase
if (headerKey !== headerKey.toLowerCase()) {
throw new Error(`payload [${payloadPropertyDisplay(property,prefix)}] keys must all be lowercase - found [${headerKey}]`);
}
// header list must be an array
const headerList = headerSet[headerKey];
if (!Array.isArray(headerList)) {
throw new Error(`expected payload [${payloadPropertyDisplay(property,prefix)}] property [${headerKey}] to be of type array`);
}
for (const item of headerList) {
// each header item in list must be of object type
if (typeof item !== 'object') {
throw new Error(`expected payload [${payloadPropertyDisplay(property,prefix)}] property [${headerKey}] items to be of type object`);
}
// if object has optional `key` property - must complement that of parent property name when lowercased
if (item.hasOwnProperty('key')) {
if (typeof item.key !== 'string') {
throw new Error(`expected payload [${payloadPropertyDisplay(property,prefix)}] property [${headerKey}[].key] to be of type string`);
}
if (headerKey !== item.key.toLowerCase()) {
throw new Error(`expected payload [${payloadPropertyDisplay(property,prefix)}] property [${headerKey}[].key] of [${item.key}] to match lowercased [${headerKey}] parent`);
}
}
// object must have a `value` property of type string
if (!item.hasOwnProperty('value')) {
throw new Error(`expected payload [${payloadPropertyDisplay(property,prefix)}] property [${headerKey}[].value] not found`);
}
if (typeof item.value !== 'string') {
throw new Error(`expected payload [${payloadPropertyDisplay(property,prefix)}] property [${headerKey}[].value] to be of type string`);
}
}
}
}
function payloadPropertyExists(payload,property,prefix) {
if (payload.hasOwnProperty(property)) {
return;
}
throw new Error(`payload property [${payloadPropertyDisplay(property,prefix)}] not found`);
}
function payloadPropertyExistsObject(payload,property,prefix) {
payloadPropertyExists(payload,property,prefix);
if (typeof payload[property] === 'object') {
return;
}
throw new Error(`payload property [${payloadPropertyDisplay(property,prefix)}] to be of type object`);
}
function payloadPropertyExistsString(payload,property,prefix) {
payloadPropertyExists(payload,property,prefix);
if (typeof payload[property] === 'string') {
return;
}
throw new Error(`payload property [${payloadPropertyDisplay(property,prefix)}] to be of type string`);
}
function payloadPropertyExistsNumber(payload,property,prefix) {
payloadPropertyExists(payload,property,prefix);
if (typeof payload[property] === 'number') {
return;
}
throw new Error(`payload property [${payloadPropertyDisplay(property,prefix)}] to be of type number`);
}
function payloadPropertyDisplay(property,prefix) {
return (prefix) ? `${prefix}.${property}` : property;
}
function isValidOriginPath(path) {
if (path === '') {
return true;
}
if (path === '/') {
return false;
}
// invalid path if not begin with forward slash, or ending with one
if ((path[0] !== '/') || (path.slice(-1) === '/')) {
return false;
}
return true;
}
function cfEventData(event) {
return event.Records[0].cf;
}
// cfEventClone() performs a basic deep copy of a CloudFront Lambda@Edge event (object/array/primitive types)
function cfEventClone(event,seen = new WeakMap()) {
// primitive type?
if (!(event instanceof Object)) {
return event;
}
// property already cloned?
if (seen.get(event)) {
// return prior clone - avoid circular refs
return seen.get(event);
}
if (Array.isArray(event)) {
// array type
const clone = [];
seen.set(event,clone);
for (const value of event) {
clone.push(cfEventClone(value,seen));
}
return clone;
}
// `{}` type
const clone = {};
seen.set(event,clone);
for (const key of Object.keys(event)) {
clone[key] = cfEventClone(event[key],seen);
}
return clone;
}
function intOrZero(value) {
value = parseInt(value,10);
return (isNaN(value)) ? 0 : value;
}
module.exports = {
EdgeEventRequestBase,
EdgeEventResponseBase,
// functions for mutating `event.Records[0].cf.request.origin.[custom|s3]`
setEdgeEventOriginCustom,
setEdgeEventOriginKeepaliveTimeout,
setEdgeEventOriginPort,
setEdgeEventOriginHttps,
setEdgeEventOriginReadTimeout,
setEdgeEventOriginSslProtocolList,
setEdgeEventOriginS3,
setEdgeEventOriginOAI,
setEdgeEventOriginHttpHeader,
addEdgeEventOriginHttpHeader,
// functions for verifying returned Lambda@Edge function payloads
payloadVerifyRequest,
payloadVerifyRequestOrigin,
payloadVerifyResponse,
// functions exported for tests
setEdgeEventResponseHttpStatusCode,
cfEventClone,
};