@factoidal/core 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.
Files changed (43) hide show
  1. package/CHANGELOG.md +121 -0
  2. package/LICENSE +201 -0
  3. package/README.md +514 -0
  4. package/browser-wasm.js +872 -0
  5. package/browser.d.ts +514 -0
  6. package/browser.js +2276 -0
  7. package/factoidal-npm-entry.js +32609 -0
  8. package/factoidal-npm-entry.wasm.assets/code-7ac046580f1bbdda8dc6.wasm +0 -0
  9. package/factoidal-npm-entry.wasm.js +455 -0
  10. package/factoidal.js +27560 -0
  11. package/factoidal.wasm.assets/code-bbe6099bfb5b10c4c3ab.wasm +0 -0
  12. package/factoidal.wasm.js +457 -0
  13. package/fn.d.ts +519 -0
  14. package/fn.js +916 -0
  15. package/hacl-init.js +92 -0
  16. package/hacl-wasm/FStar.wasm +0 -0
  17. package/hacl-wasm/Hacl_Bignum.wasm +0 -0
  18. package/hacl-wasm/Hacl_Bignum25519_51.wasm +0 -0
  19. package/hacl-wasm/Hacl_Bignum_Base.wasm +0 -0
  20. package/hacl-wasm/Hacl_Curve25519_51.wasm +0 -0
  21. package/hacl-wasm/Hacl_Ed25519.wasm +0 -0
  22. package/hacl-wasm/Hacl_Ed25519_PrecompTable.wasm +0 -0
  23. package/hacl-wasm/Hacl_Hash_Base.wasm +0 -0
  24. package/hacl-wasm/Hacl_Hash_SHA2.wasm +0 -0
  25. package/hacl-wasm/Hacl_IntTypes_Intrinsics.wasm +0 -0
  26. package/hacl-wasm/LowStar_Endianness.wasm +0 -0
  27. package/hacl-wasm/WasmSupport.wasm +0 -0
  28. package/hacl-wasm/api.js +775 -0
  29. package/hacl-wasm/api.json +3787 -0
  30. package/hacl-wasm/layouts.json +1 -0
  31. package/hacl-wasm/loader.js +568 -0
  32. package/hacl-wasm/shell.js +12 -0
  33. package/index.d.ts +1068 -0
  34. package/index.js +237 -0
  35. package/index.mjs +85 -0
  36. package/lib/api.js +2140 -0
  37. package/lib/engine-js.js +165 -0
  38. package/lib/engine-wasm.js +300 -0
  39. package/package.json +101 -0
  40. package/rdfjs.js +540 -0
  41. package/version.json +101 -0
  42. package/wasm.d.ts +130 -0
  43. package/wasm.js +158 -0
