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

cs-channel

v1.0.4

Published

一个跨端调用通信库,让socket、web worker、iframe通信、vscode(`webview`)、electron(`ipcMain`、`ipcRenderer`)等全双工通信调用像调用ajax(`async/await`)一样马上得到处理结果: ```javascript (async () => { const params = { ... }; const data = await channel.call('server-api', params);

Downloads

40

Readme

cs-channel

一个跨端调用通信库,让socket、web worker、iframe通信、vscode(webview)、electron(ipcMainipcRenderer)等全双工通信调用像调用ajax(async/await)一样马上得到处理结果:

  (async () => {
    const params = { ... };
    const data = await channel.call('server-api', params);
  })();

连异常都能处理得非常优雅哦!

try {
  await clientChannel.call<string>('http_post', data);
} catch(e) {
  expect(e.message).toBe('can not find api http_post');
}
const data = { url: 'http://www.baidu.com' };
const error = 'something wrong...';
serverChannel.on('http_get', async param => {
  throw new Error(error);
});
try {
  await clientChannel.call<string>('http_get', data);
} catch(e) {
  expect(e.message).toBe(error);
}

两端(客户端、服务端)同时使用,若服务端环境非nodejs(比如socket下的.NET或java),也可以使用预定的消息协议处理,不影响客户端使用(消息协议后文会说明)。

使用方法

初始化channel实例,定义sender发送与receiver接收两个回调(用vscode的webview开发举例):

客户端

  const vscode = acquireVsCodeApi();
  const clientChannel = new Channel({
    sender: message => void vscode.postMessage(message),
    receiver: callback => {
      window.addEventListener('message', (event: { data: any }) => {
        event && event.data && callback(event.data);
      });
    }
  });

服务端

  const serverChannel = new Channel({
    sender: message => void panel.webview.postMessage(message),
    receiver: callback => {
      panel.webview.onDidReceiveMessage((message: IMessage) => {
        message.api && callback(message);
      }), undefined, context.subscriptions);
    }
  });

此举目的是适配不同场景的消息通信方案。

然后,分为两个场景:

  • 客户端调服务端api
  • 服务端调客户端api
  // 客户端调服务端api

  // 服务端订阅api
  serverChannel.on('server-api', async param => {
    // 处理完成后返回结果
    return await http.get(param.url, param.data);
  });
  // 客户端调用api得到服务端返回的结果
  const result = await clientChannel.call('server-api', {
    url: 'http://...',
    data: { id: 1 }
  });
  // 服务端调客户端api

  // 客户订阅api
  clientChannel.on('client-api', async param => {
    // 客户端收到消息后显示出来,不返回任何数据
    document.querySelector('#msg').innerHTML = param.message;
    document.querySelector('#sendor').innerHTML = param.sendor;
  });
  // 服务端调用api得到客户端返回的结果
  const result = await serverChannel.call('client-api', {
    message: 'hello, client!',
    sender: 'server'
  });

消息协议

  interface IMessage<T=IKeyValueMap> {
    api: string;
    data: T;
    error?: Error;
    msgId: string;
  }
  • 13个单元测试已通过
  • TypeScript静态类型约束 + d.ts声明