-
Notifications
You must be signed in to change notification settings - Fork 22
/
lib.deno.ns.d.ts
3557 lines (3382 loc) · 164 KB
/
lib.deno.ns.d.ts
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
/// <reference no-default-lib="true" />
/// <reference lib="esnext" />
declare namespace Deno {
/** The current process id of the runtime.
* @i18n 当前正在运行的进程 ID。 */
export let pid: number;
/** Reflects the `NO_COLOR` environment variable.
* @i18n 显示环境变量 `NO_COLOR` 的值。
*
* See: https://no-color.org/
* @i18n 参见:https://no-color.org/*/
export let noColor: boolean;
export interface TestDefinition {
fn: () => void | Promise<void>;
name: string;
ignore?: boolean;
disableOpSanitizer?: boolean;
disableResourceSanitizer?: boolean;
}
/** Register a test which will be run when `deno test` is used on the command
* line and the containing module looks like a test module, or explicitly
* when `Deno.runTests` is used. `fn` can be async if required.
*
* @i18n 注册一个测试,它将在命令行执行 `deno test` 操作并且包含的模块看起来像一个测试模块时运行,
* 或者在使用 `Deno.runTests` 时显式运行。如果需要, `fn` 可以是异步的。
*
* import {assert, fail, assertEquals} from "https://deno.land/std/testing/asserts.ts";
*
* Deno.test({
* name: "example test",
* fn(): void {
* assertEquals("world", "world");
* },
* });
*
* Deno.test({
* name: "example ignored test",
* ignore: Deno.build.os === "win"
* fn(): void {
* // 仅在 Windows 机器上忽略这个测试。
* },
* });
*
* Deno.test({
* name: "example async test",
* async fn() {
* const decoder = new TextDecoder("utf-8");
* const data = await Deno.readFile("hello_world.txt");
* assertEquals(decoder.decode(data), "Hello world")
* }
* });
*/
export function test(t: TestDefinition): void;
/** Register a test which will be run when `deno test` is used on the command
* line and the containing module looks like a test module, or explicitly
* when `Deno.runTests` is used
*
* @i18n 注册一个测试,它将在命令行执行 `deno test` 操作并且包含的模块看起来像一个测试模块时运行,
* 或者在使用 `Deno.runTests` 时显式运行。
*
* import {assert, fail, assertEquals} from "https://deno.land/std/testing/asserts.ts";
*
* Deno.test(function myTestFunction():void {
* assertEquals("hello", "hello");
* });
*
* Deno.test(async function myAsyncTestFunction():Promise<void> {
* const decoder = new TextDecoder("utf-8");
* const data = await Deno.readFile("hello_world.txt");
* assertEquals(decoder.decode(data), "Hello world")
* });
**/
export function test(fn: () => void | Promise<void>): void;
/** Register a test which will be run when `deno test` is used on the command
* line and the containing module looks like a test module, or explicitly
* when `Deno.runTests` is used
*
* @i18n 注册一个测试,它将在命令行执行 `deno test` 操作并且包含的模块看起来像一个测试模块时运行,
* 或者在使用 `Deno.runTests` 时显式运行。
*
* import {assert, fail, assertEquals} from "https://deno.land/std/testing/asserts.ts";
*
* Deno.test("My test description", ():void => {
* assertEquals("hello", "hello");
* });
*
* Deno.test("My async test description", async ():Promise<void> => {
* const decoder = new TextDecoder("utf-8");
* const data = await Deno.readFile("hello_world.txt");
* assertEquals(decoder.decode(data), "Hello world")
* });
* */
export function test(name: string, fn: () => void | Promise<void>): void;
export interface TestMessage {
start?: {
tests: TestDefinition[];
};
testStart?: {
[P in keyof TestDefinition]: TestDefinition[P];
};
testEnd?: {
name: string;
status: "passed" | "failed" | "ignored";
duration: number;
error?: Error;
};
end?: {
filtered: number;
ignored: number;
measured: number;
passed: number;
failed: number;
duration: number;
results: Array<TestMessage["testEnd"] & {}>;
};
}
export interface RunTestsOptions {
/** If `true`, Deno will exit with status code 1 if there was
* test failure. Defaults to `true`.
* @i18n 如果为 `true`,当测试失败时 Deno 将以状态码 1 退出。默认为 `true`。*/
exitOnFail?: boolean;
/** If `true`, Deno will exit upon first test failure. Defaults to `false`.
* @i18n 如果为 `true`,Deno 将在第一次测试失败后退出。默认值为 `false`。*/
failFast?: boolean;
/** String or RegExp used to filter test to run. Only test with names
* matching provided `String` or `RegExp` will be run.
* @i18n 用于筛选要运行的测试的字符串或正则表达式。只有当测试名称与提供的 `String` 或 `RegExp` 相匹配时才会运行。*/
filter?: string | RegExp;
/** String or RegExp used to skip tests to run. Tests with names
* matching provided `String` or `RegExp` will not be run.
* @i18n 用于跳过要运行的测试的字符串或正则表达式。当测试名称与提供的 `String` 或 `RegExp` 相匹配时将不会运行。*/
skip?: string | RegExp;
/** Disable logging of the results. Defaults to `false`.
* @i18n 禁用记录结果. 默认值为 `false`。*/
disableLog?: boolean;
/** If true, report results to the console as is done for `deno test`. Defaults to `true`.
* @i18n 如果为 `true`,将 `deno test` 完成的结果输出到控制台。默认值为 `true`。*/
reportToConsole?: boolean;
/** Called for each message received from the test run.
* @i18n 回调从测试运行收到的每个消息。*/
onMessage?: (message: TestMessage) => void | Promise<void>;
}
/** Run any tests which have been registered via `Deno.test()`. Always resolves
* asynchronously.
*
* @i18n 运行所有通过 `Deno.test()` 注册的测试。始终异步 resolve。
*
* // 注册一个测试。
* Deno.test({
* name: "example test",
* fn(): void {
* assertEquals("world", "world");
* assertEquals({ hello: "world" }, { hello: "world" });
* },
* });
*
* // 运行所有已经注册过的测试。
* const runInfo = await Deno.runTests();
* console.log(runInfo.duration); // all tests duration, e.g. "5" (in ms)
* console.log(runInfo.stats.passed); //e.g. 1
* console.log(runInfo.results[0].name); //e.g. "example test"
*/
export function runTests(
opts?: RunTestsOptions
): Promise<TestMessage["end"]> & {};
/** Returns an array containing the 1, 5, and 15 minute load averages. The
* load average is a measure of CPU and IO utilization of the last one, five,
* and 15 minute periods expressed as a fractional number. Zero means there
* is no load. On Windows, the three values are always the same and represent
* the current load, not the 1, 5 and 15 minute load averages.
*
* @i18n 返回 1 分钟、5 分钟和 15 分钟平均负载的数组。
* 平均负载是对最后 1 分钟、5 分钟和 15 分钟的 CPU 以及 IO 利用率的度量,以分数表示。
* `0` 表示没有负载。
* 在 Windows 上,这 3 个值始终相同,代表当前负载,而不是 1 分钟、5 分钟和 15 分钟的平均负载。
*
* console.log(Deno.loadavg()); //e.g. [ 0.71, 0.44, 0.44 ]
*
* Requires `allow-env` permission.
* @i18n 需要 `allow-env` 权限。
*/
export function loadavg(): number[];
/** Get the `hostname` of the machine the Deno process is running on.
* @i18n 获取 Deno 进程所在的计算机主机名(`hostname`)。
*
* console.log(Deno.hostname());
*
* Requires `allow-env` permission.
* @i18n 需要 `allow-env` 权限。
*/
export function hostname(): string;
/** Returns the release version of the Operating System.
* @i18n 返回操作系统的发行版本。
*
* console.log(Deno.osRelease());
*
* Requires `allow-env` permission.
* @i18n 需要 `allow-env` 权限。
*/
export function osRelease(): string;
/** Exit the Deno process with optional exit code. If no exit code is supplied
* then Deno will exit with return code of 0.
* @i18n 退出 Deno 进程,可以指定退出码,若无则为 0。
*
* Deno.exit(5);
*/
export function exit(code?: number): never;
/** Returns a snapshot of the environment variables at invocation. Changing a
* property in the object will set that variable in the environment for the
* process. The environment object will only accept `string`s as values.
* @i18n 返回调用时环境变量的快照。如果更改环境变量对象的属性,则会在进程的环境中设置该属性。
* 环境变量对象只接受 `string` 类型的值。
*
* const myEnv = Deno.env();
* console.log(myEnv.SHELL);
* myEnv.TEST_VAR = "HELLO";
* const newEnv = Deno.env();
* console.log(myEnv.TEST_VAR === newEnv.TEST_VAR); //outputs "true"
*
* Requires `allow-env` permission.
* @i18n 需要 `allow-env` 权限。*/
export function env(): {
[index: string]: string;
};
/** Retrieve the value of an environment variable. Returns undefined if that
* key doesn't exist.
* @i18n 获取环境变量的值。如果 `key` 不存在,则返回 `undefined`。
*
* console.log(Deno.env("HOME")); //e.g. outputs "/home/alice"
* console.log(Deno.env("MADE_UP_VAR")); //outputs "Undefined"
*
* Requires `allow-env` permission.
* @i18n 需要 `allow-env` 权限。*/
export function env(key: string): string | undefined;
/** **UNSTABLE** */
export type DirKind =
| "home"
| "cache"
| "config"
| "executable"
| "data"
| "data_local"
| "audio"
| "desktop"
| "document"
| "download"
| "font"
| "picture"
| "public"
| "template"
| "tmp"
| "video";
/**
* **UNSTABLE**: Currently under evaluation to decide if method name `dir` and
* parameter type alias name `DirKind` should be renamed.
* @i18n **不稳定**: 当前正在评估中,以确定是否应重命名方法名 `dir` 和参数类型 `DirKind`。
*
* Returns the user and platform specific directories.
* @i18n 返回特定于用户和平台的目录。
*
* const homeDirectory = Deno.dir("home");
*
* Requires `allow-env` permission.
* @i18n 需要 `allow-env` 权限。
*
* Returns `null` if there is no applicable directory or if any other error
* occurs.
* @i18n 如果没有适用的目录或发生任何其他错误,则返回 `null`。
*
* Argument values: `"home"`, `"cache"`, `"config"`, `"executable"`, `"data"`,
* `"data_local"`, `"audio"`, `"desktop"`, `"document"`, `"download"`,
* `"font"`, `"picture"`, `"public"`, `"template"`, `"tmp"`, `"video"`
* @i18n 参数值包含:`"home"`, `"cache"`, `"config"`, `"executable"`, `"data"`,
* `"data_local"`, `"audio"`, `"desktop"`, `"document"`, `"download"`,
* `"font"`, `"picture"`, `"public"`, `"template"`, `"tmp"`, `"video"`
*
* `"home"`
*
* |平台 | 值 | 示例 |
* | ------- | -----------------------------------------| -----------------------|
* | Linux | `$HOME` | /home/alice |
* | macOS | `$HOME` | /Users/alice |
* | Windows | `{FOLDERID_Profile}` | C:\Users\Alice |
*
* `"cache"`
*
* |平台 | 值 | 示例 |
* | ------- | ----------------------------------- | ---------------------------- |
* | Linux | `$XDG_CACHE_HOME` or `$HOME`/.cache | /home/alice/.cache |
* | macOS | `$HOME`/Library/Caches | /Users/Alice/Library/Caches |
* | Windows | `{FOLDERID_LocalAppData}` | C:\Users\Alice\AppData\Local |
*
* `"config"`
*
* |平台 | 值 | 示例 |
* | ------- | ------------------------------------- | -------------------------------- |
* | Linux | `$XDG_CONFIG_HOME` or `$HOME`/.config | /home/alice/.config |
* | macOS | `$HOME`/Library/Preferences | /Users/Alice/Library/Preferences |
* | Windows | `{FOLDERID_RoamingAppData}` | C:\Users\Alice\AppData\Roaming |
*
* `"executable"`
*
* |平台 | 值 | 示例 |
* | ------- | --------------------------------------------------------------- | -----------------------|
* | Linux | `XDG_BIN_HOME` or `$XDG_DATA_HOME`/../bin or `$HOME`/.local/bin | /home/alice/.local/bin |
* | macOS | - | - |
* | Windows | - | - |
*
* `"data"`
*
* |平台 | 值 | 示例 |
* | ------- | ---------------------------------------- | ---------------------------------------- |
* | Linux | `$XDG_DATA_HOME` or `$HOME`/.local/share | /home/alice/.local/share |
* | macOS | `$HOME`/Library/Application Support | /Users/Alice/Library/Application Support |
* | Windows | `{FOLDERID_RoamingAppData}` | C:\Users\Alice\AppData\Roaming |
*
* `"data_local"`
*
* |平台 | 值 | 示例 |
* | ------- | ---------------------------------------- | ---------------------------------------- |
* | Linux | `$XDG_DATA_HOME` or `$HOME`/.local/share | /home/alice/.local/share |
* | macOS | `$HOME`/Library/Application Support | /Users/Alice/Library/Application Support |
* | Windows | `{FOLDERID_LocalAppData}` | C:\Users\Alice\AppData\Local |
*
* `"audio"`
*
* |平台 | 值 | 示例 |
* | ------- | ------------------ | -------------------- |
* | Linux | `XDG_MUSIC_DIR` | /home/alice/Music |
* | macOS | `$HOME`/Music | /Users/Alice/Music |
* | Windows | `{FOLDERID_Music}` | C:\Users\Alice\Music |
*
* `"desktop"`
*
* |平台 | 值 | 示例 |
* | ------- | -------------------- | ---------------------- |
* | Linux | `XDG_DESKTOP_DIR` | /home/alice/Desktop |
* | macOS | `$HOME`/Desktop | /Users/Alice/Desktop |
* | Windows | `{FOLDERID_Desktop}` | C:\Users\Alice\Desktop |
*
* `"document"`
*
* |平台 | 值 | 示例 |
* | ------- | ---------------------- | ------------------------ |
* | Linux | `XDG_DOCUMENTS_DIR` | /home/alice/Documents |
* | macOS | `$HOME`/Documents | /Users/Alice/Documents |
* | Windows | `{FOLDERID_Documents}` | C:\Users\Alice\Documents |
*
* `"download"`
*
* |平台 | 值 | 示例 |
* | ------- | ---------------------- | ------------------------ |
* | Linux | `XDG_DOWNLOAD_DIR` | /home/alice/Downloads |
* | macOS | `$HOME`/Downloads | /Users/Alice/Downloads |
* | Windows | `{FOLDERID_Downloads}` | C:\Users\Alice\Downloads |
*
* `"font"`
*
* |平台 | 值 | 示例 |
* | ------- | ---------------------------------------------------- | ------------------------------ |
* | Linux | `$XDG_DATA_HOME`/fonts or `$HOME`/.local/share/fonts | /home/alice/.local/share/fonts |
* | macOS | `$HOME/Library/Fonts` | /Users/Alice/Library/Fonts |
* | Windows | – | – |
*
* `"picture"`
*
* |平台 | 值 | 示例 |
* | ------- | --------------------- | ----------------------- |
* | Linux | `XDG_PICTURES_DIR` | /home/alice/Pictures |
* | macOS | `$HOME`/Pictures | /Users/Alice/Pictures |
* | Windows | `{FOLDERID_Pictures}` | C:\Users\Alice\Pictures |
*
* `"public"`
*
* |平台 | 值 | 示例 |
* | ------- | --------------------- | ------------------- |
* | Linux | `XDG_PUBLICSHARE_DIR` | /home/alice/Public |
* | macOS | `$HOME`/Public | /Users/Alice/Public |
* | Windows | `{FOLDERID_Public}` | C:\Users\Public |
*
* `"template"`
*
* |平台 | 值 | 示例 |
* | ------- | ---------------------- | ---------------------------------------------------------- |
* | Linux | `XDG_TEMPLATES_DIR` | /home/alice/Templates |
* | macOS | – | – |
* | Windows | `{FOLDERID_Templates}` | C:\Users\Alice\AppData\Roaming\Microsoft\Windows\Templates |
*
* `"tmp"`
*
* |平台 | 值 | 示例 |
* | ------- | ---------------------- | ---------------------------------------------------------- |
* | Linux | `TMPDIR` | /tmp |
* | macOS | `TMPDIR` | /tmp |
* | Windows | `{TMP}` | C:\Users\Alice\AppData\Local\Temp |
*
* `"video"`
*
* |平台 | 值 | 示例 |
* | ------- | ------------------- | --------------------- |
* | Linux | `XDG_VIDEOS_DIR` | /home/alice/Videos |
* | macOS | `$HOME`/Movies | /Users/Alice/Movies |
* | Windows | `{FOLDERID_Videos}` | C:\Users\Alice\Videos |
*
*/
export function dir(kind: DirKind): string | null;
/**
* Returns the path to the current deno executable.
* @i18n 返回当前 deno 可执行文件的路径。
*
* console.log(Deno.execPath()); //e.g. "/home/alice/.local/bin/deno"
*
* Requires `allow-env` permission.
* @i18n 需要 `allow-env` 权限。
*/
export function execPath(): string;
/**
* **UNSTABLE**: Currently under evaluation to decide if explicit permission is
* required to get the value of the current working directory.
* @i18n **不稳定**: 获取当前工作目录是否需要明确的权限,目前正在评估中。
*
* Return a string representing the current working directory.
* @i18n 返回当前工作目录的字符串。
*
* If the current directory can be reached via multiple paths (due to symbolic
* links), `cwd()` may return any one of them.
* @i18n 如果当前目录可以通过多个路径访问(由于符号链接导致),可能会返回其中任意一个。
*
* const currentWorkingDirectory = Deno.cwd();
*
* Throws `Deno.errors.NotFound` if directory not available.
* @i18n 如果目录不存在,则抛出 `Deno.errors.NotFound`。
*/
export function cwd(): string;
/**
* **UNSTABLE**: Currently under evaluation to decide if explicit permission is
* required to change the current working directory.
* @i18n **不稳定**: 更改当前工作目录是否需要明确的权限,目前正在评估中。
*
* Change the current working directory to the specified path.
* @i18n 将当前工作目录更改为指定路径。
*
* Deno.chdir("/home/userA");
* Deno.chdir("../userB");
* Deno.chdir("C:\\Program Files (x86)\\Java");
*
* Throws `Deno.errors.NotFound` if directory not found.
* Throws `Deno.errors.PermissionDenied` if the user does not have access
* rights
* @i18n 如果目录未被找到,则抛出 `Deno.errors.NotFound` 。
* 如果用户没有访问权限,则抛出 `Deno.errors.PermissionDenied` 。
*/
export function chdir(directory: string): void;
/**
* **UNSTABLE**: New API, yet to be vetted. This API is under consideration to
* determine if permissions are required to call it.
* @i18n **不稳定**: 新 API,没有经过审查。正在考虑调用此 API 时,是否需要申请权限。
*
* Retrieve the process umask. If `mask` is provided, sets the process umask.
* This call always returns what the umask was before the call.
* @i18n 获取进程权限掩码。如果提供 `mask`,则设置进程权限掩码。
* 此函数始终返回调用前的权限掩码。
*
* console.log(Deno.umask()); //e.g. 18 (0o022)
* const prevUmaskValue = Deno.umask(0o077); //e.g. 18 (0o022)
* console.log(Deno.umask()); //e.g. 63 (0o077)
*
* NOTE: This API is not implemented on Windows
* @i18n 注意: 此 API 未在 Windows 平台实现。
*/
export function umask(mask?: number): number;
/** **UNSTABLE**: might move to `Deno.symbols`.
* @i18n **不稳定**: 可能会移动到 `Deno.symbols`。*/
export const EOF: unique symbol;
export type EOF = typeof EOF;
/** **UNSTABLE**: might remove `"SEEK_"` prefix. Might not use all-caps.
* @i18n **不稳定**: 可能会移除 `"SEEK_"` 前缀。可能不使用全大写。*/
export enum SeekMode {
SEEK_START = 0,
SEEK_CURRENT = 1,
SEEK_END = 2,
}
/** **UNSTABLE**: might make `Reader` into iterator of some sort. */
export interface Reader {
/** Reads up to `p.byteLength` bytes into `p`. It resolves to the number of
* bytes read (`0` < `n` <= `p.byteLength`) and rejects if any error
* encountered. Even if `read()` resolves to `n` < `p.byteLength`, it may
* use all of `p` as scratch space during the call. If some data is
* available but not `p.byteLength` bytes, `read()` conventionally resolves
* to what is available instead of waiting for more.
* @i18n 最多读取 `p.byteLength` 个字节到p中,然后返回读取的字节数(`0 < n <= p.byteLength`),并在遇到任何错误时返回拒绝状态的回调函数。
* 即使 `read()` 返回值为 `n < p.byteLength`,p也可能在调用期间被用作临时空间。
* 如果有数据可用,但不存在 `p.byteLength`,`read()` 通常会返回可用值,而不是等待更多。
*
* When `read()` encounters end-of-file condition, it resolves to
* `Deno.EOF` symbol.
* @i18n 当 `read()` 遇到文件结束条件时,将返回 `Deno.EOF` 符号。
*
* When `read()` encounters an error, it rejects with an error.
* @i18n 当 `read()` 遇到错误时,它会返回拒绝状态的回调函数,参数值为错误信息。
*
* Callers should always process the `n` > `0` bytes returned before
* considering the `EOF`. Doing so correctly handles I/O errors that happen
* after reading some bytes and also both of the allowed EOF behaviors.
* @i18n 调用者应始终处理返回值为 `n > 0` 的情况,然后再考虑 `EOF`。
* 应正确处理在读取一些字节以及两种被允许的EOF行为之后可能发生的 I/O 错误。
*
* Implementations should not retain a reference to `p`.
* @i18n 实现不应保留对 `p` 的引用。
*/
read(p: Uint8Array): Promise<number | EOF>;
}
export interface ReaderSync {
/** Reads up to `p.byteLength` bytes into `p`. It resolves to the number
* of bytes read (`0` < `n` <= `p.byteLength`) and rejects if any error
* encountered. Even if `read()` returns `n` < `p.byteLength`, it may use
* all of `p` as scratch space during the call. If some data is available
* but not `p.byteLength` bytes, `read()` conventionally returns what is
* available instead of waiting for more.
* @i18n 最多读取 `p.byteLength` 个字节到p中,然后返回读取的字节数(`0 < n <= p.byteLength`),并在遇到任何错误时返回拒绝状态的回调函数。
* 即使 `readSync()` 返回值为 `n < p.byteLength`,p也可能在调用期间被用作临时空间。
* 如果有数据可用,但不存在 `p.byteLength`,`readSync()` 通常会返回可用值,而不是等待更多。
*
* When `readSync()` encounters end-of-file condition, it returns `Deno.EOF`
* symbol.
* @i18n 当 `readSync()` 遇到文件结束条件时,将返回 `Deno.EOF` 符号。
*
* When `readSync()` encounters an error, it throws with an error.
* @i18n 当 `readSync()` 遇到错误时,它会返回拒绝状态的回调函数,参数值为错误信息。
*
* Callers should always process the `n` > `0` bytes returned before
* considering the `EOF`. Doing so correctly handles I/O errors that happen
* after reading some bytes and also both of the allowed EOF behaviors.
* @i18n 调用者应始终处理返回值为 `n > 0` 的情况,然后再考虑 `EOF`。
* 应正确处理在读取一些字节以及两种被允许的EOF行为之后可能发生的 I/O 错误。
*
* Implementations should not retain a reference to `p`.
* @i18n 实现不应保留对 `p` 的引用。
*/
readSync(p: Uint8Array): number | EOF;
}
export interface Writer {
/** Writes `p.byteLength` bytes from `p` to the underlying data stream. It
* resolves to the number of bytes written from `p` (`0` <= `n` <=
* `p.byteLength`) or reject with the error encountered that caused the
* write to stop early. `write()` must reject with a non-null error if
* would resolve to `n` < `p.byteLength`. `write()` must not modify the
* slice data, even temporarily.
* @i18n 将 `p` 中的 `p.byteLength` 字节写入底层数据流。 它 resolve 时返回值为从 `p` 写入的
* 字节数(`0` <= `n` <= `p.byteLength`),reject 时返回值为导致写入提前停止的错误。
* 如果将要 resolve 一个 `n` < `p.byteLength` 的值时, `write()` 必须 reject,并且返回
* 一个非空错误。`write()` 禁止修改分片数据,即使是临时修改。
*
* Implementations should not retain a reference to `p`.
* @i18n 实现不应保留对 `p` 的引用。
*/
write(p: Uint8Array): Promise<number>;
}
export interface WriterSync {
/** Writes `p.byteLength` bytes from `p` to the underlying data
* stream. It returns the number of bytes written from `p` (`0` <= `n`
* <= `p.byteLength`) and any error encountered that caused the write to
* stop early. `writeSync()` must throw a non-null error if it returns `n` <
* `p.byteLength`. `writeSync()` must not modify the slice data, even
* temporarily.
* @i18n 将 `p` 中的 `p.byteLength` 字节写入底层数据流。它的返回值为从 `p` 写入的
* 字节数(`0` <= `n` <= `p.byteLength`)或者导致写入提前停止的错误。
* `writeSync()` 会抛出一个非空错误当返回值 `n` < `p.byteLength`。`writeSync()`
* 禁止修改分片数据,即使是临时修改。
*
* Implementations should not retain a reference to `p`.
* @i18n 实现不应保留对 `p` 的引用。
*/
writeSync(p: Uint8Array): number;
}
export interface Closer {
close(): void;
}
export interface Seeker {
/** Seek sets the offset for the next `read()` or `write()` to offset,
* interpreted according to `whence`: `SEEK_START` means relative to the
* start of the file, `SEEK_CURRENT` means relative to the current offset,
* and `SEEK_END` means relative to the end. Seek resolves to the new offset
* relative to the start of the file.
* @i18n 设置下一个 `read()` 或 `write()` 的偏移量,根据 `whence` 进行决定从哪个位置开始偏移:
* `SEEK_START` 表示相对于文件开头,`SEEK_CURRENT` 表示相对于当前位置,`SEEK_END` 表示相对于文件末尾。
* Seek 解析(resolve)的值为相对于文件开头的新偏移量。
*
* Seeking to an offset before the start of the file is an error. Seeking to
* any positive offset is legal, but the behavior of subsequent I/O
* operations on the underlying object is implementation-dependent.
* It returns the number of cursor position.
* @i18n 把偏移量设置到文件开始之前是错误的。
* 设置任何正偏移都是合法的,但是对于之后的 I/O 操作的行为则取决于实现。
* 它返回设置之后的偏移位置。
*/
seek(offset: number, whence: SeekMode): Promise<number>;
}
export interface ReadCloser extends Reader, Closer {}
export interface WriteCloser extends Writer, Closer {}
export interface ReadSeeker extends Reader, Seeker {}
export interface WriteSeeker extends Writer, Seeker {}
export interface ReadWriteCloser extends Reader, Writer, Closer {}
export interface ReadWriteSeeker extends Reader, Writer, Seeker {}
/** Copies from `src` to `dst` until either `EOF` is reached on `src` or an
* error occurs. It resolves to the number of bytes copied or rejects with
* the first error encountered while copying.
* @i18n 从 `src` 拷贝文件至 `dst`,拷贝至 `src` 的 `EOF` 或有异常出现时结束。
* `copy()` 函数返回一个 `Promise`, 成功时 resolve 并返回拷贝的字节数,失败时 reject 并返回拷贝过程中的首个异常。
*
* const source = await Deno.open("my_file.txt");
* const buffer = new Deno.Buffer()
* const bytesCopied1 = await Deno.copy(Deno.stdout, source);
* const bytesCopied2 = await Deno.copy(buffer, source);
*
* Because `copy()` is defined to read from `src` until `EOF`, it does not
* treat an `EOF` from `read()` as an error to be reported.
* @i18n 因为 `copy()` 函数在读到 `EOF` 时停止,所以不会将 `EOF` 视为异常(区别于 `read()` 函数)。
*
* @param dst The destination to copy to
* @param_i18n dst 需要拷贝至的目标位置
* @param src The source to copy from
* @param_i18n src 拷贝的源位置
*/
export function copy(dst: Writer, src: Reader): Promise<number>;
/** Turns a Reader, `r`, into an async iterator.
* @i18n 将 Reader 对象 (`r`) 转换为异步迭代器。
*
* for await (const chunk of toAsyncIterator(reader)) {
* console.log(chunk);
* }
*/
export function toAsyncIterator(r: Reader): AsyncIterableIterator<Uint8Array>;
/** Synchronously open a file and return an instance of `Deno.File`. The
* file does not need to previously exist if using the `create` or `createNew`
* open options. It is the callers responsibility to close the file when finished
* with it.
* @i18n 用同步方式打开一个文件并返回一个 `Deno.File` 实例。如果使用了 `create` 或 `createNew`配置项
* 文件可以不需要预先存在。调用者应该在完成后关闭文件。
*
* const file = Deno.openSync("/foo/bar.txt", { read: true, write: true });
* // Do work with file
* Deno.close(file.rid);
*
* Requires `allow-read` and/or `allow-write` permissions depending on options.
* @i18n 根据不同的配置需要相应的 `allow-read` 及 `allow-write` 权限。
*/
export function openSync(path: string, options?: OpenOptions): File;
/** Synchronously open a file and return an instance of `Deno.File`. The file
* may be created depending on the mode passed in. It is the callers responsibility
* to close the file when finished with it.
* @i18n 用同步方式打开一个文件并返回一个 `Deno.File` 实例。根据传入的模式,可以创建文件。
* 调用者应该在完成后关闭文件。
*
* const file = Deno.openSync("/foo/bar.txt", "r");
* // Do work with file
* Deno.close(file.rid);
*
* Requires `allow-read` and/or `allow-write` permissions depending on openMode.
* @i18n 根据不同的打开模式需要相应的 `allow-read` 及 `allow-write` 权限。
*/
export function openSync(path: string, openMode?: OpenMode): File;
/** Open a file and resolve to an instance of `Deno.File`. The
* file does not need to previously exist if using the `create` or `createNew`
* open options. It is the callers responsibility to close the file when finished
* with it.
* @i18n 打开一个文件并异步返回一个 `Deno.File` 实例。如果使用了 `create` 或 `createNew`配置项
* 文件可以不需要预先存在。调用者应该在完成后关闭文件。
*
* const file = await Deno.open("/foo/bar.txt", { read: true, write: true });
* // Do work with file
* Deno.close(file.rid);
*
* Requires `allow-read` and/or `allow-write` permissions depending on options.
* @i18n 根据不同的选项需要相应的 `allow-read` 及 `allow-write` 权限。
*/
export function open(path: string, options?: OpenOptions): Promise<File>;
/** Open a file and resolve to an instance of `Deno.File`. The file may be
* created depending on the mode passed in. It is the callers responsibility
* to close the file when finished with it.
* @i18n 打开一个文件并异步返回一个 `Deno.File` 实例。根据传入的模式,可以创建文件。
* 调用者应该在完成后关闭文件。
*
* const file = await Deno.open("/foo/bar.txt", "w+");
* // Do work with file
* Deno.close(file.rid);
*
* Requires `allow-read` and/or `allow-write` permissions depending on openMode.
* @i18n 根据不同的打开模式需要相应的 `allow-read` 及 `allow-write` 权限。
*/
export function open(path: string, openMode?: OpenMode): Promise<File>;
/** Creates a file if none exists or truncates an existing file and returns
* an instance of `Deno.File`.
* @i18n 创建文件并返回一个 `Deno.File` 实例,如果文件已存在则进行覆盖。
*
* const file = Deno.createSync("/foo/bar.txt");
*
* Requires `allow-read` and `allow-write` permissions.
* @i18n 需要 `allow-read` 和 `allow-write` 权限。
*/
export function createSync(path: string): File;
/** Creates a file if none exists or truncates an existing file and resolves to
* an instance of `Deno.File`.
* @i18n 创建文件并异步返回一个 `Deno.File` 实例,如果文件已存在则进行覆盖。
*
* const file = await Deno.create("/foo/bar.txt");
*
* Requires `allow-read` and `allow-write` permissions.
* @i18n 需要 `allow-read` 和 `allow-write` 权限。
*/
export function create(path: string): Promise<File>;
/** Synchronously read from a resource ID (`rid`) into an array buffer (`buffer`).
* @i18n 同步地从资源ID (`rid`) 读取内容,并写入到数组缓冲区 (`buffer`)。
*
* Returns either the number of bytes read during the operation or End Of File
* (`Symbol(EOF)`) if there was nothing to read.
* @i18n 如果没有要读取的内容,返回值为操作期间读取的字节数,或者文件结尾(`Symbol(EOF)`)。
*
* // 如果 "/foo/bar.txt" 文件里面有 "hello world":
* const file = Deno.openSync("/foo/bar.txt");
* const buf = new Uint8Array(100);
* const numberOfBytesRead = Deno.readSync(file.rid, buf); // 11 bytes
* const text = new TextDecoder().decode(buf); // "hello world"
* Deno.close(file.rid);
*/
export function readSync(rid: number, buffer: Uint8Array): number | EOF;
/** Read from a resource ID (`rid`) into an array buffer (`buffer`).
* @i18n 从资源ID (`rid`) 读取内容,并写入到数组缓冲区 (`buffer`)。
*
* Resolves to either the number of bytes read during the operation or End Of
* File (`Symbol(EOF)`) if there was nothing to read.
* @i18n 如果没有要读取的内容,返回值为操作期间读取的字节数,或者文件结尾(`Symbol(EOF)`)。
*
* // 如果 "/foo/bar.txt" 文件里面有 "hello world":
* const file = await Deno.open("/foo/bar.txt");
* const buf = new Uint8Array(100);
* const numberOfBytesRead = await Deno.read(file.rid, buf); // 11 bytes
* const text = new TextDecoder().decode(buf); // "hello world"
* Deno.close(file.rid);
*/
export function read(rid: number, buffer: Uint8Array): Promise<number | EOF>;
/** Synchronously write to the resource ID (`rid`) the contents of the array
* buffer (`data`).
* @i18n 同步地将数组缓冲区 (`data`) 的内容写入资源ID的所属文件 (`rid`) 。
*
* Returns the number of bytes written.
* @i18n 返回写入的字节数。
*
* const encoder = new TextEncoder();
* const data = encoder.encode("Hello world");
* const file = Deno.openSync("/foo/bar.txt");
* const bytesWritten = Deno.writeSync(file.rid, data); // 11
* Deno.close(file.rid);
*/
export function writeSync(rid: number, data: Uint8Array): number;
/** Write to the resource ID (`rid`) the contents of the array buffer (`data`).
* @i18n 将数组缓冲区 (`data`) 的内容写入资源ID的所属文件 (`rid`) 。
*
* Resolves to the number of bytes written.
* @i18n 解析为写入的字节数。
*
* const encoder = new TextEncoder();
* const data = encoder.encode("Hello world");
* const file = await Deno.open("/foo/bar.txt");
* const bytesWritten = await Deno.write(file.rid, data); // 11
* Deno.close(file.rid);
*/
export function write(rid: number, data: Uint8Array): Promise<number>;
/** Synchronously seek a resource ID (`rid`) to the given `offset` under mode
* given by `whence`. The new position within the resource (bytes from the
* start) is returned.
* @i18n 同步方式,在给定查询模式 `whence` 和偏移量 `offset` 的情况下,查找指定的资源 ID(`rid`)。
* 函数将解析并返回光标在资源中的新位置(从头开始的字节数)。
*
* const file = Deno.openSync('hello.txt', {read: true, write: true, truncate: true, create: true});
* Deno.writeSync(file.rid, new TextEncoder().encode("Hello world"));
* //advance cursor 6 bytes
* const cursorPosition = Deno.seekSync(file.rid, 6, Deno.SeekMode.SEEK_START);
* console.log(cursorPosition); // 6
* const buf = new Uint8Array(100);
* file.readSync(buf);
* console.log(new TextDecoder().decode(buf)); // "world"
*
* The seek modes work as follows:
* @i18n seek modes 的工作方式如下:
*
* // 给定内容为 "Hello world" 的 file.rid 文件,该文件长度为 11 个字节。
* // 从文件开头移动 6 个字节
* console.log(Deno.seekSync(file.rid, 6, Deno.SeekMode.SEEK_START)); //"6"
* // 从当前位置再移动 2 个字节
* console.log(Deno.seekSync(file.rid, 2, Deno.SeekMode.SEEK_CURRENT)); //"8"
* // 从文件末尾向后移动 2 个字节
* console.log(Deno.seekSync(file.rid, -2, Deno.SeekMode.SEEK_END)); //"9" (e.g. 11-2)
*/
export function seekSync(
rid: number,
offset: number,
whence: SeekMode
): number;
/** Seek a resource ID (`rid`) to the given `offset` under mode given by `whence`.
* The call resolves to the new position within the resource (bytes from the start).
* @i18n 在给定查询模式 `whence` 和偏移量 `offset` 的情况下,查找指定的资源 ID(`rid`)。
* 函数将解析并返回光标在资源中的新位置(从头开始的字节数)。
*
* const file = await Deno.open('hello.txt', {read: true, write: true, truncate: true, create: true});
* await Deno.write(file.rid, new TextEncoder().encode("Hello world"));
* // 光标前进 6 个字节
* const cursorPosition = await Deno.seek(file.rid, 6, Deno.SeekMode.SEEK_START);
* console.log(cursorPosition); // 6
* const buf = new Uint8Array(100);
* await file.read(buf);
* console.log(new TextDecoder().decode(buf)); // "world"
*
* The seek modes work as follows:
* @i18n seek modes 的工作方式如下:
*
* // 给定内容为 "Hello world" 的 file.rid 文件,该文件长度为 11 个字节。
* // 从文件开头移动 6 个字节
* console.log(await Deno.seek(file.rid, 6, Deno.SeekMode.SEEK_START)); //"6"
* // 从当前位置再移动 2 个字节
* console.log(await Deno.seek(file.rid, 2, Deno.SeekMode.SEEK_CURRENT)); //"8"
* // 从文件末尾向后移动 2 个字节
* console.log(await Deno.seek(file.rid, -2, Deno.SeekMode.SEEK_END)); //"9" (e.g. 11-2)
*/
export function seek(
rid: number,
offset: number,
whence: SeekMode
): Promise<number>;
/** Close the given resource ID (rid) which has been previously opened, such
* as via opening or creating a file. Closing a file when you are finished
* with it is important to avoid leaking resources.
* @i18n 使用给定的资源 ID (rid) 来关闭先前创建或打开的文件。
* 为避免资源泄露,事关重大,文件应当用完即关。
*
* const file = await Deno.open("my_file.txt");
* // 与 "file" 对象一起使用
* Deno.close(file.rid);
*/
export function close(rid: number): void;
/** The Deno abstraction for reading and writing files.
* @i18n 用于读取和写入文件的 Deno 抽象类。*/
export class File
implements
Reader,
ReaderSync,
Writer,
WriterSync,
Seeker,
Closer {
readonly rid: number;
constructor(rid: number);
write(p: Uint8Array): Promise<number>;
writeSync(p: Uint8Array): number;
read(p: Uint8Array): Promise<number | EOF>;
readSync(p: Uint8Array): number | EOF;
seek(offset: number, whence: SeekMode): Promise<number>;
seekSync(offset: number, whence: SeekMode): number;
close(): void;
}
/** An instance of `Deno.File` for `stdin`.
* @i18n 用于 `stdin` 的 `Deno.File` 实例。*/
export const stdin: File;
/** An instance of `Deno.File` for `stdout`.
* @i18n 用于 `stdout` 的 `Deno.File` 实例。*/
export const stdout: File;
/** An instance of `Deno.File` for `stderr`.
* @i18n 用于 `stderr` 的 `Deno.File` 实例。*/
export const stderr: File;
export interface OpenOptions {
/** Sets the option for read access. This option, when `true`, means that the
* file should be read-able if opened.
* @i18n 设置读取访问权限的选项。
* 当为 `true` 时,表示该文件在打开后即处于可读状态。*/
read?: boolean;
/** Sets the option for write access. This option, when `true`, means that
* the file should be write-able if opened. If the file already exists,
* any write calls on it will overwrite its contents, by default without
* truncating it.
* @i18n 设置写访问权限的选项。
* 当为 `true` 时,表示该文件在打开时即处于可写状态。
* 如果该文件已存在,则默认情况下,对该文件的任何写调用都将覆盖其内容,而不会截断该文件。*/
write?: boolean;
/**Sets the option for the append mode. This option, when `true`, means that
* writes will append to a file instead of overwriting previous contents.
* Note that setting `{ write: true, append: true }` has the same effect as
* setting only `{ append: true }`.
* @i18n 设置追加模式的选项。
* 当为 `true` 时,表示写入将追加到文件中,而不是覆盖先前的内容。
* 请注意,设置 `{ write: true, append: true }` 与仅设置 `{ append: true }` 具有相同的效果。*/
append?: boolean;
/** Sets the option for truncating a previous file. If a file is
* successfully opened with this option set it will truncate the file to `0`
* size if it already exists. The file must be opened with write access
* for truncate to work.
* @i18n 设置截断上一个文件的选项。
* 如果使用此选项后成功打开了文件,则文件的长度将被截断为 `0`(如果已存在)。
* 该文件必须具有写访问权限才能打开,才能进行截断。*/
truncate?: boolean;
/** Sets the option to allow creating a new file, if one doesn't already
* exist at the specified path. Requires write or append access to be
* used.
* @i18n 设置选项以允许创建新文件(如果指定路径尚不存在)。
* 需要使用写权限或追加权限。*/
create?: boolean;
/** Defaults to `false`. If set to `true`, no file, directory, or symlink is
* allowed to exist at the target location. Requires write or append
* access to be used. When createNew is set to `true`, create and truncate
* are ignored.
* @i18n 默认为 `false`。
* 如果设置为 `true`,则在目标位置不允许存在文件、目录或符号链接。
* 需要使用写权限或追加权限。
* 当 createNew 设置为 `true` 时,create 和 truncate 被忽略。*/
createNew?: boolean;
/** Permissions to use if creating the file (defaults to `0o666`, before
* the process's umask).
* Ignored on Windows.
* @i18n 创建文件时使用的权限(在进程调用 `umask` 之前默认为 `0o666`)。
* 在 Windows 上此选项被忽略。*/
mode?: number;
}
/** A set of string literals which specify how to open a file.
* @i18n 一组字符串文本,用于指定如何打开文件。
*
* |Value |Description |
* |------|--------------------------------------------------------------------------------------------------|
* |`"r"` |Read-only. Default. Starts at beginning of file. |
* |`"r+"`|Read-write. Start at beginning of file. |
* |`"w"` |Write-only. Opens and truncates existing file or creates new one for writing only. |
* |`"w+"`|Read-write. Opens and truncates existing file or creates new one for writing and reading. |
* |`"a"` |Write-only. Opens existing file or creates new one. Each write appends content to the end of file.|
* |`"a+"`|Read-write. Behaves like `"a"` and allows to read from file. |
* |`"x"` |Write-only. Exclusive create - creates new file only if one doesn't exist already. |
* |`"x+"`|Read-write. Behaves like `x` and allows reading from file. |
*
* @i18n
* |值 |描述 |
* |------|--------------------------------------------------------------------------------------------------|
* |`"r"` |只读。默认值。从文件开头开始。 |
* |`"r+"`|可读写。从文件开头开始。 |
* |`"w"` |仅写入。打开并截取现有文件或者创建一个仅写入权限的新文件。 |
* |`"w+"`|可读写。打开并截取现有文件或者创建一个可读写权限的新文件。 |
* |`"a"` |仅写入。打开现有文件或者创建新文件。每次写入都会将内容追加到文件末尾。 |
* |`"a+"`|可读写。行为类似于 `"a"` 并且允许从文件中读取。 |
* |`"x"` |仅写入。专属创建 - 仅在文件不存在时创建新文件。 |
* |`"x+"`|可读写。行为类似于 `"x"` 并且允许从文件中读取。 |
*/
export type OpenMode = "r" | "r+" | "w" | "w+" | "a" | "a+" | "x" | "x+";