@agfpd/iapeer 0.4.53 → 0.4.54
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.
- package/package.json +1 -1
- package/src/codec/index.ts +44 -2
package/package.json
CHANGED
package/src/codec/index.ts
CHANGED
|
@@ -112,7 +112,10 @@ function indexOfOutsideCdata(buffer: string, needle: string, start: number): num
|
|
|
112
112
|
function readTagContent(xml: string, tag: string): string | undefined {
|
|
113
113
|
const open = `<${tag}>`
|
|
114
114
|
const close = `</${tag}>`
|
|
115
|
-
|
|
115
|
+
// В37 — the OPEN tag must be located CDATA-aware too (the close-side already was): a
|
|
116
|
+
// message whose CDATA body QUOTES `<attachments>…</attachments>` otherwise minted
|
|
117
|
+
// phantom attachments, and a quoted `<message>` corrupted the decoded message.
|
|
118
|
+
const openIdx = indexOfOutsideCdata(xml, open, 0)
|
|
116
119
|
if (openIdx < 0) return undefined
|
|
117
120
|
let i = openIdx + open.length
|
|
118
121
|
let out = ''
|
|
@@ -191,12 +194,36 @@ export function decodeEnvelope(xml: string): IapEnvelope {
|
|
|
191
194
|
}
|
|
192
195
|
}
|
|
193
196
|
|
|
197
|
+
// В38 — a real envelope's open tag is bounded: personality/runtime ≤32 chars each,
|
|
198
|
+
// topic ≤200, intelligence one word, plus attr names and escaping. 1 KiB is far above
|
|
199
|
+
// any legitimate open tag; a longer '>'-less run after `<iap ` is prose, not a tag.
|
|
200
|
+
const MAX_OPEN_TAG_LEN = 1024
|
|
201
|
+
|
|
202
|
+
/** Classify the text at a `<iap ` occurrence: a complete VALID open tag (required attrs
|
|
203
|
+
* present), a complete-but-INVALID one / overlong tagless run (prose that merely contains
|
|
204
|
+
* the marker), or INCOMPLETE (no '>' yet within bounds — wait for the next chunk). */
|
|
205
|
+
function openTagVerdict(candidate: string): 'valid' | 'invalid' | 'incomplete' {
|
|
206
|
+
const m = /^<iap\s+([^>]*)>/.exec(candidate)
|
|
207
|
+
if (!m) {
|
|
208
|
+
const unclosed = candidate.indexOf('>') < 0
|
|
209
|
+
return unclosed && candidate.length <= MAX_OPEN_TAG_LEN ? 'incomplete' : 'invalid'
|
|
210
|
+
}
|
|
211
|
+
if (m[0].length > MAX_OPEN_TAG_LEN) return 'invalid'
|
|
212
|
+
return m[1].includes('from-personality="') && m[1].includes('from-runtime="') ? 'valid' : 'invalid'
|
|
213
|
+
}
|
|
214
|
+
|
|
194
215
|
/**
|
|
195
216
|
* Pull complete `<iap …>…</iap>` envelopes out of a streaming buffer.
|
|
196
217
|
* The closing `</iap>` is located CDATA-aware, so an envelope whose message
|
|
197
218
|
* body contains the literal text `</iap>` is not truncated. `rest` holds the
|
|
198
219
|
* trailing bytes that do not yet form a complete envelope (incl. an envelope
|
|
199
220
|
* still mid-CDATA), to be prepended to the next chunk.
|
|
221
|
+
*
|
|
222
|
+
* В38 — a FALSE start (prose that merely contains `<iap `, e.g. a quoted tool
|
|
223
|
+
* description) is never committed to: the open tag is validated first, and a
|
|
224
|
+
* candidate whose decode fails RESYNCS one char forward instead of being kept
|
|
225
|
+
* (which used to either swallow the NEXT real envelope into a mis-attributed
|
|
226
|
+
* blob, or park the buffer forever waiting for a close that never comes).
|
|
200
227
|
*/
|
|
201
228
|
export function extractEnvelopes(buffer: string): { envelopes: string[]; rest: string } {
|
|
202
229
|
const envelopes: string[] = []
|
|
@@ -208,10 +235,25 @@ export function extractEnvelopes(buffer: string): { envelopes: string[]; rest: s
|
|
|
208
235
|
return { envelopes, rest: rest.slice(Math.max(0, rest.length - '<iap '.length)) }
|
|
209
236
|
}
|
|
210
237
|
if (start > 0) rest = rest.slice(start)
|
|
238
|
+
const verdict = openTagVerdict(rest)
|
|
239
|
+
if (verdict === 'incomplete') return { envelopes, rest } // open tag split across chunks → wait
|
|
240
|
+
if (verdict === 'invalid') {
|
|
241
|
+
rest = rest.slice(1) // false start: skip past this '<' and rescan for the next '<iap '
|
|
242
|
+
continue
|
|
243
|
+
}
|
|
211
244
|
const close = indexOfOutsideCdata(rest, '</iap>', '<iap '.length)
|
|
212
245
|
if (close < 0) return { envelopes, rest } // incomplete (or mid-CDATA) → wait
|
|
213
246
|
const envelopeEnd = close + '</iap>'.length
|
|
214
|
-
|
|
247
|
+
const candidate = rest.slice(0, envelopeEnd)
|
|
248
|
+
try {
|
|
249
|
+
decodeEnvelope(candidate)
|
|
250
|
+
} catch {
|
|
251
|
+
// Structurally envelope-shaped but undecodable → a false start after all;
|
|
252
|
+
// resync so a REAL envelope inside/behind the candidate is still found.
|
|
253
|
+
rest = rest.slice(1)
|
|
254
|
+
continue
|
|
255
|
+
}
|
|
256
|
+
envelopes.push(candidate)
|
|
215
257
|
rest = rest.slice(envelopeEnd)
|
|
216
258
|
}
|
|
217
259
|
}
|