Skip to content

Commit

Permalink
[flatbuffers] Remove '__MEDIASOUP_VERSION__' replacement in JS transp…
Browse files Browse the repository at this point in the history
…iled files (#1198)

- 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.
  • Loading branch information
ibc authored Oct 27, 2023
1 parent 8e605ae commit 5870f7e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 34 deletions.
3 changes: 2 additions & 1 deletion node/src/Worker.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -334,7 +335,7 @@ export class Worker<WorkerAppData extends AppData = AppData>
{
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.
Expand Down
10 changes: 7 additions & 3 deletions node/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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];
Expand All @@ -40,6 +42,8 @@ export { observer };
*/
export { workerBin };

const logger = new Logger();

/**
* Create a Worker.
*/
Expand Down
12 changes: 12 additions & 0 deletions node/src/tests/test-mediasoup.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
30 changes: 0 additions & 30 deletions npm-scripts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ async function run()
{
installNodeDeps();
buildTypescript(/* force */ true);
replaceVersion();

break;
}
Expand Down Expand Up @@ -161,7 +160,6 @@ async function run()
case 'test:node':
{
buildTypescript(/* force */ false);
replaceVersion();
testNode();

break;
Expand All @@ -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');

Expand Down Expand Up @@ -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'))
Expand Down Expand Up @@ -463,7 +434,6 @@ function checkRelease()
installNodeDeps();
flatcNode();
buildTypescript(/* force */ true);
replaceVersion();
buildWorker();
lintNode();
lintWorker();
Expand Down

0 comments on commit 5870f7e

Please sign in to comment.