socketcluster icon indicating copy to clipboard operation
socketcluster copied to clipboard

SCWorker options.authEngine being overridden by defaults

Open vgw-rhysc opened this issue 7 years ago • 3 comments

Should the options being passed in be overridden by the defaults, or have I missed something here? Im trying to set the AuthEngine as per docs: https://socketcluster.io/#!/docs/api-scworker

You can also use the options object to pass a custom authEngine and codecEngine to the SCWorker instance. However it seems that the param is just overwritten in this line here. https://github.com/SocketCluster/socketcluster/blob/master/scworker.js#L82

var workerOptions = Object.assign({}, options, workerInitOptions);

Should this be :

var workerOptions = Object.assign({}, workerInitOptions, options);

❓

vgw-rhysc avatar Oct 11 '19 07:10 vgw-rhysc

@vgw-rhysc Nice catch, thank you!

The option to pass custom authEngine in SCWorker constructor is there since SocketCluster v14.2.0

var workerOptions = Object.assign({}, options, workerInitOptions);

AFAIK, the wrong order of the arguments hasn't caused any observable issues so far. But that's just a coincidence, because workerInitOptions object never includes nor authEngine nor codecEngine properties.

So, in fact, even with the wrong order of the arguments, the authEngine, which is passed to the SCWorker constructor, is being set correctly.

The following example works fine (throws the expected exceptions) with SocketCluster v14.4.1

// worker.js

const myMockAuthEngine = {
  itsMyMockEngine: true,
  verifyToken: () => {
    throw new Error('My custom verifyToken function has been invoked')
  },
  signToken: () => {
    throw new Error('My custom signToken function has been invoked as well')
  },
}

new Worker({
  authEngine: myMockAuthEngine,
})

I'll open a PR to swap the arguments positions.

@vgw-rhysc Could you, please, provide an example of code which fails to setup your custom authEngine correctly?

MegaGM avatar Oct 13 '19 22:10 MegaGM

Sorry this may have actually been me confusing the SC Server and SC worker - i had added logging into to both and have likely misread the logs. I think we can close this. I guess i am a bit confused as to why we have an auth engine in the SC Server that is independent to the SC Worker however?

vgw-rhysc avatar Oct 14 '19 01:10 vgw-rhysc

@vgw-rhysc said:

an auth engine in the SC Server that is independent to the SC Worker

Workers don't use authEngine at all, only SC Server does.
Each SCWorker spawns its own copy of socketcluster-server and simply passes the authEngine option down to it.

MegaGM avatar Oct 19 '19 06:10 MegaGM