Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ coverage
node_modules
v8.log
package-lock.json
node_modules/
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,21 @@
"lib/**/*.js"
],
"dependencies": {
"@digitalbazaar/http-client": "^3.2.0",
"@digitalbazaar/http-client": "^4.3.0",
"@xmldom/xmldom": "^0.8.2",
"content-type": "^1.0.4",
"get-stdin": "^9.0.0",
"jsonld": "^8.1.0",
"jsonld": "^9.0.0",
"rdfa": "^0.0.10"
},
"devDependencies": {
"@digitalbazaar/eslint-config": "^8.0.1",
"eslint": "^9.39.4"
"eslint": "^9.39.4",
"mocha": "^11.7.5",
"chai": "^6.2.2"
},
"engines": {
"node": ">=14.13.1"
"node": "^20.19.0 || ^22.13.0 || >=24.0.0"
},
"keywords": [
"JSON",
Expand All @@ -57,6 +59,7 @@
"jsonld"
],
"scripts": {
"lint": "eslint ."
"lint": "eslint .",
"test": "mocha \"test/**/*.test.js\""
}
}
9 changes: 9 additions & 0 deletions test/data/sample.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"@context": "https://www.w3.org/ns/credentials/v2",
"id": "urn:uuid:2a1d4ddb-bbda-43cb-8886-fda9855bcf5d",
"type": ["VerifiableCredential"],
"credentialSubject": {
"id": "did:example:123",
"name": "Example"
}
}
76 changes: 76 additions & 0 deletions test/jsonld-request.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import * as chai from 'chai';
import {promises as fs} from 'node:fs';
import http from 'node:http';
import {jsonldRequest} from '../lib/index.js';
import path from 'node:path';
import {spawn} from 'node:child_process';

const should = chai.should();

describe('jsonldRequest', function() {
let fixturePath;
let fixtureData;

before(async () => {
fixturePath = path.join(import.meta.dirname, 'data', 'sample.jsonld');
fixtureData = await fs.readFile(fixturePath, 'utf8');
});

it('loads a local file (file://) and parses JSON-LD', async () => {
const fileUrl = `file://${fixturePath}`;
const {data} = await jsonldRequest(fileUrl, {allow: ['file']});
data.should.deep.equal(JSON.parse(fixtureData));
});

it('loads JSON-LD over HTTP and parses it', async () => {
// start local HTTP server to serve the fixture
const server = http.createServer((req, res) => {
const headers = {
'Content-Type': 'application/ld+json; charset=utf-8'
};
res.writeHead(200, headers);
res.end(fixtureData);
});
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
const addr = server.address();
const url = `http://127.0.0.1:${addr.port}/fixture.json`;
try {
const {data} = await jsonldRequest(url, {allow: ['http', 'https']});
should.exist(data);
data.should.be.an('object');
data.should.have.property('@context');
} finally {
server.close();
}
});

it('reads JSON-LD from stdin (child process runner)', async () => {
const runner = path.join(import.meta.dirname, 'runner-stdin.js');
const child = spawn(
process.execPath,
[runner, '-']
);

let stdout = '';
let stderr = '';
child.stdout.on('data', d => {
stdout += d.toString();
});
child.stderr.on('data', d => {
stderr += d.toString();
});

// write fixture to child's stdin
child.stdin.write(fixtureData);
child.stdin.end();

const exitCode = await new Promise(resolve => child.on('close', resolve));
if(exitCode !== 0) {
throw new Error(`Runner exited with ${exitCode}: ${stderr}`);
}
const parsed = JSON.parse(stdout);
should.exist(parsed);
parsed.should.be.an('object');
parsed.should.have.property('@context');
});
});
14 changes: 14 additions & 0 deletions test/runner-stdin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env node
import {jsonldRequest} from '../lib/index.js';

const loc = process.argv[2] || '-';
const options = {allow: ['stdin']};

jsonldRequest(loc, options).then(({data}) => {
// print only the data as JSON to stdout
console.log(JSON.stringify(data));
}).catch(err => {
// print error to stderr
console.error(err && err.stack ? err.stack : String(err));
process.exit(1);
});
Loading