From e83d81a3eed2ff2c68c35273e5ac66ee49724ed4 Mon Sep 17 00:00:00 2001 From: Ryan Kois Date: Mon, 11 Dec 2023 21:38:17 -0800 Subject: [PATCH] chore: add lint action --- .github/workflows/lint.yaml | 28 +++++++++++++++++++ package.json | 1 + packages/automerge-repo/package.json | 1 + packages/automerge-repo/src/AutomergeUrl.ts | 2 +- .../src/RemoteHeadsSubscriptions.ts | 2 +- packages/automerge-repo/src/Repo.ts | 8 ++++-- .../automerge-repo/src/helpers/debounce.ts | 2 +- .../automerge-repo/src/helpers/throttle.ts | 2 +- .../src/network/NetworkSubsystem.ts | 3 +- .../src/storage/StorageSubsystem.ts | 2 +- .../synchronizer/CollectionSynchronizer.ts | 2 +- .../src/synchronizer/DocSynchronizer.ts | 4 +++ 12 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 .github/workflows/lint.yaml diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml new file mode 100644 index 000000000..d940e7f38 --- /dev/null +++ b/.github/workflows/lint.yaml @@ -0,0 +1,28 @@ +name: Tests +on: + push: + branches: + - main + pull_request: + types: [opened, synchronize, reopened, ready_for_review, review_requested] + branches: + - main +jobs: + run-tests: + if: github.event.pull_request.draft == false + name: Lint Packages + runs-on: ubuntu-latest + steps: + - uses: actions/setup-node@v3 + with: + node-version: '18.x' + registry-url: 'https://registry.npmjs.org' + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + ref: ${{ github.ref }} + - name: Install + run: | + yarn install --no-lockfile + - name: Lint + run: yarn lint diff --git a/package.json b/package.json index 43b751d2f..086f39821 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "pub": "lerna publish --yes", "start:syncserver": "cross-env DEBUG='WebsocketServer' yarn workspace @automerge/example-sync-server start", "repocheck": "manypkg check", + "lint": "lerna run lint", "test": "vitest", "test:log": "cross-env DEBUG='automerge-repo:*' vitest", "test:coverage": "vitest --coverage", diff --git a/packages/automerge-repo/package.json b/packages/automerge-repo/package.json index a96ecab94..17322a05b 100644 --- a/packages/automerge-repo/package.json +++ b/packages/automerge-repo/package.json @@ -9,6 +9,7 @@ "main": "dist/index.js", "scripts": { "build": "tsc", + "lint": "eslint --ext .ts src", "watch": "npm-watch build", "test:coverage": "c8 --reporter=lcov --reporter=html --reporter=text yarn test", "test": "vitest", diff --git a/packages/automerge-repo/src/AutomergeUrl.ts b/packages/automerge-repo/src/AutomergeUrl.ts index fc247ef09..2d4fdc18d 100644 --- a/packages/automerge-repo/src/AutomergeUrl.ts +++ b/packages/automerge-repo/src/AutomergeUrl.ts @@ -33,7 +33,7 @@ export const parseAutomergeUrl = (url: AutomergeUrl) => { export const stringifyAutomergeUrl = ( arg: UrlOptions | DocumentId | BinaryDocumentId ) => { - let documentId = + const documentId = arg instanceof Uint8Array || typeof arg === "string" ? arg : "documentId" in arg diff --git a/packages/automerge-repo/src/RemoteHeadsSubscriptions.ts b/packages/automerge-repo/src/RemoteHeadsSubscriptions.ts index 045565196..f15358d4a 100644 --- a/packages/automerge-repo/src/RemoteHeadsSubscriptions.ts +++ b/packages/automerge-repo/src/RemoteHeadsSubscriptions.ts @@ -326,7 +326,7 @@ export class RemoteHeadsSubscriptions extends EventEmitter { }: DocHandleEncodedChangePayload) => { void storageSubsystem.saveDoc(handle.documentId, doc) } - const debouncedSaveFn = handle.on( + handle.on( "heads-changed", throttle(saveFn, this.saveDebounceRate) ) @@ -153,7 +153,8 @@ export class Repo extends EventEmitter { // The network subsystem deals with sending and receiving messages to and from peers. const myPeerMetadata: Promise = new Promise( - async (resolve, reject) => + // eslint-disable-next-line no-async-promise-executor -- TODO: fix + async (resolve) => resolve({ storageId: await storageSubsystem?.id(), isEphemeral, @@ -300,6 +301,9 @@ export class Repo extends EventEmitter { handler = this.#throttledSaveSyncStateHandlers[storageId] = throttle( ({ documentId, syncState }: SyncStateMessage) => { this.storageSubsystem!.saveSyncState(documentId, storageId, syncState) + .catch(err => { + console.error("error saving sync state", { err }) + }) }, this.saveDebounceRate ) diff --git a/packages/automerge-repo/src/helpers/debounce.ts b/packages/automerge-repo/src/helpers/debounce.ts index 70614c7a4..2c55a37f1 100644 --- a/packages/automerge-repo/src/helpers/debounce.ts +++ b/packages/automerge-repo/src/helpers/debounce.ts @@ -19,7 +19,7 @@ export const throttle = ) => ReturnType>( return function (...args: Parameters) { clearTimeout(timeout) timeout = setTimeout(() => { - fn.apply(null, args) + fn(...args) }, rate) } } diff --git a/packages/automerge-repo/src/helpers/throttle.ts b/packages/automerge-repo/src/helpers/throttle.ts index 7d09a0767..24981549d 100644 --- a/packages/automerge-repo/src/helpers/throttle.ts +++ b/packages/automerge-repo/src/helpers/throttle.ts @@ -36,7 +36,7 @@ export const throttle = ) => ReturnType>( wait = lastCall + delay - Date.now() clearTimeout(timeout) timeout = setTimeout(() => { - fn.apply(null, args) + fn(...args) lastCall = Date.now() }, wait) } diff --git a/packages/automerge-repo/src/network/NetworkSubsystem.ts b/packages/automerge-repo/src/network/NetworkSubsystem.ts index f35c157df..a73110849 100644 --- a/packages/automerge-repo/src/network/NetworkSubsystem.ts +++ b/packages/automerge-repo/src/network/NetworkSubsystem.ts @@ -13,7 +13,6 @@ import { isEphemeralMessage, isValidRepoMessage, } from "./messages.js" -import { StorageId } from "../storage/types.js" type EphemeralMessageSource = `${PeerId}:${SessionId}` @@ -108,6 +107,8 @@ export class NetworkSubsystem extends EventEmitter { this.peerMetadata.then(peerMetadata => { networkAdapter.connect(this.peerId, peerMetadata) + }).catch(err => { + this.#log("error connecting to network", err) }) } diff --git a/packages/automerge-repo/src/storage/StorageSubsystem.ts b/packages/automerge-repo/src/storage/StorageSubsystem.ts index 8b9c32d25..6f202f82f 100644 --- a/packages/automerge-repo/src/storage/StorageSubsystem.ts +++ b/packages/automerge-repo/src/storage/StorageSubsystem.ts @@ -33,7 +33,7 @@ export class StorageSubsystem { } async id(): Promise { - let storedId = await this.#storageAdapter.load(["storage-adapter-id"]) + const storedId = await this.#storageAdapter.load(["storage-adapter-id"]) let id: StorageId if (storedId) { diff --git a/packages/automerge-repo/src/synchronizer/CollectionSynchronizer.ts b/packages/automerge-repo/src/synchronizer/CollectionSynchronizer.ts index 91a35994a..f6846e6f8 100644 --- a/packages/automerge-repo/src/synchronizer/CollectionSynchronizer.ts +++ b/packages/automerge-repo/src/synchronizer/CollectionSynchronizer.ts @@ -2,7 +2,7 @@ import debug from "debug" import { DocHandle } from "../DocHandle.js" import { stringifyAutomergeUrl } from "../AutomergeUrl.js" import { Repo } from "../Repo.js" -import { DocMessage, RepoMessage } from "../network/messages.js" +import { DocMessage } from "../network/messages.js" import { DocumentId, PeerId } from "../types.js" import { DocSynchronizer } from "./DocSynchronizer.js" import { Synchronizer } from "./Synchronizer.js" diff --git a/packages/automerge-repo/src/synchronizer/DocSynchronizer.ts b/packages/automerge-repo/src/synchronizer/DocSynchronizer.ts index 0858d31bf..2af261a51 100644 --- a/packages/automerge-repo/src/synchronizer/DocSynchronizer.ts +++ b/packages/automerge-repo/src/synchronizer/DocSynchronizer.ts @@ -139,6 +139,8 @@ export class DocSynchronizer extends Synchronizer { if (!pendingCallbacks) { this.#onLoadSyncState(peerId).then(syncState => { this.#initSyncState(peerId, syncState ?? A.initSyncState()) + }).catch(err => { + this.#log(`Error loading sync state for ${peerId}: ${err}`) }) pendingCallbacks = this.#pendingSyncStateCallbacks[peerId] = [] } @@ -264,6 +266,8 @@ export class DocSynchronizer extends Synchronizer { if (doc) { this.#sendSyncMessage(peerId, doc) } + }).catch(err => { + this.#log(`Error loading doc for ${peerId}: ${err}`) }) }) })