@dnax/core 0.45.2 → 0.45.3

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/utils/index.ts +63 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dnax/core",
3
- "version": "0.45.2",
3
+ "version": "0.45.3",
4
4
  "module": "index.ts",
5
5
  "type": "module",
6
6
  "bin": {
package/utils/index.ts CHANGED
@@ -14,6 +14,7 @@ import mime from "mime-types";
14
14
  import * as uuid from "uuid";
15
15
  import * as urlencode from "urlencode";
16
16
  import dotJson from "dot-object";
17
+ import fs from "fs-extra";
17
18
  import deepCopy from "deepcopy";
18
19
  import { applyPatch, createPatch, Pointer } from "rfc6902";
19
20
 
@@ -383,8 +384,70 @@ const password = {
383
384
  verify: verifyHashPassword,
384
385
  };
385
386
 
387
+ const file = {
388
+ exist: async (path: string): Promise<boolean> => {
389
+ return new Promise(async (resolve, reject) => {
390
+ try {
391
+ let exist = await fs.pathExists(path);
392
+ resolve(exist);
393
+ } catch (err: any) {
394
+ reject(err?.message);
395
+ }
396
+ });
397
+ },
398
+ saveToDisk: async (
399
+ path: string,
400
+ data: any
401
+ ): Promise<{
402
+ size: number;
403
+ type: string;
404
+ lastModified: Date;
405
+ name: string | undefined;
406
+ }> => {
407
+ // save to disk
408
+ return new Promise(async (resolve, reject) => {
409
+ try {
410
+ await Bun.write(path, data, {
411
+ createPath: true,
412
+ });
413
+ let file = await Bun.file(path);
414
+ resolve({
415
+ name: file.name,
416
+ lastModified: moment.unix(file.lastModified).toDate(),
417
+ size: file.size,
418
+ type: file.type,
419
+ });
420
+ } catch (err: any) {
421
+ reject(err?.message);
422
+ }
423
+ });
424
+ },
425
+
426
+ deleteFromDisk: async (path: string) => {
427
+ return new Promise(async (resolve, reject) => {
428
+ try {
429
+ // check if path file exists
430
+ let exist = await fs.pathExists(path);
431
+ let stat = await fs.stat(path);
432
+ if (stat.isDirectory()) {
433
+ reject("delete directory not allowed");
434
+ }
435
+
436
+ if (!exist) {
437
+ reject("File not found");
438
+ }
439
+ await fs.unlink(path);
440
+ resolve(true);
441
+ } catch (err: any) {
442
+ reject(err?.message);
443
+ }
444
+ });
445
+ },
446
+ };
447
+
386
448
  const contextError = ContextError;
387
449
  export {
450
+ file,
388
451
  mime,
389
452
  dotJson,
390
453
  uuid,