“This is the 16th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

This article is part of the KOA dependency series, the first few of which can also be viewed on the site:

  • Koa depends on the library parseURL
  • Koa relies on the libraries Type-is and Content-Disposition
  • The Koa dependent libraries accept, Content-Type, and cache-Content-Type
  • Koa relies on the libraries encodeURL and escape- HTML

Statuses are used to process status code information. They can convert status codes to text or text to status codes. At the same time, they internally encapsulate the judgment ability of common state logic. This library is used in many places in KOA, but the function is very simple, here directly look at the source code.

Statuses export the status function by default. This function has two functions: the incoming string converts the string into a status code, and the incoming status code converts the string into a string. The mapping between the string and the status code is located in codes.json file.

Codes. json is not defined manually and can be generated automatically using the update command below package.json. The fetch and build commands are executed in the update, and the related scripts are located in the scripts directory.

Fetch contains four scripts, including fetch-apache, fetch-iana, fetch-nginx and fetch-node, corresponding to four different data sources:

  • The IANA Status Code Registry
  • The Node.js project
  • The NGINX project
  • The Apache HTTP Server project

These four places have the standard definition of Status code, and Statuses will get four data sources of the four places. All data sources will be merged, and finally get a large and complete codes.json file.

In these four FETCH scripts, Apache, Nginx and Node directly pull the corresponding status code logic implementation source code, and use regular expressions to match the status code and status string in the source code. Iana directly downloads a CSV resource, uses THE CSV parsing library CSV-Parse for data parsing, and finally generates four JSON files in the SRC directory.

The build script is also very simple. The previous four JSON files have been generated, and this step will be read and stored in the Codes object at one time. Because of the sequence of reading, the definition of the state string behind the same status code will override the previous one. Apache. Finally, write codes to codes.json file, and you can define the data using the status code in index.js.

'use strict'

var path = require('path')
var write = require('./lib/write')

// all codes
var codes = {}

// initialize with all IANA codes
addData(codes, require('.. /src/iana.json'))

// add the codes from node
addData(codes, require('.. /src/node.json'))

// add the codes from nginx
addData(codes, require('.. /src/nginx.json'))

// add the codes from apache
addData(codes, require('.. /src/apache.json'))

// write the JSON object
write(path.join(__dirname, '.. /codes.json'), codes)

function addData (db, obj) {
  Object.keys(obj).forEach(function (key) {
    db[key] = db[key] || obj[key]
  })
}
Copy the code

In addition to the default exports, the Statuses library also exports raw data source information in a variety of object and array formats. In addition, StatUSES provide corresponding export sets for special status codes such as redirection, null and retry.

// status codes for redirects
status.redirect = {
  300: true.301: true.302: true.303: true.305: true.307: true.308: true
}

// status codes for empty bodies
status.empty = {
  204: true.205: true.304: true
}

// status codes for when you should retry the request
status.retry = {
  502: true.503: true.504: true
}
Copy the code

This is the statuses library. Koa uses transitions between status codes and strings, and uses redirect and empty to make state judgments. The Statuses library is short, so this article is minimal. You can read codes.json to get a more detailed look at the HTTP status codes of interest.