12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.NodeFileSystemSyncAccessHandle = void 0;
- const util_1 = require("./util");
- const buffer_1 = require("../internal/buffer");
- class NodeFileSystemSyncAccessHandle {
- constructor(fs, path, ctx) {
- this.fs = fs;
- this.path = path;
- this.ctx = ctx;
- this.fd = fs.openSync(path, 'r+');
- }
-
- async close() {
- (0, util_1.assertCanWrite)(this.ctx.mode);
- this.fs.closeSync(this.fd);
- }
-
- async flush() {
- (0, util_1.assertCanWrite)(this.ctx.mode);
- this.fs.fsyncSync(this.fd);
- }
-
- async getSize() {
- return this.fs.statSync(this.path).size;
- }
-
- async read(buffer, options = {}) {
- var _a;
- const buf = buffer instanceof ArrayBuffer ? buffer_1.Buffer.from(buffer) : buffer;
- try {
- const size = this.fs.readSync(this.fd, buf, 0, buffer.byteLength, (_a = options.at) !== null && _a !== void 0 ? _a : 0);
- return size;
- }
- catch (error) {
- if (error instanceof DOMException)
- throw error;
- if (error && typeof error === 'object') {
- switch (error.code) {
- case 'EBADF': {
- throw new DOMException('File handle already closed.', 'InvalidStateError');
- }
- }
- }
- throw error;
- }
- }
-
- async truncate(newSize) {
- (0, util_1.assertCanWrite)(this.ctx.mode);
- this.fs.truncateSync(this.fd, newSize);
- }
-
- async write(buffer, options = {}) {
- var _a;
- (0, util_1.assertCanWrite)(this.ctx.mode);
- const buf = buffer instanceof ArrayBuffer ? buffer_1.Buffer.from(buffer) : buffer;
- try {
- return this.fs.writeSync(this.fd, buf, 0, buffer.byteLength, (_a = options.at) !== null && _a !== void 0 ? _a : 0);
- }
- catch (error) {
- if (error instanceof DOMException)
- throw error;
- if (error && typeof error === 'object') {
- switch (error.code) {
- case 'EBADF': {
- throw new DOMException('File handle already closed.', 'InvalidStateError');
- }
- }
- }
- throw error;
- }
- }
- }
- exports.NodeFileSystemSyncAccessHandle = NodeFileSystemSyncAccessHandle;
|