1const DATA_URL_PATTERN = /^data:application\/json(?:[^,]*?)(;base64)?,([\s\S]*)$/;
2const JSON_URL_PATTERN = /^[^?]+\.json(\?[^#]*)?(#.*)?$/;
3
4export async function resolve(specifier, context, next) {
5  const noAttributesSpecified = context.importAttributes.type == null;
6
7  // Mutation from resolve hook should be discarded.
8  context.importAttributes.type = 'whatever';
9
10  // This fixture assumes that no other resolve hooks in the chain will error on invalid import attributes
11  // (as defaultResolve doesn't).
12  const result = await next(specifier, context);
13
14  if (noAttributesSpecified &&
15      (DATA_URL_PATTERN.test(result.url) || JSON_URL_PATTERN.test(result.url))) {
16    // Clean new import attributes object to ensure that this test isn't passing due to mutation.
17    result.importAttributes = {
18      ...(result.importAttributes ?? context.importAttributes),
19      type: 'json',
20    };
21  }
22
23  return result;
24}
25