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

koa-transfer-file

v0.1.2

Published

Transfer file stream without storing files

Downloads

23

Readme

koa-transfer-file

npm version Build Status codecov

Package under development. Please lock the specific version in package.json or package-lock.json.

Transfer file stream without storing files.

This package is mainly used for the middle layer.

Featured

  • option onDisk: (boolean, default true) It determines whether disk I/O is being used during transmission. Converting Stream to Buffer by array is unsafe when transferring big files. To avoid this problem, using file stream as a default. The temp file will be deleted after new Readable stream is built.

  • maintain files' name: When sending files to another server, filenames will be changed into tmpName because of the new readable stream. Solved by adding property name to the readable stream, due to the package form-data will name the file by filestream.name or filestream.path when appending data.

  • option appendField: (boolean, default false) Append files to ctx.request.body in order to keep it(formData) the same as before the request was sent.

  • option appendFile: (boolean, default true, deprecative) Highly recommanded false. Append all files in an array to ctx.request.body with fieldname _files. The difference between ctx.request.body._files and ctx.request.files is that _files has been formatted for the puropse of transferring directly by request.

    The default value is only for compatibility with the old versions temporarily. It's innocent when you don't care about files' fieldname.

Install

npm install koa-transfer-file

Usage

The options almost same as busboy.

const Koa = require('koa');
const transfer = require('koa-transfer-file');

const app = new Koa();

const options = {
  onDisk: true, // (boolean, default true)
  limits: {
    fileSize: 1024 * 5
  }
}

app.use(transfer(options));

Transfer

Transfer formData by request directly.

const request = require('request-promise');

app.use((ctx, next) => {
  const formData = ctx.request.body;
  request({
    method: 'POST',
    uri: 'http://localhost:3000',
    formData
  });
  next();
});

Or configure the formData's value manually when opts.onDisk=false.

const formData = {};

formData[file.fieldname] = {
  value: file.value,
  options: {
    filename: file.filename,
    contentType: file.mimetype
  }
}

Save

For each file of ctx.request.files:

  1. By default, file is a readable stream.
const rs = file;
  1. When opts.onDisk is set to false, file.value contains a Buffer.
const { Readable } = require('stream');

const rs = new Readable();
rs._read = () => {};

rs.push(file.value);

Then save the readable stream to the file.

rs.pipe(fs.createWriteStream(file.filename))
  .on('finish', () => console.log('saved'));;

API

Opts

Please refer to Featured for description of the options.

File information

Files (<object[]>) can be got from ctx.request.files.

The properties of each file are as shown below.

Certain Properties

|Key|Desc| |---|---| |filename|original name of the file| |fieldname|field name specified in the form | |encoding|-| |mimetype|-| |truncated|stream is truncated or not (file reached limit size)|

opts.onDisk = true

|Key|Desc| |---|---| |name|alias of filename|

opts.onDisk = false

|Key|Desc| |---|---| |value|file data in buffer|