@atmyapp/cli 0.0.6 → 0.0.8

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.
@@ -0,0 +1,242 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __rest = (this && this.__rest) || function (s, e) {
12
+ var t = {};
13
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
14
+ t[p] = s[p];
15
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
16
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
17
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
18
+ t[p[i]] = s[p[i]];
19
+ }
20
+ return t;
21
+ };
22
+ Object.defineProperty(exports, "__esModule", { value: true });
23
+ exports.resolveSession = resolveSession;
24
+ exports.createAmaFetch = createAmaFetch;
25
+ exports.projectUrl = projectUrl;
26
+ exports.streamSse = streamSse;
27
+ exports.encodePathSegment = encodePathSegment;
28
+ const fetch_1 = require("@better-fetch/fetch");
29
+ const config_1 = require("./config");
30
+ function resolveSession(overrides = {}) {
31
+ var _a, _b, _c;
32
+ let config;
33
+ try {
34
+ config = (0, config_1.getConfig)();
35
+ }
36
+ catch (error) {
37
+ throw new Error("AMA session not configured. Run 'ama use' first or supply --url, --token, and --project-id options.");
38
+ }
39
+ const token = (_a = overrides.token) !== null && _a !== void 0 ? _a : config.token;
40
+ const projectId = (_b = overrides.projectId) !== null && _b !== void 0 ? _b : config.projectId;
41
+ const url = (_c = overrides.url) !== null && _c !== void 0 ? _c : config.url;
42
+ if (!token) {
43
+ throw new Error("Authentication token is missing. Run 'ama use' or provide it with --token.");
44
+ }
45
+ if (!url) {
46
+ throw new Error("Project URL is missing. Run 'ama use' or provide it with --url.");
47
+ }
48
+ if (!projectId) {
49
+ throw new Error("Project ID is missing. Ensure the URL contains /projects/{id} or pass --project-id.");
50
+ }
51
+ return {
52
+ token,
53
+ projectId,
54
+ url: stripTrailingSlashes(url),
55
+ };
56
+ }
57
+ function createAmaFetch(session) {
58
+ return (0, fetch_1.createFetch)({
59
+ baseURL: ensureTrailingSlash(session.url),
60
+ headers: {
61
+ Authorization: `Bearer ${session.token}`,
62
+ },
63
+ throw: false,
64
+ });
65
+ }
66
+ function projectUrl(session, subPath, query) {
67
+ const base = ensureTrailingSlash(session.url);
68
+ const cleanPath = subPath.replace(/^\/+/, "");
69
+ const url = new URL(cleanPath, base);
70
+ if (query) {
71
+ const params = new URLSearchParams();
72
+ for (const [key, value] of Object.entries(query)) {
73
+ if (value === undefined || value === null) {
74
+ continue;
75
+ }
76
+ params.append(key, typeof value === "boolean" ? String(value) : String(value));
77
+ }
78
+ const queryString = params.toString();
79
+ if (queryString) {
80
+ url.search = queryString;
81
+ }
82
+ }
83
+ return url.toString();
84
+ }
85
+ function streamSse(options) {
86
+ return __awaiter(this, void 0, void 0, function* () {
87
+ var _a;
88
+ const { url, onEvent, signal, fetchInit = {} } = options;
89
+ const { signal: initSignal, headers: initHeaders } = fetchInit, restInit = __rest(fetchInit, ["signal", "headers"]);
90
+ const headers = new Headers(initHeaders !== null && initHeaders !== void 0 ? initHeaders : {});
91
+ if (!headers.has("Accept")) {
92
+ headers.set("Accept", "text/event-stream");
93
+ }
94
+ const requestInit = Object.assign(Object.assign({ method: (_a = restInit.method) !== null && _a !== void 0 ? _a : "GET" }, restInit), { headers, signal: signal !== null && signal !== void 0 ? signal : initSignal });
95
+ const response = yield fetch(url, requestInit);
96
+ if (!response.ok) {
97
+ const errorBody = yield safeReadText(response);
98
+ throw new Error(`Request failed with status ${response.status}: ${errorBody !== null && errorBody !== void 0 ? errorBody : "<no body>"}`);
99
+ }
100
+ if (!response.body) {
101
+ throw new Error("Streaming response body is not available.");
102
+ }
103
+ const reader = response.body.getReader();
104
+ const decoder = new TextDecoder("utf-8");
105
+ let buffer = "";
106
+ while (true) {
107
+ const { done, value } = yield reader.read();
108
+ if (done) {
109
+ break;
110
+ }
111
+ buffer += decoder.decode(value, { stream: true });
112
+ buffer = yield dispatchSseBuffer(buffer, onEvent);
113
+ }
114
+ buffer += decoder.decode();
115
+ yield dispatchSseBuffer(buffer, onEvent, true);
116
+ });
117
+ }
118
+ function encodePathSegment(path) {
119
+ return path
120
+ .split("/")
121
+ .map((segment) => segment === "" || segment === "." ? segment : encodeURIComponent(segment))
122
+ .join("/");
123
+ }
124
+ function stripTrailingSlashes(value) {
125
+ return value.replace(/\/+$/, "");
126
+ }
127
+ function ensureTrailingSlash(value) {
128
+ return value.endsWith("/") ? value : `${value}/`;
129
+ }
130
+ function dispatchSseBuffer(buffer_1, onEvent_1) {
131
+ return __awaiter(this, arguments, void 0, function* (buffer, onEvent, flush = false) {
132
+ let working = buffer;
133
+ while (true) {
134
+ const delimiter = nextDelimiter(working);
135
+ if (!delimiter) {
136
+ break;
137
+ }
138
+ const { index, length } = delimiter;
139
+ const rawEvent = working.slice(0, index);
140
+ working = working.slice(index + length);
141
+ if (!rawEvent.trim()) {
142
+ continue;
143
+ }
144
+ const event = parseSseEvent(rawEvent);
145
+ if (event) {
146
+ yield onEvent(event);
147
+ }
148
+ }
149
+ if (flush && working.trim()) {
150
+ const event = parseSseEvent(working);
151
+ if (event) {
152
+ yield onEvent(event);
153
+ }
154
+ return "";
155
+ }
156
+ return working;
157
+ });
158
+ }
159
+ function nextDelimiter(buffer) {
160
+ const lfIndex = buffer.indexOf("\n\n");
161
+ const crlfIndex = buffer.indexOf("\r\n\r\n");
162
+ if (lfIndex === -1 && crlfIndex === -1) {
163
+ return null;
164
+ }
165
+ if (lfIndex === -1) {
166
+ return { index: crlfIndex, length: 4 };
167
+ }
168
+ if (crlfIndex === -1) {
169
+ return { index: lfIndex, length: 2 };
170
+ }
171
+ return lfIndex < crlfIndex
172
+ ? { index: lfIndex, length: 2 }
173
+ : { index: crlfIndex, length: 4 };
174
+ }
175
+ function parseSseEvent(raw) {
176
+ const trimmed = raw.trim();
177
+ if (!trimmed) {
178
+ return null;
179
+ }
180
+ const lines = trimmed.split(/\r?\n/);
181
+ const dataLines = [];
182
+ const event = {
183
+ data: "",
184
+ raw: trimmed,
185
+ };
186
+ for (const line of lines) {
187
+ if (!line) {
188
+ continue;
189
+ }
190
+ if (line.startsWith(":")) {
191
+ continue;
192
+ }
193
+ const separatorIndex = line.indexOf(":");
194
+ const field = separatorIndex === -1 ? line : line.slice(0, separatorIndex).trim();
195
+ const value = separatorIndex === -1
196
+ ? ""
197
+ : line
198
+ .slice(separatorIndex + 1)
199
+ .replace(/^\s+/, "");
200
+ switch (field) {
201
+ case "event":
202
+ event.event = value;
203
+ break;
204
+ case "data":
205
+ dataLines.push(value);
206
+ break;
207
+ case "id":
208
+ event.id = value;
209
+ break;
210
+ case "retry":
211
+ {
212
+ const numeric = Number(value);
213
+ if (!Number.isNaN(numeric)) {
214
+ event.retry = numeric;
215
+ }
216
+ }
217
+ break;
218
+ default:
219
+ break;
220
+ }
221
+ }
222
+ event.data = dataLines.join("\n");
223
+ if (event.data) {
224
+ try {
225
+ event.parsed = JSON.parse(event.data);
226
+ }
227
+ catch (error) {
228
+ // Non-JSON payloads are expected occasionally (e.g. [DONE])
229
+ }
230
+ }
231
+ return event;
232
+ }
233
+ function safeReadText(response) {
234
+ return __awaiter(this, void 0, void 0, function* () {
235
+ try {
236
+ return yield response.text();
237
+ }
238
+ catch (error) {
239
+ return null;
240
+ }
241
+ });
242
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atmyapp/cli",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {
@@ -28,7 +28,7 @@
28
28
  "license": "ISC",
29
29
  "description": "",
30
30
  "devDependencies": {
31
- "@atmyapp/core": "^0.0.6",
31
+ "@atmyapp/core": "^0.0.11",
32
32
  "@types/jest": "^29.5.14",
33
33
  "@typescript-eslint/eslint-plugin": "^8.32.1",
34
34
  "@typescript-eslint/parser": "^8.32.1",
@@ -40,6 +40,7 @@
40
40
  "typescript": "^5.8.3"
41
41
  },
42
42
  "dependencies": {
43
+ "@better-fetch/fetch": "^1.1.18",
43
44
  "chalk": "^4.1.2",
44
45
  "commander": "^14.0.0",
45
46
  "fast-glob": "^3.3.3",