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

armature-core

v0.2.9

Published

A fullstack web framework using Bun

Downloads

25

Readme

Armature: Fullstack Framework

To use:

bunx create-armature-app ./project-name

JSX to HTML

Armature uses my custom JSX runtime called effective-jsx that enables JSX without React. Being server-side rendered (SSR) as HTML means:

  • className reverts to class
  • style needs to be a string, not an object (always close your attributes with semi-colons: width:50px;)
  • The DOM is simpler to interact with, eliminating the need for useRef()

Custom Element

The JSX runtime has custom elements like flex and highlight for quick alternatives to a div with utility classes:

<flex gap="5px">
    <p>this content</p>
    <p>will appear</p>
    <p>in a row</p>
</flex>

Lifecycle

Use onMount for DOM manipulation:

onMount(() => {
    document.querySelector('.class').innerHTML = "Hello World";
})

Signal-based Reactivity

Armature's reactivity system is designed to be familiar while leveraging the efficient Signals pattern.

useState() and useEffect()

These functions are similar to React hooks but with some key differences:

import { useState, useEffect } from 'armature-core'

export default () => {
    const [count, setCount] = useState<number>(0) // signal-based -> the getter must be called: count()

    useEffect(() => {
        console.log(`Count: ${count()}`)
    }) // optional: [dep] - a dependency array can be used for effects without signals
}

When state changes, only subscribers are updated, avoiding full component re-renders.

Note: Do not call the getter inside your JSX. They only need be called in your JavaScript, but the JSX Runtime calls the Signals for you.

    return (<p>{count}</p>)

API Layer

Armature provides a streamlined API layer for seamless client-server communication:

// ./src/api/users/[userid]/index.ts
import { t } from 'armature-core/api'

const users = [
    {
        id: '123',
        name: 'John Smith',
    }
]

export const GET = {
    handler: async (context: { request: { url: any; }; params: { userid: string } }) => {
        return new Response(JSON.stringify({ user: users.find(user => user.id === context.params.userid) }), {
            headers: { 'Content-Type': 'application/json' },
        });
    },
    document: {
        detail: {
            summary: 'GET request for users',
            tags: ['Users']
        }
    },
};

// ./src/api/users/index.ts
export const POST = {
    handler: async (context: { request: { url: any; }; body: { userid: string } }) => {
        return new Response(JSON.stringify({ user: users.find(user => user.id === context.body.userid) }), {
            headers: { 'Content-Type': 'application/json' },
        });
    },
    document: {
        body: t.Object(
            {
                userid: t.String()
            },
            {
                description: 'Expected a user ID'
            }
        ),
        detail: {
            summary: 'GET request for users',
            tags: ['Users']
        }
    },
};

// ./src/routes/user/[userid]/index.tsx
import { useState, useEffect } from 'armature-core'

export default async ({ userid }) => {
    const [user, setUser] = useState([])

    const { data: user } = await server.users({ userid }).get();
    setUser(user)

    return (
        <p>
            {user.name}
        </p>
    )
}

Automatic API Documentation

Scalar generates API documentation automatically based on your server-side functions.

Websocket

Using a ws folder, similar to your api folder, you can create websockets:

import { t } from "armature-core/api"
import type { ServerWebSocket } from "bun"

export default {
    body: t.Object({
        id: t.String(),
        message: t.String()
    }),
    response: t.Object({
        id: t.String(),
        message: t.String()
    }),
    open: (ws: ServerWebSocket) => {
        ws.send({ id: 'tanner', message: 'Hello, Developers!'})
    },
    message: (ws: ServerWebSocket, message: any) => {
        ws.send(message)
        if (message.message.toLowerCase().includes('hello')) {
            setTimeout(() => ws.send({ id: 'tanner', message: 'Thank you for giving Armature a try. Feedback is always welcome.' }), 1200);
        }
    },
    close: (ws: ServerWebSocket) => {
        ws.send({ id: 'tanner', message: 'This socket connection closed.'})
    },
    drain: (ws: ServerWebSocket, code: number, reason: string) => {},
}

Lightweight and Fast

Armature is designed for optimal performance:

  • Efficient Updates: Signal-based reactivity ensures minimal DOM updates
  • Fast SSR: Server-side rendering is optimized for quick initial loads

Missing Features

Features that are planned and not yet implemented:

  • Fine-grained HMR.
  • Better type safety for server API.
  • Better async component that doesn't block the page from loading.
  • CLI commands for building and running production builds (only the dev command exists)

Note: This framework was developed as an exploratory project to deepen understanding of rendering and runtimes. While not initially intended as a production-ready solution, it has proven effective and reliable for personal projects. I cannot advise using for production.

Collaboration and suggestions for improvements are welcome. Although full-time maintenance is currently not possible, continued development with sponsor support is an exciting prospect. With two months of dedicated effort invested, I am excited to see this framework evolve and reach its full potential.