eyeling 1.19.6 → 1.20.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/tools/bundle.js CHANGED
@@ -6,7 +6,10 @@ const path = require('path');
6
6
 
7
7
  const ROOT = path.resolve(__dirname, '..');
8
8
  const ENTRY = path.join(ROOT, 'lib', 'entry.js');
9
- const OUT = path.join(ROOT, 'eyeling.js');
9
+ const NODE_OUT = path.join(ROOT, 'eyeling.js');
10
+ const BROWSER_DIR = path.join(ROOT, 'dist', 'browser');
11
+ const BROWSER_BUNDLE_OUT = path.join(BROWSER_DIR, 'eyeling.browser.js');
12
+ const BROWSER_ENTRY_OUT = path.join(BROWSER_DIR, 'index.mjs');
10
13
 
11
14
  function normalizeRel(p) {
12
15
  return path.relative(ROOT, p).split(path.sep).join('/');
@@ -20,10 +23,7 @@ function resolveInternal(fromFile, req) {
20
23
  const baseDir = path.dirname(fromFile);
21
24
  let abs = path.resolve(baseDir, req);
22
25
 
23
- // If no extension, assume .js
24
- if (!path.extname(abs)) abs = abs + '.js';
25
-
26
- // If it's a directory, prefer index.js
26
+ if (!path.extname(abs)) abs += '.js';
27
27
  if (fs.existsSync(abs) && fs.statSync(abs).isDirectory()) {
28
28
  abs = path.join(abs, 'index.js');
29
29
  }
@@ -32,7 +32,6 @@ function resolveInternal(fromFile, req) {
32
32
  }
33
33
 
34
34
  function parseRequires(src) {
35
- // Very small/naive scanner – good enough for this repo.
36
35
  const re = /require\(\s*['"]([^'"]+)['"]\s*\)/g;
37
36
  const out = [];
38
37
  let m;
@@ -41,7 +40,7 @@ function parseRequires(src) {
41
40
  }
42
41
 
43
42
  /** @type {Map<string,string>} */
44
- const modules = new Map(); // rel -> source
43
+ const modules = new Map();
45
44
 
46
45
  function addModule(absFile) {
47
46
  const rel = normalizeRel(absFile);
@@ -51,153 +50,265 @@ function addModule(absFile) {
51
50
  modules.set(rel, src);
52
51
 
53
52
  for (const req of parseRequires(src)) {
54
- // Keep JSON and bare imports external.
55
53
  if (!isInternalRequest(req)) continue;
56
54
  if (req.endsWith('.json')) continue;
57
55
 
58
56
  const depAbs = resolveInternal(absFile, req);
59
- // Only bundle files inside ROOT.
60
57
  if (!normalizeRel(depAbs) || !path.resolve(depAbs).startsWith(ROOT)) continue;
61
58
  if (!fs.existsSync(depAbs)) continue;
62
59
  addModule(depAbs);
63
60
  }
64
61
  }
65
62
 
66
- addModule(ENTRY);
67
-
68
- const keys = Array.from(modules.keys()).sort();
69
-
70
- const out = [];
71
- out.push('#!/usr/bin/env node');
72
- out.push("'use strict';");
73
- out.push('');
74
- out.push('(function(){');
75
- out.push(' const __outerRequire = (typeof require === "function") ? require : null;');
76
- out.push(' const __outerModule = (typeof module !== "undefined") ? module : null;');
77
- out.push(' const __outerSelf = (typeof self !== "undefined") ? self : null;');
78
- out.push(' const __modules = Object.create(null);');
79
- out.push(' const __cache = Object.create(null);');
80
- out.push('');
81
- out.push(' // ---- bundled modules ----');
82
- for (const k of keys) {
83
- out.push(` __modules[${JSON.stringify(k)}] = function(require, module, exports){`);
84
- out.push(modules.get(k).replace(/\r\n/g, '\n'));
85
- out.push(' };');
86
- }
87
-
88
- out.push('');
89
- out.push(' function __normPath(p){');
90
- out.push(' const segs = [];');
91
- out.push(' for (const part of p.split("/")) {');
92
- out.push(' if (!part || part === ".") continue;');
93
- out.push(' if (part === "..") segs.pop();');
94
- out.push(' else segs.push(part);');
95
- out.push(' }');
96
- out.push(' return segs.join("/");');
97
- out.push(' }');
98
-
99
- out.push(' function __resolve(fromId, req){');
100
- out.push(' if (!(req && (req.startsWith("./") || req.startsWith("../")))) return req;');
101
- out.push(' const base = fromId.split("/").slice(0, -1).join("/");');
102
- out.push(' let p = base ? (base + "/" + req) : req;');
103
- out.push(' p = __normPath(p);');
104
- out.push(' if (!p.endsWith(".js") && !p.endsWith(".json")) p += ".js";');
105
- out.push(' return p;');
106
- out.push(' }');
107
-
108
- out.push(' function __makeRequire(fromId){');
109
- out.push(' function r(req){');
110
- out.push(' if (!(req && (req.startsWith("./") || req.startsWith("../")))) {');
111
- out.push(' if (__outerRequire) return __outerRequire(req);');
112
- out.push(' throw new Error("Cannot require external module: " + req);');
113
- out.push(' }');
114
- out.push(' const id = __resolve(fromId, req);');
115
- out.push(' if (!__modules[id]) {');
116
- out.push(' if (__outerRequire) return __outerRequire(req);');
117
- out.push(' throw new Error("Cannot find bundled module: " + id);');
118
- out.push(' }');
119
- out.push(' if (__cache[id]) return __cache[id].exports;');
120
- out.push(' const m = { exports: {} };');
121
- out.push(' __cache[id] = m;');
122
- out.push(' __modules[id](__makeRequire(id), m, m.exports);');
123
- out.push(' return m.exports;');
124
- out.push(' }');
125
- out.push(' r.main = (__outerRequire && __outerRequire.main) ? __outerRequire.main : null;');
126
- out.push(' return r;');
127
- out.push(' }');
128
-
129
- out.push('');
130
- out.push(' function __loadEntry(){');
131
- out.push(` const id = ${JSON.stringify(normalizeRel(ENTRY))};`);
132
- out.push(' if (!__modules[id]) throw new Error("Missing entry module: " + id);');
133
- out.push(' if (__cache[id]) return __cache[id].exports;');
134
- out.push(' const m = { exports: {} };');
135
- out.push(' __cache[id] = m;');
136
- out.push(' __modules[id](__makeRequire(id), m, m.exports);');
137
- out.push(' return m.exports;');
138
- out.push(' }');
139
-
140
- out.push(' const __entry = __loadEntry();');
141
- out.push(' const __api = { reasonStream: __entry.reasonStream, reasonRdfJs: __entry.reasonRdfJs };');
142
- out.push('');
143
- out.push(
144
- ' try { if (__outerModule && __outerModule.exports) __outerModule.exports = __api; } catch (ignoredError) {}',
145
- );
146
- out.push(' try { if (__outerSelf) __outerSelf.eyeling = __api; } catch (ignoredError) {}');
147
- out.push('');
148
- out.push(' // ---- demo.html compatibility ----');
149
- out.push(' // The original monolithic eyeling.js exposed internal functions/flags as globals.');
150
- out.push(' // demo.html still uses these via importScripts(...) inside a web worker.');
151
- out.push(' try {');
152
- out.push(' if (__outerSelf && __entry) {');
153
- out.push(' if (typeof __entry.lex === "function") __outerSelf.lex = __entry.lex;');
154
- out.push(' if (typeof __entry.Parser === "function") __outerSelf.Parser = __entry.Parser;');
155
- out.push(' if (typeof __entry.forwardChain === "function") __outerSelf.forwardChain = __entry.forwardChain;');
156
- out.push(
157
- ' if (typeof __entry.materializeRdfLists === "function") __outerSelf.materializeRdfLists = __entry.materializeRdfLists;',
158
- );
159
- out.push(
160
- ' if (typeof __entry.isGroundTriple === "function") __outerSelf.isGroundTriple = __entry.isGroundTriple;',
161
- );
162
- out.push(
163
- ' if (typeof __entry.printExplanation === "function") __outerSelf.printExplanation = __entry.printExplanation;',
164
- );
165
- out.push(' if (typeof __entry.tripleToN3 === "function") __outerSelf.tripleToN3 = __entry.tripleToN3;');
166
- out.push('');
167
- out.push(' // Expose flags as mutable globals (with live linkage to engine module state).');
168
- out.push(' const def = (name, getFn, setFn) => {');
169
- out.push(' try {');
170
- out.push(' if (typeof Object.defineProperty === "function") {');
171
- out.push(' Object.defineProperty(__outerSelf, name, {');
172
- out.push(' configurable: true,');
173
- out.push(' get: (typeof getFn === "function") ? getFn : undefined,');
174
- out.push(' set: (typeof setFn === "function") ? setFn : undefined,');
175
- out.push(' });');
176
- out.push(' } else {');
177
- out.push(' // Fallback (no live linkage)');
178
- out.push(' if (typeof getFn === "function") __outerSelf[name] = getFn();');
179
- out.push(' }');
180
- out.push(' } catch (ignoredError) {}');
181
- out.push(' };');
182
- out.push('');
183
- out.push(' def("enforceHttpsEnabled", __entry.getEnforceHttpsEnabled, __entry.setEnforceHttpsEnabled);');
184
- out.push(' def("proofCommentsEnabled", __entry.getProofCommentsEnabled, __entry.setProofCommentsEnabled);');
185
- out.push(' def("__tracePrefixes", __entry.getTracePrefixes, __entry.setTracePrefixes);');
186
- out.push(' }');
187
- out.push(' } catch (ignoredError) {}');
188
- out.push('');
189
- out.push(' try {');
190
- out.push(
191
- ' if (__outerModule && __outerRequire && __outerRequire.main === __outerModule && typeof __entry.main === "function") {',
192
- );
193
- out.push(' __entry.main();');
194
- out.push(' }');
195
- out.push(' } catch (ignoredError) {}');
196
- out.push('})();');
197
-
198
- fs.writeFileSync(OUT, out.join('\n') + '\n', { encoding: 'utf8' });
199
- try {
200
- fs.chmodSync(OUT, 0o755);
201
- } catch (_) {}
202
-
203
- console.log(`Wrote ${path.relative(process.cwd(), OUT)} with ${keys.length} modules`);
63
+ function buildBundleSource({ autoRunMain }) {
64
+ addModule(ENTRY);
65
+ const keys = Array.from(modules.keys()).sort();
66
+
67
+ const out = [];
68
+ out.push("'use strict';");
69
+ out.push('');
70
+ out.push('(function(){');
71
+ out.push(' const __outerRequire = (typeof require === "function") ? require : null;');
72
+ out.push(' const __outerModule = (typeof module !== "undefined") ? module : null;');
73
+ out.push(' const __outerSelf = (typeof self !== "undefined") ? self : null;');
74
+ out.push(' const __modules = Object.create(null);');
75
+ out.push(' const __cache = Object.create(null);');
76
+ out.push('');
77
+ out.push(' // ---- bundled modules ----');
78
+ for (const k of keys) {
79
+ out.push(` __modules[${JSON.stringify(k)}] = function(require, module, exports){`);
80
+ out.push(modules.get(k).replace(/\r\n/g, '\n'));
81
+ out.push(' };');
82
+ }
83
+
84
+ out.push('');
85
+ out.push(' function __normPath(p){');
86
+ out.push(' const segs = [];');
87
+ out.push(' for (const part of p.split("/")) {');
88
+ out.push(' if (!part || part === ".") continue;');
89
+ out.push(' if (part === "..") segs.pop();');
90
+ out.push(' else segs.push(part);');
91
+ out.push(' }');
92
+ out.push(' return segs.join("/");');
93
+ out.push(' }');
94
+
95
+ out.push(' function __resolve(fromId, req){');
96
+ out.push(' if (!(req && (req.startsWith("./") || req.startsWith("../")))) return req;');
97
+ out.push(' const base = fromId.split("/").slice(0, -1).join("/");');
98
+ out.push(' let p = base ? (base + "/" + req) : req;');
99
+ out.push(' p = __normPath(p);');
100
+ out.push(' if (!p.endsWith(".js") && !p.endsWith(".json")) p += ".js";');
101
+ out.push(' return p;');
102
+ out.push(' }');
103
+
104
+ out.push(' function __makeRequire(fromId){');
105
+ out.push(' function r(req){');
106
+ out.push(' if (!(req && (req.startsWith("./") || req.startsWith("../")))) {');
107
+ out.push(' if (__outerRequire) return __outerRequire(req);');
108
+ out.push(' throw new Error("Cannot require external module: " + req);');
109
+ out.push(' }');
110
+ out.push(' const id = __resolve(fromId, req);');
111
+ out.push(' if (!__modules[id]) {');
112
+ out.push(' if (__outerRequire) return __outerRequire(req);');
113
+ out.push(' throw new Error("Cannot find bundled module: " + id);');
114
+ out.push(' }');
115
+ out.push(' if (__cache[id]) return __cache[id].exports;');
116
+ out.push(' const m = { exports: {} };');
117
+ out.push(' __cache[id] = m;');
118
+ out.push(' __modules[id](__makeRequire(id), m, m.exports);');
119
+ out.push(' return m.exports;');
120
+ out.push(' }');
121
+ out.push(' r.main = (__outerRequire && __outerRequire.main) ? __outerRequire.main : null;');
122
+ out.push(' return r;');
123
+ out.push(' }');
124
+
125
+ out.push('');
126
+ out.push(' function __loadEntry(){');
127
+ out.push(` const id = ${JSON.stringify(normalizeRel(ENTRY))};`);
128
+ out.push(' if (!__modules[id]) throw new Error("Missing entry module: " + id);');
129
+ out.push(' if (__cache[id]) return __cache[id].exports;');
130
+ out.push(' const m = { exports: {} };');
131
+ out.push(' __cache[id] = m;');
132
+ out.push(' __modules[id](__makeRequire(id), m, m.exports);');
133
+ out.push(' return m.exports;');
134
+ out.push(' }');
135
+
136
+ out.push(' const __entry = __loadEntry();');
137
+ out.push(' const __api = __entry;');
138
+ out.push('');
139
+ out.push(
140
+ ' try { if (__outerModule && __outerModule.exports) __outerModule.exports = __api; } catch (ignoredError) {}',
141
+ );
142
+ out.push(' try { if (__outerSelf) __outerSelf.eyeling = __api; } catch (ignoredError) {}');
143
+ out.push('');
144
+ out.push(' // ---- demo.html compatibility ----');
145
+ out.push(' // The original monolithic eyeling.js exposed internal functions/flags as globals.');
146
+ out.push(' // demo.html still uses these via importScripts(...) inside a web worker.');
147
+ out.push(' try {');
148
+ out.push(' if (__outerSelf && __entry) {');
149
+ out.push(' if (typeof __entry.lex === "function") __outerSelf.lex = __entry.lex;');
150
+ out.push(' if (typeof __entry.Parser === "function") __outerSelf.Parser = __entry.Parser;');
151
+ out.push(' if (typeof __entry.forwardChain === "function") __outerSelf.forwardChain = __entry.forwardChain;');
152
+ out.push(
153
+ ' if (typeof __entry.materializeRdfLists === "function") __outerSelf.materializeRdfLists = __entry.materializeRdfLists;',
154
+ );
155
+ out.push(
156
+ ' if (typeof __entry.isGroundTriple === "function") __outerSelf.isGroundTriple = __entry.isGroundTriple;',
157
+ );
158
+ out.push(
159
+ ' if (typeof __entry.printExplanation === "function") __outerSelf.printExplanation = __entry.printExplanation;',
160
+ );
161
+ out.push(' if (typeof __entry.tripleToN3 === "function") __outerSelf.tripleToN3 = __entry.tripleToN3;');
162
+ out.push(
163
+ ' if (typeof __entry.collectOutputStringsFromFacts === "function") __outerSelf.collectOutputStringsFromFacts = __entry.collectOutputStringsFromFacts;',
164
+ );
165
+ out.push(
166
+ ' if (typeof __entry.prettyPrintQueryTriples === "function") __outerSelf.prettyPrintQueryTriples = __entry.prettyPrintQueryTriples;',
167
+ );
168
+ out.push('');
169
+ out.push(' const def = (name, getFn, setFn) => {');
170
+ out.push(' try {');
171
+ out.push(' if (typeof Object.defineProperty === "function") {');
172
+ out.push(' Object.defineProperty(__outerSelf, name, {');
173
+ out.push(' configurable: true,');
174
+ out.push(' get: (typeof getFn === "function") ? getFn : undefined,');
175
+ out.push(' set: (typeof setFn === "function") ? setFn : undefined,');
176
+ out.push(' });');
177
+ out.push(' } else {');
178
+ out.push(' if (typeof getFn === "function") __outerSelf[name] = getFn();');
179
+ out.push(' }');
180
+ out.push(' } catch (ignoredError) {}');
181
+ out.push(' };');
182
+ out.push('');
183
+ out.push(' def("enforceHttpsEnabled", __entry.getEnforceHttpsEnabled, __entry.setEnforceHttpsEnabled);');
184
+ out.push(' def("proofCommentsEnabled", __entry.getProofCommentsEnabled, __entry.setProofCommentsEnabled);');
185
+ out.push(' def("__tracePrefixes", __entry.getTracePrefixes, __entry.setTracePrefixes);');
186
+ out.push(' }');
187
+ out.push(' } catch (ignoredError) {}');
188
+ out.push('');
189
+
190
+ if (autoRunMain) {
191
+ out.push(' try {');
192
+ out.push(
193
+ ' if (__outerModule && __outerRequire && __outerRequire.main === __outerModule && typeof __entry.main === "function") {',
194
+ );
195
+ out.push(' __entry.main();');
196
+ out.push(' }');
197
+ out.push(' } catch (ignoredError) {}');
198
+ }
199
+
200
+ out.push('})();');
201
+
202
+ return { source: out.join('\n') + '\n', moduleCount: keys.length };
203
+ }
204
+
205
+ function ensureDir(dir) {
206
+ fs.mkdirSync(dir, { recursive: true });
207
+ }
208
+
209
+ function writeFile(file, contents, mode) {
210
+ ensureDir(path.dirname(file));
211
+ fs.writeFileSync(file, contents, { encoding: 'utf8' });
212
+ if (mode != null) {
213
+ try {
214
+ fs.chmodSync(file, mode);
215
+ } catch (_) {}
216
+ }
217
+ }
218
+
219
+ function buildBrowserEntry() {
220
+ return `import './eyeling.browser.js';
221
+
222
+ function getBrowserApi() {
223
+ const api = typeof globalThis !== 'undefined' ? globalThis.eyeling : undefined;
224
+ if (!api) {
225
+ throw new Error(
226
+ 'Eyeling browser bundle is not initialized. Import "eyeling/browser" only in a browser or worker runtime.',
227
+ );
228
+ }
229
+ return api;
230
+ }
231
+
232
+ export function reasonStream(input, opts) {
233
+ return getBrowserApi().reasonStream(input, opts);
234
+ }
235
+
236
+ export function reasonRdfJs(input, opts) {
237
+ return getBrowserApi().reasonRdfJs(input, opts);
238
+ }
239
+
240
+ export function registerBuiltin(iri, handler) {
241
+ return getBrowserApi().registerBuiltin(iri, handler);
242
+ }
243
+
244
+ export function unregisterBuiltin(iri) {
245
+ return getBrowserApi().unregisterBuiltin(iri);
246
+ }
247
+
248
+ export function registerBuiltinModule(mod, origin) {
249
+ return getBrowserApi().registerBuiltinModule(mod, origin);
250
+ }
251
+
252
+ export function listBuiltinIris() {
253
+ return getBrowserApi().listBuiltinIris();
254
+ }
255
+
256
+ export function collectOutputStringsFromFacts(facts, prefixes) {
257
+ return getBrowserApi().collectOutputStringsFromFacts(facts, prefixes);
258
+ }
259
+
260
+ export function prettyPrintQueryTriples(triples, prefixes) {
261
+ return getBrowserApi().prettyPrintQueryTriples(triples, prefixes);
262
+ }
263
+
264
+ export const rdfjs = new Proxy(Object.create(null), {
265
+ get(_target, prop) {
266
+ return Reflect.get(getBrowserApi().rdfjs, prop);
267
+ },
268
+ set(_target, prop, value) {
269
+ return Reflect.set(getBrowserApi().rdfjs, prop, value);
270
+ },
271
+ has(_target, prop) {
272
+ return prop in getBrowserApi().rdfjs;
273
+ },
274
+ ownKeys() {
275
+ return Reflect.ownKeys(getBrowserApi().rdfjs);
276
+ },
277
+ getOwnPropertyDescriptor(_target, prop) {
278
+ return Object.getOwnPropertyDescriptor(getBrowserApi().rdfjs, prop);
279
+ },
280
+ });
281
+
282
+ const eyeling = {
283
+ get version() {
284
+ return getBrowserApi().version;
285
+ },
286
+ reasonStream,
287
+ reasonRdfJs,
288
+ rdfjs,
289
+ registerBuiltin,
290
+ unregisterBuiltin,
291
+ registerBuiltinModule,
292
+ listBuiltinIris,
293
+ collectOutputStringsFromFacts,
294
+ prettyPrintQueryTriples,
295
+ };
296
+
297
+ export default eyeling;
298
+ `;
299
+ }
300
+
301
+ function main() {
302
+ const nodeBundle = buildBundleSource({ autoRunMain: true });
303
+ const browserBundle = buildBundleSource({ autoRunMain: false });
304
+
305
+ writeFile(NODE_OUT, nodeBundle.source);
306
+ writeFile(BROWSER_BUNDLE_OUT, browserBundle.source);
307
+ writeFile(BROWSER_ENTRY_OUT, buildBrowserEntry());
308
+
309
+ console.log(`Wrote ${path.relative(process.cwd(), NODE_OUT)} with ${nodeBundle.moduleCount} modules`);
310
+ console.log(`Wrote ${path.relative(process.cwd(), BROWSER_BUNDLE_OUT)} with ${browserBundle.moduleCount} modules`);
311
+ console.log(`Wrote ${path.relative(process.cwd(), BROWSER_ENTRY_OUT)}`);
312
+ }
313
+
314
+ main();