@fastly/hono-fastly-compute 0.3.0 → 0.3.2

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 CHANGED
@@ -7,6 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [unreleased]
9
9
 
10
+ ## [0.3.2] - 2025-10-14
11
+
12
+ ### Added
13
+
14
+ - Extend env with client and server info
15
+ - Implement ConnInfo helpers
16
+
17
+ ## [0.3.1] - 2025-10-08
18
+
19
+ ### Fixed
20
+
21
+ - Fix typings when bindings are empty
22
+
10
23
  ## [0.3.0] - 2025-10-06
11
24
 
12
25
  ### Changed
@@ -19,6 +32,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
19
32
 
20
33
  - Initial public release
21
34
 
22
- [unreleased]: https://github.com/fastly/hono-fastly-compute/compare/v0.3.0...HEAD
35
+ [unreleased]: https://github.com/fastly/hono-fastly-compute/compare/v0.3.2...HEAD
36
+ [0.3.2]: https://github.com/fastly/hono-fastly-compute/compare/v0.3.1...v0.3.2
37
+ [0.3.1]: https://github.com/fastly/hono-fastly-compute/compare/v0.3.0...v0.3.1
23
38
  [0.3.0]: https://github.com/fastly/hono-fastly-compute/compare/v0.1.0...v0.3.0
24
39
  [0.1.0]: https://github.com/fastly/hono-fastly-compute/releases/tag/v0.1.0
