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

chromite

v0.1.8

Published

Chrome Extension Message-Routing toolkit

Downloads

59

Readme

chromite - Chrome Extension Messaging Routing Kit

version downloads Node.js CI Chrome E2E Test codecov Maintainability

  • Use Router to simplify your onMessage event listener routing
  • Use Client as a shorthand for sendMessage
  • Use Logger for decorating console.log with intuitive interface

to write your Chrome Extension in the way of Web Application Development.

Why?

Message Passing plays a crucial role in the development of Chrome extensions. Therefore, as Chrome extensions become more feature-rich, the variety of messages being sent and received between the background and other contexts increases. Managing all of this within a single onMessage.addListener and dispatching to different handlers can make the code quite messy.

Which is like this:

// This is what we do usually... 😰
chrome.runtime.onMessage.addListener(async (message, sender, sendResponse) => {
  switch (message.action) {
    case '/users/list':
      const users = await Users.getAll()
      sendResponse({ users });
      break;
    case '/users/get':
      const user = await Users.get(message.userId);
      sendResponse({ user });
      break;
    case '/users/update':
      const user = Users.get(message.userId)
      // more your code here...
      break;
    default:
      sendResponse({ message: "Not Found..." });
  }
  return true;
});

This is very similar to what we write when we build a web application and routing HTTP request. Then, if we organize the code in that manner, we can create a Chrome extension source code that is more comprehensible and maintainable.

Router

Specifically, it would look like this:

const router = new Router();

router.on("/users/list", ListUsersController);
router.on("/users/{id}", GetUserController);
router.on("/users/{id}/update", UpdateUserController);
router.onNotFound(NotFoundController);

chrome.runtime.onMessage.addListener(router.listener());
// Simple! 🤗

then one of your controllers will look like this:

async function ListUsersController(message, sender) {
    const users = await Users.getAll();
    return { users }; // You don't even need sendResponse
}

async function GetUserController(this: {id: string}, message, sender) {
    // You can retrieve path parameter from `this` arg
    const user = await Users.get(this.id);
    return { user };
}

this will make our life easier.

Then simply you can send message to this listener like this:

const users = await chrome.runtime.sendMessage({action: '/users/list'});
// Nothing different to whant we do usually.

Client

In case you need some shorthand to send message, which might be a HTTP client in web application, there is Client you can use and you can avoid using action field in your message.

const client = new Client(chrome.runtime);

// path (=action) only
const users = await client.send('/users/list');

// path with request body
const updated = await client.send(`/users/${id}/update`, {name: "otiai20"});

ActiveRecord?

Now you might want something like ActiveRecord to access and OR-mapping chrome.storage. There is a separated package: jstorm - JavaScript ORM for chrome.storage and LocalStorage.

https://github.com/otiai10/jstorm

Small example:

// This uses window.sessionStorage
import { Model } from "jstorm/browser/session";

class User extends Model {
  public name: string;
  public age:  number;
}

(async () => {
  const otiai10 = User.new({name:"otiai10"});
  (otiai10._id) // null, yes
  await otiai10.save();
  (otiai10._id) // NOT null, because saved

  const found = await User.find(otiai10._id);
  (found._id == otiai10._id) // true

  otiai10.delete();
})();

Logger

Last but not least, logging is also important for us. Even though we know we can customize console.log by %c decorator, it would be messy if we do that all the time. Logger is just a memorandum for that decoration, or we can just use like following:

import {Logger, LogLevel} from "chromite";

const logger = new Logger("your_project", LogLevel.ERROR);

logger.warn("hello", 100, {name: "otiai10"});
// prints nothing because level is set to "ERROR"

// This prints these messages with colored prefix "[ERROR]"
logger.error("hey", {code: 500, msg: ["some", "problem"]});

Issues

  • https://github.com/otiai10/chromite/issues/new