unshiftio / url-parse

Small footprint URL parser that works seamlessly across Node.js and browser environments.
http://unshift.io
MIT License
1.03k stars 104 forks source link

Location is not defined #225

Closed HariniReddyP closed 2 years ago

HariniReddyP commented 2 years ago

TypeError: Cannot read properties of null (reading 'location') at lolcation (/url-parse/index.js:76:28) at new Url (/url-parse/index.js:267:14) at Url (/url-parse/index.js:240:12)

when globalVar is {}

lpinca commented 2 years ago

If you get that error, then one of window, global, or self is null in your environment.

lpinca commented 2 years ago

Can you provide more details? Where and how are you using url-parse? How can we reproduce the issue?

HariniReddyP commented 2 years ago

We are trying to test the "parse" function using chai node module, where we are just executing function using a hardcoded value 'http://localhost:1234/' which is expecting a value , but it throws the below error TypeError: Cannot read properties of null (reading 'location'). You have to use chai node module expect function to reproduce the issue.

HariniReddyP commented 2 years ago

@lpinca Even if the globalVar is an empty JSON({}), it is trying to read location which will error out for sure. That is the issue i am facing currently.

lpinca commented 2 years ago

See https://github.com/unshiftio/url-parse/blob/f7774f608418b59a43ad1816c954654fdc8b1248/index.js#L71-L74

The error is thrown only if one of window, global, or self is null which is weird.

HariniReddyP commented 2 years ago

@lpinca Can we add a condition check for null as well in that scenario?

lpinca commented 2 years ago

Yes, but why is it required? Those should not be null. That is why I asked if you used the library in a special environment.

HariniReddyP commented 2 years ago

@lpinca I have global object available but the window object is null as i am testing the code using chai node module

lpinca commented 2 years ago

It is not chai. The following example does not throw any error

$ cat index.js
const parse = require('url-parse');
const { assert } = require('chai');

assert.deepEqual(
  { ...parse('http://localhost:1234/') },
  {
    auth: '',
    hash: '',
    host: 'localhost:1234',
    hostname: 'localhost',
    href: 'http://localhost:1234/',
    origin: 'http://localhost:1234',
    password: '',
    pathname: '/',
    port: '1234',
    protocol: 'http:',
    query: '',
    slashes: true,
    username: ''
  }
);
$ node index.js
HariniReddyP commented 2 years ago

@lpinca let me check in the code where window object is made to null. Thanks for the help.

HariniReddyP commented 2 years ago

@lpinca Please go ahead and close the issue.