@cosmoner/sdk 1.2.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/README.md +178 -0
- package/dist/client.d.ts +28 -0
- package/dist/config.d.ts +35 -0
- package/dist/deployment/spec.d.ts +108 -0
- package/dist/deployment/types.d.ts +85 -0
- package/dist/deployment/validate.d.ts +30 -0
- package/dist/errors.d.ts +76 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +1004 -0
- package/dist/index.mjs +946 -0
- package/dist/retry.d.ts +39 -0
- package/dist/services/email.d.ts +33 -0
- package/dist/services/webhooks.d.ts +190 -0
- package/dist/transport.d.ts +29 -0
- package/dist/version.d.ts +12 -0
- package/dist/webhooks/signature.d.ts +48 -0
- package/package.json +47 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,946 @@
|
|
|
1
|
+
// src/config.ts
|
|
2
|
+
var DEFAULT_BASE_URL = "https://api.cosmoner.com";
|
|
3
|
+
var DEFAULT_TIMEOUT_MS = 3e4;
|
|
4
|
+
var DEFAULT_MAX_RETRIES = 2;
|
|
5
|
+
function resolveConfig(config) {
|
|
6
|
+
if (!config.apiKey) throw new Error("apiKey is required");
|
|
7
|
+
if (config.projectId !== void 0 && !config.projectId) {
|
|
8
|
+
throw new Error("projectId is required");
|
|
9
|
+
}
|
|
10
|
+
const timeout = config.timeout ?? DEFAULT_TIMEOUT_MS;
|
|
11
|
+
const maxRetries = config.maxRetries ?? DEFAULT_MAX_RETRIES;
|
|
12
|
+
if (timeout <= 0) throw new Error("timeout must be greater than 0");
|
|
13
|
+
if (maxRetries < 0) throw new Error("maxRetries cannot be negative");
|
|
14
|
+
return {
|
|
15
|
+
apiKey: config.apiKey,
|
|
16
|
+
projectId: config.projectId,
|
|
17
|
+
baseUrl: (config.baseUrl ?? DEFAULT_BASE_URL).replace(/\/$/, ""),
|
|
18
|
+
timeout,
|
|
19
|
+
maxRetries
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function resolveProjectId(config, override) {
|
|
23
|
+
const projectId = override || config.projectId;
|
|
24
|
+
if (!projectId) {
|
|
25
|
+
throw new Error(
|
|
26
|
+
"projectId is required \u2014 pass it to the Cosmoner client or to this method"
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
return projectId;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// src/services/email.ts
|
|
33
|
+
var EmailService = class {
|
|
34
|
+
constructor(transport, config) {
|
|
35
|
+
this.transport = transport;
|
|
36
|
+
this.config = config;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Sends a transactional email and resolves with the API envelope.
|
|
40
|
+
*
|
|
41
|
+
* At least one of `html` or `text` is required.
|
|
42
|
+
*/
|
|
43
|
+
// eslint-disable-next-line require-await -- `async` makes the validation below reject rather than throw synchronously.
|
|
44
|
+
async send(params) {
|
|
45
|
+
if (!params.to) throw new Error("to is required");
|
|
46
|
+
if (!params.subject) throw new Error("subject is required");
|
|
47
|
+
if (!params.html && !params.text) {
|
|
48
|
+
throw new Error("Either html or text must be provided");
|
|
49
|
+
}
|
|
50
|
+
const projectId = resolveProjectId(this.config, params.projectId);
|
|
51
|
+
return this.transport.request(
|
|
52
|
+
"POST",
|
|
53
|
+
`/v1/projects/${projectId}/email/send`,
|
|
54
|
+
{
|
|
55
|
+
body: {
|
|
56
|
+
credentialId: params.credentialId,
|
|
57
|
+
to: params.to,
|
|
58
|
+
subject: params.subject,
|
|
59
|
+
html: params.html,
|
|
60
|
+
text: params.text,
|
|
61
|
+
replyTo: params.replyTo
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// src/webhooks/signature.ts
|
|
69
|
+
import { createHmac, timingSafeEqual } from "crypto";
|
|
70
|
+
|
|
71
|
+
// src/errors.ts
|
|
72
|
+
var CosmonerError = class extends Error {
|
|
73
|
+
constructor(status, code, message, options = {}) {
|
|
74
|
+
super(message);
|
|
75
|
+
this.name = "CosmonerError";
|
|
76
|
+
this.code = code;
|
|
77
|
+
this.status = status;
|
|
78
|
+
this.details = options.details;
|
|
79
|
+
this.requestId = options.requestId;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
var CosmonerConnectionError = class extends CosmonerError {
|
|
83
|
+
constructor(message, code = "CONNECTION_ERROR") {
|
|
84
|
+
super(0, code, message);
|
|
85
|
+
this.name = "CosmonerConnectionError";
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
var CosmonerTimeoutError = class extends CosmonerConnectionError {
|
|
89
|
+
constructor(message) {
|
|
90
|
+
super(message, "TIMEOUT");
|
|
91
|
+
this.name = "CosmonerTimeoutError";
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
var AuthenticationError = class extends CosmonerError {
|
|
95
|
+
constructor(status, code, message, options) {
|
|
96
|
+
super(status, code, message, options);
|
|
97
|
+
this.name = "AuthenticationError";
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
var InsufficientScopeError = class extends CosmonerError {
|
|
101
|
+
constructor(status, code, message, options) {
|
|
102
|
+
super(status, code, message, options);
|
|
103
|
+
this.name = "InsufficientScopeError";
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
var NotFoundError = class extends CosmonerError {
|
|
107
|
+
constructor(status, code, message, options) {
|
|
108
|
+
super(status, code, message, options);
|
|
109
|
+
this.name = "NotFoundError";
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
var ConflictError = class extends CosmonerError {
|
|
113
|
+
constructor(status, code, message, options) {
|
|
114
|
+
super(status, code, message, options);
|
|
115
|
+
this.name = "ConflictError";
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
var ValidationError = class extends CosmonerError {
|
|
119
|
+
constructor(status, code, message, options) {
|
|
120
|
+
super(status, code, message, options);
|
|
121
|
+
this.name = "ValidationError";
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
var RateLimitError = class extends CosmonerError {
|
|
125
|
+
constructor(status, code, message, options = {}) {
|
|
126
|
+
super(status, code, message, options);
|
|
127
|
+
this.name = "RateLimitError";
|
|
128
|
+
this.retryAfter = options.retryAfter;
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
var ServerError = class extends CosmonerError {
|
|
132
|
+
constructor(status, code, message, options) {
|
|
133
|
+
super(status, code, message, options);
|
|
134
|
+
this.name = "ServerError";
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
var WebhookSignatureError = class extends CosmonerError {
|
|
138
|
+
constructor(message) {
|
|
139
|
+
super(0, "WEBHOOK_SIGNATURE_INVALID", message);
|
|
140
|
+
this.name = "WebhookSignatureError";
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
function errorFromResponse(status, body, headers, retryAfter) {
|
|
144
|
+
const envelope = body ?? {};
|
|
145
|
+
const code = envelope.error?.code ?? "UNKNOWN";
|
|
146
|
+
const message = envelope.error?.message ?? "Unknown error";
|
|
147
|
+
const options = {
|
|
148
|
+
details: envelope.error?.details,
|
|
149
|
+
requestId: headers?.get("x-request-id") ?? void 0
|
|
150
|
+
};
|
|
151
|
+
switch (status) {
|
|
152
|
+
case 400:
|
|
153
|
+
case 422:
|
|
154
|
+
return new ValidationError(status, code, message, options);
|
|
155
|
+
case 401:
|
|
156
|
+
return new AuthenticationError(status, code, message, options);
|
|
157
|
+
case 403:
|
|
158
|
+
return new InsufficientScopeError(status, code, message, options);
|
|
159
|
+
case 404:
|
|
160
|
+
return new NotFoundError(status, code, message, options);
|
|
161
|
+
case 409:
|
|
162
|
+
return new ConflictError(status, code, message, options);
|
|
163
|
+
case 429:
|
|
164
|
+
return new RateLimitError(status, code, message, { ...options, retryAfter });
|
|
165
|
+
default:
|
|
166
|
+
return status >= 500 ? new ServerError(status, code, message, options) : new CosmonerError(status, code, message, options);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// src/webhooks/signature.ts
|
|
171
|
+
var SIGNATURE_HEADER = "x-cosmoner-signature";
|
|
172
|
+
var DELIVERY_ID_HEADER = "x-cosmoner-delivery-id";
|
|
173
|
+
var EVENT_TYPE_HEADER = "x-cosmoner-event";
|
|
174
|
+
var DEFAULT_TOLERANCE_SECONDS = 300;
|
|
175
|
+
function parseSignatureHeader(header) {
|
|
176
|
+
const parts = {};
|
|
177
|
+
for (const segment of header.split(",")) {
|
|
178
|
+
const [key, ...rest] = segment.split("=");
|
|
179
|
+
if (key) parts[key.trim()] = rest.join("=");
|
|
180
|
+
}
|
|
181
|
+
const timestamp = Number(parts.t);
|
|
182
|
+
const v1 = parts.v1;
|
|
183
|
+
if (!Number.isFinite(timestamp) || !v1) return null;
|
|
184
|
+
return { timestamp, v1 };
|
|
185
|
+
}
|
|
186
|
+
function constantTimeEquals(a, b) {
|
|
187
|
+
const left = Buffer.from(a, "utf8");
|
|
188
|
+
const right = Buffer.from(b, "utf8");
|
|
189
|
+
if (left.length !== right.length) return false;
|
|
190
|
+
return timingSafeEqual(left, right);
|
|
191
|
+
}
|
|
192
|
+
function verifyWebhookSignature(params) {
|
|
193
|
+
const { payload, signature, secret } = params;
|
|
194
|
+
const tolerance = params.toleranceSeconds ?? DEFAULT_TOLERANCE_SECONDS;
|
|
195
|
+
if (!signature || !secret) return false;
|
|
196
|
+
const parsed = parseSignatureHeader(signature);
|
|
197
|
+
if (!parsed) return false;
|
|
198
|
+
if (tolerance > 0) {
|
|
199
|
+
const ageSeconds = Math.abs(Math.floor(Date.now() / 1e3) - parsed.timestamp);
|
|
200
|
+
if (ageSeconds > tolerance) return false;
|
|
201
|
+
}
|
|
202
|
+
const body = typeof payload === "string" ? payload : payload.toString("utf8");
|
|
203
|
+
const expected = createHmac("sha256", secret).update(`${parsed.timestamp}.${body}`).digest("hex");
|
|
204
|
+
return constantTimeEquals(expected, parsed.v1);
|
|
205
|
+
}
|
|
206
|
+
function constructEvent(params) {
|
|
207
|
+
if (!params.signature) {
|
|
208
|
+
throw new WebhookSignatureError(`Missing ${SIGNATURE_HEADER} header`);
|
|
209
|
+
}
|
|
210
|
+
if (!params.secret) {
|
|
211
|
+
throw new WebhookSignatureError("Signing secret is required");
|
|
212
|
+
}
|
|
213
|
+
if (!parseSignatureHeader(params.signature)) {
|
|
214
|
+
throw new WebhookSignatureError(
|
|
215
|
+
`Malformed ${SIGNATURE_HEADER} header \u2014 expected "t=<seconds>,v1=<hex>"`
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
if (!verifyWebhookSignature(params)) {
|
|
219
|
+
throw new WebhookSignatureError(
|
|
220
|
+
"Webhook signature verification failed \u2014 the payload was not signed with this secret, or it is outside the replay window"
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
const body = typeof params.payload === "string" ? params.payload : params.payload.toString("utf8");
|
|
224
|
+
try {
|
|
225
|
+
return JSON.parse(body);
|
|
226
|
+
} catch {
|
|
227
|
+
throw new WebhookSignatureError("Webhook payload is not valid JSON");
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// src/services/webhooks.ts
|
|
232
|
+
var WEBHOOK_EVENT_TYPES = [
|
|
233
|
+
"email.sent",
|
|
234
|
+
"email.delivered",
|
|
235
|
+
"email.delivery_delayed",
|
|
236
|
+
"email.bounced",
|
|
237
|
+
"email.complained",
|
|
238
|
+
"email.opened",
|
|
239
|
+
"email.clicked",
|
|
240
|
+
"email.rejected",
|
|
241
|
+
"email.rendering_failed",
|
|
242
|
+
"email.domain_verified",
|
|
243
|
+
"email.sending_paused",
|
|
244
|
+
"app.deployed",
|
|
245
|
+
"app.failed",
|
|
246
|
+
"domain.verified",
|
|
247
|
+
"domain.expired",
|
|
248
|
+
"server.running",
|
|
249
|
+
"server.error",
|
|
250
|
+
"member.invited",
|
|
251
|
+
"member.joined"
|
|
252
|
+
];
|
|
253
|
+
var WebhooksService = class {
|
|
254
|
+
constructor(transport, config) {
|
|
255
|
+
this.transport = transport;
|
|
256
|
+
this.config = config;
|
|
257
|
+
}
|
|
258
|
+
/** Builds the collection route for the resolved project. */
|
|
259
|
+
basePath(projectId) {
|
|
260
|
+
return `/v1/projects/${resolveProjectId(this.config, projectId)}/webhooks`;
|
|
261
|
+
}
|
|
262
|
+
/** Lists every endpoint on the project, newest first. */
|
|
263
|
+
async list(params = {}) {
|
|
264
|
+
return this.transport.request(
|
|
265
|
+
"GET",
|
|
266
|
+
this.basePath(params.projectId)
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
/** Fetches one endpoint together with its delivery counts. */
|
|
270
|
+
async get(endpointId, params = {}) {
|
|
271
|
+
if (!endpointId) throw new Error("endpointId is required");
|
|
272
|
+
return this.transport.request(
|
|
273
|
+
"GET",
|
|
274
|
+
`${this.basePath(params.projectId)}/${endpointId}`
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Creates an endpoint and returns it with its signing secret.
|
|
279
|
+
*
|
|
280
|
+
* The secret is returned by this call alone — every later read gives only
|
|
281
|
+
* `secretHint`, so persist it before discarding the response.
|
|
282
|
+
*/
|
|
283
|
+
async create(params) {
|
|
284
|
+
if (!params.name) throw new Error("name is required");
|
|
285
|
+
if (!params.url) throw new Error("url is required");
|
|
286
|
+
if (!params.events?.length) throw new Error("At least one event is required");
|
|
287
|
+
return this.transport.request(
|
|
288
|
+
"POST",
|
|
289
|
+
this.basePath(params.projectId),
|
|
290
|
+
{
|
|
291
|
+
body: {
|
|
292
|
+
name: params.name,
|
|
293
|
+
url: params.url,
|
|
294
|
+
events: params.events,
|
|
295
|
+
description: params.description
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Updates an endpoint's configuration.
|
|
302
|
+
*
|
|
303
|
+
* Re-enabling a paused endpoint through `enabled: true` also clears its
|
|
304
|
+
* failure streak, so one more failure will not immediately re-pause it.
|
|
305
|
+
*/
|
|
306
|
+
async update(endpointId, params) {
|
|
307
|
+
if (!endpointId) throw new Error("endpointId is required");
|
|
308
|
+
if (params.events && params.events.length === 0) {
|
|
309
|
+
throw new Error("At least one event is required");
|
|
310
|
+
}
|
|
311
|
+
return this.transport.request(
|
|
312
|
+
"PATCH",
|
|
313
|
+
`${this.basePath(params.projectId)}/${endpointId}`,
|
|
314
|
+
{
|
|
315
|
+
body: {
|
|
316
|
+
name: params.name,
|
|
317
|
+
url: params.url,
|
|
318
|
+
events: params.events,
|
|
319
|
+
description: params.description,
|
|
320
|
+
enabled: params.enabled
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
/** Deletes an endpoint and its delivery history. */
|
|
326
|
+
async delete(endpointId, params = {}) {
|
|
327
|
+
if (!endpointId) throw new Error("endpointId is required");
|
|
328
|
+
return this.transport.request(
|
|
329
|
+
"DELETE",
|
|
330
|
+
`${this.basePath(params.projectId)}/${endpointId}`
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Clears an auto-pause and re-queues the backlog held while it was down.
|
|
335
|
+
*
|
|
336
|
+
* An endpoint pauses itself after ten consecutive failed deliveries.
|
|
337
|
+
*/
|
|
338
|
+
async resume(endpointId, params = {}) {
|
|
339
|
+
if (!endpointId) throw new Error("endpointId is required");
|
|
340
|
+
return this.transport.request(
|
|
341
|
+
"POST",
|
|
342
|
+
`${this.basePath(params.projectId)}/${endpointId}/resume`
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Issues a new signing secret and returns it in full.
|
|
347
|
+
*
|
|
348
|
+
* The previous secret stops verifying immediately, so deploy the new one
|
|
349
|
+
* before rotating if the receiver cannot tolerate rejected deliveries.
|
|
350
|
+
*/
|
|
351
|
+
async rotateSecret(endpointId, params = {}) {
|
|
352
|
+
if (!endpointId) throw new Error("endpointId is required");
|
|
353
|
+
return this.transport.request(
|
|
354
|
+
"POST",
|
|
355
|
+
`${this.basePath(params.projectId)}/${endpointId}/rotate-secret`
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Sends a synthetic event and reports what the endpoint answered.
|
|
360
|
+
*
|
|
361
|
+
* Defaults to the endpoint's first subscribed event. Test sends never move
|
|
362
|
+
* the failure streak in either direction.
|
|
363
|
+
*/
|
|
364
|
+
async test(endpointId, params = {}) {
|
|
365
|
+
if (!endpointId) throw new Error("endpointId is required");
|
|
366
|
+
return this.transport.request(
|
|
367
|
+
"POST",
|
|
368
|
+
`${this.basePath(params.projectId)}/${endpointId}/test`,
|
|
369
|
+
{ body: { eventType: params.eventType } }
|
|
370
|
+
);
|
|
371
|
+
}
|
|
372
|
+
/** Lists deliveries for an endpoint, newest first, cursor-paginated. */
|
|
373
|
+
async listDeliveries(endpointId, params = {}) {
|
|
374
|
+
if (!endpointId) throw new Error("endpointId is required");
|
|
375
|
+
return this.transport.request(
|
|
376
|
+
"GET",
|
|
377
|
+
`${this.basePath(params.projectId)}/${endpointId}/deliveries`,
|
|
378
|
+
{
|
|
379
|
+
query: {
|
|
380
|
+
status: params.status,
|
|
381
|
+
eventType: params.eventType,
|
|
382
|
+
limit: params.limit,
|
|
383
|
+
cursor: params.cursor
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
);
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Queues a fresh delivery carrying the same payload.
|
|
390
|
+
*
|
|
391
|
+
* The endpoint must be enabled — replaying into a paused endpoint fails
|
|
392
|
+
* rather than silently queueing behind the backlog.
|
|
393
|
+
*/
|
|
394
|
+
async replayDelivery(endpointId, deliveryId, params = {}) {
|
|
395
|
+
if (!endpointId) throw new Error("endpointId is required");
|
|
396
|
+
if (!deliveryId) throw new Error("deliveryId is required");
|
|
397
|
+
return this.transport.request(
|
|
398
|
+
"POST",
|
|
399
|
+
`${this.basePath(params.projectId)}/${endpointId}/deliveries/${deliveryId}/replay`
|
|
400
|
+
);
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Reports whether a received request carries a valid signature.
|
|
404
|
+
*
|
|
405
|
+
* Needs the raw request body: a re-serialized object will not match what
|
|
406
|
+
* was signed.
|
|
407
|
+
*/
|
|
408
|
+
verify(params) {
|
|
409
|
+
return verifyWebhookSignature(params);
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Verifies a received request and returns its parsed event envelope.
|
|
413
|
+
*
|
|
414
|
+
* Throws `WebhookSignatureError` when verification fails, so there is no
|
|
415
|
+
* payload to act on unless the signature held.
|
|
416
|
+
*/
|
|
417
|
+
constructEvent(params) {
|
|
418
|
+
return constructEvent(params);
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
// src/retry.ts
|
|
423
|
+
var IDEMPOTENT_METHODS = /* @__PURE__ */ new Set(["GET", "HEAD", "OPTIONS", "PUT", "DELETE"]);
|
|
424
|
+
var BACKOFF_BASE_MS = 500;
|
|
425
|
+
var BACKOFF_CAP_MS = 8e3;
|
|
426
|
+
function shouldRetry(maxRetries, decision) {
|
|
427
|
+
const { attempt, method, status, retryNonIdempotent = false } = decision;
|
|
428
|
+
if (attempt >= maxRetries) return false;
|
|
429
|
+
if (status === 429) return true;
|
|
430
|
+
const isRetryableFailure = status === null || status >= 500;
|
|
431
|
+
if (!isRetryableFailure) return false;
|
|
432
|
+
return IDEMPOTENT_METHODS.has(method.toUpperCase()) || retryNonIdempotent;
|
|
433
|
+
}
|
|
434
|
+
function backoffMs(attempt, retryAfterSeconds) {
|
|
435
|
+
if (retryAfterSeconds !== void 0 && retryAfterSeconds >= 0) {
|
|
436
|
+
return Math.min(retryAfterSeconds * 1e3, BACKOFF_CAP_MS);
|
|
437
|
+
}
|
|
438
|
+
const ceiling = Math.min(BACKOFF_BASE_MS * 2 ** attempt, BACKOFF_CAP_MS);
|
|
439
|
+
return Math.random() * ceiling;
|
|
440
|
+
}
|
|
441
|
+
function parseRetryAfter(headers) {
|
|
442
|
+
const raw = headers?.get("retry-after");
|
|
443
|
+
if (raw === null || raw === void 0) return void 0;
|
|
444
|
+
const seconds = Number(raw);
|
|
445
|
+
return Number.isFinite(seconds) ? seconds : void 0;
|
|
446
|
+
}
|
|
447
|
+
function sleep(ms) {
|
|
448
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// src/version.ts
|
|
452
|
+
var VERSION = "1.2.0";
|
|
453
|
+
var USER_AGENT = `cosmoner-node/${VERSION}`;
|
|
454
|
+
|
|
455
|
+
// src/transport.ts
|
|
456
|
+
var WRITE_METHODS = /* @__PURE__ */ new Set(["POST", "PUT", "PATCH", "DELETE"]);
|
|
457
|
+
function newIdempotencyKey() {
|
|
458
|
+
const cryptoRef = globalThis.crypto;
|
|
459
|
+
if (typeof cryptoRef?.randomUUID === "function") return cryptoRef.randomUUID();
|
|
460
|
+
return `${Date.now().toString(16)}-${Math.random().toString(16).slice(2)}`;
|
|
461
|
+
}
|
|
462
|
+
var Transport = class {
|
|
463
|
+
constructor(config) {
|
|
464
|
+
this.config = config;
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* Builds the header set for one request.
|
|
468
|
+
*
|
|
469
|
+
* The idempotency key is generated once per logical request and reused
|
|
470
|
+
* across retries, so a replayed write can be collapsed server-side. The API
|
|
471
|
+
* does not consume this header yet — it is sent now so that retries become
|
|
472
|
+
* safe for writes as soon as it does.
|
|
473
|
+
*/
|
|
474
|
+
headers(method, hasBody, idempotencyKey) {
|
|
475
|
+
const headers = {
|
|
476
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
477
|
+
Accept: "application/json",
|
|
478
|
+
"User-Agent": USER_AGENT
|
|
479
|
+
};
|
|
480
|
+
if (hasBody) headers["Content-Type"] = "application/json";
|
|
481
|
+
if (WRITE_METHODS.has(method.toUpperCase())) {
|
|
482
|
+
headers["Idempotency-Key"] = idempotencyKey ?? newIdempotencyKey();
|
|
483
|
+
}
|
|
484
|
+
return headers;
|
|
485
|
+
}
|
|
486
|
+
/** Joins a route path and query onto the configured base URL. */
|
|
487
|
+
url(path, query) {
|
|
488
|
+
const url = `${this.config.baseUrl}${path}`;
|
|
489
|
+
if (!query) return url;
|
|
490
|
+
const params = new URLSearchParams();
|
|
491
|
+
for (const [key, value] of Object.entries(query)) {
|
|
492
|
+
if (value !== void 0) params.set(key, String(value));
|
|
493
|
+
}
|
|
494
|
+
const search = params.toString();
|
|
495
|
+
return search ? `${url}?${search}` : url;
|
|
496
|
+
}
|
|
497
|
+
/** Parses a successful response body, or throws the mapped API error. */
|
|
498
|
+
async decode(response) {
|
|
499
|
+
let body;
|
|
500
|
+
try {
|
|
501
|
+
body = await response.json();
|
|
502
|
+
} catch {
|
|
503
|
+
body = void 0;
|
|
504
|
+
}
|
|
505
|
+
if (response.ok) {
|
|
506
|
+
if (body === void 0) {
|
|
507
|
+
throw new CosmonerError(
|
|
508
|
+
response.status,
|
|
509
|
+
"INVALID_RESPONSE",
|
|
510
|
+
"API returned a non-JSON response"
|
|
511
|
+
);
|
|
512
|
+
}
|
|
513
|
+
return body;
|
|
514
|
+
}
|
|
515
|
+
throw errorFromResponse(
|
|
516
|
+
response.status,
|
|
517
|
+
body,
|
|
518
|
+
response.headers,
|
|
519
|
+
parseRetryAfter(response.headers)
|
|
520
|
+
);
|
|
521
|
+
}
|
|
522
|
+
/** Issues a request, retrying transient failures, and returns the parsed body. */
|
|
523
|
+
async request(method, path, options = {}) {
|
|
524
|
+
const url = this.url(path, options.query);
|
|
525
|
+
const hasBody = options.body !== void 0;
|
|
526
|
+
const headers = this.headers(method, hasBody, options.idempotencyKey);
|
|
527
|
+
let attempt = 0;
|
|
528
|
+
for (; ; ) {
|
|
529
|
+
let response = null;
|
|
530
|
+
let transportError = null;
|
|
531
|
+
let status = null;
|
|
532
|
+
let retryAfter;
|
|
533
|
+
try {
|
|
534
|
+
response = await fetch(url, {
|
|
535
|
+
method,
|
|
536
|
+
headers,
|
|
537
|
+
body: hasBody ? JSON.stringify(options.body) : void 0,
|
|
538
|
+
signal: AbortSignal.timeout(this.config.timeout)
|
|
539
|
+
});
|
|
540
|
+
status = response.status;
|
|
541
|
+
retryAfter = parseRetryAfter(response.headers);
|
|
542
|
+
if (response.ok) return await this.decode(response);
|
|
543
|
+
} catch (err) {
|
|
544
|
+
const name = err?.name;
|
|
545
|
+
const message = err?.message ?? String(err);
|
|
546
|
+
transportError = name === "TimeoutError" || name === "AbortError" ? new CosmonerTimeoutError(`Request timed out: ${message}`) : new CosmonerConnectionError(`Request failed: ${message}`);
|
|
547
|
+
}
|
|
548
|
+
const retry = shouldRetry(this.config.maxRetries, {
|
|
549
|
+
attempt,
|
|
550
|
+
method,
|
|
551
|
+
status,
|
|
552
|
+
retryNonIdempotent: options.retryNonIdempotent
|
|
553
|
+
});
|
|
554
|
+
if (!retry) {
|
|
555
|
+
if (transportError) throw transportError;
|
|
556
|
+
return await this.decode(response);
|
|
557
|
+
}
|
|
558
|
+
await sleep(backoffMs(attempt, retryAfter));
|
|
559
|
+
attempt += 1;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
};
|
|
563
|
+
|
|
564
|
+
// src/client.ts
|
|
565
|
+
var Cosmoner = class {
|
|
566
|
+
constructor(config) {
|
|
567
|
+
this.config = resolveConfig(config);
|
|
568
|
+
this.apiKey = this.config.apiKey;
|
|
569
|
+
this.projectId = this.config.projectId;
|
|
570
|
+
this.baseUrl = this.config.baseUrl;
|
|
571
|
+
this.timeout = this.config.timeout;
|
|
572
|
+
this.maxRetries = this.config.maxRetries;
|
|
573
|
+
this.transport = new Transport(this.config);
|
|
574
|
+
this.email = new EmailService(this.transport, this.config);
|
|
575
|
+
this.webhooks = new WebhooksService(this.transport, this.config);
|
|
576
|
+
}
|
|
577
|
+
};
|
|
578
|
+
|
|
579
|
+
// src/deployment/spec.ts
|
|
580
|
+
var APP_SCHEMA_URL = "https://cosmoner.com/schemas/app.schema.json";
|
|
581
|
+
var DEPLOYMENT_VERSION = 1;
|
|
582
|
+
var MAX_DEPLOYMENT_BYTES = 64 * 1024;
|
|
583
|
+
var DEPLOYMENT_FILE_PATHS = [
|
|
584
|
+
".cosmoner/deployment.yaml",
|
|
585
|
+
".cosmoner/deployment.yml",
|
|
586
|
+
"deployment.yaml",
|
|
587
|
+
"deployment.yml",
|
|
588
|
+
".datablock/app.yaml",
|
|
589
|
+
".datablock/app.yml"
|
|
590
|
+
];
|
|
591
|
+
var ENV_KEY_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
592
|
+
var REF_PATTERN = /^[A-Z][A-Z0-9_]*$/;
|
|
593
|
+
var ENV_FIELDS = {
|
|
594
|
+
key: {
|
|
595
|
+
kind: "string",
|
|
596
|
+
required: true,
|
|
597
|
+
minLength: 1,
|
|
598
|
+
maxLength: 256,
|
|
599
|
+
pattern: ENV_KEY_PATTERN,
|
|
600
|
+
patternMessage: "Env var keys must start with a letter or underscore and contain only letters, numbers, or underscores"
|
|
601
|
+
},
|
|
602
|
+
value: { kind: "string" },
|
|
603
|
+
secret: { kind: "boolean" },
|
|
604
|
+
from_variable: {
|
|
605
|
+
kind: "string",
|
|
606
|
+
minLength: 1,
|
|
607
|
+
maxLength: 100,
|
|
608
|
+
pattern: REF_PATTERN,
|
|
609
|
+
patternMessage: "from_variable must name a project variable, e.g. PUBLIC_API_URL"
|
|
610
|
+
},
|
|
611
|
+
from_secret: {
|
|
612
|
+
kind: "string",
|
|
613
|
+
minLength: 1,
|
|
614
|
+
maxLength: 100,
|
|
615
|
+
pattern: REF_PATTERN,
|
|
616
|
+
patternMessage: "from_secret must name a stored secret, e.g. DATABASE_PASSWORD"
|
|
617
|
+
}
|
|
618
|
+
};
|
|
619
|
+
var BUILD_FIELDS = {
|
|
620
|
+
strategy: { kind: "string", enum: ["nixpacks", "docker"] },
|
|
621
|
+
command: { kind: "string", maxLength: 1024 },
|
|
622
|
+
output_dir: { kind: "string", maxLength: 512 }
|
|
623
|
+
};
|
|
624
|
+
var SERVICE_FIELDS = {
|
|
625
|
+
name: {
|
|
626
|
+
kind: "string",
|
|
627
|
+
required: true,
|
|
628
|
+
minLength: 1,
|
|
629
|
+
maxLength: 32,
|
|
630
|
+
pattern: /^[a-z0-9][a-z0-9-]*$/,
|
|
631
|
+
patternMessage: "Service names must be lowercase letters, numbers, or hyphens, and start with a letter or number"
|
|
632
|
+
},
|
|
633
|
+
type: { kind: "string", enum: ["service", "static"], default: "service" },
|
|
634
|
+
source_dir: { kind: "string", maxLength: 512 },
|
|
635
|
+
build: { kind: "object", fields: BUILD_FIELDS },
|
|
636
|
+
run_command: { kind: "string", maxLength: 1024 },
|
|
637
|
+
port: { kind: "integer", minimum: 1, maximum: 65535 },
|
|
638
|
+
http_port: { kind: "integer", minimum: 1, maximum: 65535, deprecated: true, replacedBy: "port" },
|
|
639
|
+
internal_port: { kind: "integer", minimum: 1, maximum: 65535, deprecated: true, replacedBy: "port" },
|
|
640
|
+
instance_size: { kind: "string", maxLength: 64 },
|
|
641
|
+
instances: { kind: "integer", minimum: 1, maximum: 10 },
|
|
642
|
+
autodeploy: { kind: "boolean" },
|
|
643
|
+
envs: { kind: "array", maxItems: 100, item: { kind: "object", fields: ENV_FIELDS } }
|
|
644
|
+
};
|
|
645
|
+
var ROOT_FIELDS = {
|
|
646
|
+
$schema: { kind: "string" },
|
|
647
|
+
version: {
|
|
648
|
+
kind: "const",
|
|
649
|
+
value: DEPLOYMENT_VERSION,
|
|
650
|
+
message: `Unsupported file version \u2014 this platform reads version ${DEPLOYMENT_VERSION} files`,
|
|
651
|
+
default: DEPLOYMENT_VERSION
|
|
652
|
+
},
|
|
653
|
+
name: { kind: "string", maxLength: 100 },
|
|
654
|
+
region: { kind: "string", maxLength: 32 },
|
|
655
|
+
environment: {
|
|
656
|
+
kind: "string",
|
|
657
|
+
enum: ["default", "development", "staging", "production"]
|
|
658
|
+
},
|
|
659
|
+
services: {
|
|
660
|
+
kind: "array",
|
|
661
|
+
required: true,
|
|
662
|
+
minItems: 1,
|
|
663
|
+
minItemsMessage: "At least one service is required",
|
|
664
|
+
maxItems: 10,
|
|
665
|
+
item: { kind: "object", fields: SERVICE_FIELDS }
|
|
666
|
+
}
|
|
667
|
+
};
|
|
668
|
+
var DEPLOYMENT_KEY_ORDER = {
|
|
669
|
+
root: Object.keys(ROOT_FIELDS),
|
|
670
|
+
service: Object.keys(SERVICE_FIELDS),
|
|
671
|
+
build: Object.keys(BUILD_FIELDS),
|
|
672
|
+
env: Object.keys(ENV_FIELDS)
|
|
673
|
+
};
|
|
674
|
+
|
|
675
|
+
// src/deployment/validate.ts
|
|
676
|
+
import { parse as parseYaml } from "yaml";
|
|
677
|
+
function isMapping(value) {
|
|
678
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
679
|
+
}
|
|
680
|
+
function formatPath(path) {
|
|
681
|
+
return path.length === 0 ? "(root)" : path.join(".");
|
|
682
|
+
}
|
|
683
|
+
var Issues = class {
|
|
684
|
+
constructor() {
|
|
685
|
+
this.all = [];
|
|
686
|
+
}
|
|
687
|
+
error(path, message) {
|
|
688
|
+
this.all.push({ path: formatPath(path), message, severity: "error" });
|
|
689
|
+
}
|
|
690
|
+
warn(path, message) {
|
|
691
|
+
this.all.push({ path: formatPath(path), message, severity: "warning" });
|
|
692
|
+
}
|
|
693
|
+
};
|
|
694
|
+
function checkValue(value, spec, path, issues) {
|
|
695
|
+
switch (spec.kind) {
|
|
696
|
+
case "string": {
|
|
697
|
+
if (typeof value !== "string") {
|
|
698
|
+
issues.error(path, "Expected a string");
|
|
699
|
+
return void 0;
|
|
700
|
+
}
|
|
701
|
+
if (spec.enum && !spec.enum.includes(value)) {
|
|
702
|
+
issues.error(path, `Must be one of: ${spec.enum.join(", ")}`);
|
|
703
|
+
return void 0;
|
|
704
|
+
}
|
|
705
|
+
if (spec.minLength !== void 0 && value.length < spec.minLength) {
|
|
706
|
+
issues.error(path, "Must not be empty");
|
|
707
|
+
return void 0;
|
|
708
|
+
}
|
|
709
|
+
if (spec.maxLength !== void 0 && value.length > spec.maxLength) {
|
|
710
|
+
issues.error(path, `Must be at most ${spec.maxLength} characters`);
|
|
711
|
+
return void 0;
|
|
712
|
+
}
|
|
713
|
+
if (spec.pattern && !spec.pattern.test(value)) {
|
|
714
|
+
issues.error(path, spec.patternMessage ?? `Must match ${spec.pattern.source}`);
|
|
715
|
+
return void 0;
|
|
716
|
+
}
|
|
717
|
+
return value;
|
|
718
|
+
}
|
|
719
|
+
case "integer": {
|
|
720
|
+
if (typeof value !== "number" || !Number.isInteger(value)) {
|
|
721
|
+
issues.error(path, "Expected an integer");
|
|
722
|
+
return void 0;
|
|
723
|
+
}
|
|
724
|
+
if (value < spec.minimum || value > spec.maximum) {
|
|
725
|
+
issues.error(path, `Must be between ${spec.minimum} and ${spec.maximum}`);
|
|
726
|
+
return void 0;
|
|
727
|
+
}
|
|
728
|
+
return value;
|
|
729
|
+
}
|
|
730
|
+
case "boolean": {
|
|
731
|
+
if (typeof value !== "boolean") {
|
|
732
|
+
issues.error(path, "Expected a boolean");
|
|
733
|
+
return void 0;
|
|
734
|
+
}
|
|
735
|
+
return value;
|
|
736
|
+
}
|
|
737
|
+
case "const": {
|
|
738
|
+
if (value !== spec.value) {
|
|
739
|
+
issues.error(path, spec.message);
|
|
740
|
+
return void 0;
|
|
741
|
+
}
|
|
742
|
+
return value;
|
|
743
|
+
}
|
|
744
|
+
case "object": {
|
|
745
|
+
if (!isMapping(value)) {
|
|
746
|
+
issues.error(path, "Expected a mapping");
|
|
747
|
+
return void 0;
|
|
748
|
+
}
|
|
749
|
+
const parsed = checkMapping(value, spec.fields, path, issues);
|
|
750
|
+
CROSS_FIELD_RULES.get(spec.fields)?.(parsed, value, path, issues);
|
|
751
|
+
return parsed;
|
|
752
|
+
}
|
|
753
|
+
case "array": {
|
|
754
|
+
if (!Array.isArray(value)) {
|
|
755
|
+
issues.error(path, "Expected a list");
|
|
756
|
+
return void 0;
|
|
757
|
+
}
|
|
758
|
+
if (spec.minItems !== void 0 && value.length < spec.minItems) {
|
|
759
|
+
issues.error(
|
|
760
|
+
path,
|
|
761
|
+
spec.minItemsMessage ?? `Must have at least ${spec.minItems} items`
|
|
762
|
+
);
|
|
763
|
+
return void 0;
|
|
764
|
+
}
|
|
765
|
+
if (spec.maxItems !== void 0 && value.length > spec.maxItems) {
|
|
766
|
+
issues.error(path, `Must have at most ${spec.maxItems} items`);
|
|
767
|
+
return void 0;
|
|
768
|
+
}
|
|
769
|
+
return value.map((item, index) => checkValue(item, spec.item, [...path, index], issues));
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
function checkMapping(value, table, path, issues) {
|
|
774
|
+
for (const key of Object.keys(value)) {
|
|
775
|
+
if (!(key in table)) {
|
|
776
|
+
issues.warn([...path, key], `Unknown field "${key}" \u2014 it will be ignored`);
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
const parsed = {};
|
|
780
|
+
for (const [key, spec] of Object.entries(table)) {
|
|
781
|
+
const fieldPath = [...path, key];
|
|
782
|
+
if (!(key in value) || value[key] === void 0 || value[key] === null) {
|
|
783
|
+
if (spec.required) issues.error(fieldPath, "Required");
|
|
784
|
+
else if (spec.default !== void 0) parsed[key] = spec.default;
|
|
785
|
+
continue;
|
|
786
|
+
}
|
|
787
|
+
if (spec.deprecated && spec.replacedBy) {
|
|
788
|
+
issues.warn(fieldPath, `${key} is deprecated \u2014 use ${spec.replacedBy}`);
|
|
789
|
+
}
|
|
790
|
+
const checked = checkValue(value[key], spec, fieldPath, issues);
|
|
791
|
+
if (checked !== void 0) parsed[key] = checked;
|
|
792
|
+
}
|
|
793
|
+
return parsed;
|
|
794
|
+
}
|
|
795
|
+
function checkEnvVar(env, raw, path, issues) {
|
|
796
|
+
const sources = ["value", "secret", "from_variable", "from_secret"].filter(
|
|
797
|
+
(key) => key === "secret" ? raw.secret === true : raw[key] !== void 0 && raw[key] !== null
|
|
798
|
+
);
|
|
799
|
+
if (sources.length === 0) {
|
|
800
|
+
issues.error([...path, "value"], "Set one of value, secret, from_variable, or from_secret");
|
|
801
|
+
return;
|
|
802
|
+
}
|
|
803
|
+
if (sources.length > 1) {
|
|
804
|
+
issues.error(
|
|
805
|
+
[...path, sources[1]],
|
|
806
|
+
`A variable takes its value from one place only \u2014 remove ${sources.slice(1).join(", ")}`
|
|
807
|
+
);
|
|
808
|
+
}
|
|
809
|
+
if (env.secret === true && env.value !== void 0) {
|
|
810
|
+
issues.error(
|
|
811
|
+
[...path, "value"],
|
|
812
|
+
"A secret value must not be committed \u2014 remove value, or link a stored secret with from_secret"
|
|
813
|
+
);
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
function checkService(service, _raw, path, issues) {
|
|
817
|
+
const isStatic = service.type === "static";
|
|
818
|
+
const build = isMapping(service.build) ? service.build : void 0;
|
|
819
|
+
if (isStatic && service.run_command !== void 0) {
|
|
820
|
+
issues.error([...path, "run_command"], "Static sites cannot define a run_command");
|
|
821
|
+
}
|
|
822
|
+
if (!isStatic && build?.output_dir !== void 0) {
|
|
823
|
+
issues.error([...path, "build", "output_dir"], "output_dir only applies to static sites");
|
|
824
|
+
}
|
|
825
|
+
if (isStatic && build?.strategy !== "docker" && service.port !== void 0) {
|
|
826
|
+
issues.error(
|
|
827
|
+
[...path, "port"],
|
|
828
|
+
"port does not apply to a static site \u2014 the platform serves it"
|
|
829
|
+
);
|
|
830
|
+
}
|
|
831
|
+
const legacyPorts = ["http_port", "internal_port"].filter(
|
|
832
|
+
(key) => service[key] !== void 0
|
|
833
|
+
);
|
|
834
|
+
if (service.port !== void 0 && legacyPorts.length > 0) {
|
|
835
|
+
issues.error(
|
|
836
|
+
[...path, legacyPorts[0]],
|
|
837
|
+
`port replaces ${legacyPorts.join(" and ")} \u2014 remove ${legacyPorts.length === 1 ? "it" : "them"}`
|
|
838
|
+
);
|
|
839
|
+
}
|
|
840
|
+
if (Array.isArray(service.envs)) {
|
|
841
|
+
const seen = /* @__PURE__ */ new Set();
|
|
842
|
+
service.envs.forEach((env, index) => {
|
|
843
|
+
if (!isMapping(env) || typeof env.key !== "string") return;
|
|
844
|
+
if (seen.has(env.key)) {
|
|
845
|
+
issues.error(
|
|
846
|
+
[...path, "envs", index, "key"],
|
|
847
|
+
`Duplicate environment variable "${env.key}"`
|
|
848
|
+
);
|
|
849
|
+
}
|
|
850
|
+
seen.add(env.key);
|
|
851
|
+
});
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
function checkRoot(document, path, issues) {
|
|
855
|
+
if (!Array.isArray(document.services)) return;
|
|
856
|
+
const seen = /* @__PURE__ */ new Set();
|
|
857
|
+
document.services.forEach((service, index) => {
|
|
858
|
+
if (!isMapping(service) || typeof service.name !== "string") return;
|
|
859
|
+
if (seen.has(service.name)) {
|
|
860
|
+
issues.error([...path, "services", index, "name"], `Duplicate service name "${service.name}"`);
|
|
861
|
+
}
|
|
862
|
+
seen.add(service.name);
|
|
863
|
+
});
|
|
864
|
+
}
|
|
865
|
+
var CROSS_FIELD_RULES = /* @__PURE__ */ new Map([
|
|
866
|
+
// The root's own rule is invoked directly by validateDeploymentDocument,
|
|
867
|
+
// which is where the document is walked from, so it is not listed here.
|
|
868
|
+
[ENV_FIELDS, checkEnvVar],
|
|
869
|
+
[SERVICE_FIELDS, checkService]
|
|
870
|
+
]);
|
|
871
|
+
function validateDeploymentDocument(document, options = {}) {
|
|
872
|
+
const issues = new Issues();
|
|
873
|
+
if (document === null || document === void 0) {
|
|
874
|
+
issues.error([], "File is empty");
|
|
875
|
+
return result(issues, null, options);
|
|
876
|
+
}
|
|
877
|
+
if (!isMapping(document)) {
|
|
878
|
+
issues.error([], "Expected a mapping at the top level of the file");
|
|
879
|
+
return result(issues, null, options);
|
|
880
|
+
}
|
|
881
|
+
const parsed = checkMapping(document, ROOT_FIELDS, [], issues);
|
|
882
|
+
checkRoot(parsed, [], issues);
|
|
883
|
+
return result(issues, parsed, options);
|
|
884
|
+
}
|
|
885
|
+
function validateDeployment(source, options = {}) {
|
|
886
|
+
const issues = new Issues();
|
|
887
|
+
if (new TextEncoder().encode(source).length > MAX_DEPLOYMENT_BYTES) {
|
|
888
|
+
issues.error([], `File exceeds the ${MAX_DEPLOYMENT_BYTES / 1024} KiB limit`);
|
|
889
|
+
return result(issues, null, options);
|
|
890
|
+
}
|
|
891
|
+
let document;
|
|
892
|
+
try {
|
|
893
|
+
document = parseYaml(source);
|
|
894
|
+
} catch (err) {
|
|
895
|
+
issues.error([], `Invalid YAML: ${err instanceof Error ? err.message : String(err)}`);
|
|
896
|
+
return result(issues, null, options);
|
|
897
|
+
}
|
|
898
|
+
return validateDeploymentDocument(document, options);
|
|
899
|
+
}
|
|
900
|
+
function result(issues, template, options) {
|
|
901
|
+
const hasErrors = issues.all.some((issue) => issue.severity === "error");
|
|
902
|
+
const blocking = options.strict ? issues.all.length > 0 : hasErrors;
|
|
903
|
+
return {
|
|
904
|
+
valid: !blocking,
|
|
905
|
+
issues: issues.all,
|
|
906
|
+
template: hasErrors ? null : template
|
|
907
|
+
};
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
// src/index.ts
|
|
911
|
+
var index_default = Cosmoner;
|
|
912
|
+
export {
|
|
913
|
+
APP_SCHEMA_URL,
|
|
914
|
+
AuthenticationError,
|
|
915
|
+
ConflictError,
|
|
916
|
+
Cosmoner,
|
|
917
|
+
CosmonerConnectionError,
|
|
918
|
+
CosmonerError,
|
|
919
|
+
CosmonerTimeoutError,
|
|
920
|
+
DEFAULT_BASE_URL,
|
|
921
|
+
DEFAULT_MAX_RETRIES,
|
|
922
|
+
DEFAULT_TIMEOUT_MS,
|
|
923
|
+
DEFAULT_TOLERANCE_SECONDS,
|
|
924
|
+
DELIVERY_ID_HEADER,
|
|
925
|
+
DEPLOYMENT_FILE_PATHS,
|
|
926
|
+
DEPLOYMENT_KEY_ORDER,
|
|
927
|
+
DEPLOYMENT_VERSION,
|
|
928
|
+
EVENT_TYPE_HEADER,
|
|
929
|
+
EmailService,
|
|
930
|
+
InsufficientScopeError,
|
|
931
|
+
MAX_DEPLOYMENT_BYTES,
|
|
932
|
+
NotFoundError,
|
|
933
|
+
RateLimitError,
|
|
934
|
+
SIGNATURE_HEADER,
|
|
935
|
+
ServerError,
|
|
936
|
+
VERSION,
|
|
937
|
+
ValidationError,
|
|
938
|
+
WEBHOOK_EVENT_TYPES,
|
|
939
|
+
WebhookSignatureError,
|
|
940
|
+
WebhooksService,
|
|
941
|
+
constructEvent,
|
|
942
|
+
index_default as default,
|
|
943
|
+
validateDeployment,
|
|
944
|
+
validateDeploymentDocument,
|
|
945
|
+
verifyWebhookSignature
|
|
946
|
+
};
|