Skip to content

Commit

Permalink
Change from esm to cjs
Browse files Browse the repository at this point in the history
  • Loading branch information
illuspas committed Dec 3, 2024
1 parent c7c5d5b commit 48d6274
Show file tree
Hide file tree
Showing 18 changed files with 171 additions and 99 deletions.
5 changes: 1 addition & 4 deletions bin/app.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import NodeMediaServer from "../src/index.js";
import { createRequire } from "module";

const require = createRequire(import.meta.url);
const NodeMediaServer = require("..");
const config = require("./config.json");

const nms = new NodeMediaServer(config);
Expand Down
12 changes: 7 additions & 5 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import globals from "globals";
import pluginJs from "@eslint/js";
import jsdoc from "eslint-plugin-jsdoc";
const eslint = require("eslint");
const globals = require("globals");
const pluginJs = require("@eslint/js");
const jsdoc = require("eslint-plugin-jsdoc");

/** @type {import('eslint').Linter.Config[]} */
export default [
/** @type {eslint.Linter.Config[]} */
module.exports = [
pluginJs.configs.recommended,
jsdoc.configs["flat/recommended"],
{ files: ["**/*.js"], languageOptions: { sourceType: "commonjs" } },
{
languageOptions: { globals: globals.node },
plugins: {
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "node-media-server",
"description": "A Node.js implementation of RTMP Server",
"version": "4.0.1",
"type": "module",
"bin": "bin/app.js",
"main": "src/index.js",
"scripts": {
Expand All @@ -21,7 +20,6 @@
"license": "Apache-2.0",
"devDependencies": {
"@eslint/js": "^9.15.0",
"@types/node": "^22.10.1",
"eslint": "^9.15.0",
"eslint-plugin-jsdoc": "50.6.0",
"globals": "^15.12.0"
Expand All @@ -32,6 +30,6 @@
"nanoid": "^5.0.9"
},
"engines": {
"node": ">=18.0.0"
"node": ">=16.0.0"
}
}
4 changes: 1 addition & 3 deletions src/core/avcodec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
// Created by Chen Mingliang on 23/12/01.
// illuspas@msn.com
// Copyright (c) 2023 NodeMedia. All rights reserved.
//

export {};
//
6 changes: 4 additions & 2 deletions src/core/avpacket.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Copyright (c) 2023 NodeMedia. All rights reserved.
//

export default class AVPacket {
class AVPacket {
constructor() {
this.codec_id = 0;
this.codec_type = 0;
Expand All @@ -17,4 +17,6 @@ export default class AVPacket {
this.offset = 0;
this.data = Buffer.alloc(0);
}
}
}

module.exports = AVPacket;
10 changes: 6 additions & 4 deletions src/core/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
// Copyright (c) 2023 NodeMedia. All rights reserved.
//

import BaseSession from "../session/base_session.js";
import BroadcastServer from "../server/broadcast_server.js";
const BaseSession = require("../session/base_session.js");
const BroadcastServer = require( "../server/broadcast_server.js");

export default class Context {
class Context {
constructor(config) {
this.config = config;

Expand All @@ -18,4 +18,6 @@ export default class Context {
/** @type {Map<string, BroadcastServer>} */
this.broadcasts = new Map();
}
}
}

module.exports = Context;
2 changes: 1 addition & 1 deletion src/core/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ class Logger {
}
}

export default new Logger("debug");
module.exports = new Logger("debug");
17 changes: 8 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@
// Copyright (c) 2024 NodeMedia. All rights reserved.
//

import NodeHttpServer from "./server/http_server.js";
import NodeRtmpServer from "./server/rtmp_server.js";
import { createRequire } from "module";
import logger from "./core/logger.js";
import Context from "./core/context.js";

const require = createRequire(import.meta.url);
const NodeHttpServer = require("./server/http_server.js");
const NodeRtmpServer = require("./server/rtmp_server.js");
const logger = require("./core/logger.js");
const Context = require("./core/context.js");
const Package = require("../package.json");

export default class NodeMediaServer {
class NodeMediaServer {
constructor(config) {
logger.level = "debug";
logger.info(`Node-Media-Server v${Package.version}`);
Expand All @@ -30,4 +27,6 @@ export default class NodeMediaServer {
this.httpServer.run();
this.rtmpServer.run();
}
}
}

module.exports = NodeMediaServer;
4 changes: 2 additions & 2 deletions src/protocol/amf.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* This module provides encoding and decoding of the AMF0 format
*/

import logger from "../core/logger.js";
const logger = require("../core/logger.js");


const amf0dRules = {
Expand Down Expand Up @@ -669,7 +669,7 @@ function encodeAmf0Data(opt) {
}


export {
module.exports = {
decodeAmf0Cmd,
encodeAmf0Cmd,
decodeAmf0Data,
Expand Down
9 changes: 5 additions & 4 deletions src/protocol/flv.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
// Copyright (c) 2023 Nodemedia. All rights reserved.
//

import logger from "../core/logger.js";
import AVPacket from "../core/avpacket.js";
import * as AMF from "./amf.js";
const logger = require("../core/logger.js");
const AVPacket = require("../core/avpacket.js");
const AMF = require("./amf.js");

const FLV_MEDIA_TYPE_AUDIO = 8;
const FLV_MEDIA_TYPE_VIDEO = 9;
Expand Down Expand Up @@ -63,7 +63,7 @@ const PacketTypeMPEG2TSSequenceStart = 5;
/**
* @class
*/
export default class Flv {
class Flv {
constructor() {
this.parserBuffer = Buffer.alloc(13);
this.parserState = FLV_PARSE_INIT;
Expand Down Expand Up @@ -285,3 +285,4 @@ export default class Flv {
};
}

module.exports = Flv;
16 changes: 9 additions & 7 deletions src/protocol/rtmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
// Copyright (c) 2024 Nodemedia. All rights reserved.
//

import crypto from "node:crypto";
import logger from "../core/logger.js";
import AVPacket from "../core/avpacket.js";
import Flv from "./flv.js";
import * as AMF from "./amf.js";
const crypto = require("node:crypto");
const logger = require("../core/logger.js");
const AVPacket = require("../core/avpacket.js");
const Flv = require("./flv.js");
const AMF = require("./amf.js");

const N_CHUNK_STREAM = 8;
const RTMP_VERSION = 3;
Expand Down Expand Up @@ -237,7 +237,7 @@ class RtmpPacket {
}
}

export default class Rtmp {
class Rtmp {
constructor() {
this.handshakePayload = Buffer.alloc(RTMP_HANDSHAKE_SIZE);
this.handshakeState = RTMP_HANDSHAKE_UNINIT;
Expand Down Expand Up @@ -850,4 +850,6 @@ export default class Rtmp {
this.sendStatusMessage(this.streamId, "status", "NetStream.Play.Start", "Started playing stream.");
this.sendRtmpSampleAccess();
}
}
}

module.exports = Rtmp;
12 changes: 7 additions & 5 deletions src/server/broadcast_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
// Copyright (c) 2023 NodeMedia. All rights reserved.
//

import AVPacket from "../core/avpacket.js";
import Flv from "../protocol/flv.js";
import Rtmp from "../protocol/rtmp.js";
import BaseSession from "../session/base_session.js";
const AVPacket = require("../core/avpacket.js");
const Flv = require("../protocol/flv.js");
const Rtmp = require("../protocol/rtmp.js");
const BaseSession = require("../session/base_session.js");

export default class BroadcastServer {
class BroadcastServer {
constructor() {
/** @type {BaseSession | null} */
this.publisher = null;
Expand Down Expand Up @@ -178,3 +178,5 @@ export default class BroadcastServer {
});
};
}

module.exports = BroadcastServer;
24 changes: 13 additions & 11 deletions src/server/http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
// Copyright (c) 2023 Nodemedia. All rights reserved.
//

import fs from "node:fs";
import http from "node:http";
import http2 from "node:http2";
import express from "express";
import http2Express from "http2-express-bridge";
import FlvSession from "../session/flv_session.js";
import logger from "../core/logger.js";
import Context from "../core/context.js";

export default class NodeHttpServer {
const fs = require("fs");
const http = require("http");
const http2 = require("http2");
const express = require("express");
const http2Express = require("http2-express-bridge");
const FlvSession = require("../session/flv_session.js");
const logger = require("../core/logger.js");
const Context = require("../core/context.js");

class NodeHttpServer {
/**
* @param {Context} ctx
*/
Expand All @@ -31,7 +31,7 @@ export default class NodeHttpServer {
res.header("Access-Control-Allow-Origin", "*");
req.method === "OPTIONS" ? res.sendStatus(200) : next();
});

app.all("/:app/:name.flv", this.handleFlv);

this.server1 = http.createServer(app);
Expand All @@ -56,3 +56,5 @@ export default class NodeHttpServer {
session.run();
};
}

module.exports = NodeHttpServer;
14 changes: 8 additions & 6 deletions src/server/rtmp_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
// Copyright (c) 2023 Nodemedia. All rights reserved.
//

import Context from "../core/context.js";
import net from "node:net";
import logger from "../core/logger.js";
import RtmpSession from "../session/rtmp_session.js";
const Context = require("../core/context.js");
const net = require("net");
const logger = require("../core/logger.js");
const RtmpSession = require("../session/rtmp_session.js");

export default class NodeRtmpServer {
class NodeRtmpServer {
/**
* @param {Context} ctx
*/
Expand All @@ -33,4 +33,6 @@ export default class NodeRtmpServer {
const session = new RtmpSession(this.ctx, socket);
session.run();
};
}
}

module.exports = NodeRtmpServer;
Loading

0 comments on commit 48d6274

Please sign in to comment.