Support for promised middleware and readiness conditions

オープン
#7,606 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
15/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
活発
技術スタック
express, javascript, node.js
領域
api, backend

調査の方向性

The issue names no files: it is a design proposal, so the first step is a maintainer decision on the API shape (async listen versus a separate listenAsync) before any code is written. To scope it, read how the app is created and started in lib/express.js and lib/application.js, and how middleware is stored and invoked in lib/router/layer.js. Done means an accepted design plus tests covering a promised middleware and waitFor on both App and Router.

索引モデルが issue の本文から書いたものです。

説明

ideas

There are times when a service should wait for certain conditions to be in place before it starts accepting traffic. For instance: waiting for a database connection pool to be established.
Similarly, certain middleware may need to perform operations before it is ready to serve traffic. For example: a schema validator using openapi json schema files that needs to load the schemas before it is ready.

While modern esm can address this using top-level await, you either pay an ergonomics and readability penalty by having the middleware instantiated away from where it is used, or a performance penalty by awaiting and resolving each individually.
And this is completely unachievable using commonjs.

My proposal is as follows:

  1. App and Router gain a waitFor function that accepts Promises to be awaited on launch
  2. Any place that accepts middleware also accepts a promise of middleware
  3. Either App.listen becomes async, or App gains a separate listenAsync function
  4. Optionally, App and Router gain a ready function for code that might like to check readiness without booting the service

Example usage

import Genes from "../models/Genes.js";
import validate from "validateSchema";
import express from "express";

const router = new express.Router();

router.waitFor(Genes.ready());

router.route("/")
    .get(
         // validate returns a promise
         validate("genes.yml#/listParams", "query"),
         async (req, res) => {
               res.json(await new Genes().get(listParameters(res.query))
         }
    );

router.route("/:geneId")
    .put(
          validate("genes.yml#/input", "body"),
          async (req, res) => {
               res.json(await new Genes().update(Number(req.params.geneId), req.body));
          }
    )
主要言語
JavaScript
スター
69.7k
フォーク
25.7k
平均マージ
2日 9時間
マージ済み PR(30日)
4

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

expressjs/express のほかの issue

expressjs/express の issue をすべて見る

似ている issue

JavaScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。