npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@suplink/message

v1.2.1

Published

基于javascript postMessage的通讯库

Downloads

16

Readme

概述

WEB端跨域通信库,支持订阅模式和请求响应模式。

使用指南

示例

订阅模式

import { Message } from '@suplink/message'

const message = new Message({ origin: '*' });

// 发送
message.dispatch({ id: 'test01', data: 'test' });

// 订阅
message.subscribe((e) => {
    console.log(e.data)
});

请求响应模式

// == 客户端请求
import { Client } from '@suplink/message'

const client = new Client({ origin: '*' });

client.request('/path/test', 'request').then((response) => {
    // 显示 request:response
    console.log(response.data);
});

// == 服务端响应
import { Server } from '@suplink/message'

const server = new Server();

server.router('/path/test', (request) => {
    return `${request.data}:response`;
})

API说明

客户端Client

用于跟服务端通信

constructor

参数

| 参数 | 类型 | 说明 | 默认 | 必须 | |--------|--------|----------|------------------------|-----| | origin | string | 通信origin | window.location.origin | 否 | | target | object | 目标窗口对象 | | 否 |

案例

import { Client } from '@suplink/message';

const client = new Client({ origin: '*' });

request

向服务端发送请求

参数

| 参数 | 类型 | 说明 | 默认 | 必须 | |--------|--------|--------------|------------------------|-----| | path | string | 请求路径 | | 是 | | data | any | 请求参数 | | 否 | | config | object | 请求配置,同构造函数参数 | | 否 |

案例

import { Client } from '@suplink/message';

const client = new Client({ origin: '*' });
client.request('/path', { id: 1 }).then(res => {
    console.log(res, 'res');
});

cancel

取消与服务端通信

案例

import { Client } from '@suplink/message';

const client = new Client({ origin: '*' });
client.request('/path', { id: 1 }).then(res => {
    console.log(res, 'res');
});
client.cancel();

resume

恢复与服务端通信

案例

import { Client } from '@suplink/message';

const client = new Client({ origin: '*' });
client.request('/path', { id: 1 }).then(res => {
    console.log(res, 'res');
});
client.cancel();
client.resume();

服务端Server

用于接收并处理客户端发送过来的请求

constructor

参数

同客户端

案例

import { Server } from '@suplink/message';

const server = new Server();

router

服务端路由,用于接收客户端特定路径请求并返回响应内容

参数

| 参数 | 类型 | 说明 | 默认 | 必须 | |---------|----------|-------------------------|------------------------|-----| | path | string | 路由匹配路径,当前为精准匹配 | | 是 | | handler | function | 路由匹配后的处理函数,入参为请求体,返回响应体 | | 是 |

案例

import { Server } from '@suplink/message';

const server = new Server();

server.router('/path', (req) => {
    console.log(req, 'req');
    const { id } = req.data;
    return { result: `id: ${id}` }
});

cancel

取消与客户端通信

resume

恢复与客户端通信

消息通信Message

基于postMessage的基础通信类,为Client和Server的基类

constructor

参数

| 参数 | 类型 | 说明 | 默认 | 必须 | |--------|--------|----------|------------------------|-----| | origin | string | 通信origin | window.location.origin | 否 | | target | object | 目标窗口对象 | | 否 |

dispatch

发送消息

参数

| 参数 | 类型 | 说明 | 默认 | 必须 | |-------------|------------------|--------------|------------------------|-----| | messageData | MessageData | 要发送的消息体 | | 是 | | config | object | 发送配置,同构造函数参数 | | 否 |

MessageData

| 属性 | 类型 | 说明 | 默认 | 必须 | |------|--------|-------|------------------------|-----| | type | string | 消息体类型 | | 否 | | id | string | 消息体ID | | 是 | | data | any | 消息体数据 | | 是 |

案例

import { Message } from '@suplink/message';

const message = new Message();

message.dispatch({ type: 'request', id: 'test', data: 'test' });

subscribe

订阅消息

参数

| 参数 | 类型 | 说明 | 默认 | 必须 | |----------|----------|-----|------------------------|-----| | listener | function | 监听器 | | 是 |

返回

| 参数 | 类型 | 说明 | 默认 | 必须 | |------------|--------|---------------------------|------------------------|-----| | listenerId | string | 监听器ID,传给unsubscribe用于取消订阅 | | 是 |

案例

import { Message } from '@suplink/message';

const message = new Message();

const subscribeId = message.subscribe((e) => {
    console.log(e)
});

unsubscribe

取消订阅

参数

| 参数 | 类型 | 说明 | 默认 | 必须 | |------------|-----------------|------------|------------------------|-----| | listenerId | string/string[] | 监听器ID或ID列表 | | 是 |

案例

import { Message } from '@suplink/message';

const message = new Message();

const subscribeId = message.subscribe((e) => {
    console.log(e)
});

message.unsubscribe(subscribeId);

init

初始化。注意默认实例化时已初始化

setConfig

动态更改实例配置

connect

建立通信连接

disconnect

断开通信连接

close

关闭通信连接