
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
@xanthous/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 |
JSON | fromJSON() (TBW) | toJSON() (TBW) | Serialize/Deserialize FileBox |
The following example demos:
import { FileBox } from 'file-box'
/**
* 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 * as 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): FileBox
Alias: fromLocal()
const fileBox = FileBox.fromLocal('/tmp/test.txt')
fromUrl(url: string, name?: string, headers?: http.OutgoingHttpHeaders): FileBox
Alais: fromRemote()
const fileBox = FileBox.fromUrl(
'https://huan.github.io/file-box/images/file-box-logo.jpg',
'logo.jpg',
)
fromStream(stream: NoddeJS.ReadableStream, name: string): FileBox
const fileBox = FileBox.fromStream(res, '/tmp/download.zip')
fromBuffer(buffer: Buffer, name: string): FileBox
const fileBox = FileBox.fromBuffer(buf, '/tmp/download.zip')
FileBox.fromBase64(base64: string, name: string): FileBox
Decoded a base64 encoded file data.
const fileBox = FileBox.fromBase64('d29ybGQK', 'hello.txt')
fileBox.toFile()
FileBox.fromDataURL(dataUrl: string, name: string): FileBox
Decoded a DataURL data.
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.
WIP: Not Implement Yet
const restoredFileBox = FileBox.fromJSON(jsonText)
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')
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
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
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://zixia.github.io/file-box/images/file-box-logo.jpg',
)
fileBox.metadata = {
author : 'zixia',
githubRepo : 'https://github.com/zixia/file-box',
}
console.log(fileBox.metadata) // Output: { author: 'zixia', githubRepo: 'https://github.com/zixia/file-box' }
fileBox.metadata.author = 'Thanos' // Will throw exception
version(): string
Version of the FileBox
toJSON(): string
Serialize FileBox metadata to JSON.
To be implemented.
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"
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 "
└──────┴──────────────┴──────┴─────┘
fromBase64()
, fromDataURL()
toBuffer()
, toBase64()
and toDataURL()
to get the Buffer and BASE64 encoded file datametadata
property to store additional informations. (#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.
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)
The npm package @xanthous/file-box receives a total of 0 weekly downloads. As such, @xanthous/file-box popularity was classified as not popular.
We found that @xanthous/file-box demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 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
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.