Skip to content

Commit

Permalink
Use validator_wasm.js as default validator
Browse files Browse the repository at this point in the history
  • Loading branch information
antiphoton committed May 18, 2021
1 parent cfc5d28 commit fef13dd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
24 changes: 12 additions & 12 deletions validator/js/nodejs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ function ValidationError() {

/**
* The validator instance is a proxy object to a precompiled
* validator.js script - in practice the script was either downloaded
* from 'https://cdn.ampproject.org/v0/validator.js' or read from a
* validator_wasm.js script - in practice the script was either downloaded
* from 'https://cdn.ampproject.org/v0/validator_wasm.js' or read from a
* local file.
* @param {string} scriptContents
* @throws {!Error}
Expand All @@ -239,7 +239,8 @@ function Validator(scriptContents) {
try {
new vm.Script(scriptContents).runInContext(this.sandbox);
} catch (error) {
throw new Error('Could not instantiate validator.js - ' + error.message);
throw new Error('Could not instantiate validator_wasm.js - ' +
error.message);
}
}

Expand Down Expand Up @@ -292,7 +293,7 @@ Validator.prototype.validateString = function(inputString, htmlFormat) {
const instanceByValidatorJs = {};

/**
* Provided a URL or a filename from which to fetch the validator.js
* 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,
Expand All @@ -305,7 +306,7 @@ const instanceByValidatorJs = {};
*/
function getInstance(opt_validatorJs, opt_userAgent) {
const validatorJs =
opt_validatorJs || 'https://cdn.ampproject.org/v0/validator.js';
opt_validatorJs || 'https://cdn.ampproject.org/v0/validator_wasm.js';
const userAgent = opt_userAgent || DEFAULT_USER_AGENT;
if (instanceByValidatorJs.hasOwnProperty(validatorJs)) {
return Promise.resolve(instanceByValidatorJs[validatorJs]);
Expand All @@ -326,15 +327,17 @@ function getInstance(opt_validatorJs, opt_userAgent) {
}
instanceByValidatorJs[validatorJs] = instance;
return instance;
}).then(function(instance) {
return instance.init().then(() => instance);
});
}
exports.getInstance = getInstance;

/**
* Provided the contents of the validator.js file, e.g. as downloaded from
* 'https://cdn.ampproject.org/v0/validator.js', returns a new validator
* Provided the contents of the validator_wasm.js file, e.g. as downloaded from
* 'https://cdn.ampproject.org/v0/validator_wasm.js', returns a new validator
* instance. The tradeoff between this function and getInstance() is that this
* function is synchronous but requires the contents of the validator.js
* function is synchronous but requires the contents of the validator_wasm.js
* file as a parameter, while getInstance is asynchronous, fetches files
* from disk or the web, and caches them.
*
Expand Down Expand Up @@ -401,7 +404,7 @@ function main() {
' Latest published version by default, or\n' +
' dist/validator_minified.js (built with build.py)\n' +
' for development.',
'https://cdn.ampproject.org/v0/validator.js')
'https://cdn.ampproject.org/v0/validator_wasm.js')
.option(
'--user-agent <userAgent>', 'User agent string to use in requests.',
DEFAULT_USER_AGENT)
Expand Down Expand Up @@ -453,9 +456,6 @@ function main() {
}
}
getInstance(opts.validator_js, opts.userAgent)
.then(function(validator) {
return validator.init().then(() => validator);
})
.then(function(validator) {
Promise.all(inputs)
.then(function(resolvedInputs) {
Expand Down
4 changes: 2 additions & 2 deletions validator/js/nodejs/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const ampValidator = require('./index.js');

it('deployed validator rejects the empty file', function(done) {
// Note: This will fetch and use the validator from
// 'https://cdn.ampproject.org/v0/validator.js', since only one argument
// 'https://cdn.ampproject.org/v0/validator_wasm.js', since only one argument
// is supplied to validateString.
ampValidator.getInstance()
.then(function(instance) {
Expand Down Expand Up @@ -163,7 +163,7 @@ it('handles syntax errors in validator file', function(done) {
.catch(function(error) {
expect(error.message)
.toMatch(
/^Could not instantiate validator\.js -.*[Uu]nexpected token/);
/^Could not instantiate validator_wasm\.js -.*[Uu]nexpected token/);
done();
});
});
Expand Down

0 comments on commit fef13dd

Please sign in to comment.