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

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

Open
Pyroglyph opened this issue Feb 9, 2023 · 1 comment
Open

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

Pyroglyph opened this issue Feb 9, 2023 · 1 comment

Comments

@Pyroglyph
Copy link

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
Copy link
Author

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.

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

No branches or pull requests

1 participant