jspurefix 5.2.0 → 5.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/BACKPORT_PLAN.md +15 -29
  2. package/dist/config/js-fix-config.d.ts +2 -0
  3. package/dist/config/js-fix-config.js.map +1 -1
  4. package/dist/store/file-session-store.d.ts +42 -0
  5. package/dist/store/file-session-store.js +256 -0
  6. package/dist/store/file-session-store.js.map +1 -0
  7. package/dist/store/file-session-stream-provider.d.ts +25 -0
  8. package/dist/store/file-session-stream-provider.js +162 -0
  9. package/dist/store/file-session-stream-provider.js.map +1 -0
  10. package/dist/store/fix-session-store-factory.d.ts +13 -0
  11. package/dist/store/fix-session-store-factory.js +21 -0
  12. package/dist/store/fix-session-store-factory.js.map +1 -0
  13. package/dist/store/fix-session-store.d.ts +19 -0
  14. package/dist/store/fix-session-store.js +3 -0
  15. package/dist/store/fix-session-store.js.map +1 -0
  16. package/dist/store/index.d.ts +9 -0
  17. package/dist/store/index.js +9 -0
  18. package/dist/store/index.js.map +1 -1
  19. package/dist/store/memory-session-store.d.ts +27 -0
  20. package/dist/store/memory-session-store.js +104 -0
  21. package/dist/store/memory-session-store.js.map +1 -0
  22. package/dist/store/memory-session-stream-provider.d.ts +26 -0
  23. package/dist/store/memory-session-stream-provider.js +103 -0
  24. package/dist/store/memory-session-stream-provider.js.map +1 -0
  25. package/dist/store/session-id.d.ts +9 -0
  26. package/dist/store/session-id.js +55 -0
  27. package/dist/store/session-id.js.map +1 -0
  28. package/dist/store/session-stream-provider.d.ts +15 -0
  29. package/dist/store/session-stream-provider.js +3 -0
  30. package/dist/store/session-stream-provider.js.map +1 -0
  31. package/dist/store/store-config.d.ts +4 -0
  32. package/dist/store/store-config.js +3 -0
  33. package/dist/store/store-config.js.map +1 -0
  34. package/dist/transport/ascii/ascii-session.d.ts +6 -1
  35. package/dist/transport/ascii/ascii-session.js +37 -5
  36. package/dist/transport/ascii/ascii-session.js.map +1 -1
  37. package/dist/transport/session/session-description.d.ts +2 -0
  38. package/dist/transport/session/session-description.js.map +1 -1
  39. package/jsfix.test_client.txt +67 -67
  40. package/jsfix.test_server.txt +64 -64
  41. package/package.json +1 -1
  42. package/src/config/js-fix-config.ts +2 -0
  43. package/src/store/file-session-store.ts +294 -0
  44. package/src/store/file-session-stream-provider.ts +123 -0
  45. package/src/store/fix-session-store-factory.ts +31 -0
  46. package/src/store/fix-session-store.ts +37 -0
  47. package/src/store/index.ts +9 -0
  48. package/src/store/memory-session-store.ts +102 -0
  49. package/src/store/memory-session-stream-provider.ts +97 -0
  50. package/src/store/session-id.ts +32 -0
  51. package/src/store/session-stream-provider.ts +74 -0
  52. package/src/store/store-config.ts +15 -0
  53. package/src/transport/ascii/ascii-session.ts +57 -6
  54. package/src/transport/session/session-description.ts +2 -0
