Skip to content

Commit

Permalink
Minor improvements (#313)
Browse files Browse the repository at this point in the history
- Removed redundant dependencies
- Add linter import rules
- Reconfigured tsconfig
- Fix failing test
- Prebuffer defaults
- Updated circleci node image
  • Loading branch information
ch1ller0 authored Oct 26, 2021
1 parent 6f70140 commit ebb1245
Show file tree
Hide file tree
Showing 24 changed files with 1,656 additions and 2,834 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ orbs:
defaults: &defaults
working_directory: ~/repo
docker:
- image: circleci/node:10.15
- image: cimg/node:lts

tag-filter: &tag-filter
filters:
Expand Down
17 changes: 14 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,31 @@
"plugin:prettier/recommended"
],
"parserOptions": {
"project": "tsconfig.dev.json"
"project": "./tsconfig.dev.json"
},
"rules": {
"class-methods-use-this": 0,
"default-case": 0,
"import/prefer-default-export": 0,
"import/no-default-export": 2,
"import/no-extraneous-dependencies": 2,
"import/no-default-export": 1,
"import/order": [2, {
"groups": ["builtin", "external", "internal", "parent", "sibling", "index", "object", "type"]
}],
"import/extensions": 0,
"@typescript-eslint/no-explicit-any": 2,
"no-underscore-dangle": 0,
"no-console": 2,
"no-param-reassign": 0,
"prefer-destructuring": 1,
"prettier/prettier": [
2,
{
"singleQuote": true,
"semi": true,
"bracketSpacing": true,
"printWidth": 120
"printWidth": 120,
"trailingComma": "all"
}
]
}
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ lib
coverage
yarn-error.log
.clinic
examples/music/.test
examples/music/.test
*.tsbuildinfo
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Grigory Gorshkov
Copyright (c) 2021 @ch1ller0

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
46 changes: 22 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "@fridgefm/radio-core",
"author": "Grigory Gorshkov",
"version": "3.0.2",
"version": "3.1.2",
"description": "internet radio engine made on NodeJS platform",
"license": "MIT",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"engines": {
"node": ">=8.3.0"
"node": ">=12.22.2"
},
"keywords": [
"radio",
Expand Down Expand Up @@ -36,39 +36,37 @@
"test:cov": "yarn test --collectCoverage=true"
},
"dependencies": {
"chalk": "^4.1.0",
"chalk": "^4.1.2",
"dev-null": "^0.1.1",
"express": "^4.17.1",
"fs-extra": "^9.1.0",
"get-mp3-duration": "^1.0.0",
"highland": "^2.13.5",
"klaw-sync": "^6.0.0",
"lodash": "^4.17.20",
"node-id3": "^0.2.2",
"typed-emitter": "^1.3.1",
"node-id3": "^0.2.3",
"typed-emitter": "^1.4.0",
"winston": "^3.3.3"
},
"devDependencies": {
"@types/express": "^4.17.11",
"@types/fs-extra": "^9.0.7",
"@types/highland": "^2.12.11",
"@types/jest": "^26.0.20",
"@types/klaw-sync": "^6.0.0",
"@types/lodash": "^4.14.168",
"@types/node": "^14.14.25",
"@typescript-eslint/eslint-plugin": "^4.29.0",
"eslint": "^7.32.0",
"@types/fs-extra": "^9.0.13",
"@types/highland": "^2.12.14",
"@types/jest": "^27.0.2",
"@types/klaw-sync": "^6.0.1",
"@types/node": "^16.11.4",
"@typescript-eslint/eslint-plugin": "^5.1.0",
"@typescript-eslint/parser": "^5.1.0",
"express": "^4.17.1",
"eslint": "^8.1.0",
"eslint-config-airbnb-typescript": "^12.3.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jest": "^24.1.3",
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-jest": "^25.2.2",
"eslint-plugin-prettier": "^4.0.0",
"husky": "^4.3.8",
"jest": "^26.6.3",
"lint-staged": ">=10.5.4",
"prettier": "^2.3.2",
"ts-jest": "^26.5.0",
"typescript": "4.2.2"
"husky": "^7.0.4",
"jest": "^27.3.1",
"lint-staged": ">=11.2.3",
"prettier": "^2.4.1",
"ts-jest": "^27.0.7",
"typescript": "^4.4.4"
},
"bugs": {
"url": "https://github.com/ch1ller0/fridgefm-radio-core/issues"
Expand Down
9 changes: 8 additions & 1 deletion src/__tests__/happy-path/station.hp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ describe('public/HappyPath/Station', () => {
});

describe('control methods', () => {
const station = new Station();
const getPlaying = (pl) => pl.filter((track) => track.isPlaying);

// as
describe('start method', () => {
it('playlist empty - start method wont execute', () => {
const station = new Station();

station.start();
expect(getPlaying(station.getPlaylist())).toEqual([]);

Expand All @@ -33,12 +35,17 @@ describe('public/HappyPath/Station', () => {
});

it('playlist not empty - start method executes', () => {
const station = new Station();
station.addFolder(pathToMusic);
station.start();
expect(getPlaying(station.getPlaylist()).length).toEqual(1);
});
});

it('next method - switches to next track', () => {
const station = new Station();
station.addFolder(pathToMusic);

const playing1 = getPlaying(station.getPlaylist());

station.next();
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/test-utils.mock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable max-classes-per-file */
import * as fs from 'fs-extra';
import * as id3 from 'node-id3';
import fs from 'fs-extra';
import id3 from 'node-id3';

export const pathToMusic = `${process.cwd()}/examples/music`;

Expand Down
4 changes: 2 additions & 2 deletions src/base/Playlist/Playlist.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createList } from '../../utils/fs';
import { createTrackMap } from './methods';
import { captureTime } from '../../utils/time';
import { PUBLIC_EVENTS } from '../../features/EventBus/events';
import { createTrackMap } from './methods';

import type { TPlaylist, TrackMap, TrackList, ReorderCb, PathList, PlaylistElement } from './Playlist.types';
import type { EventBus } from '../../features/EventBus/EventBus';
Expand Down Expand Up @@ -54,7 +54,7 @@ export class Playlist implements TPlaylist {
} else {
this._currentIndex += 1;
}
const nextPath = this._list[this._currentIndex];
const nextPath = this._list[this._currentIndex] as string;
const nextTrack = this._tracksMap.get(nextPath);

if (!nextTrack) {
Expand Down
6 changes: 4 additions & 2 deletions src/base/Playlist/methods.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Mp3 from '../../utils/mp3';
import Mp3 from '../../utils/mp3';
import { extractLast, shuffleArray } from '../../utils/funcs';
import { Track } from '../Track/Track';

Expand All @@ -25,7 +25,9 @@ export const SHUFFLE_METHODS = {
({ to, from }: { to: number; from: number }) =>
(arr: TrackList) => {
const movedElement = arr.splice(from, 1)[0];
arr.splice(to, 0, movedElement);
if (movedElement) {
arr.splice(to, 0, movedElement);
}

return arr;
},
Expand Down
13 changes: 9 additions & 4 deletions src/base/Queuestream.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as devnull from 'dev-null';
import { Readable, Transform, Writable } from 'stream';
import devnull from 'dev-null';
import { captureTime } from '../utils/time';
import { Prebuffer } from '../features/Prebuffer';
import { EventBus } from '../features/EventBus/EventBus';
import { PUBLIC_EVENTS } from '../features/EventBus/events';

import type { EventBus } from '../features/EventBus/EventBus';
import type { TPlaylist } from './Playlist/Playlist.types';

type Deps = {
Expand All @@ -16,7 +17,7 @@ export class QueueStream {

// this stream is always live
private _current = new Transform({
transform: (chunk, encoding, callback) => {
transform: (chunk, _, callback) => {
this._prebuffer.modify([chunk]);
callback(undefined, chunk);
},
Expand All @@ -35,7 +36,11 @@ export class QueueStream {
}

private _handleError(error: Error, event: string) {
this._deps.eventBus.emit(PUBLIC_EVENTS.ERROR, { name: 'queuestream', error, event });
this._deps.eventBus.emit(PUBLIC_EVENTS.ERROR, {
name: 'queuestream',
error,
event,
});
this.next();
}

Expand Down
9 changes: 4 additions & 5 deletions src/base/Station.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { noop } from 'lodash';
import type { Response, Request } from 'express';
import { QueueStream } from './Queuestream';
import { Playlist } from './Playlist/Playlist';
import { EventBus } from '../features/EventBus/EventBus';
import { PUBLIC_EVENTS } from '../features/EventBus/events';
import { captureTime } from '../utils/time';
import { mergeConfig, Config } from '../config/index';
import { Playlist } from './Playlist/Playlist';
import { QueueStream } from './Queuestream';

import type { ClientRequest, ServerResponse } from 'http';
import type { TStation } from '../types/public.h';
import type { TEmitter } from '../features/EventBus/events';
import type { TPlaylist, ReorderCb } from './Playlist/Playlist.types';
Expand Down Expand Up @@ -58,7 +57,7 @@ export class Station implements TStation {
return this._deps.playlist.getList();
}

public connectListener(_: Request, res: Response, cb = noop) {
public connectListener(_: ClientRequest, res: ServerResponse, cb = () => {}) {
const { currentPipe, getPrebuffer } = this._deps.queuestream;

res.writeHead(200, this._deps.config.responseHeaders);
Expand Down
2 changes: 1 addition & 1 deletion src/base/Track/__tests__/methods.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as devnull from 'dev-null';
import devnull from 'dev-null';
import { getStats, getMetaAsync, createSoundStream } from '../methods';
import { pathToMusic, tracks, TestFile } from '../../../__tests__/test-utils.mock';

Expand Down
26 changes: 15 additions & 11 deletions src/base/Track/methods.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import * as fs from 'fs-extra';
import * as _ from 'highland';
import * as id3 from 'node-id3';
import type { Tags } from 'node-id3';
import { Readable } from 'stream';
import fs from 'fs-extra';
import _ from 'highland';
import id3 from 'node-id3';
import { extractLast } from '../../utils/funcs';
import { getDateFromMsecs } from '../../utils/time';
import * as Mp3 from '../../utils/mp3';
import Mp3 from '../../utils/mp3';

import type { Readable } from 'stream';
import type { Tags } from 'node-id3';
import type { ShallowTrackMeta, TrackPath, TrackStats } from './Track.types';

const getMetaAsync = async (stats: TrackStats): Promise<ShallowTrackMeta> => {
Expand All @@ -25,11 +26,11 @@ const getMetaAsync = async (stats: TrackStats): Promise<ShallowTrackMeta> => {
...rest,
origin: 'id3',
});
})
}),
);
};

const getStats = (fullPath: TrackPath) => {
const getStats = (fullPath: TrackPath): TrackStats => {
const file = fs.readFileSync(fullPath);
const [directory, fullName] = extractLast(fullPath, '/');
const duration = Mp3.getDuration(file);
Expand All @@ -56,16 +57,19 @@ const createSoundStream = ({ fullPath, bitrate, tagsSize }: TrackStats): [Error
throw new Error(`Not a file: '${fullPath}'`);
}

const stream = _(fs.createReadStream(fullPath, { highWaterMark: bitrate }));
// @ts-ignore
const hlStream = _(fs.createReadStream(fullPath, { highWaterMark: bitrate })) as Highland.Stream<Buffer>;

const comp = _.seq(
// @ts-ignore
_.drop(Math.floor(tagsSize / bitrate)), // remove id3tags from stream
// @ts-ignore
_.ratelimit(1, 1000)
// _.slice(60, 80), // for debuggine purposes
// @ts-ignore
_.ratelimit(1, 1000),
);

return [null, comp(stream)];
return [null, comp(hlStream)];
} catch (e) {
// skip track if it is not accessible
// @ts-ignore
Expand Down
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const DEFAULTS = {
PREBUFFER_LENGTH: 12,
};
10 changes: 5 additions & 5 deletions src/features/EventBus/EventBus.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EventEmitter } from 'events';
import { logger } from '../../utils/logger';
import { PUBLIC_EVENTS } from './events';
import { defaultConfig } from '../../config/index';
import { PUBLIC_EVENTS } from './events';

import type { TEmitter, InfoEvent, ErrorEvent } from './events';

Expand All @@ -17,7 +17,7 @@ export class EventBus {
this.on(PUBLIC_EVENTS.INFO, ({ level, ...rest }: InfoEvent) => logger.log(level || 'info', { ...rest }));

this.on(PUBLIC_EVENTS.ERROR, (event: ErrorEvent) =>
logger.log('error', { ...event, message: event.error.message })
logger.log('error', { ...event, message: event.error.message }),
);
}

Expand All @@ -27,23 +27,23 @@ export class EventBus {
event: PUBLIC_EVENTS.NEXT_TRACK,
message: tr.fsStats.stringified,
timings,
})
}),
);

this.on(PUBLIC_EVENTS.START, (_, timings) =>
this.emit(PUBLIC_EVENTS.INFO, {
event: PUBLIC_EVENTS.START,
message: 'Station started',
timings,
})
}),
);

this.on(PUBLIC_EVENTS.RESTART, (_, timings) =>
this.emit(PUBLIC_EVENTS.INFO, {
event: PUBLIC_EVENTS.RESTART,
message: 'Station restarted',
timings,
})
}),
);
}

Expand Down
Loading

0 comments on commit ebb1245

Please sign in to comment.