Neutralize Pesky Miscreants

    http-link-header
    DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/http-link-header package

    1.0.5 • Public • Published

    HTTP Link Header

    npm npm license npm downloads build status

    Parse & format HTTP link headers according to RFC 8288

    Install via npm

    $ npm install --save http-link-header

    Deviations from the RFC

    Link Target

    While RFC 8288, Section 3.1 states that relative URI-References MUST be resolved by the parsers – this library DOES NOT. This is due to the parser not having an input for the absolute or canonical URI of the related document. Currently there are no plans to add this, and it is left to the user whether or not to resolve relative URIs.

    Usage

    var LinkHeader = require( 'http-link-header' )

    Parsing a HTTP link header

    var link = LinkHeader.parse(
      '<example.com>; rel="example"; title="Example Website", ' +
      '<example-01.com>; rel="alternate"; title="Alternate Example Domain"'
    )
    
    > Link {
      refs: [
        { uri: 'example.com', rel: 'example', title: 'Example Website' },
        { uri: 'example-01.com', rel: 'alternate', title: 'Alternate Example Domain' },
      ]
    }

    Checking whether it has a reference with a given attribute & value

    link.has( 'rel', 'alternate' )
    > true

    Retrieving a reference with a given attribute & value

    link.get( 'rel', 'alternate' )
    > [
      { uri: 'example-01.com', rel: 'alternate', title: 'Alternate Example Domain' }
    ]
    // Shorthand for `rel` attributes
    link.rel( 'alternate' )
    > [
      { uri: 'example-01.com', rel: 'alternate', title: 'Alternate Example Domain' }
    ]

    Setting references

    link.set({ rel: 'next', uri: 'http://example.com/next' })
    > Link {
      refs: [
        { uri: 'example.com', rel: 'example', title: 'Example Website' },
        { uri: 'example-01.com', rel: 'alternate', title: 'Alternate Example Domain' },
        { rel: 'next', uri: 'http://example.com/next' }
      ]
    }

    Parsing multiple headers

    var link = new LinkHeader()
    
    link.parse( '<example.com>; rel="example"; title="Example Website"' )
    > Link {
      refs: [
        { uri: 'example.com', rel: 'example', title: 'Example Website' },
      ]
    }
    
    link.parse( '<example-01.com>; rel="alternate"; title="Alternate Example Domain"' )
    > Link {
      refs: [
        { uri: 'example.com', rel: 'example', title: 'Example Website' },
        { uri: 'example-01.com', rel: 'alternate', title: 'Alternate Example Domain' },
      ]
    }
    
    link.parse( '<example-02.com>; rel="alternate"; title="Second Alternate Example Domain"' )
    > Link {
      refs: [
        { uri: 'example.com', rel: 'example', title: 'Example Website' },
        { uri: 'example-01.com', rel: 'alternate', title: 'Alternate Example Domain' },
        { uri: 'example-02.com', rel: 'alternate', title: 'Second Alternate Example Domain' },
      ]
    }

    Handling extended attributes

    link.parse( '</extended-attr-example>; rel=start; title*=UTF-8\'en\'%E2%91%A0%E2%93%AB%E2%85%93%E3%8F%A8%E2%99%B3%F0%9D%84%9E%CE%BB' )
    > Link {
      refs: [
        { uri: '/extended-attr-example', rel: 'start', 'title*': { language: 'en', encoding: null, value: '①⓫⅓㏨♳𝄞λ' } }
      ]
    }

    Stringifying to HTTP header format

    link.toString()
    > '<example.com>; rel=example; title="Example Website", <example-01.com>; rel=alternate; title="Alternate Example Domain"'

    Speed

    $ npm run benchmark
    
    # http-link-header .parse() ⨉ 1000000
    ok ~1.29 s (1 s + 289696759 ns)
    
    # http-link-header #toString() ⨉ 1000000
    ok ~554 ms (0 s + 553782657 ns)
    

    Install

    npm i http-link-header

    DownloadsWeekly Downloads

    623,869

    Version

    1.0.5

    License

    MIT

    Unpacked Size

    15.4 kB

    Total Files

    5

    Last publish

    Collaborators

    • jhermsmeier