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

_tls2.default.connect is not a function #171

Open
igoventura opened this issue Feb 9, 2018 · 24 comments
Open

_tls2.default.connect is not a function #171

igoventura opened this issue Feb 9, 2018 · 24 comments
Labels

Comments

@igoventura
Copy link

I'm trying to use this in my angular+electron app and getting this error:

[DEBUG][2018-02-09T16:53:31.517Z][1][igoventura@outlook.com][imap-mail.outlook.com] Connecting to imap-mail.outlook.com : 993 polyfills.bundle.js:3186 [DEBUG][2018-02-09T16:53:31.521Z][1][igoventura@outlook.com][imap-mail.outlook.com] Entering state: 1 polyfills.bundle.js:3186 [ERROR][2018-02-09T16:53:31.534Z][1][igoventura@outlook.com][imap-mail.outlook.com] Could not connect to server TypeError: _tls2.default.connect is not a function webpackJsonp../node_modules/zone.js/dist/zone-mix.js.ZoneDelegate.invoke @ polyfills.bundle.js:3186 webpackJsonp../node_modules/zone.js/dist/zone-mix.js.Zone.run @ polyfills.bundle.js:2936 console.(anonymous function) @ polyfills.bundle.js:5867 log @ vendor.bundle.js:98237 error @ vendor.bundle.js:98245 error @ vendor.bundle.js:96366 (anonymous) @ vendor.bundle.js:95481 step @ vendor.bundle.js:95368 (anonymous) @ vendor.bundle.js:95368 webpackJsonp../node_modules/zone.js/dist/zone-mix.js.ZoneDelegate.invoke @ polyfills.bundle.js:3186 onInvoke @ vendor.bundle.js:49849 webpackJsonp../node_modules/zone.js/dist/zone-mix.js.ZoneDelegate.invoke @ polyfills.bundle.js:3185 webpackJsonp../node_modules/zone.js/dist/zone-mix.js.Zone.run @ polyfills.bundle.js:2936 (anonymous) @ polyfills.bundle.js:3638 webpackJsonp../node_modules/zone.js/dist/zone-mix.js.ZoneDelegate.invokeTask @ polyfills.bundle.js:3219 onInvokeTask @ vendor.bundle.js:49840 webpackJsonp../node_modules/zone.js/dist/zone-mix.js.ZoneDelegate.invokeTask @ polyfills.bundle.js:3218 webpackJsonp../node_modules/zone.js/dist/zone-mix.js.Zone.runTask @ polyfills.bundle.js:2986 drainMicroTaskQueue @ polyfills.bundle.js:3396 polyfills.bundle.js:3186 [DEBUG][2018-02-09T16:53:31.536Z][1][igoventura@outlook.com][imap-mail.outlook.com] Entering state: 5 polyfills.bundle.js:3186 [DEBUG][2018-02-09T16:53:31.537Z][1][igoventura@outlook.com][imap-mail.outlook.com] Closing connection... polyfills.bundle.js:5864 ERROR Error: Uncaught (in promise): TypeError: _tls2.default.connect is not a function TypeError: _tls2.default.connect is not a function at new TCPSocket (vendor.bundle.js:100809) at Function.open (vendor.bundle.js:100785) at Promise (vendor.bundle.js:97414) at new ZoneAwarePromise (polyfills.bundle.js:3655) at Imap.connect (vendor.bundle.js:97413) at Promise (vendor.bundle.js:95493) at new ZoneAwarePromise (polyfills.bundle.js:3655) at Client._openConnection (vendor.bundle.js:95489) at vendor.bundle.js:95466 at Generator.next (<anonymous>) at new TCPSocket (vendor.bundle.js:100809) at Function.open (vendor.bundle.js:100785) at Promise (vendor.bundle.js:97414) at new ZoneAwarePromise (polyfills.bundle.js:3655) at Imap.connect (vendor.bundle.js:97413) at Promise (vendor.bundle.js:95493) at new ZoneAwarePromise (polyfills.bundle.js:3655) at Client._openConnection (vendor.bundle.js:95489) at vendor.bundle.js:95466 at Generator.next (<anonymous>) at resolvePromise (polyfills.bundle.js:3589) at resolvePromise (polyfills.bundle.js:3560) at polyfills.bundle.js:3638 at ZoneDelegate.webpackJsonp../node_modules/zone.js/dist/zone-mix.js.ZoneDelegate.invokeTask (polyfills.bundle.js:3219) at Object.onInvokeTask (vendor.bundle.js:49840) at ZoneDelegate.webpackJsonp../node_modules/zone.js/dist/zone-mix.js.ZoneDelegate.invokeTask (polyfills.bundle.js:3218) at Zone.webpackJsonp../node_modules/zone.js/dist/zone-mix.js.Zone.runTask (polyfills.bundle.js:2986) at drainMicroTaskQueue (polyfills.bundle.js:3396) at <anonymous>

