Skip to content

Commit 7e2525c

Browse files
committed
Working on typescript
1 parent 280cd4e commit 7e2525c

File tree

9 files changed

+1684
-1197
lines changed

9 files changed

+1684
-1197
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
"bugs": {
7979
"url": "https://github.com/ioBroker/ioBroker.javascript/issues"
8080
},
81-
"main": "main.js",
81+
"main": "src/main.ts",
8282
"files": [
8383
"admin/",
8484
"lib/",
@@ -87,7 +87,7 @@
8787
"lib/",
8888
"io-package.json",
8989
"LICENSE",
90-
"main.js",
90+
"src/main.ts",
9191
"admin/vsFont/codicon.json"
9292
],
9393
"scripts": {

src/lib/javascript.d.ts

Lines changed: 54 additions & 84 deletions
Large diffs are not rendered by default.

src/lib/mirror.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { ScriptType } from '../types';
1717

1818
const MODE_0777 = 511;
1919

20-
export default class Mirror {
20+
export class Mirror {
2121
private adapter: ioBroker.Adapter;
2222
private readonly diskRoot: string;
2323
private readonly from: string;

src/lib/protectFs.ts

Lines changed: 119 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1-
import {BufferEncodingOption, CopyOptions, Mode, ObjectEncodingOptions, OpenMode, PathLike} from 'node:fs';
1+
import {
2+
BufferEncodingOption,
3+
CopyOptions,
4+
MakeDirectoryOptions,
5+
Mode,
6+
ObjectEncodingOptions,
7+
OpenMode,
8+
PathLike,
9+
RmDirOptions,
10+
RmOptions,
11+
StatOptions,
12+
Stats,
13+
TimeLike,
14+
} from 'node:fs';
215
import { Abortable } from 'node:events';
3-
import {FileHandle, FlagAndOpenMode} from 'fs/promises';
4-
import {NoParamCallback} from "fs";
5-
import {URL} from "node:url";
6-
import {Stream} from "node:stream";
16+
import { FileHandle, FlagAndOpenMode } from 'fs/promises';
17+
import { CopySyncOptions, NoParamCallback, PathOrFileDescriptor } from 'fs';
18+
import { URL } from 'node:url';
19+
import { Stream } from 'node:stream';
720

821
const nodeFS = require('node:fs');
922
const { sep, normalize, join } = require('node:path');
@@ -16,7 +29,14 @@ export default class ProtectFs {
1629

1730
constructor(log: ioBroker.Logger, ioBrokerDataDir: string) {
1831
this.ioBrokerDataDir = ioBrokerDataDir;
19-
this.log = log || console;
32+
this.log = log || {
33+
silly: (message: string): void => console.log(message),
34+
debug: (message: string): void => console.debug(message),
35+
info: (message: string): void => console.info(message),
36+
warn: (message: string): void => console.warn(message),
37+
error: (message: string): void => console.error(message),
38+
level: 'info',
39+
};
2040

2141
this.promises = {
2242
access: async (path: PathLike, mode?: number): Promise<void> => {
@@ -59,103 +79,115 @@ export default class ProtectFs {
5979
| Stream,
6080
options?:
6181
| (ObjectEncodingOptions & {
62-
mode?: Mode | undefined;
63-
flag?: OpenMode | undefined;
64-
/**
65-
* If all data is successfully written to the file, and `flush`
66-
* is `true`, `filehandle.sync()` is used to flush the data.
67-
* @default false
68-
*/
69-
flush?: boolean | undefined;
70-
} & Abortable)
82+
mode?: Mode | undefined;
83+
flag?: OpenMode | undefined;
84+
/**
85+
* If all data is successfully written to the file, and `flush`
86+
* is `true`, `filehandle.sync()` is used to flush the data.
87+
* @default false
88+
*/
89+
flush?: boolean | undefined;
90+
} & Abortable)
7191
| BufferEncoding
7292
| null,
7393
): Promise<void> => {
74-
this.#checkProtected(arguments[0], true);
75-
return nodeFS.promises.writeFile.apply(this, arguments); // async function writeFile(path, data, options) {
94+
this.#checkProtected(file, true);
95+
return nodeFS.promises.writeFile.call(this, file, data, options); // async function writeFile(path, data, options) {
7696
},
7797
unlink: async (path: PathLike): Promise<void> => {
7898
this.#checkProtected(path, false);
79-
return nodeFS.promises.unlink.apply(this, arguments); // async function unlink(path) {
99+
return nodeFS.promises.unlink.call(this, path); // async function unlink(path) {
80100
},
81101
appendFile: async (
82102
path: PathLike | FileHandle,
83103
data: string | Uint8Array,
84-
options?: (ObjectEncodingOptions & FlagAndOpenMode & { flush?: boolean | undefined }) | BufferEncoding | null,
104+
options?:
105+
| (ObjectEncodingOptions & FlagAndOpenMode & { flush?: boolean | undefined })
106+
| BufferEncoding
107+
| null,
85108
): Promise<void> => {
86109
this.#checkProtected(path, false);
87-
return nodeFS.promises.appendFile.apply(this, arguments); // async function appendFile(path, data, options) {
110+
return nodeFS.promises.appendFile.call(this, path, data, options); // async function appendFile(path, data, options) {
88111
},
89112
chmod: async (path: PathLike, mode: Mode): Promise<void> => {
90113
this.#checkProtected(path, false);
91-
return nodeFS.promises.chmod.apply(this, arguments); // async function chmod(path, mode) {
114+
return nodeFS.promises.chmod.call(this, path, mode); // async function chmod(path, mode) {
92115
},
93116
copyFile: async (src: PathLike, dest: PathLike, mode?: number): Promise<void> => {
94117
this.#checkProtected(src, false);
95118
this.#checkProtected(dest, false);
96-
return nodeFS.promises.copyFile.apply(this, arguments); // async function copyFile(src, dest, mode) {
119+
return nodeFS.promises.copyFile.call(this, src, dest, mode); // async function copyFile(src, dest, mode) {
97120
},
98121
rename: async (oldPath: PathLike, newPath: PathLike): Promise<void> => {
99122
this.#checkProtected(oldPath, false);
100123
this.#checkProtected(newPath, false);
101-
return nodeFS.promises.rename.apply(this, arguments); // async function rename(oldPath, newPath) {
124+
return nodeFS.promises.rename.call(this, oldPath, newPath); // async function rename(oldPath, newPath) {
102125
},
103126
open: async (path: PathLike, flags?: string | number, mode?: Mode): Promise<FileHandle> => {
104127
this.#checkProtected(path, true);
105-
return nodeFS.promises.open.apply(this, arguments); // async function open(path, flags, mode) {
128+
return nodeFS.promises.open.call(this, path, flags, mode); // async function open(path, flags, mode) {
106129
},
107130
truncate: async (path: PathLike, len?: number): Promise<void> => {
108-
this.#checkProtected(arguments[0], false);
109-
return nodeFS.promises.truncate.apply(this, arguments); // async function truncate(path, len = 0) {
131+
this.#checkProtected(path, false);
132+
return nodeFS.promises.truncate.call(this, path, len); // async function truncate(path, len = 0) {
110133
},
111-
stat: async () => {
112-
this.#checkProtected(arguments[0], true);
113-
return nodeFS.promises.stat.apply(this, arguments); // async function stat(path, options = { bigint: false }) {
134+
stat: async (path: PathLike, opts?: StatOptions): Promise<Stats> => {
135+
this.#checkProtected(path, true);
136+
return nodeFS.promises.stat.call(this, path, opts); // async function stat(path, options = { bigint: false }) {
114137
},
115-
utimes: async () => {
116-
this.#checkProtected(arguments[0], false);
117-
return nodeFS.promises.utimes.apply(this, arguments); // async function utimes(path, atime, mtime) {
138+
utimes: async (path: PathLike, atime: TimeLike, mtime: TimeLike): Promise<void> => {
139+
this.#checkProtected(path, false);
140+
return nodeFS.promises.utimes.call(this, path, atime, mtime); // async function utimes(path, atime, mtime) {
118141
},
119-
readdir: async () => {
120-
this.#checkProtected(arguments[0], true);
121-
return nodeFS.promises.readdir.apply(this, arguments); // async function readdir(path, options) {
142+
readdir: async (
143+
path: PathLike,
144+
options?: ObjectEncodingOptions & {
145+
withFileTypes: true;
146+
recursive?: boolean | undefined;
147+
},
148+
) => {
149+
this.#checkProtected(path, true);
150+
return nodeFS.promises.readdir.call(this, path, options); // async function readdir(path, options) {
122151
},
123-
lchmod: async () => {
124-
this.#checkProtected(arguments[0], false);
125-
return nodeFS.promises.lchmod.apply(this, arguments); // async function lchmod(path, mode) {
152+
lchmod: async (path: PathLike, mode: Mode): Promise<void> => {
153+
this.#checkProtected(path, false);
154+
return nodeFS.promises.lchmod.call(this, path, mode); // async function lchmod(path, mode) {
126155
},
127-
lchown: async () => {
128-
this.#checkProtected(arguments[0], false);
129-
return nodeFS.promises.lchown.apply(this, arguments); // async function lchown(path, uid, gid) {
156+
lchown: async (path: PathLike, uid: number, gid: number): Promise<void> => {
157+
this.#checkProtected(path, false);
158+
return nodeFS.promises.lchown.call(this, path, uid, gid); // async function lchown(path, uid, gid) {
130159
},
131-
link: async () => {
132-
this.#checkProtected(arguments[0], false);
133-
this.#checkProtected(arguments[1], false);
134-
return nodeFS.promises.link.apply(this, arguments); // async function link(existingPath, newPath) {
160+
link: async (existingPath: PathLike, newPath: PathLike): Promise<void> => {
161+
this.#checkProtected(existingPath, false);
162+
this.#checkProtected(newPath, false);
163+
return nodeFS.promises.link.call(this, existingPath, newPath); // async function link(existingPath, newPath) {
135164
},
136-
lstat: async () => {
137-
this.#checkProtected(arguments[0], true);
138-
return nodeFS.promises.lstat.apply(this, arguments); // async function lstat(path, options = { bigint: false }) {
165+
lstat: async (path: PathLike, opts?: StatOptions): Promise<Stats> => {
166+
this.#checkProtected(path, true);
167+
return nodeFS.promises.lstat.call(this, path, opts); // async function lstat(path, options = { bigint: false }) {
139168
},
140-
lutimes: async () => {
141-
this.#checkProtected(arguments[0], false);
142-
return nodeFS.promises.lutimes.apply(this, arguments); // async function lutimes(path, atime, mtime) {
169+
lutimes: async (path: PathLike, atime: TimeLike, mtime: TimeLike): Promise<void> => {
170+
this.#checkProtected(path, false);
171+
return nodeFS.promises.lutimes.call(this, path, atime, mtime); // async function lutimes(path, atime, mtime) {
143172
},
144-
mkdir: async () => {
145-
this.#checkProtected(arguments[0], false);
146-
return nodeFS.promises.mkdir.apply(this, arguments); // async function mkdir(path, options) {
173+
mkdir: async (
174+
path: PathLike,
175+
options?: Mode | MakeDirectoryOptions | null,
176+
): Promise<string | undefined> => {
177+
this.#checkProtected(path, false);
178+
return nodeFS.promises.mkdir.call(this, path, options); // async function mkdir(path, options) {
147179
},
148-
mkdtemp: async () => {
149-
this.#checkProtected(arguments[0], false);
150-
return nodeFS.promises.mkdtemp.apply(this, arguments); // async function mkdtemp(prefix, options) {
180+
mkdtemp: async (prefix: string, options: BufferEncodingOption): Promise<Buffer> => {
181+
this.#checkProtected(prefix, false);
182+
return nodeFS.promises.mkdtemp.call(this, prefix, options); // async function mkdtemp(prefix, options) {
151183
},
152-
rm: async () => {
153-
this.#checkProtected(arguments[0], false);
154-
return nodeFS.promises.rm.apply(this, arguments); // async function rm(path, options) {
184+
rm: async (path: PathLike, options?: RmOptions): Promise<void> => {
185+
this.#checkProtected(path, false);
186+
return nodeFS.promises.rm.call(this, path, options); // async function rm(path, options) {
155187
},
156-
rmdir: async () => {
157-
this.#checkProtected(arguments[0], false);
158-
return nodeFS.promises.rmdir.apply(this, arguments); // async function rmdir(path, options) {
188+
rmdir: async (path: PathLike, options?: RmDirOptions): Promise<void> => {
189+
this.#checkProtected(path, false);
190+
return nodeFS.promises.rmdir.call(this, path, options); // async function rmdir(path, options) {
159191
},
160192
};
161193

@@ -220,20 +252,32 @@ export default class ProtectFs {
220252
return nodeFS.cp(source, destination, opts);
221253
}
222254

223-
cpSync() {
224-
this.#checkProtected(arguments[0], false);
225-
this.#checkProtected(arguments[1], false);
226-
return nodeFS.cpSync.apply(this, arguments); // function cpSync(src, dest, options) {
227-
}
228-
229-
readFile() {
230-
this.#checkProtected(arguments[0], true);
231-
return nodeFS.readFile.apply(this, arguments); // function readFile(path, options, callback) {
255+
cpSync(source: string | URL, destination: string | URL, opts?: CopySyncOptions): void {
256+
this.#checkProtected(source, false);
257+
this.#checkProtected(destination, false);
258+
return nodeFS.cpSync.call(this, source, destination, opts); // function cpSync(src, dest, options) {
232259
}
233260

234-
readFileSync() {
235-
this.#checkProtected(arguments[0], true);
236-
return nodeFS.readFileSync.apply(this, arguments); // function readFileSync(path, options) {
261+
readFile(path: PathOrFileDescriptor, callback: (err: NodeJS.ErrnoException | null, data: Buffer) => void): void {
262+
if (typeof path !== 'number') {
263+
this.#checkProtected(path, true);
264+
}
265+
return nodeFS.readFile.call(this, path, callback); // function readFile(path, options, callback) {
266+
}
267+
268+
readFileSync(
269+
path: PathOrFileDescriptor,
270+
options:
271+
| {
272+
encoding: BufferEncoding;
273+
flag?: string | undefined;
274+
}
275+
| BufferEncoding,
276+
): string | Buffer {
277+
if (typeof path !== 'number') {
278+
this.#checkProtected(path, true);
279+
}
280+
return nodeFS.readFileSync.call(this, path, options); // function readFileSync(path, options) {
237281
}
238282

239283
readlink() {

0 commit comments

Comments
 (0)