Skip to content

Commit

Permalink
Remove default legacy rejection handling
Browse files Browse the repository at this point in the history
Enable SSR
Fixes #54
Fixes #53
  • Loading branch information
tomayac committed Jul 1, 2021
1 parent ad4399f commit 78e2d3f
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 60 deletions.
14 changes: 14 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ export function fileOpen<M extends boolean | undefined = false>(options?: {
* reference to the `reject` callback for the `Promise` returned by
* `fileOpen`, so that developers may reject the `Promise` when desired at
* that time.
* Example rejector:
*
* const file = await fileOpen({
* setupLegacyCleanupAndRejection: (rejectionHandler) => {
* const timeoutId = setTimeout(rejectionHandler, 10_000);
* return (reject) => {
* clearTimeout(timeoutId);
* if (reject) {
* reject('My error message here.');
* }
* };
* },
* });
*
* ToDo: Remove this workaround once
* https://github.com/whatwg/html/issues/6376 is specified and supported.
*/
Expand Down
46 changes: 23 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "browser-fs-access",
"version": "0.17.3",
"version": "0.18.0",
"description": "File System Access API with legacy fallback in the browser.",
"browser": "./dist/index.js",
"main": "./dist/index.js",
Expand Down Expand Up @@ -39,13 +39,13 @@
},
"homepage": "https://github.com/GoogleChromeLabs/browser-fs-access#readme",
"devDependencies": {
"eslint": "^7.28.0",
"eslint": "^7.29.0",
"eslint-config-google": "^0.14.0",
"eslint-config-prettier": "^8.3.0",
"http-server": "^0.12.3",
"prettier": "^2.3.1",
"prettier": "^2.3.2",
"shx": "^0.3.3",
"terser": "^5.7.0"
"terser": "^5.7.1"
},
"eslintConfig": {
"parserOptions": {
Expand Down
17 changes: 1 addition & 16 deletions src/legacy/directory-open.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,9 @@ export default async (options = {}) => {
if (options.setupLegacyCleanupAndRejection) {
cleanupListenersAndMaybeReject =
options.setupLegacyCleanupAndRejection(rejectionHandler);
} else {
// Default rejection behavior; works in most cases, but not in Chrome in non-secure contexts.
cleanupListenersAndMaybeReject = (reject) => {
window.removeEventListener('pointermove', rejectionHandler);
window.removeEventListener('pointerdown', rejectionHandler);
window.removeEventListener('keydown', rejectionHandler);
if (reject) {
reject(new DOMException('The user aborted a request.', 'AbortError'));
}
};

window.addEventListener('pointermove', rejectionHandler);
window.addEventListener('pointerdown', rejectionHandler);
window.addEventListener('keydown', rejectionHandler);
}

input.addEventListener('change', () => {
cleanupListenersAndMaybeReject();
cleanupListenersAndMaybeReject?.();
let files = Array.from(input.files);
if (!options.recursive) {
files = files.filter((file) => {
Expand Down
18 changes: 1 addition & 17 deletions src/legacy/file-open.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,16 @@ export default async (options = {}) => {
input.multiple = options.multiple || false;
// Empty string allows everything.
input.accept = accept || '';

// ToDo: Remove this workaround once
// https://github.com/whatwg/html/issues/6376 is specified and supported.
let cleanupListenersAndMaybeReject;
const rejectionHandler = () => cleanupListenersAndMaybeReject(reject);
if (options.setupLegacyCleanupAndRejection) {
cleanupListenersAndMaybeReject =
options.setupLegacyCleanupAndRejection(rejectionHandler);
} else {
// Default rejection behavior; works in most cases, but not in Chrome in non-secure contexts.
cleanupListenersAndMaybeReject = (reject) => {
window.removeEventListener('pointermove', rejectionHandler);
window.removeEventListener('pointerdown', rejectionHandler);
window.removeEventListener('keydown', rejectionHandler);
if (reject) {
reject(new DOMException('The user aborted a request.', 'AbortError'));
}
};

window.addEventListener('pointermove', rejectionHandler);
window.addEventListener('pointerdown', rejectionHandler);
window.addEventListener('keydown', rejectionHandler);
}

input.addEventListener('change', () => {
cleanupListenersAndMaybeReject();
cleanupListenersAndMaybeReject?.();
resolve(input.multiple ? Array.from(input.files) : input.files[0]);
});

Expand Down
4 changes: 4 additions & 0 deletions src/supported.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
* @returns {boolean} Returns `true` if the File System Access API is supported and usable, else returns `false`.
*/
const supported = (() => {
// When running in an SSR environment return `false`.
if (typeof self === 'undefined') {
return false;
}
// ToDo: Remove this check once Permissions Policy integration
// has happened, tracked in
// https://github.com/WICG/file-system-access/issues/245.
Expand Down

0 comments on commit 78e2d3f

Please sign in to comment.