Skip to content

Make urlCache as a part of load context (#707) #708

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

Merged
merged 2 commits into from
Sep 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,18 @@ function parseHttpHeaders(httpHeaders: Record<string, any>): Headers {
interface LoadOptions extends GlobalContext {
rootURL: URL;
schemas: SchemaMap;
urlCache?: Set<string>; // URL cache (prevent URLs from being loaded over and over)
httpHeaders?: Headers;
httpMethod?: string;
}

// temporary cache for load()
let urlCache = new Set<string>(); // URL cache (prevent URLs from being loaded over and over)

/** Load a schema from local path or remote URL */
export default async function load(
schema: URL | PartialSchema,
options: LoadOptions
): Promise<{ [url: string]: PartialSchema }> {
const urlCache = options.urlCache || new Set<string>();
Copy link
Contributor

Choose a reason for hiding this comment

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

How can we ensure that the cache object being passed in is of type Set when the user is consuming the library with vanilla JS? They will have types provided, but it will not throw an error if they pass a different data structure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for asking. As I see this function is not a part of the library public api. And the function uses the cache object for recursive calls optimization. Also as you may see the function does not check types of the other fields of LoadOptions.


const isJSON = schema instanceof URL === false; // if this is dynamically-passed-in JSON, we’ll have to change a few things
let schemaID = isJSON ? new URL(VIRTUAL_JSON_URL).href : (schema.href as string);

Expand Down Expand Up @@ -182,7 +182,7 @@ export default async function load(

const nextURL = isRemoteURL ? new URL(refURL) : new URL(slash(refURL), schema as URL);
refPromises.push(
load(nextURL, options).then((subschemas) => {
load(nextURL, { ...options, urlCache }).then((subschemas) => {
for (const subschemaURL of Object.keys(subschemas)) {
schemas[subschemaURL] = subschemas[subschemaURL];
}
Expand Down