@wazootech/worlds-cloudflare 0.15.6 → 0.15.7

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 (3) hide show
  1. package/README.md +11 -0
  2. package/dist/index.js +103 -0
  3. package/package.json +4 -3
package/README.md CHANGED
@@ -264,6 +264,17 @@ const { results } = await sdk.provenance({ subject });
264
264
  - **Deletion.** `purgeWorld` removes a world's records. A replace import keeps
265
265
  them.
266
266
 
267
+ ## Patterns in SPARQL
268
+
269
+ `REGEX` and `REPLACE` take patterns written like JavaScript's regular expressions.
270
+ They run on [`re2js`](https://www.npmjs.com/package/re2js), so the time a match
271
+ takes follows the length of the text.
272
+
273
+ - Look-ahead, look-behind, back-references and very large patterns are not
274
+ supported. A query that uses one fails with an error that names the pattern.
275
+ - `\s` matches a space, tab or line break, and not a non-breaking space. An emoji
276
+ counts as one character.
277
+
267
278
  ## Install
268
279
 
269
280
  ```sh
package/dist/index.js CHANGED
@@ -4,6 +4,7 @@ import { a as createD1SearchIndexRebuilder, c as emptyVectorSyncReport, d as rec
4
4
  import { a as loadAssertedQuads, i as InferenceSizeGuardError, n as D1ProvenanceStore, o as planInference, t as D1QuadStore } from "./quad-store-DX7Nqyjw.js";
5
5
  import { a as DEFAULT_D1_MAX_BATCH_WEIGHT, o as d1StatementWeight } from "./project-search-chunks-options-qI9m5FWz.js";
6
6
  import { WazooSparqlEngine } from "@wazootech/sparql-engine";
7
+ import { RE2JS } from "re2js";
7
8
  import { i as D1QuadStream, n as assertD1SchemaCompatible, r as checkD1SchemaCompatibility, t as D1RdfjsStore } from "./rdfjs-store-B3w5noFV.js";
8
9
  import { n as validateWorldId, t as D1SchemaBuilder } from "./d1-schema-builder-DmrQfhKJ.js";
9
10
  import { g as DEFAULT_D1_MAX_WRITE_BATCH_SIZE, h as D1BatchExecutor, m as quadToInsertRow, s as buildDeleteAllDerivedQuads, t as buildBulkInsertQuads, u as buildWipeAllGraphDataStatements } from "./d1-quad-insert-sql-B4ZiZZe8.js";
@@ -163,6 +164,106 @@ var D1ConnectionDriver = class {
163
164
  await this.database.exec(sql);
164
165
  }
165
166
  };
167
+ // src/cloudflare/patterns/linear-pattern.ts
168
+ /** LinearPattern matches in time that grows with the length of the text only. */
169
+ var LinearPattern = class {
170
+ compiled;
171
+ constructor(compiled) {
172
+ this.compiled = compiled;
173
+ }
174
+ test(text) {
175
+ return this.compiled.matcher(text).find();
176
+ }
177
+ replace(text, replacement) {
178
+ const match = this.compiled.matcher(text);
179
+ let result = "";
180
+ let end = 0;
181
+ while (match.find()) {
182
+ result += text.slice(end, match.start());
183
+ result += expandReplacement(replacement, match);
184
+ end = match.end();
185
+ }
186
+ return result + text.slice(end);
187
+ }
188
+ };
189
+ /**
190
+ * expandReplacement fills in a replacement as JavaScript does: `$&` is the match,
191
+ * `$1` to `$99` are its groups, and `$$` is a dollar sign. A number without a
192
+ * group stays as it is written.
193
+ */
194
+ function expandReplacement(replacement, match) {
195
+ return replacement.replace(/\$(?:([$&])|(\d\d?))/g, (written, symbol, digits = "") => {
196
+ if (symbol === "$") return "$";
197
+ if (symbol === "&") return match.group() ?? "";
198
+ for (const length of [2, 1]) {
199
+ const index = Number(digits.slice(0, length));
200
+ if (index > 0 && index <= match.groupCount()) return (match.group(index) ?? "") + digits.slice(length);
201
+ }
202
+ return written;
203
+ });
204
+ }
205
+ // src/cloudflare/patterns/create-pattern-compiler.ts
206
+ /** MAX_KEPT_PATTERNS is how many compiled patterns a compiler keeps. */
207
+ const MAX_KEPT_PATTERNS = 100;
208
+ /** MAX_PATTERN_LENGTH is how many characters a pattern may have. */
209
+ const MAX_PATTERN_LENGTH = 4e3;
210
+ /**
211
+ * MAX_PATTERN_STEPS is how large a compiled pattern may be. Matching takes up to
212
+ * this many steps for each character of the text, and `a{500}` alone has 500.
213
+ */
214
+ const MAX_PATTERN_STEPS = 1e3;
215
+ const FLAGS = {
216
+ i: RE2JS.CASE_INSENSITIVE,
217
+ m: RE2JS.MULTILINE,
218
+ s: RE2JS.DOTALL,
219
+ d: 0,
220
+ g: 0,
221
+ u: 0
222
+ };
223
+ /**
224
+ * createPatternCompiler prepares the patterns of REGEX and REPLACE for a service
225
+ * that runs other people's queries. They are written like JavaScript's regular
226
+ * expressions, without look-ahead, look-behind and back-references. A query with a
227
+ * pattern that isn't supported fails with an error that says so, and a malformed
228
+ * pattern is an evaluation error, as it is by default.
229
+ */
230
+ function createPatternCompiler() {
231
+ const kept = /* @__PURE__ */ new Map();
232
+ return (pattern, flags) => {
233
+ const key = `${flags}/${pattern}`;
234
+ if (kept.has(key)) return kept.get(key);
235
+ const compiled = compile(pattern, flags);
236
+ if (kept.size >= MAX_KEPT_PATTERNS) kept.clear();
237
+ kept.set(key, compiled);
238
+ return compiled;
239
+ };
240
+ }
241
+ function compile(pattern, flags) {
242
+ if (pattern.length > MAX_PATTERN_LENGTH) throw unsupported(pattern, "too long");
243
+ try {
244
+ new RegExp(pattern, flags);
245
+ } catch {
246
+ return;
247
+ }
248
+ let options = 0;
249
+ for (const flag of flags) {
250
+ const option = FLAGS[flag];
251
+ if (option === void 0) throw unsupported(pattern, `flag "${flag}"`);
252
+ options |= option;
253
+ }
254
+ let compiled;
255
+ try {
256
+ compiled = RE2JS.compile(RE2JS.translateRegExp(pattern), options);
257
+ } catch (error) {
258
+ throw unsupported(pattern, (error instanceof Error ? error.message : String(error)).replace("error parsing regexp: ", ""));
259
+ }
260
+ if (compiled.programSize() > MAX_PATTERN_STEPS) throw unsupported(pattern, "too complex");
261
+ return new LinearPattern(compiled);
262
+ }
263
+ function unsupported(pattern, reason) {
264
+ const start = pattern.length > 50 ? `${pattern.slice(0, 50)}…` : pattern;
265
+ return /* @__PURE__ */ new Error(`The pattern "${start}" is not supported: ${reason.slice(0, 100)}`);
266
+ }
166
267
  // src/cloudflare/provenance/provenance-sparql-engine.ts
167
268
  /**
168
269
  * ProvenanceSparqlEngine commits each SPARQL update with its request's writer
@@ -172,6 +273,7 @@ var D1ConnectionDriver = class {
172
273
  */
173
274
  var ProvenanceSparqlEngine = class {
174
275
  options;
276
+ compilePattern = createPatternCompiler();
175
277
  shared;
176
278
  constructor(options) {
177
279
  this.options = options;
@@ -185,6 +287,7 @@ var ProvenanceSparqlEngine = class {
185
287
  const { store, quadStore } = this.options;
186
288
  return new WazooSparqlEngine({
187
289
  store,
290
+ compilePattern: this.compilePattern,
188
291
  createTransaction: () => quadStore.createTransaction(provenance && { provenance })
189
292
  });
190
293
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wazootech/worlds-cloudflare",
3
- "version": "0.15.6",
3
+ "version": "0.15.7",
4
4
  "description": "Store a Worlds knowledge graph in Cloudflare D1, with keyword search, optional Vectorize search, SPARQL and provenance. Made for Cloudflare Workers.",
5
5
  "keywords": [
6
6
  "wazoo",
@@ -71,9 +71,10 @@
71
71
  "@langchain/textsplitters": "^1.0.1",
72
72
  "@rdfjs/types": "^2.0.1",
73
73
  "@wazootech/reasoner": "^0.4.4",
74
- "@wazootech/sparql-engine": "^0.5.8",
74
+ "@wazootech/sparql-engine": "^0.5.11",
75
75
  "@wazootech/worlds-sdk": "^0.11.3",
76
- "@wazootech/worlds-sqlite": "^0.8.4"
76
+ "@wazootech/worlds-sqlite": "^0.8.4",
77
+ "re2js": "2.10.0"
77
78
  },
78
79
  "devDependencies": {
79
80
  "@types/node": "24.19.2",