Skip to content

Commit

Permalink
Switch from eslint + prettier to biome (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
kixelated authored Nov 9, 2024
1 parent d96e5b5 commit 8b75654
Show file tree
Hide file tree
Showing 51 changed files with 690 additions and 3,459 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: biomejs/setup-biome@v2
with:
version: latest

- run: npm ci
- run: npm run build
- run: npm run lint
- run: biome ci .
1 change: 0 additions & 1 deletion .prettierignore

This file was deleted.

5 changes: 2 additions & 3 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"astro-build.astro-vscode",
"ms-vscode.vscode-typescript-next",
"EditorConfig.EditorConfig",
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
],
"biomejs.biome"
]
}
15 changes: 6 additions & 9 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
{
"eslint.validate": [
"javascript",
"javascriptreact",
"astro", // Enable .astro
"typescript", // Enable .ts
"typescriptreact" // Enable .tsx
],
"eslint.run": "onSave",
"editor.codeActionsOnSave": {
"source.fixAll": "explicit"
},
"editor.formatOnSave": true, // Tell VSCode to format files on save
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[javascript]": {
"editor.defaultFormatter": "biomejs.biome"
},
"[typescript]": {
"editor.defaultFormatter": "biomejs.biome"
},
"search.exclude": {
// Avoid polluting search results with lockfile content
"package-lock.json": true
Expand Down
32 changes: 32 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
"vcs": {
"enabled": false,
"clientKind": "git",
"useIgnoreFile": false
},
"files": {
"ignoreUnknown": false,
"ignore": ["node_modules", "lib/dist", "web/dist"]
},
"formatter": {
"enabled": true,
"indentStyle": "tab",
"lineWidth": 120
},
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"javascript": {
"formatter": {
"quoteStyle": "double",
"semicolons": "asNeeded"
}
}
}
56 changes: 0 additions & 56 deletions lib/.eslintrc.cjs

This file was deleted.

4 changes: 0 additions & 4 deletions lib/.prettierrc.yaml

This file was deleted.

