
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
1.0.14 version:The XFile instance could be initiated from Buffer and now could be used by Node application.
In the isExtension() method now can be passed the Array<string> to validate extension with array of allowed values
Fixed bug with build in Angular application
More cases covered with new tests
$ npm install -s ts-x-file
export class FileService {
...
loadFile(): XFile {
const buffer = fs.readFileSync('file.js');
return new XFile(buffer);
}
}
<input type="file" (change)="onFileChange($event)"/>
onFileChange(event: any): void {
const input = event.tartget as HTMLInputElement;
// FileList initiation
const xFile = new XFile(input.files);
// Or you can pass File
const xFile = new XFile(input.files.item(0));
}
You can pass a parameter to constructor to change default unit:
import { XFile } from "ts-x-file";
onFileChange(event: any): void {
const xFile = new XFile(event.target.files, FileSizeUnits.MB);
}
Then if you request a size of file it will be returned in this unit. By default, unit is KiloBytes.
isLessThan(limit: number, unit?: FileSizeUnits): boolean;
import { XFile, FileSizeUnits } from "ts-x-file";
const xFile = new XFile(event.target.files);
xFile.isLessThan(5000);
By default limit parameter is calculated as Bytes (if you did not change it through the constructor), you can change it by passing FileSizeUnit:
import { FileSizeUnits } from "ts-x-file";
const xFile = new XFile(event.target.files);
xFile.isLessThan(5, FileSizeUnits.B);
xFile.isLessThan(5, FileSizeUnits.KB);
xFile.isLessThan(5, FileSizeUnits.MB);
xFile.isLessThan(5, FileSizeUnits.GB);
This way it will ignore the default unit.
size(round?: boolean = false, unit?: FileSizeUnits): number;
const fileSize = xFile.size();
By default this method won't round size number, you can do this by passing true on the first place parameter:
const fileSize = xFile.size(true);
The unit of returned value could be ignored, as well, if you pass it as second parameter:
import { FileSizeUnits } from "ts-x-file";
const fileSizeBytes = xFile.size(true, FileSizeUnits.B);
const fileSizeKiloBytes = xFile.size(true, FileSizeUnits.KB);
const fileSizeMegaBytes = xFile.size(true, FileSizeUnits.MB);
const fileSizeGigaBytes = xFile.size(true, FileSizeUnits.GB);
getExtension(): string;
const extension = xFile.getExtension();
you are able to check file extension by using method:
isExtension(extension: string | string[]): boolean;
const isPdf = xFile.isExtension('pdf');
of you can pass the array of allowed values:
const isPdf = xFile.isExtension(['jpg', 'JPEG']);
Note: the values are not case sensitive.
Getting name of the file could be done by simply calling name getter:
console.log(xFile.name);
To set name property of file you can follow:
changeName(name: string, skipExt?: boolean): void;
const xFile = new XFile(event.target.files) // ex: react.js
console.log(xFile.name) // out: react.js
xFile.changeName('angular.js');
console.log(xFile.name) // out: angular.js
If you want to change extension of file, you just need to pass it as name parameter and set skipExt parameter to true:
xFile.changeName('angular.ts', true);
console.log(xFile.name) // out: angular.ts
// If you skip to set `skipExt` parameter you should get something like this:
xFile.changeName('angular.ts');
console.log(xFile.name) // out: angular.ts.js
File instace or Base64Somehow, on the end you will need an instance or Base64 string, so this is possible with following methods:
getFile(): File;
getBase64(): Promise<string>;
File instance example:
const file: File = xFile.getFile();
Base64 string example:
const urlString = xFile.getBase64().then(console.log) // data:image/png;base64,...
File instance exists:fileExists(): boolean;
This could be used to validate:
if (xFile.fileExists()) {
...
}
Run tests with following command:
$ jest
MIT
FAQs
A smart way to manage your File.
We found that ts-x-file demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.