@fastly/hono-compute-js-static-publish 0.4.0

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 ADDED
@@ -0,0 +1,46 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [unreleased]
9
+
10
+ ## [0.4.0] - 2024-10-06
11
+
12
+ ### Changed
13
+
14
+ - BREAKING: Moved to @fastly scope
15
+
16
+ ## [0.3.0] - 2024-08-30
17
+
18
+ ### Added
19
+
20
+ - Update for @fastly/compute-js-static-publish v7
21
+
22
+ ## [0.2.0] - 2024-04-26
23
+
24
+ ### Added
25
+
26
+ - Added files field to npm package
27
+ - Added keywords field to npm package
28
+
29
+ ## [0.1.1] - 2024-04-26
30
+
31
+ ### Added
32
+
33
+ - Added repository field to npm package
34
+
35
+ ## [0.1.0] - 2024-04-26
36
+
37
+ ### Added
38
+
39
+ - Initial public release
40
+
41
+ [unreleased]: https://github.com/harmony7/hono-compute-js-static-publish/compare/v0.4.0...HEAD
42
+ [0.4.0]: https://github.com/harmony7/hono-compute-js-static-publish/compare/v0.1.1...v0.4.0
43
+ [0.3.0]: https://github.com/harmony7/hono-compute-js-static-publish/compare/v0.1.1...v0.3.0
44
+ [0.2.0]: https://github.com/harmony7/hono-compute-js-static-publish/compare/v0.1.1...v0.2.0
45
+ [0.1.1]: https://github.com/harmony7/hono-compute-js-static-publish/compare/v0.1.0...v0.1.1
46
+ [0.1.0]: https://github.com/harmony7/hono-compute-js-static-publish/releases/tag/v0.1.0
package/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ MIT License
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,185 @@
1
+ # Compute Static Publisher for Hono
2
+
3
+ `@fastly/hono-compute-js-static-publish` is a utility for [Hono](https://hono.dev) to allow your program to serve static files under [Fastly Compute](https://www.fastly.com/products/edge-compute). It leverages the `PublisherServer` from [`@fastly/compute-js-static-publish`](https://www.npmjs.com/package/@fastly/compute-js-static-publish).
4
+
5
+ Works with:
6
+ * Hono v4.x
7
+ * [@fastly/compute-js-static-publish](https://www.npmjs.com/package/@fastly/compute-js-static-publish) v6.x and v7.x
8
+
9
+ ## Example
10
+
11
+ ### compute-js-static-publish v7
12
+
13
+ ```typescript
14
+ import { Hono } from 'hono/quick'
15
+ import { fire } from 'hono/service-worker'
16
+
17
+ import { serveStatic } from '@fastly/hono-compute-js-static-publish'
18
+ import { PublisherServer } from '@fastly/compute-js-static-publish'
19
+ import rc from '../static-publish.rc.js'
20
+ const staticPublisherServer = PublisherServer.fromStaticPublishRc(rc)
21
+
22
+ const app = new Hono()
23
+
24
+ app.get('/', (c) => {
25
+ return c.text('Hello Hono!')
26
+ })
27
+
28
+ app.get('/favicon.ico', serveStatic({ path: './favicon.ico', staticPublisherServer }))
29
+ app.get('/static/*', serveStatic({ root: './', staticPublisherServer }))
30
+
31
+ fire(app)
32
+ ```
33
+
34
+ ### compute-js-static-publish v6
35
+
36
+ ```typescript
37
+ import { Hono } from 'hono/quick'
38
+ import { fire } from 'hono/service-worker'
39
+
40
+ import { serveStatic } from '@fastly/hono-compute-js-static-publish'
41
+ import { getServer } from '../static-publisher/statics.js'
42
+
43
+ const app = new Hono()
44
+
45
+ app.get('/', (c) => {
46
+ return c.text('Hello Hono!')
47
+ });
48
+
49
+ const staticPublisherServer = getServer()
50
+ app.get('/favicon.ico', serveStatic({ path: './favicon.ico', staticPublisherServer }))
51
+ app.get('/static/*', serveStatic({ root: './', staticPublisherServer }))
52
+
53
+ fire(app)
54
+ ```
55
+
56
+ ## Usage
57
+
58
+ > [!NOTE]
59
+ > The instructions below are for @fastly/compute-js-static-publish v7.
60
+
61
+ Once you have [set up a Hono project for Fastly Compute](https://hono.dev/getting-started/fastly), add this Adapter, the Fastly CLI, and the Static Publisher library to your application:
62
+
63
+ ```
64
+ npm install @fastly/hono-compute-js-static-publish @fastly/cli @fastly/compute-js-static-publish
65
+ ```
66
+
67
+ These directions assume your static files exist directly under the `./assets` subdirectory of your Fastly Compute application.
68
+
69
+ You will need to add and modify some files at the root of your project.
70
+
71
+ * Add `static-publish.rc.js`
72
+ ```javascript
73
+ /** @type {import('@fastly/compute-js-static-publish').StaticPublishRc} */
74
+ const rc = {
75
+ kvStoreName: "site-content",
76
+ publishId: "default",
77
+ defaultCollectionName: "live",
78
+ staticPublisherWorkingDir: "./static-publisher",
79
+ };
80
+
81
+ export default rc;
82
+ ```
83
+ This sets up the Static Publisher to use a KV Store named `site-content` and to place generated temporary files in the `./static-publisher` subdirectory. The other fields are not used in `@fastly/hono-compute-js-static-publish` but are required fields.
84
+
85
+ > [!TIP]
86
+ > Add `./static-publisher/` to your `.gitignore` as these files will be regenerated on the build.
87
+
88
+ * Add `publish-content.config.js`
89
+ ```javascript
90
+ /** @type {import('@fastly/compute-js-static-publish').PublishContentConfig} */
91
+ const config = {
92
+ rootDir: './assets'
93
+ };
94
+
95
+ export default config;
96
+ ```
97
+
98
+ This instructs the Static Publisher to find static files at `./assets`.
99
+
100
+ > [!IMPORTANT]
101
+ > As this application does not use `PublisherServer` to directly serve assets, the `server` field is not applicable.
102
+
103
+ * Modify `package.json`
104
+ Add the following items under the `scripts` section:
105
+ ```json
106
+ {
107
+ "scripts": {
108
+ "dev:publish": "npx @fastly/compute-js-static-publish publish-content --local",
109
+ "dev:start": "fastly compute serve",
110
+ "fastly:deploy": "fastly compute publish",
111
+ "fastly:publish": "npx @fastly/compute-js-static-publish publish-content"
112
+ }
113
+ }
114
+ ```
115
+
116
+ * Modify `fastly.toml`
117
+ Add the following lines to the end of the file:
118
+ ```toml
119
+ [local_server.kv_stores]
120
+ site-content = { file = "./static-publisher/kvstore.json", format = "json" }
121
+
122
+ [setup.kv_stores.site-content]
123
+ ```
124
+
125
+ Finally, modify your application's `src/index.ts` file:
126
+ 1. Import `serveStatic` from this library and `PublisherServer` from `@fastly/compute-js-static-publish`
127
+ 2. Import the default constant from `static-publish.rc.js` and pass it to `PublisherServer.fromStaticPublishRc()` to obtain an instance of the Publisher Server.
128
+ 3. Call `serveStatic` to instantiate [Hono Middleware](https://hono.dev/guides/middleware), passing in the `PublisherServer` instance obtained above.
129
+
130
+ ```typescript
131
+ import { Hono } from 'hono/quick'
132
+ import { fire } from 'hono/service-worker'
133
+
134
+ import { serveStatic } from '@fastly/hono-compute-js-static-publish'
135
+ import { PublisherServer } from '@fastly/compute-js-static-publish'
136
+ import rc from '../static-publish.rc.js'
137
+ const staticPublisherServer = PublisherServer.fromStaticPublishRc(rc)
138
+
139
+ const app = new Hono()
140
+
141
+ app.get('/', (c) => {
142
+ return c.text('Hello Hono!')
143
+ })
144
+
145
+ app.get('/favicon.ico', serveStatic({ path: './favicon.ico', staticPublisherServer }))
146
+ app.get('/static/*', serveStatic({ root: './', staticPublisherServer }))
147
+
148
+ fire(app)
149
+ ```
150
+
151
+ Include either `path` or `root` in the object passed to `serveStatic()`.
152
+ * `path` - serves the static file at the specified path under the asset directory.
153
+ * e.g., a request for `/favicon.ico` serves `./assets/favicon.ico`.
154
+ * `root` - serves the file at the path under the asset directory relative to the specified directory.
155
+ * e.g., a request for `/static/foo.html` serves `./assets/static/foo.html`.
156
+
157
+ ### Run locally
158
+
159
+ ```sh
160
+ npm run dev:publish # 'publish' your files to the simulated local KV Store
161
+ npm run dev:start # preview locally
162
+ ```
163
+
164
+ Serves your app at `http://127.0.0.1:7676`, powered by a simulated KV Store.
165
+
166
+ ### Deploy to Fastly Compute
167
+
168
+ [Create a free Fastly account](https://www.fastly.com/signup/?tier=free) if you haven't already, and then:
169
+
170
+ ```sh
171
+ npm run fastly:deploy # deploy the app
172
+ npm run fastly:publish # upload your static files
173
+ ```
174
+
175
+ Further changes involving only updates to your static files can be made by running:
176
+ ```sh
177
+ npm run fastly:publish # upload your static files
178
+ ```
179
+
180
+ 📘 For more on Compute-JS Static Publisher:
181
+ [https://github.com/fastly/compute-js-static-publish](https://github.com/fastly/compute-js-static-publish)
182
+
183
+ ## License
184
+
185
+ [MIT](./LICENSE).
@@ -0,0 +1,8 @@
1
+ import { type PublisherServer } from '@fastly/compute-js-static-publish';
2
+ export type ServeStaticOptions = {
3
+ path?: string;
4
+ root?: string;
5
+ staticPublisherServer: PublisherServer;
6
+ };
7
+ export declare function serveStatic(options: ServeStaticOptions): import("hono/types").MiddlewareHandler<any, string, {}>;
8
+ //# sourceMappingURL=index.d.ts.map
package/build/index.js ADDED
@@ -0,0 +1,23 @@
1
+ import { createMiddleware } from 'hono/factory';
2
+ export function serveStatic(options) {
3
+ const server = options.staticPublisherServer;
4
+ let root = options.root ?? './';
5
+ if (!root.endsWith('/')) {
6
+ root += root;
7
+ }
8
+ const base = new URL(root, 'https://www.example.com/');
9
+ return createMiddleware(async (c, next) => {
10
+ let reqPath = options.path ?? c.req.path;
11
+ if (reqPath.startsWith('/')) {
12
+ reqPath = '.' + reqPath;
13
+ }
14
+ const pathname = new URL(reqPath, base).pathname;
15
+ const asset = await server.getMatchingAsset(pathname);
16
+ if (asset == null) {
17
+ await next();
18
+ return;
19
+ }
20
+ c.res = undefined;
21
+ c.res = await server.serveAsset(c.req.raw, asset, { cache: 'never' });
22
+ });
23
+ }
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@fastly/hono-compute-js-static-publish",
3
+ "version": "0.4.0",
4
+ "description": "Serve files from Fastly Compute Static Publisher on Hono",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/fastly/hono-compute-js-static-publish.git"
9
+ },
10
+ "author": {
11
+ "name": "Katsuyuki Omuro",
12
+ "email": "komuro@fastly.com"
13
+ },
14
+ "type": "module",
15
+ "main": "./build/index.js",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./build/index.d.ts",
19
+ "import": "./build/index.js"
20
+ }
21
+ },
22
+ "types": "./build/index.d.ts",
23
+ "scripts": {
24
+ "prepack": "npm run build",
25
+ "build": "tsc -p tsconfig.build.json",
26
+ "clean": "rm -rf build"
27
+ },
28
+ "devDependencies": {
29
+ "@fastly/compute-js-static-publish": "^7.0.0",
30
+ "hono": "^4.9.5",
31
+ "typescript": "^5.4.5"
32
+ },
33
+ "peerDependencies": {
34
+ "@fastly/compute-js-static-publish": "^6.0.0 || ^7.0.0",
35
+ "hono": "^4.0.0"
36
+ },
37
+ "files": [
38
+ "build/**/*.js",
39
+ "build/**/*.js.map",
40
+ "build/**/*.d.ts",
41
+ "LICENSE",
42
+ "README.md",
43
+ "CHANGELOG.md"
44
+ ],
45
+ "keywords": [
46
+ "hono",
47
+ "fastly",
48
+ "compute",
49
+ "static",
50
+ "serve"
51
+ ]
52
+ }