fetch-multipart-graphql
Advanced tools
Comparing version 3.1.0 to 3.1.1
@@ -32,2 +32,6 @@ "use strict"; | ||
function getClosingDelimiter(boundary) { | ||
return "\r\n--".concat(boundary, "--\r\n"); | ||
} | ||
function splitWithRest(string, delim) { | ||
@@ -66,15 +70,26 @@ var index = string.indexOf(delim); | ||
if (!(region && region.length)) { | ||
// we need more things | ||
return { | ||
newBuffer: buffer, | ||
parts: previousParts, | ||
isPreamble: isPreamble | ||
}; | ||
if (!region) { | ||
var closingDelimiter = getClosingDelimiter(boundary); | ||
var _splitWithRest3 = splitWithRest(buffer, closingDelimiter); | ||
var _splitWithRest4 = _slicedToArray(_splitWithRest3, 2); | ||
region = _splitWithRest4[0]; | ||
next = _splitWithRest4[1]; | ||
if (!region) { | ||
// we need more things | ||
return { | ||
newBuffer: buffer, | ||
parts: previousParts, | ||
isPreamble: isPreamble | ||
}; | ||
} | ||
} | ||
var _splitWithRest3 = splitWithRest(region, '\r\n\r\n'), | ||
_splitWithRest4 = _slicedToArray(_splitWithRest3, 2), | ||
_headers = _splitWithRest4[0], | ||
body = _splitWithRest4[1]; // remove trailing boundary things | ||
var _splitWithRest5 = splitWithRest(region, '\r\n\r\n'), | ||
_splitWithRest6 = _slicedToArray(_splitWithRest5, 2), | ||
_headers = _splitWithRest6[0], | ||
body = _splitWithRest6[1]; // remove trailing boundary things | ||
@@ -81,0 +96,0 @@ |
{ | ||
"name": "fetch-multipart-graphql", | ||
"version": "3.1.0", | ||
"version": "3.1.1", | ||
"description": "Cross browser function to fetch and parse streaming multipart graphql responses.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -10,8 +10,3 @@ import { PatchResolver } from '../PatchResolver'; | ||
return [ | ||
'Content-Type: application/json', | ||
'', | ||
json, | ||
`--${boundary}\r\n`, | ||
].join('\r\n'); | ||
return ['Content-Type: application/json', '', json, `--${boundary}\r\n`].join('\r\n'); | ||
} | ||
@@ -197,4 +192,115 @@ | ||
}); | ||
it('should work when final chunk ends with terminating boundary', function () { | ||
const onResponse = jest.fn(); | ||
const resolver = new PatchResolver({ | ||
onResponse, | ||
boundary, | ||
}); | ||
resolver.handleChunk(`\r\n--${boundary}\r\n`); | ||
expect(onResponse).not.toHaveBeenCalled(); | ||
resolver.handleChunk(chunk1); | ||
expect(onResponse.mock.calls[0][0]).toEqual([chunk1Data]); | ||
onResponse.mockClear(); | ||
resolver.handleChunk(chunk2); | ||
expect(onResponse.mock.calls[0][0]).toEqual([chunk2Data]); | ||
onResponse.mockClear(); | ||
resolver.handleChunk(chunk3); | ||
expect(onResponse.mock.calls[0][0]).toEqual([chunk3Data]); | ||
onResponse.mockClear(); | ||
const chunk4FinalBoundary = getMultiPartResponse(chunk4Data, `${boundary}--`); | ||
resolver.handleChunk(chunk4FinalBoundary); | ||
expect(onResponse.mock.calls[0][0]).toEqual([chunk4Data]); | ||
}); | ||
it('should work with preamble', function () { | ||
const onResponse = jest.fn(); | ||
const resolver = new PatchResolver({ | ||
onResponse, | ||
boundary, | ||
}); | ||
resolver.handleChunk(`This is some preamble data that should be ignored\r\n`); | ||
resolver.handleChunk(`\r\n--${boundary}\r\n`); | ||
expect(onResponse).not.toHaveBeenCalled(); | ||
resolver.handleChunk(chunk1); | ||
expect(onResponse.mock.calls[0][0]).toEqual([chunk1Data]); | ||
onResponse.mockClear(); | ||
resolver.handleChunk(chunk2); | ||
expect(onResponse.mock.calls[0][0]).toEqual([chunk2Data]); | ||
onResponse.mockClear(); | ||
resolver.handleChunk(chunk3); | ||
expect(onResponse.mock.calls[0][0]).toEqual([chunk3Data]); | ||
onResponse.mockClear(); | ||
const chunk4FinalBoundary = getMultiPartResponse(chunk4Data, `${boundary}--`); | ||
resolver.handleChunk(chunk4FinalBoundary); | ||
expect(onResponse.mock.calls[0][0]).toEqual([chunk4Data]); | ||
}); | ||
it('should work with epilogue', function () { | ||
const onResponse = jest.fn(); | ||
const resolver = new PatchResolver({ | ||
onResponse, | ||
boundary, | ||
}); | ||
resolver.handleChunk(`\r\n--${boundary}\r\n`); | ||
expect(onResponse).not.toHaveBeenCalled(); | ||
resolver.handleChunk(chunk1); | ||
expect(onResponse.mock.calls[0][0]).toEqual([chunk1Data]); | ||
onResponse.mockClear(); | ||
resolver.handleChunk(chunk2); | ||
expect(onResponse.mock.calls[0][0]).toEqual([chunk2Data]); | ||
onResponse.mockClear(); | ||
resolver.handleChunk(chunk3); | ||
expect(onResponse.mock.calls[0][0]).toEqual([chunk3Data]); | ||
onResponse.mockClear(); | ||
resolver.handleChunk(chunk4); | ||
resolver.handleChunk(`This is some epilogue data that should be ignored\r\n`); | ||
expect(onResponse.mock.calls[0][0]).toEqual([chunk4Data]); | ||
}); | ||
it('should work with epilogue after chunk with terminating boundary', function () { | ||
const onResponse = jest.fn(); | ||
const resolver = new PatchResolver({ | ||
onResponse, | ||
boundary, | ||
}); | ||
resolver.handleChunk(`\r\n--${boundary}\r\n`); | ||
expect(onResponse).not.toHaveBeenCalled(); | ||
resolver.handleChunk(chunk1); | ||
expect(onResponse.mock.calls[0][0]).toEqual([chunk1Data]); | ||
onResponse.mockClear(); | ||
resolver.handleChunk(chunk2); | ||
expect(onResponse.mock.calls[0][0]).toEqual([chunk2Data]); | ||
onResponse.mockClear(); | ||
resolver.handleChunk(chunk3); | ||
expect(onResponse.mock.calls[0][0]).toEqual([chunk3Data]); | ||
onResponse.mockClear(); | ||
const chunk4FinalBoundary = getMultiPartResponse(chunk4Data, `${boundary}--`); | ||
resolver.handleChunk(chunk4FinalBoundary); | ||
resolver.handleChunk(`This is some epilogue data that should be ignored\r\n`); | ||
expect(onResponse.mock.calls[0][0]).toEqual([chunk4Data]); | ||
}); | ||
}); | ||
} | ||
}); |
@@ -5,2 +5,6 @@ function getDelimiter(boundary) { | ||
function getClosingDelimiter(boundary) { | ||
return `\r\n--${boundary}--\r\n`; | ||
} | ||
function splitWithRest(string, delim) { | ||
@@ -17,3 +21,3 @@ const index = string.indexOf(delim); | ||
const [region, next] = splitWithRest(buffer, delimiter); | ||
let [region, next] = splitWithRest(buffer, delimiter); | ||
@@ -29,9 +33,14 @@ if (region !== undefined && (region.length || region.trim() === '') && isPreamble) { | ||
if (!(region && region.length)) { | ||
// we need more things | ||
return { | ||
newBuffer: buffer, | ||
parts: previousParts, | ||
isPreamble | ||
}; | ||
if (!region) { | ||
const closingDelimiter = getClosingDelimiter(boundary); | ||
[region, next] = splitWithRest(buffer, closingDelimiter); | ||
if (!region) { | ||
// we need more things | ||
return { | ||
newBuffer: buffer, | ||
parts: previousParts, | ||
isPreamble, | ||
}; | ||
} | ||
} | ||
@@ -42,5 +51,3 @@ | ||
// remove trailing boundary things | ||
body = body | ||
.replace(delimiter + '\r\n', '') | ||
.replace(delimiter + '--\r\n', ''); | ||
body = body.replace(delimiter + '\r\n', '').replace(delimiter + '--\r\n', ''); | ||
@@ -47,0 +54,0 @@ const payload = JSON.parse(body); |
42830
791