@zazuko/formats-lazy
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

@zazuko/formats-lazy

GitHub Workflow Status npm

This module bundles parser and serializer sinks for the most common RDF formats. Instances of SinkMap are used to handle different media types.

Every sink is loaded lazily when it is first used, making this package the best choice for web application. The laziness is transparent to the consuming code because streams are already lazy by definition.

Usage

Recommended usage

Install @rdfjs/environment and create an environment with the factories you need, including FormatsFactory. Then, import the lazy formats.

import Environment from '@rdfjs/environment/Environment.js'
import FormatsFactory from '@rdfjs/environment/Formats.js'
import lazyFormats from '@zazuko/formats-lazy'

export const $rdf = new Environment([FormatsFactory])

$rdf.formats.import(lazyFormats)

Simple usage

Use an existing environment, such as rdf-ext

import $rdf from 'rdf-ext'
import lazyFormats from '@zazuko/formats-lazy'

$rdf.formats.import(lazyFormats)

Direct usage

The formats object has a parsers and serializers property. Each of it is an instance of SinkMap with the most common RDF media types as key.

import formats from '@zazuko/formats-lazy'
import { Readable } from 'stream'

const input = Readable.from([`
  PREFIX s: <http://schema.org/>

  [] a s:Person;
    s:jobTitle "Professor";
    s:name "Jane Doe";
    s:telephone "(425) 123-4567";
    s:url <http://www.janedoe.com>.
`])

const output = formats.parsers.import('text/turtle', input)

output.on('data', quad => {
  console.log(`quad: ${quad.subject.value} - ${quad.predicate.value} - ${quad.object.value}`)
})

output.on('prefix', (prefix, ns) => {
  console.log(`prefix: ${prefix} ${ns.value}`)
})

Making a sink lazy

Any parser or serializer can be wrapped to be lazy loaded.

Assuming you have a @example/bin-parser package which export a class BinaryParser

import formats from '@zazuko/formats-lazy'
import { lazySink } from '@zazuko/formats-lazy/LazySink.js'

export const LazyBinaryParser = lazySink(
  async () => (await import('@example/bin-parser')).BinaryParser
)

formats.parsers.set('application/octet-stream+rdf', new LazyBinaryParser())

The constructed class LazyBinaryParser can be used just like any other parser and acts as a wrapper to BinaryParser. In TypeScript, it will preserve the constructor and import signatures. If for any reason the real parser instance needs to be accessed, call async load() method.

Package Sidebar

Install

npm i @zazuko/formats-lazy

Weekly Downloads

341

Version

1.0.1

License

MIT

Unpacked Size

11.3 kB

Total Files

10

Last publish

Collaborators

  • zazuko-bot
  • tpluscode
  • ludovicm67
  • ktk