Skip to content

Commit

Permalink
Improve jsdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
illuspas committed Nov 28, 2024
1 parent b6a7c7f commit 10b3499
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 72 deletions.
4 changes: 3 additions & 1 deletion bin/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import NodeMediaServer from "../src/index.js";
import { createRequire } from "module";

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

const nms = new NodeMediaServer(config);
nms.run();
4 changes: 2 additions & 2 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ export default [
jsdoc,
},
rules: {
"jsdoc/require-returns-description": "off",
"jsdoc/valid-types": "error",
"jsdoc/check-types": "error",
"jsdoc/require-returns-description": "off",
"jsdoc/require-param-description": "off",
"no-unused-vars": "off",
"no-undef": "warn",
"no-undef": "error",
"semi": [2, "always"],
"quotes": [2, "double"]
}
Expand Down
7 changes: 7 additions & 0 deletions src/core/avcodec.js
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
// @ts-check
//
// Created by Chen Mingliang on 23/12/01.
// illuspas@msn.com
// Copyright (c) 2023 NodeMedia. All rights reserved.
//

export {};
8 changes: 7 additions & 1 deletion src/core/avpacket.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// @ts-check
//
// Created by Chen Mingliang on 23/12/01.
// illuspas@msn.com
// Copyright (c) 2023 NodeMedia. All rights reserved.
//

export default class AVPacket {
constructor() {
Expand All @@ -11,6 +17,6 @@ export default class AVPacket {
this.offset = 0;

/**@type {Buffer} */
this.data = null;
this.data = Buffer.alloc(0);
}
}
16 changes: 16 additions & 0 deletions src/core/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @ts-check

import BaseSession from "../session/base_session.js";
import BroadcastServer from "../server/broadcast_server.js";

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

/** @type {Map<string, BaseSession>} */
this.sessions = new Map();

/** @type {Map<string, BroadcastServer>} */
this.broadcasts = new Map();
}
}
95 changes: 53 additions & 42 deletions src/core/logger.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,64 @@
// @ts-check
//
// Created by Chen Mingliang on 23/12/01.
// illuspas@msn.com
// Copyright (c) 2023 NodeMedia. All rights reserved.
//

