Skip to content

Commit

Permalink
serialisation based crash testing applied
Browse files Browse the repository at this point in the history
  • Loading branch information
Y0ursTruly authored Jul 27, 2024
1 parent 4cfcf88 commit ba979ab
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Share (and sync) Objects Online with the power of websockets. Keys, Values AND R
<br>
**Please note**:
- To view example usage of the modules this library provides, please refer to the _[tests](https://github.com/Y0ursTruly/webject/blob/main/tests.js)_
- TypeScript Definitions Added >:D
- Supporting the `Date` type soon. In the meantime it is advised to `Date` instances into their string or number equivalent

# Installation
Multiple ways
Expand Down
19 changes: 11 additions & 8 deletions for_browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

//see if an enumerable property(of key) exists(in obj)
function includes(obj,key){
if(!obj) return false;
if( (obj instanceof Array) && (key==="length") ) return true;
let existing=describe(obj,key)
return existing?existing.enumerable:false
Expand Down Expand Up @@ -80,12 +81,12 @@
}
if(typeof item!=="object") return item; //numbers, strings
//also functions but these get ignored either way
let shell=item instanceof Array? ([]): ({}), keys=Object.keys(item);
for(let i=0;i<keys.length;i++){
let ITEM=item[keys[i]]
let shell=item instanceof Array? ([]): ({}), KEYS=keys(item);
for(let i=0;i<KEYS.length;i++){
let ITEM=item[KEYS[i]]
if(typeof ITEM==="bigint" || ITEM===undefined) continue;
if(!ITEM || (!ITEM[Symbol.toStringTag] && typeof ITEM!=="object"))
shell[keys[i]] = ITEM;
shell[KEYS[i]] = ITEM;
}
return shell
}
Expand All @@ -106,10 +107,10 @@
if(!orig) return null;
let metadata=map.get(orig)
if(!metadata) return map.delete(cloned);
let keys=Object.keys(cloned);
for(let i=0;i<keys.length;i++)
if(typeof cloned[keys[i]]==="object" && cloned[keys[i]])
recursivelyDetatch(map,cloned[keys[i]]);
let KEYS=keys(cloned);
for(let i=0;i<KEYS.length;i++)
if(typeof cloned[KEYS[i]]==="object" && cloned[KEYS[i]])
recursivelyDetatch(map,cloned[KEYS[i]]);
if(--metadata[4]) return null;
map.delete(orig)
map.delete(cloned)
Expand Down Expand Up @@ -169,6 +170,8 @@
}
if(typeof item==="symbol" || (typeof item==="object" && item!==null)){
if(!temp){
const former=map.get(clone[key])
if(former) map.delete(former);
map.set(item,[Path,list.length-1,newEntry?1:0,clone[key],1]);
map.set(clone[key],item);
}
Expand Down
2 changes: 1 addition & 1 deletion for_browser.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webject",
"version": "1.4.7",
"version": "1.4.8",
"description": "Share Objects Online with the power of websockets. Keys, Values AND references. Webject is short for Web Object and it really is a system for sharing objects on the web. Someone can host an object, and make authTokens for others online to share this object",
"main": "webject.js",
"scripts": {
Expand Down
21 changes: 21 additions & 0 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,27 @@ const {serve, connect, sync, desync, objToString, stringToObj, partFilter, objVa
assert.strictEqual(testObj.a.b.f,undefined)
assert.strictEqual(testObj.a.b.c,3)
})
await t.test("Serialisation Crash Testing",async function(){
const date=new Date(), obj={events:[{date,arr:['value1']}]}
const clone=stringToObj(objToString(obj)), {events}=obj
const standard={"events":[{"date":date-0,"arr":["value1","value2","value2","value2","value2","value2"]}]}
let update=_=>stringToObj(objToString(obj),clone);
for(let i=0;i<5;i++){
obj.events[0].date-=0;
update()
obj.events=[{date}];
update()
obj.events[0].date-=0;
update()
obj.events=null;
update()
events[0].arr.push("value"+(events.length+1));
obj.events=events;
update() //the call that crashes it
}
assert.deepStrictEqual(obj,clone)
assert.deepStrictEqual(obj,standard)
})
})

//third set of tests
Expand Down

0 comments on commit ba979ab

Please sign in to comment.