@fastly/hono-fastly-compute 0.3.1 → 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,13 @@ 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
+
10
17
  ## [0.3.1] - 2025-10-08
11
18
 
12
19
  ### Fixed
@@ -25,7 +32,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
25
32
 
26
33
  - Initial public release
27
34
 
28
- [unreleased]: https://github.com/fastly/hono-fastly-compute/compare/v0.3.1...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
29
37
  [0.3.1]: https://github.com/fastly/hono-fastly-compute/compare/v0.3.0...v0.3.1
30
38
  [0.3.0]: https://github.com/fastly/hono-fastly-compute/compare/v0.1.0...v0.3.0
31
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,7 +1,7 @@
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
5
  /**
6
6
  * Registers a Hono app to handle fetch events in Fastly Compute.
7
7
  *
@@ -35,7 +35,7 @@ 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: ContextProxy<D>;
38
+ Bindings: BindingsWithClientInfo<D>;
39
39
  /** For debugging: the raw defs object you passed to buildFire */
40
40
  defs: D;
41
41
  };
@@ -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; // infer binding types automatically
54
+ * Variables: Variables,
55
+ * Bindings: typeof fire.Bindings, // infer binding types automatically
56
56
  * };
57
57
  *
58
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>();
@@ -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: ContextProxy<D>;
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
- 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.1",
3
+ "version": "0.3.2",
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.4.2",
29
+ "@fastly/compute-js-context": "^0.5.1",
30
30
  "@fastly/js-compute": "^3.0.0"
31
31
  },
32
32
  "devDependencies": {