This repository has been archived by the owner on Dec 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from epoberezkin/beta
1.0.0
- Loading branch information
Showing
7 changed files
with
130 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,8 @@ jspm_packages | |
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# browser bundles | ||
dist/ | ||
|
||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
language: node_js | ||
node_js: | ||
- "4" | ||
- "6" | ||
after_script: | ||
- coveralls < coverage/lcov.info |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,139 +1,33 @@ | ||
'use strict'; | ||
|
||
module.exports = function (ajv) { | ||
setupAsyncOpts(ajv._opts, true); | ||
return ajv; | ||
}; | ||
|
||
var ASYNC = { | ||
'*': checkGenerators, | ||
'co*': checkGenerators, | ||
'es7': checkAsyncFunction | ||
}; | ||
|
||
var TRANSPILE = { | ||
'nodent': getNodent, | ||
'regenerator': getRegenerator | ||
}; | ||
|
||
var MODES = [ | ||
{ async: 'es7' }, | ||
{ async: 'co*' }, | ||
{ async: 'es7', transpile: 'nodent' }, | ||
{ async: 'co*', transpile: 'regenerator' } | ||
]; | ||
|
||
|
||
var regenerator, nodent; | ||
|
||
var nodent = require('nodent')({ log: false, dontInstallRequireHook: true }); | ||
|
||
function setupAsyncOpts(opts, required) { | ||
var async = opts.async | ||
, transpile = opts.transpile | ||
, check; | ||
|
||
switch (typeof transpile) { | ||
case 'string': | ||
var get = TRANSPILE[transpile]; | ||
if (!get) throw new Error('bad transpiler: ' + transpile); | ||
return (opts.processCode = get(opts, required)); | ||
case 'undefined': | ||
case 'boolean': | ||
if (typeof async == 'string') { | ||
check = ASYNC[async]; | ||
if (!check) throw new Error('bad async mode: ' + async); | ||
return (opts.transpile = check(opts, required)); | ||
} | ||
|
||
for (var i=0; i<MODES.length; i++) { | ||
var _opts = MODES[i]; | ||
if (setupAsyncOpts(_opts, false)) { | ||
copy(_opts, opts); | ||
return opts.transpile; | ||
} | ||
} | ||
/* istanbul ignore next */ | ||
throw new Error('generators, nodent and regenerator are not available'); | ||
case 'function': | ||
return (opts.processCode = opts.transpile); | ||
default: | ||
throw new Error('bad transpiler: ' + transpile); | ||
} | ||
} | ||
|
||
|
||
function checkGenerators(opts, required) { | ||
/* jshint evil: true */ | ||
try { | ||
(new Function('(function*(){})()'))(); | ||
return true; | ||
} catch(e) { | ||
/* istanbul ignore next */ | ||
if (required) throw new Error('generators not supported'); | ||
module.exports = function (ajv) { | ||
var opts = ajv._opts; | ||
var t = opts.transpile; | ||
if (typeof t != 'boolean' && t !== undefined) | ||
throw new TypeError('transpile option must be boolean or undefined'); | ||
if (t) { | ||
opts.processCode = nodentTranspile; | ||
} else if (!checkAsync()) { | ||
if (t === false) throw new Error('async functions not supported'); | ||
opts.processCode = nodentTranspile; | ||
} | ||
} | ||
return ajv; | ||
}; | ||
|
||
|
||
function checkAsyncFunction(opts, required) { | ||
function checkAsync() { | ||
/* jshint evil: true */ | ||
try { | ||
(new Function('(async function(){})()'))(); | ||
/* istanbul ignore next */ | ||
return true; | ||
} catch(e) { | ||
if (required) throw new Error('es7 async functions not supported'); | ||
} | ||
} | ||
|
||
|
||
function getRegenerator(opts, required) { | ||
try { | ||
if (!regenerator) { | ||
var name = 'regenerator'; | ||
regenerator = require(name); | ||
regenerator.runtime(); | ||
} | ||
if (!opts.async || opts.async === true) | ||
opts.async = 'es7'; | ||
return regeneratorTranspile; | ||
} catch(e) { | ||
/* istanbul ignore next */ | ||
if (required) throw new Error('regenerator not available'); | ||
} | ||
} | ||
|
||
|
||
function regeneratorTranspile(code) { | ||
return regenerator.compile(code).code; | ||
} | ||
|
||
|
||
function getNodent(opts, required) { | ||
/* jshint evil: true */ | ||
try { | ||
if (!nodent) { | ||
var name = 'nodent'; | ||
nodent = require(name)({ log: false, dontInstallRequireHook: true }); | ||
} | ||
if (opts.async != 'es7') { | ||
if (opts.async && opts.async !== true) console.warn('nodent transpiles only es7 async functions'); | ||
opts.async = 'es7'; | ||
} | ||
return nodentTranspile; | ||
} catch(e) { | ||
/* istanbul ignore next */ | ||
if (required) throw new Error('nodent not available'); | ||
} | ||
} catch(e) {} | ||
} | ||
|
||
|
||
function nodentTranspile(code) { | ||
return nodent.compile(code, '', { promises: true, sourcemap: false }).code; | ||
} | ||
|
||
|
||
function copy(o, to) { | ||
to = to || {}; | ||
for (var key in o) to[key] = o[key]; | ||
return to; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/usr/bin/env sh | ||
|
||
set -e | ||
|
||
name=$(./scripts/info name) | ||
main=$(./scripts/info main) | ||
version=$(./scripts/info version) | ||
description=$(./scripts/info description) | ||
|
||
mkdir -p dist | ||
|
||
browserify -r "./$main:$name" \ | ||
-o "dist/$name.bundle.js" \ | ||
-s ajvAsync | ||
|
||
uglifyjs "dist/$name.bundle.js" -o "dist/$name.min.js" \ | ||
-c -m --source-map \ | ||
-b "beautify=false, preamble='/* $name $version: $description */'" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/usr/bin/env node | ||
|
||
'use strict'; | ||
|
||
var fs = require('fs'); | ||
var property = process.argv[2] || 'version'; | ||
var name = process.argv[3] || '.'; | ||
if (name != '.') name = 'node_modules/' + name; | ||
var json = JSON.parse(fs.readFileSync(name + '/package.json', 'utf8')); | ||
console.log(json[property]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict'; | ||
|
||
var Ajv = require('ajv'); | ||
var setupAsync = require('../index.js'); | ||
var assert = require('assert'); | ||
|
||
|
||
describe('transpile option', function() { | ||
var majorNodeVersion; | ||
if (typeof process != 'undefined') | ||
majorNodeVersion = +process.versions.node.split('.')[0]; | ||
|
||
it('should set processCode if async not supported', function() { | ||
var ajv = setupAsync(new Ajv); | ||
if (majorNodeVersion >= 7) | ||
assert.strictEqual(ajv._opts.processCode, undefined); | ||
else | ||
assert.equal(typeof ajv._opts.processCode, 'function'); | ||
}); | ||
|
||
it('should always set processCode if transpile: true', function() { | ||
var ajv = setupAsync(new Ajv({ transpile: true })); | ||
assert.equal(typeof ajv._opts.processCode, 'function'); | ||
}); | ||
|
||
it('should throw error if async not supported and transpile: false', function() { | ||
if (majorNodeVersion >= 7) assert.doesNotThrow(test); | ||
else assert.throws(test, /async functions not supported/); | ||
|
||
function test() { | ||
setupAsync(new Ajv({ transpile: false })); | ||
} | ||
}); | ||
|
||
it('should throw error with unknown transpile option', function() { | ||
test('nodent'); | ||
test({}); | ||
|
||
function test(transpile) { | ||
assert.throws(function() { | ||
setupAsync(new Ajv({ transpile: transpile })); | ||
}, /transpile option must be boolean or undefined/); | ||
} | ||
}); | ||
}); |