npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@openfn/language-sftp

v2.0.5

Published

An SFTP language package for use with Open Function

Downloads

223

Readme

Language SFTP

Language Pack for building expressions and operations to work with SFTP servers.

Documentation

Configuration

View all the required and optional properties for state.configuration in the official configuration-schema definition.

List the content of a directory

alterState(state => {
  return list('/path/To/Directory')(state).then(response => {
    console.log(`There are ${response.data.length} files.`);
    return response;
  });
});

sample getCSV expression

getCSV('path/to/file.csv', 'utf8', {
  quote: 'off',
  delimiter: ';',
  noheader: true,
  filter: {
    type: 'startsWith',
    key: 'field1',
    value: 'JO',
  },
});

A more complex example that breaks up the CSV file into multiple payloads for quicker processing.

fn(state => {
  return list('/')(state).then(state => {
    const targetNames = [
      'exportContacts', //example fileName
    ];
    console.log(`Fetching files: ${targetNames}`);
    const files = state.data
      .filter(file => file.name.split('.')[1] === 'csv')
      .filter(file =>
        targetNames.some(targetName =>
          file.name.toLowerCase().includes(targetName)
        )
      );

    if (files.length === 0) console.log('No new CSV files found.');
    return { ...state, data: {}, files };
  });
});

each(
  '$.files[*]',
  fn(state => {
    const { configuration, data } = state;

    return getCSV(`/${data.name}`)(state).then(async state => {
      const headers = state.data
        .shift()
        .split(';')
        .map(h => (h = h.replace(/"/g, '')));

      function toObject(item) {
        const values = item.split(';');

        return Object.fromEntries(
          headers.map((k, i) => {
            return values[i]
              ? [k, values[i].replace(/"/g, '')]
              : [k, values[i]];
          })
        );
      }

      let countInbox = 0;

      //to post CSV data as individual Messages to OpenFn Inbox
      const postToInbox = async data => {
        countInbox++;

        console.log(`Sending request ${countInbox} to inbox`);

        await new Promise(resolve => setTimeout(resolve, 200));

        await http.post({
          url: configuration.openfnInboxUrl,
          data: data,
          maxContentLength: Infinity,
          maxBodyLength: Infinity,
        })(state);
      };

      //To split up into multiple, smaller payloads before send to OpenFn Inbox
      const chunkSize = 500;

      console.log(
        state.data.length,
        'rows will be sent in',
        Math.ceil(state.data.length / chunkSize),
        'requests of',
        chunkSize,
        'rows each.'
      );

      while (state.data.length > 0) {
        console.log('data.length', state.data.length);
        await postToInbox({
          fileName: data.name,
          fileType: data.name.split('-')[0],
          uploadDate: new Date(data.modifyTime).toISOString(),
          json: state.data.splice(0, chunkSize).map(toObject),
        });
      }

      return { configuration, references: [], data: {} };
    });
  })
);

sample putCSV expression

This function converts JSON to CSV and post to a server

putCSV('/some/path/to_file.csv', 'utf8', { delimiter: ';', noheader: true });

Get JSON from FTP server

getJSON('path/to/file.json', 'utf8');

Custom request to an http endpoint

This adaptor exports http from language-common. Here, we outline the usage in order to make custom requests to an endpoint. It returns a promise

alterState(state => {
  return http
    .post({ url: 'yourURL', data: { name: 'Mamadou' } })(state)
    .then(response => {
      // do something with response;
      return response;
    });
});

Development

Clone the repo, run pnpm install.

Run tests using pnpm run test or pnpm run test:watch

Build the project using pnpm build.

To build the docs for this repo, pnpm build docs