Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
@juzi/file-box
Advanced tools
Pack a File into Box for easy move/transfer between servers no matter of where it is.(local path, remote url, or cloud storage)
FileBox is a virtual container for packing a file data into it for future read, and easily transport between servers with the least payload, no mater than where it is (local path, remote url, or cloud storage).
Currently the FileBox supports almost all kinds of the data input/output methods/formats:
File Type | Pack Method | Unpack Method | Description |
---|---|---|---|
Local File | fromFile() | toFile() | Local file in file system |
Remote URL | fromUrl() | toUrl() (TBW) | Remote file in a HTTP/HTTPS URL |
Buffer | fromBuffer() | toBuffer() | JavaScript Buffer |
Stream | fromStream() | toStream() | JavaScript Stream |
Base64 | fromBase64() | toBase64() | Base64 data |
DataURL | fromDataURL() | toDataURL() | DataURL data |
QRCode | fromQRCode() | toQRCode() | QR Code Image Decode/Encode |
UUID | fromUuid() | toUuid() | UUID by loader/saver helper functions |
JSON | fromJSON() | toJSON() | Serialize/Deserialize FileBox |
The following example demos:
import { FileBox } from 'file-box'
/**
* 0. Load local file
*/
const fileBox0 = FileBox.fromFile('/tmp/file.jpg')
/**
* 1. Save URL to File
*/
const fileBox1 = FileBox.fromUrl(
'https://huan.github.io/file-box/images/file-box-logo.jpg',
'logo.jpg',
)
fileBox1.toFile('/tmp/file-box-logo.jpg')
/**
* 2. Convert Buffer to Stream
*/
import fs from 'fs'
const fileBox2 = FileBox.fromBuffer(
Buffer.from('world'),
'hello.txt',
)
const writeStream = fs.createWriteStream('/tmp/hello.txt')
fileBox2.pipe(writeStream)
/**
* 3. Pack Base64, Unpack to DataURL
*/
const fileBox3 = FileBox.fromBase64('d29ybGQK', 'hello.txt')
fileBox3.toDataURL()
.then(console.log)
// Output: data:text/plain;base64,d29ybGQK
fromFile(filePath: string, name?: string, md5?: string): FileBox
Alias: fromLocal()
About optional arguments:
name: filename, if not passed, will be parsed from file path, url, etc.
md5: file md5 code, only for checking purposes and will not be computed from file.
const fileBox = FileBox.fromLocal('/tmp/test.txt')
fromUrl(url: string, options?: {headers?: HTTP.OutgoingHttpHeaders, name?: string, size?: string, md5?: string}): FileBox
Alias: fromRemote()
const fileBox = FileBox.fromUrl(
'https://huan.github.io/file-box/images/file-box-logo.jpg',
{name: 'logo.jpg'},
)
fromStream(stream: Readable, name?: string, md5?: string): FileBox
Will be named 'stream.dat' if no name is provided.
const fileBox = FileBox.fromStream(res, '/tmp/download.zip')
fromBuffer(buffer: Buffer, name?: string, md5?: string): FileBox
Will be named 'buffer.dat' if no name is provided.
const fileBox = FileBox.fromBuffer(buf, '/tmp/download.zip')
FileBox.fromBase64(base64: string, name?: string, md5?: string): FileBox
Decoded a base64 encoded file data. Will be named 'base64.dat' if no name is provided.
const fileBox = FileBox.fromBase64('d29ybGQK', 'hello.txt')
fileBox.toFile()
FileBox.fromDataURL(dataUrl: string, name?: string, md5?: string): FileBox
Decoded a DataURL data. Will be named 'data-url.dat' if no name is provided.
const fileBox = FileBox.fromDataURL('data:text/plain;base64,d29ybGQK', 'hello.txt')
fileBox.toFile()
FileBox.fromJSON()
Restore a FileBox.toJSON()
text string back to a FileBox instance.
const restoredFileBox = FileBox.fromJSON(jsonText)
FileBox.fromQRCode(qrCodeValue: string, md5?: string)
Get a FileBox instance that represent a QR Code value.
const fileBox = FileBox.fromQRCode('https://github.com')
fileBox.toFile('qrcode.png')
FileBox.fromUuid(uuid: string, options?: {name?: string, size?: string, md5?: string})
Load a FileBox from a UUID. Will be named ${uuid}.dat
if no name is provided.
See: FileBox.setUuidLoader()
toFile(name?: string): Promise<void>
Save file to current work path(cwd) of the local file system with the default name
.
if name
specified with a full path, then will use the speficied file name instead.
const fileBox = FileBox.fromRemote(
'https://huan.github.io/file-box/images/file-box-logo.jpg',
)
await fileBox.toFile('/tmp/logo.jpg')
toStream(): Readable
Get the stream of file data.
const fileBox = FileBox.fromRemote(
'https://huan.github.io/file-box/images/file-box-logo.jpg',
)
const readableStream = fileBox.toStream()
pipe(destination: Writable): Promise<void>
Pipe to a writable stream.
const fileBox = FileBox.fromRemote(
'https://huan.github.io/file-box/images/file-box-logo.jpg',
)
const writableStream = fs.createWritable('/tmp/logo.jpg')
fileBox.pipe(writableStream)
toBase64(): Promise<string>
Get the base64 data of file.
const fileBox = FileBox.fromRemote(
'https://huan.github.io/file-box/images/file-box-logo.jpg',
)
const base64Text = await fileBox.toBase64()
console.log(base64Text) // Output: the base64 encoded data of the file
toJSON(): string
Get the JSON.stringify
-ed text.
Not Implement Yet: Working In Progress...
const fileBox = FileBox.fromRemote(
'https://huan.github.io/file-box/images/file-box-logo.jpg',
)
const jsonText1 = fileBox.toJSON()
const jsonText2 = JSON.stringify(fileBox)
assert(jsonText1 === jsonText2)
console.log(jsonText1) // Output: the stringified data of the fileBox
const restoredFileBox = fileBox.fromJSON(jsonText1)
restoredFileBox.toFile('/tmp/file-box-logo.jpg')
toDataURL(): Promise<string>
Get the DataURL of the file.
const fileBox = FileBox.fromFile('tests/fixtures/hello.txt')
const dataUrl = await fileBox.toDataURL()
console.log(dataUrl) // Output: data:text/plain;base64,d29ybGQK'
toBuffer(): Promise<Buffer>
Get the Buffer of the file.
const fileBox = FileBox.fromFile('tests/fixtures/hello.txt')
const buffer = await fileBox.toBuffer()
console.log(buffer.toString()) // Output: world
toQRCode(): Promise<string>
Decode the QR Code value from the file.
const fileBox = FileBox.fromFile('qrcode.jpg')
const qrCodeValue = await fileBox.toQRCode()
console.log(`QR Code decoded value is: "${qrCodeValue}"`)
// Output: QR Code decoded value is: "https://github.com"
toUuid(): Promise<string>
Save the FileBox to a UUID file and return the UUID.
See: FileBox.setUuidSaver()
toJSON(): string
Encode a FileBox instance to JSON string so that we can transfer the FileBox on the wire.
const fileBox = FileBox.fromBase64('RmlsZUJveEJhc2U2NAo=')
const jsonText = JSON.stringify(fileBox)
// the above code equals to the following line of code:
// const jsonText = fileBox.toJSON()
// we will get the serialized data for this FileBox:
console.log(jsonText)
// Output: {"name":"qrcode.png","metadata":{},"boxType":1,"base64":"RmlsZUJveEJhc2U2NAo="}
// restore our fleBox:
// const newFileBox = FileBox.fromJSON(jsonText)
Because we want to enable the JSON.stringify(fileBox)
, which will call fileBox.toJSON()
, so the toJSON()
can not be async
, which means we can only support limited FileBoxType(s):
For other types like FileBoxType.File
, FileBoxType.Buffer
, FileBoxType.Stream
, etc, we need to transform them to FileBoxType.Base64
before we call toJSON
:
const fileBoxLazy = FileBox.fromFile('./test.txt')
const base64 = await fileBoxLazy.toBase64()
const fileBox = FileBox.fromBase64(base64, 'test.txt')
// fileBox will be serializable because it do not need async operations
const jsonText = JSON.stringify(fileBox)
console.log(jsonText)
name
File name of the file in the box
const fileBox = FileBox.fromRemote(
'https://huan.github.io/file-box/images/file-box-logo.jpg',
)
console.log(fileBox.name) // Output: file-box-logo.jpg
md5
File md5 of the file in the box. The value is set by user, not computed from file.
const fileBox = FileBox.fromUrl(
'https://huan.github.io/file-box/images/file-box-logo.jpg',
{md5: 'computed-md5-string'}
)
console.log(fileBox.md5) // Output: computed-md5-string
metadata: Metadata { [key: string]: any }
Metadata for the file in the box. This value can only be assigned once, and will be immutable afterwards, all following assign or modify actions on metadata
will throw errors
const fileBox = FileBox.fromRemote(
'https://huan.github.io/file-box/images/file-box-logo.jpg',
)
fileBox.metadata = {
author : 'huan',
githubRepo : 'https://github.com/huan/file-box',
}
console.log(fileBox.metadata) // Output: { author: 'huan', githubRepo: 'https://github.com/huan/file-box' }
fileBox.metadata.author = 'Tank' // Will throw exception
version(): string
Version of the FileBox
toJSON(): string
Serialize FileBox metadata to JSON.
ready(): Promise<void>
Update the necessary internal data and make everything ready for use.
syncRemoteName(): Promise<void>
Sync the filename with the HTTP Response Header
HTTP Header Example:
Content-Disposition: attachment; filename="filename.ext"
type: FileBoxType
Return the type of the current FileBox instance.
The currently supported types are defined at file-box-type.ts as the following demonstrated:
enum FileBoxType {
Unknown = 0,
Base64 = 1,
Url = 2,
QRCode = 3,
Buffer = 4,
File = 5,
Stream = 6,
Uuid = 7,
}
FileBox.setUuidLoader(loader: UuidLoader): void
Required by static method FileBox.fromUuid()
class FileBoxUuid extends FileBox {}
const loader: UuidLoader = async (uuid: string) => {
const stream = new PassThrough()
stream.end('hello, world!')
return stream
})
FileBoxUuid.setUuidLoader(loader)
const fileBox = FileBoxUuid.fromUuid('12345678-1234-1234-1234-123456789012', 'test.txt')
await fileBox.toFile('test.txt')
The UuidLoader
is a function that takes a UUID and return a readable stream.
type UuidLoader = (this: FileBox, uuid: string) => Readable
FileBox.setUuidSaver(saver: UuidSaver): void
Required by instance method fileBox.toUuid()
class FileBoxUuid extends FileBox {}
const saver: UuidSaver = async (stream: Readable) => {
// save the stream and get uuid
return '12345678-1234-1234-1234-123456789012'
})
FileBoxUuid.setUuidSaver(saver)
const fileBox = FileBoxUuid.fromFile('test.txt')
const uuid = await fileBox.toUuid()
The UuidSaver
is a function that takes a readable stream and return a UUID promise.
type UuidSaver = (this: FileBox, stream: Readable) => Promise<string>
size
The file box size in bytes. (-1
means unknown)
It is not the size of the target (boxed) file itself.
For example:
const fileBox = FileBox.fromUrl('http://example.com/image.png')
console.log(fileBox.size)
// > 20 <- this is the length of the URL string
remoteSize
The remote file size in bytes. (-1
or undefined
means unknown)
For example:
const fileBox = FileBox.fromUrl('http://example.com/image.png')
await fileBox.ready()
console.log(fileBox.remoteSize)
// > 102400 <- this is the size of the remote image.png
Environment variables can be used to control some behavior.
FILEBOX_HTTP_TIMEOUT
[default=60000] Socket idle timeout when FileBox downloads data from URL. For example, when the network is temporarily interrupted, the request will be considered as failed after waiting for a specified time.Node.js Documentation > URL Strings and URL Objects
┌─────────────────────────────────────────────────────────────────────────────────────────────┐
│ href │
├──────────┬──┬─────────────────────┬─────────────────────┬───────────────────────────┬───────┤
│ protocol │ │ auth │ host │ path │ hash │
│ │ │ ├──────────────┬──────┼──────────┬────────────────┤ │
│ │ │ │ hostname │ port │ pathname │ search │ │
│ │ │ │ │ │ ├─┬──────────────┤ │
│ │ │ │ │ │ │ │ query │ │
" https: // user : pass @ sub.host.com : 8080 /p/a/t/h ? query=string #hash "
│ │ │ │ │ hostname │ port │ │ │ │
│ │ │ │ ├──────────────┴──────┤ │ │ │
│ protocol │ │ username │ password │ host │ │ │ │
├──────────┴──┼──────────┴──────────┼─────────────────────┤ │ │ │
│ origin │ │ origin │ pathname │ search │ hash │
├─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────────┴───────┤
│ href │
└─────────────────────────────────────────────────────────────────────────────────────────────┘
Node.js Documentation > path.parse(path)
┌─────────────────────┬────────────┐
│ dir │ base │
├──────┬ ├──────┬─────┤
│ root │ │ name │ ext │
" / home/user/dir / file .txt "
└──────┴──────────────┴──────┴─────┘
FILEBOX_HTTP_TIMEOUT
can be set by user. see Environments. (#80, by @binsee)fileBox.md5
can be set by user. This filed is for the receiver of the filebox to check, and it is not computed from the file.fileBox.size
will be serialized to/from JSON, and present the Content-Length
of the file. (-1
means unknown)mimeType
has been renamed to mediaType
, and added to the FileBoxInterface
FileBox.fromUuid()
and FileBox.toUuid()
size
property to return the size of the file. (-1
means unknown)remoteSize
property to present the remote size of the file (if applicable, -1
means unknown)UniformResourceNameRegistry
class for providing a production-ready basic UUID management tool.FileBoxInterface
, FileBox.isInstance()
, and FileBox.isInterface()
Breaking changes:
toJSON
format renamed boxType
to type
type()
has been changed to type
version()
has been changed to version
fileBox.type()
to return the FileBoxType
of a FileBox. (wechaty/wechaty#1918)Readable
to stream.Readable
for better compatibility. (Jun 27, 2020)chunkerTransformStream
to toStream
(#44)Add support to JSON.stringify()
(#25):
FileBox.fromJSON()
- Static method for deserializationfileBox.toJSON()
- Instance method for serializationFileBox.fromQRCode()
and FileBox.toQRCode()
fromBase64()
, fromDataURL()
toBuffer()
, toBase64()
and toDataURL()
to get the Buffer and BASE64 encoded file datametadata
property to store additional information. (#3)headers
option for fromRemote()
methodInitial version.
This module is inspired by https://github.com/gulpjs/vinyl and https://github.com/DefinitelyTyped/DefinitelyTyped/pull/12368 when I need a virtual File module for my Chatie project.
Huan LI (李卓桓), Microsoft AI MVP, zixia@zixia.net
FAQs
Pack a File into Box for easy move/transfer between servers no matter of where it is.(local path, remote url, or cloud storage)
We found that @juzi/file-box demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 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.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.