@rdfjs/serializer-jsonld-ext
Advanced tools
Comparing version 3.0.0 to 4.0.0
@@ -1,3 +0,3 @@ | ||
const Sink = require('@rdfjs/sink') | ||
const SerializerStream = require('./lib/SerializerStream') | ||
import Sink from '@rdfjs/sink' | ||
import SerializerStream from './lib/SerializerStream.js' | ||
@@ -10,2 +10,2 @@ class Serializer extends Sink { | ||
module.exports = Serializer | ||
export default Serializer |
@@ -1,13 +0,15 @@ | ||
const jsonld = require('jsonld') | ||
const { Readable } = require('readable-stream') | ||
const streamConcat = require('./streamConcat') | ||
import jsonld from 'jsonld' | ||
import { Readable } from 'readable-stream' | ||
import chunks from 'stream-chunks/chunks.js' | ||
class SerializerStream extends Readable { | ||
constructor (input, { | ||
baseIRI, | ||
compact, | ||
context = {}, | ||
compact, | ||
encoding = 'object', | ||
flatten, | ||
frame, | ||
skipContext, | ||
encoding = 'object' | ||
prettyPrint, | ||
skipContext | ||
} = {}) { | ||
@@ -19,9 +21,14 @@ super({ | ||
this.compact = compact | ||
this.context = context | ||
this.compact = compact | ||
this.encoding = encoding | ||
this.flatten = flatten | ||
this.frame = frame | ||
this.prettyPrint = prettyPrint | ||
this.skipContext = skipContext | ||
this.encoding = encoding | ||
if (baseIRI) { | ||
this.context['@base'] = baseIRI.value || baseIRI.toString() | ||
} | ||
input.on('prefix', (prefix, namespace) => { | ||
@@ -38,3 +45,3 @@ if (!this.context[prefix]) { | ||
try { | ||
const quadArray = (await streamConcat(input)).map(SerializerStream.toJsonldQuad) | ||
const quadArray = (await chunks(input)).map(SerializerStream.toJsonldQuad) | ||
const rawJsonld = await jsonld.fromRDF(quadArray) | ||
@@ -68,3 +75,7 @@ const transformedJsonld = await this.transform(rawJsonld, this.options) | ||
if (this.encoding === 'string') { | ||
return JSON.stringify(data) | ||
if (this.prettyPrint) { | ||
return JSON.stringify(data, null, 2) | ||
} else { | ||
return JSON.stringify(data) | ||
} | ||
} | ||
@@ -96,2 +107,2 @@ | ||
module.exports = SerializerStream | ||
export default SerializerStream |
{ | ||
"name": "@rdfjs/serializer-jsonld-ext", | ||
"version": "3.0.0", | ||
"description": "JSON-LD serializer that implements the RDFJS Sink Interfaces and supports different output styles", | ||
"version": "4.0.0", | ||
"description": "JSON-LD serializer that implements the RDF/JS Sink interface and supports different output styles", | ||
"type": "module", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "stricter-standard && mocha" | ||
"test": "stricter-standard && c8 --reporter=lcov --reporter=text mocha" | ||
}, | ||
@@ -26,13 +27,14 @@ "repository": { | ||
"dependencies": { | ||
"@rdfjs/sink": "^1.0.3", | ||
"concat-stream": "^2.0.0", | ||
"jsonld": "^5.2.0", | ||
"readable-stream": "^3.6.0" | ||
"@rdfjs/sink": "^2.0.0", | ||
"jsonld": "^8.1.0", | ||
"readable-stream": "^4.3.0", | ||
"stream-chunks": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"@rdfjs/data-model": "^1.3.4", | ||
"@rdfjs/namespace": "^1.1.0", | ||
"mocha": "^9.1.3", | ||
"@rdfjs/data-model": "^2.0.1", | ||
"@rdfjs/namespace": "^2.0.0", | ||
"c8": "^7.13.0", | ||
"mocha": "^10.2.0", | ||
"stricter-standard": "^0.2.0" | ||
} | ||
} |
# @rdfjs/serializer-jsonld-ext | ||
[![Build Status](https://travis-ci.org/rdfjs/serializer-jsonld-ext.svg?branch=master)](https://travis-ci.org/rdfjs/serializer-jsonld-ext) | ||
[![build status](https://img.shields.io/github/actions/workflow/status/rdfjs-base/serializer-jsonld-ext/test.yaml?branch=master)](https://github.com/rdfjs-base/serializer-jsonld-ext/actions/workflows/test.yaml) | ||
[![npm version](https://img.shields.io/npm/v/@rdfjs/serializer-jsonld-ext.svg)](https://www.npmjs.com/package/@rdfjs/serializer-jsonld-ext) | ||
JSON-LD serializer that implements the [RDFJS Sink interface]((https://github.com/rdfjs/representation-task-force/)) and supports different output styles. | ||
JSON-LD serializer that implements the [RDF/JS Sink interface](http://rdf.js.org/stream-spec/#sink-interface) and supports different output styles. | ||
This package handles the stream processing and uses [jsonld.js](https://github.com/digitalbazaar/jsonld.js) for the actual serialization process. | ||
@@ -13,3 +13,3 @@ | ||
The package exports the serializer as a class, so an instance must be created before it can be used. | ||
The `.import` method, as defined in the [RDFJS specification](http://rdf.js.org/#sink-interface), must be called to do the actual serialization. | ||
The `.import` method, as defined in the [RDF/JS specification](http://rdf.js.org/#sink-interface), must be called to do the actual serialization. | ||
It expects a quad stream as argument. | ||
@@ -20,2 +20,3 @@ The method will return a stream which emits the JSON-LD as a plain object or string. | ||
- `baseIRI`: A base IRI given as `NamedNode`, `URL`, or string object. | ||
- `context`: JSON-LD context that will be used for compact, flatten or frame. | ||
@@ -32,2 +33,3 @@ The context must be given as a plain object. | ||
By default `object` is used. | ||
- `prettyPrint`: Enables pretty printing for string encoding. | ||
@@ -40,16 +42,22 @@ It's also possible to pass options as second argument to the `.import` method. | ||
This example shows how to create a serializer instance and how to feed it with a stream of quads. | ||
The output will be processed with `compact` using the given context. | ||
The object emitted by the serializer will be written to the console. | ||
The output will be processed with `compact` using the given context and baseIRI. | ||
The string emitted by the serializer will be written to the console. | ||
```javascript | ||
const rdf = require('@rdfjs/data-model') | ||
const Readable = require('stream').Readable | ||
const SerializerJsonld = require('@rdfjs/serializer-jsonld-ext') | ||
import rdf from '@rdfjs/data-model' | ||
import { Readable } from 'readable-stream' | ||
import SerializerJsonld from '@rdfjs/serializer-jsonld-ext' | ||
const context = { | ||
'@vocab': 'http://schema.org/', | ||
ex: 'http://example.org/' | ||
'@vocab': 'http://schema.org/' | ||
} | ||
const serializerJsonld = new SerializerJsonld({ context, compact: true }) | ||
const serializerJsonld = new SerializerJsonld({ | ||
baseIRI: 'http://example.org/', | ||
context, | ||
compact: true, | ||
encoding: 'string', | ||
prettyPrint: true | ||
}) | ||
const input = new Readable({ | ||
@@ -73,2 +81,3 @@ objectMode: true, | ||
}) | ||
const output = serializerJsonld.import(input) | ||
@@ -80,10 +89,17 @@ | ||
// output: | ||
// { | ||
// '@context': { '@vocab': 'http://schema.org/', ex: 'http://example.org/' }, | ||
// '@id': 'ex:sheldon-cooper', | ||
// familyName: 'Cooper', | ||
// givenName: 'Sheldon', | ||
// knows: { '@id': 'ex:amy-farrah-fowler' } | ||
// } | ||
/* output: | ||
{ | ||
"@context": { | ||
"@vocab": "http://schema.org/", | ||
"@base": "http://example.org/" | ||
}, | ||
"@id": "sheldon-cooper", | ||
"familyName": "Cooper", | ||
"givenName": "Sheldon", | ||
"knows": { | ||
"@id": "amy-farrah-fowler" | ||
} | ||
} | ||
*/ | ||
``` |
142
test/test.js
@@ -1,9 +0,10 @@ | ||
const assert = require('assert') | ||
const rdf = require('@rdfjs/data-model') | ||
const namespace = require('@rdfjs/namespace') | ||
const sinkTest = require('@rdfjs/sink/test') | ||
const { describe, it } = require('mocha') | ||
const JsonldSerializer = require('..') | ||
const streamConcat = require('../lib/streamConcat') | ||
const quadsToReadable = require('./support/quadsToReadable') | ||
import { deepStrictEqual, strictEqual } from 'assert' | ||
import rdf from '@rdfjs/data-model' | ||
import namespace from '@rdfjs/namespace' | ||
import sinkTest from '@rdfjs/sink/test/index.js' | ||
import { describe, it } from 'mocha' | ||
import { Readable } from 'readable-stream' | ||
import chunks from 'stream-chunks/chunks.js' | ||
import decode from 'stream-chunks/decode.js' | ||
import JsonldSerializer from '../index.js' | ||
@@ -21,3 +22,3 @@ const ns = { | ||
const jsonldString = JSON.stringify([{ | ||
const expected = JSON.stringify([{ | ||
'@id': 'http://example.org/subject', | ||
@@ -29,10 +30,28 @@ 'http://example.org/predicate': [{ | ||
const input = quadsToReadable([quad]) | ||
const input = Readable.from([quad]) | ||
const serializer = new JsonldSerializer({ encoding: 'string' }) | ||
const stream = serializer.import(input) | ||
const result = (await streamConcat(stream)) | ||
const result = await decode(stream) | ||
assert.strictEqual(result, jsonldString) | ||
strictEqual(result, expected) | ||
}) | ||
it('should support pretty-print string output', async () => { | ||
const quad = rdf.quad(ns.ex.subject, ns.ex.predicate, rdf.literal('object1')) | ||
const expected = JSON.stringify([{ | ||
'@id': 'http://example.org/subject', | ||
'http://example.org/predicate': [{ | ||
'@value': 'object1' | ||
}] | ||
}], null, 2) | ||
const input = Readable.from([quad]) | ||
const serializer = new JsonldSerializer({ encoding: 'string', prettyPrint: true }) | ||
const stream = serializer.import(input) | ||
const result = await decode(stream) | ||
strictEqual(result, expected) | ||
}) | ||
it('should support compact', async () => { | ||
@@ -42,3 +61,3 @@ const quad = rdf.quad(ns.ex.subject, ns.ex.predicate, rdf.literal('object1')) | ||
const jsonld = { | ||
const expected = { | ||
'@context': context, | ||
@@ -49,8 +68,8 @@ '@id': 'ex:subject', | ||
const input = quadsToReadable([quad]) | ||
const input = Readable.from([quad]) | ||
const serializer = new JsonldSerializer({ compact: true, context }) | ||
const stream = serializer.import(input) | ||
const result = (await streamConcat(stream))[0] | ||
const result = (await chunks(stream))[0] | ||
assert.deepStrictEqual(result, jsonld) | ||
deepStrictEqual(result, expected) | ||
}) | ||
@@ -75,3 +94,3 @@ | ||
const jsonld = { | ||
const expected = { | ||
'@context': { | ||
@@ -87,8 +106,8 @@ '@vocab': 'http://example.org/' | ||
const input = quadsToReadable(quads) | ||
const input = Readable.from(quads) | ||
const serializer = new JsonldSerializer({ frame: true, context }) | ||
const stream = serializer.import(input) | ||
const result = (await streamConcat(stream))[0] | ||
const result = (await chunks(stream))[0] | ||
assert.deepStrictEqual(result, jsonld) | ||
deepStrictEqual(result, expected) | ||
}) | ||
@@ -110,3 +129,3 @@ | ||
const jsonld = { | ||
const expected = { | ||
'@type': 'Thing', | ||
@@ -116,18 +135,77 @@ property0: 'value0' | ||
const input = quadsToReadable(quads) | ||
const input = Readable.from(quads) | ||
const serializer = new JsonldSerializer({ frame: true, context, skipContext: true }) | ||
const stream = serializer.import(input) | ||
const result = (await streamConcat(stream))[0] | ||
const result = (await chunks(stream))[0] | ||
assert.deepStrictEqual(result, jsonld) | ||
deepStrictEqual(result, expected) | ||
}) | ||
it('should support prefixes', async () => { | ||
it('should handle baseIRI given as NamedNode', async () => { | ||
const quad = rdf.quad(ns.ex.subject, ns.ex.predicate, rdf.literal('object1')) | ||
const context = { | ||
ex: 'http://example.org/' | ||
const expected = { | ||
'@context': { | ||
'@base': 'http://example.org/' | ||
}, | ||
'@id': 'subject', | ||
'http://example.org/predicate': 'object1' | ||
} | ||
const jsonld = { | ||
'@context': context, | ||
const input = Readable.from([quad]) | ||
const serializer = new JsonldSerializer({ baseIRI: ns.ex(''), compact: true }) | ||
const stream = serializer.import(input) | ||
const result = (await chunks(stream))[0] | ||
deepStrictEqual(result, expected) | ||
}) | ||
it('should handle baseIRI given as URL', async () => { | ||
const quad = rdf.quad(ns.ex.subject, ns.ex.predicate, rdf.literal('object1')) | ||
const expected = { | ||
'@context': { | ||
'@base': 'http://example.org/' | ||
}, | ||
'@id': 'subject', | ||
'http://example.org/predicate': 'object1' | ||
} | ||
const input = Readable.from([quad]) | ||
const serializer = new JsonldSerializer({ baseIRI: new URL(ns.ex('').value), compact: true }) | ||
const stream = serializer.import(input) | ||
const result = (await chunks(stream))[0] | ||
deepStrictEqual(result, expected) | ||
}) | ||
it('should handle baseIRI given as string', async () => { | ||
const quad = rdf.quad(ns.ex.subject, ns.ex.predicate, rdf.literal('object1')) | ||
const expected = { | ||
'@context': { | ||
'@base': 'http://example.org/' | ||
}, | ||
'@id': 'subject', | ||
'http://example.org/predicate': 'object1' | ||
} | ||
const input = Readable.from([quad]) | ||
const serializer = new JsonldSerializer({ baseIRI: ns.ex('').value, compact: true }) | ||
const stream = serializer.import(input) | ||
const result = (await chunks(stream))[0] | ||
deepStrictEqual(result, expected) | ||
}) | ||
it('should support prefixes', async () => { | ||
const quad = rdf.quad(ns.ex.subject, ns.ex.predicate, rdf.literal('object1')) | ||
const expected = { | ||
'@context': { | ||
ex: 'http://example.org/' | ||
}, | ||
'@id': 'ex:subject', | ||
@@ -137,3 +215,3 @@ 'ex:predicate': 'object1' | ||
const input = quadsToReadable([quad]) | ||
const input = Readable.from([quad]) | ||
const serializer = new JsonldSerializer({ compact: true }) | ||
@@ -144,6 +222,6 @@ const stream = serializer.import(input) | ||
const result = (await streamConcat(stream))[0] | ||
const result = (await chunks(stream))[0] | ||
assert.deepStrictEqual(result, jsonld) | ||
deepStrictEqual(result, expected) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15129
267
100
Yes
5
7
+ Addedstream-chunks@^1.0.0
+ Added@digitalbazaar/http-client@3.4.1(transitive)
+ Added@fastify/busboy@2.1.1(transitive)
+ Added@rdfjs/sink@2.0.1(transitive)
+ Addedbase64-js@1.5.1(transitive)
+ Addedbuffer@6.0.3(transitive)
+ Addeddata-uri-to-buffer@4.0.1(transitive)
+ Addedevents@3.3.0(transitive)
+ Addedfetch-blob@3.2.0(transitive)
+ Addedformdata-polyfill@4.0.10(transitive)
+ Addedieee754@1.2.1(transitive)
+ Addedjsonld@8.3.3(transitive)
+ Addedky@0.33.3(transitive)
+ Addedky-universal@0.11.0(transitive)
+ Addednode-domexception@1.0.0(transitive)
+ Addednode-fetch@3.3.2(transitive)
+ Addedprocess@0.11.10(transitive)
+ Addedreadable-stream@4.7.0(transitive)
+ Addedstream-chunks@1.0.0(transitive)
+ Addedundici@5.28.4(transitive)
+ Addedweb-streams-polyfill@3.3.3(transitive)
- Removedconcat-stream@^2.0.0
- Removed@digitalbazaar/http-client@1.2.0(transitive)
- Removed@rdfjs/sink@1.0.3(transitive)
- Removedbuffer-from@1.1.2(transitive)
- Removedconcat-stream@2.0.0(transitive)
- Removeddata-uri-to-buffer@3.0.1(transitive)
- Removedesm@3.2.25(transitive)
- Removedfetch-blob@2.1.2(transitive)
- Removedinherits@2.0.4(transitive)
- Removedjsonld@5.2.0(transitive)
- Removedky@0.25.1(transitive)
- Removedky-universal@0.8.2(transitive)
- Removednode-fetch@3.0.0-beta.9(transitive)
- Removedreadable-stream@3.6.2(transitive)
- Removedtypedarray@0.0.6(transitive)
- Removedutil-deprecate@1.0.2(transitive)
Updated@rdfjs/sink@^2.0.0
Updatedjsonld@^8.1.0
Updatedreadable-stream@^4.3.0