RusticiSoftware/TinCanJS

Node.js Issues

Closed this issue · 2 comments

I've been trying to integrate tincanJS into a node project for sending statements, and running into a couple of problems.

  1. If you don't provide a username and password, you get a "TypeError: Cannot call method 'toString' of null", as TinCanJS sets authorization to null, and then xhr tries to call the toString method when setting headers.
  2. Any attempt to send statements without a callback gets a "Error: Synchronous XHR processing not implemented".
  3. Sending statements asynchronously works well enough, but does not conform to node JS standards for async functions - ie, the pattern callback(err, result) - and what it does return is not documented anywhere I could find.
  1. The library at the moment probably isn't flexible enough to handle no auth being provided, but it should be I agree. I'll try to slate some time soon for it, but if you want to submit a PR that'd likely be faster.
  2. Since doing sync requests from Node.js is generally not suggested I don't have plans (nor would really accept the changes) to add sync handling to that particular environment file. The long term plan is to remove sync handling completely from both environments (node + browser) so that we can adopt a promises based approach.
  3. The callback arguments should be node conformant. Can you provide a code sample that doesn't work as expected? (Certainly have been issues in this area before, so I'm happy to check.) The arguments provided should be either null or the error message, and then the xhr that was used. Some calls do translation of the response on success, such as retrieveStatement which returns the TinCan.Statement itself as the second argument when possible. Trying to support backwards compat and effectively 3 environments (browser, IE, node) has been a pretty big pain, so there could certainly still be bugs in there. You might have a look at the test files for more sample code, or even better to create a test case that shows the issue you are running into. See:

https://github.com/RusticiSoftware/TinCanJS/tree/master/test/js/unit

The design of the API is lacking in this area as this was the first library created and based on an older design that was even less sufficient. I have it on my docket to solidify the context handling and callback structure to use proper response objects like the other OSS libraries have. Unfortunately I can't provide a timeline for any of that.

I should follow this up by saying that if you know what you are doing (and it sounds like you do) that it would be pretty easy to implement your own async handling that has a different callback argument structure and/or overall request handling (for instance to use Node's builtin HTTP client lib or the request lib from npm). Take a look at the current Environment/Node.js file and you can see that it is really just one method that needs to be overloaded:

https://github.com/RusticiSoftware/TinCanJS/blob/master/src/Environment/Node.js

It doesn't help you with the call structure, but it is a pretty isolated piece of code otherwise, particularly if you are controlling your environment.