Skip to content

Commit

Permalink
feat: use byte@2 and remove debug, utility deps (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored May 24, 2024
1 parent 22b1c55 commit f45adff
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 16 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ jobs:
with:
os: 'ubuntu-latest'
version: '8, 10, 12, 14, 16, 18, 20, 22'

secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

build-with-zig:
strategy:
fail-fast: false
Expand Down
5 changes: 4 additions & 1 deletion lib/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
'use strict';

var util = require('util');
var has = require('utility').has;

exports.DEFAULT_CLASSNAME = {
boolean: 'boolean',
Expand Down Expand Up @@ -167,6 +166,10 @@ function JavaExceptionError(obj, withType) {

util.inherits(JavaExceptionError, Error);

function has(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}

exports.isJavaException = function (obj) {
if (has(obj, 'detailMessage') && has(obj, 'stackTrace')) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

'use strict';

var debug = require('debug')('hessian.js:utils');
var debug = require('util').debuglog('hessian.js:utils');
var object = require('./object');

var MAX_INT_8 = exports.MAX_INT_8 = Math.pow(2, 7);
Expand Down
2 changes: 1 addition & 1 deletion lib/v1/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

'use strict';

var debug = require('debug')('hessian:v1:decoder');
var debug = require('util').debuglog('hessian:v1:decoder');
var ByteBuffer = require('byte');
var is = require('is-type-of');
var utils = require('../utils');
Expand Down
2 changes: 1 addition & 1 deletion lib/v1/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
'use strict';

var ByteBuffer = require('byte');
var debug = require('debug')('hessian:v1:encoder');
var debug = require('util').debuglog('hessian:v1:encoder');
var utils = require('../utils');
var javaObject = require('../object');
var is = require('is-type-of');
Expand Down
2 changes: 1 addition & 1 deletion lib/v2/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

var util = require('util');
var is = require('is-type-of');
var debug = require('debug')('hessian:v2:decoder');
var debug = require('util').debuglog('hessian:v2:decoder');
var DecoderV1 = require('../v1/decoder');
var utils = require('../utils');
var isJavaException = require('../object').isJavaException;
Expand Down
32 changes: 27 additions & 5 deletions lib/v2/encoder.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
'use strict';

var debug = require('debug')('hessian:v2:encoder');
var is = require('is-type-of');
var debug = require('util').debuglog('hessian:v2:encoder');
var util = require('util');
var EncoderV1 = require('../v1/encoder');
var javaObject = require('../object');
var utility = require('utility');
var Long = require('long');
var float32Test = require('../utils').float32Test;

Expand Down Expand Up @@ -82,6 +79,31 @@ proto.writeInt = function (val) {
return this;
};

// fork from https://github.com/node-modules/utility/blob/master/src/number.ts#L13
// http://www.2ality.com/2013/10/safe-integers.html
// http://es6.ruanyifeng.com/#docs/number
const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || Math.pow(2, 53) - 1;
// const MIN_SAFE_INTEGER = -MAX_SAFE_INTEGER;
const MAX_SAFE_INTEGER_STR = String(MAX_SAFE_INTEGER);
const MAX_SAFE_INTEGER_STR_LENGTH = MAX_SAFE_INTEGER_STR.length;

/**
* Detect a number string can safe convert to Javascript Number.
*
* @param {String} s number format string, like `"123"`, `"-1000123123123123123123"`
* @return {Boolean}
*/
function isSafeNumberString(s) {
if (s[0] === '-') {
s = s.substring(1);
}
if (s.length < MAX_SAFE_INTEGER_STR_LENGTH ||
(s.length === MAX_SAFE_INTEGER_STR_LENGTH && s <= MAX_SAFE_INTEGER_STR)) {
return true;
}
return false;
}

/**
* encode long
*
Expand Down Expand Up @@ -129,7 +151,7 @@ proto.writeInt = function (val) {
*/
proto.writeLong = function (val) {
if (typeof val === 'string') {
if (!utility.isSafeNumberString(val)) {
if (!isSafeNumberString(val)) {
val = Long.fromString(val);
} else {
val = Number(val);
Expand Down
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"scripts": {
"lint": "make jshint",
"test": "make test",
"ci": "npm run lint && npm run test",
"ci": "npm run lint && make cov",
"build::platform": "napi build --cargo-cwd ./lib/v2rust --platform --release ./lib/v2rust/harness/dist"
},
"repository": {
Expand All @@ -33,11 +33,9 @@
"homepage": "https://github.com/node-modules/hessian.js",
"dependencies": {
"@protobufjs/codegen": "^2.0.4",
"byte": "^1.4.1",
"debug": "^3.2.6",
"byte": "^2.0.0",
"is-type-of": "^1.2.1",
"long": "^4.0.0",
"utility": "^1.15.0"
"long": "^4.0.0"
},
"devDependencies": {
"beautify-benchmark": "^0.2.4",
Expand Down

0 comments on commit f45adff

Please sign in to comment.