From d5ac63a73ddf65f7a50f372a1998863ce6e866af Mon Sep 17 00:00:00 2001 From: Daniel Regeci <536331+ovx@users.noreply.github.com> Date: Fri, 12 Jul 2024 12:40:21 -0300 Subject: [PATCH] 0.4.0 --- README.md | 16 ++++++++++++++++ cjs/dist/crypto.d.ts | 1 - cjs/dist/crypto.js | 6 ------ cjs/dist/helpers.d.ts | 1 - cjs/dist/helpers.js | 2 -- deno_dist/README.md | 16 ++++++++++++++++ deno_dist/crypto.ts | 4 ---- dist/crypto.d.ts | 1 - dist/crypto.js | 5 ----- dist/helpers.d.ts | 1 - dist/helpers.js | 2 -- lib/crypto.ts | 4 ---- lib/helpers.ts | 2 -- package.json | 2 +- tests/challenge.test.ts | 5 +++++ tests/helpers.test.ts | 5 +++++ tests/serversignature.test.ts | 5 +++++ 17 files changed, 48 insertions(+), 30 deletions(-) delete mode 100644 cjs/dist/crypto.d.ts delete mode 100644 cjs/dist/crypto.js delete mode 100644 deno_dist/crypto.ts delete mode 100644 dist/crypto.d.ts delete mode 100644 dist/crypto.js delete mode 100644 lib/crypto.ts diff --git a/README.md b/README.md index 08d892e..6085b59 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,22 @@ const challenge = await createChallenge({ const ok = await verifySolution(payload, hmacKey); ``` +### Usage with Node.js 16 + +In Node.js version 16, there is no global reference to crypto by default. To use this library, you need to add the following code to your codebase: + +```ts +globalThis.crypto = require('node:crypto').webcrypto; +``` + +Or with `import` syntax: + +```ts +import { webcrypto } from 'node:crypto'; + +globalThis.crypto = webcrypto; +``` + ## API ### `createChallenge(options)` diff --git a/cjs/dist/crypto.d.ts b/cjs/dist/crypto.d.ts deleted file mode 100644 index cb0ff5c..0000000 --- a/cjs/dist/crypto.d.ts +++ /dev/null @@ -1 +0,0 @@ -export {}; diff --git a/cjs/dist/crypto.js b/cjs/dist/crypto.js deleted file mode 100644 index 71bf7c4..0000000 --- a/cjs/dist/crypto.js +++ /dev/null @@ -1,6 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -if (!('crypto' in globalThis)) { - // eslint-disable-next-line @typescript-eslint/no-var-requires - globalThis.crypto = require('node:crypto').webcrypto; -} diff --git a/cjs/dist/helpers.d.ts b/cjs/dist/helpers.d.ts index 089c7fa..f9f94a7 100644 --- a/cjs/dist/helpers.d.ts +++ b/cjs/dist/helpers.d.ts @@ -1,4 +1,3 @@ -import './crypto.js'; import type { Algorithm } from './types.js'; export declare const encoder: TextEncoder; export declare function ab2hex(ab: ArrayBuffer | Uint8Array): string; diff --git a/cjs/dist/helpers.js b/cjs/dist/helpers.js index f4f8885..ee42e1e 100644 --- a/cjs/dist/helpers.js +++ b/cjs/dist/helpers.js @@ -1,8 +1,6 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.randomInt = exports.randomBytes = exports.hmacHex = exports.hmac = exports.hashHex = exports.hash = exports.ab2hex = exports.encoder = void 0; -// @denoify-line-ignore -require("./crypto.js"); exports.encoder = new TextEncoder(); function ab2hex(ab) { return [...new Uint8Array(ab)] diff --git a/deno_dist/README.md b/deno_dist/README.md index 08d892e..6085b59 100644 --- a/deno_dist/README.md +++ b/deno_dist/README.md @@ -28,6 +28,22 @@ const challenge = await createChallenge({ const ok = await verifySolution(payload, hmacKey); ``` +### Usage with Node.js 16 + +In Node.js version 16, there is no global reference to crypto by default. To use this library, you need to add the following code to your codebase: + +```ts +globalThis.crypto = require('node:crypto').webcrypto; +``` + +Or with `import` syntax: + +```ts +import { webcrypto } from 'node:crypto'; + +globalThis.crypto = webcrypto; +``` + ## API ### `createChallenge(options)` diff --git a/deno_dist/crypto.ts b/deno_dist/crypto.ts deleted file mode 100644 index b35a7f9..0000000 --- a/deno_dist/crypto.ts +++ /dev/null @@ -1,4 +0,0 @@ -if (!('crypto' in globalThis)) { - // eslint-disable-next-line @typescript-eslint/no-var-requires - globalThis.crypto = require('node:crypto').webcrypto; -} diff --git a/dist/crypto.d.ts b/dist/crypto.d.ts deleted file mode 100644 index cb0ff5c..0000000 --- a/dist/crypto.d.ts +++ /dev/null @@ -1 +0,0 @@ -export {}; diff --git a/dist/crypto.js b/dist/crypto.js deleted file mode 100644 index 9a3ed67..0000000 --- a/dist/crypto.js +++ /dev/null @@ -1,5 +0,0 @@ -if (!('crypto' in globalThis)) { - // eslint-disable-next-line @typescript-eslint/no-var-requires - globalThis.crypto = require('node:crypto').webcrypto; -} -export {}; diff --git a/dist/helpers.d.ts b/dist/helpers.d.ts index 089c7fa..f9f94a7 100644 --- a/dist/helpers.d.ts +++ b/dist/helpers.d.ts @@ -1,4 +1,3 @@ -import './crypto.js'; import type { Algorithm } from './types.js'; export declare const encoder: TextEncoder; export declare function ab2hex(ab: ArrayBuffer | Uint8Array): string; diff --git a/dist/helpers.js b/dist/helpers.js index 77d4087..0ffd7c8 100644 --- a/dist/helpers.js +++ b/dist/helpers.js @@ -1,5 +1,3 @@ -// @denoify-line-ignore -import './crypto.js'; export const encoder = new TextEncoder(); export function ab2hex(ab) { return [...new Uint8Array(ab)] diff --git a/lib/crypto.ts b/lib/crypto.ts deleted file mode 100644 index b35a7f9..0000000 --- a/lib/crypto.ts +++ /dev/null @@ -1,4 +0,0 @@ -if (!('crypto' in globalThis)) { - // eslint-disable-next-line @typescript-eslint/no-var-requires - globalThis.crypto = require('node:crypto').webcrypto; -} diff --git a/lib/helpers.ts b/lib/helpers.ts index 8093bad..f3094a6 100644 --- a/lib/helpers.ts +++ b/lib/helpers.ts @@ -1,5 +1,3 @@ -// @denoify-line-ignore -import './crypto.js'; import type { Algorithm } from './types.js'; export const encoder = new TextEncoder(); diff --git a/package.json b/package.json index 731a793..818689c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "altcha-lib", - "version": "0.3.0", + "version": "0.4.0", "description": "A library for creating and verifying ALTCHA challenges for Node.js, Bun and Deno.", "author": "Daniel Regeci", "license": "MIT", diff --git a/tests/challenge.test.ts b/tests/challenge.test.ts index 6fdf7e7..04381cc 100644 --- a/tests/challenge.test.ts +++ b/tests/challenge.test.ts @@ -8,6 +8,11 @@ import { } from '../lib/index.js'; import { Challenge } from '../lib/types.js'; +if (!('crypto' in globalThis)) { + // eslint-disable-next-line @typescript-eslint/no-var-requires + globalThis.crypto = require('node:crypto').webcrypto; +} + describe('challenge', () => { const hmacKey = 'test key'; diff --git a/tests/helpers.test.ts b/tests/helpers.test.ts index b3faf48..66e6f4a 100644 --- a/tests/helpers.test.ts +++ b/tests/helpers.test.ts @@ -7,6 +7,11 @@ import { randomInt, } from '../lib/helpers.js'; +if (!('crypto' in globalThis)) { + // eslint-disable-next-line @typescript-eslint/no-var-requires + globalThis.crypto = require('node:crypto').webcrypto; +} + const encoder = new TextEncoder(); describe('helpers', () => { diff --git a/tests/serversignature.test.ts b/tests/serversignature.test.ts index ceb7f6e..adbf83d 100644 --- a/tests/serversignature.test.ts +++ b/tests/serversignature.test.ts @@ -2,6 +2,11 @@ import { describe, expect, it } from 'vitest'; import { verifyServerSignature } from '../lib/index.js'; import { hash, hmacHex } from '../lib/helpers.js'; +if (!('crypto' in globalThis)) { + // eslint-disable-next-line @typescript-eslint/no-var-requires + globalThis.crypto = require('node:crypto').webcrypto; +} + describe('server signature', () => { const hmacKey = 'test key';