9 changes: 6 additions & 3 deletions lib/common/async.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export class Deferred<T> {
promise: Promise<T>
resolve!: (value: T | PromiseLike<T>) => void
reject!: (reason: any) => void
reject!: (reason: unknown) => void
pending = true

constructor() {
Expand Down Expand Up @@ -39,12 +39,15 @@ export class Watch<T> {
}

// If we're given a function, call it with the current value
let value: T
if (v instanceof Function) {
v = v(this.#current[0])
value = v(this.#current[0])
} else {
value = v
}

const next = new Deferred<WatchNext<T>>()
this.#current = [v, next.promise]
this.#current = [value, next.promise]
this.#next.resolve(this.#current)
this.#next = next
}
Expand Down
10 changes: 5 additions & 5 deletions lib/common/error.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// I hate javascript
export function asError(e: any): Error {
export function asError(e: unknown): Error {
if (e instanceof Error) {
return e
} else if (typeof e === "string") {
}
if (typeof e === "string") {
return new Error(e)
} else {
return new Error(String(e))
}
return new Error(String(e))
}

export function isError(e: any): e is Error {
export function isError(e: unknown): e is Error {
return e instanceof Error
}
2 changes: 1 addition & 1 deletion lib/common/hex.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export function decode(str: string): Uint8Array {
const bytes = new Uint8Array(str.length / 2)
for (let i = 0; i < bytes.length; i += 1) {
bytes[i] = parseInt(str.slice(2 * i, 2 * i + 2), 16)
bytes[i] = Number.parseInt(str.slice(2 * i, 2 * i + 2), 16)
}
return bytes
}
Expand Down
4 changes: 2 additions & 2 deletions lib/common/ring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

enum STATE {
READ_POS = 0, // The current read position
WRITE_POS, // The current write position
LENGTH, // Clever way of saving the total number of enums values.
WRITE_POS = 1, // The current write position
LENGTH = 2, // Clever way of saving the total number of enums values.
}

interface FrameCopyToOptions {
Expand Down
6 changes: 3 additions & 3 deletions lib/contribute/audio.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Deferred } from "../common/async"
import { Frame } from "../karp/frame"
import { Group, Track } from "../transfork"
import type { Frame } from "../karp/frame"
import type { Group, Track } from "../transfork"
import { Closed } from "../transfork/error"

const SUPPORTED = [
Expand Down Expand Up @@ -46,7 +46,7 @@ export class Packer {
this.#current.writeFrame(frame.data)
}

#close(err?: any) {
#close(err?: unknown) {
const closed = Closed.from(err)
if (this.#current) {
this.#current.close(closed)
Expand Down
10 changes: 7 additions & 3 deletions lib/contribute/broadcast.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Transfork from "../transfork"
import * as Catalog from "../karp/catalog"
import * as Transfork from "../transfork"
import * as Audio from "./audio"
import * as Video from "./video"

Expand Down Expand Up @@ -31,14 +31,18 @@ export class Broadcast {
}

async publish(connection: Transfork.Connection) {
const broadcast: Catalog.Broadcast = { path: this.#config.path, audio: [], video: [] }
const broadcast: Catalog.Broadcast = {
path: this.#config.path,
audio: [],
video: [],
}

for (const media of this.#config.media.getTracks()) {
const settings = media.getSettings()

const info = {
name: media.id, // TODO way too verbose
priority: media.kind == "video" ? 1 : 2,
priority: media.kind === "video" ? 1 : 2,
}

const track = new Transfork.Track(this.#config.path.concat(info.name), info.priority)
Expand Down
6 changes: 4 additions & 2 deletions lib/contribute/segment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Frame } from "../karp/frame"
import type { Frame } from "../karp/frame"

export class Segment {
id: number
Expand All @@ -16,7 +16,9 @@ export class Segment {

// Set a max size for each segment, dropping the tail if it gets too long.
// We tee the reader, so this limit applies to the FASTEST reader.
const backpressure = new ByteLengthQueuingStrategy({ highWaterMark: 8_000_000 })
const backpressure = new ByteLengthQueuingStrategy({
highWaterMark: 8_000_000,
})

const transport = new TransformStream<Frame, Uint8Array>(
{
Expand Down
9 changes: 5 additions & 4 deletions lib/contribute/track.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Segment } from "./segment"
import { Notify } from "../common/async"
import { BroadcastConfig } from "./broadcast"
import type { BroadcastConfig } from "./broadcast"
import { Segment } from "./segment"

import type { Frame } from "../karp/frame"
import * as Audio from "./audio"
import * as Video from "./video"
import { Frame } from "../karp/frame"

export class Track {
name: string
Expand Down Expand Up @@ -138,7 +138,8 @@ export class Track {
if (this.#error) {
controller.error(this.#error)
return
} else if (this.#closed) {
}
if (this.#closed) {
controller.close()
return
}
Expand Down
9 changes: 5 additions & 4 deletions lib/contribute/video.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Deferred } from "../common/async"
import { Frame } from "../karp/frame"
import { Group, Track } from "../transfork"
import type { Frame } from "../karp/frame"
import type { Group, Track } from "../transfork"
import { Closed } from "../transfork/error"

const SUPPORTED = [
Expand Down Expand Up @@ -48,7 +48,7 @@ export class Packer {
frame.encode(this.#current)
}

#close(err?: any) {
#close(err?: unknown) {
const closed = Closed.from(err)
if (this.#current) {
this.#current.close(closed)
Expand Down Expand Up @@ -146,7 +146,8 @@ export class Encoder {
this.#keyframeCounter = 0
} else {
this.#keyframeCounter += 1
if (this.#keyframeCounter + this.#encoder.encodeQueueSize >= 2 * this.#encoderConfig.framerate!) {
const framesPerGop = this.#encoderConfig.framerate ? 2 * this.#encoderConfig.framerate : 60
if (this.#keyframeCounter + this.#encoder.encodeQueueSize >= framesPerGop) {
this.#keyframeNext = true
}
}
Expand Down
15 changes: 9 additions & 6 deletions lib/karp/catalog/audio.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { decodeTrack, Track } from "./track"
import { type Track, decodeTrack } from "./track"

export interface Audio {
track: Track
Expand All @@ -8,10 +8,13 @@ export interface Audio {
bitrate?: number
}

export function decodeAudio(o: any): o is Audio {
if (!decodeTrack(o.track)) return false
if (typeof o.codec !== "string") return false
if (typeof o.sample_rate !== "number") return false
if (typeof o.channel_count !== "number") return false
export function decodeAudio(o: unknown): o is Audio {
if (typeof o !== "object" || o === null) return false

const obj = o as Partial<Audio>
if (!decodeTrack(obj.track)) return false
if (typeof obj.codec !== "string") return false
if (typeof obj.sample_rate !== "number") return false
if (typeof obj.channel_count !== "number") return false
return true
}
13 changes: 8 additions & 5 deletions lib/karp/catalog/broadcast.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Transfork from "../../transfork"
import { decodeAudio, Audio } from "./audio"
import { decodeVideo, Video } from "./video"
import { type Audio, decodeAudio } from "./audio"
import { type Video, decodeVideo } from "./video"

export interface Broadcast {
path: string[]
Expand Down Expand Up @@ -46,14 +46,17 @@ export async function fetch(connection: Transfork.Connection, path: string[]): P
}
}

export function decodeBroadcast(catalog: any): catalog is Broadcast {
export function decodeBroadcast(o: unknown): o is Broadcast {
if (typeof o !== "object" || o === null) return false

const catalog = o as Partial<Broadcast>
if (catalog.audio === undefined) catalog.audio = []
if (!Array.isArray(catalog.audio)) return false
if (!catalog.audio.every((track: any) => decodeAudio(track))) return false
if (!catalog.audio.every((track: unknown) => decodeAudio(track))) return false

if (catalog.video === undefined) catalog.video = []
if (!Array.isArray(catalog.video)) return false
if (!catalog.video.every((track: any) => decodeVideo(track))) return false
if (!catalog.video.every((track: unknown) => decodeVideo(track))) return false

return true
}
8 changes: 4 additions & 4 deletions lib/karp/catalog/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Video } from "./video"
import { Audio } from "./audio"
import { Track } from "./track"
import { Broadcast, decode, encode, fetch } from "./broadcast"
import type { Audio } from "./audio"
import { type Broadcast, decode, encode, fetch } from "./broadcast"
import type { Track } from "./track"
import type { Video } from "./video"

export type { Audio, Video, Track, Broadcast }
export { encode, decode, fetch }
Loading

0 comments on commit 8b75654

Please sign in to comment.