diff --git a/index.js b/index.js index 1e6cd75..98e0b7b 100644 --- a/index.js +++ b/index.js @@ -1,36 +1,2 @@ -const packModule = require('./pack') -exports.Packr = packModule.Packr -exports.addExtension = require('./pack').addExtension -exports.Encoder = exports.Packr -const unpackModule = require('./unpack') -const extractor = tryRequire('msgpackr-extract') -if (extractor) - unpackModule.setExtractor(extractor.extractStrings) -exports.Unpackr = unpackModule.Unpackr -exports.Decoder = exports.Unpackr -exports.C1 = unpackModule.C1 -exports.PackrStream = require('./stream').PackrStream -exports.UnpackrStream = require('./stream').UnpackrStream -exports.EncoderStream = exports.PackrStream -exports.DecoderStream = exports.UnpackrStream -const packr = new exports.Packr({ useRecords: false }) -exports.unpack = unpackModule.unpack -exports.unpackMultiple = unpackModule.unpackMultiple -exports.pack = packModule.pack -exports.decode = unpackModule.unpack -exports.encode = packModule.pack -exports.useRecords = false -exports.mapsAsObjects = true -exports.FLOAT32_OPTIONS = unpackModule.FLOAT32_OPTIONS -Object.assign(exports, unpackModule.FLOAT32_OPTIONS) - -function tryRequire(moduleId) { - try { - 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) - 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 +export { Packr, Encoder, addExtension, pack, encode } from './pack.js' +export { Unpackr, Decoder, C1, unpack, unpackMultiple, decode, FLOAT32_OPTIONS } from './unpack.js' \ No newline at end of file diff --git a/node.js b/node.js new file mode 100644 index 0000000..ed4a13f --- /dev/null +++ b/node.js @@ -0,0 +1,23 @@ +export { Packr, Encoder, addExtension, pack, encode, NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT } from './pack.js' +export { Unpackr, Decoder, C1, unpack, unpackMultiple, decode, FLOAT32_OPTIONS } from './unpack.js' +export { PackrStream, UnpackrStream, PackrStream as EncoderStream, UnpackrStream as DecoderStream } from './stream.js' +import { setExtractor } from './unpack.js' +import { createRequire } from 'module' + +const extractor = tryRequire('msgpackr-extract') +if (extractor) + setExtractor(extractor.extractStrings) +/* +Object.assign(exports, unpackModule.FLOAT32_OPTIONS) +*/ +function tryRequire(moduleId) { + try { + let require = createRequire(import.meta.url) + 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/pack.js b/pack.js index f593eef..9877967 100644 --- a/pack.js +++ b/pack.js @@ -1,9 +1,5 @@ "use strict" -let unpackModule = require('./unpack') -let Unpackr = unpackModule.Unpackr -let mult10 = unpackModule.mult10 -let C1Type = unpackModule.C1Type -const typedArrays = unpackModule.typedArrays +import { Unpackr, mult10, C1Type, typedArrays, addExtension as unpackAddExtension } from './unpack.js' let textEncoder try { textEncoder = new TextEncoder() @@ -18,7 +14,7 @@ let targetView let position = 0 let safeEnd const RECORD_SYMBOL = Symbol('record-id') -class Packr extends Unpackr { +export class Packr extends Unpackr { constructor(options) { super(options) this.offset = 0 @@ -560,7 +556,6 @@ class Packr extends Unpackr { position = 0 } } -exports.Packr = Packr function copyBinary(source, target, targetOffset, offset, endOffset) { while (offset < endOffset) { @@ -753,17 +748,20 @@ function insertIds(serialized, idsToInsert) { return serialized } -exports.addExtension = function(extension) { +export function addExtension(extension) { if (extension.Class) { if (!extension.pack && !extension.write) throw new Error('Extension has no pack or write function') extensionClasses.unshift(extension.Class) extensions.unshift(extension) } - unpackModule.addExtension(extension) + unpackAddExtension(extension) } let defaultPackr = new Packr({ useRecords: false }) -exports.pack = defaultPackr.pack -exports.encode = defaultPackr.pack -Object.assign(exports, exports.FLOAT32_OPTIONS = unpackModule.FLOAT32_OPTIONS) +export const pack = defaultPackr.pack +export const encode = defaultPackr.pack +export const Encoder = Packr +export { FLOAT32_OPTIONS } from './unpack.js' +import { FLOAT32_OPTIONS } from './unpack.js' +export const { NEVER, ALWAYS, DECIMAL_ROUND, DECIMAL_FIT } = FLOAT32_OPTIONS diff --git a/package.json b/package.json index 83d5977..e7363ee 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "msgpackr", "author": "Kris Zyp", - "version": "1.2.11", + "version": "1.3.0", "description": "Ultra-fast MessagePack implementation with extensions for records and structured cloning", "license": "MIT", "types": "./index.d.ts", @@ -18,11 +18,12 @@ }, "scripts": { "benchmark": "node ./tests/benchmark.js", - "test": "./node_modules/.bin/mocha tests/test*.js -u tdd" + "build": "rollup -c", + "prepare": "npm run build", + "test": "./node_modules/.bin/mocha --experimental-json-modules tests/test.js -u tdd" }, - "type": "commonjs", - "module": "./index.mjs", - "main": "./index.js", + "type": "module", + "main": "./dist/node.cjs", "browser": { "./index.mjs": "./browser.mjs", "./index.js": "./browser.js" @@ -30,22 +31,19 @@ "exports": { ".": { "node": { - "import": "./index.mjs", - "require": "./index.js" + "require": "./dist/node.cjs", + "import": "./node.js" }, "default": { - "import": "./browser.mjs", - "require": "./browser.js" + "import": "./browser.js" } }, "./pack": { "node": { - "import": "./index.mjs", - "require": "./index.js" + "import": "./index.js" }, "default": { - "import": "./pack.mjs", - "require": "./pack.js" + "import": "./pack.js" } }, "./unpack": { @@ -60,14 +58,15 @@ } }, "optionalDependencies": { - "msgpackr-extract": "^1.0.8" + "msgpackr-extract": "^1.0.9" }, "devDependencies": { "@types/node": "latest", "chai": "^4", - "mocha": "^4", - "webpack": "^4.44.1", - "webpack-cli": "^3.3.2", - "webpack-command": "^0.4.1" + "esm": "^3.2.25", + "mocha": "^8.1.3", + "rollup": "^1.20.3", + "rollup-plugin-babel-minify": "^9.0.0", + "@rollup/plugin-json": "^4.1.0" } } diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 0000000..929930d --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,45 @@ +import minify from "rollup-plugin-babel-minify"; +import json from "@rollup/plugin-json"; + +export default [ + { + input: "node.js", + output: [ + { + file: "dist/node.cjs", + format: "cjs" + } + ] + }, + { + input: "index.js", + output: { + file: "dist/index.js", + format: "umd", + name: "msgpackr" + } + }, + { + input: "index.js", + plugins: [minify({ + })], + output: { + file: "dist/index.min.js", + format: "umd", + name: "msgpackr" + } + }, + { + input: "tests/test.js", + plugins: [json()], + external: ['chai', '../index.js'], + output: { + file: "dist/test.js", + format: "iife", + globals: { + chai: 'chai', + './index.js': 'msgpackr', + }, + } + } +]; \ No newline at end of file diff --git a/stream.js b/stream.js index 04187cf..17cb5ca 100644 --- a/stream.js +++ b/stream.js @@ -1,10 +1,10 @@ "use strict" -var Transform = require('stream').Transform -var Packr = require('./pack').Packr -const { read, getPosition, Unpackr, clearSource } = require('./unpack') +import { Transform } from 'stream' +import { Packr } from './pack.js' +import { read, getPosition, Unpackr, clearSource } from './unpack.js' var DEFAULT_OPTIONS = {objectMode: true} -class PackrStream extends Transform { +export class PackrStream extends Transform { constructor(options) { if (!options) options = {} @@ -23,7 +23,7 @@ class PackrStream extends Transform { } } -class UnpackrStream extends Transform { +export class UnpackrStream extends Transform { constructor(options) { if (!options) options = {} @@ -58,6 +58,3 @@ class UnpackrStream extends Transform { if (callback) callback() } } - -exports.PackrStream = PackrStream -exports.UnpackrStream = UnpackrStream diff --git a/tests/benchmark-stream.js b/tests/benchmark-stream.cjs similarity index 100% rename from tests/benchmark-stream.js rename to tests/benchmark-stream.cjs diff --git a/tests/benchmark.js b/tests/benchmark.cjs similarity index 100% rename from tests/benchmark.js rename to tests/benchmark.cjs diff --git a/tests/index.html b/tests/index.html index 288fbf6..b2294a5 100644 --- a/tests/index.html +++ b/tests/index.html @@ -19,7 +19,7 @@ mocha.setup('tdd') - +