@@ -0,0 +1,294 @@
1
+ import { IFixSessionStore } from './fix-session-store'
2
+ import { SessionId } from './session-id'
3
+ import { IFixMsgStoreRecord, FixMsgStoreRecord } from './fix-msg-store-record'
4
+ import { ISessionStreamProvider } from './session-stream-provider'
5
+ import { FileSessionStreamProvider } from './file-session-stream-provider'
6
+
7
+ /**
8
+ * QuickFix-compatible file-based session store.
9
+ *
10
+ * File format:
11
+ * - .seqnums: "SSSSSSSSSSSSSSSSSSSS : TTTTTTTTTTTTTTTTTTTT" (20-char right-justified sender : target)
12
+ * - .session: "YYYYMMDD-HH:MM:SS.ffffff" (session creation time)
13
+ * - .header: "seqnum,offset,length" per line (index into body)
14
+ * - .body: concatenated raw FIX messages (no delimiters)
15
+ */
16
+ export class FileSessionStore implements IFixSessionStore {
17
+ private readonly streamProvider: ISessionStreamProvider
18
+ private readonly ownsProvider: boolean
19
+
20
+ private senderSeqNumValue: number = 1
21
+ private targetSeqNumValue: number = 1
22
+ private creationTimeValue: Date = new Date()
23
+
24
+ // In-memory index: seqnum -> { offset, length }
25
+ private readonly headerIndex: Map<number, { offset: number, length: number }> = new Map()
26
+
27
+ /**
28
+ * Creates a FileSessionStore with the default file-based stream provider.
29
+ */
30
+ static createWithFiles (sessionId: SessionId, directory: string): FileSessionStore {
31
+ return new FileSessionStore(sessionId, new FileSessionStreamProvider(sessionId, directory), true)
32
+ }
33
+
34
+ /**
35
+ * Creates a FileSessionStore with a custom stream provider.
36
+ * Useful for testing with in-memory streams.
37
+ */
38
+ constructor (
39
+ public readonly sessionId: SessionId,
40
+ streamProvider: ISessionStreamProvider,
41
+ ownsProvider: boolean = false
42
+ ) {
43
+ this.streamProvider = streamProvider
44
+ this.ownsProvider = ownsProvider
45
+ }
46
+
47
+ /**
48
+ * Gets the stream provider for direct access (useful for testing).
49
+ */
50
+ getStreamProvider (): ISessionStreamProvider {
51
+ return this.streamProvider
52
+ }
53
+
54
+ // Sequence Numbers
55
+
56
+ get senderSeqNum (): number {
57
+ return this.senderSeqNumValue
58
+ }
59
+
60
+ set senderSeqNum (value: number) {
61
+ this.senderSeqNumValue = value
62
+ }
63
+
64
+ get targetSeqNum (): number {
65
+ return this.targetSeqNumValue
66
+ }
67
+
68
+ set targetSeqNum (value: number) {
69
+ this.targetSeqNumValue = value
70
+ }
71
+
72
+ async setSenderSeqNum (value: number): Promise<void> {
73
+ this.senderSeqNumValue = value
74
+ await this.persistSeqNums()
75
+ }
76
+
77
+ async setTargetSeqNum (value: number): Promise<void> {
78
+ this.targetSeqNumValue = value
79
+ await this.persistSeqNums()
80
+ }
81
+
82
+ async nextSenderSeqNum (): Promise<number> {
83
+ const next = ++this.senderSeqNumValue
84
+ await this.persistSeqNums()
85
+ return next
86
+ }
87
+
88
+ async nextTargetSeqNum (): Promise<number> {
89
+ const next = ++this.targetSeqNumValue
90
+ await this.persistSeqNums()
91
+ return next
92
+ }
93
+
94
+ // Session
95
+
96
+ get creationTime (): Date {
97
+ return this.creationTimeValue
98
+ }
99
+
100
+ async reset (): Promise<void> {
101
+ this.senderSeqNumValue = 1
102
+ this.targetSeqNumValue = 1
103
+ this.creationTimeValue = new Date()
104
+ this.headerIndex.clear()
105
+
106
+ await this.streamProvider.reset()
107
+ await this.persistSeqNums()
108
+ await this.persistSessionTime()
109
+ this.streamProvider.openBody()
110
+ }
111
+
112
+ // Message Operations
113
+
114
+ async put (record: IFixMsgStoreRecord): Promise<void> {
115
+ if (record.encoded == null) {
116
+ throw new Error('Record must have encoded content')
117
+ }
118
+
119
+ const bytes = Buffer.from(record.encoded, 'utf8')
120
+ const length = bytes.length
121
+ const offset = await this.streamProvider.appendBody(bytes)
122
+ this.headerIndex.set(record.seqNum, { offset, length })
123
+ await this.streamProvider.appendHeaderLine(`${record.seqNum},${offset},${length}`)
124
+ }
125
+
126
+ async get (seqNum: number): Promise<IFixMsgStoreRecord | null> {
127
+ const entry = this.headerIndex.get(seqNum)
128
+ if (!entry) return null
129
+ return await this.readMessage(seqNum, entry.offset, entry.length)
130
+ }
131
+
132
+ async getRange (fromSeqNum: number, toSeqNum: number): Promise<IFixMsgStoreRecord[]> {
133
+ const results: IFixMsgStoreRecord[] = []
134
+ for (let seq = fromSeqNum; seq <= toSeqNum; seq++) {
135
+ const record = await this.get(seq)
136
+ if (record) {
137
+ results.push(record)
138
+ }
139
+ }
140
+ return results
141
+ }
142
+
143
+ // Lifecycle
144
+
145
+ async initialize (): Promise<void> {
146
+ await this.loadSeqNums()
147
+ await this.loadSessionTime()
148
+ await this.loadHeaderIndex()
149
+ this.streamProvider.openBody()
150
+ }
151
+
152
+ async flush (): Promise<void> {
153
+ await this.streamProvider.flush()
154
+ }
155
+
156
+ async dispose (): Promise<void> {
157
+ await this.streamProvider.flush()
158
+ if (this.ownsProvider) {
159
+ await this.streamProvider.dispose()
160
+ }
161
+ }
162
+
163
+ // Private - persistence
164
+
165
+ private async persistSeqNums (): Promise<void> {
166
+ const sender = this.senderSeqNumValue.toString().padStart(20, ' ')
167
+ const target = this.targetSeqNumValue.toString().padStart(20, ' ')
168
+ await this.streamProvider.writeSeqNums(`${sender} : ${target}`)
169
+ }
170
+
171
+ private async loadSeqNums (): Promise<void> {
172
+ const content = await this.streamProvider.readSeqNums()
173
+ if (content == null) {
174
+ this.senderSeqNumValue = 1
175
+ this.targetSeqNumValue = 1
176
+ return
177
+ }
178
+ const parts = content.split(':')
179
+ if (parts.length === 2) {
180
+ this.senderSeqNumValue = parseInt(parts[0].trim(), 10) || 1
181
+ this.targetSeqNumValue = parseInt(parts[1].trim(), 10) || 1
182
+ }
183
+ }
184
+
185
+ private async persistSessionTime (): Promise<void> {
186
+ await this.streamProvider.writeSessionTime(FileSessionStore.formatSessionTime(this.creationTimeValue))
187
+ }
188
+
189
+ private async loadSessionTime (): Promise<void> {
190
+ const content = await this.streamProvider.readSessionTime()
191
+ if (content == null) {
192
+ this.creationTimeValue = new Date()
193
+ await this.persistSessionTime()
194
+ return
195
+ }
196
+ const parsed = FileSessionStore.parseSessionTime(content.trim())
197
+ this.creationTimeValue = parsed ?? new Date()
198
+ }
199
+
200
+ private async loadHeaderIndex (): Promise<void> {
201
+ const lines = await this.streamProvider.readHeaderLines()
202
+ for (const line of lines) {
203
+ if (!line.trim()) continue
204
+ const parts = line.split(',')
205
+ if (parts.length === 3) {
206
+ const seqNum = parseInt(parts[0], 10)
207
+ const offset = parseInt(parts[1], 10)
208
+ const length = parseInt(parts[2], 10)
209
+ if (!isNaN(seqNum) && !isNaN(offset) && !isNaN(length)) {
210
+ this.headerIndex.set(seqNum, { offset, length })
211
+ }
212
+ }
213
+ }
214
+ }
215
+
216
+ // Private - message reading
217
+
218
+ private async readMessage (seqNum: number, offset: number, length: number): Promise<IFixMsgStoreRecord | null> {
219
+ const buffer = await this.streamProvider.readBody(offset, length)
220
+ if (buffer.length !== length) return null
221
+
222
+ const encoded = buffer.toString('utf8')
223
+ const msgType = FileSessionStore.extractTag(encoded, '35')
224
+ const sendingTimeStr = FileSessionStore.extractTag(encoded, '52')
225
+ let timestamp = new Date(0)
226
+ if (sendingTimeStr) {
227
+ const parsed = FileSessionStore.parseSessionTime(sendingTimeStr)
228
+ if (parsed) timestamp = parsed
229
+ }
230
+
231
+ return new FixMsgStoreRecord(msgType ?? '', timestamp, seqNum, undefined, encoded)
232
+ }
233
+
234
+ /**
235
+ * Extract a FIX tag value from a raw message string.
236
+ * Tags are delimited by SOH (0x01) or pipe (|).
237
+ */
238
+ static extractTag (message: string, tag: string): string | null {
239
+ const tagPrefix = `${tag}=`
240
+
241
+ // Check if tag is at the start
242
+ if (message.startsWith(tagPrefix)) {
243
+ const endIndex = FileSessionStore.findDelimiter(message, tagPrefix.length)
244
+ return message.substring(tagPrefix.length, endIndex)
245
+ }
246
+
247
+ // Search for SOH + tag= or | + tag=
248
+ for (const delim of ['\x01', '|']) {
249
+ const search = `${delim}${tagPrefix}`
250
+ const idx = message.indexOf(search)
251
+ if (idx >= 0) {
252
+ const startIndex = idx + search.length
253
+ const endIndex = FileSessionStore.findDelimiter(message, startIndex)
254
+ return message.substring(startIndex, endIndex)
255
+ }
256
+ }
257
+
258
+ return null
259
+ }
260
+
261
+ private static findDelimiter (message: string, fromIndex: number): number {
262
+ for (let i = fromIndex; i < message.length; i++) {
263
+ const ch = message.charAt(i)
264
+ if (ch === '\x01' || ch === '|') return i
265
+ }
266
+ return message.length
267
+ }
268
+
269
+ /**
270
+ * Format a date as QuickFix session time: YYYYMMDD-HH:MM:SS.ffffff
271
+ */
272
+ static formatSessionTime (date: Date): string {
273
+ const y = date.getUTCFullYear()
274
+ const M = (date.getUTCMonth() + 1).toString().padStart(2, '0')
275
+ const d = date.getUTCDate().toString().padStart(2, '0')
276
+ const h = date.getUTCHours().toString().padStart(2, '0')
277
+ const m = date.getUTCMinutes().toString().padStart(2, '0')
278
+ const s = date.getUTCSeconds().toString().padStart(2, '0')
279
+ const ms = date.getUTCMilliseconds().toString().padStart(3, '0')
280
+ return `${y}${M}${d}-${h}:${m}:${s}.${ms}000`
281
+ }
282
+
283
+ /**
284
+ * Parse a QuickFix session time string: YYYYMMDD-HH:MM:SS.fff[fff]
285
+ */
286
+ static parseSessionTime (str: string): Date | null {
287
+ // Format: YYYYMMDD-HH:MM:SS.ffffff
288
+ const match = str.match(/^(\d{4})(\d{2})(\d{2})-(\d{2}):(\d{2}):(\d{2})\.(\d{3,6})$/)
289
+ if (!match) return null
290
+ const [, y, M, d, h, m, s, frac] = match
291
+ const ms = parseInt(frac.substring(0, 3), 10)
292
+ return new Date(Date.UTC(parseInt(y), parseInt(M) - 1, parseInt(d), parseInt(h), parseInt(m), parseInt(s), ms))
293
+ }
294
+ }
@@ -0,0 +1,123 @@
1
+ import * as fs from 'fs'
2
+ import { ISessionStreamProvider } from './session-stream-provider'
3
+ import { SessionId } from './session-id'
4
+
5
+ /**
6
+ * File-based implementation of ISessionStreamProvider.
7
+ * Creates QuickFix-compatible files in the specified directory.
8
+ *
9
+ * Files created:
10
+ * - {prefix}.body — concatenated raw FIX messages
11
+ * - {prefix}.header — index lines: "seqnum,offset,length"
12
+ * - {prefix}.seqnums — sender/target sequence numbers
13
+ * - {prefix}.session — session creation time
14
+ */
15
+ export class FileSessionStreamProvider implements ISessionStreamProvider {
16
+ private readonly bodyPath: string
17
+ private readonly headerPath: string
18
+ private readonly seqNumsPath: string
19
+ private readonly sessionPath: string
20
+ private bodyFd: number | null = null
21
+ private bodySize: number = 0
22
+
23
+ constructor (
24
+ sessionId: SessionId,
25
+ directory: string
26
+ ) {
27
+ fs.mkdirSync(directory, { recursive: true })
28
+ this.bodyPath = sessionId.getFilePath(directory, 'body')
29
+ this.headerPath = sessionId.getFilePath(directory, 'header')
30
+ this.seqNumsPath = sessionId.getFilePath(directory, 'seqnums')
31
+ this.sessionPath = sessionId.getFilePath(directory, 'session')
32
+ }
33
+
34
+ openBody (): void {
35
+ if (this.bodyFd !== null) return
36
+ this.bodyFd = fs.openSync(this.bodyPath, 'a+')
37
+ const stat = fs.fstatSync(this.bodyFd)
38
+ this.bodySize = stat.size
39
+ }
40
+
41
+ async appendBody (data: Buffer): Promise<number> {
42
+ if (this.bodyFd === null) {
43
+ this.openBody()
44
+ }
45
+ const offset = this.bodySize
46
+ fs.writeSync(this.bodyFd!, data, 0, data.length, offset)
47
+ this.bodySize += data.length
48
+ return offset
49
+ }
50
+
51
+ async readBody (offset: number, length: number): Promise<Buffer> {
52
+ if (this.bodyFd === null) {
53
+ this.openBody()
54
+ }
55
+ const buf = Buffer.alloc(length)
56
+ fs.readSync(this.bodyFd!, buf, 0, length, offset)
57
+ return buf
58
+ }
59
+
60
+ getBodySize (): number {
61
+ return this.bodySize
62
+ }
63
+
64
+ async appendHeaderLine (line: string): Promise<void> {
65
+ await fs.promises.appendFile(this.headerPath, line + '\n', 'utf8')
66
+ }
67
+
68
+ async readHeaderLines (): Promise<string[]> {
69
+ if (!fs.existsSync(this.headerPath)) return []
70
+ const content = await fs.promises.readFile(this.headerPath, 'utf8')
71
+ if (!content.trim()) return []
72
+ return content.split('\n').filter(l => l.trim().length > 0)
73
+ }
74
+
75
+ async readSeqNums (): Promise<string | null> {
76
+ if (!fs.existsSync(this.seqNumsPath)) return null
77
+ return await fs.promises.readFile(this.seqNumsPath, 'utf8')
78
+ }
79
+
80
+ async writeSeqNums (content: string): Promise<void> {
81
+ await fs.promises.writeFile(this.seqNumsPath, content, 'utf8')
82
+ }
83
+
84
+ async readSessionTime (): Promise<string | null> {
85
+ if (!fs.existsSync(this.sessionPath)) return null
86
+ return await fs.promises.readFile(this.sessionPath, 'utf8')
87
+ }
88
+
89
+ async writeSessionTime (content: string): Promise<void> {
90
+ await fs.promises.writeFile(this.sessionPath, content, 'utf8')
91
+ }
92
+
93
+ async reset (): Promise<void> {
94
+ if (this.bodyFd !== null) {
95
+ fs.closeSync(this.bodyFd)
96
+ this.bodyFd = null
97
+ }
98
+ this.bodySize = 0
99
+ this.deleteIfExists(this.bodyPath)
100
+ this.deleteIfExists(this.headerPath)
101
+ this.deleteIfExists(this.seqNumsPath)
102
+ this.deleteIfExists(this.sessionPath)
103
+ }
104
+
105
+ async flush (): Promise<void> {
106
+ if (this.bodyFd !== null) {
107
+ fs.fsyncSync(this.bodyFd)
108
+ }
109
+ }
110
+
111
+ async dispose (): Promise<void> {
112
+ if (this.bodyFd !== null) {
113
+ fs.closeSync(this.bodyFd)
114
+ this.bodyFd = null
115
+ }
116
+ }
117
+
118
+ private deleteIfExists (filePath: string): void {
119
+ if (fs.existsSync(filePath)) {
120
+ fs.unlinkSync(filePath)
121
+ }
122
+ }
123
+ }
@@ -0,0 +1,31 @@
1
+ import { IFixSessionStore } from './fix-session-store'
2
+ import { SessionId } from './session-id'
3
+ import { MemorySessionStore } from './memory-session-store'
4
+ import { FileSessionStore } from './file-session-store'
5
+
6
+ /**
7
+ * Factory for creating session stores.
8
+ */
9
+ export interface IFixSessionStoreFactory {
10
+ create (sessionId: SessionId): IFixSessionStore
11
+ }
12
+
13
+ /**
14
+ * Factory for creating in-memory session stores.
15
+ */
16
+ export class MemorySessionStoreFactory implements IFixSessionStoreFactory {
17
+ create (sessionId: SessionId): IFixSessionStore {
18
+ return new MemorySessionStore(sessionId)
19
+ }
20
+ }
21
+
22
+ /**
23
+ * Factory for creating file-based session stores.
24
+ */
25
+ export class FileSessionStoreFactory implements IFixSessionStoreFactory {
26
+ constructor (private readonly directory: string) {}
27
+
28
+ create (sessionId: SessionId): IFixSessionStore {
29
+ return FileSessionStore.createWithFiles(sessionId, this.directory)
30
+ }
31
+ }
@@ -0,0 +1,37 @@
1
+ import { SessionId } from './session-id'
2
+ import { IFixMsgStoreRecord } from './fix-msg-store-record'
3
+
4
+ /**
5
+ * Unified session store interface for FIX message persistence and sequence number management.
6
+ * Coordinates all persistence for a single FIX session:
7
+ * - Message storage (.body + .header files)
8
+ * - Sequence numbers (.seqnums file)
9
+ * - Session metadata (.session file)
10
+ *
11
+ * QuickFix-compatible file format for interoperability.
12
+ */
13
+ export interface IFixSessionStore {
14
+ readonly sessionId: SessionId
15
+
16
+ // Message Operations
17
+ put (record: IFixMsgStoreRecord): Promise<void>
18
+ get (seqNum: number): Promise<IFixMsgStoreRecord | null>
19
+ getRange (fromSeqNum: number, toSeqNum: number): Promise<IFixMsgStoreRecord[]>
20
+
21
+ // Sequence Number Operations
22
+ senderSeqNum: number
23
+ targetSeqNum: number
24
+ setSenderSeqNum (value: number): Promise<void>
25
+ setTargetSeqNum (value: number): Promise<void>
26
+ nextSenderSeqNum (): Promise<number>
27
+ nextTargetSeqNum (): Promise<number>
28
+
29
+ // Session Operations
30
+ readonly creationTime: Date
31
+ reset (): Promise<void>
32
+
33
+ // Lifecycle
34
+ initialize (): Promise<void>
35
+ flush (): Promise<void>
36
+ dispose (): Promise<void>
37
+ }
@@ -2,3 +2,12 @@ export * from './fix-msg-memory-store'
2
2
  export * from './fix-msg-store'
