netlify-headers-parser
Advanced tools
Comparing version 1.1.1 to 2.0.0
{ | ||
"name": "netlify-headers-parser", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"description": "Parses Netlify headers into a JavaScript object representation", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -5,2 +5,3 @@ const { parseFileHeaders } = require('./line_parser') | ||
const { normalizeHeaders } = require('./normalize') | ||
const { splitResults, concatResults } = require('./results') | ||
@@ -10,15 +11,23 @@ // Parse all headers from `netlify.toml` and `_headers` file, then normalize | ||
const parseAllHeaders = async function ({ headersFiles = [], netlifyConfigPath } = {}) { | ||
const [fileHeaders, configHeaders] = await Promise.all([ | ||
getFileHeaders(headersFiles), | ||
getConfigHeaders(netlifyConfigPath), | ||
]) | ||
const normalizedFileHeaders = normalizeHeaders(fileHeaders) | ||
const normalizedConfigHeaders = normalizeHeaders(configHeaders) | ||
return mergeHeaders({ fileHeaders: normalizedFileHeaders, configHeaders: normalizedConfigHeaders }) | ||
const [{ headers: fileHeaders, errors: fileParseErrors }, { headers: configHeaders, errors: configParseErrors }] = | ||
await Promise.all([getFileHeaders(headersFiles), getConfigHeaders(netlifyConfigPath)]) | ||
const { headers: normalizedFileHeaders, errors: fileNormalizeErrors } = normalizeHeaders(fileHeaders) | ||
const { headers: normalizedConfigHeaders, errors: configNormalizeErrors } = normalizeHeaders(configHeaders) | ||
const { headers, errors: mergeErrors } = mergeHeaders({ | ||
fileHeaders: normalizedFileHeaders, | ||
configHeaders: normalizedConfigHeaders, | ||
}) | ||
const errors = [ | ||
...fileParseErrors, | ||
...fileNormalizeErrors, | ||
...configParseErrors, | ||
...configNormalizeErrors, | ||
...mergeErrors, | ||
] | ||
return { headers, errors } | ||
} | ||
const getFileHeaders = async function (headersFiles) { | ||
const fileHeaders = await Promise.all(headersFiles.map(parseFileHeaders)) | ||
// eslint-disable-next-line unicorn/prefer-spread | ||
return [].concat(...fileHeaders) | ||
const resultsArrays = await Promise.all(headersFiles.map(parseFileHeaders)) | ||
return concatResults(resultsArrays) | ||
} | ||
@@ -28,3 +37,3 @@ | ||
if (netlifyConfigPath === undefined) { | ||
return [] | ||
return splitResults([]) | ||
} | ||
@@ -31,0 +40,0 @@ |
@@ -6,2 +6,4 @@ const fs = require('fs') | ||
const { splitResults } = require('./results') | ||
const readFileAsync = promisify(fs.readFile) | ||
@@ -13,7 +15,11 @@ | ||
if (!(await pathExists(headersFile))) { | ||
return [] | ||
return splitResults([]) | ||
} | ||
const text = await readFileAsync(headersFile, 'utf-8') | ||
return text.split('\n').map(normalizeLine).filter(hasHeader).map(parseLine).filter(Boolean).reduce(reduceLine, []) | ||
const results = text.split('\n').map(normalizeLine).filter(hasHeader).map(parseLine).filter(Boolean) | ||
const { headers, errors: parseErrors } = splitResults(results) | ||
const { headers: reducedHeaders, errors: reducedErrors } = headers.reduce(reduceLine, { headers: [], errors: [] }) | ||
const errors = [...parseErrors, ...reducedErrors] | ||
return { headers: reducedHeaders, errors } | ||
} | ||
@@ -33,3 +39,3 @@ | ||
} catch (error) { | ||
throw new Error(`Could not parse header line ${index + 1}: | ||
return new Error(`Could not parse header line ${index + 1}: | ||
${line} | ||
@@ -71,9 +77,10 @@ ${error.message}`) | ||
const reduceLine = function (headers, { path, name, value }) { | ||
const reduceLine = function ({ headers, errors }, { path, name, value }) { | ||
if (path !== undefined) { | ||
return [...headers, { for: path, values: {} }] | ||
return { headers: [...headers, { for: path, values: {} }], errors } | ||
} | ||
if (headers.length === 0) { | ||
throw new Error(`Path should come before headers`) | ||
const error = new Error(`Path should come before header "${name}"`) | ||
return { headers, errors: [...errors, error] } | ||
} | ||
@@ -85,5 +92,6 @@ | ||
const newValue = values[name] === undefined ? value : `${values[name]},${value}` | ||
return [...previousHeaders, { ...currentHeader, values: { ...values, [name]: newValue } }] | ||
const newHeaders = [...previousHeaders, { ...currentHeader, values: { ...values, [name]: newValue } }] | ||
return { headers: newHeaders, errors } | ||
} | ||
module.exports = { parseFileHeaders } |
@@ -18,10 +18,19 @@ const { inspect, isDeepStrictEqual } = require('util') | ||
const mergeHeaders = function ({ fileHeaders = [], configHeaders = [] }) { | ||
validateArray(fileHeaders) | ||
validateArray(configHeaders) | ||
return [...fileHeaders, ...configHeaders].filter(isUniqueHeader) | ||
const errors = validateArrays(fileHeaders, configHeaders) | ||
if (errors.length !== 0) { | ||
return { headers: [], errors } | ||
} | ||
const headers = [...fileHeaders, ...configHeaders].filter(isUniqueHeader) | ||
return { headers, errors: [] } | ||
} | ||
const validateArrays = function (fileHeaders, configHeaders) { | ||
const fileError = validateArray(fileHeaders) | ||
const configError = validateArray(configHeaders) | ||
return [fileError, configError].filter(Boolean) | ||
} | ||
const validateArray = function (headers) { | ||
if (!Array.isArray(headers)) { | ||
throw new TypeError(`Headers should be an array: ${inspect(headers, { colors: false })}`) | ||
return new TypeError(`Headers should be an array: ${inspect(headers, { colors: false })}`) | ||
} | ||
@@ -28,0 +37,0 @@ } |
@@ -7,2 +7,4 @@ const { readFile } = require('fs') | ||
const { splitResults } = require('./results') | ||
const pReadFile = promisify(readFile) | ||
@@ -15,7 +17,7 @@ | ||
if (!(await pathExists(netlifyConfigPath))) { | ||
return [] | ||
return splitResults([]) | ||
} | ||
const { headers = [] } = await parseConfig(netlifyConfigPath) | ||
return headers | ||
const headers = await parseConfig(netlifyConfigPath) | ||
return splitResults(headers) | ||
} | ||
@@ -29,5 +31,9 @@ | ||
// Convert `null` prototype objects to normal plain objects | ||
return JSON.parse(JSON.stringify(config)) | ||
const { headers = [] } = JSON.parse(JSON.stringify(config)) | ||
if (!Array.isArray(headers)) { | ||
throw new TypeError(`"headers" must be an array`) | ||
} | ||
return headers | ||
} catch (error) { | ||
throw new Error(`Could not parse configuration file: ${error}`) | ||
return [new Error(`Could not parse configuration file: ${error}`)] | ||
} | ||
@@ -34,0 +40,0 @@ } |
const isPlainObj = require('is-plain-obj') | ||
const mapObj = require('map-obj') | ||
const { splitResults } = require('./results') | ||
// Validate and normalize an array of `headers` objects. | ||
@@ -9,6 +11,8 @@ // This step is performed after `headers` have been parsed from either | ||
if (!Array.isArray(headers)) { | ||
throw new TypeError(`Headers must be an array not: ${headers}`) | ||
const error = new TypeError(`Headers must be an array not: ${headers}`) | ||
return splitResults([error]) | ||
} | ||
return headers.map(parseHeader).filter(Boolean) | ||
const results = headers.map(parseHeader).filter(Boolean) | ||
return splitResults(results) | ||
} | ||
@@ -18,3 +22,3 @@ | ||
if (!isPlainObj(header)) { | ||
throw new TypeError(`Header must be an object not: ${header}`) | ||
return new TypeError(`Header must be an object not: ${header}`) | ||
} | ||
@@ -25,3 +29,3 @@ | ||
} catch (error) { | ||
throw new Error(`Could not parse header number ${index + 1}: | ||
return new Error(`Could not parse header number ${index + 1}: | ||
${JSON.stringify(header)} | ||
@@ -28,0 +32,0 @@ ${error.message}`) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15976
10
319