otplib icon indicating copy to clipboard operation
otplib copied to clipboard

otplib 12 broke angular karma tests

Open pickfire opened this issue 5 years ago • 8 comments

Describe the bug A clear and concise description of what the bug is.

ReferenceError: buffer is not defined

To Reproduce Steps to reproduce the behavior:

  1. Run karma tests in angular ng t --browsers Firefox

Expected behavior A clear and concise description of what you expected to happen.

Does not break any tests.

Screenshots If applicable, add screenshots to help explain your problem.

2020-03-27-123731_1350x142_scrot

Details (please provide any relevant information):

  • otplib version: 12.0.1
  • os: Linux arch 5.5.10-zen1-1-zen #1 ZEN SMP PREEMPT Wed, 18 Mar 2020 08:40:41 +0000 x86_64 GNU/Linux
  • browser: firefox 74.0
  • node / yarn: 1.22.4
  • frameworks (if any): ionic 4.11.5 / angular 8.2.14 / karma 4.4.1 / jasmine 2.8.0

Additional context Add any other context about the problem here.

Source code https://github.com/ctiteam/apspace

pickfire avatar Mar 27 '20 04:03 pickfire

Hi @pickfire

That is expected as you're using the browser version. v12 has marked buffer as an external module. So you'll need to import buffer separately.

You can refer to: https://github.com/yeojz/otplib#in-browser

yeojz avatar Mar 28 '20 15:03 yeojz

@yeojz Is there a way to do that without:

<script src="https://unpkg.com/@otplib/preset-browser@^12.0.0/buffer.js"></script>
<script src="https://unpkg.com/@otplib/preset-browser@^12.0.0/index.js"></script>

pickfire avatar Mar 28 '20 16:03 pickfire

you probably can import buffer within your code: i.e.

import buffer from 'buffer';
window.Buffer = buffer

or use the node version

import { authenticator } from 'otplib'; // instead of otplib-browser

yeojz avatar Mar 28 '20 16:03 yeojz

@yeojz Do you mean do that inside the test?

import { authenticator } from 'otplib'; // instead of otplib-browser

Isn't that how to do it in version 11? Version 11 works this way using otplib-browser but version 12 doesn't for tests.

pickfire avatar Mar 29 '20 02:03 pickfire

No I meant when changing to v12, consider using the main entry and relay on your own bundler to package the module.

The difference between v11 and v12 is that it doesn't bundle in Buffer, and you'll have to ensure your build provides it.

yeojz avatar Mar 29 '20 16:03 yeojz

Ah, but why does it depends on Buffer? Is it a requirement? And shouldn't the preset browser includes a buffer?

pickfire avatar Mar 30 '20 03:03 pickfire

The initial thought was that the package will probably be used in a bundler. Packaging buffer would mean the end-users might bundle buffer twice, increasing the final output size.

After much thought, I acknowledge that the package should be plug and play. Will include that in v13.

yeojz avatar Jul 10 '20 11:07 yeojz

I'm also facing the same issue; @yeojz will there be a v13?

you probably can import buffer within your code: i.e. import buffer from 'buffer'; window.Buffer = buffer

Doesn't seem to do the trick unfortunately :/

Myzel394 avatar Feb 25 '23 21:02 Myzel394