lambda-remote-context
TypeScript icon, indicating that this package has built-in type declarations

3.0.1 • Public • Published

lambda-remote-context

install with npm i lambda-remote-context

Provides two classes:

  • RemoteContext (lifecycle management for RemoteClient)
  • RemoteClient (a base class for wrapping third party clients)

A subclass of RemoteClient is constructed with async init and cleanUp methods.

Typically you would create a subclasses of RemoteClient, say AliceRemoteClient and BobRemoteClient, to wrap third party adapters needed by your serverless function (e.g. Redis, Prisma, Mastodon, etc). You can construct these clients and have their lifecycle managed by RemoteContext as follows:

const arc = new AliceRemoteClient({
    init: async () => console.log('do initialization work'),
    cleanUp: async () => console.log('do cleanup tasks')
})

const brc = new BobRemoteClient({
    init: async () => console.log('do initialization work'),
    cleanUp: async () => console.log('do cleanup tasks')
})

const remoteContext = new RemoteContext()
try {
    await remoteContext
        .addClient(arc)
        .addClient(brc)
        .initialize();

    // Use the RemoteClients in your function to communicate
    // with external resources.  Each RemoteClient should wrap a 
    // client from a third party library.

} finally {
    await remoteContext.cleanUp()
}

Important Note:

Initalisation and cleanup promises all happen in parallel (i.e. in random order) and the function blocks until all are settled.

If one or more of the settled promises were rejected, then only the .reason from the rejected promise of the earliest-added client is thrown.

Readme

Keywords

Package Sidebar

Install

npm i lambda-remote-context

Weekly Downloads

1

Version

3.0.1

License

MIT

Unpacked Size

21.7 kB

Total Files

35

Last publish

Collaborators

  • mindlapse