package/README.md CHANGED
@@ -38,7 +38,6 @@ const fire = buildFire({
38
38
  type Env = {
39
39
  Bindings: typeof fire.Bindings;
40
40
  };
41
-
42
41
  const app = new Hono<Env>();
43
42
 
44
43
  app.get('/', async (c) => {
@@ -95,10 +94,50 @@ The core adapter function that connects Hono to the Fastly Compute `FetchEvent`.
95
94
  - **`bindingsDefs`**: The environment bindings definition.
96
95
  - **`options`**: An optional object with a `fetch` property.
97
96
 
97
+ ### `clientInfo` and `serverInfo`
98
+
99
+ `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 defined on `fire.Bindings` and available on `c.env`, even if the bindings definitions are empty:
100
+
101
+ ```typescript
102
+ import { Hono } from 'hono';
103
+ import { buildFire } from '@fastly/hono-fastly-compute';
104
+
105
+ const fire = buildFire({});
106
+ const app = new Hono<{Bindings: typeof fire.Bindings}>();
107
+
108
+ app.get('/', (c) => {
109
+ const clientInfo = c.env.clientInfo;
110
+ const serverInfo = c.env.serverInfo;
111
+
112
+ c.text(`${clientInfo.address} ${serverInfo.address}`);
113
+ });
114
+
115
+ fire(app);
116
+ ```
117
+
98
118
  ### `logFastlyServiceVersion()`
99
119
 
100
120
  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
121
 
122
+ ### `getConnInfo()`
123
+
124
+ An implementation of the [ConnInfo helper](https://hono.dev/docs/helpers/conninfo) for Fastly Compute.
125
+
126
+ ```typescript
127
+ import { Hono } from 'hono';
128
+ import { buildFire, getConnInfo } from '@fastly/hono-fastly-compute';
129
+
130
+ const fire = buildFire({});
131
+ const app = new Hono();
132
+
133
+ app.get('/', (c) => {
134
+ const info = getConnInfo(c); // info is `ConnInfo`
135
+ return c.text(`Your remote address is ${info.remote.address}`);
136
+ });
137
+
138
+ fire(app);
139
+ ```
140
+
102
141
  ## Issues
103
142
 
104
143
  If you encounter any non-security-related bug or unexpected behavior, please [file an issue][bug] using the bug report template.
@@ -0,0 +1,3 @@
1
+ import type { GetConnInfo } from 'hono/conninfo';
2
+ export declare const getConnInfo: GetConnInfo;
3
+ //# sourceMappingURL=conninfo.d.ts.map
@@ -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,38 +1,41 @@
1
1
  import type { Schema } from 'hono';
2
2
  import type { HonoBase } from 'hono/hono-base';
3
- import type { ContextProxy, BindingsDefs } from '@fastly/compute-js-context';
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
+ /**
6
+ * Registers a Hono app to handle fetch events in Fastly Compute.
7
+ *
8
+ * This sets up `addEventListener('fetch', handle(app, envBindingsDefs, options))`
9
+ * using the bindings you passed to `buildFire()`.
10
+ *
11
+ * @param app - The Hono application instance to serve
12
+ * @param options - Options for handling requests (fetch defaults to undefined)
13
+ *
14
+ * @example
15
+ * const fire = buildFire({ foo: "KVStore" });
16
+ *
17
+ * type Env = {
18
+ * Variables: Variables;
19
+ * Bindings: typeof fire.Bindings;
20
+ * };
21
+ *
22
+ * const app = new Hono<Env>();
23
+ * fire(app);
24
+ */
5
25
  type FireFn<D extends BindingsDefs> = {
6
- /**
7
- * Registers a Hono app to handle fetch events in Fastly Compute.
8
- *
9
- * This sets up `addEventListener('fetch', handle(app, envBindingsDefs, options))`
10
- * using the bindings you passed to `buildFire()`.
11
- *
12
- * @param app - The Hono application instance to serve
13
- * @param options - Options for handling requests (fetch defaults to undefined)
14
- *
15
- * @example
16
- * const fire = buildFire({ foo: "KVStore" });
17
- *
18
- * type Env = {
19
- * Variables: Variables;
20
- * Bindings: typeof fire.Bindings;
21
- * };
22
- *
23
- * const app = new Hono<Env>();
24
- * fire(app);
25
- */
26
26
  <V extends {
27
27
  Variables?: object;
28
- }, S extends Schema, BasePath extends string>(app: HonoBase<(keyof D extends never ? {} : {
28
+ }, S extends Schema, BasePath extends string>(app: HonoBase<(keyof D extends never ? {} : never) & V, S, BasePath>, options?: HandleOptions): void;
29
+ <V extends {
30
+ Variables?: object;
31
+ }, S extends Schema, BasePath extends string>(app: HonoBase<{
29
32
  Bindings: ContextProxy<D>;
30
- }) & V, S, BasePath>, options?: HandleOptions): void;
33
+ } & V, S, BasePath>, options?: HandleOptions): void;
31
34
  /**
32
35
  * The inferred bindings type, derived from the defs passed to `buildFire()`.
33
36
  * Use this in your Env definition: `{ Bindings: typeof fire.Bindings }`.
34
37
  */
35
- Bindings: ContextProxy<D>;
38
+ Bindings: BindingsWithClientInfo<D>;
36
39
  /** For debugging: the raw defs object you passed to buildFire */
37
40
  defs: D;
38
41
  };
@@ -48,8 +51,8 @@ type FireFn<D extends BindingsDefs> = {
48
51
  * const fire = buildFire({ grip: "ConfigStore", foo: "KVStore" });
49
52
  *
50
53
  * type Env = {
51
- * Variables: Variables;
52
- * Bindings: typeof fire.Bindings; // infer binding types automatically
54
+ * Variables: Variables,
55
+ * Bindings: typeof fire.Bindings, // infer binding types automatically
53
56
  * };
54
57
  *
55
58
  * const app = new Hono<Env>();
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; // infer binding types automatically
18
+ * Variables: Variables,
19
+ * Bindings: typeof fire.Bindings, // infer binding types automatically
16
20
  * };
17
21
  *
18
22
  * const app = new Hono<Env>();
@@ -34,9 +38,7 @@ import { handle } from './handler.js';
34
38
  * - Exposes a `.Bindings` type inferred from the given defs
35
39
  */
36
40
  export function buildFire(bindingsDefs) {
37
- const fireFn = ((app, options = {
38
- fetch: undefined,
39
- }) => {
41
+ const fireFn = ((app, options = { fetch: undefined, }) => {
40
42
  addEventListener('fetch', handle(app, bindingsDefs, options));
41
43
  });
42
44
  fireFn.defs = bindingsDefs;
@@ -1,18 +1,24 @@
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
- export type MaybeBindings<D extends BindingsDefs> = keyof D extends never ? {} : {
9
- Bindings: ContextProxy<D>;
12
+ type HandleFn = {
13
+ <D extends BindingsDefs, V extends {
14
+ Variables?: object;
15
+ }, S extends Schema, BasePath extends string>(app: HonoBase<{
16
+ Bindings: BindingsWithClientInfo<D>;
17
+ } & V, S, BasePath>, envBindingsDefs: D, opts?: HandleOptions): Handler;
10
18
  };
11
19
  /**
12
20
  * Adapter for Fastly Compute
13
21
  */
14
- export declare const handle: <D extends BindingsDefs, V extends {
15
- Variables?: object;
16
- }, S extends Schema, BasePath extends string>(app: HonoBase<MaybeBindings<D> & V, S, BasePath>, envBindingsDefs: D, opts?: HandleOptions) => Handler;
22
+ export declare const handle: HandleFn;
17
23
  export {};
18
24
  //# sourceMappingURL=handler.d.ts.map
package/build/handler.js CHANGED
@@ -1,4 +1,8 @@
1
- import { createContext, buildContextProxy, } from '@fastly/compute-js-context';
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 context = createContext();
12
- const env = buildContextProxy(context, envBindingsDefs);
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
@@ -1,5 +1,6 @@
1
1
  export * from './fire.js';
2
2
  export * from './handler.js';
3
3
  export * from './utils.js';
4
+ export * from './conninfo.js';
4
5
  export type { ResourceType, Context, ContextProxy, BindingsDefs, } from '@fastly/compute-js-context';
5
6
  //# sourceMappingURL=index.d.ts.map
package/build/index.js CHANGED
@@ -1,3 +1,8 @@
1
+ /*
2
+ * Copyright Fastly, Inc.
3
+ * Licensed under the MIT license. See LICENSE file for details.
4
+ */
1
5
  export * from './fire.js';
2
6
  export * from './handler.js';
3
7
  export * from './utils.js';
8
+ export * from './conninfo.js';
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.0",
3
+ "version": "0.3.2",
4
4
  "description": "Helper utilities for using Hono with Fastly Compute",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -26,8 +26,8 @@
26
26
  "clean": "rm -rf build"
27
27
  },
28
28
  "dependencies": {
29
- "@fastly/js-compute": "^3.0.0",
30
- "@fastly/compute-js-context": "^0.4.0"
29
+ "@fastly/compute-js-context": "^0.5.1",
30
+ "@fastly/js-compute": "^3.0.0"
31
31
  },
32
32
  "devDependencies": {
33
33
  "hono": "^4.9.5",