Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The module's structure makes it hard to customize #136

Open
jean-emmanuel opened this issue Jan 12, 2019 · 3 comments
Open

The module's structure makes it hard to customize #136

jean-emmanuel opened this issue Jan 12, 2019 · 3 comments

Comments

@jean-emmanuel
Copy link
Contributor

jean-emmanuel commented Jan 12, 2019

Not really an issue, but while trying to add support for utf8 encoded string arguments to my application, I found out osc's reader / writer functions could not be overridden easily due to the module's structure that uses a scoped copy of the osc variable (if I understood correctly).

Anyway, I ended up with the following workaround:

// require this file in node instead of 'osc'

var osc = require('osc/src/osc.js'),
    transports = require('osc/src/platforms/osc-node.js'),
    utf8 = require('utf8')

// import needed transport modules
osc.UDPPort = transports.UDPPort
osc.TCPSocketPort = transports.TCPSocketPort

osc.writeUtfString = (str)=>{
    return osc.writeString(utf8.encode(str))
}

osc.readUtfString = (dv, offsetState)=>{
    return utf8.decode(osc.readString(dv, offsetState))
}

osc.argumentTypes.s = osc.argumentTypes.S = {
    reader: 'readUtfString',
    writer: 'writeUtfString'
}

module.exports = osc
@colinbdclark
Copy link
Owner

Hi @jean-emmanuel,

Thanks for the note. I'm not sure I understand the issue you faced when trying to override osc.js' implementation. Can you provide any more details?

There is a pull request from @okofish, #92, which has been languishing for a long time due to lack of time on my part, but it also introduces UTF-8 encoding support. Could be a good opportunity to get this into a release.

Thanks again!

@jean-emmanuel
Copy link
Contributor Author

jean-emmanuel commented Jan 14, 2019

Hi, thanks for your answer ! Well at first I tried the following but it didn't work because osc.writeUtfString and osc.readUtfString couldn't not be found at runtime, which is caused I guess by this pattern: https://github.com/colinbdclark/osc.js/blob/master/src/platforms/osc-node.js#L40

var osc = require('osc'),
    utf8 = require('utf8')

osc.writeUtfString = (str)=>{
    return osc.writeString(utf8.encode(str))
}

osc.readUtfString = (dv, offsetState)=>{
    return utf8.decode(osc.readString(dv, offsetState))
}

osc.argumentTypes.s = osc.argumentTypes.S = {
    reader: 'readUtfString',
    writer: 'writeUtfString'
}

module.exports = osc

I opened this issue more to share the workaround rather than to point a flaw in the lib by the way. I had not read this PR, it sound cool indeed, I'll try to get into its details and help if I can.

@colinbdclark
Copy link
Owner

The design of osc.js was always intended to be as public and shared as possible and to avoid the sorts of encapsulation that prevent user modification, monkey patching, and unanticipated user change (for many, this may be a controversial or confusing statement, but I've written about this in more detail elsewhere). The mechanism by which modules are loaded into the shared osc namespace in Node.js was always a bit of a hack (interestingly, you'd not face this problem in the browser using plain old web idioms), and you've run into exactly the consequences of this issue.

So I agree with you @jean-emmanuel that this is a bug and should be fixed. I'm glad you've got a work-around for now, since it may take some time before I can address this. It is of course at least peripherally related to #119, too.

Thanks for explaining the issue you encountered, and sorry for the trouble.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants