-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp.js
193 lines (165 loc) · 5.74 KB
/
esp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
'use strict'
const EventStore = require('event-store-client')
const EspPrototype = {}
module.exports = (options, defaults) => {
const defaultDefaults = {
timeout: 2000,
resolveLinkTos: false,
requireMaster: false,
maxCount: 4095,
}
return new Promise((resolve, reject) => {
const esp = Object.create(EspPrototype)
esp.defaults = Object.assign({}, defaultDefaults, defaults)
esp.credentials = options.credentials
const connectionOptions = Object.assign({}, options)
connectionOptions.onConnect = () => resolve(esp)
connectionOptions.onError = (err) => reject(err)
esp.connection = new EventStore.Connection(connectionOptions)
})
}
const Readable = require('stream').Readable
const util = require('util')
const EventStoreStream = function() {
Readable.call(this, {objectMode: true});
}
util.inherits(EventStoreStream, Readable)
EventStoreStream.prototype._read = (size) => {}
EspPrototype.ExpectedVersion = EventStore.ExpectedVersion
EspPrototype.OperationResult = EventStore.OperationResult
EspPrototype.close = function() {return this.connection.close()}
EspPrototype.createGuid = () => EventStore.Connection.createGuid()
EspPrototype.sendPing = function() {
const self = this
return new Promise((resolve) => self.connection.sendPing(resolve))
}
/**
* Subscribe to future events on stream.
*
* Use stream.close() to unsubscribe.
*
* @param streamId Stream ID
* @param object options
* resolveLinkTos - Resolve Link Tos? Default: false
*
* @return EventStoreStream stream The stream of events.
*/
EspPrototype.subscribeToStream = function(streamId, params) {
const self = this
const options = Object.assign({}, self.defaults, params)
const stream = new EventStoreStream()
const correlationId = self.connection.subscribeToStream(
streamId, options.resolveLinkTos,
event => stream.push(event),
confirmed => stream.emit('open', confirmed),
err => stream.emit('error', err),
self.credentials
)
stream.close = () => self.connection.unsubscribeFromStream(correlationId, self.credentials)
return stream
}
EspPrototype.readAllEventsBackward = function() {
return this.connection.readAllEventsBackward.apply(this, arguments)
}
EspPrototype.readAllEventsForward = function() {
return this.connection.readAllEventsForward.apply(this, arguments)
}
EspPrototype.readStreamEventsBackward = function() {
return this.connection.readStreamEventsBackward.apply(this, arguments)
}
/**
* Read all existing events in a stream forward.
*
* @param string streamId Stream ID
* @param integer from Start at EventNumber
* @param object options
* maxCount - Maximum number of events Default: 4095
* resolveLinkTos - Resolve Link Tos? Default: false
* requireMaster - Require Master? Default: false
*
* @return EventStoreStream stream The stream of events.
*/
EspPrototype.readStreamEventsForward = function(streamId, from, params) {
const self = this
const options = Object.assign({}, self.defaults, params)
const stream = new EventStoreStream()
self.connection.readStreamEventsForward(
streamId, from, options.maxCount, options.resolveLinkTos, options.requireMaster,
event => stream.push(event), self.credentials,
read => stream.push(null)
)
return stream
}
/**
* Get stream of events from `from` to `to`.
*
* @param string streamId StreamId ID
* @param integer from From EventNumber
* @param integer to To EventNumber (inclusive)
* @param object options
* timeout - Timeout (in milliseconds). Default: 2000
* resolveLinkTos - Resolve Link Tos? Default: false
* requireMaster - Require Master? Default: false
*
* Example (with highlandjs):
*
* // Apply all states up to event 100 and get resulting state.
* _(es.readStreamEventsUntil(streamId, 0, 100)).reduce({}, applyEvent).pull(useResult)
*
* @todo Handle when an event has arrived between reading streamId and subscribing.
* @todo Handle more than 4095 events requested.
*/
EspPrototype.readStreamEventsUntil = function(streamId, from, to, params) {
const self = this
const options = Object.assign({}, self.defaults, params, {maxCount: to - from + 1})
const stream = new EventStoreStream()
const timer = setTimeout(
() => stream.emit('error', new Error('Timeout reached')),
options.timeout
)
const end = () => {
stream.push(null)
return clearTimeout(timer)
}
let lastEventNumber = -1
const forwardStream = self.readStreamEventsForward(streamId, from, options)
forwardStream.on('data', event => {
lastEventNumber = event.eventNumber
stream.push(event)
})
forwardStream.on('error', err => stream.emit('error', err))
forwardStream.on('end', () => {
if (lastEventNumber === to) return end()
const subscribeStream = self.subscribeToStream(streamId, options)
subscribeStream.on('data', event => {
stream.push(event)
if (event.eventNumber === to) {
subscribeStream.close()
end()
}
})
})
return stream
}
/**
* Write events to stream.
*
* @param string streamId StreamId ID
* @param integer expectedversion The expected version before this write.
* @param boolean requireMaster
* @param array events
*
* @return Promise
*/
EspPrototype.writeEvents = function(streamId, expectedVersion, requireMaster, events) {
const self = this
return new Promise((resolve, reject) => {
self.connection.writeEvents(
streamId, expectedVersion, requireMaster, events, self.credentials,
(response) => {
if (response.result === EventStore.OperationResult.Success) return resolve(response)
reject(response)
}
)
})
}