Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@travetto/asset

Package Overview
Dependencies
Maintainers
0
Versions
310
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@travetto/asset

Modular library for storing and retrieving binary assets

  • 4.1.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

Asset

Modular library for storing and retrieving binary assets

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);
  }
}

Naming Strategies

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.

Advanced Usage

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
  }
}

Keywords

FAQs

Package last updated on 20 Jun 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc