Coder Social home page Coder Social logo

deno-gopher's Introduction

tests workflow deno doc

RFC1436 Gopher Client for Deno, with basic support for Gopher+.

Example Usage

// Import the client.
import {GopherClient} from 'https://deno.land/x/gopher/mod.ts';

// Create a new GopherClient, optionally specifying the protocol version to use.
const client = new GopherClient({
  protocolVersion: GopherProtocol.RFC1436,
});

try {
  // Download the Gopher server's menu.
  const menuBytes = await client.downloadItem({
    hostname: 'gopher.example.com',
    tls: TlsSupport.PreferTls,
    // Optional: port (default 70)
    // Optional: selector (e.g. '/foo')
  });

  // To display the menu items.
  const menu = client.handler.parseMenu(menuBytes);
  for (const menuItem of menu.Items) {
    console.log(menuItem.toString());
  }

  // To download a single menu item - the response contains the body and header
  // (if it was  Gopher+ response) as well as some timing information:
  const gopherResponse = client.downloadItem(menuItem);

  /** Output timing info for the server response, e.g.
   * ```
   *  Timing info for Gopher Request:
   *  Waiting for connection: 94ms
   *  Waiting for first byte: 92ms
   *  Receiving time:         1ms
   *  Total request duration: 187ms
   * ```
   */
  console.log(`Timing info for Gopher Request:
    Waiting for connection: ${gopherResponse.timing.waitingDurationMillis}ms
    Waiting for first byte: ${gopherResponse.timing.waitingForFirstByteDurationMillis}ms
    Receiving time:         ${gopherResponse.timing.recievingDuratrionMillis}ms
    Total request duration: ${gopherResponse.timing.totalDurationMillis}ms`);

  // If it was a text entry (use the MenuItem.Type field to check) then you can
  // easily convert to a string.
  const text = new TextDecoder().decode(gopherResponse.body);
} catch (error) {
  console.error(`Error downloading from Gopher: ${error}`);
}

Getting Gopher+ attributes

// Having previously obtained the menu item (see basic example), attributes can
// be retrieved like so:
client.populateAttributes(myMenuItem)
console.log(myMenuItem);

Results:

MenuItem {
  Type: '0',
  Name: 'A file',
  Selector: '/a file.txt',
  Hostname: 'gopher.example.com',
  Port: 70,
  Original: '0A file\t/a file.txt\tgopher.example.com\t70\t+',
  Attributes: Map {
    'ADMIN' => Map {
        'Admin' => 'Foo Bar <[email protected]>',
        'Mod-Date' => 'Sun Feb 21 20:19:18 2021 <20210221201918>'
      },
    'VIEWS' => Map { 'text/plain' => '<1k>' }
  }
}

deno-gopher's People

Contributors

matt1 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

deno-gopher's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.