Loading and using npm factories

How would one go about loading a module from npm, which when imported provides a single function that acts as a factory, returning a class implementation rather than an instance?

By way of example, I have included npm module thinbus-srp in Gradle. I create an external function in Kotlin with appropriate JsModule & JsNonModule annotations (I use the browser method, rather the client method), returning a type.

Per the test code for thinbus-srp, after calling this factory function, you create an instance. Said type is defined as an external class, which I populate with applicable methods, including generateRandomSalt.

My code loads correctly loads the factory and seems to correctly create an instance. But I get told there is no such function, when I call browserClientInstance.generateRandomSalt().

First off, I already suspect my problem is more linked to a lack of understanding of the class implementation in Javascript, though there is certainly a Kotlin element to why I can’t see a solution.

Using a breakpoint in chrome, I can see I do indeed have an instance, but it is not present in the properties list of the object. It is defined in the prototype of the instance though.

This seems to imply that it may also be a problem even when not doing this Kotlin.

Why would this be experienced?
How would one go about using such a module like thinbus-srp and correctly expose these methods that do exist?

Try this.

Javascript part:

function myFactory() {
    function MyImpl() {
        this.foo = 'bar'
    }
    MyImpl.prototype.baz = function(prefix) {
        console.log(prefix + ' ' + this.foo)
    }
    return MyImpl
}

Kotlin part:

external class MyImpl {
    fun baz(prefix: String)
}

external fun myFactory(): JsClass<MyImpl>

fun main() {
    val impl = create(myFactory())
    impl.baz("baz")
}

fun <T : Any> create(cls: JsClass<T>): T {
    return js("new cls()")
}

Thanks for the response @rrlynx

It transpires I was indeed using it correctly and was indeed my lack of understanding in the Javascript side. I did not have to go as far as defining it down to the “create” method you provided.

The method I am after is somewhat hidden and I am not entirely sure why it is done this way, or if it is safe to extract certain aspects out. And this is before you consider the example I provided being a security function (i.e. I shouldn’t mess, if I don’t understand it).

The Javascript is somewhat like as follows. I was expecting access to “innerMethod” when accessing the instance returned by the factory, but that is not the case.

function MyImpl() {
    this.foo = 'bar'
}
MyImpl.prototype.innerMethod = function(prefix) {
    console.log(prefix + ' ' + this.foo)
}

function myFactory() {
    function MyWrappedImpl() {
        this.foo = 'bar'
    }
    MyWrappedImpl.prototype = new MyImpl();

    return MyWrappedImpl
}

My use was based on the examples from the npm module I was using, thus confusion why I could use the instance, but not the inner method.

Despite this, your response has oddly provided me a lot of insight and confidence around the interaction between Kotlin and JS, which is mostly driven by the fact your sample goes a lot further than any of the Kotlin docs, around explicit JS implementation and use in Kotlin.