browser defaults to a not minified bundle

This commit is contained in:
Juanra Dikal 2021-04-21 11:14:04 +02:00
parent d7f49502f5
commit 4322e3a862
13 changed files with 901 additions and 41 deletions

View File

@ -11,7 +11,6 @@ const fs = require('fs')
const pkgJson = require('../package.json') const pkgJson = require('../package.json')
const rootDir = path.join(__dirname, '..') const rootDir = path.join(__dirname, '..')
const dstDir = path.join(rootDir, pkgJson.directories.dist)
const srcDir = path.join(rootDir, 'src') const srcDir = path.join(rootDir, 'src')
function camelise (str) { function camelise (str) {
@ -40,20 +39,39 @@ const sourcemapOutputOptions = {
} }
module.exports = [ module.exports = [
{ // Browser ESM
input: input,
output: {
file: path.join(rootDir, pkgJson.browser),
format: 'es',
...sourcemapOutputOptions
},
plugins: [
replace({
IS_BROWSER: true,
preventAssignment: true
}),
typescriptPlugin(tsBundleOptions),
resolve({ // For the workers to properly load all the functions when minified (e.g. by webpack), bigint-mod-arith should be resolved
browser: true,
exportConditions: ['browser', 'module', 'import', 'default']
})
]
},
{ // Browser bundles { // Browser bundles
input: input, input: input,
output: [ output: [
{ {
file: path.join(dstDir, `bundles/${name}.iife.js`), file: path.join(rootDir, pkgJson.exports['./iife-browser-bundle']),
format: 'iife', format: 'iife',
name: pkgCamelisedName name: pkgCamelisedName
}, },
{ {
file: path.join(dstDir, `bundles/${name}.esm.js`), file: path.join(rootDir, pkgJson.exports['./esm-browser-bundle']),
format: 'es' format: 'es'
}, },
{ {
file: path.join(dstDir, `bundles/${name}.umd.js`), file: path.join(rootDir, pkgJson.exports['./umd-browser-bundle']),
format: 'umd', format: 'umd',
name: pkgCamelisedName name: pkgCamelisedName
} }
@ -110,6 +128,7 @@ module.exports = [
}), }),
typescriptPlugin(tsBundleOptions), typescriptPlugin(tsBundleOptions),
commonjs({ extensions: ['.js', '.ts'] }) // the ".ts" extension is required commonjs({ extensions: ['.js', '.ts'] }) // the ".ts" extension is required
] ],
external
} }
] ]

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

852
dist/esm/index.browser.js vendored Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -12,6 +12,6 @@
* @returns A promise that resolves to a boolean that is either true (a probably prime number) or false (definitely composite) * @returns A promise that resolves to a boolean that is either true (a probably prime number) or false (definitely composite)
*/ */
export declare function isProbablyPrime(w: number | bigint, iterations?: number, disableWorkers?: boolean): Promise<boolean>; export declare function isProbablyPrime(w: number | bigint, iterations?: number, disableWorkers?: boolean): Promise<boolean>;
export declare function _isProbablyPrime(w: bigint, iterations?: number): boolean; export declare function _isProbablyPrime(w: bigint, iterations: number): boolean;
export declare function _isProbablyPrimeWorkerUrl(): string; export declare function _isProbablyPrimeWorkerUrl(): string;
//# sourceMappingURL=isProbablyPrime.d.ts.map //# sourceMappingURL=isProbablyPrime.d.ts.map

View File

@ -1 +1 @@
{"version":3,"file":"isProbablyPrime.d.ts","sourceRoot":"","sources":["../../../../src/ts/isProbablyPrime.ts"],"names":[],"mappings":"AAOA;;;;;;;;;;;;GAYG;AACH,wBAAgB,eAAe,CAAE,CAAC,EAAE,MAAM,GAAC,MAAM,EAAE,UAAU,GAAE,MAAW,EAAE,cAAc,GAAE,OAAe,GAAG,OAAO,CAAC,OAAO,CAAC,CAqD7H;AAED,wBAAgB,gBAAgB,CAAE,CAAC,EAAE,MAAM,EAAE,UAAU,GAAE,MAAW,GAAG,OAAO,CA0T7E;AAED,wBAAgB,yBAAyB,IAAK,MAAM,CAkBnD"} {"version":3,"file":"isProbablyPrime.d.ts","sourceRoot":"","sources":["../../../../src/ts/isProbablyPrime.ts"],"names":[],"mappings":"AAOA;;;;;;;;;;;;GAYG;AACH,wBAAgB,eAAe,CAAE,CAAC,EAAE,MAAM,GAAC,MAAM,EAAE,UAAU,GAAE,MAAW,EAAE,cAAc,GAAE,OAAe,GAAG,OAAO,CAAC,OAAO,CAAC,CAqD7H;AAED,wBAAgB,gBAAgB,CAAE,CAAC,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CA0TxE;AAED,wBAAgB,yBAAyB,IAAK,MAAM,CAOnD"}

