n3 2.11.1 → 2.11.3

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/lib/N3Parser.js CHANGED
@@ -65,6 +65,9 @@ class N3Parser {
65
65
  // Disable parsing of unsupported versions by default
66
66
  this._parseUnsupportedVersions = !!options.parseUnsupportedVersions;
67
67
  this._version = options.version;
68
+ // Maximum nesting depth of triple terms (default 1024; Infinity disables the limit)
69
+ const maxTripleTermDepth = options.maxTripleTermDepth;
70
+ this._maxTripleTermDepth = maxTripleTermDepth === Infinity || Number.isInteger(maxTripleTermDepth) && maxTripleTermDepth >= 0 ? maxTripleTermDepth : 1024;
68
71
  }
69
72
 
70
73
  // ## Static class methods
@@ -96,6 +99,19 @@ class N3Parser {
96
99
  }
97
100
  }
98
101
 
102
+ // ### `_enterTripleTerm` stores the current parsing context when entering a triple term,
103
+ // failing if triple terms nest deeper than the maximum depth
104
+ _enterTripleTerm(type, subject, predicate, token) {
105
+ if (this._tripleTermDepth >= this._maxTripleTermDepth) {
106
+ this._error(`Triple terms nested deeper than ${this._maxTripleTermDepth} level${this._maxTripleTermDepth === 1 ? '' : 's'}`, token);
107
+ return false;
108
+ }
109
+ this._tripleTermDepth++;
110
+ this._saveContext(type, this._graph, subject, predicate, null);
111
+ this._graph = null;
112
+ return true;
113
+ }
114
+
99
115
  // ### `_saveContext` stores the current parsing context
100
116
  // when entering a new scope (list, blank node, formula)
