@funcd-dev/shim 0.18.0 → 0.19.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/dist/config.d.ts CHANGED
@@ -1,5 +1,3 @@
1
- export declare function listedKeys(env: NodeJS.ProcessEnv, list: string): string[];
2
- export declare function listedValue(env: NodeJS.ProcessEnv, list: string, key: string): string | undefined;
3
1
  /** A function's bound config (funcd ADR-0245): a key a secret overrides, or any unbound variable, reads as undefined. */
4
2
  export interface ConfigClient {
5
3
  get(key: string): string | undefined;
package/dist/config.js CHANGED
@@ -1,11 +1,11 @@
1
1
  // context.config — the keys of a function's bound ConfigMaps (funcd ADR-0245), read from the environment at call time.
2
- // funcd sets FUNCD_CONFIG_KEYS and FUNCD_SECRET_KEYS, comma-separated lists of the bound keys of each kind in the
3
- // environment; an absent or empty list is no keys. context.secret reads its list with the same helpers.
4
- export function listedKeys(env, list) {
2
+ // funcd sets FUNCD_CONFIG_KEYS, the comma-separated bound config keys in the environment; an absent or empty list is no
3
+ // keys. Secrets are read from funcd instead (context.secret, funcd ADR-0249).
4
+ function listedKeys(env, list) {
5
5
  const v = env[list];
6
6
  return v ? v.split(',') : [];
7
7
  }
8
- export function listedValue(env, list, key) {
8
+ function listedValue(env, list, key) {
9
9
  return listedKeys(env, list).includes(key) ? env[key] : undefined;
10
10
  }
11
11
  export function makeConfig(env = process.env) {
package/dist/secret.d.ts CHANGED
@@ -1,6 +1,8 @@
1
- /** A function's bound secrets (funcd ADR-0245): an unbound key reads as undefined. */
1
+ /** A function's bound secrets (funcd ADR-0249): an unbound key reads as undefined. */
2
2
  export interface SecretClient {
3
3
  get(key: string): Promise<string | undefined>;
4
4
  list(): Promise<string[]>;
5
5
  }
6
- export declare function makeSecret(env?: NodeJS.ProcessEnv): SecretClient;
6
+ /** Build the context.secret client. In a pool, `member` names the calling function on every request (funcd checks it
7
+ * against the pool's members); the solo shim passes none. */
8
+ export declare function makeSecret(member?: string): SecretClient;
package/dist/secret.js CHANGED
@@ -1,9 +1,35 @@
1
- // context.secret — the keys of a function's bound Secrets (funcd ADR-0245), read from the environment at call time.
2
- // The methods return Promises, as every client that calls funcd does, so a later read from funcd keeps this API.
3
- import { listedKeys, listedValue } from "./config.js";
4
- export function makeSecret(env = process.env) {
1
+ // context.secret — the keys and values of a function's bound Secrets, read from funcd over the worker-node local API
2
+ // on every call (funcd ADR-0249): GET /secret and GET /secret/{key}, checked by funcd, with no cache. No secret sits in
3
+ // the worker's environment.
4
+ import { send } from "./socket.js";
5
+ const fail = (verb, r) => new Error(`context.secret.${verb} failed: ${r.status} ${r.body.toString('utf8')}`);
6
+ const json = (verb, r) => {
7
+ try {
8
+ return JSON.parse(r.body.toString('utf8'));
9
+ }
10
+ catch (err) {
11
+ throw new Error(`context.secret.${verb} failed: ${r.status} reply is not JSON: ${err.message}`, {
12
+ cause: err,
13
+ });
14
+ }
15
+ };
16
+ /** Build the context.secret client. In a pool, `member` names the calling function on every request (funcd checks it
17
+ * against the pool's members); the solo shim passes none. */
18
+ export function makeSecret(member) {
5
19
  return {
6
- get: async (key) => listedValue(env, 'FUNCD_SECRET_KEYS', key),
7
- list: async () => listedKeys(env, 'FUNCD_SECRET_KEYS'),
20
+ async get(key) {
21
+ const r = await send('context.secret', 'GET', `/secret/${encodeURIComponent(key)}`, undefined, member);
22
+ if (r.status === 404)
23
+ return undefined;
24
+ if (r.status !== 200)
25
+ throw fail('get', r);
26
+ return json('get', r).value;
27
+ },
28
+ async list() {
29
+ const r = await send('context.secret', 'GET', '/secret', undefined, member);
30
+ if (r.status !== 200)
31
+ throw fail('list', r);
32
+ return json('list', r).keys;
33
+ },
8
34
  };
9
35
  }
package/dist/types.d.ts CHANGED
@@ -51,7 +51,7 @@ export interface FunctionContext {
51
51
  sql: SQLClient;
52
52
  /** The keys of the ConfigMaps a function binds (funcd ADR-0245); a key a bound Secret overrides is a secret. */
53
53
  config: ConfigClient;
54
- /** The keys of the Secrets a function binds (funcd ADR-0245). */
54
+ /** The keys and values of the Secrets a function binds, read from funcd on every call (funcd ADR-0249). */
55
55
  secret: SecretClient;
56
56
  /** A bound catalog's proxy URL and this function's token, by spec.bindings.catalog alias (funcd ADR-0245). */
57
57
  catalog: CatalogClient;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funcd-dev/shim",
3
- "version": "0.18.0",
3
+ "version": "0.19.1",
4
4
  "type": "module",
5
5
  "description": "Types and the contract build for writing funcd functions in TypeScript",
6
6
  "license": "Apache-2.0",