[]
js-collaboration-ot のミドルウェア機構を使うと、開発者はサーバー側の 操作変換(Operational Transformation、OT)処理フローに介入し、権限検証やログ記録などの処理をカスタマイズできます。
このドキュメントでは、ライフサイクル、登録方法、実用的な例を含め、ミドルウェアの使用方法を説明します。
ミドルウェアは DocumentServices の中核となる拡張機能であり、オペレーション(op)およびスナップショット(snapshot)の処理ステージで ユーザー定義ロジックを追加 するために使用します。
目的:
オペレーションフローに介入し、検証を行う
コンテキストを変更する、または処理を中断する
適用シナリオ: ユーザー認証、データ検証、ログ記録、パフォーマンス監視など。
フックとの違い: ミドルウェアは処理フローを中断してエラーを返せますが、フックはイベントを監視するだけです。
ミドルウェアは OT 処理フローの複数ステージをカバーし、各ステージはアクションに対応します。開発者はこれらのステージに選択的に介入できます。以下に、サポートされるミドルウェア action とトリガータイミングを示します。
アクション | 説明 |
|---|---|
| サーバーがクライアントメッセージを受信したときにトリガーされ、初期検証またはログ記録に使用します。 |
| オペレーション( |
| オペレーションをスナップショットに適用しようとするときにトリガーされます。この時点ではスナップショットは旧状態のままです。 |
| 変換後のオペレーションをデータベースにコミットしようとするときにトリガーされ、最終的な検証を行えます。 |
| オペレーションがデータベースへ正常に書き込まれた後にトリガーされ、後続処理または通知に使用します。 |
| 1 つ以上のスナップショットがデータベースからロードされるとき(例: |
| オペレーションがデータベースからロードされるときにトリガーされ、オペレーション読み取りを拒否できます。 |
| ドキュメントがクライアントにエラー以外の返信を送信する直前に呼び出されます。 |
ミドルウェアは、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 3docService.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以下に、一般的なミドルウェアのユースケースとその実装例を示します。
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
}
});docService.use('afterWrite', async (context, next) => {
console.log('Operation written to database:', context.request.op);
await next();
});