UNPKG

@alinex/datastore

Version:

Read, work and write data structures from and to differents locations and formats.

93 lines 3.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.modified = exports.save = exports.load = void 0; const debug_1 = require("debug"); const ftp = require("basic-ftp"); const streamBuffers = require("stream-buffers"); const debug = debug_1.default('datastore:protocol:ftp'); const debugDetails = debug_1.default('datastore:details'); const load = (async function (parsedUri, options) { if (!parsedUri.pathname) return Promise.reject(`No pathname given in ${parsedUri.href}`); debug(`loading ${parsedUri.protocol}//${parsedUri.username}@${parsedUri.host}${parsedUri.pathname}`); const meta = { path: parsedUri.pathname }; const writer = new streamBuffers.WritableStreamBuffer(); const client = new ftp.Client(); if (debugDetails.enabled) { client.ftp.verbose = true; client.ftp.log = debugDetails; } try { await client.access({ host: parsedUri.host, port: parsedUri.port ? Number(parsedUri.port) : undefined, user: parsedUri.username, password: parsedUri.password, secure: parsedUri.protocol == 'ftps' }); meta.stat = { modifyTime: await client.lastMod(parsedUri.pathname) }; await client.download(writer, parsedUri.pathname); client.close(); } catch (err) { client.close(); throw err; } const result = writer.getContents(); if (!result) throw new Error('Got no content from ftp server.'); return [result, meta]; }); exports.load = load; const save = (async function (parsedUri, buffer, options) { if (!parsedUri.pathname) return Promise.reject(`No pathname given in ${parsedUri.href}`); debug(`storing to ${parsedUri.protocol}//${parsedUri.username}@${parsedUri.host}${parsedUri.pathname}`); const reader = new streamBuffers.ReadableStreamBuffer(); reader.put(buffer); reader.stop(); const client = new ftp.Client(); if (debugDetails.enabled) { client.ftp.verbose = true; client.ftp.log = debugDetails; } try { await client.access({ host: parsedUri.host, port: parsedUri.port ? Number(parsedUri.port) : undefined, user: parsedUri.username, password: parsedUri.password, secure: parsedUri.protocol == 'ftps' }); await client.upload(reader, parsedUri.pathname); client.close(); } catch (err) { client.close(); throw err; } }); exports.save = save; const modified = async function (parsedUri, options) { if (!parsedUri.pathname) return Promise.reject(`No pathname given in ${parsedUri.href}`); debug(`last modification check ${parsedUri.protocol}//${parsedUri.username}@${parsedUri.host}${parsedUri.pathname}`); let date = new Date(); const client = new ftp.Client(); try { await client.access({ host: parsedUri.host, port: parsedUri.port ? Number(parsedUri.port) : undefined, user: parsedUri.username, password: parsedUri.password, secure: parsedUri.protocol == 'ftps' }); date = await client.lastMod(parsedUri.pathname); } catch (err) { } client.close(); return date; }; exports.modified = modified; //# sourceMappingURL=ftp.js.map