freedomjs / freedom-port-control

Opens ports through a NAT with NAT-PMP, PCP, and UPnP
11 stars 3 forks source link

Require not defined #7

Open louptheron opened 8 years ago

louptheron commented 8 years ago

After a successful npm install freedom-port-control and npm install freedom-for-node, running :

var freedom = require('freedom-for-node');
var portcontrol = freedom('node_modules/freedom-port-control/src/port-control.json');

give me an error : TypeError: freedom is not a function

Running : var portcontrol = freedom.freedom('node_modules/freedom-port-control/src/port-control.json');

I have this error : [ReferenceError: require is not defined]

Do you know why ?

Thank you

willscott commented 8 years ago

looks like the second line is actually

 var portcontrol = freedom.freedom('node_modules/freedom-port-control/dist/port-control.json');
louptheron commented 8 years ago

it give me no error, but when I test :

var freedom = require('freedom-for-node');
var portcontrol = freedom.freedom('node_modules/freedom-port-control/dist/port-control.json');
portcontrol.probeProtocolSupport();

there is

TypeError: portcontrol.probeProtocolSupport is not a function

Result of a console.log(portcontrol) :

lib$es6$promise$promise$$Promise {
  _id: 0,
  _state: undefined,
  _result: undefined,
  _subscribers: [] }
willscott commented 8 years ago

you probably have a promise that resolves to the expected interface. Sorry - all of the calls will be asynchronous promises, rather than direct returns.

var freedom = require('freedom-for-node');
freedom('node_modules/freedom-port-control/dist/port-control.json').then(function(portControl) {
  return portControl.probeProtocolSupport();
}).then(function(support) {
  console.log(support);
});
louptheron commented 8 years ago

Ok, I followed the code example and the freedom-port-control module is loaded (got it for the promise, thank you) :

require('freedom-for-node').freedom('node_modules/freedom-port-control/dist/port-control.json', {}).then(function(Root){
        var root = new Root();
        root.probeProtocolSupport().then(function(res){
            console.log(res);
        });
    });

but I got new errors :

Cannot instantiate provider, since it is not provided
[unbound Provider] dropping message {"to":8,"type":"message","message":{"action":"method","type":"createDataChannel","reqId":0,"text":["dummy data channel",null],"binary":[]}}
Cannot instantiate provider, since it is not provided
[unbound Provider] dropping message {"to":9,"type":"message","message":{"action":"method","type":"createDataChannel","reqId":0,"text":["dummy data channel",null],"binary":[]}}
[unbound Provider] dropping message {"to":8,"type":"message","message":{"action":"method","type":"close","reqId":1,"text":[],"binary":[]}}
[unbound Provider] dropping message {"to":9,"type":"message","message":{"action":"method","type":"close","reqId":1,"text":[],"binary":[]}}
UPnP failed at: URL is not defined

It seems that _getUpnpControlUrl() does not give the expected controlUrl as expected, maybe with a bad Ssdp response.

willscott commented 8 years ago

Sigh. Didn't realize this code had a dependency on WebRTC - probably to learn your external IP address through the STUN process.

I don't think i've ever made that work in a reliable way in node.js, although that's probably worth doing. The alternative is to not use STUN, or use it as a UDP protocol rather than bundled in webRTC.

louptheron commented 8 years ago

Mhm ok, thank you for your help !

kennysong commented 8 years ago

Actually, I think this error is because we're using the URL interface that's only supported in Chrome or Firefox, and not in node. This was fine since our initial use case was only in browser apps.

From a quick look, it seems like URL is only used on one line here, so it should be fairly easy to substitute out and remove this dependency.

Also, if I recall correctly, WebRTC is only used to determine the private IPs of the computer, which is done by gathering ICE candidates on a dummy data channel – no STUN servers are actually used.

louptheron commented 8 years ago

In fact, it is easy remove the URL interface here and here, but XMLHttpRequest is used there, and is not implemented by the node Javascript engine.

kennysong commented 8 years ago

Yeah, it looks like there will be minor incompatibilities with the current code and node. @willscott what do you think is the best way to deal with this?

bemasc commented 8 years ago

If you need XMLHttpRequest in a freedom module, you might like https://github.com/uproxy/freedom-xhr