-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathnode.js
202 lines (188 loc) · 6.45 KB
/
node.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
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
/*
* Copyright (c) 2017-2021 Digital Bazaar, Inc. All rights reserved.
*/
'use strict';
const https = require('https');
const {parseLinkHeader, buildHeaders} = require('../util');
const {LINK_HEADER_CONTEXT} = require('../constants');
const JsonLdError = require('../JsonLdError');
const RequestQueue = require('../RequestQueue');
const {prependBase} = require('../url');
const {httpClient} = require('@digitalbazaar/http-client');
/**
* Creates a built-in node document loader.
*
* @param options the options to use:
* [secure]: require all URLs to use HTTPS. (default: false)
* [strictSSL]: true to require SSL certificates to be valid,
* false not to. (default: true)
* [maxRedirects]: the maximum number of redirects to permit.
* (default: none)
* [headers]: an object (map) of headers which will be passed as
* request headers for the requested document. Accept is not
* allowed. (default: none).
* [httpAgent]: a Node.js `http.Agent` to use with 'http' requests.
* (default: none)
* [httpsAgent]: a Node.js `https.Agent` to use with 'https' requests.
* (default: An agent with rejectUnauthorized to the strictSSL
* value)
*
* @return the node document loader.
*/
module.exports = ({
secure,
strictSSL = true,
maxRedirects = -1,
headers = {},
httpAgent,
httpsAgent
} = {strictSSL: true, maxRedirects: -1, headers: {}}) => {
headers = buildHeaders(headers);
// if no default user-agent header, copy headers and set one
if(!('user-agent' in headers)) {
headers = Object.assign({}, headers, {
'user-agent': 'jsonld.js'
});
}
const http = require('http');
const queue = new RequestQueue();
return queue.wrapLoader(function(url) {
return loadDocument(url, []);
});
async function loadDocument(url, redirects) {
const isHttp = url.startsWith('http:');
const isHttps = url.startsWith('https:');
const isIpfs = url.startsWith('ipfs:');
if(!isHttp && !isHttps && !isIpfs) {
throw new JsonLdError(
'URL could not be dereferenced; only "http", "https", and "ipfs" URLs are ' +
'supported.',
'jsonld.InvalidUrl', {code: 'loading document failed', url});
}
if(secure && isHttp) {
throw new JsonLdError(
'URL could not be dereferenced; secure mode is enabled and ' +
'the URL\'s scheme is not "https".',
'jsonld.InvalidUrl', {code: 'loading document failed', url});
}
// TODO: disable cache until HTTP caching implemented
let doc = null;//cache.get(url);
if(doc !== null) {
return doc;
}
let alternate = null;
const requestUrl = !isIpfs ? url : 'https://ipfs.io/ipfs/' + url.split('ipfs://')[1];
const {res, body} = await _fetch({
url: requestUrl, headers, strictSSL, httpAgent, httpsAgent
});
doc = {contextUrl: null, documentUrl: url, document: body || null};
// handle error
const statusText = http.STATUS_CODES[res.status];
if(res.status >= 400) {
throw new JsonLdError(
`URL "${url}" could not be dereferenced: ${statusText}`,
'jsonld.InvalidUrl', {
code: 'loading document failed',
url,
httpStatusCode: res.status
});
}
const link = res.headers.get('link');
let location = res.headers.get('location');
const contentType = res.headers.get('content-type');
// handle Link Header
if(link && contentType !== 'application/ld+json') {
// only 1 related link header permitted
const linkHeaders = parseLinkHeader(link);
const linkedContext = linkHeaders[LINK_HEADER_CONTEXT];
if(Array.isArray(linkedContext)) {
throw new JsonLdError(
'URL could not be dereferenced, it has more than one associated ' +
'HTTP Link Header.',
'jsonld.InvalidUrl',
{code: 'multiple context link headers', url});
}
if(linkedContext) {
doc.contextUrl = linkedContext.target;
}
// "alternate" link header is a redirect
alternate = linkHeaders.alternate;
if(alternate &&
alternate.type == 'application/ld+json' &&
!(contentType || '')
.match(/^application\/(\w*\+)?json$/)) {
location = prependBase(url, alternate.target);
}
}
// handle redirect
if((alternate ||
res.status >= 300 && res.status < 400) && location) {
if(redirects.length === maxRedirects) {
throw new JsonLdError(
'URL could not be dereferenced; there were too many redirects.',
'jsonld.TooManyRedirects', {
code: 'loading document failed',
url,
httpStatusCode: res.status,
redirects
});
}
if(redirects.indexOf(url) !== -1) {
throw new JsonLdError(
'URL could not be dereferenced; infinite redirection was detected.',
'jsonld.InfiniteRedirectDetected', {
code: 'recursive context inclusion',
url,
httpStatusCode: res.status,
redirects
});
}
redirects.push(url);
// location can be relative, turn into full url
const nextUrl = new URL(location, url).href;
return loadDocument(nextUrl, redirects);
}
// cache for each redirected URL
redirects.push(url);
// TODO: disable cache until HTTP caching implemented
/*
for(let i = 0; i < redirects.length; ++i) {
cache.set(
redirects[i],
{contextUrl: null, documentUrl: redirects[i], document: body});
}
*/
return doc;
}
};
async function _fetch({url, headers, strictSSL, httpAgent, httpsAgent}) {
try {
const options = {
headers,
redirect: 'manual',
// ky specific to avoid redirects throwing
throwHttpErrors: false
};
const isHttps = url.startsWith('https:');
if(isHttps) {
options.agent =
httpsAgent || new https.Agent({rejectUnauthorized: strictSSL});
} else {
if(httpAgent) {
options.agent = httpAgent;
}
}
const res = await httpClient.get(url, options);
return {res, body: res.data};
} catch(e) {
// HTTP errors have a response in them
// ky considers redirects HTTP errors
if(e.response) {
return {res: e.response, body: null};
}
throw new JsonLdError(
'URL could not be dereferenced, an error occurred.',
'jsonld.LoadDocumentError',
{code: 'loading document failed', url, cause: e});
}
}