UNPKG

traverson

Version:

Hypermedia API/HATEOAS client for Node.js and the browser

61 lines (52 loc) 1.53 kB
'use strict'; var minilog = require('minilog') , log = minilog('traverson') , abortTraversal = require('../abort_traversal') , isContinuation = require('../is_continuation') , httpRequests = require('../http_requests'); var nextTickAvailable = process && Object.hasOwnProperty.call(process, 'nextTick'); /* * Execute the next step in the traversal. In most cases that is an HTTP get to *the next URL. */ function fetchResource(t, callback) { if (isContinuation(t)) { convertContinuation(t, callback); } else { fetchViaHttp(t, callback); } } fetchResource.isAsync = true; /* * This is a continuation of an earlier traversal process. * We need to shortcut to the next step (without executing the final HTTP * request of the last traversal again. */ function convertContinuation(t, callback) { log.debug('continuing from last traversal process (walker)'); if (nextTickAvailable) { return process.nextTick(function() { // de-zalgo continuations callback(t); }); } return callback(t); } function fetchViaHttp(t, callback) { // always check for aborted before doing an HTTP request if (t.aborted) { return abortTraversal.callCallbackOnAbort(t); } httpRequests.fetchResource(t, function(err, t) { log.debug('fetchResource returned'); if (err) { if (!err.aborted) { log.debug('error while executing http request'); log.error(err); } return t.callback(err); } callback(t); }); } module.exports = fetchResource;