-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
411 lines (371 loc) · 13.3 KB
/
index.js
File metadata and controls
411 lines (371 loc) · 13.3 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
var decodeFGHIURL = function(encodedString){
// Convert any %-encoded FGHI URL string to a real string:
// 1) replace '+' to a space
// 2) find %-encoded fragments, run them through decodeURIComponent()
var spaceDecodedString = encodedString.replace(/\+/g, ' ');
var SourceArray =
spaceDecodedString.split(/((?:%[0-9ABCDEFabcdef][0-9ABCDEFabcdef])+)/);
if (SourceArray.length === 1) {
// No encoded content detected :-)
return spaceDecodedString;
} else {
// Converting all URLencoded fragments
for (var ijk = 1; ijk < SourceArray.length; ijk += 2){
SourceArray[ijk] = decodeURIComponent(SourceArray[ijk]);
}
return SourceArray.join('');
}
};
// the following helper functions are used in object's context (this)!
var parseStation = function(){
// takes this.station and makes slices:
// this.stationZone, this.stationNet,
// this.stationNode, this.stationPoint, this.stationDomain
var Parsed = this.station.match(
/^(([0-9]+):)?([0-9]+)\/([0-9]+)(\.([0-9]+))?(@(.+))?$/
);
if (Parsed === null){
throw new Error(this.errors.INVALID_STATION);
} else {
this.stationZone = Parsed[2];
if (this.stationZone === undefined) this.stationZone = '';
this.stationNet = Parsed[3];
this.stationNode = Parsed[4];
this.stationPoint = Parsed[6];
if (this.stationPoint === undefined) this.stationPoint = '';
this.stationDomain = Parsed[8];
if (this.stationDomain === undefined) {
this.stationDomain = '';
} else this.stationDomain = decodeFGHIURL(this.stationDomain);
}
};
var noStation = function(){
// like parseStation() above, but there's no station
this.station = '';
this.stationZone = '';
this.stationNet = '';
this.stationNode = '';
this.stationPoint = '';
this.stationDomain = '';
};
var parseObjectPath = function(){
// takes this.objectPath string
// makes this.objectPathParts array
this.objectPathParts = [];
var PathBuf = this.objectPath;
while (PathBuf.length > 0){
// next step: getting another slashed slice
var SlashPos = PathBuf.indexOf('/');
if (SlashPos < 0){ // No more slashes!
this.objectPathParts.push(decodeFGHIURL(PathBuf));
PathBuf = '';
} else if (SlashPos === 0){
throw new Error(this.errors.DOUBLE_SLASH);
} else if( SlashPos === (PathBuf.length - 1) ){
// Trailing slash. IMPORTANT! Must not be ignored.
this.objectPathParts.push(decodeFGHIURL(PathBuf.slice(0, -1)));
this.objectPathParts.push('/');
PathBuf = '';
} else { // There are more slices yet.
this.objectPathParts.push(
decodeFGHIURL(PathBuf.slice(0, SlashPos))
);
PathBuf = PathBuf.slice(SlashPos + 1);
}
} // PathBuf is completely processed when while(){} ends
};
var noObjectPath = function(){
// like parseObjectPath() above, but there's no object-path
this.objectPath = '';
this.objectPathParts = [];
};
var parseFundamentalSections = function(pfsString){
// takes a string containing the URL
// makes: this.scheme,
// this.schemeSpecificPart,
// this.requiredPart, this.optionalPart
var separatorMatch = pfsString.match(/:(\/{2})?/);
// NB: the above regexp is greedy ^^^^^^^^
if( separatorMatch === null ){
throw new Error(this.errors.NO_SEPARATOR); // URL is invalid!
}
var separatorPosition = pfsString.indexOf(separatorMatch[0]);
var separatorMatchLen = separatorMatch[0].length;
this.scheme = pfsString.slice(0, separatorPosition);
this.scheme = this.scheme.toLowerCase(); // force ignore scheme case
this.schemeSpecificPart = pfsString.slice(
separatorPosition + separatorMatchLen
);
var questionPos = this.schemeSpecificPart.indexOf('?');
if (questionPos < 0) { // optional part is empty!
this.optionalPart = '';
this.requiredPart = this.schemeSpecificPart;
return;
}
this.requiredPart = this.schemeSpecificPart.slice(0, questionPos);
this.optionalPart = this.schemeSpecificPart.slice(questionPos + 1);
};
var parseOptionalPart = function(){
// takes this.optionalPart
// makes this.optionalParams array
this.optionalParams = [];
if (this.optionalPart === ''){ // no optional params!
return;
}
// parsing optional params...
var optionalBuf = this.optionalPart;
// if the optional parts ends with ampersand (&), kill it:
if( optionalBuf.charAt(optionalBuf.length-1) === '&' ){
if (
optionalBuf.length > 1 &&
optionalBuf.charAt(optionalBuf.length-2) === '&'
){
throw new Error(this.errors.EMPTY_OPTIONAL_PAIR);
}
optionalBuf = optionalBuf.slice(0, -1);
}
while (optionalBuf.length > 0){
// each step: getting the next param name+value pair
var ampersandPos = optionalBuf.indexOf('&');
var ParamValuePair = '';
if( ampersandPos === 0 ){
// Mistake: nothing precedes an ampersand,
// or unexpected double ampersand (&&) is encountered!
throw new Error(this.errors.EMPTY_OPTIONAL_PAIR);
} else if( ampersandPos < 0 ){
// No more ampersands!
ParamValuePair = optionalBuf;
optionalBuf = '';
} else {
// grab the first remaining slice:
ParamValuePair = optionalBuf.slice(0, ampersandPos);
optionalBuf = optionalBuf.slice(ampersandPos + 1);
}
// optionalBuf decremented by the slice;
// and now we have our next ParamValuePair; let's parse it!
var equalsPos = ParamValuePair.indexOf('=');
var ParamValueObject = {};
if( equalsPos === 0 ){
// Mistake: empty parameter name!
throw new Error(this.errors.EMPTY_OPTIONAL_NAME);
} else if( equalsPos < 0 ){
// The equals sign is absent!
// The ParamValuePair contains just the name
ParamValueObject.name = decodeFGHIURL(ParamValuePair);
ParamValueObject.value = '';
} else {
// grab the slices:
ParamValueObject.name =
decodeFGHIURL(ParamValuePair.slice(0, equalsPos));
ParamValueObject.value =
decodeFGHIURL(ParamValuePair.slice(equalsPos + 1));
}
// ParamValueObject is formed
this.optionalParams.push(ParamValueObject);
} // optionalBuf is processed
};
var decodeEchoNames = function(stringEchoNames){
if( stringEchoNames.length < 1 ){
return [];
}
return stringEchoNames.split(/\+|%20/).map(function(atEchotag){
if( atEchotag.length < 1 ){
throw new Error(this.errors.EMPTY_FQAN);
}
return atEchotag.split('@').map(function(echonamePart){
if( echonamePart.length < 1 ){
throw new Error(this.errors.EMPTY_FQAN_PART);
}
return decodeFGHIURL(echonamePart);
});
});
};
var netmailRequiredPart = function(){
// process this.requiredPart for 'netmail:' scheme
// it contains this.station (with subsections)
this.request = '';
noObjectPath.call(this);
this.echoNames = [];
this.station = this.requiredPart;
parseStation.call(this);
};
var areafixOrEchomailRequiredPart = function(){
// process this.requiredPart for 'areafix:' or 'echomail:' scheme
// it contains this.echoName
this.request = '';
noObjectPath.call(this);
noStation.call(this);
this.echoNames = decodeEchoNames.call(this, this.requiredPart);
};
var areaOrFechoRequiredPart = function(){
// process this.requiredPart for 'area://' or 'fecho://' scheme
// it contains this.echoNames and this.objectPath
this.request = '';
noStation.call(this);
var slashPos = this.requiredPart.indexOf('/');
if (slashPos < 0){
// No object-path!
noObjectPath.call(this);
this.echoNames = decodeEchoNames.call(this, this.requiredPart);
} else if (slashPos === 0){
throw new Error(this.errors.EMPTY_AREA_NAME);
} else {
this.echoNames =
decodeEchoNames.call(this, this.requiredPart.slice(0, slashPos));
this.objectPath = this.requiredPart.slice(slashPos + 1);
parseObjectPath.call(this);
}
};
var faqservRequiredPart = function(){
// process this.requiredPart for 'faqserv://' scheme
// it contains this.station and this.objectPath
this.echoNames = [];
var FreqBuf = '';
var firstSlashPos = this.requiredPart.indexOf('/');
if( firstSlashPos <= 0 ) throw new Error(this.errors.INVALID_STATION);
var secondSlashPos = this.requiredPart.indexOf('/', firstSlashPos+1);
if (secondSlashPos < 0) { // no request, no object-path
this.request = '';
noObjectPath.call(this);
this.station = this.requiredPart;
parseStation.call(this);
return;
}
this.station = this.requiredPart.slice(0, secondSlashPos);
parseStation.call(this);
FreqBuf = this.requiredPart.slice(secondSlashPos + 1);
var thirdSlashPos = FreqBuf.indexOf('/');
if (thirdSlashPos < 0){ // no object-path
noObjectPath.call(this);
this.request = decodeFGHIURL(FreqBuf);
} else if (thirdSlashPos === 0){
// Empty requests are invalid in FGHI URL post-0.3 drafts
throw new Error(this.errors.EMPTY_FAQ_REQUEST);
} else {
this.request = decodeFGHIURL(FreqBuf.slice(0, thirdSlashPos));
this.objectPath = FreqBuf.slice(thirdSlashPos + 1);
parseObjectPath.call(this);
}
};
var freqRequiredPart = function(){
// process this.requiredPart for 'freq://' scheme
// it contains this.station and this.objectPath
this.request = '';
this.echoNames = [];
var firstSlashPos = this.requiredPart.indexOf('/');
if( firstSlashPos <= 0 ) throw new Error(this.errors.INVALID_STATION);
var secondSlashPos = this.requiredPart.indexOf('/', firstSlashPos+1);
if (secondSlashPos < 0) { // no object-path
noObjectPath.call(this);
this.station = this.requiredPart;
parseStation.call(this);
return;
}
this.station = this.requiredPart.slice(0, secondSlashPos);
parseStation.call(this);
this.objectPath = this.requiredPart.slice(secondSlashPos+1);
parseObjectPath.call(this);
};
var parseRequiredPart = function(){
// Now we start parsing requiredPart.
// This is going to be very scheme-specific.
// Our goal is to fill one (or more) of the following:
// *) station (stationZone, stationNet, stationNode, stationPoint,
// stationDomain)
// *) echoNames
// *) objectPath (and the objectPathParts[] array)
// *) request (for faqserv://)
switch(this.scheme){ // in order of appearance in the FGHI URL standard:
case 'netmail':
netmailRequiredPart.call(this);
break;
case 'areafix':
case 'echomail':
areafixOrEchomailRequiredPart.call(this);
break;
case 'area':
case 'fecho':
areaOrFechoRequiredPart.call(this);
break;
case 'faqserv':
faqservRequiredPart.call(this);
break;
case 'freq':
freqRequiredPart.call(this);
break;
default: throw new Error(this.errors.UNKNOWN_SCHEME);
}
};
// The FGHI URL object's constructor
// takes a string
function FidonetURL(initialString){
if(!( this instanceof FidonetURL )){
return new FidonetURL(initialString);
}
parseFundamentalSections.call(this, initialString);
parseOptionalPart.call(this);
parseRequiredPart.call(this);
}
FidonetURL.prototype.hasFilters = function(){
if( this.scheme !== 'area' ) return false;
var arrFilters = [
'msgid',
'time',
'from',
'twit',
'find',
'subj',
'findsb',
'to',
'sender',
'geomark',
'geofrom',
'tag',
'ttop'
];
for( var i = 0; i < this.optionalParams.length; i++ ){
if( arrFilters.indexOf(this.optionalParams[i].name) >= 0 ){
return true;
}
}
return false;
};
FidonetURL.prototype.getView = function(supportedViews){
if( typeof supportedViews === 'string' && supportedViews.length > 0 ){
supportedViews = supportedViews.split(/\s+/);
}
if( !Array.isArray(supportedViews) ) return '';
if( supportedViews.length < 1 ) return '';
var viewLists = this.optionalParams.filter(function(optionalParam){
return optionalParam.name === 'view' && optionalParam.value.length > 0;
}).map(function(optionalParam){
return optionalParam.value.split(/\s+/).filter(function(value){
return supportedViews.indexOf(value) > -1;
});
}).filter(function(viewList){
return viewList.length > 0;
}).map(function(viewList){
return viewList[0];
});
if( viewLists.length === 0 ) return '';
return viewLists.reduce(function(prevView, currView){
if(supportedViews.indexOf(prevView) < supportedViews.indexOf(currView)){
return prevView;
} else {
return currView;
}
});
};
FidonetURL.prototype.errors = {
NO_SEPARATOR : 'No :// separator in URL!',
EMPTY_OPTIONAL_NAME : 'An optional parameter name is empty!',
EMPTY_OPTIONAL_PAIR : 'Empty parameter=value pair detected!',
INVALID_STATION : 'Fidonet station address violates FSP-1004!',
EMPTY_AREA_NAME : 'Area name is empty!',
DOUBLE_SLASH : 'Unexpected double slash!',
UNKNOWN_SCHEME : 'Unknown Fidonet URL scheme!',
EMPTY_FAQ_REQUEST : 'FAQ request is empty!',
EMPTY_FQAN : 'A fully qualified area name is empty!',
EMPTY_FQAN_PART : 'A part of a fully qualified area name is empty!'
};
module.exports = FidonetURL;