Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
@travetto/asset
Advanced tools
Install: @travetto/asset
npm install @travetto/asset
# or
yarn add @travetto/asset
The asset module requires an Streaming to provide functionality for reading and writing streams. You can use any existing providers to serve as your Streaming, or you can roll your own.
Install: provider
npm install @travetto/model-{provider}
# or
yarn add @travetto/model-{provider}
Currently, the following are packages that provide Streaming support:
Code: Configuration Methods
import { InjectableFactory } from '@travetto/di';
import { ModelStreamSupport } from '@travetto/model';
import { AssetModelⲐ, AssetService } from '@travetto/asset';
class SymbolBasedConfiguration {
@InjectableFactory(AssetModelⲐ)
static getAssetModelService(service: ModelStreamSupport) {
return service;
}
}
/* OR */
class FullConfiguration {
@InjectableFactory()
static getAssetService(service: ModelStreamSupport) {
return new AssetService(service);
}
}
Reading of and writing assets uses the AssetService. Below you can see an example dealing with a user's profile image.
Code: User Profile Images
import { ModelCrudSupport } from '@travetto/model';
import { AssetService, Asset } from '@travetto/asset';
import { User } from './user';
export class UserProfileService {
constructor(
public asset: AssetService,
public model: ModelCrudSupport
) { }
async saveProfileImage(userId: string, image: Asset) {
const path = await this.asset.upsert(image);
const user = await this.model.get(User, userId);
user.profileImage = path;
await this.model.update(User, user);
}
async getProfileImage(userId: string) {
const user = await this.model.get(User, userId);
return await this.asset.get(user.profileImage);
}
}
By default, the assets are stored by path, as specified in the Asset object. This is standard, and expected, but some finer control may be desired. In addition to standard naming, the module also supports naming by hash, to prevent duplicate storage of the same files with different hashes. This is generally useful when surfacing a lot of public (within the application) user-generated content.
The underlying contract for a AssetNamingStrategy looks like:
Code: AssetNamingStrategy
export interface AssetNamingStrategy {
readonly prefix: string;
/**
* Produce a path for a given asset
* @param asset Get path from an asset
*/
resolve(asset: StreamMeta): string;
}
By extending this, and making it @Injectable, the naming strategy will become the default for the system.
In addition to reading and writing, you can also retrieve information on the saved asset, including basic information, and additional meta data. The structure of the Asset looks like:
Code: Asset Structure
import { Readable } from 'node:stream';
import { StreamMeta } from '@travetto/model';
/**
* An asset, for storage
*
* @concrete ./internal/types#AssetImpl
*/
export interface Asset extends StreamMeta {
source: Readable | string | Buffer;
localFile?: string;
}
/**
* An asset response
*/
export interface AssetResponse extends StreamMeta {
stream(): Readable;
/**
* Response byte range, inclusive
*/
range?: [start: number, end: number];
}
To get the asset information, you would call:
Code: Fetching Asset Info
import { UserProfileService } from './user-profile';
import { User } from './user';
export class UserProfileTagService extends UserProfileService {
async getImageContentType(userId: string) {
const user = await this.model.get(User, userId);
const info = await this.asset.describe(user.profileImage);
return info.contentType; // Return image content type
}
}
FAQs
Modular library for storing and retrieving binary assets
The npm package @travetto/asset receives a total of 49 weekly downloads. As such, @travetto/asset popularity was classified as not popular.
We found that @travetto/asset demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.