diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 36628bce..cc710854 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -112,15 +112,18 @@ jobs: path: dist key: dist-${{ hashFiles('src/**/*.ts') }}-${{ hashFiles('tsconfig/*.json') }}-${{ hashFiles('.github/workflows/*.yml') }}-${{ hashFiles('package.json') }} - run: find test -type f -name '*.mjs' -print0 | xargs -0 sed -i -e "s/await import(/require(/g" - if: ${{ startsWith(matrix.node-version, '12') }} - - name: Test Node.js crypto + if: ${{ startsWith(matrix.node-version, '12') }} + - name: Test with Node.js crypto module run: npm run test - - name: Test Node.js crypto w/ CryptoKey + - name: Test with Node.js crypto module w/ CryptoKey run: npm run test-cryptokey - if: ${{ !startsWith(matrix.node-version, '14') && !startsWith(matrix.node-version, '12') }} - - name: Test Web Cryptography API + if: ${{ !startsWith(matrix.node-version, '14') && !startsWith(matrix.node-version, '12') }} + - name: Test with Node.js Web Cryptography API run: npm run test-webcrypto - if: ${{ !startsWith(matrix.node-version, '14') && !startsWith(matrix.node-version, '12') }} + if: ${{ !startsWith(matrix.node-version, '14') && !startsWith(matrix.node-version, '12') }} + - name: Test with Node.js Web API + run: npm run test-webapi + if: ${{ matrix.experimental }} - run: git reset HEAD --hard test-deno: diff --git a/package.json b/package.json index dbdb58fe..51e3d3d1 100644 --- a/package.json +++ b/package.json @@ -69,6 +69,12 @@ "#dist/webcrypto": { "import": "./dist/node/webcrypto/esm/index.js", "require": "./dist/node/webcrypto/cjs/index.js" + }, + "#dist/webapi/*": { + "import": "./dist/browser/*.js" + }, + "#dist/webapi": { + "import": "./dist/browser/index.js" } }, "exports": { @@ -138,6 +144,7 @@ "test-deno": "deno test --reload --jobs --allow-net --allow-read test-deno", "test-browsers": "find test-browser -type f -name '*.js' -print0 | xargs -0 npx esbuild --log-level=warning --outdir=dist-browser-tests --bundle && karma start", "test-cryptokey": "CRYPTOKEY=true npm test", + "test-webapi": "WEBAPI=true NODE_OPTIONS='--experimental-fetch --experimental-global-webcrypto --no-warnings' npm test", "test-webcrypto": "WEBCRYPTO=true npm test", "prettier": "npx prettier --loglevel silent --write ./test ./src ./tools ./test-browser ./test-deno ./test-cloudflare-workers" }, diff --git a/test/dist.mjs b/test/dist.mjs new file mode 100644 index 00000000..ae6d719d --- /dev/null +++ b/test/dist.mjs @@ -0,0 +1,32 @@ +import test from 'ava' + +let root +let keyRoot + +if ('WEBCRYPTO' in process.env) { + root = keyRoot = '#dist/webcrypto' +} else if ('CRYPTOKEY' in process.env) { + root = '#dist' + keyRoot = '#dist/webcrypto' +} else if ('WEBAPI' in process.env) { + root = keyRoot = '#dist/webapi' +} else { + root = keyRoot = '#dist' +} + +export { root, keyRoot } + +export function conditional({ webcrypto = 1, electron = 1 } = {}) { + let run = test + if ( + !webcrypto && + ('WEBCRYPTO' in process.env || 'WEBAPI' in process.env || 'CRYPTOKEY' in process.env) + ) { + run = run.failing + } + + if (!electron && 'electron' in process.versions) { + run = run.failing + } + return run +} diff --git a/test/jwe/compact.decrypt.test.mjs b/test/jwe/compact.decrypt.test.mjs index 475d901c..e757db11 100644 --- a/test/jwe/compact.decrypt.test.mjs +++ b/test/jwe/compact.decrypt.test.mjs @@ -1,6 +1,6 @@ import test from 'ava' +import { root } from '../dist.mjs' -const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto' const { CompactEncrypt, compactDecrypt, base64url } = await import(root) test('JWE format validation', async (t) => { diff --git a/test/jwe/compact.encrypt.test.mjs b/test/jwe/compact.encrypt.test.mjs index 8cd0384a..1345a5c2 100644 --- a/test/jwe/compact.encrypt.test.mjs +++ b/test/jwe/compact.encrypt.test.mjs @@ -1,6 +1,6 @@ import test from 'ava' +import { root } from '../dist.mjs' -const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto' const { CompactEncrypt } = await import(root) test.before(async (t) => { diff --git a/test/jwe/cookbook.test.mjs b/test/jwe/cookbook.test.mjs index ea8892be..b902f793 100644 --- a/test/jwe/cookbook.test.mjs +++ b/test/jwe/cookbook.test.mjs @@ -1,16 +1,5 @@ import test from 'ava' - -let root -let keyRoot - -if ('WEBCRYPTO' in process.env) { - root = keyRoot = '#dist/webcrypto' -} else if ('CRYPTOKEY' in process.env) { - root = '#dist' - keyRoot = '#dist/webcrypto' -} else { - root = keyRoot = '#dist' -} +import { root, keyRoot } from '../dist.mjs' const { FlattenedEncrypt, flattenedDecrypt, CompactEncrypt, compactDecrypt, base64url } = await import(root) @@ -665,6 +654,7 @@ const vectors = [ { title: 'https://www.rfc-editor.org/rfc/rfc7520#section-5.9 - Compressed Content', webcrypto: true, + webapi: false, electron: false, reproducible: true, input: { @@ -917,7 +907,13 @@ const vectors = [ for (const vector of vectors) { let run = test - if (('WEBCRYPTO' in process.env || 'CRYPTOKEY' in process.env) && vector.webcrypto === false) { + if ( + ('WEBCRYPTO' in process.env || 'CRYPTOKEY' in process.env || 'WEBAPI' in process.env) && + vector.webcrypto === false + ) { + run = run.failing + } + if ('WEBAPI' in process.env && vector.webapi === false) { run = run.failing } if ('electron' in process.versions && vector.electron === false) { diff --git a/test/jwe/flattened.decrypt.test.mjs b/test/jwe/flattened.decrypt.test.mjs index 48417a54..a00350d5 100644 --- a/test/jwe/flattened.decrypt.test.mjs +++ b/test/jwe/flattened.decrypt.test.mjs @@ -1,7 +1,7 @@ import test from 'ava' import * as crypto from 'crypto' +import { root } from '../dist.mjs' -const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto' const { FlattenedEncrypt, flattenedDecrypt, base64url } = await import(root) test.before(async (t) => { diff --git a/test/jwe/flattened.encrypt.test.mjs b/test/jwe/flattened.encrypt.test.mjs index d6347a80..cc530e58 100644 --- a/test/jwe/flattened.encrypt.test.mjs +++ b/test/jwe/flattened.encrypt.test.mjs @@ -1,6 +1,6 @@ import test from 'ava' +import { root } from '../dist.mjs' -const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto' const { FlattenedEncrypt } = await import(root) test.before(async (t) => { diff --git a/test/jwe/general.test.mjs b/test/jwe/general.test.mjs index 8f0057ea..f0f87c02 100644 --- a/test/jwe/general.test.mjs +++ b/test/jwe/general.test.mjs @@ -1,7 +1,7 @@ import test from 'ava' import * as crypto from 'crypto' +import { root } from '../dist.mjs' -const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto' const { GeneralEncrypt, generalDecrypt, generateKeyPair, base64url } = await import(root) test.before(async (t) => { diff --git a/test/jwe/smoke.test.mjs b/test/jwe/smoke.test.mjs index 6087822e..bcf6cf8e 100644 --- a/test/jwe/smoke.test.mjs +++ b/test/jwe/smoke.test.mjs @@ -1,17 +1,6 @@ import test from 'ava' import * as crypto from 'crypto' - -let root -let keyRoot - -if ('WEBCRYPTO' in process.env) { - root = keyRoot = '#dist/webcrypto' -} else if ('CRYPTOKEY' in process.env) { - root = '#dist' - keyRoot = '#dist/webcrypto' -} else { - root = keyRoot = '#dist' -} +import { conditional, root, keyRoot } from '../dist.mjs' const { FlattenedEncrypt, @@ -311,18 +300,6 @@ test(smoke, 'oct256c') test(smoke, 'oct384c') test(smoke, 'oct512c') -function conditional({ webcrypto = 1, electron = 1 } = {}) { - let run = test - if (!webcrypto && ('WEBCRYPTO' in process.env || 'CRYPTOKEY' in process.env)) { - run = run.failing - } - - if (!electron && 'electron' in process.versions) { - run = run.failing - } - return run -} - conditional({ webcrypto: 0 })(smoke, 'rsa1_5') conditional({ webcrypto: 0, electron: 0 })(smoke, 'x25519kw') conditional({ webcrypto: 0 })(smoke, 'x25519dir') diff --git a/test/jwk/embedded.test.mjs b/test/jwk/embedded.test.mjs index af78049b..2e6ef6fc 100644 --- a/test/jwk/embedded.test.mjs +++ b/test/jwk/embedded.test.mjs @@ -1,16 +1,5 @@ import test from 'ava' - -let root -let keyRoot - -if ('WEBCRYPTO' in process.env) { - root = keyRoot = '#dist/webcrypto' -} else if ('CRYPTOKEY' in process.env) { - root = '#dist' - keyRoot = '#dist/webcrypto' -} else { - root = keyRoot = '#dist' -} +import { root, keyRoot } from '../dist.mjs' const { FlattenedSign, flattenedVerify } = await import(root) const { importJWK, EmbeddedJWK } = await import(keyRoot) diff --git a/test/jwk/jwk2key.test.mjs b/test/jwk/jwk2key.test.mjs index 7377547e..42c04d46 100644 --- a/test/jwk/jwk2key.test.mjs +++ b/test/jwk/jwk2key.test.mjs @@ -1,16 +1,5 @@ import test from 'ava' - -let root -let keyRoot - -if ('WEBCRYPTO' in process.env) { - root = keyRoot = '#dist/webcrypto' -} else if ('CRYPTOKEY' in process.env) { - root = '#dist' - keyRoot = '#dist/webcrypto' -} else { - root = keyRoot = '#dist' -} +import { conditional, keyRoot } from '../dist.mjs' const { importJWK, exportJWK } = await import(keyRoot) @@ -199,18 +188,6 @@ test('Uin8tArray can be transformed to a JWK', async (t) => { ) }) -function conditional({ webcrypto = 1, electron = 1 } = {}) { - let run = test - if (!webcrypto && ('WEBCRYPTO' in process.env || 'CRYPTOKEY' in process.env)) { - run = run.failing - } - - if (!electron && 'electron' in process.versions) { - run = run.failing - } - return run -} - conditional({ webcrypto: 0 })('secret key object can be transformed to a JWK', async (t) => { const keylike = await importJWK( { diff --git a/test/jwk/thumbprint.test.mjs b/test/jwk/thumbprint.test.mjs index 7bbd6fa8..458b045e 100644 --- a/test/jwk/thumbprint.test.mjs +++ b/test/jwk/thumbprint.test.mjs @@ -1,16 +1,5 @@ import test from 'ava' - -let root -let keyRoot - -if ('WEBCRYPTO' in process.env) { - root = keyRoot = '#dist/webcrypto' -} else if ('CRYPTOKEY' in process.env) { - root = '#dist' - keyRoot = '#dist/webcrypto' -} else { - root = keyRoot = '#dist' -} +import { keyRoot } from '../dist.mjs' const { calculateJwkThumbprint } = await import(keyRoot) diff --git a/test/jwks/local.test.mjs b/test/jwks/local.test.mjs index cb90ce63..cf0897d0 100644 --- a/test/jwks/local.test.mjs +++ b/test/jwks/local.test.mjs @@ -1,16 +1,5 @@ import test from 'ava' - -let root -let keyRoot - -if ('WEBCRYPTO' in process.env) { - root = keyRoot = '#dist/webcrypto' -} else if ('CRYPTOKEY' in process.env) { - root = '#dist' - keyRoot = '#dist/webcrypto' -} else { - root = keyRoot = '#dist' -} +import { root, keyRoot } from '../dist.mjs' const { jwtVerify, diff --git a/test/jwks/remote.test.mjs b/test/jwks/remote.test.mjs index 7f85c8a8..1f7a9a80 100644 --- a/test/jwks/remote.test.mjs +++ b/test/jwks/remote.test.mjs @@ -3,18 +3,10 @@ import nock from 'nock' import timekeeper from 'timekeeper' import { createServer } from 'http' import { once } from 'events' +import { root, keyRoot } from '../dist.mjs' -let root -let keyRoot - -if ('WEBCRYPTO' in process.env) { - root = keyRoot = '#dist/webcrypto' -} else if ('CRYPTOKEY' in process.env) { - root = '#dist' - keyRoot = '#dist/webcrypto' -} else { - root = keyRoot = '#dist' -} +const skipOnUndiciTest = 'WEBAPI' in process.env ? test.skip : test +const skipOnUndiciTestSerial = 'WEBAPI' in process.env ? test.skip : test.serial const { jwtVerify, @@ -54,7 +46,7 @@ test.afterEach((t) => { test.afterEach(() => timekeeper.reset()) -test.serial('RemoteJWKSet', async (t) => { +skipOnUndiciTestSerial('RemoteJWKSet', async (t) => { const keys = [ { e: 'AQAB', @@ -212,7 +204,7 @@ test.serial('RemoteJWKSet', async (t) => { } }) -test.serial('refreshes the JWKS once off cooldown', async (t) => { +skipOnUndiciTestSerial('refreshes the JWKS once off cooldown', async (t) => { timekeeper.freeze(now * 1000) let jwk = { crv: 'P-256', @@ -256,7 +248,7 @@ test.serial('refreshes the JWKS once off cooldown', async (t) => { } }) -test.serial('throws on invalid JWKSet', async (t) => { +skipOnUndiciTestSerial('throws on invalid JWKSet', async (t) => { const scope = nock('https://as.example.com').get('/jwks').once().reply(200, 'null') const url = new URL('https://as.example.com/jwks') @@ -300,7 +292,7 @@ test.serial('throws on invalid JWKSet', async (t) => { }) }) -test('handles ENOTFOUND', async (t) => { +skipOnUndiciTest('handles ENOTFOUND', async (t) => { nock.enableNetConnect() const url = new URL('https://op.example.com/jwks') const JWKS = createRemoteJWKSet(url) @@ -309,7 +301,7 @@ test('handles ENOTFOUND', async (t) => { }) }) -test('handles ECONNREFUSED', async (t) => { +skipOnUndiciTest('handles ECONNREFUSED', async (t) => { nock.enableNetConnect() const url = new URL('http://localhost:3001/jwks') const JWKS = createRemoteJWKSet(url) @@ -318,7 +310,7 @@ test('handles ECONNREFUSED', async (t) => { }) }) -test('handles ECONNRESET', async (t) => { +skipOnUndiciTest('handles ECONNRESET', async (t) => { nock.enableNetConnect() const url = new URL('http://localhost:3000/jwks') t.context.server.once('connection', (socket) => { @@ -330,7 +322,7 @@ test('handles ECONNRESET', async (t) => { }) }) -test('handles a timeout', async (t) => { +skipOnUndiciTest('handles a timeout', async (t) => { t.timeout(1000) nock.enableNetConnect() const url = new URL('http://localhost:3000/jwks') diff --git a/test/jws/compact.sign.test.mjs b/test/jws/compact.sign.test.mjs index 7d861418..56a4fb66 100644 --- a/test/jws/compact.sign.test.mjs +++ b/test/jws/compact.sign.test.mjs @@ -1,6 +1,6 @@ import test from 'ava' +import { root } from '../dist.mjs' -const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto' const { CompactSign } = await import(root) test.before((t) => { diff --git a/test/jws/compact.verify.test.mjs b/test/jws/compact.verify.test.mjs index 4a435f24..fce72deb 100644 --- a/test/jws/compact.verify.test.mjs +++ b/test/jws/compact.verify.test.mjs @@ -1,7 +1,7 @@ import test from 'ava' import * as crypto from 'crypto' +import { root } from '../dist.mjs' -const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto' const { compactVerify, CompactSign } = await import(root) test.before(async (t) => { diff --git a/test/jws/cookbook.test.mjs b/test/jws/cookbook.test.mjs index b549cfcc..987d1c88 100644 --- a/test/jws/cookbook.test.mjs +++ b/test/jws/cookbook.test.mjs @@ -1,16 +1,5 @@ import test from 'ava' - -let root -let keyRoot - -if ('WEBCRYPTO' in process.env) { - root = keyRoot = '#dist/webcrypto' -} else if ('CRYPTOKEY' in process.env) { - root = '#dist' - keyRoot = '#dist/webcrypto' -} else { - root = keyRoot = '#dist' -} +import { root, keyRoot } from '../dist.mjs' const { FlattenedSign, flattenedVerify, CompactSign, compactVerify } = await import(root) const { importJWK } = await import(keyRoot) @@ -479,7 +468,10 @@ const vectors = [ for (const vector of vectors) { let conditional - if (('WEBCRYPTO' in process.env || 'CRYPTOKEY' in process.env) && vector.webcrypto === false) { + if ( + ('WEBCRYPTO' in process.env || 'CRYPTOKEY' in process.env || 'WEBAPI' in process.env) && + vector.webcrypto === false + ) { conditional = test.failing } else { conditional = test diff --git a/test/jws/crit.test.mjs b/test/jws/crit.test.mjs index 458f22bc..d24e0f23 100644 --- a/test/jws/crit.test.mjs +++ b/test/jws/crit.test.mjs @@ -1,6 +1,6 @@ import test from 'ava' +import { root } from '../dist.mjs' -const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto' const { FlattenedSign } = await import(root) const encode = TextEncoder.prototype.encode.bind(new TextEncoder()) diff --git a/test/jws/flattened.sign.test.mjs b/test/jws/flattened.sign.test.mjs index 87523dd9..6d1afe00 100644 --- a/test/jws/flattened.sign.test.mjs +++ b/test/jws/flattened.sign.test.mjs @@ -1,6 +1,6 @@ import test from 'ava' +import { root } from '../dist.mjs' -const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto' const { FlattenedSign } = await import(root) test.before(async (t) => { diff --git a/test/jws/flattened.verify.test.mjs b/test/jws/flattened.verify.test.mjs index e5440647..1870af75 100644 --- a/test/jws/flattened.verify.test.mjs +++ b/test/jws/flattened.verify.test.mjs @@ -1,7 +1,7 @@ import test from 'ava' import * as crypto from 'crypto' +import { root } from '../dist.mjs' -const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto' const { FlattenedSign, flattenedVerify } = await import(root) test.before(async (t) => { diff --git a/test/jws/general.test.mjs b/test/jws/general.test.mjs index 7ec1a55b..5ea6e50f 100644 --- a/test/jws/general.test.mjs +++ b/test/jws/general.test.mjs @@ -1,7 +1,7 @@ import test from 'ava' import * as crypto from 'crypto' +import { root } from '../dist.mjs' -const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto' const { GeneralSign, generalVerify } = await import(root) test.before(async (t) => { diff --git a/test/jws/restrictions.test.mjs b/test/jws/restrictions.test.mjs index b6a033da..428f4028 100644 --- a/test/jws/restrictions.test.mjs +++ b/test/jws/restrictions.test.mjs @@ -1,17 +1,6 @@ import test from 'ava' import * as crypto from 'crypto' - -let root -let keyRoot - -if ('WEBCRYPTO' in process.env) { - root = keyRoot = '#dist/webcrypto' -} else if ('CRYPTOKEY' in process.env) { - root = '#dist' - keyRoot = '#dist/webcrypto' -} else { - root = keyRoot = '#dist' -} +import { conditional, root, keyRoot } from '../dist.mjs' const { FlattenedSign, flattenedVerify, FlattenedEncrypt, flattenedDecrypt, base64url } = await import(root) @@ -127,17 +116,5 @@ test(testECDSASigEncoding, 'ES256') test(testECDSASigEncoding, 'ES384') test(testECDSASigEncoding, 'ES512') -function conditional({ webcrypto = 1, electron = 1 } = {}) { - let run = test - if (!webcrypto && ('WEBCRYPTO' in process.env || 'CRYPTOKEY' in process.env)) { - run = run.failing - } - - if (!electron && 'electron' in process.versions) { - run = run.failing - } - return run -} - conditional({ webcrypto: 0 })(testRSAenc, 'RSA1_5') conditional({ webcrypto: 0, electron: 0 })(testECDSASigEncoding, 'ES256K') diff --git a/test/jws/smoke.test.mjs b/test/jws/smoke.test.mjs index 73cb0897..49aac791 100644 --- a/test/jws/smoke.test.mjs +++ b/test/jws/smoke.test.mjs @@ -1,17 +1,6 @@ import test from 'ava' import * as crypto from 'crypto' - -let root -let keyRoot - -if ('WEBCRYPTO' in process.env) { - root = keyRoot = '#dist/webcrypto' -} else if ('CRYPTOKEY' in process.env) { - root = '#dist' - keyRoot = '#dist/webcrypto' -} else { - root = keyRoot = '#dist' -} +import { conditional, root, keyRoot } from '../dist.mjs' const { FlattenedSign, @@ -208,18 +197,6 @@ test('as keyobject', smoke, 'oct256', true) test('as keyobject', smoke, 'oct384', true) test('as keyobject', smoke, 'oct512', true) -function conditional({ webcrypto = 1, electron = 1 } = {}) { - let run = test - if (!webcrypto && ('WEBCRYPTO' in process.env || 'CRYPTOKEY' in process.env)) { - run = run.failing - } - - if (!electron && 'electron' in process.versions) { - run = run.failing - } - return run -} - conditional({ webcrypto: 0, electron: 0 })(smoke, 'secp256k1') conditional({ webcrypto: 1 })(smoke, 'ed25519') conditional({ webcrypto: 1, electron: 0 })(smoke, 'ed448') diff --git a/test/jws/unencoded.test.mjs b/test/jws/unencoded.test.mjs index 80563aad..4f51bc07 100644 --- a/test/jws/unencoded.test.mjs +++ b/test/jws/unencoded.test.mjs @@ -1,6 +1,6 @@ import test from 'ava' +import { root } from '../dist.mjs' -const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto' const { FlattenedSign, flattenedVerify } = await import(root) const encode = TextEncoder.prototype.encode.bind(new TextEncoder()) diff --git a/test/jwt/decrypt.test.mjs b/test/jwt/decrypt.test.mjs index d215e8c6..715ead25 100644 --- a/test/jwt/decrypt.test.mjs +++ b/test/jwt/decrypt.test.mjs @@ -1,7 +1,7 @@ import test from 'ava' import timekeeper from 'timekeeper' +import { root } from '../dist.mjs' -const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto' const { EncryptJWT, jwtDecrypt, CompactEncrypt } = await import(root) const now = 1604416038 diff --git a/test/jwt/encrypt.test.mjs b/test/jwt/encrypt.test.mjs index 11618b58..07c04a49 100644 --- a/test/jwt/encrypt.test.mjs +++ b/test/jwt/encrypt.test.mjs @@ -1,7 +1,7 @@ import test from 'ava' import timekeeper from 'timekeeper' +import { root } from '../dist.mjs' -const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto' const { EncryptJWT, compactDecrypt, jwtDecrypt } = await import(root) const now = 1604416038 diff --git a/test/jwt/sign.test.mjs b/test/jwt/sign.test.mjs index 7068616b..b38e7a59 100644 --- a/test/jwt/sign.test.mjs +++ b/test/jwt/sign.test.mjs @@ -1,7 +1,7 @@ import test from 'ava' import timekeeper from 'timekeeper' +import { root } from '../dist.mjs' -const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto' const { SignJWT, compactVerify, jwtVerify } = await import(root) const now = 1604416038 diff --git a/test/jwt/unsecured.test.mjs b/test/jwt/unsecured.test.mjs index ee272e4c..ce275c74 100644 --- a/test/jwt/unsecured.test.mjs +++ b/test/jwt/unsecured.test.mjs @@ -1,7 +1,7 @@ import test from 'ava' import timekeeper from 'timekeeper' +import { root } from '../dist.mjs' -const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto' const { UnsecuredJWT } = await import(root) const now = 1604416038 diff --git a/test/jwt/verify.test.mjs b/test/jwt/verify.test.mjs index 7795e3fc..259c45e8 100644 --- a/test/jwt/verify.test.mjs +++ b/test/jwt/verify.test.mjs @@ -1,7 +1,7 @@ import test from 'ava' import timekeeper from 'timekeeper' +import { root } from '../dist.mjs' -const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto' const { SignJWT, jwtVerify, CompactSign } = await import(root) const now = 1604416038 diff --git a/test/key/importexport.test.mjs b/test/key/importexport.test.mjs index 9686433f..8d8f7c00 100644 --- a/test/key/importexport.test.mjs +++ b/test/key/importexport.test.mjs @@ -1,31 +1,8 @@ import test from 'ava' - -let root -let keyRoot - -if ('WEBCRYPTO' in process.env) { - root = keyRoot = '#dist/webcrypto' -} else if ('CRYPTOKEY' in process.env) { - root = '#dist' - keyRoot = '#dist/webcrypto' -} else { - root = keyRoot = '#dist' -} +import { conditional, keyRoot } from '../dist.mjs' const jose = await import(keyRoot) -function conditional({ webcrypto = 1, electron = 1 } = {}) { - let run = test - if (!webcrypto && ('WEBCRYPTO' in process.env || 'CRYPTOKEY' in process.env)) { - run = run.failing - } - - if (!electron && 'electron' in process.versions) { - run = run.failing - } - return run -} - const keys = { rsa: { privateKey: diff --git a/test/unit/buffer_utils.test.mjs b/test/unit/buffer_utils.test.mjs index a81209f0..52279830 100644 --- a/test/unit/buffer_utils.test.mjs +++ b/test/unit/buffer_utils.test.mjs @@ -1,6 +1,6 @@ import test from 'ava' +import { root } from '../dist.mjs' -const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto' const { uint32be } = await import(`${root}/lib/buffer_utils`) test('lib/buffer_utils.ts', (t) => { diff --git a/test/unit/cek.test.mjs b/test/unit/cek.test.mjs index eb80ae58..b7602c87 100644 --- a/test/unit/cek.test.mjs +++ b/test/unit/cek.test.mjs @@ -1,6 +1,6 @@ import test from 'ava' +import { root } from '../dist.mjs' -const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto' const { default: cek } = await import(`${root}/lib/cek`) test('lib/cek.ts', (t) => { diff --git a/test/unit/check_iv_length.test.mjs b/test/unit/check_iv_length.test.mjs index 521a52fc..cfaad3fa 100644 --- a/test/unit/check_iv_length.test.mjs +++ b/test/unit/check_iv_length.test.mjs @@ -1,6 +1,6 @@ import test from 'ava' +import { root } from '../dist.mjs' -const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto' const { default: checkIvLength } = await import(`${root}/lib/check_iv_length`) test('lib/check_iv_length.ts', (t) => { diff --git a/test/unit/check_key_type.test.mjs b/test/unit/check_key_type.test.mjs index d9e3453f..9975f6ef 100644 --- a/test/unit/check_key_type.test.mjs +++ b/test/unit/check_key_type.test.mjs @@ -1,20 +1,9 @@ import test from 'ava' - -let root -let keyRoot - -if ('WEBCRYPTO' in process.env) { - root = keyRoot = '#dist/webcrypto' -} else if ('CRYPTOKEY' in process.env) { - root = '#dist' - keyRoot = '#dist/webcrypto' -} else { - root = keyRoot = '#dist' -} +import { root, keyRoot } from '../dist.mjs' let types = 'KeyObject or Uint8Array' let asymmetricTypes = 'KeyObject' -if ('WEBCRYPTO' in process.env) { +if ('WEBCRYPTO' in process.env || 'WEBAPI' in process.env) { types = 'CryptoKey or Uint8Array' asymmetricTypes = 'CryptoKey' } else if (parseInt(process.versions.node) >= 16) { @@ -78,7 +67,7 @@ test('lib/check_key_type.ts', async (t) => { message: `${asymmetricTypes} instances for symmetric algorithms must be of type "secret"`, }) - if (keyRoot.includes('webcrypto')) { + if (keyRoot.includes('web')) { t.throws(() => checkKeyType('PS256', keypair.privateKey, 'verify'), { ...expected, message: `${asymmetricTypes} instances for asymmetric algorithm verifying must be of type "public"`, @@ -93,7 +82,7 @@ test('lib/check_key_type.ts', async (t) => { message: `${asymmetricTypes} instances for asymmetric algorithm decryption must be of type "private"`, }) - if (keyRoot.includes('webcrypto')) { + if (keyRoot.includes('web')) { t.throws(() => checkKeyType('ECDH-ES', keypair.privateKey, 'encrypt'), { ...expected, message: `${asymmetricTypes} instances for asymmetric algorithm encryption must be of type "public"`, diff --git a/test/unit/check_p2s.test.mjs b/test/unit/check_p2s.test.mjs index 1c491137..6867823b 100644 --- a/test/unit/check_p2s.test.mjs +++ b/test/unit/check_p2s.test.mjs @@ -1,6 +1,6 @@ import test from 'ava' +import { root } from '../dist.mjs' -const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto' const { default: checkP2s } = await import(`${root}/lib/check_p2s`) test('lib/check_p2s.ts', (t) => { diff --git a/test/unit/iv.test.mjs b/test/unit/iv.test.mjs index 3dc65a75..67e4e8b6 100644 --- a/test/unit/iv.test.mjs +++ b/test/unit/iv.test.mjs @@ -1,6 +1,6 @@ import test from 'ava' +import { root } from '../dist.mjs' -const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto' const { default: iv } = await import(`${root}/lib/iv`) test('lib/iv.ts', (t) => { diff --git a/test/unit/secs.test.mjs b/test/unit/secs.test.mjs index e543a487..14b77cd7 100644 --- a/test/unit/secs.test.mjs +++ b/test/unit/secs.test.mjs @@ -1,6 +1,6 @@ import test from 'ava' +import { root } from '../dist.mjs' -const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto' const { default: secs } = await import(`${root}/lib/secs`) test('lib/secs.ts', (t) => { diff --git a/test/util/decode_jwt.test.mjs b/test/util/decode_jwt.test.mjs index 28dd4148..2fec4a5a 100644 --- a/test/util/decode_jwt.test.mjs +++ b/test/util/decode_jwt.test.mjs @@ -1,6 +1,6 @@ import test from 'ava' +import { root } from '../dist.mjs' -const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto' const { decodeJwt, errors, base64url } = await import(root) test('invalid inputs', (t) => { diff --git a/test/util/decode_protected_header.test.mjs b/test/util/decode_protected_header.test.mjs index eb864ab4..765f3573 100644 --- a/test/util/decode_protected_header.test.mjs +++ b/test/util/decode_protected_header.test.mjs @@ -1,6 +1,6 @@ import test from 'ava' +import { root } from '../dist.mjs' -const root = !('WEBCRYPTO' in process.env) ? '#dist' : '#dist/webcrypto' const { decodeProtectedHeader } = await import(root) test('invalid inputs', (t) => { diff --git a/test/util/generators.test.mjs b/test/util/generators.test.mjs index 74b2906b..9df982d8 100644 --- a/test/util/generators.test.mjs +++ b/test/util/generators.test.mjs @@ -1,16 +1,5 @@ import test from 'ava' - -let root -let keyRoot - -if ('WEBCRYPTO' in process.env) { - root = keyRoot = '#dist/webcrypto' -} else if ('CRYPTOKEY' in process.env) { - root = '#dist' - keyRoot = '#dist/webcrypto' -} else { - root = keyRoot = '#dist' -} +import { conditional, root, keyRoot } from '../dist.mjs' const { generateKeyPair, generateSecret } = await import(keyRoot) @@ -143,18 +132,6 @@ if ('WEBCRYPTO' in process.env || 'CRYPTOKEY' in process.env) { test('with extractable: true', testKeyPair, 'PS256', { extractable: true }) } -function conditional({ webcrypto = 1, electron = 1 } = {}) { - let run = test - if (!webcrypto && ('WEBCRYPTO' in process.env || 'CRYPTOKEY' in process.env)) { - run = run.failing - } - - if (!electron && 'electron' in process.versions) { - run = run.failing - } - return run -} - conditional({ webcrypto: 1 })(testKeyPair, 'EdDSA') conditional({ webcrypto: 1 })('crv: Ed25519', testKeyPair, 'EdDSA', { crv: 'Ed25519' }) conditional({ webcrypto: 1, electron: 0 })('crv: Ed448', testKeyPair, 'EdDSA', {