Skip to content

Commit 7e35334

Browse files
committed
remove node dependency
1 parent 3ba7781 commit 7e35334

File tree

6 files changed

+92
-126
lines changed

6 files changed

+92
-126
lines changed

README.ja.md

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -183,31 +183,24 @@ const inspected = inspectTile(encoded.bytes);
183183
const decoded = await decodeTile(encoded.bytes);
184184
```
185185

186-
CLI 出力相当の高レベルヘルパーを使う場合:
186+
高レベルヘルパーを使う場合(コアライブラリはランタイム非依存で、ファイル I/O は呼び出し側で実施):
187187

188188
```ts
189189
import {
190190
decodeTile,
191191
decodeTileToCsv,
192-
decodeTileFileToCsv,
193-
encodeTileToFile,
194-
inspectTileFile,
195-
inspectTileToText,
192+
encodeTile,
193+
inspectTile,
196194
} from 'mesh-data-tile';
197-
import { readFile } from 'node:fs/promises';
198-
199-
const inspectResult = await inspectTileFile('in.tile');
200-
console.log(inspectResult.text);
201-
202-
const { csv } = await decodeTileFileToCsv('in.tile');
203-
console.log(csv);
195+
import { readFile, writeFile } from 'node:fs/promises';
204196

205197
const bytes = new Uint8Array(await readFile('in.tile'));
206-
const fromBytes = inspectTileToText(bytes);
207-
const csvFromBytes = await decodeTileToCsv(bytes);
198+
const inspected = inspectTile(bytes);
199+
const { csv } = await decodeTileToCsv(bytes);
208200
const decoded = await decodeTile(bytes);
201+
console.log(inspected.header.tile_id, csv);
209202

