Skip to content

Commit a6ccfd3

Browse files
committed
feat: improve build safety
1 parent 5ef13c0 commit a6ccfd3

File tree

9 files changed

+100
-111
lines changed

9 files changed

+100
-111
lines changed

__tests__/test.spec.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ import * as assert from 'assert';
1313
import { range } from 'rxjs';
1414
import { filter } from 'rxjs/operators';
1515

16-
import { Session } from '..';
16+
import { Session } from '../index.js';
1717

18-
describe('RTPSession', () => {
18+
import { Packet } from '../lib/index.js';
1919

20-
test('packet send-recieve serialize-deserialize', done => {
20+
describe('RTPSession', () => {
21+
test('packet send-recieve serialize-deserialize', (done) => {
2122
const s = new Session(1373);
22-
s.on('message', (msg) => {
23+
s.on('message', (msg: Packet) => {
2324
assert.equal(s.sequenceNumber, msg.sequenceNumber + 1);
2425
assert.equal(s.ssrc, msg.ssrc);
2526
assert.equal('Hello world', msg.payload.toString());
@@ -29,26 +30,31 @@ describe('RTPSession', () => {
2930
s.send(Buffer.from('Hello world')).catch((err) => {
3031
done(err);
3132
});
32-
})
33+
});
3334

34-
test('rxjs', done => {
35+
test('rxjs', (done) => {
3536
const s = new Session(1372, 72);
3637
const initialSequenceNumber = s.sequenceNumber;
3738

38-
s.message$.pipe(
39-
filter(msg => msg.sequenceNumber === initialSequenceNumber + 9)
40-
).subscribe((msg) => {
41-
assert.equal(72, msg.payloadType);
42-
assert.equal('Hello world of rxjs - 10', msg.payload.toString());
43-
s.close();
44-
done();
45-
});
39+
s.message$
40+
.pipe(filter((msg) => msg.sequenceNumber === initialSequenceNumber + 9))
41+
.subscribe({
42+
next: (msg) => {
43+
assert.equal(72, msg.payloadType);
44+
assert.equal('Hello world of rxjs - 10', msg.payload.toString());
45+
s.close();
46+
done();
47+
},
48+
error: (err) => {
49+
done(err);
50+
},
51+
});
4652

4753
range(1, 10).subscribe((i: number) => {
4854
s.send(Buffer.from(`Hello world of rxjs - ${i}`)).catch((err) => {
49-
done(err)
50-
})
55+
done(err);
56+
});
5157
});
52-
})
58+
});
59+
});
5360

54-
})

examples/e2.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { Session, ReadRTPStream, WriteRTPStream } from "..";
1+
import { Session, ReadRTPStream, WriteRTPStream } from "../index.js";
22

33
const s = new Session(1372);
44
const r = new ReadRTPStream(s);
55
const w = new WriteRTPStream(s, "127.0.0.1");
66

7-
r.on("data", (chunk) => {
7+
r.on("data", (chunk: Buffer) => {
88
console.log(chunk.toString());
99
});
1010

index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from "./lib";
1+
export * from "./lib/index.js";

lib/Control.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,24 @@ export class ControlSR {
3737
}
3838

3939
public static deserialize(buff: Buffer): ControlSR {
40+
if (buff.length < 28) {
41+
throw new Error("invalid rtcp packet");
42+
}
4043
// header
4144

4245
// buff[0] = (V << 6 | P << 5 | RC)
43-
if ((buff[0] & (0xc0 >> 6)) !== 2) {
46+
if ((buff[0]! & (0xc0 >> 6)) !== 2) {
4447
throw new Error("invalid rtcp packet");
4548
}
4649
// buff[1] = PT
47-
if (buff[1] !== 200) {
50+
if (buff[1]! !== 200) {
4851
throw new Error("invalid rtcp packet");
4952
}
5053
// buff[2, 3] = length
5154
const length: number = (buff.readUInt16BE(2) + 1) * 4;
55+
if (buff.length < length) {
56+
throw new Error("invalid rtcp packet");
57+
}
5258
if (buff.length !== length) {
5359
throw new Error("invalid rtcp packet");
5460
}

lib/Packet.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ export class Packet {
5656

5757
/* CSRC section */
5858
for (let i = 0; i < this.csrc.length; i++) {
59-
buff.writeUInt32BE(this.csrc[i], 12 + i * 4);
59+
const csrc = this.csrc[i];
60+
if (csrc === undefined) {
61+
continue;
62+
}
63+
buff.writeUInt32BE(csrc, 12 + i * 4);
6064
}
6165

6266
this.payload.copy(buff, 12 + this.csrc.length * 4, 0);
@@ -65,15 +69,21 @@ export class Packet {
6569
}
6670

6771
static deserialize(buff: Buffer): Packet {
72+
if (buff.length < 12) {
73+
throw new Error("invalid rtp packet");
74+
}
6875
const csrc: Array<number> = [];
6976

7077
/* buff[0] = (V << 6 | P << 5 | X << 4 | CC) */
71-
if ((buff[0] & 0xc0) >> 6 !== 2) {
72-
throw new Error(`invalid rtp packet ${buff[0] & 0xc0}`);
78+
if ((buff[0]! & 0xc0) >> 6 !== 2) {
79+
throw new Error(`invalid rtp packet ${buff[0]! & 0xc0}`);
80+
}
81+
const cc = buff[0]! & 0x0f;
82+
if (buff.length < 12 + cc * 4) {
83+
throw new Error("invalid rtp packet");
7384
}
74-
const cc = buff[0] & 0x0f;
7585
/* buff[1] = (M << 7 | PT) */
76-
const payloadType = buff[1] & 0x7f;
86+
const payloadType = buff[1]! & 0x7f;
7787
/* buff[2, 3] = SN */
7888
const sequenceNumber = buff.readUInt16BE(2);
7989
/* buff[4, 5, 6, 7] = TS */

lib/Session.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,10 @@ import * as dgram from "dgram";
1212
import { EventEmitter } from "events";
1313
import { Observable, fromEvent } from "rxjs";
1414

15-
import { Packet } from "./Packet";
16-
import { ControlSR } from "./Control";
15+
import { Packet } from "./Packet.js";
16+
import { ControlSR } from "./Control.js";
1717
import { Readable, Writable } from "stream";
1818

19-
export declare interface Session {
20-
on(
21-
event: "message",
22-
listener: (msg: Packet, rinfo: dgram.RemoteInfo) => void,
23-
): this;
24-
on(event: "close", listener: () => void): this;
25-
on(event: string, listener: Function): this;
26-
}
27-
2819
export class ReadRTPStream extends Readable {
2920
private onMessage: (msg: Packet) => void;
3021

@@ -40,7 +31,7 @@ export class ReadRTPStream extends Readable {
4031
};
4132
}
4233

43-
_read(_size?: number) {
34+
_read() {
4435
if (this.session.listeners("message").indexOf(this.onMessage) === -1) {
4536
this.session.on("message", this.onMessage);
4637
}
@@ -74,6 +65,16 @@ export class WriteRTPStream extends Writable {
7465
* communicating with RTP.
7566
*/
7667
export class Session extends EventEmitter {
68+
public on(
69+
event: "message" | "close" | string,
70+
listener:
71+
| ((msg: Packet, rinfo: dgram.RemoteInfo) => void)
72+
| (() => void)
73+
| ((...args: unknown[]) => void),
74+
): this {
75+
return super.on(event, listener as (...args: any[]) => void);
76+
}
77+
7778
/*
7879
* The SSRC field identifies
7980
* the synchronization source

lib/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* krtp implements real-time protocol based on RFC 3350.
33
*/
4-
export * from "./Packet";
5-
export * from "./Session";
6-
export * from "./Control";
4+
export * from "./Packet.js";
5+
export * from "./Session.js";
6+
export * from "./Control.js";

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "3.2.1",
44
"description": "Node implementation of rtp protocol. RFC 3550",
55
"main": "dist/index.js",
6+
"type": "module",
67
"repository": {
78
"type": "git",
89
"url": "git+https://github.com/1995parham/krtp"

tsconfig.json

Lines changed: 33 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,37 @@
11
{
2+
// Visit https://aka.ms/tsconfig to read more about this file
23
"compilerOptions": {
3-
/* Visit https://aka.ms/tsconfig.json to read more about this file */
4-
5-
/* Basic Options */
6-
// "incremental": true, /* Enable incremental compilation */
7-
"target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */,
8-
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
9-
// "lib": [], /* Specify library files to be included in the compilation. */
10-
// "allowJs": true, /* Allow javascript files to be compiled. */
11-
// "checkJs": true, /* Report errors in .js files. */
12-
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', 'react', 'react-jsx' or 'react-jsxdev'. */
13-
// "declaration": true /* Generates corresponding '.d.ts' file. */,
14-
// "declarationMap": true /* Generates a sourcemap for each corresponding '.d.ts' file. */,
15-
// "sourceMap": true, /* Generates corresponding '.map' file. */
16-
// "outFile": "./", /* Concatenate and emit output to single file. */
17-
"outDir": "./dist" /* Redirect output structure to the directory. */,
18-
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
19-
// "composite": true, /* Enable project compilation */
20-
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
21-
// "removeComments": true, /* Do not emit comments to output. */
22-
// "noEmit": true, /* Do not emit outputs. */
23-
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
24-
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
25-
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
26-
27-
/* Strict Type-Checking Options */
28-
"strict": true /* Enable all strict type-checking options. */,
29-
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
30-
// "strictNullChecks": true, /* Enable strict null checks. */
31-
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
32-
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
33-
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
34-
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
35-
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
36-
37-
/* Additional Checks */
38-
// "noUnusedLocals": true, /* Report errors on unused locals. */
39-
// "noUnusedParameters": true, /* Report errors on unused parameters. */
40-
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
41-
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
42-
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
43-
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an 'override' modifier. */
44-
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
45-
46-
/* Module Resolution Options */
47-
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
48-
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
49-
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
50-
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
51-
// "typeRoots": [], /* List of folders to include type definitions from. */
52-
// "types": [], /* Type declaration files to be included in compilation. */
53-
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
54-
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
55-
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
56-
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
57-
58-
/* Source Map Options */
59-
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
60-
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
61-
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
62-
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
63-
64-
/* Experimental Options */
65-
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
66-
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
67-
68-
/* Advanced Options */
69-
"skipLibCheck": true /* Skip type checking of declaration files. */,
70-
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
4+
// File Layout
5+
// "rootDir": "./src",
6+
// "outDir": "./dist",
7+
// Environment Settings
8+
// See also https://aka.ms/tsconfig/module
9+
"module": "nodenext",
10+
"target": "esnext",
11+
"lib": ["esnext"],
12+
"types": ["node", "jest"],
13+
// and npm install -D @types/node
14+
// Other Outputs
15+
"sourceMap": false,
16+
"declaration": false,
17+
"declarationMap": false,
18+
// Stricter Typechecking Options
19+
"noUncheckedIndexedAccess": true,
20+
"exactOptionalPropertyTypes": true,
21+
// Style Options
22+
// "noImplicitReturns": true,
23+
// "noImplicitOverride": true,
24+
// "noUnusedLocals": true,
25+
// "noUnusedParameters": true,
26+
// "noFallthroughCasesInSwitch": true,
27+
// "noPropertyAccessFromIndexSignature": true,
28+
// Recommended Options
29+
"strict": true,
30+
"jsx": "react-jsx",
31+
"verbatimModuleSyntax": true,
32+
"isolatedModules": true,
33+
"noUncheckedSideEffectImports": true,
34+
"moduleDetection": "force",
35+
"skipLibCheck": true
7136
}
7237
}

0 commit comments

Comments
 (0)