Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(node): node 12 compatible util module and improved console #11165

Merged
merged 21 commits into from
Oct 3, 2019
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
ee4555a
feat(node): node 12 compatible util module and improved console
janvennemann Aug 21, 2019
28ca293
fix: fix typo and add tests for weak map/set
janvennemann Aug 22, 2019
7af73e9
feat: enable color mode for inspect
janvennemann Aug 22, 2019
2d4d0c8
chore: update eslint config
janvennemann Aug 22, 2019
1edff08
chore: fix linting issues
janvennemann Aug 22, 2019
934fd56
fix: disable bigint type checks
janvennemann Aug 22, 2019
5492f35
fix: rename isRegexp usage to isRegExp
janvennemann Aug 22, 2019
d6170c8
fix: use correct should assertion syntax
janvennemann Aug 22, 2019
595189f
chore: disable lint warning
janvennemann Aug 22, 2019
a847183
feat: add custom inspect behavior for buffers
janvennemann Aug 23, 2019
78569bb
Merge branch 'master' into TIMOB-27345
sgtcoolguy Aug 30, 2019
b93ec69
Merge branch 'master' into TIMOB-27345
sgtcoolguy Sep 6, 2019
a6d1d89
chore: add node license header
janvennemann Sep 10, 2019
8193f8e
style(eslint): adjust linting rules, fix various warnings
janvennemann Jan 1, 2019
9e79450
chore: add node license header
janvennemann Sep 10, 2019
7b1cc8d
Merge branch 'master' into TIMOB-27345
sgtcoolguy Sep 12, 2019
5f0038d
Merge branch 'master' into TIMOB-27345
sgtcoolguy Sep 13, 2019
648f1e3
Merge branch 'master' into TIMOB-27345
sgtcoolguy Sep 13, 2019
714219f
Merge branch 'master' into TIMOB-27345
sgtcoolguy Sep 18, 2019
8ed8a82
Merge branch 'master' into TIMOB-27345
janvennemann Sep 26, 2019
0d381ce
test(util): update format tests to pass on ios and android
sgtcoolguy Oct 3, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
"sourceType": "script"
},
"globals": {
"Ti": false,
"Titanium": false,
"__filename": false,
"__dirname": false,
"kroll": false
"Ti": "readonly",
"Titanium": "readonly",
"__filename": "readonly",
"__dirname": "readonly",
"kroll": "readonly"
},
"rules": {
"strict": ["error", "global"]
},
"overrides": [
{
"files": [ "tests/Resources/es6.*.js" ],
"files": [ "tests/Resources/es6.*.js", "tests/Resources/util.test.js" ],
Copy link
Contributor

Choose a reason for hiding this comment

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

at this point, perhaps just bite the bullet and say all test/Resources/** are 2017/module? Or even 2018/2019?

"parserOptions": {
"ecmaVersion": 2017,
"sourceType": "module"
Expand Down Expand Up @@ -46,7 +46,7 @@
{
"files": [ "common/Resources/**/*.js" ],
Copy link
Contributor

Choose a reason for hiding this comment

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

So maybe merge this and the entry above?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had to bump the version to 2018 for either object rest spread or async/generator support, not sure anymore.

Maybe we should even switch to axway/+babel or manually setting the parser to babel-eslint for stuff like this that gets transpiled?

"parserOptions": {
"ecmaVersion": 2017,
"ecmaVersion": 2018,
"sourceType": "module"
},
"rules": {
Expand Down
30 changes: 30 additions & 0 deletions common/Resources/ti.internal/extensions/js/console.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { formatWithOptions } from '../node/internal/util/inspect';

const nativeDebug = console.debug;
const nativeError = console.error;
const nativeInfo = console.info;
const nativeLog = console.log;
const nativeWarn = console.warn;

const kColorInspectOptions = { colors: true };
const kNoColorInspectOptions = {};

console.debug = function (...args) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Out of the scope of this PR, but we do have an open ticket to expand our console support. @ewanharris had added time/timeLog before, but it'd be good to start moving this all into this common area.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was actually thinking to go one step ahead and adopt more of Nodes console API but then left it as is to not blow this out of proportion.

Is there any open ticket for the extended console functionality?

Copy link
Contributor

Choose a reason for hiding this comment

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

nativeDebug.call(console, formatWithOptions(kColorInspectOptions, ...args));
};

console.error = function (...args) {
nativeError.call(console, formatWithOptions(kNoColorInspectOptions, ...args));
};

console.info = function (...args) {
nativeInfo.call(console, formatWithOptions(kColorInspectOptions, ...args));
};

console.log = function (...args) {
nativeLog.call(console, formatWithOptions(kColorInspectOptions, ...args));
};

console.warn = function (...args) {
nativeWarn.call(console, formatWithOptions(kNoColorInspectOptions, ...args));
};
1 change: 1 addition & 0 deletions common/Resources/ti.internal/extensions/js/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// Load all JavaScript extensions/polyfills
import './console';
import './Error';
6 changes: 3 additions & 3 deletions common/Resources/ti.internal/extensions/node/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@ function deepEqual(actual, expected, strictness, references) {
}

let comparison = COMPARE_TYPE.Object;
if (util.types.isRegexp(actual)) { // RegExp source and flags should match
if (!util.types.isRegexp(expected) || actual.flags !== expected.flags || actual.source !== expected.source) {
if (util.types.isRegExp(actual)) { // RegExp source and flags should match
if (!util.types.isRegExp(expected) || actual.flags !== expected.flags || actual.source !== expected.source) {
return false;
}
// continue on to check properties...
Expand Down Expand Up @@ -624,7 +624,7 @@ function checkError(actual, expected, message) {
// Error type - check type matches
// Error instance - compare properties
if (typeof expected === 'object') {
if (util.types.isRegexp(expected)) {
if (util.types.isRegExp(expected)) {
return expected.test(actual); // does the error match the RegExp expression? if so, pass
}

Expand Down
74 changes: 65 additions & 9 deletions common/Resources/ti.internal/extensions/node/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,17 @@
* a Uint8Array in any of our APIs that take a Ti.Buffer and eventually deprecating/removing Ti.Buffer.
*/

import { isInsideNodeModules } from './internal/util';
import {
customInspectSymbol,
getOwnNonIndexProperties,
isBuffer,
isInsideNodeModules,
propertyFilter
} from './internal/util';

import { inspect as utilInspect } from './internal/util/inspect';

const { ALL_PROPERTIES, ONLY_ENUMERABLE } = propertyFilter;

// https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings
const TI_CODEC_MAP = new Map();
Expand Down Expand Up @@ -51,6 +61,8 @@ const uint8DoubleArray = new Uint8Array(doubleArray.buffer);
const floatArray = new Float32Array(1);
const uint8FloatArray = new Uint8Array(floatArray.buffer);

let INSPECT_MAX_BYTES = 50;

class Buffer {
/**
* Constructs a new buffer.
Expand Down Expand Up @@ -80,17 +92,23 @@ class Buffer {

const tiBuffer = arg;
let start = encodingOrOffset;
this._tiBuffer = tiBuffer;
if (start === undefined) {
start = 0;
}
this.byteOffset = start;
if (length === undefined) {
this.length = tiBuffer.length - this.byteOffset;
} else {
this.length = length;
length = tiBuffer.length - start;
}
this._isBuffer = true;
Object.defineProperties(this, {
byteOffset: {
value: start
},
length: {
value: length
},
_tiBuffer: {
value: tiBuffer
}
});
// FIXME: Support .buffer property that holds an ArrayBuffer!
}

Expand Down Expand Up @@ -1405,10 +1423,46 @@ class Buffer {
* @returns {boolean}
*/
static isBuffer(obj) {
return obj !== null && obj !== undefined && obj._isBuffer === true;
return obj !== null && obj !== undefined && obj[isBuffer] === true;
}

// Override how buffers are presented by util.inspect().
[customInspectSymbol](recurseTimes, ctx) {
const max = INSPECT_MAX_BYTES;
const actualMax = Math.min(max, this.length);
const remaining = this.length - max;
let str = this.slice(0, actualMax).toString('hex').replace(/(.{2})/g, '$1 ').trim();
if (remaining > 0) {
str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`;
}
// Inspect special properties as well, if possible.
if (ctx) {
let extras = false;
const filter = ctx.showHidden ? ALL_PROPERTIES : ONLY_ENUMERABLE;
const obj = getOwnNonIndexProperties(this, filter).reduce((obj, key) => {
extras = true;
obj[key] = this[key];
return obj;
}, Object.create(null));
if (extras) {
if (this.length !== 0) {
str += ', ';
}
// '[Object: null prototype] {'.length === 26
// This is guarded with a test.
str += utilInspect(obj, {
...ctx,
breakLength: Infinity,
compact: true
}).slice(27, -2);
}
}
return `<${this.constructor.name} ${str}>`;
}
}

Buffer.prototype.inspect = Buffer.prototype[customInspectSymbol];

Buffer.poolSize = 8192;

export default {
Expand Down Expand Up @@ -1543,6 +1597,8 @@ const arrayIndexHandler = {
if (Number.isSafeInteger(num)) {
return getAdjustedIndex(target, num);
}
} else if (propKey === isBuffer) {
return true;
}
return Reflect.get(target, propKey, receiver);
},
Expand Down Expand Up @@ -1589,7 +1645,7 @@ function setAdjustedIndex(buf, index, value) {
* @returns {Buffer} wrapped inside a Proxy
*/
function newBuffer(...args) {
return new Proxy(new Buffer(...args), arrayIndexHandler); // eslint-disable-line security/detect-new-buffer
return new Proxy(new Buffer(...args), arrayIndexHandler); // eslint-disable-line security/detect-new-buffer
}

/**
Expand Down
26 changes: 26 additions & 0 deletions common/Resources/ti.internal/extensions/node/internal/assert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { codes } from './errors';

let error;
function lazyError() {
if (!error) {
// @fixme rollup cannot handle lazy loaded modules, maybe move to webpack?
// error = require('./errors').codes.ERR_INTERNAL_ASSERTION;
error = codes.ERR_INTERNAL_ASSERTION;
}
return error;
}
function assert(value, message) {
if (!value) {
const ERR_INTERNAL_ASSERTION = lazyError();
throw new ERR_INTERNAL_ASSERTION(message);
}
}

function fail(message) {
const ERR_INTERNAL_ASSERTION = lazyError();
throw new ERR_INTERNAL_ASSERTION(message);
}

assert.fail = fail;

export default assert;