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

Fix sendHttpRequest method to use the Headers class in the response #888

Merged
merged 1 commit into from
Apr 29, 2024
Merged
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
5 changes: 4 additions & 1 deletion packages/kernel/src/kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,10 @@ export class StliteKernel {
},
"http:response"
).then((data) => {
return data.response;
return {
...data.response,
headers: new Headers(Object.fromEntries(data.response.headers)),
};
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ vi.mock("../../kernel", () => {
if (htmlResponse) {
return Promise.resolve({
statusCode: 200,
headers: new Map([["Content-Type", htmlResponse.contentType]]),
headers: new Headers([["Content-Type", htmlResponse.contentType]]),
body: new TextEncoder().encode(htmlResponse.body),
});
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export function manipulateIFrameDocument(
if (statusCode !== 200) {
return;
}
const blob = new Blob([body], { type: headers.get("Content-Type") });
const type = headers.get("Content-Type");
const blob = new Blob([body], type ? { type } : undefined);
return blob.text().then((text) => {
const newScriptTag = document.createElement("script");
newScriptTag.text = text;
Expand Down Expand Up @@ -75,7 +76,8 @@ export function manipulateIFrameDocument(
if (statusCode !== 200) {
return;
}
const blob = new Blob([body], { type: headers.get("Content-Type") });
const type = headers.get("Content-Type");
const blob = new Blob([body], type ? { type } : undefined);
return blob.text().then((text) => {
const newStyleTag = document.createElement("style");
newStyleTag.innerHTML = text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ vi.mock("../../kernel", () => {
if (htmlResponse) {
return Promise.resolve({
statusCode: 200,
headers: new Map([["Content-Type", htmlResponse.contentType]]),
headers: new Headers([["Content-Type", htmlResponse.contentType]]),
body: new TextEncoder().encode(htmlResponse.body),
});
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ const InnerIFrame = React.forwardRef<
return;
}

const blob = new Blob([body], { type: headers.get("Content-Type") });
const type = headers.get("Content-Type");
const blob = new Blob([body], type ? { type } : undefined);
blob.text().then((text) => {
if (released) {
return;
Expand Down
3 changes: 2 additions & 1 deletion packages/kernel/src/react-helpers/download-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export function downloadFileFromStlite(
? parseContentDispositionHeader(contentDispositionHeader)
: "";

const blob = new Blob([body], { type: headers.get("Content-Type") });
const type = headers.get("Content-Type");
const blob = new Blob([body], type ? { type } : undefined);
const objectUrl = URL.createObjectURL(blob);

const link = document.createElement("a");
Expand Down
6 changes: 4 additions & 2 deletions packages/kernel/src/react-helpers/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export function useStliteMediaObjectUrl(rawUrl: string): string {
return;
}

const blob = new Blob([body], { type: headers.get("Content-Type") });
const type = headers.get("Content-Type");
const blob = new Blob([body], type ? { type } : undefined);
objectUrl = URL.createObjectURL(blob);
setUrl(objectUrl);
});
Expand Down Expand Up @@ -95,7 +96,8 @@ export function useStliteMediaObjects<T extends { url?: string | null }>(
return obj;
}

const blob = new Blob([body], { type: headers.get("Content-Type") });
const type = headers.get("Content-Type");
const blob = new Blob([body], type ? { type } : undefined);
const objectUrl = URL.createObjectURL(blob);
generatedObjectUrls.push(objectUrl);
return {
Expand Down
7 changes: 5 additions & 2 deletions packages/kernel/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
}
export interface HttpResponse {
statusCode: number;
headers: Map<string, string>;
headers: Headers;
body: Uint8Array;
}
export interface HttpResponseInMessage extends Omit<HttpResponse, "headers"> {
headers: Map<string, string>;
}
export interface EmscriptenFile {
data: string | ArrayBufferView;
opts?: Record<string, string>;
Expand Down Expand Up @@ -90,7 +93,7 @@
data: {
path: string;
data: string | ArrayBufferView;
opts?: Record<string, any>;

Check warning on line 96 in packages/kernel/src/types.ts

View workflow job for this annotation

GitHub Actions / test-kernel

Unexpected any. Specify a different type
};
}
export interface InMessageFileRename extends InMessageBase {
Expand Down Expand Up @@ -171,12 +174,12 @@
interface ReplyMessageBase {
type: string;
error?: Error;
data?: any;

Check warning on line 177 in packages/kernel/src/types.ts

View workflow job for this annotation

GitHub Actions / test-kernel

Unexpected any. Specify a different type
}
export interface ReplyMessageHttpResponse extends ReplyMessageBase {
type: "http:response";
data: {
response: HttpResponse;
response: HttpResponseInMessage;
};
}
export interface ReplyMessageGeneralReply extends ReplyMessageBase {
Expand Down
Loading