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

Problems with CBOR in Angular App #148

Closed
dibog opened this issue Jul 15, 2021 · 20 comments
Closed

Problems with CBOR in Angular App #148

dibog opened this issue Jul 15, 2021 · 20 comments

Comments

@dibog
Copy link

dibog commented Jul 15, 2021

I tried to use 'cbor-web' in an Angular Frontend.
But when I type ng build I get the following error:

Uncaught ReferenceError: process is not defined
    at Object.push.MCLT.exports.debuglog (util.js:100)
    at Object.481 (cbor.js:2)
    at o (cbor.js:2)
    at Object.830 (cbor.js:2)
    at o (cbor.js:2)
    at Object.20 (cbor.js:2)
    at o (cbor.js:2)
    at Object.141 (cbor.js:2)
    at o (cbor.js:2)
    at cbor.js:2
    at cbor.js:2
    at cbor.js:2
    at cbor.js:2
    at Object.4uv0 (cbor.js:2)
    at __webpack_require__ (bootstrap:79)
@hildjj
Copy link
Owner

hildjj commented Jul 15, 2021

As a work-around npm install -D process and add this to your JS file:

// eslint-disable-next-line no-unused-vars
const process = require('process')

I'll see if I can fix it so you don't need it in the future.

@dibog
Copy link
Author

dibog commented Jul 15, 2021

Thanks, I'll try that out.

So a dependency is missing, could those be automatically resolved by npm?

The following dependency is also missing:

  • bignumber.js

I also get the following warning (should I open an own issue for it):

WARNING in <path/filename> depends on 'cbor-web'. CommonJS and AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies

@dibog
Copy link
Author

dibog commented Jul 15, 2021

I'm using typescript for my Angular app. This is how I'm importing the necessary imports from cbor-web:

import { decode } from 'cbor-web';

I tried the following for process dependency:

import p from 'process';

ng build or ng serve works, but when I try to load my app I get the following in the log:

util.js:100 Uncaught ReferenceError: process is not defined
    at Object.push.MCLT.exports.debuglog (util.js:100)
    at Object.481 (cbor.js:2)
    at o (cbor.js:2)
    at Object.830 (cbor.js:2)
    at o (cbor.js:2)
    at Object.20 (cbor.js:2)
    at o (cbor.js:2)
    at Object.141 (cbor.js:2)
    at o (cbor.js:2)
    at cbor.js:2
    at cbor.js:2
    at cbor.js:2
    at cbor.js:2
    at Object.4uv0 (cbor.js:2)
    at __webpack_require__ (bootstrap:79)

@hildjj
Copy link
Owner

hildjj commented Jul 15, 2021

Did you add process as a dependency? Yes, you'll probably also need bignumber.js as a dependency until I land the fix for #140 in the next major release (which I'm getting very close to having a PR for. Follow my work in the node12required branch)

@dibog
Copy link
Author

dibog commented Jul 16, 2021

Please have a look at my repo here: https://github.com/dibog/angular-cbor

Its basically a freshly created Angular App (ng new angular-cbor), then I ripped out all the html stuff and
added via npm install

  • cbor-web
  • bignumber.js
  • process

Still same error. I really would like to use your library, but it so far I'm not successfull with my tries.

@hildjj
Copy link
Owner

hildjj commented Jul 16, 2021

I'm just pulling it down now, thanks for cutting the repro down. I can't reproduce needing process in my environment anymore, so I'm hoping this will help me find it.

@hildjj
Copy link
Owner

hildjj commented Jul 16, 2021

What version of node are you using?

@dibog
Copy link
Author

dibog commented Jul 16, 2021

C:\>node
Welcome to Node.js v15.3.0.
Type ".help" for more information.

C:\>npm --version
6.14.9

@hildjj
Copy link
Owner

hildjj commented Jul 16, 2021

I think the issue is that your setup is using an old webpack version that is pulling in the util package, which then includes process. You can see the work-around I used in

(combined with pulling node-inspect-extracted into the HTML file), but I don't know how to do that in angular. I'll have a chance to look for a work-around this afternoon, but I think moving to the node12required branch will remove all traces of util and fix this problem more completely. If it does, I'll accelerate moving to a release of cbor@8 this afternoon instead.

@dibog
Copy link
Author

dibog commented Jul 16, 2021 via email

