All files producer.js

100% Statements 14/14
100% Branches 2/2
100% Functions 1/1
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 283x 3x   3x 3x     3x 3x   3x   3x 3x     2x 2x   1x 1x       3x      
require('dotenv').config();
const { PubSub } = require('@google-cloud/pubsub');
 
const projectId = 'ecni2-2026';
const pubSubClient = new PubSub({ projectId });
 
async function publishMessage(tags) {
  const studentId = process.env.ECNI2_STUDENT_ID || '1';
  const topicName = `ecni2-${studentId}`;
 
  const dataBuffer = Buffer.from(JSON.stringify({ tags }));
 
  try {
    const messageId = await pubSubClient
      .topic(topicName)
      .publishMessage({ data: dataBuffer });
    console.log(`Message ${messageId} publiƩ sur le topic ${topicName}`);
    return messageId;
  } catch (error) {
    console.error(`Erreur lors de la publication sur ${topicName}:`, error);
    throw error;
  }
}
 
module.exports = {
  publishMessage
};