@rg-dev/stdlib 1.0.34 → 1.0.35
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/lib/node-env.cjs +41 -19
- package/lib/node-env.d.cts +5 -4
- package/lib/node-env.d.ts +5 -4
- package/lib/node-env.js +41 -19
- package/package.json +1 -1
package/lib/node-env.cjs
CHANGED
|
@@ -194,6 +194,7 @@ __export(node_env_exports, {
|
|
|
194
194
|
checkIfFileExistsOrThrow: () => checkIfFileExistsOrThrow,
|
|
195
195
|
chmodPlusX: () => chmodPlusX,
|
|
196
196
|
createTempDir: () => createTempDir,
|
|
197
|
+
createTempFilePath: () => createTempFilePath,
|
|
197
198
|
isWindows: () => isWindows,
|
|
198
199
|
throwIfDirNotEmpty: () => throwIfDirNotEmpty
|
|
199
200
|
});
|
|
@@ -207,11 +208,11 @@ var import_command_exists = __toESM(require_command_exists2(), 1);
|
|
|
207
208
|
var SSEResponse = class {
|
|
208
209
|
res;
|
|
209
210
|
isOpen = true;
|
|
210
|
-
constructor(
|
|
211
|
-
this.res =
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
211
|
+
constructor(resObject) {
|
|
212
|
+
this.res = resObject;
|
|
213
|
+
resObject.setHeader("Content-Type", "text/event-stream");
|
|
214
|
+
resObject.setHeader("Cache-Control", "no-cache");
|
|
215
|
+
resObject.setHeader("Connection", "keep-alive");
|
|
215
216
|
}
|
|
216
217
|
flushHeaders() {
|
|
217
218
|
var _a;
|
|
@@ -219,7 +220,9 @@ var SSEResponse = class {
|
|
|
219
220
|
}
|
|
220
221
|
/** Send JSON to client, optionally with a custom event type */
|
|
221
222
|
emit(data, event) {
|
|
222
|
-
if (!this.isOpen)
|
|
223
|
+
if (!this.isOpen) {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
223
226
|
try {
|
|
224
227
|
let payload = "";
|
|
225
228
|
if (event) {
|
|
@@ -287,11 +290,13 @@ var SSEClient = class extends import_events.EventEmitter {
|
|
|
287
290
|
const { value, done } = await reader.read();
|
|
288
291
|
if (done) break;
|
|
289
292
|
buffer += decoder.decode(value, { stream: true });
|
|
290
|
-
|
|
293
|
+
const lines = buffer.split("\n");
|
|
291
294
|
buffer = lines.pop();
|
|
292
295
|
for (let line of lines) {
|
|
293
296
|
line = line.trim();
|
|
294
|
-
if (!line)
|
|
297
|
+
if (!line) {
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
295
300
|
if (line.startsWith("event:")) {
|
|
296
301
|
eventName = line.slice(6).trim();
|
|
297
302
|
} else if (line.startsWith("data:")) {
|
|
@@ -338,8 +343,12 @@ function chmodPlusX(filePath) {
|
|
|
338
343
|
async function checkIfDirExistsOrThrow(path2) {
|
|
339
344
|
if (!path2) throw "path is empty";
|
|
340
345
|
path2 = removeQuotes(path2);
|
|
341
|
-
if (!await fs.pathExists(path2))
|
|
342
|
-
|
|
346
|
+
if (!await fs.pathExists(path2)) {
|
|
347
|
+
throw new Error(`path ${path2} not exists`);
|
|
348
|
+
}
|
|
349
|
+
if (!(await fs.stat(path2)).isDirectory()) {
|
|
350
|
+
throw new Error(`${path2} is a file, require dir`);
|
|
351
|
+
}
|
|
343
352
|
}
|
|
344
353
|
function createTempDir() {
|
|
345
354
|
const tmpDir = import_os.default.tmpdir();
|
|
@@ -350,10 +359,19 @@ function createTempDir() {
|
|
|
350
359
|
fs.mkdirSync(tempDirPath);
|
|
351
360
|
return tempDirPath;
|
|
352
361
|
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
const
|
|
356
|
-
|
|
362
|
+
function createTempFilePath() {
|
|
363
|
+
const tmpDir = import_os.default.tmpdir();
|
|
364
|
+
const timestamp = Date.now();
|
|
365
|
+
return import_path.default.join(tmpDir, `temp_file_${timestamp}`);
|
|
366
|
+
}
|
|
367
|
+
async function throwIfDirNotEmpty(dirPath) {
|
|
368
|
+
if (!await fs.pathExists(dirPath)) {
|
|
369
|
+
throw new Error("dir not exists");
|
|
370
|
+
}
|
|
371
|
+
const isEmpty = await fs.readdir(dirPath).then((x) => x.length === 0);
|
|
372
|
+
if (!isEmpty) {
|
|
373
|
+
throw new Error(`dir ${dirPath} must be empty`);
|
|
374
|
+
}
|
|
357
375
|
}
|
|
358
376
|
async function checkCommandExistsOrThrow(cmd) {
|
|
359
377
|
try {
|
|
@@ -362,11 +380,15 @@ async function checkCommandExistsOrThrow(cmd) {
|
|
|
362
380
|
throw new Error(`cmd ${cmd} not exists`);
|
|
363
381
|
}
|
|
364
382
|
}
|
|
365
|
-
async function checkIfFileExistsOrThrow(
|
|
366
|
-
if (!
|
|
367
|
-
|
|
368
|
-
if (!await fs.pathExists(
|
|
369
|
-
|
|
383
|
+
async function checkIfFileExistsOrThrow(thePath) {
|
|
384
|
+
if (!thePath) throw "path is empty";
|
|
385
|
+
thePath = removeQuotes(thePath);
|
|
386
|
+
if (!await fs.pathExists(thePath)) {
|
|
387
|
+
throw new Error(`path ${thePath} not exists`);
|
|
388
|
+
}
|
|
389
|
+
if (!(await fs.stat(thePath)).isFile()) {
|
|
390
|
+
throw new Error(`${thePath} is a dir, require file`);
|
|
391
|
+
}
|
|
370
392
|
}
|
|
371
393
|
function removeQuotes(str) {
|
|
372
394
|
if (str.startsWith('"') && str.endsWith('"')) {
|
package/lib/node-env.d.cts
CHANGED
|
@@ -4,7 +4,7 @@ import { EventEmitter } from 'events';
|
|
|
4
4
|
declare class SSEResponse<Events extends string = 'message'> {
|
|
5
5
|
private res;
|
|
6
6
|
private isOpen;
|
|
7
|
-
constructor(
|
|
7
|
+
constructor(resObject: Response);
|
|
8
8
|
flushHeaders(): void;
|
|
9
9
|
/** Send JSON to client, optionally with a custom event type */
|
|
10
10
|
emit(data: unknown, event?: Events): void;
|
|
@@ -31,8 +31,9 @@ declare function isWindows(): boolean;
|
|
|
31
31
|
declare function chmodPlusX(filePath: string): void;
|
|
32
32
|
declare function checkIfDirExistsOrThrow(path: string): Promise<void>;
|
|
33
33
|
declare function createTempDir(): string;
|
|
34
|
-
declare function
|
|
34
|
+
declare function createTempFilePath(): string;
|
|
35
|
+
declare function throwIfDirNotEmpty(dirPath: string): Promise<void>;
|
|
35
36
|
declare function checkCommandExistsOrThrow(cmd: string): Promise<void>;
|
|
36
|
-
declare function checkIfFileExistsOrThrow(
|
|
37
|
+
declare function checkIfFileExistsOrThrow(thePath: string): Promise<void>;
|
|
37
38
|
|
|
38
|
-
export { SSEClient, SSEResponse, checkCommandExistsOrThrow, checkIfDirExistsOrThrow, checkIfFileExistsOrThrow, chmodPlusX, createTempDir, isWindows, throwIfDirNotEmpty };
|
|
39
|
+
export { SSEClient, SSEResponse, checkCommandExistsOrThrow, checkIfDirExistsOrThrow, checkIfFileExistsOrThrow, chmodPlusX, createTempDir, createTempFilePath, isWindows, throwIfDirNotEmpty };
|
package/lib/node-env.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { EventEmitter } from 'events';
|
|
|
4
4
|
declare class SSEResponse<Events extends string = 'message'> {
|
|
5
5
|
private res;
|
|
6
6
|
private isOpen;
|
|
7
|
-
constructor(
|
|
7
|
+
constructor(resObject: Response);
|
|
8
8
|
flushHeaders(): void;
|
|
9
9
|
/** Send JSON to client, optionally with a custom event type */
|
|
10
10
|
emit(data: unknown, event?: Events): void;
|
|
@@ -31,8 +31,9 @@ declare function isWindows(): boolean;
|
|
|
31
31
|
declare function chmodPlusX(filePath: string): void;
|
|
32
32
|
declare function checkIfDirExistsOrThrow(path: string): Promise<void>;
|
|
33
33
|
declare function createTempDir(): string;
|
|
34
|
-
declare function
|
|
34
|
+
declare function createTempFilePath(): string;
|
|
35
|
+
declare function throwIfDirNotEmpty(dirPath: string): Promise<void>;
|
|
35
36
|
declare function checkCommandExistsOrThrow(cmd: string): Promise<void>;
|
|
36
|
-
declare function checkIfFileExistsOrThrow(
|
|
37
|
+
declare function checkIfFileExistsOrThrow(thePath: string): Promise<void>;
|
|
37
38
|
|
|
38
|
-
export { SSEClient, SSEResponse, checkCommandExistsOrThrow, checkIfDirExistsOrThrow, checkIfFileExistsOrThrow, chmodPlusX, createTempDir, isWindows, throwIfDirNotEmpty };
|
|
39
|
+
export { SSEClient, SSEResponse, checkCommandExistsOrThrow, checkIfDirExistsOrThrow, checkIfFileExistsOrThrow, chmodPlusX, createTempDir, createTempFilePath, isWindows, throwIfDirNotEmpty };
|
package/lib/node-env.js
CHANGED
|
@@ -195,11 +195,11 @@ import path from "path";
|
|
|
195
195
|
var SSEResponse = class {
|
|
196
196
|
res;
|
|
197
197
|
isOpen = true;
|
|
198
|
-
constructor(
|
|
199
|
-
this.res =
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
198
|
+
constructor(resObject) {
|
|
199
|
+
this.res = resObject;
|
|
200
|
+
resObject.setHeader("Content-Type", "text/event-stream");
|
|
201
|
+
resObject.setHeader("Cache-Control", "no-cache");
|
|
202
|
+
resObject.setHeader("Connection", "keep-alive");
|
|
203
203
|
}
|
|
204
204
|
flushHeaders() {
|
|
205
205
|
var _a;
|
|
@@ -207,7 +207,9 @@ var SSEResponse = class {
|
|
|
207
207
|
}
|
|
208
208
|
/** Send JSON to client, optionally with a custom event type */
|
|
209
209
|
emit(data, event) {
|
|
210
|
-
if (!this.isOpen)
|
|
210
|
+
if (!this.isOpen) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
211
213
|
try {
|
|
212
214
|
let payload = "";
|
|
213
215
|
if (event) {
|
|
@@ -275,11 +277,13 @@ var SSEClient = class extends EventEmitter {
|
|
|
275
277
|
const { value, done } = await reader.read();
|
|
276
278
|
if (done) break;
|
|
277
279
|
buffer += decoder.decode(value, { stream: true });
|
|
278
|
-
|
|
280
|
+
const lines = buffer.split("\n");
|
|
279
281
|
buffer = lines.pop();
|
|
280
282
|
for (let line of lines) {
|
|
281
283
|
line = line.trim();
|
|
282
|
-
if (!line)
|
|
284
|
+
if (!line) {
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
283
287
|
if (line.startsWith("event:")) {
|
|
284
288
|
eventName = line.slice(6).trim();
|
|
285
289
|
} else if (line.startsWith("data:")) {
|
|
@@ -326,8 +330,12 @@ function chmodPlusX(filePath) {
|
|
|
326
330
|
async function checkIfDirExistsOrThrow(path2) {
|
|
327
331
|
if (!path2) throw "path is empty";
|
|
328
332
|
path2 = removeQuotes(path2);
|
|
329
|
-
if (!await fs.pathExists(path2))
|
|
330
|
-
|
|
333
|
+
if (!await fs.pathExists(path2)) {
|
|
334
|
+
throw new Error(`path ${path2} not exists`);
|
|
335
|
+
}
|
|
336
|
+
if (!(await fs.stat(path2)).isDirectory()) {
|
|
337
|
+
throw new Error(`${path2} is a file, require dir`);
|
|
338
|
+
}
|
|
331
339
|
}
|
|
332
340
|
function createTempDir() {
|
|
333
341
|
const tmpDir = os.tmpdir();
|
|
@@ -338,10 +346,19 @@ function createTempDir() {
|
|
|
338
346
|
fs.mkdirSync(tempDirPath);
|
|
339
347
|
return tempDirPath;
|
|
340
348
|
}
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
const
|
|
344
|
-
|
|
349
|
+
function createTempFilePath() {
|
|
350
|
+
const tmpDir = os.tmpdir();
|
|
351
|
+
const timestamp = Date.now();
|
|
352
|
+
return path.join(tmpDir, `temp_file_${timestamp}`);
|
|
353
|
+
}
|
|
354
|
+
async function throwIfDirNotEmpty(dirPath) {
|
|
355
|
+
if (!await fs.pathExists(dirPath)) {
|
|
356
|
+
throw new Error("dir not exists");
|
|
357
|
+
}
|
|
358
|
+
const isEmpty = await fs.readdir(dirPath).then((x) => x.length === 0);
|
|
359
|
+
if (!isEmpty) {
|
|
360
|
+
throw new Error(`dir ${dirPath} must be empty`);
|
|
361
|
+
}
|
|
345
362
|
}
|
|
346
363
|
async function checkCommandExistsOrThrow(cmd) {
|
|
347
364
|
try {
|
|
@@ -350,11 +367,15 @@ async function checkCommandExistsOrThrow(cmd) {
|
|
|
350
367
|
throw new Error(`cmd ${cmd} not exists`);
|
|
351
368
|
}
|
|
352
369
|
}
|
|
353
|
-
async function checkIfFileExistsOrThrow(
|
|
354
|
-
if (!
|
|
355
|
-
|
|
356
|
-
if (!await fs.pathExists(
|
|
357
|
-
|
|
370
|
+
async function checkIfFileExistsOrThrow(thePath) {
|
|
371
|
+
if (!thePath) throw "path is empty";
|
|
372
|
+
thePath = removeQuotes(thePath);
|
|
373
|
+
if (!await fs.pathExists(thePath)) {
|
|
374
|
+
throw new Error(`path ${thePath} not exists`);
|
|
375
|
+
}
|
|
376
|
+
if (!(await fs.stat(thePath)).isFile()) {
|
|
377
|
+
throw new Error(`${thePath} is a dir, require file`);
|
|
378
|
+
}
|
|
358
379
|
}
|
|
359
380
|
function removeQuotes(str) {
|
|
360
381
|
if (str.startsWith('"') && str.endsWith('"')) {
|
|
@@ -371,6 +392,7 @@ export {
|
|
|
371
392
|
checkIfFileExistsOrThrow,
|
|
372
393
|
chmodPlusX,
|
|
373
394
|
createTempDir,
|
|
395
|
+
createTempFilePath,
|
|
374
396
|
isWindows,
|
|
375
397
|
throwIfDirNotEmpty
|
|
376
398
|
};
|