[]
        
(Showing Draft Content)

OT ミドルウェア

js-collaboration-ot  のミドルウェア機構を使うと、開発者はサーバー側の 操作変換(Operational Transformation、OT)処理フローに介入し、権限検証やログ記録などの処理をカスタマイズできます。

このドキュメントでは、ライフサイクル、登録方法、実用的な例を含め、ミドルウェアの使用方法を説明します。

基本概念

ミドルウェアは DocumentServices の中核となる拡張機能であり、オペレーション(op)およびスナップショット(snapshot)の処理ステージで ユーザー定義ロジックを追加 するために使用します。

目的:

  • オペレーションフローに介入し、検証を行う

  • コンテキストを変更する、または処理を中断する

適用シナリオ: ユーザー認証、データ検証、ログ記録、パフォーマンス監視など。

フックとの違い: ミドルウェアは処理フローを中断してエラーを返せますが、フックはイベントを監視するだけです。

ライフサイクル

ミドルウェアは OT 処理フローの複数ステージをカバーし、各ステージはアクションに対応します。開発者はこれらのステージに選択的に介入できます。以下に、サポートされるミドルウェア action とトリガータイミングを示します。

アクション

説明

receive

サーバーがクライアントメッセージを受信したときにトリガーされ、初期検証またはログ記録に使用します。

submit

オペレーション(op)をサブミットしようとするときにトリガーされ、オペレーションデータの検証を行えます。

apply

オペレーションをスナップショットに適用しようとするときにトリガーされます。この時点ではスナップショットは旧状態のままです。

commit

変換後のオペレーションをデータベースにコミットしようとするときにトリガーされ、最終的な検証を行えます。

afterWrite

オペレーションがデータベースへ正常に書き込まれた後にトリガーされ、後続処理または通知に使用します。

readSnapshots

1 つ以上のスナップショットがデータベースからロードされるとき(例: fetch または subscribe の実行時)にトリガーされ、特定のスナップショット読み取りを拒否できます。

readOp

オペレーションがデータベースからロードされるときにトリガーされ、オペレーション読み取りを拒否できます。

reply

ドキュメントがクライアントにエラー以外の返信を送信する直前に呼び出されます。

ミドルウェア関数

ミドルウェアは、context と next パラメータを受け取る非同期関数です。

  • context: 現在の action に関するコンテキスト情報(例: connection、request)を含みます。

  • next: 実行を継続するための関数です。エラーを渡すことでフローを中断できます。

    • await next(): 後続のミドルウェア、または処理ロジックの実行を継続します。

    • await next(error): フローを中断し、エラーを返します。

例: 基本的なミドルウェア

docService.use('submit', async (context, next) => {
    const user = context.connection.tags.get('user');
    if (user.role === 'editor') {
        console.log('Submitting operation:', context.request.op);
        await next(); // Continue
    } else {
        await next('No permission'); // Abort
    }
});

ミドルウェアの登録

ミドルウェアはDocumentServices の use メソッドで登録します。

import { DocumentServices } from '@mescius/js-collaboration-ot';

const docService = new DocumentServices();

docService.use('receive', async (context, next) => {
    console.log('Message received:', context.request);
    await next();
});

ミドルウェアの実行順序

同じアクションに対する複数のミドルウェアは、登録順に実行されます。ミドルウェアが next(error) を呼び出した場合、後続のミドルウェアはスキップされます。

例: 実行順序

docService.use('submit', async (context, next) => {
  console.log('Middleware 1');
  await next();
});
docService.use('submit', async (context, next) => {
  console.log('Middleware 2');
  await next();
});
docService.use('submit', async (context, next) => {
  console.log('Middleware 3');
  await next();
});
// Output:
// Middleware 1
// Middleware 2
// Middleware 3

例: エラーハンドリング

docService.use('submit', async (context, next) => {
    console.log('Middleware 1');
    await next();
});
docService.use('submit', async (context, next) => {
    console.log('Middleware 2');
    await next('Test error');
});
docService.use('submit', async (context, next) => {
    console.log('Middleware 3'); // Will not be executed
    await next();
});
// Output:
// Middleware 1
// Middleware 2
// Error returned to the client, triggering the client-side error event

ユースケース

以下に、一般的なミドルウェアのユースケースとその実装例を示します。

1. 権限検証

docService.use('submit', async (context, next) => {
    const user = context.connection.tags.get('user');
    if (user.role === 'editor') {
        console.log('Submitting operation:', context.request.op);
        await next(); // Continue
    } else {
        await next('No permission'); // Abort
    }
});

2. ログ記録

docService.use('afterWrite', async (context, next) => {
    console.log('Operation written to database:', context.request.op);
    await next();
});