Skip to content

Commit e85c039

Browse files
authored
Merge branch 'kriszyp:master' into chore/semantic-release
2 parents b648ec0 + 7a7e7c5 commit e85c039

File tree

7 files changed

+48
-9
lines changed

7 files changed

+48
-9
lines changed

SECURITY.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Security Policy
2+
3+
## Supported Versions
4+
5+
| Version | Supported |
6+
| ------- | ------------------ |
7+
| 1.4.x | :white_check_mark: |
8+
9+
## Reporting a Vulnerability
10+
11+
Please report security vulnerabilities to [email protected].

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { Unpackr, Decoder, unpack, decode, addExtension, FLOAT32_OPTIONS, clearSource } from './unpack'
1+
export { Unpackr, Decoder, unpack, decode, addExtension, FLOAT32_OPTIONS, clearSource, roundFloat32 } from './unpack'
22
import { Options } from './unpack'
33
export { Packr, Encoder, pack, encode } from './pack'
44
import { Transform, Readable } from 'stream'

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export { Packr, Encoder, addExtension, pack, encode, NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT } from './pack.js'
1+
export { Packr, Encoder, addExtension, pack, encode, NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT, REUSE_BUFFER_MODE } from './pack.js'
22
export { Unpackr, Decoder, C1, unpack, unpackMultiple, decode, FLOAT32_OPTIONS, clearSource, roundFloat32 } from './unpack.js'
33
export { decodeIter, encodeIter } from './iterators.js'
44
export const useRecords = false

pack.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class Packr extends Unpackr {
5858
let transitionsCount = 0
5959
let serializationsSinceTransitionRebuild = 0
6060

61-
this.pack = this.encode = function(value) {
61+
this.pack = this.encode = function(value, encodeOptions) {
6262
if (!target) {
6363
target = new ByteArrayAllocate(8192)
6464
targetView = new DataView(target.buffer, 0, 8192)
@@ -123,6 +123,11 @@ export class Packr extends Unpackr {
123123
referenceMap = null
124124
return serialized
125125
}
126+
if (encodeOptions === REUSE_BUFFER_MODE) {
127+
target.start = start
128+
target.end = position
129+
return target
130+
}
126131
return target.subarray(start, position) // position can change if we call pack again in saveStructures, so we get the buffer now
127132
} finally {
128133
if (sharedStructures) {
@@ -146,13 +151,15 @@ export class Packr extends Unpackr {
146151
if (sharedStructures.length > sharedLength) {
147152
sharedStructures = sharedStructures.slice(0, sharedLength)
148153
}
149-
154+
// we can't rely on start/end with REUSE_BUFFER_MODE since they will (probably) change when we save
155+
let returnBuffer = target.subarray(start, position)
150156
if (packr.saveStructures(sharedStructures, lastSharedStructuresLength) === false) {
151157
// get updated structures and try again if the update failed
152158
packr._mergeStructures(packr.getStructures())
153159
return packr.pack(value)
154160
}
155161
lastSharedStructuresLength = sharedLength
162+
return returnBuffer
156163
}
157164
}
158165
}
@@ -817,3 +824,4 @@ export const Encoder = Packr
817824
export { FLOAT32_OPTIONS } from './unpack.js'
818825
import { FLOAT32_OPTIONS } from './unpack.js'
819826
export const { NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT } = FLOAT32_OPTIONS
827+
export const REUSE_BUFFER_MODE = 1000

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"name": "msgpackr",
33
"author": "Kris Zyp",
4-
"version": "1.4.4",
4+
"version": "1.4.5",
55
"description": "Ultra-fast MessagePack implementation with extensions for records and structured cloning",
66
"license": "MIT",
77
"types": "./index.d.ts",
8+
"main": "./dist/index.js",
89
"keywords": [
910
"MessagePack",
1011
"msgpack",
@@ -19,6 +20,7 @@
1920
"scripts": {
2021
"benchmark": "node ./tests/benchmark.cjs",
2122
"build": "rollup -c",
23+
"dry-run": "npm publish --dry-run",
2224
"prepare": "npm run build",
2325
"semantic-release": "semantic-release",
2426
"test": "mocha tests/test**.*js -u tdd --experimental-json-modules"

tests/test-compatibility.cjs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
const data = require('./example4.json');
2-
const { pack, unpack } = require('msgpackr/pack');
2+
const { pack, unpack, Packr } = require('msgpackr/pack');
33
const chai = require('chai');
44

55
function tryRequire(module) {
66
try {
77
return require(module)
88
} catch(error) {
9+
console.log(error)
910
}
1011
}
1112
//if (typeof chai === 'undefined') { chai = require('chai') }
1213
const assert = chai.assert
1314
//if (typeof msgpackr === 'undefined') { msgpackr = require('..') }
1415
var msgpack_msgpack = tryRequire('@msgpack/msgpack');
1516
var msgpack_lite = tryRequire('msgpack-lite');
17+
var msgpack = tryRequire('msgpack');
1618

1719
const addCompatibilitySuite = (data) => () => {
1820
if (msgpack_msgpack) {
@@ -41,8 +43,22 @@ const addCompatibilitySuite = (data) => () => {
4143
assert.deepEqual(deserialized, data)
4244
})
4345
}
46+
if (msgpack) {
47+
test.skip('from msgpack', function(){
48+
var serialized = msgpack.pack(data)
49+
var deserialized = unpack(serialized)
50+
assert.deepEqual(deserialized, data)
51+
})
52+
53+
test('to msgpack', function(){
54+
var serialized = pack(data)
55+
var deserialized = msgpack.unpack(serialized)
56+
assert.deepEqual(deserialized, data)
57+
})
58+
}
4459
}
4560

4661
suite('msgpackr compatibility tests (example)', addCompatibilitySuite(require('./example.json')))
4762
suite('msgpackr compatibility tests (example4)', addCompatibilitySuite(require('./example4.json')))
48-
suite('msgpackr compatibility tests (example5)', addCompatibilitySuite(require('./example5.json')))
63+
suite('msgpackr compatibility tests (example5)', addCompatibilitySuite(require('./example5.json')))
64+
suite.skip('msgpackr compatibility tests with dates', addCompatibilitySuite({ date: new Date() }))

unpack.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ export interface Options {
2525
interface Extension {
2626
Class: Function
2727
type: number
28-
pack(value: any): Buffer | Uint8Array
29-
unpack(messagePack: Buffer | Uint8Array): any
28+
pack?(value: any): Buffer | Uint8Array
29+
unpack?(messagePack: Buffer | Uint8Array): any
30+
read?(datum: any): any
31+
write?(instance: any): any
3032
}
3133
export class Unpackr {
3234
constructor(options?: Options)

0 commit comments

Comments
 (0)