@grainulation/harvest 1.0.2 → 1.0.4

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.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/lib/farmer.js +82 -13
  3. package/package.json +1 -2
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  </p>
4
4
 
5
5
  <p align="center">
6
- <a href="https://www.npmjs.com/package/@grainulation/harvest"><img src="https://img.shields.io/npm/v/@grainulation/harvest" alt="npm version"></a> <a href="https://www.npmjs.com/package/@grainulation/harvest"><img src="https://img.shields.io/npm/dm/@grainulation/harvest" alt="npm downloads"></a> <a href="https://github.com/grainulation/harvest/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-green" alt="license"></a> <a href="https://nodejs.org"><img src="https://img.shields.io/node/v/@grainulation/harvest" alt="node"></a> <a href="https://github.com/grainulation/harvest/actions"><img src="https://github.com/grainulation/harvest/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
6
+ <a href="https://www.npmjs.com/package/@grainulation/harvest"><img src="https://img.shields.io/npm/v/@grainulation/harvest?label=%40grainulation%2Fharvest" alt="npm version"></a> <a href="https://www.npmjs.com/package/@grainulation/harvest"><img src="https://img.shields.io/npm/dm/@grainulation/harvest" alt="npm downloads"></a> <a href="https://github.com/grainulation/harvest/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-green" alt="license"></a> <a href="https://nodejs.org"><img src="https://img.shields.io/node/v/@grainulation/harvest" alt="node"></a> <a href="https://github.com/grainulation/harvest/actions"><img src="https://github.com/grainulation/harvest/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
7
7
  <a href="https://deepwiki.com/grainulation/harvest"><img src="https://deepwiki.com/badge.svg" alt="Explore on DeepWiki"></a>
8
8
  </p>
9
9
 
package/lib/farmer.js CHANGED
@@ -5,13 +5,27 @@ const path = require("node:path");
5
5
  const http = require("node:http");
6
6
  const https = require("node:https");
7
7
 
8
+ /** Track whether we have already warned about missing token */
9
+ let _warnedNoToken = false;
10
+
8
11
  /**
9
12
  * POST an activity event to farmer.
10
13
  * Graceful failure -- catch and warn, never crash.
11
14
  * @param {string} farmerUrl - Base URL of farmer (e.g. http://localhost:9090)
12
15
  * @param {object} event - Event object (e.g. { type: "analyze", data: {...} })
16
+ * @param {object} [opts] - Options
17
+ * @param {string} [opts.token] - Bearer token for Authorization header
13
18
  */
