Installations
npm install browser-fs-access
Developer Guide
Typescript
Yes
Module System
ESM
Node Version
16.20.0
NPM Version
9.8.1
Score
99.5
Supply Chain
99.5
Quality
76.2
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
Download Statistics
Total Downloads
4,228,129
Last Day
5,735
Last Week
44,499
Last Month
204,002
Last Year
2,172,607
GitHub Statistics
1,421 Stars
281 Commits
85 Forks
18 Watching
2 Branches
35 Contributors
Package Meta Information
Latest Version
0.35.0
Package Id
browser-fs-access@0.35.0
Unpacked Size
45.65 kB
Size
13.33 kB
File Count
6
NPM Version
9.8.1
Node Version
16.20.0
Publised On
14 Sept 2023
Total Downloads
Cumulative downloads
Total Downloads
4,228,129
Last day
-34.9%
5,735
Compared to previous day
Last week
-3.4%
44,499
Compared to previous week
Last month
3.5%
204,002
Compared to previous month
Last year
50.2%
2,172,607
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Browser-FS-Access
This module allows you to easily use the
File System Access API on supporting browsers,
with a transparent fallback to the <input type="file">
and <a download>
legacy methods.
This library is a ponyfill.
Read more on the background of this module in my post Progressive Enhancement In the Age of Fugu APIs.
Live Demo
Try the library in your browser: https://googlechromelabs.github.io/browser-fs-access/demo/.
Installation
You can install the module with npm.
1npm install --save browser-fs-access
Usage Examples
The module feature-detects support for the File System Access API and only loads the actually relevant code.
Importing what you need
Import only the features that you need. In the code sample below, all features are loaded. The imported methods will use the File System Access API or a fallback implementation.
1import { 2 fileOpen, 3 directoryOpen, 4 fileSave, 5 supported, 6} from 'https://unpkg.com/browser-fs-access';
Feature detection
You can check supported
to see if the File System Access API is
supported.
1if (supported) { 2 console.log('Using the File System Access API.'); 3} else { 4 console.log('Using the fallback implementation.'); 5}
Opening a file
1const blob = await fileOpen({ 2 mimeTypes: ['image/*'], 3});
Opening multiple files
1const blobs = await fileOpen({
2 mimeTypes: ['image/*'],
3 multiple: true,
4});
Opening files of different MIME types
1const blobs = await fileOpen([ 2 { 3 description: 'Image files', 4 mimeTypes: ['image/jpg', 'image/png', 'image/gif', 'image/webp'], 5 extensions: ['.jpg', '.jpeg', '.png', '.gif', '.webp'], 6 multiple: true, 7 }, 8 { 9 description: 'Text files', 10 mimeTypes: ['text/*'], 11 extensions: ['.txt'], 12 }, 13]);
Opening all files in a directory
Optionally, you can recursively include subdirectories.
1const blobsInDirectory = await directoryOpen({ 2 recursive: true, 3});
Saving a file
1await fileSave(blob, {
2 fileName: 'Untitled.png',
3 extensions: ['.png'],
4});
Saving a Response
that will be streamed
1const response = await fetch('foo.png'); 2await fileSave(response, { 3 fileName: 'foo.png', 4 extensions: ['.png'], 5});
Saving a Promise<Blob>
that will be streamed.
No need to await
the Blob
to be created.
1const blob = createBlobAsyncWhichMightTakeLonger(someData);
2await fileSave(blob, {
3 fileName: 'Untitled.png',
4 extensions: ['.png'],
5});
API Documentation
Opening files:
1// Options are optional. You can pass an array of options, too. 2const options = { 3 // List of allowed MIME types, defaults to `*/*`. 4 mimeTypes: ['image/*'], 5 // List of allowed file extensions (with leading '.'), defaults to `''`. 6 extensions: ['.png', '.jpg', '.jpeg', '.webp'], 7 // Set to `true` for allowing multiple files, defaults to `false`. 8 multiple: true, 9 // Textual description for file dialog , defaults to `''`. 10 description: 'Image files', 11 // Suggested directory in which the file picker opens. A well-known directory, or a file or directory handle. 12 startIn: 'downloads', 13 // By specifying an ID, the user agent can remember different directories for different IDs. 14 id: 'projects', 15 // Include an option to not apply any filter in the file picker, defaults to `false`. 16 excludeAcceptAllOption: true, 17}; 18 19const blobs = await fileOpen(options);
Opening directories:
1// Options are optional. 2const options = { 3 // Set to `true` to recursively open files in all subdirectories, defaults to `false`. 4 recursive: true, 5 // Open the directory with `"read"` or `"readwrite"` permission, defaults to `"read"`. 6 mode: 7 // Suggested directory in which the file picker opens. A well-known directory, or a file or directory handle. 8 startIn: 'downloads', 9 // By specifying an ID, the user agent can remember different directories for different IDs. 10 id: 'projects', 11 // Callback to determine whether a directory should be entered, return `true` to skip. 12 skipDirectory: (entry) => entry.name[0] === '.', 13}; 14 15const blobs = await directoryOpen(options);
The module also polyfills a webkitRelativePath
property on returned files in a consistent way, regardless of the underlying implementation.
Saving files:
1// Options are optional. You can pass an array of options, too. 2const options = { 3 // Suggested file name to use, defaults to `''`. 4 fileName: 'Untitled.txt', 5 // Suggested file extensions (with leading '.'), defaults to `''`. 6 extensions: ['.txt'], 7 // Suggested directory in which the file picker opens. A well-known directory, or a file or directory handle. 8 startIn: 'downloads', 9 // By specifying an ID, the user agent can remember different directories for different IDs. 10 id: 'projects', 11 // Include an option to not apply any filter in the file picker, defaults to `false`. 12 excludeAcceptAllOption: true, 13}; 14 15// Optional file handle to save back to an existing file. 16// This will only work with the File System Access API. 17// Get a `FileHandle` from the `handle` property of the `Blob` 18// you receive from `fileOpen()` (this is non-standard). 19const existingHandle = previouslyOpenedBlob.handle; 20 21// Optional flag to determine whether to throw (rather than open a new file 22// save dialog) when `existingHandle` is no longer good, for example, because 23// the underlying file was deleted. Defaults to `false`. 24const throwIfExistingHandleNotGood = true; 25 26// `blobOrPromiseBlobOrResponse` is a `Blob`, a `Promise<Blob>`, or a `Response`. 27await fileSave( 28 blobOrResponseOrPromiseBlob, 29 options, 30 existingHandle, 31 throwIfExistingHandleNotGood 32);
File operations and exceptions
The File System Access API supports exceptions, so apps can throw when problems occur (permissions
not granted, out of disk space,…), or when the user cancels the dialog. The legacy methods,
unfortunately, do not support exceptions (albeit there is an
HTML issue open for this request). If your app depends
on exceptions, see the file
index.d.ts
for the
documentation of the legacySetup
parameter.
Browser-FS-Access in Action
You can see the module in action in the Excalidraw drawing app.
It also powers the SVGcode app that converts raster images to SVGs.
Alternatives
A similar, but more extensive library called native-file-system-adapter is provided by @jimmywarting.
Ecosystem
If you are looking for a similar solution for dragging and dropping of files, check out @placemarkio/flat-drop-files.
Acknowledgements
Thanks to @developit
for improving the dynamic module loading
and @dwelle for the helpful feedback,
issue reports, and the Windows build fix.
Directory operations were made consistent regarding webkitRelativePath
and parallelized and sped up significantly by
@RReverser.
The TypeScript type annotations were initially provided by
@nanaian.
Dealing correctly with cross-origin iframes was contributed by
@nikhilbghodke and
@kbariotis.
The exception handling of the legacy methods was contributed by
@jmrog.
The streams and blob saving was improved by @tmcw.
License and Note
Apache 2.0.
This is not an official Google product.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: Apache License 2.0: LICENSE:0
Reason
Found 4/25 approved changesets -- score normalized to 1
Reason
SAST tool is not run on all commits -- score normalized to 1
Details
- Warn: 1 commits out of 9 are checked with a SAST tool
Reason
9 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-jchw-25xp-jwwc
- Warn: Project is vulnerable to: GHSA-cxjh-pqwp-8mfp
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-7fh5-64p2-3v2j
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/main.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:10: update your workflow using https://app.stepsecurity.io/secureworkflow/GoogleChromeLabs/browser-fs-access/main.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/main.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/GoogleChromeLabs/browser-fs-access/main.yml/main?enable=pin
- Info: 0 out of 1 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Score
2.8
/10
Last Scanned on 2024-12-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More