@osterland
Copy link

sounds like we're sittin in the same boat #168

@felixhammerl
Copy link
Contributor

Are you sure that the node net and tls APIs are available in your electron app where you're trying to invoke this?

@felixhammerl
Copy link
Contributor

The way the tcp socket shim resolves the API is by querying whether some specific properties of the platform are present. For example, all node-related platforms have process available (see here), hence we then jump to the node net or tls socket (see here)

@igoventura
Copy link
Author

The error seems to be caused by:

var _tls = __webpack_require__("./node_modules/node-libs-browser/mock/empty.js");

var _tls2 = _interopRequireDefault(_tls);

...

this._socket = this.ssl ? _tls2.default.connect(this.port, this.host, {}, function () {
      return _this._emit('open');
    }) : _net2.default.connect(this.port, this.host, function () {
      return _this._emit('open');
    });

Because the error message is _tls2.default.connect is not a function.

@felixhammerl
Copy link
Contributor

Ooooh, I think I see what the problem is...

Here is the import for net and tls respectively. The way babel is polyfilling it is by checking whether it's a babel-transpiled module, i.e. whether it has a property called __esModule. Mixing imports for ES6 and commonjs modules is actually illegal in the ECMAScript standard. Roughly speaking: import is for ES6 modules and require for commonjs modules, since the two have different runtime behaviors.

Can you do me a favor and put a breakpoint in line 53 of node-socket.js in the tcp-socket shim and see what the object looks like? after require, net and tls should not have the __esModule property set. If the property is missing, it should behave correctly, as babel polyfills it. if the property is there, that's the source of the error.

@felixhammerl
Copy link
Contributor

felixhammerl commented Feb 15, 2018

I just pushed a change where the tcp-socket integration test does not babelify on the fly and uses the transpiled pre-built sources just as any other project would consume the lib. The weird thing is: It runs fine.

emailjs/emailjs-tcp-socket@a6600f0

Here's the test... https://github.com/emailjs/emailjs-tcp-socket/blob/master/test/node/node-integration.js

Is your repo open so that I could check out where it breaks for you? This sucks and I can't seem to reproduce it...

And one more thing: AFAIK in electron, net and tls needs to be run on the node side of things (in main), not from the renderer process. However, it's been a while since I've played with electron, so this might have changed.

@igoventura
Copy link
Author

It's just a test project so i don't have put into an repo. I'll test with the breakpoint you mentioned and comment the result. Also, I'll put the project in one repo and share it here.

@felixhammerl
Copy link
Contributor

any news on this?

@sujidivakar
Copy link

any updates for this issue?
"Could not connect to server TypeError: _net2.default.connect is not a function"

@felixhammerl
Copy link
Contributor

@igoventura i assume that means the issue was resolved?

@felixhammerl
Copy link
Contributor

@sujidivakar any news on your end? seems list @igoventura dropped out

@felixhammerl felixhammerl reopened this Apr 2, 2018
@felixhammerl
Copy link
Contributor

