-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
177 lines (139 loc) · 4.94 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
var through = require('through2')
var parsePrefix = require('irc-prefix-parser')
var iso8601 = require('iso8601-convert')
var parse = function(data) {
var message = {
raw: data,
tags: {},
prefix: null,
command: null,
params: []
}
// position and nextspace are used by the parser as a reference.
var position = 0
var nextspace = 0
// The first thing we check for is IRCv3.2 message tags.
// http://ircv3.atheme.org/specification/message-tags-3.2
if (data.charCodeAt(0) === 64) {
var nextspace = data.indexOf(' ')
if (nextspace === -1) {
// Malformed IRC message.
return null
}
// Tags are split by a semi colon.
var rawTags = data.slice(1, nextspace).split(';')
for (var i = 0; i < rawTags.length; i++) {
// Tags delimited by an equals sign are key=value tags.
// If there's no equals, we assign the tag a value of true.
var tag = rawTags[i]
var pair = tag.split('=')
message.tags[pair[0]] = pair[1] || true
}
position = nextspace + 1
}
// Skip any trailing whitespace.
while (data.charCodeAt(position) === 32) {
position++
}
// Extract the message's prefix if present. Prefixes are prepended
// with a colon.
if (data.charCodeAt(position) === 58) {
nextspace = data.indexOf(' ', position)
// If there's nothing after the prefix, deem this message to be
// malformed.
if (nextspace === -1) {
// Malformed IRC message.
return null
}
message.prefix = data.slice(position + 1, nextspace)
position = nextspace + 1
// Skip any trailing whitespace.
while (data.charCodeAt(position) === 32) {
position++
}
}
nextspace = data.indexOf(' ', position)
// If there's no more whitespace left, extract everything from the
// current position to the end of the string as the command.
if (nextspace === -1) {
if (data.length > position) {
message.command = data.slice(position)
return message
}
return null
}
// Else, the command is the current position up to the next space. After
// that, we expect some parameters.
message.command = data.slice(position, nextspace)
position = nextspace + 1
// Skip any trailing whitespace.
while (data.charCodeAt(position) === 32) {
position++
}
while (position < data.length) {
nextspace = data.indexOf(' ', position)
// If the character is a colon, we've got a trailing parameter.
// At this point, there are no extra params, so we push everything
// from after the colon to the end of the string, to the params array
// and break out of the loop.
if (data.charCodeAt(position) === 58) {
message.params.push(data.slice(position + 1))
break
}
// If we still have some whitespace...
if (nextspace !== -1) {
// Push whatever's between the current position and the next
// space to the params array.
message.params.push(data.slice(position, nextspace))
position = nextspace + 1
// Skip any trailing whitespace and continue looping.
while (data.charCodeAt(position) === 32) {
position++
}
continue
}
// If we don't have any more whitespace and the param isn't trailing,
// push everything remaining to the params array.
if (nextspace === -1) {
message.params.push(data.slice(position))
break
}
}
return message
}
var createStream = function(options) {
var options = options || {}
var convertTimestamps = options.convertTimestamps || false
var shouldParsePrefix = options.parsePrefix || false
var buffer = ''
var stream = through.obj(function(chunk, encoding, done) {
buffer += chunk.toString()
messages = buffer.split(/\r\n/)
buffer = messages.pop()
for (var i = 0; i < messages.length; i++) {
var message = messages[i]
var parsed = parse(message)
if (parsed === null) {
this.emit('error', new Error('Invalid IRC message'))
continue
}
// support for IRCv3.2 server-time spec
var timestamp = parsed.tags.time
if (timestamp && convertTimestamps) {
var converted = iso8601.toDate(timestamp)
parsed.tags.time = (converted) ? converted : new Date(NaN)
}
//
if (shouldParsePrefix) {
parsed.prefix = parsePrefix(parsed.prefix)
}
this.push(parsed)
}
done()
})
return stream
}
module.exports = {
parse: parse,
createStream: createStream
}