View File

@ -135,7 +135,7 @@ w MUST be >= 0
A promise that resolves to a boolean that is either true (a probably prime number) or false (definitely composite) A promise that resolves to a boolean that is either true (a probably prime number) or false (definitely composite)
Defined in: [src/ts/isProbablyPrime.ts:21](https://github.com/juanelas/bigint-crypto-utils/blob/8580d40/src/ts/isProbablyPrime.ts#L21) Defined in: [src/ts/isProbablyPrime.ts:21](https://github.com/juanelas/bigint-crypto-utils/blob/d7f4950/src/ts/isProbablyPrime.ts#L21)
___ ___
@ -275,7 +275,7 @@ bitLength MUST be > 0
A promise that resolves to a bigint probable prime of bitLength bits. A promise that resolves to a bigint probable prime of bitLength bits.
Defined in: [src/ts/prime.ts:21](https://github.com/juanelas/bigint-crypto-utils/blob/8580d40/src/ts/prime.ts#L21) Defined in: [src/ts/prime.ts:21](https://github.com/juanelas/bigint-crypto-utils/blob/d7f4950/src/ts/prime.ts#L21)
___ ___
@ -300,7 +300,7 @@ bitLength MUST be > 0
A bigint probable prime of bitLength bits. A bigint probable prime of bitLength bits.
Defined in: [src/ts/prime.ts:100](https://github.com/juanelas/bigint-crypto-utils/blob/8580d40/src/ts/prime.ts#L100) Defined in: [src/ts/prime.ts:100](https://github.com/juanelas/bigint-crypto-utils/blob/d7f4950/src/ts/prime.ts#L100)
___ ___
@ -324,7 +324,7 @@ Arguments MUST be: max > 0 && min >=0 && max > min
A cryptographically secure random bigint between [min,max] A cryptographically secure random bigint between [min,max]
Defined in: [src/ts/randBetween.ts:15](https://github.com/juanelas/bigint-crypto-utils/blob/8580d40/src/ts/randBetween.ts#L15) Defined in: [src/ts/randBetween.ts:15](https://github.com/juanelas/bigint-crypto-utils/blob/d7f4950/src/ts/randBetween.ts#L15)
___ ___
@ -348,7 +348,7 @@ bitLength MUST be > 0
A Promise that resolves to a UInt8Array/Buffer (Browser/Node.js) filled with cryptographically secure random bits A Promise that resolves to a UInt8Array/Buffer (Browser/Node.js) filled with cryptographically secure random bits
Defined in: [src/ts/randBits.ts:14](https://github.com/juanelas/bigint-crypto-utils/blob/8580d40/src/ts/randBits.ts#L14) Defined in: [src/ts/randBits.ts:14](https://github.com/juanelas/bigint-crypto-utils/blob/d7f4950/src/ts/randBits.ts#L14)
___ ___
@ -372,7 +372,7 @@ bitLength MUST be > 0
A Uint8Array/Buffer (Browser/Node.js) filled with cryptographically secure random bits A Uint8Array/Buffer (Browser/Node.js) filled with cryptographically secure random bits
Defined in: [src/ts/randBits.ts:45](https://github.com/juanelas/bigint-crypto-utils/blob/8580d40/src/ts/randBits.ts#L45) Defined in: [src/ts/randBits.ts:45](https://github.com/juanelas/bigint-crypto-utils/blob/d7f4950/src/ts/randBits.ts#L45)
___ ___
@ -396,7 +396,7 @@ byteLength MUST be > 0
A promise that resolves to a UInt8Array/Buffer (Browser/Node.js) filled with cryptographically secure random bytes A promise that resolves to a UInt8Array/Buffer (Browser/Node.js) filled with cryptographically secure random bytes
Defined in: [src/ts/randBytes.ts:12](https://github.com/juanelas/bigint-crypto-utils/blob/8580d40/src/ts/randBytes.ts#L12) Defined in: [src/ts/randBytes.ts:12](https://github.com/juanelas/bigint-crypto-utils/blob/d7f4950/src/ts/randBytes.ts#L12)
___ ___
@ -420,7 +420,7 @@ byteLength MUST be > 0
A UInt8Array/Buffer (Browser/Node.js) filled with cryptographically secure random bytes A UInt8Array/Buffer (Browser/Node.js) filled with cryptographically secure random bytes
Defined in: [src/ts/randBytes.ts:46](https://github.com/juanelas/bigint-crypto-utils/blob/8580d40/src/ts/randBytes.ts#L46) Defined in: [src/ts/randBytes.ts:46](https://github.com/juanelas/bigint-crypto-utils/blob/d7f4950/src/ts/randBytes.ts#L46)
___ ___

View File

@ -26,7 +26,7 @@
}, },
"types": "./dist/esm/types/index.d.ts", "types": "./dist/esm/types/index.d.ts",
"main": "./dist/cjs/index.node.cjs", "main": "./dist/cjs/index.node.cjs",
"browser": "./dist/bundles/bigint-crypto-utils.esm.js", "browser": "./dist/esm/index.browser.js",
"module": "./dist/esm/index.node.js", "module": "./dist/esm/index.node.js",
"exports": { "exports": {
".": { ".": {
@ -34,12 +34,12 @@
"require": "./dist/cjs/index.node.cjs", "require": "./dist/cjs/index.node.cjs",
"import": "./dist/esm/index.node.js" "import": "./dist/esm/index.node.js"
}, },
"default": "./dist/bundles/bigint-crypto-utils.esm.js" "default": "./dist/esm/index.browser.js"
}, },
"./esm-browser-bundle": "./dist/bundles/bigint-crypto-utils.esm.js", "./esm-browser-bundle": "./dist/bundles/bigint-crypto-utils.esm.js",
"./iife-browser-bundle": "./dist/bundles/bigint-crypto-utils.iife.js", "./iife-browser-bundle": "./dist/bundles/bigint-crypto-utils.iife.js",
"./umd-browser-bundle": "./dist/bundles/bigint-crypto-utils.umd.js", "./umd-browser-bundle": "./dist/bundles/bigint-crypto-utils.umd.js",
"./types/": "./dist/esm/types/" "./types": "./dist/esm/types/index.d.ts"
}, },
"directories": { "directories": {
"build": "./build", "build": "./build",

View File

@ -2,7 +2,7 @@ export function fromBuffer (buf: Uint8Array|Buffer): bigint {
let ret = 0n let ret = 0n
for (const i of buf.values()) { for (const i of buf.values()) {
const bi = BigInt(i) const bi = BigInt(i)
ret = (ret << BigInt(8)) + bi ret = (ret << 8n) + bi
} }
return ret return ret
} }

View File

@ -73,7 +73,7 @@ export function isProbablyPrime (w: number|bigint, iterations: number = 16, disa
} }
} }
export function _isProbablyPrime (w: bigint, iterations: number = 16): boolean { export function _isProbablyPrime (w: bigint, iterations: number): boolean {
/* /*
PREFILTERING. Even values but 2 are not primes, so don't test. PREFILTERING. Even values but 2 are not primes, so don't test.
1 is not a prime and the M-R algorithm needs w>1. 1 is not a prime and the M-R algorithm needs w>1.
@ -393,18 +393,7 @@ export function _isProbablyPrimeWorkerUrl (): string {
// Let's us first add all the required functions // Let's us first add all the required functions
let workerCode = `'use strict';const ${eGcd.name}=${eGcd.toString()};const ${modInv.name}=${modInv.toString()};const ${modPow.name}=${modPow.toString()};const ${toZn.name}=${toZn.toString()};const ${randBitsSync.name}=${randBitsSync.toString()};const ${randBytesSync.name}=${randBytesSync.toString()};const ${randBetween.name}=${randBetween.toString()};const ${isProbablyPrime.name}=${_isProbablyPrime.toString()};${bitLength.toString()};${fromBuffer.toString()};` let workerCode = `'use strict';const ${eGcd.name}=${eGcd.toString()};const ${modInv.name}=${modInv.toString()};const ${modPow.name}=${modPow.toString()};const ${toZn.name}=${toZn.toString()};const ${randBitsSync.name}=${randBitsSync.toString()};const ${randBytesSync.name}=${randBytesSync.toString()};const ${randBetween.name}=${randBetween.toString()};const ${isProbablyPrime.name}=${_isProbablyPrime.toString()};${bitLength.toString()};${fromBuffer.toString()};`
workerCode += ` workerCode += `onmessage=async function(e){const m={isPrime:await ${isProbablyPrime.name}(e.data.rnd,e.data.iterations),value:e.data.rnd,id:e.data.id};postMessage(m);}`
onmessage = async function (event) { // Let's start once we are called
// event.data = {rnd: <bigint>, iterations: <number>}
const isPrime = await ${isProbablyPrime.name}(event.data.rnd, event.data.iterations);
const msg = {
isPrime: isPrime,
value: event.data.rnd,
id: event.data.id
};
postMessage(msg);
}
`
return _workerUrl(workerCode) return _workerUrl(workerCode)
} }