Skip to content

Commit

Permalink
feat: remove stream and add file function
Browse files Browse the repository at this point in the history
  • Loading branch information
aaydin-tr committed Jul 11, 2024
1 parent 91135b0 commit 0708d34
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 39 deletions.
26 changes: 0 additions & 26 deletions __test__/ikari.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,25 +540,6 @@ test("Context", async () => {
return ctx.buffer(Buffer.from("test"));
}

@Get("/get-return-stream")
@After((ctx: Context) => {
if(!ctx.locals.has("filePath"))
return
const filePath = ctx.locals.get("filePath") as string;
setTimeout(() => {
unlinkSync(filePath);
}, 1000);
})
public async getReturnStream(ctx: Context) {
// create temporary file
const filePath = import.meta.dir + "/test-file";
await Bun.write(filePath, "test");
const f = Bun.file(filePath);
ctx.stream(f.stream());
ctx.locals.set("filePath", filePath);
return ctx.next();
}

@Get("/get-return-raw")
public getReturnRaw(ctx: Context) {
return ctx.raw(new Response("test"));
Expand Down Expand Up @@ -776,13 +757,6 @@ test("Context", async () => {
body: "test",
statusCode: StatusCode.OK,
},
{
path: "/get-return-stream",
method: HTTPMethod.GET,
bodyType: "text",
body: "test",
statusCode: StatusCode.OK,
},
{
path: "/get-return-raw",
method: HTTPMethod.GET,
Expand Down
26 changes: 13 additions & 13 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Server } from "bun";
import { BunFile, Server } from "bun";
import { HTTPMethod } from "./utils";
import { parse } from "fast-querystring";
import { Routes } from "./route";
Expand Down Expand Up @@ -435,26 +435,26 @@ export class Context {
}

/**
* Sets the specified stream to the Response object.
* Returns the specified file as a response.
* @example
* ```ts
* ctx.stream(fs.createReadStream("test.txt"));
* ctx.file(Bun.file("test.txt"));
* ```
* @param data
* @param status Default: 200
* @param contentType Default: application/octet-stream
* @param contentType
**/
public stream(
data: ReadableStream,
status?: number,
contentType: string = "application/octet-stream"
): Context {
public file(data: BunFile, status?: number, contentType?: string): Context {
const headers = {
...this.res.headers.toJSON(),
};
if (contentType) {
headers["Content-Type"] = contentType;
}

this.res = new Response(data, {
status: status || this.res.status,
headers: {
...this.res.headers.toJSON(),
"Content-Type": contentType,
},
headers: headers,
});

return this;
Expand Down

0 comments on commit 0708d34

Please sign in to comment.