-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathamp4test.js
353 lines (325 loc) · 11 KB
/
amp4test.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
/**
* Copyright 2017 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const app = require('express').Router();
const cors = require('./amp-cors');
const minimist = require('minimist');
const argv = minimist(process.argv.slice(2), {
boolean: ['strictBabelTransform'],
});
const multer = require('multer');
const path = require('path');
const {renderShadowViewer} = require('./shadow-viewer');
const {replaceUrls} = require('./app-utils');
const upload = multer();
/* eslint-disable max-len */
const KARMA_SERVER_PORT = 9876;
const {SERVE_MODE} = process.env;
/**
* Logs the given messages to the console when --verbose is specified.
* @param {*} messages
*/
function log(...messages) {
if (argv.verbose) {
console.log.apply(console, messages);
}
}
app.use('/compose-doc', function(req, res) {
res.setHeader('X-XSS-Protection', '0');
const {body, css, experiments, extensions, spec} = req.query;
const compiled = process.env.SERVE_MODE == 'compiled';
const frameHtml = compiled
? 'dist.3p/current-min/frame.html'
: 'dist.3p/current/frame.max.html';
let experimentsBlock = '';
if (experiments) {
const string = `"${experiments.split(',').join('","')}"`;
// TODO: Why is setting localDev necessary?
// `allow-doc-opt-in` enables any experiment to be enabled via doc opt-in.
experimentsBlock = `<script>
window.AMP_CONFIG = window.AMP_CONFIG || {"localDev": true};
window.AMP_CONFIG['allow-doc-opt-in'] = (window.AMP_CONFIG['allow-doc-opt-in'] || []).concat([${string}]);
</script>
<meta name="amp-experiments-opt-in" content="${experiments}">`;
}
// TODO: Do we need to inject amp-3p-iframe-src for non-ad tests?
const head = `${experimentsBlock}
<meta name="amp-3p-iframe-src" content="http://localhost:9876/${frameHtml}">`;
const doc = composeDocument({
body,
css,
extensions: extensions ? extensions.split(',') : '',
head,
spec,
});
res.cookie('test-cookie', 'test');
res.send(doc);
});
app.use('/compose-html', function(req, res) {
res.setHeader('X-XSS-Protection', '0');
res.send(`
<!doctype html>
<html>
<head>
<title>NON-AMP TEST</title>
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
</head>
<body>
${req.query.body}
</body>
</html>
`);
});
app.use('/compose-shadow', function(req, res) {
const {docUrl} = req.query;
const viewerHtml = renderShadowViewer({
src: docUrl.replace(/^\//, ''),
port: KARMA_SERVER_PORT,
baseHref: path.dirname(req.url),
});
res.send(replaceUrls(SERVE_MODE, viewerHtml));
});
/**
* A server side temporary request storage which is useful for testing
* browser sent HTTP requests.
*/
const bank = {};
/**
* Deposit a request. An ID has to be specified. Will override previous request
* if the same ID already exists.
*/
app.use('/request-bank/:bid/deposit/:id/', upload.array(), (req, res) => {
cors.enableCors(req, res);
if (!bank[req.params.bid]) {
bank[req.params.bid] = {};
}
const key = req.params.id;
log('SERVER-LOG [DEPOSIT]: ', key);
if (typeof bank[req.params.bid][key] === 'function') {
bank[req.params.bid][key](req);
} else {
bank[req.params.bid][key] = req;
}
res.end();
});
/**
* Withdraw a request. If the request of the given ID is already in the bank,
* return it immediately. Otherwise wait until it gets deposited
* The same request cannot be withdrawn twice at the same time.
*/
app.use('/request-bank/:bid/withdraw/:id/', (req, res) => {
cors.enableCors(req, res);
if (!bank[req.params.bid]) {
bank[req.params.bid] = {};
}
const key = req.params.id;
log('SERVER-LOG [WITHDRAW]: ' + key);
const result = bank[req.params.bid][key];
if (typeof result === 'function') {
return res.status(500).send('another client is withdrawing this ID');
}
const callback = function(result) {
if (result === undefined) {
res.status(404).end();
} else {
res.json({
headers: result.headers,
body: result.body,
url: result.url,
});
}
delete bank[req.params.bid][key];
};
if (result) {
callback(result);
} else {
bank[req.params.bid][key] = callback;
}
});
/**
* Clean up all pending withdraw & deposit requests.
*/
app.use('/request-bank/:bid/teardown/', (req, res) => {
log('SERVER-LOG [TEARDOWN]');
const b = bank[req.params.bid];
for (const id in b) {
const callback = b[id];
if (typeof callback === 'function') {
// Respond 404 to pending request.
callback();
}
delete b[id];
}
res.end();
});
/**
* Serves a fake ad for test-amp-ad-fake.js
*/
app.get('/a4a/:bid', (req, res) => {
cors.enableCors(req, res);
const {bid} = req.params;
const body = `
<a href=https://ampbyexample.com target=_blank>
<amp-img alt="AMP Ad" height=250 src=//localhost:9876/amp4test/request-bank/${bid}/deposit/image width=300></amp-img>
</a>
<amp-pixel src="//localhost:9876/amp4test/request-bank/${bid}/deposit/pixel/foo?cid=CLIENT_ID(a)"></amp-pixel>
<amp-analytics>
<script type="application/json">
{
"requests": {
"pageview": "//localhost:9876/amp4test/request-bank/${bid}/deposit/analytics/bar"
},
"triggers": {
"pageview": {
"on": "visible",
"request": "pageview",
"extraUrlParams": {
"timestamp": "\${timestamp}",
"title": "\${title}",
"ampdocUrl": "\${ampdocUrl}",
"canonicalUrl": "\${canonicalUrl}",
"cid": "\${clientId(a)}",
"img": "\${htmlAttr(amp-img,src)}",
"navTiming": "\${navTiming(requestStart,requestStart)}",
"navType": "\${navType}",
"navRedirectCount": "\${navRedirectCount}",
"sourceUrl": "\${sourceUrl}",
"cookie": "\${cookie(test-cookie)}"
}
}
}
}
</script>
</amp-analytics>`;
const doc = composeDocument({
spec: 'amp4ads',
body,
css: 'body { background-color: #f4f4f4; }',
extensions: ['amp-analytics'],
});
res.cookie('test-cookie', 'test');
res.send(doc);
});
/**
* @param {{body: string, css: string|undefined, extensions: Array<string>|undefined, head: string|undefined, spec: string|undefined}} config
*/
function composeDocument(config) {
const {body, css, extensions, head, spec, mode} = config;
const m = mode || process.env.SERVE_MODE;
const cdn = m === 'cdn';
const compiled = m === 'compiled';
const cssTag = css ? `<style amp-custom>${css}</style>` : '';
// Set link[rel=canonical], CSS boilerplate and runtime <script> depending
// on the AMP spec.
let canonical, boilerplate, runtime;
const amp = spec || 'amp';
switch (amp) {
case 'amp':
canonical = '<link rel="canonical" href="http://nonblocking.io" />';
boilerplate =
'<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>';
runtime = cdn
? 'https://cdn.ampproject.org/v0.js'
: `/dist/${compiled ? 'v0' : 'amp'}.js`;
break;
case 'amp4ads':
canonical = '';
boilerplate =
'<style amp4ads-boilerplate>body{visibility:hidden}</style>';
runtime = cdn
? 'https://cdn.ampproject.org/amp4ads-v0.js'
: `/dist/${compiled ? 'amp4ads-v0' : 'amp-inabox'}.js`;
break;
case 'amp4email':
canonical = '';
boilerplate =
'<style amp4email-boilerplate>body{visibility:hidden}</style>';
runtime = cdn
? 'https://cdn.ampproject.org/v0.js'
: `/dist/${compiled ? 'v0' : 'amp'}.js`;
break;
default:
throw new Error('Unrecognized AMP spec: ' + spec);
}
const runtimeScript = `<script async src="${runtime}"></script>`;
// Generate extension <script> markup.
let extensionScripts = '';
if (extensions) {
extensionScripts = extensions
.map(extension => {
const tuple = extension.split(':');
const name = tuple[0];
const version = tuple[1] || '0.1';
const src = cdn
? `https://cdn.ampproject.org/v0/${name}-${version}.js`
: `/dist/v0/${name}-${version}.${compiled ? '' : 'max.'}js`;
return `<script async custom-element="${name}" src="${src}"></script>`;
})
.join('\n');
}
const topHalfOfHtml = `<!doctype html>
<html ${amp}>
<head>
<title>AMP TEST</title>
<meta charset="utf-8">
${canonical}
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
${head || ''}
${boilerplate}
${runtimeScript}
${extensionScripts}
${cssTag}
</head>`;
// To enable A4A FIE, a <script amp-ad-metadata> tag must exist.
let ampAdMeta = '';
if (amp === 'amp4ads') {
// `ampRuntimeUtf16CharOffsets` is used to cut out all runtime scripts,
// which are not needed in FIE mode.
const start = topHalfOfHtml.indexOf(runtimeScript);
let end = start + runtimeScript.length;
let customElements = [],
extensionsMap = [];
if (extensions) {
end = topHalfOfHtml.indexOf(extensionScripts) + extensionScripts.length;
// Filter out extensions that are not custom elements, e.g. amp-mustache.
customElements = extensions.filter(e => e !== 'amp-mustache');
extensionsMap = customElements.map(ce => {
return {
'custom-element': ce,
// TODO: Should this be a local URL i.e. /dist/v0/...?
'src': `https://cdn.ampproject.org/v0/${ce}-0.1.js`,
};
});
}
ampAdMeta = `<script amp-ad-metadata type=application/json>
{
"ampRuntimeUtf16CharOffsets": [ ${start}, ${end} ],
"customElementExtensions": ${JSON.stringify(customElements)},
"extensions": ${JSON.stringify(extensionsMap)}
}
</script>`;
}
return `${topHalfOfHtml}
<body>
${body}
${ampAdMeta}
</body>
</html>`;
}
module.exports = {
app,
log,
};