Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added File.toJSON method #84

Closed
wants to merge 137 commits into from
Closed

Added File.toJSON method #84

wants to merge 137 commits into from

Conversation

Marak
Copy link

@Marak Marak commented Apr 20, 2016

  • Serializes vinyl File object into JSON
  • First pass. Seems to be working
  • No new tests. Old tests passing.

Hello. Did a quick PR to resolve issue #83

Let me know what you think. I'm still testing this locally, but it seems OK.

Accidentally did two PRs ( missed a project LINT rule ). Sorry.

Contra and others added 30 commits December 20, 2013 20:01
Cloning the stats object would remove its prototype,
meaning it would lose methods normally available on fs.Stats
instances, such as Stats.isDirectory() or Stats.isFile()

This resulted in errors being thrown whenever a cloned file
was piped through to a gulp.dest() stream when it expected
the isDirectory() method.
Correct File.clone() treatment of File.stats
Correct README about pipe's end option.
Spotted this because the file had funny syntax highlighting on GitHub.
LICENSE: Remove executable mode
@yocontra
Copy link
Member

LGTM, @phated ?

var self = this;
return {
basename: self.basename,
contents: self.contents.toString(),
Copy link
Member

Choose a reason for hiding this comment

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

this doesn't seem to handle streaming or null contents

@phated
Copy link
Member

phated commented Apr 20, 2016

Just 1 problem and then I need to run it against gulp-debug

@Marak
Copy link
Author

Marak commented Apr 20, 2016

I'm open to suggestion on how to address the null / streaming problem.

@yocontra
Copy link
Member

@Marak The same logic from clone can be used in toJSON - clone also handles custom attributes, streaming contents, null stats, etc.

@phated
Copy link
Member

phated commented Apr 21, 2016

I think toJSON is supposed to result in a serializable object

@yocontra
Copy link
Member

yocontra commented Apr 21, 2016

Yeah idk you can't really serialize a stream synchronously so I guess it'll have to exclude contents if it isn't null or a buffer

@Marak
Copy link
Author

Marak commented Apr 21, 2016

Serialization of stream would require entire stream processed into buffer.

So I think maybe method should have option to accept optional callback?

I think in most cases the user would want the actual contents of the file.

@phated
Copy link
Member

phated commented Apr 21, 2016

An optional callback wouldn't work because the toJSON method is called when JSON.stringify is used on the object. Maybe we need to use placeholders (something like <Stream> and <Buffer>)?

@Marak
Copy link
Author

Marak commented Apr 21, 2016

Where is JSON.stringify being called? Why would File.toJSON get called via JSON.stringify?

You've lost me. Place holders seem okay, but I'd still need a way to serialize the File instance ( including the streaming file data put into a buffer ).

The goal here is to be able to ship a single File instance over the wire as single buffered JSON message.

@phated
Copy link
Member

phated commented Apr 21, 2016

@Marak Sorry, what I meant is that doing JSON.stringify(new Vinyl()) will call into toJSON

@Marak
Copy link
Author

Marak commented Apr 21, 2016

So I think an optional callback would still work?

In the case of JSON.stringify(new Vinyl()), no callback would be provided, which could result in placeholders such as <Stream> or <buffer>?

@Marak
Copy link
Author

Marak commented Apr 21, 2016

I'm also wondering, is anyone actually doing JSON.stringify(new Vinyl()) anywhere already? Seems like it wouldn't work right to begin with.

@phated
Copy link
Member

phated commented Apr 21, 2016

I think the best way to handle buffering and serializing would be to use https://www.npmjs.com/package/vinyl-buffer like:

var buffer = require('vinyl-buffer');
var through = require('through2');
vfs.src('**/*.txt', { buffer: false }) // totally contrived
  .pipe(buffer())
  .pipe(through.obj(function(file, enc, cb){
    cb(null, JSON.stringify(file));
  });

I think it makes a lot of sense to just throw on a stream type (or anything non-serializable) which is similar to how JSON.stringify handles circular objects.

@phated
Copy link
Member

phated commented Apr 21, 2016

Currently, calling JSON.stringify on a vinyl object causes the stream object to be serialized, which is terrible!

@Marak
Copy link
Author

Marak commented Apr 21, 2016

I see. I'll take a look at bl and vinyl-buffer library a bit.

I've got my fork working here, so I'll continue to push on the use-case I require.

I'll keep a watch on this issue and will post any more code changes I have to make on my fork.

Cheers.

@phated
Copy link
Member

phated commented Apr 21, 2016

After discussions and reading the spec, throwing a TypeError seems wrong/bad. As per @terinjokes, it's looking like nulling contents if it is a stream is the best behavior.

@terinjokes
Copy link

Let's make sure to document this, and how a user can convert (and why they shouldn't if necessary)

@Marak
Copy link
Author

Marak commented Apr 21, 2016

@phated @terinjokes -

If you null the contents of a stream on serialization, how will you know if the contents were actually null to begin with versus a Stream which got converted to null?

Is that information important to track?

@coveralls
Copy link

Coverage Status

Coverage decreased (-4.08%) to 95.918% when pulling f5d33ef on Marak:master into 896f68d on gulpjs:master.

@Marak
Copy link
Author

Marak commented Apr 22, 2016

More commits will follow. Still working on my integration over here.

@phated
Copy link
Member

phated commented Apr 22, 2016

@Marak cool, really interested to see what behavior you need when you get to streaming contents (if you are using vinyl-fs: vfs.src(glob, { buffer: false })).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet