@ps-aux/nodebup 0.9.7 → 0.9.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -27,7 +27,7 @@ const parseConnectionUrl = url => {
27
27
  var _url$match;
28
28
 
29
29
  // TODO what if there are query params in the end?
30
- const regex = /postgres:\/\/(?<username>.*):(?<password>.*)@(?<host>.*):(?<port>\d*)(\/(?<database>.*))?$/;
30
+ const regex = /postgres:\/\/(?<username>.*):(?<password>.*)@(?<host>.*):(?<port>\d*)$/;
31
31
  const match = (_url$match = url.match(regex)) === null || _url$match === void 0 ? void 0 : _url$match.groups;
32
32
  if (!match || !match.username || !match.password || !match.host || !match.port) throw new Error(`The Postgres connection URL does not match required regex: ${regex.toString()}`);
33
33
  return {
@@ -103,7 +103,7 @@ let PgBackupController = (_dec = (0, _inversify.injectable)(), _dec2 = Reflect.m
103
103
  const version = this.getVersion(pgVersion);
104
104
  const storage = this.storageBackendProvider.provide(); // To validate
105
105
 
106
- parseConnectionUrl(pgUrl);
106
+ const con = parseConnectionUrl(pgUrl);
107
107
  this.log.info(`Restoring Postgres database, version=${version}`);
108
108
  await this.fs.inTmpDir('pg-restore', async dirStr => {
109
109
  const dir = _Path.AbsPath.from(dirStr);
@@ -115,10 +115,13 @@ let PgBackupController = (_dec = (0, _inversify.injectable)(), _dec2 = Reflect.m
115
115
  await this.zip.unzipDir(zipFile, dir);
116
116
  this.fs.rmFile(zipFile);
117
117
  const outFile = this.fs.listFiles(dir)[0];
118
- this.log.debug('Will run psql import from %s', outFile.str()); // Don't forget that this itself might be run in docker
118
+ this.log.debug('Will run psql import from %s', outFile.str()); // Database is set to 'postgres' so that also users which don't have db created can use db-less URL
119
+ // Restoring user needs to have the admin access anyway
120
+
121
+ const connectionArgs = `-h ${con.host} -p ${con.port} -U ${con.username} -d postgres`; // Don't forget that this itself might be run in docker
119
122
  // therefore volume mounts are not usable (will apply to the location at host)
120
123
 
121
- this.sh.exec(`docker run --network host -i ` + `postgres:${version} ` + `psql ${pgUrl} -v ON_ERROR_STOP=0 < ${outFile.str()}`);
124
+ this.sh.exec(`docker run --network host -i ` + `-e PGPASSWORD=${con.password} ` + `postgres:${version} ` + `psql ${connectionArgs} -v ON_ERROR_STOP=0 < ${outFile.str()}`);
122
125
  });
123
126
  });
124
127
  }
@@ -3,9 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.Zipper = void 0;
7
-
8
- var _admZip = _interopRequireDefault(require("adm-zip"));
6
+ exports.buildZipFromDirectory = exports.Zipper = void 0;
9
7
 
10
8
  var _inversify = require("inversify");
11
9
 
@@ -15,6 +13,12 @@ var _unzipper = _interopRequireDefault(require("unzipper"));
15
13
 
16
14
  var _nodeFs = _interopRequireDefault(require("node:fs"));
17
15
 
16
+ var _jszip = _interopRequireDefault(require("jszip"));
17
+
18
+ var _path = _interopRequireDefault(require("path"));
19
+
20
+ var _promises = _interopRequireDefault(require("stream/promises"));
21
+
18
22
  var _dec, _class;
19
23
 
20
24
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -25,19 +29,39 @@ let Zipper = (_dec = (0, _inversify.injectable)(), _dec(_class = class Zipper {
25
29
  constructor() {
26
30
  _defineProperty(this, "log", (0, _logging.classObjLog)(this));
27
31
 
28
- _defineProperty(this, "zipDir", async (dir, to) => {
29
- this.log.trace(`Zipping %s to %s`, dir.str(), to.str());
30
- const z = new _admZip.default();
31
- z.addLocalFolder(dir.str());
32
- z.writeZip(to.str());
32
+ _defineProperty(this, "zipDir", async (dirPath, to) => {
33
+ const srcDir = dirPath.str();
34
+ const dst = to.str();
35
+
36
+ const dstDir = _path.default.dirname(dst);
37
+
38
+ this.log.trace(`Zipping %s to %s`, srcDir, dst);
39
+ const zip = new _jszip.default();
40
+ buildZipFromDirectory(srcDir, zip);
41
+ const r = await zip.generateNodeStream();
42
+
43
+ if (!_nodeFs.default.existsSync(dstDir)) {
44
+ _nodeFs.default.mkdirSync(dstDir);
45
+ }
46
+
47
+ const ws = _nodeFs.default.createWriteStream(dst);
48
+
49
+ await _promises.default.pipeline(r, ws);
33
50
  });
34
51
 
35
52
  _defineProperty(this, "unzipDir", async (zipFile, toDir) => {
53
+ // TODO can be replaced with JsZip ?
36
54
  this.log.trace(`Unzipping %s to %s`, zipFile.str(), toDir.str());
55
+
56
+ const input = _nodeFs.default.createReadStream(zipFile.str());
57
+
58
+ const out = _unzipper.default.Extract({
59
+ path: toDir.str()
60
+ }); // Cannot use Streams.pipeline bcs it sometimes resolves before the file exists
61
+
62
+
37
63
  return new Promise((res, rej) => {
38
- _nodeFs.default.createReadStream(zipFile.str()).pipe(_unzipper.default.Extract({
39
- path: toDir.str()
40
- })).on('close', () => {
64
+ input.pipe(out).on('close', () => {
41
65
  res();
42
66
  }).on('error', rej);
43
67
  });
@@ -45,4 +69,26 @@ let Zipper = (_dec = (0, _inversify.injectable)(), _dec(_class = class Zipper {
45
69
  }
46
70
 
47
71
  }) || _class);
48
- exports.Zipper = Zipper;
72
+ exports.Zipper = Zipper;
73
+
74
+ const buildZipFromDirectory = (dir, zip, root = dir) => {
75
+ const list = _nodeFs.default.readdirSync(dir);
76
+
77
+ for (let file of list) {
78
+ file = _path.default.resolve(dir, file);
79
+
80
+ const stat = _nodeFs.default.statSync(file);
81
+
82
+ if (stat && stat.isDirectory()) {
83
+ buildZipFromDirectory(file, zip, root);
84
+ } else {
85
+ const filedata = _nodeFs.default.createReadStream(file);
86
+
87
+ const p = _path.default.relative(root, file);
88
+
89
+ zip.file(p, filedata);
90
+ }
91
+ }
92
+ };
93
+
94
+ exports.buildZipFromDirectory = buildZipFromDirectory;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ps-aux/nodebup",
3
- "version": "0.9.7",
3
+ "version": "0.9.9",
4
4
  "description": "",
5
5
  "module": "lib/index.js",
6
6
  "main": "lib/index.js",
@@ -80,12 +80,12 @@
80
80
  "@hapi/joi": "^17.1.1",
81
81
  "@ps-aux/nclif": "^0.0.7-alpha.1",
82
82
  "@types/hapi__joi": "^17.1.8",
83
- "adm-zip": "^0.5.9",
84
83
  "axios": "^0.24.0",
85
84
  "handlebars": "^4.7.7",
86
85
  "ini": "^2.0.0",
87
86
  "inversify": "^6.0.1",
88
87
  "js-yaml": "^4.1.0",
88
+ "jszip": "^3.10.1",
89
89
  "pino": "^7.11.0",
90
90
  "pino-pretty": "^9.1.1",
91
91
  "ramda": "^0.27.1",