davalapar / what-the-pack Goto Github PK
View Code? Open in Web Editor NEWUltra-fast MessagePack for NodeJS & Browsers | msgpack.org[Javascript/NodeJS]
Home Page: http://npmjs.com/package/what-the-pack
License: MIT License
Ultra-fast MessagePack for NodeJS & Browsers | msgpack.org[Javascript/NodeJS]
Home Page: http://npmjs.com/package/what-the-pack
License: MIT License
Additional Optional Initialization Options
boolean
ignores object properties with undefined
valueboolean
ignores empty objectsboolean
ignores empty arraysReferences
for-loop
+ instanceof
const { extend } = MessagePack.initialize(2 ** 24);
class Vector {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
// transforms class instance to msgpack-compatible data
const encodeVector = (vector) => {
return [vector.x, vector.y];
};
// transforms data into new class instance
const decodeVector = (data) => {
return new Vector(data[0], data[1]);
};
extend(Vector, encodeVector, decodeVector);
// `extend` parameters:
// - class (we use instanceof for recognition) : `Object`
// - encodeTransformFunction : `Function`
// - decodeTransformFunction : `Function`
Algo
User
class instance in client-side can be automatically encoded only using its id
propertyid
property can be automatically transformed back into a User
class instanceUser
-> id
-> encode
-> transport
-> decode
-> id
-> User
fixext
and ext
formatfixext 1 | 11010100 | 0xd4 |
---|---|---|
fixext 2 | 11010101 | 0xd5 |
fixext 4 | 11010110 | 0xd6 |
fixext 8 | 11010111 | 0xd7 |
fixext 16 | 11011000 | 0xd8 |
fixext 1 stores an integer and a byte array whose length is 1 byte
+--------+--------+--------+
| 0xd4 | type | data |
+--------+--------+--------+
fixext 2 stores an integer and a byte array whose length is 2 bytes
+--------+--------+--------+--------+
| 0xd5 | type | data |
+--------+--------+--------+--------+
fixext 4 stores an integer and a byte array whose length is 4 bytes
+--------+--------+--------+--------+--------+--------+
| 0xd6 | type | data |
+--------+--------+--------+--------+--------+--------+
fixext 8 stores an integer and a byte array whose length is 8 bytes
+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
| 0xd7 | type | data |
+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
fixext 16 stores an integer and a byte array whose length is 16 bytes
+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
| 0xd8 | type | data
+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
+--------+--------+--------+--------+--------+--------+--------+--------+
data (cont.) |
+--------+--------+--------+--------+--------+--------+--------+--------+
ext 8 stores an integer and a byte array whose length is upto (2^8)-1 bytes:
+--------+--------+--------+========+
| 0xc7 |XXXXXXXX| type | data |
+--------+--------+--------+========+
ext 16 stores an integer and a byte array whose length is upto (2^16)-1 bytes:
+--------+--------+--------+--------+========+
| 0xc8 |YYYYYYYY|YYYYYYYY| type | data |
+--------+--------+--------+--------+========+
ext 32 stores an integer and a byte array whose length is upto (2^32)-1 bytes:
+--------+--------+--------+--------+--------+--------+========+
| 0xc9 |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ| type | data |
+--------+--------+--------+--------+--------+--------+========+
where
* XXXXXXXX is a 8-bit unsigned integer which represents N
* YYYYYYYY_YYYYYYYY is a 16-bit big-endian unsigned integer which represents N
* ZZZZZZZZ_ZZZZZZZZ_ZZZZZZZZ_ZZZZZZZZ is a big-endian 32-bit unsigned integer which represents N
* N is a length of data
* type is a signed 8-bit signed integer
* type < 0 is reserved for future extension including 2-byte type information
For implementation correctness, the byteOffset must be considered.
Usage of Buffer isn't really necessary for browsers.
We just want simpler functions that let us encode and decode data to/from UInt8Array instances.
Looking at https://github.com/feross/buffer/blob/master/index.js the implemetation actually has internal checks and assertions which could be skipped, which could speed up our encoding and decoding process in the browser.
If this ever succeeds, we can even use these UInt8Array functions on the node implementation for speed, since UInt8Arrays can be converted to Buffer instances anyways.
Notes:
fetch
requests accepts UInt8Array instances directly as ArrayBufferView
, XMLHttpRequest
also accepts UInt8Array as BufferSource
fetch
response body has .arrayBuffer()
method which returns an ArrayBuffernew UInt8Array(arrayBuffer);
( new UInt8Array() ).buffer
References
The string "WALL·E – Typeset in the Future" decodes as "W" and causes further decoding problems if it is contained by an object with other data in it.
This appears to be due to the fixstr condition in index.js, which checks if the number of characters is less than 32, rather than the number of bytes as defined by the MessagePack spec and implemented in decode(). "WALL·E – Typeset in the Future" is 30 characters long but uses 33 bytes in utf8.
internalEncode
and internalDecode
must be wrapped in ONE try-catch block to catch encoding and decoding errors. Doing the try-catch block within (instead outside) will nest it recursively and will be costly.
README
const { encode, decode } = //...
const response = await fetch('https://example.com', {
method: 'POST',
body: JSON.stringify({foo: true}),
headers: { 'content-type': 'application/octet-stream' }
});
if (!response.ok) {
throw new HTTPError('Fetch error:', response.statusText);
}
const parsed = decode(await response.arrayBuffer());
console.log(parsed);
//=> `{ asd: '123'}`
const headers = { 'content-type': 'application/octet-stream' };
const parsed = decode(await ky.post('https://example.com', {
headers,
body: encode({ some: 'data' }),
}).arrayBuffer());
When used with websocket clients with .binaryType
= 'arraybuffer'
, messages received will be instances of ArrayBuffer
. .decode
function on the other hand always expects a Buffer instance, hence what should be called is .decode(Buffer.from(MessageEvent.data))
.
Could be user error, but using register to try to use dictionary results in the same output as not using it. I'm using exactly as per docs example.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.