Skip to content

Commit

Permalink
Merge pull request #427 from dajiaji/rename-isnode
Browse files Browse the repository at this point in the history
Add isDeno instead of isNode.
  • Loading branch information
dajiaji authored Oct 11, 2024
2 parents 9ae4e67 + b26599a commit 97d2c03
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 48 deletions.
2 changes: 1 addition & 1 deletion packages/common/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export {
hexToBytes,
i2Osp,
isCryptoKeyPair,
isDeno,
isDenoV1,
isNode,
kemToKeyGenAlgorithm,
loadCrypto,
loadSubtleCrypto,
Expand Down
10 changes: 7 additions & 3 deletions packages/common/src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/core/test/cipherSuite.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions packages/hpke-js/test/cipherSuite.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions packages/hpke-js/test/cipherSuiteBackwardCompat.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -372,7 +372,7 @@ describe("CipherSuite(backward-compat)", () => {
});

it("should work normally with importKey('jwk')", async () => {
if (!isNode()) {
if (isDeno()) {
return;
}

Expand Down Expand Up @@ -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();
Expand Down
26 changes: 13 additions & 13 deletions packages/hpke-js/test/conformance.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions packages/hpke-js/test/dhkemPrimitives.test.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
10 changes: 5 additions & 5 deletions packages/hpke-js/test/kemContext.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -200,7 +200,7 @@ describe("deriveKeyPair", () => {
});

it("should return a proper instance with DhkemP521HkdfSha512", async () => {
if (!isNode()) {
if (isDeno()) {
return;
}
const cryptoApi = await loadCrypto();
Expand Down Expand Up @@ -326,7 +326,7 @@ describe("serialize/deserializePublicKey", () => {
});

it("should return a proper instance with DhkemP521HkdfSha512", async () => {
if (!isNode()) {
if (isDeno()) {
return;
}

Expand Down Expand Up @@ -525,7 +525,7 @@ describe("serialize/deserializePrivateKey", () => {
});

it("should return a proper instance with DhkemP521HkdfSha512", async () => {
if (!isNode()) {
if (isDeno()) {
return;
}

Expand Down Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions packages/hpke-js/test/keyValidationEc.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -24,7 +24,7 @@ describe("EC key validation", () => {

describe("P-256", () => {
it("should validate properly", async () => {
if (!isNode()) {
if (isDeno()) {
return;
}

Expand All @@ -49,7 +49,7 @@ describe("EC key validation", () => {

describe("P-384", () => {
it("should validate properly", async () => {
if (!isNode()) {
if (isDeno()) {
return;
}

Expand All @@ -74,7 +74,7 @@ describe("EC key validation", () => {

describe("P-521", () => {
it("should validate properly", async () => {
if (!isNode()) {
if (isDeno()) {
return;
}

Expand Down
12 changes: 6 additions & 6 deletions packages/hpke-js/test/sample.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -284,7 +284,7 @@ describe("README examples", () => {
});

it("should work normally with instances", async () => {
if (!isNode()) {
if (isDeno()) {
return;
}

Expand Down Expand Up @@ -319,7 +319,7 @@ describe("README examples", () => {
});

it("should work normally with importKey('jwk')", async () => {
if (!isNode()) {
if (isDeno()) {
return;
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
Loading

0 comments on commit 97d2c03

Please sign in to comment.