Meteor Npm-module client-side?

2020-02-08 15:23发布

Is it possible to use Npm-Modules on client-side?

More specifically:

I want to use node.js built-in crypto-module for encrypting a password the user enters and then send the encrypted password with a signature(/hmac) to my server.

I need to do it that way, because I must be able to retrieve the original password server-side, because afterwards I'm going to send it to a ldap-server, because the user should authenticate with the same username/password he is registered with on that server.

This is what I did:

created in packages/crypto/: -package.js:

Package.on_use(function(api) { api.add_files('crypto.js',['server','client']);});

-crypto.js: crypto = Npm.require("crypto");

It works fine on the server, but on the client it says "Reference Error: Npm is not defined". So, is it possible to use the crypto-module on client-side?

Are there any alternatives for achieving this goal?

Thank you!

Edit: Is there any good alternative for getting the password to the server in a secure way, so that the server can retrieve the original password? I think doing the ldap()-request on the client-side (like: if(checkLdap(usrname,password)){<login>} else{fail}) can be easily bypassed?

4条回答
够拽才男人
2楼-- · 2020-02-08 16:05

You can try to add the js-files you need on client-side from .npm folder under crypto's package directory. So, your package.js file might look like this:

Package.describe({
  summary: 'Description of your crypto package'
});

Npm.depends({
  'crypto': '1.0.0'
});

Package.on_use(function (api) {
  api.add_files('crypto.js', 'server');
  api.add_files('.npm/node_modules/crypto/crypto.js', 'client');
});
查看更多
唯我独甜
3楼-- · 2020-02-08 16:06

You can use https://github.com/elidoran/cosmos-browserify now to archive this. I used wrapped packages before and it was real pain to update them and to create new ones. Now with browserify support I can include library with just several lines of code. See their example how to do it. I don't publish it here as it may be subject of change.

查看更多
做个烂人
4楼-- · 2020-02-08 16:23

Its not possible to use Npm modules on the client side since Npm modules are extensions via node.js which only runs on the server end.

If you want to use a file like crypto you would have to make a client side only version and put it in /client/lib of your Meteor app

While this may be possible officially, Meteor doesn't support this.

You would have to include requirejs manually using this project: https://github.com/apendua/require

查看更多
Deceive 欺骗
5楼-- · 2020-02-08 16:28

You can use browserify to build a .js bundle with all all the Npm modules you want on the client side. See:

2013 Meteor NPM Packages

查看更多
登录 后发表回答