cborg 4.5.5 → 4.5.7
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/CHANGELOG.md +4 -0
- package/lib/2bytes.js +2 -2
- package/lib/3string.js +1 -2
- package/lib/byte-utils.js +8 -2
- package/lib/decode.js +4 -1
- package/package.json +1 -1
- package/types/lib/3string.d.ts.map +1 -1
- package/types/lib/byte-utils.d.ts.map +1 -1
- package/types/lib/decode.d.ts.map +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
## [4.5.7](https://github.com/rvagg/cborg/compare/v4.5.6...v4.5.7) (2026-01-21)
|
|
2
|
+
|
|
3
|
+
## [4.5.6](https://github.com/rvagg/cborg/compare/v4.5.5...v4.5.6) (2026-01-21)
|
|
4
|
+
|
|
1
5
|
## [4.5.5](https://github.com/rvagg/cborg/compare/v4.5.4...v4.5.5) (2026-01-20)
|
|
2
6
|
|
|
3
7
|
### Trivial Changes
|
package/lib/2bytes.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Token, Type } from './token.js'
|
|
2
2
|
import { assertEnoughData, decodeErrPrefix } from './common.js'
|
|
3
3
|
import * as uint from './0uint.js'
|
|
4
|
-
import { compare, fromString
|
|
4
|
+
import { compare, fromString } from './byte-utils.js'
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* @typedef {import('../interface').ByteWriter} ByteWriter
|
|
@@ -17,7 +17,7 @@ import { compare, fromString, slice } from './byte-utils.js'
|
|
|
17
17
|
*/
|
|
18
18
|
function toToken (data, pos, prefix, length) {
|
|
19
19
|
assertEnoughData(data, pos, prefix + length)
|
|
20
|
-
const buf = slice(
|
|
20
|
+
const buf = data.slice(pos + prefix, pos + prefix + length)
|
|
21
21
|
return new Token(Type.bytes, buf, prefix + length)
|
|
22
22
|
}
|
|
23
23
|
|
package/lib/3string.js
CHANGED
|
@@ -2,7 +2,6 @@ import { Token, Type } from './token.js'
|
|
|
2
2
|
import { assertEnoughData, decodeErrPrefix } from './common.js'
|
|
3
3
|
import * as uint from './0uint.js'
|
|
4
4
|
import { encodeBytes } from './2bytes.js'
|
|
5
|
-
import { slice } from './byte-utils.js'
|
|
6
5
|
|
|
7
6
|
const textDecoder = new TextDecoder()
|
|
8
7
|
|
|
@@ -52,7 +51,7 @@ function toToken (data, pos, prefix, length, options) {
|
|
|
52
51
|
assertEnoughData(data, pos, totLength)
|
|
53
52
|
const tok = new Token(Type.string, toStr(data, pos + prefix, pos + totLength), totLength)
|
|
54
53
|
if (options.retainStringBytes === true) {
|
|
55
|
-
tok.byteValue = slice(
|
|
54
|
+
tok.byteValue = data.slice(pos + prefix, pos + totLength)
|
|
56
55
|
}
|
|
57
56
|
return tok
|
|
58
57
|
}
|
package/lib/byte-utils.js
CHANGED
|
@@ -33,13 +33,19 @@ export function asU8A (buf) {
|
|
|
33
33
|
return isBuffer(buf) ? new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength) : buf
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
// Threshold for manual UTF-8 encoding vs native methods.
|
|
37
|
+
// Node.js Buffer.from: crossover ~24 chars
|
|
38
|
+
// Browser TextEncoder: crossover ~200 chars
|
|
39
|
+
const FROM_STRING_THRESHOLD_BUFFER = 24
|
|
40
|
+
const FROM_STRING_THRESHOLD_TEXTENCODER = 200
|
|
41
|
+
|
|
36
42
|
export const fromString = useBuffer
|
|
37
43
|
? // eslint-disable-line operator-linebreak
|
|
38
44
|
/**
|
|
39
45
|
* @param {string} string
|
|
40
46
|
*/
|
|
41
47
|
(string) => {
|
|
42
|
-
return string.length
|
|
48
|
+
return string.length >= FROM_STRING_THRESHOLD_BUFFER
|
|
43
49
|
? // eslint-disable-line operator-linebreak
|
|
44
50
|
// @ts-ignore
|
|
45
51
|
globalThis.Buffer.from(string)
|
|
@@ -51,7 +57,7 @@ export const fromString = useBuffer
|
|
|
51
57
|
* @param {string} string
|
|
52
58
|
*/
|
|
53
59
|
(string) => {
|
|
54
|
-
return string.length
|
|
60
|
+
return string.length >= FROM_STRING_THRESHOLD_TEXTENCODER ? textEncoder.encode(string) : utf8ToBytes(string)
|
|
55
61
|
}
|
|
56
62
|
|
|
57
63
|
/**
|
package/lib/decode.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { decodeErrPrefix } from './common.js'
|
|
2
2
|
import { Type } from './token.js'
|
|
3
3
|
import { jump, quick } from './jump.js'
|
|
4
|
+
import { asU8A } from './byte-utils.js'
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* @typedef {import('./token.js').Token} Token
|
|
@@ -183,7 +184,9 @@ function decodeFirst (data, options) {
|
|
|
183
184
|
throw new Error(`${decodeErrPrefix} data to decode must be a Uint8Array`)
|
|
184
185
|
}
|
|
185
186
|
options = Object.assign({}, defaultDecodeOptions, options)
|
|
186
|
-
|
|
187
|
+
// Convert Buffer to plain Uint8Array for faster slicing in decode path
|
|
188
|
+
const u8aData = asU8A(data)
|
|
189
|
+
const tokeniser = options.tokenizer || new Tokeniser(u8aData, options)
|
|
187
190
|
const decoded = tokensToObject(tokeniser, options)
|
|
188
191
|
if (decoded === DONE) {
|
|
189
192
|
throw new Error(`${decodeErrPrefix} did not find any content to decode`)
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"3string.d.ts","sourceRoot":"","sources":["../../lib/3string.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"3string.d.ts","sourceRoot":"","sources":["../../lib/3string.js"],"names":[],"mappings":"AA0DA;;;;;;GAMG;AACH,0CANW,UAAU,OACV,MAAM,SACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;;;GAMG;AACH,oCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;;;GAMG;AACH,qCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAED;;;;;;GAMG;AACH,qCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAIjB;AAGD;;;;;;GAMG;AACH,qCANW,UAAU,OACV,MAAM,UACN,MAAM,WACN,aAAa,GACX,KAAK,CAQjB;AAED,8CAAuC;yBA1G1B,OAAO,cAAc,EAAE,UAAU;4BACjC,OAAO,cAAc,EAAE,aAAa;sBAbrB,YAAY;4BAGZ,aAAa"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"byte-utils.d.ts","sourceRoot":"","sources":["../../lib/byte-utils.js"],"names":[],"mappings":"AAuBA;;;GAGG;AACH,2BAHW,UAAU,GAAC,MAAM,EAAE,GACjB,UAAU,CAQtB;
|
|
1
|
+
{"version":3,"file":"byte-utils.d.ts","sourceRoot":"","sources":["../../lib/byte-utils.js"],"names":[],"mappings":"AAuBA;;;GAGG;AACH,2BAHW,UAAU,GAAC,MAAM,EAAE,GACjB,UAAU,CAQtB;AA0MD;;;;GAIG;AACH,4BAJW,UAAU,MACV,UAAU,GACR,MAAM,CAgBlB;AA6CD;;;GAGG;AACH,kDAHW,MAAM,EAAE,GACN,MAAM,CAkBlB;AA3TD,gCAMkD;AA+BlD,mCAGe,MAAM,iDAYN,MAAM,yCAIhB;AAOE,+BAHI,MAAM,EAAE,GACN,UAAU,CAItB;AAIG;;;;GAIG;AAEH,6BALW,UAAU,SACV,MAAM,OACN,MAAM,2BAQhB;AAcD;;;;GAIG;AACH,+BAJW,UAAU,EAAE,UACZ,MAAM,GACJ,UAAU,CActB;AAwBD;;;GAGG;AACH,4BAHW,MAAM,GACJ,UAAU,CAMtB;AAaD;;;GAGG;AACH,yBAHW,UAAU,GACR,MAAM,CAQlB;AAiBH;;;GAGG;AACD,6BAHS,MAAM,GAAC,UAAU,GACf,UAAU,CAQpB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../../lib/decode.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../../lib/decode.js"],"names":[],"mappings":"oBAMa,OAAO,YAAY,EAAE,KAAK;4BAC1B,OAAO,cAAc,EAAE,aAAa;8BACpC,OAAO,cAAc,EAAE,eAAe;AAUnD;;GAEG;AACH,kCAFgB,eAAe;IAG7B;;;OAGG;IACH,kBAHW,UAAU,YACV,aAAa,EAMvB;IAHC,aAAa;IACb,kCAAgB;IAChB,8CAAsB;IAGxB,cAEC;IAED,gBAEC;IAED,mCAgBC;CACF;AA8ED;;;;GAIG;AACH,0CAJW,eAAe,WACf,aAAa,GACX,GAAG,6BAAW,CAoC1B;AAyBD;;;;GAIG;AACH,6BAJW,UAAU,YACV,aAAa,GACX,GAAG,CAQf;AAlCD;;;;GAIG;AACH,kCAJW,UAAU,YACV,aAAa,GACX,CAAC,GAAG,EAAE,UAAU,CAAC,CAkB7B;AAzID,mCAAiC;AADjC,kCAA+B"}
|