// import Pino from "pino";
// export default Pino();
class Logger {
constructor(level = "info") {
this.levels = ["trace", "debug", "info", "warn", "error"];
this.level = this.levels.includes(level) ? level : "info";
}

log(message, logLevel = "info") {
const messageLevel = this.levels.indexOf(logLevel);
const currentLevel = this.levels.indexOf(this.level);

class Logger {
constructor (level = "info") {
this.levels = ["trace", "debug", "info", "warn", "error"];
this.level = this.levels.includes(level) ? level : "info";
}

log (message, logLevel = "info") {
const messageLevel = this.levels.indexOf(logLevel);
const currentLevel = this.levels.indexOf(this.level);

if (messageLevel >= currentLevel) {
console.log(`[${this.getTime()}] [${logLevel.toUpperCase()}] ${message}`);
}
}

getTime () {
const now = new Date();
return now.toLocaleString();
}

trace (message) {
this.log(message, "trace");
}

debug (message) {
this.log(message, "debug");
}

info (message) {
this.log(message, "info");
}

warn (message) {
this.log(message, "warn");
}

error (message) {
this.log(message, "error");
if (messageLevel >= currentLevel) {
console.log(`[${this.getTime()}] [${logLevel.toUpperCase()}] ${message}`);
}
}


getTime() {
const now = new Date();
return now.toLocaleString();
}

/**
* @param {string} message
*/
trace(message) {
this.log(message, "trace");
}

/**
* @param {string} message
*/
debug(message) {
this.log(message, "debug");
}

/**
* @param {string} message
*/
info(message) {
this.log(message, "info");
}

/**
* @param {string} message
*/
warn(message) {
this.log(message, "warn");
}

/**
* @param {string} message
*/
error(message) {
this.log(message, "error");
}
}

export default new Logger("debug");

8 changes: 2 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@ 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 Package = require("../package.json");


export default class NodeMediaServer {
constructor(config) {
logger.level = "debug";
logger.info(`Node-Media-Server v${Package.version}`);
logger.info(`Homepage: ${Package.homepage}`);
logger.info(`License: ${Package.license}`);
logger.info(`Author: ${Package.author}`);
this.ctx = {
config,
sessions: new Map(),
broadcasts: new Map()
};
this.ctx = new Context(config);
this.httpServer = new NodeHttpServer(this.ctx);
this.rtmpServer = new NodeRtmpServer(this.ctx);
}
Expand Down
5 changes: 5 additions & 0 deletions src/protocol/flv.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
//
// Created by Chen Mingliang on 23/12/01.
// illuspas@msn.com
Expand Down Expand Up @@ -28,6 +29,10 @@ const PacketTypeCodedFramesX = 3;
const PacketTypeMetadata = 4;
const PacketTypeMPEG2TSSequenceStart = 5;

/**
* @class
* @augments EventEmitter
*/
export default class Flv extends EventEmitter {
constructor() {
super();
Expand Down
12 changes: 6 additions & 6 deletions src/server/broadcast_server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

// @ts-check
//
// Created by Chen Mingliang on 23/11/30.
// illuspas@msn.com
Expand All @@ -11,7 +11,7 @@ import BaseSession from "../session/base_session.js";

export default class BroadcastServer {
constructor() {
/** @type {BaseSession} */
/** @type {BaseSession | null} */
this.publisher = null;

/** @type {Map<string, BaseSession>} */
Expand All @@ -20,13 +20,13 @@ export default class BroadcastServer {
/** @type {Buffer} */
this.flvHeader = Flv.createHeader(true, true);

/** @type {Buffer} */
/** @type {Buffer | null} */
this.flvMetaData = null;

/** @type {Buffer} */
/** @type {Buffer | null} */
this.flvAudioHeader = null;

/** @type {Buffer} */
/** @type {Buffer | null} */
this.flvVideoHeader = null;
}

Expand Down Expand Up @@ -61,7 +61,7 @@ export default class BroadcastServer {

/**
* @param {BaseSession} session
* @returns {string}
* @returns {string | null}
*/
postPush = (session) => {
if (this.publisher == null) {
Expand Down
11 changes: 11 additions & 0 deletions src/server/http_server.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
// @ts-check
//
// Created by Chen Mingliang on 23/12/01.
// illuspas@msn.com
// 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 {
/**
* @param {Context} ctx
*/
constructor(ctx) {
this.ctx = ctx;
const app = http2Express(express);
Expand Down
7 changes: 6 additions & 1 deletion src/server/rtmp_server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@

// @ts-check
//
// Created by Chen Mingliang on 23/12/01.
// illuspas@msn.com
// Copyright (c) 2023 Nodemedia. All rights reserved.
//

export default class NodeRtmpServer {
constructor(config) {
Expand Down
4 changes: 4 additions & 0 deletions src/session/base_session.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// @ts-check
//
// Created by Chen Mingliang on 23/11/30.
// illuspas@msn.com
// Copyright (c) 2023 Nodemedia. All rights reserved.
//

import { customAlphabet } from "nanoid";

const nanoid = customAlphabet("1234567890abcdefghijklmnopqrstuvwxyz", 16);


export default class BaseSession {
constructor() {
this.id = nanoid();
Expand All @@ -29,6 +32,7 @@ export default class BaseSession {
}

/**
* @abstract
* @param {Buffer} buffer
*/
sendBuffer = (buffer) => {
Expand Down
29 changes: 16 additions & 13 deletions src/session/flv_session.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
//
// Created by Chen Mingliang on 23/11/30.
// illuspas@msn.com
Expand All @@ -10,19 +11,24 @@ import BroadcastServer from "../server/broadcast_server.js";
import Flv from "../protocol/flv.js";
import AMF from "../protocol/amf.js";
import logger from "../core/logger.js";
import Context from "../core/context.js";

/**
* @class
* @augments BaseSession
*/
export default class FlvSession extends BaseSession {
/**
* @param {*} ctx
* @param {Context} ctx
* @param {express.Request} req
* @param {express.Response} res
*/
constructor(ctx, req, res) {
super(ctx);
super();
this.ctx = ctx;
this.req = req;
this.res = res;
this.ip = req.ip;
this.ip = req.ip ?? "0.0.0.0";
this.protocol = "flv";
this.streamHost = req.hostname;
this.streamApp = req.params.app;
Expand All @@ -35,16 +41,8 @@ export default class FlvSession extends BaseSession {
req.on("error", this.onError);
req.socket.on("close", this.onClose);

/** @type {Map<string, BaseSession>} */
this.sessions = this.ctx.sessions;
this.sessions.set(this.id, this);

/** @type {Map<string, BaseSession>} */
this.broadcasts = this.ctx.broadcasts;

/** @type {BroadcastServer} */
this.broadcast = this.broadcasts.has(this.streamPath) ? this.broadcasts.get(this.streamPath) : new BroadcastServer();
this.broadcasts.set(this.streamPath, this.broadcast);
this.broadcast = this.ctx.broadcasts.get(this.streamPath) ?? new BroadcastServer();
this.ctx.broadcasts.set(this.streamPath, this.broadcast);
}

doPlay = () => {
Expand Down Expand Up @@ -80,6 +78,10 @@ export default class FlvSession extends BaseSession {
}
};

/**
*
* @param {string} err
*/
onError = (err) => {
logger.info(`FLV session ${this.id} ${this.ip} error, ${err}`);
};
Expand Down Expand Up @@ -113,6 +115,7 @@ export default class FlvSession extends BaseSession {
};

/**
* @override
* @param {Buffer} buffer
*/
sendBuffer = (buffer) => {
Expand Down

0 comments on commit 10b3499

Please sign in to comment.