@6river/pubcap 中文文档教程

发布于 3年前 浏览 25 项目主页 更新于 3年前

PubCap

PubCap 旨在捕获发布到 Google Pub/Sub 主题的消息。

Install and Setup

将组件安装为开发依赖项:

npm i @sixriver/pubcap -D

Usage

Bellow 是 Mocha 框架的典型设置。

设置 before/after 挂钩以注册正在收听的主题和代码以在测试结束后清理混乱:

const pubcap = new PubCap({

});

before(async function() {
    await pubcap.listen(pubsub, ['topic1', 'topic2']);
});

after(async function() {
    await pubcap.close();
});

可选地设置 beforeEachafterEach 挂钩从已注册主题中排出消息:

beforeEach(async function() {
    await pubcap.drain();
})

使用 PubCap#messages 方法访问捕获的消息:

it('should capture messages', async function() {
    const myMessages = await pubcap.messages('my-topic');
    // ...
});

Getting Raw Pub/Sub messages

方法 PubCap#messages 默认返回 JSON.parse(msg.数据.toString())。 使用 Raw 解码器获取原始 Pub/Sub 消息:

import {PubCap, RAW} from '@6river/pubcap';

const pubcap = new PubCap();
// ...
const messages = await pubcap.messages('my-topic', {decoder: RAW});

Writing your own message decoder

实现 Decoder 接口以将原始消息转换为某种自定义(或类型安全)方式。

import {Message} from '@google-cloud/pubsub';
import {Decoder, PubCap} from '@6river/pubcap';

// Suppose we have some message type
interface FooMessage {
    id: string;
    foo: boolean;
}

// Here is our decoder
class FooDecoder implements Decoder<FooMessage> {
    decode(msg: Message): FooMessage {
        const foo: FooMessage = ...
        return foo;
    }
}


// now make the decoder and use it
const decoder = new FooDecoder();
const messages = await pubcap.messages('my-topic', {decoder});

Se also

test/pubcap.spec.ts 作为示例。

PubCap

PubCap is designed to capture messages published to Google Pub/Sub topics.

Install and Setup

Install the component as development dependency:

npm i @sixriver/pubcap -D

Usage

Bellow is the typical setup for Mocha framework.

Setup before/after hooks to register topics being listened and the code to cleanup the mess after the tests are over:

const pubcap = new PubCap({

});

before(async function() {
    await pubcap.listen(pubsub, ['topic1', 'topic2']);
});

after(async function() {
    await pubcap.close();
});

Optionally setup beforeEach or afterEach hook to drain messages from the registered topics:

beforeEach(async function() {
    await pubcap.drain();
})

Access captured messages using PubCap#messages method:

it('should capture messages', async function() {
    const myMessages = await pubcap.messages('my-topic');
    // ...
});

Getting Raw Pub/Sub messages

Method PubCap#messages by default returns the result of JSON.parse(msg.data.toString()). Use Raw decoder to get raw Pub/Sub messages:

import {PubCap, RAW} from '@6river/pubcap';

const pubcap = new PubCap();
// ...
const messages = await pubcap.messages('my-topic', {decoder: RAW});

Writing your own message decoder

Implement the Decoder interface to get raw messages converted some custom (or typesafe) way.

import {Message} from '@google-cloud/pubsub';
import {Decoder, PubCap} from '@6river/pubcap';

// Suppose we have some message type
interface FooMessage {
    id: string;
    foo: boolean;
}

// Here is our decoder
class FooDecoder implements Decoder<FooMessage> {
    decode(msg: Message): FooMessage {
        const foo: FooMessage = ...
        return foo;
    }
}


// now make the decoder and use it
const decoder = new FooDecoder();
const messages = await pubcap.messages('my-topic', {decoder});

Se also

test/pubcap.spec.ts as an example.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文