or run

tessl search
Log in

Version

Files

tile.json

task.mdevals/scenario-2/

JSON Stream Processor

Build a Node.js application that processes streaming JSON data from an HTTP endpoint and extracts specific information based on patterns.

Requirements

Create a module that:

  1. Connects to a JSON streaming endpoint that returns user activity data
  2. Extracts and processes user objects as they arrive in the stream
  3. Filters users based on specific criteria (active status)
  4. Collects email addresses from matching users
  5. Reports when processing is complete

Input

The streaming endpoint returns JSON in this structure:

{
  "users": [
    {
      "id": 1,
      "name": "Alice",
      "email": "alice@example.com",
      "active": true
    },
    {
      "id": 2,
      "name": "Bob",
      "email": "bob@example.com",
      "active": false
    }
  ]
}

Expected Behavior

Your implementation should:

  • Process user objects incrementally as they arrive
  • Only collect emails from users where active is true
  • Return an array of collected email addresses when streaming completes
  • Handle errors gracefully

Function Signature

/**
 * Fetches and processes streaming JSON data from the given URL
 * @param {string} url - The URL to fetch JSON data from
 * @param {function} callback - Called with (error, emails) when complete
 */
function processUserStream(url, callback) {
  // Your implementation here
}

Dependencies { .dependencies }

oboe { .dependency }

Provides streaming JSON parsing capabilities for processing data as it arrives.

Test Cases

Test 1: Basic user filtering { .test }

Setup:

const http = require('http');

// Mock server that returns user data
const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'application/json' });
  res.end(JSON.stringify({
    users: [
      { id: 1, name: "Alice", email: "alice@example.com", active: true },
      { id: 2, name: "Bob", email: "bob@example.com", active: false },
      { id: 3, name: "Charlie", email: "charlie@example.com", active: true }
    ]
  }));
});
server.listen(3000);

Input:

processUserStream('http://localhost:3000', (err, emails) => {
  console.log(emails);
});

Expected Output:

['alice@example.com', 'charlie@example.com']

Cleanup:

server.close();

Test 2: Empty results { .test }

Setup:

const http = require('http');

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'application/json' });
  res.end(JSON.stringify({
    users: [
      { id: 1, name: "Bob", email: "bob@example.com", active: false },
      { id: 2, name: "Dave", email: "dave@example.com", active: false }
    ]
  }));
});
server.listen(3001);

Input:

processUserStream('http://localhost:3001', (err, emails) => {
  console.log(emails);
});

Expected Output:

[]

Cleanup:

server.close();

Implementation Notes

  • Your solution should use streaming techniques rather than waiting for the complete response
  • Process each user object as soon as it's available in the stream
  • The callback should be called once when all data has been processed