jspurefix 5.2.0 → 5.5.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 (92) hide show
  1. package/BACKPORT_PLAN.md +135 -39
  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/dictionary/parser/quickfix/dictionary-validator.d.ts +39 -0
  5. package/dist/dictionary/parser/quickfix/dictionary-validator.js +321 -0
  6. package/dist/dictionary/parser/quickfix/dictionary-validator.js.map +1 -0
  7. package/dist/dictionary/parser/quickfix/index-visitor.d.ts +10 -0
  8. package/dist/dictionary/parser/quickfix/index-visitor.js +68 -0
  9. package/dist/dictionary/parser/quickfix/index-visitor.js.map +1 -0
  10. package/dist/dictionary/parser/quickfix/index.d.ts +7 -0
  11. package/dist/dictionary/parser/quickfix/index.js +7 -0
  12. package/dist/dictionary/parser/quickfix/index.js.map +1 -1
  13. package/dist/dictionary/parser/quickfix/quick-fix-graph-file-parser.d.ts +13 -0
  14. package/dist/dictionary/parser/quickfix/quick-fix-graph-file-parser.js +65 -0
  15. package/dist/dictionary/parser/quickfix/quick-fix-graph-file-parser.js.map +1 -0
  16. package/dist/dictionary/parser/quickfix/quick-fix-graph-parser.d.ts +73 -0
  17. package/dist/dictionary/parser/quickfix/quick-fix-graph-parser.js +363 -0
  18. package/dist/dictionary/parser/quickfix/quick-fix-graph-parser.js.map +1 -0
  19. package/dist/dictionary/parser/quickfix/sax-tree-builder.d.ts +5 -0
  20. package/dist/dictionary/parser/quickfix/sax-tree-builder.js +103 -0
  21. package/dist/dictionary/parser/quickfix/sax-tree-builder.js.map +1 -0
  22. package/dist/dictionary/parser/quickfix/validation-error.d.ts +17 -0
  23. package/dist/dictionary/parser/quickfix/validation-error.js +32 -0
  24. package/dist/dictionary/parser/quickfix/validation-error.js.map +1 -0
  25. package/dist/dictionary/parser/quickfix/x-element.d.ts +26 -0
  26. package/dist/dictionary/parser/quickfix/x-element.js +82 -0
  27. package/dist/dictionary/parser/quickfix/x-element.js.map +1 -0
  28. package/dist/store/file-session-store.d.ts +42 -0
  29. package/dist/store/file-session-store.js +256 -0
  30. package/dist/store/file-session-store.js.map +1 -0
  31. package/dist/store/file-session-stream-provider.d.ts +25 -0
  32. package/dist/store/file-session-stream-provider.js +162 -0
  33. package/dist/store/file-session-stream-provider.js.map +1 -0
  34. package/dist/store/fix-msg-ascii-store-resend.js +6 -0
  35. package/dist/store/fix-msg-ascii-store-resend.js.map +1 -1
  36. package/dist/store/fix-session-store-factory.d.ts +13 -0
  37. package/dist/store/fix-session-store-factory.js +21 -0
  38. package/dist/store/fix-session-store-factory.js.map +1 -0
  39. package/dist/store/fix-session-store.d.ts +19 -0
  40. package/dist/store/fix-session-store.js +3 -0
  41. package/dist/store/fix-session-store.js.map +1 -0
  42. package/dist/store/index.d.ts +9 -0
  43. package/dist/store/index.js +9 -0
  44. package/dist/store/index.js.map +1 -1
  45. package/dist/store/memory-session-store.d.ts +27 -0
  46. package/dist/store/memory-session-store.js +104 -0
  47. package/dist/store/memory-session-store.js.map +1 -0
  48. package/dist/store/memory-session-stream-provider.d.ts +26 -0
  49. package/dist/store/memory-session-stream-provider.js +103 -0
  50. package/dist/store/memory-session-stream-provider.js.map +1 -0
  51. package/dist/store/session-id.d.ts +9 -0
  52. package/dist/store/session-id.js +55 -0
  53. package/dist/store/session-id.js.map +1 -0
  54. package/dist/store/session-stream-provider.d.ts +15 -0
  55. package/dist/store/session-stream-provider.js +3 -0
  56. package/dist/store/session-stream-provider.js.map +1 -0
  57. package/dist/store/store-config.d.ts +5 -0
  58. package/dist/store/store-config.js +3 -0
  59. package/dist/store/store-config.js.map +1 -0
  60. package/dist/transport/ascii/ascii-session.d.ts +6 -1
  61. package/dist/transport/ascii/ascii-session.js +37 -5
  62. package/dist/transport/ascii/ascii-session.js.map +1 -1
  63. package/dist/transport/session/session-description.d.ts +2 -0
  64. package/dist/transport/session/session-description.js.map +1 -1
  65. package/dist/util/definition-factory.js +1 -1
  66. package/dist/util/definition-factory.js.map +1 -1
  67. package/jsfix.test_client.txt +67 -67
  68. package/jsfix.test_server.txt +64 -64
  69. package/package.json +6 -6
  70. package/src/config/js-fix-config.ts +2 -0
  71. package/src/dictionary/parser/quickfix/dictionary-validator.ts +473 -0
  72. package/src/dictionary/parser/quickfix/index-visitor.ts +100 -0
  73. package/src/dictionary/parser/quickfix/index.ts +7 -0
  74. package/src/dictionary/parser/quickfix/quick-fix-graph-file-parser.ts +63 -0
  75. package/src/dictionary/parser/quickfix/quick-fix-graph-parser.ts +450 -0
  76. package/src/dictionary/parser/quickfix/sax-tree-builder.ts +112 -0
  77. package/src/dictionary/parser/quickfix/validation-error.ts +34 -0
  78. package/src/dictionary/parser/quickfix/x-element.ts +115 -0
  79. package/src/store/file-session-store.ts +294 -0
  80. package/src/store/file-session-stream-provider.ts +123 -0
  81. package/src/store/fix-msg-ascii-store-resend.ts +8 -0
  82. package/src/store/fix-session-store-factory.ts +31 -0
  83. package/src/store/fix-session-store.ts +37 -0
  84. package/src/store/index.ts +9 -0
  85. package/src/store/memory-session-store.ts +102 -0
  86. package/src/store/memory-session-stream-provider.ts +97 -0
  87. package/src/store/session-id.ts +32 -0
  88. package/src/store/session-stream-provider.ts +74 -0
  89. package/src/store/store-config.ts +16 -0
  90. package/src/transport/ascii/ascii-session.ts +57 -6
  91. package/src/transport/session/session-description.ts +2 -0
  92. package/src/util/definition-factory.ts +2 -2
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Lightweight in-memory XML element tree — provides DOM-like random access
3
+ * over XML parsed by SAX. Mirrors the C# XDocument/XElement API surface
4
+ * used by QuickFixXmlFileParser and DictionaryValidator.
5
+ */
6
+ export interface XElement {
7
+ readonly name: string
8
+ readonly attributes: Record<string, string>
9
+ readonly children: XElement[]
10
+ readonly line?: number
11
+ }
12
+
13
+ /**
14
+ * Query helper wrapping an XElement tree with C#-style traversal methods.
15
+ * Mirrors XDocument/XElement LINQ-to-XML API used in cspurefix.
16
+ */
17
+ export class XNode {
18
+ constructor (public readonly element: XElement) {}
19
+
20
+ /** Attribute value by name, or undefined if not present. */
21
+ attribute (name: string): string | undefined {
22
+ return this.element.attributes[name]
23
+ }
24
+
25
+ /** Direct children with the given tag name. */
26
+ elements (name?: string): XNode[] {
27
+ const children = name
28
+ ? this.element.children.filter(c => c.name === name)
29
+ : this.element.children
30
+ return children.map(c => new XNode(c))
31
+ }
32
+
33
+ /** First direct child with the given tag name, or undefined. */
34
+ firstElement (name: string): XNode | undefined {
35
+ const child = this.element.children.find(c => c.name === name)
36
+ return child ? new XNode(child) : undefined
37
+ }
38
+
39
+ /** All descendants (recursive) with the given tag name. */
40
+ descendants (name: string): XNode[] {
41
+ const result: XNode[] = []
42
+ this.collectDescendants(this.element, name, result)
43
+ return result
44
+ }
45
+
46
+ /** First descendant with the given tag name, or undefined. */
47
+ firstDescendant (name: string): XNode | undefined {
48
+ return this.findDescendant(this.element, name)
49
+ }
50
+
51
+ /** Shorthand: attribute value, throwing if missing. */
52
+ requiredAttribute (name: string): string {
53
+ const val = this.element.attributes[name]
54
+ if (val === undefined) {
55
+ throw new Error(`missing required attribute '${name}' on <${this.element.name}> at line ${this.element.line ?? '?'}`)
56
+ }
57
+ return val
58
+ }
59
+
60
+ /** The element's tag name. */
61
+ get name (): string {
62
+ return this.element.name
63
+ }
64
+
65
+ /** The element's source line number, if available. */
66
+ get line (): number | undefined {
67
+ return this.element.line
68
+ }
69
+
70
+ private collectDescendants (el: XElement, name: string, result: XNode[]): void {
71
+ for (const child of el.children) {
72
+ if (child.name === name) {
73
+ result.push(new XNode(child))
74
+ }
75
+ this.collectDescendants(child, name, result)
76
+ }
77
+ }
78
+
79
+ private findDescendant (el: XElement, name: string): XNode | undefined {
80
+ for (const child of el.children) {
81
+ if (child.name === name) {
82
+ return new XNode(child)
83
+ }
84
+ const found = this.findDescendant(child, name)
85
+ if (found) return found
86
+ }
87
+ return undefined
88
+ }
89
+ }
90
+
91
+ /**
92
+ * Parsed XML document — wraps the root element with query methods.
93
+ * Equivalent to C# XDocument.
94
+ */
95
+ export class XDocument {
96
+ public readonly root: XNode
97
+
98
+ constructor (rootElement: XElement) {
99
+ this.root = new XNode(rootElement)
100
+ }
101
+
102
+ /** All descendants of the root with the given tag name (including the root itself if it matches). */
103
+ descendants (name: string): XNode[] {
104
+ const result: XNode[] = []
105
+ if (this.root.name === name) result.push(this.root)
106
+ result.push(...this.root.descendants(name))
107
+ return result
108
+ }
109
+
110
+ /** First descendant of the root with the given tag name (including the root itself if it matches). */
111
+ firstDescendant (name: string): XNode | undefined {
112
+ if (this.root.name === name) return this.root
113
+ return this.root.firstDescendant(name)
114
+ }
115
+ }
@@ -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
+ }
@@ -13,6 +13,14 @@ export class FixMsgAsciiStoreResend {
13
13
  }
14
14
 
15
15
  public async getResendRequest (startSeq: number, endSeq: number): Promise<IFixMsgStoreRecord[]> {
16
+ // Safety feature: If ResendGapFillOnly is enabled, ALWAYS send GapFill instead of
17
+ // replaying stored messages. This prevents accidental duplicate order execution.
18
+ if (this.config.description.store?.resendGapFillOnly === true) {
19
+ const gapFillOnly: IFixMsgStoreRecord[] = []
20
+ this.gap(startSeq, endSeq + 1, gapFillOnly)
21
+ return gapFillOnly
22
+ }
23
+
16
24
  // need to cover request from start to end where any missing numbers are
17
25
  // included as gaps to allow vector of messages to be sent by the session
18
26
  // on a request
@@ -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'