|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const Archetype = require('archetype'); |
| 4 | +const authorize = require('../../authorize'); |
| 5 | + |
| 6 | +const StreamDocumentChangesParams = new Archetype({ |
| 7 | + model: { |
| 8 | + $type: 'string', |
| 9 | + $required: true |
| 10 | + }, |
| 11 | + documentId: { |
| 12 | + $type: 'string', |
| 13 | + $required: true |
| 14 | + }, |
| 15 | + roles: { |
| 16 | + $type: ['string'] |
| 17 | + } |
| 18 | +}).compile('StreamDocumentChangesParams'); |
| 19 | + |
| 20 | +module.exports = ({ db, changeStream }) => async function* streamDocumentChanges(params) { |
| 21 | + const { model, documentId, roles } = new StreamDocumentChangesParams(params); |
| 22 | + |
| 23 | + await authorize('Model.streamDocumentChanges', roles); |
| 24 | + |
| 25 | + const Model = db.models[model]; |
| 26 | + if (Model == null) { |
| 27 | + throw new Error(`Model ${model} not found`); |
| 28 | + } |
| 29 | + |
| 30 | + if (!changeStream) { |
| 31 | + throw new Error('Change streams are not enabled'); |
| 32 | + } |
| 33 | + |
| 34 | + const collectionName = Model.collection.name; |
| 35 | + const targetId = String(documentId); |
| 36 | + |
| 37 | + const queue = []; |
| 38 | + let resolveQueue = null; |
| 39 | + let streamError = null; |
| 40 | + let streamEnded = false; |
| 41 | + |
| 42 | + function enqueue(payload) { |
| 43 | + queue.push(payload); |
| 44 | + if (resolveQueue) { |
| 45 | + const resolve = resolveQueue; |
| 46 | + resolveQueue = null; |
| 47 | + resolve(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + function handleChange(change) { |
| 52 | + if (!change || change.ns?.coll !== collectionName) { |
| 53 | + return; |
| 54 | + } |
| 55 | + if (!change.documentKey || change.documentKey._id == null) { |
| 56 | + return; |
| 57 | + } |
| 58 | + if (String(change.documentKey._id) !== targetId) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + enqueue({ |
| 63 | + type: 'change', |
| 64 | + operationType: change.operationType, |
| 65 | + documentKey: change.documentKey, |
| 66 | + ns: change.ns, |
| 67 | + updateDescription: change.updateDescription, |
| 68 | + clusterTime: change.clusterTime |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + function handleError(err) { |
| 73 | + streamError = err || new Error('Change stream error'); |
| 74 | + enqueue({ type: 'error', message: streamError.message }); |
| 75 | + } |
| 76 | + |
| 77 | + function handleEnd() { |
| 78 | + streamEnded = true; |
| 79 | + enqueue({ type: 'end' }); |
| 80 | + } |
| 81 | + |
| 82 | + changeStream.on('change', handleChange); |
| 83 | + changeStream.on('error', handleError); |
| 84 | + changeStream.on('end', handleEnd); |
| 85 | + |
| 86 | + try { |
| 87 | + while (true) { |
| 88 | + if (streamError) { |
| 89 | + throw streamError; |
| 90 | + } |
| 91 | + |
| 92 | + if (queue.length === 0) { |
| 93 | + await new Promise(resolve => { |
| 94 | + resolveQueue = resolve; |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + if (streamError) { |
| 99 | + throw streamError; |
| 100 | + } |
| 101 | + |
| 102 | + while (queue.length > 0) { |
| 103 | + const payload = queue.shift(); |
| 104 | + if (payload?.type === 'end') { |
| 105 | + return; |
| 106 | + } |
| 107 | + yield payload; |
| 108 | + } |
| 109 | + |
| 110 | + if (streamEnded) { |
| 111 | + return; |
| 112 | + } |
| 113 | + } |
| 114 | + } finally { |
| 115 | + changeStream.off('change', handleChange); |
| 116 | + changeStream.off('error', handleError); |
| 117 | + changeStream.off('end', handleEnd); |
| 118 | + if (resolveQueue) { |
| 119 | + resolveQueue(); |
| 120 | + resolveQueue = null; |
| 121 | + } |
| 122 | + } |
| 123 | +}; |
0 commit comments