Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const keys = [];
const values = [];
request.headers.forEach(({ name, value }) => {
keys.push(name);
values.push(value);
});
const headersString =
request.method + request.url + keys.join() + values.join();
// startline: [method] [url] HTTP/1.1\r\n = 12
// endline: \r\n = 2
// every header + \r\n = * 2
// add 2 for each combined header
return getByteLength(headersString) + keys.length * 2 + 2 + 12 + 2;
}
function headersSize(response) {
const keys = [];
const values = [];
response.headers.forEach(({ name, value }) => {
keys.push(name);
values.push(value);
});
const headersString = keys.join() + values.join();
// endline: \r\n = 2
// every header + \r\n = * 2
// add 2 for each combined header
return getByteLength(headersString) + keys.length * 2 + 2 + 2;
}
const limit = 1024 * 1024 * 5; // 5 MB
const usedSpace = unescape(
encodeURIComponent(JSON.stringify(sessionStorage))
).length;
const remSpace = limit - usedSpace;
this.log(`session storage bytes in use: ${usedSpace}`);
this.log(`remaining bytes available: ${remSpace}`);
// get a string of our data.
const jsonString = JSON.stringify({
data: { wordsbyCollections: liveData },
latestPublishTime: latestPublishTime
});
const dataLength = getLength(jsonString);
// if our string takes more storage than what's left, clear the storage
if (dataLength > remSpace) sessionStorage.clear();
// store data in session storage so that reloads dont require more http requests.
sessionStorage.setItem(storageName, jsonString);
this.log(`session data stored in key: ${storageName}`);
}
validate: (value) => {
const nameList = value.split('\n').filter(name => name)
for (const name of nameList) {
if (stringLength(name) > 256) return false
}
return true
}
},
this.content = {
mimeType: getFirstHeader(response, 'Content-Type') || 'text/plain'
};
if (response.body && typeof response.body === 'string') {
this.content.text = response.body;
}
const contentLength = getFirstHeader(response, 'Content-Length');
if (contentLength) {
this.content.size = parseInt(contentLength, 10);
} else {
this.content.size = this.content.text
? getByteLength(this.content.text)
: 0;
}
this.bodySize = this.content.size;
}
}
params: []
};
if (typeof request.body === 'string') {
this.postData.text = request.body;
}
}
const contentLength = getFirstHeader(request, 'Content-Length');
if (contentLength) {
this.bodySize = parseInt(contentLength, 10);
} else {
this.bodySize =
this.postData && this.postData.text
? getByteLength(this.postData.text)
: 0;
}
}
}
validate: (value, [length]) => {
return stringLength(value) <= length
}
},