14
- function notify(farmerUrl, event) {
19
+ function notify(farmerUrl, event, opts) {
20
+ const token = (opts && opts.token) || null;
21
+
22
+ if (!token && !_warnedNoToken) {
23
+ _warnedNoToken = true;
24
+ process.stderr.write(
25
+ "[harvest] no farmer token configured -- requests are unauthenticated\n",
26
+ );
27
+ }
28
+
15
29
  return new Promise((resolve) => {
16
30
  try {
17
31
  const payload = JSON.stringify({
@@ -23,16 +37,21 @@ function notify(farmerUrl, event) {
23
37
  const url = new URL(`${farmerUrl}/hooks/activity`);
24
38
  const transport = url.protocol === "https:" ? https : http;
25
39
 
40
+ const headers = {
41
+ "Content-Type": "application/json",
42
+ "Content-Length": Buffer.byteLength(payload),
43
+ };
44
+ if (token) {
45
+ headers["Authorization"] = `Bearer ${token}`;
46
+ }
47
+
26
48
  const req = transport.request(
27
49
  {
28
50
  hostname: url.hostname,
29
51
  port: url.port,
30
52
  path: url.pathname,
31
53
  method: "POST",
32
- headers: {
33
- "Content-Type": "application/json",
34
- "Content-Length": Buffer.byteLength(payload),
35
- },
54
+ headers,
36
55
  timeout: 5000,
37
56
  },
38
57
  (res) => {
@@ -76,7 +95,7 @@ async function connect(targetDir, args) {
76
95
  const subcommand = args[0];
77
96
  if (subcommand !== "farmer") {
78
97
  console.error(
79
- "Usage: harvest connect farmer [--url http://localhost:9090]",
98
+ "Usage: harvest connect farmer --url http://localhost:9090 [--token <t>]",
80
99
  );
81
100
  process.exit(1);
82
101
  }
@@ -86,16 +105,30 @@ async function connect(targetDir, args) {
86
105
  const urlIdx = args.indexOf("--url");
87
106
  if (urlIdx !== -1 && args[urlIdx + 1]) {
88
107
  const url = args[urlIdx + 1];
89
- const config = { url };
108
+
109
+ // Read optional --token flag
110
+ const tokenIdx = args.indexOf("--token");
111
+ const token =
112
+ tokenIdx !== -1 && args[tokenIdx + 1] ? args[tokenIdx + 1] : null;
113
+
114
+ const config = token ? { url, token } : { url };
90
115
  fs.writeFileSync(configPath, JSON.stringify(config, null, 2), "utf8");
91
116
  console.log(`Farmer connection saved to ${configPath}`);
92
117
  console.log(` URL: ${url}`);
118
+ if (token) {
119
+ console.log(" Token: configured");
120
+ } else {
121
+ console.log(
122
+ " Token: not configured (use --token <t> to enable authenticated requests)",
123
+ );
124
+ }
93
125
 
94
126
  // Test the connection
95
- const result = await notify(url, {
96
- type: "connect",
97
- data: { tool: "harvest" },
98
- });
127
+ const result = await notify(
128
+ url,
129
+ { type: "connect", data: { tool: "harvest" } },
130
+ { token },
131
+ );
99
132
  if (result.ok) {
100
133
  console.log(" Connection test: OK");
101
134
  } else {
@@ -113,11 +146,47 @@ async function connect(targetDir, args) {
113
146
  if (fs.existsSync(configPath)) {
114
147
  const config = JSON.parse(fs.readFileSync(configPath, "utf8"));
115
148
  console.log(`Farmer connection: ${config.url}`);
149
+ if (config.token) {
150
+ console.log(" Token: configured");
151
+ } else {
152
+ console.log(" Token: not configured");
153
+ }
116
154
  console.log(`Config: ${configPath}`);
117
155
  } else {
118
156
  console.log("No farmer connection configured.");
119
- console.log("Usage: harvest connect farmer --url http://localhost:9090");
157
+ console.log(
158
+ "Usage: harvest connect farmer --url http://localhost:9090 [--token <t>]",
159
+ );
160
+ }
161
+ }
162
+
163
+ /**
164
+ * Read .farmer.json from a directory.
165
+ * @param {string} dir - Directory containing .farmer.json
166
+ * @returns {{ url: string, token?: string } | null}
167
+ */
168
+ function loadConfig(dir) {
169
+ const configPath = path.join(dir, ".farmer.json");
170
+ if (!fs.existsSync(configPath)) return null;
171
+ try {
172
+ return JSON.parse(fs.readFileSync(configPath, "utf8"));
173
+ } catch {
174
+ return null;
175
+ }
176
+ }
177
+
178
+ /**
179
+ * Convenience: read .farmer.json and notify with auth if token exists.
180
+ * @param {string} dir - Directory containing .farmer.json
181
+ * @param {object} event - Event object
182
+ * @returns {Promise<{ok: boolean, status?: number, body?: string, error?: string}>}
183
+ */
184
+ function notifyFromConfig(dir, event) {
185
+ const config = loadConfig(dir);
186
+ if (!config || !config.url) {
187
+ return Promise.resolve({ ok: false, error: "no farmer configured" });
120
188
  }
189
+ return notify(config.url, event, { token: config.token || null });
121
190
  }
122
191
 
123
- module.exports = { connect, notify };
192
+ module.exports = { connect, notify, loadConfig, notifyFromConfig };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grainulation/harvest",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Analytics and retrospective layer for research sprints -- learn from every decision you've made",
5
5
  "main": "lib/analyzer.js",
6
6
  "exports": {
@@ -33,7 +33,6 @@
33
33
  ],
34
34
  "author": "grainulation contributors",
35
35
  "license": "MIT",
36
- "type": "module",
37
36
  "files": [
38
37
  "bin/",
39
38
  "lib/",