-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·151 lines (123 loc) · 3.31 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
'use strict'
class Bronze {
constructor (options = {}) {
options = Object.assign({}, options)
if (Number.isSafeInteger(options.sequence) === true) {
this.sequence = options.sequence
} else {
this.sequence = 0
}
if (Number.isSafeInteger(options.pid) === true) {
this.pid = options.pid
} else {
this.pid = process.pid
}
if (typeof options.name === 'string') {
this.nameRaw = options.name
} else if (typeof process === 'object' && typeof process.env.HOSTNAME === 'string') {
this.nameRaw = process.env.HOSTNAME
} else {
this.nameRaw = this.constructor.defaultName
}
this.name = this.nameRaw.replace(/[\\/]/g, '_')
if (this.constructor.specs[options.spec] === true) {
this.spec = options.spec
} else {
this.spec = this.constructor.defaultSpec
}
}
nextSequence () {
this.sequence++
if (Number.isSafeInteger(this.sequence) === false) {
this.sequence = 0
}
}
generate (options = {}) {
const results = {
timestamp: Date.now(),
sequence: this.sequence,
pid: this.pid,
name: this.name,
spec: this.spec
}
options = Object.assign({}, options)
if (this.constructor.specs[results.spec] !== true) {
throw new Error('Unknown spec')
}
if (Number.isSafeInteger(results.pid) === false) {
throw new Error('Process ID is not a safe integer')
}
this.nextSequence()
if (options.json === true) {
return results
} else {
switch (results.spec) {
case '1a':
return `${results.timestamp}-${results.sequence}-${results.pid}-${results.name}-${results.spec}`
case '1b':
return `${results.name}-${results.pid}-${results.timestamp}-${results.sequence}-${results.spec}`
}
}
}
static get defaultName () {
return 'localhost'
}
static get defaultSpec () {
return '1a'
}
static get specs () {
return {
'1a': true,
'1b': true
}
}
static parse (id = '') {
const results = {
valid: false
}
if (typeof id !== 'string') {
return results
}
const pieces = id.split('-')
if (pieces.length < 5) {
return results
}
const spec = pieces[pieces.length - 1]
let timestamp = NaN
let sequence = NaN
let pid = NaN
let name = ''
switch (spec) {
case '1a':
timestamp = Number(pieces[0])
sequence = Number(pieces[1])
pid = Number(pieces[2])
name = pieces.slice(3, pieces.length - 1).join('-')
if (/[\\/]/.test(name)) {
return results
}
break
case '1b':
sequence = Number(pieces[pieces.length - 2])
timestamp = Number(pieces[pieces.length - 3])
pid = Number(pieces[pieces.length - 4])
name = pieces.slice(0, -4).join('-')
if (/[\\/]/.test(name)) {
return results
}
break
default:
return results
}
if (Number.isSafeInteger(timestamp) === true && Number.isSafeInteger(sequence) === true && Number.isSafeInteger(pid) === true) {
results.valid = true
results.timestamp = timestamp
results.sequence = sequence
results.pid = pid
results.name = name
results.spec = spec
}
return results
}
}
module.exports = Bronze