Skip to content

Commit

Permalink
doc
Browse files Browse the repository at this point in the history
  • Loading branch information
gfx committed May 4, 2019
1 parent 4cec7f1 commit 7463c53
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ const extensionCodec = new ExtensionCodec();
// Set<T>
extensionCodec.register({
type: 0,
encode: (object: unknown) => {
encode: (object: unknown): Uint8Array | null => {
if (object instanceof Set) {
return encode([...object]);
} else {
return null;
}
},
decode: (data) => {
decode: (data: Uint8Array) => {
const array = decode(data) as Array<any>;
return new Set(array);
},
Expand All @@ -69,14 +69,14 @@ extensionCodec.register({
// Map<T>
extensionCodec.register({
type: 1,
encode: (object: unknown) => {
encode: (object: unknown): Uint8Array => {
if (object instanceof Map) {
return encode([...object]);
} else {
return null;
}
},
decode: (data) => {
decode: (data: Uint8Array) => {
const array = decode(data) as Array<[unknown, unknown]>;
return new Map(array);
},
Expand All @@ -91,6 +91,18 @@ const decoded = decode(encoded, { extensionCodec });

Not that extension types for custom objects must be `[0, 127]`, while `[-1, -128]` is reserved to MessagePack itself.

## Prerequsites

* ES5 language features
* Typed Arrays (ES2015; [Can I use Typed Arrays?](https://caniuse.com/#feat=typedarrays))
* String.prototype.padStart (ES2017; [caniuse](https://caniuse.com/#feat=pad-start-end))

You can use polyfills for them.

### NodeJS

If you use this library in NodeJS, v10 or later is required.

## Distrubition

The NPM package distributed in npmjs.com includes both ES2015+ and ES5 files:
Expand Down
1 change: 0 additions & 1 deletion src/Encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export class Encoder {
}

resizeBuffer(newSize: number) {
// TODO: ensure the size to be multiple of 4 and use Uint32Array for performance
const newBuffer = new ArrayBuffer(newSize);

new Uint8Array(newBuffer).set(new Uint8Array(this.view.buffer));
Expand Down
8 changes: 4 additions & 4 deletions test/ExtensionCodec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ describe("ExtensionCodec", () => {
// Set<T>
extensionCodec.register({
type: 0,
encode: (object: unknown) => {
encode: (object: unknown): Uint8Array | null => {
if (object instanceof Set) {
return encode([...object]);
} else {
return null;
}
},
decode: (data) => {
decode: (data: Uint8Array) => {
const array = decode(data) as Array<any>;
return new Set(array);
},
Expand All @@ -60,14 +60,14 @@ describe("ExtensionCodec", () => {
// Map<T>
extensionCodec.register({
type: 1,
encode: (object: unknown) => {
encode: (object: unknown): Uint8Array | null => {
if (object instanceof Map) {
return encode([...object]);
} else {
return null;
}
},
decode: (data) => {
decode: (data: Uint8Array) => {
const array = decode(data) as Array<[unknown, unknown]>;
return new Map(array);
},
Expand Down

0 comments on commit 7463c53

Please sign in to comment.