How to use the content-type.parse function in content-type

To help you get started, we’ve selected a few content-type 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 apifytech / actor-scraper / web-scraper / src / crawler_setup.js View on Github external
async _waitForLoadEventWhenXml(page, response) {
        // Response can sometimes be null.
        if (!response) return;

        const cTypeHeader = response.headers()['content-type'];
        try {
            const { type } = contentType.parse(cTypeHeader);
            if (!/^(text|application)\/xml$|\+xml$/.test(type)) return;
        } catch (err) {
            // Invalid type is not XML.
            return;
        }

        try {
            const timeout = this.input.pageLoadTimeoutSecs * 1000;
            await page.waitFor(() => document.readyState === 'complete', { timeout });
        } catch (err) {
            if (err.stack.startsWith('TimeoutError')) {
                throw new Error('Parsing of XML in the page timed out. If you\'re expecting a large XML file, '
                    + ' such as a site map, try increasing the Page load timeout input setting.');
            } else {
                throw err;
            }
github koajs / charset / index.js View on Github external
return function* charset(next) {
    yield* next;
    // manually turn off charset by `this.charset = false`
    if (this.charset === false) return;
    if (!this.body) return;
    if (!text(this.type)) return;

    var contentType = this.response.get('Content-Type');

    // first this.charset
    // then global.charset
    // at last check charset in `content-type`
    var charset = (this.charset
      || options.charset
      || typer.parse(contentType).parameters.charset
      || '').toLowerCase();

    if (!charset
      || charset === 'utf-8'
      || charset === 'utf8') return;

    // set type with charset
    var type = this.type;
    this.type = type + '; charset=' + charset;

    // buffer / string body
    if (Buffer.isBuffer(this.body) || typeof this.body === 'string') {
      return this.body = iconv.encode(this.body, charset);
    }
    // stream body
    if (typeof this.body.pipe === 'function') {
github bevacqua / twitter-leads / index.js View on Github external
function parse (res, body, next) {
      var config = {
        relax: true,
        columns: true,
        auto_parse: true,
        auto_parse_date: true
      };
      if (res.statusCode < 200 || res.statusCode >= 300) {
        next(null, []);
        return;
      }
      var parsedContentType = contentType.parse(res).type;
      if (parsedContentType !== 'application/csv') {
        next(null, []);
        return;
      }
      csv(body, config, next);
    },
    function parsed (data, next) {
github wisnuc / appifi / patch / body-parser / 1.13.3 / urlencoded.js View on Github external
function getCharset(req) {
  try {
    return contentType.parse(req).parameters.charset.toLowerCase()
  } catch (e) {
    return undefined
  }
}
github lucleray / object-detection / api / index.js View on Github external
handleError(async (req, res) => {
    if (req.method === 'OPTIONS') {
      return send(res, 200)
    }

    if (req.url === '/api/warm') {
      await loadTf()
      await loadModel()
      return send(res, 200)
    }

    if (req.url === '/api/predict') {
      const { type: mimeType } = contentType.parse(req)

      if (CONTENT_TYPES_IMAGE.includes(mimeType)) {
        const tf = await loadTf()
        const tfModel = await loadModel()
        
        const buf = await buffer(req, { limit: '5mb' })
        const { tensor, width, height } = await imgToTensor(buf)

        const { scores, boxes } = await predict(tfModel, tensor)

        const bboxes = await tensorsToBBoxes({ scores, boxes, width, height })

        return send(res, 200, bboxes)
      }

      throw BadRequestError('Only images are supported at the moment')
github jamesshore / lets_code_javascript / node_modules / karma / node_modules / connect / node_modules / body-parser / lib / types / json.js View on Github external
function getCharset(req) {
  try {
    return contentType.parse(req).parameters.charset.toLowerCase()
  } catch (e) {
    return undefined
  }
}
github ifgyong / demo / React-native / Helloword / node_modules / body-parser / lib / types / urlencoded.js View on Github external
function getCharset(req) {
    try {
        return contentType.parse(req).parameters.charset.toLowerCase()
    } catch (e) {
        return undefined
    }
}
github expressjs / body-parser / lib / types / urlencoded.js View on Github external
function getCharset (req) {
  try {
    return (contentType.parse(req).parameters.charset || '').toLowerCase()
  } catch (e) {
    return undefined
  }
}
github middyjs / middy / src / middlewares / urlEncodeBodyParser.js View on Github external
before: (handler, next) => {
    const options = Object.assign({}, defaults, opts)

    const parserFn = options.extended ? require('qs').parse : require('querystring').decode

    if (handler.event.headers && handler.event.headers['Content-Type']) {
      const { type } = contentType.parse(handler.event.headers['Content-Type'])

      if (type === 'application/x-www-form-urlencoded') {
        handler.event.body = parserFn(handler.event.body)
      }
    }

    next()
  }
})
github jamesshore / lets_code_javascript / node_modules / karma / node_modules / body-parser / lib / types / text.js View on Github external
function getCharset(req) {
  try {
    return contentType.parse(req).parameters.charset.toLowerCase()
  } catch (e) {
    return undefined
  }
}

content-type

Create and parse HTTP Content-Type header

MIT
Latest version published 1 year ago

Package Health Score

70 / 100
Full package analysis

Popular content-type functions