Skip to content

Commit

Permalink
fix all the multipart streams (#269)
Browse files Browse the repository at this point in the history
* fix all the multipart streams

* npm i & bump bundle watch

* changeset
  • Loading branch information
mayakoneval authored Aug 23, 2023
1 parent 8822ffa commit 1a57130
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 8 deletions.
7 changes: 7 additions & 0 deletions .changeset/popular-rules-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@apollo/explorer": patch
"@apollo/sandbox": patch
"@apollo/explorer-helpers": patch
---

We were out of protocol for HTTP Multipart Subscriptions. I was using an older version of the draft protocol. This should update to use the real HTTP Multipart Subscription protocol: https://www.apollographql.com/docs/router/executing-operations/subscription-multipart-protocol/.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@
"files": [
{
"path": "packages/sandbox/dist/**/*.js",
"maxSize": "40kb"
"maxSize": "41kb"
},
{
"path": "packages/explorer/dist/**/*.js",
"maxSize": "40kb"
"maxSize": "41kb"
}
],
"ci": {
Expand Down
18 changes: 17 additions & 1 deletion packages/explorer-helpers/src/readMultipartWebStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,23 @@ export function readMultipartWebStream(response: Response, mimeType: MIMEType) {
buffer = buffer.slice(boundaryIndex + messageBoundary.length);

if (message.trim()) {
const messageStartIndex = message.indexOf('\r\n\r\n');
const newLineSequence = '\r\n\r\n';
let messageStartIndex: number | undefined;
// if there are two instances of newLineSequence, this is a response with multiple parts
// and the first part is a heartbeat: https://www.apollographql.com/docs/router/executing-operations/subscription-multipart-protocol/
if (
message.lastIndexOf(newLineSequence) !==
message.indexOf(newLineSequence)
) {
const heartbeatStartIndex =
message.indexOf(newLineSequence) + newLineSequence.length;
messageStartIndex =
message
.substring(heartbeatStartIndex)
.indexOf(newLineSequence) + heartbeatStartIndex;
} else {
messageStartIndex = message.indexOf(newLineSequence);
}

const chunkHeaders = Object.fromEntries(
message
Expand Down
18 changes: 17 additions & 1 deletion packages/explorer/src/helpers/readMultipartWebStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,23 @@ export function readMultipartWebStream(response: Response, mimeType: MIMEType) {
buffer = buffer.slice(boundaryIndex + messageBoundary.length);

if (message.trim()) {
const messageStartIndex = message.indexOf('\r\n\r\n');
const newLineSequence = '\r\n\r\n';
let messageStartIndex: number | undefined;
// if there are two instances of newLineSequence, this is a response with multiple parts
// and the first part is a heartbeat: https://www.apollographql.com/docs/router/executing-operations/subscription-multipart-protocol/
if (
message.lastIndexOf(newLineSequence) !==
message.indexOf(newLineSequence)
) {
const heartbeatStartIndex =
message.indexOf(newLineSequence) + newLineSequence.length;
messageStartIndex =
message
.substring(heartbeatStartIndex)
.indexOf(newLineSequence) + heartbeatStartIndex;
} else {
messageStartIndex = message.indexOf(newLineSequence);
}

const chunkHeaders = Object.fromEntries(
message
Expand Down
18 changes: 17 additions & 1 deletion packages/sandbox/src/helpers/readMultipartWebStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,23 @@ export function readMultipartWebStream(response: Response, mimeType: MIMEType) {
buffer = buffer.slice(boundaryIndex + messageBoundary.length);

if (message.trim()) {
const messageStartIndex = message.indexOf('\r\n\r\n');
const newLineSequence = '\r\n\r\n';
let messageStartIndex: number | undefined;
// if there are two instances of newLineSequence, this is a response with multiple parts
// and the first part is a heartbeat: https://www.apollographql.com/docs/router/executing-operations/subscription-multipart-protocol/
if (
message.lastIndexOf(newLineSequence) !==
message.indexOf(newLineSequence)
) {
const heartbeatStartIndex =
message.indexOf(newLineSequence) + newLineSequence.length;
messageStartIndex =
message
.substring(heartbeatStartIndex)
.indexOf(newLineSequence) + heartbeatStartIndex;
} else {
messageStartIndex = message.indexOf(newLineSequence);
}

const chunkHeaders = Object.fromEntries(
message
Expand Down

0 comments on commit 1a57130

Please sign in to comment.