Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@rdfjs/serializer-jsonld-ext

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@rdfjs/serializer-jsonld-ext - npm Package Compare versions

Comparing version 3.0.0 to 4.0.0

.github/workflows/test.yaml

6

index.js

@@ -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"
}
}
*/
```

@@ -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)
})
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc