@osmix/pbf 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @osmix/pbf
2
2
 
3
+ ## 0.0.5
4
+
5
+ ### Patch Changes
6
+
7
+ - f468db5: Fix publishing (2)
8
+
9
+ ## 0.0.4
10
+
11
+ ### Patch Changes
12
+
13
+ - 68d6bd8: Fix publishing for packages.
14
+
3
15
  ## 0.0.3
4
16
 
5
17
  ### Patch Changes
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Blob-to-block conversion utilities.
3
+ *
4
+ * Handles decompression and protobuf decoding of raw OSM PBF blobs into
5
+ * typed header and primitive block structures.
6
+ *
7
+ * @module
8
+ */
9
+ import { type OsmPbfBlock, type OsmPbfHeaderBlock } from "./proto/osmformat";
10
+ /**
11
+ * Decompress and decode a stream of raw PBF blobs into typed blocks.
12
+ *
13
+ * This async generator handles the transition from compressed bytes to parsed
14
+ * protobuf structures. The first blob is always decoded as a header block;
15
+ * subsequent blobs are decoded as primitive blocks containing OSM entities.
16
+ *
17
+ * @param blobs - Async or sync generator yielding compressed blob payloads.
18
+ * @param decompress - Optional decompression function (defaults to Web Streams zlib).
19
+ * @yields Header block first, then primitive blocks.
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * import { osmPbfBlobsToBlocksGenerator, createOsmPbfBlobGenerator } from "@osmix/pbf"
24
+ *
25
+ * const generateBlobs = createOsmPbfBlobGenerator()
26
+ * const blobsGen = (async function* () {
27
+ * for await (const chunk of stream) {
28
+ * yield* generateBlobs(chunk)
29
+ * }
30
+ * })()
31
+ *
32
+ * for await (const block of osmPbfBlobsToBlocksGenerator(blobsGen)) {
33
+ * // First iteration yields header, rest yield primitive blocks
34
+ * }
35
+ * ```
36
+ */
37
+ export declare function osmPbfBlobsToBlocksGenerator(blobs: AsyncGenerator<Uint8Array<ArrayBuffer>> | Generator<Uint8Array<ArrayBuffer>>, decompress?: (data: Uint8Array<ArrayBuffer>) => Promise<Uint8Array<ArrayBuffer>>): AsyncGenerator<OsmPbfHeaderBlock | OsmPbfBlock, void, unknown>;
38
+ /**
39
+ * Decompress and parse a header block from a compressed blob.
40
+ *
41
+ * @param compressedBlob - Zlib-compressed protobuf header blob.
42
+ * @param decompress - Optional decompression function.
43
+ * @returns Parsed header block with required/optional features and bbox.
44
+ */
45
+ export declare function readOsmHeaderBlock(compressedBlob: Uint8Array<ArrayBuffer>, decompress?: (data: Uint8Array<ArrayBuffer>) => Promise<Uint8Array<ArrayBuffer>>): Promise<OsmPbfHeaderBlock>;
46
+ /**
47
+ * Decompress and parse a primitive block from a compressed blob.
48
+ *
49
+ * @param compressedBlob - Zlib-compressed protobuf primitive blob.
50
+ * @param decompress - Optional decompression function.
51
+ * @returns Parsed primitive block with string table and primitive groups.
52
+ */
53
+ export declare function readOsmPrimitiveBlock(compressedBlob: Uint8Array<ArrayBuffer>, decompress?: (data: Uint8Array<ArrayBuffer>) => Promise<Uint8Array<ArrayBuffer>>): Promise<OsmPbfBlock>;
54
+ //# sourceMappingURL=blobs-to-blocks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"blobs-to-blocks.d.ts","sourceRoot":"","sources":["../src/blobs-to-blocks.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EACN,KAAK,WAAW,EAChB,KAAK,iBAAiB,EAGtB,MAAM,mBAAmB,CAAA;AAG1B;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAuB,4BAA4B,CAClD,KAAK,EACF,cAAc,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC,GACvC,SAAS,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC,EACrC,UAAU,GAAE,CACX,IAAI,EAAE,UAAU,CAAC,WAAW,CAAC,KACzB,OAAO,CAAC,UAAU,CAAC,WAAW,CAAC,CAAiB,kEAWrD;AAED;;;;;;GAMG;AACH,wBAAsB,kBAAkB,CACvC,cAAc,EAAE,UAAU,CAAC,WAAW,CAAC,EACvC,UAAU,GAAE,CACX,IAAI,EAAE,UAAU,CAAC,WAAW,CAAC,KACzB,OAAO,CAAC,UAAU,CAAC,WAAW,CAAC,CAAiB,GACnD,OAAO,CAAC,iBAAiB,CAAC,CAI5B;AAED;;;;;;GAMG;AACH,wBAAsB,qBAAqB,CAC1C,cAAc,EAAE,UAAU,CAAC,WAAW,CAAC,EACvC,UAAU,GAAE,CACX,IAAI,EAAE,UAAU,CAAC,WAAW,CAAC,KACzB,OAAO,CAAC,UAAU,CAAC,WAAW,CAAC,CAAiB,GACnD,OAAO,CAAC,WAAW,CAAC,CAItB"}
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Blob-to-block conversion utilities.
3
+ *
4
+ * Handles decompression and protobuf decoding of raw OSM PBF blobs into
5
+ * typed header and primitive block structures.
6
+ *
7
+ * @module
8
+ */
9
+ import Pbf from "pbf";
10
+ import { readHeaderBlock, readPrimitiveBlock, } from "./proto/osmformat";
11
+ import { webDecompress } from "./utils";
12
+ /**
13
+ * Decompress and decode a stream of raw PBF blobs into typed blocks.
14
+ *
15
+ * This async generator handles the transition from compressed bytes to parsed
16
+ * protobuf structures. The first blob is always decoded as a header block;
17
+ * subsequent blobs are decoded as primitive blocks containing OSM entities.
18
+ *
19
+ * @param blobs - Async or sync generator yielding compressed blob payloads.
20
+ * @param decompress - Optional decompression function (defaults to Web Streams zlib).
21
+ * @yields Header block first, then primitive blocks.
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * import { osmPbfBlobsToBlocksGenerator, createOsmPbfBlobGenerator } from "@osmix/pbf"
26
+ *
27
+ * const generateBlobs = createOsmPbfBlobGenerator()
28
+ * const blobsGen = (async function* () {
29
+ * for await (const chunk of stream) {
30
+ * yield* generateBlobs(chunk)
31
+ * }
32
+ * })()
33
+ *
34
+ * for await (const block of osmPbfBlobsToBlocksGenerator(blobsGen)) {
35
+ * // First iteration yields header, rest yield primitive blocks
36
+ * }
37
+ * ```
38
+ */
39
+ export async function* osmPbfBlobsToBlocksGenerator(blobs, decompress = webDecompress) {
40
+ let headerRead = false;
41
+ for await (const blob of blobs) {
42
+ if (!headerRead) {
43
+ headerRead = true;
44
+ yield readOsmHeaderBlock(blob, decompress);
45
+ }
46
+ else {
47
+ yield readOsmPrimitiveBlock(blob, decompress);
48
+ }
49
+ }
50
+ }
51
+ /**
52
+ * Decompress and parse a header block from a compressed blob.
53
+ *
54
+ * @param compressedBlob - Zlib-compressed protobuf header blob.
55
+ * @param decompress - Optional decompression function.
56
+ * @returns Parsed header block with required/optional features and bbox.
57
+ */
58
+ export async function readOsmHeaderBlock(compressedBlob, decompress = webDecompress) {
59
+ const decompressedBlob = await decompress(compressedBlob);
60
+ const pbf = new Pbf(decompressedBlob);
61
+ return readHeaderBlock(pbf);
62
+ }
63
+ /**
64
+ * Decompress and parse a primitive block from a compressed blob.
65
+ *
66
+ * @param compressedBlob - Zlib-compressed protobuf primitive blob.
67
+ * @param decompress - Optional decompression function.
68
+ * @returns Parsed primitive block with string table and primitive groups.
69
+ */
70
+ export async function readOsmPrimitiveBlock(compressedBlob, decompress = webDecompress) {
71
+ const decompressedBlob = await decompress(compressedBlob);
72
+ const pbf = new Pbf(decompressedBlob);
73
+ return readPrimitiveBlock(pbf);
74
+ }
75
+ //# sourceMappingURL=blobs-to-blocks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"blobs-to-blocks.js","sourceRoot":"","sources":["../src/blobs-to-blocks.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,GAAG,MAAM,KAAK,CAAA;AACrB,OAAO,EAGN,eAAe,EACf,kBAAkB,GAClB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAEvC;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,CAAC,KAAK,SAAS,CAAC,CAAC,4BAA4B,CAClD,KAEqC,EACrC,aAEwC,aAAa;IAErD,IAAI,UAAU,GAAG,KAAK,CAAA;IACtB,IAAI,KAAK,EAAE,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QAChC,IAAI,CAAC,UAAU,EAAE,CAAC;YACjB,UAAU,GAAG,IAAI,CAAA;YACjB,MAAM,kBAAkB,CAAC,IAAI,EAAE,UAAU,CAAC,CAAA;QAC3C,CAAC;aAAM,CAAC;YACP,MAAM,qBAAqB,CAAC,IAAI,EAAE,UAAU,CAAC,CAAA;QAC9C,CAAC;IACF,CAAC;AACF,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CACvC,cAAuC,EACvC,aAEwC,aAAa;IAErD,MAAM,gBAAgB,GAAG,MAAM,UAAU,CAAC,cAAc,CAAC,CAAA;IACzD,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,gBAAgB,CAAC,CAAA;IACrC,OAAO,eAAe,CAAC,GAAG,CAAC,CAAA;AAC5B,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,qBAAqB,CAC1C,cAAuC,EACvC,aAEwC,aAAa;IAErD,MAAM,gBAAgB,GAAG,MAAM,UAAU,CAAC,cAAc,CAAC,CAAA;IACzD,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,gBAAgB,CAAC,CAAA;IACrC,OAAO,kBAAkB,CAAC,GAAG,CAAC,CAAA;AAC/B,CAAC"}
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Block-to-PBF serialization utilities.
3
+ *
4
+ * Converts parsed OSM header and primitive blocks back into spec-compliant
5
+ * PBF byte sequences with proper framing, compression, and size validation.
6
+ *
7
+ * @module
8
+ */
9
+ import type { OsmPbfBlock, OsmPbfHeaderBlock } from "./proto/osmformat";
10
+ /**
11
+ * Serialize a header or primitive block into spec-compliant PBF bytes.
12
+ *
13
+ * Handles protobuf encoding, zlib compression, blob wrapping, and length prefixing.
14
+ * Validates output against OSM PBF specification size limits and logs warnings
15
+ * if recommended sizes are exceeded.
16
+ *
17
+ * @param block - Parsed header or primitive block to encode.
18
+ * @param compress - Optional compression function (defaults to Web Streams zlib).
19
+ * @returns Complete blob bytes: 4-byte length prefix + BlobHeader + Blob.
20
+ * @throws If blob exceeds maximum size limits (64 KiB header, 32 MiB data).
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * import { osmBlockToPbfBlobBytes } from "@osmix/pbf"
25
+ *
26
+ * const headerBytes = await osmBlockToPbfBlobBytes({
27
+ * required_features: ["OsmSchema-V0.6", "DenseNodes"],
28
+ * optional_features: [],
29
+ * })
30
+ * ```
31
+ */
32
+ export declare function osmBlockToPbfBlobBytes(block: OsmPbfBlock | OsmPbfHeaderBlock, compress?: (data: Uint8Array<ArrayBuffer>) => Promise<Uint8Array<ArrayBuffer>>): Promise<Uint8Array<ArrayBufferLike>>;
33
+ /**
34
+ * Web `TransformStream` that encodes OSM blocks into PBF byte chunks.
35
+ *
36
+ * Accepts a stream of header and primitive blocks and outputs spec-compliant
37
+ * PBF bytes. The header block must be the first item in the stream.
38
+ *
39
+ * @throws If a primitive block is received before the header.
40
+ *
41
+ * @example
42
+ * ```ts
43
+ * import { OsmBlocksToPbfBytesTransformStream } from "@osmix/pbf"
44
+ *
45
+ * const pbfStream = blocksStream.pipeThrough(new OsmBlocksToPbfBytesTransformStream())
46
+ * await pbfStream.pipeTo(writableFile)
47
+ * ```
48
+ */
49
+ export declare class OsmBlocksToPbfBytesTransformStream extends TransformStream<OsmPbfHeaderBlock | OsmPbfBlock, Uint8Array> {
50
+ headerEnqueued: boolean;
51
+ constructor();
52
+ }
53
+ //# sourceMappingURL=blocks-to-pbf.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"blocks-to-pbf.d.ts","sourceRoot":"","sources":["../src/blocks-to-pbf.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,OAAO,KAAK,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AAUvE;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAsB,sBAAsB,CAC3C,KAAK,EAAE,WAAW,GAAG,iBAAiB,EACtC,QAAQ,GAAE,CACT,IAAI,EAAE,UAAU,CAAC,WAAW,CAAC,KACzB,OAAO,CAAC,UAAU,CAAC,WAAW,CAAC,CAAe,wCAqDnD;AAED;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,kCAAmC,SAAQ,eAAe,CACtE,iBAAiB,GAAG,WAAW,EAC/B,UAAU,CACV;IACA,cAAc,UAAQ;;CAYtB"}
@@ -0,0 +1,110 @@
1
+ /**
2
+ * Block-to-PBF serialization utilities.
3
+ *
4
+ * Converts parsed OSM header and primitive blocks back into spec-compliant
5
+ * PBF byte sequences with proper framing, compression, and size validation.
6
+ *
7
+ * @module
8
+ */
9
+ import Pbf from "pbf";
10
+ import { writeBlob, writeBlobHeader } from "./proto/fileformat";
11
+ import { writeHeaderBlock, writePrimitiveBlock } from "./proto/osmformat";
12
+ import { MAX_BLOB_SIZE_BYTES, MAX_HEADER_SIZE_BYTES, RECOMMENDED_BLOB_SIZE_BYTES, RECOMMENDED_HEADER_SIZE_BYTES, } from "./spec";
13
+ import { concatUint8, uint32BE, webCompress } from "./utils";
14
+ /**
15
+ * Serialize a header or primitive block into spec-compliant PBF bytes.
16
+ *
17
+ * Handles protobuf encoding, zlib compression, blob wrapping, and length prefixing.
18
+ * Validates output against OSM PBF specification size limits and logs warnings
19
+ * if recommended sizes are exceeded.
20
+ *
21
+ * @param block - Parsed header or primitive block to encode.
22
+ * @param compress - Optional compression function (defaults to Web Streams zlib).
23
+ * @returns Complete blob bytes: 4-byte length prefix + BlobHeader + Blob.
24
+ * @throws If blob exceeds maximum size limits (64 KiB header, 32 MiB data).
25
+ *
26
+ * @example
27
+ * ```ts
28
+ * import { osmBlockToPbfBlobBytes } from "@osmix/pbf"
29
+ *
30
+ * const headerBytes = await osmBlockToPbfBlobBytes({
31
+ * required_features: ["OsmSchema-V0.6", "DenseNodes"],
32
+ * optional_features: [],
33
+ * })
34
+ * ```
35
+ */
36
+ export async function osmBlockToPbfBlobBytes(block, compress = webCompress) {
37
+ const contentPbf = new Pbf();
38
+ let type;
39
+ if ("primitivegroup" in block) {
40
+ type = "OSMData";
41
+ writePrimitiveBlock(block, contentPbf);
42
+ }
43
+ else {
44
+ type = "OSMHeader";
45
+ writeHeaderBlock(block, contentPbf);
46
+ }
47
+ const contentData = contentPbf.finish();
48
+ const raw_size = contentData.length;
49
+ const compressedBuffer = await compress(contentData);
50
+ const blobPbf = new Pbf();
51
+ writeBlob({
52
+ raw_size,
53
+ zlib_data: compressedBuffer,
54
+ }, blobPbf);
55
+ const blob = blobPbf.finish();
56
+ const blobHeaderPbf = new Pbf();
57
+ writeBlobHeader({
58
+ type,
59
+ datasize: blob.length,
60
+ }, blobHeaderPbf);
61
+ const blobHeader = blobHeaderPbf.finish();
62
+ const blobHeaderSize = uint32BE(blobHeader.byteLength);
63
+ // Check the BlobHeader and Blob sizes, log error if over the recommended size, throw error if over the maximum size
64
+ if (blobHeader.byteLength > RECOMMENDED_HEADER_SIZE_BYTES) {
65
+ const sizeKiB = (blobHeader.byteLength / 1024).toFixed(2);
66
+ if (blobHeader.byteLength > MAX_HEADER_SIZE_BYTES) {
67
+ throw new Error(`BlobHeader is ${sizeKiB} KiB, the maximum size is 64KiB`);
68
+ }
69
+ console.warn(`BlobHeader is ${sizeKiB} KiB, the recommended size is 32KiB`);
70
+ }
71
+ if (blob.byteLength > RECOMMENDED_BLOB_SIZE_BYTES) {
72
+ const sizeMiB = (blob.byteLength / 1024 / 1024).toFixed(2);
73
+ if (blob.byteLength > MAX_BLOB_SIZE_BYTES) {
74
+ throw new Error(`Blob is ${sizeMiB} MiB, the maximum size is 32MiB`);
75
+ }
76
+ console.warn(`Blob is ${sizeMiB} MiB, the recommended size is 16MiB`);
77
+ }
78
+ return concatUint8(blobHeaderSize, blobHeader, blob);
79
+ }
80
+ /**
81
+ * Web `TransformStream` that encodes OSM blocks into PBF byte chunks.
82
+ *
83
+ * Accepts a stream of header and primitive blocks and outputs spec-compliant
84
+ * PBF bytes. The header block must be the first item in the stream.
85
+ *
86
+ * @throws If a primitive block is received before the header.
87
+ *
88
+ * @example
89
+ * ```ts
90
+ * import { OsmBlocksToPbfBytesTransformStream } from "@osmix/pbf"
91
+ *
92
+ * const pbfStream = blocksStream.pipeThrough(new OsmBlocksToPbfBytesTransformStream())
93
+ * await pbfStream.pipeTo(writableFile)
94
+ * ```
95
+ */
96
+ export class OsmBlocksToPbfBytesTransformStream extends TransformStream {
97
+ headerEnqueued = false;
98
+ constructor() {
99
+ super({
100
+ transform: async (block, controller) => {
101
+ if ("primitivegroup" in block && !this.headerEnqueued) {
102
+ throw Error("Header first in ReadableStream of blocks.");
103
+ }
104
+ this.headerEnqueued = true;
105
+ controller.enqueue(await osmBlockToPbfBlobBytes(block));
106
+ },
107
+ });
108
+ }
109
+ }
110
+ //# sourceMappingURL=blocks-to-pbf.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"blocks-to-pbf.js","sourceRoot":"","sources":["../src/blocks-to-pbf.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,GAAG,MAAM,KAAK,CAAA;AACrB,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AAE/D,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AACzE,OAAO,EACN,mBAAmB,EACnB,qBAAqB,EACrB,2BAA2B,EAC3B,6BAA6B,GAC7B,MAAM,QAAQ,CAAA;AACf,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAE5D;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAC3C,KAAsC,EACtC,WAEwC,WAAW;IAEnD,MAAM,UAAU,GAAG,IAAI,GAAG,EAAE,CAAA;IAC5B,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CAAqC,CAAC,CAAA;IAC5E,CAAC;IACD,IAAI,IAAI,CAAC,UAAU,GAAG,2BAA2B,EAAE,CAAC;QACnD,MAAM,OAAO,GAAG,CAAC,IAAI,CAAC,UAAU,GAAG,IAAI,GAAG,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAA;QAC1D,IAAI,IAAI,CAAC,UAAU,GAAG,mBAAmB,EAAE,CAAC;YAC3C,MAAM,IAAI,KAAK,CAAC,WAAW,OAAO,iCAAiC,CAAC,CAAA;QACrE,CAAC;QACD,OAAO,CAAC,IAAI,CAAC,WAAW,OAAO,qCAAqC,CAAC,CAAA;IACtE,CAAC;IAED,OAAO,WAAW,CAAC,cAAc,EAAE,UAAU,EAAE,IAAI,CAAC,CAAA;AACrD,CAAC;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,OAAO,kCAAmC,SAAQ,eAGvD;IACA,cAAc,GAAG,KAAK,CAAA;IACtB;QACC,KAAK,CAAC;YACL,SAAS,EAAE,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,EAAE;gBACtC,IAAI,gBAAgB,IAAI,KAAK,IAAI,CAAC,IAAI,CAAC,cAAc,EAAE,CAAC;oBACvD,MAAM,KAAK,CAAC,2CAA2C,CAAC,CAAA;gBACzD,CAAC;gBACD,IAAI,CAAC,cAAc,GAAG,IAAI,CAAA;gBAC1B,UAAU,CAAC,OAAO,CAAC,MAAM,sBAAsB,CAAC,KAAK,CAAC,CAAC,CAAA;YACxD,CAAC;SACD,CAAC,CAAA;IACH,CAAC;CACD"}
@@ -0,0 +1,37 @@
1
+ /**
2
+ * @osmix/pbf - Low-level OSM PBF parsing and serialization.
3
+ *
4
+ * Provides streaming primitives for reading and writing OpenStreetMap PBF files
5
+ * using Web Streams and native compression APIs. Stays close to the official
6
+ * protobuf schema (`osmformat.proto`, `fileformat.proto`) while exposing
7
+ * predictable TypeScript types.
8
+ *
9
+ * Key capabilities:
10
+ * - **Parse**: Read headers and primitive blocks from `ArrayBuffer`, async iterables, or `ReadableStream`.
11
+ * - **Stream**: Use `TransformStream` helpers to process large files without buffering entirely in memory.
12
+ * - **Serialize**: Write header and primitive blocks back to spec-compliant blobs with size guardrails.
13
+ * - **Types**: Generated protobuf readers/writers and TypeScript interfaces for OSM data structures.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { readOsmPbf } from "@osmix/pbf"
18
+ *
19
+ * const { header, blocks } = await readOsmPbf(Bun.file('./monaco.pbf').stream())
20
+ * console.log(header.required_features)
21
+ *
22
+ * for await (const block of blocks) {
23
+ * console.log(block.primitivegroup.length, "groups")
24
+ * }
25
+ * ```
26
+ *
27
+ * @module @osmix/pbf
28
+ */
29
+ export * from "./blobs-to-blocks";
30
+ export * from "./blocks-to-pbf";
31
+ export * from "./pbf-to-blobs";
32
+ export * from "./pbf-to-blocks";
33
+ export * from "./proto/fileformat";
34
+ export * from "./proto/osmformat";
35
+ export * from "./spec";
36
+ export * from "./utils";
37
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,cAAc,mBAAmB,CAAA;AACjC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,oBAAoB,CAAA;AAClC,cAAc,mBAAmB,CAAA;AACjC,cAAc,QAAQ,CAAA;AACtB,cAAc,SAAS,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,37 @@
1
+ /**
2
+ * @osmix/pbf - Low-level OSM PBF parsing and serialization.
3
+ *
4
+ * Provides streaming primitives for reading and writing OpenStreetMap PBF files
5
+ * using Web Streams and native compression APIs. Stays close to the official
6
+ * protobuf schema (`osmformat.proto`, `fileformat.proto`) while exposing
7
+ * predictable TypeScript types.
8
+ *
9
+ * Key capabilities:
10
+ * - **Parse**: Read headers and primitive blocks from `ArrayBuffer`, async iterables, or `ReadableStream`.
11
+ * - **Stream**: Use `TransformStream` helpers to process large files without buffering entirely in memory.
12
+ * - **Serialize**: Write header and primitive blocks back to spec-compliant blobs with size guardrails.
13
+ * - **Types**: Generated protobuf readers/writers and TypeScript interfaces for OSM data structures.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { readOsmPbf } from "@osmix/pbf"
18
+ *
19
+ * const { header, blocks } = await readOsmPbf(Bun.file('./monaco.pbf').stream())
20
+ * console.log(header.required_features)
21
+ *
22
+ * for await (const block of blocks) {
23
+ * console.log(block.primitivegroup.length, "groups")
24
+ * }
25
+ * ```
26
+ *
27
+ * @module @osmix/pbf
28
+ */
29
+ export * from "./blobs-to-blocks";
30
+ export * from "./blocks-to-pbf";
31
+ export * from "./pbf-to-blobs";
32
+ export * from "./pbf-to-blocks";
33
+ export * from "./proto/fileformat";
34
+ export * from "./proto/osmformat";
35
+ export * from "./spec";
36
+ export * from "./utils";
37
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,cAAc,mBAAmB,CAAA;AACjC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,oBAAoB,CAAA;AAClC,cAAc,mBAAmB,CAAA;AACjC,cAAc,QAAQ,CAAA;AACtB,cAAc,SAAS,CAAA"}
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Create a stateful parser that extracts compressed blobs from raw PBF bytes.
3
+ *
4
+ * OSM PBF files consist of length-prefixed blobs. This function returns a generator
5
+ * that accumulates incoming byte chunks and yields complete compressed blobs as they
6
+ * become available. The caller is responsible for decompression.
7
+ *
8
+ * The first yielded blob contains the file header; subsequent blobs contain primitive data.
9
+ *
10
+ * @returns A generator function that accepts byte chunks and yields compressed blob payloads.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * import { createOsmPbfBlobGenerator } from "@osmix/pbf"
15
+ *
16
+ * const generateBlobs = createOsmPbfBlobGenerator()
17
+ *
18
+ * for await (const chunk of stream) {
19
+ * for (const compressedBlob of generateBlobs(chunk)) {
20
+ * // Decompress and parse blob...
21
+ * }
22
+ * }
23
+ * ```
24
+ */
25
+ export declare function createOsmPbfBlobGenerator(): (chunk: Uint8Array) => Generator<Uint8Array<ArrayBuffer>, void, unknown>;
26
+ //# sourceMappingURL=pbf-to-blobs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pbf-to-blobs.d.ts","sourceRoot":"","sources":["../src/pbf-to-blobs.ts"],"names":[],"mappings":"AAQA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,yBAAyB,KAWb,OAAO,UAAU,uDAiC5C"}
@@ -0,0 +1,70 @@
1
+ import Pbf from "pbf";
2
+ import { HEADER_LENGTH_BYTES } from "./pbf-to-blocks";
3
+ import { readBlob, readBlobHeader, } from "./proto/fileformat";
4
+ /**
5
+ * Create a stateful parser that extracts compressed blobs from raw PBF bytes.
6
+ *
7
+ * OSM PBF files consist of length-prefixed blobs. This function returns a generator
8
+ * that accumulates incoming byte chunks and yields complete compressed blobs as they
9
+ * become available. The caller is responsible for decompression.
10
+ *
11
+ * The first yielded blob contains the file header; subsequent blobs contain primitive data.
12
+ *
13
+ * @returns A generator function that accepts byte chunks and yields compressed blob payloads.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { createOsmPbfBlobGenerator } from "@osmix/pbf"
18
+ *
19
+ * const generateBlobs = createOsmPbfBlobGenerator()
20
+ *
21
+ * for await (const chunk of stream) {
22
+ * for (const compressedBlob of generateBlobs(chunk)) {
23
+ * // Decompress and parse blob...
24
+ * }
25
+ * }
26
+ * ```
27
+ */
28
+ export function createOsmPbfBlobGenerator() {
29
+ let pbf = new Pbf(new Uint8Array(0));
30
+ let state = "header-length";
31
+ let bytesNeeded = HEADER_LENGTH_BYTES;
32
+ let blobHeader = null;
33
+ /**
34
+ * Feed the parser with the next chunk of bytes and yield any complete compressed blobs.
35
+ * @param chunk - Raw bytes from the PBF file.
36
+ * @yields Compressed blob payloads (zlib-compressed protobuf data).
37
+ */
38
+ return function* nextChunk(chunk) {
39
+ const currentBuffer = pbf.buf.slice(pbf.pos);
40
+ const tmpBuffer = new Uint8Array(currentBuffer.buffer.byteLength + chunk.byteLength);
41
+ tmpBuffer.set(currentBuffer.subarray(0));
42
+ tmpBuffer.set(new Uint8Array(chunk), currentBuffer.byteLength);
43
+ pbf = new Pbf(tmpBuffer);
44
+ while (pbf.pos + bytesNeeded <= pbf.length) {
45
+ if (state === "header-length") {
46
+ const dataView = new DataView(pbf.buf.buffer);
47
+ bytesNeeded = dataView.getInt32(pbf.pos, false); // network byte order
48
+ pbf.pos += HEADER_LENGTH_BYTES;
49
+ state = "header";
50
+ }
51
+ else if (state === "header") {
52
+ blobHeader = readBlobHeader(pbf, pbf.pos + bytesNeeded);
53
+ bytesNeeded = blobHeader.datasize;
54
+ state = "blob";
55
+ }
56
+ else if (state === "blob") {
57
+ if (blobHeader == null)
58
+ throw Error("Blob header has not been read");
59
+ const blob = readBlob(pbf, pbf.pos + bytesNeeded);
60
+ if (blob.zlib_data === undefined || blob.zlib_data.length === 0)
61
+ throw Error("Blob has no zlib data. Format is unsupported.");
62
+ yield blob.zlib_data;
63
+ state = "header-length";
64
+ bytesNeeded = HEADER_LENGTH_BYTES;
65
+ blobHeader = null;
66
+ }
67
+ }
68
+ };
69
+ }
70
+ //# sourceMappingURL=pbf-to-blobs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pbf-to-blobs.js","sourceRoot":"","sources":["../src/pbf-to-blobs.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,KAAK,CAAA;AACrB,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;AACrD,OAAO,EAEN,QAAQ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AAQ,EAAE,CAAC;gBAC/B,UAAU,GAAG,cAAc,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,GAAG,WAAW,CAAC,CAAA;gBACvD,WAAW,GAAG,UAAU,CAAC,QAAQ,CAAA;gBACjC,KAAK,GAAG,MAAM,CAAA;YACf,CAAC;iBAAM,IAAI,KAAK,KAAK,MAAM,EAAE,CAAC;gBAC7B,IAAI,UAAU,IAAI,IAAI;oBAAE,MAAM,KAAK,CAAC,+BAA+B,CAAC,CAAA;gBACpE,MAAM,IAAI,GAAG,QAAQ,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,GAAG,WAAW,CAAC,CAAA;gBACjD,IAAI,IAAI,CAAC,SAAS,KAAK,SAAS,IAAI,IAAI,CAAC,SAAS,CAAC,MAAM,KAAK,CAAC;oBAC9D,MAAM,KAAK,CAAC,+CAA+C,CAAC,CAAA;gBAE7D,MAAM,IAAI,CAAC,SAAoC,CAAA;gBAE/C,KAAK,GAAG,eAAe,CAAA;gBACvB,WAAW,GAAG,mBAAmB,CAAA;gBACjC,UAAU,GAAG,IAAI,CAAA;YAClB,CAAC;QACF,CAAC;IACF,CAAC,CAAA;AACF,CAAC"}
@@ -0,0 +1,63 @@
1
+ import { type OsmPbfBlock, type OsmPbfHeaderBlock } from "./proto/osmformat";
2
+ import { type AsyncGeneratorValue } from "./utils";
3
+ /** Number of bytes used to encode the BlobHeader length prefix (big-endian uint32). */
4
+ export declare const HEADER_LENGTH_BYTES = 4;
5
+ /**
6
+ * Parse an OSM PBF file from various input sources.
7
+ *
8
+ * Accepts `ArrayBuffer`, `Uint8Array`, `ReadableStream<Uint8Array>`, or async generators.
9
+ * Returns the file header and a lazy async generator of primitive blocks for on-demand parsing.
10
+ *
11
+ * @param data - PBF bytes as buffer, stream, or async iterable.
12
+ * @returns Object with `header` (file metadata) and `blocks` (async generator of primitive blocks).
13
+ * @throws If the header block is missing or malformed.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { readOsmPbf } from "@osmix/pbf"
18
+ *
19
+ * // From a file stream
20
+ * const { header, blocks } = await readOsmPbf(Bun.file('./monaco.pbf').stream())
21
+ *
22
+ * // From a fetch response
23
+ * const response = await fetch('/data/monaco.pbf')
24
+ * const { header, blocks } = await readOsmPbf(response.body!)
25
+ *
26
+ * // Iterate blocks lazily
27
+ * for await (const block of blocks) {
28
+ * for (const group of block.primitivegroup) {
29
+ * console.log(group.dense?.id.length ?? 0, "dense nodes")
30
+ * }
31
+ * }
32
+ * ```
33
+ */
34
+ export declare function readOsmPbf(data: AsyncGeneratorValue<Uint8Array<ArrayBufferLike>>): Promise<{
35
+ header: OsmPbfHeaderBlock;
36
+ blocks: AsyncGenerator<OsmPbfBlock>;
37
+ }>;
38
+ /**
39
+ * Web `TransformStream` that decodes raw PBF byte chunks into OSM header and data blocks.
40
+ *
41
+ * The first blob in an OSM PBF file is always the header block; subsequent blobs
42
+ * contain primitive data (nodes, ways, relations). This stream handles the framing,
43
+ * decompression, and protobuf decoding automatically.
44
+ *
45
+ * @example
46
+ * ```ts
47
+ * import { OsmPbfBytesToBlocksTransformStream } from "@osmix/pbf"
48
+ *
49
+ * const response = await fetch('/data/monaco.pbf')
50
+ * const blocksStream = response.body!
51
+ * .pipeThrough(new OsmPbfBytesToBlocksTransformStream())
52
+ *
53
+ * const reader = blocksStream.getReader()
54
+ * const { value: header } = await reader.read() // First read yields header
55
+ * // Subsequent reads yield primitive blocks
56
+ * ```
57
+ */
58
+ export declare class OsmPbfBytesToBlocksTransformStream extends TransformStream<Uint8Array<ArrayBufferLike>, OsmPbfHeaderBlock | OsmPbfBlock> {
59
+ generateBlobsFromChunk: (chunk: Uint8Array) => Generator<Uint8Array<ArrayBuffer>, void, unknown>;
60
+ header: OsmPbfHeaderBlock | null;
61
+ constructor(decompress?: (data: Uint8Array<ArrayBuffer>) => Promise<Uint8Array<ArrayBuffer>>);
62
+ }
63
+ //# sourceMappingURL=pbf-to-blocks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pbf-to-blocks.d.ts","sourceRoot":"","sources":["../src/pbf-to-blocks.ts"],"names":[],"mappings":"AAGA,OAAO,EACN,KAAK,WAAW,EAChB,KAAK,iBAAiB,EAGtB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EACN,KAAK,mBAAmB,EAGxB,MAAM,SAAS,CAAA;AAEhB,uFAAuF;AACvF,eAAO,MAAM,mBAAmB,IAAI,CAAA;AAEpC;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAsB,UAAU,CAC/B,IAAI,EAAE,mBAAmB,CAAC,UAAU,CAAC,eAAe,CAAC,CAAC;;YAkBnC,cAAc,CAAC,WAAW,CAAC;GAE9C;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,kCAAmC,SAAQ,eAAe,CACtE,UAAU,CAAC,eAAe,CAAC,EAC3B,iBAAiB,GAAG,WAAW,CAC/B;IACA,sBAAsB,2EAA8B;IACpD,MAAM,EAAE,iBAAiB,GAAG,IAAI,CAAO;gBAEtC,UAAU,GAAE,CACX,IAAI,EAAE,UAAU,CAAC,WAAW,CAAC,KACzB,OAAO,CAAC,UAAU,CAAC,WAAW,CAAC,CAAiB;CAiBtD"}