Skip to content

Commit

Permalink
Add/fix ESM support
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Sep 14, 2020
1 parent 66b21be commit 5e4d2e3
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ The following options properties can be provided to the Packr or Unpackr constru
* `mapsAsObjects` - If `true`, this will decode MessagePack maps and JS `Object`s with the map entries decoded to object properties. If `false`, maps are decoded as JavaScript `Map`s. This is disabled by default if `useRecords` is enabled (which allows `Map`s to be preserved), and is enabled by default if `useRecords` is disabled.
* `useFloat32` - This will enable msgpackr to encode non-integer numbers as `float32`. See next section for possible values.
* `variableMapSize` - This will use varying map size definition (fixmap, map16, map32) based on the number of keys when encoding objects, which yields slightly more compact encodings (for small objects), but is typically 5-10% slower during encoding. This is only relevant when record extension is disabled.
* `copyBuffers` - When decoding a MessagePack with binary data (Buffers are encoded as binary data), copy the buffer rather than providing a slice/view of the buffer. If you want your input data to be discarded or modified while the decoded embedded buffer continues to be used, you can use this option (there is extra overhead to copying).
* `copyBuffers` - When decoding a MessagePack with binary data (Buffers are encoded as binary data), copy the buffer rather than providing a slice/view of the buffer. If you want your input data to be collected or modified while the decoded embedded buffer continues to live on, you can use this option (there is extra overhead to copying).
* `useTimestamp32` - Encode JS `Date`s in 32-bit format when possible by dropping the milliseconds. This is a more efficient encoding of dates. You can also cause dates to use 32-bit format by manually setting the milliseconds to zero (`date.setMilliseconds(0)`).

### 32-bit Float Options
Expand Down
37 changes: 37 additions & 0 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import packModule from './pack.js'
import unpackModule from './unpack.js'

export const Packr = packModule.Packr
export const addExtension = packModule.addExtension
export const Encoder = packModule.Packr
let extractor = tryRequire('msgpackr-extract')
if (extractor)
unpackModule.setExtractor(extractor.extractStrings)
export const Unpackr = unpackModule.Unpackr
export const Decoder = unpackModule.Unpackr
import stream from './stream.js'
export const PackrStream = stream.PackrStream
export const UnpackrStream = stream.UnpackrStream
export const EncoderStream = stream.PackrStream
export const DecoderStream = stream.UnpackrStream
let packr = new packModule.Packr({ useRecords: false })
export const unpack = packr.unpack
export const pack = packr.pack
export const decode = packr.unpack
export const encode = packr.pack
export const ALWAYS = 1
export const DECIMAL_ROUND = 3
export const DECIMAL_FIT = 4


function tryRequire(moduleId) {
try {
let require = module.createRequire()
return require(moduleId)
} catch (error) {
if (typeof window == 'undefined')
console.warn('Native extraction module not loaded, msgpackr will still run, but with decreased performance. ' + error.message.split('\n')[0])
else
console.warn('For browser usage, directly use msgpackr/unpack or msgpackr/pack modules. ' + error.message.split('\n')[0])
}
}
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
"benchmark": "node ./tests/benchmark.js",
"test": "./node_modules/.bin/mocha tests/test*.js -u tdd"
},
"type": "commonjs",
"main": "./index.js",

This comment has been minimized.

Copy link
@chase-moskal

chase-moskal Sep 29, 2020

might be worth including the unofficial package.json field "module": "./index.mjs", which some community tooling like pika might respond to

"exports": {
"import": "./index.mjs",
"require": "./index.js"
},
"optionalDependencies": {
"msgpackr-extract": "^0.3.2"
},
Expand Down
1 change: 1 addition & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var inflateSync = zlib.inflateSync
var deflateSync = zlib.brotliCompressSync
var inflateSync = zlib.brotliDecompressSync
var constants = zlib.constants
import('./test.mjs')
try {
// var { decode, encode } = require('msgpack-lite')
} catch (error) {}
Expand Down
31 changes: 31 additions & 0 deletions tests/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { pack, unpack } from '../index.mjs'
var ITERATIONS = 10000

suite('msgpackr basic tests from module', function(){
test('pack/unpack data', function(){
var data = {
data: [
{ a: 1, name: 'one', type: 'odd', isOdd: true },
{ a: 2, name: 'two', type: 'even'},
{ a: 3, name: 'three', type: 'odd', isOdd: true },
{ a: 4, name: 'four', type: 'even'},
{ a: 5, name: 'five', type: 'odd', isOdd: true },
{ a: 6, name: 'six', type: 'even', isOdd: null }
],
description: 'some names',
types: ['odd', 'even'],
convertEnumToNum: [
{ prop: 'test' },
{ prop: 'test' },
{ prop: 'test' },
{ prop: 1 },
{ prop: 2 },
{ prop: [undefined] },
{ prop: null }
]
}
var serialized = pack(data)
var deserialized = unpack(serialized)
assert.deepEqual(deserialized, data)
})
})

2 comments on commit 5e4d2e3

@kriszyp
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chase-moskal noticed on kawanet/msgpack-lite#113 you might be looking for msgpack implementation with ESM support.

@chase-moskal
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kriszyp i'm happy to see that you're bringing msgpack into the future! awesome work, i hope you keep it up!

Please sign in to comment.