@fastly/hono-fastly-compute 0.3.1 → 0.3.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 +20 -1
- package/README.md +59 -7
- package/build/conninfo.d.ts +3 -0
- package/build/conninfo.js +28 -0
- package/build/fire.d.ts +20 -6
- package/build/fire.js +16 -3
- package/build/handler.d.ts +5 -4
- package/build/handler.js +10 -3
- package/build/index.d.ts +1 -0
- package/build/index.js +5 -0
- package/build/utils.js +4 -0
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.3.3] - 2025-10-15
|
|
11
|
+
|
|
12
|
+
### Changed
|
|
13
|
+
|
|
14
|
+
- Change debugging definition field to `_defs`
|
|
15
|
+
|
|
16
|
+
### Added
|
|
17
|
+
|
|
18
|
+
- Expose default `fire` and `Bindings`
|
|
19
|
+
|
|
20
|
+
## [0.3.2] - 2025-10-14
|
|
21
|
+
|
|
22
|
+
### Added
|
|
23
|
+
|
|
24
|
+
- Extend env with client and server info
|
|
25
|
+
- Implement ConnInfo helpers
|
|
26
|
+
|
|
10
27
|
## [0.3.1] - 2025-10-08
|
|
11
28
|
|
|
12
29
|
### Fixed
|
|
@@ -25,7 +42,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
25
42
|
|
|
26
43
|
- Initial public release
|
|
27
44
|
|
|
28
|
-
[unreleased]: https://github.com/fastly/hono-fastly-compute/compare/v0.3.
|
|
45
|
+
[unreleased]: https://github.com/fastly/hono-fastly-compute/compare/v0.3.3...HEAD
|
|
46
|
+
[0.3.3]: https://github.com/fastly/hono-fastly-compute/compare/v0.3.2...v0.3.3
|
|
47
|
+
[0.3.2]: https://github.com/fastly/hono-fastly-compute/compare/v0.3.1...v0.3.2
|
|
29
48
|
[0.3.1]: https://github.com/fastly/hono-fastly-compute/compare/v0.3.0...v0.3.1
|
|
30
49
|
[0.3.0]: https://github.com/fastly/hono-fastly-compute/compare/v0.1.0...v0.3.0
|
|
31
50
|
[0.1.0]: https://github.com/fastly/hono-fastly-compute/releases/tag/v0.1.0
|
package/README.md
CHANGED
|
@@ -35,11 +35,7 @@ const fire = buildFire({
|
|
|
35
35
|
});
|
|
36
36
|
|
|
37
37
|
// Use the inferred Bindings type in your Hono environment
|
|
38
|
-
|
|
39
|
-
Bindings: typeof fire.Bindings;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const app = new Hono<Env>();
|
|
38
|
+
const app = new Hono<{ Bindings: typeof fire.Bindings }>();
|
|
43
39
|
|
|
44
40
|
app.get('/', async (c) => {
|
|
45
41
|
// Access your bindings from the context
|
|
@@ -51,15 +47,33 @@ app.get('/', async (c) => {
|
|
|
51
47
|
fire(app);
|
|
52
48
|
```
|
|
53
49
|
|
|
50
|
+
### Example with no user resources
|
|
51
|
+
|
|
52
|
+
An application that defines no user resources is even simpler:
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
import { Hono } from 'hono';
|
|
56
|
+
import { fire, type Bindings } from '@fastly/hono-fastly-compute';
|
|
57
|
+
|
|
58
|
+
const app = new Hono<{ Bindings: Bindings }>();
|
|
59
|
+
|
|
60
|
+
app.get('/', async (c) => {
|
|
61
|
+
// `clientInfo` and `serverInfo` are always available on `c.env`.
|
|
62
|
+
const clientInfo = c.env.clientInfo;
|
|
63
|
+
c.text(`Accessed from ${clientInfo.address}`);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
fire(app);
|
|
67
|
+
```
|
|
68
|
+
|
|
54
69
|
### Using the `logFastlyServiceVersion` Middleware
|
|
55
70
|
|
|
56
71
|
This package includes a simple middleware to log the `FASTLY_SERVICE_VERSION` for debugging purposes.
|
|
57
72
|
|
|
58
73
|
```typescript
|
|
59
74
|
import { Hono } from 'hono';
|
|
60
|
-
import {
|
|
75
|
+
import { fire, logFastlyServiceVersion } from '@fastly/hono-fastly-compute';
|
|
61
76
|
|
|
62
|
-
const fire = buildFire({});
|
|
63
77
|
const app = new Hono();
|
|
64
78
|
|
|
65
79
|
// Use the middleware
|
|
@@ -95,10 +109,48 @@ The core adapter function that connects Hono to the Fastly Compute `FetchEvent`.
|
|
|
95
109
|
- **`bindingsDefs`**: The environment bindings definition.
|
|
96
110
|
- **`options`**: An optional object with a `fetch` property.
|
|
97
111
|
|
|
112
|
+
### `clientInfo` and `serverInfo`
|
|
113
|
+
|
|
114
|
+
`clientInfo` ([ClientInfo](https://github.com/fastly/js-compute-runtime/blob/f9d6a121f13efbb586d6af210dedec61661dfc6d/types/globals.d.ts#L419-L436)) and `serverInfo` ([ServerInfo](https://github.com/fastly/js-compute-runtime/blob/f9d6a121f13efbb586d6af210dedec61661dfc6d/types/globals.d.ts#L438-L446)) are always defined on `fire.Bindings` and can be made available on `c.env`, even if the bindings definitions are empty:
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
import { Hono } from 'hono';
|
|
118
|
+
import { fire, type Bindings } from '@fastly/hono-fastly-compute';
|
|
119
|
+
|
|
120
|
+
const app = new Hono<{ Bindings: Bindings }>();
|
|
121
|
+
|
|
122
|
+
app.get('/', (c) => {
|
|
123
|
+
const clientInfo = c.env.clientInfo;
|
|
124
|
+
const serverInfo = c.env.serverInfo;
|
|
125
|
+
|
|
126
|
+
c.text(`${clientInfo.address} ${serverInfo.address}`);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
fire(app);
|
|
130
|
+
```
|
|
131
|
+
|
|
98
132
|
### `logFastlyServiceVersion()`
|
|
99
133
|
|
|
100
134
|
A Hono middleware that logs to the console the string `FASTLY_SERVICE_VERSION` followed by the value of the environment variable **FASTLY_SERVICE_VERSION**.
|
|
101
135
|
|
|
136
|
+
### `getConnInfo()`
|
|
137
|
+
|
|
138
|
+
An implementation of the [ConnInfo helper](https://hono.dev/docs/helpers/conninfo) for Fastly Compute.
|
|
139
|
+
|
|
140
|
+
```typescript
|
|
141
|
+
import { Hono } from 'hono';
|
|
142
|
+
import { fire, getConnInfo } from '@fastly/hono-fastly-compute';
|
|
143
|
+
|
|
144
|
+
const app = new Hono();
|
|
145
|
+
|
|
146
|
+
app.get('/', (c) => {
|
|
147
|
+
const info = getConnInfo(c); // info is `ConnInfo`
|
|
148
|
+
return c.text(`Your remote address is ${info.remote.address}`);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
fire(app);
|
|
152
|
+
```
|
|
153
|
+
|
|
102
154
|
## Issues
|
|
103
155
|
|
|
104
156
|
If you encounter any non-security-related bug or unexpected behavior, please [file an issue][bug] using the bug report template.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright Fastly, Inc.
|
|
3
|
+
* Licensed under the MIT license. See LICENSE file for details.
|
|
4
|
+
*/
|
|
5
|
+
const ipv4Regex = /^(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
|
|
6
|
+
const ipv6Regex = /^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/;
|
|
7
|
+
export const getConnInfo = (c) => {
|
|
8
|
+
const clientInfo = c.env.clientInfo;
|
|
9
|
+
if (clientInfo == null) {
|
|
10
|
+
throw new TypeError('env has to include clientInfo.');
|
|
11
|
+
}
|
|
12
|
+
let address = undefined;
|
|
13
|
+
let addressType = undefined;
|
|
14
|
+
if (ipv4Regex.test(clientInfo.address)) {
|
|
15
|
+
address = clientInfo.address;
|
|
16
|
+
addressType = 'IPv4';
|
|
17
|
+
}
|
|
18
|
+
else if (ipv6Regex.test(clientInfo.address)) {
|
|
19
|
+
address = clientInfo.address;
|
|
20
|
+
addressType = 'IPv6';
|
|
21
|
+
}
|
|
22
|
+
return {
|
|
23
|
+
remote: {
|
|
24
|
+
address,
|
|
25
|
+
addressType,
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
};
|
package/build/fire.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Schema } from 'hono';
|
|
2
2
|
import type { HonoBase } from 'hono/hono-base';
|
|
3
|
-
import type {
|
|
4
|
-
import { type HandleOptions } from './handler.js';
|
|
3
|
+
import type { BindingsDefs, ContextProxy } from '@fastly/compute-js-context';
|
|
4
|
+
import { type BindingsWithClientInfo, type HandleOptions } from './handler.js';
|
|
5
5
|
/**
|
|
6
6
|
* Registers a Hono app to handle fetch events in Fastly Compute.
|
|
7
7
|
*
|
|
@@ -35,9 +35,9 @@ type FireFn<D extends BindingsDefs> = {
|
|
|
35
35
|
* The inferred bindings type, derived from the defs passed to `buildFire()`.
|
|
36
36
|
* Use this in your Env definition: `{ Bindings: typeof fire.Bindings }`.
|
|
37
37
|
*/
|
|
38
|
-
Bindings:
|
|
38
|
+
Bindings: BindingsWithClientInfo<D>;
|
|
39
39
|
/** For debugging: the raw defs object you passed to buildFire */
|
|
40
|
-
|
|
40
|
+
_defs: D;
|
|
41
41
|
};
|
|
42
42
|
/**
|
|
43
43
|
* Creates a `fire` function bound to a specific set of environment bindings.
|
|
@@ -51,8 +51,8 @@ type FireFn<D extends BindingsDefs> = {
|
|
|
51
51
|
* const fire = buildFire({ grip: "ConfigStore", foo: "KVStore" });
|
|
52
52
|
*
|
|
53
53
|
* type Env = {
|
|
54
|
-
* Variables: Variables
|
|
55
|
-
* Bindings: typeof fire.Bindings
|
|
54
|
+
* Variables: Variables,
|
|
55
|
+
* Bindings: typeof fire.Bindings, // infer binding types automatically
|
|
56
56
|
* };
|
|
57
57
|
*
|
|
58
58
|
* const app = new Hono<Env>();
|
|
@@ -71,8 +71,22 @@ type FireFn<D extends BindingsDefs> = {
|
|
|
71
71
|
* (e.g. `{ foo: "KVStore", bar: "ConfigStore" }`).
|
|
72
72
|
* @returns A `fire` function that:
|
|
73
73
|
* - Registers your Hono app to handle fetch events
|
|
74
|
+
* - Applies `bindingsDefs` to `c.env`
|
|
74
75
|
* - Exposes a `.Bindings` type inferred from the given defs
|
|
75
76
|
*/
|
|
76
77
|
export declare function buildFire<D extends BindingsDefs>(bindingsDefs: D): FireFn<D>;
|
|
78
|
+
/**
|
|
79
|
+
* Registers your Hono app to handle fetch events. No user-defined bindings are
|
|
80
|
+
* applied to `c.env`.
|
|
81
|
+
* `fire.Bindings` is a type you can use when defining your `Env`,
|
|
82
|
+
* providing access to platform-defined bindings such as `clientInfo` and
|
|
83
|
+
* `serverInfo`.
|
|
84
|
+
*/
|
|
85
|
+
export declare const fire: FireFn<{}>;
|
|
86
|
+
/**
|
|
87
|
+
* Default bindings which can be used when no user-defined bindings are present.
|
|
88
|
+
* Alias of `fire.Bindings`.
|
|
89
|
+
*/
|
|
90
|
+
export type Bindings = typeof fire.Bindings;
|
|
77
91
|
export {};
|
|
78
92
|
//# sourceMappingURL=fire.d.ts.map
|
package/build/fire.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright Fastly, Inc.
|
|
3
|
+
* Licensed under the MIT license. See LICENSE file for details.
|
|
4
|
+
*/
|
|
1
5
|
import { handle } from './handler.js';
|
|
2
6
|
/**
|
|
3
7
|
* Creates a `fire` function bound to a specific set of environment bindings.
|
|
@@ -11,8 +15,8 @@ import { handle } from './handler.js';
|
|
|
11
15
|
* const fire = buildFire({ grip: "ConfigStore", foo: "KVStore" });
|
|
12
16
|
*
|
|
13
17
|
* type Env = {
|
|
14
|
-
* Variables: Variables
|
|
15
|
-
* Bindings: typeof fire.Bindings
|
|
18
|
+
* Variables: Variables,
|
|
19
|
+
* Bindings: typeof fire.Bindings, // infer binding types automatically
|
|
16
20
|
* };
|
|
17
21
|
*
|
|
18
22
|
* const app = new Hono<Env>();
|
|
@@ -31,12 +35,21 @@ import { handle } from './handler.js';
|
|
|
31
35
|
* (e.g. `{ foo: "KVStore", bar: "ConfigStore" }`).
|
|
32
36
|
* @returns A `fire` function that:
|
|
33
37
|
* - Registers your Hono app to handle fetch events
|
|
38
|
+
* - Applies `bindingsDefs` to `c.env`
|
|
34
39
|
* - Exposes a `.Bindings` type inferred from the given defs
|
|
35
40
|
*/
|
|
36
41
|
export function buildFire(bindingsDefs) {
|
|
37
42
|
const fireFn = ((app, options = { fetch: undefined, }) => {
|
|
38
43
|
addEventListener('fetch', handle(app, bindingsDefs, options));
|
|
39
44
|
});
|
|
40
|
-
fireFn.
|
|
45
|
+
fireFn._defs = bindingsDefs;
|
|
41
46
|
return fireFn;
|
|
42
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Registers your Hono app to handle fetch events. No user-defined bindings are
|
|
50
|
+
* applied to `c.env`.
|
|
51
|
+
* `fire.Bindings` is a type you can use when defining your `Env`,
|
|
52
|
+
* providing access to platform-defined bindings such as `clientInfo` and
|
|
53
|
+
* `serverInfo`.
|
|
54
|
+
*/
|
|
55
|
+
export const fire = buildFire({});
|
package/build/handler.d.ts
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
import type { Schema } from 'hono';
|
|
2
2
|
import type { HonoBase } from 'hono/hono-base';
|
|
3
3
|
import { type BindingsDefs, type ContextProxy } from '@fastly/compute-js-context';
|
|
4
|
+
export type BindingsWithClientInfo<D extends BindingsDefs> = ContextProxy<D> & {
|
|
5
|
+
clientInfo: ClientInfo;
|
|
6
|
+
serverInfo: ServerInfo;
|
|
7
|
+
};
|
|
4
8
|
type Handler = (evt: FetchEvent) => void;
|
|
5
9
|
export type HandleOptions = {
|
|
6
10
|
fetch?: typeof fetch;
|
|
7
11
|
};
|
|
8
12
|
type HandleFn = {
|
|
9
|
-
<D extends BindingsDefs, V extends {
|
|
10
|
-
Variables?: object;
|
|
11
|
-
}, S extends Schema, BasePath extends string>(app: HonoBase<(keyof D extends never ? {} : never) & V, S, BasePath>, envBindingsDefs: D, opts?: HandleOptions): Handler;
|
|
12
13
|
<D extends BindingsDefs, V extends {
|
|
13
14
|
Variables?: object;
|
|
14
15
|
}, S extends Schema, BasePath extends string>(app: HonoBase<{
|
|
15
|
-
Bindings:
|
|
16
|
+
Bindings: BindingsWithClientInfo<D>;
|
|
16
17
|
} & V, S, BasePath>, envBindingsDefs: D, opts?: HandleOptions): Handler;
|
|
17
18
|
};
|
|
18
19
|
/**
|
package/build/handler.js
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
/*
|
|
2
|
+
* Copyright Fastly, Inc.
|
|
3
|
+
* Licensed under the MIT license. See LICENSE file for details.
|
|
4
|
+
*/
|
|
5
|
+
import { buildContextProxyOn, } from '@fastly/compute-js-context';
|
|
2
6
|
/**
|
|
3
7
|
* Adapter for Fastly Compute
|
|
4
8
|
*/
|
|
@@ -8,8 +12,11 @@ export const handle = (app, envBindingsDefs, opts = {
|
|
|
8
12
|
}) => {
|
|
9
13
|
return (evt) => {
|
|
10
14
|
evt.respondWith((async () => {
|
|
11
|
-
const
|
|
12
|
-
|
|
15
|
+
const envBase = {
|
|
16
|
+
clientInfo: evt.client,
|
|
17
|
+
serverInfo: evt.server,
|
|
18
|
+
};
|
|
19
|
+
const env = buildContextProxyOn(envBase, envBindingsDefs);
|
|
13
20
|
const res = await app.fetch(evt.request, env, {
|
|
14
21
|
waitUntil: evt.waitUntil.bind(evt),
|
|
15
22
|
passThroughOnException() {
|
package/build/index.d.ts
CHANGED
package/build/index.js
CHANGED
package/build/utils.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright Fastly, Inc.
|
|
3
|
+
* Licensed under the MIT license. See LICENSE file for details.
|
|
4
|
+
*/
|
|
1
5
|
import { env } from 'fastly:env';
|
|
2
6
|
import { createMiddleware } from 'hono/factory';
|
|
3
7
|
export const logFastlyServiceVersion = () => createMiddleware(async (_, next) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastly/hono-fastly-compute",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"description": "Helper utilities for using Hono with Fastly Compute",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"clean": "rm -rf build"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@fastly/compute-js-context": "^0.
|
|
29
|
+
"@fastly/compute-js-context": "^0.5.1",
|
|
30
30
|
"@fastly/js-compute": "^3.0.0"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|