-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.js
More file actions
167 lines (130 loc) · 4.03 KB
/
main.js
File metadata and controls
167 lines (130 loc) · 4.03 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
(function() {
const HT = '\t';
const SP = ' ';
const CR = '\r';
const NF = '\n';
const SPACES = [SP, HT, CR, NF];
const SEPARATORS = [
'(', ')', '<', '>', '@',
',', ';', ':', '\\', '"',
'/', '[', ']', '?', '=',
'{', '}', SP, HT
];
function skipSpaces(value, pos) {
while (pos < value.length && SPACES.indexOf(value.charAt(pos)) >= 0) pos++;
return pos;
}
function readToken(value, pos) {
var start = pos;
while (pos < value.length && SEPARATORS.indexOf(value.charAt(pos)) == -1) {
pos++;
}
return value.substring(start, pos);
}
function readQuotedString(value, pos) {
var ch;
var start = pos;
pos++;
while (pos < value.length) {
ch = value.charAt(pos);
if (ch === '"') break;
if (ch === '\\') pos++;
pos++;
}
return value.substring(start, pos + 1);
}
function decodeQuotedString(value) {
value = value.substr(1, value.length - 2);
var start = 0, p;
var result = '';
while((p = value.indexOf('\\', start)) != -1) {
result += value.substring(start, p);
start = p + 2;
}
result += value.substring(start);
return result;
}
function readLinkParam(value, pos, link) {
var pname = readToken(value, pos);
pos = skipSpaces(value, pos + pname.length);
if (value.charAt(pos) !== '=')
throw new Error('Unexpected token: ' + pos);
pos++;
var isQuotedString = value.charAt(pos) === '"';
var pvalue;
if (isQuotedString) {
pvalue = readQuotedString(value, pos);
pos += pvalue.length;
pvalue = decodeQuotedString(pvalue);
} else {
pvalue = readToken(value, pos);
pos += pvalue.length;
if (pname == 'type') {
if (value.charAt(pos) !== '/')
throw new Error('Unexpected token: ' + pos);
pos++;
var subtype = readToken(value, pos);
pos += subtype.length;
pvalue += '/' + subtype;
}
}
link[pname] = pvalue;
return pos;
}
function readLink(value, pos, link) {
if (value.charAt(pos) !== '<')
throw new Error('Unexpected token: ' + pos);
var p = value.indexOf('>', pos);
if (p === -1) throw new Error('Unexpected token: ' + pos);
link.href = value.substring(pos + 1, p);
pos = skipSpaces(value, p + 1);
while(pos < value.length && value.charAt(pos) === ';') {
pos = skipSpaces(value, pos + 1);
pos = readLinkParam(value, pos, link);
pos = skipSpaces(value, pos);
}
return pos;
}
var httpLink = {};
/**
* Parse the given string.
* @param {String} value string as defined in http://www.w3.org/wiki/LinkHeader
* @return {Array} array of link objects
* @example '<http://example.com/TheBook/chapter2>; rel="previous"' -> [{href: 'http://example.com/TheBook/chapter2', rel: 'previous'}]
*/
httpLink.parse = function(value) {
var pos = 0;
var links = [];
var link;
while (pos < value.length && (pos === 0 || value.charAt(pos) === ',' && pos++)) {
link = {};
pos = skipSpaces(value, pos);
pos = readLink(value, pos, link);
links.push(link);
pos = skipSpaces(value, pos);
}
if (pos < value.length)
throw new Error('Unexpected token: ' + pos);
return links;
};
httpLink.stringify = function(array) {
return array.map(function(obj) {
var href = obj.href;
var attr = Object.keys(obj).filter(function(key) {
return key !== 'href';
}).map(function(key) {
return key + '=' + JSON.stringify(obj[key]);
});
return ['<' + obj.href + '>'].concat(attr).join('; ');
}).join(', ');
}
if (typeof define === 'function' && define.amd) { // RequireJS AMD
define('http-link', [], function () {
return httpLink;
});
} else if (typeof module === 'object' && module.exports) { // NodeJS, CommonJS
module.exports = httpLink;
} else { // browser <script>
this.httpLink = httpLink;
}
})();