Skip to content

Commit

Permalink
Change TSLint to ESLint (#193)
Browse files Browse the repository at this point in the history
* Initial eslint

* Remove types simple-peer

* ESLint for core module

* Convert JS class factory code to TS

* Remove redundant version declaration

* Hls.js: rewrite initialize from JS to TS

* Shaka: rewrite initialize from JS to TS

* Update package versions

* Fix Webpack 5 build

* Smaller size for hlsjs module via Browserify

* Make Webpack config more common

* Use eslint everywhere

* WIP: Fixing linter issues in hlsjs engine

* Better console handling

* Disable errors on triple slash dirictives

* Add types to loader interface messages

* Disable types in tests

* Normalize demo dependencies

* Fix setInterval return type compiler error

* Fix hls.js module linter errors

* Forgotten package lock file

* Types for Shaka player with ESLint fixes

* Fix linter issues
  • Loading branch information
mrlika authored Mar 30, 2021
1 parent 85adce9 commit 4921ba0
Show file tree
Hide file tree
Showing 68 changed files with 12,966 additions and 11,598 deletions.
3 changes: 3 additions & 0 deletions p2p-media-loader-core/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
dist
build
32 changes: 32 additions & 0 deletions p2p-media-loader-core/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": ["./tsconfig.json", "./tsconfig.test.json"]
},
"plugins": [
"@typescript-eslint"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking"
],
"env": {
"browser": true
},
"rules": {
"quotes": ["error", "double"],
"no-console": ["error", { "allow": ["warn", "error"] }],
"eqeqeq": "error",
"brace-style": ["error", "1tbs"],
"eol-last": "error",
"func-call-spacing": "error",
"no-trailing-spaces": "error",
"no-multi-spaces": "error",
"@typescript-eslint/no-misused-promises": ["error", { "checksVoidReturn": false }],
"@typescript-eslint/require-await": "off",
"@typescript-eslint/triple-slash-reference": "off"
}
}
8 changes: 2 additions & 6 deletions p2p-media-loader-core/.npmignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
tsconfig.json
tsconfig.test.json
tsconfig.tslint.json
tslint.json
.*
tsconfig.*
lib
test
.editorconfig
build/dist
webpackfile.js
*.tgz
10 changes: 5 additions & 5 deletions p2p-media-loader-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ If `settings` is specified, then the default settings (shown below) will be over
### SegmentsStorage interface
```typescript
interface SegmentsStorage {
storeSegment(segment: Segment): Promise<void>;
getSegmentsMap(masterSwarmId: string): Promise<Map<string, {segment: Segment}>>;
getSegment(id: string, masterSwarmId: string): Promise<Segment | undefined>;
clean(lockedSementsfilter?: (id: string) => boolean): Promise<boolean>;
destroy(): Promise<void>;
storeSegment: (segment: Segment) => Promise<void>;
getSegmentsMap: (masterSwarmId: string) => Promise<Map<string, {segment: Segment}>>;
getSegment: (id: string, masterSwarmId: string) => Promise<Segment | undefined>;
clean: (lockedSegmentsFilter?: (id: string) => boolean) => Promise<boolean>;
destroy: () => Promise<void>;
}
```

