* 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)
| 286 | * @export |
| 287 | */ |
| 288 | function 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 | } |
| 315 | exports.getInstance = getInstance; |
| 316 | |
| 317 | /** |
no test coverage detected