galkahana / HummusJS

Node.js module for high performance creation, modification and parsing of PDF files and streams
http://www.pdfhummus.com
Other
1.14k stars 169 forks source link

Error using Node `stream.Writable` #391

Open drewjs opened 5 years ago

drewjs commented 5 years ago

There seems to be an issue using core Node stream.Writable for output. According to the docs, hummus should be able to use any writable stream. However you get errors when calling createWriter and createWriterToModify with the writable object.

Tested similar code in Node 10 and 12:

import { Writable } from 'streams' // core Node streams

const writable = new Writable()
const out = new PDFStreamForResponse(writable)
const writer = createWriterToModify(new PDFRStreamForBuffer(inputBuffer), out)
FATAL ERROR: v8::ToLocalChecked Empty MaybeLocal.
 1: 0x10003c597 node::Abort() [/usr/local/Cellar/nvm/0.34.0/versions/node/v10.15.3/bin/node]
 2: 0x10003c7a1 node::OnFatalError(char const*, char const*) [/usr/local/Cellar/nvm/0.34.0/versions/node/v10.15.3/bin/node]
 3: 0x1001afce0 v8::V8::ToLocalEmpty() [/usr/local/Cellar/nvm/0.34.0/versions/node/v10.15.3/bin/node]
 4: 0x10503e20f ObjectByteWriterWithPosition::Write(unsigned char const*, unsigned long) [/Users/andrew/workspace/nidus/node_modules/hummus/binding/hummus.node]
 5: 0x10507e1bd ObjectsContext::WriteComment(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) [/Users/andrew/workspace/nidus/node_modules/hummus/binding/hummus.node]
 6: 0x105061e49 PDFHummus::DocumentContext::WriteHeader(EPDFVersion) [/Users/andrew/workspace/nidus/node_modules/hummus/binding/hummus.node]
 7: 0x105034740 PDFWriterDriver::StartPDF(v8::Local<v8::Object>, EPDFVersion, LogConfiguration const&, PDFCreationSettings const&) [/Users/andrew/workspace/nidus/node_modules/hummus/binding/hummus.node]
 8: 0x10503f1e2 CreateWriter(v8::FunctionCallbackInfo<v8::Value> const&) [/Users/andrew/workspace/nidus/node_modules/hummus/binding/hummus.node]
 9: 0x10023663f v8::internal::FunctionCallbackArguments::Call(v8::internal::CallHandlerInfo*) [/usr/local/Cellar/nvm/0.34.0/versions/node/v10.15.3/bin/node]
10: 0x100235b81 v8::internal::MaybeHandle<v8::internal::Object> v8::internal::(anonymous namespace)::HandleApiCallHelper<false>(v8::internal::Isolate*, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::FunctionTemplateInfo>, v8::internal::Handle<v8::internal::Object>, v8::internal::BuiltinArguments) [/usr/local/Cellar/nvm/0.34.0/versions/node/v10.15.3/bin/node]
11: 0x100235220 v8::internal::Builtin_Impl_HandleApiCall(v8::internal::BuiltinArguments, v8::internal::Isolate*) [/usr/local/Cellar/nvm/0.34.0/versions/node/v10.15.3/bin/node]
12: 0x22558f4dbe3d
13: 0x22558f4918d5
14: 0x22558f7d19b2

However it works perfectly using memory-streams, which utilizes readable-streams as opposed to core Node streams:

import { WritableStream } from 'memory-streams'

const writable = new WritableStream()
const out = new PDFStreamForResponse(writable) 
const writer = createWriterToModify(new PDFRStreamForBuffer(inputBuffer), out)

PDFStreamForResponse only seems to utilize the write method, so I'm not sure why there would be an incompatibility, but for now I would at least mention in the docs to avoid using Writable.

flowHater commented 5 years ago

https://nodejs.org/api/stream.html#stream_stream

import { Writable } from 'streams'

Should be:

import { Writable } from 'stream'

But i can confirm this issue, I got the same

mohammedabualsoud commented 4 years ago

It does exists, this is really bad, you must re-create the stream using the Hummus provided one. I coudl't find a way to detect the type of the stream if it's file system, buffer, or it's a Response stream.