All files / json-pack/src/nfs/v4/server/__tests__ setup.ts

100% Statements 30/30
100% Branches 0/0
100% Functions 9/9
100% Lines 29/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 7125x 25x 25x 25x 25x             279x 279x 279x     758x     279x     757x     279x 758x   279x 757x   279x     25x 279x     279x             279x 279x 279x 279x         279x 279x 279x 279x 279x   279x                      
import {Duplex, PassThrough} from 'stream';
import {memfs} from 'memfs';
import {Nfsv4Connection} from '../Nfsv4Connection';
import {Nfsv4TcpClient} from '../../client/Nfsv4TcpClient';
import {Nfsv4OperationsNode} from '../operations/node/Nfsv4OperationsNode';
 
/**
 * Creates a pair of connected Duplex streams (client and server).
 * Data written to client flows to server and vice versa.
 */
function makeDuplexPair(): {client: Duplex; server: Duplex} {
  const clientToServer = new PassThrough();
  const serverToClient = new PassThrough();
  const client = new Duplex({
    read() {},
    write(chunk, _enc, cb) {
      clientToServer.write(chunk, cb);
    },
  });
  const server = new Duplex({
    read() {},
    write(chunk, _enc, cb) {
      serverToClient.write(chunk, cb);
    },
  });
  clientToServer.on('data', (chunk) => {
    server.push(chunk);
  });
  serverToClient.on('data', (chunk) => {
    client.push(chunk);
  });
  return {client, server};
}
 
export const setupNfsClientServerTestbed = async () => {
  const {vol, fs} = memfs();
 
  // Populate the filesystem
  vol.fromJSON({
    '/export': null,
    '/export/file.txt': 'Hello, NFS v4!\n',
    '/export/subdir': null,
    '/export/subdir/nested.dat': 'nested data',
  });
 
  const {client: clientDuplex, server: serverDuplex} = makeDuplexPair();
  const client = Nfsv4TcpClient.fromDuplex(clientDuplex, {debug: false});
  const ops = new Nfsv4OperationsNode({fs: fs as any, dir: '/export'});
  const connection = new Nfsv4Connection({
    duplex: serverDuplex,
    ops,
    debug: false,
  });
  const stop = async () => {
    connection.close();
    client.close();
    clientDuplex.destroy();
    serverDuplex.destroy();
  };
  return {
    vol,
    fs,
    client,
    ops,
    connection,
    clientDuplex,
    serverDuplex,
    stop,
  };
};