-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Replace url parse by url class #1147
Conversation
Ps. the failure on 10.x is due to some issue with the websocket implementation and has imho no relation to this PR. |
afc3930
to
ac98686
Compare
I have rebased to current master and resolved the merge conflict. However master still fails on node v10. |
@YoDaMa can you please review ? |
What did you mean by
can we use spread syntax? Or do we actually have to use destructuring? |
@seriousme we need to do a rollback... so you'll have to rebase once more :) also there's a conflict in ws.js... one moment. |
You can just do an Object.assign on the URL object,e.g.: > u=new URL("https://www.example.com:8080/")
URL {
href: 'https://www.example.com:8080/',
origin: 'https://www.example.com:8080',
protocol: 'https:',
username: '',
password: '',
host: 'www.example.com:8080',
hostname: 'www.example.com',
port: '8080',
pathname: '/',
search: '',
searchParams: URLSearchParams {},
hash: '' }
> a= Object.assign({},u)
{ [Symbol(context)]:
URLContext {
flags: 400,
scheme: 'https:',
username: '',
password: '',
host: 'www.example.com',
port: 8080,
path: [ '' ],
query: null,
fragment: null },
[Symbol(query)]: URLSearchParams {} } And on a more theoretical note: you should not copy the internal structure of a class but only use its accessors ;-) Kind regards, |
Bit of a bummer :-( Kind regards, |
Maybe it would be prudent to look at how the parsed url is actually utilized. |
That would be a good idea, however the opts object is scattered around the code base. If you really want to go that route then I'd advise a more serious refactoring operation. Kind regards, |
opts.port = Number(parsed.port) || null | ||
opts.username = parsed.username | ||
opts.password = parsed.password | ||
opts.searchParams = parsed.searchParams |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are you sure this is all the info we could possibly need from the URL? I'm worried something might be missed without just the extend.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am pretty sure we don't need more. And the tests agreed with me on that ;-)
This is what url.parse returns:
> url.parse("https://user:[email protected]:8080/?a=b")
Url {
protocol: 'https:',
slashes: true,
auth: 'user:password',
host: 'www.example.com:8080',
port: '8080',
hostname: 'www.example.com',
hash: null,
search: '?a=b',
query: 'a=b',
pathname: '/',
path: '/?a=b',
href: 'https://user:[email protected]:8080/?a=b'
Kind regards,
Hans
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW: @nosovk already approved on September 2 so its not just me ;-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should actually be opts.host = parsed.hostname
This project uses host
as the name for hostname
(just domain, without port)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Except for the 11 locations where opts.hostname is being used ;-)
https://github.com/mqttjs/MQTT.js/search?q=opts.hostname
and currently (without my PR) the results from url.parse are copied into opts.
So currently:
opts.host
is domain with port (www.example.com:8080)opts.hostname
is domain without port (www.example.com)
The comments in type definitions seem to be a bit ahead of the code ;-)
But anyway:
For me its just a hobby, and I saw the issue and thought I'd help out.
If you all feel it can be done way better than I did, feel free!
(its only a few lines of code and from my PR you know where to look ;-))
That would save me the effort of rebasing (again) as well.
If you still want me to rebase because you are all way too busy, just let me know when its safe to do so I don't need to rebase a third time ;-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
appreciate your contributions just doing due diligence :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, in that case: #1148 is also still open ;-)
It would convenient for me if that could be either merged or fixed in some other way better way :-)
(e.g. generate fresh certs if not available etc )
This is approximately what // set/used in connect/index.js
interface Opts {
protocol: string; //protocol without ":"
hostname: string; //actually `host` (doesn't include port)
port: string | number; //sometimes parsed as Number
path: string; //url path, like /a/b
// currently missing: search: string, // ?a=a&b=b
query?: { clientId?: string }; // comes from url
cert: unknown;
key: unknown;
clean?: boolean; // default = true
servers: { host: string; port: string | number; protocol?: Protocol }[];
}
// in client.js
var defaultConnectOptions = {
keepalive: 60,
reschedulePings: true,
protocolId: "MQTT",
protocolVersion: 4,
reconnectPeriod: 1000,
connectTimeout: 30 * 1000,
clean: true,
resubscribe: true,
};
// debug printed
interface Options {
protocol: string;
protocolVersion: 4 | 5;
username: string;
keepalive: number;
reconnectPeriod: number; // default 30s
rejectUnauthorized: unknown;
}
// other
interface Options {
clientId: string;
customHandleAcks: Function;
outgoingStore: Store;
incomingStore: Store;
queueQoSZero: boolean; // default = true
}
// this.options.customHandleAcks =
// (options.protocolVersion === 5 && options.customHandleAcks)
// ? options.customHandleAcks
// : function () { arguments[3](0) }
// passed to mqtt-packet/parser.js
interface ParserOpts {
protocolVersion: 4 | 5;
}
type Properties = unknown[] | Record<any,unknown>;
// passed to mqtt-packet/writeToStream.js#connect
// this packet has options set to it as prototype, so some options will be available that way
interface ConnectPacket {
protocolId: "MQTT" | unknown,
protocolVersion?: 3 | 4 | 5, //default 4
will: {
topic: string,
payload: string | Buffer,
retain?: boolean,
qos?: QoS,
properties: Properties, //only if mqtt5
},
clean: boolean,
keepalive?: number, //default 0
clientId?: string, //default ''
username: string,
password: string,
properties: Properties, //only if mqtt5
}
interface PacketOpts {
protocolVersion: 3 | 4 | 5; //default 4
} |
ok @seriousme can you rebase and figure out what the conflict is with ws.js? |
ac98686
to
6aef40f
Compare
Ok, I have rebased and squashed into a single commit. The 10.x fail looks unrelated to this PR and the 12.x and 14.x succeed. Kind regards, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
This fixes #1130 by replacing url.parse by the URL class as advised by the Node documentation on the subject:
Kind regards,
Hans