fixed badges. small improvements

This commit is contained in:
Juanra Dikal 2022-10-03 17:35:35 +02:00
parent 438d9bd9f3
commit 59cd071971
34 changed files with 1129 additions and 3262 deletions

View File

@ -1,7 +1,7 @@
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)](CODE_OF_CONDUCT.md) [![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)](CODE_OF_CONDUCT.md)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
[![Node.js CI](https://github.com/juanelas/bigint-mod-arith/workflows/build/badge.svg)](https://github.com/juanelas/bigint-mod-arith/actions?query=workflow%3A%22build%22) [![Node.js CI](https://github.com/juanelas/bigint-mod-arith/actions/workflows/nodejs.yml/badge.svg)](https://github.com/juanelas/bigint-mod-arith/actions/workflows/nodejs.yml)
[![Coverage Status](https://coveralls.io/repos/github/juanelas/bigint-mod-arith/badge.svg?branch=master)](https://coveralls.io/github/juanelas/bigint-mod-arith?branch=master) [![Coverage Status](https://coveralls.io/repos/github/juanelas/bigint-mod-arith/badge.svg?branch=master)](https://coveralls.io/github/juanelas/bigint-mod-arith?branch=master)
# bigint-mod-arith # bigint-mod-arith
@ -24,7 +24,7 @@ Then either require (Node.js CJS):
const bigintModArith = require('bigint-mod-arith') const bigintModArith = require('bigint-mod-arith')
``` ```
> **Node >=10.4 <11**. `bigint-mod-arith` uses workers to speed up some operations. Workers are enabled by default with Node.js from version 11. In order to use them with Node >=10.4 and <11, you need to execute node with the flag `--experimental-worker`, and require the .js file manually (otherwise .cjs is required by default and would not be supported by the workers) > **Node >=10.4 <11**. `bigint-mod-arith` uses workers to speed up some operations. Workers are enabled by default with Node.js from version 11. In order to use them with Node >=10.4 and <11, you need to execute node with the flag `--experimental-worker`, and require the `.js` file manually (otherwise `.cjs` is required by default and would not be supported by the workers)
> >
> ```javascript > ```javascript
> const bigintCryptoUtils = require('bigint-crypto-utils/dist/cjs/index.node') // ONLY FOR node >=10.4 <11 ! > const bigintCryptoUtils = require('bigint-crypto-utils/dist/cjs/index.node') // ONLY FOR node >=10.4 <11 !

View File

@ -1,75 +1,166 @@
#! /usr/bin/env node #! /usr/bin/env node
const fs = require('fs')
const path = require('path') const path = require('path')
const childProcess = require('child_process') const glob = require('glob')
const minimatch = require('minimatch')
const rimraf = require('rimraf')
const runScript = require('../run-script.cjs')
const rootDir = path.join(__dirname, '../..') const rootDir = path.join(__dirname, '../..')
const mochaTsRelativeDir = '.mocha-ts'
const minimatch = require('minimatch')
const glob = require('glob')
// First let us prepare the args to pass to mocha. const pkgJson = require(path.join(rootDir, 'package.json'))
// ts.files will be replaced by their js-transpiled counterparts
// a watch file to our semaphore will be added
const processedArgs = processArgs(process.argv.slice(2))
// Now we can run a script and invoke a callback when complete, e.g. const mochaTsRelativeDir = pkgJson.directories['mocha-ts']
runScript(path.join(rootDir, 'node_modules/mocha/bin/mocha'), processedArgs) const mochaTsDir = path.join(rootDir, mochaTsRelativeDir)
// clean .mocha-ts directory
rimraf.sync(mochaTsDir)
const semaphorePath = `${mochaTsRelativeDir}/semaphore`
const tempDir = mochaTsDir
fs.mkdirSync(tempDir, { recursive: true })
const usage = `Usage: mocha-ts [options] [spec]
mocha against ts tests and modules
Arguments:
spec One or more files, directories, or globs to test (default:
"{src/ts/**/*.spec.ts,test/**/*.ts}")
Options:
-w, --watch run in watch mode. Since mocha only supports CJS in watch
mode. This option implies -cjs as well (default: false)
-cjs, --commonjs run tests against the CJS bundle instead of the ESM one
(default: false)
-h, --help display help for command
`
function parse () {
const args = process.argv.slice(2)
const help = getBooleanOption(args, '--help', '-h')
if (help) {
console.log(usage)
process.exit()
}
const requiredFile = getOption(args, '--require')
const watch = getBooleanOption(args, '--watch', '-w')
const commonjs = getBooleanOption(args, '--commonjs', '-cjs')
if (commonjs === false && watch === true) {
console.log('ERROR: mocha in watch mode only supports commonjs')
console.log(usage)
process.exit(1)
}
let testsGlob = (args.pop() ?? '').replace(/^['"]/, '').replace(/['"]$/, '') // Let us remove surrounding quotes in string (it gives issues in windows)
if (testsGlob === '') {
testsGlob = '{src/ts/**/*.spec.ts,test/**/*.ts}'
}
const mochaArgs = []
if (requiredFile !== '') {
mochaArgs.push('--require')
mochaArgs.push(requiredFile)
}
mochaArgs.push('--require')
mochaArgs.push('build/testing/mocha/mocha-init.cjs')
if (watch) {
mochaArgs.push('-w')
mochaArgs.push('--watch-files')
mochaArgs.push(semaphorePath)
}
if (testsGlob.substring(0, 1) === '-') {
console.log(usage)
process.exit(9)
}
let filenames = []
try {
filenames = glob.sync(testsGlob, { cwd: rootDir, matchBase: true })
} catch (error) {}
if (filenames.length === 0) {
console.error('invalid or empty glob pattern: ' + testsGlob)
console.log()
console.log(usage)
process.exit(9)
}
const testFiles = []
const jsTestFiles = []
function processArgs (args) {
args = process.argv.slice(2).map(arg => {
// Let us first remove surrounding quotes in string (it gives issues in windows)
arg = arg.replace(/^['"]/, '').replace(/['"]$/, '')
const filenames = glob.sync(arg, { cwd: rootDir, matchBase: true })
if (filenames.length > 0) { if (filenames.length > 0) {
return filenames.map(file => { filenames.forEach(file => {
const isTsTestFile = minimatch(file, '{test/**/*.ts,src/**/*.spec.ts}', { matchBase: true }) const isTsTestFile = minimatch(file, '{test/**/*.ts,src/**/*.spec.ts}', { matchBase: true })
if (isTsTestFile) { if (isTsTestFile) {
return `${mochaTsRelativeDir}/${file.slice(0, -3)}.js` testFiles.push(file)
const extension = commonjs ? 'cjs' : 'js'
jsTestFiles.push(`${mochaTsRelativeDir}/${file.slice(0, -3)}.${extension}`)
} }
return file
}) })
} }
return arg mochaArgs.push(...jsTestFiles)
})
const processedArgs = [] return {
mochaArgs,
let addSemaphore = false testFiles,
let semaphoreAdded = false commonjs
for (const arg of args) {
if (Array.isArray(arg)) {
processedArgs.push(...arg)
} else {
processedArgs.push(arg)
if (arg === '--watch' || arg === '-w') {
addSemaphore = true
} else if (arg === '--watch-files') {
processedArgs.push(`${mochaTsRelativeDir}/semaphore`)
semaphoreAdded = true
} }
}
}
if (addSemaphore === true || semaphoreAdded === false) {
processedArgs.push('--watch-files')
processedArgs.push(`${mochaTsRelativeDir}/semaphore`)
}
return processedArgs
} }
function runScript (scriptPath, args) { const processedArgs = parse()
const mochaCmd = childProcess.fork(scriptPath, args, { const commonjs = processedArgs.commonjs
cwd: rootDir const testFiles = processedArgs.testFiles
}) const mochaArgs = processedArgs.mochaArgs
mochaCmd.on('error', (error) => { // prepare setup for mocha (it should be written to a JSON file that will be loaded by the mocha-init.cjs)
throw error const mochaSetup = {
}) testFiles,
commonjs
}
fs.writeFileSync(path.join(tempDir, 'testSetup.json'), JSON.stringify(mochaSetup, undefined, 2), { encoding: 'utf-8' })
// execute the callback once the process has finished running if (commonjs) {
mochaCmd.on('exit', function (code) { console.log('\x1b[33m [mocha-ts] Running tests against the CommonJS module \x1b[0m\n')
if (code !== 0) { } else {
throw new Error('exit code ' + code) console.log('\x1b[33m [mocha-ts] Running tests against the ESM module \x1b[0m\n')
}
const rollupBuilder = require('../testing/mocha/builders/RollupBuilder.cjs').rollupBuilder
rollupBuilder.start({ commonjs, watch: false }).then(() => {
rollupBuilder.close()
const testsBuilder = require('../testing/mocha/builders/TestsBuilder.cjs').testBuilder
testsBuilder.start({ commonjs, testFiles }).then(() => {
testsBuilder.close()
// Now run mocha
runScript(path.join(rootDir, 'node_modules/mocha/bin/mocha'), mochaArgs)
})
})
function getBooleanOption (args, ...optionNames) {
let found = false
optionNames.forEach((option) => {
const index = args.indexOf(option)
if (index > -1) {
found = true
args.splice(index, 1)
} }
}) })
return found
}
function getOption (args, option) {
const index = args.indexOf(option)
if (index > -1 && index < args.length - 2) {
return args.splice(index, 2)[1]
}
return ''
} }

View File

@ -3,7 +3,9 @@
const fs = require('fs') const fs = require('fs')
const TypeDoc = require('typedoc') const TypeDoc = require('typedoc')
const path = require('path') const path = require('path')
const json5 = require('json5')
const pkgJson = require('../package.json') const pkgJson = require('../package.json')
const rimraf = require('rimraf')
const rootDir = path.join(__dirname, '..') const rootDir = path.join(__dirname, '..')
@ -14,15 +16,25 @@ function camelise (str) {
}) })
} }
const tsConfigPath = path.join(rootDir, 'tsconfig.json')
const tempTsConfigPath = path.join(rootDir, '.tsconfig.json')
async function typedoc () { async function typedoc () {
const app = new TypeDoc.Application() const app = new TypeDoc.Application()
// prepare tsconfig
const tsConfig = json5.parse(fs.readFileSync(tsConfigPath, 'utf8'))
tsConfig.include = ['src/ts/**/*', 'build/typings/**/*.d.ts']
tsConfig.exclude = ['src/**/*.spec.ts']
fs.writeFileSync(tempTsConfigPath, JSON.stringify(tsConfig, undefined, 2))
// If you want TypeDoc to load tsconfig.json / typedoc.json files // If you want TypeDoc to load tsconfig.json / typedoc.json files
app.options.addReader(new TypeDoc.TSConfigReader()) app.options.addReader(new TypeDoc.TSConfigReader())
app.options.addReader(new TypeDoc.TypeDocReader()) app.options.addReader(new TypeDoc.TypeDocReader())
app.bootstrap({ app.bootstrap({
// typedoc options here // typedoc options here
tsconfig: tempTsConfigPath,
entryPoints: ['src/ts/index.ts'], entryPoints: ['src/ts/index.ts'],
plugin: ['typedoc-plugin-markdown'], plugin: ['typedoc-plugin-markdown'],
includeVersion: true, includeVersion: true,
@ -84,7 +96,7 @@ if (repoProvider) {
iifeBundle = `[IIFE bundle](https://raw.githubusercontent.com/${repoUsername}/${repoName}/master/${iifeBundlePath})` iifeBundle = `[IIFE bundle](https://raw.githubusercontent.com/${repoUsername}/${repoName}/master/${iifeBundlePath})`
esmBundle = `[ESM bundle](https://raw.githubusercontent.com/${repoUsername}/${repoName}/master/${esmBundlePath})` esmBundle = `[ESM bundle](https://raw.githubusercontent.com/${repoUsername}/${repoName}/master/${esmBundlePath})`
umdBundle = `[UMD bundle](https://raw.githubusercontent.com/${repoUsername}/${repoName}/master/${umdBundlePath})` umdBundle = `[UMD bundle](https://raw.githubusercontent.com/${repoUsername}/${repoName}/master/${umdBundlePath})`
workflowBadget = `[![Node.js CI](https://github.com/${repoUsername}/${repoName}/workflows/build/badge.svg)](https://github.com/${repoUsername}/${repoName}/actions?query=workflow%3A%22build%22)` workflowBadget = `[![Node.js CI](https://github.com/${repoUsername}/${repoName}/actions/workflows/nodejs.yml/badge.svg)](https://github.com/${repoUsername}/${repoName}/actions/workflows/nodejs.yml)`
coverallsBadge = `[![Coverage Status](https://coveralls.io/repos/github/${repoUsername}/${repoName}/badge.svg?branch=master)](https://coveralls.io/github/${repoUsername}/${repoName}?branch=master)` coverallsBadge = `[![Coverage Status](https://coveralls.io/repos/github/${repoUsername}/${repoName}/badge.svg?branch=master)](https://coveralls.io/github/${repoUsername}/${repoName}?branch=master)`
break break
@ -117,3 +129,5 @@ const readmeFile = path.join(rootDir, 'README.md')
fs.writeFileSync(readmeFile, template) fs.writeFileSync(readmeFile, template)
typedoc() typedoc()
rimraf.sync(tempTsConfigPath)

View File

@ -8,8 +8,8 @@ import commonjs from '@rollup/plugin-commonjs'
import json from '@rollup/plugin-json' import json from '@rollup/plugin-json'
import { join } from 'path' import { join } from 'path'
import { existsSync } from 'fs-extra' import { existsSync } from 'fs'
import { directories, name as _name, dependencies, peerDependencies, exports } from '../package.json' import { directories, name as _name, exports } from '../package.json'
import { compile } from './rollup-plugin-dts.js' import { compile } from './rollup-plugin-dts.js'
const rootDir = join(__dirname, '..') const rootDir = join(__dirname, '..')
@ -37,8 +37,6 @@ const tsBundleOptions = {
exclude: ['src/**/*.spec.ts'] exclude: ['src/**/*.spec.ts']
} }
const external = [...Object.keys(dependencies || {}), ...Object.keys(peerDependencies || {})]
const sourcemapOutputOptions = { const sourcemapOutputOptions = {
sourcemap: 'inline', sourcemap: 'inline',
sourcemapExcludeSources: true sourcemapExcludeSources: true
@ -54,83 +52,9 @@ function compileDts () {
} }
export default [ export default [
{ // ESM for browsers and declarations { // Node ESM with declarations
input: input, input: input,
output: [ output: [
{
file: join(rootDir, exports['.'].default),
...sourcemapOutputOptions,
format: 'es'
}
],
plugins: [
replace({
IS_BROWSER: true,
preventAssignment: true
}),
typescriptPlugin(tsBundleOptions),
compileDts(),
json()
],
external
},
{ // Browser bundles
input: input,
output: [
{
file: join(dstDir, 'bundles/iife.js'),
format: 'iife',
name: pkgCamelisedName,
plugins: [terser()]
},
{
file: join(dstDir, 'bundles/esm.js'),
...sourcemapOutputOptions,
format: 'es'
},
{
file: join(dstDir, 'bundles/esm.min.js'),
format: 'es',
plugins: [terser()]
},
{
file: join(dstDir, 'bundles/umd.js'),
format: 'umd',
name: pkgCamelisedName,
plugins: [terser()]
}
],
plugins: [
replace({
IS_BROWSER: true,
preventAssignment: true
}),
typescriptPlugin(tsBundleOptions),
resolve({
browser: true,
exportConditions: ['browser', 'default'],
mainFields: ['browser', 'main']
}),
commonjs({
// namedExports: {
// 'bn.js': ['BN'],
// 'hash.js': ['hmac', 'ripemd160', 'sha256', 'sha512'],
// elliptic: ['ec'],
// 'scrypt-js': ['scrypt', 'syncScrypt']
// }
}),
json()
]
},
{ // Node
input: input,
output: [
{
file: join(rootDir, exports['.'].node.require),
...sourcemapOutputOptions,
format: 'cjs',
exports: 'auto'
},
{ {
file: join(rootDir, exports['.'].node.import), file: join(rootDir, exports['.'].node.import),
...sourcemapOutputOptions, ...sourcemapOutputOptions,
@ -143,9 +67,111 @@ export default [
preventAssignment: true preventAssignment: true
}), }),
typescriptPlugin(tsBundleOptions), typescriptPlugin(tsBundleOptions),
compileDts(),
// resolve({
// browser: false,
// exportConditions: ['node']
// }),
commonjs({ extensions: ['.js', '.cjs', '.ts', '.jsx', '.cjsx', '.tsx'] }), // the ".ts" extension is required commonjs({ extensions: ['.js', '.cjs', '.ts', '.jsx', '.cjsx', '.tsx'] }), // the ".ts" extension is required
json() json()
]
},
{ // Node CJS
input: input,
output: [
{
file: join(rootDir, exports['.'].node.require),
...sourcemapOutputOptions,
format: 'cjs',
exports: 'auto'
}
], ],
external plugins: [
// replace({
// 'await import(': 'require(',
// delimiters: ['', ''],
// preventAssignment: true
// }),
replace({
IS_BROWSER: false,
preventAssignment: true
}),
typescriptPlugin(tsBundleOptions),
// resolve({
// browser: false,
// exportConditions: ['node']
// }),
commonjs({ extensions: ['.js', '.cjs', '.ts', '.jsx', '.cjsx', '.tsx'] }), // the ".ts" extension is required
json()
]
},
{ // ESM for browsers
input: input,
output: [
{
file: join(rootDir, exports['.'].default),
...sourcemapOutputOptions,
format: 'es'
},
{
file: join(dstDir, 'bundles/esm.js'),
...sourcemapOutputOptions,
format: 'es'
},
{
file: join(dstDir, 'bundles/esm.min.js'),
format: 'es',
plugins: [terser()]
}
],
plugins: [
replace({
IS_BROWSER: true,
preventAssignment: true
}),
typescriptPlugin(tsBundleOptions),
resolve({
browser: true,
exportConditions: ['browser', 'default']
}),
commonjs({ extensions: ['.js', '.cjs', '.ts', '.jsx', '.cjsx', '.tsx'] }), // the ".ts" extension is required
json()
]
},
{ // Browser bundles
input: input,
output: [
{
file: join(dstDir, 'bundles/iife.js'),
format: 'iife',
name: pkgCamelisedName,
plugins: [terser()]
},
{
file: join(dstDir, 'bundles/umd.js'),
format: 'umd',
name: pkgCamelisedName,
plugins: [terser()]
}
],
plugins: [
// replace({
// 'await import(': 'require(',
// delimiters: ['', ''],
// preventAssignment: true
// }),
replace({
IS_BROWSER: true,
preventAssignment: true
}),
typescriptPlugin(tsBundleOptions),
resolve({
browser: true,
exportConditions: ['browser', 'default'],
mainFields: ['browser', 'module', 'main']
}),
commonjs({ extensions: ['.js', '.cjs', '.ts', '.jsx', '.cjsx', '.tsx'] }), // the ".ts" extension is required
json()
]
} }
] ]

26
build/run-script.cjs Normal file
View File

@ -0,0 +1,26 @@
const childProcess = require('child_process')
const rootDir = require('path').join(__dirname, '../')
function runScript (scriptPath, args) {
return new Promise((resolve, reject) => {
const cmd = childProcess.fork(scriptPath, args, {
cwd: rootDir
})
cmd.on('error', (error) => {
reject(error)
})
// execute the callback once the process has finished running
cmd.on('exit', function (code) {
if (code !== 0) {
const error = new Error('exit code ' + code)
reject(error)
}
resolve()
})
})
}
module.exports = runScript

View File

@ -29,11 +29,11 @@ const browserTests = async (
} }
if (ignore) return if (ignore) return
let text = (message.args().length > 0) ? message.args()[0]._remoteObject.value : message.text() let text = (message.args().length > 0) ? message.args()[0].remoteObject().value : message.text()
const args = [] const args = []
if (message.args() !== undefined && message.args().length > 1) { if (message.args() !== undefined && message.args().length > 1) {
for (let i = 1; i < message.args().length; i++) { for (let i = 1; i < message.args().length; i++) {
args.push(message.args()[i]._remoteObject.value) args.push(message.args()[i].remoteObject().value)
} }
} }

View File

@ -13,6 +13,7 @@ const multi = require('@rollup/plugin-multi-entry')
const typescriptPlugin = require('@rollup/plugin-typescript') const typescriptPlugin = require('@rollup/plugin-typescript')
const commonjs = require('@rollup/plugin-commonjs') const commonjs = require('@rollup/plugin-commonjs')
const json = require('@rollup/plugin-json') const json = require('@rollup/plugin-json')
const runScript = require('../../run-script.cjs')
const rootDir = path.join(__dirname, '..', '..', '..') const rootDir = path.join(__dirname, '..', '..', '..')
@ -40,10 +41,6 @@ const indexHtml = `<!DOCTYPE html>
timeout: 90000 timeout: 90000
}) })
</script> </script>
<script type="module">
import * as _pkg from './${name}.esm.js'
self._pkg = _pkg
</script>
<script type="module"> <script type="module">
import './tests.js' import './tests.js'
window._mocha = mocha.run() window._mocha = mocha.run()
@ -62,6 +59,11 @@ async function buildTests (testFiles) {
input, input,
plugins: [ plugins: [
multi({ exports: true }), multi({ exports: true }),
replace({
'#pkg': `/${name}.esm.js`,
delimiters: ['', ''],
preventAssignment: true
}),
replace({ replace({
IS_BROWSER: true, IS_BROWSER: true,
preventAssignment: true preventAssignment: true
@ -69,12 +71,12 @@ async function buildTests (testFiles) {
typescriptPlugin(tsBundleOptions), typescriptPlugin(tsBundleOptions),
resolve({ resolve({
browser: true, browser: true,
exportConditions: ['browser', 'module', 'import', 'default'] exportConditions: ['browser', 'default']
}), }),
commonjs(), commonjs(),
json() json()
], ],
external: [pkgJson.name] external: [`/${name}.esm.js`]
} }
const bundle = await rollup.rollup(inputOptions) const bundle = await rollup.rollup(inputOptions)
const { output } = await bundle.generate({ format: 'esm' }) const { output } = await bundle.generate({ format: 'esm' })
@ -82,7 +84,8 @@ async function buildTests (testFiles) {
let bundledCode = output[0].code let bundledCode = output[0].code
const replacements = _getEnvVarsReplacements(bundledCode) const replacements = _getEnvVarsReplacements(bundledCode)
for (const replacement in replacements) { for (const replacement in replacements) {
bundledCode = bundledCode.replaceAll(replacement, replacements[replacement]) const regExp = new RegExp(replacement, 'g')
bundledCode = bundledCode.replace(regExp, replacements[replacement])
} }
return bundledCode return bundledCode
} }
@ -93,10 +96,15 @@ class TestServer {
} }
async init (testFiles) { async init (testFiles) {
/** Let us first check if the necessary files are built, and if not, build */
if (!fs.existsSync(pkgJson.exports['./esm-browser-bundle'])) {
await runScript(path.join(rootDir, 'node_modules', '.bin', 'rollup'), ['-c', 'build/rollup.config.js'])
}
const tests = await buildTests(testFiles) const tests = await buildTests(testFiles)
this.server.on('request', function (req, res) { this.server.on('request', function (req, res) {
if (req.url === `/${name}.esm.js`) { if (req.url === `/${name}.esm.js`) {
fs.readFile(path.join(rootDir, pkgJson.directories.dist, 'bundles/esm.js'), function (err, data) { fs.readFile(path.join(rootDir, pkgJson.exports['./esm-browser-bundle']), function (err, data) {
if (err) { if (err) {
res.writeHead(404) res.writeHead(404)
res.end(JSON.stringify(err)) res.end(JSON.stringify(err))

View File

@ -6,10 +6,13 @@ module.exports = class Builder extends EventEmitter {
constructor (semaphoreFile, name = 'builder') { constructor (semaphoreFile, name = 'builder') {
super() super()
this.name = name this.name = name
this.firstBuild = true
fs.mkdirSync(path.dirname(semaphoreFile), { recursive: true }) fs.mkdirSync(path.dirname(semaphoreFile), { recursive: true })
this.semaphoreFile = semaphoreFile this.semaphoreFile = semaphoreFile
if (!fs.existsSync(this.semaphoreFile)) {
fs.writeFileSync(this.semaphoreFile, '', { encoding: 'utf8' })
}
this._ready = false this._ready = false
this.on('message', (...message) => { this.on('message', (...message) => {
@ -24,12 +27,12 @@ module.exports = class Builder extends EventEmitter {
} }
}) })
this.on('ready', () => { this.on('ready', (updateSemaphore = true) => {
if (this.firstBuild === false) { const now = Date.now()
fs.writeFileSync(this.semaphoreFile, '', 'utf-8') if (updateSemaphore) {
} else { fs.utimesSync(this.semaphoreFile, now, now)
this.firstBuild = false
} }
this._ready = true this._ready = true
}) })

View File

@ -3,36 +3,49 @@ const fs = require('fs')
const path = require('path') const path = require('path')
const rollup = require('rollup') const rollup = require('rollup')
const loadAndParseConfigFile = require('rollup/dist/loadConfigFile.js') const loadAndParseConfigFile = require('rollup/dist/loadConfigFile')
const Builder = require('./Builder.cjs') const Builder = require('./Builder.cjs')
const rootDir = path.join(__dirname, '../../../../') const rootDir = path.join(__dirname, '../../../../')
const pkgJson = require(path.join(rootDir, 'package.json')) const pkgJson = require(path.join(rootDir, 'package.json'))
module.exports = class RollupBuilder extends Builder { const mochaTsRelativeDir = pkgJson.directories['mocha-ts']
constructor ({ name = 'rollup', configPath = path.join(rootDir, 'rollup.config.js'), tempDir = path.join(rootDir, '.mocha-ts'), watch = false }) { const mochaTsDir = path.join(rootDir, mochaTsRelativeDir)
class RollupBuilder extends Builder {
constructor ({ name, configPath, tempDir }) {
super(path.join(tempDir, 'semaphore'), name) super(path.join(tempDir, 'semaphore'), name)
this.tempDir = tempDir
this.configPath = configPath this.configPath = configPath
this.watch = watch this.firstBuild = true
} }
async start () { async start ({ watch = false, commonjs = false }) {
await super.start() await super.start()
this.watch = watch
this.commonjs = commonjs
this.watchedModule = commonjs ? pkgJson.exports['.'].node.require : pkgJson.exports['.'].node.import
const { options } = await loadAndParseConfigFile(this.configPath) const { options } = await loadAndParseConfigFile(this.configPath)
// Watch only the Node CJS module, that is the one we are going to use with mocha
// Instead of compiling all the outputs let us just take the one we are using with mocha (either cjs or esm)
const rollupOptions = options.filter(bundle => { const rollupOptions = options.filter(bundle => {
const file = (bundle.output[0].dir !== undefined) const file = (bundle.output[0].dir !== undefined)
? path.join(bundle.output[0].dir, bundle.output[0].entryFileNames) ? path.join(bundle.output[0].dir, bundle.output[0].entryFileNames)
: bundle.output[0].file : bundle.output[0].file
return file === path.join(rootDir, pkgJson.main) return file === path.join(rootDir, this.watchedModule)
})[0] })[0]
delete rollupOptions.output.pop() // remove the second output if (rollupOptions.output.length > 1) {
rollupOptions.output = rollupOptions.output[0]
}
this.builder = new RollupBundler(rollupOptions, this.watch) this.builder = new RollupBundler({ rollupOptions, watch: this.watch, watchedModule: this.watchedModule })
this.builder.on('event', event => { this.builder.on('event', event => {
let updateSemaphore = true
switch (event.code) { switch (event.code) {
case 'START': case 'START':
this.emit('busy') this.emit('busy')
@ -49,13 +62,23 @@ module.exports = class RollupBuilder extends Builder {
case 'END': case 'END':
if (event.result) event.result.close() if (event.result) event.result.close()
this.emit('ready')
// fs.mkdirSync(path.join(this.tempDir, path.dirname(this.watchedModule)), { recursive: true })
// // console.log(path.join(this.tempDir, path.dirname(this.watchedModule)))
// fs.copyFileSync(this.watchedModule, path.join(this.tempDir, this.watchedModule))
if (this.firstBuild) {
this.firstBuild = false
updateSemaphore = false
}
this.emit('ready', updateSemaphore)
break break
case 'ERROR': case 'ERROR':
if (event.result) event.result.close() if (event.result) event.result.close()
this.emit('error', event.error) this.emit('error', event.error)
fs.writeFileSync(path.join(rootDir, pkgJson.main), '', 'utf8') fs.writeFileSync(path.join(rootDir, this.watchedModule), '', 'utf8')
// fs.writeFileSync(path.join(this.tempDir, this.watchedModule), '', 'utf8')
this.emit('ready') this.emit('ready')
break break
@ -77,10 +100,11 @@ module.exports = class RollupBuilder extends Builder {
} }
class RollupBundler extends EventEmitter { class RollupBundler extends EventEmitter {
constructor (rollupOptions, watch = false) { constructor ({ rollupOptions, watchedModule, watch = false }) {
super() super()
this.rollupOptions = rollupOptions this.rollupOptions = rollupOptions
this.watch = watch this.watch = watch
this.watchedModule = watchedModule
} }
async start () { async start () {
@ -91,7 +115,7 @@ class RollupBundler extends EventEmitter {
this.emit('event', event) this.emit('event', event)
}) })
} else { } else {
if (fs.existsSync(path.join(rootDir, pkgJson.main)) === false) { if (!fs.existsSync(path.join(rootDir, this.watchedModule))) {
await this._bundle() await this._bundle()
} else { } else {
this.emit('event', { code: 'END', noBuild: true }) this.emit('event', { code: 'END', noBuild: true })
@ -121,3 +145,10 @@ class RollupBundler extends EventEmitter {
if (this.watcher !== undefined) this.watcher.close() if (this.watcher !== undefined) this.watcher.close()
} }
} }
exports.RollupBuilder = RollupBuilder
exports.rollupBuilder = new RollupBuilder({
name: 'rollup',
configPath: path.join(rootDir, pkgJson.directories.build, 'rollup.config.js'),
tempDir: mochaTsDir
})

View File

@ -2,12 +2,15 @@ const path = require('path')
const fs = require('fs') const fs = require('fs')
const ts = require('typescript') const ts = require('typescript')
const JSON5 = require('json5') const json5 = require('json5')
const Builder = require('./Builder.cjs') const Builder = require('./Builder.cjs')
const rootDir = path.join(__dirname, '../../../../') const rootDir = path.join(__dirname, '../../../../')
const mochaTsRelativeDir = '.mocha-ts'
const pkgJson = require(path.join(rootDir, 'package.json'))
const mochaTsRelativeDir = pkgJson.directories['mocha-ts']
const mochaTsDir = path.join(rootDir, mochaTsRelativeDir) const mochaTsDir = path.join(rootDir, mochaTsRelativeDir)
const formatHost = { const formatHost = {
@ -16,23 +19,65 @@ const formatHost = {
getNewLine: () => ts.sys.newLine getNewLine: () => ts.sys.newLine
} }
module.exports = class TestsBuilder extends Builder { function fileChecksum (filePath) {
constructor ({ name = 'tsc', configPath = path.join(rootDir, 'tsconfig.json'), tempDir = mochaTsDir }) { return require('crypto')
super(path.join(tempDir, 'semaphore'), name) .createHash('md5')
.update(fs.readFileSync(filePath, { encoding: 'utf-8' }), 'utf8')
.digest('hex')
}
if (fs.existsSync(configPath) !== true) throw new Error(`Couldn't find a tsconfig file at ${configPath}`) function renameJsToCjs (dir, fileList = []) {
const files = fs.readdirSync(dir)
files.forEach(file => {
if (fs.statSync(path.join(dir, file)).isDirectory()) {
fileList = renameJsToCjs(path.join(dir, file), fileList)
} else {
const match = file.match(/(.*)\.js$/)
if (match !== null) {
const filename = match[1]
fs.renameSync(path.join(dir, file), path.join(dir, `${filename}.cjs`))
}
}
})
}
class TestsBuilder extends Builder {
constructor ({ name, configPath, tempDir }) {
super(path.join(tempDir, 'semaphore'), name)
this.tempDir = tempDir this.tempDir = tempDir
const tsConfig = JSON5.parse(fs.readFileSync(configPath, 'utf8')) if (fs.existsSync(configPath) !== true) throw new Error(`Couldn't find a tsconfig file at ${configPath}`)
tsConfig.file = undefined this.tsConfigPath = configPath
// Exclude already transpiled files in src this.testFilesChecksums = {}
tsConfig.exclude = ['src/ts/**/!(*.spec).ts'] }
async start ({ testFiles = [], commonjs = false }) {
await super.start()
this.commonjs = commonjs
const tsConfig = json5.parse(fs.readFileSync(this.tsConfigPath, 'utf8'))
if (testFiles.length > 0) {
delete tsConfig.files
tsConfig.include = ['build/typings/**/*.d.ts'].concat(testFiles)
for (let i = 0; i < testFiles.length; i++) {
this.testFilesChecksums[testFiles[i]] = fileChecksum(testFiles[i])
}
} else {
tsConfig.include = ['build/typings/**/*.d.ts', 'test/**/*', 'src/ts/**/*.spec.ts']
}
tsConfig.exclude = ['src/ts/**/!(.spec).ts']
if (this.commonjs) {
tsConfig.compilerOptions.module = 'commonjs'
}
// "noResolve": true // "noResolve": true
tsConfig.compilerOptions.noResolve = false // tsConfig.compilerOptions.noResolve = true
// we don't need declaration files // we don't need declaration files
tsConfig.compilerOptions.declaration = false tsConfig.compilerOptions.declaration = false
@ -41,25 +86,24 @@ module.exports = class TestsBuilder extends Builder {
tsConfig.compilerOptions.noEmit = false tsConfig.compilerOptions.noEmit = false
// source mapping eases debuging // source mapping eases debuging
tsConfig.compilerOptions.sourceMap = true tsConfig.compilerOptions.inlineSourceMap = true
// This prevents SyntaxError: Cannot use import statement outside a module tsConfig.compilerOptions.rootDir = '.'
tsConfig.compilerOptions.module = 'commonjs'
// Removed typeroots (it causes issues) // Removed typeroots (it causes issues)
tsConfig.compilerOptions.typeRoots = undefined tsConfig.compilerOptions.typeRoots = undefined
tsConfig.compilerOptions.outDir = path.isAbsolute(tempDir) ? path.relative(rootDir, tempDir) : tempDir tsConfig.compilerOptions.outDir = path.isAbsolute(this.tempDir) ? path.relative(rootDir, this.tempDir) : this.tempDir
this.tempTsConfigPath = path.join(rootDir, '.tsconfig.json') this.tempTsConfigPath = path.join(rootDir, '.tsconfig.json')
fs.writeFileSync(this.tempTsConfigPath, JSON.stringify(tsConfig, undefined, 2)) fs.writeFileSync(this.tempTsConfigPath, JSON.stringify(tsConfig, undefined, 2), { encoding: 'utf-8' })
const createProgram = ts.createSemanticDiagnosticsBuilderProgram const createProgram = ts.createSemanticDiagnosticsBuilderProgram
const reportDiagnostic = (diagnostic) => { const reportDiagnostic = (diagnostic) => {
const filePath = path.relative(rootDir, diagnostic.file.fileName) const filePath = path.relative(rootDir, diagnostic.file.fileName)
const tranpiledJsPath = `${path.join(tempDir, filePath).slice(0, -3)}.js` const tranpiledJsPath = `${path.join(this.tempDir, filePath).slice(0, -3)}.js`
const errorLine = diagnostic.file.text.slice(0, diagnostic.start).split(/\r\n|\r|\n/).length const errorLine = diagnostic.file.text.slice(0, diagnostic.start).split(/\r\n|\r|\n/).length
if (fs.existsSync(tranpiledJsPath)) { if (fs.existsSync(tranpiledJsPath)) {
fs.writeFileSync(tranpiledJsPath, '', 'utf8') fs.writeFileSync(tranpiledJsPath, '', 'utf8')
@ -69,7 +113,19 @@ module.exports = class TestsBuilder extends Builder {
const reportWatchStatusChanged = (diagnostic, newLine, options, errorCount) => { const reportWatchStatusChanged = (diagnostic, newLine, options, errorCount) => {
if (errorCount !== undefined) { if (errorCount !== undefined) {
this.emit('ready') // only change semaphore if test files are modified
let updateSemaphore = false
for (let i = 0; i < testFiles.length; i++) {
const checksum = fileChecksum(testFiles[i])
if (this.testFilesChecksums[testFiles[i]] !== checksum) {
updateSemaphore = true
this.testFilesChecksums[testFiles[i]] = checksum
}
}
if (this.commonjs) {
renameJsToCjs(mochaTsDir)
}
this.emit('ready', updateSemaphore)
} else { } else {
this.emit('busy') this.emit('busy')
if (diagnostic.code === 6031) { if (diagnostic.code === 6031) {
@ -90,13 +146,11 @@ module.exports = class TestsBuilder extends Builder {
reportDiagnostic, reportDiagnostic,
reportWatchStatusChanged reportWatchStatusChanged
) )
}
async start () {
await super.start()
// `createWatchProgram` creates an initial program, watches files, and updates // `createWatchProgram` creates an initial program, watches files, and updates
// the program over time. // the program over time.
this.watcher = ts.createWatchProgram(this.host) this.watcher = ts.createWatchProgram(this.host)
this.watcher.getProgram()
return await this.ready() return await this.ready()
} }
@ -106,3 +160,10 @@ module.exports = class TestsBuilder extends Builder {
fs.unlinkSync(this.tempTsConfigPath) fs.unlinkSync(this.tempTsConfigPath)
} }
} }
exports.TestsBuilder = TestsBuilder
exports.testBuilder = new TestsBuilder({
name: 'tsc',
configPath: path.join(rootDir, 'tsconfig.json'),
tempDir: mochaTsDir
})

View File

@ -1,76 +1,66 @@
'use strict' 'use strict'
const fs = require('fs')
const path = require('path') const path = require('path')
const chai = require('chai') const chai = require('chai')
const rimraf = require('rimraf') const rimraf = require('rimraf')
const RollupBuilder = require('./builders/RollupBuilder.cjs')
const TestsBuilder = require('./builders/TestsBuilder.cjs')
require('dotenv').config() require('dotenv').config()
const rollupBuilder = require('./builders/RollupBuilder.cjs').rollupBuilder
const testsBuilder = require('./builders/TestsBuilder.cjs').testBuilder
const rootDir = path.join(__dirname, '../../../') const rootDir = path.join(__dirname, '../../../')
const pkgJson = require(path.join(rootDir, 'package.json'))
const mochaTsRelativeDir = pkgJson.directories['mocha-ts']
const tempDir = path.join(rootDir, mochaTsRelativeDir)
global.chai = chai global.chai = chai
loadPkgToGlobal()
global.IS_BROWSER = false global.IS_BROWSER = false
const watch = process.argv.includes('--watch') || process.argv.includes('-w') const watch = process.argv.includes('--watch') || process.argv.includes('-w')
const tempDir = path.join(rootDir, '.mocha-ts') const setup = JSON.parse(fs.readFileSync(path.join(tempDir, 'testSetup.json'), 'utf-8'))
const rollupBuilder = new RollupBuilder({ name: 'rollup', configPath: path.join(rootDir, 'build/rollup.config.js'), tempDir, watch }) const testFiles = setup.testFiles
const testBuilder = new TestsBuilder({ name: 'tsc', tempDir }) let commonjs = setup.commonjs
rollupBuilder.start() // This should be in exports.mochaGlobalSetup but mocha fails when not in watch mode (DIRT...) commonjs = watch ? true : commonjs // mocha in watch mode only supports commonjs
testBuilder.start() // This should be in exports.mochaGlobalSetup but mocha fails when not in watch mode (DIRT...)
exports.mochaGlobalSetup = async function () {
if (watch) {
await rollupBuilder.start({ commonjs, watch })
testsBuilder.start({ testFiles, commonjs })
}
}
exports.mochaHooks = { exports.mochaHooks = {
beforeAll: [ beforeAll: [
async function () { async function () {
this.timeout('120000') this.timeout('120000')
await Promise.all([rollupBuilder.ready(), testBuilder.ready()]) if (watch) {
await Promise.all([rollupBuilder.ready(), testsBuilder.ready()])
// Just in case our module had been modified. Reload it when the tests are repeated (for mocha watch mode). // reset any transpiled module (just delete the cache so it is fully reloaded)
delete require.cache[require.resolve(rootDir)]
loadPkgToGlobal()
// And now reset any other transpiled module (just delete the cache so it is fully reloaded)
for (const key in require.cache) { for (const key in require.cache) {
const relativePath = path.relative(rootDir, key) const relativePath = path.relative(rootDir, key)
if (relativePath.startsWith(`.mocha-ts${path.sep}`)) { if (relativePath.startsWith(`.mocha-ts${path.sep}`) || relativePath.startsWith(`dist${path.sep}`)) {
delete require.cache[key] delete require.cache[key]
} }
} }
} }
}
] ]
} }
// exports.mochaGlobalSetup = async function () {
// await rollupBuilder.start()
// await testBuilder.start()
// }
exports.mochaGlobalTeardown = async function () { exports.mochaGlobalTeardown = async function () {
await testBuilder.close() if (watch) {
await testsBuilder.close()
await rollupBuilder.close() await rollupBuilder.close()
}
// I use the sync version of rimraf precisely because it blocks the // I use the sync version of rimraf precisely because it blocks the
// main thread and thus the mocha watcher, which otherwise would complain // main thread and thus the mocha watcher, which otherwise would complain
// about files being deleted // about files being deleted
rimraf.sync(tempDir, { disableGlob: true }) rimraf.sync(tempDir, { disableGlob: true })
} }
function loadPkgToGlobal () {
const _pkg = require(rootDir)
if (typeof _pkg === 'function') { // If it is just a default export, load it as named (for compatibility)
global._pkg = {
default: _pkg
}
} else {
global._pkg = _pkg
}
}

View File

@ -1,4 +0,0 @@
import * as _pkgModule from '../../src/ts/index'
export as namespace _pkg
export = _pkgModule

2
dist/bundles/esm.js vendored
View File

@ -10,7 +10,7 @@ function abs(a) {
} }
/** /**
* Returns the bitlength of a number * Returns the (minimum) length of a number expressed in bits.
* *
* @param a * @param a
* @returns The bit length * @returns The bit length

View File

@ -14,7 +14,7 @@ function abs(a) {
} }
/** /**
* Returns the bitlength of a number * Returns the (minimum) length of a number expressed in bits.
* *
* @param a * @param a
* @returns The bit length * @returns The bit length

View File

@ -10,7 +10,7 @@ function abs(a) {
} }
/** /**
* Returns the bitlength of a number * Returns the (minimum) length of a number expressed in bits.
* *
* @param a * @param a
* @returns The bit length * @returns The bit length

View File

@ -10,7 +10,7 @@ function abs(a) {
} }
/** /**
* Returns the bitlength of a number * Returns the (minimum) length of a number expressed in bits.
* *
* @param a * @param a
* @returns The bit length * @returns The bit length

View File

@ -43,7 +43,7 @@ The absolute value of a
#### Defined in #### Defined in
[abs.ts:8](https://github.com/juanelas/bigint-mod-arith/blob/f8f1fb2/src/ts/abs.ts#L8) [abs.ts:8](https://github.com/juanelas/bigint-mod-arith/blob/438d9bd/src/ts/abs.ts#L8)
___ ___
@ -51,7 +51,7 @@ ___
**bitLength**(`a`): `number` **bitLength**(`a`): `number`
Returns the bitlength of a number Returns the (minimum) length of a number expressed in bits.
#### Parameters #### Parameters
@ -67,7 +67,7 @@ The bit length
#### Defined in #### Defined in
[bitLength.ts:7](https://github.com/juanelas/bigint-mod-arith/blob/f8f1fb2/src/ts/bitLength.ts#L7) [bitLength.ts:7](https://github.com/juanelas/bigint-mod-arith/blob/438d9bd/src/ts/bitLength.ts#L7)
___ ___
@ -97,7 +97,7 @@ A triple (g, x, y), such that ax + by = g = gcd(a, b).
#### Defined in #### Defined in
[egcd.ts:17](https://github.com/juanelas/bigint-mod-arith/blob/f8f1fb2/src/ts/egcd.ts#L17) [egcd.ts:17](https://github.com/juanelas/bigint-mod-arith/blob/438d9bd/src/ts/egcd.ts#L17)
___ ___
@ -122,7 +122,7 @@ The greatest common divisor of a and b
#### Defined in #### Defined in
[gcd.ts:10](https://github.com/juanelas/bigint-mod-arith/blob/f8f1fb2/src/ts/gcd.ts#L10) [gcd.ts:10](https://github.com/juanelas/bigint-mod-arith/blob/438d9bd/src/ts/gcd.ts#L10)
___ ___
@ -147,7 +147,7 @@ The least common multiple of a and b
#### Defined in #### Defined in
[lcm.ts:10](https://github.com/juanelas/bigint-mod-arith/blob/f8f1fb2/src/ts/lcm.ts#L10) [lcm.ts:10](https://github.com/juanelas/bigint-mod-arith/blob/438d9bd/src/ts/lcm.ts#L10)
___ ___
@ -172,7 +172,7 @@ Maximum of numbers a and b
#### Defined in #### Defined in
[max.ts:9](https://github.com/juanelas/bigint-mod-arith/blob/f8f1fb2/src/ts/max.ts#L9) [max.ts:9](https://github.com/juanelas/bigint-mod-arith/blob/438d9bd/src/ts/max.ts#L9)
___ ___
@ -197,7 +197,7 @@ Minimum of numbers a and b
#### Defined in #### Defined in
[min.ts:9](https://github.com/juanelas/bigint-mod-arith/blob/f8f1fb2/src/ts/min.ts#L9) [min.ts:9](https://github.com/juanelas/bigint-mod-arith/blob/438d9bd/src/ts/min.ts#L9)
___ ___
@ -226,7 +226,7 @@ The inverse modulo n
#### Defined in #### Defined in
[modInv.ts:13](https://github.com/juanelas/bigint-mod-arith/blob/f8f1fb2/src/ts/modInv.ts#L13) [modInv.ts:13](https://github.com/juanelas/bigint-mod-arith/blob/438d9bd/src/ts/modInv.ts#L13)
___ ___
@ -256,7 +256,7 @@ b**e mod n
#### Defined in #### Defined in
[modPow.ts:15](https://github.com/juanelas/bigint-mod-arith/blob/f8f1fb2/src/ts/modPow.ts#L15) [modPow.ts:15](https://github.com/juanelas/bigint-mod-arith/blob/438d9bd/src/ts/modPow.ts#L15)
___ ___
@ -289,4 +289,4 @@ A bigint with the smallest positive representation of a modulo n
#### Defined in #### Defined in
[toZn.ts:14](https://github.com/juanelas/bigint-mod-arith/blob/f8f1fb2/src/ts/toZn.ts#L14) [toZn.ts:14](https://github.com/juanelas/bigint-mod-arith/blob/438d9bd/src/ts/toZn.ts#L14)

View File

@ -16,7 +16,7 @@
#### Defined in #### Defined in
[egcd.ts:2](https://github.com/juanelas/bigint-mod-arith/blob/f8f1fb2/src/ts/egcd.ts#L2) [egcd.ts:2](https://github.com/juanelas/bigint-mod-arith/blob/438d9bd/src/ts/egcd.ts#L2)
___ ___
@ -26,7 +26,7 @@ ___
#### Defined in #### Defined in
[egcd.ts:3](https://github.com/juanelas/bigint-mod-arith/blob/f8f1fb2/src/ts/egcd.ts#L3) [egcd.ts:3](https://github.com/juanelas/bigint-mod-arith/blob/438d9bd/src/ts/egcd.ts#L3)
___ ___
@ -36,4 +36,4 @@ ___
#### Defined in #### Defined in
[egcd.ts:4](https://github.com/juanelas/bigint-mod-arith/blob/f8f1fb2/src/ts/egcd.ts#L4) [egcd.ts:4](https://github.com/juanelas/bigint-mod-arith/blob/438d9bd/src/ts/egcd.ts#L4)

3520
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -42,32 +42,44 @@
"./umd-browser-bundle": "./dist/bundles/umd.js", "./umd-browser-bundle": "./dist/bundles/umd.js",
"./types": "./types/index.d.ts" "./types": "./types/index.d.ts"
}, },
"imports": {
"#pkg": {
"import": "./dist/esm/index.node.js",
"require": "./dist/cjs/index.node.cjs"
}
},
"directories": { "directories": {
"build": "./build", "build": "./build",
"dist": "./dist", "dist": "./dist",
"docs": "./docs", "docs": "./docs",
"src": "./src", "src": "./src",
"test": "./test" "test": "./test",
"benchmark": "./benchmark",
"mocha-ts": "./.mocha-ts"
}, },
"scripts": { "scripts": {
"build": "run-s lint build:js docs", "build": "run-s lint build:js docs",
"build:js": "rollup -c build/rollup.config.js", "build:js": "rollup -c build/rollup.config.js",
"clean": "rimraf .nyc_output .mocha-ts coverage dist types docs", "clean": "rimraf .mocha-ts coverage dist types docs",
"coverage": "nyc --check-coverage --exclude build --exclude '{src/ts/**/*.spec.ts,test/**/*.ts,.mocha-ts/**/*}' --reporter=text --reporter=lcov node ./build/bin/mocha-ts.cjs --require build/testing/mocha/mocha-init.cjs '{src/ts/**/*.spec.ts,test/**/*.ts}'", "coverage": "c8 --check-coverage --exclude '{src/ts/**/*.spec.ts,test,test-vectors,build}' --exclude-after-remap --reporter=text --reporter=lcov node ./build/bin/mocha-ts.cjs --commonjs ",
"docs": "node build/build.docs.cjs", "docs": "node build/build.docs.cjs",
"git:add": "git add -A", "git:add": "git add -A",
"lint": "ts-standard --fix", "lint": "ts-standard --fix",
"mocha-ts": "node ./build/bin/mocha-ts.cjs --require build/testing/mocha/mocha-init.cjs ", "mocha-ts": "node --experimental-modules --es-module-specifier-resolution=node ./build/bin/mocha-ts.cjs ",
"mocha-ts:cjs": "node ./build/bin/mocha-ts.cjs --commonjs ",
"mocha-ts:watch": "npm run mocha-ts:cjs -- --watch ",
"mocha-ts:browser": "node build/testing/browser/index.cjs ", "mocha-ts:browser": "node build/testing/browser/index.cjs ",
"mocha-ts:browser-headless": "node build/testing/browser/index.cjs headless ", "mocha-ts:browser-headless": "node build/testing/browser/index.cjs headless ",
"preversion": "run-s clean lint build:js coverage test:browser-headless", "preversion": "run-s clean lint build:js coverage test:browser-headless",
"version": "run-s docs git:add", "version": "run-s docs git:add",
"postversion": "git push --follow-tags", "postversion": "git push --follow-tags",
"test": "run-s test:browser-headless test:node", "test": "run-s test:node test:browser-headless",
"test:browser": "npm run mocha-ts:browser ", "test:browser": "npm run mocha-ts:browser",
"test:browser-headless": "npm run mocha-ts:browser-headless ", "test:browser-headless": "npm run mocha-ts:browser-headless",
"test:node": "npm run mocha-ts -- '{src/ts/**/*.spec.ts,test/**/*.ts}'", "test:node": "run-s test:node-cjs test:node-esm",
"watch": "npm run mocha-ts:node -- --watch '{src/ts/**/*.spec.ts,test/**/*.ts}'" "test:node-cjs": "npm run mocha-ts:cjs ",
"test:node-esm": "npm run mocha-ts ",
"watch": "npm run mocha-ts:watch "
}, },
"ts-standard": { "ts-standard": {
"env": [ "env": [
@ -77,36 +89,35 @@
"IS_BROWSER", "IS_BROWSER",
"browser", "browser",
"page", "page",
"_pkg",
"chai" "chai"
], ],
"project": "./tsconfig.json", "project": "tsconfig.json",
"ignore": [ "ignore": [
"dist/**/*", "dist/**/*",
"examples/**/*",
"types/**/*", "types/**/*",
"build/testing/types/**/*" "build/testing/mocha/mocha-init.js"
] ]
}, },
"devDependencies": { "devDependencies": {
"@rollup/plugin-commonjs": "^22.0.0", "@rollup/plugin-commonjs": "^22.0.0",
"@rollup/plugin-json": "^4.1.0", "@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-multi-entry": "^4.0.0", "@rollup/plugin-multi-entry": "^4.0.0",
"@rollup/plugin-node-resolve": "^13.0.5", "@rollup/plugin-node-resolve": "^14.1.0",
"@rollup/plugin-replace": "^4.0.0", "@rollup/plugin-replace": "^4.0.0",
"@rollup/plugin-typescript": "^8.2.0", "@rollup/plugin-typescript": "^8.2.0",
"@types/chai": "^4.2.22", "@types/chai": "^4.2.22",
"@types/mocha": "^9.0.0", "@types/mocha": "^10.0.0",
"c8": "^7.12.0",
"chai": "^4.3.3", "chai": "^4.3.3",
"dotenv": "^16.0.0", "dotenv": "^16.0.3",
"fs-extra": "^10.0.0",
"glob": "^8.0.1", "glob": "^8.0.1",
"json5": "^2.2.0", "json5": "^2.2.0",
"minimatch": "^5.0.1", "minimatch": "^5.0.1",
"mocha": "^10.0.0", "mocha": "^10.0.0",
"npm-run-all": "^4.1.5", "npm-run-all": "^4.1.5",
"nyc": "^15.1.0",
"pirates": "^4.0.1", "pirates": "^4.0.1",
"puppeteer": "^15.5.0", "puppeteer": "^18.0.5",
"rimraf": "^3.0.2", "rimraf": "^3.0.2",
"rollup": "^2.57.0", "rollup": "^2.57.0",
"rollup-plugin-terser": "^7.0.2", "rollup-plugin-terser": "^7.0.2",

View File

@ -23,7 +23,7 @@ Then either require (Node.js CJS):
const {{PKG_CAMELCASE}} = require('{{PKG_NAME}}') const {{PKG_CAMELCASE}} = require('{{PKG_NAME}}')
``` ```
> **Node >=10.4 <11**. `{{PKG_NAME}}` uses workers to speed up some operations. Workers are enabled by default with Node.js from version 11. In order to use them with Node >=10.4 and <11, you need to execute node with the flag `--experimental-worker`, and require the .js file manually (otherwise .cjs is required by default and would not be supported by the workers) > **Node >=10.4 <11**. `{{PKG_NAME}}` uses workers to speed up some operations. Workers are enabled by default with Node.js from version 11. In order to use them with Node >=10.4 and <11, you need to execute node with the flag `--experimental-worker`, and require the `.js` file manually (otherwise `.cjs` is required by default and would not be supported by the workers)
> >
> ```javascript > ```javascript
> const bigintCryptoUtils = require('bigint-crypto-utils/dist/cjs/index.node') // ONLY FOR node >=10.4 <11 ! > const bigintCryptoUtils = require('bigint-crypto-utils/dist/cjs/index.node') // ONLY FOR node >=10.4 <11 !
@ -52,11 +52,11 @@ const a = BigInt('5')
const b = BigInt('2') const b = BigInt('2')
const n = 19n const n = 19n
console.log(bigintModArith.modPow(a, b, n)) // prints 6 console.log({{PKG_CAMELCASE}}.modPow(a, b, n)) // prints 6
console.log(bigintModArith.modInv(2n, 5n)) // prints 3 console.log({{PKG_CAMELCASE}}.modInv(2n, 5n)) // prints 3
console.log(bigintModArith.modInv(BigInt('3'), BigInt('5'))) // prints 2 console.log({{PKG_CAMELCASE}}.modInv(BigInt('3'), BigInt('5'))) // prints 2
``` ```
## API reference documentation ## API reference documentation

View File

@ -1,5 +1,5 @@
/** /**
* Returns the bitlength of a number * Returns the (minimum) length of a number expressed in bits.
* *
* @param a * @param a
* @returns The bit length * @returns The bit length

View File

@ -1,3 +1,5 @@
import * as bma from '#pkg'
describe('abs', function () { describe('abs', function () {
const inputs = [ const inputs = [
{ {
@ -20,7 +22,7 @@ describe('abs', function () {
for (const input of inputs) { for (const input of inputs) {
describe(`abs(${input.value})`, function () { describe(`abs(${input.value})`, function () {
it(`should return ${input.abs}`, function () { it(`should return ${input.abs}`, function () {
const ret = _pkg.abs(input.value) const ret = bma.abs(input.value)
chai.expect(ret).to.equal(input.abs) chai.expect(ret).to.equal(input.abs)
}) })
}) })

View File

@ -1,3 +1,5 @@
import * as bma from '#pkg'
describe('bitLength', function () { describe('bitLength', function () {
const inputs = [ const inputs = [
{ {
@ -21,7 +23,7 @@ describe('bitLength', function () {
for (const input of inputs) { for (const input of inputs) {
describe(`bitLength(${input.value})`, function () { describe(`bitLength(${input.value})`, function () {
it(`should return ${input.bitLength}`, function () { it(`should return ${input.bitLength}`, function () {
const ret = _pkg.bitLength(input.value) const ret = bma.bitLength(input.value)
chai.expect(ret).to.equal(input.bitLength) chai.expect(ret).to.equal(input.bitLength)
}) })
}) })

View File

@ -1,3 +1,5 @@
import * as bma from '#pkg'
describe('egcd', function () { describe('egcd', function () {
const inputs = [ const inputs = [
{ {
@ -31,7 +33,7 @@ describe('egcd', function () {
for (const input of inputs) { for (const input of inputs) {
describe(`eGcd(${input.a}, ${input.b})`, function () { describe(`eGcd(${input.a}, ${input.b})`, function () {
it('should return the egcd', function () { it('should return the egcd', function () {
const ret = _pkg.eGcd(input.a, input.b) const ret = bma.eGcd(input.a, input.b)
chai.expect(ret).to.eql(input.egcd) chai.expect(ret).to.eql(input.egcd)
}) })
}) })

View File

@ -1,3 +1,5 @@
import * as bma from '#pkg'
describe('gcd', function () { describe('gcd', function () {
const inputs = [ const inputs = [
{ {
@ -49,7 +51,7 @@ describe('gcd', function () {
for (const input of inputs) { for (const input of inputs) {
describe(`gcd(${input.a}, ${input.b})`, function () { describe(`gcd(${input.a}, ${input.b})`, function () {
it(`should return ${input.gcd}`, function () { it(`should return ${input.gcd}`, function () {
const ret = _pkg.gcd(input.a, input.b) const ret = bma.gcd(input.a, input.b)
chai.expect(ret).to.equal(input.gcd) chai.expect(ret).to.equal(input.gcd)
}) })
}) })

View File

@ -1,3 +1,5 @@
import * as bma from '#pkg'
describe('lcm', function () { describe('lcm', function () {
const inputs = [ const inputs = [
{ {
@ -34,7 +36,7 @@ describe('lcm', function () {
for (const input of inputs) { for (const input of inputs) {
describe(`lcm(${input.a}, ${input.b})`, function () { describe(`lcm(${input.a}, ${input.b})`, function () {
it(`should return ${input.lcm}`, function () { it(`should return ${input.lcm}`, function () {
const ret = _pkg.lcm(input.a, input.b) const ret = bma.lcm(input.a, input.b)
chai.expect(ret).to.equal(input.lcm) chai.expect(ret).to.equal(input.lcm)
}) })
}) })

View File

@ -1,3 +1,5 @@
import * as bma from '#pkg'
describe('max', function () { describe('max', function () {
const inputs = [ const inputs = [
{ {
@ -76,7 +78,7 @@ describe('max', function () {
for (const input of inputs) { for (const input of inputs) {
describe(`max(${input.value[0]}, ${input.value[1]})`, function () { describe(`max(${input.value[0]}, ${input.value[1]})`, function () {
it(`should return ${input.max}`, function () { it(`should return ${input.max}`, function () {
const ret = _pkg.max(input.value[0], input.value[1]) const ret = bma.max(input.value[0], input.value[1])
chai.expect(ret).to.equal(input.max) chai.expect(ret).to.equal(input.max)
}) })
}) })

View File

@ -1,3 +1,5 @@
import * as bma from '#pkg'
describe('min', function () { describe('min', function () {
const inputs = [ const inputs = [
{ {
@ -76,7 +78,7 @@ describe('min', function () {
for (const input of inputs) { for (const input of inputs) {
describe(`min(${input.value[0]}, ${input.value[1]})`, function () { describe(`min(${input.value[0]}, ${input.value[1]})`, function () {
it(`should return ${input.min}`, function () { it(`should return ${input.min}`, function () {
const ret = _pkg.min(input.value[0], input.value[1]) const ret = bma.min(input.value[0], input.value[1])
chai.expect(ret).to.equal(input.min) chai.expect(ret).to.equal(input.min)
}) })
}) })

View File

@ -1,3 +1,5 @@
import * as bma from '#pkg'
describe('modInv', function () { describe('modInv', function () {
const inputs = [ const inputs = [
{ {
@ -33,7 +35,7 @@ describe('modInv', function () {
for (const input of inputs) { for (const input of inputs) {
describe(`modInv(${input.a}, ${input.n})`, function () { describe(`modInv(${input.a}, ${input.n})`, function () {
it(`should return ${input.modInv}`, function () { it(`should return ${input.modInv}`, function () {
const ret = _pkg.modInv(input.a, input.n) const ret = bma.modInv(input.a, input.n)
// chai.assert( String(ret) === String(input.modInv) ); // chai.assert( String(ret) === String(input.modInv) );
chai.expect(String(ret)).to.be.equal(String(input.modInv)) chai.expect(String(ret)).to.be.equal(String(input.modInv))
}) })
@ -43,7 +45,7 @@ describe('modInv', function () {
describe(`modInv(${input.a}, ${input.n})`, function () { describe(`modInv(${input.a}, ${input.n})`, function () {
it('should throw RangeError', function () { it('should throw RangeError', function () {
try { try {
_pkg.modInv(input.a, input.n) bma.modInv(input.a, input.n)
throw new Error('should have failed') throw new Error('should have failed')
} catch (err) { } catch (err) {
chai.expect(err).to.be.instanceOf(RangeError) chai.expect(err).to.be.instanceOf(RangeError)

View File

@ -1,3 +1,5 @@
import * as bma from '#pkg'
describe('modPow', function () { describe('modPow', function () {
const inputs = [ const inputs = [
{ {
@ -43,7 +45,7 @@ describe('modPow', function () {
for (const input of inputs) { for (const input of inputs) {
describe(`modPow(${input.a}, ${input.b}, ${input.n})`, function () { describe(`modPow(${input.a}, ${input.b}, ${input.n})`, function () {
it(`should return ${input.modPow}`, function () { it(`should return ${input.modPow}`, function () {
const ret = _pkg.modPow(input.a, input.b, input.n) const ret = bma.modPow(input.a, input.b, input.n)
chai.expect(String(ret)).to.equal(String(input.modPow)) chai.expect(String(ret)).to.equal(String(input.modPow))
}) })
}) })
@ -52,7 +54,7 @@ describe('modPow', function () {
describe(`modPow(${input.a}, ${input.b}, ${input.n})`, function () { describe(`modPow(${input.a}, ${input.b}, ${input.n})`, function () {
it('should throw RangeError', function () { it('should throw RangeError', function () {
try { try {
_pkg.modPow(input.a, input.b, input.n) bma.modPow(input.a, input.b, input.n)
throw new Error('should have failed') throw new Error('should have failed')
} catch (err) { } catch (err) {
chai.expect(err).to.be.instanceOf(RangeError) chai.expect(err).to.be.instanceOf(RangeError)
@ -67,7 +69,7 @@ describe('modPow', function () {
const b = BigInt('313632271690673451924314047671460131678794095260951233878123501752357966284491455239133687519908410656818506813151659324961829045286402303082891913186909806785080978448037486178337722667190743610785429936585699831407575170854873682955317589189564880931807976657385223632835801016017549762825562427694700595') const b = BigInt('313632271690673451924314047671460131678794095260951233878123501752357966284491455239133687519908410656818506813151659324961829045286402303082891913186909806785080978448037486178337722667190743610785429936585699831407575170854873682955317589189564880931807976657385223632835801016017549762825562427694700595')
const e = BigInt('452149997592306202232720864363485824701879487303880767747217308770351197801836846325633986474037061753983278534192061455638289551714281047915315943771002615269860312318606105460307037327329178890486613832051027105330475852552183444938408408863970975090778239473049899109989825645608770309107015209564444316') const e = BigInt('452149997592306202232720864363485824701879487303880767747217308770351197801836846325633986474037061753983278534192061455638289551714281047915315943771002615269860312318606105460307037327329178890486613832051027105330475852552183444938408408863970975090778239473049899109989825645608770309107015209564444316')
while (iterations > 0) { while (iterations > 0) {
_pkg.modPow(b, e, p) bma.modPow(b, e, p)
iterations-- iterations--
} }
}) })

View File

@ -1,3 +1,5 @@
import * as bma from '#pkg'
describe('toZn', function () { describe('toZn', function () {
const inputs = [ const inputs = [
{ {
@ -26,7 +28,7 @@ describe('toZn', function () {
for (const input of inputs) { for (const input of inputs) {
describe(`toZn(${input.a}, ${input.n})`, function () { describe(`toZn(${input.a}, ${input.n})`, function () {
it(`should return ${input.toZn}`, function () { it(`should return ${input.toZn}`, function () {
const ret = _pkg.toZn(input.a, input.n) const ret = bma.toZn(input.a, input.n)
chai.expect(ret).to.equal(input.toZn) chai.expect(ret).to.equal(input.toZn)
}) })
}) })
@ -35,7 +37,7 @@ describe('toZn', function () {
describe(`toZn(${input.a}, ${input.n})`, function () { describe(`toZn(${input.a}, ${input.n})`, function () {
it('should throw RangeError', function () { it('should throw RangeError', function () {
try { try {
_pkg.toZn(input.a, input.n) bma.toZn(input.a, input.n)
throw new Error('should have failed') throw new Error('should have failed')
} catch (err) { } catch (err) {
chai.expect(err).to.be.instanceOf(RangeError) chai.expect(err).to.be.instanceOf(RangeError)

View File

@ -2,6 +2,7 @@
"$schema": "https://json.schemastore.org/tsconfig", "$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": { "compilerOptions": {
"target": "ES2020", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ "target": "ES2020", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "ESNext",
// "lib": [ "es2020" ], /* Specify library files to be included in the compilation. */ // "lib": [ "es2020" ], /* Specify library files to be included in the compilation. */
"allowJs": true, /* Allow javascript files to be compiled. */ "allowJs": true, /* Allow javascript files to be compiled. */
"outDir": ".dst", /* If not set we cannot import .js files without a warning that is going to be overwritten. outDir is not going to be used in any case */ "outDir": ".dst", /* If not set we cannot import .js files without a warning that is going to be overwritten. outDir is not going to be used in any case */
@ -35,7 +36,10 @@
/* Advanced Options */ /* Advanced Options */
"skipLibCheck": true, /* Skip type checking of declaration files. */ "skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file. */ "forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file. */
"resolveJsonModule": true "resolveJsonModule": true,
"paths": {
"#pkg": ["."]
}
}, },
"include": ["src/ts/**/*", "test/**/*", "build/typings/**/*.d.ts"] "include": ["src/ts/**/*", "build/typings/**/*.d.ts", "test/**/*", "test-vectors/**/*.ts", "benchmark/**/*.ts"]
} }

View File

@ -1,5 +1,5 @@
/** /**
* Returns the bitlength of a number * Returns the (minimum) length of a number expressed in bits.
* *
* @param a * @param a
* @returns The bit length * @returns The bit length