filed - Simplified file library

228 views
Skip to first unread message

Mikeal Rogers

unread,
Oct 27, 2011, 11:14:24 PM10/27/11
to nod...@googlegroups.com
I've been talking about this forever but I finally wrote it and have been using it on a non-public project for a few weeks now.

Released it today. Basically it's a full file server in a single object. It's kind of like request for files.


# filed -- Simplified file library.

npm install filed

## Super simple to use

Filed does a lazy stat call so you can actually open a file and being writing to it and if the file isn't there it will just be created.

var request = require('filed');
var f = filed('/newfile')
f.write('test')
f.end()

## Streaming

The returned file object is a stream so you can do standard stream stuff to it. Based on *what* you do the object it will be a read stream, a write stream.

So if you send data to it, it'll be a write stream.

fs.createReadStream(filed('/newfile'))

If you pipe it to a destination it'll be a read stream.

filed('/myfile').pipe(fs.createWriteStream('/out'))

And of course you can pipe a filed object from itself to itself and it'll figure it out.

filed('/myfile').pipe(filed('/newfile'))

Those familiar with [request](http://github.com/mikeal/request) will be familiar seeing object capability detection when doing HTTP. filed does this as well.

http.createServer(function (req, resp) {
  filed('/data.json').pipe(resp)
})

Not only does the JSON file get streamed to the HTTP Response it will include an Etag, Last-Modified, Content-Length, and a Content-Type header based on the filed extension.

http.createServer(function (req, resp) {
  req.pipe(filed('/newfile')).pipe(resp)
})

When accepting a PUT request data will be streamed to the file and a 201 status will be sent on the HTTP Response when the upload is finished.

During a GET request a 404 Response will be sent if the file does not exist.

http.createServer(function (req, resp) {
  req.pipe(filed('/data.json')).pipe(resp)
})

The Etag and Last-Modified headers filed creates are based solely on the stat() call so if you pipe a request to an existing file the cache control headers will be taken in to account a 304 response will be sent if the cache control headers match a new stat() call. This can be very helpful in avoiding unnecessary disc reads.

http.createServer(function (req, resp) {
  req.pipe(filed('/directory')).pipe(resp)
})

Just to round out the full feature set and make it full file server if you give filed an existing directory it will actually check for an index.html file in that directory and serve it if it exists.

Dominic Tarr

unread,
Oct 27, 2011, 11:27:30 PM10/27/11
to nod...@googlegroups.com
oh, wow. this is awesome.

and not a callback in sight.

see every one! streams kick ass!

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Jorge

unread,
Oct 28, 2011, 3:19:50 AM10/28/11
to nod...@googlegroups.com
On 28/10/2011, at 05:27, Dominic Tarr wrote:

> oh, wow. this is awesome.
>
> and not a callback in sight.
>
> see every one! streams kick ass!

Yeah... streams kick ass for streaming.
--
Jorge.

Dominic Tarr

unread,
Oct 28, 2011, 8:16:50 AM10/28/11
to nod...@googlegroups.com
you are mad to try to stream stuff any other way!

Pedro Teixeira

unread,
Oct 28, 2011, 10:37:58 AM10/28/11
to nodejs
Mikeal keeps simplifying everyday Node.
Thanks again!

Isaac Schlueter

unread,
Oct 28, 2011, 11:47:01 AM10/28/11
to nod...@googlegroups.com
Yeah, this is super badass. Nicely done, Mikeal.

I'm probably going to integrate with this in my tar thingie.

On Fri, Oct 28, 2011 at 07:37, Pedro Teixeira <pedro.t...@gmail.com> wrote:
>> var request = require('filed');
>> var f = filed('/newfile')
>> f.write('test')
>> f.end()

Assuming that should be `var filed = require('filed')`?

>> fs.createReadStream(filed('/newfile'))

Am I reading this right? You're passing a filed response to
createReadStream? Some kind of toString magic, or should there be a
.pipe() in there somewhere, or flip the nesting?

>> http.createServer(function (req, resp) {
>>   req.pipe(filed('/directory')).pipe(resp)
>>
>> })
>>
>> Just to round out the full feature set and make it full file server if you give filed an existing directory it will actually check for an index.html file in that directory and serve it if it exists.

The only thing missing is filed.createServer('/directory').listen(80)

Mikeal Rogers

unread,
Oct 28, 2011, 11:54:40 AM10/28/11
to nod...@googlegroups.com

On Oct 28, 2011, at October 28, 20118:47 AM, Isaac Schlueter wrote:

> Yeah, this is super badass. Nicely done, Mikeal.
>
> I'm probably going to integrate with this in my tar thingie.
>
> On Fri, Oct 28, 2011 at 07:37, Pedro Teixeira <pedro.t...@gmail.com> wrote:
>>> var request = require('filed');
>>> var f = filed('/newfile')
>>> f.write('test')
>>> f.end()
>
> Assuming that should be `var filed = require('filed')`?

yeah, i got two pull requests for that doc bug 10 minutes after release :) fixed.

>
>>> fs.createReadStream(filed('/newfile'))
>
> Am I reading this right? You're passing a filed response to
> createReadStream? Some kind of toString magic, or should there be a
> .pipe() in there somewhere, or flip the nesting?

whoops, yeah, that should be .pipe(filed('/newfile'))

>
>>> http.createServer(function (req, resp) {
>>> req.pipe(filed('/directory')).pipe(resp)
>>>
>>> })
>>>
>>> Just to round out the full feature set and make it full file server if you give filed an existing directory it will actually check for an index.html file in that directory and serve it if it exists.
>
> The only thing missing is filed.createServer('/directory').listen(80)

i rarely, if ever, what to create a server that *only* serves static files. i could add this easily, and might, but the framework i've been building (which is also being used for this non-public project) has a really nice api for serving files for a route and uses filed under the hood. that will be released eventually as well.

Joshua Cohen

unread,
Oct 28, 2011, 11:58:22 AM10/28/11
to nod...@googlegroups.com
Serving only static files is conditionally very useful, mostly in development environments when you need an easy way to share something you've been working on with other devs (c.f. Python's SimpleHTTPServer).

Nicolas Chambrier

unread,
Oct 29, 2011, 6:10:00 PM10/29/11
to nod...@googlegroups.com
Impressive work. API is quite neat, it's a pleasure working with this.

Congratulations and many thanks for your work!
Reply all
Reply to author
Forward
0 new messages