koa-etag源码解析

介绍

Etag support for Koa responses using etag.

意为使用etag库,设置koa响应头Etag

demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const conditional = require('koa-conditional-get');
const etag = require('koa-etag');
const Koa = require('koa');
const app = new Koa();

// etag works together with conditional-get
app.use(conditional());
app.use(etag());

app.use(function (ctx) {
ctx.body = 'Hello World';
});

app.listen(3000);

console.log('listening on port 3000');

源码 koa-etag 4.0.0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

'use strict'
const calculate = require('etag')
const Stream = require('stream')
const promisify = require('util').promisify
const fs = require('fs')

const stat = promisify(fs.stat)

/**
* Expose `etag`.
* Add ETag header field.
* @param {object} [options] see https://github.com/jshttp/etag#options
* @param {boolean} [options.weak]
* @return {Function}
* @api public
*/
module.exports = function etag (options) {
return async function etag (ctx, next) {
// 先进行后续的中间件逻辑,之后再处理etag
await next()
const entity = await getResponseEntity(ctx)
setEtag(ctx, entity, options)
}
}

async function getResponseEntity (ctx) {
// 如果没有body,或已存在etag,则return跳出
const body = ctx.body
if (!body || ctx.response.get('etag')) return

// 获取http状态码 第一位数字
const status = ctx.status / 100 | 0

// 2xx 除2xx的非正常情况都跳出
if (status !== 2) return

// 1、如果为流,返回fs.stat调用后的文件信息对象
// 2、如果为字符串或buffer则直接返回
// 3、否则转为字符串返回
if (body instanceof Stream) {
if (!body.path) return
return await stat(body.path)
} else if ((typeof body === 'string') || Buffer.isBuffer(body)) {
return body
} else {
return JSON.stringify(body)
}
}

function setEtag (ctx, entity, options) {
// 如果body,即响应体不存在则不设置tag
if (!entity) return

// 调用etag能力并设置到响应头etag中
ctx.response.etag = calculate(entity, options)
}

参考链接:

打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!

给阿姨来一杯卡普基诺~

支付宝
微信