ping?

@felixhammerl
Copy link
Contributor

Since nobody bothered to respond, i am closing this ticket for now.

@allgood
Copy link

allgood commented May 13, 2018

I am having this error when trying to do a progressive web app that runs on browser. Does this lib support this?

@felixhammerl
Copy link
Contributor

I see why this might be confusing. The web stack traditionally does not have a concept of raw sockets. So you can't just drop the library into the browser. Does not work.

However, what you might wanna look into is run a proxy that allows opening a WebSockets-TCP proxy. The emailjs stack was built to support this. Please know that this needs a lot of discussion as it might open up your proxy to a lot of abuse. You can find out more about a TCP proxy here: https://github.com/emailjs/emailjs-tcp-proxy

@allgood
Copy link

allgood commented May 14, 2018

@felixhammerl I'm now running the proxy on my localhost for testing, but I was unable to find how tell the application to used it. Initially I thought that options of imap-client would accept the ws: {} key, but look that this is not the way. Can you help me?

@felixhammerl
Copy link
Contributor

You are on the right track. On the one hand, you have to run the proxy so that it can open TCP connection on your behalf, and then you need to connect to the proxy as well. Check the documentation here for how to do it: https://github.com/emailjs/emailjs-tcp-socket#web-sockets

It is actually as simple as telling where to find your socket.io instance and you're good to go :)

If you receive any errors down the road, but see that the general connection starts, it might be TLS certificate errors. What out for that, as the error is a bit tricky and unfortunately get thrown out of band :(

@allgood
Copy link

allgood commented May 15, 2018

@felixhammerl , I think I am missing something.

I have cloned emaijs-tcp-proxy and ran on my localhost:

PROXY_PORT=9000 npm start

On my code I start the socket with the example code:

var socket = TCPSocket.open('127.0.0.1', 9000, {
  ws: {
    url: 'http://localhost:8889',
    options: {
        upgrade: false
    }
  }
})

Looks like I am missing on how to start the service on port 8889.

Another thing I do not know how to do is how to use the started tcpsocket with ImapClient.

@felixhammerl
Copy link
Contributor

felixhammerl commented May 15, 2018

Here you go: https://github.com/emailjs/emailjs-tcp-proxy#usage-as-standalone

> git clone git@github.com:emailjs/emailjs-tcp-proxy.git
> cd emailjs-tcp-proxy
> PROXY_PORT=8889 npm start

That should get you started. Run it on the port you're pointing the TCPSocket instance to, in this case 8889 :)

If you're serving your app from an express instance already, you can also just hook the proxy into your express instance: https://github.com/emailjs/emailjs-tcp-proxy#usage-as-standalone

@allgood
Copy link

allgood commented May 15, 2018

I made this, only used port 9000 but the code I pasted above still gives me the error:

_net2.default.connect is not a function

@felixhammerl
Copy link
Contributor

damn. i will look into it.

@felixhammerl felixhammerl reopened this May 16, 2018
@allgood
Copy link

allgood commented May 16, 2018

@felixhammerl ... I do not know if I am missing something or if this is a bug. I am only using emailjs on ionic running on browser served by the ionic serve command and the emailjs-tcp-proxy on localhost as instructed, nothing more! You mentioned express, I am not using it.

@osterland
Copy link

im using ionic as well. Tried the combination of tcp-proxy + tcpClient and i get the _net2.default.connect is not a function as well. Let me know if i can do more helpful stuff than just reading this feed... ;)

@thomas-topway-it
Copy link

hello, I have installed the library on a vue.js app and I get the same error.
I'm just importing the library from a component using
import ImapClient from 'emailjs-imap-client'
and then after initializing the client using
var client = new ImapClient(....
I get the error

TypeError: _tls2.default.connect is not a function
    at new TCPSocket

How can I solve it ?
thanks
(Thomas)

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

No branches or pull requests

6 participants