#1 request(...).on is not a function

Closed
opened 3 months ago by jslegers · 2 comments

I tried just replacing request with @root/request in html2canvas-proxy, but it seems like @root/request isn't feature-complete enough to allow this.

This library uses request(...).on, which doesn't seem to be supported by @root/request.

Full code :

const express = require('express');
const url = require('url');
const cors = require('cors');
const request = require('@root/request');

function validUrl(req, res, next) {
    if (!req.query.url) {
        next(new Error('No url specified'));
    } else if (typeof req.query.url !== 'string' || url.parse(req.query.url).host === null) {
        next(new Error(`Invalid url specified: ${req.query.url}`));
    } else {
        next();
    }
}

module.exports = () => {
    const app = express.Router();
    app.get('/', cors(), validUrl, (req, res, next) => {
        switch (req.query.responseType) {
    case 'blob':
        req.pipe(request(req.query.url).on('error', next)).pipe(res);
        break;
    case 'text':
    default:
        request({url: req.query.url, encoding: 'binary'}, (error, response, body) => {
            if (error) {
                return next(error);
            }
            res.send(
            `data:${response.headers['content-type']};base64,${Buffer.from(
                body,
                'binary'
            ).toString('base64')}`
        );
    });
    }
});

    return app;
};


I tried just replacing `request` with `@root/request` in [`html2canvas-proxy`](https://www.npmjs.com/package/html2canvas-proxy), but it seems like `@root/request` isn't feature-complete enough to allow this. This library uses `request(...).on`, which doesn't seem to be supported by `@root/request`. Full code : ````js const express = require('express'); const url = require('url'); const cors = require('cors'); const request = require('@root/request'); function validUrl(req, res, next) { if (!req.query.url) { next(new Error('No url specified')); } else if (typeof req.query.url !== 'string' || url.parse(req.query.url).host === null) { next(new Error(`Invalid url specified: ${req.query.url}`)); } else { next(); } } module.exports = () => { const app = express.Router(); app.get('/', cors(), validUrl, (req, res, next) => { switch (req.query.responseType) { case 'blob': req.pipe(request(req.query.url).on('error', next)).pipe(res); break; case 'text': default: request({url: req.query.url, encoding: 'binary'}, (error, response, body) => { if (error) { return next(error); } res.send( `data:${response.headers['content-type']};base64,${Buffer.from( body, 'binary' ).toString('base64')}` ); }); } }); return app; }; ````
jslegers commented 3 months ago
Poster

I tried just dropping the .on('error', next) part, since all it seemed to be doing, was error handling anyway...

Unfortunately, this just produced a different error :

_stream_readable.js:832
      dest.emit('unpipe', this, unpipeInfo);
           ^

TypeError: dest.emit is not a function
    at IncomingMessage.Readable.unpipe (_stream_readable.js:832:12)
    at unpipe (D:\projects\Adshot Development\src\core-adshot\node_modules\unpipe\index.js:47:12)
    at send (D:\projects\Adshot Development\src\core-adshot\node_modules\finalhandler\index.js:306:3)
    at D:\projects\Adshot Development\src\core-adshot\node_modules\finalhandler\index.js:133:5
    at D:\projects\Adshot Development\src\core-adshot\node_modules\express\lib\router\index.js:635:15
    at next (D:\projects\Adshot Development\src\core-adshot\node_modules\express\lib\router\index.js:260:14)
    at Layer.handle_error (D:\projects\Adshot Development\src\core-adshot\node_modules\express\lib\router\layer.js:67:12)
    at trim_prefix (D:\projects\Adshot Development\src\core-adshot\node_modules\express\lib\router\index.js:315:13)
    at D:\projects\Adshot Development\src\core-adshot\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (D:\projects\Adshot Development\src\core-adshot\node_modules\express\lib\router\index.js:335:12)
    at next (D:\projects\Adshot Development\src\core-adshot\node_modules\express\lib\router\index.js:275:10)
    at Layer.handle_error (D:\projects\Adshot Development\src\core-adshot\node_modules\express\lib\router\layer.js:67:12)
    at trim_prefix (D:\projects\Adshot Development\src\core-adshot\node_modules\express\lib\router\index.js:315:13)
    at D:\projects\Adshot Development\src\core-adshot\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (D:\projects\Adshot Development\src\core-adshot\node_modules\express\lib\router\index.js:335:12)
    at Immediate.next (D:\projects\Adshot Development\src\core-adshot\node_modules\express\lib\router\index.js:275:10)
I tried just dropping the `.on('error', next)` part, since all it seemed to be doing, was error handling anyway... Unfortunately, this just produced a different error : ```` _stream_readable.js:832 dest.emit('unpipe', this, unpipeInfo); ^ TypeError: dest.emit is not a function at IncomingMessage.Readable.unpipe (_stream_readable.js:832:12) at unpipe (D:\projects\Adshot Development\src\core-adshot\node_modules\unpipe\index.js:47:12) at send (D:\projects\Adshot Development\src\core-adshot\node_modules\finalhandler\index.js:306:3) at D:\projects\Adshot Development\src\core-adshot\node_modules\finalhandler\index.js:133:5 at D:\projects\Adshot Development\src\core-adshot\node_modules\express\lib\router\index.js:635:15 at next (D:\projects\Adshot Development\src\core-adshot\node_modules\express\lib\router\index.js:260:14) at Layer.handle_error (D:\projects\Adshot Development\src\core-adshot\node_modules\express\lib\router\layer.js:67:12) at trim_prefix (D:\projects\Adshot Development\src\core-adshot\node_modules\express\lib\router\index.js:315:13) at D:\projects\Adshot Development\src\core-adshot\node_modules\express\lib\router\index.js:284:7 at Function.process_params (D:\projects\Adshot Development\src\core-adshot\node_modules\express\lib\router\index.js:335:12) at next (D:\projects\Adshot Development\src\core-adshot\node_modules\express\lib\router\index.js:275:10) at Layer.handle_error (D:\projects\Adshot Development\src\core-adshot\node_modules\express\lib\router\layer.js:67:12) at trim_prefix (D:\projects\Adshot Development\src\core-adshot\node_modules\express\lib\router\index.js:315:13) at D:\projects\Adshot Development\src\core-adshot\node_modules\express\lib\router\index.js:284:7 at Function.process_params (D:\projects\Adshot Development\src\core-adshot\node_modules\express\lib\router\index.js:335:12) at Immediate.next (D:\projects\Adshot Development\src\core-adshot\node_modules\express\lib\router\index.js:275:10) ````
coolaj86 commented 3 months ago
Owner
Issues at https://git.rootprojects.org/root/request.js/issues/1
Sign in to join this conversation.
No Label
No Milestone
No Assignees
2 Participants
Due Date

No due date set.

Dependencies

This issue currently doesn't have any dependencies.

Loading…
Cancel
Save
There is no content yet.