@prismatic-io/components-sftp 0.0.0-stage → 0.0.1

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.
@@ -0,0 +1,734 @@
1
+ //#region \0rolldown/runtime.js
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
+ key = keys[i];
11
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
+ get: ((k) => from[k]).bind(null, key),
13
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
+ });
15
+ }
16
+ return to;
17
+ };
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
+ value: mod,
20
+ enumerable: true
21
+ }) : target, mod));
22
+ //#endregion
23
+ let _prismatic_io_spectral = require("@prismatic-io/spectral");
24
+ let node_path = require("node:path");
25
+ let mime_types = require("mime-types");
26
+ mime_types = __toESM(mime_types);
27
+ let ssh2_sftp_client = require("ssh2-sftp-client");
28
+ ssh2_sftp_client = __toESM(ssh2_sftp_client);
29
+ let node_fs = require("node:fs");
30
+ let minimatch = require("minimatch");
31
+ //#region src/inputs/common.ts
32
+ const connection = (0, _prismatic_io_spectral.input)({
33
+ label: "Connection",
34
+ type: "connection",
35
+ required: true,
36
+ comments: "The SFTP connection to use."
37
+ });
38
+ const path = (0, _prismatic_io_spectral.input)({
39
+ label: "Path",
40
+ placeholder: "Enter path",
41
+ type: "string",
42
+ required: true,
43
+ comments: "The path of the directory on the SFTP server to list files from.",
44
+ example: "/path/to/directory/",
45
+ clean: _prismatic_io_spectral.util.types.toString
46
+ });
47
+ const recursive = (0, _prismatic_io_spectral.input)({
48
+ label: "Include Subfolders",
49
+ comments: "When true, recursively creates any missing directories in the path.",
50
+ type: "boolean",
51
+ required: false,
52
+ default: "true",
53
+ clean: _prismatic_io_spectral.util.types.toBool
54
+ });
55
+ const returnBuffer = (0, _prismatic_io_spectral.input)({
56
+ label: "Return Buffer",
57
+ type: "boolean",
58
+ required: true,
59
+ default: "false",
60
+ comments: "When true, treats the file as a binary file with content type 'application/octet-stream', even if it is a text file. This is helpful if you are processing non-UTF-8 text files, as the runner assumes text files are UTF-8.",
61
+ clean: _prismatic_io_spectral.util.types.toBool
62
+ });
63
+ const outputPath = (0, _prismatic_io_spectral.input)({
64
+ label: "Path",
65
+ placeholder: "Enter path",
66
+ type: "string",
67
+ required: true,
68
+ comments: "The path to the file on the SFTP server.",
69
+ example: "/we/love/commas.csv",
70
+ clean: _prismatic_io_spectral.util.types.toString
71
+ });
72
+ const outputPathAppend = (0, _prismatic_io_spectral.input)({
73
+ ...outputPath,
74
+ placeholder: "Enter path",
75
+ comments: "The path on the SFTP server where data will be appended.",
76
+ example: "/path/to/remote/file.txt"
77
+ });
78
+ const data = (0, _prismatic_io_spectral.input)({
79
+ label: "Data",
80
+ placeholder: "Enter data",
81
+ type: "text",
82
+ required: true,
83
+ comments: "The text or data to write into the file.",
84
+ clean: (value) => _prismatic_io_spectral.util.types.isData(value) ? _prismatic_io_spectral.util.types.toData(value).data : Buffer.from(typeof value === "string" ? value : JSON.stringify(value))
85
+ });
86
+ const dataAppend = (0, _prismatic_io_spectral.input)({
87
+ ...data,
88
+ placeholder: "Enter data",
89
+ comments: "The text or data to append to the file."
90
+ });
91
+ const pattern = (0, _prismatic_io_spectral.input)({
92
+ label: "Pattern",
93
+ default: "*",
94
+ placeholder: "Enter pattern",
95
+ type: "string",
96
+ required: false,
97
+ comments: "The glob-style pattern for filtering files (e.g., *.txt).",
98
+ example: "*.txt",
99
+ clean: _prismatic_io_spectral.util.types.toString
100
+ });
101
+ const includeSubdirectories = (0, _prismatic_io_spectral.input)({
102
+ label: "Include Subdirectories",
103
+ default: "false",
104
+ type: "boolean",
105
+ required: false,
106
+ comments: "When true, recursively monitors files in all subdirectories. When false, only monitors files in the specified directory.",
107
+ clean: _prismatic_io_spectral.util.types.toBool
108
+ });
109
+ const includeDirectories = (0, _prismatic_io_spectral.input)({
110
+ label: "Include Directories",
111
+ default: "false",
112
+ type: "boolean",
113
+ required: false,
114
+ comments: "When true, lists directories in addition to files. When false, only lists files.",
115
+ clean: _prismatic_io_spectral.util.types.toBool
116
+ });
117
+ //#endregion
118
+ //#region src/connections/index.ts
119
+ const commonInputs = {
120
+ username: {
121
+ label: "Username",
122
+ placeholder: "Enter username",
123
+ type: "string",
124
+ required: true,
125
+ comments: "The username for SFTP authentication.",
126
+ example: "john.doe"
127
+ },
128
+ host: {
129
+ label: "Host",
130
+ placeholder: "Enter host",
131
+ type: "string",
132
+ required: true,
133
+ comments: "The address of the SFTP server. This should be either an IP address or hostname.",
134
+ example: "sftp.example.com",
135
+ onPremControlled: true
136
+ },
137
+ port: {
138
+ label: "Port",
139
+ placeholder: "Enter port",
140
+ default: "22",
141
+ required: true,
142
+ comments: "The port of the SFTP server.",
143
+ type: "string",
144
+ example: "2222",
145
+ onPremControlled: true
146
+ },
147
+ timeout: {
148
+ label: "Timeout",
149
+ placeholder: "Enter timeout",
150
+ required: false,
151
+ comments: "How long the client will await a request.",
152
+ type: "string",
153
+ default: "3000",
154
+ example: "4000"
155
+ },
156
+ enableUnsecureServerHostKeyAlgorithms: {
157
+ label: "Enable Unsecure Server Host Key Algorithms",
158
+ comments: "When true, unsecure server host key algorithms will be added to the connection.",
159
+ required: false,
160
+ type: "boolean"
161
+ },
162
+ enableUnsecureCiphers: {
163
+ label: "Enable Unsecure Ciphers",
164
+ comments: "When true, CBC ciphers will be added to the connection.",
165
+ required: false,
166
+ type: "boolean"
167
+ },
168
+ customServerHostKeyAlgorithms: {
169
+ label: "Custom Server Host Key Algorithms",
170
+ comments: "A comma-separated list of custom server host key algorithms. Overrides the default server host key algorithms. Algorithm order matters. Advanced setting.",
171
+ placeholder: "Enter algorithms",
172
+ example: ["ssh-rsa", "ssh-dss"].join(", "),
173
+ required: false,
174
+ type: "string"
175
+ },
176
+ customCiphers: {
177
+ label: "Custom Ciphers",
178
+ comments: "A comma-separated list of custom ciphers. Overrides the default ciphers. Cipher order matters. Advanced setting.",
179
+ placeholder: "Enter ciphers",
180
+ example: [
181
+ "aes128-ctr",
182
+ "aes192-ctr",
183
+ "aes256-ctr"
184
+ ].join(", "),
185
+ required: false,
186
+ type: "string"
187
+ },
188
+ customKexAlgorithms: {
189
+ label: "Custom KEX Algorithms",
190
+ comments: "A comma-separated list of custom key exchange algorithms. Algorithm order matters. Advanced setting.",
191
+ placeholder: "Enter KEX algorithms",
192
+ example: ["diffie-hellman-group14-sha1", "diffie-hellman-group14-sha256"].join(", "),
193
+ required: false,
194
+ type: "string"
195
+ }
196
+ };
197
+ const basic = (0, _prismatic_io_spectral.onPremConnection)({
198
+ key: "basic",
199
+ display: {
200
+ label: "Basic Authentication",
201
+ description: "Authenticate using username and password"
202
+ },
203
+ inputs: {
204
+ password: {
205
+ label: "Password",
206
+ comments: "The password for SFTP authentication.",
207
+ placeholder: "Enter password",
208
+ type: "password",
209
+ required: true,
210
+ example: "p@s$W0Rd"
211
+ },
212
+ ...commonInputs
213
+ }
214
+ });
215
+ const privateKey = (0, _prismatic_io_spectral.onPremConnection)({
216
+ key: "privateKey",
217
+ display: {
218
+ label: "Private Key",
219
+ description: "Authenticate using SSH private key"
220
+ },
221
+ inputs: {
222
+ privateKey: {
223
+ label: "Private Key",
224
+ comments: "The SSH private key for authentication.",
225
+ placeholder: "Enter private key",
226
+ type: "text",
227
+ required: true,
228
+ example: "-----BEGIN OPENSSH PRIVATE KEY-----\nabc123..."
229
+ },
230
+ passphrase: {
231
+ label: "Key Passphrase",
232
+ comments: "The passphrase for the private key. Leave blank if none.",
233
+ placeholder: "Enter passphrase",
234
+ type: "password",
235
+ required: false,
236
+ example: "p@s$PHr@$3"
237
+ },
238
+ password: {
239
+ label: "Password",
240
+ comments: "Though uncommon, some SFTP servers that use private keys may also require a password. Leave blank if none.",
241
+ placeholder: "Enter password",
242
+ type: "password",
243
+ required: false,
244
+ example: "p@s$W0Rd"
245
+ },
246
+ ...commonInputs
247
+ }
248
+ });
249
+ var connections_default = [basic, privateKey];
250
+ //#endregion
251
+ //#region src/constants.ts
252
+ const secureServerHostKeyAlgorithms = [
253
+ "ssh-ed25519",
254
+ "ecdsa-sha2-nistp256",
255
+ "ecdsa-sha2-nistp384",
256
+ "ecdsa-sha2-nistp521",
257
+ "rsa-sha2-512",
258
+ "rsa-sha2-256"
259
+ ];
260
+ const unsecureServerHostKeyAlgorithms = ["ssh-rsa", "ssh-dss"];
261
+ const secureCipherAlgorithms = [
262
+ "chacha20-poly1305@openssh.com",
263
+ "aes128-gcm@openssh.com",
264
+ "aes256-gcm@openssh.com",
265
+ "aes128-ctr",
266
+ "aes192-ctr",
267
+ "aes256-ctr"
268
+ ];
269
+ const unsecureCipherAlgorithms = ["aes256-cbc"];
270
+ //#endregion
271
+ //#region src/client.ts
272
+ const getAuthParams = (connection) => {
273
+ switch (connection.key) {
274
+ case basic.key: return {
275
+ username: _prismatic_io_spectral.util.types.toString(connection.fields.username),
276
+ password: _prismatic_io_spectral.util.types.toString(connection.fields.password)
277
+ };
278
+ case privateKey.key: return {
279
+ username: _prismatic_io_spectral.util.types.toString(connection.fields.username),
280
+ privateKey: _prismatic_io_spectral.util.types.toString(connection.fields.privateKey),
281
+ passphrase: _prismatic_io_spectral.util.types.toString(connection.fields.passphrase),
282
+ password: _prismatic_io_spectral.util.types.toString(connection.fields.password)
283
+ };
284
+ default: throw new _prismatic_io_spectral.ConnectionError(connection, "Unknown Connection type provided.");
285
+ }
286
+ };
287
+ const getSftpClient = async (connection, debug) => {
288
+ const sftp = new ssh2_sftp_client.default();
289
+ sftp.on("keyboard-interactive", (_name, _instructions, _instructionsLang, _prompts, finish) => {
290
+ if (debug) console.debug("This SFTP server requires keyboard-interactive login. Falling back to keyboard-interactive.");
291
+ finish([connection.fields.password]);
292
+ });
293
+ const { host, port, timeout, enableUnsecureCiphers, enableUnsecureServerHostKeyAlgorithms, customServerHostKeyAlgorithms, customCiphers, customKexAlgorithms } = connection.fields;
294
+ try {
295
+ let cipher = _prismatic_io_spectral.util.types.toBool(enableUnsecureCiphers) ? [...secureCipherAlgorithms, ...unsecureCipherAlgorithms] : secureCipherAlgorithms;
296
+ let serverHostKey = _prismatic_io_spectral.util.types.toBool(enableUnsecureServerHostKeyAlgorithms) ? [...secureServerHostKeyAlgorithms, ...unsecureServerHostKeyAlgorithms] : secureServerHostKeyAlgorithms;
297
+ const customServerHostKeyAlgorithmsString = _prismatic_io_spectral.util.types.toString(customServerHostKeyAlgorithms);
298
+ if (customServerHostKeyAlgorithmsString) serverHostKey = customServerHostKeyAlgorithmsString.replace(/\s+/g, "").split(",");
299
+ const customCiphersString = _prismatic_io_spectral.util.types.toString(customCiphers);
300
+ if (customCiphersString) cipher = customCiphersString.replace(/\s+/g, "").split(",");
301
+ const customKexAlgorithmsString = _prismatic_io_spectral.util.types.toString(customKexAlgorithms);
302
+ const kex = customKexAlgorithmsString ? customKexAlgorithmsString.replace(/\s+/g, "").split(",") : void 0;
303
+ await sftp.connect({
304
+ host: _prismatic_io_spectral.util.types.toString(host),
305
+ port: _prismatic_io_spectral.util.types.toInt(port),
306
+ readyTimeout: _prismatic_io_spectral.util.types.toInt(timeout) || 3e3,
307
+ tryKeyboard: true,
308
+ debug: (msg) => {
309
+ if (debug) console.debug(msg);
310
+ },
311
+ algorithms: {
312
+ serverHostKey,
313
+ cipher,
314
+ ...kex ? { kex } : {}
315
+ },
316
+ ...getAuthParams(connection)
317
+ });
318
+ } catch (err) {
319
+ throw new _prismatic_io_spectral.ConnectionError(connection, `Unable to connect to SFTP server. ${err}`);
320
+ }
321
+ return sftp;
322
+ };
323
+ //#endregion
324
+ //#region src/examplePayloads.ts
325
+ const readFileExamplePayload = {
326
+ data: "Sample file contents",
327
+ contentType: "text/plain"
328
+ };
329
+ const fastGetExamplePayload = {
330
+ data: Buffer.from("Sample file contents"),
331
+ contentType: "text/plain"
332
+ };
333
+ const writeFileExamplePayload = { data: "Uploaded data stream to /upload/path/to/file.txt" };
334
+ const appendFileExamplePayload = { data: "Appended data to /upload/path/to/file.txt" };
335
+ const deleteFileExamplePayload = { data: null };
336
+ const moveFileExamplePayload = { data: null };
337
+ const statFileExamplePayload = { data: {
338
+ mode: 33279,
339
+ uid: 1e3,
340
+ gid: 985,
341
+ size: 5,
342
+ accessTime: 1566868566e3,
343
+ modifyTime: 1566868566e3,
344
+ isDirectory: false,
345
+ isFile: true,
346
+ isBlockDevice: false,
347
+ isCharacterDevice: false,
348
+ isSymbolicLink: false,
349
+ isFIFO: false,
350
+ isSocket: false
351
+ } };
352
+ const listDirectoryExamplePayload = { data: [
353
+ "folder1/file.txt",
354
+ "folder1/subfolder/example.txt",
355
+ "root.txt"
356
+ ] };
357
+ const createDirectoryExamplePayload = { data: "/path/to/new/directory/" };
358
+ //#endregion
359
+ //#region src/actions/files/index.ts
360
+ var files_default = {
361
+ readFile: (0, _prismatic_io_spectral.action)({
362
+ display: {
363
+ label: "Read File",
364
+ description: "Read a file from SFTP"
365
+ },
366
+ perform: async (context, { connection, inputPath, returnBuffer }) => {
367
+ const sftp = await getSftpClient(connection, context.debug.enabled);
368
+ try {
369
+ return {
370
+ data: await sftp.get(inputPath),
371
+ contentType: returnBuffer ? mime_types.types.bin : mime_types.lookup((0, node_path.basename)(inputPath)) || mime_types.types.bin
372
+ };
373
+ } finally {
374
+ await sftp.end();
375
+ }
376
+ },
377
+ inputs: {
378
+ connection,
379
+ inputPath: (0, _prismatic_io_spectral.input)({
380
+ label: "Path",
381
+ placeholder: "Path on SFTP server to read data from",
382
+ type: "string",
383
+ required: true,
384
+ comments: "Path of file on SFTP server to read data from",
385
+ example: "/path/to/file.txt",
386
+ clean: _prismatic_io_spectral.util.types.toString
387
+ }),
388
+ returnBuffer
389
+ },
390
+ examplePayload: readFileExamplePayload
391
+ }),
392
+ fastGet: (0, _prismatic_io_spectral.action)({
393
+ display: {
394
+ label: "Fast Get",
395
+ description: "Read a file from SFTP"
396
+ },
397
+ perform: async (context, { connection, inputPath }) => {
398
+ const sftp = await getSftpClient(connection, context.debug.enabled);
399
+ try {
400
+ const fileName = (0, node_path.basename)(inputPath);
401
+ const localPath = `/tmp/${fileName}`;
402
+ await sftp.fastGet(inputPath, localPath);
403
+ return {
404
+ data: await node_fs.promises.readFile(localPath),
405
+ contentType: returnBuffer ? mime_types.types.bin : mime_types.lookup(fileName) || mime_types.types.bin
406
+ };
407
+ } finally {
408
+ await sftp.end();
409
+ }
410
+ },
411
+ inputs: {
412
+ connection,
413
+ inputPath: (0, _prismatic_io_spectral.input)({
414
+ label: "Path",
415
+ placeholder: "Path on SFTP server to read data from",
416
+ type: "string",
417
+ required: true,
418
+ comments: "Path of file on SFTP server to read data from",
419
+ example: "/path/to/file.txt",
420
+ clean: _prismatic_io_spectral.util.types.toString
421
+ }),
422
+ returnBuffer
423
+ },
424
+ examplePayload: fastGetExamplePayload
425
+ }),
426
+ statFile: (0, _prismatic_io_spectral.action)({
427
+ display: {
428
+ label: "Stat File",
429
+ description: "Pull statistics about a file"
430
+ },
431
+ perform: async (context, { connection, inputPath }) => {
432
+ const sftp = await getSftpClient(connection, context.debug.enabled);
433
+ try {
434
+ return { data: await sftp.stat(inputPath) };
435
+ } finally {
436
+ await sftp.end();
437
+ }
438
+ },
439
+ inputs: {
440
+ connection,
441
+ inputPath: (0, _prismatic_io_spectral.input)({
442
+ label: "Path",
443
+ placeholder: "Path on SFTP server to read data from",
444
+ type: "string",
445
+ required: true,
446
+ comments: "Path of file on SFTP server to read data from",
447
+ example: "/path/to/file.txt",
448
+ clean: _prismatic_io_spectral.util.types.toString
449
+ })
450
+ },
451
+ examplePayload: statFileExamplePayload
452
+ }),
453
+ writeFile: (0, _prismatic_io_spectral.action)({
454
+ display: {
455
+ label: "Write File",
456
+ description: "Write a file to SFTP"
457
+ },
458
+ perform: async (context, { connection, data, outputPath }) => {
459
+ const sftp = await getSftpClient(connection, context.debug.enabled);
460
+ try {
461
+ return { data: await sftp.put(data, outputPath) };
462
+ } finally {
463
+ await sftp.end();
464
+ }
465
+ },
466
+ inputs: {
467
+ connection,
468
+ outputPath,
469
+ data
470
+ },
471
+ examplePayload: writeFileExamplePayload
472
+ }),
473
+ appendFile: (0, _prismatic_io_spectral.action)({
474
+ display: {
475
+ label: "Append File",
476
+ description: "Append data to an existing file on a SFTP server."
477
+ },
478
+ perform: async (context, { connection, data, outputPath }) => {
479
+ const sftp = await getSftpClient(connection, context.debug.enabled);
480
+ try {
481
+ await sftp.append(data, outputPath);
482
+ return { data: `Appended data to ${outputPath}` };
483
+ } finally {
484
+ await sftp.end();
485
+ }
486
+ },
487
+ inputs: {
488
+ connection,
489
+ outputPath: outputPathAppend,
490
+ data: dataAppend
491
+ },
492
+ examplePayload: appendFileExamplePayload
493
+ }),
494
+ deleteFile: (0, _prismatic_io_spectral.action)({
495
+ display: {
496
+ label: "Delete File",
497
+ description: "Delete a file from a SFTP server"
498
+ },
499
+ perform: async (context, { connection, path }) => {
500
+ const sftp = await getSftpClient(connection, context.debug.enabled);
501
+ try {
502
+ await sftp.delete(path);
503
+ } finally {
504
+ await sftp.end();
505
+ }
506
+ return null;
507
+ },
508
+ inputs: {
509
+ connection,
510
+ path: (0, _prismatic_io_spectral.input)({
511
+ label: "Path",
512
+ placeholder: "Path of file to delete",
513
+ type: "string",
514
+ required: true,
515
+ comments: "Path of file to delete",
516
+ example: "/path/to/file.txt",
517
+ clean: _prismatic_io_spectral.util.types.toString
518
+ })
519
+ },
520
+ examplePayload: deleteFileExamplePayload
521
+ }),
522
+ moveFile: (0, _prismatic_io_spectral.action)({
523
+ display: {
524
+ label: "Move File",
525
+ description: "Move a file on an SFTP server"
526
+ },
527
+ perform: async (context, { connection, sourcePath, destinationPath }) => {
528
+ const sftp = await getSftpClient(connection, context.debug.enabled);
529
+ try {
530
+ await sftp.rename(sourcePath, destinationPath);
531
+ } finally {
532
+ await sftp.end();
533
+ }
534
+ return null;
535
+ },
536
+ inputs: {
537
+ connection,
538
+ sourcePath: (0, _prismatic_io_spectral.input)({
539
+ label: "Source Path",
540
+ placeholder: "Path of file to move",
541
+ type: "string",
542
+ required: true,
543
+ comments: "Path of file to move",
544
+ example: "/my/starting/path.txt",
545
+ clean: _prismatic_io_spectral.util.types.toString
546
+ }),
547
+ destinationPath: (0, _prismatic_io_spectral.input)({
548
+ label: "Destination Path",
549
+ placeholder: "Path to move the file to",
550
+ type: "string",
551
+ required: true,
552
+ comments: "Path of file to move",
553
+ example: "/my/destination/path.txt",
554
+ clean: _prismatic_io_spectral.util.types.toString
555
+ })
556
+ },
557
+ examplePayload: moveFileExamplePayload
558
+ })
559
+ };
560
+ //#endregion
561
+ //#region src/actions/directories/index.ts
562
+ var directories_default = {
563
+ createDirectory: (0, _prismatic_io_spectral.action)({
564
+ display: {
565
+ label: "Create Directory",
566
+ description: "Create a new directory. When Include Subfolders is enabled, recursively creates any missing directories in the path."
567
+ },
568
+ perform: async (context, { connection, path, recursive }) => {
569
+ const sftp = await getSftpClient(connection, context.debug.enabled);
570
+ try {
571
+ return { data: await sftp.mkdir(path, recursive) };
572
+ } finally {
573
+ await sftp.end();
574
+ }
575
+ },
576
+ inputs: {
577
+ connection,
578
+ path,
579
+ recursive
580
+ },
581
+ examplePayload: createDirectoryExamplePayload
582
+ }),
583
+ listDirectory: (0, _prismatic_io_spectral.action)({
584
+ display: {
585
+ label: "List Directory",
586
+ description: "List files and directories in a directory on an SFTP server. Optionally list files in subdirectories."
587
+ },
588
+ perform: async (context, { connection, path, pattern, includeSubdirectories, includeDirectories }) => {
589
+ const sftp = await getSftpClient(connection, context.debug.enabled);
590
+ try {
591
+ const listDirectoryFilesRecursive = async (currentPath) => {
592
+ const fileList = await sftp.list(currentPath, (file) => pattern ? (0, minimatch.minimatch)(file.name, pattern) : true);
593
+ const results = [];
594
+ for (const file of fileList) {
595
+ const fullPath = `${currentPath}/${file.name}`.replace(/\/+/g, "/");
596
+ if (file.type === "d") {
597
+ if (includeDirectories) results.push(fullPath);
598
+ const subDirFiles = await listDirectoryFilesRecursive(fullPath);
599
+ results.push(...subDirFiles);
600
+ } else results.push(fullPath);
601
+ }
602
+ return results.sort();
603
+ };
604
+ const listDirectoryFiles = async (currentPath) => {
605
+ return (await sftp.list(currentPath, (file) => pattern ? (0, minimatch.minimatch)(file.name, pattern) : true)).filter(({ type }) => includeDirectories ? true : type !== "d").map(({ name }) => name).sort();
606
+ };
607
+ return { data: includeSubdirectories ? await listDirectoryFilesRecursive(path) : await listDirectoryFiles(path) };
608
+ } finally {
609
+ await sftp.end();
610
+ }
611
+ },
612
+ inputs: {
613
+ connection,
614
+ path,
615
+ pattern,
616
+ includeSubdirectories,
617
+ includeDirectories
618
+ },
619
+ examplePayload: listDirectoryExamplePayload
620
+ })
621
+ };
622
+ //#endregion
623
+ //#region src/actions/index.ts
624
+ var actions_default = {
625
+ ...files_default,
626
+ ...directories_default
627
+ };
628
+ //#endregion
629
+ //#region src/util/index.ts
630
+ const listFilesRecursive = async (sftp, currentPath, filePattern, recursive, fileMap) => {
631
+ const fileList = await sftp.list(currentPath);
632
+ for (const file of fileList) {
633
+ const fullPath = `${currentPath}/${file.name}`.replace(/\/+/g, "/");
634
+ if (file.type === "d") {
635
+ if (recursive) await listFilesRecursive(sftp, fullPath, filePattern, recursive, fileMap);
636
+ } else if (!filePattern || (0, minimatch.minimatch)(file.name, filePattern)) fileMap[fullPath] = {
637
+ size: file.size,
638
+ modifiedAt: file.modifyTime
639
+ };
640
+ }
641
+ };
642
+ const computeFileChanges = (currentFileMap, previousFileMap) => {
643
+ const newFiles = [];
644
+ const modifiedFiles = [];
645
+ for (const [filePath, metadata] of Object.entries(currentFileMap)) {
646
+ const previous = previousFileMap[filePath];
647
+ if (!previous) newFiles.push({
648
+ path: filePath,
649
+ size: metadata.size,
650
+ modifiedAt: metadata.modifiedAt
651
+ });
652
+ else if (previous.size !== metadata.size || previous.modifiedAt !== metadata.modifiedAt) modifiedFiles.push({
653
+ path: filePath,
654
+ size: metadata.size,
655
+ modifiedAt: metadata.modifiedAt
656
+ });
657
+ }
658
+ return {
659
+ newFiles,
660
+ modifiedFiles
661
+ };
662
+ };
663
+ const resolveFileChanges = (data) => {
664
+ const changesObject = data ?? {
665
+ newFiles: [],
666
+ modifiedFiles: []
667
+ };
668
+ return [...(changesObject.newFiles ?? []).map((record) => ({
669
+ changeType: "new",
670
+ record
671
+ })), ...(changesObject.modifiedFiles ?? []).map((record) => ({
672
+ changeType: "modified",
673
+ record
674
+ }))];
675
+ };
676
+ //#endregion
677
+ //#region src/triggers/index.ts
678
+ var triggers_default = { newOrModifiedFiles: (0, _prismatic_io_spectral.pollingTrigger)({
679
+ display: {
680
+ label: "New or Modified Files",
681
+ description: "Checks for new and modified files in a directory on an SFTP server on a configured schedule."
682
+ },
683
+ inputs: {
684
+ connection,
685
+ path,
686
+ pattern,
687
+ includeSubdirectories
688
+ },
689
+ triggerResolverSupport: "valid",
690
+ batchConfig: { batchSize: 50 },
691
+ triggerResolver: { resolveItems: (_context, { payload }) => resolveFileChanges(payload.body.data) },
692
+ perform: async (context, payload, { connection, path, pattern, includeSubdirectories }) => {
693
+ const sftp = await getSftpClient(connection, context.debug.enabled);
694
+ try {
695
+ const currentFileMap = {};
696
+ await listFilesRecursive(sftp, path, pattern, includeSubdirectories, currentFileMap);
697
+ const { newFiles, modifiedFiles } = computeFileChanges(currentFileMap, context.polling.getState()?.fileMap ?? {});
698
+ context.polling.setState({ fileMap: currentFileMap });
699
+ return {
700
+ payload: {
701
+ ...payload,
702
+ body: { data: {
703
+ newFiles,
704
+ modifiedFiles
705
+ } }
706
+ },
707
+ polledNoChanges: newFiles.length === 0 && modifiedFiles.length === 0
708
+ };
709
+ } finally {
710
+ await sftp.end();
711
+ }
712
+ }
713
+ }) };
714
+ //#endregion
715
+ //#region src/index.ts
716
+ var src_default = (0, _prismatic_io_spectral.component)({
717
+ key: "sftp",
718
+ documentationUrl: "https://prismatic.io/docs/components/sftp/",
719
+ public: true,
720
+ display: {
721
+ label: "SFTP",
722
+ description: "Read, write, move and delete files on an SFTP server",
723
+ iconPath: "icon.png",
724
+ category: "Data Platforms"
725
+ },
726
+ actions: actions_default,
727
+ triggers: triggers_default,
728
+ connections: connections_default
729
+ });
730
+ //#endregion
731
+ //#region npm-dist/.entry.ts
732
+ var _entry_default = (0, _prismatic_io_spectral.callableComponent)(src_default);
733
+ //#endregion
734
+ module.exports = _entry_default;