Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Support all the webs #1123

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"build:smithy-client": "yarn build:crypto-dependencies && lerna run --scope '@aws-sdk/client-rds-data' --include-dependencies pretest",
"build:all": "yarn build:crypto-dependencies && lerna run pretest --include-dependencies --include-dependents",
"pretest:all": "yarn build:all",
"test:all": "jest --coverage --passWithNoTests && lerna run test --scope @aws-sdk/stream-collector-browser --scope @aws-sdk/hash-blob-browser",
"test:all": "jest --coverage --passWithNoTests && lerna run test --scope @aws-sdk/stream-collector-web --scope @aws-sdk/hash-blob-browser",
"test:functional": "jest --config tests/functional/jest.config.js --passWithNoTests",
"test:integration": "cucumber-js --fail-fast",
"test:protocols": "yarn build:protocols && lerna run test --scope '@aws-sdk/aws-*'"
Expand Down
32 changes: 23 additions & 9 deletions packages/fetch-http-handler/src/fetch-http-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe("httpHandler", () => {
["bizz", "bazz"]
])
},
body: "FOO" //should be a ReadableStream in real life.
blob: jest.fn().mockResolvedValue(new Blob(["FOO"])),
};
const mockFetch = jest.fn().mockResolvedValue(mockResponse);

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

expect(mockFetch.mock.calls.length).toBe(1);
expect(response.response.body).toBe("FOO");
console.log(response.response.body, typeof response.response.body);
expect(await blobToText(response.response.body)).toBe("FOO");
});

it("properly constructs url", async () => {
Expand All @@ -61,7 +62,7 @@ describe("httpHandler", () => {
["bizz", "bazz"]
])
},
body: ""
blob: jest.fn().mockResolvedValue(new Blob()),
};
const mockFetch = jest.fn().mockResolvedValue(mockResponse);

Expand Down Expand Up @@ -92,8 +93,7 @@ describe("httpHandler", () => {
["bizz", "bazz"]
])
},
blob: jest.fn().mockResolvedValue(""),
body: "test"
blob: jest.fn().mockResolvedValue(new Blob()),
};
const mockFetch = jest.fn().mockResolvedValue(mockResponse);

Expand All @@ -119,8 +119,7 @@ describe("httpHandler", () => {
["bizz", "bazz"]
])
},
blob: jest.fn().mockResolvedValue(""),
body: "test"
blob: jest.fn().mockResolvedValue(new Blob()),
};
const mockFetch = jest.fn().mockResolvedValue(mockResponse);
(global as any).fetch = mockFetch;
Expand All @@ -145,8 +144,7 @@ describe("httpHandler", () => {
["bizz", "bazz"]
])
},
blob: jest.fn().mockResolvedValue(""),
body: "test"
blob: jest.fn().mockResolvedValue(new Blob()),
};
const mockFetch = jest.fn().mockResolvedValue(mockResponse);
(global as any).fetch = mockFetch;
Expand Down Expand Up @@ -207,4 +205,20 @@ describe("httpHandler", () => {
expect(httpHandler.destroy()).toBeUndefined();
});
});

// The Blob implementation does not implement Blob.text, so we deal with it here.
async function blobToText(blob: Blob): Promise<string> {
const reader = new FileReader();

return new Promise((resolve) => {
// This fires after the blob has been read/loaded.
reader.addEventListener('loadend', (e) => {
const text = e.target!.result as string;
resolve(text);
});

// Start reading the blob as text.
reader.readAsText(blob);
});
}
});
12 changes: 3 additions & 9 deletions packages/fetch-http-handler/src/fetch-http-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ export interface BrowserHttpOptions {
* terminated.
*/
requestTimeout?: number;
/**
* Buffer the whole response body before returning. This option is useful in the
* runtime that doesn't support ReadableStream in response like ReactNative. When
* set to true, the response body of http handler will be a Blob instead of
* ReadableStream.
* This option is only useful in ReactNative.
*/
bufferBody?: boolean;
}

export class FetchHttpHandler implements HttpHandler {
Expand Down Expand Up @@ -80,8 +72,10 @@ export class FetchHttpHandler implements HttpHandler {
transformedHeaders[pair[0]] = pair[1];
}

const hasReadableStream = response.body !== undefined;

// Return the response with buffered body
if (this.httpOptions.bufferBody) {
if (!hasReadableStream) {
return response.blob().then(body => ({
response: new HttpResponse({
headers: transformedHeaders,
Expand Down
Loading