diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..8789181 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,23 @@ +name: Run Linter + +on: + pull_request: + branches: + - main + +permissions: + contents: read + +jobs: + lint: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - uses: denoland/setup-deno@v2 + with: + cache: true + deno-version: v2.x + + - run: deno lint diff --git a/.github/workflows/publish-dry.yml b/.github/workflows/publish-dry.yml new file mode 100644 index 0000000..d7b6b17 --- /dev/null +++ b/.github/workflows/publish-dry.yml @@ -0,0 +1,20 @@ +name: Check Publish + +on: + pull_request: + branches: + - main + +jobs: + publish-dry: + runs-on: ubuntu-latest + + permissions: + contents: read + id-token: write + + steps: + - uses: actions/checkout@v4 + + - name: Publish package + run: npx jsr publish --dry-run diff --git a/.github/workflows/typecheck.yml b/.github/workflows/typecheck.yml new file mode 100644 index 0000000..49a6d99 --- /dev/null +++ b/.github/workflows/typecheck.yml @@ -0,0 +1,23 @@ +name: Type Check + +on: + pull_request: + branches: + - main + +permissions: + contents: read + +jobs: + type-check: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - uses: denoland/setup-deno@v2 + with: + cache: true + deno-version: v2.x + + - run: deno check diff --git a/deno.json b/deno.json index aec04d1..65faab9 100644 --- a/deno.json +++ b/deno.json @@ -3,7 +3,12 @@ "version": "1.1.5", "exports": "./src/index.ts", "publish": { - "include": ["jsr.json", "LICENSE", "README.md", "src/**/*.ts"] + "include": [ + "jsr.json", + "LICENSE", + "README.md", + "src/**/*.ts" + ] }, "tasks": { "console": "deno run --watch examples/write-to-console.ts", @@ -14,6 +19,11 @@ "useBraces": "always", "bracePosition": "sameLineUnlessHanging" }, + "lint": { + "exclude": [ + "examples/" + ] + }, "imports": { "@std/assert": "jsr:@std/assert@^1.0.15", "@std/bytes": "jsr:@std/bytes@^1.0.6", diff --git a/examples/abort-controller.ts b/examples/abort-controller.ts index 3e4cdf2..098b894 100644 --- a/examples/abort-controller.ts +++ b/examples/abort-controller.ts @@ -15,4 +15,4 @@ receiver.on("event", (message) => console.log(message)); controller.abort(); -console.log("Closed the socket") +console.log("Closed the socket"); diff --git a/src/logReceiver.ts b/src/logReceiver.ts index f040371..2f26513 100644 --- a/src/logReceiver.ts +++ b/src/logReceiver.ts @@ -2,7 +2,7 @@ import { EventEmitter } from "node:events"; import { createSocket, type RemoteInfo, type Socket } from "node:dgram"; import { parsePacket } from "./parser.ts"; -import type { EventData, TypedEventEmitter } from "./types.ts"; +import type { EventData, MessageEvents, TypedEventEmitter } from "./types.ts"; /** * The socket options for the UDP socket @@ -30,10 +30,7 @@ export interface LogReceiverOptions { signal?: AbortSignal; } -type MessageEvents = { - error: (error: Error) => void; - event: (message: EventData) => void; -}; +const LogEmitter = EventEmitter as (new () => TypedEventEmitter); /** * An event emitter that will emit a message event when a valid UDP log is created on the server @@ -99,7 +96,7 @@ type MessageEvents = { * * For security reasons, you should always use a log secret to prevent evaluation of potentially malicious messages. Do this by looking at the password field. In order to set up the log secret, you can use the `sv_logsecret` command */ -export class LogReceiver extends (EventEmitter as new () => TypedEventEmitter) implements Disposable { +export class LogReceiver extends LogEmitter implements Disposable { #socket: Socket; /** diff --git a/src/types.ts b/src/types.ts index ae97b66..fdcc78f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,20 @@ import type { RemoteInfo } from "node:dgram"; +/** + * The possible events that can be emitted + */ +export type MessageEvents = { + /** + * Emitted when an error occurs + */ + error: (error: Error) => void; + + /** + * Emitted when data is received from the server + */ + event: (message: EventData) => void; +}; + /** * Represents the parsed information from the message data */