Skip to content

RemoteContext and abstract RemoteClient for lifecycle mgmt of remote clients in Lambda

Notifications You must be signed in to change notification settings

mindlapse/lambda-remote-context

Repository files navigation

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.

About

RemoteContext and abstract RemoteClient for lifecycle mgmt of remote clients in Lambda

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published