diff --git a/.github/workflows/backend-tests.yml b/.github/workflows/backend-tests.yml index 09bc44c43..ee3d3547b 100644 --- a/.github/workflows/backend-tests.yml +++ b/.github/workflows/backend-tests.yml @@ -72,14 +72,11 @@ jobs: - uses: actions/checkout@v3 if: needs.changes.outputs.changed == 'true' - - name: Use Node.js ${{ matrix.node-version }} + - name: Setup node if: needs.changes.outputs.changed == 'true' - uses: actions/setup-node@v3 + uses: ./.github/actions/setup-node with: - node-version: ${{ matrix.node-version }} - - - run: yarn install --immutable - if: needs.changes.outputs.changed == 'true' + NODE_VERSION: ${{ env.NODE_VERSION }} - run: yarn run build if: needs.changes.outputs.changed == 'true' @@ -93,14 +90,11 @@ jobs: with: fetch-depth: 0 - - name: Use Node.js ${{ env.NODEJS_VERSION }} + - name: Setup node if: needs.changes.outputs.changed == 'true' - uses: actions/setup-node@v3 + uses: ./.github/actions/setup-node with: - node-version: ${{ env.NODEJS_VERSION }} - - - run: yarn install --immutable - if: needs.changes.outputs.changed == 'true' + NODE_VERSION: ${{ env.NODE_VERSION }} - run: yarn run test:cov if: needs.changes.outputs.changed == 'true' diff --git a/backend/docker/Dockerfile b/backend/docker/Dockerfile index 855e93abb..dec27a1fc 100644 --- a/backend/docker/Dockerfile +++ b/backend/docker/Dockerfile @@ -26,20 +26,27 @@ USER node WORKDIR /usr/src/app COPY --chown=node .yarn/plugins .yarn/plugins +COPY --chown=node .yarn/patches .yarn/patches COPY --chown=node .yarn/releases .yarn/releases COPY --chown=node .yarnrc.yml .yarnrc.yml COPY --chown=node package.json package.json COPY --chown=node yarn.lock yarn.lock COPY --chown=node backend/package.json backend/ +COPY --chown=node commons/package.json commons/ COPY --chown=node frontend/package.json frontend/ # Install dependencies first to not invalidate the cache on every source change RUN --mount=type=cache,sharing=locked,uid=1000,gid=1000,target=/tmp/.yarn \ yarn install --immutable && yarn workspaces focus @hedgedoc/backend +COPY --chown=node commons/ commons/ COPY --chown=node backend/nest-cli.json backend/tsconfig.json backend/tsconfig.build.json backend/ COPY --chown=node backend/src backend/src +WORKDIR /usr/src/app/commons + +RUN yarn build + ## Stage 2a: Dev config files and tests FROM code-with-deps as development USER node diff --git a/backend/package.json b/backend/package.json index 86747103e..80f81f419 100644 --- a/backend/package.json +++ b/backend/package.json @@ -25,7 +25,7 @@ }, "dependencies": { "@azure/storage-blob": "12.12.0", - "@hedgedoc/realtime": "0.3.0", + "@hedgedoc/commons": "workspace:commons", "@mrdrogdrog/optional": "1.0.0", "@nestjs/common": "9.2.1", "@nestjs/config": "2.2.0", diff --git a/backend/src/realtime/realtime-note/realtime-note.spec.ts b/backend/src/realtime/realtime-note/realtime-note.spec.ts index d3dd0791b..e4099dfc3 100644 --- a/backend/src/realtime/realtime-note/realtime-note.spec.ts +++ b/backend/src/realtime/realtime-note/realtime-note.spec.ts @@ -6,7 +6,7 @@ import { encodeDocumentDeletedMessage, encodeMetadataUpdatedMessage, -} from '@hedgedoc/realtime'; +} from '@hedgedoc/commons'; import { Mock } from 'ts-mockery'; import { Note } from '../../notes/note.entity'; diff --git a/backend/src/realtime/realtime-note/realtime-note.ts b/backend/src/realtime/realtime-note/realtime-note.ts index 0a2457370..6ef212364 100644 --- a/backend/src/realtime/realtime-note/realtime-note.ts +++ b/backend/src/realtime/realtime-note/realtime-note.ts @@ -6,10 +6,9 @@ import { encodeDocumentDeletedMessage, encodeMetadataUpdatedMessage, -} from '@hedgedoc/realtime'; +} from '@hedgedoc/commons'; import { Logger } from '@nestjs/common'; -import { EventEmitter } from 'events'; -import TypedEventEmitter, { EventMap } from 'typed-emitter'; +import { EventEmitter2 } from 'eventemitter2'; import { Awareness } from 'y-protocols/awareness'; import { Note } from '../../notes/note.entity'; @@ -17,18 +16,10 @@ import { WebsocketAwareness } from './websocket-awareness'; import { WebsocketConnection } from './websocket-connection'; import { WebsocketDoc } from './websocket-doc'; -export type RealtimeNoteEvents = { - beforeDestroy: () => void; - destroy: () => void; -}; - -type TypedEventEmitterConstructor = - new () => TypedEventEmitter; - /** * Represents a note currently being edited by a number of clients. */ -export class RealtimeNote extends (EventEmitter as TypedEventEmitterConstructor) { +export class RealtimeNote extends EventEmitter2 { protected logger: Logger; private readonly websocketDoc: WebsocketDoc; private readonly websocketAwareness: WebsocketAwareness; diff --git a/backend/src/realtime/realtime-note/test-utils/mock-realtime-note.ts b/backend/src/realtime/realtime-note/test-utils/mock-realtime-note.ts index 0c035f94c..e7ac0198f 100644 --- a/backend/src/realtime/realtime-note/test-utils/mock-realtime-note.ts +++ b/backend/src/realtime/realtime-note/test-utils/mock-realtime-note.ts @@ -3,18 +3,17 @@ * * SPDX-License-Identifier: AGPL-3.0-only */ -import { EventEmitter } from 'events'; +import { EventEmitter2 } from 'eventemitter2'; import { Mock } from 'ts-mockery'; -import TypedEmitter from 'typed-emitter'; import { Note } from '../../../notes/note.entity'; -import { RealtimeNote, RealtimeNoteEvents } from '../realtime-note'; +import { RealtimeNote } from '../realtime-note'; import { WebsocketAwareness } from '../websocket-awareness'; import { WebsocketDoc } from '../websocket-doc'; import { mockAwareness } from './mock-awareness'; import { mockWebsocketDoc } from './mock-websocket-doc'; -class MockRealtimeNote extends (EventEmitter as new () => TypedEmitter) { +class MockRealtimeNote extends EventEmitter2 { constructor( private note: Note, private doc: WebsocketDoc, diff --git a/backend/src/realtime/realtime-note/test-utils/mock-websocket-transporter.ts b/backend/src/realtime/realtime-note/test-utils/mock-websocket-transporter.ts index 46ef5c887..b5ea303f6 100644 --- a/backend/src/realtime/realtime-note/test-utils/mock-websocket-transporter.ts +++ b/backend/src/realtime/realtime-note/test-utils/mock-websocket-transporter.ts @@ -3,13 +3,11 @@ * * SPDX-License-Identifier: AGPL-3.0-only */ -import { WebsocketTransporter } from '@hedgedoc/realtime'; -import { MessageTransporterEvents } from '@hedgedoc/realtime/dist/mjs/y-doc-message-transporter'; -import { EventEmitter } from 'events'; +import { WebsocketTransporter } from '@hedgedoc/commons'; +import { EventEmitter2 } from 'eventemitter2'; import { Mock } from 'ts-mockery'; -import TypedEmitter from 'typed-emitter'; -class MockMessageTransporter extends (EventEmitter as new () => TypedEmitter) { +class MockMessageTransporter extends EventEmitter2 { setupWebsocket(): void { //intentionally left blank } diff --git a/backend/src/realtime/realtime-note/websocket-awareness.spec.ts b/backend/src/realtime/realtime-note/websocket-awareness.spec.ts index c22405037..e27c7f917 100644 --- a/backend/src/realtime/realtime-note/websocket-awareness.spec.ts +++ b/backend/src/realtime/realtime-note/websocket-awareness.spec.ts @@ -3,7 +3,7 @@ * * SPDX-License-Identifier: AGPL-3.0-only */ -import * as hedgedocRealtimeModule from '@hedgedoc/realtime'; +import * as hedgedocRealtimeModule from '@hedgedoc/commons'; import { Mock } from 'ts-mockery'; import { RealtimeNote } from './realtime-note'; @@ -12,6 +12,8 @@ import { ClientIdUpdate, WebsocketAwareness } from './websocket-awareness'; import { WebsocketConnection } from './websocket-connection'; import { WebsocketDoc } from './websocket-doc'; +jest.mock('@hedgedoc/commons'); + describe('websocket-awareness', () => { it('distributes content updates to other synced clients', () => { const mockEncodedUpdate = new Uint8Array([0, 1, 2, 3]); diff --git a/backend/src/realtime/realtime-note/websocket-awareness.ts b/backend/src/realtime/realtime-note/websocket-awareness.ts index 60cc55936..a9d0a963c 100644 --- a/backend/src/realtime/realtime-note/websocket-awareness.ts +++ b/backend/src/realtime/realtime-note/websocket-awareness.ts @@ -3,7 +3,7 @@ * * SPDX-License-Identifier: AGPL-3.0-only */ -import { encodeAwarenessUpdateMessage } from '@hedgedoc/realtime'; +import { encodeAwarenessUpdateMessage } from '@hedgedoc/commons'; import { Awareness } from 'y-protocols/awareness'; import { RealtimeNote } from './realtime-note'; diff --git a/backend/src/realtime/realtime-note/websocket-connection.spec.ts b/backend/src/realtime/realtime-note/websocket-connection.spec.ts index 9388277af..eac4155f4 100644 --- a/backend/src/realtime/realtime-note/websocket-connection.spec.ts +++ b/backend/src/realtime/realtime-note/websocket-connection.spec.ts @@ -3,8 +3,8 @@ * * SPDX-License-Identifier: AGPL-3.0-only */ -import * as hedgedocRealtimeModule from '@hedgedoc/realtime'; -import { WebsocketTransporter } from '@hedgedoc/realtime'; +import * as hedgedocRealtimeModule from '@hedgedoc/commons'; +import { WebsocketTransporter } from '@hedgedoc/commons'; import { Mock } from 'ts-mockery'; import WebSocket from 'ws'; import * as yProtocolsAwarenessModule from 'y-protocols/awareness'; @@ -25,6 +25,8 @@ import { WebsocketDoc } from './websocket-doc'; import SpyInstance = jest.SpyInstance; +jest.mock('@hedgedoc/commons'); + describe('websocket connection', () => { let mockedDoc: WebsocketDoc; let mockedAwareness: WebsocketAwareness; diff --git a/backend/src/realtime/realtime-note/websocket-connection.ts b/backend/src/realtime/realtime-note/websocket-connection.ts index 289bbc064..1058d53fe 100644 --- a/backend/src/realtime/realtime-note/websocket-connection.ts +++ b/backend/src/realtime/realtime-note/websocket-connection.ts @@ -3,7 +3,7 @@ * * SPDX-License-Identifier: AGPL-3.0-only */ -import { WebsocketTransporter } from '@hedgedoc/realtime'; +import { WebsocketTransporter } from '@hedgedoc/commons'; import { Logger } from '@nestjs/common'; import WebSocket from 'ws'; import { Awareness, removeAwarenessStates } from 'y-protocols/awareness'; diff --git a/backend/src/realtime/realtime-note/websocket-doc.spec.ts b/backend/src/realtime/realtime-note/websocket-doc.spec.ts index fedc65fab..7458e2840 100644 --- a/backend/src/realtime/realtime-note/websocket-doc.spec.ts +++ b/backend/src/realtime/realtime-note/websocket-doc.spec.ts @@ -3,7 +3,7 @@ * * SPDX-License-Identifier: AGPL-3.0-only */ -import * as hedgedocRealtimeModule from '@hedgedoc/realtime'; +import * as hedgedocRealtimeModule from '@hedgedoc/commons'; import { Mock } from 'ts-mockery'; import { RealtimeNote } from './realtime-note'; @@ -11,6 +11,8 @@ import { mockConnection } from './test-utils/mock-connection'; import { WebsocketConnection } from './websocket-connection'; import { WebsocketDoc } from './websocket-doc'; +jest.mock('@hedgedoc/commons'); + describe('websocket-doc', () => { it('saves the initial content', () => { const textContent = 'textContent'; diff --git a/backend/src/realtime/realtime-note/websocket-doc.ts b/backend/src/realtime/realtime-note/websocket-doc.ts index effb43f20..36bec32cf 100644 --- a/backend/src/realtime/realtime-note/websocket-doc.ts +++ b/backend/src/realtime/realtime-note/websocket-doc.ts @@ -3,7 +3,7 @@ * * SPDX-License-Identifier: AGPL-3.0-only */ -import { encodeDocumentUpdateMessage } from '@hedgedoc/realtime'; +import { encodeDocumentUpdateMessage } from '@hedgedoc/commons'; import { Doc } from 'yjs'; import { RealtimeNote } from './realtime-note';