@rg-dev/stdlib 1.0.34 → 1.0.36

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 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(res) {
211
- this.res = res;
212
- res.setHeader("Content-Type", "text/event-stream");
213
- res.setHeader("Cache-Control", "no-cache");
214
- res.setHeader("Connection", "keep-alive");
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) return;
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
- let lines = buffer.split("\n");
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) continue;
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)) throw new Error(`path ${path2} not exists`);
342
- if (!(await fs.stat(path2)).isDirectory()) throw new Error(`${path2} is a file, require dir`);
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,28 @@ function createTempDir() {
350
359
  fs.mkdirSync(tempDirPath);
351
360
  return tempDirPath;
352
361
  }
353
- async function throwIfDirNotEmpty(dir_path) {
354
- if (!await fs.pathExists(dir_path)) throw new Error("dir not exists");
355
- const isEmpty = await fs.readdir(dir_path).then((x) => x.length === 0);
356
- if (!isEmpty) throw new Error(`dir ${dir_path} must be empty`);
362
+ function createTempFilePath() {
363
+ const tmpDir = import_os.default.tmpdir();
364
+ const timestamp = Date.now();
365
+ const p = import_path.default.join(tmpDir, `temp_file_${timestamp}`);
366
+ return {
367
+ getName: () => p,
368
+ cleanUp: () => {
369
+ try {
370
+ fs.unlinkSync(p);
371
+ } catch (e) {
372
+ }
373
+ }
374
+ };
375
+ }
376
+ async function throwIfDirNotEmpty(dirPath) {
377
+ if (!await fs.pathExists(dirPath)) {
378
+ throw new Error("dir not exists");
379
+ }
380
+ const isEmpty = await fs.readdir(dirPath).then((x) => x.length === 0);
381
+ if (!isEmpty) {
382
+ throw new Error(`dir ${dirPath} must be empty`);
383
+ }
357
384
  }
358
385
  async function checkCommandExistsOrThrow(cmd) {
359
386
  try {
@@ -362,11 +389,15 @@ async function checkCommandExistsOrThrow(cmd) {
362
389
  throw new Error(`cmd ${cmd} not exists`);
363
390
  }
364
391
  }
365
- async function checkIfFileExistsOrThrow(the_path) {
366
- if (!the_path) throw "path is empty";
367
- the_path = removeQuotes(the_path);
368
- if (!await fs.pathExists(the_path)) throw new Error(`path ${the_path} not exists`);
369
- if (!(await fs.stat(the_path)).isFile()) throw new Error(`${the_path} is a dir, require file`);
392
+ async function checkIfFileExistsOrThrow(thePath) {
393
+ if (!thePath) throw "path is empty";
394
+ thePath = removeQuotes(thePath);
395
+ if (!await fs.pathExists(thePath)) {
396
+ throw new Error(`path ${thePath} not exists`);
397
+ }
398
+ if (!(await fs.stat(thePath)).isFile()) {
399
+ throw new Error(`${thePath} is a dir, require file`);
400
+ }
370
401
  }
371
402
  function removeQuotes(str) {
372
403
  if (str.startsWith('"') && str.endsWith('"')) {
@@ -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(res: Response);
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,12 @@ 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 throwIfDirNotEmpty(dir_path: string): Promise<void>;
34
+ declare function createTempFilePath(): {
35
+ getName: () => string;
36
+ cleanUp: () => void;
37
+ };
38
+ declare function throwIfDirNotEmpty(dirPath: string): Promise<void>;
35
39
  declare function checkCommandExistsOrThrow(cmd: string): Promise<void>;
36
- declare function checkIfFileExistsOrThrow(the_path: string): Promise<void>;
40
+ declare function checkIfFileExistsOrThrow(thePath: string): Promise<void>;
37
41
 
38
- export { SSEClient, SSEResponse, checkCommandExistsOrThrow, checkIfDirExistsOrThrow, checkIfFileExistsOrThrow, chmodPlusX, createTempDir, isWindows, throwIfDirNotEmpty };
42
+ 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(res: Response);
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,12 @@ 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 throwIfDirNotEmpty(dir_path: string): Promise<void>;
34
+ declare function createTempFilePath(): {
35
+ getName: () => string;
36
+ cleanUp: () => void;
37
+ };
38
+ declare function throwIfDirNotEmpty(dirPath: string): Promise<void>;
35
39
  declare function checkCommandExistsOrThrow(cmd: string): Promise<void>;
36
- declare function checkIfFileExistsOrThrow(the_path: string): Promise<void>;
40
+ declare function checkIfFileExistsOrThrow(thePath: string): Promise<void>;
37
41
 
38
- export { SSEClient, SSEResponse, checkCommandExistsOrThrow, checkIfDirExistsOrThrow, checkIfFileExistsOrThrow, chmodPlusX, createTempDir, isWindows, throwIfDirNotEmpty };
42
+ 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(res) {
199
- this.res = res;
200
- res.setHeader("Content-Type", "text/event-stream");
201
- res.setHeader("Cache-Control", "no-cache");
202
- res.setHeader("Connection", "keep-alive");
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) return;
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
- let lines = buffer.split("\n");
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) continue;
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)) throw new Error(`path ${path2} not exists`);
330
- if (!(await fs.stat(path2)).isDirectory()) throw new Error(`${path2} is a file, require dir`);
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,28 @@ function createTempDir() {
338
346
  fs.mkdirSync(tempDirPath);
339
347
  return tempDirPath;
340
348
  }
341
- async function throwIfDirNotEmpty(dir_path) {
342
- if (!await fs.pathExists(dir_path)) throw new Error("dir not exists");
343
- const isEmpty = await fs.readdir(dir_path).then((x) => x.length === 0);
344
- if (!isEmpty) throw new Error(`dir ${dir_path} must be empty`);
349
+ function createTempFilePath() {
350
+ const tmpDir = os.tmpdir();
351
+ const timestamp = Date.now();
352
+ const p = path.join(tmpDir, `temp_file_${timestamp}`);
353
+ return {
354
+ getName: () => p,
355
+ cleanUp: () => {
356
+ try {
357
+ fs.unlinkSync(p);
358
+ } catch (e) {
359
+ }
360
+ }
361
+ };
362
+ }
363
+ async function throwIfDirNotEmpty(dirPath) {
364
+ if (!await fs.pathExists(dirPath)) {
365
+ throw new Error("dir not exists");
366
+ }
367
+ const isEmpty = await fs.readdir(dirPath).then((x) => x.length === 0);
368
+ if (!isEmpty) {
369
+ throw new Error(`dir ${dirPath} must be empty`);
370
+ }
345
371
  }
346
372
  async function checkCommandExistsOrThrow(cmd) {
347
373
  try {
@@ -350,11 +376,15 @@ async function checkCommandExistsOrThrow(cmd) {
350
376
  throw new Error(`cmd ${cmd} not exists`);
351
377
  }
352
378
  }
353
- async function checkIfFileExistsOrThrow(the_path) {
354
- if (!the_path) throw "path is empty";
355
- the_path = removeQuotes(the_path);
356
- if (!await fs.pathExists(the_path)) throw new Error(`path ${the_path} not exists`);
357
- if (!(await fs.stat(the_path)).isFile()) throw new Error(`${the_path} is a dir, require file`);
379
+ async function checkIfFileExistsOrThrow(thePath) {
380
+ if (!thePath) throw "path is empty";
381
+ thePath = removeQuotes(thePath);
382
+ if (!await fs.pathExists(thePath)) {
383
+ throw new Error(`path ${thePath} not exists`);
384
+ }
385
+ if (!(await fs.stat(thePath)).isFile()) {
386
+ throw new Error(`${thePath} is a dir, require file`);
387
+ }
358
388
  }
359
389
  function removeQuotes(str) {
360
390
  if (str.startsWith('"') && str.endsWith('"')) {
@@ -371,6 +401,7 @@ export {
371
401
  checkIfFileExistsOrThrow,
372
402
  chmodPlusX,
373
403
  createTempDir,
404
+ createTempFilePath,
374
405
  isWindows,
375
406
  throwIfDirNotEmpty
376
407
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rg-dev/stdlib",
3
- "version": "1.0.34",
3
+ "version": "1.0.36",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",