Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const {
ObjectDefineProperty,
Promise,
PromisePrototypeThen,
PromiseReject,
PromiseResolve,
ReflectApply,
SafeMap,
Expand Down Expand Up @@ -574,8 +575,12 @@ function openAsBlob(path, options = kEmptyObject) {
// The underlying implementation here returns the Blob synchronously for now.
// To give ourselves flexibility to maybe return the Blob asynchronously,
// this API returns a Promise.
path = getValidatedPath(path);
return PromiseResolve(createBlobFromFilePath(path, { type }));
try {
path = getValidatedPath(path);
return PromiseResolve(createBlobFromFilePath(path, { type }));
} catch (error) {
return PromiseReject(error);
}
}

/**
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-fs-openasblob-promise-contract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const assert = require('assert');
const fs = require('fs');

// Verify invalid paths throw synchronously and return a Promise
(async () => {
let promise;
assert.doesNotThrow(() => { promise = fs.openAsBlob('does-not-exist'); });
assert.strictEqual(typeof promise?.then, 'function');
await assert.rejects(promise, { code: 'ERR_INVALID_ARG_VALUE' });
})();


// Verify that invalid arguments throw synchronously and return a Promise
(async () => {
let promise;
assert.doesNotThrow(() => { promise = fs.openAsBlob(123); });
assert.strictEqual(typeof promise?.then, 'function');
await assert.rejects(promise, { code: 'ERR_INVALID_ARG_TYPE' });
})();