n3 2.12.2 → 2.13.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/lib/N3Lexer.js CHANGED
@@ -135,6 +135,8 @@ class N3Lexer {
135
135
  for (const key in this) {
136
136
  if (!(key in lineModeRegExps) && this[key] instanceof RegExp) this[key] = invalidRegExp;
137
137
  }
138
+ // The only keyword in N-Triples and N-Quads is VERSION, which is case-sensitive
139
+ this._keyword = /VERSION(?=[\s#<])/y;
138
140
  }
139
141
  // When not in line mode, enable N3 functionality by default
140
142
  else {
package/lib/N3Writer.js CHANGED
@@ -41,6 +41,9 @@ const escape = /["\\\t\n\r\b\f\u0000-\u0019\ud800-\udbff]/,
41
41
  '\f': '\\f'
42
42
  };
43
43
 
44
+ // Characters that a version label cannot contain
45
+ const invalidVersionLabel = /["\\\u0000-\u001f\u007f]|[\ud800-\udbff](?![\udc00-\udfff])|(?:^|[^\ud800-\udbff])[\udc00-\udfff]/;
46
+
44
47
  // A local name (PN_LOCAL) as written in a prefixed name, without backslash escapes,
45
48
  // so that it denotes the same characters as the IRI it is taken from.
46
49
  // Characters outside the Basic Multilingual Plane are escaped before matching, so they never occur.
@@ -88,12 +91,17 @@ class N3Writer {
88
91
  this._endStream = options.end === undefined ? true : !!options.end;
89
92
  }
90
93
 
94
+ // A version label is written as-is, so it cannot contain characters that need escaping,
95
+ // nor unpaired surrogates (which cannot be encoded)
96
+ if (options.version && invalidVersionLabel.test(options.version)) throw new Error(`Invalid version label: ${JSON.stringify(options.version)}`);
97
+
91
98
  // Initialize writer, depending on the format
92
99
  this._subject = null;
93
100
  if (!/triple|quad/i.test(options.format)) {
94
101
  this._lineMode = false;
95
102
  this._escape = escape, this._escapeAll = escapeAll, this._characterReplacer = characterReplacer;
96
103
  this._graph = DEFAULTGRAPH;
104
+ if (options.version) this._write(`@version "${options.version}".\n`);
97
105
  this._prefixIRIs = Object.create(null);
98
106
  // Escaped prefix IRIs and names for the prefix matcher, computed once per prefix
99
107
  this._prefixPatterns = Object.create(null);
@@ -108,6 +116,7 @@ class N3Writer {
108
116
  // N-Triples and N-Quads are written in their canonical form
109
117
  this._escape = canonicalEscape, this._escapeAll = canonicalEscapeAll;
110
118
  this._characterReplacer = canonicalCharacterReplacer;
119
+ if (options.version) this._write(`VERSION "${options.version}"\n`);
111
120
  }
112
121
  }
113
122
 
@@ -123,6 +132,15 @@ class N3Writer {
123
132
  this._outputStream.write(string, 'utf8', callback);
124
133
  }
125
134
 
135
+ // ### `_endStatement` finishes a pending statement and closes an open graph block
136
+ _endStatement() {
137
+ if (this._subject !== null) {
138
+ this._write(this._inDefaultGraph ? '.\n' : '\n}\n');
139
+ this._subject = null;
140
+ this._graph = DEFAULTGRAPH;
141
+ }
142
+ }
143
+
126
144
  // ### `_writeQuad` writes the quad to the output stream
127
145
  _writeQuad(subject, predicate, object, graph, done) {
128
146
  try {
@@ -325,10 +343,7 @@ class N3Writer {
325
343
  if (typeof iri !== 'string') iri = iri.value;
326
344
  hasPrefixes = true;
327
345
  // Finish a possible pending quad
328
- if (this._subject !== null) {
329
- this._write(this._inDefaultGraph ? '.\n' : '\n}\n');
330
- this._subject = null, this._graph = '';
331
- }
346
+ this._endStatement();
332
347
  // Store and write the prefix
333
348
  this._prefixIRIs[iri] = prefix += ':';
334
349
  this._prefixPatterns[iri] = [(0, _Util.escapeRegex)(iri), (0, _Util.escapeRegex)(prefix)];
@@ -407,10 +422,7 @@ class N3Writer {
407
422
  // ### `end` signals the end of the output stream
408
423
  end(done) {
409
424
  // Finish a possible pending quad
410
- if (this._subject !== null) {
411
- this._write(this._inDefaultGraph ? '.\n' : '\n}\n');
412
- this._subject = null;
413
- }
425
+ this._endStatement();
414
426
  // Disallow further writing
415
427
  this._write = this._blockedWrite;
416
428
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "n3",
3
- "version": "2.12.2",
3
+ "version": "2.13.0",
4
4
  "description": "Lightning fast, asynchronous, streaming Turtle / N3 / RDF library.",
5
5
  "author": "Ruben Verborgh <ruben.verborgh@gmail.com>",
6
6
  "keywords": [
package/src/N3Lexer.js CHANGED
@@ -101,6 +101,8 @@ export default class N3Lexer {
101
101
  if (!(key in lineModeRegExps) && this[key] instanceof RegExp)
102
102
  this[key] = invalidRegExp;
103
103
  }
104
+ // The only keyword in N-Triples and N-Quads is VERSION, which is case-sensitive
105
+ this._keyword = /VERSION(?=[\s#<])/y;
104
106
  }
105
107
  // When not in line mode, enable N3 functionality by default
106
108
  else {
package/src/N3Writer.js CHANGED
@@ -24,6 +24,9 @@ const escape = /["\\\t\n\r\b\f\u0000-\u0019\ud800-\udbff]/,
24
24
  '\n': '\\n', '\r': '\\r', '\b': '\\b', '\f': '\\f',
25
25
  };
26
26
 
27
+ // Characters that a version label cannot contain
28
+ const invalidVersionLabel = /["\\\u0000-\u001f\u007f]|[\ud800-\udbff](?![\udc00-\udfff])|(?:^|[^\ud800-\udbff])[\udc00-\udfff]/;
29
+
27
30
  // A local name (PN_LOCAL) as written in a prefixed name, without backslash escapes,
28
31
  // so that it denotes the same characters as the IRI it is taken from.
29
32
  // Characters outside the Basic Multilingual Plane are escaped before matching, so they never occur.
@@ -70,12 +73,19 @@ export default class N3Writer {
70
73
  this._endStream = options.end === undefined ? true : !!options.end;
71
74
  }
72
75
 
76
+ // A version label is written as-is, so it cannot contain characters that need escaping,
77
+ // nor unpaired surrogates (which cannot be encoded)
78
+ if (options.version && invalidVersionLabel.test(options.version))
79
+ throw new Error(`Invalid version label: ${JSON.stringify(options.version)}`);
80
+
73
81
  // Initialize writer, depending on the format
74
82
  this._subject = null;
75
83
  if (!(/triple|quad/i).test(options.format)) {
76
84
  this._lineMode = false;
77
85
  this._escape = escape, this._escapeAll = escapeAll, this._characterReplacer = characterReplacer;
78
86
  this._graph = DEFAULTGRAPH;
87
+ if (options.version)
88
+ this._write(`@version "${options.version}".\n`);
79
89
  this._prefixIRIs = Object.create(null);
80
90
  // Escaped prefix IRIs and names for the prefix matcher, computed once per prefix
81
91
  this._prefixPatterns = Object.create(null);
@@ -92,6 +102,8 @@ export default class N3Writer {
92
102
  // N-Triples and N-Quads are written in their canonical form
93
103
  this._escape = canonicalEscape, this._escapeAll = canonicalEscapeAll;
94
104
  this._characterReplacer = canonicalCharacterReplacer;
105
+ if (options.version)
106
+ this._write(`VERSION "${options.version}"\n`);
95
107
  }
96
108
  }
97
109
 
@@ -107,6 +119,15 @@ export default class N3Writer {
107
119
  this._outputStream.write(string, 'utf8', callback);
108
120
  }
109
121
 
122
+ // ### `_endStatement` finishes a pending statement and closes an open graph block
123
+ _endStatement() {
124
+ if (this._subject !== null) {
125
+ this._write(this._inDefaultGraph ? '.\n' : '\n}\n');
126
+ this._subject = null;
127
+ this._graph = DEFAULTGRAPH;
128
+ }
129
+ }
130
+
110
131
  // ### `_writeQuad` writes the quad to the output stream
111
132
  _writeQuad(subject, predicate, object, graph, done) {
112
133
  try {
@@ -340,10 +361,7 @@ export default class N3Writer {
340
361
  iri = iri.value;
341
362
  hasPrefixes = true;
342
363
  // Finish a possible pending quad
343
- if (this._subject !== null) {
344
- this._write(this._inDefaultGraph ? '.\n' : '\n}\n');
345
- this._subject = null, this._graph = '';
346
- }
364
+ this._endStatement();
347
365
  // Store and write the prefix
348
366
  this._prefixIRIs[iri] = (prefix += ':');
349
367
  this._prefixPatterns[iri] = [escapeRegex(iri), escapeRegex(prefix)];
@@ -426,10 +444,7 @@ export default class N3Writer {
426
444
  // ### `end` signals the end of the output stream
427
445
  end(done) {
428
446
  // Finish a possible pending quad
429
- if (this._subject !== null) {
430
- this._write(this._inDefaultGraph ? '.\n' : '\n}\n');
431
- this._subject = null;
432
- }
447
+ this._endStatement();
433
448
  // Disallow further writing
434
449
  this._write = this._blockedWrite;
435
450