|
| 1 | +import rdf, { BlankNode, Quad, SomeTerm } from "@ontologies/core"; |
| 2 | +import * as rdfx from "@ontologies/rdf"; |
| 3 | +// @ts-ignore |
| 4 | +import NdjsonStream from "can-ndjson-stream"; |
| 5 | + |
| 6 | +import { |
| 7 | + ExtensionResponse, |
| 8 | + RDFLibFetcherResponse, |
| 9 | + ResponseAndFallbacks, |
| 10 | + ResponseTransformer, |
| 11 | +} from "../types"; |
| 12 | + |
| 13 | +enum HexPosition { |
| 14 | + Subject = 0, |
| 15 | + Predicate, |
| 16 | + Value, |
| 17 | + Datatype, |
| 18 | + Language, |
| 19 | + Graph, |
| 20 | +} |
| 21 | + |
| 22 | +let hasReadableStreamConstructor = false; |
| 23 | + |
| 24 | +try { |
| 25 | + // tslint:disable-next-line:typedef no-empty no-unused-expression |
| 26 | + new ReadableStream({ start() {} }); |
| 27 | + hasReadableStreamConstructor = true; |
| 28 | +} catch (e) { |
| 29 | + // ignore |
| 30 | +} |
| 31 | + |
| 32 | +export const hextupleTransformer: ResponseTransformer = async (res: ResponseAndFallbacks): Promise<Quad[]> => { |
| 33 | + const bnMap: { [s: string]: BlankNode } = {}; |
| 34 | + // Skip the (expensive) proxy object when parsing |
| 35 | + const quad = rdf.quad.bind(rdf); |
| 36 | + const literal = rdf.literal.bind(rdf); |
| 37 | + const namedNode = rdf.namedNode.bind(rdf); |
| 38 | + const blankNodeF = rdf.blankNode.bind(rdf); |
| 39 | + const defaultGraph = rdf.defaultGraph.bind(rdf); |
| 40 | + |
| 41 | + const blankNode = (v: string): BlankNode => { |
| 42 | + if (!bnMap[v]) { |
| 43 | + bnMap[v] = blankNodeF(); |
| 44 | + } |
| 45 | + |
| 46 | + return bnMap[v]; |
| 47 | + }; |
| 48 | + |
| 49 | + const object = (v: string, dt: string, l: string): SomeTerm => { |
| 50 | + if (l) { |
| 51 | + return literal(v, l); |
| 52 | + } else if (dt === rdfx.ns("namedNode").value) { |
| 53 | + return namedNode(v); |
| 54 | + } else if (dt === rdfx.ns("blankNode").value) { |
| 55 | + return blankNode(v); |
| 56 | + } |
| 57 | + |
| 58 | + return literal(v, namedNode(dt)); |
| 59 | + }; |
| 60 | + |
| 61 | + const lineToQuad = (h: string[]): Quad => quad( |
| 62 | + h[HexPosition.Subject].startsWith("_:") ? blankNode(h[HexPosition.Subject]) : namedNode(h[HexPosition.Subject]), |
| 63 | + namedNode(h[HexPosition.Predicate]), |
| 64 | + object(h[HexPosition.Value], h[HexPosition.Datatype], h[HexPosition.Language]), |
| 65 | + h[HexPosition.Graph] ? namedNode(h[HexPosition.Graph]) : defaultGraph(), |
| 66 | + ); |
| 67 | + |
| 68 | + const delta: Quad[] = []; |
| 69 | + let parse; |
| 70 | + |
| 71 | + if (res instanceof Response && hasReadableStreamConstructor) { |
| 72 | + const stream = new NdjsonStream(res.body); |
| 73 | + const reader = stream.getReader(); |
| 74 | + |
| 75 | + let read: any; |
| 76 | + parse = reader |
| 77 | + .read() |
| 78 | + .then(read = (result: { done: boolean, value: string[] }): undefined => { |
| 79 | + if (result.done) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + delta.push(lineToQuad(result.value)); |
| 84 | + |
| 85 | + return reader.read().then(read); |
| 86 | + }); |
| 87 | + } else { |
| 88 | + let body; |
| 89 | + |
| 90 | + if (res instanceof Response) { |
| 91 | + body = res.text(); |
| 92 | + } else if (typeof XMLHttpRequest !== "undefined" |
| 93 | + && res instanceof XMLHttpRequest |
| 94 | + || typeof (res as any).responseText === "string") { |
| 95 | + body = Promise.resolve((res as XMLHttpRequest | RDFLibFetcherResponse).responseText); |
| 96 | + } else { |
| 97 | + body = Promise.resolve((res as ExtensionResponse).body); |
| 98 | + } |
| 99 | + |
| 100 | + parse = body |
| 101 | + .then((text) => { |
| 102 | + for (const line of text.split("\n")) { |
| 103 | + if (line.length > 0) { |
| 104 | + delta.push(lineToQuad(JSON.parse(line))); |
| 105 | + } |
| 106 | + } |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + await parse; |
| 111 | + |
| 112 | + return delta; |
| 113 | +}; |
0 commit comments