html-pdf-chrome icon indicating copy to clipboard operation
html-pdf-chrome copied to clipboard

generate stream without promise

Open graingert opened this issue 8 years ago • 13 comments

streams are already async, and don't need to be wrapped in a promise.

graingert avatar Aug 23 '17 10:08 graingert

This is just a WIP as I'm not sure what the interface should be.

graingert avatar Aug 23 '17 10:08 graingert

htmlPdf.create(html, options) is what needs to be wrapped in a Promise. If you look here, the Stream is not being wrapped in a Promise.

Here's example usage:

const pdf = await htmlPdf.create(html, options);
const stream = pdf.toStream(); // Look mom, no Promises!

The only async CreateResult is:

async toFile(filename: string): Promise<void>

westy92 avatar Aug 23 '17 14:08 westy92

@westy92 code should be:

const stream = htmlPdf.createToStream(html, options); // Look parent, no Promises!

graingert avatar Aug 23 '17 14:08 graingert

@westy92 because const pdf = await htmlPdf.create(html, options); is promise code that's not needed if the user wants a stream.

graingert avatar Aug 23 '17 14:08 graingert

Codecov Report

Merging #43 into master will decrease coverage by 6.77%. The diff coverage is 20%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #43      +/-   ##
==========================================
- Coverage     100%   93.22%   -6.78%     
==========================================
  Files           3        3              
  Lines         108      118      +10     
  Branches       12       12              
==========================================
+ Hits          108      110       +2     
- Misses          0        8       +8
Impacted Files Coverage Δ
src/index.ts 86.88% <20%> (-13.12%) :arrow_down:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 4a4f21c...b202f82. Read the comment docs.

codecov[bot] avatar Aug 23 '17 14:08 codecov[bot]

@westy92 obviously I'll update the tests if you agree with this interface.

graingert avatar Aug 23 '17 14:08 graingert

I'm not sure what's with the Promisephobia. The reason results are extracted into a CreateResult is so it can be abstracted away and then consumed in a desired format. Adding a special case just for Streams seems counterintuitive.

Can you provide a use case for needing this functionality?

westy92 avatar Aug 23 '17 14:08 westy92

@westy92 I don't have Promise phobia, I just don't want them in the stream interface where they're unused.

graingert avatar Aug 23 '17 14:08 graingert

@westy92 eg it would match the node API for createReadStream: https://nodejs.org/api/fs.html#fs_fs_createreadstream_path_options

graingert avatar Aug 23 '17 14:08 graingert

Your implementation is just hiding the Promise; it's still there. If you want to do this, feel free to put this wrapper inside your own project.

westy92 avatar Aug 23 '17 14:08 westy92

@westy92 sure it's hiding the promise, that's what streams are for.

graingert avatar Aug 23 '17 14:08 graingert

@westy92 if you were to add an Observable interface, eg when they land natively in node, it would look very strange to do:

Observable.from(htmlPdf.create(html, options)).flatMap(v => v.toObservable());

vs:

htmlPdf.createObservable(html, options);

Which is analogous to the current stream API.

graingert avatar Aug 23 '17 14:08 graingert

It appears that https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF now has a transferMode option: image

That should allow us to implement this fairly simply.

westy92 avatar Aug 15 '20 04:08 westy92