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

super-simple-fastify-server

v0.1.2

Published

Super simple fastify web server for rapid dev/test

Downloads

726

Readme

Super Simple Fastify Web Server

npm version CI Status Badge

Super-simple Fastify web server! Quickly spin up a server for dev/test without all the bloat and boilerplate.

NOTE: This is still a WIP!

Install

npm i --save-dev super-simple-fastify-server

Usage

import { FastifyInstance, SimpleFastifyServer } from 'super-simple-fastify-server';

const server = new SimpleFastifyServer(
    async (app: FastifyInstance) => {
        fastify.get('/hello-world', async (request, _reply) => {
            return { message: `Hello, ${request.query.target}!` };
        });
    },
    {
        host: '127.0.0.1',
        port: 3456,
    }
);
await server.start();

// ...
// [12:54:50.668] INFO: Server listening at http://127.0.0.1:3456
// [12:54:50.669] INFO: Server http://127.0.0.1:3456 ready to accept connections!

// Now you can hit http://127.0.0.1:3456/hello-world?target=World

And when you're done:

await server.stop();

// ...
// [12:57:16.480] INFO: Server http://127.0.0.1:3456 stopped.

Configuration

The SimpleFastifyServer constructor supports the following configuration options. For any omitted options, it will attempt to pull a value from environment before falling back to the default:

  • host
    • Env Var: SIMPLE_FASTIFY_SERVER_HOST
    • Default: '0.0.0.0'
  • port
    • Env Var: SIMPLE_FASTIFY_SERVER_PORT
    • Default: 3456

Dockerizing the Server

See the Docker README for more info.

Miscellaneous

Returning response streams

You may want to test a client that consumes a large quantity of data from a JSON response stream. Here is a very simple example using the built-in buildObjectStreamResponse utility:

import { FastifyInstance, SimpleFastifyServer, buildObjectStreamResponse, sleep } from 'super-simple-fastify-server';

const server = new SimpleFastifyServer(async (app: FastifyInstance) => {
    app.get('/hello-world/stream', async function (_request, reply) {
        async function* myGeneratorFn() {
            await sleep(2000);
            yield *[
                { message: 'Hello' },
                { message: ', ' },
                { message: 'World!' }
            ];
        }
        const stream = buildObjectStreamResponse(myGeneratorFn());
        reply.header('Content-Type', 'application/octet-stream');
        return stream;
    });
});

And, an example client (using Axios) to process the response:

import stream from 'stream';
import { chain } from 'stream-chain';
import { withParser } from 'stream-json/streamers/StreamValues';

const response = await myAxiosClient.get('/hello-world/stream', { responseType: 'stream' });
const rawStream: stream.Readable = response.data;

const streamChain = chain([
    rawStream,
    withParser(),
    (parsed) => parsed.value
]);

let fullMessage = '';
for await (const obj of streamChain) {
    fullMessage += obj.message;
}
console.log(fullMessage); // Hello, World!