From 6f3085d756ab1fb0959259bd13d7499fa065a76c Mon Sep 17 00:00:00 2001 From: Stephane BACHELIER Date: Wed, 10 Feb 2016 16:12:38 +0100 Subject: [PATCH] fix(hydrate): support headers serialization from string --- lib/hydrate.js | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/lib/hydrate.js b/lib/hydrate.js index 5323690..1f86634 100644 --- a/lib/hydrate.js +++ b/lib/hydrate.js @@ -7,17 +7,43 @@ value should have the following format status: '', statusText: '' }, - headers: { - // map of HTTP response headers - } + headers: '' } */ + +// borrow from superagent +function trim (s) { + return ''.trim ? s.trim(s) : s.replace(/(^\s*|\s*$)/g, '') +} + +// borrow from superagent +function parseHeader (str) { + var lines = str.split(/\r?\n/) + var fields = {} + var index + var line + var field + var val + + lines.pop() // trailing CRLF + + for (let i = 0, len = lines.length; i < len; i += 1) { + line = lines[i] + index = line.indexOf(':') + field = line.slice(0, index).toLowerCase() + val = trim(line.slice(index + 1)) + fields[field] = val + } + + return fields +} + export default function hydrate (value) { - const headers = data.headers || {} const xhr = value.body || {} + const headers = parseHeader(value.headers || {}) xhr.getAllResponseHeaders = function () { - return headers + return value.headers } xhr.getResponseHeader = function (header) {