From 7916072a7ec29794d5fe8fad7c935c220dbf6af5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20Baz=20Castillo?= Date: Fri, 27 Oct 2023 11:39:10 +0200 Subject: [PATCH] [flatbuffers] Remove '__MEDIASOUP_VERSION__' replacement in JS transpiled files - Instead of writing into all transpiled JS files in `node/lib/`, read `package.json` just once in launch time and read "version" from it. - I'm not happy with this, not sure if better than before. But clearly we are gonna remove this useless `mediasoup.version` public getter in v4. --- node/src/Worker.ts | 3 ++- node/src/index.ts | 10 +++++++--- node/src/tests/test-mediasoup.ts | 12 ++++++++++++ npm-scripts.mjs | 30 ------------------------------ 4 files changed, 21 insertions(+), 34 deletions(-) diff --git a/node/src/Worker.ts b/node/src/Worker.ts index 5e4f5f7805..d4e930970c 100644 --- a/node/src/Worker.ts +++ b/node/src/Worker.ts @@ -1,6 +1,7 @@ import * as process from 'node:process'; import * as path from 'node:path'; import { spawn, ChildProcess } from 'node:child_process'; +import { version } from './'; import { Logger } from './Logger'; import { EnhancedEventEmitter } from './EnhancedEventEmitter'; import * as ortc from './ortc'; @@ -334,7 +335,7 @@ export class Worker { env : { - MEDIASOUP_VERSION : '__MEDIASOUP_VERSION__', + MEDIASOUP_VERSION : version, // Let the worker process inherit all environment variables, useful // if a custom and not in the path GCC is used so the user can set // LD_LIBRARY_PATH environment variable for runtime. diff --git a/node/src/index.ts b/node/src/index.ts index c0cf76ba4d..4277b665d9 100644 --- a/node/src/index.ts +++ b/node/src/index.ts @@ -1,3 +1,5 @@ +import * as fs from 'node:fs'; +import * as path from 'node:path'; import { Logger } from './Logger'; import { EnhancedEventEmitter } from './EnhancedEventEmitter'; import { workerBin, Worker, WorkerSettings } from './Worker'; @@ -14,15 +16,15 @@ export { types }; /** * Expose mediasoup version. */ -export const version = '__MEDIASOUP_VERSION__'; +export const { version } = JSON.parse(fs.readFileSync( + path.join(__dirname, '..', '..', 'package.json'), { encoding: 'utf-8' } +)); /** * Expose parseScalabilityMode() function. */ export { parse as parseScalabilityMode } from './scalabilityModes'; -const logger = new Logger(); - export type ObserverEvents = { newworker: [Worker]; @@ -40,6 +42,8 @@ export { observer }; */ export { workerBin }; +const logger = new Logger(); + /** * Create a Worker. */ diff --git a/node/src/tests/test-mediasoup.ts b/node/src/tests/test-mediasoup.ts index ea080b7bb6..0032e49322 100644 --- a/node/src/tests/test-mediasoup.ts +++ b/node/src/tests/test-mediasoup.ts @@ -1,10 +1,22 @@ +import * as fs from 'node:fs'; +import * as path from 'node:path'; import * as mediasoup from '../'; +const PKG = JSON.parse(fs.readFileSync( + path.join(__dirname, '..', '..', '..', 'package.json'), { encoding: 'utf-8' }) +); + const { + version, getSupportedRtpCapabilities, parseScalabilityMode } = mediasoup; +test('mediasoup.version matches version field in package.json', () => +{ + expect(version).toBe(PKG.version); +}); + test('mediasoup.getSupportedRtpCapabilities() returns the mediasoup RTP capabilities', () => { const rtpCapabilities = getSupportedRtpCapabilities(); diff --git a/npm-scripts.mjs b/npm-scripts.mjs index 6403f1adc9..290a4b3a5d 100644 --- a/npm-scripts.mjs +++ b/npm-scripts.mjs @@ -96,7 +96,6 @@ async function run() { installNodeDeps(); buildTypescript(/* force */ true); - replaceVersion(); break; } @@ -161,7 +160,6 @@ async function run() case 'test:node': { buildTypescript(/* force */ false); - replaceVersion(); testNode(); break; @@ -177,7 +175,6 @@ async function run() case 'coverage:node': { buildTypescript(/* force */ false); - replaceVersion(); executeCmd('jest --coverage'); executeCmd('open-cli coverage/lcov-report/index.html'); @@ -272,32 +269,6 @@ async function run() } } -function replaceVersion() -{ - logInfo('replaceVersion()'); - - const files = fs.readdirSync('node/lib', - { - withFileTypes : true, - recursive : true - }); - - for (const file of files) - { - if (!file.isFile()) - { - continue; - } - - // NOTE: dirent.path is only available in Node >= 20. - const filePath = path.join(file.path ?? 'node/lib', file.name); - const text = fs.readFileSync(filePath, { encoding: 'utf8' }); - const result = text.replace(/__MEDIASOUP_VERSION__/g, PKG.version); - - fs.writeFileSync(filePath, result, { encoding: 'utf8' }); - } -} - function deleteNodeLib() { if (!fs.existsSync('node/lib')) @@ -463,7 +434,6 @@ function checkRelease() installNodeDeps(); flatcNode(); buildTypescript(/* force */ true); - replaceVersion(); buildWorker(); lintNode(); lintWorker();