TypeScript - ohttp-ts
Installation
npm install ohttp-ts hpke
Client Usage
import { OHTTPClient, KeyConfig } from 'ohttp-ts';
import {
CipherSuite,
KEM_DHKEM_X25519_HKDF_SHA256,
KDF_HKDF_SHA256,
AEAD_AES_128_GCM
} from 'hpke';
// Setup cipher suite
const suite = new CipherSuite(
KEM_DHKEM_X25519_HKDF_SHA256,
KDF_HKDF_SHA256,
AEAD_AES_128_GCM
);
// Fetch gateway's public key
const keyBytes = await fetch(
'https://gateway.ohttp.info/.well-known/ohttp-gateway'
).then(r => r.arrayBuffer());
const keyConfig = KeyConfig.parse(new Uint8Array(keyBytes));
// Create client
const client = new OHTTPClient(suite, keyConfig);
// Encapsulate request
const innerRequest = new Request('https://api.example.com/data', {
method: 'POST',
body: JSON.stringify({ query: 'example' })
});
const { request, context } = await client.encapsulateRequest(
innerRequest,
'https://relay.ohttp.info/ohttp'
);
// Send through relay
const encResponse = await fetch(request);
// Decapsulate response
const response = await context.decapsulateResponse(encResponse);
const data = await response.json();
Server Usage
import { OHTTPServer, KeyConfig } from 'ohttp-ts';
// Generate or load key configuration
const keyConfig = await KeyConfig.generate(suite, keyId, [
{ kdfId: KdfId.HKDF_SHA256, aeadId: AeadId.AES_128_GCM }
]);
// Create server
const server = new OHTTPServer([keyConfig]);
// Handle incoming OHTTP request
const { request, context } = await server.decapsulateRequest(ohttpRequest);
// Process the inner request
const response = await processRequest(request);
// Encapsulate response
const encapsulatedResponse = await context.encapsulateResponse(response);
Browser Support
ohttp-ts works in modern browsers with WebCrypto support. For older browsers, you may need polyfills for:
crypto.subtleTextEncoder/TextDecoder
Platform Support
| Platform | Support |
|---|---|
| Node.js 18+ | Full |
| Deno | Full |
| Cloudflare Workers | Full |
| Browsers (Chrome 90+, Firefox 89+, Safari 15+) | Full |