@funcd-dev/shim 0.35.0 → 0.36.0

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/addon.js CHANGED
@@ -2,6 +2,7 @@
2
2
  // worker-node local API, which checks the binding and the path before it forwards the request.
3
3
  import http from 'node:http';
4
4
  import { MEMBER_HEADER } from "./invoke.js";
5
+ import { localApi } from "./localapi.js";
5
6
  import { startClientSpan } from "./tracespan.js";
6
7
  /** The local API header that marks an answer as the engine's; a call that funcd refused carries none. */
7
8
  export const ADDON_REPLY_HEADER = 'x-funcd-addon-reply';
@@ -33,8 +34,8 @@ export function makeAddon(opts = {}) {
33
34
  span?.end('ERROR', err.message, httpStatus);
34
35
  throw err;
35
36
  };
36
- const socketPath = process.env.FUNCD_INVOKE_SOCKET;
37
- if (!socketPath) {
37
+ const api = localApi();
38
+ if (!api) {
38
39
  return fail(new Error('context.addon: worker-node local API socket unavailable (FUNCD_INVOKE_SOCKET unset)'));
39
40
  }
40
41
  if (!path.startsWith('/')) {
@@ -48,6 +49,7 @@ export function makeAddon(opts = {}) {
48
49
  });
49
50
  if (body)
50
51
  headers['content-length'] = String(body.length);
52
+ Object.assign(headers, api.headers);
51
53
  if (opts.member)
52
54
  headers[MEMBER_HEADER] = opts.member;
53
55
  if (span)
@@ -55,7 +57,7 @@ export function makeAddon(opts = {}) {
55
57
  const target = new URL(request.url);
56
58
  const reply = await new Promise((resolve, reject) => {
57
59
  const req = http.request({
58
- socketPath,
60
+ ...api.options,
59
61
  path: `/addon/${encodeURIComponent(alias)}${target.pathname}${target.search}`,
60
62
  method: request.method,
61
63
  headers,
package/dist/funclog.js CHANGED
@@ -5,14 +5,17 @@
5
5
  // (Node + Python) emit the SAME wire, so the reader is language-agnostic.
6
6
  //
7
7
  // Channel selection at startup (env, in order):
8
- // FUNCD_LOG_FD = a numeric fd (e.g. "3") → fs.writeSync(fd, line) [SYNCHRONOUS — minimal crash-tail loss]
9
- // FUNCD_LOG_SOCK = a unix socket path → net.connect(path), write lines to it
10
- // neither set → no capture; console stays as-is (Path A / stdout)
8
+ // FUNCD_LOG_FD = a numeric fd (e.g. "3") → fs.writeSync(fd, line) [SYNCHRONOUS — minimal crash-tail loss]
9
+ // FUNCD_LOG_SOCK = a unix socket path → net.connect(path), write lines to it
10
+ // FUNCD_LOG_STDOUT = set, with neither above → every console call prints its line to stdout (funcd ADR-0346
11
+ // Decision 4: a worker in a container has no channel, and funcd
12
+ // keeps its stdout as lines, without the records' fields)
13
+ // none set → no capture; console stays as-is (Path A / stdout)
11
14
  //
12
15
  // Pure Node, built-ins only (node:fs, node:net, node:worker_threads) — no npm deps, so it bundles into shim.mjs/pool.mjs.
13
16
  import { writeSync } from 'node:fs';
14
17
  import { connect } from 'node:net';
15
- import { inspect } from 'node:util';
18
+ import { format, inspect } from 'node:util';
16
19
  import { threadId } from 'node:worker_threads';
17
20
  import { currentInv } from "./invcontext.js";
18
21
  const SEVERITY = {
@@ -404,8 +407,12 @@ export function openChannel(env, lock) {
404
407
  * already opened (ADR-0101: log + trace capture share ONE channel), else it opens from env. A pool
405
408
  * worker passes its `member` name, stamped on every record as `funcd.member`. */
406
409
  export function installConsoleCapture(env = process.env, sink = openChannel(env), member) {
407
- if (!sink)
408
- return false;
410
+ if (!sink) {
411
+ if (!env.FUNCD_LOG_STDOUT)
412
+ return false;
413
+ printToStdout();
414
+ return true;
415
+ }
409
416
  const bound = recordBound(env);
410
417
  const methods = ['debug', 'log', 'info', 'warn', 'error'];
411
418
  for (const method of methods) {
@@ -420,3 +427,18 @@ export function installConsoleCapture(env = process.env, sink = openChannel(env)
420
427
  }
421
428
  return true;
422
429
  }
430
+ /** printToStdout makes every console call print its formatted line to stdout, synchronously: the record funcd keeps
431
+ * as a line when FUNCD_LOG_STDOUT is set and no channel is (funcd ADR-0346 Decision 4). */
432
+ function printToStdout() {
433
+ const methods = ['debug', 'log', 'info', 'warn', 'error'];
434
+ for (const method of methods) {
435
+ console[method] = (...args) => {
436
+ try {
437
+ writeSync(1, `${format(...args)}\n`);
438
+ }
439
+ catch {
440
+ // a closed stdout must never crash the function — drop the line.
441
+ }
442
+ };
443
+ }
444
+ }
package/dist/invoke.js CHANGED
@@ -2,6 +2,7 @@
2
2
  // platform bind-mounts the per-sandbox socket and provides its path via FUNCD_INVOKE_SOCKET; the
3
3
  // handler calls context.invoke(alias, input) and the platform brokers the call to the linked target.
4
4
  import http from 'node:http';
5
+ import { localApi } from "./localapi.js";
5
6
  import { startClientSpan } from "./tracespan.js";
6
7
  /** Build the context.invoke implementation. The returned function POSTs `input` to
7
8
  * /invoke/<alias> over the worker-node UDS and resolves the target's JSON output, or rejects on a
@@ -18,13 +19,14 @@ export function makeInvoke(opts = {}) {
18
19
  span?.end('ERROR', err.message, httpStatus);
19
20
  reject(err);
20
21
  };
21
- const socketPath = process.env.FUNCD_INVOKE_SOCKET;
22
- if (!socketPath) {
22
+ const api = localApi();
23
+ if (!api) {
23
24
  fail(new Error('context.invoke: worker-node local API socket unavailable (FUNCD_INVOKE_SOCKET unset)'));
24
25
  return;
25
26
  }
26
27
  const body = JSON.stringify(input ?? null);
27
28
  const headers = {
29
+ ...api.headers,
28
30
  'content-type': 'application/json',
29
31
  'content-length': Buffer.byteLength(body),
30
32
  };
@@ -33,7 +35,7 @@ export function makeInvoke(opts = {}) {
33
35
  if (span)
34
36
  headers.traceparent = span.traceparent;
35
37
  const req = http.request({
36
- socketPath,
38
+ ...api.options,
37
39
  path: `/invoke/${encodeURIComponent(alias)}`,
38
40
  method: 'POST',
39
41
  headers,
@@ -84,8 +86,8 @@ export function makeFetch(opts = {}) {
84
86
  span?.end('ERROR', err.message, httpStatus);
85
87
  throw err;
86
88
  };
87
- const socketPath = process.env.FUNCD_INVOKE_SOCKET;
88
- if (!socketPath) {
89
+ const api = localApi();
90
+ if (!api) {
89
91
  return fail(new Error('context.fetch: worker-node local API socket unavailable (FUNCD_INVOKE_SOCKET unset)'));
90
92
  }
91
93
  if (!path.startsWith('/')) {
@@ -99,6 +101,7 @@ export function makeFetch(opts = {}) {
99
101
  });
100
102
  if (body)
101
103
  headers['content-length'] = String(body.length);
104
+ Object.assign(headers, api.headers);
102
105
  if (opts.member)
103
106
  headers[MEMBER_HEADER] = opts.member;
104
107
  if (span)
@@ -106,7 +109,7 @@ export function makeFetch(opts = {}) {
106
109
  const target = new URL(request.url);
107
110
  const reply = await new Promise((resolve, reject) => {
108
111
  const req = http.request({
109
- socketPath,
112
+ ...api.options,
110
113
  path: `/fetch/${encodeURIComponent(alias)}${target.pathname}${target.search}`,
111
114
  method: request.method,
112
115
  headers,
@@ -0,0 +1,12 @@
1
+ import type http from 'node:http';
2
+ /** The header that carries a container worker's token to its own local API listener (funcd ADR-0346 Decision 6). */
3
+ export declare const TOKEN_HEADER = "x-funcd-invoke-token";
4
+ /** Where a request reaches the local API: the options that dial it and the headers every request carries. */
5
+ export interface LocalApi {
6
+ options: http.RequestOptions;
7
+ headers: Record<string, string>;
8
+ }
9
+ /** localApi is where this worker reaches funcd's local API: FUNCD_INVOKE_URL, the listener of a worker in a container,
10
+ * with FUNCD_INVOKE_TOKEN in TOKEN_HEADER on every request (funcd ADR-0346 Decision 6), else the socket
11
+ * FUNCD_INVOKE_SOCKET (funcd ADR-0064); undefined when neither is set. */
12
+ export declare function localApi(env?: NodeJS.ProcessEnv): LocalApi | undefined;
@@ -0,0 +1,17 @@
1
+ /** The header that carries a container worker's token to its own local API listener (funcd ADR-0346 Decision 6). */
2
+ export const TOKEN_HEADER = 'x-funcd-invoke-token';
3
+ /** localApi is where this worker reaches funcd's local API: FUNCD_INVOKE_URL, the listener of a worker in a container,
4
+ * with FUNCD_INVOKE_TOKEN in TOKEN_HEADER on every request (funcd ADR-0346 Decision 6), else the socket
5
+ * FUNCD_INVOKE_SOCKET (funcd ADR-0064); undefined when neither is set. */
6
+ export function localApi(env = process.env) {
7
+ const url = env.FUNCD_INVOKE_URL;
8
+ if (url) {
9
+ const u = new URL(url);
10
+ const headers = {};
11
+ if (env.FUNCD_INVOKE_TOKEN)
12
+ headers[TOKEN_HEADER] = env.FUNCD_INVOKE_TOKEN;
13
+ return { options: { host: u.hostname, port: u.port === '' ? 80 : Number(u.port) }, headers };
14
+ }
15
+ const socketPath = env.FUNCD_INVOKE_SOCKET;
16
+ return socketPath ? { options: { socketPath }, headers: {} } : undefined;
17
+ }
package/dist/socket.js CHANGED
@@ -1,16 +1,17 @@
1
1
  // The worker-node local API client shared by context.kv, context.blob and context.sql: one HTTP request over the
2
- // per-sandbox socket (FUNCD_INVOKE_SOCKET), as the calling pool member when one is given.
2
+ // local API (localApi), as the calling pool member when one is given.
3
3
  import http from 'node:http';
4
4
  import { MEMBER_HEADER } from "./invoke.js";
5
+ import { localApi } from "./localapi.js";
5
6
  /** send makes one request with `extra` headers; `client` names the context client in its errors ("context.kv"). */
6
7
  export function send(client, method, path, body, member, contentType, extra) {
7
8
  return new Promise((resolve, reject) => {
8
- const socketPath = process.env.FUNCD_INVOKE_SOCKET;
9
- if (!socketPath) {
9
+ const api = localApi();
10
+ if (!api) {
10
11
  reject(new Error(`${client}: worker-node local API socket unavailable (FUNCD_INVOKE_SOCKET unset)`));
11
12
  return;
12
13
  }
13
- const headers = {};
14
+ const headers = { ...api.headers };
14
15
  if (body)
15
16
  headers['content-length'] = body.byteLength;
16
17
  if (contentType)
@@ -18,7 +19,7 @@ export function send(client, method, path, body, member, contentType, extra) {
18
19
  if (member)
19
20
  headers[MEMBER_HEADER] = member;
20
21
  Object.assign(headers, extra);
21
- const req = http.request({ socketPath, path, method, headers }, (res) => {
22
+ const req = http.request({ ...api.options, path, method, headers }, (res) => {
22
23
  const chunks = [];
23
24
  // A connection that drops mid-body errors the response, not the request, and never ends it.
24
25
  res.on('error', (err) => reject(new Error(`${client} ${method} ${path} failed: connection closed before the reply ended`, { cause: err })));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funcd-dev/shim",
3
- "version": "0.35.0",
3
+ "version": "0.36.0",
4
4
  "type": "module",
5
5
  "description": "Types and the contract build for writing funcd functions in TypeScript",
6
6
  "license": "Apache-2.0",