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

request: Replace deprecated request module with net.request. #993

Merged
merged 4 commits into from
Jul 14, 2020

Conversation

manavmehta
Copy link
Collaborator

@manavmehta manavmehta commented Jun 27, 2020

Any background context you want to provide?
Fixes: #886

You have tested this PR on:

  • macOS Catalina 10.15.5
  • Windows
  • Linux/Ubuntu

Checklist

  • domain-util
  • reconnect-util
  • linuxupdater
  • check add-new-server
  • check UA on test server

We may think of a more appropriate and suitable name of the new added request.ts

@manavmehta
Copy link
Collaborator Author

Also, for the record, remote is now available as a package (on it's way to be moved out from electron core)

@andersk
Copy link
Member

andersk commented Jun 29, 2020

Let’s move discussion about remote to #915.

(But to answer your questions in short: yes, we should eliminate remote. Yes, we should eliminate it completely. No, there are no cases of it that should not be eliminated. It’s being moved from core to a package because it should not be used, not because it should be used. In case you haven’t read this page, which I linked from #915 and is also linked from the Electron bug and the @electron/remote documentation, see Electron’s ‘remote’ module considered harmful. I think we’re all on the same page here. Good? Good.)

@manavmehta manavmehta marked this pull request as ready for review July 1, 2020 19:45
@abhigyank
Copy link
Collaborator

abhigyank commented Jul 4, 2020

The original functionality remains intact for me after the migration. Further, now we don't need certificate-util, I tested adding certificates to Chrome store and using certutilon Linux, and server gets added without a hitch.
All in all LGTM.
One minor issue, in case of certificate error on server addition, we show two dialog box errors - one from electron.net and one we create. But I think that's a bug in how we handle certificate errors, could be a follow-up PR.

@manavmehta
Copy link
Collaborator Author

manavmehta commented Jul 4, 2020

Going with this approach, I think it will handle two dialog boxes.

app/main/request.ts Outdated Show resolved Hide resolved
@abhigyank
Copy link
Collaborator

Let's keep getProxy for now? I feel it completes the the utility of a file like proxy-util though unused. Could be wrong here.
Removing request-util is correct.

@andersk
Copy link
Member

andersk commented Jul 5, 2020

Disagree. There’s no point in keeping around code that won’t be used with electron.net. If it burns, set it on fire.

@abhigyank
Copy link
Collaborator

I agree it won't be used with electron.net, however I disagree that keeping it could be worse. I disagree also that we should fire to anything that burns, houses, cars everything burns, I don't suppose we should set fire to them. Just because anything burns, we shouldn't burn them.

@andersk
Copy link
Member

andersk commented Jul 5, 2020

Uh…unused code is not a house or a car. Unused code is a readability complication, a maintenance burden, a potential attack surface, and an all around waste of time and space. There are rare cases where unused code is useful for API symmetry or to help the type checker or something, but this isn’t one of them. This code should just go away.

@abhigyank
Copy link
Collaborator

There are rare cases where unused code is useful for API symmetry.

Yeah I was going for this, so that a util could have a set and get function. Since, we don't have it as an API explicitly, we could let it go then. One can always re-add it later if required. We can keep the commit that removes it :)

@andersk
Copy link
Member

andersk commented Jul 5, 2020

We don’t have a set function and a get function here. A get function should return exactly the data that you put into a set function—symmetry. This getProxy function, on the other hand, returns the result of some weird string-mangling logic and maybe also sets an environment variable as a side effect. Its only purpose was to support the specific library that we’re now removing.

@abhigyank
Copy link
Collaborator

@andersk Can you review this for merging?

app/main/linuxupdater.ts Outdated Show resolved Hide resolved
session: Session
};

return new Promise((resolve, reject) => {
Copy link
Member

@andersk andersk Jul 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s try to extract the raw Promise logic into minimal shared helper functions and lean on async/await as much as possible. With a small fix to the Electron typings in typings.d.ts (justified by the documentation: “IncomingMessage implements the Readable Stream interface”):

declare module 'electron' {
    interface IncomingMessage extends NodeJS.ReadableStream {}
}

we can do this:

import getStream from 'get-stream';

async function fetchResponse(request: ClientRequest): Promise<IncomingMessage> {
    return new Promise((resolve, reject) => {
        request.on("response", resolve);
        request.on("abort", reject);
        request.on("error", reject);
        request.end();
    });
}

export async function _getServerSettings(): Promise<ServerConf> {
    const response = await fetchResponse(net.request({
        url: domain + '/api/v1/server_settings',
        session,
    }));
    if (response.statusCode !== 200) {
        throw response;  // maybe we want this to be an actual Error?
    }
    const {realm_name, realm_uri, realm_icon} = JSON.parse(await getStream.buffer(response));
    
}

app/main/request.ts Outdated Show resolved Hide resolved
return `${dir}/${hash >>> 0}${extension}`;
};

export const _getServerSettings = async (domain: string, Session: Electron.session): Promise<ServerConf> => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don’t capitalize the variable Session.

app/main/request.ts Outdated Show resolved Hide resolved
app/renderer/js/utils/reconnect-util.ts Outdated Show resolved Hide resolved
app/main/request.ts Outdated Show resolved Hide resolved
Signed-off-by: Anders Kaseorg <anders@zulip.com>
@andersk
Copy link
Member

andersk commented Jul 10, 2020

This also seems to trigger an extremely unfortunate bug where one of the renderer processes goes into a 100% CPU loop that eventually consumes all available memory. I’m trying to debug this…

manavmehta and others added 3 commits July 9, 2020 21:33
The request module has been deprecated by electron and this commit replaces the same with net.request.
The net api is a main process api and thus the functions have been moved for convention.

Fixes: zulip#886

Co-authored-by: Anders Kaseorg <anders@zulip.com>
Signed-off-by: Anders Kaseorg <anders@zulip.com>
net.request handles proxy by itself and moving away from request module makes request-util unused.
The function is now not used upon migration to electron's native net.request and thus is being removed
@andersk
Copy link
Member

andersk commented Jul 10, 2020

The bug had something to do with trying to serialize an entire ReconnectUtil over the IPC channel. We only need its url, so yeah, let’s not do that.

@manavmehta
Copy link
Collaborator Author

Thanks @andersk. Though,

one of the renderer processes goes into a 100% CPU loop that eventually consumes all available memory.

I didn't get this totally. Can you please explain it to me?

@akashnimare akashnimare changed the title request: Replace deprecated request module with net.request request: Replace deprecated request module with net.request. Jul 14, 2020
@akashnimare akashnimare merged commit 14a1f5d into zulip:master Jul 14, 2020
@akashnimare
Copy link
Member

Thanks, everyone 🤝

@manavmehta manavmehta deleted the net branch July 14, 2020 10:26
@manavmehta manavmehta restored the net branch August 27, 2020 12:46
@manavmehta manavmehta deleted the net branch August 27, 2020 12:47
andersk added a commit to andersk/zulip-desktop that referenced this pull request Sep 11, 2020
Version 5.4.0 and later use electron.net for all network
requests (zulip#993), so custom certificates can now be configured in the
same system certificate store that Chrome uses.

https://zulip.com/help/custom-certificates#desktop

Signed-off-by: Anders Kaseorg <anders@zulip.com>
andersk added a commit to andersk/zulip-desktop that referenced this pull request Sep 11, 2020
Version 5.4.0 and later uses electron.net for all network
requests (zulip#993), so custom certificates can now be configured in the
same system certificate store that Chrome uses.

https://zulip.com/help/custom-certificates#desktop

Signed-off-by: Anders Kaseorg <anders@zulip.com>
andersk added a commit to andersk/zulip-desktop that referenced this pull request Sep 12, 2020
Version 5.4.0 and later uses electron.net for all network
requests (zulip#993), so custom certificates can now be configured in the
same system certificate store that Chrome uses.

https://zulip.com/help/custom-certificates#desktop

Signed-off-by: Anders Kaseorg <anders@zulip.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove deprecated request module
5 participants