From 3dd7e46e91143b61fdf231ce4ed8d80db90e1d4d Mon Sep 17 00:00:00 2001 From: Robin Berjon Date: Fri, 23 Oct 2015 15:32:14 -0400 Subject: [PATCH] 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. --- lib/module.js | 2 ++ test/fixtures/elementary.jsonld | 3 +++ test/fixtures/invalid.jsonld | 4 ++++ test/parallel/test-require-jsonld.js | 14 ++++++++++++++ 4 files changed, 23 insertions(+) create mode 100644 test/fixtures/elementary.jsonld create mode 100644 test/fixtures/invalid.jsonld create mode 100644 test/parallel/test-require-jsonld.js diff --git a/lib/module.js b/lib/module.js index ef0fad209ff9c8..3dc966b770f38e 100644 --- a/lib/module.js +++ b/lib/module.js @@ -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) { diff --git a/test/fixtures/elementary.jsonld b/test/fixtures/elementary.jsonld new file mode 100644 index 00000000000000..1c6a141ed28c12 --- /dev/null +++ b/test/fixtures/elementary.jsonld @@ -0,0 +1,3 @@ +{ + "@id": "moo" +} diff --git a/test/fixtures/invalid.jsonld b/test/fixtures/invalid.jsonld new file mode 100644 index 00000000000000..f5ba6ca634c28e --- /dev/null +++ b/test/fixtures/invalid.jsonld @@ -0,0 +1,4 @@ +{ + "@id": "moo" + "@type": "http://example.org" +} diff --git a/test/parallel/test-require-jsonld.js b/test/parallel/test-require-jsonld.js new file mode 100644 index 00000000000000..679b841a7bce6b --- /dev/null +++ b/test/parallel/test-require-jsonld.js @@ -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');