210-
await encodeTileToFile('out.tile', {
203+
const encoded = await encodeTile({
211204
tile_id: 99n,
212205
mesh_kind: 'jis-x0410',
213206
rows: 2,
@@ -217,8 +210,11 @@ await encodeTileToFile('out.tile', {
217210
endianness: 'little',
218211
data: [1, 2, 3, 4],
219212
});
213+
await writeFile('out.tile', encoded.bytes);
220214
```
221215

216+
CLI と同等の inspect テキスト出力やファイルパス直接処理は `mesh-data-tile-cli` を利用してください。
217+
222218
## MapLibre addProtocol 対応
223219

224220
```ts

README.md

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -183,31 +183,24 @@ const inspected = inspectTile(encoded.bytes);
183183
const decoded = await decodeTile(encoded.bytes);
184184
```
185185

186-
Use high-level helpers equivalent to CLI output:
186+
Use high-level helpers (core library is runtime-neutral; file I/O is caller-managed):
187187

188188
```ts
189189
import {
190190
decodeTile,
191191
decodeTileToCsv,
192-
decodeTileFileToCsv,
193-
encodeTileToFile,
194-
inspectTileFile,
195-
inspectTileToText,
192+
encodeTile,
193+
inspectTile,
196194
} from 'mesh-data-tile';
197-
import { readFile } from 'node:fs/promises';
198-
199-
const inspectResult = await inspectTileFile('in.tile');
200-
console.log(inspectResult.text);
201-
202-
const { csv } = await decodeTileFileToCsv('in.tile');
203-
console.log(csv);
195+
import { readFile, writeFile } from 'node:fs/promises';
204196

205197
const bytes = new Uint8Array(await readFile('in.tile'));
206-
const fromBytes = inspectTileToText(bytes);
207-
const csvFromBytes = await decodeTileToCsv(bytes);
198+
const inspected = inspectTile(bytes);
199+
const { csv } = await decodeTileToCsv(bytes);
208200
const decoded = await decodeTile(bytes);
201+
console.log(inspected.header.tile_id, csv);
209202

210-
await encodeTileToFile('out.tile', {
203+
const encoded = await encodeTile({
211204
tile_id: 99n,
212205
mesh_kind: 'jis-x0410',
213206
rows: 2,
@@ -217,8 +210,11 @@ await encodeTileToFile('out.tile', {
217210
endianness: 'little',
218211
data: [1, 2, 3, 4],
219212
});
213+
await writeFile('out.tile', encoded.bytes);
220214
```
221215

216+
For CLI-style inspect text output and file-path based operations, use `mesh-data-tile-cli`.
217+
222218
## MapLibre addProtocol support
223219

224220
```ts

packages/mesh-data-tile-cli/src/cli.ts

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import { readFile, writeFile } from 'node:fs/promises';
33
import { Command } from 'commander';
44
import {
55
decodeTileToCsv,
6-
encodeTileToFile,
7-
inspectTileToText,
6+
decodeXyzTileId,
7+
encodeTile,
8+
inspectTile,
89
TileFormatError,
10+
type InspectTileResult,
911
type TileEncodeInput,
1012
type DType,
1113
type Endianness,
@@ -38,6 +40,11 @@ interface EncodeCommandOptions {
3840
noData?: string;
3941
}
4042

43+
interface InspectTileTextResult {
44+
info: InspectTileResult;
45+
text: string;
46+
}
47+
4148
function fail(message: string, code = 1): never {
4249
console.error(`error: ${message}`);
4350
process.exit(code);
@@ -114,15 +121,65 @@ async function writeOutput(path: string | undefined, content: Uint8Array | strin
114121
await writeFile(path, content);
115122
}
116123

124+
function formatInspectOutput(info: InspectTileResult): string {
125+
const lines = [
126+
`Format Major: ${info.header.format_major}`,
127+
`Tile ID: ${info.header.tile_id.toString()}`,
128+
`Mesh Kind: ${info.header.mesh_kind}`,
129+
`DType: ${info.header.dtype}`,
130+
`Endianness: ${info.header.endianness}`,
131+
`Compression: ${info.header.compression}`,
132+
`Rows: ${info.header.dimensions.rows}`,
133+
`Cols: ${info.header.dimensions.cols}`,
134+
`Bands: ${info.header.dimensions.bands}`,
135+
`NoData: ${info.header.no_data === null ? 'null' : String(info.header.no_data)}`,
136+
`Uncompressed Payload Bytes: ${info.header.payload.uncompressed_bytes}`,
137+
`Compressed Payload Bytes: ${info.header.payload.compressed_bytes}`,
138+
`Payload CRC32: ${info.header.checksum.payload_crc32}`,
139+
`Header CRC32: ${info.header.checksum.header_crc32}`,
140+
`Header Length: ${info.header_length}`,
141+
`Payload Offset: ${info.payload_offset}`,
142+
`Payload Length: ${info.payload_length}`,
143+
];
144+
145+
if (info.header.mesh_kind === 'xyz') {
146+
const xyz = decodeXyzTileId(info.header.tile_id);
147+
lines.push(`XYZ Zoom: ${xyz.zoom}`);
148+
lines.push(`XYZ X: ${xyz.x}`);
149+
lines.push(`XYZ Y: ${xyz.y}`);
150+
lines.push(`XYZ Quadkey Integer: ${xyz.quadkey_integer.toString()}`);
151+
}
152+
153+
return lines.join('\n');
154+
}
155+
156+
function inspectTileToText(bytes: Uint8Array): InspectTileTextResult {
157+
const info = inspectTile(bytes);
158+
return {
159+
info,
160+
text: formatInspectOutput(info),
161+
};
162+
}
163+
164+
async function decodeTileFileToCsv(path: string) {
165+
const bytes = new Uint8Array(await readFile(path));
166+
return decodeTileToCsv(bytes);
167+
}
168+
169+
async function encodeTileToFile(path: string, input: TileEncodeInput) {
170+
const encoded = await encodeTile(input);
171+
await writeFile(path, encoded.bytes);
172+
return encoded;
173+
}
174+
117175
async function runInspect(inputPath: string): Promise<void> {
118176
const input = new Uint8Array(await readFile(inputPath));
119177
const result = inspectTileToText(input);
120178
console.log(result.text);
121179
}
122180

123181
async function runDecode(inputPath: string, options: DecodeCommandOptions): Promise<void> {
124-
const input = new Uint8Array(await readFile(inputPath));
125-
const result = await decodeTileToCsv(input);
182+
const result = await decodeTileFileToCsv(inputPath);
126183
await writeOutput(options.output, result.csv);
127184
}
128185

src/api.ts

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,12 @@
1-
import { readFile, writeFile } from 'node:fs/promises';
21
import { createError } from './errors.js';
3-
import { decodeTile, encodeTile, inspectTile } from './tile-format.js';
4-
import { decodeXyzTileId } from './tile-id.js';
5-
import type { EncodeResult } from './tile-format.js';
6-
import type { DecodedTile, InspectTileResult, TileEncodeInput } from './types.js';
7-
8-
export interface InspectTileTextResult {
9-
info: InspectTileResult;
10-
text: string;
11-
}
2+
import { decodeTile } from './tile-format.js';
3+
import type { DecodedTile } from './types.js';
124

135
export interface DecodeTileCsvResult {
146
decoded: DecodedTile;
157
csv: string;
168
}
179

18-
export function formatInspectOutput(info: InspectTileResult): string {
19-
const lines = [
20-
`Format Major: ${info.header.format_major}`,
21-
`Tile ID: ${info.header.tile_id.toString()}`,
22-
`Mesh Kind: ${info.header.mesh_kind}`,
23-
`DType: ${info.header.dtype}`,
24-
`Endianness: ${info.header.endianness}`,
25-
`Compression: ${info.header.compression}`,
26-
`Rows: ${info.header.dimensions.rows}`,
27-
`Cols: ${info.header.dimensions.cols}`,
28-
`Bands: ${info.header.dimensions.bands}`,
29-
`NoData: ${info.header.no_data === null ? 'null' : String(info.header.no_data)}`,
30-
`Uncompressed Payload Bytes: ${info.header.payload.uncompressed_bytes}`,
31-
`Compressed Payload Bytes: ${info.header.payload.compressed_bytes}`,
32-
`Payload CRC32: ${info.header.checksum.payload_crc32}`,
33-
`Header CRC32: ${info.header.checksum.header_crc32}`,
34-
`Header Length: ${info.header_length}`,
35-
`Payload Offset: ${info.payload_offset}`,
36-
`Payload Length: ${info.payload_length}`,
37-
];
38-
39-
if (info.header.mesh_kind === 'xyz') {
40-
const xyz = decodeXyzTileId(info.header.tile_id);
41-
lines.push(`XYZ Zoom: ${xyz.zoom}`);
42-
lines.push(`XYZ X: ${xyz.x}`);
43-
lines.push(`XYZ Y: ${xyz.y}`);
44-
lines.push(`XYZ Quadkey Integer: ${xyz.quadkey_integer.toString()}`);
45-
}
46-
47-
return lines.join('\n');
48-
}
49-
5010
export function formatDecodedCsv(values: ArrayLike<number>, rows: number, cols: number, bands: number): string {
5111
const expected = rows * cols * bands;
5212
if (values.length !== expected) {
@@ -72,14 +32,6 @@ export function formatDecodedCsv(values: ArrayLike<number>, rows: number, cols:
7232
return lines.join('\n');
7333
}
7434

75-
export function inspectTileToText(bytes: Uint8Array): InspectTileTextResult {
76-
const info = inspectTile(bytes);
77-
return {
78-
info,
79-
text: formatInspectOutput(info),
80-
};
81-
}
82-
8335
export async function decodeTileToCsv(bytes: Uint8Array): Promise<DecodeTileCsvResult> {
8436
const decoded = await decodeTile(bytes);
8537
const { rows, cols, bands } = decoded.header.dimensions;
@@ -88,19 +40,3 @@ export async function decodeTileToCsv(bytes: Uint8Array): Promise<DecodeTileCsvR
8840
csv: formatDecodedCsv(decoded.data, rows, cols, bands),
8941
};
9042
}
91-
92-
export async function inspectTileFile(path: string): Promise<InspectTileTextResult> {
93-
const bytes = new Uint8Array(await readFile(path));
94-
return inspectTileToText(bytes);
95-
}
96-
97-
export async function decodeTileFileToCsv(path: string): Promise<DecodeTileCsvResult> {
98-
const bytes = new Uint8Array(await readFile(path));
99-
return decodeTileToCsv(bytes);
100-
}
101-
102-
export async function encodeTileToFile(path: string, input: TileEncodeInput): Promise<EncodeResult> {
103-
const encoded = await encodeTile(input);
104-
await writeFile(path, encoded.bytes);
105-
return encoded;
106-
}

src/index.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,8 @@ export {
3939
renderMeshTileUrlTemplate,
4040
} from './maplibre-source.js';
4141
export {
42-
formatInspectOutput,
4342
formatDecodedCsv,
44-
inspectTileToText,
4543
decodeTileToCsv,
46-
inspectTileFile,
47-
decodeTileFileToCsv,
48-
encodeTileToFile,
4944
} from './api.js';
5045
export {
5146
encodeTile,

test/conformance.test.ts

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,12 @@ import { join } from 'node:path';
66
import { tmpdir } from 'node:os';
77
import {
88
decodeTile,
9-
decodeTileFileToCsv,
109
decodeTileToCsv,
1110
decodeXyzTileId,
1211
encodeTile,
13-
encodeTileToFile,
1412
encodeXyzTileId,
1513
formatDecodedCsv,
16-
formatInspectOutput,
1714
inspectTile,
18-
inspectTileFile,
19-
inspectTileToText,
2015
} from '../src/index.js';
2116
import { TileFormatError } from '../src/errors.js';
2217

@@ -346,33 +341,24 @@ describe('mesh tile v1 conformance', () => {
346341
);
347342
});
348343

349-
it('library api inspect text includes xyz coordinates', async () => {
350-
const input = new Uint8Array(await fs.readFile(join(fixturesDir, 'xyz-uncompressed.tile')));
351-
const result = inspectTileToText(input);
352-
const fromFormatter = formatInspectOutput(inspectTile(input));
353-
354-
assert.equal(result.text, fromFormatter);
355-
assert.match(result.text, /XYZ Zoom: 12/);
356-
assert.match(result.text, /XYZ X: 3639/);
357-
assert.match(result.text, /XYZ Y: 1612/);
358-
});
359-
360-
it('library api file helpers roundtrip', async () => {
344+
it('library api roundtrip with caller-managed file io', async () => {
361345
const tempDir = await fs.mkdtemp(join(tmpdir(), 'mesh-data-tile-'));
362346
const outputPath = join(tempDir, 'api-roundtrip.tile');
363347

364348
try {
365-
await encodeTileToFile(outputPath, {
349+
const encoded = await encodeTile({
366350
...tileTemplate(),
367351
tile_id: 2001n,
368352
dtype: 'uint16',
369353
data: [10, 20, 30, 40],
370354
});
355+
await fs.writeFile(outputPath, encoded.bytes);
371356

372-
const inspected = await inspectTileFile(outputPath);
373-
const decoded = await decodeTileFileToCsv(outputPath);
357+
const bytes = new Uint8Array(await fs.readFile(outputPath));
358+
const inspected = inspectTile(bytes);
359+
const decoded = await decodeTileToCsv(bytes);
374360

375-
assert.equal(inspected.info.header.tile_id, 2001n);
361+
assert.equal(inspected.header.tile_id, 2001n);
376362
assert.equal(decoded.csv, ['x,y,b0', '0,0,10', '1,0,20', '0,1,30', '1,1,40'].join('\n'));
377363
} finally {
378364
await fs.rm(tempDir, { recursive: true, force: true });

0 commit comments

Comments
 (0)