Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public static async parseQueryResultResponseAsync(
bodyStream: stream.Stream,
conventions: DocumentConventions,
fromCache: boolean,
bodyCallback?: (body: string) => void): Promise {
const resultsPromise = RavenCommandResponsePipeline.create()
.collectBody(bodyCallback)
.parseJsonAsync([
pick({ filter: "Results" }),
streamArray()
])
.streamKeyCaseTransform("camel", "DOCUMENT_LOAD") // we don't care about the case in facets
.collectResult((result, next) => [...result, next["value"]], [])
.process(bodyStream);
const includesPromise = parseDocumentIncludes(bodyStream, conventions);
const restPromise = parseRestOfOutput(bodyStream, /^Results|Includes$/);
const [results, includes, rest] = await Promise.all([resultsPromise, includesPromise, restPromise]);
const rawResult = Object.assign({} as any, rest, { results, includes }) as QueryResult;
const queryResult = conventions.objectMapper.fromObjectLiteral(rawResult, {
typeName: QueryResult.name,
nestedTypes: {
indexTimestamp: "date",
lastQueryTime: "date"
.streamKeyCaseTransform({
defaultTransform: "camel",
ignorePaths: [/\./],
})
.collectResult({
initResult: [] as GetResponse[],
reduceResults: (result: GetResponse[], next) => {
return [...result, next["value"]];
}
})
.process(bodyStream);
const responsesResultsPromise = this._pipeline()
.parseJsonAsync([
pick({ filter: "Results" }),
pick({ filter: /^\d+\.Result\b/i }),
streamValues()
])
.collectResult({
initResult: [] as string[],
reduceResults: (result: string[], next) => {
// TODO try read it another way
const resResult = JSON.stringify(next["value"]);
return [...result, resResult];
}
})
.process(bodyStream);
const [responses, responsesResults] = await Promise.all([responsesPromise, responsesResultsPromise]);
for (let i = 0; i < responses.length; i++) {
const res = responses[i];
res.result = responsesResults[i];
export function getDocumentResultsPipeline(
conventions: DocumentConventions): RavenCommandResponsePipeline {
return RavenCommandResponsePipeline.create()
.parseJsonAsync([
pick({ filter: "Results" }),
streamArray(),
])
.streamKeyCaseTransform(conventions.entityFieldNameConvention, "DOCUMENT_LOAD");
}
export function parseDocumentIncludes(
bodyStream: stream.Stream,
conventions: DocumentConventions) {
return RavenCommandResponsePipeline.create<{ [key: string]: object }>()
.parseJsonAsync([
pick({ filter: "Includes" }),
streamObject()
])
.streamKeyCaseTransform(conventions.entityFieldNameConvention, "DOCUMENT_LOAD")
.collectResult((result, next) => {
result[next["key"]] = next["value"];
return result;
}, {})
.process(bodyStream);
}