-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdaap-parser.js
More file actions
168 lines (157 loc) · 4.23 KB
/
daap-parser.js
File metadata and controls
168 lines (157 loc) · 4.23 KB
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
const contentCodes = require('./contentcodes.json');
const CONTENT_TYPE = {
BYTE: 1,
SHORT: 3,
INT: 5,
LONG: 7,
STRING: 9,
DATE: 10,
VERSION: 11,
CONTAINER: 12,
};
const fnMap = {
[CONTENT_TYPE.CONTAINER]: 'container',
[CONTENT_TYPE.BYTE]: 'number',
[CONTENT_TYPE.SHORT]: 'number',
[CONTENT_TYPE.INT]: 'number',
[CONTENT_TYPE.LONG]: 'hex',
[CONTENT_TYPE.STRING]: 'string',
[CONTENT_TYPE.DATE]: 'date',
[CONTENT_TYPE.VERSION]: 'version',
};
const LENGTHS = {
[CONTENT_TYPE.BYTE]: 1,
[CONTENT_TYPE.SHORT]: 2,
[CONTENT_TYPE.INT]: 4,
[CONTENT_TYPE.DATE]: 4,
[CONTENT_TYPE.VERSION]: 4,
};
const parse = {
number(part) {
return part.readUIntBE(0, part.length);
},
string(part, encoding = 'utf8') {
return part.toString(encoding);
},
date(part) {
return new Date(parse.number(part) * 1000);
},
hex(part) {
return `0x${part.toString('hex')}`;
},
version(part) {
return [
parse.number(part.slice(0, 2)),
parse.number(part.slice(2, 3)),
parse.number(part.slice(3)),
].join('.');
},
container(part) {
const parsed = {};
let subpart = part;
while (subpart.length > 8) {
const code = parse.string(subpart.slice(0, 4), 'latin1');
const end = parse.number(subpart.slice(4, 8)) + 8;
const content = parse.tag(code, subpart.slice(8, end));
if (!parsed[code]) {
parsed[code] = content;
if (contentCodes[code] && contentCodes[code].isArray) {
parsed[code] = [parsed[code]];
}
} else {
if (!Array.isArray(parsed[code])) {
parsed[code] = [parsed[code]];
if (!contentCodes[code] || !contentCodes[code].isArray) {
console.warn(`contentParser: ${code} is array!`);
}
}
parsed[code].push(content);
}
subpart = subpart.slice(end);
}
return parsed;
},
tag(code, part) {
const type = contentCodes[code] ? contentCodes[code].type : CONTENT_TYPE.LONG;
const encoding = type === CONTENT_TYPE.STRING
&& contentCodes[code]
&& contentCodes[code].encoding;
return parse[fnMap[type] || 'hex'](part, encoding);
},
translate(data, short) {
let parsed;
if (Array.isArray(data)) {
parsed = data.map(item => parse.translate(item, short));
} else if (Object.prototype.toString.call(data) === '[object Object]') {
parsed = Object.keys(data).reduce((memo, code) => {
const value = parse.translate(data[code], short);
const { name } = contentCodes[code] || {};
const preKey = short || !name ? `[${code}]` : '';
memo[[preKey, name].filter(Boolean).join(' ')] = value;
return memo;
}, {});
} else {
parsed = data;
}
return parsed;
},
};
const encode = {
parse(obj) {
return Object.keys(obj).reduce((buffer, code) => {
const codeInfo = contentCodes[code] || { type: CONTENT_TYPE.LONG };
let part;
if (codeInfo.type === CONTENT_TYPE.CONTAINER) {
if (codeInfo.isArray) {
part = Buffer.concat(obj[code].map(
child => encode.wrapTag(code, encode.parse(child))
));
} else {
part = encode.wrapTag(code, encode.parse(obj[code]));
}
} else if (codeInfo.isArray) {
part = Buffer.concat(obj[code].map(
child => encode.tag(code, child, codeInfo.type)
));
} else {
part = encode.tag(code, obj[code], codeInfo.type, codeInfo.encoding);
}
return Buffer.concat([buffer, part]);
}, Buffer.alloc(0));
},
wrapTag(code, data) {
return Buffer.concat([encode.string(code, 'latin1'), encode.number(data.length, 4), data]);
},
tag(code, data, type, encoding) {
const arg = type === CONTENT_TYPE.STRING ? encoding : LENGTHS[type];
const fn = fnMap[type || CONTENT_TYPE.LONG];
const decoded = encode[fn](data, arg);
return this.wrapTag(code, decoded);
},
number(data, length) {
const buffer = Buffer.alloc(length);
buffer.writeUIntBE(data, 0, length);
return buffer;
},
string(data, encoding = 'utf8') {
return Buffer.from(data, encoding);
},
version(data) {
const ver = data.split('.');
return Buffer.concat([2, 1, 1].map(
(length, index) => encode.number(ver[index], length)
));
},
date(data, length) {
return encode.number(new Date(data) / 1000, length);
},
hex(data) {
return Buffer.from(data.startsWith('0x') ? data.substr(2) : data, 'hex');
},
};
module.exports = {
parse: parse.container,
parseTag: parse.tag,
encode: encode.parse,
translate: parse.translate,
};