1
1
const concat = require ( 'concat-stream' )
2
2
const jsonld = require ( 'jsonld' )
3
- const rdf = require ( 'rdf-data-model' )
4
3
const Readable = require ( 'readable-stream' )
4
+ const rdf = require ( '@rdfjs/data-model' )
5
+ const RdfTerms = require ( 'rdf-terms' )
5
6
6
7
class ParserStream extends Readable {
7
8
constructor ( input , options ) {
@@ -38,20 +39,21 @@ class ParserStream extends Readable {
38
39
} )
39
40
}
40
41
41
- term ( options ) {
42
- if ( options . type === 'IRI' ) {
43
- return this . factory . namedNode ( options . value )
42
+ /**
43
+ * Function rewrite with term.termType.
44
+ * This is done to fix issue #243, see https://github.com/digitalbazaar/jsonld.js/issues/243 .
45
+ * **/
46
+ term ( term ) {
47
+ switch ( term . termType ) {
48
+ case 'NamedNode' :
49
+ return this . factory . namedNode ( term . value )
50
+ case 'BlankNode' :
51
+ return this . factory . blankNode ( term . value . substr ( 2 ) ) // Remove the '_:' prefix. see https://github.com/digitalbazaar/jsonld.js/issues/244
52
+ case 'Literal' :
53
+ return this . factory . literal ( term . value , term . language || term . datatype )
54
+ case 'DefaultGraph' :
55
+ return this . factory . defaultGraph ( )
44
56
}
45
-
46
- if ( options . type === 'blank node' ) {
47
- if ( ! ( options . value in this . blankNodes ) ) {
48
- this . blankNodes [ options . value ] = this . factory . blankNode ( )
49
- }
50
-
51
- return this . blankNodes [ options . value ]
52
- }
53
-
54
- return this . factory . literal ( options . value , options . language || options . datatype )
55
57
}
56
58
57
59
parse ( data ) {
@@ -72,17 +74,11 @@ class ParserStream extends Readable {
72
74
73
75
return jsonld . promises ( ) . toRDF ( json , toRdfOptions )
74
76
} ) . then ( ( rawGraph ) => {
75
- Object . keys ( rawGraph ) . forEach ( ( graphIri ) => {
76
- const graph = graphIri !== '@default' ? this . factory . namedNode ( graphIri ) : null
77
-
78
- rawGraph [ graphIri ] . forEach ( ( triple ) => {
79
- this . push ( this . factory . quad (
80
- this . term ( triple . subject ) ,
81
- this . term ( triple . predicate ) ,
82
- this . term ( triple . object ) ,
83
- graph ) )
84
- } )
85
- } )
77
+ for ( let index in rawGraph ) {
78
+ this . push ( RdfTerms . mapTerms ( rawGraph [ index ] , ( value ) => this . term ( value ) , this . factory ) )
79
+ }
80
+ } ) . catch ( ( error ) => {
81
+ this . emit ( 'error' , error )
86
82
} )
87
83
}
88
84
0 commit comments