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

wstransaction

v2.0.0

Published

Control flow based communication for WebSocket-like connections.

Downloads

1

Readme

WSTransaction

Control flow based communication for WebSocket-like connections.

This library does not care whether it's on the client or server side (it's symmetric), the usage procedures are the same on both sides.

TODO: full documentation

Runtime guarantees

  • No uncaught exeptions will be thrown as a result of WSTransactor.constructor or [object WSTransactor].handle

  • Transactions started before connection will be fufilled on open.

  • Invalid received WSTransaction packets will never cause an uncaught error (SeriX serialization has litle checking however and will confuse same-size types; TODO: Debug/Safe Mode).

  • Transactions fail if:

    • An error ocurrs in the handler.
    • Messages are sent or received in a mismatched order.
  • Failed transactions will:

    • throw from [object WSTransaction].do
    • catch in [object WSTransaction].handle
    • log a warning message

General usage

const websocket: WebSocket /* browser websocket */ | import('ws').WebSocket

// Create a listener to be loaded directly into the transactor
// No risk of being missed
const listener = new WSTransactionHandle(
  'routeName',
  async io => {
    const dataIn = await io.receive(primitive.number)

    // ... do something ...

    io.send(dataOutA, primitive.string)
  },
)

const transactor = WSTransactor.wrap(websocket, [
  // Each one of these is equivalent to `handle(...)` but
  // it guarantees it is called before the 'HELLO' packet is sent.
  listener,
])
// alternatively, the constructor may be called manually:
// `const transactor = new WSTransactor(new WS(new RawWS(websocket)),[listener]);`
// Though this method is not preferable

// Listen for a transaction.
transactor.listen(
  'routeName',
  async io => {
    const dataIn = await io.receive(primitive.i32)

    // ... do something ...

    io.send(dataOutA, primitive.u8Array)
  },
)

// Request a transaction.
const dataOutA = getSomeDataToSend()
const result = await transactor.do(
  'otherRouteName',
  async io => {
    io.send(dataOutA, primitive.i32)

    // ... do something ...

    const dataIn = await io.receive(primitive.boolean)

    return someReceivedData
  },
)

Sending / Receiving non-primitive data types


/** (de)serialization information for your data */
class DataSerializationProfile extends SerializationProfile<Data, [number, number, string, OtherData]> {
  constructor () {
    super([
      primitive.number.action, // `NumberSerializationAction.default` or `new NumberSerializationAction()` also works
      primitive.i32.action,
      primitive.string.action,
      OtherData.serix.action, // `new OtherDataSerializationProfile()` also works
    ])
  }

  destructure ({ someNumber, someText, otherDataToo }: Data): [number, string, OtherData] {
    return [someNumber, someText, otherDataToo]
  }

  restructure (data: [number, string, OtherData]): Data {
    return new Data(...data)
  }
}

/** Your data class */
class Data {
  constructor (
    readonly someNumber: number,
    readonly someText: string,
    readonly otherDataToo: OtherData,
  ) {}

  static readonly serix = new SeriX(new DataSerializationProfile())
}

// Send and receive data containing your object
await transactor.do(
  'doSomethingIdk',
  async io => {
    const dataOut: Data
    io.send(dataOut, Data.serix)

    // ... do something ...

    const dataIn: Data = await io.receive(Data.serix)
  },
)