Skip to content

Commit

Permalink
add options support to sftp client
Browse files Browse the repository at this point in the history
  • Loading branch information
williamstein committed Aug 26, 2023
1 parent 2273172 commit 96a98bc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
14 changes: 8 additions & 6 deletions websocketfs/lib/mount.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { callback } from "awaiting";
import SftpFuse from "./sftp-fuse";
import SftpFuse, { IClientOptions } from "./sftp-fuse";
import Fuse from "@cocalc/fuse-native";
import debug from "debug";

Expand All @@ -9,25 +9,27 @@ interface Options {
path: string; // e.g., ./mnt
remote: string; // e.g., websocket server -- ws://localhost:4389
// NOTE: we change some options from the defaults, but you can set anything
// explicitly via mountOpts, overriding our non-default options.
mountOpts?: Fuse.OPTIONS;
// explicitly via mountOptions, overriding our non-default options.
mountOptions?: Fuse.OPTIONS;
connectOptions?: IClientOptions;
apiKey?: string; // used for
}

export default async function mount(
opts: Options,
): Promise<{ fuse: Fuse; client: SftpFuse; unmount: () => Promise<void> }> {
log("mount", opts);
const { path, remote, mountOpts } = opts;
const { path, remote, connectOptions, mountOptions } = opts;

const client = new SftpFuse(remote);
await client.connect();
await client.connect(connectOptions);
const fuse = new Fuse(path, client, {
debug: log.enabled,
force: true,
mkdir: true,
fsname: remote,
autoUnmount: true,
...mountOpts,
...mountOptions,
});
await callback(fuse.mount.bind(fuse));
const unmount = async () => {
Expand Down
8 changes: 5 additions & 3 deletions websocketfs/lib/sftp-fuse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Some relevant docs:
- https://filezilla-project.org/specs/draft-ietf-secsh-filexfer-02.txt
//
*/
import { Client as SftpClient } from "websocket-sftp/lib/sftp";
import { Client as SftpClient, IClientOptions } from "websocket-sftp/lib/sftp";
import { RenameFlags } from "websocket-sftp/lib/fs-api";
import type { SftpError } from "websocket-sftp/lib/util";
import {
Expand All @@ -19,6 +19,8 @@ import { convertOpenFlags } from "./flags";
import Fuse from "@cocalc/fuse-native";
import debug from "debug";

export type { IClientOptions };

const log = debug("websocketfs:sftp");

type Callback = Function;
Expand All @@ -37,9 +39,9 @@ export default class SftpFuse {
bindMethods(this);
}

async connect() {
async connect(options?: IClientOptions) {
log("connecting to ", this.remote);
await callback(this.sftp.connect, this.remote, {});
await callback(this.sftp.connect, this.remote, options ?? {});
}

end() {
Expand Down

0 comments on commit 96a98bc

Please sign in to comment.