Types 2.0 cbor (#12355)

* Types 2.0 cbor

* Respond to reviewer feedback.
This commit is contained in:
Jeffery Grajkowski
2016-11-02 07:15:10 -07:00
committed by Masahiro Wakame
parent e1b8d6829c
commit 59a23e55da
3 changed files with 120 additions and 0 deletions

65
cbor/cbor-tests.ts Normal file
View File

@@ -0,0 +1,65 @@
/// <reference types="assert" />
/// <reference types="node" />
import cbor = require('cbor');
import assert = require('assert');
import fs = require('fs');
var encoded = cbor.encode(true); // returns <Buffer f5>
cbor.decodeFirst(encoded, function(error, obj) {
// error != null if there was an error
// obj is the unpacked object
assert.ok(obj === true);
});
// Use integers as keys?
var m = new Map();
m.set(1, 2);
encoded = cbor.encode(m); // <Buffer a1 01 02>
var d = new cbor.Decoder();
d.on('data', function(obj: any) {
console.log(obj);
});
var s = fs.createReadStream('foo');
s.pipe(d);
var d2 = new cbor.Decoder({ input: '00', encoding: 'hex' });
d.on('data', function(obj: any) {
console.log(obj);
});
try {
console.log(cbor.decodeFirstSync('02')); // 2
console.log(cbor.decodeAllSync('0202')); // [2, 2]
} catch (e) {
// throws on invalid input
}
class Bar {
three: number;
constructor() {
this.three = 3;
}
}
const enc = new cbor.Encoder()
enc.addSemanticType(Bar, (encoder, b) => {
encoder.pushAny(b.three);
})
class Foo {
one: number;
two: string;
}
const d3 = new cbor.Decoder({
tags: {
64000: (val) => {
// check val to make sure it's an Array as expected, etc.
const foo = new Foo();
foo.one = val[0];
foo.two = val[1];
return foo;
}
}
})

36
cbor/index.d.ts vendored Normal file
View File

@@ -0,0 +1,36 @@
// Type definitions for cbor 2.0.2
// Project: https://github.com/hildjj/node-cbor
// Definitions by: Jeffery Grajkowski <https://github.com/pushplay>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
import stream = require("stream");
export function decode(input: Buffer | string): any;
export function decodeAll(input: Buffer | string, callback: (error: any, objs: any[]) => void): void;
export function decodeAllSync(input: Buffer | string): any[];
export function decodeFirst(input: Buffer | string, callback: (error: any, obj: any) => void): void;
export function decodeFirstSync(input: Buffer | string): any;
export function encode(input: any): Buffer;
export class Decoder extends stream.Transform {
constructor(params?: {
input?: Buffer | string;
encoding?: string;
tags?: {[tag: number]: (val: any[]) => any}
});
}
export class Encoder extends stream.Transform {
constructor();
addSemanticType<T>(type: new (...args: any[]) => T, encodeFunction: (encoder: Encoder, t: T) => void): void;
pushAny(input: any): void;
}
export namespace leveldb {
export function decode(input: Buffer | string): any[];
export function encode(input: any): Buffer;
export const buffer: boolean;
export const name: string;
}

19
cbor/tsconfig.json Normal file
View File

@@ -0,0 +1,19 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noImplicitAny": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"cbor-tests.ts"
]
}