-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
188 lines (163 loc) · 4.83 KB
/
index.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
const { Cookie } = require("tough-cookie");
const TriggerBehavior = {
Never: "never",
NoHistory: "no-history",
WhenExpired: "when-expired",
Always: "always",
};
const defaultTriggerBehaviour = TriggerBehavior.Never;
const ResponseCookieTag = {
name: "responseCookie",
displayName: "Response Cookie",
description: "Cookie from response of chained request.",
args: [
{
name: "request",
displayName: "Request",
type: "model",
model: "Request",
},
{
name: "cookieName",
type: "string",
displayName: "Cookie Name",
},
{
name: "triggerBehavior",
displayName: "Trigger Behavior",
help: "Configure when to resend the dependent request",
type: "enum",
defaultValue: defaultTriggerBehaviour,
options: [
{
displayName: "Never",
description: "never resend request",
value: TriggerBehavior.Never,
},
{
displayName: "No History",
description: "resend when no responses present",
value: TriggerBehavior.NoHistory,
},
{
displayName: "When Expired",
description: "resend when existing response has expired",
value: TriggerBehavior.WhenExpired,
},
{
displayName: "Always",
description: "resend request when needed",
value: TriggerBehavior.Always,
},
],
},
{
name: "maxAgeSeconds",
displayName: "Max age (seconds)",
help: "The maximum age of a response to use before it expires",
type: "number",
hide: (args) => {
return (
parseOptions(args.map((a) => a.value)).triggerBehavior !==
TriggerBehavior.WhenExpired
);
},
defaultValue: 60,
},
],
async run(context, ...args) {
const options = parseOptions(args);
if (!options.reqId) {
throw new Error("No request specified");
}
if (!options.cookieName) {
throw new Error("No cookie specified");
}
const request = await context.util.models.request.getById(options.reqId);
if (!request) {
throw new Error(`Could not find request ${reqId}`);
}
const environmentId = context.context.getEnvironmentId();
let response = await context.util.models.response.getLatestForRequestId(
request._id,
environmentId
);
let shouldResend = checkShouldResend(response, options);
if (shouldResend && context.renderPurpose === "send") {
response = (await resendRequest(context, request)) || response;
}
if (!response) {
log("No response found");
throw new Error("No responses for request");
}
if (response.error) {
log("Response error " + response.error);
throw new Error("Failed to send dependent request " + response.error);
}
if (!response.statusCode) {
log("Invalid status code " + response.statusCode);
throw new Error("No successful responses for request");
}
const cookies = getCookies(response);
if (!cookies.length) {
throw new Error("No cookies set for response");
}
const cookie = cookies.filter((c) => c.key == options.cookieName)[0];
if (!cookie) {
const cookieNames = cookies.map((c) => c.key).join(", ");
throw new Error(
`No ${options.cookieName} cookie for response. Choices are ${cookieNames}.`
);
}
return cookie.value;
},
};
module.exports.templateTags = [
ResponseCookieTag,
];
function parseOptions(args) {
return {
reqId: args[0],
cookieName: args[1],
triggerBehavior: (args[2] || defaultTriggerBehaviour).toLowerCase(),
maxAgeSeconds: args[3],
};
}
async function resendRequest(context, request) {
const requestChain = context.context.getExtraInfo("requestChain") || [];
// Make sure we only send the request once per render so we don't have infinite recursion
if (requestChain.some((id) => id === request._id)) {
log("Preventing recursive render");
return;
}
log("Resending dependency");
requestChain.push(request._id);
return await context.network.sendRequest(request, [
{ name: "requestChain", value: requestChain },
]);
}
function getCookies(response) {
return response.headers
.filter((h) => h.name.toLowerCase() == "set-cookie")
.map((h) => Cookie.parse(h.value));
}
function checkShouldResend(response, options) {
switch (options.triggerBehavior) {
case TriggerBehavior.Always:
return true;
case TriggerBehavior.NoHistory:
return !response;
case TriggerBehavior.WhenExpired:
if (!response) {
return true;
} else {
const ageSeconds = (Date.now() - response.created) / 1000;
return ageSeconds > options.maxAgeSeconds;
}
}
return false;
}
const logPrefix = `[${ResponseCookieTag.name} tag]`
function log(msg) {
console.log(`${logPrefix} ${msg}`);
}