js-binary (Python)
Encode/decode to js-binary's custom binary format: much more compact and faster than JSON/BSON.
This is an independent, unofficial Python port of the npm js-binary package v1.2.0 (MIT), with 100% binary-format compatibility. It is not affiliated with, sponsored by, or endorsed by the original author. The distribution name is js-binary; the import package is js_binary.
Like the original, it is analogous to JSON.stringify / JSON.parse, but encodes to a compact custom binary format. The format carries no type information, so you must define a schema to encode/decode. A nice side effect is free input validation against the schema. It is well suited to very regular data, like API input/output.
Install
pip install js-binary
Usage
from js_binary import Type
user = {
"name": {
"first": "Guilherme",
"last": "Souza",
},
"pass": bytes.fromhex("042697a30b2dafbdf91bf66bdacdcba8"),
"creationDate": 1397251352504, # 2014-04-11T21:22:32.504Z, milliseconds since epoch
"active": True,
"achievements": [3, 14, 15, 92, 65, 35],
}
schema = Type({
"name": {
"first": "string",
"last": "string",
},
"pass": "Buffer",
"creationDate": "date",
"active": "boolean",
"achievements": ["uint"],
"optionalField?": "int",
})
encoded = schema.encode(user)
decoded = schema.decode(encoded)
Available types
Basic types
uint: unsigned integer (between 0 and 2^53)int: signed integer (between -2^53 and 2^53)float: a 64-bit floating-point (the JavaScript number type)string: a utf-8 stringBuffer: a byte bufferboolean: a booleanjson: any data supported by the JSON formatoid: a mongodb ObjectId (24-char hex string on decode)regex: a regular expressiondate: a date/time value
Compound types
A compound type is an object with (optional) fields. Those fields may be arrays, but with the restriction that every element has the same data schema.
Examples:
- Nested fields:
{"a": {"b": "int", "d": {"e": "int"}}} - Optional fields:
{"a": "int", "b?": "int", "c?": {"d": "int"}} - Array fields:
{"a": ["int"]} - All together now:
{"a?": [{"b?": "int"}]}
Array type
An array type in which every element has the same data schema.
- Int array:
["int"] - Object array:
[{"v": "int", "f": "string"}]
Optional fields
Optional fields are named with a trailing ?, for example "optionalField?". The empty value is None (equivalent to JS undefined / null).
Type mapping (JS -> Python)
JS (js-binary) |
Python |
|---|---|
Buffer |
bytes |
Date |
datetime.datetime (UTC-aware on decode; encode accepts a datetime or int milliseconds) |
RegExp |
js_binary.Regex (has .source and .flags) |
oid |
24-char hex str |
| object | dict |
| array | list |
| json | native Python types |
| boolean | bool |
| uint / int | int |
| float | float |
API
Public names: js_binary.Type, js_binary.Data, js_binary.Field, js_binary.ReadState, js_binary.types, js_binary.Regex.
Type exposes the methods encode, decode, write, read, and get_hash (alias getHash), plus the class attribute Type.TYPE.
Low-level classes expose snake_case methods with camelCase aliases (for example get_hash / getHash) to ease porting from the JavaScript API.
Notes / deviations
- An unknown basic type raises
TypeError('Unknown basic type: ...'). Noneis the "empty" value for optional fields (equivalent to JSundefined/null).- Reading past the end of a buffer raises
IndexError. - Error messages replicate the original library's messages.
datedecodes to a UTC-awaredatetime.datetime. JavaScript dates span roughly ±8.64e15 ms, beyond Python'sdatetimerange; such out-of-range values decode to anintof milliseconds instead (never lost), and can be re-encoded as-is.regexround-trips losslessly asjs_binary.Regex. Decoding does not attempt to compile it with Python'sre, since JS and Python regex syntaxes differ.- Arrays whose element type consumes no bytes (e.g.
[{}]) raiseValueError('Array element type consumes no bytes')on decode; otherwise the declared length could drive unbounded allocation from a tiny payload. The original only bounds this at 2^32 elements. Type.decoderequires a bytes-like argument; any other type raisesTypeError.
License
MIT. See LICENSE. This is an unofficial Python port; the original JavaScript project is sitegui/js-binary (MIT).
Metadata
Release files for js-binary 1.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| js_binary-1.2.0.tar.gz | 11.1 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| js_binary-1.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 25.0 kB
Release files / js_binary-1.2.0.tar.gz
| Download URL | js_binary-1.2.0.tar.gz |
|---|---|
| Size | 11.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1d23fe4874931defbec86ffa8491c3fed2a19e00d3021e380e63d1009bca6d56
|
|
BLAKE2b-256 checksum How to use checksums |
7db9745b8b5b1344eae0ffaab5a14b807dfae3467cb9cf908ba460fbb194de88
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.
Transparency logRelease files / js_binary-1.2.0-py3-none-any.whl
| Download URL | js_binary-1.2.0-py3-none-any.whl |
|---|---|
| Size | 13.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b7eba455ce2139611759109d327632682a37d0e6f8c13f999f526bab908e8197
|
|
BLAKE2b-256 checksum How to use checksums |
f2e58c2f95b285df9fcbeecb1ce9cdffa661d82fb8ec4a937c8597b6756ada4e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.
Transparency logSecurity for js-binary 1.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for js_binary-1.2.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.