From b26599a52346b8884bee79f647567a934c9bd2bb Mon Sep 17 00:00:00 2001 From: Ajitomi Daisuke Date: Sat, 12 Oct 2024 07:56:58 +0900 Subject: [PATCH] Add isDeno instead of isNode. --- packages/common/mod.ts | 2 +- packages/common/src/utils/misc.ts | 10 ++++--- packages/core/test/cipherSuite.test.ts | 4 +-- packages/hpke-js/test/cipherSuite.test.ts | 4 +-- .../test/cipherSuiteBackwardCompat.test.ts | 8 +++--- packages/hpke-js/test/conformance.test.ts | 26 +++++++++---------- packages/hpke-js/test/dhkemPrimitives.test.ts | 8 +++--- packages/hpke-js/test/kemContext.test.ts | 10 +++---- packages/hpke-js/test/keyValidationEc.test.ts | 8 +++--- packages/hpke-js/test/sample.test.ts | 12 ++++----- packages/hpke-js/test/utils.ts | 8 +++--- 11 files changed, 52 insertions(+), 48 deletions(-) diff --git a/packages/common/mod.ts b/packages/common/mod.ts index 8caf5ac1e..71135bd0a 100644 --- a/packages/common/mod.ts +++ b/packages/common/mod.ts @@ -34,8 +34,8 @@ export { hexToBytes, i2Osp, isCryptoKeyPair, + isDeno, isDenoV1, - isNode, kemToKeyGenAlgorithm, loadCrypto, loadSubtleCrypto, diff --git a/packages/common/src/utils/misc.ts b/packages/common/src/utils/misc.ts index d368ec056..35eacaa44 100644 --- a/packages/common/src/utils/misc.ts +++ b/packages/common/src/utils/misc.ts @@ -4,13 +4,17 @@ export const isDenoV1 = (): boolean => // deno-lint-ignore no-explicit-any (globalThis as any).process === undefined; -export function isNode(): boolean { +/** + * Checks whether the runtime is Deno or not (Node.js). + * @returns boolean - true if the runtime is Deno, false Node.js. + */ +export function isDeno(): boolean { // deno-lint-ignore no-explicit-any if ((globalThis as any).process === undefined) { - return false; + return true; } // deno-lint-ignore no-explicit-any - return (globalThis as any).process?.versions?.deno === undefined; + return (globalThis as any).process?.versions?.deno !== undefined; } /** diff --git a/packages/core/test/cipherSuite.test.ts b/packages/core/test/cipherSuite.test.ts index 3853c42a2..11e94b715 100644 --- a/packages/core/test/cipherSuite.test.ts +++ b/packages/core/test/cipherSuite.test.ts @@ -1,7 +1,7 @@ import { assertEquals, assertRejects, assertThrows } from "@std/assert"; import { describe, it } from "@std/testing/bdd"; -import { hexToBytes, isNode } from "@hpke/common"; +import { hexToBytes, isDenoV1 } from "@hpke/common"; import { AeadId, @@ -516,7 +516,7 @@ describe("deriveKeyPair", () => { describe("with official test-vector for DhkemP256HkdfSha256.", () => { it("should derive a proper key pair.", async () => { - if (!isNode()) { + if (isDenoV1()) { return; } const ikmR = hexToBytes( diff --git a/packages/hpke-js/test/cipherSuite.test.ts b/packages/hpke-js/test/cipherSuite.test.ts index 133223ff9..75061cefc 100644 --- a/packages/hpke-js/test/cipherSuite.test.ts +++ b/packages/hpke-js/test/cipherSuite.test.ts @@ -1,7 +1,7 @@ import { assertEquals, assertRejects, assertThrows } from "@std/assert"; import { describe, it } from "@std/testing/bdd"; -import { hexToBytes, isNode } from "@hpke/common"; +import { hexToBytes, isDeno } from "@hpke/common"; import { AeadId, DeserializeError, @@ -479,7 +479,7 @@ describe("deriveKeyPair", () => { describe("with official test-vector for DhkemP256HkdfSha256.", () => { it("should derive a proper key pair.", async () => { - if (!isNode()) { + if (isDeno()) { return; } const ikmR = hexToBytes( diff --git a/packages/hpke-js/test/cipherSuiteBackwardCompat.test.ts b/packages/hpke-js/test/cipherSuiteBackwardCompat.test.ts index 3bb7236ab..f84fc8110 100644 --- a/packages/hpke-js/test/cipherSuiteBackwardCompat.test.ts +++ b/packages/hpke-js/test/cipherSuiteBackwardCompat.test.ts @@ -1,7 +1,7 @@ import { assertEquals, assertRejects, assertThrows } from "@std/assert"; import { describe, it } from "@std/testing/bdd"; -import { concat, hexToBytes, isNode, loadCrypto } from "@hpke/common"; +import { concat, hexToBytes, isDeno, loadCrypto } from "@hpke/common"; import { DeserializeError, InvalidParamError, @@ -337,7 +337,7 @@ describe("CipherSuite(backward-compat)", () => { describe("A README example of Base mode (Kem.DhkemP521HkdfSha512/Kdf.HkdfSha512)", () => { it("should work normally with generateKeyPair", async () => { - if (!isNode()) { + if (isDeno()) { return; } @@ -372,7 +372,7 @@ describe("CipherSuite(backward-compat)", () => { }); it("should work normally with importKey('jwk')", async () => { - if (!isNode()) { + if (isDeno()) { return; } @@ -1454,7 +1454,7 @@ describe("CipherSuite(backward-compat)", () => { describe("A README example of Oblivious HTTP (HKDF-SHA512)", () => { it("should work normally", async () => { - if (!isNode()) { + if (isDeno()) { return; } const te = new TextEncoder(); diff --git a/packages/hpke-js/test/conformance.test.ts b/packages/hpke-js/test/conformance.test.ts index 30c63dcc4..244b2b1a7 100644 --- a/packages/hpke-js/test/conformance.test.ts +++ b/packages/hpke-js/test/conformance.test.ts @@ -1,6 +1,6 @@ import { afterAll, beforeAll, describe, it } from "@std/testing/bdd"; -import { isDenoV1, isNode } from "@hpke/common"; +import { isDeno, isDenoV1 } from "@hpke/common"; import type { ConformanceTester } from "./conformanceTester.ts"; import type { TestVector } from "./testVector.ts"; @@ -53,7 +53,7 @@ describe("RFC9180 conformance", () => { it("should match demonstrated values", async () => { for (const v of testVectors) { if (v.mode === 0 && v.kem_id === 0x0012 && v.aead_id <= 0x0002) { - if (!isNode()) { + if (isDeno()) { continue; } await tester.test(v); @@ -66,7 +66,7 @@ describe("RFC9180 conformance", () => { it("should match demonstrated values", async () => { for (const v of testVectors) { if (v.mode === 0 && v.kem_id < 0x0020 && v.aead_id === 0x0003) { - if (isDenoV1() || (v.kem_id === 0x0012 && !isNode())) { + if (isDenoV1() || (v.kem_id === 0x0012 && isDeno())) { continue; } await tester.test(v); @@ -79,7 +79,7 @@ describe("RFC9180 conformance", () => { it("should match demonstrated values", async () => { for (const v of testVectors) { if (v.mode === 0 && v.kem_id < 0x0020 && v.aead_id === 0xFFFF) { - if (isDenoV1() || (v.kem_id === 0x0012 && !isNode())) { + if (isDenoV1() || (v.kem_id === 0x0012 && isDeno())) { continue; } await tester.test(v); @@ -152,7 +152,7 @@ describe("RFC9180 conformance", () => { it("should match demonstrated values", async () => { for (const v of testVectors) { if (v.mode === 1 && v.kem_id < 0x0020 && v.aead_id <= 0x0002) { - if (isDenoV1() || (v.kem_id === 0x0012 && !isNode())) { + if (isDenoV1() || (v.kem_id === 0x0012 && isDeno())) { continue; } await tester.test(v); @@ -165,7 +165,7 @@ describe("RFC9180 conformance", () => { it("should match demonstrated values", async () => { for (const v of testVectors) { if (v.mode === 1 && v.kem_id < 0x0020 && v.aead_id === 0x0003) { - if (isDenoV1() || (v.kem_id === 0x0012 && !isNode())) { + if (isDenoV1() || (v.kem_id === 0x0012 && isDeno())) { continue; } await tester.test(v); @@ -178,7 +178,7 @@ describe("RFC9180 conformance", () => { it("should match demonstrated values", async () => { for (const v of testVectors) { if (v.mode === 1 && v.kem_id < 0x0020 && v.aead_id === 0xFFFF) { - if (isDenoV1() || (v.kem_id === 0x0012 && !isNode())) { + if (isDenoV1() || (v.kem_id === 0x0012 && isDeno())) { continue; } await tester.test(v); @@ -251,7 +251,7 @@ describe("RFC9180 conformance", () => { it("should match demonstrated values", async () => { for (const v of testVectors) { if (v.mode === 2 && v.kem_id < 0x0020 && v.aead_id <= 0x0002) { - if (isDenoV1() || (v.kem_id === 0x0012 && !isNode())) { + if (isDenoV1() || (v.kem_id === 0x0012 && isDeno())) { continue; } await tester.test(v); @@ -264,7 +264,7 @@ describe("RFC9180 conformance", () => { it("should match demonstrated values", async () => { for (const v of testVectors) { if (v.mode === 2 && v.kem_id < 0x0020 && v.aead_id === 0x0003) { - if (isDenoV1() || (v.kem_id === 0x0012 && !isNode())) { + if (isDenoV1() || (v.kem_id === 0x0012 && isDeno())) { continue; } await tester.test(v); @@ -277,7 +277,7 @@ describe("RFC9180 conformance", () => { it("should match demonstrated values", async () => { for (const v of testVectors) { if (v.mode === 2 && v.kem_id < 0x0020 && v.aead_id === 0xFFFF) { - if (isDenoV1() || (v.kem_id === 0x0012 && !isNode())) { + if (isDenoV1() || (v.kem_id === 0x0012 && isDeno())) { continue; } await tester.test(v); @@ -350,7 +350,7 @@ describe("RFC9180 conformance", () => { it("should match demonstrated values", async () => { for (const v of testVectors) { if (v.mode === 3 && v.kem_id < 0x0020 && v.aead_id <= 0x0002) { - if (isDenoV1() || (v.kem_id === 0x0012 && !isNode())) { + if (isDenoV1() || (v.kem_id === 0x0012 && isDeno())) { continue; } await tester.test(v); @@ -363,7 +363,7 @@ describe("RFC9180 conformance", () => { it("should match demonstrated values", async () => { for (const v of testVectors) { if (v.mode === 3 && v.kem_id < 0x0020 && v.aead_id === 0x0003) { - if (isDenoV1() || (v.kem_id === 0x0012 && !isNode())) { + if (isDenoV1() || (v.kem_id === 0x0012 && isDeno())) { continue; } await tester.test(v); @@ -376,7 +376,7 @@ describe("RFC9180 conformance", () => { it("should match demonstrated values", async () => { for (const v of testVectors) { if (v.mode === 3 && v.kem_id < 0x0020 && v.aead_id === 0xFFFF) { - if (isDenoV1() || (v.kem_id === 0x0012 && !isNode())) { + if (isDenoV1() || (v.kem_id === 0x0012 && isDeno())) { continue; } await tester.test(v); diff --git a/packages/hpke-js/test/dhkemPrimitives.test.ts b/packages/hpke-js/test/dhkemPrimitives.test.ts index fe66e2325..241a3a477 100644 --- a/packages/hpke-js/test/dhkemPrimitives.test.ts +++ b/packages/hpke-js/test/dhkemPrimitives.test.ts @@ -1,7 +1,7 @@ import { assertEquals, assertRejects } from "@std/assert"; import { describe, it } from "@std/testing/bdd"; -import { DeserializeError, Ec, isNode, KemId } from "@hpke/common"; +import { DeserializeError, Ec, isDeno, KemId } from "@hpke/common"; import { HkdfSha256, X25519 } from "@hpke/dhkem-x25519"; import { HkdfSha512, X448 } from "@hpke/dhkem-x448"; @@ -44,7 +44,7 @@ describe("derivePublicKey", () => { }); it("should return a proper public key with Ec(DhkemP521HkdfSha512)", async () => { - if (!isNode()) { + if (isDeno()) { return; } const kdf = new HkdfSha512(); @@ -113,7 +113,7 @@ describe("derivePublicKey", () => { }); it("should throw DeserializeError on Ec(DhkemP256HkdfSha256) with a P-521 private key", async () => { - if (!isNode()) { + if (isDeno()) { return; } const kdf = new HkdfSha256(); @@ -192,7 +192,7 @@ describe("derivePublicKey", () => { }); it("should throw DeserializeError on X25519 with a P-521 private key", async () => { - if (!isNode()) { + if (isDeno()) { return; } const kdf = new HkdfSha256(); diff --git a/packages/hpke-js/test/kemContext.test.ts b/packages/hpke-js/test/kemContext.test.ts index 66239caae..89d7e9856 100644 --- a/packages/hpke-js/test/kemContext.test.ts +++ b/packages/hpke-js/test/kemContext.test.ts @@ -1,7 +1,7 @@ import { assertEquals, assertRejects } from "@std/assert"; import { describe, it } from "@std/testing/bdd"; -import { isDenoV1, isNode, loadCrypto } from "@hpke/common"; +import { isDeno, isDenoV1, loadCrypto } from "@hpke/common"; import { DeriveKeyPairError, DeserializeError, @@ -200,7 +200,7 @@ describe("deriveKeyPair", () => { }); it("should return a proper instance with DhkemP521HkdfSha512", async () => { - if (!isNode()) { + if (isDeno()) { return; } const cryptoApi = await loadCrypto(); @@ -326,7 +326,7 @@ describe("serialize/deserializePublicKey", () => { }); it("should return a proper instance with DhkemP521HkdfSha512", async () => { - if (!isNode()) { + if (isDeno()) { return; } @@ -525,7 +525,7 @@ describe("serialize/deserializePrivateKey", () => { }); it("should return a proper instance with DhkemP521HkdfSha512", async () => { - if (!isNode()) { + if (isDeno()) { return; } @@ -762,7 +762,7 @@ describe("importKey", () => { }); it("should return a valid private key for DhkemP521HkdfSha512 from JWK", async () => { - if (!isNode()) { + if (isDeno()) { return; } const kemContext = new DhkemP521HkdfSha512(); diff --git a/packages/hpke-js/test/keyValidationEc.test.ts b/packages/hpke-js/test/keyValidationEc.test.ts index 72567bd66..72b83049f 100644 --- a/packages/hpke-js/test/keyValidationEc.test.ts +++ b/packages/hpke-js/test/keyValidationEc.test.ts @@ -1,6 +1,6 @@ import { afterAll, beforeAll, describe, it } from "@std/testing/bdd"; -import { isNode } from "@hpke/common"; +import { isDeno } from "@hpke/common"; import type { ConformanceTester } from "./conformanceTester.ts"; import type { WycheproofTestVector } from "./testVector.ts"; @@ -24,7 +24,7 @@ describe("EC key validation", () => { describe("P-256", () => { it("should validate properly", async () => { - if (!isNode()) { + if (isDeno()) { return; } @@ -49,7 +49,7 @@ describe("EC key validation", () => { describe("P-384", () => { it("should validate properly", async () => { - if (!isNode()) { + if (isDeno()) { return; } @@ -74,7 +74,7 @@ describe("EC key validation", () => { describe("P-521", () => { it("should validate properly", async () => { - if (!isNode()) { + if (isDeno()) { return; } diff --git a/packages/hpke-js/test/sample.test.ts b/packages/hpke-js/test/sample.test.ts index 86e9b7aa4..5fdf42b8a 100644 --- a/packages/hpke-js/test/sample.test.ts +++ b/packages/hpke-js/test/sample.test.ts @@ -1,7 +1,7 @@ import { assertEquals, assertRejects } from "@std/assert"; import { describe, it } from "@std/testing/bdd"; -import { concat, isNode, loadCrypto } from "@hpke/common"; +import { concat, isDeno, loadCrypto } from "@hpke/common"; import { AeadId, Aes128Gcm, KdfId, KemId, NotSupportedError } from "@hpke/core"; import { DhkemX25519HkdfSha256, HkdfSha256 } from "@hpke/dhkem-x25519"; import { DhkemX448HkdfSha512, HkdfSha512 } from "@hpke/dhkem-x448"; @@ -249,7 +249,7 @@ describe("README examples", () => { describe("Base mode with DhkemP521HkdfSha512/HkdfSha512/Aes128Gcm", () => { it("should work normally with ids", async () => { - if (!isNode()) { + if (isDeno()) { return; } @@ -284,7 +284,7 @@ describe("README examples", () => { }); it("should work normally with instances", async () => { - if (!isNode()) { + if (isDeno()) { return; } @@ -319,7 +319,7 @@ describe("README examples", () => { }); it("should work normally with importKey('jwk')", async () => { - if (!isNode()) { + if (isDeno()) { return; } @@ -1034,7 +1034,7 @@ describe("README examples", () => { describe("Oblivious HTTP with DhkemP384HkdfSha384/HkdfSha384/Aes256Gcm", () => { it("should work normally", async () => { - if (!isNode()) { + if (isDeno()) { return; } const te = new TextEncoder(); @@ -1121,7 +1121,7 @@ describe("README examples", () => { describe("Oblivious HTTP with DhkemP521HkdfSha512/HkdfSha512/Aes256Gcm", () => { it("should work normally", async () => { - if (!isNode()) { + if (isDeno()) { return; } const te = new TextEncoder(); diff --git a/packages/hpke-js/test/utils.ts b/packages/hpke-js/test/utils.ts index d8fcdfea2..acf1fd688 100644 --- a/packages/hpke-js/test/utils.ts +++ b/packages/hpke-js/test/utils.ts @@ -1,11 +1,11 @@ import { dirname, fromFileUrl, join } from "@std/path"; -import { isNode } from "@hpke/common"; +import { isDeno } from "@hpke/common"; export function getPath(name: string): string { const currentPath = dirname(fromFileUrl(import.meta.url)); - if (isNode()) { - return join(currentPath, "../../", name); + if (isDeno()) { + return join(currentPath, name); } - return join(currentPath, name); + return join(currentPath, "../../", name); }