Skip to content

Commit 3dd7e46

Browse files
committed
module: load JSON-LD with standard extension
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.
1 parent aaf9b48 commit 3dd7e46

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

lib/module.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,8 @@ Module._extensions['.json'] = function(module, filename) {
444444
}
445445
};
446446

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

448450
//Native extension for .node
449451
Module._extensions['.node'] = function(module, filename) {

test/fixtures/elementary.jsonld

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"@id": "moo"
3+
}

test/fixtures/invalid.jsonld

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"@id": "moo"
3+
"@type": "http://example.org"
4+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
5+
assert.throws(
6+
function() {
7+
require('../fixtures/invalid.jsonld');
8+
},
9+
/test[\/\\]fixtures[\/\\]invalid.jsonld: Unexpected string/,
10+
'require() jsonld error should include path'
11+
);
12+
13+
var ld = require('../fixtures/elementary.jsonld');
14+
assert.strictEqual(ld['@id'], 'moo', 'require() loads jsonld');

0 commit comments

Comments
 (0)