From 5e4d2e39c93e242e4adf70570cfe35f6f7a666e2 Mon Sep 17 00:00:00 2001 From: Kris Zyp Date: Sun, 13 Sep 2020 20:26:35 -0600 Subject: [PATCH] Add/fix ESM support --- README.md | 2 +- index.mjs | 37 +++++++++++++++++++++++++++++++++++++ package.json | 5 +++++ tests/test.js | 1 + tests/test.mjs | 31 +++++++++++++++++++++++++++++++ 5 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 index.mjs create mode 100644 tests/test.mjs diff --git a/README.md b/README.md index e739394..f5e1f15 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..68a8924 --- /dev/null +++ b/index.mjs @@ -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]) + } +} \ No newline at end of file diff --git a/package.json b/package.json index b3f857f..2a9f710 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,12 @@ "benchmark": "node ./tests/benchmark.js", "test": "./node_modules/.bin/mocha tests/test*.js -u tdd" }, + "type": "commonjs", "main": "./index.js", + "exports": { + "import": "./index.mjs", + "require": "./index.js" + }, "optionalDependencies": { "msgpackr-extract": "^0.3.2" }, diff --git a/tests/test.js b/tests/test.js index 586bb32..a664bd8 100644 --- a/tests/test.js +++ b/tests/test.js @@ -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) {} diff --git a/tests/test.mjs b/tests/test.mjs new file mode 100644 index 0000000..9bdd3e8 --- /dev/null +++ b/tests/test.mjs @@ -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) + }) +})