-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
extensions-impl.js
636 lines (596 loc) · 18.8 KB
/
extensions-impl.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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/**
* Copyright 2016 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.
*/
import {Deferred} from '../utils/promise';
import {Services} from '../services';
import {
calculateExtensionScriptUrl,
parseExtensionUrl,
} from './extension-location';
import {dev, devAssert, rethrowAsync, user} from '../log';
import {getMode} from '../mode';
import {installStylesForDoc} from '../style-installer';
import {map} from '../utils/object';
import {registerServiceBuilder, registerServiceBuilderForDoc} from '../service';
import {startsWith} from '../string';
import {
stubElementIfNotKnown,
upgradeOrRegisterElement,
} from './custom-element-registry';
export const LEGACY_ELEMENTS = ['amp-ad', 'amp-embed', 'amp-video'];
const TAG = 'extensions';
const UNKNOWN_EXTENSION = '_UNKNOWN_';
const CUSTOM_TEMPLATES = ['amp-mustache'];
const LOADER_PROP = '__AMP_EXT_LDR';
/**
* Default milliseconds to wait for all extensions to load before erroring.
* (8 seconds is the same as the CSS boilerplate timoeout)
* @const
*/
const LOAD_TIMEOUT = 16000;
/**
* Contains data for the declaration of a custom element.
*
* @typedef {{
* implementationClass:
* typeof ../base-element.BaseElement,
* css: (?string|undefined),
* }}
*/
let ExtensionElementDef;
/**
* Contains data for the declaration of an extension service.
*
* @typedef {{serviceName: string, serviceClass: function(new:Object, !./ampdoc-impl.AmpDoc)}}
*/
let ExtensionServiceDef;
/**
* The structure that contains the resources declared by an extension.
*
* @typedef {{
* elements: !Object<string, !ExtensionElementDef>,
* services: !Object<string, !ExtensionServiceDef>,
* }}
*/
let ExtensionDef;
/**
* Internal structure that maintains the state of an extension through loading.
*
* @typedef {{
* extension: !ExtensionDef,
* auto: boolean,
* docFactories: !Array<function(!./ampdoc-impl.AmpDoc)>,
* promise: (!Promise<!ExtensionDef>|undefined),
* resolve: (function(!ExtensionDef)|undefined),
* reject: (function(!Error)|undefined),
* loaded: (boolean|undefined),
* error: (!Error|undefined),
* scriptPresent: (boolean|undefined),
* }}
* @private
*/
let ExtensionHolderDef;
/**
* @param {string} extensionId
* @return {boolean}
*/
export function isTemplateExtension(extensionId) {
return CUSTOM_TEMPLATES.indexOf(extensionId) >= 0;
}
/**
* @param {string} extensionId
* @return {boolean}
*/
function isIntermediateExtension(extensionId) {
return startsWith(extensionId, '_');
}
/**
* Install extensions service.
* @param {!Window} window
* @restricted
*/
export function installExtensionsService(window) {
registerServiceBuilder(window, 'extensions', Extensions);
}
/**
* The services that manages extensions in the runtime.
* @visibleForTesting
*/
export class Extensions {
/**
* @param {!Window} win
*/
constructor(win) {
/** @const {!Window} */
this.win = win;
/** @const @private */
this.ampdocService_ = Services.ampdocServiceFor(win);
/** @private @const {!Object<string, !ExtensionHolderDef>} */
this.extensions_ = {};
/** @private {?string} */
this.currentExtensionId_ = null;
}
/**
* Register and process the specified extension. The factory is called
* immediately, which in turn is expected to register elements, templates,
* services and document factories. This method is called by the extension's
* script itself when it's loaded using the regular `AMP.push()` callback.
* @param {string} extensionId
* @param {function(!Object, !Object)} factory
* @param {!Object} arg
* @restricted
*/
registerExtension(extensionId, factory, arg) {
const holder = this.getExtensionHolder_(extensionId, /* auto */ true);
try {
this.currentExtensionId_ = extensionId;
factory(arg, arg['_']);
if (getMode(this.win).localDev || getMode(this.win).test) {
if (Object.freeze) {
const m = holder.extension;
m.elements = Object.freeze(m.elements);
holder.extension = Object.freeze(m);
}
}
holder.loaded = true;
if (holder.resolve) {
holder.resolve(holder.extension);
}
} catch (e) {
holder.error = e;
if (holder.reject) {
holder.reject(e);
}
throw e;
} finally {
this.currentExtensionId_ = null;
}
}
/**
* Waits for the previously included extension to complete
* loading/registration.
* @param {!Window} win
* @param {string} extensionId
* @param {number=} opt_timeout
* @return {!Promise<?ExtensionDef>}
*/
waitForExtension(win, extensionId, opt_timeout) {
return /** @type {!Promise<?ExtensionDef>} */ (Services.timerFor(
win
).timeoutPromise(
opt_timeout || LOAD_TIMEOUT,
this.waitFor_(this.getExtensionHolder_(extensionId, /* auto */ false)),
`Render timeout waiting for extension ${extensionId} to be load.`
));
}
/**
* Returns the promise that will be resolved when the extension has been
* loaded. If necessary, adds the extension script to the page.
* @param {string} extensionId
* @param {string=} opt_extensionVersion
* @return {!Promise<!ExtensionDef>}
*/
preloadExtension(extensionId, opt_extensionVersion) {
if (extensionId == 'amp-embed') {
extensionId = 'amp-ad';
}
const holder = this.getExtensionHolder_(extensionId, /* auto */ false);
this.insertExtensionScriptIfNeeded_(
extensionId,
holder,
opt_extensionVersion
);
return this.waitFor_(holder);
}
/**
* Returns the promise that will be resolved when the extension has been
* loaded. If necessary, adds the extension script to the page.
* @param {!./ampdoc-impl.AmpDoc} ampdoc
* @param {string} extensionId
* @param {string=} opt_extensionVersion
* @return {!Promise<!ExtensionDef>}
*/
installExtensionForDoc(ampdoc, extensionId, opt_extensionVersion) {
const rootNode = ampdoc.getRootNode();
let extLoaders = rootNode[LOADER_PROP];
if (!extLoaders) {
extLoaders = rootNode[LOADER_PROP] = map();
}
if (extLoaders[extensionId]) {
return extLoaders[extensionId];
}
stubElementIfNotKnown(ampdoc.win, extensionId);
return (extLoaders[extensionId] = this.preloadExtension(
extensionId,
opt_extensionVersion
).then(() => this.installExtensionInDoc(ampdoc, extensionId)));
}
/**
* Reloads the new version of the extension.
* @param {string} extensionId
* @return {?Promise<!ExtensionDef>}
*/
reloadExtension(extensionId) {
// Ignore inserted script elements to prevent recursion.
const els = this.getExtensionScripts_(
extensionId,
/* includeInserted */ false
);
if (!els.length) {
const TAG = 'reloadExtension';
user().error(
TAG,
'Extension script for "%s" is missing or was already reloaded.',
extensionId
);
return null;
}
// The previously awaited extension loader must not have finished or
// failed.
const holder = this.extensions_[extensionId];
if (holder) {
devAssert(!holder.loaded && !holder.error);
holder.scriptPresent = false;
}
els.forEach((el) =>
el.setAttribute('i-amphtml-loaded-new-version', extensionId)
);
const urlParts = parseExtensionUrl(els[0].src);
return this.preloadExtension(extensionId, urlParts.extensionVersion);
}
/**
* Returns the extension <script> element and attribute for the given
* extension ID, if it exists. Otherwise, returns null.
* @param {string} extensionId
* @param {boolean=} includeInserted If true, includes script elements that
* are inserted by the runtime dynamically. Default is true.
* @return {!Array<!Element>}
* @private
*/
getExtensionScripts_(extensionId, includeInserted = true) {
// Always ignore <script> elements that have a mismatched RTV.
const modifier =
':not([i-amphtml-loaded-new-version])' +
(includeInserted ? '' : ':not([i-amphtml-inserted])');
// We have to match against "src" because a few extensions, such as
// "amp-viewer-integration", do not have "custom-element" attribute.
const matches = this.win.document.head./*OK*/ querySelectorAll(
`script[src*="/${extensionId}-"]` + modifier
);
const filtered = [];
for (let i = 0; i < matches.length; i++) {
const match = matches[i];
const urlParts = parseExtensionUrl(match.src);
if (urlParts.extensionId === extensionId) {
filtered.push(match);
}
}
return filtered;
}
/**
* Returns the promise that will be resolved with the extension element's
* class when the extension has been loaded. If necessary, adds the extension
* script to the page.
* @param {string} elementName
* @return {!Promise<typeof ../base-element.BaseElement>}
*/
loadElementClass(elementName) {
return this.preloadExtension(elementName).then((extension) => {
const element = devAssert(
extension.elements[elementName],
'Element not found: %s',
elementName
);
return element.implementationClass;
});
}
/**
* Add an element to the extension currently being registered. This is a
* restricted method and it's allowed to be called only during the overall
* extension registration.
* @param {string} name
* @param {typeof ../base-element.BaseElement} implementationClass
* @param {?string|undefined} css
* @restricted
*/
addElement(name, implementationClass, css) {
const holder = this.getCurrentExtensionHolder_(name);
holder.extension.elements[name] = {implementationClass, css};
this.addDocFactory((ampdoc) => {
this.installElement_(ampdoc, name, implementationClass, css);
});
}
/**
* Installs the specified element implementation in the ampdoc.
* @param {!./ampdoc-impl.AmpDoc} ampdoc
* @param {string} name
* @param {typeof ../base-element.BaseElement} implementationClass
* @param {?string|undefined} css
* @private
*/
installElement_(ampdoc, name, implementationClass, css) {
if (css) {
installStylesForDoc(
ampdoc,
css,
() => {
this.registerElementInWindow_(ampdoc.win, name, implementationClass);
},
/* isRuntimeCss */ false,
name
);
} else {
this.registerElementInWindow_(ampdoc.win, name, implementationClass);
}
}
/**
* @param {!Window} win
* @param {string} name
* @param {typeof ../base-element.BaseElement} implementationClass
* @private
*/
registerElementInWindow_(win, name, implementationClass) {
// Register the element in the window.
upgradeOrRegisterElement(win, name, implementationClass);
// Register this extension to resolve its Service Promise.
registerServiceBuilder(win, name, emptyService);
}
/**
* Add a service to the extension currently being registered. This is a
* restricted method and it's allowed to be called only during the overall
* extension registration.
* @param {string} name
* @param {function(new:Object, !./ampdoc-impl.AmpDoc)} implementationClass
*/
addService(name, implementationClass) {
const holder = this.getCurrentExtensionHolder_();
holder.extension.services.push(
/** @type {!ExtensionServiceDef} */ ({
serviceName: name,
serviceClass: implementationClass,
})
);
this.addDocFactory((ampdoc) => {
registerServiceBuilderForDoc(
ampdoc,
name,
implementationClass,
/* instantiate */ true
);
});
}
/**
* Add a ampdoc factory to the extension currently being registered. This is a
* restricted method and it's allowed to be called only during the overall
* extension registration.
* @param {function(!./ampdoc-impl.AmpDoc)} factory
* @param {string=} opt_forName
* @restricted
*/
addDocFactory(factory, opt_forName) {
const holder = this.getCurrentExtensionHolder_(opt_forName);
holder.docFactories.push(factory);
// If a single-doc mode, run factory right away if it's included by the doc.
if (this.currentExtensionId_ && this.ampdocService_.isSingleDoc()) {
const ampdoc = this.ampdocService_.getAmpDoc(this.win.document);
const extensionId = dev().assertString(this.currentExtensionId_);
// Note that this won't trigger for FIE extensions that are not present
// in the parent doc.
if (ampdoc.declaresExtension(extensionId) || holder.auto) {
factory(ampdoc);
}
}
}
/**
* Installs all ampdoc factories previously registered with
* `addDocFactory`.
* @param {!./ampdoc-impl.AmpDoc} ampdoc
* @param {!Array<string>} extensionIds
* @return {!Promise}
* @restricted
*/
installExtensionsInDoc(ampdoc, extensionIds) {
const promises = [];
extensionIds.forEach((extensionId) => {
promises.push(this.installExtensionInDoc(ampdoc, extensionId));
});
return Promise.all(promises);
}
/**
* Installs all ampdoc factories for the specified extension.
* @param {!./ampdoc-impl.AmpDoc} ampdoc
* @param {string} extensionId
* @return {!Promise}
*/
installExtensionInDoc(ampdoc, extensionId) {
const holder = this.getExtensionHolder_(extensionId, /* auto */ false);
return this.waitFor_(holder).then(() => {
ampdoc.declareExtension(extensionId);
holder.docFactories.forEach((factory) => {
try {
factory(ampdoc);
} catch (e) {
rethrowAsync('Doc factory failed: ', e, extensionId);
}
});
});
}
/**
* Creates or returns an existing extension holder.
* @param {string} extensionId
* @param {boolean} auto
* @return {!ExtensionHolderDef}
* @private
*/
getExtensionHolder_(extensionId, auto) {
let holder = this.extensions_[extensionId];
if (!holder) {
const extension = /** @type {ExtensionDef} */ ({
elements: {},
services: [],
});
holder = /** @type {ExtensionHolderDef} */ ({
extension,
auto,
docFactories: [],
promise: undefined,
resolve: undefined,
reject: undefined,
loaded: undefined,
error: undefined,
scriptPresent: undefined,
});
this.extensions_[extensionId] = holder;
}
return holder;
}
/**
* Returns the holder for the extension currently being registered.
* @param {string=} opt_forName Used for logging only.
* @return {!ExtensionHolderDef}
* @private
*/
getCurrentExtensionHolder_(opt_forName) {
if (!this.currentExtensionId_ && !getMode(this.win).test) {
dev().error(TAG, 'unknown extension for ', opt_forName);
}
return this.getExtensionHolder_(
this.currentExtensionId_ || UNKNOWN_EXTENSION,
/* auto */ true
);
}
/**
* Creates or returns an existing promise that will yield as soon as the
* extension has been loaded.
* @param {!ExtensionHolderDef} holder
* @return {!Promise<!ExtensionDef>}
* @private
*/
waitFor_(holder) {
if (!holder.promise) {
if (holder.loaded) {
holder.promise = Promise.resolve(holder.extension);
} else if (holder.error) {
holder.promise = Promise.reject(holder.error);
} else {
const deferred = new Deferred();
holder.promise = deferred.promise;
holder.resolve = deferred.resolve;
holder.reject = deferred.reject;
}
}
return holder.promise;
}
/**
* Ensures that the script has already been injected in the page.
* @param {string} extensionId
* @param {!ExtensionHolderDef} holder
* @param {string=} opt_extensionVersion
* @private
*/
insertExtensionScriptIfNeeded_(extensionId, holder, opt_extensionVersion) {
if (this.isExtensionScriptRequired_(extensionId, holder)) {
const scriptElement = this.createExtensionScript_(
extensionId,
opt_extensionVersion
);
this.win.document.head.appendChild(scriptElement);
holder.scriptPresent = true;
}
}
/**
* Determine the need to add amp extension script to document.
* @param {string} extensionId
* @param {!ExtensionHolderDef} holder
* @return {boolean}
* @private
*/
isExtensionScriptRequired_(extensionId, holder) {
if (holder.loaded || holder.error) {
return false;
}
if (holder.scriptPresent === undefined) {
const scriptsInHead = this.getExtensionScripts_(extensionId);
holder.scriptPresent = scriptsInHead.length > 0;
}
return !holder.scriptPresent;
}
/**
* Create the missing amp extension HTML script element.
* @param {string} extensionId
* @param {string=} opt_extensionVersion
* @return {!Element} Script object
* @private
*/
createExtensionScript_(extensionId, opt_extensionVersion) {
const scriptElement = this.win.document.createElement('script');
scriptElement.async = true;
if (isIntermediateExtension(extensionId)) {
opt_extensionVersion = '';
} else {
scriptElement.setAttribute(
this.attributeForExtension_(extensionId),
extensionId
);
}
scriptElement.setAttribute('data-script', extensionId);
scriptElement.setAttribute('i-amphtml-inserted', '');
// Propagate nonce to all generated script tags.
const currentScript = this.win.document.head.querySelector('script[nonce]');
if (currentScript) {
scriptElement.setAttribute('nonce', currentScript.getAttribute('nonce'));
}
// Allow error information to be collected
// https://github.com/ampproject/amphtml/issues/7353
scriptElement.setAttribute('crossorigin', 'anonymous');
let loc = this.win.location;
if (getMode(this.win).test && this.win.testLocation) {
loc = this.win.testLocation;
}
const scriptSrc = calculateExtensionScriptUrl(
loc,
extensionId,
opt_extensionVersion,
getMode(this.win).localDev
);
scriptElement.src = scriptSrc;
return scriptElement;
}
/**
* @param {string} extensionId
* @return {string}
* @private
*/
attributeForExtension_(extensionId) {
return isTemplateExtension(extensionId)
? 'custom-template'
: 'custom-element';
}
}
/**
* @param {!Window} win
*/
export function stubLegacyElements(win) {
LEGACY_ELEMENTS.forEach((name) => {
stubElementIfNotKnown(win, name);
});
}
/**
* @return {!Object}
*/
function emptyService() {
// All services need to resolve to an object.
return {};
}