aws / aws-iot-device-sdk-js

SDK for connecting to AWS IoT from a device using JavaScript/Node.js
Apache License 2.0
964 stars 384 forks source link

Uncaught TypeError: filesys.existsSync is not a function #76

Closed Krieke closed 8 years ago

Krieke commented 8 years ago

Using the aws-iot-sdk-browser-bundle.js (browsery of node.js aws-iot-device-sdk module);

var awsIot = require('aws-iot-device-sdk');

var device = awsIot.device({
    endpoint: 'https://******************.iot.eu-central-1.amazonaws.com',
    keyPath: '../aws/************-private.pem.key',
    certPath: '../aws/***********-certificate.pem.crt',
            caPath: '../aws/***********-public.pem.key',
    clientId: "******************",
            region: "******" 
});
Krieke commented 8 years ago

//begin module /**

Krieke commented 8 years ago

fs.exists(path, callback)# Added in: v0.0.2 Deprecated since: v1.0.0 Stability: 0 - Deprecated: Use fs.stat() or fs.access() instead.

rongsaws commented 8 years ago

First thought, since the browser can't access local files, you may want to put the key/certs in buffers and include them in the bundle, or (preferably) use Cognito authentication just like all of the browser examples .

Krieke commented 8 years ago

@rongsaws : i think it is related to a deprecate function.... it doesn't recognize it as a function at the moment...I am running in all in plain javascript that is perfectly capable of accessing the local file system.

Daniel Vangrieken

Email : daniel.vangrieken@gmail.com Phone: +32 (0) 476 / 99 65 61

2016-07-20 21:00 GMT+02:00 rongsaws notifications@github.com:

First thought, since the browser can't access local files, you may want to put the key/certs in buffers and include them in the bundle, or (preferably) use Cognito authentication just like all of the browser examples .

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/aws/aws-iot-device-sdk-js/issues/76#issuecomment-234048040, or mute the thread https://github.com/notifications/unsubscribe-auth/AS4X3nDnkgT7C69VNkFNkPmC2MQ9awr4ks5qXnBkgaJpZM4JQ78y .

rongsaws commented 8 years ago

Are you not running your script in browser?

Krieke commented 8 years ago

Yes... sorry for the confusion. But supporting "window.File, window.FileReader. I started with buffering, but maybe I just should go directly to Cognito authentication. Actually : I will try both

Daniel Vangrieken

Email : daniel.vangrieken@gmail.com Phone: +32 (0) 476 / 99 65 61

2016-07-20 21:22 GMT+02:00 rongsaws notifications@github.com:

Are you not running your script in browser?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/aws/aws-iot-device-sdk-js/issues/76#issuecomment-234054112, or mute the thread https://github.com/notifications/unsubscribe-auth/AS4X3qgvcKIgdvMKeeYqOhxT3DDjPNxrks5qXnWFgaJpZM4JQ78y .

rongsaws commented 8 years ago

I don't think Browserify transforms fs to HTML5 local file APIs, yet. In the mean time, using Cognito or passing cert buffers through the parameters below is probably your best bet.

clientCert: same as certPath, but can also accept a buffer containing client certificate data
privateKey: same as keyPath, but can also accept a buffer containing private key data
caCert: same as caPath, but can also accept a buffer containing CA certificate data
Krieke commented 8 years ago

Hey rongsaws : any javascript tips on creating these buffers?

rongsaws commented 8 years ago

Would something like this work for you?

C:\Users\rongs>node --version
v4.4.3

C:\Users\rongs>node
> var cert = new Buffer('--begin certificate-- ab...cd --end certifcate--');
undefined
> Buffer.isBuffer(cert)
true
Krieke commented 8 years ago

Actually not : I have a plain javascript project and this Buffer is part of the nodejs package "buffer.js". Also we can't use Cognito authentication since for privacy law we can only make use of Frankfurt which currently is not supporting this service. So my only option left here is to make this buffer mechanism work otherwise this PoC becomes not a success for our customers.

