@data-fair/catalog-sftp 0.1.1 → 0.3.0

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/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { CatalogPlugin } from '@data-fair/lib-common-types/catalog/index.js'
1
+ import type { CatalogPlugin } from '@data-fair/types-catalogs'
2
2
  import { type SFTPConfig, configSchema, assertConfigValid } from '#types'
3
3
  import capabilities from './lib/capabilities.ts'
4
4
 
@@ -39,15 +39,10 @@ const plugin: CatalogPlugin<SFTPConfig, typeof capabilities> = {
39
39
  },
40
40
 
41
41
  async getResource (context) {
42
- const { getResource } = await import('./lib/imports.ts')
42
+ const { getResource } = await import('./lib/download.ts')
43
43
  return getResource(context)
44
44
  },
45
45
 
46
- async downloadResource (context) {
47
- const { downloadResource } = await import('./lib/download.ts')
48
- return downloadResource(context)
49
- },
50
-
51
46
  metadata: {
52
47
  title: 'Catalog SFTP',
53
48
  description: 'SFTP plugin for Data Fair Catalog',
package/lib/download.ts CHANGED
@@ -1,7 +1,31 @@
1
- import type { DownloadResourceContext } from '@data-fair/lib-common-types/catalog/index.js'
2
1
  import type { SFTPConfig } from '#types'
2
+ import type { CatalogPlugin, GetResourceContext, Resource } from '@data-fair/types-catalogs'
3
3
  import { type Config, NodeSSH } from 'node-ssh'
4
4
 
5
+ /**
6
+ * Download localy a specific resource from a SFTP server, and retrieves the metadata with the filepath of the downloaded file.
7
+ *
8
+ * @param catalogConfig - The SFTP configuration object.
9
+ * @param resourceId - The identifier (path) of the resource.
10
+ * @returns A `Resource` object representing the file.
11
+ */
12
+ export const getResource = async (context: GetResourceContext<SFTPConfig>): ReturnType<CatalogPlugin['getResource']> => {
13
+ const resource = await getMetaData(context)
14
+ resource.filePath = await downloadResource(context)
15
+ return resource
16
+ }
17
+
18
+ export const getMetaData = async ({ catalogConfig, resourceId }: GetResourceContext<SFTPConfig>): Promise<Resource> => {
19
+ const pointPos = resourceId.lastIndexOf('.')
20
+ return {
21
+ id: resourceId,
22
+ title: resourceId.substring(resourceId.lastIndexOf('/') + 1),
23
+ format: (pointPos === -1) ? '' : (resourceId.substring(pointPos + 1)),
24
+ origin: catalogConfig.url + ':' + catalogConfig.port,
25
+ filePath: ''
26
+ }
27
+ }
28
+
5
29
  /**
6
30
  * Downloads a resource (file) from the SFTP server to a temporary directory.
7
31
  *
@@ -9,7 +33,7 @@ import { type Config, NodeSSH } from 'node-ssh'
9
33
  * @returns The local path to the downloaded file, or `undefined` if the download fails.
10
34
  * @throws Will throw an error if the connection configuration is invalid or not supported.
11
35
  */
12
- export const downloadResource = async ({ catalogConfig, resourceId, secrets, tmpDir }: DownloadResourceContext<SFTPConfig>) => {
36
+ const downloadResource = async ({ catalogConfig, resourceId, secrets, tmpDir }:GetResourceContext<SFTPConfig>) => {
13
37
  const ssh = new NodeSSH()
14
38
 
15
39
  const paramsConnection: Config = {
@@ -31,12 +55,12 @@ export const downloadResource = async ({ catalogConfig, resourceId, secrets, tmp
31
55
  throw new Error('Configuration invalide')
32
56
  }
33
57
 
34
- const fs = await import('node:fs/promises')
58
+ // const fs = await import('node:fs/promises')
35
59
  resourceId = resourceId.substring(resourceId.indexOf('./') + 2)
36
- const destinationPath = tmpDir + '/' + resourceId
60
+ const destinationPath = tmpDir + '/' + resourceId.substring(resourceId.lastIndexOf('/') + 1)
37
61
 
38
62
  // creation du dossier pour stocker le fichier distant
39
- await fs.mkdir(destinationPath.substring(0, destinationPath.lastIndexOf('/')), { recursive: true })
63
+ // await fs.mkdir(destinationPath.substring(0, destinationPath.lastIndexOf('/')), { recursive: true })
40
64
 
41
65
  try {
42
66
  await ssh.getFile(destinationPath, resourceId)
package/lib/imports.ts CHANGED
@@ -1,9 +1,11 @@
1
1
  import type { SFTPConfig } from '#types'
2
- import type { SFTPWrapper } from 'ssh2'
2
+ import type { FileEntryWithStats, SFTPWrapper } from 'ssh2'
3
3
  import type capabilities from './capabilities.ts'
4
- import type { ListContext, Folder, Resource, GetResourceContext } from '@data-fair/lib-common-types/catalog/index.js'
4
+ import type { ListContext, Folder, CatalogPlugin } from '@data-fair/types-catalogs'
5
5
  import { type Config, NodeSSH } from 'node-ssh'
6
6
 
7
+ type ResourceList = Awaited<ReturnType<CatalogPlugin['list']>>['results']
8
+
7
9
  /**
8
10
  * Stores the most recently used SFTP configuration.
9
11
  * This variable holds the last SFTPConfig object that was used,
@@ -26,17 +28,30 @@ let clientSFTP: SFTPWrapper
26
28
  *
27
29
  * @param list - The array of file objects returned by the SFTP `readdir` method.
28
30
  * @param path - The current directory path.
29
- * @returns An array of `Folder` or `Resource` objects representing the files and folders.
31
+ * @returns An array of `Folder` or `ResourceList` objects representing the files and folders.
30
32
  */
31
- const prepareFiles = (list: any[], path: string): (Folder | Resource)[] => {
32
- return list.map((file) => {
33
+ const prepareFiles = (list: FileEntryWithStats[], path: string): (Folder[] | ResourceList) => {
34
+ return list.map((file: FileEntryWithStats) => {
33
35
  const pointPos = file.filename.lastIndexOf('.')
34
- return {
35
- id: path + '/' + file.filename,
36
- title: file.filename,
37
- type: (file.longname.charAt(0) === 'd') ? 'folder' : 'resource',
38
- url: path + '/' + file.filename,
39
- format: (pointPos === -1) ? '' : (file.filename.substring(pointPos + 1))
36
+ if (file.longname.charAt(0) === 'd') {
37
+ // Folder
38
+ return {
39
+ id: path + '/' + file.filename,
40
+ title: file.filename,
41
+ type: 'folder'
42
+ } as Folder
43
+ } else {
44
+ // ResourceList
45
+ return {
46
+ id: path + '/' + file.filename,
47
+ title: file.filename,
48
+ description: '',
49
+ format: (pointPos === -1) ? '' : (file.filename.substring(pointPos + 1)),
50
+ mimeType: '',
51
+ origin: path + '/' + file.filename,
52
+ size: file.attrs.size,
53
+ type: 'resource'
54
+ } as ResourceList[number]
40
55
  }
41
56
  })
42
57
  }
@@ -48,7 +63,7 @@ const prepareFiles = (list: any[], path: string): (Folder | Resource)[] => {
48
63
  * @returns An object containing the count of items, the list of results (folders and resources), and the path as an array of folders.
49
64
  * @throws Will throw an error if the connection configuration is invalid or not supported.
50
65
  */
51
- export const list = async ({ catalogConfig, secrets, params }: ListContext<SFTPConfig, typeof capabilities>): Promise<{ count: number; results: (Folder | Resource)[]; path: Folder[] }> => {
66
+ export const list = async ({ catalogConfig, secrets, params }: ListContext<SFTPConfig, typeof capabilities>): ReturnType<CatalogPlugin['list']> => {
52
67
  if (!(lastConfig && ssh && clientSFTP) ||
53
68
  JSON.stringify(lastConfig) !== JSON.stringify(catalogConfig) ||
54
69
  JSON.stringify(lastSecrets) !== JSON.stringify(secrets)) {
@@ -77,7 +92,7 @@ export const list = async ({ catalogConfig, secrets, params }: ListContext<SFTPC
77
92
  clientSFTP = await ssh.requestSFTP()
78
93
  }
79
94
  const path = params.currentFolderId ?? '.'
80
- const files: any[] = await new Promise((resolve, reject) => {
95
+ const files: FileEntryWithStats[] = await new Promise((resolve, reject) => {
81
96
  if (!clientSFTP) {
82
97
  throw new Error('Configuration invalide')
83
98
  }
@@ -109,21 +124,3 @@ export const list = async ({ catalogConfig, secrets, params }: ListContext<SFTPC
109
124
  path: pathFolder
110
125
  }
111
126
  }
112
-
113
- /**
114
- * Retrieves metadata for a specific resource (file) on the SFTP server.
115
- *
116
- * @param catalogConfig - The SFTP configuration object.
117
- * @param resourceId - The identifier (path) of the resource.
118
- * @returns A `Resource` object representing the file.
119
- */
120
- export const getResource = async ({ catalogConfig, secrets, resourceId }: GetResourceContext<SFTPConfig>): Promise<Resource> => {
121
- const pointPos = resourceId.lastIndexOf('.')
122
- return {
123
- id: resourceId,
124
- title: resourceId.substring(resourceId.lastIndexOf('/') + 1),
125
- type: 'resource',
126
- format: (pointPos === -1) ? '' : (resourceId.substring(pointPos + 1)),
127
- url: resourceId
128
- }
129
- }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@data-fair/catalog-sftp",
3
3
  "description": "SFTP plugin for the Data Fair catalogs service.",
4
- "version": "0.1.1",
4
+ "version": "0.3.0",
5
5
  "main": "index.ts",
6
6
  "type": "module",
7
7
  "scripts": {
@@ -39,7 +39,7 @@
39
39
  "devDependencies": {
40
40
  "@commitlint/cli": "^19.8.0",
41
41
  "@commitlint/config-conventional": "^19.8.0",
42
- "@data-fair/lib-common-types": "^1.11.0",
42
+ "@data-fair/types-catalogs": "^0.1.0",
43
43
  "@data-fair/lib-types-builder": "^1.8.0",
44
44
  "@types/debug": "^4.1.12",
45
45
  "@types/fs-extra": "^11.0.4",
@@ -49,8 +49,5 @@
49
49
  "husky": "^9.1.7",
50
50
  "neostandard": "^0.12.1",
51
51
  "typescript": "^5.8.3"
52
- },
53
- "relativeDependencies": {
54
- "@data-fair/lib-common-types": "../../lib/packages/common-types"
55
52
  }
56
53
  }
@@ -20,7 +20,7 @@ export function returnValid(data, options) {
20
20
  }
21
21
 
22
22
  export const schema = {
23
- "$id": "https://github.com/data-fair/catalog-udata/catalog-config",
23
+ "$id": "https://github.com/data-fair/catalog-sftp/catalog-config",
24
24
  "x-exports": [
25
25
  "types",
26
26
  "validate",
@@ -4,10 +4,10 @@
4
4
  "use strict";
5
5
  export const validate = validate14;
6
6
  export default validate14;
7
- const schema16 = {"$id":"https://github.com/data-fair/catalog-udata/catalog-config","x-exports":["types","validate","schema"],"title":"SFTPConfig","type":"object","additionalProperties":false,"required":["url","port","connectionKey","login"],"properties":{"url":{"type":"string","title":"Adresse de l'hôte","description":"The host adress","x-i18n-description":{"fr":"L'adresse de l'hôte du catalogue (veuillez vous référez à la documentation technique du catalogue)"},"examples":["exemple-d-hote.fr"]},"port":{"type":"integer","title":"port","description":"The port of the catalog","x-i18n-description":{"fr":"Le port de l'API du catalogue (veuillez vous référez à la documentation technique du catalogue)"},"default":22},"login":{"type":"string","title":"Identifiant","description":"The user login","x-i18n-description":{"fr":"L'identifiant (login) utilisé pour accéder au catalogue"}},"connectionKey":{"type":"object","title":"Mode de connexion","description":"Le mode de connexion à utiliser (Mot de passe, clé SSH)","oneOfLayout":{"label":"Chosir un mode de connexion"},"oneOf":[{"title":"Mot de Passe","required":["password"],"properties":{"password":{"type":"string","description":"Enter your account password for the remote server","x-i18n-description":{"fr":"Renseigner le mot de passe associé à votre compte sur le serveur distant"}},"key":{"const":"password"}}},{"title":"Clé SHH","required":["sshKey"],"properties":{"key":{"const":"sshKey"},"sshKey":{"type":"string","layout":"textarea","description":"Enter your ssh key","x-i18n-description":{"fr":"Renseigner le votre clé SSH privée"}}}}]}}};
7
+ const schema16 = {"$id":"https://github.com/data-fair/catalog-sftp/catalog-config","x-exports":["types","validate","schema"],"title":"SFTPConfig","type":"object","additionalProperties":false,"required":["url","port","connectionKey","login"],"properties":{"url":{"type":"string","title":"Adresse de l'hôte","description":"The host adress","x-i18n-description":{"fr":"L'adresse de l'hôte du catalogue (veuillez vous référez à la documentation technique du catalogue)"},"examples":["exemple-d-hote.fr"]},"port":{"type":"integer","title":"port","description":"The port of the catalog","x-i18n-description":{"fr":"Le port de l'API du catalogue (veuillez vous référez à la documentation technique du catalogue)"},"default":22},"login":{"type":"string","title":"Identifiant","description":"The user login","x-i18n-description":{"fr":"L'identifiant (login) utilisé pour accéder au catalogue"}},"connectionKey":{"type":"object","title":"Mode de connexion","description":"Le mode de connexion à utiliser (Mot de passe, clé SSH)","oneOfLayout":{"label":"Chosir un mode de connexion"},"oneOf":[{"title":"Mot de Passe","required":["password"],"properties":{"password":{"type":"string","description":"Enter your account password for the remote server","x-i18n-description":{"fr":"Renseigner le mot de passe associé à votre compte sur le serveur distant"}},"key":{"const":"password"}}},{"title":"Clé SHH","required":["sshKey"],"properties":{"key":{"const":"sshKey"},"sshKey":{"type":"string","layout":"textarea","description":"Enter your ssh key","x-i18n-description":{"fr":"Renseigner le votre clé SSH privée"}}}}]}}};
8
8
 
9
9
  function validate14(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){
10
- /*# sourceURL="https://github.com/data-fair/catalog-udata/catalog-config" */;
10
+ /*# sourceURL="https://github.com/data-fair/catalog-sftp/catalog-config" */;
11
11
  let vErrors = null;
12
12
  let errors = 0;
13
13
  if(data && typeof data == "object" && !Array.isArray(data)){
@@ -1,5 +1,5 @@
1
1
  {
2
- "$id": "https://github.com/data-fair/catalog-udata/catalog-config",
2
+ "$id": "https://github.com/data-fair/catalog-sftp/catalog-config",
3
3
  "x-exports": [
4
4
  "types",
5
5
  "validate",