package/index.js ADDED
@@ -0,0 +1,237 @@
1
+ // factoidal — formally-verified SPARQL 1.1 + RDF 1.1 for Node.
2
+ //
3
+ // CommonJS entry point (js_of_ocaml engine). Public API:
4
+ //
5
+ // parse(text, {format, baseIRI}) -> Promise<Dataset>
6
+ // query(data, sparql, {entail}) -> Promise<Bindings[] | boolean | Dataset>
7
+ // queryHdt(hdtBytes, sparql) -> Promise<Bindings[] | boolean>
8
+ // (read-only HDT artifact, CLI-only,
9
+ // no npm-entry bundle needed)
10
+ // update(data, updateText) -> Promise<Dataset>
11
+ // serialize(data, {format}) -> Promise<string>
12
+ // canonicalize(data, {format}) -> Promise<string> (RDFC-1.0)
13
+ // graphs(dataset) -> Array<[iri, Dataset]> (named graphs)
14
+ // canonicalHash(datasetOrGraph) -> Promise<string> (RDFC-1.0, one graph)
15
+ // shaclValidate(data, shapes) -> Promise<{conforms, report: Dataset}>
16
+ // shexValidate(data, schema, focus, shape) -> Promise<boolean|null>
17
+ // owlClosure(data, mode) -> Promise<Dataset> ('RDFS'|'OWL-RL')
18
+ // rmlMap(mapping, sourceData, kind) -> Promise<Dataset> (kind: 'json'|'csv')
19
+ // csvwToRdf(csvText, metadataJson, options) -> Promise<Dataset> (CSVW csv2rdf)
20
+ // jsonldToRdf(jsonldText, options) -> Promise<Dataset>
21
+ // rifEval(data, rifRulesXml) -> Promise<Dataset> (RIF Core saturation)
22
+ // openCottas(bytes) -> Promise<string> (handle; bytes is
23
+ // hex|Uint8Array|ArrayBuffer of a
24
+ // whole .cottas artifact)
25
+ // queryCottas(handle, sparql) -> Promise<Bindings[] | boolean | Dataset>
26
+ // (in-memory COTTAS bytes store --
27
+ // rows decode lazily, no heap Dataset)
28
+ // closeCottas(handle) -> Promise<void>
29
+ // toCottas(data) -> Promise<Uint8Array> (native COTTAS
30
+ // writer; round-trips into openCottas)
31
+ // dataFactory -> RDF/JS DataFactory
32
+ // Dataset -> RDF/JS DatasetCore class
33
+ //
34
+ // shaclValidate/shexValidate/owlClosure/rmlMap/csvwToRdf/jsonldToRdf/rifEval/
35
+ // openCottas/queryCottas/closeCottas/toCottas need the npm-entry ABI bundle
36
+ // (same gate as update()/canonicalize()); see capabilities() to probe for it.
37
+ // See docs/designissues/2026-07-06-inmemory-bytes-store.md for the
38
+ // openCottas/queryCottas/toCottas design and its documented divergences
39
+ // from parse()/query() (no entailment, no write overlay, read-only).
40
+ //
41
+ // Bindings are Maps of variable name -> RDF/JS Term. See index.d.ts
42
+ // for the full types and README.md for examples.
43
+ //
44
+ // The legacy single-shot API from 0.1.0-alpha scaffolding is kept as
45
+ // queryRaw(dataString, queryString, {dataFormat, entail, output}) —
46
+ // it returns parsed SPARQL Results JSON (or a raw string for
47
+ // non-JSON output formats).
48
+ //
49
+ // For the wasm engine with the same API, require('factoidal/wasm').
50
+
51
+ 'use strict';
52
+
53
+ const fs = require('node:fs');
54
+ const path = require('node:path');
55
+
56
+ const engine = require('./lib/engine-js.js');
57
+ const { buildApi } = require('./lib/api.js');
58
+ const rdfjs = require('./rdfjs.js');
59
+
60
+ const pkg = require('./package.json');
61
+ const version = pkg.version;
62
+
63
+ // ---------------------------------------------------------------------
64
+ // npm-entry ABI loader. The entry bundle (built from
65
+ // bin/npm-entry/entry_jsoo.ml) registers `factoidalNpmEntry` on
66
+ // module.exports (Node) / globalThis (browser). Optional — everything
67
+ // the CLI bundle can do works without it.
68
+ // ---------------------------------------------------------------------
69
+
70
+ function entryCandidates() {
71
+ const c = [];
72
+ if (process.env.FACTOIDAL_NPM_ENTRY) c.push(process.env.FACTOIDAL_NPM_ENTRY);
73
+ c.push(path.join(__dirname, 'factoidal-npm-entry.js'));
74
+ c.push(path.resolve(
75
+ __dirname, '..', '..', 'docs', 'fstar-extracted', 'factoidal-npm-entry.js'));
76
+ return c;
77
+ }
78
+
79
+ function loadEntry() {
80
+ for (const p of entryCandidates()) {
81
+ if (!p || !fs.existsSync(p)) continue;
82
+ const mod = require(p);
83
+ const abi = (mod && mod.factoidalNpmEntry) ||
84
+ (globalThis.factoidalNpmEntry);
85
+ if (abi && typeof abi.queryDataset === 'function') return abi;
86
+ }
87
+ return null;
88
+ }
89
+
90
+ const api = buildApi({
91
+ engineName: 'js',
92
+ runCli: engine.runCli,
93
+ loadEntry,
94
+ // VC crypto init hook: the typed vc* wrappers auto-await this once,
95
+ // on the first VC call, so a Node caller never has to remember the
96
+ // HACL* wasm init step. initHacl() is idempotent + memoized.
97
+ initCrypto: () => require('./hacl-init.js').initHacl(),
98
+ });
99
+
100
+ // ---------------------------------------------------------------------
101
+ // Legacy raw API (pre-RDF/JS scaffolding surface, kept for the smoke
102
+ // tests and for callers that want the CLI's other output formats).
103
+ // ---------------------------------------------------------------------
104
+
105
+ const OUTPUT_FORMATS = new Set(['json', 'csv', 'tsv', 'xml', 'table', 'ntriples']);
106
+ const ENTAIL_VALUES = new Set(['none', 'RDFS', 'OWL-RL']);
107
+ const DATA_FORMAT_EXT = {
108
+ turtle: 'ttl', ttl: 'ttl', ntriples: 'nt', nt: 'nt', nquads: 'nq',
109
+ nq: 'nq', trig: 'trig', rdfxml: 'rdf', 'rdf-xml': 'rdf', rdf: 'rdf',
110
+ };
111
+
112
+ function queryRaw(dataString, queryString, options) {
113
+ return Promise.resolve().then(() => {
114
+ if (typeof dataString !== 'string') {
115
+ throw new TypeError('queryRaw: dataString must be a string');
116
+ }
117
+ if (typeof queryString !== 'string') {
118
+ throw new TypeError('queryRaw: queryString must be a string');
119
+ }
120
+ const opts = options || {};
121
+ const dataFormat = String(opts.dataFormat || 'turtle').toLowerCase();
122
+ const entail = opts.entail || 'none';
123
+ const output = opts.output || 'json';
124
+ if (!(dataFormat in DATA_FORMAT_EXT)) {
125
+ throw new TypeError(`queryRaw: unknown dataFormat '${opts.dataFormat}'`);
126
+ }
127
+ if (!ENTAIL_VALUES.has(entail)) {
128
+ throw new TypeError(
129
+ `queryRaw: entail must be one of ${[...ENTAIL_VALUES].join(', ')}`);
130
+ }
131
+ if (!OUTPUT_FORMATS.has(output)) {
132
+ throw new TypeError(
133
+ `queryRaw: output must be one of ${[...OUTPUT_FORMATS].join(', ')}`);
134
+ }
135
+
136
+ const name = `/static/data.${DATA_FORMAT_EXT[dataFormat]}`;
137
+ const args = ['-d', name, '-e', queryString, '-o', output];
138
+ if (entail !== 'none') args.push('--entail', entail);
139
+
140
+ const res = engine.runCli(args, [{ name, content: dataString }]);
141
+ if (res.exitCode !== 0) {
142
+ const msg = (res.stderr || res.stdout ||
143
+ `factoidal exited with code ${res.exitCode}`).trim();
144
+ const err = new Error('SPARQL query failed: ' + msg);
145
+ err.exitCode = res.exitCode;
146
+ err.stderr = res.stderr;
147
+ err.stdout = res.stdout;
148
+ throw err;
149
+ }
150
+ if (output !== 'json') return res.stdout;
151
+
152
+ const first = res.stdout.indexOf('{');
153
+ const last = res.stdout.lastIndexOf('}');
154
+ if (first < 0 || last < first) {
155
+ const err = new Error(
156
+ 'factoidal did not produce JSON on stdout. Raw output: ' + res.stdout);
157
+ err.stdout = res.stdout;
158
+ err.stderr = res.stderr;
159
+ throw err;
160
+ }
161
+ return JSON.parse(res.stdout.slice(first, last + 1));
162
+ });
163
+ }
164
+
165
+ module.exports = {
166
+ // Public typed API
167
+ parse: api.parse,
168
+ query: api.query,
169
+ queryHdt: api.queryHdt,
170
+ registerExtensionFunction: api.registerExtensionFunction,
171
+ unregisterExtensionFunction: api.unregisterExtensionFunction,
172
+ clearExtensionFunctions: api.clearExtensionFunctions,
173
+ registerServiceEndpoint: api.registerServiceEndpoint,
174
+ clearServiceEndpoints: api.clearServiceEndpoints,
175
+ update: api.update,
176
+ serialize: api.serialize,
177
+ canonicalize: api.canonicalize,
178
+ graphs: api.graphs,
179
+ canonicalHash: api.canonicalHash,
180
+ shaclValidate: api.shaclValidate,
181
+ shexValidate: api.shexValidate,
182
+ owlClosure: api.owlClosure,
183
+ coreRdfsClosure: api.coreRdfsClosure,
184
+ rdfsPlusClosure: api.rdfsPlusClosure,
185
+ coreRdfsCheck: api.coreRdfsCheck,
186
+ rhoDfClosure: api.rhoDfClosure,
187
+ rhoDfFragmentCheck: api.rhoDfFragmentCheck,
188
+ tableauMaterialise: api.tableauMaterialise,
189
+ tableauDlInconsistent: api.tableauDlInconsistent,
190
+ owlIsConsistent: api.owlIsConsistent,
191
+ owlEntails: api.owlEntails,
192
+ rmlMap: api.rmlMap,
193
+ csvwToRdf: api.csvwToRdf,
194
+ jsonldToRdf: api.jsonldToRdf,
195
+ jsonldFromRdf: api.jsonldFromRdf,
196
+ didKeyResolve: api.didKeyResolve,
197
+ xmlWellformed: api.xmlWellformed,
198
+ xpathEval: api.xpathEval,
199
+ rifEval: api.rifEval,
200
+ xsltTransform: api.xsltTransform,
201
+ mathmlEval: api.mathmlEval,
202
+ xformsRecalc: api.xformsRecalc,
203
+ jsonSchemaValidate: api.jsonSchemaValidate,
204
+ schematronValidate: api.schematronValidate,
205
+ toanSummation: api.toanSummation,
206
+ toanProduct: api.toanProduct,
207
+ toanSimplify: api.toanSimplify,
208
+ toanDiff: api.toanDiff,
209
+ toanSubst: api.toanSubst,
210
+ matrixDeterminant: api.matrixDeterminant,
211
+ matrixScalarProduct: api.matrixScalarProduct,
212
+ matrixVectorProduct: api.matrixVectorProduct,
213
+ matrixOuterProduct: api.matrixOuterProduct,
214
+ sigmoidPoints: api.sigmoidPoints,
215
+ sigmoidFormulaMathml: api.sigmoidFormulaMathml,
216
+ vcSha256Hex: api.vcSha256Hex,
217
+ vcEd25519SecretToPublic: api.vcEd25519SecretToPublic,
218
+ vcEd25519Sign: api.vcEd25519Sign,
219
+ vcEd25519Verify: api.vcEd25519Verify,
220
+ vcEddsaCreateFromCanonical: api.vcEddsaCreateFromCanonical,
221
+ vcEddsaVerifyFromCanonical: api.vcEddsaVerifyFromCanonical,
222
+ vcCheckCredential: api.vcCheckCredential,
223
+ vcCheckCredentialSubject: api.vcCheckCredentialSubject,
224
+ vcCheckNoDataLoss: api.vcCheckNoDataLoss,
225
+ vcCheckRelatedResourceDigests: api.vcCheckRelatedResourceDigests,
226
+ openCottas: api.openCottas,
227
+ queryCottas: api.queryCottas,
228
+ closeCottas: api.closeCottas,
229
+ toCottas: api.toCottas,
230
+ capabilities: api.capabilities,
231
+ Dataset: rdfjs.Dataset,
232
+ dataFactory: rdfjs.dataFactory,
233
+ // Legacy raw surface
234
+ queryRaw,
235
+ // Metadata
236
+ version,
237
+ };
package/index.mjs ADDED
@@ -0,0 +1,85 @@
1
+ // ESM entry point. Re-exports the CJS implementation so we have one
2
+ // source of truth for the Node-side driver. Browsers should import
3
+ // `./browser.js` via the `"browser"` export condition instead.
4
+ //
5
+ // import { parse, query, canonicalize, dataFactory } from '@factoidal/core';
6
+ // const ds = await parse('@prefix : <http://ex/> . :a :p :b .');
7
+ // const rows = await query(ds, 'SELECT * WHERE { ?s ?p ?o }');
8
+
9
+ import cjs from './index.js';
10
+
11
+ export const parse = cjs.parse;
12
+ export const query = cjs.query;
13
+ export const queryHdt = cjs.queryHdt;
14
+ export const registerExtensionFunction = cjs.registerExtensionFunction;
15
+ export const unregisterExtensionFunction = cjs.unregisterExtensionFunction;
16
+ export const clearExtensionFunctions = cjs.clearExtensionFunctions;
17
+ export const registerServiceEndpoint = cjs.registerServiceEndpoint;
18
+ export const clearServiceEndpoints = cjs.clearServiceEndpoints;
19
+ export const update = cjs.update;
20
+ export const serialize = cjs.serialize;
21
+ export const canonicalize = cjs.canonicalize;
22
+ export const graphs = cjs.graphs;
23
+ export const canonicalHash = cjs.canonicalHash;
24
+ export const capabilities = cjs.capabilities;
25
+ export const Dataset = cjs.Dataset;
26
+ export const dataFactory = cjs.dataFactory;
27
+ export const queryRaw = cjs.queryRaw;
28
+ export const version = cjs.version;
29
+
30
+ // Validation / inference / mapping engines (need the npm-entry bundle).
31
+ export const shaclValidate = cjs.shaclValidate;
32
+ export const shexValidate = cjs.shexValidate;
33
+ export const owlClosure = cjs.owlClosure;
34
+ export const coreRdfsClosure = cjs.coreRdfsClosure;
35
+ export const rdfsPlusClosure = cjs.rdfsPlusClosure;
36
+ export const coreRdfsCheck = cjs.coreRdfsCheck;
37
+ export const rhoDfClosure = cjs.rhoDfClosure;
38
+ export const rhoDfFragmentCheck = cjs.rhoDfFragmentCheck;
39
+ export const tableauMaterialise = cjs.tableauMaterialise;
40
+ export const tableauDlInconsistent = cjs.tableauDlInconsistent;
41
+ export const owlIsConsistent = cjs.owlIsConsistent;
42
+ export const owlEntails = cjs.owlEntails;
43
+ export const rmlMap = cjs.rmlMap;
44
+ export const csvwToRdf = cjs.csvwToRdf;
45
+ export const jsonldToRdf = cjs.jsonldToRdf;
46
+ export const jsonldFromRdf = cjs.jsonldFromRdf;
47
+ export const didKeyResolve = cjs.didKeyResolve;
48
+ export const xmlWellformed = cjs.xmlWellformed;
49
+ export const xpathEval = cjs.xpathEval;
50
+ export const rifEval = cjs.rifEval;
51
+
52
+ // In-memory COTTAS/Parquet bytes store (need the npm-entry bundle).
53
+ export const openCottas = cjs.openCottas;
54
+ export const queryCottas = cjs.queryCottas;
55
+ export const closeCottas = cjs.closeCottas;
56
+ export const toCottas = cjs.toCottas;
57
+
58
+ // Typed engine functions (#74 npm FP surface).
59
+ export const xsltTransform = cjs.xsltTransform;
60
+ export const mathmlEval = cjs.mathmlEval;
61
+ export const xformsRecalc = cjs.xformsRecalc;
62
+ export const jsonSchemaValidate = cjs.jsonSchemaValidate;
63
+ export const schematronValidate = cjs.schematronValidate;
64
+ export const toanSummation = cjs.toanSummation;
65
+ export const toanProduct = cjs.toanProduct;
66
+ export const toanSimplify = cjs.toanSimplify;
67
+ export const toanDiff = cjs.toanDiff;
68
+ export const toanSubst = cjs.toanSubst;
69
+ export const matrixDeterminant = cjs.matrixDeterminant;
70
+ export const matrixScalarProduct = cjs.matrixScalarProduct;
71
+ export const matrixVectorProduct = cjs.matrixVectorProduct;
72
+ export const matrixOuterProduct = cjs.matrixOuterProduct;
73
+ export const sigmoidPoints = cjs.sigmoidPoints;
74
+ export const sigmoidFormulaMathml = cjs.sigmoidFormulaMathml;
75
+
76
+ // VC Data Integrity crypto (eddsa-rdfc-2022, HACL* wasm backend). The
77
+ // typed wrappers auto-await initHacl() on first call (see index.js).
78
+ export const vcSha256Hex = cjs.vcSha256Hex;
79
+ export const vcEd25519SecretToPublic = cjs.vcEd25519SecretToPublic;
80
+ export const vcEd25519Sign = cjs.vcEd25519Sign;
81
+ export const vcEd25519Verify = cjs.vcEd25519Verify;
82
+ export const vcEddsaCreateFromCanonical = cjs.vcEddsaCreateFromCanonical;
83
+ export const vcEddsaVerifyFromCanonical = cjs.vcEddsaVerifyFromCanonical;
84
+
85
+ export default cjs;