@oxilite/common 0.1.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.
- package/README.md +5 -0
- package/dist/index.d.ts +150 -0
- package/dist/index.js +211 -0
- package/package.json +36 -0
package/README.md
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# @oxilite/common
|
|
2
|
+
|
|
3
|
+
RDF/JS terms (`DataFactory`, `NamedNode`, `Literal`, `Quad`…), JSON conversions and result types shared by [`@oxilite/node`](https://www.npmjs.com/package/@oxilite/node) and [`@oxilite/d1`](https://www.npmjs.com/package/@oxilite/d1). You normally get it through one of those packages.
|
|
4
|
+
|
|
5
|
+
See the [oxilite repository](https://github.com/Volland/oxilite) for documentation.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/** Any RDF/JS term (from any library: @rdfjs/data-model, N3.js, oxigraph, …). */
|
|
2
|
+
export interface TermLike {
|
|
3
|
+
termType: string;
|
|
4
|
+
value: string;
|
|
5
|
+
language?: string;
|
|
6
|
+
direction?: string;
|
|
7
|
+
datatype?: TermLike;
|
|
8
|
+
subject?: TermLike;
|
|
9
|
+
predicate?: TermLike;
|
|
10
|
+
object?: TermLike;
|
|
11
|
+
graph?: TermLike;
|
|
12
|
+
}
|
|
13
|
+
declare abstract class BaseTerm {
|
|
14
|
+
abstract readonly termType: string;
|
|
15
|
+
abstract readonly value: string;
|
|
16
|
+
equals(other: TermLike | null | undefined): boolean;
|
|
17
|
+
}
|
|
18
|
+
export declare class NamedNode extends BaseTerm {
|
|
19
|
+
readonly value: string;
|
|
20
|
+
readonly termType: "NamedNode";
|
|
21
|
+
constructor(value: string);
|
|
22
|
+
toString(): string;
|
|
23
|
+
}
|
|
24
|
+
export declare class BlankNode extends BaseTerm {
|
|
25
|
+
readonly value: string;
|
|
26
|
+
readonly termType: "BlankNode";
|
|
27
|
+
constructor(value: string);
|
|
28
|
+
toString(): string;
|
|
29
|
+
}
|
|
30
|
+
export declare class Literal extends BaseTerm {
|
|
31
|
+
readonly value: string;
|
|
32
|
+
readonly language: string;
|
|
33
|
+
readonly direction: "" | "ltr" | "rtl";
|
|
34
|
+
readonly termType: "Literal";
|
|
35
|
+
readonly datatype: NamedNode;
|
|
36
|
+
constructor(value: string, language?: string, datatype?: NamedNode, direction?: "" | "ltr" | "rtl");
|
|
37
|
+
toString(): string;
|
|
38
|
+
}
|
|
39
|
+
export declare class DefaultGraph extends BaseTerm {
|
|
40
|
+
readonly termType: "DefaultGraph";
|
|
41
|
+
readonly value: "";
|
|
42
|
+
toString(): string;
|
|
43
|
+
}
|
|
44
|
+
export declare class Variable extends BaseTerm {
|
|
45
|
+
readonly value: string;
|
|
46
|
+
readonly termType: "Variable";
|
|
47
|
+
constructor(value: string);
|
|
48
|
+
toString(): string;
|
|
49
|
+
}
|
|
50
|
+
export type Subject = NamedNode | BlankNode | Quad | Variable;
|
|
51
|
+
export type Object_ = NamedNode | BlankNode | Literal | Quad | Variable;
|
|
52
|
+
export type Graph = NamedNode | BlankNode | DefaultGraph | Variable;
|
|
53
|
+
export type Term = NamedNode | BlankNode | Literal | DefaultGraph | Variable | Quad;
|
|
54
|
+
export declare class Quad extends BaseTerm {
|
|
55
|
+
readonly subject: Subject;
|
|
56
|
+
readonly predicate: NamedNode | Variable;
|
|
57
|
+
readonly object: Object_;
|
|
58
|
+
readonly graph: Graph;
|
|
59
|
+
readonly termType: "Quad";
|
|
60
|
+
readonly value: "";
|
|
61
|
+
constructor(subject: Subject, predicate: NamedNode | Variable, object: Object_, graph?: Graph);
|
|
62
|
+
toString(): string;
|
|
63
|
+
}
|
|
64
|
+
/** RDF/JS DataFactory. */
|
|
65
|
+
export declare const DataFactory: {
|
|
66
|
+
namedNode: (value: string) => NamedNode;
|
|
67
|
+
blankNode: (value?: string) => BlankNode;
|
|
68
|
+
literal: (value: string, languageOrDatatype?: string | TermLike) => Literal;
|
|
69
|
+
defaultGraph: () => DefaultGraph;
|
|
70
|
+
variable: (value: string) => Variable;
|
|
71
|
+
quad: (subject: Subject, predicate: NamedNode | Variable, object: Object_, graph?: Graph) => Quad;
|
|
72
|
+
triple: (subject: Subject, predicate: NamedNode | Variable, object: Object_) => Quad;
|
|
73
|
+
fromTerm: (t: TermLike) => Term;
|
|
74
|
+
fromQuad: (q: TermLike) => Quad;
|
|
75
|
+
};
|
|
76
|
+
export declare const namedNode: (value: string) => NamedNode;
|
|
77
|
+
export declare const blankNode: (value?: string) => BlankNode;
|
|
78
|
+
export declare const literal: (value: string, languageOrDatatype?: string | TermLike) => Literal;
|
|
79
|
+
export declare const defaultGraph: () => DefaultGraph;
|
|
80
|
+
export declare const variable: (value: string) => Variable;
|
|
81
|
+
export declare const quad: (subject: Subject, predicate: NamedNode | Variable, object: Object_, graph?: Graph) => Quad;
|
|
82
|
+
export declare const triple: (subject: Subject, predicate: NamedNode | Variable, object: Object_) => Quad;
|
|
83
|
+
/** JSON term as produced by the oxilite core. */
|
|
84
|
+
export type TermJson = {
|
|
85
|
+
termType: string;
|
|
86
|
+
value: string;
|
|
87
|
+
language?: string;
|
|
88
|
+
direction?: string;
|
|
89
|
+
datatype?: TermJson;
|
|
90
|
+
subject?: TermJson;
|
|
91
|
+
predicate?: TermJson;
|
|
92
|
+
object?: TermJson;
|
|
93
|
+
graph?: TermJson;
|
|
94
|
+
};
|
|
95
|
+
/** Converts any RDF/JS term to the core's JSON form. */
|
|
96
|
+
export declare function toJson(t: TermLike): TermJson;
|
|
97
|
+
/** Builds a term from the core's JSON form. */
|
|
98
|
+
export declare function fromJson(j: TermJson): Term;
|
|
99
|
+
/** Output of the core (see `oxilite_core::json::output_to_json`). */
|
|
100
|
+
export type Output = {
|
|
101
|
+
kind: "solutions";
|
|
102
|
+
variables: string[];
|
|
103
|
+
rows: (TermJson | null)[][];
|
|
104
|
+
} | {
|
|
105
|
+
kind: "boolean";
|
|
106
|
+
value: boolean;
|
|
107
|
+
} | {
|
|
108
|
+
kind: "quads";
|
|
109
|
+
quads: TermJson[];
|
|
110
|
+
} | {
|
|
111
|
+
kind: "number";
|
|
112
|
+
value: number;
|
|
113
|
+
} | {
|
|
114
|
+
kind: "text";
|
|
115
|
+
value: string;
|
|
116
|
+
} | {
|
|
117
|
+
kind: "ok";
|
|
118
|
+
};
|
|
119
|
+
/** SELECT solutions (like Oxigraph's JS API: one Map per solution), ASK boolean, or quads. */
|
|
120
|
+
export type QueryResult = Map<string, Term>[] | boolean | Quad[] | string;
|
|
121
|
+
export declare function outputToResult(out: Output): QueryResult;
|
|
122
|
+
/** Query options accepted by both packages (Oxigraph's JS option names). */
|
|
123
|
+
export interface QueryOptions {
|
|
124
|
+
base_iri?: string;
|
|
125
|
+
use_default_graph_as_union?: boolean;
|
|
126
|
+
default_graph?: TermLike | TermLike[];
|
|
127
|
+
named_graphs?: TermLike[];
|
|
128
|
+
results_format?: string;
|
|
129
|
+
/** Query-time entailment (oxilite extension): `"none"` (default), `"rdfs"` or `"owl-ql"`. */
|
|
130
|
+
reasoning?: "none" | "rdfs" | "owl-ql";
|
|
131
|
+
/** Also match inferences stored by `materialize()` (oxilite extension). */
|
|
132
|
+
include_inferred?: boolean;
|
|
133
|
+
}
|
|
134
|
+
/** Load options (Oxigraph's JS option names). */
|
|
135
|
+
export interface LoadOptions {
|
|
136
|
+
format: string;
|
|
137
|
+
base_iri?: string;
|
|
138
|
+
to_graph_name?: TermLike;
|
|
139
|
+
unchecked?: boolean;
|
|
140
|
+
no_transaction?: boolean;
|
|
141
|
+
}
|
|
142
|
+
/** Dump options (Oxigraph's JS option names). */
|
|
143
|
+
export interface DumpOptions {
|
|
144
|
+
format: string;
|
|
145
|
+
from_graph_name?: TermLike;
|
|
146
|
+
}
|
|
147
|
+
/** Content of `load()`: a string, bytes, or a list of them (like Oxigraph's JS API). */
|
|
148
|
+
export type LoadData = string | Uint8Array | (string | Uint8Array)[];
|
|
149
|
+
export declare function loadDataToString(data: LoadData): string;
|
|
150
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
// RDF/JS-compatible terms and conversions shared by @oxilite/node and @oxilite/d1.
|
|
2
|
+
//
|
|
3
|
+
// The JSON forms exchanged with the oxilite core are defined in
|
|
4
|
+
// crates/oxilite-core/src/json.rs.
|
|
5
|
+
class BaseTerm {
|
|
6
|
+
equals(other) {
|
|
7
|
+
return other != null && termEquals(this, other);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export class NamedNode extends BaseTerm {
|
|
11
|
+
value;
|
|
12
|
+
termType = "NamedNode";
|
|
13
|
+
constructor(value) {
|
|
14
|
+
super();
|
|
15
|
+
this.value = value;
|
|
16
|
+
}
|
|
17
|
+
toString() {
|
|
18
|
+
return `<${this.value}>`;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export class BlankNode extends BaseTerm {
|
|
22
|
+
value;
|
|
23
|
+
termType = "BlankNode";
|
|
24
|
+
constructor(value) {
|
|
25
|
+
super();
|
|
26
|
+
this.value = value;
|
|
27
|
+
}
|
|
28
|
+
toString() {
|
|
29
|
+
return `_:${this.value}`;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const XSD_STRING = "http://www.w3.org/2001/XMLSchema#string";
|
|
33
|
+
const RDF_LANG_STRING = "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString";
|
|
34
|
+
const RDF_DIR_LANG_STRING = "http://www.w3.org/1999/02/22-rdf-syntax-ns#dirLangString";
|
|
35
|
+
function escape(s) {
|
|
36
|
+
return s.replace(/[\\"\n\r]/g, (c) => ({ "\\": "\\\\", '"': '\\"', "\n": "\\n", "\r": "\\r" })[c]);
|
|
37
|
+
}
|
|
38
|
+
export class Literal extends BaseTerm {
|
|
39
|
+
value;
|
|
40
|
+
language;
|
|
41
|
+
direction;
|
|
42
|
+
termType = "Literal";
|
|
43
|
+
datatype;
|
|
44
|
+
constructor(value, language = "", datatype, direction = "") {
|
|
45
|
+
super();
|
|
46
|
+
this.value = value;
|
|
47
|
+
this.language = language;
|
|
48
|
+
this.direction = direction;
|
|
49
|
+
this.datatype =
|
|
50
|
+
datatype ?? new NamedNode(language ? (direction ? RDF_DIR_LANG_STRING : RDF_LANG_STRING) : XSD_STRING);
|
|
51
|
+
}
|
|
52
|
+
toString() {
|
|
53
|
+
const v = `"${escape(this.value)}"`;
|
|
54
|
+
if (this.language)
|
|
55
|
+
return `${v}@${this.language}${this.direction ? `--${this.direction}` : ""}`;
|
|
56
|
+
if (this.datatype.value === XSD_STRING)
|
|
57
|
+
return v;
|
|
58
|
+
return `${v}^^${this.datatype.toString()}`;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
export class DefaultGraph extends BaseTerm {
|
|
62
|
+
termType = "DefaultGraph";
|
|
63
|
+
value = "";
|
|
64
|
+
toString() {
|
|
65
|
+
return "DEFAULT";
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
export class Variable extends BaseTerm {
|
|
69
|
+
value;
|
|
70
|
+
termType = "Variable";
|
|
71
|
+
constructor(value) {
|
|
72
|
+
super();
|
|
73
|
+
this.value = value;
|
|
74
|
+
}
|
|
75
|
+
toString() {
|
|
76
|
+
return `?${this.value}`;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
export class Quad extends BaseTerm {
|
|
80
|
+
subject;
|
|
81
|
+
predicate;
|
|
82
|
+
object;
|
|
83
|
+
graph;
|
|
84
|
+
termType = "Quad";
|
|
85
|
+
value = "";
|
|
86
|
+
constructor(subject, predicate, object, graph = new DefaultGraph()) {
|
|
87
|
+
super();
|
|
88
|
+
this.subject = subject;
|
|
89
|
+
this.predicate = predicate;
|
|
90
|
+
this.object = object;
|
|
91
|
+
this.graph = graph;
|
|
92
|
+
}
|
|
93
|
+
toString() {
|
|
94
|
+
const inner = (t) => (t instanceof Quad ? `<<( ${t.subject} ${t.predicate} ${t.object} )>>` : t.toString());
|
|
95
|
+
const g = this.graph.termType === "DefaultGraph" ? "" : ` ${this.graph}`;
|
|
96
|
+
return `${inner(this.subject)} ${this.predicate} ${inner(this.object)}${g}`;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function termEquals(a, b) {
|
|
100
|
+
if (a.termType !== b.termType || a.value !== b.value)
|
|
101
|
+
return false;
|
|
102
|
+
if (a.termType === "Literal") {
|
|
103
|
+
return ((a.language ?? "") === (b.language ?? "") &&
|
|
104
|
+
(a.direction ?? "") === (b.direction ?? "") &&
|
|
105
|
+
(a.datatype?.value ?? XSD_STRING) === (b.datatype?.value ?? XSD_STRING));
|
|
106
|
+
}
|
|
107
|
+
if (a.termType === "Quad") {
|
|
108
|
+
const parts = ["subject", "predicate", "object", "graph"];
|
|
109
|
+
return parts.every((p) => {
|
|
110
|
+
const x = a[p];
|
|
111
|
+
const y = b[p];
|
|
112
|
+
if (!x || !y)
|
|
113
|
+
return (x?.termType ?? "DefaultGraph") === (y?.termType ?? "DefaultGraph");
|
|
114
|
+
return termEquals(x, y);
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
/** RDF/JS DataFactory. */
|
|
120
|
+
export const DataFactory = {
|
|
121
|
+
namedNode: (value) => new NamedNode(value),
|
|
122
|
+
blankNode: (value) => new BlankNode(value ?? `b${Math.random().toString(36).slice(2)}`),
|
|
123
|
+
literal: (value, languageOrDatatype) => {
|
|
124
|
+
if (typeof languageOrDatatype === "string") {
|
|
125
|
+
const [lang, dir] = languageOrDatatype.split("--");
|
|
126
|
+
return new Literal(value, lang, undefined, dir ?? "");
|
|
127
|
+
}
|
|
128
|
+
if (languageOrDatatype)
|
|
129
|
+
return new Literal(value, "", new NamedNode(languageOrDatatype.value));
|
|
130
|
+
return new Literal(value);
|
|
131
|
+
},
|
|
132
|
+
defaultGraph: () => new DefaultGraph(),
|
|
133
|
+
variable: (value) => new Variable(value),
|
|
134
|
+
quad: (subject, predicate, object, graph) => new Quad(subject, predicate, object, graph ?? new DefaultGraph()),
|
|
135
|
+
triple: (subject, predicate, object) => new Quad(subject, predicate, object, new DefaultGraph()),
|
|
136
|
+
fromTerm: (t) => fromJson(toJson(t)),
|
|
137
|
+
fromQuad: (q) => fromJson(toJson(q)),
|
|
138
|
+
};
|
|
139
|
+
export const namedNode = DataFactory.namedNode;
|
|
140
|
+
export const blankNode = DataFactory.blankNode;
|
|
141
|
+
export const literal = DataFactory.literal;
|
|
142
|
+
export const defaultGraph = DataFactory.defaultGraph;
|
|
143
|
+
export const variable = DataFactory.variable;
|
|
144
|
+
export const quad = DataFactory.quad;
|
|
145
|
+
export const triple = DataFactory.triple;
|
|
146
|
+
/** Converts any RDF/JS term to the core's JSON form. */
|
|
147
|
+
export function toJson(t) {
|
|
148
|
+
switch (t.termType) {
|
|
149
|
+
case "Literal":
|
|
150
|
+
return {
|
|
151
|
+
termType: "Literal",
|
|
152
|
+
value: t.value,
|
|
153
|
+
language: t.language ?? "",
|
|
154
|
+
direction: t.direction ?? "",
|
|
155
|
+
datatype: { termType: "NamedNode", value: t.datatype?.value ?? XSD_STRING },
|
|
156
|
+
};
|
|
157
|
+
case "Quad":
|
|
158
|
+
return {
|
|
159
|
+
termType: "Quad",
|
|
160
|
+
value: "",
|
|
161
|
+
subject: toJson(t.subject),
|
|
162
|
+
predicate: toJson(t.predicate),
|
|
163
|
+
object: toJson(t.object),
|
|
164
|
+
graph: t.graph ? toJson(t.graph) : { termType: "DefaultGraph", value: "" },
|
|
165
|
+
};
|
|
166
|
+
default:
|
|
167
|
+
return { termType: t.termType, value: t.value };
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
/** Builds a term from the core's JSON form. */
|
|
171
|
+
export function fromJson(j) {
|
|
172
|
+
switch (j.termType) {
|
|
173
|
+
case "NamedNode":
|
|
174
|
+
return new NamedNode(j.value);
|
|
175
|
+
case "BlankNode":
|
|
176
|
+
return new BlankNode(j.value);
|
|
177
|
+
case "Literal":
|
|
178
|
+
return new Literal(j.value, j.language ?? "", j.datatype ? new NamedNode(j.datatype.value) : undefined, j.direction ?? "");
|
|
179
|
+
case "DefaultGraph":
|
|
180
|
+
return new DefaultGraph();
|
|
181
|
+
case "Quad":
|
|
182
|
+
return new Quad(fromJson(j.subject), fromJson(j.predicate), fromJson(j.object), j.graph ? fromJson(j.graph) : new DefaultGraph());
|
|
183
|
+
default:
|
|
184
|
+
throw new Error(`Unsupported termType ${j.termType}`);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
export function outputToResult(out) {
|
|
188
|
+
switch (out.kind) {
|
|
189
|
+
case "solutions":
|
|
190
|
+
return out.rows.map((row) => {
|
|
191
|
+
const m = new Map();
|
|
192
|
+
row.forEach((t, i) => {
|
|
193
|
+
if (t)
|
|
194
|
+
m.set(out.variables[i], fromJson(t));
|
|
195
|
+
});
|
|
196
|
+
return m;
|
|
197
|
+
});
|
|
198
|
+
case "boolean":
|
|
199
|
+
return out.value;
|
|
200
|
+
case "quads":
|
|
201
|
+
return out.quads.map((q) => fromJson(q));
|
|
202
|
+
case "text":
|
|
203
|
+
return out.value;
|
|
204
|
+
default:
|
|
205
|
+
throw new Error(`Unexpected output ${out.kind}`);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
export function loadDataToString(data) {
|
|
209
|
+
const one = (d) => (typeof d === "string" ? d : new TextDecoder().decode(d));
|
|
210
|
+
return Array.isArray(data) ? data.map(one).join("\n") : one(data);
|
|
211
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@oxilite/common",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "RDF/JS terms and result conversion shared by the oxilite JavaScript packages",
|
|
5
|
+
"license": "MIT OR Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "tsc -p ."
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/Volland/oxilite.git",
|
|
18
|
+
"directory": "packages/common"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://github.com/Volland/oxilite#readme",
|
|
21
|
+
"bugs": "https://github.com/Volland/oxilite/issues",
|
|
22
|
+
"author": "Volodymyr Pavlyshyn",
|
|
23
|
+
"keywords": [
|
|
24
|
+
"rdf",
|
|
25
|
+
"sparql",
|
|
26
|
+
"sqlite",
|
|
27
|
+
"oxigraph",
|
|
28
|
+
"rdfjs"
|
|
29
|
+
],
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=18"
|
|
35
|
+
}
|
|
36
|
+
}
|