Adyen / adyen-cse-web

[DEPRECATED] Client-side encryption on JavaScript
MIT License
15 stars 39 forks source link

Node module returns sjcl lib instead of adyen #16

Closed agudulin closed 8 years ago

agudulin commented 8 years ago

When I try to use no-dom version as a node module, I get the wrong object.

// package.json
"dependencies": {
  "adyen-cse-js": "git+https://github.com/Adyen/CSE-JS.git#v0.1.17"
}

// app.js
import adyenEncrypt from 'adyen-cse-js'
console.log(adyenEncrypt)
// → Object { cipher, hash, keyexchange, mode, misc, codec, exception, bitArray, prng, random, json, encrypt, decrypt }

Why

Every node module has a module variable as a reference to the object representing the current module.

And I figured out that this variable is used in sjcl library, and you include this library into your module without any encapsulation.

As a result, when I import your library I get sjcl library instead.

How to fix

You may want to wrap this code into closure, and pass an empty object as module variable:

(function (module) {
  // ... current sjcl code on L149
})({});

Also after defining AMD module it would be great to make it obvious what exactly do we want to export:

// L172
if (module && module.exports) {
  module.exports = encrypt;
}
agudulin commented 8 years ago

I've created corresponding PR for that: https://github.com/Adyen/CSE-JS/pull/17

vkurlyan commented 8 years ago

if (module && module.exports) {

this code leads to the exception in a browser because 'module' is undefined

agudulin commented 8 years ago

@vkurlyan no, it does not :)

the second condition will never be executed if the first one is falsy.

so if (undefined && undefined.exports) { ... } will never show the execption

agudulin commented 8 years ago

however the corresponding PR has been already merged and this issue can be closed

vkurlyan commented 8 years ago

If module is undefined we will have excaption because it's undefined variable but not property. You need to change code to do it without excaption:

if (typeof module !== 'undefined' && module.exports)

vkurlyan commented 8 years ago

I added pull request https://github.com/Adyen/CSE-JS/pull/25

agudulin commented 8 years ago

@vkurlyan ah, I see, thanks!