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

@ethicdevs/fastify-custom-session

v0.6.0

Published

A Fastify (v3.x+) plugin that let you use session and decide only where to load/save from/to

Downloads

10

Readme

fastify-custom-session

NPM MIT License ci-master Average issue resolution time Number of open issues

A Fastify (v3.x+) plugin that let you use session and decide only where to load/save from/to through the adapter pattern.

Built-in adapters

  • FirebaseSessionAdapter (firebase-admin) [fully working]
  • MockSessionAdapter (in-memory, for writing tests) [fully working]
  • PrismaSessionAdapter (@prisma/client compat layer) [fully working]
  • PostgresSessionAdapter (pg, pg-pool) [wip]

Installation

$ yarn add @ethicdevs/fastify-custom-session
# or
$ npm i @ethicdevs/fastify-custom-session

Usage

import fastifyCookies from "@fastify/cookie";
import fastifyCustomSession, {
  FirebaseSessionAdapter, // Firebase Firestore adapter
  MockSessionAdapter, // In Memory adapter (for testing)
  PostgresSessionAdapter, // PostgreSQL adapter (pg/pg-pool)
  PrismaSessionAdapter, // Prisma Client adapter
} from "@ethicdevs/fastify-custom-session";

let server = null;

function main() {
  server = fastify(); // provide your own

  // some cookies options
  const cookieSecret = "super-secret-session-secret"; // or better: Env.SESSION_SECRET
  const cookiesOptions = {
    domain: `.my-app.com`, // or better: `.${Env.DEPLOYMENT_DOMAIN}`,
    httpOnly: true,
    expires: new Date(Date.now() + 10 * (8 * 3600) * 1000), // 10 days in secs
    path: "/",
    secure: false,
    sameSite: "lax",
    signed: true,
  };
  // register the cookies plugin FIRST.
  server.register(fastifyCookies, {
    parseOptions: cookiesOptions,
    secret: cookieSecret,
  });
  // then register the customSession plugin.
  server.register(fastifyCustomSession, {
    password: cookieSecret,
    cookieName: "my_app_session_id", // or better: Env.COOKIE_NAME,
    cookieOptions,
    storeAdapter: new MockSessionAdapter({
      /* ... AdapterOptions ... */
    }) as any,
    ttl: 30 * 60, // expire session after 30 minutes (in seconds)
    initialSession: { // initial data in session (so you can avoid null's)
      whateverYouWant: '<unset>',
      aNullableProp: null,
      mySuperObject: {
        foo: '<unset>',
        bar: 0,
        baz: '<unset>',
      };
    },
  });
}

main();

then if you are a TypeScript user you will need to define the shape of the session.data object, you can do so easily by adding the following lines to your types/global/index.d.ts file:

// use declaration merging to provide custom session interface
declare module "@ethicdevs/fastify-custom-session" {
  declare interface CustomSession {
    // request.session.data shape
    whateverYouWant: string;
    aNullableProp: null | string;
    mySuperObject: {
      foo: string;
      bar: number;
      baz: string;
    };
  }
}

later on during request before you send the headers/response, typically in your controller/handler:

const myRequestHandler = async (request, reply) => {
  request.session.data.whateverYouWant = "foo";
  request.session.data.aNullableProp = "not null anymore";
  request.session.data.mySuperObject = {
    foo: "bar",
    bar: 42,
    baz: "quxx",
  };

  return reply.send("Hello with session!");
};

const mySecondRequestHandler = async (request, reply) => {
  // if you're a typescript user, enjoy ;)
  /* request.session.data.
                    | [p] whateverYouWant
                    | [p] mySuperObject */

  request.session.data.whateverYouWant; // "foo"
  request.session.data.aNullableProp; // "not null anymore"
  request.session.data.object.foo; // "bar"
  request.session.data.object.bar; // 42

  return reply.send(
    "Cool value from session:" + request.session.data.object.baz,
  );
  // "Cool value from session: quxx"
};

then enjoy the session being available both in your controllers/handlers and in your data store table/collection (linked to the Adapter you chosen in first step).

Debugging

This library make use of the debug package, thus it's possible to make the output of the program more verbose.

The plugin itself, and the adapters have their own "scopes" of logs so its possible to troubleshoot only with logs from the plugin, or with logs from the adapter(s) or both.

# Syntax and examples:
# DEBUG=$scope:$subscope $(...command)

# Show all logs kinds from the customSession plugin
$ DEBUG=customSession:* yarn dev

# Show only trace logs from the customSession plugin
$ DEBUG=customSession:trace yarn dev

# Show only error logs from the customSession plugin
$ DEBUG=customSession:error yarn dev

Note: in this example, $scope could be:

  • customSession ;
  • firebaseSessionAdapter ;
  • mockSessionAdapter ;
  • prismaSessionAdapter ;
  • postgresSessionAdapter ;
  • yourOwnSessionAdapter.

License

The MIT license.