Socket
Socket
Sign inDemoInstall

jsonld-document-loader

Package Overview
Dependencies
Maintainers
6
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsonld-document-loader - npm Package Compare versions

Comparing version 1.1.0 to 1.2.0

6

CHANGELOG.md
# jsonld-document-loader
## 1.2.0 - 2021-04-28
### Added
- Adds `setProtocolHandler()` method (and a did-specific alias for it,
`setDidResolver()`).
## 1.1.0 - 2020-11-19

@@ -4,0 +10,0 @@

2

lib/index.js
/*!
* Copyright (c) 2019-2020 Digital Bazaar, Inc. All rights reserved.
* Copyright (c) 2019-2021 Digital Bazaar, Inc. All rights reserved.
*/

@@ -4,0 +4,0 @@ 'use strict';

/*!
* Copyright (c) 2019-2020 Digital Bazaar, Inc. All rights reserved.
* Copyright (c) 2019-2021 Digital Bazaar, Inc. All rights reserved.
*/

@@ -9,2 +9,3 @@ 'use strict';

this.documents = new Map();
this.protocolHandlers = new Map();
}

@@ -22,4 +23,48 @@

/**
* Adds a custom protocol handler to the loader.
*
* @example
* // Using did-io's CachedResolver instance as a did: protocol handler
* jld.addProtocolHandler({protocol: 'did', handler: cachedResolver});
*
* @param {string} protocol - Protocol id, such as 'did', 'https', etc.
* @param {{get: Function}} handler - Protocol handler object, exposes an
* async `get({url})` method.
*/
setProtocolHandler({protocol, handler} = {}) {
this.protocolHandlers.set(protocol, handler);
}
/**
* Sets a DID resolver (either a method-specific resolver to support just a
* single DID method, or a general driver harness to support multiple DID
* methods) as the 'did' protocol handler.
*
* @param {object} didResolver - A did-io style did method driver, such
* as did-method-key and did-veres-one. (Exposes a `.method` property,
* and a `.get()` function.)
*/
setDidResolver(didResolver) {
this.setProtocolHandler({protocol: 'did', handler: didResolver});
}
async documentLoader(url) {
const document = this.documents.get(url);
if(!_isString(url)) {
throw new TypeError('The "url" parameter must be a string.');
}
let document;
// First, check in the static document list
document = this.documents.get(url);
if(!document) {
const [protocol] = url.split(':');
// Check if this protocol has a custom handler installed
if(this.protocolHandlers.has(protocol)) {
document = await this.protocolHandlers.get(protocol)
.get({url});
}
}
if(document) {

@@ -35,2 +80,18 @@ return {

}
/**
* Returns the self-contained `documentLoader` function for passing to various
* json-ld libraries.
*
* @example
* import {JsonLdDocumentLoader} from 'jsonld-document-loader';
* const jdl = new JsonLdDocumentLoader();
* jdl.addSatic(MY_URL, MY_CONTEXT);
* const doucmentLoader = jdl.build();
*
* @returns {Function}
*/
build() {
return this.documentLoader.bind(this);
}
}

@@ -37,0 +98,0 @@

{
"name": "jsonld-document-loader",
"version": "1.1.0",
"version": "1.2.0",
"description": "A document loader API for jsonld.js.",

@@ -35,3 +35,5 @@ "main": "lib",

},
"dependencies": {},
"devDependencies": {
"@digitalbazaar/did-io": "^1.0.0",
"chai": "^4.2.0",

@@ -53,4 +55,4 @@ "cross-env": "^7.0.2",

"engines": {
"node": ">=8.3.0"
"node": ">=12"
}
}

@@ -1,1 +0,154 @@

# jsonld-document-loader
# JSON-LD Document Loader _(jsonld-document-loader)_
[![Build Status](https://img.shields.io/github/workflow/status/digitalbazaar/jsonld-document-loader/Node.js%20CI)](https://github.com/digitalbazaar/jsonld-document-loader/actions?query=workflow%3A%22Node.js+CI%22)
[![NPM Version](https://img.shields.io/npm/v/jsonld-document-loader.svg)](https://npm.im/@digitalbazaar/jsonld-document-loader)
> A document loader API for jsonld.js.
## Table of Contents
- [Background](#background)
- [Security](#security)
- [Install](#install)
- [Usage](#usage)
- [Contribute](#contribute)
- [Commercial Support](#commercial-support)
- [License](#license)
## Background
TBD
## Security
TBD
## Install
- Node.js 12+ is required.
### NPM
To install via NPM:
```
npm install --save jsonld-document-loader
```
### Development
To install locally (for development):
```
git clone https://github.com/digitalbazaar/jsonld-document-loader.git
cd jsonld-document-loader
npm install
```
## Usage
```js
import {JsonLdDocumentLoader} from 'jsonld-document-loader';
// or
const {JsonLdDocumentLoader} = require('jsonld-document-loader');
const loader = new JsonLdDocumentLoader();
```
### `addStatic()`
The `addStatic()` method allows developers to load fixed static contexts and
documents, to ensure known versions and contents, without going out to the
network.
For example, to add support for the DID Core context, the VC context, and crypto
suite specific contexts:
```js
import cred from 'credentials-context';
import didContext from 'did-context';
import ed25519Ctx from 'ed25519-signature-2020-context';
const {contexts: credentialsContexts, constants: {CREDENTIALS_CONTEXT_V1_URL}} =
cred;
const jdl = new JsonLdDocumentLoader();
jdl.addStatic(ed25519Ctx.CONTEXT_URL, ed25519Ctx.CONTEXT);
jdl.addStatic(
didContext.constants.DID_CONTEXT_URL,
didContext.contexts.get(didContext.constants.DID_CONTEXT_URL)
);
jdl.addStatic(
CREDENTIALS_CONTEXT_V1_URL,
credentialsContexts.get(CREDENTIALS_CONTEXT_V1_URL)
);
const documentLoader = jdl.build();
// Pass to jsonld, jsonld-signatures, vc-js and similar libraries
```
### `setDidResolver()`
To add support for resolving DIDs and DID-related key ids:
```js
import * as didKey from '@digitalbazaar/did-method-key';
import {CachedResolver} from '@digitalbazaar/did-io';
const cachedResolver = new CachedResolver();
const jdl = new JsonLdDocumentLoader();
cachedResolver.use(didKey.driver());
jdl.setDidResolver(cachedResolver);
// Now you can resolve did:key type DIDs and key objects
const verificationKeyId = 'did:key:z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv#z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv';
await jdl.documentLoader(verificationKeyId);
// ->
{
documentUrl: 'did:key:z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv#z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv',
document: {
"@context": "https://w3id.org/security/suites/ed25519-2020/v1",
"type": "Ed25519VerificationKey2020",
"controller": "did:key:z6MkuBLrjSGt1PPADAvuv6rmvj4FfSAfffJotC6K8ZEorYmv",
"publicKeyMultibase": "zFj5p9C2Sfqth6g6DEXtw5dWFqrtpFn4TCBBPJHGnwKzY",
// ...
}
}
```
### `setProtocolHandler()`
You can also add support for a custom protocol handler:
```js
const customHandler = {
get({url}) {
// return document
}
}
jdl.setProtocolHandler({protocol: 'ipfs', handler: customHandler});
```
## Contribute
See [the contribute file](https://github.com/digitalbazaar/bedrock/blob/master/CONTRIBUTING.md)!
PRs accepted.
If editing the Readme, please conform to the
[standard-readme](https://github.com/RichardLitt/standard-readme) specification.
## Commercial Support
Commercial support for this library is available upon request from
Digital Bazaar: support@digitalbazaar.com
## License
[New BSD License (3-clause)](LICENSE) © Digital Bazaar

Sorry, the diff of this file is not supported yet

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