Skip to content

Commit

Permalink
Merge pull request #90 from Kanahiro/rmstream
Browse files Browse the repository at this point in the history
rm hono/stream, publish as library
  • Loading branch information
Kanahiro authored Jun 22, 2024
2 parents dc7e9bf + c67a615 commit 6e6134d
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 30 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/npm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Publish Package to npmjs
on:
release:
types: [published]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v4
with:
node-version: '20.x'
registry-url: 'https://registry.npmjs.org'
- run: npm ci
- run: npm publish --provenance --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
{
"type": "module",
"name": "chiitiler",
"version": "1.11.0",
"version": "1.11.1",
"description": "Tiny map rendering server for MapLibre Style Spec",
"main": "dist/main.js",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "tsc",
"dev": "tsx watch src/main.ts tile-server -D",
Expand Down
21 changes: 18 additions & 3 deletions src/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ describe('run chiitiler', () => {
vi.spyOn(server, 'initServer').mockImplementation(
(opts: server.InitServerOptions) => {
options = opts;
return { app: {} as any, start: vi.fn() };
return {
app: {} as any,
tiles: {} as any,
bbox: {} as any,
start: vi.fn(),
};
},
);

Expand All @@ -29,7 +34,12 @@ describe('run chiitiler', () => {
vi.spyOn(server, 'initServer').mockImplementation(
(opts: server.InitServerOptions) => {
options = opts;
return { app: {} as any, start: vi.fn() };
return {
app: {} as any,
tiles: {} as any,
bbox: {} as any,
start: vi.fn(),
};
},
);

Expand All @@ -46,7 +56,12 @@ describe('run chiitiler', () => {
vi.spyOn(server, 'initServer').mockImplementation(
(opts: server.InitServerOptions) => {
options = opts;
return { app: {} as any, start: vi.fn() };
return {
app: {} as any,
tiles: {} as any,
bbox: {} as any,
start: vi.fn(),
};
},
);

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { initServer, type InitServerOptions } from './server/index.js';
12 changes: 4 additions & 8 deletions src/render/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ async function getRenderedBboxBuffer({
cache: Cache;
ext: SupportedFormat;
quality: number;
}): Promise<Sharp> {
}): Promise<Buffer> {
const style = await loadStyle(stylejson, cache);

const { zoom, width, height, center } = calcRenderingParams(bbox, size);
Expand All @@ -199,17 +199,13 @@ async function getRenderedBboxBuffer({
});
switch (ext) {
case 'png':
_sharp = await _sharp.png();
break;
return await _sharp.png().toBuffer();
case 'jpeg':
case 'jpg':
_sharp = await _sharp.jpeg({ quality });
break;
return await _sharp.jpeg({ quality }).toBuffer();
case 'webp':
_sharp = await _sharp.webp({ quality, effort: 0 });
break;
return await _sharp.webp({ quality, effort: 0 }).toBuffer();
}
return _sharp;
}

export { getRenderedTileBuffer, getRenderedBboxBuffer, type SupportedFormat };
21 changes: 6 additions & 15 deletions src/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Hono } from 'hono';
import { stream } from 'hono/streaming';
import { serve } from '@hono/node-server';
import {
type StyleSpecification,
Expand Down Expand Up @@ -133,7 +132,7 @@ function initServer(options: InitServerOptions) {
const size = Number(c.req.query('size') ?? 1024);

try {
const sharp = await getRenderedBboxBuffer({
const buf = await getRenderedBboxBuffer({
stylejson: url,
bbox: [minx, miny, maxx, maxy],
size,
Expand All @@ -142,12 +141,7 @@ function initServer(options: InitServerOptions) {
quality,
});
c.header('Content-Type', `image/${ext}`);
return stream(c, async (stream) => {
for await (const chunk of sharp) {
stream.write(chunk);
}
stream.close();
});
return c.body(buf);
} catch (e) {
console.error(`render error: ${e}`);
return c.body('failed to render bbox', 400);
Expand All @@ -171,7 +165,7 @@ function initServer(options: InitServerOptions) {
const size = Number(c.req.query('size') ?? 1024);

try {
const sharp = await getRenderedBboxBuffer({
const buf = await getRenderedBboxBuffer({
stylejson: style,
bbox: [minx, miny, maxx, maxy],
size,
Expand All @@ -180,12 +174,7 @@ function initServer(options: InitServerOptions) {
quality,
});
c.header('Content-Type', `image/${ext}`);
return stream(c, async (stream) => {
for await (const chunk of sharp) {
stream.write(chunk);
}
stream.close();
});
return c.body(buf);
} catch (e) {
console.error(`render error: ${e}`);
return c.body('failed to render bbox', 400);
Expand All @@ -203,6 +192,8 @@ function initServer(options: InitServerOptions) {

return {
app: hono,
tiles,
bbox,
start: () => serve({ port: options.port, fetch: hono.fetch }),
};
}
Expand Down
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"moduleResolution": "NodeNext",
"outDir": "./dist",
"resolveJsonModule": true,
"esModuleInterop": true
"esModuleInterop": true,
"declaration": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "build", "dist"]
"exclude": ["node_modules", "build", "dist", "**/*.test.ts"]
}

0 comments on commit 6e6134d

Please sign in to comment.