<commit_before>import inflection from 'inflection'
import deepEqual from 'deep-equal'
import { reservedKeys, inBrackets, isField, isFilter,
  mediaType, pageOffset, pageLimit } from './settings'


export function initializeContext (context, request) {
  const { uriTemplate, methodMap, inputMethods, recordTypes,
    adapter, keys, methods, options: { inflectType, inflectKeys },
    errors: { NotAcceptableError, UnsupportedError, NotFoundError }
  } = this

  // According to the spec, no accept parameters.
  if (request.headers['accept'] && ~request.headers['accept'].indexOf(';'))
    throw new NotAcceptableError(`Accept parameters are not allowed.`)

  // According to the spec, no content-type parameters.
  if (request.headers['content-type'] &&
  ~request.headers['content-type'].indexOf(';'))
    throw new UnsupportedError(`Media type parameters are not allowed.`)

  const { request: { serializerInput, serializerOutput, meta } } = context

  const method = context.request.method = methodMap[request.method]

  // Not according to the spec but probably a good idea in practice, do not
  // allow a different media type for input.
  if (serializerInput !== serializerOutput && inputMethods.has(method))
    throw new UnsupportedError(
      `The media type of the input must be "${mediaType}".`)

  // Decode URI Component only for the query string.
  const uriObject = uriTemplate.fromUri(request.url)

  if (!Object.keys(uriObject).length && request.url.length > 1)
    throw new NotFoundError(`Invalid URI.`)

  context.request.uriObject = uriObject

  context.request.type = uriObject.type ? inflectType ?
    inflection.singularize(uriObject.type) : uriObject.type : null

  context.request.ids = uriObject.ids ?
    (Array.isArray(uriObject.ids) ?
    uriObject.ids : [ uriObject.ids ])
    .map(castId) : null
<commit_msg>fix Accept header handling<commit_after>import inflection from 'inflection'
import deepEqual from 'deep-equal'
import { reservedKeys, inBrackets, isField, isFilter,
  mediaType, pageOffset, pageLimit } from './settings'


export function initializeContext (context, request) {
  const { uriTemplate, methodMap, inputMethods, recordTypes,
    adapter, keys, methods, options: { inflectType, inflectKeys },
    errors: { NotAcceptableError, UnsupportedError, NotFoundError }
  } = this

  // According to the spec, if the media type is provided in the Accept
  // header, it should be included at least once without any media type
  // parameters.
  if (request.headers['accept'] &&
    ~request.headers['accept'].indexOf(mediaType)) {
    const escapedMediaType = mediaType.replace(/[\+\.]/g, '\\$&')
    const mediaTypeRegex = new RegExp(`${escapedMediaType}(?!;)`, 'g')
    if (!request.headers['accept'].match(mediaTypeRegex))
      throw new NotAcceptableError(`The Accept header should contain` +
        `at least one instance of the JSON media type without any` +
        `media type parameters.`)
  }

  const { request: { serializerInput, serializerOutput, meta } } = context

  const method = context.request.method = methodMap[request.method]

  // Not according to the spec but probably a good idea in practice, do not
  // allow a different media type for input.
  if (serializerInput !== serializerOutput && inputMethods.has(method))
    throw new UnsupportedError(
      `The media type of the input must be "${mediaType}".`)

  // Decode URI Component only for the query string.
  const uriObject = uriTemplate.fromUri(request.url)

  if (!Object.keys(uriObject).length && request.url.length > 1)
    throw new NotFoundError(`Invalid URI.`)

  context.request.uriObject = uriObject

  context.request.type = uriObject.type ? inflectType ?
    inflection.singularize(uriObject.type) : uriObject.type : null

  context.request.ids = uriObject.ids ?
    (Array.isArray(uriObject.ids) ?
    uriObject.ids : [ uriObject.ids ])
    .map(castId) : null

{'fraction_non_alphanumeric': 0.11350243402510889, 'fraction_numerical': 0.0005124263387138099, 'mean_word_length': 4.089960886571056, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3774423', 'n_tokens_mistral': 1108, 'n_tokens_neox': 1081, 'n_words': 338}