Rob--W / cors-anywhere

CORS Anywhere is a NodeJS reverse proxy which adds CORS headers to the proxied request.
MIT License
8.5k stars 5.92k forks source link

Does not work in Deno (TypeError on every request) #449

Open Pyroglyph opened 1 year ago

Pyroglyph commented 1 year ago

Simple usage does not work at all in Deno. Every request ends in the same TypeError.

Example using PokeAPI:

// cors.ts
import corsProxy from 'npm:cors-anywhere';
corsProxy.createServer().listen(8080, 'localhost', () => {
    console.log('Running CORS Anywhere');
    fetch('http://localhost:8080/pokeapi.co:443/api/v2/');
});

Result:

❯ deno run --allow-net cors.ts
Running CORS Anywhere
TypeError: Cannot read properties of undefined (reading 'encrypted')
    at ServerImpl.<anonymous> (file:///home/pyro/.cache/deno/npm/registry.npmjs.org/cors-anywhere/0.4.4/lib/cors-anywhere.js:384:47)
    at ServerImpl.emit (https://deno.land/std@0.177.0/node/_events.mjs:379:28)
    at https://deno.land/std@0.177.0/node/http.ts:634:16
    at new Promise (<anonymous>)
    at handler (https://deno.land/std@0.177.0/node/http.ts:632:16)
    at Object.serve (internal:ext/flash/01_http.js:572:24)
    at async Promise.all (index 0)
    at async serve (internal:ext/flash/01_http.js:688:7)

Not even the sample URLs given in the readme work. Navigating to / works if you launch Deno with --allow-read, and so does /iscorsneeded. Nothing else works though.

Pyroglyph commented 1 year ago

After more investigating, it appears that the connection property is not present on req when this is run through Deno.

Simply changing this line

var isRequestedOverHttps = req.connection.encrypted || /^\s*https/.test(req.headers['x-forwarded-proto']);

to this

var isRequestedOverHttps = req.connection?.encrypted || /^\s*https/.test(req.headers['x-forwarded-proto']);
                                         ^

should solve the issue.