-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
151 lines (122 loc) · 3.46 KB
/
index.js
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2021 SmugMug, Inc.
// Licensed under the terms of the MIT license. Please see LICENSE file in the project root for terms.
import { createHash } from 'crypto';
import { parse } from 'url';
/**
* The default digest algorithm to use
* @see https://nodejs.org/dist/latest-v16.x/docs/api/crypto.html#cryptocreatehashalgorithm-options
*/
const DEFAULT_ALGORITHM = 'md5';
/**
* The default digest encoding to return
* @type {BufferEncoding}
*/
const DEFAULT_ENCODING = 'hex';
/**
* Returns a new `crypto.Hash` stream using the specified algorithm and encoding
* (defaults to "md5" and "hex"). You can pipe your `http.IncomingMessage` in
* and get a hash back.
*
* @param {string} algorithm
* @param {BufferEncoding} encoding
* @returns {import('crypto').Hash}
* @example
*
* import hash from 'incoming-message-hash'
* import { createServer } from 'http'
*
* createServer((req, res) => {
* req.pipe(hash()).pipe(res)
* })
*/
export default function createStream(algorithm = DEFAULT_ALGORITHM, encoding = DEFAULT_ENCODING) {
const hash = createHash(algorithm);
hash.on('pipe', function (req) {
updateHash(hash, req);
});
hash.setEncoding(encoding);
return hash;
}
// backwards-compatibility
createStream.sync = sync;
createStream.promise = promise;
/**
* Synchronous version of `hash()` that accepts an http.IncomingMessage and its
* body and returns the hash. You must buffer up the request body yourself
* if you wish to use this method.
* @param {import('http').IncomingMessage} req
* @param {string|Buffer} body
* @param {string} algorithm
* @param {BufferEncoding} encoding
* @async
* @returns {string}
* @example
*
* import { promise } from 'incoming-message-hash'
* import { createServer } from 'http'
*
* createServer(async function (req, res) {
* let body = ''
*
* req.on('data', chunk => body += String(chunk))
*
* req.on('end', () => {
* res.end(sync(req, body))
* })
* })
*/
export function sync(req, body, algorithm = DEFAULT_ALGORITHM, encoding = DEFAULT_ENCODING) {
const hash = createHash(algorithm);
updateHash(hash, req);
hash.write(body);
return hash.digest(encoding);
}
/**
* Asynchronous version of `hash()` that accepts an `http.IncomingMessage` and
* buffers the request body up for you.
*
* @param {import('http').IncomingMessage} req
* @param {string} algorithm
* @param {BufferEncoding} encoding
* @async
* @returns {Promise<string>}
* @example
*
* import { promise } from 'incoming-message-hash'
* import { createServer } from 'http'
*
* createServer(async (req, res) => {
* res.end(await promise(req))
* })
*/
export function promise(req, algorithm = DEFAULT_ALGORITHM, encoding = DEFAULT_ENCODING) {
const hash = createHash(algorithm);
updateHash(hash, req);
return new Promise(function (resolve, reject) {
req.on('error', function (err) {
reject(err);
});
req.on('data', function (data) {
hash.write(data);
});
req.on('end', function () {
resolve(hash.digest(encoding));
});
});
}
function updateHash(hash, req) {
const parts = parse(req.url, true);
hash.update(req.httpVersion);
hash.update(req.method);
hash.update(parts.pathname);
hash.update(JSON.stringify(sort(parts.query)));
hash.update(JSON.stringify(sort(req.headers)));
hash.update(JSON.stringify(sort(req.trailers)));
}
function sort(obj) {
var ret = {};
Object.keys(obj).sort().forEach(function (key) {
ret[key] = obj[key];
});
return ret;
}