From e06bd4892b840f6c42fdf421652f2077c44885da Mon Sep 17 00:00:00 2001 From: Lalit Kapoor Date: Thu, 5 Dec 2024 07:50:21 -0500 Subject: [PATCH] Add support for node v18 (#349) * support v18 * add license * test node v18 in ci * changeset * test node v22 in ci * test latest node version in ci --- .changeset/flat-jeans-agree.md | 5 +++++ .github/workflows/ci.yml | 5 ++++- packages/livekit-server-sdk/package.json | 2 +- packages/livekit-server-sdk/src/WebhookReceiver.ts | 4 ++-- packages/livekit-server-sdk/src/digest.ts | 14 ++++++++++++++ 5 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 .changeset/flat-jeans-agree.md create mode 100644 packages/livekit-server-sdk/src/digest.ts diff --git a/.changeset/flat-jeans-agree.md b/.changeset/flat-jeans-agree.md new file mode 100644 index 00000000..5c38cb73 --- /dev/null +++ b/.changeset/flat-jeans-agree.md @@ -0,0 +1,5 @@ +--- +'livekit-server-sdk': patch +--- + +support for node v18 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fa66c5e9..d224a586 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -70,6 +70,9 @@ jobs: test: name: Test + strategy: + matrix: + node-version: [18, 20, 22, latest] runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -77,7 +80,7 @@ jobs: - name: Setup Node.js uses: actions/setup-node@v4 with: - node-version: 20 + node-version: ${{ matrix.node-version }} cache: pnpm - name: Install dependencies run: pnpm install diff --git a/packages/livekit-server-sdk/package.json b/packages/livekit-server-sdk/package.json index 129da7d3..64949776 100644 --- a/packages/livekit-server-sdk/package.json +++ b/packages/livekit-server-sdk/package.json @@ -53,6 +53,6 @@ "vitest": "^2.0.0" }, "engines": { - "node": ">=19" + "node": ">=18" } } diff --git a/packages/livekit-server-sdk/src/WebhookReceiver.ts b/packages/livekit-server-sdk/src/WebhookReceiver.ts index 37db2476..2cddac13 100644 --- a/packages/livekit-server-sdk/src/WebhookReceiver.ts +++ b/packages/livekit-server-sdk/src/WebhookReceiver.ts @@ -4,6 +4,7 @@ import type { BinaryReadOptions, JsonReadOptions, JsonValue } from '@bufbuild/protobuf'; import { WebhookEvent as ProtoWebhookEvent } from '@livekit/protocol'; import { TokenVerifier } from './AccessToken.js'; +import digest from './digest.js'; export const authorizeHeader = 'Authorize'; @@ -66,8 +67,7 @@ export class WebhookReceiver { } const claims = await this.verifier.verify(authHeader); // confirm sha - const encoder = new TextEncoder(); - const hash = await crypto.subtle.digest('SHA-256', encoder.encode(body)); + const hash = await digest(body); const hashDecoded = btoa( Array.from(new Uint8Array(hash)) .map((v) => String.fromCharCode(v)) diff --git a/packages/livekit-server-sdk/src/digest.ts b/packages/livekit-server-sdk/src/digest.ts new file mode 100644 index 00000000..e0d41718 --- /dev/null +++ b/packages/livekit-server-sdk/src/digest.ts @@ -0,0 +1,14 @@ +// SPDX-FileCopyrightText: 2024 LiveKit, Inc. +// +// SPDX-License-Identifier: Apache-2.0 + +// Use the Web Crypto API if available, otherwise fallback to Node.js crypto +export default async function digest(data: string): Promise { + if (globalThis.crypto?.subtle) { + const encoder = new TextEncoder(); + return crypto.subtle.digest('SHA-256', encoder.encode(data)); + } else { + const nodeCrypto = await import('node:crypto'); + return nodeCrypto.createHash('sha256').update(data).digest(); + } +}