@osmix/pbf 0.0.1 → 0.0.3
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 +12 -0
- package/README.md +75 -82
- package/dist/src/blobs-to-blocks.d.ts +54 -0
- package/dist/src/blobs-to-blocks.d.ts.map +1 -0
- package/dist/src/blobs-to-blocks.js +75 -0
- package/dist/src/blobs-to-blocks.js.map +1 -0
- package/dist/src/blocks-to-pbf.d.ts +53 -0
- package/dist/src/blocks-to-pbf.d.ts.map +1 -0
- package/dist/{blocks-to-pbf.js → src/blocks-to-pbf.js} +44 -7
- package/dist/src/blocks-to-pbf.js.map +1 -0
- package/dist/src/index.d.ts +37 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +37 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/pbf-to-blobs.d.ts +26 -0
- package/dist/src/pbf-to-blobs.d.ts.map +1 -0
- package/dist/{pbf-to-blobs.js → src/pbf-to-blobs.js} +25 -3
- package/dist/src/pbf-to-blobs.js.map +1 -0
- package/dist/src/pbf-to-blocks.d.ts +63 -0
- package/dist/src/pbf-to-blocks.d.ts.map +1 -0
- package/dist/src/pbf-to-blocks.js +96 -0
- package/dist/src/pbf-to-blocks.js.map +1 -0
- package/dist/{proto → src/proto}/fileformat.d.ts +1 -0
- package/dist/src/proto/fileformat.d.ts.map +1 -0
- package/dist/{proto → src/proto}/fileformat.js +1 -0
- package/dist/src/proto/fileformat.js.map +1 -0
- package/dist/{proto → src/proto}/osmformat.d.ts +1 -0
- package/dist/src/proto/osmformat.d.ts.map +1 -0
- package/dist/{proto → src/proto}/osmformat.js +1 -0
- package/dist/src/proto/osmformat.js.map +1 -0
- package/dist/{spec.d.ts → src/spec.d.ts} +1 -0
- package/dist/src/spec.d.ts.map +1 -0
- package/dist/{spec.js → src/spec.js} +1 -0
- package/dist/src/spec.js.map +1 -0
- package/dist/{utils.d.ts → src/utils.d.ts} +5 -10
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/{utils.js → src/utils.js} +8 -32
- package/dist/src/utils.js.map +1 -0
- package/dist/test/blobs-to-blocks.test.d.ts +2 -0
- package/dist/test/blobs-to-blocks.test.d.ts.map +1 -0
- package/dist/test/blobs-to-blocks.test.js +61 -0
- package/dist/test/blobs-to-blocks.test.js.map +1 -0
- package/dist/test/helpers.d.ts +11 -0
- package/dist/test/helpers.d.ts.map +1 -0
- package/dist/test/helpers.js +58 -0
- package/dist/test/helpers.js.map +1 -0
- package/dist/test/pbf-to-blobs.test.d.ts +2 -0
- package/dist/test/pbf-to-blobs.test.d.ts.map +1 -0
- package/dist/test/pbf-to-blobs.test.js +77 -0
- package/dist/test/pbf-to-blobs.test.js.map +1 -0
- package/dist/test/read.bench.d.ts +2 -0
- package/dist/test/read.bench.d.ts.map +1 -0
- package/dist/test/read.bench.js +31 -0
- package/dist/test/read.bench.js.map +1 -0
- package/dist/test/read.test.d.ts +2 -0
- package/dist/test/read.test.d.ts.map +1 -0
- package/dist/test/read.test.js +34 -0
- package/dist/test/read.test.js.map +1 -0
- package/dist/test/streams.test.d.ts +2 -0
- package/dist/test/streams.test.d.ts.map +1 -0
- package/dist/test/streams.test.js +78 -0
- package/dist/test/streams.test.js.map +1 -0
- package/dist/test/utils.d.ts +25 -0
- package/dist/test/utils.d.ts.map +1 -0
- package/dist/test/utils.js +47 -0
- package/dist/test/utils.js.map +1 -0
- package/dist/test/utils.test.d.ts +2 -0
- package/dist/test/utils.test.d.ts.map +1 -0
- package/dist/test/utils.test.js +44 -0
- package/dist/test/utils.test.js.map +1 -0
- package/dist/test/write.test.d.ts +2 -0
- package/dist/test/write.test.d.ts.map +1 -0
- package/dist/test/write.test.js +69 -0
- package/dist/test/write.test.js.map +1 -0
- package/package.json +7 -7
- package/src/blobs-to-blocks.ts +78 -7
- package/src/blocks-to-pbf.ts +42 -5
- package/src/index.ts +29 -0
- package/src/pbf-to-blobs.ts +24 -3
- package/src/pbf-to-blocks.ts +52 -12
- package/src/utils.ts +0 -7
- package/test/blobs-to-blocks.test.ts +25 -19
- package/test/pbf-to-blobs.test.ts +23 -17
- package/test/read.bench.ts +8 -4
- package/test/read.test.ts +5 -5
- package/test/streams.test.ts +22 -13
- package/test/utils.test.ts +8 -8
- package/test/utils.ts +8 -8
- package/test/write.test.ts +14 -9
- package/dist/blobs-to-blocks.d.ts +0 -5
- package/dist/blobs-to-blocks.js +0 -21
- package/dist/blocks-to-pbf.d.ts +0 -16
- package/dist/index.d.ts +0 -8
- package/dist/index.js +0 -8
- package/dist/pbf-to-blobs.d.ts +0 -6
- package/dist/pbf-to-blocks.d.ts +0 -20
- package/dist/pbf-to-blocks.js +0 -53
- package/test/utils.bun.test.ts +0 -327
- package/test/verify-pbf-reading.bun.test.ts +0 -39
- package/vitest.config.ts +0 -7
package/dist/blobs-to-blocks.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import Pbf from "pbf";
|
|
2
|
-
import { readHeaderBlock, readPrimitiveBlock } from "./proto/osmformat";
|
|
3
|
-
import { decompress } from "./utils";
|
|
4
|
-
/**
|
|
5
|
-
* Decompresses raw OSM PBF blobs and yields typed header and primitive blocks.
|
|
6
|
-
* Expects the first blob to contain the file header and streams the rest as data blocks.
|
|
7
|
-
*/
|
|
8
|
-
export async function* osmPbfBlobsToBlocksGenerator(blobs) {
|
|
9
|
-
let headerRead = false;
|
|
10
|
-
for await (const blob of blobs) {
|
|
11
|
-
const decompressedBlob = await decompress(blob);
|
|
12
|
-
const pbf = new Pbf(decompressedBlob);
|
|
13
|
-
if (!headerRead) {
|
|
14
|
-
headerRead = true;
|
|
15
|
-
yield readHeaderBlock(pbf);
|
|
16
|
-
}
|
|
17
|
-
else {
|
|
18
|
-
yield readPrimitiveBlock(pbf);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
package/dist/blocks-to-pbf.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { OsmPbfBlock, OsmPbfHeaderBlock } from "./proto/osmformat";
|
|
2
|
-
/**
|
|
3
|
-
* Serializes a header or primitive block into a spec-compliant compressed blob.
|
|
4
|
-
* Automatically sets the blob type, compresses the payload, and enforces size guardrails.
|
|
5
|
-
* @param block - Parsed header or primitive block to encode.
|
|
6
|
-
* @returns BlobHeader length prefix + Blob bytes as a single Uint8Array.
|
|
7
|
-
*/
|
|
8
|
-
export declare function osmBlockToPbfBlobBytes(block: OsmPbfBlock | OsmPbfHeaderBlock): Promise<Uint8Array<ArrayBufferLike>>;
|
|
9
|
-
/**
|
|
10
|
-
* Web `TransformStream` that converts OSM header/data blocks into PBF byte chunks.
|
|
11
|
-
* Throws if the header is not the first block and reuses `osmBlockToPbfBlobBytes` for encoding.
|
|
12
|
-
*/
|
|
13
|
-
export declare class OsmBlocksToPbfBytesTransformStream extends TransformStream<OsmPbfHeaderBlock | OsmPbfBlock, Uint8Array> {
|
|
14
|
-
headerEnqueued: boolean;
|
|
15
|
-
constructor();
|
|
16
|
-
}
|
package/dist/index.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export * from "./blobs-to-blocks";
|
|
2
|
-
export * from "./blocks-to-pbf";
|
|
3
|
-
export * from "./pbf-to-blobs";
|
|
4
|
-
export * from "./pbf-to-blocks";
|
|
5
|
-
export * from "./proto/fileformat";
|
|
6
|
-
export * from "./proto/osmformat";
|
|
7
|
-
export * from "./spec";
|
|
8
|
-
export * from "./utils";
|
package/dist/index.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export * from "./blobs-to-blocks";
|
|
2
|
-
export * from "./blocks-to-pbf";
|
|
3
|
-
export * from "./pbf-to-blobs";
|
|
4
|
-
export * from "./pbf-to-blocks";
|
|
5
|
-
export * from "./proto/fileformat";
|
|
6
|
-
export * from "./proto/osmformat";
|
|
7
|
-
export * from "./spec";
|
|
8
|
-
export * from "./utils";
|
package/dist/pbf-to-blobs.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Creates a stateful parser that slices incoming bytes into compressed OSM PBF blobs.
|
|
3
|
-
* Works with buffers, iterables, or streams and yields `Uint8Array` payloads ready to decompress.
|
|
4
|
-
* The first blob represents the file header; subsequent blobs hold primitive data.
|
|
5
|
-
*/
|
|
6
|
-
export declare function createOsmPbfBlobGenerator(): (chunk: Uint8Array) => Generator<Uint8Array<ArrayBuffer>, void, unknown>;
|
package/dist/pbf-to-blocks.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { type OsmPbfBlock, type OsmPbfHeaderBlock } from "./proto/osmformat";
|
|
2
|
-
import { type AsyncGeneratorValue } from "./utils";
|
|
3
|
-
export declare const HEADER_LENGTH_BYTES = 4;
|
|
4
|
-
/**
|
|
5
|
-
* Parses OSM PBF bytes from buffers, streams, or generators into header + block iterators.
|
|
6
|
-
* Returns the decoded header and a lazy async generator of primitive blocks.
|
|
7
|
-
*/
|
|
8
|
-
export declare function readOsmPbf(data: AsyncGeneratorValue<ArrayBufferLike>): Promise<{
|
|
9
|
-
header: OsmPbfHeaderBlock;
|
|
10
|
-
blocks: AsyncGenerator<OsmPbfBlock>;
|
|
11
|
-
}>;
|
|
12
|
-
/**
|
|
13
|
-
* Web `TransformStream` that turns raw PBF byte chunks into OSM header/data blocks.
|
|
14
|
-
* Assumes the first decoded blob carries the header and emits it before any primitive blocks.
|
|
15
|
-
*/
|
|
16
|
-
export declare class OsmPbfBytesToBlocksTransformStream extends TransformStream<ArrayBufferLike, OsmPbfHeaderBlock | OsmPbfBlock> {
|
|
17
|
-
generateBlobsFromChunk: (chunk: Uint8Array) => Generator<Uint8Array<ArrayBuffer>, void, unknown>;
|
|
18
|
-
header: OsmPbfHeaderBlock | null;
|
|
19
|
-
constructor();
|
|
20
|
-
}
|
package/dist/pbf-to-blocks.js
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import Pbf from "pbf";
|
|
2
|
-
import { osmPbfBlobsToBlocksGenerator } from "./blobs-to-blocks";
|
|
3
|
-
import { createOsmPbfBlobGenerator } from "./pbf-to-blobs";
|
|
4
|
-
import { readHeaderBlock, readPrimitiveBlock, } from "./proto/osmformat";
|
|
5
|
-
import { decompress, toAsyncGenerator } from "./utils";
|
|
6
|
-
export const HEADER_LENGTH_BYTES = 4;
|
|
7
|
-
/**
|
|
8
|
-
* Parses OSM PBF bytes from buffers, streams, or generators into header + block iterators.
|
|
9
|
-
* Returns the decoded header and a lazy async generator of primitive blocks.
|
|
10
|
-
*/
|
|
11
|
-
export async function readOsmPbf(data) {
|
|
12
|
-
const generateBlobsFromChunk = createOsmPbfBlobGenerator();
|
|
13
|
-
const blocks = osmPbfBlobsToBlocksGenerator((async function* () {
|
|
14
|
-
for await (const chunk of toAsyncGenerator(data)) {
|
|
15
|
-
for await (const blob of generateBlobsFromChunk(new Uint8Array(chunk))) {
|
|
16
|
-
yield blob;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
})());
|
|
20
|
-
const header = (await blocks.next()).value;
|
|
21
|
-
if (header == null || !("required_features" in header)) {
|
|
22
|
-
throw Error("OSM PBF header block not found");
|
|
23
|
-
}
|
|
24
|
-
return {
|
|
25
|
-
header,
|
|
26
|
-
blocks: blocks,
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Web `TransformStream` that turns raw PBF byte chunks into OSM header/data blocks.
|
|
31
|
-
* Assumes the first decoded blob carries the header and emits it before any primitive blocks.
|
|
32
|
-
*/
|
|
33
|
-
export class OsmPbfBytesToBlocksTransformStream extends TransformStream {
|
|
34
|
-
generateBlobsFromChunk = createOsmPbfBlobGenerator();
|
|
35
|
-
header = null;
|
|
36
|
-
constructor() {
|
|
37
|
-
super({
|
|
38
|
-
transform: async (bytesChunk, controller) => {
|
|
39
|
-
for await (const rawBlobs of this.generateBlobsFromChunk(new Uint8Array(bytesChunk))) {
|
|
40
|
-
const decompressed = await decompress(rawBlobs);
|
|
41
|
-
const pbf = new Pbf(decompressed);
|
|
42
|
-
if (this.header == null) {
|
|
43
|
-
this.header = readHeaderBlock(pbf);
|
|
44
|
-
controller.enqueue(this.header);
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
controller.enqueue(readPrimitiveBlock(pbf));
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
},
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
}
|
package/test/utils.bun.test.ts
DELETED
|
@@ -1,327 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from "vitest"
|
|
2
|
-
import { concatUint8, isBun, toAsyncGenerator, uint32BE } from "../src/utils"
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Bun decompress.
|
|
6
|
-
*/
|
|
7
|
-
async function bunDecompress(
|
|
8
|
-
data: Uint8Array<ArrayBuffer>,
|
|
9
|
-
): Promise<Uint8Array<ArrayBuffer>> {
|
|
10
|
-
const { inflate } = await import("node:zlib")
|
|
11
|
-
const result = await new Promise<ArrayBuffer>((resolve, reject) => {
|
|
12
|
-
inflate(data, (error, result) => {
|
|
13
|
-
if (error) reject(error)
|
|
14
|
-
else resolve(result.buffer as ArrayBuffer)
|
|
15
|
-
})
|
|
16
|
-
})
|
|
17
|
-
return new Uint8Array(result)
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Bun compression.
|
|
22
|
-
*/
|
|
23
|
-
export async function bunCompress(
|
|
24
|
-
data: Uint8Array<ArrayBuffer>,
|
|
25
|
-
): Promise<Uint8Array<ArrayBuffer>> {
|
|
26
|
-
const { deflate } = await import("node:zlib")
|
|
27
|
-
const result = await new Promise<ArrayBuffer>((resolve, reject) => {
|
|
28
|
-
deflate(data, (error, result) => {
|
|
29
|
-
if (error) reject(error)
|
|
30
|
-
else resolve(result.buffer as ArrayBuffer)
|
|
31
|
-
})
|
|
32
|
-
})
|
|
33
|
-
return new Uint8Array(result)
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
describe.runIf(isBun())("utils", () => {
|
|
37
|
-
test("wraps values into an async generator", async () => {
|
|
38
|
-
const generator = toAsyncGenerator(3)
|
|
39
|
-
const first = await generator.next()
|
|
40
|
-
expect(first).toEqual({ value: 3, done: false })
|
|
41
|
-
const done = await generator.next()
|
|
42
|
-
expect(done).toEqual({ value: undefined, done: true })
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
test("consumes readable streams", async () => {
|
|
46
|
-
const stream = new ReadableStream<number>({
|
|
47
|
-
start(controller) {
|
|
48
|
-
controller.enqueue(1)
|
|
49
|
-
controller.enqueue(2)
|
|
50
|
-
controller.close()
|
|
51
|
-
},
|
|
52
|
-
})
|
|
53
|
-
const values: number[] = []
|
|
54
|
-
for await (const value of toAsyncGenerator(stream)) values.push(value)
|
|
55
|
-
expect(values).toEqual([1, 2])
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
test("throws on nullish inputs", async () => {
|
|
59
|
-
const invalidInput = null as unknown as never
|
|
60
|
-
await expect(toAsyncGenerator(invalidInput).next()).rejects.toThrow(
|
|
61
|
-
"Value is null",
|
|
62
|
-
)
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
test("concatenates Uint8Array segments", () => {
|
|
66
|
-
const a = Uint8Array.of(1, 2)
|
|
67
|
-
const b = Uint8Array.of(3)
|
|
68
|
-
expect(concatUint8(a, b)).toEqual(Uint8Array.of(1, 2, 3))
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
test("encodes big-endian 32-bit integers", () => {
|
|
72
|
-
expect(uint32BE(0x01020304)).toEqual(Uint8Array.of(1, 2, 3, 4))
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
test("compresses and decompresses data with deflate", async () => {
|
|
76
|
-
const input = new TextEncoder().encode("osmix") as Uint8Array<ArrayBuffer>
|
|
77
|
-
const compressed = await bunCompress(input)
|
|
78
|
-
expect(compressed).not.toEqual(input)
|
|
79
|
-
const decompressed = await bunDecompress(compressed)
|
|
80
|
-
expect(decompressed).toEqual(input)
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
test("compresses and decompresses larger data", async () => {
|
|
84
|
-
const input = new TextEncoder().encode(
|
|
85
|
-
"a".repeat(1000),
|
|
86
|
-
) as Uint8Array<ArrayBuffer>
|
|
87
|
-
const compressed = await bunCompress(input)
|
|
88
|
-
expect(compressed.length).toBeLessThan(input.length)
|
|
89
|
-
const decompressed = await bunDecompress(compressed)
|
|
90
|
-
expect(decompressed).toEqual(input)
|
|
91
|
-
})
|
|
92
|
-
|
|
93
|
-
test("handles Uint8Array input", async () => {
|
|
94
|
-
const input = Uint8Array.of(1, 2, 3, 4, 5)
|
|
95
|
-
const compressed = await bunCompress(input)
|
|
96
|
-
const decompressed = await bunDecompress(compressed)
|
|
97
|
-
expect(decompressed).toEqual(input)
|
|
98
|
-
})
|
|
99
|
-
|
|
100
|
-
test("uses Bun runtime with Node.js zlib compatibility", () => {
|
|
101
|
-
// This test verifies that Bun is available in the runtime
|
|
102
|
-
expect(isBun()).toBe(true)
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
test("Node.js zlib methods work in Bun", async () => {
|
|
106
|
-
const { deflateSync, inflateSync } = await import("node:zlib")
|
|
107
|
-
const input = new TextEncoder().encode("test bun compression with zlib")
|
|
108
|
-
const compressed = deflateSync(input)
|
|
109
|
-
expect(compressed.length).toBeGreaterThan(0)
|
|
110
|
-
expect(compressed).not.toEqual(input)
|
|
111
|
-
|
|
112
|
-
const decompressed = inflateSync(compressed)
|
|
113
|
-
expect(new Uint8Array(decompressed)).toEqual(input)
|
|
114
|
-
})
|
|
115
|
-
|
|
116
|
-
test("compress/decompress are compatible with OSM PBF zlib format", async () => {
|
|
117
|
-
// Test that our compress/decompress functions produce zlib-compatible data
|
|
118
|
-
// This is critical for OSM PBF compatibility
|
|
119
|
-
const { deflateSync, inflateSync } = await import("node:zlib")
|
|
120
|
-
const input = new TextEncoder().encode(
|
|
121
|
-
"OSM PBF uses zlib format (deflate with headers)",
|
|
122
|
-
) as Uint8Array<ArrayBuffer>
|
|
123
|
-
|
|
124
|
-
// Compress with our function
|
|
125
|
-
const ourCompressed = await bunCompress(input)
|
|
126
|
-
|
|
127
|
-
// Decompress with Node.js zlib (what OSM PBF uses)
|
|
128
|
-
const decompressedWithNodeZlib = inflateSync(ourCompressed)
|
|
129
|
-
expect(new Uint8Array(decompressedWithNodeZlib)).toEqual(input)
|
|
130
|
-
|
|
131
|
-
// Compress with Node.js zlib
|
|
132
|
-
const nodeCompressed = deflateSync(input)
|
|
133
|
-
|
|
134
|
-
// Decompress with our function
|
|
135
|
-
const decompressedWithOurs = await bunDecompress(
|
|
136
|
-
new Uint8Array(nodeCompressed),
|
|
137
|
-
)
|
|
138
|
-
expect(decompressedWithOurs).toEqual(input)
|
|
139
|
-
})
|
|
140
|
-
})
|
|
141
|
-
|
|
142
|
-
describe.skip("CompressionStream polyfill", () => {
|
|
143
|
-
test("compresses data using deflate format", async () => {
|
|
144
|
-
const input = new TextEncoder().encode("test compression stream")
|
|
145
|
-
const compressor = new CompressionStream("deflate")
|
|
146
|
-
|
|
147
|
-
const writer = compressor.writable.getWriter()
|
|
148
|
-
writer.write(input)
|
|
149
|
-
writer.close()
|
|
150
|
-
|
|
151
|
-
const chunks: Uint8Array[] = []
|
|
152
|
-
const reader = compressor.readable.getReader()
|
|
153
|
-
while (true) {
|
|
154
|
-
const { done, value } = await reader.read()
|
|
155
|
-
if (done) break
|
|
156
|
-
chunks.push(value)
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
const compressed = concatUint8(...chunks)
|
|
160
|
-
expect(compressed.length).toBeGreaterThan(0)
|
|
161
|
-
expect(compressed).not.toEqual(input)
|
|
162
|
-
|
|
163
|
-
// Verify it's valid deflate data by decompressing
|
|
164
|
-
const decompressed = await bunDecompress(new Uint8Array(compressed))
|
|
165
|
-
expect(decompressed).toEqual(input)
|
|
166
|
-
})
|
|
167
|
-
|
|
168
|
-
test("returns proper Uint8Array<ArrayBuffer> instances", async () => {
|
|
169
|
-
const input = new TextEncoder().encode("type safety check")
|
|
170
|
-
const compressor = new CompressionStream("deflate")
|
|
171
|
-
|
|
172
|
-
const writer = compressor.writable.getWriter()
|
|
173
|
-
writer.write(input)
|
|
174
|
-
writer.close()
|
|
175
|
-
|
|
176
|
-
const reader = compressor.readable.getReader()
|
|
177
|
-
const { value } = await reader.read()
|
|
178
|
-
|
|
179
|
-
expect(value).toBeDefined()
|
|
180
|
-
if (!value) throw new Error("No value read")
|
|
181
|
-
|
|
182
|
-
// Verify it's a Uint8Array
|
|
183
|
-
expect(value).toBeInstanceOf(Uint8Array)
|
|
184
|
-
// Verify the buffer is an ArrayBuffer (not Buffer or SharedArrayBuffer)
|
|
185
|
-
expect(value.buffer).toBeInstanceOf(ArrayBuffer)
|
|
186
|
-
// Verify it's not a Node.js Buffer
|
|
187
|
-
expect(value.constructor.name).toBe("Uint8Array")
|
|
188
|
-
})
|
|
189
|
-
|
|
190
|
-
test("handles multiple writes", async () => {
|
|
191
|
-
const compressor = new CompressionStream("deflate")
|
|
192
|
-
const writer = compressor.writable.getWriter()
|
|
193
|
-
|
|
194
|
-
// Write multiple chunks
|
|
195
|
-
writer.write(new TextEncoder().encode("first "))
|
|
196
|
-
writer.write(new TextEncoder().encode("second "))
|
|
197
|
-
writer.write(new TextEncoder().encode("third"))
|
|
198
|
-
writer.close()
|
|
199
|
-
|
|
200
|
-
const chunks: Uint8Array[] = []
|
|
201
|
-
const reader = compressor.readable.getReader()
|
|
202
|
-
while (true) {
|
|
203
|
-
const { done, value } = await reader.read()
|
|
204
|
-
if (done) break
|
|
205
|
-
chunks.push(value)
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
const compressed = concatUint8(...chunks)
|
|
209
|
-
const decompressed = await bunDecompress(new Uint8Array(compressed))
|
|
210
|
-
expect(new TextDecoder().decode(decompressed)).toBe("first second third")
|
|
211
|
-
})
|
|
212
|
-
})
|
|
213
|
-
|
|
214
|
-
describe.skip("DecompressionStream polyfill", () => {
|
|
215
|
-
test("decompresses deflate data", async () => {
|
|
216
|
-
const input = new TextEncoder().encode(
|
|
217
|
-
"test decompression stream",
|
|
218
|
-
) as Uint8Array<ArrayBuffer>
|
|
219
|
-
const compressed = await bunCompress(input)
|
|
220
|
-
|
|
221
|
-
const decompressor = new DecompressionStream("deflate")
|
|
222
|
-
const writer = decompressor.writable.getWriter()
|
|
223
|
-
writer.write(compressed as Uint8Array<ArrayBuffer>)
|
|
224
|
-
writer.close()
|
|
225
|
-
|
|
226
|
-
const chunks: Uint8Array[] = []
|
|
227
|
-
const reader = decompressor.readable.getReader()
|
|
228
|
-
while (true) {
|
|
229
|
-
const { done, value } = await reader.read()
|
|
230
|
-
if (done) break
|
|
231
|
-
chunks.push(value)
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
const decompressed = concatUint8(...chunks)
|
|
235
|
-
expect(decompressed).toEqual(input)
|
|
236
|
-
})
|
|
237
|
-
|
|
238
|
-
test("returns proper Uint8Array<ArrayBuffer> instances", async () => {
|
|
239
|
-
const input = new TextEncoder().encode(
|
|
240
|
-
"type safety check",
|
|
241
|
-
) as Uint8Array<ArrayBuffer>
|
|
242
|
-
const compressed = await bunCompress(input)
|
|
243
|
-
|
|
244
|
-
const decompressor = new DecompressionStream("deflate")
|
|
245
|
-
const writer = decompressor.writable.getWriter()
|
|
246
|
-
writer.write(compressed as Uint8Array<ArrayBuffer>)
|
|
247
|
-
writer.close()
|
|
248
|
-
|
|
249
|
-
const reader = decompressor.readable.getReader()
|
|
250
|
-
const { value } = await reader.read()
|
|
251
|
-
|
|
252
|
-
expect(value).toBeDefined()
|
|
253
|
-
if (!value) throw new Error("No value read")
|
|
254
|
-
|
|
255
|
-
// Verify it's a Uint8Array
|
|
256
|
-
expect(value).toBeInstanceOf(Uint8Array)
|
|
257
|
-
// Verify the buffer is an ArrayBuffer (not Buffer or SharedArrayBuffer)
|
|
258
|
-
expect(value.buffer).toBeInstanceOf(ArrayBuffer)
|
|
259
|
-
// Verify it's not a Node.js Buffer
|
|
260
|
-
expect(value.constructor.name).toBe("Uint8Array")
|
|
261
|
-
})
|
|
262
|
-
|
|
263
|
-
test("handles chunked compressed data", async () => {
|
|
264
|
-
const input = new TextEncoder().encode(
|
|
265
|
-
"test chunked data",
|
|
266
|
-
) as Uint8Array<ArrayBuffer>
|
|
267
|
-
const compressed = await bunCompress(input)
|
|
268
|
-
|
|
269
|
-
const decompressor = new DecompressionStream("deflate")
|
|
270
|
-
const writer = decompressor.writable.getWriter()
|
|
271
|
-
|
|
272
|
-
// Write compressed data in chunks
|
|
273
|
-
const chunkSize = 5
|
|
274
|
-
for (let i = 0; i < compressed.length; i += chunkSize) {
|
|
275
|
-
const chunk = compressed.slice(i, i + chunkSize)
|
|
276
|
-
writer.write(chunk)
|
|
277
|
-
}
|
|
278
|
-
writer.close()
|
|
279
|
-
|
|
280
|
-
const chunks: Uint8Array[] = []
|
|
281
|
-
const reader = decompressor.readable.getReader()
|
|
282
|
-
while (true) {
|
|
283
|
-
const { done, value } = await reader.read()
|
|
284
|
-
if (done) break
|
|
285
|
-
chunks.push(value)
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
const decompressed = concatUint8(...chunks)
|
|
289
|
-
expect(decompressed).toEqual(input)
|
|
290
|
-
})
|
|
291
|
-
|
|
292
|
-
test("round-trip compression and decompression", async () => {
|
|
293
|
-
const input = new TextEncoder().encode("round trip test data")
|
|
294
|
-
|
|
295
|
-
// Compress
|
|
296
|
-
const compressor = new CompressionStream("deflate")
|
|
297
|
-
const compressorWriter = compressor.writable.getWriter()
|
|
298
|
-
compressorWriter.write(input)
|
|
299
|
-
compressorWriter.close()
|
|
300
|
-
|
|
301
|
-
const compressedChunks: Uint8Array[] = []
|
|
302
|
-
const compressorReader = compressor.readable.getReader()
|
|
303
|
-
while (true) {
|
|
304
|
-
const { done, value } = await compressorReader.read()
|
|
305
|
-
if (done) break
|
|
306
|
-
compressedChunks.push(value)
|
|
307
|
-
}
|
|
308
|
-
const compressed = concatUint8(...compressedChunks)
|
|
309
|
-
|
|
310
|
-
// Decompress
|
|
311
|
-
const decompressor = new DecompressionStream("deflate")
|
|
312
|
-
const decompressorWriter = decompressor.writable.getWriter()
|
|
313
|
-
decompressorWriter.write(new Uint8Array(compressed))
|
|
314
|
-
decompressorWriter.close()
|
|
315
|
-
|
|
316
|
-
const decompressedChunks: Uint8Array[] = []
|
|
317
|
-
const decompressorReader = decompressor.readable.getReader()
|
|
318
|
-
while (true) {
|
|
319
|
-
const { done, value } = await decompressorReader.read()
|
|
320
|
-
if (done) break
|
|
321
|
-
decompressedChunks.push(value)
|
|
322
|
-
}
|
|
323
|
-
const decompressed = concatUint8(...decompressedChunks)
|
|
324
|
-
|
|
325
|
-
expect(decompressed).toEqual(input)
|
|
326
|
-
})
|
|
327
|
-
})
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { getFixtureFile } from "@osmix/shared/test/fixtures"
|
|
2
|
-
import { describe, expect, test } from "vitest"
|
|
3
|
-
import { readOsmPbf } from "../src/pbf-to-blocks"
|
|
4
|
-
import { isBun } from "../src/utils"
|
|
5
|
-
|
|
6
|
-
describe.runIf(isBun())("PBF Reading with Bun", () => {
|
|
7
|
-
test("can read an actual OSM PBF file", async () => {
|
|
8
|
-
// Load a real PBF fixture
|
|
9
|
-
const pbfData = await getFixtureFile("monaco.pbf")
|
|
10
|
-
|
|
11
|
-
// Try to read it
|
|
12
|
-
const result = await readOsmPbf(pbfData)
|
|
13
|
-
|
|
14
|
-
// Basic sanity checks
|
|
15
|
-
expect(result.header).toBeDefined()
|
|
16
|
-
expect(result.header.required_features).toBeDefined()
|
|
17
|
-
expect(result.blocks).toBeDefined()
|
|
18
|
-
|
|
19
|
-
// Try to read at least one block
|
|
20
|
-
const firstBlock = await result.blocks.next()
|
|
21
|
-
expect(firstBlock.done).toBe(false)
|
|
22
|
-
expect(firstBlock.value).toBeDefined()
|
|
23
|
-
|
|
24
|
-
// If it's a primitive block, it should have primitivegroup
|
|
25
|
-
if ("primitivegroup" in firstBlock.value) {
|
|
26
|
-
expect(Array.isArray(firstBlock.value.primitivegroup)).toBe(true)
|
|
27
|
-
}
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
test("header contains expected OSM data", async () => {
|
|
31
|
-
const pbfData = await getFixtureFile("monaco.pbf")
|
|
32
|
-
|
|
33
|
-
const result = await readOsmPbf(pbfData)
|
|
34
|
-
|
|
35
|
-
// Check that header has the required OSM features
|
|
36
|
-
expect(result.header.required_features).toBeDefined()
|
|
37
|
-
expect(result.header.required_features.length).toBeGreaterThan(0)
|
|
38
|
-
})
|
|
39
|
-
})
|