jaydenseric / extract-files

A function to recursively extract files and their object paths within a value, replacing them with null in a deep clone without mutating the original value. FileList instances are treated as File instance arrays. Files are typically File and Blob instances.
https://npm.im/extract-files
MIT License
56 stars 23 forks source link

Handle circular references #14

Closed jaydenseric closed 3 years ago

jaydenseric commented 4 years ago

This will cause an infinite loop in the recursion logic, ending with a Maximum call stack size exceeded error:

const a = {};
a.b = a;
extractFiles(a)

Perhaps the recursion logic should somehow detect when it's revisiting a node that has already been visited, and bail on further recursion? The cloned object should probably contain the same circular references as the original. I'm not sure yet the best way to do all this.

In the meantime, avoid providing extractFiles a value that may contain circular references.

jaydenseric commented 4 years ago

Maybe keep a Set of all objects encountered, and do a check if the object being recursed is in the set before entering?

jaydenseric commented 4 years ago

I guess arrays can be circular too, not just plain objects.

jaydenseric commented 3 years ago

Progress can be tracked here:

https://github.com/jaydenseric/extract-files/compare/master...jaydenseric/work-in-progress

I've merged in the latest changes from master branch, but I can't really remember what I was up to last year when I pushed the work in progress:

https://github.com/jaydenseric/extract-files/commit/617e51a0313c2654254465042e3fc2703d4f1848

The tests pass but I feel like there was more to be done, so when I get time I'll try to re-immerse in the problem.