Skip to content

Commit 49d72a8

Browse files
authored
sftp_password_based_auth-watch-remote-directory and refactor (#14940)
1 parent 3db8dff commit 49d72a8

File tree

11 files changed

+164
-254
lines changed

11 files changed

+164
-254
lines changed

components/sftp/actions/upload-file/upload-file.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
key: "sftp-upload-file",
77
name: "Upload File",
88
description: "Uploads a file or string in UTF-8 format to the SFTP server. [See the documentation](https://github.com/theophilusx/ssh2-sftp-client#org99d1b64)",
9-
version: "0.2.0",
9+
version: "0.3.0",
1010
type: "action",
1111
props: {
1212
app,

components/sftp/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/sftp",
3-
"version": "0.3.8",
3+
"version": "0.4.0",
44
"description": "Pipedream SFTP Components",
55
"main": "sftp.app.mjs",
66
"keywords": [
@@ -10,8 +10,8 @@
1010
"homepage": "https://pipedream.com/apps/sftp",
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"dependencies": {
13-
"@pipedream/platform": "^1.2.0",
14-
"ssh2-sftp-client": "^8.1.0"
13+
"@pipedream/platform": "^3.0.3",
14+
"ssh2-sftp-client": "^11.0.0"
1515
},
1616
"gitHead": "e12480b94cc03bed4808ebc6b13e7fdb3a1ba535",
1717
"publishConfig": {

components/sftp/sftp.app.mjs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,27 @@ export default {
2020
},
2121
},
2222
methods: {
23-
async connect() {
23+
getConfig() {
2424
const {
2525
host,
2626
username,
2727
privateKey,
2828
} = this.$auth;
2929

30-
const config = {
30+
return {
3131
host,
3232
username,
3333
privateKey,
3434
};
35-
35+
},
36+
async connect() {
3637
const sftp = new Client();
37-
await sftp.connect(config);
38+
await sftp.connect(this.getConfig());
3839
return sftp;
3940
},
41+
async disconnect(sftp) {
42+
await sftp.end();
43+
},
4044
async put({
4145
buffer,
4246
remotePath,

components/sftp/sources/common/base.mjs

Lines changed: 0 additions & 125 deletions
This file was deleted.

components/sftp/sources/watch-remote-directory/watch-remote-directory.mjs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
import sftpApp from "../../sftp.app.mjs";
1+
import { createHash } from "crypto";
2+
import app from "../../sftp.app.mjs";
23
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
34

45
export default {
56
key: "sftp-watch-remote-directory",
67
name: "New Remote Directory Watcher",
78
description: "Emit new events when files get created, changed or deleted from a remote directory. [See the docs](https://github.com/theophilusx/ssh2-sftp-client#orgfac43d1)",
8-
version: "0.0.4",
9+
version: "0.1.0",
910
type: "source",
1011
dedupe: "unique",
1112
props: {
12-
sftpApp,
13+
app,
1314
timer: {
1415
type: "$.interface.timer",
1516
default: {
@@ -121,9 +122,15 @@ export default {
121122
}
122123
},
123124
getEventId(event) {
124-
return `${event.event}|${event.path}|${event.size}|${event.modifyTime}`;
125+
const uniqueIdentifier =
126+
createHash("md5")
127+
.update(event.path + event.modifyTime)
128+
.digest("hex");
129+
return `${event.event}|${event.path}|${event.size}|${event.modifyTime}|${uniqueIdentifier}`;
125130
},
126131
emitEvents(events) {
132+
// sort events by modification time in ascending order
133+
events.sort((a, b) => a.modifyTime - b.modifyTime);
127134
for (const fileChangeEvent of events) {
128135
this.$emit(fileChangeEvent, {
129136
id: this.getEventId(fileChangeEvent),
@@ -132,17 +139,11 @@ export default {
132139
});
133140
}
134141
},
135-
async connect() {
136-
return await this.sftpApp.connect();
137-
},
138-
async disconnect(sftp) {
139-
await sftp.end();
140-
},
141142
},
142143
async run() {
143144
this.validateRootDirectory(this.rootDirectory);
144145

145-
const sftp = await this.connect();
146+
const sftp = await this.app.connect();
146147

147148
let directories = [
148149
{
@@ -159,6 +160,6 @@ export default {
159160
this.emitEvents(filesChangesWithEvent);
160161

161162
this.setFiles(currentFiles);
162-
await this.disconnect(sftp);
163+
await this.app.disconnect(sftp);
163164
},
164165
};
Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,14 @@
1-
// legacy_hash_id: a_YEikdQ
2-
import Client from "ssh2-sftp-client";
1+
import utils from "../../common/utils.mjs";
2+
import component from "../../../sftp/actions/upload-file/upload-file.mjs";
33

44
export default {
5+
...component,
6+
props: utils.buildAppProps({
7+
component,
8+
}),
59
key: "sftp_password_based_auth-upload-file",
6-
name: "Upload String as File",
7-
description: "Uploads a UTF-8 string as a file on an SFTP server",
8-
version: "0.1.1",
10+
name: "Upload File (Password Auth)",
11+
description: "Uploads a file or string in UTF-8 format to the SFTP server. [See the documentation](https://github.com/theophilusx/ssh2-sftp-client#org99d1b64)",
12+
version: "0.2.0",
913
type: "action",
10-
props: {
11-
sftp_password_based_auth: {
12-
type: "app",
13-
app: "sftp_password_based_auth",
14-
},
15-
data: {
16-
type: "string",
17-
description: "A UTF-8 string to upload as a file on the remote server.",
18-
},
19-
remotePath: {
20-
type: "string",
21-
label: "Remote Path",
22-
description: "The path to the remote file to be created on the server.",
23-
},
24-
},
25-
async run({ $ }) {
26-
const {
27-
host,
28-
username,
29-
password,
30-
} = this.sftp_password_based_auth.$auth;
31-
32-
const config = {
33-
host,
34-
username,
35-
password,
36-
};
37-
38-
const sftp = new Client();
39-
40-
await sftp.connect(config);
41-
$.export("putResponse", await sftp.put(Buffer.from(this.data), this.remotePath));
42-
await sftp.end();
43-
},
4414
};
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import app from "../sftp_password_based_auth.app.mjs";
2+
3+
function buildPropDefinitions({
4+
app = {}, props = {},
5+
}) {
6+
return Object.entries(props)
7+
.reduce((newProps, [
8+
key,
9+
prop,
10+
]) => {
11+
if (!prop.propDefinition) {
12+
return Object.assign(newProps, {
13+
[key]: prop,
14+
});
15+
}
16+
17+
const [
18+
, ...propDefinitionItems
19+
] = prop.propDefinition;
20+
21+
return Object.assign(newProps, {
22+
[key]: Object.assign(prop, {
23+
propDefinition: [
24+
app,
25+
...propDefinitionItems,
26+
],
27+
}),
28+
});
29+
}, {});
30+
}
31+
32+
function getPropsOnly({
33+
component, omitProps = [], appLabel, addedProps = {},
34+
} = {}) {
35+
const {
36+
// eslint-disable-next-line no-unused-vars
37+
[appLabel]: appProp,
38+
...props
39+
} = component.props;
40+
const builtProps = Object.entries(props)
41+
.reduce((reduction, [
42+
key,
43+
value,
44+
]) => {
45+
if (omitProps.includes(key)) {
46+
return reduction;
47+
}
48+
return Object.assign(reduction, {
49+
[key]: value,
50+
});
51+
}, {});
52+
return {
53+
...builtProps,
54+
...addedProps,
55+
};
56+
}
57+
58+
function buildAppProps({
59+
component, omitProps = [], appLabel = "app", addedProps,
60+
} = {}) {
61+
return {
62+
[appLabel]: app,
63+
...buildPropDefinitions({
64+
app,
65+
props: getPropsOnly({
66+
component,
67+
omitProps,
68+
appLabel,
69+
addedProps,
70+
}),
71+
}),
72+
};
73+
}
74+
75+
export default {
76+
buildPropDefinitions,
77+
getPropsOnly,
78+
buildAppProps,
79+
};

0 commit comments

Comments
 (0)