Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature for setting docs & protocolHandlers from constructor. #11

Closed
wants to merge 12 commits into from
Closed
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# jsonld-document-loader

## 2.1.0 -

### Added
- Constructor now can accept a Map of contexts for the loader.
- Constructor can now accept a Map of protocolHandlers for the loader.

## 2.0.0 - 2023-02-06

### Changed
Expand Down
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
- [Security](#security)
- [Install](#install)
- [Usage](#usage)
- [JsonLdDocumentLoader](#jsonlddocumentloader)
- [addStatic](#addstatic)
- [setDidResolver](#setdidresolver)
- [setProtocolHandler](#setprotocolhandler)
- [Contribute](#contribute)
- [Commercial Support](#commercial-support)
- [License](#license)
Expand Down Expand Up @@ -47,12 +51,49 @@ npm install

## Usage

### `JsonLdDocumentLoader`
```js
import {JsonLdDocumentLoader} from 'jsonld-document-loader';

const loader = new JsonLdDocumentLoader();
```

The Constructor for `JsonLdDocumentLoader` can be passed two optional
parameters: `documents` & `protocolHandlers`. Documents needs to be a Map
with the keys as strings and the values as jsonld documents such as contexts.

```js
import {JsonLdDocumentLoader} from 'jsonld-document-loader';

const url = 'https://example.org/context';
const context = {
"@context": {
"id": "@id",
"type": "@type",
"@protected": true,
"myTerm": {
"@id": "https://example.org/context#myTerm",
"@type": "https://example.org/types#myType"
}
}
};
const documents = new Map([[url, context]]);
const jldl = new JsonLdDocumentLoader({documents});
```
In order to pass in protocolHandlers init a Map with the
key as the protocol as a string and the value an object with a get function.

```js
const protocol = 'ftp',
const handler = {
get: async (url) => {
return ftp.get(url);
}
}
const protocolHandlers = new Map([[protocol, handler]]);
const jldl = new JsonLdDocumentLoader({protocolHandlers});
```

### `addStatic()`

The `addStatic()` method allows developers to load fixed static contexts and
Expand Down
27 changes: 22 additions & 5 deletions lib/JsonLdDocumentLoader.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
/*!
* Copyright (c) 2019-2023 Digital Bazaar, Inc. All rights reserved.
* Copyright (c) 2019-2024 Digital Bazaar, Inc. All rights reserved.
*/
/*
* Class for loading linked data documents.
* @example const jldl = new JsonLdDocumentLoader();
* @example const jldl = new JsonLdDocumentLoader({documents});
* @example const jldl = new JsonLdDocumentLoader({protocolHandlers});
*
*/

export class JsonLdDocumentLoader {
constructor() {
this.documents = new Map();
this.protocolHandlers = new Map();
/*
* @param {object} options - Document Loader Options.
* @param {Map<string, object>} [options.documents = new Map()] - A Map
* containing URLs and their associated contexts.
* @param {Map<string, {get: Function}>} [options.protocolHandlers =
* new Map()] - A Map containing protocolHandlers for the loader.
*/
constructor({
documents = new Map(),
protocolHandlers = new Map()
} = {}) {
// make a new Map to avoid mutation by reference
this.documents = new Map(documents);
this.protocolHandlers = new Map(protocolHandlers);
}

addStatic(url, document) {
Expand Down
68 changes: 68 additions & 0 deletions test/10-test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,5 +184,73 @@ describe('jsonld-document-loader', () => {
result.documentUrl.should.equal(exampleDid);
result.document['@context'].should.equal('https://www.w3.org/ns/did/v1');
});
it('accepts static documents from Constructor', async () => {
const expectedUrl = 'https://example.org/example/v1';
const expectedDocument = {
'@context': {
id: '@id',
type: '@type',
'@protected': true
}
};
const documents = new Map([[expectedUrl, expectedDocument]]);
const jldl = new JsonLdDocumentLoader({documents});
const result = await jldl.documentLoader(expectedUrl);
should.exist(result, `Expected ${expectedUrl} to return ` +
`a result.`);
result.should.have.keys(['contextUrl', 'document', 'documentUrl', 'tag']);
result.documentUrl.should.equal(expectedUrl);
should.exist(result.document, `Expected ${expectedUrl} to return ` +
`a document.`);
result.document.should.eql(expectedDocument);
});
it('copies static documents from Constructor', async () => {
const expectedUrl = 'https://example.org/example/v1';
const expectedDocument = {
'@context': {
id: '@id',
type: '@type',
'@protected': true
}
};
const documents = new Map([[expectedUrl, expectedDocument]]);
const jldl = new JsonLdDocumentLoader({documents});
// replace the context passed in
documents.set(expectedUrl, false);
const result = await jldl.documentLoader(expectedUrl);
should.exist(result, `Expected ${expectedUrl} to return ` +
`a result.`);
result.should.have.keys(['contextUrl', 'document', 'documentUrl', 'tag']);
result.documentUrl.should.equal(expectedUrl);
should.exist(result.document, `Expected ${expectedUrl} to return ` +
`a document.`);
// original document is still in this.documents
result.document.should.eql(expectedDocument);
// document is replaced by new value in original map
documents.get(expectedUrl).should.be.false;
});
it('accepts protocolHandlers from Constructor', async () => {
const expectedProtocol = 'ftp';
const expectedUrl = 'ftp://example.org/example/v1';
const expectedDocument = {
'@context': expectedUrl,
id: 'did:ex:12345'
};
const expectedMethod = {
get: async () => {
return {...expectedDocument};
}
};
const protocolHandlers = new Map([[expectedProtocol, expectedMethod]]);
const jldl = new JsonLdDocumentLoader({protocolHandlers});
const result = await jldl.documentLoader(expectedUrl);
should.exist(result, `Expected ${expectedUrl} to return ` +
`a result.`);
result.should.have.keys(['contextUrl', 'document', 'documentUrl']);
result.documentUrl.should.equal(expectedUrl);
should.exist(result.document, `Expected ${expectedUrl} to return ` +
`a document.`);
result.document.should.eql(expectedDocument);
});
}); // end documentLoader API
});