- const hash = crypto.createHash(algorithm);
- hash.update(content);
- return hash.digest('hex');
- }
-
-
- /**
- * A request skeleton config.
- * @param {String} method
- * @param {String} requestUrl
- * @param {String} body
- * @param {Object} params
- */
- static _axiosConfig(method, requestUrl, body, params = {}, headers = {}) {
- const urlObj = new URL(requestUrl);
- const config = {
- method,
- url: `${urlObj.origin}${urlObj.pathname}`,
- params: urlObj.searchParams,
- headers,
- ...(body && { data: body }),
- // Setting this does not appear to be enough to keep axios from parsing JSON response into object
- responseType: 'text',
- // So force the matter by eliding all response transformations
- transformResponse: [ (res) => res ],
- };
- Object.entries(params).map(([k, v]) => config.params.set(k, v));
- return config;
- }
-
-
- /**
- * Create request config for verifying an intent.
- * @param {URL} requestUrl
- * @param {String} topicUrl
- * @param {String} mode
- * @param {Integer} leaseSeconds
- * @param {String} challenge
- */
- static _intentVerifyAxiosConfig(requestUrl, topicUrl, mode, leaseSeconds, challenge) {
- // Explicitly convert leaseSeconds to string, due to some DB backends. (Looking at you, sqlite..)
- leaseSeconds = leaseSeconds.toString();
-
- return Communication._axiosConfig('GET', requestUrl, undefined, {
- 'hub.mode': mode,
- 'hub.topic': topicUrl,
- 'hub.challenge': challenge,
- 'hub.lease_seconds': leaseSeconds,
- }, {});
- }
-
-
- /**
- * Create request config for denying an intent.
- * @param {String} requestUrl
- * @param {String} topicUrl
- * @param {String} reason
- * @returns {String}
- */
- static _intentDenyAxiosConfig(requestUrl, topicUrl, reason) {
- return Communication._axiosConfig('GET', requestUrl, undefined, {
- 'hub.mode': Enum.Mode.Denied,
- 'hub.topic': topicUrl,
- ...(reason && { 'hub.reason': reason }),
- }, {});
- }
-
-
- /**
- * Create request config for querying publisher for subscription validation.
- * @param {Topic} topic
- * @param {Verification} verification
- * @returns {String}
- */
- static _publisherValidationAxiosConfig(topic, verification) {
- const body = {
- callback: verification.callback,
- topic: topic.url,
- ...(verification.httpFrom && { from: verification.httpFrom }),
- ...(verification.httpRemoteAddr && { address: verification.httpRemoteAddr }),
- };
- return Communication._axiosConfig('POST', topic.publisherValidationUrl, body, {}, {
- [Enum.Header.ContentType]: Enum.ContentType.ApplicationJson,
- });
- }
-
-
- /**
- * Create request config for fetching topic content.
- * Prefer existing content-type, but accept anything.
- * @param {Topic} topic
- * @returns {String}
- */
- static _topicFetchAxiosConfig(topic) {
- const acceptWildcard = '*/*' + (topic.contentType ? ';q=0.9' : '');
- const acceptPreferred = [topic.contentType, acceptWildcard].filter((x) => x).join(', ');
- return Communication._axiosConfig('GET', topic.url, undefined, {}, {
- [Enum.Header.Accept]: acceptPreferred,
- ...(topic.httpEtag && { [Enum.Header.IfNoneMatch]: topic.httpEtag }),
- ...(topic.httpLastModified && { [Enum.Header.IfModifiedSince]: topic.httpLastModified }),
- });