From 9d8ca9e751d5286ebe8e4bd4be0a8e6935a28d5f Mon Sep 17 00:00:00 2001 From: kittenfromvoid Date: Fri, 20 Aug 2021 13:38:10 +0200 Subject: [PATCH] dodanie tempstream Summary: Ref T2491 Reviewers: #reviewers Subscribers: jenkins-user Maniphest Tasks: T2491 Differential Revision: https://hub.sealcode.org/D1174 --- package-lock.json | 13 ++++++++++++- package.json | 3 ++- src/back/html.ts | 12 ++++++------ src/back/routes/homepage.ts | 14 ++++++++------ src/back/routes/login.ts | 6 +++--- src/back/routes/tasks.ts | 4 ++-- 6 files changed, 33 insertions(+), 19 deletions(-) diff --git a/package-lock.json b/package-lock.json index abe2736..66bc42a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,7 +14,8 @@ "multiple-scripts-tmux": "^1.0.3", "nodemon": "^2.0.7", "sealious": "^0.13.38", - "stimulus": "^2.0.0" + "stimulus": "^2.0.0", + "tempstream": "^0.0.7" }, "devDependencies": { "@sealcode/ansi-html-stream": "^1.0.1", @@ -8662,6 +8663,11 @@ "node": ">= 6" } }, + "node_modules/tempstream": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/tempstream/-/tempstream-0.0.7.tgz", + "integrity": "sha512-Etn/RJSwL+u7QGpk3acTXkxsJ8wgg20glZt1sQ0Zah1pPhoxqqOANjS9WrlILYfcdr8XrAgnhmk1ntN+Y2x0ew==" + }, "node_modules/term-size": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/term-size/-/term-size-2.2.1.tgz", @@ -16608,6 +16614,11 @@ } } }, + "tempstream": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/tempstream/-/tempstream-0.0.7.tgz", + "integrity": "sha512-Etn/RJSwL+u7QGpk3acTXkxsJ8wgg20glZt1sQ0Zah1pPhoxqqOANjS9WrlILYfcdr8XrAgnhmk1ntN+Y2x0ew==" + }, "term-size": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/term-size/-/term-size-2.2.1.tgz", diff --git a/package.json b/package.json index bf78eec..e5b3bfe 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,8 @@ "multiple-scripts-tmux": "^1.0.3", "nodemon": "^2.0.7", "sealious": "^0.13.38", - "stimulus": "^2.0.0" + "stimulus": "^2.0.0", + "tempstream": "^0.0.7" }, "devDependencies": { "@sealcode/ansi-html-stream": "^1.0.1", diff --git a/src/back/html.ts b/src/back/html.ts index 7460965..1b00ced 100644 --- a/src/back/html.ts +++ b/src/back/html.ts @@ -1,10 +1,10 @@ -import { Context } from "sealious"; +import { Templatable, tempstream } from "tempstream"; +import { Readable } from "stream"; +import { BaseContext } from "koa"; -export default async function html( - _context: Context, - body: string -): Promise { - return /* HTML */ ` +export default function html(ctx: BaseContext, body: Templatable): Readable { + ctx.set("content-type", "text/html;charset=utf-8"); + return tempstream/* HTML */ ` diff --git a/src/back/routes/homepage.ts b/src/back/routes/homepage.ts index 56df810..571b108 100644 --- a/src/back/routes/homepage.ts +++ b/src/back/routes/homepage.ts @@ -2,22 +2,24 @@ import Router from "@koa/router"; import { Context, Middlewares } from "sealious"; import html from "../html"; import { NewTask, TaskList } from "../views/tasks"; +import { BaseContext } from "koa"; +import { Readable } from "stream"; const router = new Router(); -export async function MainView(context: Context): Promise { - return await html( - context, - /* HTML */ `My ToDo App +export async function MainView(ctx: BaseContext): Promise { + return html( + ctx, + /* HTML */ ` My ToDo App

My ToDo App

- ${await TaskList(context)} ${NewTask()} + ${await TaskList(ctx.$context)} ${NewTask()} ` ); } router.get("/", Middlewares.extractContext(), async (ctx) => { - ctx.body = await MainView(ctx.$context); + ctx.body = MainView(ctx); }); export default router; diff --git a/src/back/routes/login.ts b/src/back/routes/login.ts index bd738f5..12095d4 100644 --- a/src/back/routes/login.ts +++ b/src/back/routes/login.ts @@ -6,7 +6,7 @@ import html from "../html"; const router = new Router(); router.get("/", Middlewares.extractContext(), async (ctx) => { - ctx.body = await html(ctx.$context, LoginForm()); + ctx.body = html(ctx, LoginForm()); }); router.post( @@ -27,8 +27,8 @@ router.post( ctx.redirect("/user"); } catch (e) { ctx.status = 422; - ctx.body = await html( - ctx.$context, + ctx.body = html( + ctx, LoginForm(ctx.$body.username as string, (e as Error).message) ); } diff --git a/src/back/routes/tasks.ts b/src/back/routes/tasks.ts index eb4b250..b655b0d 100644 --- a/src/back/routes/tasks.ts +++ b/src/back/routes/tasks.ts @@ -15,7 +15,7 @@ router.post( done: false, }) .save(ctx.$context); - ctx.body = await MainView(ctx.$context); + ctx.body = MainView(ctx); } ); @@ -25,7 +25,7 @@ router.delete("/:task_id", Middlewares.extractContext(), async (ctx) => { ctx.params.task_id ); await task.remove(ctx.$context); - ctx.body = await MainView(ctx.$context); + ctx.body = MainView(ctx); }); export default router;