rongsaws commented 8 years ago

Wouldn't Browserify transform buffer.js into the bundle too? It should work.

Krieke commented 8 years ago

@rongsaws : I indeed can use the buffer.js with browserify, but then I have to browserify my own app code as well which I actually didn't want to do (Ok maybe I am overlooking something, but the whole nodejs stuff is fairly new to me being a C developer and trying to prototype a plain javascript app asap. On the aws-iot-device-sdk bundle I could just depend by adding require in my main script since this was exported by the aws....bundle. But HOW DO I DO THIS for buffer = A NICE example would be welcome

By the way : when providing the keys in a buffer (I managed due to read above moving my whole project in a bundle) it still will not work via a browser since you still trigger these lines of code :

Lines 157 - 174 (in the browserified aws bundle). // Parse PEM files. Options ending in 'Path' must be files // and will override options which do not end in 'Path'.

if (filesys.existsSync(options.keyPath)) { options.key = filesys.readFileSync(options.keyPath); } else if (!isUndefined(options.keyPath)) { throw new Error(exceptions.INVALID_KEY_PATH_OPTION); } if (filesys.existsSync(options.certPath)) { options.cert = filesys.readFileSync(options.certPath); } else if (!isUndefined(options.certPath)) { throw new Error(exceptions.INVALID_CERT_PATH_OPTION); } if (filesys.existsSync(options.caPath)) { options.ca = filesys.readFileSync(options.caPath); } else if (!isUndefined(options.caPath)) { throw new Error(exceptions.INVALID_CA_PATH_OPTION); }

So the conclusion is that based on this sdk everyone with a browser have to modify the code or use Cognito authentication which we can't since we can only use Frankfurt region.

Krieke commented 8 years ago

I first had to read the documentation: apperently AWS only support mqtt over wss. I thought that with the modern browser raw tcp protocol was also implemented in the sdk so mqtt over tcp could be supported. So the buffering is not required anymore and the above remark about hitting this code can also be ignored.

This topic issue can be closed... unless you guys want to provide support in browsers for mqtts via tcp ;-)

rongsaws commented 8 years ago

Since you are doing a prototype, instead of using Cognito, you could use IAM key and credential to initialize your Websocket connection. Browserifying TSL connection is currently not supported unfortunately, so you may want to stick with the Websocket solution for browser applications for now.

Krieke commented 8 years ago

Indeed... I currently got my prototype working... with WSS. So I am pubbing as a device, as a thingshadow and reading the last state via REST... this will do for my current PoC

Thx for responding.

Next step will be to write a more advance app using Angular + Ionic. : will see when I have the time to dive in to this... at least this has a lower priority since I have a bunch of other tasks to do.

Regards,

Daniel

Daniel Vangrieken

Email : daniel.vangrieken@gmail.com Phone: +32 (0) 476 / 99 65 61

2016-07-23 2:23 GMT+02:00 rongsaws notifications@github.com:

Since you are doing a prototype, instead of using Cognito, you could use IAM key and credential to initialize your Websocket connection. Browserifying TSL connection is currently not supported unfortunately, so you want to stick with the Websocket solution for browser applications for now.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/aws/aws-iot-device-sdk-js/issues/76#issuecomment-234686712, or mute the thread https://github.com/notifications/unsubscribe-auth/AS4X3uWGReaB1QSRWKS95uHLOYO9LcWhks5qYV8XgaJpZM4JQ78y .

0xc0d3r commented 7 years ago

I've created an app using create-react-app and installed aws-iot-device-sdk. Now I am trying use this sdk in browser environment and getting the same issue. Then I went through the docs and generated browser bundle for this sdk and added that js file in public/index.html. No luck, still getting the issue. Any inputs to get rid of this issue

juppdes commented 6 years ago

Anybody has solved this question ?

samarahmad commented 5 years ago

i am also getting same problem how can i solve this issue pls tell me

borch84 commented 5 years ago

I posted a question regarding the same issue on another open post here: