@orpc/server
Version:
<div align="center"> <image align="center" src="https://orpc.unnoq.com/logo.webp" width=280 alt="oRPC logo" /> </div>
150 lines (144 loc) • 5.13 kB
text/typescript
import { SerializeOptions, ParseOptions } from 'cookie';
/**
* Encodes a Uint8Array to base64url format
* Base64url is URL-safe and doesn't use padding
*
* @example
* ```ts
* const text = "Hello World"
* const encoded = encodeBase64url(new TextEncoder().encode(text))
* const decoded = decodeBase64url(encoded)
* expect(new TextDecoder().decode(decoded)).toEqual(text)
* ```
*/
declare function encodeBase64url(data: Uint8Array): string;
/**
* Decodes a base64url string to Uint8Array
* Returns undefined if the input is invalid
*
* @example
* ```ts
* const text = "Hello World"
* const encoded = encodeBase64url(new TextEncoder().encode(text))
* const decoded = decodeBase64url(encoded)
* expect(new TextDecoder().decode(decoded)).toEqual(text)
* ```
*/
declare function decodeBase64url(base64url: string | undefined | null): Uint8Array | undefined;
interface SetCookieOptions extends SerializeOptions {
/**
* Specifies the value for the [`Path` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.4).
*
* @default '/'
*/
path?: string;
}
/**
* Sets a cookie in the response headers,
*
* Does nothing if `headers` is `undefined`.
*
* @example
* ```ts
* const headers = new Headers()
*
* setCookie(headers, 'sessionId', 'abc123', { httpOnly: true, maxAge: 3600 })
*
* expect(headers.get('Set-Cookie')).toBe('sessionId=abc123; HttpOnly; Max-Age=3600')
* ```
*
*/
declare function setCookie(headers: Headers | undefined, name: string, value: string, options?: SetCookieOptions): void;
interface GetCookieOptions extends ParseOptions {
}
/**
* Gets a cookie value from request headers
*
* Returns `undefined` if the cookie is not found or headers are `undefined`.
*
* @example
* ```ts
* const headers = new Headers({ 'Cookie': 'sessionId=abc123; theme=dark' })
*
* const sessionId = getCookie(headers, 'sessionId')
*
* expect(sessionId).toEqual('abc123')
* ```
*/
declare function getCookie(headers: Headers | undefined, name: string, options?: GetCookieOptions): string | undefined;
/**
* Deletes a cookie by marking it expired.
*/
declare function deleteCookie(headers: Headers | undefined, name: string, options?: Omit<SetCookieOptions, 'maxAge'>): void;
/**
* Encrypts a string using AES-GCM with a secret key.
* The output is base64url encoded to be URL-safe.
*
* @example
* ```ts
* const encrypted = await encrypt("Hello, World!", "test-secret-key")
* const decrypted = await decrypt(encrypted, "test-secret-key")
* expect(decrypted).toBe("Hello, World!")
* ```
*/
declare function encrypt(value: string, secret: string): Promise<string>;
/**
* Decrypts a base64url encoded string using AES-GCM with a secret key.
* Returns the original string if decryption is successful, or undefined if it fails.
*
* @example
* ```ts
* const encrypted = await encrypt("Hello, World!", "test-secret-key")
* const decrypted = await decrypt(encrypted, "test-secret-key")
* expect(decrypted).toBe("Hello, World!")
* ```
*/
declare function decrypt(encrypted: string | undefined | null, secret: string): Promise<string | undefined>;
/**
* Signs a string value using HMAC-SHA256 with a secret key.
*
* This function creates a cryptographic signature that can be used to verify
* the integrity and authenticity of the data. The signature is appended to
* the original value, separated by a dot, using base64url encoding (no padding).
*
*
* @example
* ```ts
* const signedValue = await sign("user123", "my-secret-key")
* expect(signedValue).toEqual("user123.oneQsU0r5dvwQFHFEjjV1uOI_IR3gZfkYHij3TRauVA")
* ```
*/
declare function sign(value: string, secret: string): Promise<string>;
/**
* Verifies and extracts the original value from a signed string.
*
* This function validates the signature of a previously signed value using the same
* secret key. If the signature is valid, it returns the original value. If the
* signature is invalid or the format is incorrect, it returns undefined.
*
*
* @example
* ```ts
* const signedValue = "user123.oneQsU0r5dvwQFHFEjjV1uOI_IR3gZfkYHij3TRauVA"
* const originalValue = await unsign(signedValue, "my-secret-key")
* expect(originalValue).toEqual("user123")
* ```
*/
declare function unsign(signedValue: string | undefined | null, secret: string): Promise<string | undefined>;
/**
* Extracts the value part from a signed string without verification.
*
* This function simply extracts the original value from a signed string
* without performing any signature verification. It's useful when you need
* to access the value quickly without the overhead of cryptographic verification.
*
* @example
* ```ts
* const signedValue = "user123.oneQsU0r5dvwQFHFEjjV1uOI_IR3gZfkYHij3TRauVA"
* const value = getSignedValue(signedValue)
* expect(value).toEqual("user123")
* ```
*/
declare function getSignedValue(signedValue: string | undefined | null): string | undefined;
export { decodeBase64url, decrypt, deleteCookie, encodeBase64url, encrypt, getCookie, getSignedValue, setCookie, sign, unsign };
export type { GetCookieOptions, SetCookieOptions };