Skip to content

Commit

Permalink
module: load JSON-LD with standard extension
Browse files Browse the repository at this point in the history
The JSON-LD standard (http://www.w3.org/TR/json-ld/) requires sending
what is effectively JSON content using a different MIME type. Because
of that, it is usually required to use a file extension other than
.json since that is what is typically used in MIME type mapping in
HTTP servers. The IANA-registered file extension is .jsonld
(http://www.iana.org/assignments/media-types/application/ld+json).

This patch makes .jsonld documents loadable through require() in the
exact same manner that .json files are.
  • Loading branch information
darobin committed Oct 27, 2015
1 parent aaf9b48 commit 3dd7e46
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,8 @@ Module._extensions['.json'] = function(module, filename) {
}
};

// Native extension for .jsonld, same as JSON (but different MIME type)
Module._extensions['.jsonld'] = Module._extensions['.json'];

//Native extension for .node
Module._extensions['.node'] = function(module, filename) {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/elementary.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"@id": "moo"
}
4 changes: 4 additions & 0 deletions test/fixtures/invalid.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"@id": "moo"
"@type": "http://example.org"
}
14 changes: 14 additions & 0 deletions test/parallel/test-require-jsonld.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';
require('../common');
const assert = require('assert');

assert.throws(
function() {
require('../fixtures/invalid.jsonld');
},
/test[\/\\]fixtures[\/\\]invalid.jsonld: Unexpected string/,
'require() jsonld error should include path'
);

var ld = require('../fixtures/elementary.jsonld');
assert.strictEqual(ld['@id'], 'moo', 'require() loads jsonld');

0 comments on commit 3dd7e46

Please sign in to comment.