Erlpack is a fast encoder and decoder for the Erlang Term Format (version 131) for Python and JavaScript.
- Null
- Booleans
- Strings
- Atoms
- Unicode Strings
- Floats
- Integers
- Longs
- Longs over 64 bits
- Objects
- Arrays
- Tuples
- PIDs
- Ports
- Exports
- References
leterlpack=require("erlpack");packed=erlpack.pack({'a': true,'list': ['of',3,'things','to','pack']});Note: Unpacking requires the binary data be a Uint8Array or Buffer. For those using electron/libchromium see the gotcha below.
leterlpack=require("erlpack");letunpacked=null;letpacked=newBuffer('','binary');try{unpacked=erlpack.unpack(packed);}catch(e){// got an exception parsing}Some versions of libchromium replace the native data type backing TypedArrays with a custom data type called blink::WebArrayBuffer. To keep erlpack' dependencies simple this data type is not supported directly. If you're using Electron / Libchromium you need to convert the blink::WebArrayBuffer into a node::Buffer before passing to erlpack. You will need to add this code into your native package somewhere:
v8::Local<v8::Value> ConvertToNodeBuffer(const v8::Local<v8::Object>& blinkArray)
{
if (node::Buffer::HasInstance(blinkArray)) {
return blinkArray;
}
elseif (blinkArray->IsArrayBufferView()) {
auto byteArray = v8::ArrayBufferView::Cast(*blinkArray);
returnnode::Buffer::Copy(v8::Isolate::GetCurrent(), (constchar*)byteArray->Buffer()->GetContents().Data(), byteArray->ByteLength()).ToLocalChecked();
}
return v8::Local<v8::Primitive>(v8::Null(v8::Isolate::GetCurrent()));
}Then in JavaScript something like:
letpacked=NativeUtils.convertToNodeBuffer(newUint8Array(binaryPayload));// unpack now using erlpack.unpack(packed)- None
- Booleans
- Strings
- Atoms
- Unicode Strings
- Floats
- Integers
- Longs
- Longs over 64 bits
- Dictionaries
- Lists
- Tuples
- User Types (via an encode hook)
- PIDs
- Ports
- Exports
- References
fromerlpackimportpackpacked=pack(["thing", "to", "pack"])fromerlpackimportunpackunpacked=unpack(packed)fromerlpackimportAtom, packpacked=pack(Atom('hello'))fromerlpackimportErlangTermEncoderdefencode_hook(obj):
ifisinstance(obj, datetime.datetime):
returnobj.isoformat()
encoder=ErlangTermEncoder(encode_hook=encode_hook)
packed=encoder.pack(datetime.datetime(2015, 12, 25, 12, 23, 55))fromerlpackimportpack, AtomclassUser(object):
def__init__(self, name, age):
self.name=nameself.age=agedef__erlpack__(self):
return {
Atom('name'): self.name,
Atom('age'): self.age
}
u=User(name='Jake', age=23)
packed=pack(u)Discord has moved away from Go internally and so we do not maintain a version of erlpack in Go ourselves. However, all is not lost!, please check out: https://github.com/JakeMakesStuff/go-erlpack