Skip to content

Commit

Permalink
Merge branch 'modules'
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed May 14, 2021
2 parents 539aee5 + 0028b77 commit 8af8983
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 167 deletions.
38 changes: 2 additions & 36 deletions index.js
Original file line number Diff line number Diff line change
@@ -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])
}
}
export { Packr, Encoder, addExtension, pack, encode } from './pack.js'
export { Unpackr, Decoder, C1, unpack, unpackMultiple, decode, FLOAT32_OPTIONS } from './unpack.js'
23 changes: 23 additions & 0 deletions node.js
Original file line number Diff line number Diff line change
@@ -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])
}
}
22 changes: 10 additions & 12 deletions pack.js
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -560,7 +556,6 @@ class Packr extends Unpackr {
position = 0
}
}
exports.Packr = Packr

function copyBinary(source, target, targetOffset, offset, endOffset) {
while (offset < endOffset) {
Expand Down Expand Up @@ -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
35 changes: 17 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -18,34 +18,32 @@
},
"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"
},
"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": {
Expand All @@ -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"
}
}
45 changes: 45 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -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',
},
}
}
];
13 changes: 5 additions & 8 deletions stream.js
Original file line number Diff line number Diff line change
@@ -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 = {}
Expand All @@ -23,7 +23,7 @@ class PackrStream extends Transform {
}
}

class UnpackrStream extends Transform {
export class UnpackrStream extends Transform {
constructor(options) {
if (!options)
options = {}
Expand Down Expand Up @@ -58,6 +58,3 @@ class UnpackrStream extends Transform {
if (callback) callback()
}
}

exports.PackrStream = PackrStream
exports.UnpackrStream = UnpackrStream
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
mocha.setup('tdd')
</script>
<script src="../dist/index.js"></script>
<script src="../tests/test.js"></script>
<script src="../dist/test.js"></script>
<div id="mocha"></div>
<script>
mocha.run()
Expand Down
16 changes: 9 additions & 7 deletions tests/test-compatibility.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
var data = require('./example4.json');
import * as data from './example4.json';
import * as msgpackr from '..'
import * as chai from 'chai'

function tryRequire(module) {
/*function tryRequire(module) {
try {
return require(module)
} catch(error) {
}
}
if (typeof chai === 'undefined') { chai = require('chai') }
//if (typeof chai === 'undefined') { chai = require('chai') }
assert = chai.assert
if (typeof msgpackr === 'undefined') { msgpackr = require('..') }
//if (typeof msgpackr === 'undefined') { msgpackr = require('..') }
var msgpack_msgpack = tryRequire('@msgpack/msgpack');
var msgpack_lite = tryRequire('msgpack-lite');
var msgpack_lite = tryRequire('msgpack-lite');*/
var unpack = msgpackr.unpack
var pack = msgpackr.pack

Expand Down Expand Up @@ -43,6 +45,6 @@ addCompatibilitySuite = (data) => () => {
}
}

suite('msgpackr compatibility tests (example)', addCompatibilitySuite(require('./example.json')))
/*suite('msgpackr compatibility tests (example)', addCompatibilitySuite(require('./example.json')))
suite('msgpackr compatibility tests (example4)', addCompatibilitySuite(require('./example4.json')))
suite('msgpackr compatibility tests (example5)', addCompatibilitySuite(require('./example5.json')))
suite('msgpackr compatibility tests (example5)', addCompatibilitySuite(require('./example5.json')))*/
48 changes: 48 additions & 0 deletions tests/test-node-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { PackrStream, UnpackrStream } from '../node.js'
import stream from 'stream'

suite('msgpackr node stream tests', function(){
test('serialize/parse stream', () => {
const serializeStream = new PackrStream({
})
const parseStream = new UnpackrStream()
serializeStream.pipe(parseStream)
const received = []
parseStream.on('data', data => {
received.push(data)
})
const messages = [{
name: 'first'
}, {
name: 'second'
}, {
name: 'third'
}, {
name: 'third',
extra: [1, 3, { foo: 'hi'}, 'bye']
}]
for (const message of messages)
serializeStream.write(message)
return new Promise((resolve, reject) => {
setTimeout(() => {
assert.deepEqual(received, messages)
resolve()
}, 10)
})
})
test('stream from buffer', () => new Promise(async resolve => {
const parseStream = new UnpackrStream()
let values = []
parseStream.on('data', (value) => {
values.push(value)
})
parseStream.on('end', () => {
assert.deepEqual(values, [1, 2])
resolve()
})
let bufferStream = new stream.Duplex()
bufferStream.pipe(parseStream)
bufferStream.push(new Uint8Array([1, 2]))
bufferStream.push(null)
}))
})
Loading

0 comments on commit 8af8983

Please sign in to comment.