@alexgyver/bson 2.2.0 → 2.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.
- package/bson.js +13 -6
- package/package.json +1 -1
package/bson.js
CHANGED
|
@@ -41,15 +41,19 @@ const BS_CODE_PREFIX = "__BS#";
|
|
|
41
41
|
* @returns {Object|Array|*}
|
|
42
42
|
*/
|
|
43
43
|
export function decodeBson(buf, codes = []) {
|
|
44
|
-
if (!(buf instanceof Uint8Array)) return undefined;
|
|
44
|
+
if (!(buf instanceof Uint8Array) || !buf.length) return undefined;
|
|
45
45
|
|
|
46
46
|
const reader = {
|
|
47
47
|
buf,
|
|
48
48
|
offset: 0,
|
|
49
|
+
cont: 'root',
|
|
49
50
|
decoder: new TextDecoder(),
|
|
51
|
+
error(e) {
|
|
52
|
+
return new Error(e + ' in ' + JSON.stringify(this.cont));
|
|
53
|
+
},
|
|
50
54
|
read(len) {
|
|
51
55
|
if (this.offset + len > buf.length) {
|
|
52
|
-
throw
|
|
56
|
+
throw this.error("Overflow");
|
|
53
57
|
}
|
|
54
58
|
const res = this.buf.subarray(this.offset, this.offset + len);
|
|
55
59
|
this.offset += len;
|
|
@@ -60,7 +64,9 @@ export function decodeBson(buf, codes = []) {
|
|
|
60
64
|
}
|
|
61
65
|
};
|
|
62
66
|
|
|
63
|
-
|
|
67
|
+
let res = _decode(reader, codes);
|
|
68
|
+
if (reader.offset != reader.buf.length) throw reader.error("Broken packet");
|
|
69
|
+
return res;
|
|
64
70
|
}
|
|
65
71
|
|
|
66
72
|
function _decode(r, codes) {
|
|
@@ -76,14 +82,15 @@ function _decode(r, codes) {
|
|
|
76
82
|
let key;
|
|
77
83
|
|
|
78
84
|
while (true) {
|
|
85
|
+
r.cont = cont;
|
|
79
86
|
let res = _decode(r, codes);
|
|
80
87
|
|
|
81
88
|
if (res && res.__close !== undefined) {
|
|
82
89
|
if (isArr === res.__close) {
|
|
83
|
-
if (!isArr && expect) throw
|
|
90
|
+
if (!isArr && expect) throw r.error("Missed value");
|
|
84
91
|
return cont;
|
|
85
92
|
} else {
|
|
86
|
-
throw
|
|
93
|
+
throw r.error("Wrong close");
|
|
87
94
|
}
|
|
88
95
|
}
|
|
89
96
|
|
|
@@ -98,7 +105,7 @@ function _decode(r, codes) {
|
|
|
98
105
|
} else if (data & BS_CONT_CLOSE) {
|
|
99
106
|
return { __close: !!(data & BS_CONT_ARR) };
|
|
100
107
|
} else {
|
|
101
|
-
throw
|
|
108
|
+
throw r.error("Unknown cont");
|
|
102
109
|
}
|
|
103
110
|
// break;
|
|
104
111
|
|