@blocklive/cli 0.0.0-stage → 0.7.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/LICENSE +3 -0
- package/README.md +202 -2
- package/dist/browser.js +194 -0
- package/dist/catalog.js +1016 -0
- package/dist/change.js +181 -0
- package/dist/cli.js +1176 -0
- package/dist/config.js +105 -0
- package/dist/csv.js +76 -0
- package/dist/errors.js +75 -0
- package/dist/http.js +189 -0
- package/dist/idempotency.js +39 -0
- package/dist/image-file.js +95 -0
- package/dist/lock.js +186 -0
- package/dist/login.js +203 -0
- package/dist/main.js +49 -0
- package/dist/read.js +155 -0
- package/dist/reads/developer-request.js +31 -0
- package/dist/reads/developers.js +53 -0
- package/dist/reads/domains.js +84 -0
- package/dist/reads/events.js +956 -0
- package/dist/reads/index.js +47 -0
- package/dist/reads/invoices.js +177 -0
- package/dist/reads/payment-account.js +177 -0
- package/dist/reads/places.js +55 -0
- package/dist/reads/promo-import.js +183 -0
- package/dist/reads/promos.js +62 -0
- package/dist/reads/requests.js +119 -0
- package/dist/reads/tickets.js +386 -0
- package/dist/safe.js +88 -0
- package/dist/secrets.js +267 -0
- package/dist/stdin.js +72 -0
- package/dist/style.js +44 -0
- package/dist/unattended.js +311 -0
- package/package.json +30 -5
package/dist/lock.js
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.withLockSync = exports.withLock = exports.takeOver = void 0;
|
|
4
|
+
const crypto_1 = require("crypto");
|
|
5
|
+
const fs_1 = require("fs");
|
|
6
|
+
const path_1 = require("path");
|
|
7
|
+
const errors_1 = require("./errors");
|
|
8
|
+
// One process at a time per profile while it renews the sign-in: a refresh
|
|
9
|
+
// token used twice ends the sign-in, so two commands started together must
|
|
10
|
+
// not both spend it. The lock is a file made with O_EXCL holding this
|
|
11
|
+
// process's own token. Its holder touches it every few seconds; one left
|
|
12
|
+
// untouched for 30 seconds (its process died) is taken over. A holder only
|
|
13
|
+
// ever removes its own.
|
|
14
|
+
//
|
|
15
|
+
// Taking over is done under a second file made with O_EXCL, <lock>.takeover
|
|
16
|
+
// (takeOver): one waiter at a time looks at the lock again there, and
|
|
17
|
+
// removes it only if it is still the stale file it saw (the same file, the
|
|
18
|
+
// same holder, still untouched for 30 seconds). So two waiters that both
|
|
19
|
+
// found it stale can't both take it, and a live lock (touched in time, or
|
|
20
|
+
// another file claimed in its place) is never removed. A waiter that finds
|
|
21
|
+
// .takeover held tries again in a moment. .takeover is held for a few
|
|
22
|
+
// system calls; one left for 10 seconds (its process died there) is stale
|
|
23
|
+
// too, moved aside and removed only if it is still that file.
|
|
24
|
+
//
|
|
25
|
+
// withLockSync is the same lock for a moment's work that can't wait for the
|
|
26
|
+
// event loop: the sign-in file's read and write (secrets.ts).
|
|
27
|
+
const STALE_MS = 30000;
|
|
28
|
+
const BEAT_MS = 5000;
|
|
29
|
+
const STEP_MS = 100;
|
|
30
|
+
const TAKEOVER_STALE_MS = 10000;
|
|
31
|
+
function claim(path, mine) {
|
|
32
|
+
try {
|
|
33
|
+
const fd = (0, fs_1.openSync)(path, 'wx', 0o600);
|
|
34
|
+
try {
|
|
35
|
+
(0, fs_1.writeSync)(fd, mine);
|
|
36
|
+
}
|
|
37
|
+
finally {
|
|
38
|
+
(0, fs_1.closeSync)(fd);
|
|
39
|
+
}
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
if (error.code !== 'EEXIST')
|
|
44
|
+
throw error;
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function holder(path) {
|
|
49
|
+
try {
|
|
50
|
+
return (0, fs_1.readFileSync)(path, 'utf8');
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function seen(path) {
|
|
57
|
+
try {
|
|
58
|
+
const stat = (0, fs_1.statSync)(path);
|
|
59
|
+
return { ino: stat.ino, at: stat.mtimeMs, holder: holder(path) };
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
// A .takeover whose waiter died inside: moved aside (one rename, so only one
|
|
66
|
+
// waiter moves it), and removed only if it is the stale one; one claimed in
|
|
67
|
+
// its place meanwhile goes back.
|
|
68
|
+
function clearStaleGuard(guard, mine, now) {
|
|
69
|
+
const found = seen(guard);
|
|
70
|
+
if (!found || now() - found.at <= TAKEOVER_STALE_MS)
|
|
71
|
+
return;
|
|
72
|
+
const aside = `${guard}.${mine}.stale`;
|
|
73
|
+
try {
|
|
74
|
+
(0, fs_1.renameSync)(guard, aside);
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
try {
|
|
80
|
+
if ((0, fs_1.statSync)(aside).ino !== found.ino)
|
|
81
|
+
try {
|
|
82
|
+
(0, fs_1.linkSync)(aside, guard);
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
// Another holds it now.
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
finally {
|
|
89
|
+
(0, fs_1.rmSync)(aside, { force: true });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Removes the stale lock a waiter found at path (`found`), if it still is:
|
|
94
|
+
* the same file, the same holder, untouched for staleMs. True when this
|
|
95
|
+
* waiter removed it (and may claim the place); false when another waiter is
|
|
96
|
+
* taking over now, got there first, or a live lock is there (left as it
|
|
97
|
+
* was).
|
|
98
|
+
*/
|
|
99
|
+
function takeOver(path, found, mine, { staleMs = STALE_MS, now = Date.now } = {}) {
|
|
100
|
+
const guard = `${path}.takeover`;
|
|
101
|
+
if (!claim(guard, mine)) {
|
|
102
|
+
clearStaleGuard(guard, mine, now);
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
try {
|
|
106
|
+
const there = seen(path);
|
|
107
|
+
if (!there ||
|
|
108
|
+
there.ino !== found.ino ||
|
|
109
|
+
there.holder !== found.holder ||
|
|
110
|
+
now() - there.at <= staleMs)
|
|
111
|
+
return false;
|
|
112
|
+
(0, fs_1.rmSync)(path, { force: true });
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
finally {
|
|
116
|
+
if (holder(guard) === mine)
|
|
117
|
+
(0, fs_1.rmSync)(guard, { force: true });
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
exports.takeOver = takeOver;
|
|
121
|
+
async function withLock(path, wait, work, { timeoutMs = 15000, now = Date.now } = {}) {
|
|
122
|
+
(0, fs_1.mkdirSync)((0, path_1.dirname)(path), { recursive: true, mode: 0o700 });
|
|
123
|
+
const mine = (0, crypto_1.randomBytes)(16).toString('hex');
|
|
124
|
+
const ends = now() + timeoutMs;
|
|
125
|
+
while (!claim(path, mine)) {
|
|
126
|
+
const found = seen(path);
|
|
127
|
+
// Let go meanwhile: claim it.
|
|
128
|
+
if (!found)
|
|
129
|
+
continue;
|
|
130
|
+
if (now() - found.at > STALE_MS && takeOver(path, found, mine, { now }))
|
|
131
|
+
continue;
|
|
132
|
+
if (now() >= ends)
|
|
133
|
+
throw new errors_1.CliError('Another blocklive command is renewing this sign-in. Try again in a moment.');
|
|
134
|
+
await wait(STEP_MS);
|
|
135
|
+
}
|
|
136
|
+
const beat = setInterval(() => {
|
|
137
|
+
try {
|
|
138
|
+
if (holder(path) === mine)
|
|
139
|
+
(0, fs_1.utimesSync)(path, new Date(), new Date());
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
// Gone: release below finds it so too.
|
|
143
|
+
}
|
|
144
|
+
}, BEAT_MS);
|
|
145
|
+
beat.unref?.();
|
|
146
|
+
try {
|
|
147
|
+
return await work();
|
|
148
|
+
}
|
|
149
|
+
finally {
|
|
150
|
+
clearInterval(beat);
|
|
151
|
+
if (holder(path) === mine)
|
|
152
|
+
(0, fs_1.rmSync)(path, { force: true });
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
exports.withLock = withLock;
|
|
156
|
+
const pause = (ms) => {
|
|
157
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
158
|
+
};
|
|
159
|
+
/**
|
|
160
|
+
* The lock for work of a few system calls, waiting without the event loop:
|
|
161
|
+
* stale after 5 seconds untouched, given up after 10.
|
|
162
|
+
*/
|
|
163
|
+
function withLockSync(path, work, { staleMs = 5000, timeoutMs = 10000, now = Date.now, sleep = pause } = {}) {
|
|
164
|
+
(0, fs_1.mkdirSync)((0, path_1.dirname)(path), { recursive: true, mode: 0o700 });
|
|
165
|
+
const mine = (0, crypto_1.randomBytes)(16).toString('hex');
|
|
166
|
+
const ends = now() + timeoutMs;
|
|
167
|
+
while (!claim(path, mine)) {
|
|
168
|
+
const found = seen(path);
|
|
169
|
+
if (!found)
|
|
170
|
+
continue;
|
|
171
|
+
if (now() - found.at > staleMs &&
|
|
172
|
+
takeOver(path, found, mine, { staleMs, now }))
|
|
173
|
+
continue;
|
|
174
|
+
if (now() >= ends)
|
|
175
|
+
throw new errors_1.CliError('Another blocklive command is saving a sign-in. Try again in a moment.');
|
|
176
|
+
sleep(25);
|
|
177
|
+
}
|
|
178
|
+
try {
|
|
179
|
+
return work();
|
|
180
|
+
}
|
|
181
|
+
finally {
|
|
182
|
+
if (holder(path) === mine)
|
|
183
|
+
(0, fs_1.rmSync)(path, { force: true });
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
exports.withLockSync = withLockSync;
|
package/dist/login.js
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.deviceLogin = exports.browserLogin = exports.typedHere = exports.answerWindowMs = exports.ANSWER_GRACE_SECONDS = void 0;
|
|
4
|
+
const crypto_1 = require("crypto");
|
|
5
|
+
const os_1 = require("os");
|
|
6
|
+
const errors_1 = require("./errors");
|
|
7
|
+
const http_1 = require("./http");
|
|
8
|
+
const secrets_1 = require("./secrets");
|
|
9
|
+
const client = () => ({
|
|
10
|
+
version: http_1.VERSION,
|
|
11
|
+
os: (0, os_1.platform)(),
|
|
12
|
+
hostname: (0, os_1.hostname)().slice(0, 64),
|
|
13
|
+
});
|
|
14
|
+
function done(pair) {
|
|
15
|
+
const tokens = (0, secrets_1.splitTokens)(`${pair.access_token}:${pair.refresh_token}`);
|
|
16
|
+
if (!tokens || !pair.session?.id)
|
|
17
|
+
throw new errors_1.CliError('Blocklive sent an unexpected answer. Try again.');
|
|
18
|
+
return { tokens, session: pair.session };
|
|
19
|
+
}
|
|
20
|
+
// The page is always on the Blocklive this CLI talks to, whatever an answer
|
|
21
|
+
// says, and shown as its host.
|
|
22
|
+
const shown = (baseUrl) => new URL(baseUrl).host;
|
|
23
|
+
// How long to wait for an answer: the time Blocklive takes one in
|
|
24
|
+
// (expires_in, 5 minutes today; at most 15), and half a minute more, so an
|
|
25
|
+
// answer given at the last moment still finds this CLI waiting.
|
|
26
|
+
exports.ANSWER_GRACE_SECONDS = 30;
|
|
27
|
+
function answerWindowMs(expiresIn) {
|
|
28
|
+
const seconds = Number(expiresIn) > 0 ? Number(expiresIn) : 300;
|
|
29
|
+
return (Math.min(seconds, 900) + exports.ANSWER_GRACE_SECONDS) * 1000;
|
|
30
|
+
}
|
|
31
|
+
exports.answerWindowMs = answerWindowMs;
|
|
32
|
+
// The code goes on the page, never in the terminal: anything typed here
|
|
33
|
+
// before Enter gets that said (Enter still opens the page). Also while
|
|
34
|
+
// `login --unattended` waits for Activate (cli.ts).
|
|
35
|
+
function typedHere(enter, say, style, where) {
|
|
36
|
+
enter?.typed?.((text) => {
|
|
37
|
+
if (text.trim())
|
|
38
|
+
say(` ${style.purple('◆')} Type the code on ${where}, not here.`);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
exports.typedHere = typedHere;
|
|
42
|
+
async function browserLogin({ client: api, baseUrl, browser, wait, say, style, }) {
|
|
43
|
+
const verifier = (0, crypto_1.randomBytes)(32).toString('base64url');
|
|
44
|
+
const state = (0, crypto_1.randomBytes)(16).toString('base64url');
|
|
45
|
+
const callback = await browser.listen(state);
|
|
46
|
+
let outcome = 'failed';
|
|
47
|
+
let enter = undefined;
|
|
48
|
+
try {
|
|
49
|
+
const start = await api.post('/login', {
|
|
50
|
+
client: client(),
|
|
51
|
+
browser: {
|
|
52
|
+
code_challenge: (0, crypto_1.createHash)('sha256')
|
|
53
|
+
.update(verifier)
|
|
54
|
+
.digest('base64url'),
|
|
55
|
+
port: callback.port,
|
|
56
|
+
state,
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
if (typeof start.link !== 'string' ||
|
|
60
|
+
!/^[0-9a-f]{64}$/.test(start.link) ||
|
|
61
|
+
typeof start.check_code !== 'string' ||
|
|
62
|
+
!/^[A-Z]{4}-[A-Z]{4}$/.test(start.check_code))
|
|
63
|
+
throw new errors_1.CliError('Blocklive sent an unexpected answer. Try again.');
|
|
64
|
+
// The check code goes nowhere but this terminal: never in the link.
|
|
65
|
+
const url = `${baseUrl}/cli#login=${start.link}`;
|
|
66
|
+
enter = browser.enter?.();
|
|
67
|
+
say('');
|
|
68
|
+
for (const line of style.block([
|
|
69
|
+
style.bold('Sign in to Blocklive'),
|
|
70
|
+
'',
|
|
71
|
+
enter
|
|
72
|
+
? `Press ${style.key('Enter')} to open ${shown(baseUrl)} in your browser.`
|
|
73
|
+
: 'Open the link below in a browser on this computer.',
|
|
74
|
+
'Type this code on that page, not here:',
|
|
75
|
+
'',
|
|
76
|
+
` ${style.code(start.check_code)}`,
|
|
77
|
+
'',
|
|
78
|
+
style.dim(enter ? `Or open: ${url}` : url),
|
|
79
|
+
style.dim('Two-step verification comes first. Never type a code someone sent you.'),
|
|
80
|
+
style.dim('No browser here? Run blocklive login --no-browser'),
|
|
81
|
+
]))
|
|
82
|
+
say(line);
|
|
83
|
+
say('');
|
|
84
|
+
say(` ${style.purple('◆')} Waiting for you to approve it in the browser… ${style.dim('Ctrl-C to cancel')}`);
|
|
85
|
+
typedHere(enter, say, style, 'the page in your browser');
|
|
86
|
+
void enter?.pressed.then(() => {
|
|
87
|
+
if (!browser.open(url))
|
|
88
|
+
say(" Couldn't open a browser here: open the link above.");
|
|
89
|
+
});
|
|
90
|
+
const answer = await callback.wait(answerWindowMs(start.expires_in));
|
|
91
|
+
enter?.stop();
|
|
92
|
+
if (!answer)
|
|
93
|
+
throw new errors_1.CliError('The sign-in expired before it was approved. Run `blocklive login` again.', errors_1.EXIT.refused, 'expired_token');
|
|
94
|
+
if ('refused' in answer) {
|
|
95
|
+
outcome = 'refused';
|
|
96
|
+
throw new errors_1.CliError('The sign-in was refused.', errors_1.EXIT.refused, 'access_denied');
|
|
97
|
+
}
|
|
98
|
+
const signedIn = done(await redeem(api, { grant: answer.grant, verifier, state }, wait));
|
|
99
|
+
outcome = 'signed_in';
|
|
100
|
+
return signedIn;
|
|
101
|
+
}
|
|
102
|
+
finally {
|
|
103
|
+
enter?.stop();
|
|
104
|
+
// Signed in, the tab goes on to the Blocklive this CLI uses (its own
|
|
105
|
+
// base address, never one from the answer).
|
|
106
|
+
callback.finish(outcome, `${baseUrl}/cli/signed-in`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
exports.browserLogin = browserLogin;
|
|
110
|
+
// The grant is good for 2 minutes and is spent once Blocklive reads it: try
|
|
111
|
+
// again only when the request didn't get there. It goes back with the proof
|
|
112
|
+
// and the state it came with.
|
|
113
|
+
async function redeem(api, { grant, verifier, state, }, wait) {
|
|
114
|
+
for (let attempt = 1;; attempt += 1) {
|
|
115
|
+
try {
|
|
116
|
+
return await api.post('/login/token', {
|
|
117
|
+
grant,
|
|
118
|
+
code_verifier: verifier,
|
|
119
|
+
state,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
catch (error) {
|
|
123
|
+
const code = error instanceof errors_1.CliError ? error.code : undefined;
|
|
124
|
+
if (code === 'unreachable' && attempt < 3) {
|
|
125
|
+
await wait(2000);
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (code === 'expired_token' || code === 'invalid_grant')
|
|
129
|
+
throw new errors_1.CliError('Blocklive didn’t accept this sign-in. Run `blocklive login` again.', errors_1.EXIT.refused, code);
|
|
130
|
+
throw error;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
async function deviceLogin({ client: api, baseUrl, browser, wait, say, style, now = Date.now, }) {
|
|
135
|
+
const start = await api.post('/login', { client: client() });
|
|
136
|
+
if (typeof start.device_code !== 'string' ||
|
|
137
|
+
!/^[A-Z]{4}-[A-Z]{4}$/.test(start.user_code))
|
|
138
|
+
throw new errors_1.CliError('Blocklive sent an unexpected answer. Try again.');
|
|
139
|
+
// The code is printed for the person to type: no link with it filled in,
|
|
140
|
+
// so a code someone else sends you can't be approved with one click.
|
|
141
|
+
const page = `${baseUrl}/cli`;
|
|
142
|
+
const enter = browser?.enter?.();
|
|
143
|
+
say('');
|
|
144
|
+
for (const line of style.block([
|
|
145
|
+
style.bold('Sign in to Blocklive'),
|
|
146
|
+
'',
|
|
147
|
+
enter
|
|
148
|
+
? `Press ${style.key('Enter')} to open ${shown(baseUrl)}/cli in your browser.`
|
|
149
|
+
: `On any device, open ${page} in a browser.`,
|
|
150
|
+
'Type this code on that page, not here:',
|
|
151
|
+
'',
|
|
152
|
+
` ${style.code(start.user_code)}`,
|
|
153
|
+
'',
|
|
154
|
+
style.dim('Never type a code someone sent you.'),
|
|
155
|
+
]))
|
|
156
|
+
say(line);
|
|
157
|
+
say('');
|
|
158
|
+
say('Waiting for you to approve it there (two-step verification needed)…');
|
|
159
|
+
typedHere(enter, say, style, `${shown(baseUrl)}/cli`);
|
|
160
|
+
void enter?.pressed.then(() => {
|
|
161
|
+
if (!browser.open(page))
|
|
162
|
+
say(` Couldn't open a browser here: open ${page} yourself.`);
|
|
163
|
+
});
|
|
164
|
+
try {
|
|
165
|
+
return await poll(api, start, wait, now);
|
|
166
|
+
}
|
|
167
|
+
finally {
|
|
168
|
+
enter?.stop();
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
exports.deviceLogin = deviceLogin;
|
|
172
|
+
// The device code is sent in the body, never in a URL.
|
|
173
|
+
async function poll(api, start, wait, now) {
|
|
174
|
+
const ends = now() + answerWindowMs(start.expires_in);
|
|
175
|
+
let interval = Math.max(start.interval, 5);
|
|
176
|
+
while (now() < ends) {
|
|
177
|
+
await wait(interval * 1000);
|
|
178
|
+
try {
|
|
179
|
+
return done(await api.post('/login/poll', {
|
|
180
|
+
device_code: start.device_code,
|
|
181
|
+
}));
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
const code = error instanceof errors_1.CliError ? error.code : undefined;
|
|
185
|
+
// A dropped connection or a moment's outage isn't an answer: the
|
|
186
|
+
// code stays good until it expires.
|
|
187
|
+
if (code === 'authorization_pending' ||
|
|
188
|
+
code === 'unreachable' ||
|
|
189
|
+
code === 'unavailable')
|
|
190
|
+
continue;
|
|
191
|
+
if (code === 'slow_down') {
|
|
192
|
+
interval += 5;
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
if (code === 'access_denied')
|
|
196
|
+
throw new errors_1.CliError('The sign-in was refused.', errors_1.EXIT.refused, code);
|
|
197
|
+
if (code === 'expired_token')
|
|
198
|
+
break;
|
|
199
|
+
throw error;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
throw new errors_1.CliError('The code expired before it was approved. Run `blocklive login` again.', errors_1.EXIT.refused, 'expired_token');
|
|
203
|
+
}
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
const cli_1 = require("./cli");
|
|
5
|
+
const style_1 = require("./style");
|
|
6
|
+
// The browser's pieces (a local HTTP server, the system's opener) and
|
|
7
|
+
// readline are loaded only when a command uses them, so `--version`, `help`,
|
|
8
|
+
// `commands` and reads start without them.
|
|
9
|
+
const browser = () => require('./browser');
|
|
10
|
+
const stdin = () => require('./stdin');
|
|
11
|
+
// A reader that stops early (`| head`) ends the output, without a stack trace.
|
|
12
|
+
for (const stream of [process.stdout, process.stderr])
|
|
13
|
+
stream.on('error', (error) => {
|
|
14
|
+
if (error.code !== 'EPIPE')
|
|
15
|
+
throw error;
|
|
16
|
+
process.exit(Number(process.exitCode ?? 0));
|
|
17
|
+
});
|
|
18
|
+
// A question on the terminal; none when stdin isn't one (a script, a pipe).
|
|
19
|
+
const ask = process.stdin.isTTY
|
|
20
|
+
? (question) => new Promise((resolve) => {
|
|
21
|
+
const { createInterface } = require('readline');
|
|
22
|
+
const lines = createInterface({
|
|
23
|
+
input: process.stdin,
|
|
24
|
+
output: process.stderr,
|
|
25
|
+
});
|
|
26
|
+
lines.question(question, (answer) => {
|
|
27
|
+
lines.close();
|
|
28
|
+
resolve(answer);
|
|
29
|
+
});
|
|
30
|
+
})
|
|
31
|
+
: undefined;
|
|
32
|
+
(0, cli_1.runCli)(process.argv.slice(2), {
|
|
33
|
+
out: (line) => process.stdout.write(`${line}\n`),
|
|
34
|
+
err: (line) => process.stderr.write(`${line}\n`),
|
|
35
|
+
env: process.env,
|
|
36
|
+
platform: process.platform,
|
|
37
|
+
fetch,
|
|
38
|
+
wait: (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
|
|
39
|
+
color: (0, style_1.colorFor)(process.stderr, process.env),
|
|
40
|
+
browser: {
|
|
41
|
+
listen: (state) => browser().listenForCallback(state),
|
|
42
|
+
open: (url) => browser().openInBrowser(url),
|
|
43
|
+
enter: () => browser().onEnter(process.stdin),
|
|
44
|
+
},
|
|
45
|
+
ask,
|
|
46
|
+
readSecret: (prompt) => stdin().readSecretLine(process.stdin, process.stderr, prompt),
|
|
47
|
+
}).then((code) => {
|
|
48
|
+
process.exitCode = code;
|
|
49
|
+
});
|
package/dist/read.js
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.pages = exports.named = exports.day = exports.table = exports.whenSpan = exports.dayWords = exports.when = exports.linkedJson = exports.withUrls = exports.blocklivePath = void 0;
|
|
4
|
+
const errors_1 = require("./errors");
|
|
5
|
+
// A page on the Blocklive this CLI talks to, as an address: a path there
|
|
6
|
+
// (/manage/…, /cli/confirm#request=…), never another site (//elsewhere,
|
|
7
|
+
// @elsewhere). Undefined for anything else.
|
|
8
|
+
function blocklivePath(base, path) {
|
|
9
|
+
if (typeof path !== 'string' ||
|
|
10
|
+
!/^\/(?!\/)[A-Za-z0-9\-._~!$&'()*+,;=:@%\/?#]*$/.test(path))
|
|
11
|
+
return undefined;
|
|
12
|
+
try {
|
|
13
|
+
return new URL(`${base}${path}`).origin === base
|
|
14
|
+
? `${base}${path}`
|
|
15
|
+
: undefined;
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.blocklivePath = blocklivePath;
|
|
22
|
+
/** An answer with `url` beside each Blocklive `path` it has, for --json. */
|
|
23
|
+
function withUrls(base, value) {
|
|
24
|
+
if (Array.isArray(value))
|
|
25
|
+
return value.map((v) => withUrls(base, v));
|
|
26
|
+
if (!value || typeof value !== 'object')
|
|
27
|
+
return value;
|
|
28
|
+
const out = {};
|
|
29
|
+
for (const [key, item] of Object.entries(value))
|
|
30
|
+
out[key] = withUrls(base, item);
|
|
31
|
+
const url = 'url' in out ? undefined : blocklivePath(base, out.path);
|
|
32
|
+
if (url)
|
|
33
|
+
out.url = url;
|
|
34
|
+
return out;
|
|
35
|
+
}
|
|
36
|
+
exports.withUrls = withUrls;
|
|
37
|
+
/** One line of --json output, with withUrls. */
|
|
38
|
+
function linkedJson(base, line) {
|
|
39
|
+
try {
|
|
40
|
+
return JSON.stringify(withUrls(base, JSON.parse(line)));
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return line;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.linkedJson = linkedJson;
|
|
47
|
+
// A time as people read it, in the event's own time zone: 13 Nov 2026, 18:00.
|
|
48
|
+
function when(at, timeZone) {
|
|
49
|
+
if (at === null || at === undefined || at === '')
|
|
50
|
+
return '';
|
|
51
|
+
const time = new Date(Number(at));
|
|
52
|
+
if (Number.isNaN(time.getTime()))
|
|
53
|
+
return '';
|
|
54
|
+
const zone = timeZone?.replace(/^'+|'+(::text)?$/g, '');
|
|
55
|
+
try {
|
|
56
|
+
return time.toLocaleString('en-GB', {
|
|
57
|
+
timeZone: zone || 'UTC',
|
|
58
|
+
dateStyle: 'medium',
|
|
59
|
+
timeStyle: 'short',
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return time.toISOString().slice(0, 16).replace('T', ' ') + ' UTC';
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
exports.when = when;
|
|
67
|
+
/** Days as people read them: "Day 1, Day 2". */
|
|
68
|
+
function dayWords(days) {
|
|
69
|
+
return (days ?? []).map((day) => `Day ${day}`).join(', ');
|
|
70
|
+
}
|
|
71
|
+
exports.dayWords = dayWords;
|
|
72
|
+
/**
|
|
73
|
+
* An event's start and end: "5 Dec 2026, 18:00 – 21:30", or with both dates
|
|
74
|
+
* when it ends on another day. Without an end, the start alone. A multi-day
|
|
75
|
+
* event (2 or 3 days) reads as its dates and how many days:
|
|
76
|
+
* "4 Dec 2026 – 5 Dec 2026 · 2 days".
|
|
77
|
+
*/
|
|
78
|
+
function whenSpan(startsAt, endsAt, timeZone, days) {
|
|
79
|
+
if (days && days.length >= 2) {
|
|
80
|
+
const first = when(days[0].startsAt, timeZone).split(', ')[0];
|
|
81
|
+
const last = when(days[days.length - 1].startsAt, timeZone).split(', ')[0];
|
|
82
|
+
if (first && last)
|
|
83
|
+
return `${first} – ${last} · ${days.length} days`;
|
|
84
|
+
}
|
|
85
|
+
const start = when(startsAt, timeZone);
|
|
86
|
+
const end = when(endsAt, timeZone);
|
|
87
|
+
if (!start || !end || Number(endsAt) <= Number(startsAt))
|
|
88
|
+
return start;
|
|
89
|
+
const [startDay] = start.split(', ');
|
|
90
|
+
const [endDay, endTime] = end.split(', ');
|
|
91
|
+
return endDay === startDay && endTime
|
|
92
|
+
? `${start} – ${endTime}`
|
|
93
|
+
: `${start} – ${end}`;
|
|
94
|
+
}
|
|
95
|
+
exports.whenSpan = whenSpan;
|
|
96
|
+
function table(rows) {
|
|
97
|
+
if (!rows.length)
|
|
98
|
+
return [];
|
|
99
|
+
const widths = rows[0].map((_, i) => Math.max(...rows.map((r) => (r[i] ?? '').length)));
|
|
100
|
+
return rows.map((r) => r
|
|
101
|
+
.map((cell, i) => cell.padEnd(widths[i]))
|
|
102
|
+
.join(' ')
|
|
103
|
+
.trimEnd());
|
|
104
|
+
}
|
|
105
|
+
exports.table = table;
|
|
106
|
+
// A day, as people write it: 7 Nov 2026. Empty when it isn't a time.
|
|
107
|
+
function day(at) {
|
|
108
|
+
if (at === null || at === undefined)
|
|
109
|
+
return '';
|
|
110
|
+
const time = new Date(typeof at === 'number' ? at : String(at));
|
|
111
|
+
return Number.isNaN(time.getTime())
|
|
112
|
+
? ''
|
|
113
|
+
: time.toLocaleDateString('en-GB', {
|
|
114
|
+
timeZone: 'UTC',
|
|
115
|
+
day: 'numeric',
|
|
116
|
+
month: 'short',
|
|
117
|
+
year: 'numeric',
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
exports.day = day;
|
|
121
|
+
// The one thing a read is about, by its ID or slug.
|
|
122
|
+
function named(rest, expected, what) {
|
|
123
|
+
const args = rest[0] === expected ? rest.slice(1) : rest;
|
|
124
|
+
if (args.length !== 1 || !/^[A-Za-z0-9][A-Za-z0-9-]{0,199}$/.test(args[0]))
|
|
125
|
+
throw (0, errors_1.usage)(`Name the ${what}, by its ID or slug.`);
|
|
126
|
+
return args[0];
|
|
127
|
+
}
|
|
128
|
+
exports.named = named;
|
|
129
|
+
function limitOf(value) {
|
|
130
|
+
if (value === undefined)
|
|
131
|
+
return undefined;
|
|
132
|
+
if (!/^[1-9][0-9]{0,2}$/.test(value) || Number(value) > 100)
|
|
133
|
+
throw (0, errors_1.usage)('--limit is a number from 1 to 100.');
|
|
134
|
+
return value;
|
|
135
|
+
}
|
|
136
|
+
// Every page with --all, else the first; returns the rows and whether more
|
|
137
|
+
// were left.
|
|
138
|
+
async function pages(read, path, rows) {
|
|
139
|
+
const limit = limitOf(read.values.limit);
|
|
140
|
+
const all = [];
|
|
141
|
+
let after;
|
|
142
|
+
let first;
|
|
143
|
+
for (;;) {
|
|
144
|
+
const body = await read.client.get(path, {
|
|
145
|
+
limit,
|
|
146
|
+
after: after === undefined ? undefined : JSON.stringify(after),
|
|
147
|
+
});
|
|
148
|
+
first ??= body;
|
|
149
|
+
all.push(...rows(body));
|
|
150
|
+
if (!body.more || !read.values.all)
|
|
151
|
+
return { rows: all, more: !!body.more, first };
|
|
152
|
+
after = body.after;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
exports.pages = pages;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.addDeveloper = void 0;
|
|
4
|
+
const errors_1 = require("../errors");
|
|
5
|
+
const read_1 = require("../read");
|
|
6
|
+
const requests_1 = require("./requests");
|
|
7
|
+
function change(r) {
|
|
8
|
+
const where = `${r.place.kind} ${r.place.name ?? ''}`.trim();
|
|
9
|
+
return r.action === 'developer.add'
|
|
10
|
+
? `make ${r.person?.name ?? 'them'}${r.person?.email ? ` (${r.person.email})` : ''} a developer of the ${where}`
|
|
11
|
+
: `invite ${r.email} to be a developer of the ${where}`;
|
|
12
|
+
}
|
|
13
|
+
async function addDeveloper(read, rest) {
|
|
14
|
+
const place = (0, read_1.named)(rest, 'add', 'team or organization');
|
|
15
|
+
const email = read.values.email;
|
|
16
|
+
if (!email || !/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(email))
|
|
17
|
+
throw (0, errors_1.usage)('Name who: --email someone@example.com');
|
|
18
|
+
const asked = await read.client.post(`/developers/${encodeURIComponent(place)}/requests`, { email, ...(read.values.org ? { kind: 'organization' } : {}) });
|
|
19
|
+
read.io.err(`Asked to ${change(asked)}.`);
|
|
20
|
+
const now = await (0, requests_1.confirmOnBlocklive)(read, asked);
|
|
21
|
+
if (!now)
|
|
22
|
+
return errors_1.EXIT.ok;
|
|
23
|
+
if (read.json)
|
|
24
|
+
read.io.out(JSON.stringify(now));
|
|
25
|
+
else
|
|
26
|
+
read.io.out(now.action === 'developer.add'
|
|
27
|
+
? `Done: ${now.person?.name ?? 'they'} can now make API keys for ${now.place.name}. They get an email saying so.`
|
|
28
|
+
: `Done: ${now.email} is invited. They accept on Blocklive.`);
|
|
29
|
+
return errors_1.EXIT.ok;
|
|
30
|
+
}
|
|
31
|
+
exports.addDeveloper = addDeveloper;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.listDevelopers = void 0;
|
|
4
|
+
const errors_1 = require("../errors");
|
|
5
|
+
const read_1 = require("../read");
|
|
6
|
+
async function listDevelopers(read, rest) {
|
|
7
|
+
const place = (0, read_1.named)(rest, 'list', 'team or organization');
|
|
8
|
+
const answer = await read.client.get(`/developers/${encodeURIComponent(place)}`, { kind: read.values.org ? 'organization' : undefined });
|
|
9
|
+
if (read.json) {
|
|
10
|
+
read.io.out(JSON.stringify(answer));
|
|
11
|
+
return errors_1.EXIT.ok;
|
|
12
|
+
}
|
|
13
|
+
const out = read.io.out;
|
|
14
|
+
out(`${answer.place.name} (${answer.place.kind})`);
|
|
15
|
+
out(`Developer access: ${access(answer)}`);
|
|
16
|
+
out('');
|
|
17
|
+
if (answer.developers.length) {
|
|
18
|
+
for (const line of (0, read_1.table)([
|
|
19
|
+
['DEVELOPER', 'EMAIL', 'ADDED AT', 'ADDED', ''],
|
|
20
|
+
...answer.developers.map((d) => [
|
|
21
|
+
d.name ?? '',
|
|
22
|
+
d.email ?? '',
|
|
23
|
+
d.from === answer.place.kind ? `this ${d.from}` : d.from,
|
|
24
|
+
(0, read_1.day)(d.addedAt),
|
|
25
|
+
d.paused ? 'paused: add them again' : '',
|
|
26
|
+
]),
|
|
27
|
+
]))
|
|
28
|
+
out(line);
|
|
29
|
+
}
|
|
30
|
+
else
|
|
31
|
+
out('No developers yet.');
|
|
32
|
+
if (answer.invites.length) {
|
|
33
|
+
out('');
|
|
34
|
+
out('Invited, no answer yet:');
|
|
35
|
+
for (const i of answer.invites)
|
|
36
|
+
out(` ${i.email} (until ${(0, read_1.day)(i.expiresAt)})`);
|
|
37
|
+
}
|
|
38
|
+
if (answer.path) {
|
|
39
|
+
out('');
|
|
40
|
+
out(`Add or remove developers on Blocklive: ${read.base}${answer.path}`);
|
|
41
|
+
}
|
|
42
|
+
return errors_1.EXIT.ok;
|
|
43
|
+
}
|
|
44
|
+
exports.listDevelopers = listDevelopers;
|
|
45
|
+
function access({ access, place }) {
|
|
46
|
+
if (!access.on)
|
|
47
|
+
return 'off';
|
|
48
|
+
if (access.by === 'team')
|
|
49
|
+
return 'on, for this team alone (set by Blocklive)';
|
|
50
|
+
if (place.kind === 'team' && access.organization)
|
|
51
|
+
return `on, through ${access.organization.name}`;
|
|
52
|
+
return 'on';
|
|
53
|
+
}
|