@dibog
Copy link
Author

dibog commented Jul 16, 2021

With the newest npm and node for windows it does not even compile.
I have to install 'util' and 'bignumber.js' and then I got during building this:

./node_modules/cbor-web/dist/cbor.js:5:13-36 - Warning: Module not found: Error: Can't resolve 'bignumber.js' in 'D:\angular-cbor2\node_modules\cbor-web\dist'

Warning: D:\angular-cbor2\src\app\app.component.ts depends on 'cbor-web'. CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies



Error: src/app/app.component.ts:2:24 - error TS7016: Could not find a declaration file for module 'cbor-web'. 'D:/angular-cbor2/node_modules/cbor-web/dist/cbor.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/cbor-web` if it exists or add a new declaration (.d.ts) file containing `declare module 'cbor-web';`

2 import { decode } from 'cbor-web';
                         ~~~~~~~~~~

@hildjj
Copy link
Owner

hildjj commented Jul 16, 2021

Nod. Ok, I think the next major release should fix all of that (I hope). I'm surfing (what I hope is) the last set of changes right now, so will ping you when I have a release candidate to test. In the meantime, can you update your test repo with the modern version, perhaps on a branch so we can switch back and forth?

@hildjj
Copy link
Owner

hildjj commented Jul 16, 2021

Try this:

npm add https://gitpkg.now.sh/hildjj/node-cbor/packages/cbor-web\?node12required

and remove process as a dependency, and remove the import process from 'process';

(this is just to see if it works. I don't know angular enough to be able to tell if this fixed the problem :( )

@dibog
Copy link
Author

dibog commented Jul 16, 2021

Nod. Ok, I think the next major release should fix all of that (I hope). I'm surfing (what I hope is) the last set of changes right now, so will ping you when I have a release candidate to test. In the meantime, can you update your test repo with the modern version, perhaps on a branch so we can switch back and forth?

Looking forward to it. Do you have any idea when you have such release candidate?

@dibog
Copy link
Author

dibog commented Jul 16, 2021

Try this:

npm add https://gitpkg.now.sh/hildjj/node-cbor/packages/cbor-web\?node12required

and remove process as a dependency, and remove the import process from 'process';

(this is just to see if it works. I don't know angular enough to be able to tell if this fixed the problem :( )

I'll try that out, but probably not before monday. Thanks!

@dibog
Copy link
Author

dibog commented Jul 23, 2021

So its now Friday and not Monday :-)

Taken your last post into account I was able to start my app. On decoding I got the following error:

Error: Unknown input type
    at Object.t.guessEncoding (cbor.js:2) [angular]
    at decodeFirstSync (cbor.js:2) [angular]

The message I got is from a python backend. I assume the CBOR message is valid, but I can't prove it. :-)
So take this error message with a grain of salt.

@hildjj
Copy link
Owner

hildjj commented Jul 23, 2021

We've made good progress at least. :)

guessEncoding should work if you passed in a hex-encoded string, a Buffer, an ArrayBuffer, or a Uint8Array. I wonder what else you might have passed in?

@dibog
Copy link
Author

dibog commented Jul 26, 2021

Hi Joe,

I looked it up. I put a Blob object I got from a websocket into the decode method.

    try {
      console.log('trying to decode message:', message);                 // 105
      const blob = message as Blob;
      const decodedMessage = decode(blob.arrayBuffer());
      console.log('decoded', message, 'to', decodedMessage);
      this.emitMessage(decodedMessage);
    }
    catch (error: any) {
      console.log('can\'t decode cbor:', error);                                  // 112
    }

and I got in the log:

DBCConnector: handleCBORMessage:  emitter_length= 1
dbc.connector.ts:105 trying to decode message: Blob
dbc.connector.ts:112 can't decode cbor: Error: Unknown input type
    at Object.t.guessEncoding (cbor.js:2) [angular]
    at decodeFirstSync (cbor.js:2) [angular]
    at DataBusConnector.handleCBORMessage (dbc.connector.ts:107) [angular]
    ...

@hildjj
Copy link
Owner

hildjj commented Jul 26, 2021

add an await blob.arrayBuffer() in there? I may add support for Blob directly if we get this working.

@dibog
Copy link
Author

dibog commented Jul 26, 2021 via email

@hildjj hildjj closed this as completed in 86696c1 Aug 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants