Skip to content

Commit

Permalink
Revert inputFormat change.
Browse files Browse the repository at this point in the history
The `inputFormat` change can break callers that mistakenly pass in an
incorrect `inputFormat`. jsonld.js was doing this. Backing this out for
now and it will be added to a future major version release.
  • Loading branch information
davidlehn committed Jun 22, 2023
1 parent 4cb8485 commit 0c3118a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 64 deletions.
3 changes: 0 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@
by the canonicalization algorithm. This feature is particularly useful when
the resulting bnode labels need to be changed for use cases such as selective
disclosure.
- Add `inputFormat` option. Use "application/n-quads" for a N-Quads string that
will be parsed. Omit option for a JSON dataset or legacy dataset. This can
simplify a common case of using the internal parser to generate a dataset.

## 3.3.0 - 2022-09-17

Expand Down
21 changes: 4 additions & 17 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,8 @@ try {
rdfCanonizeNative = require('rdf-canonize-native');
} catch(e) {}

// return a dataset from input dataset, legacy dataset, or n-quads
function _inputToDataset(input, options) {
if('inputFormat' in options) {
if(options.inputFormat === 'application/n-quads') {
return exports.NQuads.parse(input);
}
throw new Error(
`Unknown canonicalization input format: "${options.inputFormat}".`);
}
// return a dataset from input dataset or legacy dataset
function _inputToDataset(input/*, options*/) {
// back-compat with legacy dataset
if(!Array.isArray(input)) {
return exports.NQuads.legacyDatasetToQuads(input);
Expand Down Expand Up @@ -81,7 +74,7 @@ exports._rdfCanonizeNative = function(api) {
* Asynchronously canonizes an RDF dataset.
*
* @param {Array|object|string} input - The input to canonize given as a
* dataset, legacy dataset, or format specified by 'inputFormat' option.
* dataset or legacy dataset.
* @param {object} options - The options to use:
* {string} algorithm - The canonicalization algorithm to use, `URDNA2015`.
* {Function} [createMessageDigest] - A factory function for creating a
Expand All @@ -92,9 +85,6 @@ exports._rdfCanonizeNative = function(api) {
* {Map} [canonicalIdMap] - An optional Map to be populated by the canonical
* identifier issuer with the bnode identifier mapping generated by the
* canonicalization algorithm.
* {string} [inputFormat] - The format of the input. Use
* 'application/n-quads' for a N-Quads string that will be parsed. Omit for
* a JSON dataset or legacy dataset.
* {boolean} [useNative=false] - Use native implementation.
* {number} [maxDeepIterations=Infinity] - The maximum number of times to run
* deep comparison algorithms (such as the N-Degree Hash Quads algorithm
Expand Down Expand Up @@ -137,17 +127,14 @@ exports.canonize = async function(input, options) {
* browser.
*
* @param {Array|object|string} input - The input to canonize given as a
* dataset, legacy dataset, or format specified by 'inputFormat' option.
* dataset or legacy dataset.
* @param {object} options - The options to use:
* {string} algorithm - The canonicalization algorithm to use, `URDNA2015`.
* {Function} [createMessageDigest] - A factory function for creating a
* `MessageDigest` interface that overrides the built-in message digest
* implementation used by the canonize algorithm; note that using a hash
* algorithm (or HMAC algorithm) that differs from the one specified by
* the canonize algorithm will result in different output.
* {string} [inputFormat] - The format of the input. Use
* 'application/n-quads' for a N-Quads string that will be parsed. Omit for
* a JSON dataset or legacy dataset.
* {boolean} [useNative=false] - Use native implementation.
* {number} [maxDeepIterations=Infinity] - The maximum number of times to run
* deep comparison algorithms (such as the N-Degree Hash Quads algorithm
Expand Down
48 changes: 4 additions & 44 deletions test/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,9 @@
const rdfCanonize = require('..');
const assert = require('assert');

describe('API tests', () => {
it('should reject invalid inputFormat', async () => {
let error;
try {
await rdfCanonize.canonize('', {
algorithm: 'URDNA2015',
inputFormat: 'application/bogus',
format: 'application/n-quads'
});
} catch(e) {
error = e;
}
assert(error);
});

it('should fail to parse empty dataset as N-Quads', async () => {
let error;
try {
await rdfCanonize.canonize([], {
algorithm: 'URDNA2015',
inputFormat: 'application/bogus',
format: 'application/n-quads'
});
} catch(e) {
error = e;
}
assert(error);
});

it('should fail to parse empty legacy dataset as N-Quads', async () => {
let error;
try {
await rdfCanonize.canonize({}, {
algorithm: 'URDNA2015',
inputFormat: 'application/bogus',
format: 'application/n-quads'
});
} catch(e) {
error = e;
}
assert(error);
});
const {NQuads} = rdfCanonize;

describe('API tests', () => {
it('should set canonicalIdMap data', async () => {
const input = `\
_:b0 <urn:p0> _:b1 .
Expand All @@ -64,9 +24,9 @@ _:c14n1 <urn:p1> "v1" .
}));

const canonicalIdMap = new Map();
const output = await rdfCanonize.canonize(input, {
const dataset = NQuads.parse(input);
const output = await rdfCanonize.canonize(dataset, {
algorithm: 'URDNA2015',
inputFormat: 'application/n-quads',
format: 'application/n-quads',
canonicalIdMap
});
Expand Down

0 comments on commit 0c3118a

Please sign in to comment.