-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.ts
71 lines (59 loc) · 2.39 KB
/
util.ts
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
/*
* Copyright Fastly, Inc.
* Licensed under the MIT license. See LICENSE file for details.
*/
import { diag } from '@opentelemetry/api';
import { addFetchEventAction, onInit } from '../core/index.js';
import { FastlySDK } from './FastlySDK.js';
let _target!: FastlySDK;
const respondWith_called = Symbol();
onInit(() => {
(_target as any) = null;
// Patch event.respondWith to ensure that if the SDK has been started,
// then the SDK's shutdown method will be called automatically before
// the end of the event's lifetime.
addFetchEventAction(0, event => {
diag.debug('sdk-fastly: running listener');
if(_target == null) {
diag.warn('sdk-fastly: listener called, but sdk-fastly not initialized. Did you call sdk.start()?');
return;
}
// Allow the SDK to respond at the very first opportunity to react
// after the listener starts.
_target.onEventStart(event);
diag.debug('sdk-fastly: patching event.respondWith()');
const origRespondWith = event.respondWith;
event.respondWith = (response) => {
// Only do this patchwork on the first call to event.respondWith().
// event.respondWith() can only be called once on a single event.
if((event as any)[respondWith_called]) {
diag.warn('sdk-fastly: detected multiple calls to respondWith() on a single event');
diag.debug('sdk-fastly: calling previous event.respondWith()');
origRespondWith.call(event, response);
return;
}
(event as any)[respondWith_called] = true;
diag.debug('sdk-fastly: running patched event.respondWith()');
// Create a promise to extend the life of this event.
// Calling resolveExtension later will settle this promise
// and allow the event's lifetime to end.
let resolveExtension: () => void;
let extension = new Promise<void>(resolve => {
resolveExtension = resolve;
});
event.waitUntil(extension);
Promise.resolve(response)
.finally(() => {
diag.debug('sdk-fastly: response settled, shutting down.');
// This ensures that target's shutdown is enqueued
event.waitUntil(_target.shutdown());
resolveExtension!();
});
diag.debug('sdk-fastly: calling previous event.respondWith()');
origRespondWith.call(event, response);
};
});
});
export function setPatchTarget(target: FastlySDK) {
_target = target;
}