GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Load JSON-LD in the same way as JSON by darobin · Pull Request #3502 · nodejs/node

Conversation

@darobin

@darobin darobin commented Oct 23, 2015

Copy link
Copy Markdown

The JSON-LD standard 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.

This patch makes .jsonld documents loadable through require() in the exact same manner that .json files are.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Fishrock123

Fishrock123 commented Oct 23, 2015

Copy link
Copy Markdown
Contributor

If I understand correctly, this could be done in an npm module? Perhaps that would be a better place for this?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@darobin

darobin commented Oct 23, 2015

Copy link
Copy Markdown
Author

@Fishrock123 Well, loading .json could be done in an npm module, but the idiomatic way to do it is with require('./foo.json'). Unfortunately there is a standard JSON format out there that pretty much requires a different file extension. This just exposes the existing, idiomatic, built-in Node mechanism to a greater range of JSON content (which is why it's a rather simple patch).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fishrock123

Fishrock123 commented Oct 23, 2015

Copy link
Copy Markdown
Contributor

I mean a module that sets Module._extensions['.jsonld'] = Module._extensions['.json'];.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mscdex mscdex added the module Issues and PRs related to the module subsystem. label Oct 23, 2015
@darobin

darobin commented Oct 24, 2015

Copy link
Copy Markdown
Author

Sure, but again: it seems strange to be able to load some JSON without a module, and to require a module for other JSON.

Don't get me wrong: I think the JSON-LD made the wrong decision to require a distinct MIME type and file extension. I think such approaches should be discouraged. But it's there, it's standard, it seems to be increasingly common, and it seems to me like it ought to be processed identically to JSON so that us developers don't have to worry about superficial differences.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@targos targos added the semver-minor PRs that contain new features and should be released in the next minor version. label Oct 24, 2015
@bnoordhuis

bnoordhuis commented Oct 27, 2015

Copy link
Copy Markdown
Member

Has a conclusion been reached on this? Is it something that should be brought before the TC?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@seishun

seishun commented Oct 27, 2015

Copy link
Copy Markdown
Contributor

I mean a module that sets Module._extensions['.jsonld'] = Module._extensions['.json'];.

I don't think we should encourage writing modules that use internal undocumented functionality.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jasnell

jasnell commented Oct 27, 2015

Copy link
Copy Markdown
Member

Hmm... I tend to agree that we shouldn't be requiring modules to use undocumented bits to enable this kind of thing, and being a user of JSON-LD I've been hit by this also. In my case, I simply have my JSON-LD documents use the *.json suffix to avoid the problem. This change is minor, JSON-LD is a W3C Recommendation and there are people using it in real world apps. I'd say LGTM. Let's land it.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@targos

targos commented Oct 27, 2015

Copy link
Copy Markdown
Member

CI: https://ci.nodejs.org/job/node-test-pull-request/628/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bnoordhuis
bnoordhuis reviewed Oct 27, 2015
View reviewed changes
Comment thread test/parallel/test-require-jsonld.js Outdated

@bnoordhuis bnoordhuis Oct 27, 2015

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use const here?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bnoordhuis

bnoordhuis commented Oct 27, 2015

Copy link
Copy Markdown
Member

Couple of nits regarding the test. LGTM if you fix those and squash the commits.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@darobin
module: load JSON-LD with standard extension
3dd7e46
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.
@darobin
darobin force-pushed the load-jsonld branch from ef74b22 to 3dd7e46 Compare October 27, 2015 18:28
@darobin

darobin commented Oct 27, 2015

Copy link
Copy Markdown
Author

@bnoordhuis Changes applied, squashed, and pushed. Thanks!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bnoordhuis

bnoordhuis commented Oct 27, 2015

Copy link
Copy Markdown
Member

Thanks, LGTM. CI: https://ci.nodejs.org/job/node-test-pull-request/633/

Before I land this prematurely, does anyone feel this should be discussed more or be brought up at the next TC call?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@targos

targos commented Oct 27, 2015

Copy link
Copy Markdown
Member

LGTM

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rvagg

rvagg commented Oct 27, 2015

Copy link
Copy Markdown
Member

I'd like to hold this one off until we get a @nodejs/tsc discussion on this, I'm really not sure we should be adding anything new to _extensions.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rvagg rvagg added the tsc-agenda label Oct 27, 2015
@rvagg rvagg mentioned this pull request Oct 28, 2015
Node Foundation CTC Meeting 2015-10-28 #3561
Closed
@jasnell

jasnell commented Oct 28, 2015

Copy link
Copy Markdown
Member

This was discussed on the CTC call today. Consensus decision was not to land this change, largely because (a) it opens up the possibility of a slippery slope with other formats that others may want to support and (b) since JSON-LD is really just JSON, there's a simple workaround.

If you look at https://www.npmjs.com/package/activitystreams-context, you can see an example of a module that loads JSON-LD using the *.json file extension. For all reasonable cases, this works well enough and using the *.jsonld file extension is not strictly required.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jasnell jasnell closed this Oct 28, 2015
@Fishrock123 Fishrock123 removed the tsc-agenda label Oct 28, 2015
@ChALkeR

ChALkeR commented Oct 28, 2015

Copy link
Copy Markdown
Member

Just noticed this PR.

@darobin

the idiomatic way to do it is with require('./foo.json')

Am I getting it correct that you wanted an require('file.jsonld') support out-of-the-box?
I'm pretty sure that would promote erroneous use-cases.

require() should not be used for file loading after your app is started.
You also wouldn't want to run require() on a huge amount of small files, even in a sync script.

The correct way to process a small json file is to read file first (and do that in an async way) and then do a json parse. The correct way to process a huge json file containing a lot of separate entries involves a streaming json parser.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@darobin

darobin commented Oct 28, 2015

Copy link
Copy Markdown
Author

I won't object to the decision further, but I feel that the rationale for rejecting the change is unconvincing:

  • The slippery slope argument is a well-known fallacy: if we saw a large influx of json extensions coming it would hold, but I don't think that's the case here.
  • I am well aware that you can just use .json, that's pretty obvious and has never been in doubt, but that then forces us to special-case those files in order to serve them over HTTP (as indicated in the PR).
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@darobin

darobin commented Oct 28, 2015

Copy link
Copy Markdown
Author

@ChALkeR This does not promote anything that isn't already supported, the goal of the PR is simply to avoid special-casing some types of JSON and/or introducing non-idiomatic dirty workarounds (which we have to do now, and it seems will have to continue doing).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jasnell

jasnell commented Oct 28, 2015

Copy link
Copy Markdown
Member

@darobin ... in those cases, the only fallback would be the workaround the @Fishrock123 suggests, albeit keeping in mind that the workaround involves using an undocumented and unsupported "feature"

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ChALkeR

ChALkeR commented Oct 28, 2015

Copy link
Copy Markdown
Member

@darobin What's your exact use-case, once again?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ChALkeR

ChALkeR commented Oct 28, 2015

Copy link
Copy Markdown
Member

Btw, one more thing to note: Module API stability index is Locked.
This PR should have been automatically rejected just because it's a semver-minor feature request in the module system.

Refs:
https://nodejs.org/api/modules.html#modules_modules
https://nodejs.org/api/documentation.html#documentation_stability_index

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@darobin

darobin commented Oct 28, 2015

Copy link
Copy Markdown
Author

@jasnell I know. We don't want to go there, sticking with the unpleasant special-casing hack we have rather than switching to an unpleasant unsupported-feature hack is better :) I guess that groups with "TC", "WG", etc. in their name tend to lead to this sort of trade-off.

@ChALkeR I'm not sure what's unclear? The use case is to load JSON in the same manner that require() can do it today, but for a standard class of JSON that has an extension different .json. (I didn't label this semver-minor.)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ChALkeR

ChALkeR commented Oct 28, 2015

Copy link
Copy Markdown
Member

@darobin Guessing that this is not theoretical, so I suppose that you have some app where you want to utilize that.

  1. Is that a server-side app? A desktop app?
  2. Do you load that file at the start-up time? At the runtime?
  3. Do you want to load a single (or a definite small amount) of hard-coded files? Or is that a directory / generated files / files supplied by user input?
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ChALkeR

ChALkeR commented Oct 28, 2015

Copy link
Copy Markdown
Member

@darobin As for the semver-minor, that label is correct, it doesn't matter who added it. Note that this PR is requesting an API change that is backward compatible, but not forward compatible, hence the semver-minor tag.

The doc clearly states:

Only fixes related to security, performance, or bug fixes will be accepted.

This is neither of those.

For this PR to properly get in (even putting all the other uncertainty aside), the stability index should be first lowered, and my guess would be that it doesn't worth that.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@darobin

darobin commented Oct 28, 2015

Copy link
Copy Markdown
Author

@ChALkeR Indeed, it's not theoretical; it wouldn't make for a very interesting theoretical case :)

It is both server and client, in fact many of the JSON-LD files we have are part of libraries that we reuse in various places. The server loads these files and uses the information in order to process things (e.g. to know that a given type found here is equivalent to another). Some client apps will load them at build time (where the lack of support for loading .jsonld is also a nuisance), others over HTTP (where the MIME type needs to be correct).

I guess "bug" is in the eye of the beholder :) Anyway, I disagree with the decision but I don't want to insist for something rather small.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jasnell