3
3
  export * from './fix-msg-store-record'
4
4
  export * from './fix-msg-ascii-store-resend'
5
+ export * from './session-id'
6
+ export * from './fix-session-store'
7
+ export * from './memory-session-store'
8
+ export * from './fix-session-store-factory'
9
+ export * from './session-stream-provider'
10
+ export * from './memory-session-stream-provider'
11
+ export * from './file-session-stream-provider'
12
+ export * from './file-session-store'
13
+ export * from './store-config'
@@ -0,0 +1,102 @@
1
+ import { IFixSessionStore } from './fix-session-store'
2
+ import { SessionId } from './session-id'
3
+ import { IFixMsgStoreRecord } from './fix-msg-store-record'
4
+
5
+ /**
6
+ * In-memory session store for testing and development.
7
+ * Not persistent - all data lost on dispose.
8
+ */
9
+ export class MemorySessionStore implements IFixSessionStore {
10
+ private readonly messages: Map<number, IFixMsgStoreRecord> = new Map()
11
+ private senderSeqNumValue: number = 1
12
+ private targetSeqNumValue: number = 1
13
+ private creationTimeValue: Date = new Date()
14
+
15
+ constructor (public readonly sessionId: SessionId) {}
16
+
17
+ // Sequence Numbers
18
+
19
+ get senderSeqNum (): number {
20
+ return this.senderSeqNumValue
21
+ }
22
+
23
+ set senderSeqNum (value: number) {
24
+ this.senderSeqNumValue = value
25
+ }
26
+
27
+ get targetSeqNum (): number {
28
+ return this.targetSeqNumValue
29
+ }
30
+
31
+ set targetSeqNum (value: number) {
32
+ this.targetSeqNumValue = value
33
+ }
34
+
35
+ async setSenderSeqNum (value: number): Promise<void> {
36
+ this.senderSeqNumValue = value
37
+ }
38
+
39
+ async setTargetSeqNum (value: number): Promise<void> {
40
+ this.targetSeqNumValue = value
41
+ }
42
+
43
+ async nextSenderSeqNum (): Promise<number> {
44
+ return ++this.senderSeqNumValue
45
+ }
46
+
47
+ async nextTargetSeqNum (): Promise<number> {
48
+ return ++this.targetSeqNumValue
49
+ }
50
+
51
+ // Session
52
+
53
+ get creationTime (): Date {
54
+ return this.creationTimeValue
55
+ }
56
+
57
+ async reset (): Promise<void> {
58
+ this.senderSeqNumValue = 1
59
+ this.targetSeqNumValue = 1
60
+ this.creationTimeValue = new Date()
61
+ this.messages.clear()
62
+ }
63
+
64
+ // Message Operations
65
+
66
+ async put (record: IFixMsgStoreRecord): Promise<void> {
67
+ this.messages.set(record.seqNum, record.clone())
68
+ }
69
+
70
+ async get (seqNum: number): Promise<IFixMsgStoreRecord | null> {
71
+ const record = this.messages.get(seqNum)
72
+ return record ? record.clone() : null
73
+ }
74
+
75
+ async getRange (fromSeqNum: number, toSeqNum: number): Promise<IFixMsgStoreRecord[]> {
76
+ const results: IFixMsgStoreRecord[] = []
77
+ const keys = Array.from(this.messages.keys())
78
+ .filter(k => k >= fromSeqNum && k <= toSeqNum)
79
+ .sort((a, b) => a - b)
80
+ for (const seq of keys) {
81
+ const record = this.messages.get(seq)
82
+ if (record) {
83
+ results.push(record.clone())
84
+ }
85
+ }
86
+ return results
87
+ }
88
+
89
+ // Lifecycle
90
+
91
+ async initialize (): Promise<void> {
92
+ // No-op for memory store
93
+ }
94
+
95
+ async flush (): Promise<void> {
96
+ // No-op for memory store
97
+ }
98
+
99
+ async dispose (): Promise<void> {
100
+ // No-op for memory store
101
+ }
102
+ }
@@ -0,0 +1,97 @@
1
+ import { ISessionStreamProvider } from './session-stream-provider'
2
+
3
+ /**
4
+ * In-memory implementation of ISessionStreamProvider for testing.
5
+ * Stores all data in buffers and strings for inspection.
6
+ */
7
+ export class MemorySessionStreamProvider implements ISessionStreamProvider {
8
+ private bodyBuffer: Buffer = Buffer.alloc(0)
9
+ private headerLines: string[] = []
10
+ private seqNumsContent: string | null = null
11
+ private sessionTimeContent: string | null = null
12
+
13
+ // Inspection methods for tests
14
+
15
+ getBodyBytes (): Buffer {
16
+ return Buffer.from(this.bodyBuffer)
17
+ }
18
+
19
+ getBodyString (): string {
20
+ return this.bodyBuffer.toString('utf8')
21
+ }
22
+
23
+ getHeaderString (): string {
24
+ return this.headerLines.join('\n')
25
+ }
26
+
27
+ getHeaderLinesSnapshot (): string[] {
28
+ return [...this.headerLines]
29
+ }
30
+
31
+ getSeqNumsContent (): string | null {
32
+ return this.seqNumsContent
33
+ }
34
+
35
+ getSessionTimeContent (): string | null {
36
+ return this.sessionTimeContent
37
+ }
38
+
39
+ // ISessionStreamProvider implementation
40
+
41
+ openBody (): void {
42
+ // No-op for memory provider, body is always available
43
+ }
44
+
45
+ async appendBody (data: Buffer): Promise<number> {
46
+ const offset = this.bodyBuffer.length
47
+ this.bodyBuffer = Buffer.concat([this.bodyBuffer, data])
48
+ return offset
49
+ }
50
+
51
+ async readBody (offset: number, length: number): Promise<Buffer> {
52
+ return this.bodyBuffer.subarray(offset, offset + length)
53
+ }
54
+
55
+ getBodySize (): number {
56
+ return this.bodyBuffer.length
57
+ }
58
+
59
+ async appendHeaderLine (line: string): Promise<void> {
60
+ this.headerLines.push(line)
61
+ }
62
+
63
+ async readHeaderLines (): Promise<string[]> {
64
+ return [...this.headerLines]
65
+ }
66
+
67
+ async readSeqNums (): Promise<string | null> {
68
+ return this.seqNumsContent
69
+ }
70
+
71
+ async writeSeqNums (content: string): Promise<void> {
72
+ this.seqNumsContent = content
73
+ }
74
+
75
+ async readSessionTime (): Promise<string | null> {
76
+ return this.sessionTimeContent
77
+ }
78
+
79
+ async writeSessionTime (content: string): Promise<void> {
80
+ this.sessionTimeContent = content
81
+ }
82
+
83
+ async reset (): Promise<void> {
84
+ this.bodyBuffer = Buffer.alloc(0)
85
+ this.headerLines = []
86
+ this.seqNumsContent = null
87
+ this.sessionTimeContent = null
88
+ }
89
+
90
+ async flush (): Promise<void> {
91
+ // No-op for memory provider
92
+ }
93
+
94
+ async dispose (): Promise<void> {
95
+ // No-op for memory provider
96
+ }
97
+ }