AvanzaCast/packages/backend-api/scripts/test_generate_token.cjs
Cesar Mendivil 8b458a3ddf feat: add initial LiveKit Meet integration with utility scripts, configs, and core components
- Add Next.js app structure with base configs, linting, and formatting
- Implement LiveKit Meet page, types, and utility functions
- Add Docker, Compose, and deployment scripts for backend and token server
- Provide E2E and smoke test scaffolding with Puppeteer and Playwright helpers
- Include CSS modules and global styles for UI
- Add postMessage and studio integration utilities
- Update package.json with dependencies and scripts for development and testing
2025-11-20 12:50:38 -07:00

65 lines
3.1 KiB
JavaScript

#!/usr/bin/env node
// CommonJS diagnostic script: tries to load livekit-server-sdk and generate an AccessToken
// Usage: node scripts/test_generate_token.cjs
require('dotenv').config();
const LIVEKIT_API_KEY = process.env.LIVEKIT_API_KEY || 'devkey';
const LIVEKIT_API_SECRET = process.env.LIVEKIT_API_SECRET || 'secret';
const ROOM = process.argv[2] || 'diagnostic-room';
const IDENTITY = process.argv[3] || 'diag-user';
(async function main(){
console.log('LIVEKIT_API_KEY=', LIVEKIT_API_KEY ? '[set]' : '[not set]');
console.log('LIVEKIT_API_SECRET=', LIVEKIT_API_SECRET ? '[set]' : '[not set]');
try {
const sdk = require('livekit-server-sdk');
console.log('livekit-server-sdk imported:', !!sdk);
const AccessToken = sdk.AccessToken || (sdk.default && sdk.default.AccessToken);
const VideoGrant = sdk.VideoGrant || (sdk.default && sdk.default.VideoGrant);
if (!AccessToken) {
console.error('AccessToken class not found on SDK exports. Export keys:', Object.keys(sdk));
process.exit(2);
}
const at = new AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET, { identity: IDENTITY, name: IDENTITY });
try {
if (VideoGrant) {
const g = new VideoGrant({ room: ROOM });
if (typeof at.addGrant === 'function') at.addGrant(g);
else if (typeof at.add_grant === 'function') at.add_grant(g);
else console.warn('No addGrant method on AccessToken instance; skipping addGrant');
} else {
if (typeof at.addGrant === 'function') at.addGrant({ room: ROOM, roomJoin: true, canPublish: true, canSubscribe: true });
else console.warn('No VideoGrant and no addGrant method; continuing');
}
} catch (e) {
console.warn('Grant add failed:', String(e));
}
const token = (typeof at.toJwt === 'function') ? await at.toJwt() : at.jwt;
console.log('Generated token (prefix 200 chars):', token ? token.slice(0,200) : token);
// try decode payload (unsafe inspect)
try {
const parts = token.split('.');
if (parts.length >= 2) {
const payloadB64 = parts[1].replace(/-/g, '+').replace(/_/g, '/');
const padded = payloadB64 + '='.repeat((4 - (payloadB64.length % 4)) % 4);
const payloadJson = Buffer.from(padded, 'base64').toString('utf8');
console.log('Token payload sample:', payloadJson.slice(0,1000));
}
} catch (e) { console.warn('Failed to decode token payload', e); }
} catch (err) {
console.error('Failed to import or use livekit-server-sdk:', String(err));
console.log('Attempting fallback: generate mock JWT (HMAC) to verify environment');
try {
const jwt = require('jsonwebtoken');
const payload = { iss: LIVEKIT_API_KEY, sub: IDENTITY, room: ROOM, iat: Math.floor(Date.now()/1000), exp: Math.floor(Date.now()/1000)+3600 };
const token = jwt.sign(payload, LIVEKIT_API_SECRET, { algorithm: 'HS256' });
console.log('Generated mock JWT (prefix 200 chars):', token.slice(0,200));
console.log('Decoded payload:', jwt.decode(token));
} catch (e) {
console.error('Fallback JWT generation failed:', String(e));
}
process.exit(0);
}
})();