@adobe/helix-deploy 11.1.15 → 12.0.0-pre.2
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/index.js +2 -1
- package/package.json +2 -12
- package/params.json +3 -0
- package/src/ActionBuilder.js +20 -19
- package/src/BaseConfig.js +9 -3
- package/src/bundler/BaseBundler.js +6 -16
- package/src/bundler/WebpackBundler.js +12 -1
- package/src/cli.js +59 -13
- package/src/bundler/EdgeBundler.js +0 -132
- package/src/deploy/CloudflareConfig.js +0 -74
- package/src/deploy/CloudflareDeployer.js +0 -178
- package/src/deploy/ComputeAtEdgeConfig.js +0 -91
- package/src/deploy/ComputeAtEdgeDeployer.js +0 -185
- package/src/gateway/FastlyConfig.js +0 -96
- package/src/gateway/FastlyGateway.js +0 -525
- package/src/template/adapter-utils.js +0 -17
- package/src/template/cloudflare-adapter.js +0 -65
- package/src/template/fastly-adapter.js +0 -128
- package/src/template/polyfills/fetch.js +0 -20
- package/src/template/serviceworker-index.js +0 -26
|
@@ -1,525 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2020 Adobe. All rights reserved.
|
|
3
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
-
*
|
|
7
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
-
* governing permissions and limitations under the License.
|
|
11
|
-
*/
|
|
12
|
-
// eslint-disable-next-line import/no-named-default
|
|
13
|
-
import { default as Fastly, loghelpers } from '@adobe/fastly-native-promises';
|
|
14
|
-
import chalk from 'chalk-template';
|
|
15
|
-
import FastlyConfig from './FastlyConfig.js';
|
|
16
|
-
import BaseDeployer from '../deploy/BaseDeployer.js';
|
|
17
|
-
|
|
18
|
-
const {
|
|
19
|
-
toString, vcl, time, req, res, str, concat,
|
|
20
|
-
} = loghelpers;
|
|
21
|
-
|
|
22
|
-
export default class FastlyGateway {
|
|
23
|
-
constructor(baseConfig, config) {
|
|
24
|
-
Object.assign(this, {
|
|
25
|
-
cfg: baseConfig,
|
|
26
|
-
_cfg: config,
|
|
27
|
-
isGateway: true,
|
|
28
|
-
id: 'fastly',
|
|
29
|
-
_fastly: null,
|
|
30
|
-
_deployers: [],
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
ready() {
|
|
35
|
-
return !!this._cfg.service && !!this._cfg.auth;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
canDeploy() {
|
|
39
|
-
return this.ready() && this._deployers.length > 0;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
async updateLinks(links, version) {
|
|
43
|
-
this.log.info(`--: updating links on the Gateway for version ${version}...`);
|
|
44
|
-
const fakeDeployer = new BaseDeployer({
|
|
45
|
-
links, version, log: this.log,
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
const versionstrings = fakeDeployer
|
|
49
|
-
.getLinkVersions()
|
|
50
|
-
.map((versionstring) => `/${this.cfg.packageName}/${this.cfg.name.replace(/@.*/, '')}@${versionstring}`)
|
|
51
|
-
.map((key) => ({
|
|
52
|
-
item_key: key,
|
|
53
|
-
item_value: `@${version}`,
|
|
54
|
-
op: 'upsert',
|
|
55
|
-
}));
|
|
56
|
-
|
|
57
|
-
await this._fastly.bulkUpdateDictItems(undefined, 'aliases', ...versionstrings);
|
|
58
|
-
await this._fastly.discard();
|
|
59
|
-
this.log.info(chalk`{green ok:} updated links on the Gateway for version ${version}.`);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
init() {
|
|
63
|
-
if ((this.ready() || this.updateable) && !this._fastly) {
|
|
64
|
-
this._fastly = Fastly(this._cfg.auth, this._cfg.service);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
withDeployer(value) {
|
|
69
|
-
this._deployers.push(value);
|
|
70
|
-
return this;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
get log() {
|
|
74
|
-
return this.cfg.log;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
async updatePackage() {
|
|
78
|
-
this.log.info('--: updating app (package) parameters on Fastly gateway ...');
|
|
79
|
-
|
|
80
|
-
const packageparams = Object
|
|
81
|
-
.entries(this.cfg.packageParams)
|
|
82
|
-
.map(([key, value]) => ({
|
|
83
|
-
item_key: `${this.cfg.packageName}.${key}`,
|
|
84
|
-
item_value: value,
|
|
85
|
-
op: 'upsert',
|
|
86
|
-
}));
|
|
87
|
-
|
|
88
|
-
if (packageparams.length !== 0) {
|
|
89
|
-
await this._fastly.bulkUpdateDictItems(undefined, 'packageparams', ...packageparams);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
try {
|
|
93
|
-
await this._fastly.updateDictItem(undefined, 'tokens', this.cfg.packageToken, `${Math.floor(Date.now() / 1000) + (365 * 24 * 3600)}`);
|
|
94
|
-
} catch (fe) {
|
|
95
|
-
if (fe.message.match('Exceeding max_dictionary_items')) {
|
|
96
|
-
const dictinfo = await this._fastly.readDictItems(undefined, 'tokens');
|
|
97
|
-
const items = dictinfo.data;
|
|
98
|
-
const outdated = items
|
|
99
|
-
.filter((item) => parseInt(item.item_value, 10) < new Date().getTime() / 1000);
|
|
100
|
-
const olds = items.slice(0, 5);
|
|
101
|
-
// cleanup all old and outdated tokens
|
|
102
|
-
await Promise.all([...outdated, ...olds].map((item) => this._fastly.deleteDictItem(undefined, 'tokens', item.item_key)));
|
|
103
|
-
// try again
|
|
104
|
-
await this._fastly.updateDictItem(undefined, 'tokens', this.cfg.packageToken, `${Math.floor(Date.now() / 1000) + (365 * 24 * 3600)}`);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
this._fastly.discard();
|
|
109
|
-
this.log.info(chalk`{green ok:} updating app (package) parameters on Fastly gateway.`);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
selectBackendVCL() {
|
|
113
|
-
// declare a local variable for each backend
|
|
114
|
-
const init = this._deployers.map((deployer) => `declare local var.${deployer.name.toLowerCase()} INTEGER;`);
|
|
115
|
-
|
|
116
|
-
// get the desired weight for each backend
|
|
117
|
-
const set = this._deployers.map((deployer) => `set var.${deployer.name.toLowerCase()} = std.atoi(table.lookup(priorities, "${deployer.name.toLowerCase()}", "${Math.floor((100 / this._deployers.length))}"));`);
|
|
118
|
-
|
|
119
|
-
// for all but the first, sum up the weights
|
|
120
|
-
const increment = this._deployers
|
|
121
|
-
.slice(1)
|
|
122
|
-
.map((deployer, i) => ([deployer.name, this._deployers[i].name]))
|
|
123
|
-
.map(([current, previous]) => `set var.${current.toLowerCase()} += var.${previous.toLowerCase()};`);
|
|
124
|
-
|
|
125
|
-
const backendvcl = `
|
|
126
|
-
declare local var.i INTEGER;
|
|
127
|
-
set var.i = randomint(0, 100);
|
|
128
|
-
|
|
129
|
-
set req.http.X-Backend-Health = ${this._deployers.map((deployer) => `backend.F_${deployer.name}.healthy`).join(' + " " + ')};
|
|
130
|
-
|
|
131
|
-
if (false) {}`;
|
|
132
|
-
|
|
133
|
-
const middle = this._deployers.map((deployer) => `if((var.i <= var.${deployer.name.toLowerCase()} && backend.F_${deployer.name}.healthy) && subfield(req.http.x-ow-version-lock, "env", "&") !~ ".?" || subfield(req.http.x-ow-version-lock, "env", "&") == "${deployer.name.toLowerCase()}") {
|
|
134
|
-
set req.backend = F_${deployer.name};
|
|
135
|
-
${this._deployers[0].customVCL}
|
|
136
|
-
}`);
|
|
137
|
-
|
|
138
|
-
const fallback = `{
|
|
139
|
-
set req.backend = F_${this._deployers[0].name};
|
|
140
|
-
${this._deployers[0].customVCL}
|
|
141
|
-
}`;
|
|
142
|
-
|
|
143
|
-
return [...init, ...set, ...increment].join('\n') + [backendvcl, ...middle, fallback].join(' else ');
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Generates a VCL snippet (for each package deployed) that lists all package parameter
|
|
148
|
-
* names and looks up their values from the secret edge dictionary.
|
|
149
|
-
* @returns {string} VCL snippet to look up package parameters from edge dict
|
|
150
|
-
*/
|
|
151
|
-
listPackageParamsVCL() {
|
|
152
|
-
const pre = `
|
|
153
|
-
if (obj.status == 600 && req.url.path ~ "^/${this.cfg.packageName}/") {
|
|
154
|
-
set obj.status = 200;
|
|
155
|
-
set obj.response = "OK";
|
|
156
|
-
set obj.http.content-type = "application/json";
|
|
157
|
-
synthetic "{" + `;
|
|
158
|
-
const post = `+ "}";
|
|
159
|
-
return(deliver);
|
|
160
|
-
}`;
|
|
161
|
-
const middle = Object
|
|
162
|
-
.keys(this.cfg.packageParams)
|
|
163
|
-
.map((paramname, index) => `"%22${paramname}%22:%22" json.escape(table.lookup(packageparams, "${this.cfg.packageName}.${paramname}")) "%22${(index + 1) < Object.keys(this.cfg.packageParams).length ? ',' : ''}"`).join(' + ');
|
|
164
|
-
|
|
165
|
-
return pre + middle + post;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
setURLVCL() {
|
|
169
|
-
const pre = `
|
|
170
|
-
declare local var.package STRING;
|
|
171
|
-
declare local var.action STRING;
|
|
172
|
-
declare local var.version STRING;
|
|
173
|
-
declare local var._version STRING;
|
|
174
|
-
declare local var.atversion STRING;
|
|
175
|
-
declare local var.slashversion STRING;
|
|
176
|
-
declare local var.rest STRING;
|
|
177
|
-
declare local var.fullpath STRING;
|
|
178
|
-
|
|
179
|
-
set var.version = "";
|
|
180
|
-
set var.rest = "";
|
|
181
|
-
|
|
182
|
-
if (req.url ~ "^/([^/]+)/([^/@_]+)([@_]([^/@_?]+)+)?(.*$)") {
|
|
183
|
-
log "match";
|
|
184
|
-
set var.package = re.group.1;
|
|
185
|
-
set var.action = re.group.2;
|
|
186
|
-
set var.version = re.group.3;
|
|
187
|
-
|
|
188
|
-
set var.fullpath = "/" + var.package + "/" + var.action + regsub(var.version, "[@_]", "@");
|
|
189
|
-
|
|
190
|
-
set var.version = table.lookup(aliases, var.fullpath, var.version);
|
|
191
|
-
|
|
192
|
-
set var.rest = re.group.5;
|
|
193
|
-
|
|
194
|
-
// normalize version divider
|
|
195
|
-
set var._version = regsub(var.version, "[@_]", "_");
|
|
196
|
-
set var.atversion = regsub(var.version, "[@_]", "@");
|
|
197
|
-
set var.slashversion = regsub(var.version, "[@_]", "/");
|
|
198
|
-
}
|
|
199
|
-
`;
|
|
200
|
-
return pre + this._deployers.map((deployer) => `
|
|
201
|
-
if (req.backend == F_${deployer.name}) {
|
|
202
|
-
set bereq.url = ${deployer.urlVCL};
|
|
203
|
-
}
|
|
204
|
-
`).join('\n');
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
async enableLogging(version) {
|
|
208
|
-
if (this._cfg.coralogixToken) {
|
|
209
|
-
this.log.info(chalk`--: Set up Gateway logging to {yellow Coralogix}`);
|
|
210
|
-
await this._fastly.writeHttps(version, 'helix-coralogix', {
|
|
211
|
-
name: 'helix-coralogix',
|
|
212
|
-
format: toString({
|
|
213
|
-
timestamp: vcl`time.start.msec`,
|
|
214
|
-
subsystemName: str(vcl`req.service_id`),
|
|
215
|
-
severity: concat(
|
|
216
|
-
vcl`if(resp.status<400, "3", "")`,
|
|
217
|
-
vcl`if(resp.status>=400 && resp.status<500, "4", "")`,
|
|
218
|
-
vcl`if(resp.status>=500, "5", "")`,
|
|
219
|
-
),
|
|
220
|
-
json: {
|
|
221
|
-
ow: {
|
|
222
|
-
environment: str(vcl`regsub(req.backend, ".*_", "")`),
|
|
223
|
-
actionName: str(vcl`regsub(req.url, "^/([^/]+)/([^/@_]+)([@_]([^/@_?]+)+)?(.*$)", "\\\\1/\\\\2@\\\\4")`),
|
|
224
|
-
activationId: str(concat(
|
|
225
|
-
vcl`if(resp.http.x-openwhisk-activation-id != "", resp.http.x-openwhisk-activation-id, "")`,
|
|
226
|
-
vcl`if(resp.http.Apigw-Requestid != "", resp.http.Apigw-Requestid, "")`,
|
|
227
|
-
vcl`if(resp.http.Function-Execution-Id != "", resp.http.Function-Execution-Id, "")`,
|
|
228
|
-
)),
|
|
229
|
-
transactionId: str(concat(
|
|
230
|
-
vcl`if(resp.http.x-request-id != "", resp.http.x-request-id, "")`,
|
|
231
|
-
vcl`if(resp.http.x-amazn-trace-id != "", resp.http.x-amazn-trace-id, "")`,
|
|
232
|
-
vcl`if(resp.http.X-Cloud-Trace-Context != "", resp.http.X-Cloud-Trace-Context, "")`,
|
|
233
|
-
)),
|
|
234
|
-
},
|
|
235
|
-
time: {
|
|
236
|
-
start: str(
|
|
237
|
-
concat(
|
|
238
|
-
time`begin:%Y-%m-%dT%H:%M:%S`,
|
|
239
|
-
'.',
|
|
240
|
-
time`begin:msec_frac`,
|
|
241
|
-
time`begin:%z`,
|
|
242
|
-
),
|
|
243
|
-
),
|
|
244
|
-
start_msec: vcl`time.start.msec`,
|
|
245
|
-
end: str(
|
|
246
|
-
concat(
|
|
247
|
-
time`end:%Y-%m-%dT%H:%M:%S`,
|
|
248
|
-
'.',
|
|
249
|
-
time`end:msec_frac`,
|
|
250
|
-
time`end:%z`,
|
|
251
|
-
),
|
|
252
|
-
),
|
|
253
|
-
end_msec: vcl`time.end.msec`,
|
|
254
|
-
elapsed: '%D',
|
|
255
|
-
},
|
|
256
|
-
client: {
|
|
257
|
-
name: str(vcl`client.as.name`),
|
|
258
|
-
number: vcl`client.as.number`,
|
|
259
|
-
location_geopoint: {
|
|
260
|
-
lat: vcl`client.geo.latitude`,
|
|
261
|
-
lon: vcl`client.geo.longitude`,
|
|
262
|
-
},
|
|
263
|
-
city_name: str(vcl`client.geo.city.ascii`),
|
|
264
|
-
country_name: str(vcl`client.geo.country_name.ascii`),
|
|
265
|
-
connection_speed: str(vcl`client.geo.conn_speed`),
|
|
266
|
-
ip: str(
|
|
267
|
-
vcl`regsuball(req.http.x-forwarded-for, ",.*", "")`,
|
|
268
|
-
),
|
|
269
|
-
},
|
|
270
|
-
request: {
|
|
271
|
-
id: str(vcl`if(req.http.X-CDN-Request-ID, req.http.X-CDN-Request-ID, randomstr(8, "0123456789abcdef") + "-" + randomstr(4, "0123456789abcdef") + "-" + randomstr(4, "0123456789abcdef") + "-" + randomstr(1, "89ab") + randomstr(3, "0123456789abcdef") + "-" + randomstr(12, "0123456789abcdef"))`),
|
|
272
|
-
method: str('%m'),
|
|
273
|
-
protocol: str(vcl`if(fastly_info.is_h2, "HTTP/2", "HTTP/1.1")`),
|
|
274
|
-
h2: vcl`if(fastly_info.is_h2, "true", "false")`,
|
|
275
|
-
is_ipv6: vcl`if(req.is_ipv6, "true", "false")`,
|
|
276
|
-
url: str(vcl`cstr_escape(if(req.http.X-Orig-Url, req.http.X-Orig-Url, req.url))`),
|
|
277
|
-
referer: req`Referer`,
|
|
278
|
-
user_agent: req`User-Agent`,
|
|
279
|
-
accept_content: req`Accept`,
|
|
280
|
-
accept_language: req`Accept-Language`,
|
|
281
|
-
accept_encoding: req`Accept-Encoding`,
|
|
282
|
-
accept_charset: req`Accept-Charset`,
|
|
283
|
-
xfh: req`X-Forwarded-Host`,
|
|
284
|
-
via: req`Via`,
|
|
285
|
-
cache_control: req`Cache-Control`,
|
|
286
|
-
header_size: vcl`req.header_bytes_read`,
|
|
287
|
-
body_size: vcl`req.body_bytes_read`,
|
|
288
|
-
restarts: vcl`req.restarts`,
|
|
289
|
-
versionlock: req`X-OW-Version-Lock`,
|
|
290
|
-
},
|
|
291
|
-
origin: {
|
|
292
|
-
host: str('%v'),
|
|
293
|
-
url: str(vcl`if(req.http.x-backend-url, req.http.x-backend-url, req.url)`),
|
|
294
|
-
},
|
|
295
|
-
response: {
|
|
296
|
-
status: '%s',
|
|
297
|
-
error: str(vcl`resp.http.x-error`),
|
|
298
|
-
content_type: res`Content-Type`,
|
|
299
|
-
header_size: vcl`resp.header_bytes_written`,
|
|
300
|
-
body_size: '%B',
|
|
301
|
-
},
|
|
302
|
-
edge: {
|
|
303
|
-
cache_status: str(vcl`fastly_info.state`),
|
|
304
|
-
datacenter: str(vcl`server.datacenter`),
|
|
305
|
-
ip: str('%A'),
|
|
306
|
-
},
|
|
307
|
-
},
|
|
308
|
-
applicationName: str(this._cfg.coralogixApp),
|
|
309
|
-
}),
|
|
310
|
-
url: 'https://api.coralogix.com/logs/rest/singles',
|
|
311
|
-
request_max_bytes: 2000000,
|
|
312
|
-
content_type: 'application/json',
|
|
313
|
-
header_name: 'private_key',
|
|
314
|
-
header_value: this._cfg.coralogixToken,
|
|
315
|
-
json_format: 1,
|
|
316
|
-
service_id: this._cfg.service,
|
|
317
|
-
});
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
async deploy() {
|
|
322
|
-
this.log.info(chalk`--: Set up {yellow Fastly} Gateway`);
|
|
323
|
-
try {
|
|
324
|
-
await this._fastly.transact(async (newversion) => {
|
|
325
|
-
await this.enableLogging(newversion);
|
|
326
|
-
|
|
327
|
-
this.log.info('--: create condition');
|
|
328
|
-
await this._fastly.writeCondition(newversion, 'false', {
|
|
329
|
-
name: 'false',
|
|
330
|
-
statement: 'false',
|
|
331
|
-
type: 'request',
|
|
332
|
-
});
|
|
333
|
-
|
|
334
|
-
this.log.info('--: create dictionaries');
|
|
335
|
-
await this._fastly.writeDictionary(newversion, 'priorities', {
|
|
336
|
-
name: 'priorities',
|
|
337
|
-
write_only: 'false',
|
|
338
|
-
});
|
|
339
|
-
|
|
340
|
-
await this._fastly.writeDictionary(newversion, 'aliases', {
|
|
341
|
-
name: 'aliases',
|
|
342
|
-
write_only: 'false',
|
|
343
|
-
});
|
|
344
|
-
|
|
345
|
-
await this._fastly.writeDictionary(newversion, 'tokens', {
|
|
346
|
-
name: 'tokens',
|
|
347
|
-
write_only: 'false',
|
|
348
|
-
});
|
|
349
|
-
|
|
350
|
-
await this._fastly.writeDictionary(newversion, 'packageparams', {
|
|
351
|
-
name: 'packageparams',
|
|
352
|
-
write_only: 'true',
|
|
353
|
-
});
|
|
354
|
-
|
|
355
|
-
if (this._cfg.checkinterval > 0 && this._cfg.checkpath) {
|
|
356
|
-
this.log.info('--: setup health-check');
|
|
357
|
-
// set up health checks
|
|
358
|
-
await Promise.all(this._deployers
|
|
359
|
-
.map((deployer) => ({
|
|
360
|
-
check_interval: this._cfg.checkinterval,
|
|
361
|
-
expected_response: 200,
|
|
362
|
-
host: deployer.host,
|
|
363
|
-
http_version: '1.1',
|
|
364
|
-
method: 'GET',
|
|
365
|
-
initial: 1,
|
|
366
|
-
name: `${deployer.name}Check`,
|
|
367
|
-
path: `${deployer.basePath}${this._cfg.checkpath}`,
|
|
368
|
-
threshold: 2,
|
|
369
|
-
timeout: 5000,
|
|
370
|
-
window: 3,
|
|
371
|
-
}))
|
|
372
|
-
.map((healthcheck) => this._fastly
|
|
373
|
-
.writeHealthcheck(newversion, healthcheck.name, healthcheck)));
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
// set up backends
|
|
377
|
-
await Promise.all(this._deployers
|
|
378
|
-
.map((deployer) => ({
|
|
379
|
-
hostname: deployer.host,
|
|
380
|
-
ssl_cert_hostname: deployer.host,
|
|
381
|
-
ssl_sni_hostname: deployer.host,
|
|
382
|
-
address: deployer.host,
|
|
383
|
-
override_host: deployer.host,
|
|
384
|
-
name: deployer.name,
|
|
385
|
-
error_threshold: 0,
|
|
386
|
-
first_byte_timeout: 60000,
|
|
387
|
-
weight: 100,
|
|
388
|
-
connect_timeout: 5000,
|
|
389
|
-
port: 443,
|
|
390
|
-
between_bytes_timeout: 10000,
|
|
391
|
-
shield: '', // 'bwi-va-us',
|
|
392
|
-
max_conn: 200,
|
|
393
|
-
use_ssl: true,
|
|
394
|
-
request_condition: 'false',
|
|
395
|
-
}))
|
|
396
|
-
.map((backend) => {
|
|
397
|
-
const retval = backend;
|
|
398
|
-
if (this._cfg.checkinterval > 0 && this._cfg.checkpath) {
|
|
399
|
-
retval.healthcheck = `${backend.name}Check`;
|
|
400
|
-
}
|
|
401
|
-
return retval;
|
|
402
|
-
})
|
|
403
|
-
.map(async (backend) => {
|
|
404
|
-
this.log.info(`--: create backend ${backend.name} -> ${backend.hostname}`);
|
|
405
|
-
try {
|
|
406
|
-
return await this._fastly.createBackend(newversion, backend);
|
|
407
|
-
} catch (e) {
|
|
408
|
-
return this._fastly.updateBackend(newversion, backend.name, backend);
|
|
409
|
-
}
|
|
410
|
-
}));
|
|
411
|
-
|
|
412
|
-
this.log.info('--: write VLC snippets');
|
|
413
|
-
await this._fastly.writeSnippet(newversion, 'packageparams.auth', {
|
|
414
|
-
name: 'packageparams.auth',
|
|
415
|
-
priority: 9,
|
|
416
|
-
dynamic: 0,
|
|
417
|
-
type: 'recv',
|
|
418
|
-
content: `
|
|
419
|
-
if (req.http.Authorization) {
|
|
420
|
-
if(time.is_after(std.time(table.lookup(tokens, regsub(req.http.Authorization, "^Bearer ", ""), "expired"), std.integer2time(0)), time.start)) {
|
|
421
|
-
error 600 "Get Package Params";
|
|
422
|
-
}
|
|
423
|
-
}`,
|
|
424
|
-
});
|
|
425
|
-
|
|
426
|
-
await this._fastly.writeSnippet(newversion, `${this.cfg.packageName}.params`, {
|
|
427
|
-
name: `${this.cfg.packageName}.params`,
|
|
428
|
-
priority: 10,
|
|
429
|
-
dynamic: 0,
|
|
430
|
-
type: 'error',
|
|
431
|
-
content: this.listPackageParamsVCL(),
|
|
432
|
-
});
|
|
433
|
-
|
|
434
|
-
await this._fastly.writeSnippet(newversion, 'backend', {
|
|
435
|
-
name: 'backend',
|
|
436
|
-
priority: 10,
|
|
437
|
-
dynamic: 0,
|
|
438
|
-
type: 'recv',
|
|
439
|
-
content: this.selectBackendVCL(),
|
|
440
|
-
});
|
|
441
|
-
|
|
442
|
-
await this._fastly.writeSnippet(newversion, 'missurl', {
|
|
443
|
-
name: 'missurl',
|
|
444
|
-
priority: 10,
|
|
445
|
-
dynamic: 0,
|
|
446
|
-
type: 'miss',
|
|
447
|
-
content: this.setURLVCL(),
|
|
448
|
-
});
|
|
449
|
-
|
|
450
|
-
await this._fastly.writeSnippet(newversion, 'passurl', {
|
|
451
|
-
name: 'passurl',
|
|
452
|
-
priority: 10,
|
|
453
|
-
dynamic: 0,
|
|
454
|
-
type: 'pass',
|
|
455
|
-
content: this.setURLVCL(),
|
|
456
|
-
});
|
|
457
|
-
|
|
458
|
-
await this._fastly.writeSnippet(newversion, 'logurl', {
|
|
459
|
-
name: 'logurl',
|
|
460
|
-
priority: 10,
|
|
461
|
-
dynamic: 0,
|
|
462
|
-
type: 'fetch',
|
|
463
|
-
content: `set beresp.http.X-Backend-URL = bereq.url;
|
|
464
|
-
set beresp.http.X-Backend-Name = req.backend;
|
|
465
|
-
set beresp.http.X-Backend-Health = req.http.X-Backend-Health;
|
|
466
|
-
set beresp.cacheable = false;`,
|
|
467
|
-
});
|
|
468
|
-
|
|
469
|
-
await this._fastly.writeSnippet(newversion, 'stashsurrogates', {
|
|
470
|
-
name: 'stashsurrogates',
|
|
471
|
-
priority: 10,
|
|
472
|
-
dynamic: 0,
|
|
473
|
-
type: 'fetch',
|
|
474
|
-
content: `
|
|
475
|
-
set beresp.http.X-Surrogate-Key = beresp.http.Surrogate-Key;
|
|
476
|
-
set beresp.http.X-Surrogate-Control = beresp.http.Surrogate-Control;`,
|
|
477
|
-
});
|
|
478
|
-
|
|
479
|
-
let restartcontent = `
|
|
480
|
-
# restart the request in case of flakiness
|
|
481
|
-
if (req.restarts < 2 && (resp.status == 503 || resp.status == 504) && (req.request == "GET" || req.request == "HEAD" || req.request == "PUT" || req.request == "DELETE")) {
|
|
482
|
-
restart;
|
|
483
|
-
}
|
|
484
|
-
set resp.http.x-gateway-restarts = req.restarts;
|
|
485
|
-
unset resp.http.Fastly-Restarts;`;
|
|
486
|
-
|
|
487
|
-
if (this._deployers.find((deployer) => deployer.name === 'Google')) {
|
|
488
|
-
restartcontent += `
|
|
489
|
-
# If Google can't find a function, it sends a redirect to the login page instead
|
|
490
|
-
# of a 404. This fixes it.
|
|
491
|
-
if (resp.status == 302 && req.backend == F_Google && resp.http.Location ~ "^https://accounts.google.com/ServiceLogin") {
|
|
492
|
-
set resp.status = 404;
|
|
493
|
-
}
|
|
494
|
-
`;
|
|
495
|
-
}
|
|
496
|
-
await this._fastly.writeSnippet(newversion, 'restart', {
|
|
497
|
-
name: 'restart',
|
|
498
|
-
priority: 10,
|
|
499
|
-
dynamic: 0,
|
|
500
|
-
type: 'deliver',
|
|
501
|
-
content: restartcontent,
|
|
502
|
-
});
|
|
503
|
-
|
|
504
|
-
await this._fastly.writeSnippet(newversion, 'restoresurrogates', {
|
|
505
|
-
name: 'restoresurrogates',
|
|
506
|
-
priority: 10,
|
|
507
|
-
dynamic: 0,
|
|
508
|
-
type: 'deliver',
|
|
509
|
-
content: `
|
|
510
|
-
set resp.http.Surrogate-Key = resp.http.X-Surrogate-Key;
|
|
511
|
-
set resp.http.Surrogate-Control = resp.http.X-Surrogate-Control;`,
|
|
512
|
-
});
|
|
513
|
-
}, true);
|
|
514
|
-
|
|
515
|
-
this.log.info(chalk`{green ok}: Set up {yellow Fastly} Gateway done.`);
|
|
516
|
-
} catch (e) {
|
|
517
|
-
this.log.error(chalk`{red error}: failed to setup gateway: ${e.message}`);
|
|
518
|
-
throw e;
|
|
519
|
-
} finally {
|
|
520
|
-
this._fastly.discard();
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
FastlyGateway.Config = FastlyConfig;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2024 Adobe. All rights reserved.
|
|
3
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
-
*
|
|
7
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
-
* governing permissions and limitations under the License.
|
|
11
|
-
*/
|
|
12
|
-
/* eslint-env serviceworker */
|
|
13
|
-
|
|
14
|
-
export function extractPathFromURL(request) {
|
|
15
|
-
const suffixMatches = /^https?:\/\/[^/]+([^?]+)/.exec(request.url);
|
|
16
|
-
return suffixMatches ? suffixMatches[1] : request.url.replace(/\?.*/, '');
|
|
17
|
-
}
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2021 Adobe. All rights reserved.
|
|
3
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
-
*
|
|
7
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
-
* governing permissions and limitations under the License.
|
|
11
|
-
*/
|
|
12
|
-
/* eslint-env serviceworker */
|
|
13
|
-
|
|
14
|
-
const { extractPathFromURL } = require('./adapter-utils.js');
|
|
15
|
-
|
|
16
|
-
async function handler(event) {
|
|
17
|
-
// console.log(event);
|
|
18
|
-
const { request } = event;
|
|
19
|
-
// eslint-disable-next-line import/no-unresolved,global-require
|
|
20
|
-
const { main } = require('./main.js');
|
|
21
|
-
const context = {
|
|
22
|
-
resolver: null,
|
|
23
|
-
pathInfo: {
|
|
24
|
-
suffix: extractPathFromURL(request),
|
|
25
|
-
},
|
|
26
|
-
runtime: {
|
|
27
|
-
name: 'cloudflare-workers',
|
|
28
|
-
region: request.cf.colo,
|
|
29
|
-
},
|
|
30
|
-
func: {
|
|
31
|
-
name: null,
|
|
32
|
-
package: null,
|
|
33
|
-
version: null,
|
|
34
|
-
fqn: null,
|
|
35
|
-
app: null,
|
|
36
|
-
},
|
|
37
|
-
invocation: {
|
|
38
|
-
id: null,
|
|
39
|
-
deadline: null,
|
|
40
|
-
transactionId: null,
|
|
41
|
-
requestId: null,
|
|
42
|
-
},
|
|
43
|
-
// eslint-disable-next-line no-undef
|
|
44
|
-
env: new Proxy(globalThis, {
|
|
45
|
-
get: (target, prop) => target[prop] || target.PACKAGE.get(prop),
|
|
46
|
-
}),
|
|
47
|
-
storage: null,
|
|
48
|
-
};
|
|
49
|
-
const response = await main(request, context);
|
|
50
|
-
return response;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function cloudflare() {
|
|
54
|
-
console.log('checking for cloudflare environment');
|
|
55
|
-
try {
|
|
56
|
-
if (caches && caches.default) {
|
|
57
|
-
return handler;
|
|
58
|
-
}
|
|
59
|
-
} catch {
|
|
60
|
-
return false;
|
|
61
|
-
}
|
|
62
|
-
return false;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
module.exports = cloudflare;
|