How to use the asynciterator.BufferedIterator function in asynciterator

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 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 / triple-pattern-fragments / TurtleFragmentIterator.js View on Github external
function TurtleFragmentIterator(source, fragmentUrl) {
  if (!(this instanceof TurtleFragmentIterator))
    return new TurtleFragmentIterator(source, fragmentUrl);
  TransformIterator.call(this, source);
  this._fragmentUrl = fragmentUrl;

  // Expose an additional metadata stream
  this.metadataStream = new BufferedIterator();
  if (source && source.ended) return this.metadataStream._push(null);
  // When a metadata listener is added, drain the source to read metadata
  var self = this;
  this.metadataStream.on('newListener', function metadataListenerAdded(event) {
    if (event === 'data' || event === 'end') {
      this.removeListener('newListener', metadataListenerAdded);
      self.maxBufferSize = Infinity;
    }
  });

  // 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;