MCPcopy Index your code
hub / github.com/ampproject/amphtml / getInstance

Function getInstance

validator/js/nodejs/index.js:288–314  ·  view source on GitHub ↗

* Provided a URL or a filename from which to fetch the validator_wasm.js * file, fetches, instantiates, and caches the validator instance * asynchronously. If you prefer to implement your own fetching / * caching logic, you may want to consider newInstance() instead, * which is synchronous and

(opt_validatorJs, opt_userAgent)

Source from the content-addressed store, hash-verified

286 * @export
287 */
288function getInstance(opt_validatorJs, opt_userAgent) {
289 const validatorJs =
290 opt_validatorJs || 'https://cdn.ampproject.org/v0/validator_wasm.js';
291 const userAgent = opt_userAgent || DEFAULT_USER_AGENT;
292 if (instanceByValidatorJs.hasOwnProperty(validatorJs)) {
293 return Promise.resolve(instanceByValidatorJs[validatorJs]);
294 }
295 const validatorJsPromise = isHttpOrHttpsUrl(validatorJs) ?
296 readFromUrl(validatorJs, userAgent) :
297 readFromFile(validatorJs);
298 return validatorJsPromise.then(function(scriptContents) {
299 let instance;
300 try {
301 instance = new Validator(scriptContents);
302 } catch (error) {
303 // It may be useful to cache errors and exceptions encountered
304 // here, but for now we don't do this for e.g. http errors when
305 // fetching the validator, so we shouldn't do it for syntax
306 // errors etc. either (which lead to the varructor throwing an error).
307 throw error;
308 }
309 instanceByValidatorJs[validatorJs] = instance;
310 return instance;
311 }).then(function(instance) {
312 return instance.init().then(() => instance);
313 });
314}
315exports.getInstance = getInstance;
316
317/**

Callers 1

mainFunction · 0.85

Calls 6

isHttpOrHttpsUrlFunction · 0.85
readFromUrlFunction · 0.85
readFromFileFunction · 0.85
resolveMethod · 0.80
thenMethod · 0.45
initMethod · 0.45

Tested by

no test coverage detected