Skip to content
This repository has been archived by the owner on Jul 3, 2019. It is now read-only.

Improve documentation for simple object read write? #126

Open
ErichBSchulz opened this issue Mar 16, 2018 · 4 comments
Open

Improve documentation for simple object read write? #126

ErichBSchulz opened this issue Mar 16, 2018 · 4 comments

Comments

@ErichBSchulz
Copy link

Hey folk,

Just posting this so others may hopefully spend less time on this than I did to do simple object read-writes because I'm on Node 8.9.x and reading buffers is in a state of flux.

My JSON output from get item looked like this:

data <Buffer 7b 0a 20 20 22 68 61 73 68 22 3a 20 22 24 32 61 24 30 38 24 43 6e 6e 76 6b 44 36 74 67 32 43 57 56 71 2f 69 67 74 2f 59 45 65 69 49 47 32 38 4f 41 45 ... >

After way to long, I offer this as an ES6 example of how to make this into a promise whose return value you can work with:

const cacache = require('cacache/en')
const path = '/somewherespecial'
const put = obj => {                                                             
     return cacache.put(path, obj.key, JSON.stringify(obj, null, 2)) 
   }
const get = key => {                                                             
     return cacache.get(path, key)                               
     .then(encodedRecord => {                                                
       const {data, metadata} = encodedRecord                                
       console.log('metadata', metadata)                                     
       const string = String.fromCharCode.apply(null, new Uint16Array(data)) 
       const objOut = JSON.parse(string)                                     
       console.log('objOut', objOut)                                         
       return objOut                                                         
     })                                                                      
   }

Perhaps you want to include something similar in the Readme? or maybe even close this in the knowledge someone else maybe able to find it here.

@ErichBSchulz
Copy link
Author

really the key bit is:

String.fromCharCode.apply(null, new Uint16Array(data))

@zkat
Copy link
Owner

zkat commented Mar 16, 2018

What's wrong with JSON.parse(data.toString('utf8'))? That's usually what you wanna do when converting buffers to strings.

@ErichBSchulz
Copy link
Author

ha ha - now you know what? I bet that works to - I might have been googling the wrong key words but it took me quite a while to find the right way to do it - perhaps menting that in the doco could save others. Happy to do a PR if you want.

@gkohen
Copy link

gkohen commented Oct 3, 2018

First of all let me thank you for writing an awesome library. I think what @ErichBSchulz was pointing out is that DX(Developer eXperience) for that API was not intuitive. I think if you just want to get strings and don't care about buffering having a convenience method and/or a more complete example involving strings would be useful. Especially for novice developers. @zkat , I'd love to hear you thoughts on this.
PS: I've tripped on the same wire as @ErichBSchulz.

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

No branches or pull requests

3 participants