| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent c96f1a5 commit 2ebefe0
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -628,15 +628,17 @@ Read from a file and write to an array of {ArrayBufferView}s | |||
| 628 | 628 | <!-- YAML | |
| 629 | 629 | added: v10.0.0 | |
| 630 | 630 | changes: | |
| 631 | + - version: REPLACEME | ||
| 632 | + pr-url: https://github.com/nodejs/node/pull/57775 | ||
| 633 | + description: Now accepts an additional `signal` property to allow aborting the operation. | ||
| 631 | 634 | - version: v10.5.0 | |
| 632 | 635 | pr-url: https://github.com/nodejs/node/pull/20220 | |
| 633 | - description: Accepts an additional `options` object to specify whether | ||
| 634 | - the numeric values returned should be bigint. | ||
| 636 | + description: Accepts an additional `options` object to specify whether the numeric values returned should be bigint. | ||
| 635 | 637 | --> | |
| 636 | 638 | ||
| 637 | 639 | * `options` {Object} | |
| 638 | - * `bigint` {boolean} Whether the numeric values in the returned | ||
| 639 | - {fs.Stats} object should be `bigint`. **Default:** `false`. | ||
| 640 | + * `bigint` {boolean} Whether the numeric values in the returned {fs.Stats} object should be `bigint`. **Default:** `false`. | ||
| 641 | + * `signal` {AbortSignal} An AbortSignal to cancel the operation. **Default:** `undefined`. | ||
| 640 | 642 | * Returns: {Promise} Fulfills with an {fs.Stats} for the file. | |
| 641 | 643 | ||
| 642 | 644 | #### `filehandle.sync()` | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1652,7 +1652,7 @@ function lstat(path, options = { bigint: false }, callback) { | |||
| 1652 | 1652 | /** | |
| 1653 | 1653 | * Asynchronously gets the stats of a file. | |
| 1654 | 1654 | * @param {string | Buffer | URL} path | |
| 1655 | - * @param {{ bigint?: boolean; }} [options] | ||
| 1655 | + * @param {{ bigint?: boolean, signal?: AbortSignal }} [options] | ||
| 1656 | 1656 | * @param {( | |
| 1657 | 1657 | * err?: Error, | |
| 1658 | 1658 | * stats?: Stats | |
@@ -1663,8 +1663,16 @@ function stat(path, options = { bigint: false, throwIfNoEntry: true }, callback) | |||
| 1663 | 1663 | if (typeof options === 'function') { | |
| 1664 | 1664 | callback = options; | |
| 1665 | 1665 | options = kEmptyObject; | |
| 1666 | + } else if (options === null || typeof options !== 'object') { | ||
| 1667 | + options = kEmptyObject; | ||
| 1668 | + } else { | ||
| 1669 | + options = getOptions(options, { bigint: false }); | ||
| 1666 | 1670 | } | |
| 1671 | + | ||
| 1667 | 1672 | callback = makeStatsCallback(callback); | |
| 1673 | + path = getValidatedPath(path); | ||
| 1674 | + | ||
| 1675 | + if (checkAborted(options.signal, callback)) return; | ||
| 1668 | 1676 | ||
| 1669 | 1677 | const req = new FSReqCallback(options.bigint); | |
| 1670 | 1678 | req.oncomplete = callback; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,34 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + require('../common'); | ||
| 4 | + const test = require('node:test'); | ||
| 5 | + const assert = require('node:assert'); | ||
| 6 | + const fs = require('node:fs'); | ||
| 7 | + const tmpdir = require('../common/tmpdir'); | ||
| 8 | + | ||
| 9 | + test('fs.stat should throw AbortError when called with an already aborted AbortSignal', async () => { | ||
| 10 | + // This test verifies that fs.stat immediately throws an AbortError if the provided AbortSignal | ||
| 11 | + // has already been canceled. This approach is used because trying to abort an fs.stat call in flight | ||
| 12 | + // is unreliable given that file system operations tend to complete very quickly on many platforms. | ||
| 13 | + tmpdir.refresh(); | ||
| 14 | + | ||
| 15 | + const filePath = tmpdir.resolve('temp.txt'); | ||
| 16 | + fs.writeFileSync(filePath, 'Test'); | ||
| 17 | + | ||
| 18 | + // Create an already aborted AbortSignal. | ||
| 19 | + const signal = AbortSignal.abort(); | ||
| 20 | + | ||
| 21 | + const { promise, resolve, reject } = Promise.withResolvers(); | ||
| 22 | + fs.stat(filePath, { signal }, (err, stats) => { | ||
| 23 | + if (err) { | ||
| 24 | + return reject(err); | ||
| 25 | + } | ||
| 26 | + resolve(stats); | ||
| 27 | + }); | ||
| 28 | + | ||
| 29 | + // Assert that the promise is rejected with an AbortError. | ||
| 30 | + await assert.rejects(promise, { name: 'AbortError' }); | ||
| 31 | + | ||
| 32 | + fs.unlinkSync(filePath); | ||
| 33 | + tmpdir.refresh(); | ||
| 34 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments