@rg-dev/stdlib
Advanced tools
+41
-19
@@ -197,2 +197,3 @@ var __create = Object.create; | ||
| createTempDir: () => createTempDir, | ||
| createTempFilePath: () => createTempFilePath, | ||
| isWindows: () => isWindows, | ||
@@ -211,7 +212,7 @@ throwIfDirNotEmpty: () => throwIfDirNotEmpty | ||
| isOpen = true; | ||
| constructor(res) { | ||
| this.res = res; | ||
| res.setHeader("Content-Type", "text/event-stream"); | ||
| res.setHeader("Cache-Control", "no-cache"); | ||
| res.setHeader("Connection", "keep-alive"); | ||
| constructor(resObject) { | ||
| this.res = resObject; | ||
| resObject.setHeader("Content-Type", "text/event-stream"); | ||
| resObject.setHeader("Cache-Control", "no-cache"); | ||
| resObject.setHeader("Connection", "keep-alive"); | ||
| } | ||
@@ -224,3 +225,5 @@ flushHeaders() { | ||
| emit(data, event) { | ||
| if (!this.isOpen) return; | ||
| if (!this.isOpen) { | ||
| return; | ||
| } | ||
| try { | ||
@@ -293,7 +296,9 @@ let payload = ""; | ||
| buffer += decoder.decode(value, { stream: true }); | ||
| let lines = buffer.split("\n"); | ||
| const lines = buffer.split("\n"); | ||
| buffer = lines.pop(); | ||
| for (let line of lines) { | ||
| line = line.trim(); | ||
| if (!line) continue; | ||
| if (!line) { | ||
| continue; | ||
| } | ||
| if (line.startsWith("event:")) { | ||
@@ -345,4 +350,8 @@ eventName = line.slice(6).trim(); | ||
| path2 = removeQuotes(path2); | ||
| if (!await fs.pathExists(path2)) throw new Error(`path ${path2} not exists`); | ||
| if (!(await fs.stat(path2)).isDirectory()) throw new Error(`${path2} is a file, require dir`); | ||
| if (!await fs.pathExists(path2)) { | ||
| throw new Error(`path ${path2} not exists`); | ||
| } | ||
| if (!(await fs.stat(path2)).isDirectory()) { | ||
| throw new Error(`${path2} is a file, require dir`); | ||
| } | ||
| } | ||
@@ -358,7 +367,16 @@ function createTempDir() { | ||
| } | ||
| async function throwIfDirNotEmpty(dir_path) { | ||
| if (!await fs.pathExists(dir_path)) throw new Error("dir not exists"); | ||
| const isEmpty = await fs.readdir(dir_path).then((x) => x.length === 0); | ||
| if (!isEmpty) throw new Error(`dir ${dir_path} must be empty`); | ||
| function createTempFilePath() { | ||
| const tmpDir = import_os.default.tmpdir(); | ||
| const timestamp = Date.now(); | ||
| return import_path.default.join(tmpDir, `temp_file_${timestamp}`); | ||
| } | ||
| async function throwIfDirNotEmpty(dirPath) { | ||
| if (!await fs.pathExists(dirPath)) { | ||
| throw new Error("dir not exists"); | ||
| } | ||
| const isEmpty = await fs.readdir(dirPath).then((x) => x.length === 0); | ||
| if (!isEmpty) { | ||
| throw new Error(`dir ${dirPath} must be empty`); | ||
| } | ||
| } | ||
| async function checkCommandExistsOrThrow(cmd) { | ||
@@ -371,7 +389,11 @@ try { | ||
| } | ||
| async function checkIfFileExistsOrThrow(the_path) { | ||
| if (!the_path) throw "path is empty"; | ||
| the_path = removeQuotes(the_path); | ||
| if (!await fs.pathExists(the_path)) throw new Error(`path ${the_path} not exists`); | ||
| if (!(await fs.stat(the_path)).isFile()) throw new Error(`${the_path} is a dir, require file`); | ||
| async function checkIfFileExistsOrThrow(thePath) { | ||
| if (!thePath) throw "path is empty"; | ||
| thePath = removeQuotes(thePath); | ||
| if (!await fs.pathExists(thePath)) { | ||
| throw new Error(`path ${thePath} not exists`); | ||
| } | ||
| if (!(await fs.stat(thePath)).isFile()) { | ||
| throw new Error(`${thePath} is a dir, require file`); | ||
| } | ||
| } | ||
@@ -378,0 +400,0 @@ function removeQuotes(str) { |
@@ -7,3 +7,3 @@ import { Response } from 'express'; | ||
| private isOpen; | ||
| constructor(res: Response); | ||
| constructor(resObject: Response); | ||
| flushHeaders(): void; | ||
@@ -35,6 +35,7 @@ /** Send JSON to client, optionally with a custom event type */ | ||
| declare function createTempDir(): string; | ||
| declare function throwIfDirNotEmpty(dir_path: string): Promise<void>; | ||
| declare function createTempFilePath(): string; | ||
| declare function throwIfDirNotEmpty(dirPath: string): Promise<void>; | ||
| declare function checkCommandExistsOrThrow(cmd: string): Promise<void>; | ||
| declare function checkIfFileExistsOrThrow(the_path: string): Promise<void>; | ||
| declare function checkIfFileExistsOrThrow(thePath: string): Promise<void>; | ||
| export { SSEClient, SSEResponse, checkCommandExistsOrThrow, checkIfDirExistsOrThrow, checkIfFileExistsOrThrow, chmodPlusX, createTempDir, isWindows, throwIfDirNotEmpty }; | ||
| export { SSEClient, SSEResponse, checkCommandExistsOrThrow, checkIfDirExistsOrThrow, checkIfFileExistsOrThrow, chmodPlusX, createTempDir, createTempFilePath, isWindows, throwIfDirNotEmpty }; |
@@ -7,3 +7,3 @@ import { Response } from 'express'; | ||
| private isOpen; | ||
| constructor(res: Response); | ||
| constructor(resObject: Response); | ||
| flushHeaders(): void; | ||
@@ -35,6 +35,7 @@ /** Send JSON to client, optionally with a custom event type */ | ||
| declare function createTempDir(): string; | ||
| declare function throwIfDirNotEmpty(dir_path: string): Promise<void>; | ||
| declare function createTempFilePath(): string; | ||
| declare function throwIfDirNotEmpty(dirPath: string): Promise<void>; | ||
| declare function checkCommandExistsOrThrow(cmd: string): Promise<void>; | ||
| declare function checkIfFileExistsOrThrow(the_path: string): Promise<void>; | ||
| declare function checkIfFileExistsOrThrow(thePath: string): Promise<void>; | ||
| export { SSEClient, SSEResponse, checkCommandExistsOrThrow, checkIfDirExistsOrThrow, checkIfFileExistsOrThrow, chmodPlusX, createTempDir, isWindows, throwIfDirNotEmpty }; | ||
| export { SSEClient, SSEResponse, checkCommandExistsOrThrow, checkIfDirExistsOrThrow, checkIfFileExistsOrThrow, chmodPlusX, createTempDir, createTempFilePath, isWindows, throwIfDirNotEmpty }; |
+41
-19
@@ -198,7 +198,7 @@ var __create = Object.create; | ||
| isOpen = true; | ||
| constructor(res) { | ||
| this.res = res; | ||
| res.setHeader("Content-Type", "text/event-stream"); | ||
| res.setHeader("Cache-Control", "no-cache"); | ||
| res.setHeader("Connection", "keep-alive"); | ||
| constructor(resObject) { | ||
| this.res = resObject; | ||
| resObject.setHeader("Content-Type", "text/event-stream"); | ||
| resObject.setHeader("Cache-Control", "no-cache"); | ||
| resObject.setHeader("Connection", "keep-alive"); | ||
| } | ||
@@ -211,3 +211,5 @@ flushHeaders() { | ||
| emit(data, event) { | ||
| if (!this.isOpen) return; | ||
| if (!this.isOpen) { | ||
| return; | ||
| } | ||
| try { | ||
@@ -280,7 +282,9 @@ let payload = ""; | ||
| buffer += decoder.decode(value, { stream: true }); | ||
| let lines = buffer.split("\n"); | ||
| const lines = buffer.split("\n"); | ||
| buffer = lines.pop(); | ||
| for (let line of lines) { | ||
| line = line.trim(); | ||
| if (!line) continue; | ||
| if (!line) { | ||
| continue; | ||
| } | ||
| if (line.startsWith("event:")) { | ||
@@ -332,4 +336,8 @@ eventName = line.slice(6).trim(); | ||
| path2 = removeQuotes(path2); | ||
| if (!await fs.pathExists(path2)) throw new Error(`path ${path2} not exists`); | ||
| if (!(await fs.stat(path2)).isDirectory()) throw new Error(`${path2} is a file, require dir`); | ||
| if (!await fs.pathExists(path2)) { | ||
| throw new Error(`path ${path2} not exists`); | ||
| } | ||
| if (!(await fs.stat(path2)).isDirectory()) { | ||
| throw new Error(`${path2} is a file, require dir`); | ||
| } | ||
| } | ||
@@ -345,7 +353,16 @@ function createTempDir() { | ||
| } | ||
| async function throwIfDirNotEmpty(dir_path) { | ||
| if (!await fs.pathExists(dir_path)) throw new Error("dir not exists"); | ||
| const isEmpty = await fs.readdir(dir_path).then((x) => x.length === 0); | ||
| if (!isEmpty) throw new Error(`dir ${dir_path} must be empty`); | ||
| function createTempFilePath() { | ||
| const tmpDir = os.tmpdir(); | ||
| const timestamp = Date.now(); | ||
| return path.join(tmpDir, `temp_file_${timestamp}`); | ||
| } | ||
| async function throwIfDirNotEmpty(dirPath) { | ||
| if (!await fs.pathExists(dirPath)) { | ||
| throw new Error("dir not exists"); | ||
| } | ||
| const isEmpty = await fs.readdir(dirPath).then((x) => x.length === 0); | ||
| if (!isEmpty) { | ||
| throw new Error(`dir ${dirPath} must be empty`); | ||
| } | ||
| } | ||
| async function checkCommandExistsOrThrow(cmd) { | ||
@@ -358,7 +375,11 @@ try { | ||
| } | ||
| async function checkIfFileExistsOrThrow(the_path) { | ||
| if (!the_path) throw "path is empty"; | ||
| the_path = removeQuotes(the_path); | ||
| if (!await fs.pathExists(the_path)) throw new Error(`path ${the_path} not exists`); | ||
| if (!(await fs.stat(the_path)).isFile()) throw new Error(`${the_path} is a dir, require file`); | ||
| async function checkIfFileExistsOrThrow(thePath) { | ||
| if (!thePath) throw "path is empty"; | ||
| thePath = removeQuotes(thePath); | ||
| if (!await fs.pathExists(thePath)) { | ||
| throw new Error(`path ${thePath} not exists`); | ||
| } | ||
| if (!(await fs.stat(thePath)).isFile()) { | ||
| throw new Error(`${thePath} is a dir, require file`); | ||
| } | ||
| } | ||
@@ -380,4 +401,5 @@ function removeQuotes(str) { | ||
| createTempDir, | ||
| createTempFilePath, | ||
| isWindows, | ||
| throwIfDirNotEmpty | ||
| }; |
+1
-1
| { | ||
| "name": "@rg-dev/stdlib", | ||
| "version": "1.0.34", | ||
| "version": "1.0.35", | ||
| "description": "", | ||
@@ -5,0 +5,0 @@ "scripts": { |
540844
0.14%13533
0.33%