matter() caches the content even when an error is thrown #121

Open
opened 2021-05-01 04:57:39 +00:00 by hankei6km · 0 comments
hankei6km commented 2021-05-01 04:57:39 +00:00 (Migrated from github.com)

It cancels throwing an error when processing the cached content.

Steps to reproduce

import matter from 'gray-matter';

const fixture = '---whatever\nABC: XYZ\n---';
console.log('=== start');
try {
  matter(fixture);
} catch (err) {
  console.log(`Error1: ${err}`);
}
console.log('===');
try {
  matter(fixture);
} catch (err) {
  console.log(`Error2: ${err}`);
}
console.log('=== done');

// === start
// Error1: Error: gray-matter engine "whatever" is not registered
// ===
// === done

Workaround

https://github.com/jonschlinkert/gray-matter/issues/43#issuecomment-318258919

  matter(fixture, {});

or

github.com/hankei6km/gray-matter@cae943deb9/index.js (L29)

function matter(input, options) {
  if (input === '') {
    return { data: {}, content: input, excerpt: '', orig: input };
  }

  let file = toFile(input);
  const cached = matter.cache[file.content];

  if (!options) {
    if (cached) {
      file = Object.assign({}, cached);
      file.orig = cached.orig;
      return file;
    }

    const ret = parseMatter(file, options);

    // only cache if there are no options passed. if we cache when options
    // are passed, we would need to also cache options values, which would
    // negate any performance benefits of caching
    matter.cache[file.content] = file;

    return ret;
  }

  return parseMatter(file, options);
}
It cancels throwing an error when processing the cached content. ## Steps to reproduce ```javascript import matter from 'gray-matter'; const fixture = '---whatever\nABC: XYZ\n---'; console.log('=== start'); try { matter(fixture); } catch (err) { console.log(`Error1: ${err}`); } console.log('==='); try { matter(fixture); } catch (err) { console.log(`Error2: ${err}`); } console.log('=== done'); // === start // Error1: Error: gray-matter engine "whatever" is not registered // === // === done ``` ## Workaround https://github.com/jonschlinkert/gray-matter/issues/43#issuecomment-318258919 ```javascript matter(fixture, {}); ``` or https://github.com/hankei6km/gray-matter/blob/cae943deb97562eed22c1ad4f29ac7e93527d864/index.js#L29 ```javascript function matter(input, options) { if (input === '') { return { data: {}, content: input, excerpt: '', orig: input }; } let file = toFile(input); const cached = matter.cache[file.content]; if (!options) { if (cached) { file = Object.assign({}, cached); file.orig = cached.orig; return file; } const ret = parseMatter(file, options); // only cache if there are no options passed. if we cache when options // are passed, we would need to also cache options values, which would // negate any performance benefits of caching matter.cache[file.content] = file; return ret; } return parseMatter(file, options); } ```
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
rtlabs-llm-agents/gms-gray-matter#121
No description provided.