question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Buffers and custom encoding examples

See original GitHub issue

Is there a canonical set of examples for no string encoding and or custom encodings?

The first example I found was https://github.com/Level/levelup/blob/master/test/inject-encoding-test.js but that requires learning about msgpack-js.

Second example I found was https://github.com/Level/levelup/issues/412, which described binary (a string encoding format, aliasing latin1 as per nodejs documentation) somehow returning a Buffer

I just want to put(keyBuffer, valueBuffer), get(keyBuffer, (err, valueBuffer) => {}) and it isn’t entirely clear how to do that…

At least with a Buffer, I could then just push it through something like https://github.com/dominictarr/varstruct for type checking.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
peakjicommented, Jan 24, 2017

@dcousens the encoding here is actually the codec name of your custom-encoding , which represents a pair of encode and decode functions, and a buffer option. utf8 and binaryare just built-in bidirectional codecs, in fact, theres also one called json.

1reaction
peakjicommented, Jan 24, 2017

I just want to put(keyBuffer, valueBuffer), get(keyBuffer, (err, valueBuffer) => {}) and it isn’t entirely clear how to do that…

Set keyEncoding: "binary" and valueEncoding : "binary" in options of any main interface method.

It should probably also document the lifetime of any Buffer passed, since the arguments are not copied-on-use the underlying data committed can change… 😨

We can’t document it in levelup because the lifetime depends on the implementation of storage backend. For the put method in the default leveldown backend, it is handled by database_async.cc#L175, so its not safe to modify buffer values until callback:

db.open(function(err) {
	var buf = Buffer.from([0x00, 0x01, 0x02]);
	db.put("test", buf, {
		valueEncoding: "binary"
	}, function(err) {
		buf[0] = 0x04;
		db.get("test", {
			valueEncoding: "binary"
		}, function(err, val) {
			console.log(val);	// <Buffer 03 01 02>
		});
	});
	buf[0] = 0x03;
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Encoding | Protocol Buffers - Google Developers
A protocol buffer message is a series of key-value pairs. The binary version of a message just uses the field's number as the...
Read more >
Using Buffers in Node.js - DigitalOcean
Byte encoding is the format of the byte. A buffer in Node.js uses the UTF-8 encoding scheme by default if it's initialized with...
Read more >
Managing Typed Buffers
The system supports encoding and decoding for the typed buffer. ... Listing 3-5 Putting Data in a Message Buffer - Example 1.
Read more >
Managing groups of resources with argument buffers
This sample code project shows how to specify, encode, set, ... The Metal Shading Language defines argument buffers as custom structures.
Read more >
Encoding Infinispan caches and marshalling data
Using alternative and custom marshaller implementations ... For example if you configure cache encoding as text/plain; charset=UTF-8 then REST clients ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found