microcbor 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,169 @@
1
+ import { getFloat16Precision, getFloat32Precision, Precision } from "fp16";
2
+ export function encodingLength(value) {
3
+ if (value === false) {
4
+ return 1;
5
+ }
6
+ else if (value === true) {
7
+ return 1;
8
+ }
9
+ else if (value === null) {
10
+ return 1;
11
+ }
12
+ else if (value === undefined) {
13
+ return 1;
14
+ }
15
+ else if (typeof value === "number") {
16
+ return numberEncodingLength(value);
17
+ }
18
+ else if (typeof value === "string") {
19
+ return stringEncodingLength(value);
20
+ }
21
+ else if (value instanceof Uint8Array) {
22
+ return bytesEncodingLength(value);
23
+ }
24
+ else if (Array.isArray(value)) {
25
+ let length = argumentEncodingLength(value.length);
26
+ for (const element of value) {
27
+ length += encodingLength(element);
28
+ }
29
+ return length;
30
+ }
31
+ else if (typeof value === "object") {
32
+ const keys = Object.keys(value);
33
+ let length = argumentEncodingLength(keys.length);
34
+ for (const key of keys) {
35
+ if (typeof key === "string") {
36
+ length += stringEncodingLength(key);
37
+ length += encodingLength(value[key]);
38
+ }
39
+ else {
40
+ throw new Error("object keys must be strings");
41
+ }
42
+ }
43
+ return length;
44
+ }
45
+ else {
46
+ throw new Error("invalid value");
47
+ }
48
+ }
49
+ function argumentEncodingLength(argument) {
50
+ if (argument < 24) {
51
+ return 1;
52
+ }
53
+ else if (argument < 0x100) {
54
+ return 1 + 1;
55
+ }
56
+ else if (argument < 0x10000) {
57
+ return 1 + 2;
58
+ }
59
+ else if (argument < 0x100000000) {
60
+ return 1 + 4;
61
+ }
62
+ else {
63
+ return 1 + 8;
64
+ }
65
+ }
66
+ function numberEncodingLength(value) {
67
+ if (Object.is(value, 0)) {
68
+ return integerEncodingLength(value);
69
+ }
70
+ else if (Object.is(value, -0)) {
71
+ return floatEncodingLength(value);
72
+ }
73
+ else if (Math.floor(value) === value &&
74
+ Number.MIN_SAFE_INTEGER <= value &&
75
+ value <= Number.MAX_SAFE_INTEGER) {
76
+ return integerEncodingLength(value);
77
+ }
78
+ else {
79
+ return floatEncodingLength(value);
80
+ }
81
+ }
82
+ function integerEncodingLength(value) {
83
+ if (value < 0) {
84
+ return argumentEncodingLength(-value - 1);
85
+ }
86
+ else {
87
+ return argumentEncodingLength(value);
88
+ }
89
+ }
90
+ function floatEncodingLength(value) {
91
+ if (getFloat16Precision(value) === Precision.Exact) {
92
+ return 1 + 2;
93
+ }
94
+ else if (getFloat32Precision(value) === Precision.Exact) {
95
+ return 1 + 4;
96
+ }
97
+ else {
98
+ return 1 + 8;
99
+ }
100
+ }
101
+ function stringEncodingLength(value) {
102
+ const length = byteLength(value);
103
+ return argumentEncodingLength(length) + length;
104
+ }
105
+ function bytesEncodingLength(value) {
106
+ const length = value.byteLength;
107
+ return argumentEncodingLength(length) + length;
108
+ }
109
+ // https://github.com/feross/buffer/blob/57caad4450d241207066ca3832fb8e9095ad402f/index.js#L434
110
+ function byteLength(string) {
111
+ let codePoint;
112
+ const length = string.length;
113
+ let leadSurrogate = null;
114
+ let bytes = 0;
115
+ for (let i = 0; i < length; ++i) {
116
+ codePoint = string.charCodeAt(i);
117
+ // is surrogate component
118
+ if (codePoint > 0xd7ff && codePoint < 0xe000) {
119
+ // last char was a lead
120
+ if (!leadSurrogate) {
121
+ // no lead yet
122
+ if (codePoint > 0xdbff) {
123
+ // unexpected trail
124
+ bytes += 3;
125
+ continue;
126
+ }
127
+ else if (i + 1 === length) {
128
+ // unpaired lead
129
+ bytes += 3;
130
+ continue;
131
+ }
132
+ // valid lead
133
+ leadSurrogate = codePoint;
134
+ continue;
135
+ }
136
+ // 2 leads in a row
137
+ if (codePoint < 0xdc00) {
138
+ bytes += 3;
139
+ leadSurrogate = codePoint;
140
+ continue;
141
+ }
142
+ // valid surrogate pair
143
+ codePoint =
144
+ (((leadSurrogate - 0xd800) << 10) | (codePoint - 0xdc00)) + 0x10000;
145
+ }
146
+ else if (leadSurrogate) {
147
+ // valid bmp char, but last char was a lead
148
+ bytes += 3;
149
+ }
150
+ leadSurrogate = null;
151
+ // encode utf8
152
+ if (codePoint < 0x80) {
153
+ bytes += 1;
154
+ }
155
+ else if (codePoint < 0x800) {
156
+ bytes += 2;
157
+ }
158
+ else if (codePoint < 0x10000) {
159
+ bytes += 3;
160
+ }
161
+ else if (codePoint < 0x110000) {
162
+ bytes += 4;
163
+ }
164
+ else {
165
+ throw new Error("Invalid code point");
166
+ }
167
+ }
168
+ return bytes;
169
+ }
package/lib/index.d.ts CHANGED
@@ -1,2 +1,7 @@
1
- export * from "./encode.js";
2
- export * from "./decode.js";
1
+ export type { CBORValue, CBORMap, CBORArray } from "./types.js";
2
+ export { encode, Encoder } from "./encode.js";
3
+ export { decode } from "./decode.js";
4
+ export { encodeStream } from "./encodeStream.js";
5
+ export { decodeStream } from "./decodeStream.js";
6
+ export { encodingLength } from "./encodingLength.js";
7
+ export { UnsafeIntegerError } from "./utils.js";
package/lib/index.js CHANGED
@@ -1,2 +1,6 @@
1
- export * from "./encode.js";
2
- export * from "./decode.js";
1
+ export { encode, Encoder } from "./encode.js";
2
+ export { decode } from "./decode.js";
3
+ export { encodeStream } from "./encodeStream.js";
4
+ export { decodeStream } from "./decodeStream.js";
5
+ export { encodingLength } from "./encodingLength.js";
6
+ export { UnsafeIntegerError } from "./utils.js";
package/lib/types.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export declare type CBORValue = undefined | null | boolean | number | string | Uint8Array | CBORArray | CBORMap;
2
+ export interface CBORArray extends Array<CBORValue> {
3
+ }
4
+ export interface CBORMap {
5
+ [key: string]: CBORValue;
6
+ }
package/lib/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/lib/utils.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export declare const maxSafeInteger: bigint;
2
+ export declare const minSafeInteger: bigint;
3
+ export declare class UnsafeIntegerError extends RangeError {
4
+ readonly value: bigint;
5
+ constructor(message: string, value: bigint);
6
+ }
package/lib/utils.js ADDED
@@ -0,0 +1,8 @@
1
+ export const maxSafeInteger = BigInt(Number.MAX_SAFE_INTEGER);
2
+ export const minSafeInteger = BigInt(Number.MIN_SAFE_INTEGER);
3
+ export class UnsafeIntegerError extends RangeError {
4
+ constructor(message, value) {
5
+ super(message);
6
+ this.value = value;
7
+ }
8
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "microcbor",
3
- "version": "0.1.0",
4
- "description": "Encode JSON values as canonical CBOR",
3
+ "version": "0.2.1",
4
+ "description": "Encode JavaScript values as canonical CBOR",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
7
7
  "types": "lib/index.d.ts",
@@ -27,11 +27,11 @@
27
27
  },
28
28
  "homepage": "https://github.com/joeltg/microcbor#readme",
29
29
  "devDependencies": {
30
- "ava": "^4.0.0-alpha.2",
31
- "cbor": "^8.0.0",
32
- "typescript": "^4.4.2"
30
+ "ava": "^4.3.1",
31
+ "cbor": "^8.0.2",
32
+ "typescript": "^4.8.2"
33
33
  },
34
34
  "dependencies": {
35
- "fp16": "^0.1.2"
35
+ "fp16": "^0.2.0"
36
36
  }
37
37
  }