@fastly/compute-js-context 0.4.2 → 0.5.1
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 +19 -1
- package/README.md +2 -4
- package/build/proxy.d.ts +2 -2
- package/build/proxy.js +24 -7
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.5.1] - 2025-10-14
|
|
11
|
+
|
|
12
|
+
### Fixed
|
|
13
|
+
|
|
14
|
+
- Updated README
|
|
15
|
+
|
|
16
|
+
## [0.5.0] - 2025-10-14
|
|
17
|
+
|
|
18
|
+
### Changed
|
|
19
|
+
|
|
20
|
+
- BREAKING: Make build context proxy create the context internally
|
|
21
|
+
|
|
22
|
+
### Added
|
|
23
|
+
|
|
24
|
+
- Make proxy object possible to extend an existing object
|
|
25
|
+
|
|
10
26
|
## [0.4.2] - 2025-10-08
|
|
11
27
|
|
|
12
28
|
### Fixed
|
|
@@ -25,7 +41,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
25
41
|
|
|
26
42
|
- Initial public release
|
|
27
43
|
|
|
28
|
-
[unreleased]: https://github.com/fastly/compute-js-context/compare/v0.
|
|
44
|
+
[unreleased]: https://github.com/fastly/compute-js-context/compare/v0.5.1...HEAD
|
|
45
|
+
[0.5.1]: https://github.com/fastly/compute-js-context/compare/v0.5.0...v0.5.1
|
|
46
|
+
[0.5.0]: https://github.com/fastly/compute-js-context/compare/v0.4.2...v0.5.0
|
|
29
47
|
[0.4.2]: https://github.com/fastly/compute-js-context/compare/v0.4.1...v0.4.2
|
|
30
48
|
[0.4.1]: https://github.com/fastly/compute-js-context/compare/v0.2.0...v0.4.1
|
|
31
49
|
[0.2.0]: https://github.com/fastly/compute-js-context/releases/tag/v0.2.0
|
package/README.md
CHANGED
|
@@ -80,7 +80,7 @@ Then, pass these definitions to `buildContextProxy()`.
|
|
|
80
80
|
|
|
81
81
|
```typescript
|
|
82
82
|
/// <reference types="@fastly/js-compute" />
|
|
83
|
-
import {
|
|
83
|
+
import { buildContextProxy, type BindingsDefs } from '@fastly/compute-js-context';
|
|
84
84
|
|
|
85
85
|
// Define your application's bindings
|
|
86
86
|
const bindingsDefs = {
|
|
@@ -99,10 +99,8 @@ type Bindings = ContextProxy<typeof bindingsDefs>;
|
|
|
99
99
|
|
|
100
100
|
addEventListener('fetch', (event) => event.respondWith(handler(event)));
|
|
101
101
|
async function handler(event: FetchEvent): Promise<Response> {
|
|
102
|
-
const ctx = createContext();
|
|
103
|
-
|
|
104
102
|
// Create the typed environment
|
|
105
|
-
const bindings: Bindings = buildContextProxy(
|
|
103
|
+
const bindings: Bindings = buildContextProxy(bindingsDefs);
|
|
106
104
|
|
|
107
105
|
// Now use your custom bindings!
|
|
108
106
|
const asset = await bindings.assets?.get('/index.html');
|
package/build/proxy.d.ts
CHANGED
|
@@ -4,7 +4,6 @@ import type { ConfigStore } from 'fastly:config-store';
|
|
|
4
4
|
import type { KVStore } from 'fastly:kv-store';
|
|
5
5
|
import type { Logger } from 'fastly:logger';
|
|
6
6
|
import type { SecretStore } from 'fastly:secret-store';
|
|
7
|
-
import type { Context } from './index.js';
|
|
8
7
|
type Def<T extends string> = T | `${T}:${string}`;
|
|
9
8
|
type Defs<T extends string> = T extends string ? Def<T> : never;
|
|
10
9
|
declare const BindingStringToContextKeyMapping: {
|
|
@@ -32,6 +31,7 @@ export type ResourceInstance<T> = T extends Def<infer K extends keyof BindingStr
|
|
|
32
31
|
export type ContextProxy<T extends BindingsDefs> = keyof T extends never ? Record<string, never> : {
|
|
33
32
|
[K in keyof T]: ResourceInstance<T[K]>;
|
|
34
33
|
};
|
|
35
|
-
export declare function
|
|
34
|
+
export declare function buildContextProxyOn<TTarget extends object, T extends BindingsDefs>(target: TTarget, bindingsDefs: T): (TTarget & ContextProxy<T>);
|
|
35
|
+
export declare function buildContextProxy<T extends BindingsDefs>(bindingsDefs: T): ContextProxy<T>;
|
|
36
36
|
export {};
|
|
37
37
|
//# sourceMappingURL=proxy.d.ts.map
|
package/build/proxy.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* Copyright Fastly, Inc.
|
|
3
3
|
* Licensed under the MIT license. See LICENSE file for details.
|
|
4
4
|
*/
|
|
5
|
+
import { createContext } from './index.js';
|
|
5
6
|
const BindingStringToContextKeyMapping = {
|
|
6
7
|
Acl: 'ACLS',
|
|
7
8
|
Backend: 'BACKENDS',
|
|
@@ -25,10 +26,10 @@ function getResourceName(key, typeName) {
|
|
|
25
26
|
const [, seg,] = typeName.split(':');
|
|
26
27
|
return seg ?? key;
|
|
27
28
|
}
|
|
28
|
-
export function
|
|
29
|
-
const
|
|
29
|
+
export function buildContextProxyOn(target, bindingsDefs) {
|
|
30
|
+
const context = createContext();
|
|
30
31
|
const getEntry = (key) => {
|
|
31
|
-
if (typeof key !== "string") {
|
|
32
|
+
if (typeof key !== "string" || !(key in bindingsDefs)) {
|
|
32
33
|
return undefined;
|
|
33
34
|
}
|
|
34
35
|
const typeName = bindingsDefs[key];
|
|
@@ -45,12 +46,28 @@ export function buildContextProxy(context, bindingsDefs) {
|
|
|
45
46
|
return contextEntry[resourceName];
|
|
46
47
|
};
|
|
47
48
|
const handler = {
|
|
48
|
-
get(
|
|
49
|
-
|
|
49
|
+
get(target, key, receiver) {
|
|
50
|
+
if (typeof key !== "string") {
|
|
51
|
+
return Reflect.get(target, key, receiver);
|
|
52
|
+
}
|
|
53
|
+
const entry = getEntry(key);
|
|
54
|
+
if (entry !== undefined) {
|
|
55
|
+
return entry;
|
|
56
|
+
}
|
|
57
|
+
return Reflect.get(target, key, receiver);
|
|
50
58
|
},
|
|
51
|
-
has(
|
|
52
|
-
|
|
59
|
+
has(target, key) {
|
|
60
|
+
if (typeof key !== "string") {
|
|
61
|
+
return Reflect.has(target, key);
|
|
62
|
+
}
|
|
63
|
+
if (getEntry(key) !== undefined) {
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
return Reflect.has(target, key);
|
|
53
67
|
},
|
|
54
68
|
};
|
|
55
69
|
return new Proxy(target, handler);
|
|
56
70
|
}
|
|
71
|
+
export function buildContextProxy(bindingsDefs) {
|
|
72
|
+
return buildContextProxyOn({}, bindingsDefs);
|
|
73
|
+
}
|