Skip to content

Commit

Permalink
fix(schemaproxy.js): reuse previously proxied subschemas to fix max c…
Browse files Browse the repository at this point in the history
…all stack size exceeded error

Solution proposed by @bmaranville in
#252 (comment). Updated to work with
7.1.1.

fix #252
  • Loading branch information
adriancampos committed Sep 22, 2022
1 parent cf5db93 commit 1388449
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions lib/schemaProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function loadExamples(file, num = 1) {
}

const handler = ({
root = '', fullpath = null, filename = '.', schemas, parent = null, slugger,
root = '', fullpath = null, filename = '.', schemas, subschemas, parent = null, slugger,
}) => {
const meta = {};

Expand Down Expand Up @@ -162,14 +162,23 @@ const handler = ({
}

// console.log('making new proxy from', target, prop, 'receiver', receiver[symbols.id]);
const subschema = new Proxy(retval, handler({
root: `${root}/${prop}`,
parent: receiver,
fullpath,
filename,
schemas,
slugger,
}));
let subschema;
if (subschemas.has(retval)) {
subschema = subschemas.get(retval);
}
else {
subschema = new Proxy(retval, handler({
root: `${root}/${prop}`,
parent: receiver,
fullpath,
filename,
schemas,
subschemas,
slugger,
}));

subschemas.set(retval, subschema);
}

if (subschema[keyword`$id`]) {
// stow away the schema for lookup
Expand All @@ -190,14 +199,16 @@ export default function loader() {
files: {},
};

const subschemas = new Map();

const slugger = new GhSlugger();

return (/** @type {string} */ name, /** @type {any} */ schema) => {
// console.log('loading', name);
const filename = path.basename(name);
const fullpath = name === filename ? undefined : name;
const proxied = new Proxy(schema, handler({
filename, fullpath, schemas, slugger,
filename, fullpath, schemas, subschemas, slugger,
}));
schemas.loaded.push(proxied);
if (proxied[keyword`$id`]) {
Expand Down

0 comments on commit 1388449

Please sign in to comment.