Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

OpenAPI based binary serialization

License

NotificationsYou must be signed in to change notification settings

compactr/compactr.js

Repository files navigation

Compactr

Compactr

OpenAPI serialization



  • OpenAPI 3.x Compatible Use your existing OpenAPI schemas, no extra definitions required
  • Cuts bandwidth in half Average API responses are multiple times smaller than JSON
  • Built for actual APIs Specialty field types to optimize common items, ex: UUID, date-time, ivp4
  • Zero dependencies and can be bundled down to ~5kb!

Install

npm install compactr

Usage

Basic Example

import{schema}from'compactr';constuserSchema=schema({type:'object',properties:{id:{type:'string',format:'uuid'},name:{type:'string'},age:{type:'integer',format:'int32'},balance:{type:'number',format:'double'},created:{type:'string',format:'date-time'},tags:{type:'array',items:{type:'string'}}}});constdata={id:'550e8400-e29b-4d4e-a7d4-426614174000',name:'Alice',age:30,balance:1234.56,created:'2025-10-28T14:30:00.000Z',tags:['premium','verified']};constbuffer=userSchema.write(data);constdecoded=userSchema.read(buffer);

Compactr also supports component references ($ref).

Only local references are allowed

Component References

constschema=schema({user:{$ref:'#/User'},product:{$ref:'#/Product'}},{schemas:{User:{type:'object',properties:{id:{type:'integer',format:'int32'},name:{type:'string'}}},Product:{type:'object',properties:{sku:{type:'string'},price:{type:'number',format:'double'}}}}});

Supported Types

Compactr supports the following OpenAPI types and formats:

TypeFormatBytesDescription
boolean-1Boolean value
integerint32432-bit integer
integerint64864-bit integer
numberfloat432-bit floating point
numberdouble864-bit floating point
string-2/charUTF-8 string
stringuuid16UUID (compressed)
stringipv44IPv4 address
stringipv616IPv6 address
stringdate4Date (YYYY-MM-DD)
stringdate-time8ISO 8601 date-time
stringbinaryvariableBase64 binary data
array-variableArray of items
object-variableNested object

Performance

I realistic scenarios, compactr performs a bit slower than JSON.stringify/ JSON.parse as well asprotobuf, but can yield a byte reduction of 3.5x compared to JSON.

[JSON-API Reponse] JSON x 379 ops/sec ±0.66% (92 runs sampled)[JSON-API Reponse] Compactr x 167 ops/sec ±0.75% (85 runs sampled)[JSON-API Reponse] Protobuf x 358 ops/sec ±1.36% (91 runs sampled)[JSON-API Reponse] MsgPack x 161 ops/sec ±2.06% (79 runs sampled)Buffer size (bytes): { json: 277, compactr: 80, protobuf: 129, msgpack: 227 }

Testing

Run the test suite:

npmtest

Run benchmarks:

npm run bench

Build the project:

npm run build

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

Apache 2.0 (c) 2025 Frederic Charette

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp