-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Remove default Node.js support
BREAKING CHANGE: Default Node.js support is removed. It is hard to use different APIs in different environments right in today's bundled world. Closes #116.
- Loading branch information
Showing
10 changed files
with
236 additions
and
402 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
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,54 @@ | ||
import { EntropyProvider } from './entropyProvider'; | ||
import { UnsignedTypedArray } from './unsignedTypedArray'; | ||
|
||
export class BrowserEntropyProvider implements EntropyProvider { | ||
/** | ||
* The crypto implementation used in the browser. The window.crypto object is used. | ||
*/ | ||
private readonly crypto?: Crypto; | ||
|
||
/** | ||
* According to the Web Crypto standard, there is a 2 ** 16 bytes quota for requesting entropy at once. | ||
*/ | ||
private static readonly BROWSER_ENTROPY_QUOTA_BYTES = 65536; | ||
|
||
public constructor() { | ||
if ( | ||
typeof window === 'undefined' || | ||
typeof window.crypto === 'undefined' || | ||
typeof window.crypto.getRandomValues === 'undefined' | ||
) { | ||
throw new Error('window.crypto.getRandomValues is not available'); | ||
} | ||
|
||
this.crypto = window.crypto; | ||
} | ||
|
||
/** | ||
* Puts random values into the given @param array, and returns the array. | ||
* If the array's length is greater than the general @member BROWSER_ENTROPY_QUOTA_BYTES, | ||
* it is divided into chunks, and filled chunk-by-chunk. | ||
*/ | ||
public getRandomValues<T extends UnsignedTypedArray>(array: T): T { | ||
if (this.crypto === undefined) { | ||
throw new Error('AssertError: no crypto'); | ||
} | ||
|
||
if (array.byteLength <= BrowserEntropyProvider.BROWSER_ENTROPY_QUOTA_BYTES) { | ||
return this.crypto.getRandomValues(array); | ||
} | ||
|
||
let remainingBytes = array.byteLength; | ||
while (remainingBytes > 0) { | ||
const availableEntropyBytes = Math.min(remainingBytes, BrowserEntropyProvider.BROWSER_ENTROPY_QUOTA_BYTES); | ||
const chunkStart = array.byteLength - remainingBytes; | ||
const chunkLength = availableEntropyBytes / array.BYTES_PER_ELEMENT; | ||
const chunkEnd = chunkStart + chunkLength; | ||
const chunkToFill = array.subarray(chunkStart, chunkEnd); | ||
this.crypto.getRandomValues(chunkToFill); | ||
remainingBytes -= availableEntropyBytes; | ||
} | ||
|
||
return array; | ||
} | ||
} |
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,5 +1,5 @@ | ||
import { UnsignedTypedArray } from './unsignedTypedArray'; | ||
|
||
export interface EntropyProvider { | ||
getRandomValues<T extends UnsignedTypedArray>(array: T): Promise<T>; | ||
getRandomValues<T extends UnsignedTypedArray>(array: T): T | Promise<T>; | ||
} |
This file was deleted.
Oops, something went wrong.
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,96 @@ | ||
import { expect } from 'chai'; | ||
import { beforeEach } from 'mocha'; | ||
import { BrowserEntropyProvider } from '../../src/browserEntropyProvider'; | ||
import { EntropyProvider } from '../../src/entropyProvider'; | ||
import { windowMock } from '../utils/windowMock'; | ||
|
||
describe('BrowserEntropyProvider', () => { | ||
let entropyProvider: EntropyProvider; | ||
|
||
before(() => { | ||
// @ts-ignore | ||
global.window = windowMock(); | ||
}); | ||
|
||
after(() => { | ||
// @ts-ignore | ||
global.window = undefined; | ||
}); | ||
|
||
beforeEach(() => { | ||
entropyProvider = new BrowserEntropyProvider(); | ||
}); | ||
|
||
it('should work', async () => { | ||
const array = new Uint8Array(10); | ||
expect(array.every(v => v === 0)).to.be.true; | ||
const sameArray = await entropyProvider.getRandomValues(array); | ||
expect(array).to.deep.equal(sameArray); | ||
expect(array.some(v => v !== 0)).to.be.true; | ||
}); | ||
|
||
it('should work for arrays larger than 65536 bytes', async () => { | ||
const array = new Uint8Array(100000); | ||
expect(array.every(v => v === 0)).to.be.true; | ||
const sameArray = await entropyProvider.getRandomValues(array); | ||
expect(array).to.deep.equal(sameArray); | ||
expect(array.some(v => v !== 0)).to.be.true; | ||
expect(array.subarray(80000, 90000).some(v => v !== 0)).to.be.true; | ||
}); | ||
|
||
it('should throw if window is not available', () => { | ||
// @ts-ignore | ||
global.window = undefined; | ||
|
||
try { | ||
new BrowserEntropyProvider(); | ||
expect.fail('did not throw'); | ||
} catch (e) { | ||
expect(e.message).to.equal('window.crypto.getRandomValues is not available'); | ||
} | ||
|
||
// @ts-ignore | ||
global.window = windowMock(); | ||
}); | ||
|
||
it('should throw if window.crypto is not available', () => { | ||
// @ts-ignore | ||
global.window.crypto = undefined; | ||
|
||
try { | ||
new BrowserEntropyProvider(); | ||
expect.fail('did not throw'); | ||
} catch (e) { | ||
expect(e.message).to.equal('window.crypto.getRandomValues is not available'); | ||
} | ||
|
||
// @ts-ignore | ||
global.window = windowMock(); | ||
}); | ||
|
||
it('should throw if window.crypto.getRandomValues is not available', () => { | ||
// @ts-ignore | ||
global.window.crypto.getRandomValues = undefined; | ||
|
||
try { | ||
new BrowserEntropyProvider(); | ||
expect.fail('did not throw'); | ||
} catch (e) { | ||
expect(e.message).to.equal('window.crypto.getRandomValues is not available'); | ||
} | ||
|
||
// @ts-ignore | ||
global.window = windowMock(); | ||
}); | ||
|
||
it('should throw if browserCrypto is undefined', async () => { | ||
// @ts-ignore | ||
entropyProvider.crypto = undefined; | ||
|
||
try { | ||
await entropyProvider.getRandomValues(new Uint8Array(1)); | ||
} catch (e) { | ||
expect(e.message).to.equal('AssertError: no crypto'); | ||
} | ||
}); | ||
}); |
Oops, something went wrong.