jasnell commented Oct 28, 2015

Copy link
Copy Markdown
Member

@darobin ... on possible approach you could take here is to do a polyfill wrap around the built-in require method. It's a bit hacky, but using require.resolve you can locate the path to the JSON-LD file and load it but fall through to the built in require if the file does not use the .jsonld path.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ChALkeR

ChALkeR commented Oct 28, 2015

Copy link
Copy Markdown
Member

@darobin I'm pretty sure that you have something wrong in your use-cases.

Note that require() is both blocking (it introduces a lag on all requests when used at runtime) and caching (the amount of used memory would grow if you do an unlimited amount of those).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ChALkeR

ChALkeR commented Oct 28, 2015

Copy link
Copy Markdown
Member

@Fishrock123

I mean a module that sets Module._extensions['.jsonld'] = Module._extensions['.json'];.

I belive that require.extensions should be used instead of Module._extensions here (if one insists on using it, which I wouldn't recommend). require.extensions is exposed on the purpose. It's still deprecated and not recommended to use, but it's a bit better than doing the same with Module._extensions.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rvagg

rvagg commented Oct 28, 2015

Copy link
Copy Markdown
Member

Here's your slippery slope https://en.wikipedia.org/wiki/List_of_XML_markup_languages

And yes, XML might have been a mistake to all us now enlightened folks, but we're now using JSON for basically the same things that we were using XML for and filename extension bloat is inevitable as people come up with specific variants. If we add .jsonld then the argument for rejecting the next one is more difficult than the argument for rejecting the first one.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This was referenced Apr 29, 2023
[Snyk] Security upgrade karma from 3.1.4 to 5.0.8 aliscco/alisco-node#375
Open
[Snyk] Security upgrade karma from 3.1.4 to 5.0.8 aliscco/alisco-node#379
Open
This was referenced Jun 22, 2023
[Snyk] Security upgrade karma from 3.1.4 to 5.0.8 aliscco/alisco-node#494
Open
[Snyk] Security upgrade karma from 3.1.4 to 5.0.8 aliscco/alisco-node#532
Open
[Snyk] Security upgrade karma from 4.4.1 to 5.0.8 aliscco/alisco-node#562
Open
[Snyk] Security upgrade karma from 4.4.1 to 5.0.8 aliscco/alisco-node#595
Open
[Snyk] Security upgrade karma from 3.1.4 to 5.0.8 aliscco/alisco-node#614
Open
@leonardoadame leonardoadame mentioned this pull request Jul 9, 2023
[Snyk] Security upgrade karma from 3.1.4 to 5.0.8 leonardoadame/node#143
Open
@aliscco aliscco mentioned this pull request Oct 27, 2023
[Snyk] Security upgrade karma from 3.1.4 to 5.0.8 aliscco/alisco-node#646
Open
@aliscco aliscco mentioned this pull request Nov 28, 2023
[Snyk] Security upgrade karma from 3.1.4 to 5.0.8 aliscco/alisco-node#695
Open
This was referenced Nov 29, 2023
[Snyk] Security upgrade karma from 3.1.4 to 5.0.8 leonardoadame/node#211
Open
[Snyk] Security upgrade karma from 3.1.4 to 5.0.8 leonardoadame/node#268
Open
@aliscco aliscco mentioned this pull request Dec 2, 2023
[Snyk] Security upgrade karma from 3.1.4 to 5.0.8 aliscco/alisco-node#1024
Open
@leonardoadame leonardoadame mentioned this pull request Dec 3, 2023
[Snyk] Security upgrade karma from 3.1.4 to 5.0.8 leonardoadame/node#479
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

module Issues and PRs related to the module subsystem. semver-minor PRs that contain new features and should be released in the next minor version.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

9 participants

@darobin @Fishrock123 @bnoordhuis @seishun @jasnell @targos @rvagg @ChALkeR @mscdex
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.