-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
submissionXmlToFieldStream: handle stream end properly #1256
base: master
Are you sure you want to change the base?
Changes from all commits
b051b6e
4b422f7
796b12d
33f1822
74688a8
b6b9c65
eeb8351
518f478
14dea7c
c4a0a2b
b136688
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,14 @@ const submissionXmlToFieldStream = (fields, xml, includeStructuralAttrs = false, | |
const stack = new SchemaStack(fields, true); | ||
let textBuffer = ''; // agglomerates text nodes that come as multiple events. | ||
const parser = new hparser.Parser({ | ||
onend: () => { | ||
if (!stack.hasExited()) { | ||
outStream.destroy(new Error('Stack has not exited yet')); | ||
} else { | ||
parser.reset(); | ||
outStream.push(null); | ||
} | ||
}, | ||
onopentag: (name, attrs) => { | ||
const field = stack.push(name); | ||
if (field != null) { | ||
|
@@ -58,18 +66,15 @@ const submissionXmlToFieldStream = (fields, xml, includeStructuralAttrs = false, | |
textBuffer += text; | ||
}, | ||
onclosetag: () => { | ||
if (stack.hasExited()) throw new Error('Stack has already exited'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't feel totally sure about throwing here. Is it impossible for there to be XML after the last tag for the fields in the If it is the case that we should throw, I feel like the throw should happen in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'm not sure, but I expect having a close tag after the stack is empty represents something very unexpected happening.
I don't think there's any guarantee there will be an |
||
|
||
const field = stack.pop(); | ||
|
||
if (textBuffer !== '' || includeEmptyNodes) { | ||
if ((field != null) && !field.isStructural()) // don't output useless whitespace | ||
outStream.push({ field, text: textBuffer }); | ||
textBuffer = ''; | ||
} | ||
|
||
if (stack.hasExited()) { | ||
parser.reset(); | ||
outStream.push(null); | ||
} | ||
} | ||
}, { xmlMode: true, decodeEntities: true }); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't feel quite sure about what
parser.reset()
is doing here. I don't seereset()
mentioned in the docs for htmlparser2 (though I know we're using an old version). If the parser has ended, and we're not reusing the parser, why do we need to reset it? Part of the reason I'm asking is that I wonder whetherreset()
had more of an effect before. Couldreset()
have had more of an effect when it was called inonclosetag
, like causing the parser to stop parsing early?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copied from current line 70. I'm also not sure why it's required, but my guess is similar to yours. Would you rather remove it?