1.2.12 • Published 2 months ago

@hazae41/piscine v1.2.12

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

piscine

Create async pools with automatic retry

npm i @hazae41/piscine

Node Package 📦

Features

Current features

  • 100% TypeScript and ESM
  • No external dependency
  • Simple API
  • Automatic retry
  • Get a fast random value using Math's PRNG
  • Get a secure random value using WebCrypto's CSPRNG

Usage

WebSockets

Create a pool of 5 WebSockets

import { Pool } from "@hazae41/piscine"

const pool = new Pool<Disposer<WebSocket>>(async ({ pool, index, signal }) => {
  const raw = new WebSocket(`/api`)

  /**
   * Define the socket
   */
  using presocket = new Box(new Disposer(raw, () => raw.close()))

  /**
   * Prepare the entry
   */
  const onCloseOrError = () => {
    pool.restart(index)
  }

  raw.addEventListener("error", onCloseOrError)
  raw.addEventListener("close", onCloseOrError)

  /**
   * Move socket in the entry
   */
  const socket = presocket.moveOrThrow()

  const onEntryClean = () => {
    /** 
     * Dispose the socket if the entry still owns it
     */
    using postsocket = socket

    /**
     * Clean the entry
     */
    raw.removeEventListener("error", onCloseOrError)
    raw.removeEventListener("close", onCloseOrError)
  }

  /**
   * Define the entry
   */
  using preentry = new Box(new Disposer(socket, onEntryClean))

  /**
   * Move the entry in the pool
   */
  return new Ok(preentry.unwrapOrThrow())
}, { capacity: 5 })

Random

Get a random open socket using Math's PRNG

const socket = await pool.random()

socket.send("Hello world")

Get a random open socket using WebCrypto's CSPRNG

const socket = await pool.cryptoRandom()

socket.send("Hello world")

Iteration

Pools are iterator, so you can loop through open sockets or create an array

for (const socket of pool)
  socket.send("Hello world")
const sockets = [...pool]
1.2.12

2 months ago

1.2.11

2 months ago

1.2.8

3 months ago

1.2.7

3 months ago

1.2.6

3 months ago

1.2.10

3 months ago

1.2.9

3 months ago

1.2.5

4 months ago

1.2.4

4 months ago

1.2.3

5 months ago

1.2.2

5 months ago

1.2.0

5 months ago

1.2.1

5 months ago

1.1.12-1

8 months ago

1.1.10-3

8 months ago

1.1.12-0

8 months ago

1.1.10-2

8 months ago

1.1.10-1

8 months ago

1.1.1

9 months ago

1.1.0

9 months ago

1.0.39

10 months ago

1.1.10-7

8 months ago

1.1.9

8 months ago

1.1.8

8 months ago

1.1.10-5

8 months ago

1.1.7

8 months ago

1.1.10-4

8 months ago

1.1.6

8 months ago

1.1.5

8 months ago

1.1.4

8 months ago

1.1.3

8 months ago

1.1.10-8

8 months ago

1.1.2

8 months ago

1.0.40

10 months ago

1.1.12

7 months ago

1.1.11

8 months ago

1.1.10

8 months ago

1.0.42

9 months ago

1.0.41

9 months ago

1.1.16

6 months ago

1.1.15

6 months ago

1.1.14

6 months ago

1.1.13

6 months ago

1.1.19

6 months ago

1.1.18

6 months ago

1.1.17

6 months ago

1.1.23

6 months ago

1.1.22

6 months ago

1.1.20

6 months ago

1.1.25

6 months ago

1.0.19

12 months ago

1.0.18

12 months ago

1.0.17

12 months ago

1.0.38

11 months ago

1.0.16

12 months ago

1.0.9

12 months ago

1.0.8

12 months ago

1.0.7

12 months ago

1.0.22

12 months ago

1.0.21

12 months ago

1.0.20

12 months ago

1.0.26

12 months ago

1.0.25

12 months ago

1.0.24

12 months ago

1.0.23

12 months ago

1.0.29

11 months ago

1.0.28

12 months ago

1.0.27

12 months ago

1.0.33

11 months ago

1.0.11

12 months ago

1.0.32

11 months ago

1.0.10

12 months ago

1.0.31

11 months ago

1.0.30

11 months ago

1.0.37

11 months ago

1.0.15

12 months ago

1.0.36

11 months ago

1.0.14

12 months ago

1.0.13

12 months ago

1.0.34

11 months ago

1.0.12

12 months ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.0

1 year ago