npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rdfjs/express-handler

v2.0.3

Published

Handle incoming and outgoing RDF data in Express

Downloads

6,026

Readme

@rdfjs/express-handler

build status npm version

The @rdfjs/express-handler middleware provides methods to parse incoming RDF data from request with content like POST or PUT requests. It also provides methods to serialize outgoing RDF data.

Usage

The package returns a factory function to create express middlewares.

Factory

Adding it to all routes of the app would look like this:

import express from 'express'
import rdfHandler from '@rdfjs/express-handler'

const app = express()

app.use(rdfHandler())

The factory accepts the following options:

  • factory: The factory used to create Dataset instances. Default: require('rdf-ext')
  • formats: An object with parsers and serializers, each given as @rdfjs/sink-map. Default: require('@rdfjs/formats-common')
  • defaultMediaType: If an unknown Content-Type is given, this media type will be used. Default: undefined
  • baseIriFromRequest: If true, will call absolute-url to get the requested IRI and pass as base IRI to the parser. It can also be a function async (req) => string which can be used to compute the base IRI for the parser.
  • sendTriples: If true, the RDF/JS Quads sent using res.dataset() or res.quadStream() are converted to triples (default graph). Default undefined.

Request

Routes following the RDF Handler can access the parsed Quads from the request as a Stream or Dataset. The req.dataset() and req.quadStream() methods can be used for this. But the methods are only attached if the request contains content in a type supported by one of the parsers. The logic to access the Quads could look like this:

app.use((req, res) => {
  if (!req.dataset) {
    return res.status(406).end() // send 406 not acceptable if the content can't be parsed
  }

  const dataset = await req.dataset()
})

req.dataset() requires await as it's an async method. req.quadStream() is a sync method. Both methods accept an options object, which is forwarded to the parser.

Response

The RDF Handler middleware always attaches the res.dataset() and res.quadStream() methods. The methods must be called with the Dataset or Stream, which should be sent. A second options object can be given, which will be forwarded to the serializer. Sending a Dataset would look like this:

app.use((req, res) => {
  await res.dataset(dataset)
})

Both methods are async and finished once the response was sent.

Attaching

If you don't know if @rdfjs/express-handler is used earlier in the application, it's possible to attach it dynamically. That is useful inside of a middleware where you want to use an application RDF Handler instance and it's options, but fallback to a local instance if there is no RDF Handler earlier in the routes. The .attach() function can be used. await must be used, as it's an async function.

app.use((req, res) => {
  await rdfHandler.attach(req, res)

  if (req.dataset) {
    const dataset = await req.dataset()
  }

  await res.dataset(rdf.dataset())
})