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

@ldhop/core

v0.0.1-alpha.16

Published

Follow your nose through linked data resources - core

Downloads

41

Readme

@ldhop/core

LDhop - Follow your nose through Linked Data graph. This is the core engine package.

Usage

npm install @ldhop/core --save
yarn add @ldhop/core
import { QueryAndStore, RdfQuery, fetchRdfDocument, run } from '@ldhop/core'
import { foaf, rdfs } from 'rdf-namespaces'

// specify the steps of the query
// in this case fetch the whole foaf social network
// and also look in extended profile documents
const friendOfAFriendQuery: RdfQuery = [
  {
    type: 'match',
    subject: '?person',
    predicate: foaf.knows,
    pick: 'object',
    target: '?person',
  },
  {
    type: 'match',
    subject: '?person',
    predicate: rdfs.seeAlso,
    pick: 'object',
    target: '?extendedProfile',
  },
  {
    type: 'add resources',
    variable: '?extendedProfile',
  },
]

// specify starting points
const initialVariables = { person: new Set([webId]) }

// initialize the walk
const qas = new QueryAndStore(friendOfAFriendQuery, initialVariables)

// now, you need to ask for missing resources, fetch them, add them to QueryAndStore
// and keep doing that until there are no missing resources left
// you can use a simple helper provided by this library
await run(qas, fetch)
// or implement your own walk, using the following methods repeatedly:
const [missingResource] = qas.getMissingResources()
if (missingResource) {
  const { data } = await fetchRdfDocument(missingResource, fetch)
  qas.addResource(missingResource, data)
}

// now, you have the whole RDF graph collected in qas.store, which is n3.Store
// each triple has a graph element that corresponds to the url of the document of that triple
const store = qas.store

// you can show specific variable
qas.getVariable('person')
// or all variables
qas.getAllVariables()

Query

Query is an array of instructions to follow in order to discover and fetch desired Linked data.

It proceeds lazily - only requests next documents when they're needed for next steps, or if explicitly instructed.

The following steps are supported:

// step through the graph
type Match = {
  type: 'match'
  // optional constraints, either URI, or variable starting with ?
  subject?: string
  predicate?: string
  object?: string
  graph?: string
  // which of the quad components to assign to the target variable?
  pick: 'subject' | 'predicate' | 'object' | 'graph'
  // variable that results will be assigned to, starting with ?
  target: `?${string}`
}
// fetch documents behind variable, even if it isn't needed for next steps
type AddResources = {
  type: 'add resources'
  variable: `?${string}` // variable to fetch
}
// change variable, for example get container of a resource
type TransformVariable = {
  type: 'transform variable'
  source: `?${string}`
  target: `?${string}`
  // function to transform
  transform: (uri: Term) => Term | undefined
}
// edit the whole store in place
// this is a dangerous operation, because you can ruin the inner consistency of QueryAndStore
// don't use this unless you really really have to
type TransformStore = (qas: QueryAndStore) => void

Example query: Fetch Solid WebId Profile

See Solid WebID Profile specification for context.

import type { RdfQuery } from '@ldhop/core'

// find person and their profile documents
const webIdProfileQuery: RdfQuery = [
  // find and fetch preferences file
  {
    type: 'match',
    subject: '?person',
    predicate: pim.preferencesFile,
    pick: 'object',
    target: '?preferencesFile',
  },
  { type: 'add resources', variable: '?preferencesFile' },
  // find extended profile documents
  {
    type: 'match',
    subject: '?person',
    predicate: rdfs.seeAlso,
    pick: 'object',
    target: '?profileDocument',
  },
  // fetch the extended profile documents
  { type: 'add resources', variable: '?profileDocument' },
  // find public type index
  {
    type: 'match',
    subject: '?person',
    predicate: solid.publicTypeIndex,
    pick: 'object',
    target: '?publicTypeIndex',
  },
  // find private type index
  {
    type: 'match',
    subject: '?person',
    predicate: solid.privateTypeIndex,
    pick: 'object',
    target: '?privateTypeIndex',
  },
  // find inbox
  {
    type: 'match',
    subject: '?person',
    predicate: ldp.inbox,
    pick: 'object',
    target: '?inbox',
  },
]

The query corresponds to the following picture. The resources identified by the URIs within the variables in bold circles are fetched while it is executed.

webId profile query visualized

API

QueryAndStore

creating an instance

const qas = new QueryAndStore(query, startingPoints, (store = new n3.Store()))

properties

  • store: n3.Store - store containing RDF graph
  • query: RdfQuery - ldhop query

methods

  • getMissingResources() - returns list of resources that still need to be fetched and added
  • addResource(uri: string, quads: n3.Quad[], status: 'success' | 'error' = 'success') - add resource after it has been fetched - you can use fetchRdfDocument function provided by this library to receive correct input for this function
  • removeResource(uri: string) - delete resource from store
  • getVariable(variableName: string) - get list of RDF nodes belonging to this variable
  • getAllVariables() - get dictionary of lists of all discovered variables
  • getResources(status?: 'missing', 'added', 'failed') - get list of resources with specific status, or all resources regardless of their status

fetchRdfDocument

Fetch turtle document and parse it to ldhop-compatible quads

const { data } = await fetchRdfDocument(uri, fetch)
qas.addResource(uri, data)

run

Execute the ldhop query until the walk through the graph is finished.

It runs simple loop that continues as long as qas.getMissingResources() returns something: It fetches one missing resource, adds it with qas.addResource() and repeats. You can probably implement something safer or more efficient yourself, e.g. fetch missing resources in parallel.

You can provide custom (e.g. authenticated) fetch.

const qas = new QueryAndStore(query, initialVariables)
await run(qas, fetch)