tritech / node-icalendar

iCalendar parser and generator for Node.js
MIT License
234 stars 50 forks source link

Support streams #29

Closed clarkbw closed 7 years ago

clarkbw commented 9 years ago

Hi -

I have a need for getting the calendar as a stream instead of as a string. My requirement is about pushing the calendar up to a pkgcloud service but perhaps others also want another way to serve calendars; see why you should use streams.

I'm wondering if you'd accept a pull request along those lines and if so looking for any guidance you might have.

There are certainly good arguments to be made that much of the calendar object is already in memory so streaming it only saves from creating a large string representation of the objects already in memory. As I said, my request is a bit about convenience operating with things expecting a stream and so performance while nice isn't my top line goal.

Currently I'm using the following to take the calendar toString method and coerce it into into a stream.

var  _      = require('lodash');
var ical   = require('icalendar');
var Readable = require('stream').Readable;

var calendar = new ical.iCalendar();
var s = new Readable();
s._read = _.noop;
s.push(calendar.toString());
s.push(null);
var stream3 = s3.uploadStream('ical', 'uid.ics');
s.pipe(stream3);

My plan is to make the following changes:

Probably insert the _read function between the toString and format functions. https://github.com/tritech/node-icalendar/blob/master/lib/base.js#L236

This change should give the Calendar object ( by inheritance ) the ability to be piped like a stream.

var ical   = require('icalendar');

var calendar = new ical.iCalendar();
var stream3 = s3.uploadStream('ical', 'uid.ics');
calendar.pipe(stream3);

Maybe I'll just write it now that I spec'd it all out.

mreinstein commented 8 years ago

is this ready to be published to npm? I see the readable stream code was merged a while ago.