Skip to content

Commit c849d93

Browse files
authored
replace hexoid with equivalent functionality (#441)
1 parent efe8448 commit c849d93

File tree

5 files changed

+100
-5
lines changed

5 files changed

+100
-5
lines changed

benchmark/generateId.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict'
2+
3+
const { Suite } = require('benchmark')
4+
const { generateId } = require('../lib/generateId')
5+
6+
const suite = new Suite()
7+
8+
suite.add('id', generateId)
9+
10+
suite
11+
.on('cycle', function (event) {
12+
console.log(String(event.target))
13+
})
14+
.on('complete', function () {
15+
console.log('Fastest is ' + this.filter('fastest').map('name'))
16+
console.log('Slowest is ' + this.filter('slowest').map('name'))
17+
})
18+
.run()

index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const eos = require('end-of-stream')
77
const { createWriteStream } = require('fs')
88
const { unlink } = require('fs').promises
99
const path = require('path')
10-
const hexoid = require('hexoid')
10+
const { generateId } = require('./lib/generateId')
1111
const util = require('util')
1212
const createError = require('@fastify/error')
1313
const sendToWormhole = require('stream-wormhole')
@@ -222,8 +222,6 @@ function fastifyMultipart (fastify, options, done) {
222222
await request.cleanRequestFiles()
223223
})
224224

225-
const toID = hexoid()
226-
227225
function isMultipart () {
228226
return this.raw[kMultipart] || false
229227
}
@@ -538,7 +536,7 @@ function fastifyMultipart (fastify, options, done) {
538536
const tmpdir = (options && options.tmpdir) || os.tmpdir()
539537
this.tmpUploads = []
540538
for await (const file of files) {
541-
const filepath = path.join(tmpdir, toID() + path.extname(file.filename))
539+
const filepath = path.join(tmpdir, generateId() + path.extname(file.filename))
542540
const target = createWriteStream(filepath)
543541
try {
544542
await pump(file.file, target)

lib/generateId.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict'
2+
3+
const HEX = [
4+
'00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '0a', '0b', '0c', '0d', '0e', '0f',
5+
'10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '1a', '1b', '1c', '1d', '1e', '1f',
6+
'20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '2a', '2b', '2c', '2d', '2e', '2f',
7+
'30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '3a', '3b', '3c', '3d', '3e', '3f',
8+
'40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '4a', '4b', '4c', '4d', '4e', '4f',
9+
'50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '5a', '5b', '5c', '5d', '5e', '5f',
10+
'60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '6a', '6b', '6c', '6d', '6e', '6f',
11+
'70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '7a', '7b', '7c', '7d', '7e', '7f',
12+
'80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '8a', '8b', '8c', '8d', '8e', '8f',
13+
'90', '91', '92', '93', '94', '95', '96', '97', '98', '99', '9a', '9b', '9c', '9d', '9e', '9f',
14+
'a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'aa', 'ab', 'ac', 'ad', 'ae', 'af',
15+
'b0', 'b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8', 'b9', 'ba', 'bb', 'bc', 'bd', 'be', 'bf',
16+
'c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9', 'ca', 'cb', 'cc', 'cd', 'ce', 'cf',
17+
'd0', 'd1', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8', 'd9', 'da', 'db', 'dc', 'dd', 'de', 'df',
18+
'e0', 'e1', 'e2', 'e3', 'e4', 'e5', 'e6', 'e7', 'e8', 'e9', 'ea', 'eb', 'ec', 'ed', 'ee', 'ef',
19+
'f0', 'f1', 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9', 'fa', 'fb', 'fc', 'fd', 'fe', 'ff'
20+
]
21+
22+
const random = Math.random
23+
24+
function seed () {
25+
return (
26+
HEX[0xff * random() | 0] +
27+
HEX[0xff * random() | 0] +
28+
HEX[0xff * random() | 0] +
29+
HEX[0xff * random() | 0] +
30+
HEX[0xff * random() | 0] +
31+
HEX[0xff * random() | 0] +
32+
HEX[0xff * random() | 0]
33+
)
34+
}
35+
36+
module.exports.generateId = (function generateIdFn () {
37+
let num = 0
38+
let str = seed()
39+
return function generateId () {
40+
return (num === 255) // eslint-disable-line no-return-assign
41+
? (str = seed()) + HEX[num = 0]
42+
: str + HEX[++num]
43+
}
44+
})()

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
"@fastify/swagger-ui": "^1.8.0",
1313
"end-of-stream": "^1.4.4",
1414
"fastify-plugin": "^4.0.0",
15-
"hexoid": "^1.0.0",
1615
"secure-json-parse": "^2.4.0",
1716
"stream-wormhole": "^1.1.0"
1817
},
@@ -21,6 +20,7 @@
2120
"@types/node": "^20.1.0",
2221
"@typescript-eslint/eslint-plugin": "^5.30.7",
2322
"@typescript-eslint/parser": "^5.30.7",
23+
"benchmark": "^2.1.4",
2424
"climem": "^1.0.3",
2525
"concat-stream": "^2.0.0",
2626
"eslint": "^8.20.0",

test/generate-id.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict'
2+
3+
const { test } = require('tap')
4+
const { generateId } = require('../lib/generateId')
5+
6+
test('returns', t => {
7+
t.plan(3)
8+
t.type(generateId, 'function', 'is a function')
9+
t.type(generateId(), 'string', '~> returns a string')
10+
t.equal(generateId().length, 16, '~> has 16 characters (default)')
11+
})
12+
13+
test('length', t => {
14+
const iterations = 1e3
15+
t.plan(iterations)
16+
17+
let i = 0
18+
let tmp = ''
19+
for (; i < iterations; ++i) {
20+
tmp = generateId()
21+
t.equal(tmp.length, 16, `"${tmp}" is 16 characters`)
22+
}
23+
})
24+
25+
test('unique /1', t => {
26+
t.plan(1)
27+
t.not(generateId(), generateId(), '~> single')
28+
})
29+
30+
test('unique /2', t => {
31+
t.plan(1)
32+
const items = new Set()
33+
for (let i = 5e6; i--;) items.add(generateId())
34+
t.equal(items.size, 5e6, '~> 5,000,000 unique ids')
35+
})

0 commit comments

Comments
 (0)