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

@affinity-lab/storm-storage-server-sveltekit

v1.0.207

Published

Storm Storage file server for sveltekit

Downloads

406

Readme

Storm Storage Server for SvelteKit

This package will help to resolve the paths of the files stored in the Storage plugin of Storm ORM. It will automatically generate the URL of the files, and the URL of the images.

Usage

Have a project setup with SvelteKit, and Storm ORM with the Storage plugin.

Set your Storage paths to the static folder of your SvelteKit project.

.env

PATH_FILES="static/file"
PATH_IMG="static/img"

Add the image resolver to the routes.

src/routes/img/[image]/+serevr.ts

import {namedImgDimensions} from "$lib/named-img";
import {services} from "$lib/services";
import {imgServerFactory} from "@affinity-lab/storm-storage-server-sveltekit/server";
import {keysOfNamedImageDimensions} from "./named-image-dimensions";

export const GET = imgServerFactory(
	services.config.storage.imgPath, // Path to the images
	services.config.storage.filePath, // Path to the files
	{
		namedImageDimensions: namedImageDimensions, // Named image dimensions
		allowFree: false, // Allow free image dimensions for the images
		fileNameParam: "image" // The name of the parameter that will be used to get the image name
	}
);

You should always set the allowFree option to false in production to avoid security issues.

Create the named image dimensions.

src/lib/named-img.ts

import {keysOfNamedImageDimensions} from "@affinity-lab/storm-storage-server-sveltekit";

export const namedImgDimensions = {
	user: {
		avatar: {
			mini: {width: 48, height: 48},
			big: {width: 192, height: 192}
		}
	}
}

export const namedImg = keysOfNamedImageDimensions(namedImgDimensions); // this will create the named image dimensions map for client side

Configure Collection Handlers in the +layout.ts

src/routes/+layout.svelte

import {ImageCollectionHandler} from "@affinity-lab/storm-storage-server-sveltekit";
import {FileCollectionHandler} from "@affinity-lab/storm-storage-server-sveltekit";

FileCollectionHandler.url = "/file";
ImageCollectionHandler.url = "/img";

/file and /img are the default settings, if you stick to the default settings, you can skip this step.

Use the handlers in the components.

Any component or page. eg: src/routes/+page.svelte


<script lang="ts">
	import {namedImg} from "$lib/named-img";
	import {ImageCollectionHandler} from "@affinity-lab/storm-storage-server-sveltekit";

	let {data} = $props();
</script>

{#each ImageCollectionHandler.create(data.user?.avatar).files as file, i}

	You can use the file URL directly:
	<a href={file.url} target="_blank">{file.name}</a>

	You have access to the metadata of the file:
	{file.metadata.width}

	You can use the img property's size, height, and width methods
	to generate the resized image URL. This is only available when
	the allowFree option is set to true in the imgServerFactory.
	<img src={file.img.size(50,50)} srcset="{file.img(50,50).x2} 2x">

	You can use the named method to generate the named image URL:
	<img src={file.img.named(namedImg.user.avatar.mini)}>

	You can set the format of the image. The default is "webp".
	When the as() function called without any arguments, it will use
	the original format of the image.
	<img src={file.img.named(namedImg.user.avatar.mini).as("png")}>

	You can use the x2, x3, x4 properties to generate the 2x, 3x, 4x sized
	images for high-resolution displays.
	<img src={file.img.named(namedImg.user.avatar.mini)}
	     srcset="{file.img.named(namedImg.user.avatar.mini).x2} 2x">

	You can use the srcset method to generate the srcset attribute for the image
	up to the specified resolution (max 4x, default: 3x).
	<img src={file.img.named(namedImg.user.avatar.mini)}
	     srcset="{file.img.named(namedImg.user.avatar.mini).srcset(2)}">

{/each}

Handle the content of the static/img folder.

All images in the static/img folder will be generated automatically by the server. You can delete those images from the folder regularly, to save space, and the server will generate them again, when the image is requested.

For example run a cron job to delete the images older than 1 day.