@composable-os/cli 0.0.0-stage → 0.1.0-beta.11
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/LICENSE +109 -0
- package/README.md +24 -2
- package/dist/accounts-UHYLCLEY.js +45 -0
- package/dist/blocks-5IFU25J6.js +233 -0
- package/dist/chunk-2ABSVMGB.js +13 -0
- package/dist/chunk-2IVEVPMY.js +669 -0
- package/dist/chunk-2TWV3SVB.js +349 -0
- package/dist/chunk-2XC5JLBO.js +1931 -0
- package/dist/chunk-324WJXNP.js +320 -0
- package/dist/chunk-5KSXSK7Y.js +47 -0
- package/dist/chunk-6ELZRNUU.js +116 -0
- package/dist/chunk-6UWRT4VM.js +220 -0
- package/dist/chunk-6WGFA2IV.js +643 -0
- package/dist/chunk-6XTU2MTP.js +11278 -0
- package/dist/chunk-7CQGMVDO.js +63 -0
- package/dist/chunk-7D4GKZOX.js +615 -0
- package/dist/chunk-7OGCMQXG.js +284 -0
- package/dist/chunk-DHS2CV2M.js +139 -0
- package/dist/chunk-DU6OIFOU.js +2358 -0
- package/dist/chunk-E4WCYTCT.js +347 -0
- package/dist/chunk-E64SG24J.js +88 -0
- package/dist/chunk-FJIZGS5Q.js +698 -0
- package/dist/chunk-GYTEW66E.js +50 -0
- package/dist/chunk-HDIOVFAZ.js +544 -0
- package/dist/chunk-K34UKWEI.js +298 -0
- package/dist/chunk-KGHA67HG.js +765 -0
- package/dist/chunk-LAAL4YGR.js +365 -0
- package/dist/chunk-LO6WGU5Y.js +26189 -0
- package/dist/chunk-MBIKVRAC.js +1425 -0
- package/dist/chunk-P6UWZ6QQ.js +426 -0
- package/dist/chunk-PDD2ETVF.js +1302 -0
- package/dist/chunk-UB4I2QKR.js +31575 -0
- package/dist/chunk-WHKJGF36.js +2418 -0
- package/dist/chunk-XIDLW3EP.js +15471 -0
- package/dist/chunk-YFTLIEWU.js +504 -0
- package/dist/chunk-YNUZRTPH.js +182 -0
- package/dist/chunk-ZAWES2UD.js +9364 -0
- package/dist/chunk-ZZFWMVJO.js +704 -0
- package/dist/cli-5UNY5G5L.js +136 -0
- package/dist/cli-UYZMVQMR.js +24534 -0
- package/dist/cli.js +25 -0
- package/dist/client-repo-SE4EKG6W.js +55 -0
- package/dist/create.js +25 -0
- package/dist/fields-FTWWMD7N.js +50 -0
- package/dist/grants-LOW6TSWD.js +32 -0
- package/dist/invoke-6KVT6WJI.js +41 -0
- package/dist/moves-E2HKYCTK.js +41 -0
- package/dist/order-U4KZLLSJ.js +19 -0
- package/dist/person-UHLVBVLR.js +67 -0
- package/dist/plan-on-sale-TJPFCNL7.js +26 -0
- package/dist/platform/migrations/0001_init.sql +59 -0
- package/dist/platform/migrations/0002_users_settings.sql +34 -0
- package/dist/platform/migrations/0003_people_organisations.sql +225 -0
- package/dist/platform/migrations/0004_activities_tasks.sql +88 -0
- package/dist/platform/migrations/0005_views_audit.sql +79 -0
- package/dist/platform/migrations/0006_search.sql +223 -0
- package/dist/platform/migrations/0007_crm_views.sql +141 -0
- package/dist/platform/migrations/0008_audit_redaction.sql +116 -0
- package/dist/platform/migrations/0010_approval_queue.sql +90 -0
- package/dist/platform/migrations/0011_connected_apps.sql +36 -0
- package/dist/platform/migrations/0020_extensions.sql +373 -0
- package/dist/platform/migrations/0030_extension_proposals.sql +29 -0
- package/dist/platform/migrations/0050_live_row_indexes.sql +32 -0
- package/dist/platform/migrations/0060_audit_read_counts.sql +31 -0
- package/dist/platform/migrations/0061_audit_retention.sql +95 -0
- package/dist/platform/migrations/0062_run_history_retention.sql +6 -0
- package/dist/platform/migrations/0063_pending_action_compaction.sql +18 -0
- package/dist/platform/migrations/0064_drop_rate_limit.sql +6 -0
- package/dist/platform/migrations/0065_storage_stats.sql +13 -0
- package/dist/platform/migrations/0070_drop_proposals_ext_idx.sql +9 -0
- package/dist/platform/migrations/0080_views_dashboards.sql +46 -0
- package/dist/platform/migrations/0100_stage_history.sql +54 -0
- package/dist/platform/migrations/0110_integrations.sql +103 -0
- package/dist/platform/migrations/0111_email_calendar_sync.sql +137 -0
- package/dist/platform/migrations/0112_sync_item_occurred_at.sql +8 -0
- package/dist/platform/migrations/0130_automations.sql +113 -0
- package/dist/platform/migrations/0131_notifications.sql +29 -0
- package/dist/platform/migrations/0132_automation_runtime.sql +14 -0
- package/dist/platform/migrations/0150_ai_summaries_embeddings.sql +48 -0
- package/dist/platform/migrations/0160_releases.sql +41 -0
- package/dist/platform/migrations/0161_setup_wizard.sql +30 -0
- package/dist/platform/migrations/0170_automation_connections.sql +32 -0
- package/dist/platform/migrations/0171_automation_outbox.sql +23 -0
- package/dist/platform/migrations/0172_notification_delivery.sql +46 -0
- package/dist/platform/migrations/0200_assistant_conversations.sql +25 -0
- package/dist/platform/migrations/0205_policy_adjudicator.sql +61 -0
- package/dist/platform/migrations/0210_daily_digest.sql +33 -0
- package/dist/platform/migrations/0220_entitlements.sql +13 -0
- package/dist/platform/migrations/0221_support_grants.sql +33 -0
- package/dist/platform/migrations/0250_judge.sql +11 -0
- package/dist/platform/migrations/0251_adjudication_judgement.sql +8 -0
- package/dist/platform/migrations/0252_judge_usage.sql +14 -0
- package/dist/platform/migrations/0255_summary_checks.sql +9 -0
- package/dist/platform/migrations/0256_outbound_check.sql +9 -0
- package/dist/platform/migrations/0260_ai_usage.sql +20 -0
- package/dist/platform/migrations/0270_assistant_conversation_refs.sql +29 -0
- package/dist/platform/migrations/0310_assignment_rules.sql +54 -0
- package/dist/platform/migrations/0320_duplicates.sql +41 -0
- package/dist/platform/migrations/0330_erase_completeness.sql +54 -0
- package/dist/platform/migrations/0350_assistant_thread.sql +38 -0
- package/dist/platform/migrations/0351_assistant_model_roles.sql +26 -0
- package/dist/platform/migrations/0352_assistant_memory.sql +74 -0
- package/dist/platform/migrations/0358_ai_cache_observation.sql +24 -0
- package/dist/platform/migrations/0359_ai_compaction_decision.sql +25 -0
- package/dist/platform/migrations/0360_ai_cache_estimate.sql +35 -0
- package/dist/platform/migrations/0361_voice_preferences.sql +27 -0
- package/dist/platform/migrations/0362_voice_usage.sql +17 -0
- package/dist/platform/migrations/0365_routines.sql +38 -0
- package/dist/platform/migrations/0370_approval_execution.sql +20 -0
- package/dist/platform/migrations/0371_agent_jobs.sql +65 -0
- package/dist/platform/migrations/0380_rec_views.sql +94 -0
- package/dist/platform/migrations/0400_customer_audit_actor.sql +142 -0
- package/dist/platform/migrations/0401_customer_accounts.sql +61 -0
- package/dist/platform/migrations/0402_erase_requests.sql +34 -0
- package/dist/platform/migrations/0410_idempotency_keys.sql +45 -0
- package/dist/platform/migrations/0411_device_sessions.sql +33 -0
- package/dist/platform/migrations/0420_transition_history.sql +100 -0
- package/dist/platform/migrations/0421_orders.sql +134 -0
- package/dist/platform/migrations/0422_order_payments.sql +109 -0
- package/dist/platform/migrations/0440_capacity.sql +100 -0
- package/dist/platform/migrations/0441_capacity_views.sql +40 -0
- package/dist/platform/migrations/0450_order_grants.sql +21 -0
- package/dist/platform/migrations/0451_pack_roles.sql +18 -0
- package/dist/platform/migrations/0460_content.sql +59 -0
- package/dist/platform/migrations/0461_media.sql +52 -0
- package/dist/platform/migrations/0470_publish_versions.sql +36 -0
- package/dist/platform/migrations/0471_permission_grants.sql +18 -0
- package/dist/platform/migrations/0490_staff_invites.sql +40 -0
- package/dist/platform/migrations/0491_sign_in_mail_kind.sql +10 -0
- package/dist/platform/migrations/0492_email_confirmation_and_sign_in_confirm.sql +30 -0
- package/dist/platform/migrations/0500_published_media_field.sql +25 -0
- package/dist/platform/migrations/0501_import_keys.sql +23 -0
- package/dist/platform/migrations/0510_video_and_file_media.sql +109 -0
- package/dist/platform/migrations/0516_order_tax.sql +63 -0
- package/dist/platform/migrations/0520_publish_requests.sql +20 -0
- package/dist/platform/migrations/0530_subscriptions.sql +173 -0
- package/dist/platform/migrations/0531_subscription_trials.sql +56 -0
- package/dist/platform/migrations/0532_subscription_offers.sql +80 -0
- package/dist/platform/migrations/0533_allowances.sql +262 -0
- package/dist/platform/migrations/0560_recurring.sql +75 -0
- package/dist/platform/migrations/0580_subscription_grants.sql +21 -0
- package/dist/platform/migrations/0581_once_per_person.sql +34 -0
- package/dist/platform/migrations/0600_tool_actions_ran_at_queue.sql +22 -0
- package/dist/platform/migrations/0610_waitlists.sql +54 -0
- package/dist/platform/migrations/0630_subscription_changes.sql +122 -0
- package/dist/platform/migrations/0650_person_links.sql +90 -0
- package/dist/platform/migrations/0670_unmoved_guard.sql +52 -0
- package/dist/platform/migrations/0690_sellable_refund_due.sql +98 -0
- package/dist/platform/sdk/allowance.ts +522 -0
- package/dist/platform/sdk/automation-check.ts +534 -0
- package/dist/platform/sdk/automation.ts +743 -0
- package/dist/platform/sdk/behaviour.ts +215 -0
- package/dist/platform/sdk/capacity.ts +303 -0
- package/dist/platform/sdk/contract.ts +909 -0
- package/dist/platform/sdk/document.ts +935 -0
- package/dist/platform/sdk/field-types.ts +111 -0
- package/dist/platform/sdk/formula/ast.ts +72 -0
- package/dist/platform/sdk/formula/compile.ts +454 -0
- package/dist/platform/sdk/formula/index.ts +4 -0
- package/dist/platform/sdk/formula/parser.ts +417 -0
- package/dist/platform/sdk/index.ts +36 -0
- package/dist/platform/sdk/manifest.ts +1785 -0
- package/dist/platform/sdk/media.ts +108 -0
- package/dist/platform/sdk/permissions.ts +60 -0
- package/dist/platform/sdk/recurring.ts +321 -0
- package/dist/platform/sdk/renamed.ts +63 -0
- package/dist/platform/sdk/rich-text-fixture.ts +68 -0
- package/dist/platform/sdk/rich-text.ts +221 -0
- package/dist/platform/sdk/runtime.ts +410 -0
- package/dist/platform/sdk/sellable.ts +1138 -0
- package/dist/platform/sdk/slug.ts +25 -0
- package/dist/platform/sdk/states.ts +598 -0
- package/dist/platform/sdk/testing.ts +847 -0
- package/dist/platform/sdk/unique.ts +81 -0
- package/dist/products-2H5DR232.js +11 -0
- package/dist/run-7P4RQALH.js +648 -0
- package/dist/run-PO3W5BFA.js +508 -0
- package/dist/run-ZCGFMEZF.js +309 -0
- package/dist/src-CRO35DYY.js +46 -0
- package/dist/src-I3IJ4NKC.js +354 -0
- package/dist/token-MOJUA2U6.js +70 -0
- package/dist/token-util-TENRN4V7.js +6 -0
- package/dist/visibility-TVXZ67FD.js +41 -0
- package/package.json +33 -4
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/import-door/sign-in.ts
|
|
4
|
+
import { execFile } from "node:child_process";
|
|
5
|
+
import { createServer } from "node:http";
|
|
6
|
+
import { hostname } from "node:os";
|
|
7
|
+
|
|
8
|
+
// ../client/src/binding.ts
|
|
9
|
+
var SESSION_CHECK_INTERVAL_MS = 24 * 60 * 60 * 1e3;
|
|
10
|
+
|
|
11
|
+
// ../client/src/errors.ts
|
|
12
|
+
var ApiError = class extends Error {
|
|
13
|
+
constructor(code, message, status, details) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.code = code;
|
|
16
|
+
this.status = status;
|
|
17
|
+
this.details = details;
|
|
18
|
+
}
|
|
19
|
+
code;
|
|
20
|
+
status;
|
|
21
|
+
details;
|
|
22
|
+
name = "ApiError";
|
|
23
|
+
};
|
|
24
|
+
var NetworkError = class extends Error {
|
|
25
|
+
constructor(message, cause) {
|
|
26
|
+
super(message);
|
|
27
|
+
this.cause = cause;
|
|
28
|
+
}
|
|
29
|
+
cause;
|
|
30
|
+
name = "NetworkError";
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// ../client/src/session.ts
|
|
34
|
+
async function post(fetcher, url, body, headers = {}) {
|
|
35
|
+
let response;
|
|
36
|
+
try {
|
|
37
|
+
response = await fetcher(url, {
|
|
38
|
+
method: "POST",
|
|
39
|
+
headers: { "content-type": "application/json", ...headers },
|
|
40
|
+
body: JSON.stringify(body)
|
|
41
|
+
});
|
|
42
|
+
} catch (error) {
|
|
43
|
+
throw new NetworkError("Could not reach the host to sign in", error);
|
|
44
|
+
}
|
|
45
|
+
const parsed = JSON.parse(await response.text());
|
|
46
|
+
if (response.status === 200 && parsed.data) return parsed.data;
|
|
47
|
+
throw new ApiError(
|
|
48
|
+
parsed.error?.code ?? "internal",
|
|
49
|
+
parsed.error?.message ?? `The host answered ${response.status}`,
|
|
50
|
+
response.status,
|
|
51
|
+
parsed.error?.details
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
function tokenSession(options) {
|
|
55
|
+
const base = options.baseUrl.replace(/\/+$/, "");
|
|
56
|
+
const fetcher = options.fetch ?? globalThis.fetch?.bind(globalThis);
|
|
57
|
+
const early = options.refreshEarlyMs ?? 3e4;
|
|
58
|
+
const now = options.now ?? (() => Date.now());
|
|
59
|
+
let refreshing = null;
|
|
60
|
+
async function refresh(tokens) {
|
|
61
|
+
try {
|
|
62
|
+
const next = await post(fetcher, `${base}/api/v1/auth/refresh`, {
|
|
63
|
+
refreshToken: tokens.refreshToken
|
|
64
|
+
});
|
|
65
|
+
await options.store.save(next);
|
|
66
|
+
return next;
|
|
67
|
+
} catch (error) {
|
|
68
|
+
if (error instanceof ApiError) await options.store.save(null);
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
/**
|
|
74
|
+
* Exchanges a sign-in for tokens. `credentials` are the headers that carry it, such as the
|
|
75
|
+
* cookie the sign-in answered with.
|
|
76
|
+
*/
|
|
77
|
+
async exchange(deviceName, credentials) {
|
|
78
|
+
const tokens = await post(fetcher, `${base}/api/v1/auth/token`, { deviceName }, credentials);
|
|
79
|
+
await options.store.save(tokens);
|
|
80
|
+
return tokens;
|
|
81
|
+
},
|
|
82
|
+
/**
|
|
83
|
+
* Asks the host to email a sign-in code (ADR 0455). The answer is the same whoever the
|
|
84
|
+
* address is; it throws only when the host refuses to send any (`rate_limited`, or sign-in
|
|
85
|
+
* not set up).
|
|
86
|
+
*/
|
|
87
|
+
async requestCode(email, audience = "customer") {
|
|
88
|
+
await post(fetcher, `${base}/api/v1/auth/${audience}/code`, { email });
|
|
89
|
+
},
|
|
90
|
+
/**
|
|
91
|
+
* Signs in with the emailed code and keeps the device session the host answers with. A wrong,
|
|
92
|
+
* used or expired code throws `forbidden` (`invalid_code`). No cookie is involved.
|
|
93
|
+
*/
|
|
94
|
+
async signInWithCode(email, code, deviceName, audience = "customer") {
|
|
95
|
+
const signedIn = await post(fetcher, `${base}/api/v1/auth/${audience}/sign-in`, {
|
|
96
|
+
email,
|
|
97
|
+
code,
|
|
98
|
+
deviceName
|
|
99
|
+
});
|
|
100
|
+
const { account: _account, ...tokens } = signedIn;
|
|
101
|
+
await options.store.save(tokens);
|
|
102
|
+
return signedIn;
|
|
103
|
+
},
|
|
104
|
+
/**
|
|
105
|
+
* Finishes a staff sign-in in the system browser (`startBrowserSignIn`): checks the state it
|
|
106
|
+
* was sent back with and exchanges the code, with the verifier, for a device session.
|
|
107
|
+
*/
|
|
108
|
+
async finishBrowserSignIn(input) {
|
|
109
|
+
const query = input.callbackUrl.includes("?") ? input.callbackUrl.split("?")[1] ?? "" : "";
|
|
110
|
+
const params = new Map(
|
|
111
|
+
query.split("&").filter(Boolean).map((pair) => {
|
|
112
|
+
const [key = "", value = ""] = pair.split("=");
|
|
113
|
+
return [decodeURIComponent(key), decodeURIComponent(value.replace(/\+/g, " "))];
|
|
114
|
+
})
|
|
115
|
+
);
|
|
116
|
+
const code = params.get("code");
|
|
117
|
+
if (!code || params.get("state") !== input.state) {
|
|
118
|
+
throw new ApiError(
|
|
119
|
+
"forbidden",
|
|
120
|
+
"The sign-in did not come back from this app's request",
|
|
121
|
+
401,
|
|
122
|
+
{
|
|
123
|
+
reason: "invalid_code"
|
|
124
|
+
}
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
const tokens = await post(fetcher, `${base}/api/v1/auth/staff/exchange`, {
|
|
128
|
+
code,
|
|
129
|
+
codeVerifier: input.verifier,
|
|
130
|
+
redirectUri: input.redirectUri,
|
|
131
|
+
deviceName: input.deviceName
|
|
132
|
+
});
|
|
133
|
+
await options.store.save(tokens);
|
|
134
|
+
return tokens;
|
|
135
|
+
},
|
|
136
|
+
/** The headers for a call: a current access token, refreshed first when it is about to end. */
|
|
137
|
+
async headers() {
|
|
138
|
+
let tokens = await options.store.load();
|
|
139
|
+
if (!tokens) return {};
|
|
140
|
+
if (Date.parse(tokens.accessExpiresAt) - early <= now()) {
|
|
141
|
+
refreshing ??= refresh(tokens).finally(() => {
|
|
142
|
+
refreshing = null;
|
|
143
|
+
});
|
|
144
|
+
tokens = await refreshing;
|
|
145
|
+
if (!tokens) return {};
|
|
146
|
+
}
|
|
147
|
+
return { authorization: `Bearer ${tokens.accessToken}` };
|
|
148
|
+
},
|
|
149
|
+
/**
|
|
150
|
+
* Ends this device's session on the host (ADR 0455) with its refresh token, which any account
|
|
151
|
+
* may do whatever it may call, then forgets the tokens, even when the host could not be
|
|
152
|
+
* reached.
|
|
153
|
+
*/
|
|
154
|
+
async signOut() {
|
|
155
|
+
const tokens = await options.store.load();
|
|
156
|
+
try {
|
|
157
|
+
if (tokens) {
|
|
158
|
+
await post(fetcher, `${base}/api/v1/auth/sign-out`, {
|
|
159
|
+
refreshToken: tokens.refreshToken
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
} catch {
|
|
163
|
+
} finally {
|
|
164
|
+
await options.store.save(null);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
function base64url(bytes) {
|
|
170
|
+
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
|
171
|
+
let out = "";
|
|
172
|
+
for (let i = 0; i < bytes.length; i += 3) {
|
|
173
|
+
const a = bytes[i] ?? 0;
|
|
174
|
+
const b = bytes[i + 1];
|
|
175
|
+
const c = bytes[i + 2];
|
|
176
|
+
out += alphabet[a >> 2];
|
|
177
|
+
out += alphabet[(a & 3) << 4 | (b ?? 0) >> 4];
|
|
178
|
+
if (b !== void 0) out += alphabet[(b & 15) << 2 | (c ?? 0) >> 6];
|
|
179
|
+
if (c !== void 0) out += alphabet[c & 63];
|
|
180
|
+
}
|
|
181
|
+
return out;
|
|
182
|
+
}
|
|
183
|
+
async function startBrowserSignIn(options) {
|
|
184
|
+
const verifier = `${options.randomKey()}${options.randomKey()}`.replace(/[^A-Za-z0-9._~-]/g, "").slice(0, 128);
|
|
185
|
+
if (verifier.length < 43) throw new Error("randomKey must give at least 22 characters");
|
|
186
|
+
const state = options.randomKey().replace(/[^A-Za-z0-9._~-]/g, "");
|
|
187
|
+
const challenge = base64url(await options.sha256(verifier));
|
|
188
|
+
const query = [
|
|
189
|
+
["redirect_uri", options.redirectUri],
|
|
190
|
+
["code_challenge", challenge],
|
|
191
|
+
["code_challenge_method", "S256"],
|
|
192
|
+
["state", state]
|
|
193
|
+
].map(([k, v]) => `${k}=${encodeURIComponent(v)}`).join("&");
|
|
194
|
+
return {
|
|
195
|
+
url: `${options.hostUrl.replace(/\/+$/, "")}/native/staff-sign-in?${query}`,
|
|
196
|
+
state,
|
|
197
|
+
verifier
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// src/import-door/sign-in.ts
|
|
202
|
+
var LOOPBACK_PATH = "/cos-sign-in";
|
|
203
|
+
function memoryStore() {
|
|
204
|
+
let saved = null;
|
|
205
|
+
return {
|
|
206
|
+
load: async () => saved,
|
|
207
|
+
save: async (tokens) => {
|
|
208
|
+
saved = tokens;
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
var hex = (bytes) => [...bytes].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
213
|
+
function openBrowser(url, io) {
|
|
214
|
+
io.out(`Sign in in your browser: ${url}`);
|
|
215
|
+
const command = process.platform === "darwin" ? "open" : process.platform === "win32" ? "explorer" : "xdg-open";
|
|
216
|
+
execFile(command, [url], () => {
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
async function loopback() {
|
|
220
|
+
let settle;
|
|
221
|
+
const callback = new Promise((resolve_) => {
|
|
222
|
+
settle = resolve_;
|
|
223
|
+
});
|
|
224
|
+
const server = createServer((req, res) => {
|
|
225
|
+
const url = new URL(req.url ?? "/", "http://127.0.0.1");
|
|
226
|
+
if (url.pathname !== LOOPBACK_PATH) {
|
|
227
|
+
res.writeHead(404).end();
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
res.writeHead(200, { "content-type": "text/plain; charset=utf-8" }).end("Signed in. You can close this tab and go back to the terminal.");
|
|
231
|
+
settle(`http://127.0.0.1${req.url}`);
|
|
232
|
+
});
|
|
233
|
+
await new Promise((done) => server.listen(0, "127.0.0.1", done));
|
|
234
|
+
const port = server.address().port;
|
|
235
|
+
return {
|
|
236
|
+
redirectUri: `http://127.0.0.1:${port}${LOOPBACK_PATH}`,
|
|
237
|
+
callback,
|
|
238
|
+
close: () => server.close()
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
async function signInForRun(host, email, io, label) {
|
|
242
|
+
const session = tokenSession({ baseUrl: host, store: memoryStore() });
|
|
243
|
+
const deviceName = `${label} on ${hostname()}`.slice(0, 100);
|
|
244
|
+
if (email) {
|
|
245
|
+
await session.requestCode(email, "staff");
|
|
246
|
+
io.out(
|
|
247
|
+
`If ${email} may sign in by code, a code is on its way. If none arrives in a minute or two, check on the host: the address is one an admin invited, or one you confirmed (your account menu, Confirm your email), and Settings \u2192 Users shows sign-in codes going out (Send sign-in codes without review). Or run without --email to sign in with the browser.`
|
|
248
|
+
);
|
|
249
|
+
const code = await io.ask(`The sign-in code sent to ${email}`, { secret: true });
|
|
250
|
+
await session.signInWithCode(email, code.trim(), deviceName, "staff");
|
|
251
|
+
return session;
|
|
252
|
+
}
|
|
253
|
+
const back = await loopback();
|
|
254
|
+
try {
|
|
255
|
+
const random = () => hex(crypto.getRandomValues(new Uint8Array(16)));
|
|
256
|
+
const started = await startBrowserSignIn({
|
|
257
|
+
hostUrl: host,
|
|
258
|
+
redirectUri: back.redirectUri,
|
|
259
|
+
randomKey: random,
|
|
260
|
+
sha256: async (text) => new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(text)))
|
|
261
|
+
});
|
|
262
|
+
openBrowser(started.url, io);
|
|
263
|
+
const callbackUrl = await back.callback;
|
|
264
|
+
await session.finishBrowserSignIn({
|
|
265
|
+
callbackUrl,
|
|
266
|
+
state: started.state,
|
|
267
|
+
verifier: started.verifier,
|
|
268
|
+
redirectUri: back.redirectUri,
|
|
269
|
+
deviceName
|
|
270
|
+
});
|
|
271
|
+
return session;
|
|
272
|
+
} finally {
|
|
273
|
+
back.close();
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
function confirmer(io, yes) {
|
|
277
|
+
return async (summary) => {
|
|
278
|
+
if (yes) return true;
|
|
279
|
+
if (!io.interactive) {
|
|
280
|
+
io.err(`${summary}. Pass --yes to confirm it when running from a script.`);
|
|
281
|
+
return false;
|
|
282
|
+
}
|
|
283
|
+
const answer = await io.ask(`${summary}. Go ahead? (yes/no)`, { default: "no" });
|
|
284
|
+
return /^y(es)?$/i.test(answer.trim());
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// src/import-door/host.ts
|
|
289
|
+
async function readError(response) {
|
|
290
|
+
const body = await response.json().catch(() => ({}));
|
|
291
|
+
return body.error?.message ?? `HTTP ${response.status}`;
|
|
292
|
+
}
|
|
293
|
+
function sessionHost(base, headers, fetcher = fetch, outside = fetcher) {
|
|
294
|
+
return {
|
|
295
|
+
async call(name, input) {
|
|
296
|
+
const response = await fetcher(`${base}/api/v1/op/${encodeURIComponent(name)}`, {
|
|
297
|
+
method: "POST",
|
|
298
|
+
headers: {
|
|
299
|
+
"content-type": "application/json",
|
|
300
|
+
"idempotency-key": crypto.randomUUID(),
|
|
301
|
+
...await headers()
|
|
302
|
+
},
|
|
303
|
+
body: JSON.stringify(input)
|
|
304
|
+
});
|
|
305
|
+
const body = await response.json();
|
|
306
|
+
if (!response.ok || body.error) {
|
|
307
|
+
throw Object.assign(new Error(body.error?.message ?? `${name}: HTTP ${response.status}`), {
|
|
308
|
+
code: body.error?.code,
|
|
309
|
+
details: body.error?.details
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
return body.data;
|
|
313
|
+
},
|
|
314
|
+
async upload(bytes, name) {
|
|
315
|
+
const response = await fetcher(`${base}/api/v1/media?${new URLSearchParams({ name })}`, {
|
|
316
|
+
method: "POST",
|
|
317
|
+
headers: { "content-type": "application/octet-stream", ...await headers() },
|
|
318
|
+
body: new Uint8Array(bytes)
|
|
319
|
+
});
|
|
320
|
+
const body = await response.json();
|
|
321
|
+
if (!response.ok || !body.data) {
|
|
322
|
+
throw new Error(`${name}: ${body.error?.message ?? `HTTP ${response.status}`}`);
|
|
323
|
+
}
|
|
324
|
+
return body.data;
|
|
325
|
+
},
|
|
326
|
+
async uploadCaption(id, language, label, vtt) {
|
|
327
|
+
const query = new URLSearchParams({ language, label });
|
|
328
|
+
const response = await fetcher(
|
|
329
|
+
`${base}/api/v1/media/${encodeURIComponent(id)}/captions?${query}`,
|
|
330
|
+
{
|
|
331
|
+
method: "POST",
|
|
332
|
+
headers: { "content-type": "text/vtt", ...await headers() },
|
|
333
|
+
body: new Uint8Array(vtt)
|
|
334
|
+
}
|
|
335
|
+
);
|
|
336
|
+
if (!response.ok) throw new Error(`captions (${language}): ${await readError(response)}`);
|
|
337
|
+
},
|
|
338
|
+
send: (url, init) => outside(url, init)
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
export {
|
|
343
|
+
LOOPBACK_PATH,
|
|
344
|
+
signInForRun,
|
|
345
|
+
confirmer,
|
|
346
|
+
sessionHost
|
|
347
|
+
};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
ConfigError
|
|
4
|
+
} from "./chunk-6XTU2MTP.js";
|
|
5
|
+
|
|
6
|
+
// ../core/src/orders/gateway.ts
|
|
7
|
+
var PAYMENTS_VENDOR = "payments";
|
|
8
|
+
var PAYMENTS_RESOURCE_PATTERN = "payments://merchant/:provider";
|
|
9
|
+
var notConnected = () => new ConfigError(
|
|
10
|
+
"Payments are not connected: an admin connects a payment account in Settings \u2192 Integrations"
|
|
11
|
+
);
|
|
12
|
+
async function paymentsTarget(db) {
|
|
13
|
+
const rows = await db.selectFrom("core_integration_connection").select(["id", "resources", "status", "scope"]).where("vendor", "=", PAYMENTS_VENDOR).where("scope", "=", "org").where("status", "=", "active").orderBy("created_at").execute();
|
|
14
|
+
for (const row of rows) {
|
|
15
|
+
let resources = [];
|
|
16
|
+
try {
|
|
17
|
+
const parsed = JSON.parse(row.resources);
|
|
18
|
+
if (Array.isArray(parsed)) resources = parsed;
|
|
19
|
+
} catch {
|
|
20
|
+
}
|
|
21
|
+
for (const bound of resources) {
|
|
22
|
+
if (bound.pattern !== PAYMENTS_RESOURCE_PATTERN || typeof bound.key !== "string") continue;
|
|
23
|
+
const provider = typeof bound.url === "string" ? providerOfUrl(bound.url) : null;
|
|
24
|
+
if (provider) return { connectionId: row.id, resource: bound.key, provider };
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
throw notConnected();
|
|
28
|
+
}
|
|
29
|
+
function providerOfUrl(url) {
|
|
30
|
+
const match = /^payments:\/\/merchant\/(stripe|fake)$/.exec(url);
|
|
31
|
+
return match ? match[1] : null;
|
|
32
|
+
}
|
|
33
|
+
function requireHostCalls(integrations) {
|
|
34
|
+
const call = integrations?.callAsHost;
|
|
35
|
+
if (!integrations || !call) {
|
|
36
|
+
throw new ConfigError("Payments need the integration host, which is not reachable here");
|
|
37
|
+
}
|
|
38
|
+
return call.bind(integrations);
|
|
39
|
+
}
|
|
40
|
+
async function openProviderCheckout(integrations, target, request) {
|
|
41
|
+
const call = requireHostCalls(integrations);
|
|
42
|
+
const result = await call({
|
|
43
|
+
connectionId: target.connectionId,
|
|
44
|
+
resource: target.resource,
|
|
45
|
+
method: "createCheckout",
|
|
46
|
+
args: [request],
|
|
47
|
+
purpose: "orders"
|
|
48
|
+
});
|
|
49
|
+
const value = result.value;
|
|
50
|
+
if (!value || typeof value.ref !== "string" || typeof value.url !== "string") {
|
|
51
|
+
throw new ConfigError("The payments gatekeeper did not open a checkout");
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
provider: value.provider === "stripe" ? "stripe" : target.provider,
|
|
55
|
+
ref: value.ref,
|
|
56
|
+
url: value.url,
|
|
57
|
+
expiresAt: typeof value.expiresAt === "string" ? value.expiresAt : null
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
async function requestProviderRefund(integrations, target, caller, request) {
|
|
61
|
+
if (!integrations) {
|
|
62
|
+
throw new ConfigError("Payments need the integration host, which is not reachable here");
|
|
63
|
+
}
|
|
64
|
+
return integrations.call({
|
|
65
|
+
connectionId: target.connectionId,
|
|
66
|
+
resource: target.resource,
|
|
67
|
+
method: "refund",
|
|
68
|
+
args: [request],
|
|
69
|
+
caller
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
async function callSubscriptionGatekeeper(integrations, target, method, request) {
|
|
73
|
+
const call = requireHostCalls(integrations);
|
|
74
|
+
return call({
|
|
75
|
+
connectionId: target.connectionId,
|
|
76
|
+
resource: target.resource,
|
|
77
|
+
method,
|
|
78
|
+
args: [request],
|
|
79
|
+
purpose: "orders"
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export {
|
|
84
|
+
paymentsTarget,
|
|
85
|
+
openProviderCheckout,
|
|
86
|
+
requestProviderRefund,
|
|
87
|
+
callSubscriptionGatekeeper
|
|
88
|
+
};
|