fs-stream-sync 2.0.21 → 2.0.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/index.d.ts +55 -1
- package/index.js +15 -1
- package/index.js.map +1 -0
- package/lib/errors.d.ts +46 -0
- package/lib/errors.js +43 -0
- package/lib/errors.js.map +1 -0
- package/lib/interface.d.ts +84 -0
- package/lib/interface.js +7 -0
- package/lib/interface.js.map +1 -0
- package/lib/internal.d.ts +90 -0
- package/lib/internal.js +109 -0
- package/lib/internal.js.map +1 -0
- package/lib/util.js.map +1 -0
- package/package.json +2 -2
- package/read-sync.d.ts +73 -0
- package/read-sync.js +104 -8
- package/read-sync.js.map +1 -0
- package/read.d.ts +49 -0
- package/read.js +41 -0
- package/read.js.map +1 -0
- package/write-sync.d.ts +101 -1
- package/write-sync.js +93 -5
- package/write-sync.js.map +1 -0
- package/write.d.ts +83 -2
- package/write.js +52 -0
- package/write.js.map +1 -0
package/read.js
CHANGED
|
@@ -1,31 +1,72 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* fs-stream-sync - 檔案系統流處理工具
|
|
4
|
+
*
|
|
5
|
+
* 這個模組提供了檔案系統的同步和異步流處理功能,包括讀取和寫入流。
|
|
6
|
+
*
|
|
7
|
+
* @module fs-stream-sync
|
|
8
|
+
* @author bluelovers
|
|
9
|
+
*/
|
|
2
10
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
11
|
exports.ReadStream = void 0;
|
|
4
12
|
exports.createReadStream = createReadStream;
|
|
5
13
|
const tslib_1 = require("tslib");
|
|
6
14
|
const fs_1 = tslib_1.__importDefault(require("fs"));
|
|
7
15
|
const internal_1 = require("./lib/internal");
|
|
16
|
+
/**
|
|
17
|
+
* 讀取流類 - 擴展 Node.js 的 ReadStream
|
|
18
|
+
*
|
|
19
|
+
* 這個類提供了檔案讀取流的功能,支援同步和異步操作。
|
|
20
|
+
*
|
|
21
|
+
* @class ReadStream
|
|
22
|
+
* @extends fs.ReadStream
|
|
23
|
+
*/
|
|
8
24
|
// @ts-ignore
|
|
9
25
|
class ReadStream extends fs_1.default.ReadStream {
|
|
26
|
+
/**
|
|
27
|
+
* 構造函數
|
|
28
|
+
*
|
|
29
|
+
* @param {PathLike} path 檔案路徑
|
|
30
|
+
* @param {string | IFsWriteStreamOptions} [options] 選項
|
|
31
|
+
*/
|
|
10
32
|
constructor(path, options) {
|
|
33
|
+
// 調用父類構造函數
|
|
11
34
|
// @ts-ignore
|
|
12
35
|
super(path, options);
|
|
36
|
+
// 初始化流資料
|
|
13
37
|
(0, internal_1.getFsStreamData)(this);
|
|
14
38
|
}
|
|
39
|
+
/** 創建函數別名 */
|
|
15
40
|
static get create() {
|
|
16
41
|
return createReadStream;
|
|
17
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* 打開流
|
|
45
|
+
*
|
|
46
|
+
* 這個函數會打開檔案流,並設置相關狀態。
|
|
47
|
+
*/
|
|
18
48
|
open() {
|
|
19
49
|
if (typeof (0, internal_1.getFsStreamData)(this) !== 'boolean') {
|
|
20
50
|
this[internal_1.SYM_FS_STREAM_DATA].opened = true;
|
|
51
|
+
// 調用父類的 open 方法
|
|
21
52
|
// @ts-ignore
|
|
22
53
|
fs_1.default.ReadStream.prototype.open.call(this);
|
|
23
54
|
}
|
|
24
55
|
}
|
|
25
56
|
}
|
|
26
57
|
exports.ReadStream = ReadStream;
|
|
58
|
+
/**
|
|
59
|
+
* 創建讀取流
|
|
60
|
+
*
|
|
61
|
+
* 這個函數會創建一個 ReadStream 實例。
|
|
62
|
+
*
|
|
63
|
+
* @param {PathLike} path 檔案路徑
|
|
64
|
+
* @param {string | IFsReadStreamOptions} [options] 選項
|
|
65
|
+
* @returns {ReadStream} 讀取流實例
|
|
66
|
+
*/
|
|
27
67
|
function createReadStream(path, options) {
|
|
28
68
|
return new ReadStream(path, options);
|
|
29
69
|
}
|
|
70
|
+
/** 預設導出 - ReadStream 類 */
|
|
30
71
|
exports.default = ReadStream;
|
|
31
72
|
//# sourceMappingURL=read.js.map
|
package/read.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"read.js","sourceRoot":"","sources":["read.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;AAmGH,4CAGC;;AAnGD,oDAAoB;AAEpB,6CAAqE;AAErE;;;;;;;GAOG;AACH,aAAa;AACb,MAAa,UAAW,SAAQ,YAAE,CAAC,UAAU;IAmC5C;;;;;OAKG;IACH,YAAY,IAAc,EAAE,OAAwC;QAEnE,WAAW;QACX,aAAa;QACb,KAAK,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;QAEpB,SAAS;QACT,IAAA,0BAAe,EAAC,IAAI,CAAC,CAAA;IACtB,CAAC;IAED,aAAa;IACb,MAAM,KAAK,MAAM;QAEhB,OAAO,gBAAgB,CAAA;IACxB,CAAC;IAED;;;;OAIG;IACH,IAAI;QAEH,IAAI,OAAO,IAAA,0BAAe,EAAC,IAAI,CAAC,KAAK,SAAS,EAC9C,CAAC;YACA,IAAI,CAAC,6BAAkB,CAAC,CAAC,MAAM,GAAG,IAAI,CAAA;YACtC,gBAAgB;YAChB,aAAa;YACb,YAAE,CAAC,UAAU,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QACxC,CAAC;IACF,CAAC;CACD;AAxED,gCAwEC;AAED;;;;;;;;GAQG;AACH,SAAgB,gBAAgB,CAAC,IAAc,EAAE,OAAuC;IAEvF,OAAO,IAAI,UAAU,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;AACrC,CAAC;AAED,0BAA0B;AAC1B,kBAAe,UAAU,CAAA","sourcesContent":["/**\n * fs-stream-sync - 檔案系統流處理工具\n * \n * 這個模組提供了檔案系統的同步和異步流處理功能,包括讀取和寫入流。\n * \n * @module fs-stream-sync\n * @author bluelovers\n */\n\nimport { PathLike } from \"fs\";\nimport fs from 'fs';\nimport { IFsReadStreamOptions, IFsStreamData, IFsStreamState, IFsWriteStreamOptions } from './lib/interface';\nimport { getFsStreamData, SYM_FS_STREAM_DATA } from './lib/internal';\n\n/**\n * 讀取流類 - 擴展 Node.js 的 ReadStream\n * \n * 這個類提供了檔案讀取流的功能,支援同步和異步操作。\n * \n * @class ReadStream\n * @extends fs.ReadStream\n */\n// @ts-ignore\nexport class ReadStream extends fs.ReadStream\n{\n\t/** 是否自動關閉 */\n\tprotected autoClose: boolean\n\n\t/** 檔案標誌 */\n\tprotected flags: string\n\n\t/** 檔案描述符 */\n\tfd: number\n\n\t/** 檔案模式 */\n\tprotected mode: number\n\n\t/** 當前位置 */\n\tprotected pos: number\n\n\t/** 是否已關閉 */\n\tprotected override closed: boolean\n\n\t/** 是否已銷毀 */\n\tprotected override destroyed: boolean\n\n\t/** 可寫狀態 */\n\tprotected _writableState: IFsStreamState\n\n\t/** 可讀狀態 */\n\tprotected _readableState: IFsStreamState\n\n\t/** 流資料 */\n\tprotected [SYM_FS_STREAM_DATA]: IFsStreamData\n\n\t/** 結束位置 */\n\tprotected end: number\n\n\t/**\n\t * 構造函數\n\t * \n\t * @param {PathLike} path 檔案路徑\n\t * @param {string | IFsWriteStreamOptions} [options] 選項\n\t */\n\tconstructor(path: PathLike, options?: string | IFsWriteStreamOptions)\n\t{\n\t\t// 調用父類構造函數\n\t\t// @ts-ignore\n\t\tsuper(path, options)\n\n\t\t// 初始化流資料\n\t\tgetFsStreamData(this)\n\t}\n\n\t/** 創建函數別名 */\n\tstatic get create()\n\t{\n\t\treturn createReadStream\n\t}\n\n\t/**\n\t * 打開流\n\t * \n\t * 這個函數會打開檔案流,並設置相關狀態。\n\t */\n\topen()\n\t{\n\t\tif (typeof getFsStreamData(this) !== 'boolean')\n\t\t{\n\t\t\tthis[SYM_FS_STREAM_DATA].opened = true\n\t\t\t// 調用父類的 open 方法\n\t\t\t// @ts-ignore\n\t\t\tfs.ReadStream.prototype.open.call(this)\n\t\t}\n\t}\n}\n\n/**\n * 創建讀取流\n * \n * 這個函數會創建一個 ReadStream 實例。\n * \n * @param {PathLike} path 檔案路徑\n * @param {string | IFsReadStreamOptions} [options] 選項\n * @returns {ReadStream} 讀取流實例\n */\nexport function createReadStream(path: PathLike, options?: string | IFsReadStreamOptions)\n{\n\treturn new ReadStream(path, options)\n}\n\n/** 預設導出 - ReadStream 類 */\nexport default ReadStream\n\n"]}
|
package/write-sync.d.ts
CHANGED
|
@@ -1,18 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 同步寫入流實作
|
|
3
|
+
* Synchronous Write Stream Implementation
|
|
4
|
+
*
|
|
5
|
+
* 提供同步檔案寫入流功能
|
|
6
|
+
* Provides synchronous file writing stream functionality
|
|
7
|
+
*
|
|
8
|
+
* @module fs-stream-sync/write-sync
|
|
9
|
+
*/
|
|
1
10
|
import { PathLike } from "fs";
|
|
2
11
|
import { IFsWriteStreamOptions } from './lib/interface';
|
|
3
12
|
import { WriteStream } from './write';
|
|
13
|
+
/**
|
|
14
|
+
* 同步寫入流類別
|
|
15
|
+
* Synchronous Write Stream Class
|
|
16
|
+
*
|
|
17
|
+
* 提供同步檔案寫入功能,繼承自 WriteStream
|
|
18
|
+
* Provides synchronous file writing functionality, extends WriteStream
|
|
19
|
+
*/
|
|
4
20
|
export declare class SyncWriteStream extends WriteStream {
|
|
21
|
+
/**
|
|
22
|
+
* 構造函數
|
|
23
|
+
* Constructor
|
|
24
|
+
*
|
|
25
|
+
* @param {PathLike} path - 檔案路徑 / File path
|
|
26
|
+
* @param {string | IFsWriteStreamOptions} [options] - 選項 / Options
|
|
27
|
+
*/
|
|
5
28
|
constructor(path: PathLike, options?: string | IFsWriteStreamOptions);
|
|
29
|
+
/**
|
|
30
|
+
* 建立函數別名
|
|
31
|
+
* Factory method alias
|
|
32
|
+
*/
|
|
6
33
|
static get create(): typeof createSyncWriteStream;
|
|
34
|
+
/**
|
|
35
|
+
* 開啟檔案流
|
|
36
|
+
* Open file stream
|
|
37
|
+
*
|
|
38
|
+
* 同步開啟檔案並發送 open 和 ready 事件
|
|
39
|
+
* Opens file synchronously and emits open and ready events
|
|
40
|
+
*/
|
|
7
41
|
open(): void;
|
|
42
|
+
/**
|
|
43
|
+
* 寫入資料
|
|
44
|
+
* Write data
|
|
45
|
+
*
|
|
46
|
+
* 將資料寫入流,檢查流狀態後呼叫父類方法
|
|
47
|
+
* Writes data to stream, checks stream state before calling parent method
|
|
48
|
+
*
|
|
49
|
+
* @param {any} chunk - 要寫入的資料 / Data to write
|
|
50
|
+
* @param {(error: Error | null | undefined) => void} [cb] - 回調函數 / Callback function
|
|
51
|
+
* @returns {boolean} 寫入結果 / Write result
|
|
52
|
+
*/
|
|
8
53
|
write(chunk: any, cb?: (error: Error | null | undefined) => void): boolean;
|
|
54
|
+
/**
|
|
55
|
+
* 寫入資料(帶編碼)
|
|
56
|
+
* Write data with encoding
|
|
57
|
+
*
|
|
58
|
+
* @param {any} chunk - 要寫入的資料 / Data to write
|
|
59
|
+
* @param {string} [encoding] - 編碼格式 / Encoding
|
|
60
|
+
* @param {(error: Error | null | undefined) => void} [cb] - 回調函數 / Callback function
|
|
61
|
+
* @returns {boolean} 寫入結果 / Write result
|
|
62
|
+
*/
|
|
9
63
|
write(chunk: any, encoding?: string, cb?: (error: Error | null | undefined) => void): boolean;
|
|
10
64
|
/**
|
|
11
|
-
*
|
|
65
|
+
* 內部寫入方法
|
|
66
|
+
* Internal write method
|
|
67
|
+
*
|
|
68
|
+
* 同步寫入資料到檔案
|
|
69
|
+
* Writes data to file synchronously
|
|
70
|
+
*
|
|
71
|
+
* @fixme 存在未知錯誤導致 stream.write 只能執行一次
|
|
72
|
+
* @fixme Unknown bug makes stream.write only run once
|
|
73
|
+
*
|
|
74
|
+
* @param {Buffer} chunk - 要寫入的緩衝區 / Buffer to write
|
|
75
|
+
* @param {string} encoding - 編碼格式(未使用)/ Encoding (unused)
|
|
76
|
+
* @param {Function} callback - 完成回調 / Completion callback
|
|
77
|
+
* @returns {boolean | this} 寫入結果 / Write result
|
|
12
78
|
*/
|
|
13
79
|
_write(chunk: Buffer, encoding: string, callback: Function): boolean | this;
|
|
80
|
+
/**
|
|
81
|
+
* 關閉流
|
|
82
|
+
* Close stream
|
|
83
|
+
*
|
|
84
|
+
* 關閉檔案流並發送 close 事件
|
|
85
|
+
* Closes file stream and emits close event
|
|
86
|
+
*
|
|
87
|
+
* @param {Function} [cb] - 關閉回調 / Close callback
|
|
88
|
+
*/
|
|
14
89
|
close(cb?: Function): void;
|
|
90
|
+
/**
|
|
91
|
+
* 銷毀流
|
|
92
|
+
* Destroy stream
|
|
93
|
+
*
|
|
94
|
+
* 清理資源並關閉檔案描述符
|
|
95
|
+
* Cleans up resources and closes file descriptor
|
|
96
|
+
*
|
|
97
|
+
* @param {Error | null} error - 錯誤物件或 null / Error object or null
|
|
98
|
+
* @param {(error: Error | null) => void} callback - 完成回調 / Completion callback
|
|
99
|
+
*/
|
|
15
100
|
_destroy(error: Error | null, callback: (error: Error | null) => void): void;
|
|
16
101
|
}
|
|
102
|
+
/**
|
|
103
|
+
* 建立同步寫入流
|
|
104
|
+
* Create synchronous write stream
|
|
105
|
+
*
|
|
106
|
+
* 工廠函數,建立 SyncWriteStream 實例
|
|
107
|
+
* Factory function that creates a SyncWriteStream instance
|
|
108
|
+
*
|
|
109
|
+
* @param {PathLike} path - 檔案路徑 / File path
|
|
110
|
+
* @param {string | IFsWriteStreamOptions} [options] - 選項 / Options
|
|
111
|
+
* @returns {SyncWriteStream} 同步寫入流實例 / Synchronous write stream instance
|
|
112
|
+
*/
|
|
17
113
|
export declare function createSyncWriteStream(path: PathLike, options?: string | IFsWriteStreamOptions): SyncWriteStream;
|
|
114
|
+
/**
|
|
115
|
+
* 預設導出 - SyncWriteStream 類別
|
|
116
|
+
* Default export - SyncWriteStream class
|
|
117
|
+
*/
|
|
18
118
|
export default SyncWriteStream;
|
package/write-sync.js
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 同步寫入流實作
|
|
3
|
+
* Synchronous Write Stream Implementation
|
|
4
|
+
*
|
|
5
|
+
* 提供同步檔案寫入流功能
|
|
6
|
+
* Provides synchronous file writing stream functionality
|
|
7
|
+
*
|
|
8
|
+
* @module fs-stream-sync/write-sync
|
|
9
|
+
*/
|
|
1
10
|
'use strict';
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
exports.SyncWriteStream = void 0;
|
|
@@ -8,14 +17,39 @@ const errors_1 = require("./lib/errors");
|
|
|
8
17
|
const internal_1 = require("./lib/internal");
|
|
9
18
|
const internal_2 = tslib_1.__importDefault(require("./lib/internal"));
|
|
10
19
|
const write_1 = require("./write");
|
|
20
|
+
/**
|
|
21
|
+
* 同步寫入流類別
|
|
22
|
+
* Synchronous Write Stream Class
|
|
23
|
+
*
|
|
24
|
+
* 提供同步檔案寫入功能,繼承自 WriteStream
|
|
25
|
+
* Provides synchronous file writing functionality, extends WriteStream
|
|
26
|
+
*/
|
|
11
27
|
class SyncWriteStream extends write_1.WriteStream {
|
|
28
|
+
/**
|
|
29
|
+
* 構造函數
|
|
30
|
+
* Constructor
|
|
31
|
+
*
|
|
32
|
+
* @param {PathLike} path - 檔案路徑 / File path
|
|
33
|
+
* @param {string | IFsWriteStreamOptions} [options] - 選項 / Options
|
|
34
|
+
*/
|
|
12
35
|
constructor(path, options) {
|
|
13
36
|
// @ts-ignore
|
|
14
37
|
super(path, options);
|
|
15
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* 建立函數別名
|
|
41
|
+
* Factory method alias
|
|
42
|
+
*/
|
|
16
43
|
static get create() {
|
|
17
44
|
return createSyncWriteStream;
|
|
18
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* 開啟檔案流
|
|
48
|
+
* Open file stream
|
|
49
|
+
*
|
|
50
|
+
* 同步開啟檔案並發送 open 和 ready 事件
|
|
51
|
+
* Opens file synchronously and emits open and ready events
|
|
52
|
+
*/
|
|
19
53
|
open() {
|
|
20
54
|
if (typeof (0, internal_1.getFsStreamData)(this) !== 'boolean') {
|
|
21
55
|
this[internal_1.SYM_FS_STREAM_DATA].opened = true;
|
|
@@ -34,6 +68,7 @@ class SyncWriteStream extends write_1.WriteStream {
|
|
|
34
68
|
throw new NodeLikeError(EnumFsStreamErrorCode.ERR_STREAM_WRITE_AFTER_END, `write after end`)
|
|
35
69
|
}
|
|
36
70
|
*/
|
|
71
|
+
// 檢查流是否已銷毀 / Check if stream is destroyed
|
|
37
72
|
if (this._writableState.destroyed) {
|
|
38
73
|
throw new errors_1.NodeLikeError(errors_1.EnumFsStreamErrorCode.ERR_STREAM_DESTROYED, `Cannot call write after a stream was destroyed`);
|
|
39
74
|
}
|
|
@@ -41,57 +76,110 @@ class SyncWriteStream extends write_1.WriteStream {
|
|
|
41
76
|
return super.write(chunk, ...argv);
|
|
42
77
|
}
|
|
43
78
|
/**
|
|
44
|
-
*
|
|
79
|
+
* 內部寫入方法
|
|
80
|
+
* Internal write method
|
|
81
|
+
*
|
|
82
|
+
* 同步寫入資料到檔案
|
|
83
|
+
* Writes data to file synchronously
|
|
84
|
+
*
|
|
85
|
+
* @fixme 存在未知錯誤導致 stream.write 只能執行一次
|
|
86
|
+
* @fixme Unknown bug makes stream.write only run once
|
|
87
|
+
*
|
|
88
|
+
* @param {Buffer} chunk - 要寫入的緩衝區 / Buffer to write
|
|
89
|
+
* @param {string} encoding - 編碼格式(未使用)/ Encoding (unused)
|
|
90
|
+
* @param {Function} callback - 完成回調 / Completion callback
|
|
91
|
+
* @returns {boolean | this} 寫入結果 / Write result
|
|
45
92
|
*/
|
|
46
93
|
_write(chunk, encoding, callback) {
|
|
47
94
|
let self = this;
|
|
48
95
|
//console.dir({chunk, encoding, callback} );
|
|
96
|
+
// 驗證資料類型 / Validate data type
|
|
49
97
|
if (!(chunk instanceof Buffer)) {
|
|
50
98
|
return this.emit('error', new Error('Invalid data'));
|
|
51
99
|
}
|
|
100
|
+
// 等待檔案開啟 / Wait for file to open
|
|
52
101
|
if (typeof this.fd !== 'number') {
|
|
53
102
|
return this.once('open', function () {
|
|
54
103
|
self._write(chunk, encoding, callback);
|
|
55
104
|
});
|
|
56
105
|
}
|
|
57
106
|
try {
|
|
107
|
+
// 同步寫入檔案 / Write to file synchronously
|
|
58
108
|
let bytes = fs_1.default.writeSync(this.fd, chunk, 0, chunk.length, this.pos);
|
|
59
109
|
this.bytesWritten += bytes;
|
|
60
110
|
}
|
|
61
111
|
catch (e) {
|
|
112
|
+
// 發生錯誤時呼叫錯誤回調 / Call error callback on failure
|
|
62
113
|
internal_2.default._error_callback(this, e, callback);
|
|
63
114
|
}
|
|
115
|
+
// 更新寫入位置 / Update write position
|
|
64
116
|
if (this.pos !== undefined) {
|
|
65
117
|
this.pos += chunk.length;
|
|
66
118
|
}
|
|
67
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* 關閉流
|
|
122
|
+
* Close stream
|
|
123
|
+
*
|
|
124
|
+
* 關閉檔案流並發送 close 事件
|
|
125
|
+
* Closes file stream and emits close event
|
|
126
|
+
*
|
|
127
|
+
* @param {Function} [cb] - 關閉回調 / Close callback
|
|
128
|
+
*/
|
|
68
129
|
close(cb) {
|
|
69
130
|
if (cb) {
|
|
70
131
|
if (this.closed) {
|
|
132
|
+
// 已關閉,直接執行回調 / Already closed, execute callback directly
|
|
71
133
|
cb();
|
|
72
134
|
return;
|
|
73
135
|
}
|
|
74
136
|
else {
|
|
137
|
+
// 監聽 close 事件 / Listen for close event
|
|
75
138
|
// @ts-ignore
|
|
76
139
|
this.on('close', cb);
|
|
77
140
|
}
|
|
78
141
|
}
|
|
79
|
-
//
|
|
80
|
-
// destroy on 'finish'
|
|
142
|
+
// 若非自動關閉模式,在 finish 時銷毀
|
|
143
|
+
// If not auto-closing, destroy on 'finish'
|
|
81
144
|
if (!this.autoClose) {
|
|
82
145
|
this.on('finish', this.destroy.bind(this));
|
|
83
146
|
}
|
|
84
|
-
//
|
|
85
|
-
// https://github.com/nodejs/node/issues/2006
|
|
147
|
+
// 使用 end() 而非 destroy() 因為 Node.js issue #2006
|
|
148
|
+
// Use end() instead of destroy() due to https://github.com/nodejs/node/issues/2006
|
|
86
149
|
this.end();
|
|
87
150
|
}
|
|
151
|
+
/**
|
|
152
|
+
* 銷毀流
|
|
153
|
+
* Destroy stream
|
|
154
|
+
*
|
|
155
|
+
* 清理資源並關閉檔案描述符
|
|
156
|
+
* Cleans up resources and closes file descriptor
|
|
157
|
+
*
|
|
158
|
+
* @param {Error | null} error - 錯誤物件或 null / Error object or null
|
|
159
|
+
* @param {(error: Error | null) => void} callback - 完成回調 / Completion callback
|
|
160
|
+
*/
|
|
88
161
|
_destroy(error, callback) {
|
|
89
162
|
internal_2.default._destroy(this, error, callback);
|
|
90
163
|
}
|
|
91
164
|
}
|
|
92
165
|
exports.SyncWriteStream = SyncWriteStream;
|
|
166
|
+
/**
|
|
167
|
+
* 建立同步寫入流
|
|
168
|
+
* Create synchronous write stream
|
|
169
|
+
*
|
|
170
|
+
* 工廠函數,建立 SyncWriteStream 實例
|
|
171
|
+
* Factory function that creates a SyncWriteStream instance
|
|
172
|
+
*
|
|
173
|
+
* @param {PathLike} path - 檔案路徑 / File path
|
|
174
|
+
* @param {string | IFsWriteStreamOptions} [options] - 選項 / Options
|
|
175
|
+
* @returns {SyncWriteStream} 同步寫入流實例 / Synchronous write stream instance
|
|
176
|
+
*/
|
|
93
177
|
function createSyncWriteStream(path, options) {
|
|
94
178
|
return new SyncWriteStream(path, options);
|
|
95
179
|
}
|
|
180
|
+
/**
|
|
181
|
+
* 預設導出 - SyncWriteStream 類別
|
|
182
|
+
* Default export - SyncWriteStream class
|
|
183
|
+
*/
|
|
96
184
|
exports.default = SyncWriteStream;
|
|
97
185
|
//# sourceMappingURL=write-sync.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"write-sync.js","sourceRoot":"","sources":["write-sync.ts"],"names":[],"mappings":"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","sourcesContent":["/**\n * 同步寫入流實作\n * Synchronous Write Stream Implementation\n *\n * 提供同步檔案寫入流功能\n * Provides synchronous file writing stream functionality\n *\n * @module fs-stream-sync/write-sync\n */\n\n'use strict';\n\nimport fs from \"fs\";\nimport { PathLike } from \"fs\";\nimport { EnumFsStreamErrorCode, NodeLikeError } from './lib/errors';\nimport {\n\tIFsStream,\n\tIFsStreamState,\n\tIFsStreamOptions,\n\tIFsWriteStreamOptions,\n\tIFsStreamData,\n\tIFsPath,\n} from './lib/interface';\nimport { getFsStreamData, SYM_FS_STREAM_DATA } from './lib/internal';\nimport internal from \"./lib/internal\";\nimport { WriteStream } from './write'\n\n/**\n * 同步寫入流類別\n * Synchronous Write Stream Class\n *\n * 提供同步檔案寫入功能,繼承自 WriteStream\n * Provides synchronous file writing functionality, extends WriteStream\n */\nexport class SyncWriteStream extends WriteStream\n{\n\t/**\n\t * 構造函數\n\t * Constructor\n\t *\n\t * @param {PathLike} path - 檔案路徑 / File path\n\t * @param {string | IFsWriteStreamOptions} [options] - 選項 / Options\n\t */\n\tconstructor(path: PathLike, options?: string | IFsWriteStreamOptions)\n\t{\n\t\t// @ts-ignore\n\t\tsuper(path, options)\n\t}\n\n\t/**\n\t * 建立函數別名\n\t * Factory method alias\n\t */\n\tstatic override get create()\n\t{\n\t\treturn createSyncWriteStream\n\t}\n\n\t/**\n\t * 開啟檔案流\n\t * Open file stream\n\t *\n\t * 同步開啟檔案並發送 open 和 ready 事件\n\t * Opens file synchronously and emits open and ready events\n\t */\n\toverride open(): void\n\t{\n\t\tif (typeof getFsStreamData(this) !== 'boolean')\n\t\t{\n\t\t\tthis[SYM_FS_STREAM_DATA].opened = true\n\t\t\tinternal.open(this)\n\t\t}\n\t\telse if (this[SYM_FS_STREAM_DATA].opened === true)\n\t\t{\n\t\t\tthis[SYM_FS_STREAM_DATA].opened = false\n\t\t\tthis.emit('open', this.fd);\n\t\t\tthis.emit('ready');\n\t\t}\n\t}\n\n\t/**\n\t * 寫入資料\n\t * Write data\n\t *\n\t * 將資料寫入流,檢查流狀態後呼叫父類方法\n\t * Writes data to stream, checks stream state before calling parent method\n\t *\n\t * @param {any} chunk - 要寫入的資料 / Data to write\n\t * @param {(error: Error | null | undefined) => void} [cb] - 回調函數 / Callback function\n\t * @returns {boolean} 寫入結果 / Write result\n\t */\n\toverride write(chunk: any, cb?: (error: Error | null | undefined) => void): boolean;\n\t/**\n\t * 寫入資料(帶編碼)\n\t * Write data with encoding\n\t *\n\t * @param {any} chunk - 要寫入的資料 / Data to write\n\t * @param {string} [encoding] - 編碼格式 / Encoding\n\t * @param {(error: Error | null | undefined) => void} [cb] - 回調函數 / Callback function\n\t * @returns {boolean} 寫入結果 / Write result\n\t */\n\toverride write(chunk: any, encoding?: string, cb?: (error: Error | null | undefined) => void): boolean;\n\toverride write(chunk: any, ...argv)\n\t{\n\t\t/*\n\t\tif (this.closed)\n\t\t{\n\t\t\tthrow new NodeLikeError(EnumFsStreamErrorCode.ERR_STREAM_WRITE_AFTER_END, `write after end`)\n\t\t}\n\t\t*/\n\t\t// 檢查流是否已銷毀 / Check if stream is destroyed\n\t\tif (this._writableState.destroyed)\n\t\t{\n\t\t\tthrow new NodeLikeError(EnumFsStreamErrorCode.ERR_STREAM_DESTROYED, `Cannot call write after a stream was destroyed`)\n\t\t}\n\n\t\t//console.dir({chunk,argv} );\n\n\t\treturn super.write(chunk, ...argv)\n\t}\n\n\t/**\n\t * 內部寫入方法\n\t * Internal write method\n\t *\n\t * 同步寫入資料到檔案\n\t * Writes data to file synchronously\n\t *\n\t * @fixme 存在未知錯誤導致 stream.write 只能執行一次\n\t * @fixme Unknown bug makes stream.write only run once\n\t *\n\t * @param {Buffer} chunk - 要寫入的緩衝區 / Buffer to write\n\t * @param {string} encoding - 編碼格式(未使用)/ Encoding (unused)\n\t * @param {Function} callback - 完成回調 / Completion callback\n\t * @returns {boolean | this} 寫入結果 / Write result\n\t */\n\toverride _write(chunk: Buffer, encoding: string, callback: Function)\n\t{\n\t\tlet self = this\n\n\t\t//console.dir({chunk, encoding, callback} );\n\n\t\t// 驗證資料類型 / Validate data type\n\t\tif (!(chunk instanceof Buffer))\n\t\t{\n\t\t\treturn this.emit('error', new Error('Invalid data'));\n\t\t}\n\n\t\t// 等待檔案開啟 / Wait for file to open\n\t\tif (typeof this.fd !== 'number')\n\t\t{\n\t\t\treturn this.once('open', function ()\n\t\t\t{\n\t\t\t\tself._write(chunk, encoding, callback);\n\t\t\t});\n\t\t}\n\n\t\ttry\n\t\t{\n\t\t\t// 同步寫入檔案 / Write to file synchronously\n\t\t\tlet bytes = fs.writeSync(this.fd, chunk, 0, chunk.length, this.pos);\n\n\t\t\tthis.bytesWritten += bytes;\n\t\t}\n\t\tcatch (e)\n\t\t{\n\t\t\t// 發生錯誤時呼叫錯誤回調 / Call error callback on failure\n\t\t\tinternal._error_callback(this, e, callback)\n\t\t}\n\n\t\t// 更新寫入位置 / Update write position\n\t\tif (this.pos !== undefined)\n\t\t{\n\t\t\tthis.pos += chunk.length;\n\t\t}\n\t}\n\n\t/**\n\t * 關閉流\n\t * Close stream\n\t *\n\t * 關閉檔案流並發送 close 事件\n\t * Closes file stream and emits close event\n\t *\n\t * @param {Function} [cb] - 關閉回調 / Close callback\n\t */\n\toverride close(cb?: Function)\n\t{\n\t\tif (cb)\n\t\t{\n\t\t\tif (this.closed)\n\t\t\t{\n\t\t\t\t// 已關閉,直接執行回調 / Already closed, execute callback directly\n\t\t\t\tcb();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\t// 監聽 close 事件 / Listen for close event\n\t\t\t\t// @ts-ignore\n\t\t\t\tthis.on('close', cb);\n\t\t\t}\n\t\t}\n\n\t\t// 若非自動關閉模式,在 finish 時銷毀\n\t\t// If not auto-closing, destroy on 'finish'\n\t\tif (!this.autoClose)\n\t\t{\n\t\t\tthis.on('finish', this.destroy.bind(this));\n\t\t}\n\n\t\t// 使用 end() 而非 destroy() 因為 Node.js issue #2006\n\t\t// Use end() instead of destroy() due to https://github.com/nodejs/node/issues/2006\n\t\tthis.end();\n\t}\n\n\t/**\n\t * 銷毀流\n\t * Destroy stream\n\t *\n\t * 清理資源並關閉檔案描述符\n\t * Cleans up resources and closes file descriptor\n\t *\n\t * @param {Error | null} error - 錯誤物件或 null / Error object or null\n\t * @param {(error: Error | null) => void} callback - 完成回調 / Completion callback\n\t */\n\toverride _destroy(error: Error | null, callback: (error: Error | null) => void): void\n\t{\n\t\tinternal._destroy(this, error, callback)\n\t}\n}\n\n/**\n * 建立同步寫入流\n * Create synchronous write stream\n *\n * 工廠函數,建立 SyncWriteStream 實例\n * Factory function that creates a SyncWriteStream instance\n *\n * @param {PathLike} path - 檔案路徑 / File path\n * @param {string | IFsWriteStreamOptions} [options] - 選項 / Options\n * @returns {SyncWriteStream} 同步寫入流實例 / Synchronous write stream instance\n */\nexport function createSyncWriteStream(path: PathLike, options?: string | IFsWriteStreamOptions)\n{\n\treturn new SyncWriteStream(path, options)\n}\n\n/**\n * 預設導出 - SyncWriteStream 類別\n * Default export - SyncWriteStream class\n */\nexport default SyncWriteStream\n"]}
|
package/write.d.ts
CHANGED
|
@@ -1,29 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 寫入流實作
|
|
3
|
+
* Write Stream Implementation
|
|
4
|
+
*
|
|
5
|
+
* 提供非同步檔案寫入流功能,擴展 Node.js fs.WriteStream
|
|
6
|
+
* Provides asynchronous file writing stream functionality, extends Node.js fs.WriteStream
|
|
7
|
+
*
|
|
8
|
+
* @module fs-stream-sync/write
|
|
9
|
+
*/
|
|
1
10
|
import fs from "fs";
|
|
2
11
|
import { PathLike } from 'fs';
|
|
3
12
|
import { IFsPath, IFsStreamData, IFsStreamState, IFsWriteStreamOptions } from './lib/interface';
|
|
4
13
|
import { SYM_FS_STREAM_DATA } from './lib/internal';
|
|
14
|
+
/**
|
|
15
|
+
* 寫入流類別
|
|
16
|
+
* Write Stream Class
|
|
17
|
+
*
|
|
18
|
+
* 擴展 Node.js fs.WriteStream,提供額外功能
|
|
19
|
+
* Extends Node.js fs.WriteStream with additional functionality
|
|
20
|
+
*/
|
|
5
21
|
export declare class WriteStream extends fs.WriteStream {
|
|
22
|
+
/**
|
|
23
|
+
* 自動關閉標誌 / Auto close flag
|
|
24
|
+
*/
|
|
6
25
|
protected autoClose: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* 檔案開啟標誌 / File open flags
|
|
28
|
+
*/
|
|
7
29
|
protected flags: string;
|
|
30
|
+
/**
|
|
31
|
+
* 檔案描述符 / File descriptor
|
|
32
|
+
*/
|
|
8
33
|
fd: number;
|
|
34
|
+
/**
|
|
35
|
+
* 檔案權限模式 / File permission mode
|
|
36
|
+
*/
|
|
9
37
|
protected mode: number;
|
|
38
|
+
/**
|
|
39
|
+
* 當前位置 / Current position
|
|
40
|
+
*/
|
|
10
41
|
protected pos: number;
|
|
42
|
+
/**
|
|
43
|
+
* 是否已關閉 / Whether closed
|
|
44
|
+
*/
|
|
11
45
|
protected closed: boolean;
|
|
46
|
+
/**
|
|
47
|
+
* 是否已銷毀 / Whether destroyed
|
|
48
|
+
*/
|
|
12
49
|
protected destroyed: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* 可寫狀態 / Writable state
|
|
52
|
+
*/
|
|
13
53
|
protected _writableState: IFsStreamState;
|
|
54
|
+
/**
|
|
55
|
+
* 可讀狀態 / Readable state
|
|
56
|
+
*/
|
|
14
57
|
protected _readableState: IFsStreamState;
|
|
58
|
+
/**
|
|
59
|
+
* 流資料符號屬性 / Stream data symbol property
|
|
60
|
+
*/
|
|
15
61
|
protected [SYM_FS_STREAM_DATA]: IFsStreamData;
|
|
16
62
|
/**
|
|
17
|
-
*
|
|
63
|
+
* 已寫入的位元組數(不包含佇列中的資料)
|
|
64
|
+
* Number of bytes written so far (excludes queued data)
|
|
18
65
|
*/
|
|
19
66
|
bytesWritten: number;
|
|
20
67
|
/**
|
|
21
|
-
*
|
|
68
|
+
* 檔案路徑(字串或 Buffer)
|
|
69
|
+
* File path (string or Buffer)
|
|
22
70
|
*/
|
|
23
71
|
readonly path: IFsPath;
|
|
72
|
+
/**
|
|
73
|
+
* 構造函數
|
|
74
|
+
* Constructor
|
|
75
|
+
*
|
|
76
|
+
* @param {PathLike} path - 檔案路徑 / File path
|
|
77
|
+
* @param {string | IFsWriteStreamOptions} [options] - 選項 / Options
|
|
78
|
+
*/
|
|
24
79
|
constructor(path: PathLike, options?: string | IFsWriteStreamOptions);
|
|
80
|
+
/**
|
|
81
|
+
* 建立函數別名
|
|
82
|
+
* Factory method alias
|
|
83
|
+
*/
|
|
25
84
|
static get create(): typeof createWriteStream;
|
|
85
|
+
/**
|
|
86
|
+
* 開啟檔案流
|
|
87
|
+
* Open file stream
|
|
88
|
+
*
|
|
89
|
+
* 開啟檔案並發送 open 和 ready 事件
|
|
90
|
+
* Opens file and emits open and ready events
|
|
91
|
+
*/
|
|
26
92
|
open(): void;
|
|
27
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* 建立寫入流
|
|
96
|
+
* Create write stream
|
|
97
|
+
*
|
|
98
|
+
* 工廠函數,建立 WriteStream 實例
|
|
99
|
+
* Factory function that creates a WriteStream instance
|
|
100
|
+
*
|
|
101
|
+
* @param {PathLike} path - 檔案路徑 / File path
|
|
102
|
+
* @param {string | IFsWriteStreamOptions} [options] - 選項 / Options
|
|
103
|
+
* @returns {WriteStream} 寫入流實例 / Write stream instance
|
|
104
|
+
*/
|
|
28
105
|
export declare function createWriteStream(path: PathLike, options?: string | IFsWriteStreamOptions): WriteStream;
|
|
106
|
+
/**
|
|
107
|
+
* 預設導出 - WriteStream 類別
|
|
108
|
+
* Default export - WriteStream class
|
|
109
|
+
*/
|
|
29
110
|
export default WriteStream;
|
package/write.js
CHANGED
|
@@ -1,24 +1,61 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* 寫入流實作
|
|
4
|
+
* Write Stream Implementation
|
|
5
|
+
*
|
|
6
|
+
* 提供非同步檔案寫入流功能,擴展 Node.js fs.WriteStream
|
|
7
|
+
* Provides asynchronous file writing stream functionality, extends Node.js fs.WriteStream
|
|
8
|
+
*
|
|
9
|
+
* @module fs-stream-sync/write
|
|
10
|
+
*/
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
exports.WriteStream = void 0;
|
|
4
13
|
exports.createWriteStream = createWriteStream;
|
|
5
14
|
const tslib_1 = require("tslib");
|
|
6
15
|
const fs_1 = tslib_1.__importDefault(require("fs"));
|
|
7
16
|
const internal_1 = require("./lib/internal");
|
|
17
|
+
/**
|
|
18
|
+
* 寫入流類別
|
|
19
|
+
* Write Stream Class
|
|
20
|
+
*
|
|
21
|
+
* 擴展 Node.js fs.WriteStream,提供額外功能
|
|
22
|
+
* Extends Node.js fs.WriteStream with additional functionality
|
|
23
|
+
*/
|
|
8
24
|
// @ts-ignore
|
|
9
25
|
class WriteStream extends fs_1.default.WriteStream {
|
|
26
|
+
/**
|
|
27
|
+
* 構造函數
|
|
28
|
+
* Constructor
|
|
29
|
+
*
|
|
30
|
+
* @param {PathLike} path - 檔案路徑 / File path
|
|
31
|
+
* @param {string | IFsWriteStreamOptions} [options] - 選項 / Options
|
|
32
|
+
*/
|
|
10
33
|
constructor(path, options) {
|
|
11
34
|
// @ts-ignore
|
|
12
35
|
super(path, options);
|
|
36
|
+
// 初始化流資料 / Initialize stream data
|
|
13
37
|
(0, internal_1.getFsStreamData)(this);
|
|
14
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* 建立函數別名
|
|
41
|
+
* Factory method alias
|
|
42
|
+
*/
|
|
15
43
|
static get create() {
|
|
16
44
|
return createWriteStream;
|
|
17
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* 開啟檔案流
|
|
48
|
+
* Open file stream
|
|
49
|
+
*
|
|
50
|
+
* 開啟檔案並發送 open 和 ready 事件
|
|
51
|
+
* Opens file and emits open and ready events
|
|
52
|
+
*/
|
|
18
53
|
open() {
|
|
19
54
|
if (!(0, internal_1.getFsStreamData)(this).opened) {
|
|
20
55
|
const self = this;
|
|
21
56
|
this[internal_1.SYM_FS_STREAM_DATA].opened = true;
|
|
57
|
+
// 監聽 open 事件並發送 ready 事件
|
|
58
|
+
// Listen for open event and emit ready event
|
|
22
59
|
this.once('open', function () {
|
|
23
60
|
process.nextTick(function () {
|
|
24
61
|
self.emit('ready');
|
|
@@ -30,8 +67,23 @@ class WriteStream extends fs_1.default.WriteStream {
|
|
|
30
67
|
}
|
|
31
68
|
}
|
|
32
69
|
exports.WriteStream = WriteStream;
|
|
70
|
+
/**
|
|
71
|
+
* 建立寫入流
|
|
72
|
+
* Create write stream
|
|
73
|
+
*
|
|
74
|
+
* 工廠函數,建立 WriteStream 實例
|
|
75
|
+
* Factory function that creates a WriteStream instance
|
|
76
|
+
*
|
|
77
|
+
* @param {PathLike} path - 檔案路徑 / File path
|
|
78
|
+
* @param {string | IFsWriteStreamOptions} [options] - 選項 / Options
|
|
79
|
+
* @returns {WriteStream} 寫入流實例 / Write stream instance
|
|
80
|
+
*/
|
|
33
81
|
function createWriteStream(path, options) {
|
|
34
82
|
return new WriteStream(path, options);
|
|
35
83
|
}
|
|
84
|
+
/**
|
|
85
|
+
* 預設導出 - WriteStream 類別
|
|
86
|
+
* Default export - WriteStream class
|
|
87
|
+
*/
|
|
36
88
|
exports.default = WriteStream;
|
|
37
89
|
//# sourceMappingURL=write.js.map
|
package/write.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"write.js","sourceRoot":"","sources":["write.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;;;AA2IH,8CAGC;;AA5ID,oDAAoB;AAGpB,6CAAqE;AAErE;;;;;;GAMG;AACH,aAAa;AACb,MAAa,WAAY,SAAQ,YAAE,CAAC,WAAW;IAyD9C;;;;;;OAMG;IACH,YAAY,IAAc,EAAE,OAAwC;QAEnE,aAAa;QACb,KAAK,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;QAEpB,kCAAkC;QAClC,IAAA,0BAAe,EAAC,IAAI,CAAC,CAAA;IACtB,CAAC;IAED;;;OAGG;IACH,MAAM,KAAK,MAAM;QAEhB,OAAO,iBAAiB,CAAA;IACzB,CAAC;IAED;;;;;;OAMG;IACH,IAAI;QAEH,IAAI,CAAC,IAAA,0BAAe,EAAC,IAAI,CAAC,CAAC,MAAM,EACjC,CAAC;YACA,MAAM,IAAI,GAAG,IAAI,CAAA;YAEjB,IAAI,CAAC,6BAAkB,CAAC,CAAC,MAAM,GAAG,IAAI,CAAA;YAEtC,yBAAyB;YACzB,6CAA6C;YAC7C,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE;gBAEjB,OAAO,CAAC,QAAQ,CAAC;oBAEhB,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;gBACnB,CAAC,CAAC,CAAA;YACH,CAAC,CAAC,CAAA;YACF,aAAa;YACb,YAAE,CAAC,WAAW,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QACzC,CAAC;IACF,CAAC;CAED;AA/GD,kCA+GC;AAED;;;;;;;;;;GAUG;AACH,SAAgB,iBAAiB,CAAC,IAAc,EAAE,OAAwC;IAEzF,OAAO,IAAI,WAAW,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;AACtC,CAAC;AAED;;;GAGG;AACH,kBAAe,WAAW,CAAA","sourcesContent":["/**\n * 寫入流實作\n * Write Stream Implementation\n *\n * 提供非同步檔案寫入流功能,擴展 Node.js fs.WriteStream\n * Provides asynchronous file writing stream functionality, extends Node.js fs.WriteStream\n *\n * @module fs-stream-sync/write\n */\n\nimport fs from \"fs\";\nimport { PathLike } from 'fs';\nimport { IFsPath, IFsStreamData, IFsStreamState, IFsWriteStreamOptions } from './lib/interface';\nimport { getFsStreamData, SYM_FS_STREAM_DATA } from './lib/internal';\n\n/**\n * 寫入流類別\n * Write Stream Class\n *\n * 擴展 Node.js fs.WriteStream,提供額外功能\n * Extends Node.js fs.WriteStream with additional functionality\n */\n// @ts-ignore\nexport class WriteStream extends fs.WriteStream\n{\n\t/**\n\t * 自動關閉標誌 / Auto close flag\n\t */\n\tprotected autoClose: boolean\n\t/**\n\t * 檔案開啟標誌 / File open flags\n\t */\n\tprotected flags: string\n\t/**\n\t * 檔案描述符 / File descriptor\n\t */\n\tfd: number\n\t/**\n\t * 檔案權限模式 / File permission mode\n\t */\n\tprotected mode: number\n\t/**\n\t * 當前位置 / Current position\n\t */\n\tprotected pos: number\n\t/**\n\t * 是否已關閉 / Whether closed\n\t */\n\tprotected override closed: boolean\n\t/**\n\t * 是否已銷毀 / Whether destroyed\n\t */\n\tprotected override destroyed: boolean\n\n\t/**\n\t * 可寫狀態 / Writable state\n\t */\n\tprotected _writableState: IFsStreamState\n\t/**\n\t * 可讀狀態 / Readable state\n\t */\n\tprotected _readableState: IFsStreamState\n\n\t/**\n\t * 流資料符號屬性 / Stream data symbol property\n\t */\n\tprotected [SYM_FS_STREAM_DATA]: IFsStreamData\n\n\t/**\n\t * 已寫入的位元組數(不包含佇列中的資料)\n\t * Number of bytes written so far (excludes queued data)\n\t */\n\tpublic override bytesWritten: number\n\n\t/**\n\t * 檔案路徑(字串或 Buffer)\n\t * File path (string or Buffer)\n\t */\n\tpublic override readonly path: IFsPath\n\n\t/**\n\t * 構造函數\n\t * Constructor\n\t *\n\t * @param {PathLike} path - 檔案路徑 / File path\n\t * @param {string | IFsWriteStreamOptions} [options] - 選項 / Options\n\t */\n\tconstructor(path: PathLike, options?: string | IFsWriteStreamOptions)\n\t{\n\t\t// @ts-ignore\n\t\tsuper(path, options)\n\n\t\t// 初始化流資料 / Initialize stream data\n\t\tgetFsStreamData(this)\n\t}\n\n\t/**\n\t * 建立函數別名\n\t * Factory method alias\n\t */\n\tstatic get create()\n\t{\n\t\treturn createWriteStream\n\t}\n\n\t/**\n\t * 開啟檔案流\n\t * Open file stream\n\t *\n\t * 開啟檔案並發送 open 和 ready 事件\n\t * Opens file and emits open and ready events\n\t */\n\topen()\n\t{\n\t\tif (!getFsStreamData(this).opened)\n\t\t{\n\t\t\tconst self = this\n\n\t\t\tthis[SYM_FS_STREAM_DATA].opened = true\n\n\t\t\t// 監聽 open 事件並發送 ready 事件\n\t\t\t// Listen for open event and emit ready event\n\t\t\tthis.once('open', function ()\n\t\t\t{\n\t\t\t\tprocess.nextTick(function ()\n\t\t\t\t{\n\t\t\t\t\tself.emit('ready')\n\t\t\t\t})\n\t\t\t})\n\t\t\t// @ts-ignore\n\t\t\tfs.WriteStream.prototype.open.call(this)\n\t\t}\n\t}\n\n}\n\n/**\n * 建立寫入流\n * Create write stream\n *\n * 工廠函數,建立 WriteStream 實例\n * Factory function that creates a WriteStream instance\n *\n * @param {PathLike} path - 檔案路徑 / File path\n * @param {string | IFsWriteStreamOptions} [options] - 選項 / Options\n * @returns {WriteStream} 寫入流實例 / Write stream instance\n */\nexport function createWriteStream(path: PathLike, options?: string | IFsWriteStreamOptions)\n{\n\treturn new WriteStream(path, options)\n}\n\n/**\n * 預設導出 - WriteStream 類別\n * Default export - WriteStream class\n */\nexport default WriteStream\n"]}
|