Skip to content

Commit

Permalink
feat: add msg-pack parser
Browse files Browse the repository at this point in the history
  • Loading branch information
shahradelahi committed Feb 6, 2024
1 parent 345efa7 commit 55b41b3
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 7 deletions.
20 changes: 20 additions & 0 deletions docs/examples/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,23 @@ const value = client.get('key');

console.log(value);
```

## 3. Use `msgpack` serializer

By default `JSON` serializer is used. You can use following code to use `msgpack` serializer.

```typescript
import { Client } from '@litehex/storage-box';
import { FsDriver } from '@litehex/storage-box/driver';
import { MSGPack } from '@litehex/storage-box/parser';

const filePath = resovle(process.cwd(), 'data.pack');
const driver = new FsDriver(filePath, { parser: MSGPack });
const client = new Client(driver);

client.set('key', 'value');

const value = client.get('key');

console.log(value);
```
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
},
"packageManager": "[email protected]",
"dependencies": {
"@msgpack/msgpack": "3.0.0-beta2",
"debounce": "^2.0.0",
"type-fest": "^4.10.2"
},
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,4 @@ export type * from '@/typings.ts';

// -----------

export { JsonMap } from '@/parser';
export { JsonMap, MSGPack } from '@/parser';
1 change: 1 addition & 0 deletions src/parser/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * as JsonMap from './json-map.ts';
export * as MSGPack from './msg-pack.ts';
26 changes: 26 additions & 0 deletions src/parser/msg-pack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { JsonValue } from 'type-fest';
import { decode, encode } from '@msgpack/msgpack';

export function parse(data: any): Map<string, JsonValue> {
if (typeof data !== 'string') {
throw new Error('MessagePack data must be a string');
}
const buffer = Buffer.from(data, 'base64');
const decoded = decode(buffer);

if (typeof decoded !== 'object') {
throw new Error('MessagePack data must be an object');
}

return new Map(Object.entries(decoded as object));
}

export function stringify(data: any): string {
if (!(data instanceof Map)) {
throw new Error('MessagePack data must be a Map');
}
const obj = Object.fromEntries(data);
const encoded = encode(obj);
const buffer = Buffer.from(encoded.buffer, encoded.byteOffset, encoded.byteLength);
return buffer.toString('base64');
}
38 changes: 32 additions & 6 deletions tests/parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { JsonMap } from '@/parser';
import { JsonMap, MSGPack } from '@/parser';
import { expect } from 'chai';

describe('JSON-MAP', () => {
const data = {
foo: 'bar',
bar: 'baz'
};
const data = {
foo: 'bar',
bar: 'baz'
};

describe('JSON-MAP', () => {
it('stringify', () => {
const hashMap = new Map<string, string>();
hashMap.set('foo', 'bar');
Expand All @@ -20,3 +20,29 @@ describe('JSON-MAP', () => {
expect(hashMap.get('foo')).to.equal('bar');
});
});

describe('MSGPack-MAP', () => {
function isBase64(str: string): boolean {
// Regular expression to check if a string is base64 encoded
const base64Regex = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;

return base64Regex.test(str);
}

it('stringify', () => {
const hashMap = new Map<string, string>();
hashMap.set('foo', 'bar');
hashMap.set('bar', 'baz');
const msgPack = MSGPack.stringify(hashMap);
expect(isBase64(msgPack)).to.be.true;

console.log(msgPack);
});

it('parse', () => {
const msgPack = MSGPack.stringify(new Map(Object.entries(data)));
const hashMap = MSGPack.parse(msgPack);
expect(hashMap.get('foo')).to.equal('bar');
expect(hashMap.get('bar')).to.equal('baz');
});
});

0 comments on commit 55b41b3

Please sign in to comment.