Modules@robinpath/cookie
cookie

@robinpath/cookie

0.1.1Node.jsPublic

HTTP cookie parsing, serialization, signing/verification, Set-Cookie handling, and cookie jar management

Cookie

HTTP cookie parsing, serialization, signing/verification, Set-Cookie handling, and cookie jar management

Package: @robinpath/cookie | Category: Web | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the cookie module when you need to:

  • Parse Cookie header string -- Use cookie.parse to perform this operation
  • Serialize Set-Cookie header -- Use cookie.serialize to perform this operation
  • Sign cookie value with HMAC -- Use cookie.sign to perform this operation
  • Verify and unsign cookie -- Use cookie.unsign to perform this operation
  • Get single cookie from header -- Use cookie.get to perform this operation

Quick Reference

FunctionDescriptionReturns
parseParse Cookie header stringName-value pairs
serializeSerialize Set-Cookie headerSet-Cookie header value
signSign cookie value with HMACSigned value
unsignVerify and unsign cookieOriginal value or null
getGet single cookie from headerValue or null
removeGenerate removal Set-CookieSet-Cookie header
parseSetCookieParse Set-Cookie headerCookie object with attributes
isExpiredCheck if cookie is expiredtrue if expired
jarCreate cookie jarCookie jar object
toHeaderBuild Cookie header from pairsCookie header string
encodeURL-encode cookie valueEncoded value
decodeURL-decode cookie valueDecoded value

Functions

parse

Parse Cookie header string

Module: cookie | Returns: object -- Name-value pairs

cookie.parse "session=abc; theme=dark"
ParameterTypeRequiredDescription
headerstringYesCookie header value

serialize

Serialize Set-Cookie header

Module: cookie | Returns: string -- Set-Cookie header value

cookie.serialize "session" "abc123" {"httpOnly": true, "maxAge": 3600}
ParameterTypeRequiredDescription
namestringYesCookie name
valuestringYesCookie value
optionsobjectNo{domain, path, expires, maxAge, secure, httpOnly, sameSite}

sign

Sign cookie value with HMAC

Module: cookie | Returns: string -- Signed value

cookie.sign "userId=123" "my-secret"
ParameterTypeRequiredDescription
valuestringYesValue to sign
secretstringYesSigning secret

unsign

Verify and unsign cookie

Module: cookie | Returns: string -- Original value or null

cookie.unsign $signed "my-secret"
ParameterTypeRequiredDescription
signedstringYesSigned value
secretstringYesSigning secret

get

Get single cookie from header

Module: cookie | Returns: string -- Value or null

cookie.get $header "session"
ParameterTypeRequiredDescription
headerstringYesCookie header
namestringYesCookie name

remove

Generate removal Set-Cookie

Module: cookie | Returns: string -- Set-Cookie header

cookie.remove "session"
ParameterTypeRequiredDescription
namestringYesCookie name
optionsobjectNo{domain, path}

parseSetCookie

Parse Set-Cookie header

Module: cookie | Returns: object -- Cookie object with attributes

cookie.parseSetCookie "session=abc; HttpOnly; Max-Age=3600"
ParameterTypeRequiredDescription
headerstringYesSet-Cookie header

isExpired

Check if cookie is expired

Module: cookie | Returns: boolean -- true if expired

cookie.isExpired $cookie
ParameterTypeRequiredDescription
cookieobjectYesCookie object

jar

Create cookie jar

Module: cookie | Returns: object -- Cookie jar object

cookie.jar
ParameterTypeRequiredDescription
(none)NoCall with no arguments

toHeader

Build Cookie header from pairs

Module: cookie | Returns: string -- Cookie header string

cookie.toHeader {"session": "abc", "theme": "dark"}
ParameterTypeRequiredDescription
cookiesobjectYesName-value pairs

encode

URL-encode cookie value

Module: cookie | Returns: string -- Encoded value

cookie.encode "hello world"
ParameterTypeRequiredDescription
valuestringYesValue

decode

URL-decode cookie value

Module: cookie | Returns: string -- Decoded value

cookie.decode "hello%20world"
ParameterTypeRequiredDescription
valuestringYesEncoded value

Error Handling

All functions throw on failure. Common errors:

ErrorCause
(standard errors)Check function parameters and authentication
@desc "Parse and validate result"
do
  set $result as cookie.parse "session=abc; theme=dark"
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. List and iterate

Retrieve all items and loop through them.

@desc "Get and iterate results"
do
  set $result as cookie.get $header "session"
  each $item in $result
    print $item
  end
enddo

2. Multi-step Cookie workflow

Chain multiple cookie operations together.

@desc "Parse, serialize, and more"
do
  set $r_parse as cookie.parse "session=abc; theme=dark"
  set $r_serialize as cookie.serialize "session" "abc123" {"httpOnly": true, "maxAge": 3600}
  set $r_sign as cookie.sign "userId=123" "my-secret"
  print "All operations complete"
enddo

3. Safe parse with validation

Check results before proceeding.

@desc "Parse and validate result"
do
  set $result as cookie.parse "session=abc; theme=dark"
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.1latest1 months ago
Install
$ robinpath add @robinpath/cookie

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.1
LicenseMIT
Unpacked Size5.1 KB
Versions1
Weekly Downloads26
Total Downloads26
Stars0
Last Publish1 months ago
Created1 months ago

Keywords

Category

web