Expand Down
11 changes: 6 additions & 5 deletions p2p-media-loader-core/lib/bandwidth-approximator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,21 @@ export class BandwidthApproximator {
private currentBytesSum = 0;
private lastBandwidth: NumberWithTime[] = [];

public addBytes(bytes: number, timeStamp: number): void {
public addBytes = (bytes: number, timeStamp: number): void => {
this.lastBytes.push(new NumberWithTime(bytes, timeStamp));
this.currentBytesSum += bytes;

while (timeStamp - this.lastBytes[0].timeStamp > SMOOTH_INTERVAL) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.currentBytesSum -= this.lastBytes.shift()!.value;
}

this.lastBandwidth.push(new NumberWithTime(this.currentBytesSum / SMOOTH_INTERVAL, timeStamp));
}

// in bytes per millisecond
public getBandwidth(timeStamp: number): number {
while (this.lastBandwidth.length != 0 && timeStamp - this.lastBandwidth[0].timeStamp > MEASURE_INTERVAL) {
public getBandwidth = (timeStamp: number): number => {
while (this.lastBandwidth.length !== 0 && timeStamp - this.lastBandwidth[0].timeStamp > MEASURE_INTERVAL) {
this.lastBandwidth.shift();
}

Expand All @@ -53,11 +54,11 @@ export class BandwidthApproximator {
return maxBandwidth;
}

public getSmoothInterval(): number {
public getSmoothInterval = (): number => {
return SMOOTH_INTERVAL;
}

public getMeasureInterval(): number {
public getMeasureInterval = (): number => {
return MEASURE_INTERVAL;
}
}
26 changes: 0 additions & 26 deletions p2p-media-loader-core/lib/browser-init-webpack.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@
* limitations under the License.
*/

declare const URL: any;
import debug from "debug";
import * as events from "events";

import "./browser-init";

window.p2pml._shared = { debug, events };
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
* limitations under the License.
*/

import * as p2pml from "./index";

if (!window.p2pml) {
window.p2pml = {};
}

window.p2pml.shaka = require("p2p-media-loader-shaka");
window.p2pml.core = p2pml;
9 changes: 5 additions & 4 deletions p2p-media-loader-core/lib/declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
*/

declare module "bittorrent-tracker/client";
declare module "get-browser-rtc";
declare module "simple-peer";
declare module "sha.js/sha1";

declare const __P2PML_VERSION__: string;
declare module "sha.js/sha1" {
import { HashStatic } from "sha.js";
const sha1: HashStatic;
export default sha1;
}
43 changes: 22 additions & 21 deletions p2p-media-loader-core/lib/http-media-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* limitations under the License.
*/

import * as Debug from "debug";
import Debug from "debug";

import { STEEmitter } from "./stringly-typed-event-emitter";
import { Segment } from "./loader-interface";
import { SegmentValidatorCallback, XhrSetupCallback, SegmentUrlBuilder } from "./hybrid-loader";
Expand All @@ -23,8 +24,8 @@ export class HttpMediaManager extends STEEmitter<
"segment-loaded" | "segment-error" | "bytes-downloaded"
> {

private xhrRequests: Map<string, {xhr: XMLHttpRequest, segment: Segment}> = new Map();
private failedSegments: Map<string, number> = new Map();
private xhrRequests = new Map<string, {xhr: XMLHttpRequest, segment: Segment}>();
private failedSegments = new Map<string, number>();
private debug = Debug("p2pml:http-media-manager");

public constructor(readonly settings: {
Expand All @@ -37,7 +38,7 @@ export class HttpMediaManager extends STEEmitter<
super();
}

public download(segment: Segment, downloadedPieces?: ArrayBuffer[]): void {
public download = (segment: Segment, downloadedPieces?: ArrayBuffer[]): void => {
if (this.isDownloading(segment)) {
return;
}
Expand Down Expand Up @@ -82,7 +83,7 @@ export class HttpMediaManager extends STEEmitter<
xhr.send();
}

public abort(segment: Segment): void {
public abort = (segment: Segment): void => {
const request = this.xhrRequests.get(segment.id);

if (request) {
Expand All @@ -92,46 +93,46 @@ export class HttpMediaManager extends STEEmitter<
}
}

public isDownloading(segment: Segment): boolean {
public isDownloading = (segment: Segment): boolean => {
return this.xhrRequests.has(segment.id);
}

public isFailed(segment: Segment): boolean {
public isFailed = (segment: Segment): boolean => {
const time = this.failedSegments.get(segment.id);
return time !== undefined && time > this.now();
}

public getActiveDownloads(): ReadonlyMap<string, {segment: Segment}> {
public getActiveDownloads = (): ReadonlyMap<string, {segment: Segment}> => {
return this.xhrRequests;
}

public getActiveDownloadsCount(): number {
public getActiveDownloadsCount = (): number => {
return this.xhrRequests.size;
}

public destroy(): void {
public destroy = (): void => {
this.xhrRequests.forEach(request => request.xhr.abort());
this.xhrRequests.clear();
}

private setupXhrEvents(xhr: XMLHttpRequest, segment: Segment, downloadedPieces?: ArrayBuffer[]) {
private setupXhrEvents = (xhr: XMLHttpRequest, segment: Segment, downloadedPieces?: ArrayBuffer[]) => {
let prevBytesLoaded = 0;

xhr.addEventListener("progress", (event: any) => {
xhr.addEventListener("progress", (event) => {
const bytesLoaded = event.loaded - prevBytesLoaded;
this.emit("bytes-downloaded", bytesLoaded);
prevBytesLoaded = event.loaded;
});

xhr.addEventListener("load", async (event: any) => {
if ((event.target.status < 200) || (event.target.status >= 300)) {
xhr.addEventListener("load", async (event) => {
if ((xhr.status < 200) || (xhr.status >= 300)) {
this.segmentFailure(segment, event, xhr);
return;
}

let data = event.target.response;
let data = xhr.response as ArrayBuffer;

if ((downloadedPieces !== undefined) && (event.target.status === 206)) {
if ((downloadedPieces !== undefined) && (xhr.status === 206)) {
let bytesDownloaded = 0;
for (const piece of downloadedPieces) {
bytesDownloaded += piece.byteLength;
Expand All @@ -152,16 +153,16 @@ export class HttpMediaManager extends STEEmitter<
await this.segmentDownloadFinished(segment, data, xhr);
});

xhr.addEventListener("error", (event: any) => {
xhr.addEventListener("error", (event: unknown) => {
this.segmentFailure(segment, event, xhr);
});

xhr.addEventListener("timeout", (event: any) => {
xhr.addEventListener("timeout", (event: unknown) => {
this.segmentFailure(segment, event, xhr);
});
}

private async segmentDownloadFinished(segment: Segment, data: ArrayBuffer, xhr: XMLHttpRequest) {
private segmentDownloadFinished = async (segment: Segment, data: ArrayBuffer, xhr: XMLHttpRequest) => {
segment.responseUrl = xhr.responseURL === null ? undefined : xhr.responseURL;

if (this.settings.segmentValidator) {
Expand All @@ -178,15 +179,15 @@ export class HttpMediaManager extends STEEmitter<
this.emit("segment-loaded", segment, data);
}

private segmentFailure(segment: Segment, error: any, xhr: XMLHttpRequest) {
private segmentFailure = (segment: Segment, error: unknown, xhr: XMLHttpRequest) => {
segment.responseUrl = xhr.responseURL === null ? undefined : xhr.responseURL;

this.xhrRequests.delete(segment.id);
this.failedSegments.set(segment.id, this.now() + this.settings.httpFailedSegmentTimeout);
this.emit("segment-error", segment, error);
}

private cleanTimedOutFailedSegments() {
private cleanTimedOutFailedSegments = () => {
const now = this.now();
const candidates: string[] = [];

Expand Down
Loading

0 comments on commit 4921ba0

Please sign in to comment.