How to use the ferrum.setdefault function in ferrum

To help you get started, we’ve selected a few ferrum examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github adobe / helix-pipeline / test / testJSON.js View on Github external
(context) => {
        const { content } = context;
        const res = setdefault(context, 'response', {});
        // this is the main function (normally it would be the template function)
        // but we use it to assert that pre-processing has happened
        assert.ok(content.body);
        assert.ok(content.mdast);
        assert.ok(content.meta);
        assert.equal(content.meta.template, 'Medium');
        assert.equal(content.intro, 'Project Helix');
        assert.equal(content.title, 'Bill, Welcome to the future');
        // and return a different status code
        res.status = 201;
        res.body = { foo: 'bar' };
        setdefault(res, 'headers', {})['Content-Type'] = 'text/plain+json';
      },
      {},
github adobe / helix-pipeline / src / html / set-status.js View on Github external
// if a status is already default, keep it.
  if (res.status) {
    return;
  }

  if (!err) {
    res.status = 200;
    return;
  }

  // error handling
  logger.debug('context.error -> 500');
  res.status = 500;
  res.body = '';

  if (!isProduction() || setdefault(request, 'headers', {})['x-debug']) {
    res.body = `<h1>500</h1><pre>${err}</pre>`;
    headers['Content-Type'] = 'text/html';
  }
};
github adobe / helix-pipeline / src / html / fetch-markdown.js View on Github external
// if there is a github token, send it in the Authorization header
  const token = secrets.GITHUB_TOKEN || (request.headers ? request.headers['x-github-token'] : '');
  if (token) {
    setdefault(options, 'headers', {
      Authorization: `token ${token}`,
    });
  }

  logger.debug(`fetching Markdown from ${options.uri}`);
  try {
    content.body = await client(options);
    if (branch && branch !== ref) {
      setdefault(content, 'sources', []).push(uri(rootPath, owner, repo, branch, path));
    } else {
      setdefault(content, 'sources', []).push(options.uri);
    }
  } catch (e) {
    if (e.statusCode === 404) {
      logger.error(`Could not find Markdown at ${options.uri}`);
      setdefault(context, 'response', {}).status = 404;
    } else if ((e.response && e.response.elapsedTime && e.response.elapsedTime > timeout) || (e.cause && e.cause.code && (e.cause.code === 'ESOCKETTIMEDOUT' || e.cause.code === 'ETIMEDOUT'))) {
      // return gateway timeout
      logger.error(`Gateway timeout of ${timeout} milliseconds exceeded for ${options.uri}`);
      setdefault(context, 'response', {}).status = 504;
    } else {
      logger.error(`Error while fetching Markdown from ${options.uri} with the following `
                   + `options:\n${inspect(options, { depth: null })}`);
      setdefault(context, 'response', {}).status = 502;
    }
    context.error = e;
  }
github adobe / helix-pipeline / src / json / set-json-status.js View on Github external
return ({ response = {}, error }, { logger, request = {} }) => {
    // if a status is already default, keep it.
    if (response.status) {
      return {};
    }
    if (!error) {
      return {
        response: {
          status: 200,
        },
      };
    }
    // error handling
    logger.debug('context.error -> 500');
    if (prod && !setdefault(request, 'headers', {})['x-debug']) {
      return {
        response: {
          status: 500,
          body: '',
        },
      };
    }
    return setVerboseError(error);
  };
}
github adobe / helix-pipeline / src / xml / set-xml-status.js View on Github external
// if a status is already default, keep it.
  if (res.status) {
    return;
  }

  if (!err) {
    res.status = 200;
    return;
  }

  logger.debug('context.error -&gt; 500');
  res.status = 500;
  res.body = '';

  if (!isProduction() || setdefault(request, 'headers', {})['x-debug']) {
    res.body = `<code>500</code>${err.trim()}`;
    headers['Content-Type'] = 'application/xml';
  }
}
github adobe / helix-pipeline / src / html / set-status.js View on Github external
const selectStatus = (context, { logger, request = {} }) => {
  const res = setdefault(context, 'response', {});
  const headers = setdefault(res, 'headers', {});
  const err = context.error;

  // if a status is already default, keep it.
  if (res.status) {
    return;
  }

  if (!err) {
    res.status = 200;
    return;
  }

  // error handling
  logger.debug('context.error -> 500');
  res.status = 500;
github adobe / helix-pipeline / src / utils / dump-context.js View on Github external
async function record(context, action, index, name) {
  const debug = setdefault(action, 'debug', { contextDumps: [] });
  const info = {
    index,
    name: name ? name.split(':').pop() : 'undef',
    time: new Date(),
    json: JSON.stringify(context, null, 2),
  };
  debug.contextDumps.push(info);

  if (action.logger.level === 'silly' && !disableDisk()) {
    await ensureDumpDir(context, action);
    await writeDump(context, action, info);
  }
}
github adobe / helix-pipeline / src / xml / emit-xml.js View on Github external
function emit(context, { secrets, logger }) {
  const cont = setdefault(context, 'content', {});
  const res = setdefault(context, 'response', {});

  if (res.body) {
    logger.debug('Response body already exists');
    return;
  }

  if (!cont.xml) {
    logger.debug('No XML to emit');
    return;
  }

  logger.debug(`Emitting XML from ${typeof cont.xml}`);
  const xml = builder.create(cont.xml, { encoding: 'utf-8' });
  res.body = xml.end({ pretty: !!secrets.XML_PRETTY });
}
github adobe / helix-pipeline / src / utils / set-content-type.js View on Github external
return function setmime(context) {
    const res = setdefault(context, 'response', {});
    const head = setdefault(res, 'headers', {});
    setdefault(head, 'Content-Type', mime);
  };
}
github adobe / helix-pipeline / src / utils / conditional-sections.js View on Github external
function selectstrain(context, { request, logger }) {
  const cont = setdefault(context, 'content', { mdast: {} });
  const params = request.params || {};
  const { strain } = params;
  const sections = cont.mdast.children;

  if (!strain || !sections) {
    return;
  }

  logger.debug(`Filtering sections not intended for strain ${strain}`);
  each(sections, (section) => {
    const meta = setdefault(section, 'meta', {});
    const strainList = type(meta.strain) === Array ? meta.strain : [meta.strain];
    meta.hidden = Boolean(meta.strain && meta.strain !== [] && !strainList.includes(strain));
  });
}

ferrum

Features from the rust language in javascript: Provides Traits/Type classes & an advanced library for working with sequences/iterators in js.

Apache-2.0
Latest version published 3 years ago

Package Health Score

60 / 100
Full package analysis