All files / fs / _node_fs_file.ts

100.00% Branches 0/0
3.35% Lines 6/179
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
 
 
x3
x3
 
x3
 
 
 
 
x3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x3























































































































































































































// Copyright 2018-2025 the Deno authors. MIT license.

import { getNodeFs, getNodeStream, getNodeTty, getNodeUtil } from "./_utils.ts";
import { mapError } from "./_map_error.ts";
import type { FileInfo, FsFile, SetRawOptions } from "./unstable_types.ts";
import { toFileInfo } from "./_to_file_info.ts";

/**
 * The internal class to convert a Node file descriptor into a FsFile object.
 */
export class NodeFsFile implements FsFile {
  #nodeFs = getNodeFs();
  #nodeStream = getNodeStream();
  #nodeTty = getNodeTty();
  #nodeUtil = getNodeUtil();
  #nodeReadFd = this.#nodeUtil.promisify(this.#nodeFs.read);
  #nodeWriteFd = this.#nodeUtil.promisify(this.#nodeFs.write);

  #closed: boolean;
  #rid: number;
  #readableStream?: ReadableStream<Uint8Array>;
  #writableStream?: WritableStream<Uint8Array>;

  constructor(fd: number) {
    this.#rid = fd;
    this.#closed = false;
  }

  get readable(): ReadableStream<Uint8Array> {
    if (this.#readableStream == null) {
      const readStream = this.#nodeFs.createReadStream(null as unknown, {
        fd: this.#rid,
        autoClose: false,
      });
      this.#readableStream = this.#nodeStream.Readable.toWeb(readStream);
    }
    return this.#readableStream as ReadableStream;
  }

  get writable(): WritableStream<Uint8Array> {
    if (this.#writableStream == null) {
      const writeStream = this.#nodeFs.createWriteStream(null as unknown, {
        fd: this.#rid,
        autoClose: false,
      });
      this.#writableStream = this.#nodeStream.Writable.toWeb(writeStream);
    }
    return this.#writableStream as WritableStream;
  }

  [Symbol.dispose](): void {
    if (!this.#closed) {
      this.close();
    }
  }

  close(): void {
    this.#closed = true;
    this.#nodeFs.closeSync(this.#rid);
  }

  isTerminal(): boolean {
    return this.#nodeTty.isatty(this.#rid);
  }

  // deno-lint-ignore require-await no-unused-vars
  async lock(exclusive?: boolean): Promise<void> {
    throw new Error("Method not implemented");
  }

  // deno-lint-ignore no-unused-vars
  lockSync(exclusive?: boolean): void {
    throw new Error("Method not implemented");
  }

  async read(p: Uint8Array): Promise<number | null> {
    try {
      const { bytesRead } = await this.#nodeReadFd(
        this.#rid,
        p,
        0,
        p.length,
        null,
      );
      return bytesRead === 0 ? null : bytesRead;
    } catch (error) {
      throw mapError(error);
    }
  }

  readSync(p: Uint8Array): number | null {
    try {
      const bytesRead = this.#nodeFs.readSync(this.#rid, p);
      return bytesRead === 0 ? null : bytesRead;
    } catch (error) {
      throw mapError(error);
    }
  }

  //deno-lint-ignore no-unused-vars
  setRaw(mode: boolean, options?: SetRawOptions): void {
    throw new Error("Method not implemented");
  }

  async stat(): Promise<FileInfo> {
    const nodeStatFd = this.#nodeUtil.promisify(this.#nodeFs.fstat);
    try {
      const fdStat = await nodeStatFd(this.#rid);
      return toFileInfo(fdStat);
    } catch (error) {
      throw mapError(error);
    }
  }

  statSync(): FileInfo {
    try {
      const fdStat = this.#nodeFs.fstatSync(this.#rid);
      return toFileInfo(fdStat);
    } catch (error) {
      throw mapError(error);
    }
  }

  async sync(): Promise<void> {
    const nodeFsyncFd = this.#nodeUtil.promisify(this.#nodeFs.fsync);
    try {
      await nodeFsyncFd(this.#rid);
    } catch (error) {
      throw mapError(error);
    }
  }

  syncSync(): void {
    try {
      this.#nodeFs.fsyncSync(this.#rid);
    } catch (error) {
      throw mapError(error);
    }
  }

  async syncData(): Promise<void> {
    const nodeFdatasyncFd = this.#nodeUtil.promisify(this.#nodeFs.fdatasync);
    try {
      await nodeFdatasyncFd(this.#rid);
    } catch (error) {
      throw mapError(error);
    }
  }

  syncDataSync(): void {
    try {
      this.#nodeFs.fdatasyncSync(this.#rid);
    } catch (error) {
      throw mapError(error);
    }
  }

  async truncate(len?: number): Promise<void> {
    const nodeTruncateFd = this.#nodeUtil.promisify(this.#nodeFs.ftruncate);
    try {
      await nodeTruncateFd(this.#rid, len);
    } catch (error) {
      throw mapError(error);
    }
  }

  truncateSync(len?: number): void {
    try {
      this.#nodeFs.ftruncateSync(this.#rid, len);
    } catch (error) {
      throw mapError(error);
    }
  }

  // deno-lint-ignore require-await
  async unlock(): Promise<void> {
    throw new Error("Method not implemented");
  }

  unlockSync(): void {
    throw new Error("Method not implemented");
  }

  async utime(atime: number | Date, mtime: number | Date): Promise<void> {
    const nodeUtimeFd = this.#nodeUtil.promisify(this.#nodeFs.futimes);
    try {
      await nodeUtimeFd(this.#rid, atime, mtime);
    } catch (error) {
      throw mapError(error);
    }
  }

  utimeSync(atime: number | Date, mtime: number | Date): void {
    try {
      this.#nodeFs.futimesSync(this.#rid, atime, mtime);
    } catch (error) {
      throw mapError(error);
    }
  }

  async write(p: Uint8Array): Promise<number> {
    try {
      const { bytesWritten } = await this.#nodeWriteFd(this.#rid, p);
      return bytesWritten;
    } catch (error) {
      throw mapError(error);
    }
  }

  writeSync(p: Uint8Array): number {
    try {
      return this.#nodeFs.writeSync(this.#rid, p);
    } catch (error) {
      throw mapError(error);
    }
  }
}