forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathamp-geo.js
600 lines (550 loc) · 18.9 KB
/
amp-geo.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
/**
* Copyright 2018 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.
*/
/**
* @fileoverview Sets location specific CSS, bind variables, and attributes on
* AMP pages
* Example:
* <code>
* <amp-geo>
* <script type="application-json">
* {
* ISOCountryGroups: {
* "anz": [ "au", "nz" ],
* "nafta": [' "ca", "mx", "us", "unknown" ],
* "iceland": [ "is" ]
* }
* }
* </scirpt>
* </amp-geo>
* </code>
*
* the amp-geo element's layout type is nodisplay.
*/
import {Deferred} from '../../../src/utils/promise';
import {Services} from '../../../src/services';
/**
* GOOGLE AND THE AMP PROJECT ARE PROVIDING THIS INFORMATION AS A COURTESY BUT
* DO NOT GUARANTEE THE ACCURACY OR COMPLETENESS OF ANY INFORMATION CONTAINED
* HEREIN. THIS INFORMATION IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
import {US_CA_CODE, ampGeoPresets} from './amp-geo-presets';
import {GEO_IN_GROUP} from './amp-geo-in-group';
import {dev, user, userAssert} from '../../../src/log';
import {getMode} from '../../../src/mode';
import {isArray, isObject} from '../../../src/types';
import {isCanary} from '../../../src/experiments';
import {isJsonScriptTag} from '../../../src/dom';
import {tryParseJson} from '../../../src/json';
import {urls} from '../../../src/config';
/** @const */
const TAG = 'amp-geo';
/**
* COUNTRY is a special const the magic string AMP_ISO_COUNTRY_HOTPATCH
* is replaced at serving time with the two letter country code or
* error value padded to length to avoid breaking .map files. We
* then trim it and store is as this.country_
*
* So don't change the magic string!
*/
const COUNTRY = '{{AMP_ISO_COUNTRY_HOTPATCH}}';
const COUNTRY_PREFIX = 'amp-iso-country-';
const GROUP_PREFIX = 'amp-geo-group-';
const PRE_RENDER_REGEX = new RegExp(`${COUNTRY_PREFIX}(\\w+)`);
const GEO_ID = 'ampGeo';
const SERVICE_TAG = 'geo';
const API_TIMEOUT = 60; // Seconds
const GEO_HOTPATCH_STR_REGEX = /^(?:(\w{2})(?:\s(\w{2}-\w{2}))?)?\s*/;
/**
* Operating Mode
* @enum {number}
*/
const mode = {
GEO_HOT_PATCH: 0, // Default mode, geo is patched by GFE when js is served
GEO_PRERENDER: 1, // We've been prerendered by an AMP Cache or publisher CMS
GEO_OVERRIDE: 2, // We've been overriden in test by #amp-geo=xx
GEO_API: 3, // Query API when cache patching unavailable
};
// TODO(zhouyx@): Rename if we have generic subdivision group support
/**
* @typedef {{
* ISOCountry: string,
* matchedISOCountryGroups: !Array<string>,
* allISOCountryGroups: !Array<string>,
* isInCountryGroup: (function(string):GEO_IN_GROUP),
* }}
*/
export let GeoDef;
export class AmpGeo extends AMP.BaseElement {
/** @param {!AmpElement} element */
constructor(element) {
super(element);
/** @private {number} */
this.mode_ = mode.GEO_HOT_PATCH;
/** @private {boolean} */
this.error_ = false;
/** @private {string} */
this.country_ = 'unknown';
/** @private {string} */
this.subdivision_ = 'unknown';
/** @private {Array<string>} */
this.matchedGroups_ = [];
/** @private {Array<string>} */
this.definedGroups_ = [];
}
/** @override */
prerenderAllowed() {
return true;
}
/** @override */
buildCallback() {
// All geo config within the amp-geo component.
// The validator only allows one amp-geo per page
const {children} = this.element;
if (children.length) {
this.assertWithErrorReturn_(
children.length === 1 && isJsonScriptTag(children[0]),
`${TAG} can only have one <script type="application/json"> child`
);
}
const config = children.length
? tryParseJson(children[0].textContent, () =>
this.assertWithErrorReturn_(false, `${TAG} Unable to parse JSON`)
)
: {};
/** @type {!Promise<!GeoDef>} */
const geo = this.addToBody_(config || {});
/* resolve the service promise singleton we stashed earlier */
geoDeferred.resolve(geo);
}
/**
* resolves geoDeferred with null if not shouldBeTrueish and then calls
* userAssert() to deal with the error as normal.
* @param {T} shouldBeTrueish The value to assert.
* The assert fails if it does not evaluate to true.
* @param {string=} opt_message The assertion message
* @return {T} The value of shouldBeTrueish.
* @template T
* @private
*/
assertWithErrorReturn_(shouldBeTrueish, opt_message) {
if (!shouldBeTrueish) {
geoDeferred.resolve(null);
return userAssert(shouldBeTrueish, opt_message);
}
return shouldBeTrueish;
}
/**
* findCountry_, sets this.country_ and this.mode_
* @param {!../../../src/service/ampdoc-impl.AmpDoc} ampdoc
* @return {Promise}
*/
findCountry_(ampdoc) {
// Flag to see if we've been pre-rendered with a country
const preRenderMatch = ampdoc.getBody().className.match(PRE_RENDER_REGEX);
// Trim the spaces off the patched country.
// This is guaranteed to always match
// - Correctly patched will have the two-char country code and whitespace.
// - Unknown country will not have the country code, but will match all
// whitespace.
// - Unpatched will match, but will not have a country code nor whitespace.
// 'xx ': trimmedGeoMatch is ["xx ", "xx", undefined]
// 'xx xx-xx ': trimmedGeoMatch is ["xx xx-xx ", "xx", "xx-xx"];
// ' ': trimmedGeoMatch is [" ", undefined, undefined];
// '{{AMP_ISO_COUNTRY_HOTPATCH}}': ["", undefined, undefined]
const trimmedGeoMatch = GEO_HOTPATCH_STR_REGEX.exec(COUNTRY);
// default country is 'unknown' which is also the zero length case
if (
getMode(this.win).geoOverride &&
(isCanary(this.win) || getMode(this.win).localDev)
) {
// debug override case, only works in canary or localdev
// match to \w characters only to prevent xss vector
const overrideGeoMatch = GEO_HOTPATCH_STR_REGEX.exec(
getMode(this.win).geoOverride.toLowerCase()
);
if (overrideGeoMatch[1]) {
this.country_ = overrideGeoMatch[1].toLowerCase();
if (overrideGeoMatch[2]) {
// Allow subdivision_ to be customized for testing, not checking us-ca
this.subdivision_ = overrideGeoMatch[2].toLowerCase();
}
this.mode_ = mode.GEO_OVERRIDE;
}
} else if (
preRenderMatch &&
!Services.urlForDoc(this.element).isProxyOrigin(this.win.location)
) {
// pre-rendered by a publisher case, if we're a cache we ignore that
// since there is no way the publisher could know the geo of the client.
// When caches start pre-rendering geo we'll need to add specifc code
// to handle that.
this.mode_ = mode.GEO_PRERENDER;
this.country_ = preRenderMatch[1];
} else if (trimmedGeoMatch[1]) {
// We have a valid 2 letter ISO country
this.mode_ = mode.GEO_HOT_PATCH;
this.country_ = trimmedGeoMatch[1].toLowerCase();
if (
trimmedGeoMatch[2] &&
trimmedGeoMatch[2].toLowerCase() === US_CA_CODE
) {
// Has subdivision code support (us-ca only)
this.subdivision_ = US_CA_CODE;
}
} else if (trimmedGeoMatch[0] === '' && urls.geoApi) {
// We were not patched, but an API is available
this.mode_ = mode.GEO_API;
} else if (trimmedGeoMatch[0] === '' && !getMode(this.win).localDev) {
// We were not patched, if we're not in dev this is an error
// and we leave the country at the default 'unknown'
this.error_ = true;
dev().error(
TAG,
'GEONOTPATCHED: amp-geo served unpatched, ISO country not set'
);
}
return this.mode_ !== mode.GEO_API
? Promise.resolve()
: this.fetchCountry_().then((data) => {
if (data) {
const {country, subdivision} = data;
// Country is required and guaranteed to exist if data is available.
this.country_ = country;
// Subdivision is optional and only us-ca is currently supported.
if (subdivision && `${country}-${subdivision}` === US_CA_CODE) {
this.subdivision_ = US_CA_CODE;
}
} else {
// if API request fails, leave the country at the default 'unknown'
this.error_ = true;
dev().error(
TAG,
'GEONOTPATCHED: amp-geo served unpatched and API response not valid, ISO country not set'
);
}
});
}
/**
* Ensure API URL definition is usable and cast its type
* @param {*} url
* @return {?string}
* @private
*/
validateApiUrl_(url) {
if (typeof url !== 'string') {
user().error(TAG, 'geoApiUrl must be a string URL');
return null;
}
if (!Services.urlForDoc(this.element).isSecure(url)) {
user().error(TAG, 'geoApiUrl must be secure (https)');
return null;
}
return url;
}
/**
* Fetch country from API defined in config.urls
*
* JSON schema of Geo API response - version 0.2:
* {
* "$schema": "http://json-schema.org/draft-07/schema#",
* "type": "object",
* "properties": {
* "country": {
* "type": "string",
* "title": "ISO 3166-1 alpha-2 (case insensitive) country code of client request",
* "default": "",
* "pattern": "^[a-zA-Z]{2}$"
* },
* "subdivision": {
* "type": "string",
* "title": "Subdivision part of ISO 3166-2 (case insensitive) country-subdivision code of client request",
* "default": "",
* "pattern": "^[a-zA-Z0-9]{1,3}$"
* }
* },
* "required": [
* "country"
* ]
* }
*
* Sample response - country only:
* {
* "country": "de"
* }
*
* Sample response - country and subdivision:
* {
* "country": "us",
* "subdivision": "ca"
* }
*
* @return {Promise<?Object.<string, ?string>>}
* @private
*/
fetchCountry_() {
const url = this.validateApiUrl_(urls.geoApi);
if (!url) {
return Promise.resolve(null);
}
user().info(
TAG,
'API request is being used for country, this may result in FOUC'
);
return Services.timerFor(this.win)
.timeoutPromise(
API_TIMEOUT * 1000,
Services.xhrFor(this.win)
.fetchJson(url, {
mode: 'cors',
method: 'GET',
credentials: 'omit',
})
.then((res) => res.json())
.then((json) => {
if (!/^[a-z]{2}$/i.test(json['country'])) {
user().error(
TAG,
'Invalid API response, expected schema not matched for property "country"'
);
return null;
}
return {
country: json['country'].toLowerCase(),
subdivision: /^[a-z0-9]{1,3}$/i.test(json['subdivision'])
? json['subdivision'].toLowerCase()
: null,
};
})
.catch((reason) => {
user().error(TAG, 'XHR country request failed', reason);
return null;
}),
`Timeout (${API_TIMEOUT} sec) reached waiting for API response`
)
.catch((error) => {
user().error(TAG, error);
return null;
});
}
/**
* Find matching country groups
* @param {Object} config
*/
matchCountryGroups_(config) {
// ISOCountryGroups are optional but if specified at least one must exist
const ISOCountryGroups = /** @type {!Object<string, !Array<string>>} */ (config[
'ISOCountryGroups'
]);
const errorPrefix = '<amp-geo> ISOCountryGroups'; // code size
if (ISOCountryGroups) {
// TODO(zhouyx@): Change the name with generic ISO subdivision support
this.assertWithErrorReturn_(
isObject(ISOCountryGroups),
`${errorPrefix} must be an object`
);
this.definedGroups_ = Object.keys(ISOCountryGroups);
this.definedGroups_.forEach((group) => {
this.assertWithErrorReturn_(
/^[a-z]+[a-z0-9]*$/i.test(group) && !/^amp/.test(group),
`${errorPrefix}[${group}] name is invalid`
);
this.assertWithErrorReturn_(
isArray(ISOCountryGroups[group]),
`${errorPrefix}[${group}] must be an array`
);
if (this.checkGroup_(ISOCountryGroups[group])) {
this.matchedGroups_.push(group);
}
});
}
}
/**
* checkGroup_() does this.country_ match the group
* after expanding any presets and forceing to lower case.
* @param {!Array<string>} countryGroup The group to match against
* @return {boolean}
*/
checkGroup_(countryGroup) {
/** @type {!Array<string>} */
const expandedGroup = countryGroup
.reduce((countries, country) => {
// If it's a valid preset then we expand it.
if (/^preset-/.test(country)) {
this.assertWithErrorReturn_(
isArray(ampGeoPresets[country]),
`<amp-geo> preset ${country} not found`
);
return countries.concat(ampGeoPresets[country]);
}
// Otherwise we add the country to the list
if (country == 'unknown' || /^[a-zA-Z]{2}$/.test(country)) {
countries.push(country);
} else {
user().error(TAG, ' country %s not valid, will be ignored', country);
}
return countries;
}, [])
.map((c) => c.toLowerCase());
return (
expandedGroup.includes(this.country_) ||
(expandedGroup.includes(US_CA_CODE) && this.subdivision_ == US_CA_CODE)
);
}
/**
* clearPreRender_()
* Returns a list of classes to remove if pre-render has
* been invalidated by way of being on an amp cache
* @param {Element} body
* @return {Array<string>}
*/
clearPreRender_(body) {
const {classList} = body;
const classesToRemove = [];
const stripRe = new RegExp('^' + COUNTRY_PREFIX + '|^' + GROUP_PREFIX, 'i');
for (let i = classList.length - 1; i > 0; i--) {
if (stripRe.test(classList[i])) {
classesToRemove.push(classList[i]);
}
}
return classesToRemove;
}
/**
* Adds the given country groups to HTML element as classes
* @param {Object} config
* @return {!Promise<!GeoDef>} service response
* @private
*/
addToBody_(config) {
const ampdoc = this.getAmpDoc();
/** @type {Object} */
const states = {};
const self = this;
// Wait for the body before we figure anything out because we might be
// prerendered and we know that from body classes
return ampdoc
.whenReady()
.then(() => ampdoc.waitForBodyOpen())
.then((body) => {
return self.findCountry_(ampdoc).then(() => body);
})
.then((body) => {
self.matchCountryGroups_(config);
let classesToRemove = [];
switch (self.mode_) {
case mode.GEO_OVERRIDE:
classesToRemove = self.clearPreRender_(body);
// Intentionally fall through.
case mode.GEO_HOT_PATCH:
case mode.GEO_API:
// Build the AMP State, add classes
states.ISOCountry = self.country_;
const classesToAdd = self.matchedGroups_.map((group) => {
states[group] = true;
return GROUP_PREFIX + group;
});
if (!self.matchedGroups_.length) {
classesToAdd.push('amp-geo-no-group');
}
if (self.error_) {
classesToAdd.push('amp-geo-error');
}
states.ISOCountryGroups = self.matchedGroups_;
classesToAdd.push(COUNTRY_PREFIX + this.country_);
// Let the runtime know we're mutating the AMP body
// Actual change happens in callback so runtime can
// optimize dom mutations.
self.mutateElement(() => {
const {classList} = body;
// Always remove the pending class
classesToRemove.push('amp-geo-pending');
classesToRemove.forEach((toRemove) => {
/** @type {!DOMTokenList} */ (classList).remove(toRemove);
});
// add the new classes to <body>
classesToAdd.forEach((toAdd) => classList.add(toAdd));
// Only include amp state if user requests it to
// avoid validator issue with missing amp-bind js
if (config['AmpBind']) {
const geoState = ampdoc.getElementById(GEO_ID);
if (geoState) {
geoState.parentNode.removeChild(geoState);
}
const state = ampdoc.win.document.createElement('amp-state');
const confScript = ampdoc.win.document.createElement('script');
confScript.setAttribute('type', 'application/json');
confScript.textContent = JSON.stringify(
/** @type {!JsonObject} */ (states)
);
state.appendChild(confScript);
state.id = GEO_ID;
body.appendChild(state);
}
}, body);
break;
case mode.GEO_PRERENDER:
break;
}
return {
ISOCountry: self.country_,
matchedISOCountryGroups: self.matchedGroups_,
allISOCountryGroups: this.definedGroups_,
/* API */
isInCountryGroup: this.isInCountryGroup.bind(this),
};
});
}
/**
* isInCountryGroup API
* @param {string} targetGroup group or comma delimited list of groups
* @return {GEO_IN_GROUP}
* @public
*/
isInCountryGroup(targetGroup) {
const targets = targetGroup.trim().split(/,\s*/);
// If any of the group are missing it's an error
if (
targets.filter((group) => {
return this.definedGroups_.indexOf(group) >= 0;
}).length !== targets.length
) {
return GEO_IN_GROUP.NOT_DEFINED;
}
// If any of the groups match it's a match
if (
targets.filter((group) => {
return this.matchedGroups_.indexOf(group) >= 0;
}).length > 0
) {
return GEO_IN_GROUP.IN;
}
// If we got here nothing matched
return GEO_IN_GROUP.NOT_IN;
}
}
/**
* Create the service promise at load time to prevent race between extensions
*/
/** singleton */
let geoDeferred = null;
AMP.extension('amp-geo', '0.1', (AMP) => {
geoDeferred = new Deferred();
AMP.registerElement(TAG, AmpGeo);
AMP.registerServiceForDoc(SERVICE_TAG, function () {
return geoDeferred.promise;
});
});