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

@composerjs/virtual-file

v1.0.2

Published

file metadata

Downloads

9

Readme

Installation

  npm i -S @composerjs/virtual-file

Usage

  import fs from 'fs';
  import { VirtualFile } from '@composerjs/virtual-file';
  
  const file = VirtualFile.Factory({
    path: './package.json',
    content: fs.readFileSync('./package.json'),
    encoding: 'utf8',
    tags: ['npm-package']
  });
  
  console.info(file.toString());  // '{"name":"@composerjs/virtual-file"...
  console.info(file.mediaType);   // 'application/json'
  console.info(file.byteLength)   // 341 

API

VirtualFile.Factory({options})

Constructs a new instance of a VirtualFile where an instance represents a single file. All attributes of a VirtualFile are readonly so once constructed the properties cannot be changed.

options

options.path: string Required

Relative or absolute path location of the file.

options.content: Buffer Required

Buffer of the file.

options.encoding: string Optional

Encoding of the buffer.

Default: utf8

options.tags: string[]

An array of strings useful for adding additional metadata descriptions to an instance of VirtualFile.

Static Methods

VirtualFile.IsVirtualFile(file: VirtualFile): boolean

Returns true if the provided value is an instance of VirtualFile.

Instance Methods

file.toString(): string

Returns the Buffer as a string. Internally this uses StringDecoder, but only when encoding is set to utf8 or utf16

file.toJSON(): object

Called when an instance of VirtualFile has been JSON.stringify()'d. This returns a flat object of picked properties from the instance. Use file.toObject() instead as semantically it's more appropriate.

file.toObject(): object

Alias of file.toJSON().

file.extend(file: VirtualFile): void

Extends the instance with values from the provided VirtualFile instance.

file.clone(): VirtualFile

Returns a clone of the VirtualFile instance.

Instance Properties

file.content: Buffer

Buffer representation of the file content.

file.byteLength: number

Alias of this.content.byteLength. Returns the Buffer size in bytes.

Example: 341

file.encoding: string

file encoding string

Example: utf8, utf16, buffer

file.tags: string[]

An array of strings useful for adding additional context about the file the instance of VirtualFile is representing.

At the moment tags are just an array of strings to provide additional context for the file.

Example: \['package-json'\]

file.path: string

Complete file path supplied via constructor.

Example: dir/file.txt

file.name: string

Name of the file via via path.parse().

If path is a URL name will be a SLD i.e. google in google.com

Example: file

file.isURL: boolean

If the provided path is a valid URL this will be true.

Default: false

file.ext: string

File extension via path.parse().

If path is a URL ext will be TLD i.e. .com in google.com

Example: .txt

file.absolute: string

Absolute path via path.resolve().

Example: /home/user/dir/file.txt

file.dir: string

Directory of file via path.parse()

Example: /home/user/dir

file.base: string

Base name of file via path.parse()

Example: file.txt

file.root: string

Root path via path.parse()

Example: /

file.mediaType: string | undefined

Media Type (formerly called Mime Type) of the file.

This value may not be set.

Example: application/json

file.contentType: string | undefined

The Content-Type entity header value used in HTTP transactions.

This value may not be set.

Example: application/json

[Symbol.toStringTag]: string

Set by default in all instances. Calls to Object.prototype.toString.call(file) for instances of VirtualFile will return the constant string value.

Default: VirtualFile

nodejs.util.inspect.custom: string

Comparable to [Symbol.toStringTag] this symbol is used by node's util.inspect.