First Commit of new way of building the Platform

This commit is contained in:
Luis Fernando Molina committed 2020-01-28 17:27:23 +01:00
1 parent e48cf0dae5
commit 40e60b42cc
4616 files changed
+713402

No files matched your search

+71
View File
@@ -0,0 +1,71 @@
const Benchmark = require('benchmark')
const suite = new Benchmark.Suite()
const { inspect } = require('util')
const jsonStringifySafe = require('json-stringify-safe')
const fastSafeStringify = require('./')
const array = new Array(10).fill(0).map((_, i) => i)
const obj = { foo: array }
const circ = JSON.parse(JSON.stringify(obj))
circ.o = { obj: circ, array }
const deep = require('./package.json')
deep.deep = JSON.parse(JSON.stringify(deep))
deep.deep.deep = JSON.parse(JSON.stringify(deep))
deep.deep.deep.deep = JSON.parse(JSON.stringify(deep))
deep.array = array
const deepCirc = JSON.parse(JSON.stringify(deep))
deepCirc.deep.deep.deep.circ = deepCirc
deepCirc.deep.deep.circ = deepCirc
deepCirc.deep.circ = deepCirc
deepCirc.array = array
suite.add('util.inspect: simple object', function () {
inspect(obj)
})
suite.add('util.inspect: circular ', function () {
inspect(circ)
})
suite.add('util.inspect: deep ', function () {
inspect(deep)
})
suite.add('util.inspect: deep circular', function () {
inspect(deepCirc)
})
suite.add('\njson-stringify-safe: simple object', function () {
jsonStringifySafe(obj)
})
suite.add('json-stringify-safe: circular ', function () {
jsonStringifySafe(circ)
})
suite.add('json-stringify-safe: deep ', function () {
jsonStringifySafe(deep)
})
suite.add('json-stringify-safe: deep circular', function () {
jsonStringifySafe(deepCirc)
})
suite.add('\nfast-safe-stringify: simple object', function () {
fastSafeStringify(obj)
})
suite.add('fast-safe-stringify: circular ', function () {
fastSafeStringify(circ)
})
suite.add('fast-safe-stringify: deep ', function () {
fastSafeStringify(deep)
})
suite.add('fast-safe-stringify: deep circular', function () {
fastSafeStringify(deepCirc)
})
// add listeners
suite.on('cycle', function (event) {
console.log(String(event.target))
})
suite.on('complete', function () {
console.log('\nFastest is ' + this.filter('fastest').map('name'))
})
suite.run({ delay: 1, minSamples: 150 })
+103
View File
@@ -0,0 +1,103 @@
module.exports = stringify
stringify.default = stringify
stringify.stable = deterministicStringify
stringify.stableStringify = deterministicStringify
var arr = []
// Regular stringify
function stringify (obj, replacer, spacer) {
decirc(obj, '', [], undefined)
var res = JSON.stringify(obj, replacer, spacer)
while (arr.length !== 0) {
var part = arr.pop()
part[0][part[1]] = part[2]
}
return res
}
function decirc (val, k, stack, parent) {
var i
if (typeof val === 'object' && val !== null) {
for (i = 0; i < stack.length; i++) {
if (stack[i] === val) {
parent[k] = '[Circular]'
arr.push([parent, k, val])
return
}
}
stack.push(val)
// Optimize for Arrays. Big arrays could kill the performance otherwise!
if (Array.isArray(val)) {
for (i = 0; i < val.length; i++) {
decirc(val[i], i, stack, val)
}
} else {
var keys = Object.keys(val)
for (i = 0; i < keys.length; i++) {
var key = keys[i]
decirc(val[key], key, stack, val)
}
}
stack.pop()
}
}
// Stable-stringify
function compareFunction (a, b) {
if (a < b) {
return -1
}
if (a > b) {
return 1
}
return 0
}
function deterministicStringify (obj, replacer, spacer) {
var tmp = deterministicDecirc(obj, '', [], undefined) || obj
var res = JSON.stringify(tmp, replacer, spacer)
while (arr.length !== 0) {
var part = arr.pop()
part[0][part[1]] = part[2]
}
return res
}
function deterministicDecirc (val, k, stack, parent) {
var i
if (typeof val === 'object' && val !== null) {
for (i = 0; i < stack.length; i++) {
if (stack[i] === val) {
parent[k] = '[Circular]'
arr.push([parent, k, val])
return
}
}
if (typeof val.toJSON === 'function') {
return
}
stack.push(val)
// Optimize for Arrays. Big arrays could kill the performance otherwise!
if (Array.isArray(val)) {
for (i = 0; i < val.length; i++) {
deterministicDecirc(val[i], i, stack, val)
}
} else {
// Create a temporary object in the required way
var tmp = {}
var keys = Object.keys(val).sort(compareFunction)
for (i = 0; i < keys.length; i++) {
var key = keys[i]
deterministicDecirc(val[key], key, stack, val)
tmp[key] = val[key]
}
if (parent !== undefined) {
arr.push([parent, k, val])
parent[k] = tmp
} else {
return tmp
}
}
stack.pop()
}
}
+90
View File
@@ -0,0 +1,90 @@
{
"_args": [
[
"fast-safe-stringify@2.0.6",
"C:\\Users\\Luis\\source\\repos\\AAMasters\\TaskServer"
]
],
"_from": "fast-safe-stringify@2.0.6",
"_id": "fast-safe-stringify@2.0.6",
"_inBundle": false,
"_integrity": "sha512-q8BZ89jjc+mz08rSxROs8VsrBBcn1SIw1kq9NjolL509tkABRk9io01RAjSaEv1Xb2uFLt8VtRiZbGp5H8iDtg==",
"_location": "/fast-safe-stringify",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "fast-safe-stringify@2.0.6",
"name": "fast-safe-stringify",
"escapedName": "fast-safe-stringify",
"rawSpec": "2.0.6",
"saveSpec": null,
"fetchSpec": "2.0.6"
},
"_requiredBy": [
"/logform"
],
"_resolved": "https://registry.npmjs.org/fast-safe-stringify/-/fast-safe-stringify-2.0.6.tgz",
"_spec": "2.0.6",
"_where": "C:\\Users\\Luis\\source\\repos\\AAMasters\\TaskServer",
"author": {
"name": "David Mark Clements"
},
"bugs": {
"url": "https://github.com/davidmarkclements/fast-safe-stringify/issues"
},
"contributors": [
{
"name": "Ruben Bridgewater"
},
{
"name": "Matteo Collina"
},
{
"name": "Ben Gourley"
},
{
"name": "Gabriel Lesperance"
},
{
"name": "Alex Liu"
},
{
"name": "Christoph Walcher"
},
{
"name": "Nicholas Young"
}
],
"dependencies": {},
"description": "Safely and quickly serialize JavaScript objects",
"devDependencies": {
"benchmark": "^2.1.4",
"clone": "^2.1.0",
"json-stringify-safe": "^5.0.1",
"standard": "^11.0.0",
"tap": "^12.0.0"
},
"homepage": "https://github.com/davidmarkclements/fast-safe-stringify#readme",
"keywords": [
"stable",
"stringify",
"JSON",
"JSON.stringify",
"safe",
"serialize"
],
"license": "MIT",
"main": "index.js",
"name": "fast-safe-stringify",
"repository": {
"type": "git",
"url": "git+https://github.com/davidmarkclements/fast-safe-stringify.git"
},
"scripts": {
"benchmark": "node benchmark.js",
"test": "standard && tap test.js test-stable.js"
},
"typings": "index",
"version": "2.0.6"
}
+246
View File
@@ -0,0 +1,246 @@
const test = require('tap').test
const fss = require('./').stable
const clone = require('clone')
const s = JSON.stringify
test('circular reference to root', function (assert) {
const fixture = { name: 'Tywin Lannister' }
fixture.circle = fixture
const expected = s(
{ circle: '[Circular]', name: 'Tywin Lannister' }
)
const actual = fss(fixture)
assert.is(actual, expected)
assert.end()
})
test('nested circular reference to root', function (assert) {
const fixture = { name: 'Tywin Lannister' }
fixture.id = { circle: fixture }
const expected = s(
{ id: { circle: '[Circular]' }, name: 'Tywin Lannister' }
)
const actual = fss(fixture)
assert.is(actual, expected)
assert.end()
})
test('child circular reference', function (assert) {
const fixture = { name: 'Tywin Lannister', child: { name: 'Tyrion Lannister' } }
fixture.child.dinklage = fixture.child
const expected = s({
child: {
dinklage: '[Circular]', name: 'Tyrion Lannister'
},
name: 'Tywin Lannister'
})
const actual = fss(fixture)
assert.is(actual, expected)
assert.end()
})
test('nested child circular reference', function (assert) {
const fixture = { name: 'Tywin Lannister', child: { name: 'Tyrion Lannister' } }
fixture.child.actor = { dinklage: fixture.child }
const expected = s({
child: {
actor: { dinklage: '[Circular]' }, name: 'Tyrion Lannister'
},
name: 'Tywin Lannister'
})
const actual = fss(fixture)
assert.is(actual, expected)
assert.end()
})
test('circular objects in an array', function (assert) {
const fixture = { name: 'Tywin Lannister' }
fixture.hand = [fixture, fixture]
const expected = s({
hand: ['[Circular]', '[Circular]'], name: 'Tywin Lannister'
})
const actual = fss(fixture)
assert.is(actual, expected)
assert.end()
})
test('nested circular references in an array', function (assert) {
const fixture = {
name: 'Tywin Lannister',
offspring: [{ name: 'Tyrion Lannister' }, { name: 'Cersei Lannister' }]
}
fixture.offspring[0].dinklage = fixture.offspring[0]
fixture.offspring[1].headey = fixture.offspring[1]
const expected = s({
name: 'Tywin Lannister',
offspring: [
{ dinklage: '[Circular]', name: 'Tyrion Lannister' },
{ headey: '[Circular]', name: 'Cersei Lannister' }
]
})
const actual = fss(fixture)
assert.is(actual, expected)
assert.end()
})
test('circular arrays', function (assert) {
const fixture = []
fixture.push(fixture, fixture)
const expected = s(['[Circular]', '[Circular]'])
const actual = fss(fixture)
assert.is(actual, expected)
assert.end()
})
test('nested circular arrays', function (assert) {
const fixture = []
fixture.push(
{ name: 'Jon Snow', bastards: fixture },
{ name: 'Ramsay Bolton', bastards: fixture }
)
const expected = s([
{ bastards: '[Circular]', name: 'Jon Snow' },
{ bastards: '[Circular]', name: 'Ramsay Bolton' }
])
const actual = fss(fixture)
assert.is(actual, expected)
assert.end()
})
test('repeated non-circular references in objects', function (assert) {
const daenerys = { name: 'Daenerys Targaryen' }
const fixture = {
motherOfDragons: daenerys,
queenOfMeereen: daenerys
}
const expected = s(fixture)
const actual = fss(fixture)
assert.is(actual, expected)
assert.end()
})
test('repeated non-circular references in arrays', function (assert) {
const daenerys = { name: 'Daenerys Targaryen' }
const fixture = [daenerys, daenerys]
const expected = s(fixture)
const actual = fss(fixture)
assert.is(actual, expected)
assert.end()
})
test('double child circular reference', function (assert) {
// create circular reference
const child = { name: 'Tyrion Lannister' }
child.dinklage = child
// include it twice in the fixture
const fixture = { name: 'Tywin Lannister', childA: child, childB: child }
const cloned = clone(fixture)
const expected = s({
childA: {
dinklage: '[Circular]', name: 'Tyrion Lannister'
},
childB: {
dinklage: '[Circular]', name: 'Tyrion Lannister'
},
name: 'Tywin Lannister'
})
const actual = fss(fixture)
assert.is(actual, expected)
// check if the fixture has not been modified
assert.deepEqual(fixture, cloned)
assert.end()
})
test('child circular reference with toJSON', function (assert) {
// Create a test object that has an overriden `toJSON` property
TestObject.prototype.toJSON = function () { return { special: 'case' } }
function TestObject (content) {}
// Creating a simple circular object structure
const parentObject = {}
parentObject.childObject = new TestObject()
parentObject.childObject.parentObject = parentObject
// Creating a simple circular object structure
const otherParentObject = new TestObject()
otherParentObject.otherChildObject = {}
otherParentObject.otherChildObject.otherParentObject = otherParentObject
// Making sure our original tests work
assert.deepEqual(parentObject.childObject.parentObject, parentObject)
assert.deepEqual(otherParentObject.otherChildObject.otherParentObject, otherParentObject)
// Should both be idempotent
assert.equal(fss(parentObject), '{"childObject":{"special":"case"}}')
assert.equal(fss(otherParentObject), '{"special":"case"}')
// Therefore the following assertion should be `true`
assert.deepEqual(parentObject.childObject.parentObject, parentObject)
assert.deepEqual(otherParentObject.otherChildObject.otherParentObject, otherParentObject)
assert.end()
})
test('null object', function (assert) {
const expected = s(null)
const actual = fss(null)
assert.is(actual, expected)
assert.end()
})
test('null property', function (assert) {
const expected = s({ f: null })
const actual = fss({ f: null })
assert.is(actual, expected)
assert.end()
})
test('nested child circular reference in toJSON', function (assert) {
var circle = { some: 'data' }
circle.circle = circle
var a = {
b: {
toJSON: function () {
a.b = 2
return '[Redacted]'
}
},
baz: {
circle,
toJSON: function () {
a.baz = circle
return '[Redacted]'
}
}
}
var o = {
a,
bar: a
}
const expected = s({
a: {
b: '[Redacted]',
baz: '[Redacted]'
},
bar: {
// TODO: This is a known limitation of the current implementation.
// The ideal result would be:
//
// b: 2,
// baz: {
// circle: '[Circular]',
// some: 'data'
// }
//
b: '[Redacted]',
baz: '[Redacted]'
}
})
const actual = fss(o)
assert.is(actual, expected)
assert.end()
})
+240
View File
@@ -0,0 +1,240 @@
const test = require('tap').test
const fss = require('./')
const clone = require('clone')
const s = JSON.stringify
test('circular reference to root', function (assert) {
const fixture = { name: 'Tywin Lannister' }
fixture.circle = fixture
const expected = s(
{ name: 'Tywin Lannister', circle: '[Circular]' }
)
const actual = fss(fixture)
assert.is(actual, expected)
assert.end()
})
test('nested circular reference to root', function (assert) {
const fixture = { name: 'Tywin Lannister' }
fixture.id = { circle: fixture }
const expected = s(
{ name: 'Tywin Lannister', id: { circle: '[Circular]' } }
)
const actual = fss(fixture)
assert.is(actual, expected)
assert.end()
})
test('child circular reference', function (assert) {
const fixture = { name: 'Tywin Lannister', child: { name: 'Tyrion Lannister' } }
fixture.child.dinklage = fixture.child
const expected = s({
name: 'Tywin Lannister',
child: {
name: 'Tyrion Lannister', dinklage: '[Circular]'
}
})
const actual = fss(fixture)
assert.is(actual, expected)
assert.end()
})
test('nested child circular reference', function (assert) {
const fixture = { name: 'Tywin Lannister', child: { name: 'Tyrion Lannister' } }
fixture.child.actor = { dinklage: fixture.child }
const expected = s({
name: 'Tywin Lannister',
child: {
name: 'Tyrion Lannister', actor: { dinklage: '[Circular]' }
}
})
const actual = fss(fixture)
assert.is(actual, expected)
assert.end()
})
test('circular objects in an array', function (assert) {
const fixture = { name: 'Tywin Lannister' }
fixture.hand = [fixture, fixture]
const expected = s({
name: 'Tywin Lannister', hand: ['[Circular]', '[Circular]']
})
const actual = fss(fixture)
assert.is(actual, expected)
assert.end()
})
test('nested circular references in an array', function (assert) {
const fixture = {
name: 'Tywin Lannister',
offspring: [{ name: 'Tyrion Lannister' }, { name: 'Cersei Lannister' }]
}
fixture.offspring[0].dinklage = fixture.offspring[0]
fixture.offspring[1].headey = fixture.offspring[1]
const expected = s({
name: 'Tywin Lannister',
offspring: [
{ name: 'Tyrion Lannister', dinklage: '[Circular]' },
{ name: 'Cersei Lannister', headey: '[Circular]' }
]
})
const actual = fss(fixture)
assert.is(actual, expected)
assert.end()
})
test('circular arrays', function (assert) {
const fixture = []
fixture.push(fixture, fixture)
const expected = s(['[Circular]', '[Circular]'])
const actual = fss(fixture)
assert.is(actual, expected)
assert.end()
})
test('nested circular arrays', function (assert) {
const fixture = []
fixture.push(
{ name: 'Jon Snow', bastards: fixture },
{ name: 'Ramsay Bolton', bastards: fixture }
)
const expected = s([
{ name: 'Jon Snow', bastards: '[Circular]' },
{ name: 'Ramsay Bolton', bastards: '[Circular]' }
])
const actual = fss(fixture)
assert.is(actual, expected)
assert.end()
})
test('repeated non-circular references in objects', function (assert) {
const daenerys = { name: 'Daenerys Targaryen' }
const fixture = {
motherOfDragons: daenerys,
queenOfMeereen: daenerys
}
const expected = s(fixture)
const actual = fss(fixture)
assert.is(actual, expected)
assert.end()
})
test('repeated non-circular references in arrays', function (assert) {
const daenerys = { name: 'Daenerys Targaryen' }
const fixture = [daenerys, daenerys]
const expected = s(fixture)
const actual = fss(fixture)
assert.is(actual, expected)
assert.end()
})
test('double child circular reference', function (assert) {
// create circular reference
const child = { name: 'Tyrion Lannister' }
child.dinklage = child
// include it twice in the fixture
const fixture = { name: 'Tywin Lannister', childA: child, childB: child }
const cloned = clone(fixture)
const expected = s({
name: 'Tywin Lannister',
childA: {
name: 'Tyrion Lannister', dinklage: '[Circular]'
},
childB: {
name: 'Tyrion Lannister', dinklage: '[Circular]'
}
})
const actual = fss(fixture)
assert.is(actual, expected)
// check if the fixture has not been modified
assert.deepEqual(fixture, cloned)
assert.end()
})
test('child circular reference with toJSON', function (assert) {
// Create a test object that has an overriden `toJSON` property
TestObject.prototype.toJSON = function () { return { special: 'case' } }
function TestObject (content) {}
// Creating a simple circular object structure
const parentObject = {}
parentObject.childObject = new TestObject()
parentObject.childObject.parentObject = parentObject
// Creating a simple circular object structure
const otherParentObject = new TestObject()
otherParentObject.otherChildObject = {}
otherParentObject.otherChildObject.otherParentObject = otherParentObject
// Making sure our original tests work
assert.deepEqual(parentObject.childObject.parentObject, parentObject)
assert.deepEqual(otherParentObject.otherChildObject.otherParentObject, otherParentObject)
// Should both be idempotent
assert.equal(fss(parentObject), '{"childObject":{"special":"case"}}')
assert.equal(fss(otherParentObject), '{"special":"case"}')
// Therefore the following assertion should be `true`
assert.deepEqual(parentObject.childObject.parentObject, parentObject)
assert.deepEqual(otherParentObject.otherChildObject.otherParentObject, otherParentObject)
assert.end()
})
test('null object', function (assert) {
const expected = s(null)
const actual = fss(null)
assert.is(actual, expected)
assert.end()
})
test('null property', function (assert) {
const expected = s({ f: null })
const actual = fss({ f: null })
assert.is(actual, expected)
assert.end()
})
test('nested child circular reference in toJSON', function (assert) {
const circle = { some: 'data' }
circle.circle = circle
const a = {
b: {
toJSON: function () {
a.b = 2
return '[Redacted]'
}
},
baz: {
circle,
toJSON: function () {
a.baz = circle
return '[Redacted]'
}
}
}
const o = {
a,
bar: a
}
const expected = s({
a: {
b: '[Redacted]',
baz: '[Redacted]'
},
bar: {
b: 2,
baz: {
some: 'data',
circle: '[Circular]'
}
}
})
const actual = fss(o)
assert.is(actual, expected)
assert.end()
})