Skip to content

Commit 1161d88

Browse files
feat: support compression when importing/exporting events
1 parent e1a7bfb commit 1161d88

15 files changed

Lines changed: 1392 additions & 100 deletions

.changeset/quick-maps-marry.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"nostream": minor
3+
---
4+
5+
Add gzip and xz compression support to event import/export flows.
6+
7+
- Export supports `--compress`/`-z` with `--format gzip|gz|xz`.
8+
- Import auto-detects compressed input by extension and magic bytes and decompresses in a stream pipeline.
9+
- Includes docs updates and unit/integration test coverage for compression paths.

.knip.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
"project": [
99
"src/**/*.ts"
1010
],
11+
"ignoreDependencies": [
12+
"lzma-native"
13+
],
1114
"ignoreFiles": [],
1215
"commitlint": false,
1316
"eslint": false,

README.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,23 @@ Print the I2P hostname:
246246
247247
### Importing events from JSON Lines
248248
249-
You can import NIP-01 events from a `.jsonl` file directly into the relay database.
249+
You can import NIP-01 events from `.jsonl` files directly into the relay database.
250+
Compressed files are also supported and decompressed on-the-fly:
251+
252+
- `.jsonl.gz` (Gzip)
253+
- `.jsonl.xz` (XZ)
250254
251255
Basic import:
252256
```
253257
npm run import -- ./events.jsonl
254258
```
255259
260+
Import a compressed backup:
261+
```
262+
npm run import -- ./events.jsonl.gz
263+
npm run import -- ./events.jsonl.xz
264+
```
265+
256266
Set a custom batch size (default: `1000`):
257267
```
258268
npm run import -- ./events.jsonl --batch-size 500
@@ -636,16 +646,42 @@ To observe client and subscription counts in real-time during a test, you can in
636646
```bash
637647
docker compose logs -f nostream
638648
```
639-
640649
## Export Events
641650

642651
Export all stored events to a [JSON Lines](https://jsonlines.org/) (`.jsonl`) file. Each line is a valid NIP-01 Nostr event JSON object. The export streams rows from the database using cursors, so it works safely on relays with millions of events without loading them into memory.
643652

653+
Optional compression is supported for lower storage and transfer costs:
654+
655+
- Gzip via Node's native `zlib`
656+
- XZ via `lzma-native`
657+
644658
```
645659
npm run export # writes to events.jsonl
646660
npm run export -- backup-2024-01-01.jsonl # custom filename
661+
npm run export -- backup.jsonl.gz --compress --format=gzip
662+
npm run export -- backup.jsonl.xz --compress --format=xz
647663
```
648664

665+
Flags:
666+
667+
- `--compress` / `-z`: enable compression.
668+
- `--format <gzip|gz|xz>`: compression format. If omitted while compression is enabled,
669+
format is inferred from file extension (`.gz` / `.xz`) and defaults to `gzip`.
670+
671+
After completion, the exporter prints a summary with:
672+
673+
- Raw bytes generated from JSONL lines
674+
- Output bytes written to disk
675+
- Compression delta (smaller/larger)
676+
- Throughput in events/sec and bytes/sec
677+
678+
Optional XZ tuning (environment variables):
679+
680+
- `NOSTREAM_XZ_THREADS`: max worker threads for XZ compression.
681+
Defaults to `4` and is automatically capped to available CPU cores minus one.
682+
- `NOSTREAM_XZ_PRESET`: compression preset from `0` (fastest, larger output)
683+
to `9` (slowest, smallest output). Default is `6`.
684+
649685
The script reads the same `DB_*` environment variables used by the relay (see [CONFIGURATION.md](CONFIGURATION.md)).
650686
## Relay Maintenance
651687

package-lock.json

Lines changed: 48 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
"express": "4.22.1",
132132
"js-yaml": "4.1.1",
133133
"knex": "2.4.2",
134+
"lzma-native": "^8.0.6",
134135
"pg": "8.9.0",
135136
"pg-query-stream": "4.3.0",
136137
"ramda": "0.28.0",

0 commit comments

Comments
 (0)