101
117
  _saveContext(type, graph, subject, predicate, object) {
@@ -338,13 +354,9 @@ class N3Parser {
338
354
  }
339
355
  case '<<(':
340
356
  if (!this._n3Mode) return this._error('Disallowed triple term as subject', token);
341
- this._saveContext('<<(', this._graph, null, null, null);
342
- this._graph = null;
343
- return this._readSubject;
357
+ return this._enterTripleTerm('<<(', null, null, token) && this._readSubject;
344
358
  case '<<':
345
- this._saveContext('<<', this._graph, null, null, null);
346
- this._graph = null;
347
- return this._readSubject;
359
+ return this._enterTripleTerm('<<', null, null, token) && this._readSubject;
348
360
  default:
349
361
  // Read the subject entity
350
362
  if ((this._subject = this._readEntity(token)) === undefined) return;
@@ -467,13 +479,9 @@ class N3Parser {
467
479
  this._saveContext('formula', this._graph, this._subject, this._predicate, this._graph = this._factory.blankNode());
468
480
  return this._readInFormulaContext;
469
481
  case '<<(':
470
- this._saveContext('<<(', this._graph, this._subject, this._predicate, null);
471
- this._graph = null;
472
- return this._readSubject;
482
+ return this._enterTripleTerm('<<(', this._subject, this._predicate, token) && this._readSubject;
473
483
  case '<<':
474
- this._saveContext('<<', this._graph, this._subject, this._predicate, null);
475
- this._graph = null;
476
- return this._readSubject;
484
+ return this._enterTripleTerm('<<', this._subject, this._predicate, token) && this._readSubject;
477
485
  default:
478
486
  // Read the object entity
479
487
  if ((this._object = this._readEntity(token)) === undefined) return;
@@ -668,13 +676,8 @@ class N3Parser {
668
676
  this._subject = null;
669
677
  return this._readInFormulaContext;
670
678
  case '<<(':
671
- this._saveContext('<<(', this._graph, null, null, null);
672
- this._graph = null;
673
- next = this._readSubject;
674
- break;
675
679
  case '<<':
676
- this._saveContext('<<', this._graph, null, null, null);
677
- this._graph = null;
680
+ if (!this._enterTripleTerm(token.type, null, null, token)) return;
678
681
  next = this._readSubject;
679
682
  break;
680
683
  default:
@@ -1166,6 +1169,7 @@ class N3Parser {
1166
1169
  // Read the quad and restore the previous context
1167
1170
  const quad = this._createQuad(this._subject, this._predicate, this._object, this._graph, this._inversePredicate);
1168
1171
  this._restoreContext('<<(', token);
1172
+ this._tripleTermDepth--;
1169
1173
 
1170
1174
  // If we're in a list, continue processing that list
1171
1175
  const stack = this._contextStack,
@@ -1201,6 +1205,7 @@ class N3Parser {
1201
1205
  this._tripleTerm = null;
1202
1206
  const reifier = this._readTripleTerm();
1203
1207
  this._restoreContext('<<', token);
1208
+ this._tripleTermDepth--;
1204
1209
 
1205
1210
  // // If we're in a list, continue processing that list
1206
1211
  const stack = this._contextStack,
@@ -1532,6 +1537,7 @@ class N3Parser {
1532
1537
  this._prefixChanges = null;
1533
1538
  this._quantifiedChanges = null;
1534
1539
  this._emptyFormula = false;
1540
+ this._tripleTermDepth = 0;
1535
1541
  let readToken = token => {
1536
1542
  return this._readCallback = this._readCallback(token);
1537
1543
  };
package/lib/N3Writer.js CHANGED
@@ -77,6 +77,8 @@ class N3Writer {
77
77
  this._lineMode = false;
78
78
  this._graph = DEFAULTGRAPH;
79
79
  this._prefixIRIs = Object.create(null);
80
+ // Escaped prefix IRIs and names for the prefix matcher, computed once per prefix
81
+ this._prefixPatterns = Object.create(null);
80
82
  if (options.baseIRI) {
81
83
  this._baseIri = new _BaseIRI.default(options.baseIRI);
82
84
  if (options.writeBase) this._write(`@base <${options.baseIRI}>.\n`);
@@ -302,6 +304,7 @@ class N3Writer {
302
304
  }
303
305
  // Store and write the prefix
304
306
  this._prefixIRIs[iri] = prefix += ':';
307
+ this._prefixPatterns[iri] = [(0, _Util.escapeRegex)(iri), (0, _Util.escapeRegex)(prefix)];
305
308
  this._write(`@prefix ${prefix} <${iri}>.\n`);
306
309
  }
307
310
  // Recreate the prefix matcher
@@ -309,12 +312,12 @@ class N3Writer {
309
312
  this._hasPrefixes = true;
310
313
  let IRIlist = '',
311
314
  prefixList = '';
312
- for (const prefixIRI in this._prefixIRIs) {
313
- IRIlist += IRIlist ? `|${prefixIRI}` : prefixIRI;
314
- prefixList += (prefixList ? '|' : '') + this._prefixIRIs[prefixIRI];
315
+ for (const prefixIRI in this._prefixPatterns) {
316
+ const [IRIpattern, prefixPattern] = this._prefixPatterns[prefixIRI];
317
+ IRIlist += IRIlist ? `|${IRIpattern}` : IRIpattern;
318
+ prefixList += prefixList ? `|${prefixPattern}` : prefixPattern;
315
319
  }
316
- IRIlist = (0, _Util.escapeRegex)(IRIlist, /[\]\/\(\)\*\+\?\.\\\$]/g, '\\$&');
317
- this._prefixRegex = new RegExp(`^(?:${prefixList})[^\/]*$|` + `^(${IRIlist})([_a-zA-Z0-9](?:\\.?[\\-_a-zA-Z0-9])*)$`);
320
+ this._prefixRegex = new RegExp(`^(?:${prefixList})[^/]*$|` + `^(${IRIlist})([_a-zA-Z0-9](?:\\.?[\\-_a-zA-Z0-9])*)$`);
318
321
  }
319
322
  // End a prefix block with a newline
320
323
  this._write(hasPrefixes ? '\n' : '', done);
package/lib/Util.js CHANGED
@@ -5,5 +5,5 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.escapeRegex = escapeRegex;
7
7
  function escapeRegex(regex) {
8
- return regex.replace(/[\]\/\(\)\*\+\?\.\\\$]/g, '\\$&');
8
+ return regex.replace(/[.*+?^${}()|[\]\\/-]/g, '\\$&');
9
9
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "n3",
3
- "version": "2.11.1",
3
+ "version": "2.11.3",
4
4
  "description": "Lightning fast, asynchronous, streaming Turtle / N3 / RDF library.",
5
5
  "author": "Ruben Verborgh <ruben.verborgh@gmail.com>",
6
6
  "keywords": [
@@ -60,44 +60,17 @@
60
60
  "test": "jest",
61
61
  "lint": "eslint src perf test spec",
62
62
  "prepare": "npm run build",
63
- "spec": "npm run spec-1-1 && npm run spec-1-2 && npm run spec-n3",
64
- "spec-1-1": "npm run spec-1-1-turtle && npm run spec-1-1-ntriples && npm run spec-1-1-nquads && npm run spec-1-1-trig",
63
+ "spec": "node spec/run.js",
65
64
  "spec-1-1-earl": "npm run spec-1-1-earl-turtle && npm run spec-1-1-earl-ntriples && npm run spec-1-1-earl-nquads && npm run spec-1-1-earl-trig",
66
- "spec-1-1-ntriples": "rdf-test-suite spec/parser.js https://w3c.github.io/rdf-tests/rdf/rdf11/rdf-n-triples/manifest.ttl -i '{ \"format\": \"n-triples\" }' -c .rdf-test-suite-cache/",
67
- "spec-1-1-nquads": "rdf-test-suite spec/parser.js https://w3c.github.io/rdf-tests/rdf/rdf11/rdf-n-quads/manifest.ttl -i '{ \"format\": \"n-quads\" }' -c .rdf-test-suite-cache/",
68
- "spec-1-1-turtle": "rdf-test-suite spec/parser.js https://w3c.github.io/rdf-tests/rdf/rdf11/rdf-turtle/manifest.ttl -i '{ \"format\": \"turtle\" }' -c .rdf-test-suite-cache/ --skip \"(bad-numeric-escape|bareword_decimal)\"",
69
- "spec-1-1-trig": "rdf-test-suite spec/parser.js https://w3c.github.io/rdf-tests/rdf/rdf11/rdf-trig/manifest.ttl -i '{ \"format\": \"trig\" }' -c .rdf-test-suite-cache/ --skip \"(bad-numeric-escape|IRI-resolution-01)\"",
70
65
  "spec-1-1-earl-ntriples": "rdf-test-suite spec/parser.js https://w3c.github.io/rdf-tests/rdf/rdf11/rdf-n-triples/manifest.ttl -i '{ \"format\": \"n-triples\" }' -c .rdf-test-suite-cache/ -o earl -p spec/earl-meta.json > spec/earl-ntriples.ttl",
71
66
  "spec-1-1-earl-nquads": "rdf-test-suite spec/parser.js https://w3c.github.io/rdf-tests/rdf/rdf11/rdf-n-quads/manifest.ttl -i '{ \"format\": \"n-quads\" }' -c .rdf-test-suite-cache/ -o earl -p spec/earl-meta.json > spec/earl-nquads.ttl",
72
67
  "spec-1-1-earl-turtle": "rdf-test-suite spec/parser.js https://w3c.github.io/rdf-tests/rdf/rdf11/rdf-turtle/manifest.ttl -i '{ \"format\": \"turtle\" }' -c .rdf-test-suite-cache/ -o earl -p spec/earl-meta.json > spec/earl-turtle.ttl",
73
68
  "spec-1-1-earl-trig": "rdf-test-suite spec/parser.js https://w3c.github.io/rdf-tests/rdf/rdf11/rdf-trig/manifest.ttl -i '{ \"format\": \"trig\" }' -c .rdf-test-suite-cache/ -o earl -p spec/earl-meta.json > spec/earl-trig.ttl",
74
- "spec-1-2": "npm run spec-1-2-turtle && npm run spec-1-2-ntriples && npm run spec-1-2-nquads && npm run spec-1-2-trig && npm run spec-1-2-turtle-eval && npm run spec-1-2-trig-eval",
75
69
  "spec-1-2-earl": "npm run spec-1-2-earl-turtle && npm run spec-1-2-earl-ntriples && npm run spec-1-2-earl-nquads && npm run spec-1-2-earl-trig",
76
- "spec-1-2-ntriples": "rdf-test-suite spec/parser.js https://w3c.github.io/rdf-tests/rdf/rdf12/rdf-n-triples/syntax/manifest.ttl -i '{ \"format\": \"n-triples\" }' -c .rdf-test-suite-cache/",
77
- "spec-1-2-nquads": "rdf-test-suite spec/parser.js https://w3c.github.io/rdf-tests/rdf/rdf12/rdf-n-quads/syntax/manifest.ttl -i '{ \"format\": \"n-quads\" }' -c .rdf-test-suite-cache/",
78
- "spec-1-2-turtle": "rdf-test-suite spec/parser.js https://w3c.github.io/rdf-tests/rdf/rdf12/rdf-turtle/syntax/manifest.ttl -i '{ \"format\": \"turtle\" }' -c .rdf-test-suite-cache/",
79
- "spec-1-2-trig": "rdf-test-suite spec/parser.js https://w3c.github.io/rdf-tests/rdf/rdf12/rdf-trig/syntax/manifest.ttl -i '{ \"format\": \"trig\" }' -c .rdf-test-suite-cache/",
80
- "spec-1-2-turtle-eval": "rdf-test-suite spec/parser.js https://w3c.github.io/rdf-tests/rdf/rdf12/rdf-turtle/eval/manifest.ttl -i '{ \"format\": \"turtle\" }' -c .rdf-test-suite-cache/",
81
- "spec-1-2-trig-eval": "rdf-test-suite spec/parser.js https://w3c.github.io/rdf-tests/rdf/rdf12/rdf-trig/eval/manifest.ttl -i '{ \"format\": \"trig\" }' -c .rdf-test-suite-cache/",
82
70
  "spec-1-2-earl-ntriples": "rdf-test-suite spec/parser.js https://w3c.github.io/rdf-tests/rdf/rdf12/rdf-n-triples/syntax/manifest.ttl -i '{ \"format\": \"n-triples\" }' -c .rdf-test-suite-cache/ -o earl -p spec/earl-meta.json > spec/earl-ntriples.ttl",
83
71
  "spec-1-2-earl-nquads": "rdf-test-suite spec/parser.js https://w3c.github.io/rdf-tests/rdf/rdf12/rdf-n-quads/syntax/manifest.ttl -i '{ \"format\": \"n-quads\" }' -c .rdf-test-suite-cache/ -o earl -p spec/earl-meta.json > spec/earl-nquads.ttl",
84
72
  "spec-1-2-earl-turtle": "rdf-test-suite spec/parser.js https://w3c.github.io/rdf-tests/rdf/rdf12/rdf-turtle/syntax/manifest.ttl -i '{ \"format\": \"turtle\" }' -c .rdf-test-suite-cache/ -o earl -p spec/earl-meta.json > spec/earl-turtle.ttl",
85
73
  "spec-1-2-earl-trig": "rdf-test-suite spec/parser.js https://w3c.github.io/rdf-tests/rdf/rdf12/rdf-trig/syntax/manifest.ttl -i '{ \"format\": \"trig\" }' -c .rdf-test-suite-cache/ -o earl -p spec/earl-meta.json > spec/earl-trig.ttl",
86
- "spec-writer": "npm run spec-writer-1-1-turtle && npm run spec-writer-1-1-ntriples && npm run spec-writer-1-1-nquads && npm run spec-writer-1-1-trig && npm run spec-writer-1-2-turtle && npm run spec-writer-1-2-ntriples && npm run spec-writer-1-2-nquads && npm run spec-writer-1-2-trig && npm run spec-writer-1-2-turtle-eval && npm run spec-writer-1-2-trig-eval",
87
- "spec-writer-1-1-turtle": "rdf-test-suite spec/writer.js https://w3c.github.io/rdf-tests/rdf/rdf11/rdf-turtle/manifest.ttl -i '{ \"format\": \"turtle\" }' -c .rdf-test-suite-cache/ --skip \"(bad-numeric-escape|bareword_decimal)\"",
88
- "spec-writer-1-1-ntriples": "rdf-test-suite spec/writer.js https://w3c.github.io/rdf-tests/rdf/rdf11/rdf-n-triples/manifest.ttl -i '{ \"format\": \"n-triples\" }' -c .rdf-test-suite-cache/",
89
- "spec-writer-1-1-nquads": "rdf-test-suite spec/writer.js https://w3c.github.io/rdf-tests/rdf/rdf11/rdf-n-quads/manifest.ttl -i '{ \"format\": \"n-quads\" }' -c .rdf-test-suite-cache/",
90
- "spec-writer-1-1-trig": "rdf-test-suite spec/writer.js https://w3c.github.io/rdf-tests/rdf/rdf11/rdf-trig/manifest.ttl -i '{ \"format\": \"trig\" }' -c .rdf-test-suite-cache/ --skip \"(bad-numeric-escape|IRI-resolution-01)\"",
91
- "spec-writer-1-2-turtle": "rdf-test-suite spec/writer.js https://w3c.github.io/rdf-tests/rdf/rdf12/rdf-turtle/syntax/manifest.ttl -i '{ \"format\": \"turtle\" }' -c .rdf-test-suite-cache/",
92
- "spec-writer-1-2-ntriples": "rdf-test-suite spec/writer.js https://w3c.github.io/rdf-tests/rdf/rdf12/rdf-n-triples/syntax/manifest.ttl -i '{ \"format\": \"n-triples\" }' -c .rdf-test-suite-cache/",
93
- "spec-writer-1-2-nquads": "rdf-test-suite spec/writer.js https://w3c.github.io/rdf-tests/rdf/rdf12/rdf-n-quads/syntax/manifest.ttl -i '{ \"format\": \"n-quads\" }' -c .rdf-test-suite-cache/",
94
- "spec-writer-1-2-trig": "rdf-test-suite spec/writer.js https://w3c.github.io/rdf-tests/rdf/rdf12/rdf-trig/syntax/manifest.ttl -i '{ \"format\": \"trig\" }' -c .rdf-test-suite-cache/",
95
- "spec-writer-1-2-turtle-eval": "rdf-test-suite spec/writer.js https://w3c.github.io/rdf-tests/rdf/rdf12/rdf-turtle/eval/manifest.ttl -i '{ \"format\": \"turtle\" }' -c .rdf-test-suite-cache/",
96
- "spec-writer-1-2-trig-eval": "rdf-test-suite spec/writer.js https://w3c.github.io/rdf-tests/rdf/rdf12/rdf-trig/eval/manifest.ttl -i '{ \"format\": \"trig\" }' -c .rdf-test-suite-cache/",
97
- "spec-n3": "npm run spec-n3-parser && npm run spec-n3-extended",
98
- "spec-n3-fetch": "sh scripts/fetch-n3-tests.sh",
99
- "spec-n3-parser": "npm run spec-n3-fetch && rdf-test-suite spec/parser.js https://w3c-cg.github.io/N3/tests/N3Tests/manifest-parser.ttl -i '{ \"format\": \"text/n3\" }' -m 'https://w3c-cg.github.io/N3/tests/~.n3-tests/tests/'",
100
- "spec-n3-extended": "npm run spec-n3-fetch && rdf-test-suite spec/parser.js https://w3c-cg.github.io/N3/tests/N3Tests/manifest-extended.ttl -i '{ \"format\": \"text/n3\" }' -m 'https://w3c-cg.github.io/N3/tests/~.n3-tests/tests/'",
101
74
  "spec-clean": "rm -rf .rdf-test-suite-cache/ .n3-tests/",
102
75
  "docs": "cd src && docco *.js -o ../docs && cd .."
103
76
  },