diff --git a/ext/node/polyfills/internal/fs/handle.ts b/ext/node/polyfills/internal/fs/handle.ts index e22c3e0ae6..4412e79c15 100644 --- a/ext/node/polyfills/internal/fs/handle.ts +++ b/ext/node/polyfills/internal/fs/handle.ts @@ -158,6 +158,14 @@ export class FileHandle extends EventEmitter { return promises.chmod(this.#path, mode); } + datasync(): Promise { + return fsCall(promises.fdatasync, this); + } + + sync(): Promise { + return fsCall(promises.fsync, this); + } + utimes( atime: number | string | Date, mtime: number | string | Date, diff --git a/tests/node_compat/config.jsonc b/tests/node_compat/config.jsonc index c2edc32ef6..4cd0056e68 100644 --- a/tests/node_compat/config.jsonc +++ b/tests/node_compat/config.jsonc @@ -480,6 +480,7 @@ "test-fs-opendir.js", "test-fs-promises-exists.js", "test-fs-promises-file-handle-stat.js", + "test-fs-promises-file-handle-sync.js", "test-fs-promises-file-handle-write.js", "test-fs-promises-readfile-empty.js", "test-fs-promises-readfile-with-fd.js", diff --git a/tests/node_compat/test/parallel/test-fs-promises-file-handle-sync.js b/tests/node_compat/test/parallel/test-fs-promises-file-handle-sync.js new file mode 100644 index 0000000000..fa7022cb47 --- /dev/null +++ b/tests/node_compat/test/parallel/test-fs-promises-file-handle-sync.js @@ -0,0 +1,42 @@ +// deno-fmt-ignore-file +// deno-lint-ignore-file + +// Copyright Joyent and Node contributors. All rights reserved. MIT license. +// Taken from Node 20.11.1 +// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually. + +'use strict'; +require('../common'); +const assert = require('assert'); +const fixtures = require('../common/fixtures'); +const tmpdir = require('../common/tmpdir'); + +const { access, copyFile, open } = require('fs').promises; + +async function validate() { + tmpdir.refresh(); + const dest = tmpdir.resolve('baz.js'); + await assert.rejects( + copyFile(fixtures.path('baz.js'), dest, 'r'), + { + code: 'ERR_INVALID_ARG_TYPE', + } + ); + await copyFile(fixtures.path('baz.js'), dest); + await assert.rejects( + access(dest, 'r'), + { code: 'ERR_INVALID_ARG_TYPE', message: /mode/ } + ); + await access(dest); + const handle = await open(dest, 'r+'); + await handle.datasync(); + await handle.sync(); + const buf = Buffer.from('hello world'); + await handle.write(buf); + const ret = await handle.read(Buffer.alloc(11), 0, 11, 0); + assert.strictEqual(ret.bytesRead, 11); + assert.deepStrictEqual(ret.buffer, buf); + await handle.close(); +} + +validate();