Skip to content

API quick start ​

Read synthetic settings through WinScpProfileSource, print a small metadata projection, select an exact profile and search by host. The example creates and removes its own temporary fixture, so it does not read your installed application's settings.

Run ​

From an installed source checkout:

sh
npm run build
node examples/test-api.mjs

For your own project, install the library, import the same class, and select your supported file/configuration explicitly. This example only uses the selected adapter; the shared libraries do not download other adapters.

Complete code ​

js
import assert from 'node:assert/strict';
import {mkdtemp, mkdir, writeFile, rm} from 'node:fs/promises';
import {tmpdir} from 'node:os';
import path from 'node:path';
import {WinScpProfileSource} from '@jalsoedesign/winscp-core';

const files = {
    "WinSCP.ini": "[Sessions\\Demo]\nHostName=127.0.0.1\nUserName=deploy\nFSProtocol=5\nPublicKeyFile=C:\\keys\\example.ppk\nRemoteDirectory=/srv/example\n"
};
const directory = await mkdtemp(path.join(tmpdir(), 'profile-api-demo-'));

try {
    for (const [name, contents] of Object.entries(files)) {
        const target = path.join(directory, name);

        await mkdir(path.dirname(target), {recursive: true});
        await writeFile(target, contents);
    }

    const source = await WinScpProfileSource.fromFile(path.join(directory, 'WinSCP.ini'));
    const profiles = source.list();

    assert.ok(profiles.length > 0);

    for (const profile of profiles) {
        const connections = profile.connections.map(connection => ({
            protocol: connection.protocol,
            host: connection.endpoint?.host ?? null,
        }));

        console.log(JSON.stringify({path: profile.path, connections}));
    }

    const selected = source.get(profiles[0].path);
    const matches = source.search('127.0.0.1', {fields: 'all'});

    assert.equal(selected.path, profiles[0].path);
    assert.ok(matches.length > 0);
    console.log('Exact selection:', selected.path);
    console.log('Host matches:', matches.length);
    console.log('Metadata only; no remote connection.');
} finally {
    await rm(directory, {recursive: true, force: true});
    console.log('Temporary files removed.');
}

Output ​

text
{"path":"Demo","connections":[{"protocol":"ftp","host":"127.0.0.1"}]}
Exact selection: Demo
Host matches: 1
Metadata only; no remote connection.
Temporary files removed.

Only the explicit console.log calls print these lines. The source returns normalized metadata and diagnostics; credential values are not resolved. Private-key paths are metadata and do not require opening the key file.

What happens next ​

Use the exact returned path for selection. A missing exact path returns undefined; search returns an array and may match more than one profile. Inspect diagnostics before choosing a connection, especially profiles with SSH-hop relationships or unsupported native settings.

The source is a snapshot; reopen it after native settings change. Raw native settings may contain stored secrets. See read/select/search, the method reference, and connecting a profile.