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

"toString()" failed Page.printToPDF #317

Closed
AmarildoK opened this issue Jan 3, 2018 · 6 comments
Closed

"toString()" failed Page.printToPDF #317

AmarildoK opened this issue Jan 3, 2018 · 6 comments

Comments

@AmarildoK
Copy link

AmarildoK commented Jan 3, 2018

Component Version
Operating system Ubuntu 16.04 LTS
Node.js 6.10.0
Chrome/Chromium/... 63.0.3239.108
chrome-remote-interface 0.24.5

Is Chrome running in a container? NO

Hi,
First of all let me thank you for your great work. This package has been a lot of help, but
recently I got this error ' "toString()" failed'.

Here is the full error stack trace.

buffer.js:509
    throw new Error('"toString()" failed');
    ^

Error: "toString()" failed
    at Buffer.toString (buffer.js:509:11)
    at Receiver.dataMessage (/home/amarildo/Documents/projects/generate-pdf/node_modules/ws/lib/Receiver.js:385:28)
    at extension.decompress (/home/amarildo/Documents/projects/generate-pdf/node_modules/ws/lib/Receiver.js:354:40)
    at _inflate.flush (/home/amarildo/Documents/projects/generate-pdf/node_modules/ws/lib/PerMessageDeflate.js:279:12)
    at afterWrite (_stream_writable.js:383:3)
    at onwrite (_stream_writable.js:374:7)
    at afterTransform (_stream_transform.js:79:3)
    at TransformState.afterTransform (_stream_transform.js:54:12)
    at Zlib.callback (zlib.js:625:5)

This Error happens when I try to generate a pdf from a huge html file(a lot of high res pic in it) with Page.printToPDF(someOptions).

The code itself is straightforward, go to page, and generatePdf then save it to s3.
A link to the code the error starts

I can only think of the limit of max buffer size being hit and that being the reason the code throws an error.
Have you ever encounter this error before?

@cyrus-and
Copy link
Owner

Thanks! :)

What version of ws are you using?

@AmarildoK
Copy link
Author

@cyrus-and
The version currently installed is 2.0.3

@cyrus-and
Copy link
Owner

cyrus-and commented Jan 3, 2018

I can only think of the limit of max buffer size being hit and that being the reason the code throws an error.

Well yes, it looks like it (see nodejs/node#3175). Tl;dr:

$ node -p 'new Buffer((1 << 28) - 16 + 1).toString().length'
buffer.js:571
    return this.utf8Slice(0, this.length);
                ^

Error: "toString()" failed
    at Buffer.toString (buffer.js:571:17)
    at [eval]:1:32
    at ContextifyScript.Script.runInThisContext (vm.js:44:33)
    at Object.runInThisContext (vm.js:116:38)
    at Object.<anonymous> ([eval]-wrapper:6:22)
    at Module._compile (module.js:569:30)
    at evalScript (bootstrap_node.js:432:27)
    at startup (bootstrap_node.js:139:9)
    at bootstrap_node.js:575:3

My suggestion is to send out the buffer in chunks.

@AmarildoK
Copy link
Author

Hmmm,
But the error happens in the Page.printToPdf(), how can I change the inner code?

@cyrus-and
Copy link
Owner

Sorry, my bad, I was thinking about something else; ignore that phrase.

Let me look into this.

@cyrus-and
Copy link
Owner

OK, so this is ultimately a Node.js/V8 limitation of the String object, the latest Node.js version (9.3.0) seems to work fine.

I suspect though that a different implementation of ws could make it work even with LTS Node.js, but that would require to switch to Buffer instead of String to manage big text messages; probably not worth it, but you could try to file an issue for that project anyway.

Here's a minimal way to reproduce this using python-websocket-server and wscat:

from websocket_server import WebsocketServer

s = 'x' * ((1 << 28) - 16 + 1)

def new_client(client, server):
    server.send_message(client, s)

server = WebsocketServer(1234)
server.set_fn_new_client(new_client)
server.run_forever()

Start the above then wscat -c localhost:1234.

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

No branches or pull requests

2 participants