How to use asynciterator - 10 common examples

To help you get started, we’ve selected a few asynciterator examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github LinkedDataFragments / Client.js / lib / browser / Request.js View on Github external
request.onload = function () {
    // Convert the response into an iterator
    var response = AsyncIterator.single(request.responseText || '');
    response.statusCode = request.status;

    // Parse the response headers
    var resHeaders = response.headers = {},
        rawHeaders = request.getAllResponseHeaders() || '',
        headerMatcher = /^([^:\n\r]+):[ \t]*([^\r\n]*)$/mg, match;
    while (match = headerMatcher.exec(rawHeaders))
      resHeaders[match[1].toLowerCase()] = match[2];

    // Emit the response
    requestProxy.emit('response', response);

    // If the resource was time-negotiated, store its queryless URI
    // to enable the PERFORMANCE HACK explained above
    if (reqHeaders['accept-datetime'] && resHeaders['memento-datetime']) {
      var resource = removeQuery(resHeaders['content-location'] || settings.url);
github Callidon / sparql-engine / src / operators / exists-operator.js View on Github external
_transform (bindings, done) {
    let exists = false
    // build an iterator to evaluate the EXISTS clause using the set of bindings
    // using a LIMIT 1, to minimize the evaluation cost
    const iterator = this._builder._buildWhere(single(bindings), this._groups, this._options).take(1)
    iterator.on('error', err => this.emit('error', err))
    iterator.on('end', () => {
      if (exists && (!this._notexists)) {
        // EXISTS case
        this._push(bindings)
      } else if ((!exists) && this._notexists) {
        // NOT EXISTS case
        this._push(bindings)
      }
      done()
    })
    iterator.on('data', () => {
      exists = true
    })
  }
}
github comunica / comunica / packages / actor-rdf-resolve-quad-pattern-sparql-json / lib / ActorRdfResolveQuadPatternSparqlJson.ts View on Github external
public async queryBindings(endpoint: string, query: string, context: ActionContext): Promise {
    // Parse each binding and push it in our buffered iterator
    const bindingsStream: BufferedIterator = new BufferedIterator(
      { autoStart: false, maxBufferSize: Infinity });
    let initialized: boolean = false;
    const superRead = bindingsStream._read;
    bindingsStream._read = (count: number, done: () => void) => {
      if (!initialized) {
        initialized = true;
        this.fetchBindingsStream(endpoint, query, context).then((responseStream) => {
          const rawBindingsStream = new SparqlJsonParser({ prefixVariableQuestionMark: true })
            .parseJsonResultsStream(responseStream);
          responseStream.on('error', (error) => rawBindingsStream.emit('error', error));

          rawBindingsStream.on('error', (error) => bindingsStream.emit('error', error));
          rawBindingsStream.on('data', (rawBindings) => bindingsStream._push(Bindings(rawBindings)));
          rawBindingsStream.on('end', () => {
            bindingsStream.close();
          });
github LinkedDataFragments / Server.js / lib / datasources / Datasource.js View on Github external
Datasource.prototype.select = function (query, onError) {
  if (!this.initialized)
    return onError && onError(new Error('The datasource is not initialized yet'));
  if (!this.supportsQuery(query))
    return onError && onError(new Error('The datasource does not support the given query'));

  // Translate blank nodes IRIs in the query to blank nodes
  var blankNodePrefix = this._blankNodePrefix, blankNodePrefixLength = this._blankNodePrefixLength;
  if (query.subject && query.subject.indexOf(blankNodePrefix) === 0)
    (query = _.clone(query)).subject = '_:' + query.subject.substr(blankNodePrefixLength);
  if (query.object  && query.object.indexOf(blankNodePrefix) === 0)
    (query = _.clone(query)).object  = '_:' + query.object.substr(blankNodePrefixLength);

  // Translate blank nodes in the result to blank node IRIs
  var destination = new BufferedIterator(), outputTriples;
  outputTriples = destination.map(function (triple) {
    if (triple.subject[0] === '_') triple.subject = blankNodePrefix + triple.subject.substr(2);
    if (triple.object[0]  === '_') triple.object  = blankNodePrefix + triple.object.substr(2);
    return triple;
  });
  outputTriples.copyProperties(destination, ['metadata']);
  onError && outputTriples.on('error', onError);

  // Execute the query
  try { this._executeQuery(query, destination); }
  catch (error) { outputTriples.emit('error', error); }
  return outputTriples;
};
github LinkedDataFragments / Client.js / lib / sparql / UnionIterator.js View on Github external
function UnionIterator(sources, options) {
  if (!(this instanceof UnionIterator))
    return new UnionIterator(sources, options);
  BufferedIterator.call(this, options);

  // Construct a list of readable sources
  this._sources = [];
  this._sourceIndex = 0;
  if (sources && sources.length) {
    // Create event listeners
    var self = this;
    function fillBuffer()     { self._fillBuffer(); }
    function emitError(error) { self.emit('error', error); }

    // Add all readable sources and listen to their events
    for (var i = 0, l = sources.length; i < l; i++) {
      var source = sources[i];
      if (source && !source.ended) {
        this._sources.push(source);
        source.on('readable', fillBuffer);
github LinkedDataFragments / Client.js / lib / sparql / UnionIterator.js View on Github external
function fillBuffer()     { self._fillBuffer(); }
    function emitError(error) { self.emit('error', error); }

    // Add all readable sources and listen to their events
    for (var i = 0, l = sources.length; i < l; i++) {
      var source = sources[i];
      if (source && !source.ended) {
        this._sources.push(source);
        source.on('readable', fillBuffer);
        source.on('end',      fillBuffer);
        source.on('error',    emitError);
      }
    }
  }
}
BufferedIterator.subclass(UnionIterator);

// Reads items from the sources in a round-robin way
UnionIterator.prototype._read = function (count, done) {
  var sources = this._sources, item = null, attempts = sources.length;
  // While no item has been found, attempt to read all sources once
  while (item === null && attempts--) {
    var source = sources[this._sourceIndex];
    item = source.read();
    // Remove the current source if it has ended
    if (source.ended)
      sources.splice(this._sourceIndex, 1);
    // Otherwise, start from the succeeding source next time
    else
      this._sourceIndex++;
    if (this._sourceIndex >= sources.length)
      this._sourceIndex = 0;
github LinkedDataFragments / Client.js / lib / triple-pattern-fragments / federated / FederatedFragmentsClient.js View on Github external
// Process a fragment error
    fragment.once('error', function (error) {
      // Only error if the threshold across fragments has been reached
      if (errorThreshold-- === 0)
        return compoundFragment.emit('error', error);
      // Otherwise, silently assume this fragment has no results
      processMetadata({});
      fragmentDone();
    });
  });

  // Make the compound fragment become readable
  function setReadable() { compoundFragment.readable = true; }
}
BufferedIterator.subclass(CompoundFragment);

// Reads elements of the first non-empty child fragments
CompoundFragment.prototype._read = function (count, done) {
  var fragments = this._fragments;
  for (var index in fragments) {
    var fragment = fragments[index], item;
    // Try to read as much items from the fragment as possible
    while (count > 0 && (item = fragment.read()))
      this._push(item), count--;
    // Stop if we have read sufficient elements
    if (!count) break;
  }
  done();
};

// Empties the fragment
github LinkedDataFragments / Client.js / lib / triple-pattern-fragments / TurtleFragmentIterator.js View on Github external
// Convert Turtle into triples using the N3 parser
  this._parser = new N3.Parser({ documentURI: fragmentUrl });
  this._parser.parse({
    // Use dummy stream to capture `data` and `end` callbacks
    on: function (event, callback) {
      if (event === 'data') self._parseData = callback;
      else if (event === 'end') self._parseEnd = callback;
    },
  },
  // Process each triple and emit possible errors
  function (error, triple) {
    if (error) self.emit('error', error);
    else if (triple) self._processTriple(triple);
  });
}
TransformIterator.subclass(TurtleFragmentIterator);

// Sends a chunk of Turtle to the N3 parser to convert it to triples
TurtleFragmentIterator.prototype._transform = function (chunk, done) {
  this._parseData(chunk), done();
};

// Sends the given parsed triple to the data or metadata stream
TurtleFragmentIterator.prototype._processTriple = function (triple) {
  // This separation between data and metadata/controls is an approximation;
  // for a proper separation, use an RDF format with graph support (see TrigFragmentParser).
  if (triple.subject !== this._fragmentUrl && triple.predicate.indexOf(rdf.HYDRA) !== 0)
    this._push(triple);
  else
    this.metadataStream._push(triple);
};
github LinkedDataFragments / Client.js / lib / sparql / SparqlIterator.js View on Github external
return new UnionIterator(group.patterns.map(function (patternToken) {
      return new SparqlGroupIterator(source.clone(), patternToken, childOptions);
    }), options);
  case 'filter':
    // A set of bindings does not match the filter
    // if it evaluates to 0/false, or errors
    var evaluate = new SparqlExpressionEvaluator(group.expression);
    return source.filter(function (bindings) {
      try { return !/^"false"|^"0"/.test(evaluate(bindings)); }
      catch (error) { return false; }
    });
  default:
    throw new Error('Unsupported group type: ' + group.type);
  }
}
AsyncIterator.subclass(SparqlGroupIterator);


// Error thrown when the query has a syntax error
var InvalidQueryError = createErrorType('InvalidQueryError', function (query, cause) {
  this.message = 'Syntax error in query\n' + cause.message;
});

// Error thrown when no combination of iterators can solve the query
var UnsupportedQueryError = createErrorType('UnsupportedQueryError', function (query, cause) {
  this.message = 'The query is not yet supported\n' + cause.message;
});


module.exports = SparqlIterator;
SparqlIterator.InvalidQueryError = InvalidQueryError;
SparqlIterator.UnsupportedQueryError = UnsupportedQueryError;
github openplannerteam / planner.js / src / planner / public-transport / JourneyExtractorDefault.ts View on Github external
public async extractJourneys(
    profilesByStop: IProfilesByStop,
    query: IResolvedQuery,
  ): Promise> {
    const filteredProfilesByStop: IProfilesByStop = ProfileUtil.filterInfinity(profilesByStop);

    const departureLocation: ILocation = query.from[0];
    const arrivalLocation: ILocation = query.to[0];

    const paths: IPath[] = [];

    const departureLocationProfiles: IProfile[] = filteredProfilesByStop[departureLocation.id];

    // Can't find departure stop;
    if (!departureLocationProfiles) {
      return new ArrayIterator(paths);
    }

    for (const profile of departureLocationProfiles) {

      for (let amountOfTransfers = 0; amountOfTransfers < profile.transferProfiles.length; amountOfTransfers++) {
        const transferProfile: ITransferProfile = profile.transferProfiles[amountOfTransfers];

        if (this.checkBestArrivalTime(transferProfile, departureLocation, arrivalLocation)) {
          try {
            paths.push(await this.extractJourney(
              departureLocation,
              arrivalLocation,
              transferProfile,
              amountOfTransfers,
              filteredProfilesByStop,
            ));