Skip to content

Commit 0bc10e8

Browse files
fix: support all the webs
* Collapse stream-collector-browser and stream-collector-native into stream-collector-web * Update FetchHttpHandler to return blob if stream not implemented
1 parent 65a3658 commit 0bc10e8

38 files changed

+91
-1100
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"build:smithy-client": "yarn build:crypto-dependencies && lerna run --scope '@aws-sdk/client-rds-data' --include-dependencies pretest",
1616
"build:all": "yarn build:crypto-dependencies && lerna run pretest --include-dependencies --include-dependents",
1717
"pretest:all": "yarn build:all",
18-
"test:all": "jest --coverage --passWithNoTests && lerna run test --scope @aws-sdk/stream-collector-browser --scope @aws-sdk/hash-blob-browser",
18+
"test:all": "jest --coverage --passWithNoTests && lerna run test --scope @aws-sdk/stream-collector-web --scope @aws-sdk/hash-blob-browser",
1919
"test:functional": "jest --config tests/functional/jest.config.js --passWithNoTests",
2020
"test:integration": "cucumber-js --fail-fast",
2121
"test:protocols": "yarn build:protocols && lerna run test --scope '@aws-sdk/protocol-tests-*'"

packages/fetch-http-handler/src/fetch-http-handler.spec.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe("httpHandler", () => {
4040
["bizz", "bazz"]
4141
])
4242
},
43-
body: "FOO" //should be a ReadableStream in real life.
43+
blob: jest.fn().mockResolvedValue(new Blob(["FOO"])),
4444
};
4545
const mockFetch = jest.fn().mockResolvedValue(mockResponse);
4646

@@ -50,7 +50,8 @@ describe("httpHandler", () => {
5050
let response = await fetchHttpHandler.handle({} as any, {});
5151

5252
expect(mockFetch.mock.calls.length).toBe(1);
53-
expect(response.response.body).toBe("FOO");
53+
console.log(response.response.body, typeof response.response.body);
54+
expect(await blobToText(response.response.body)).toBe("FOO");
5455
});
5556

5657
it("properly constructs url", async () => {
@@ -61,7 +62,7 @@ describe("httpHandler", () => {
6162
["bizz", "bazz"]
6263
])
6364
},
64-
body: ""
65+
blob: jest.fn().mockResolvedValue(new Blob()),
6566
};
6667
const mockFetch = jest.fn().mockResolvedValue(mockResponse);
6768

@@ -92,8 +93,7 @@ describe("httpHandler", () => {
9293
["bizz", "bazz"]
9394
])
9495
},
95-
blob: jest.fn().mockResolvedValue(""),
96-
body: "test"
96+
blob: jest.fn().mockResolvedValue(new Blob()),
9797
};
9898
const mockFetch = jest.fn().mockResolvedValue(mockResponse);
9999

@@ -119,8 +119,7 @@ describe("httpHandler", () => {
119119
["bizz", "bazz"]
120120
])
121121
},
122-
blob: jest.fn().mockResolvedValue(""),
123-
body: "test"
122+
blob: jest.fn().mockResolvedValue(new Blob()),
124123
};
125124
const mockFetch = jest.fn().mockResolvedValue(mockResponse);
126125
(global as any).fetch = mockFetch;
@@ -145,8 +144,7 @@ describe("httpHandler", () => {
145144
["bizz", "bazz"]
146145
])
147146
},
148-
blob: jest.fn().mockResolvedValue(""),
149-
body: "test"
147+
blob: jest.fn().mockResolvedValue(new Blob()),
150148
};
151149
const mockFetch = jest.fn().mockResolvedValue(mockResponse);
152150
(global as any).fetch = mockFetch;
@@ -207,4 +205,20 @@ describe("httpHandler", () => {
207205
expect(httpHandler.destroy()).toBeUndefined();
208206
});
209207
});
208+
209+
// The Blob implementation does not implement Blob.text, so we deal with it here.
210+
async function blobToText(blob: Blob): Promise<string> {
211+
const reader = new FileReader();
212+
213+
return new Promise((resolve) => {
214+
// This fires after the blob has been read/loaded.
215+
reader.addEventListener('loadend', (e) => {
216+
const text = e.target!.result as string;
217+
resolve(text);
218+
});
219+
220+
// Start reading the blob as text.
221+
reader.readAsText(blob);
222+
});
223+
}
210224
});

packages/fetch-http-handler/src/fetch-http-handler.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,6 @@ export interface BrowserHttpOptions {
1414
* terminated.
1515
*/
1616
requestTimeout?: number;
17-
/**
18-
* Buffer the whole response body before returning. This option is useful in the
19-
* runtime that doesn't support ReadableStream in response like ReactNative. When
20-
* set to true, the response body of http handler will be a Blob instead of
21-
* ReadableStream.
22-
* This option is only useful in ReactNative.
23-
*/
24-
bufferBody?: boolean;
2517
}
2618

2719
export class FetchHttpHandler implements HttpHandler {
@@ -80,8 +72,10 @@ export class FetchHttpHandler implements HttpHandler {
8072
transformedHeaders[pair[0]] = pair[1];
8173
}
8274

75+
const hasResponseStream = typeof ReadableStream === "function";
76+
8377
// Return the response with buffered body
84-
if (this.httpOptions.bufferBody) {
78+
if (!hasResponseStream) {
8579
return response.blob().then(body => ({
8680
response: new HttpResponse({
8781
headers: transformedHeaders,

0 commit comments

Comments
 (0)