jsdom icon indicating copy to clipboard operation
jsdom copied to clipboard

"Error: error while reading from input stream" when img has an SVG

Open molant opened this issue 8 years ago • 6 comments

Basic info:

  • Node.js version: 8.9.3
  • jsdom version: 11.5.1

Minimal reproduction case

Code is available in https://github.com/molant/svg-tests

// Server configuration

const html = `<img src="/image.svg" />`;
const svg = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="400" height="180">
<rect x="50" y="20" rx="20" ry="20" width="150" height="150" style="fill:red;stroke: black;stroke-width:5;opacity:0.5" />
</svg>`;

const express = require('express');
const app = express();

app.get('/', function (req, res) {
    console.log('Server: HTML requested');
    res.send(html);
});

app.get('/image.svg', function (req, res) {
    console.log('Server: image requested');
    res.setHeader('Content-Type', 'image/svg+xml');
    res.send(svg);
    res.end();
});

// jsdom API tests

function newApi() {
    const jsdom = require('jsdom');
    const { JSDOM } = jsdom;
    console.log('---------------');
    console.log('Testing new API\n');

    JSDOM.fromURL('http://localhost:3000', {
        resources: 'usable'
    });
}

app.listen(3000, newApi);

The following error appears in the console (in bold what I think is relevant):

Error: Could not load img: "http://localhost:3000/image.svg" at C:\projects\svg-tests\node_modules\jsdom\lib\jsdom\browser\resource-loader.js:44:23 at Object.check (C:\projects\svg-tests\node_modules\jsdom\lib\jsdom\living\nodes\Document-impl.js:91:11) at C:\projects\svg-tests\node_modules\jsdom\lib\jsdom\living\nodes\Document-impl.js:110:12 at wrappedEnqueued (C:\projects\svg-tests\node_modules\jsdom\lib\jsdom\browser\resource-loader.js:255:16) at Request.request [as _callback] (C:\projects\svg-tests\node_modules\jsdom\lib\jsdom\browser\resource-loader.js:205:9) at Request.self.callback (C:\projects\svg-tests\node_modules\request\request.js:186:22) at emitTwo (events.js:126:13) at Request.emit (events.js:214:7) at Request. (C:\projects\svg-tests\node_modules\request\request.js:1163:10) at emitOne (events.js:116:13) Error: Error: error while reading from input stream at HTMLImageElementImpl.resourceLoader.load (C:\projects\svg-tests\node_modules\jsdom\lib\jsdom\living\nodes\HTMLImageElement-impl.js:29:19) at C:\projects\svg-tests\node_modules\jsdom\lib\jsdom\browser\resource-loader.js:31:22 at Object.check (C:\projects\svg-tests\node_modules\jsdom\lib\jsdom\living\nodes\Document-impl.js:91:11) at C:\projects\svg-tests\node_modules\jsdom\lib\jsdom\living\nodes\Document-impl.js:110:12 at wrappedEnqueued (C:\projects\svg-tests\node_modules\jsdom\lib\jsdom\browser\resource-loader.js:255:16) at Request.request [as _callback] (C:\projects\svg-tests\node_modules\jsdom\lib\jsdom\browser\resource-loader.js:205:9) at Request.self.callback (C:\projects\svg-tests\node_modules\request\request.js:186:22) at emitTwo (events.js:126:13) at Request.emit (events.js:214:7) at Request. (C:\projects\svg-tests\node_modules\request\request.js:1163:10)

How does similar code behave in browsers?

  • The same page loads fine in a browser.
  • Also no errors are reported when using jsdom's old API.

molant avatar Dec 12 '17 18:12 molant

Very interesting; this really does seem like it should work.

Can you try just using request directly and doing request("http://localhost:3000/image.svg")?

domenic avatar Dec 12 '17 18:12 domenic

I've updated the code in the repo with that. Good output for request (I do console.log of the body), bad for jsdom:

Server: image requested
---------------
Testing request

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="400" height="180">
<rect x="50" y="20" rx="20" ry="20" width="150" height="150" style="fill:red;stroke: black;stroke-width:5;opacity:0.5" />
</svg>
---------------
Testing new API

Server: HTML requested
Server: image requested
Error: Could not load img: "http://localhost:3000/image.svg"
    at C:\projects\svg-tests\node_modules\jsdom\lib\jsdom\browser\resource-loader.js:44:23
    at Object.check (C:\projects\svg-tests\node_modules\jsdom\lib\jsdom\living\nodes\Document-impl.js:91:11)
    at C:\projects\svg-tests\node_modules\jsdom\lib\jsdom\living\nodes\Document-impl.js:110:12
    at wrappedEnqueued (C:\projects\svg-tests\node_modules\jsdom\lib\jsdom\browser\resource-loader.js:255:16)
    at Request.request [as _callback] (C:\projects\svg-tests\node_modules\jsdom\lib\jsdom\browser\resource-loader.js:205:9)
    at Request.self.callback (C:\projects\svg-tests\node_modules\request\request.js:186:22)
    at emitTwo (events.js:126:13)
    at Request.emit (events.js:214:7)
    at Request.<anonymous> (C:\projects\svg-tests\node_modules\request\request.js:1163:10)
    at emitOne (events.js:116:13) Error: Error: error while reading from input stream
    at HTMLImageElementImpl.resourceLoader.load (C:\projects\svg-tests\node_modules\jsdom\lib\jsdom\living\nodes\HTMLImageElement-impl.js:29:19)
    at C:\projects\svg-tests\node_modules\jsdom\lib\jsdom\browser\resource-loader.js:31:22
    at Object.check (C:\projects\svg-tests\node_modules\jsdom\lib\jsdom\living\nodes\Document-impl.js:91:11)
    at C:\projects\svg-tests\node_modules\jsdom\lib\jsdom\living\nodes\Document-impl.js:110:12
    at wrappedEnqueued (C:\projects\svg-tests\node_modules\jsdom\lib\jsdom\browser\resource-loader.js:255:16)
    at Request.request [as _callback] (C:\projects\svg-tests\node_modules\jsdom\lib\jsdom\browser\resource-loader.js:205:9)
    at Request.self.callback (C:\projects\svg-tests\node_modules\request\request.js:186:22)
    at emitTwo (events.js:126:13)
    at Request.emit (events.js:214:7)
    at Request.<anonymous> (C:\projects\svg-tests\node_modules\request\request.js:1163:10)

molant avatar Dec 12 '17 19:12 molant

OK, nice. That definitely makes it easier for us to take time tracking this down; we'll just try to figure out what jsdom is doing differently and fix it.

domenic avatar Dec 12 '17 19:12 domenic

Pretty sure this is in fact coming from node-canvas, which doesn't support SVG through Image. This has in fact been fixed in node-canvas v2.x in https://github.com/Automattic/node-canvas/pull/811 provided that librsvg is installed, but we don't support node-canvas v2.x yet (#1964).

TimothyGu avatar Dec 13 '17 08:12 TimothyGu

is that fixed? I facing this issue with canvas v2.3.1 and jsdom v13.2.0

Dineshagrawal85 avatar Jan 27 '19 06:01 Dineshagrawal85

The prebuilt binaries should have svg support already: https://github.com/Automattic/node-canvas/issues/1297

Although there are some issues in that report for node-canvas, svg and windows support.

molant avatar Jan 27 '19 17:01 molant