@instahost/cli 0.0.0-stage → 0.1.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 +21 -0
- package/README.md +125 -2
- package/bin/instahost.mjs +26 -0
- package/dist/api-client.js +124 -0
- package/dist/commands/context.js +1 -0
- package/dist/commands/delete.js +41 -0
- package/dist/commands/publish.js +75 -0
- package/dist/commands/share.js +83 -0
- package/dist/config.js +66 -0
- package/dist/errors.js +54 -0
- package/dist/index.js +6 -0
- package/dist/main.js +227 -0
- package/dist/output.js +79 -0
- package/dist/redact.js +24 -0
- package/dist/ttl.js +22 -0
- package/dist/upload.js +320 -0
- package/dist/vendor/paths.js +255 -0
- package/dist/walk.js +158 -0
- package/package.json +53 -3
- package/src/node-check.mjs +34 -0
- package/src/vendor/paths.ts +255 -0
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
// A verbatim copy of the path rules the InstaHost API enforces on the server. Do not edit it
|
|
2
|
+
// here: changes start in the service, whose tests fail when this copy differs from its source.
|
|
3
|
+
// Path rules shared by the InstaHost API and the CLI: path normalisation, invisible and
|
|
4
|
+
// control characters, sensitive file names and the directory-walk exclusions.
|
|
5
|
+
//
|
|
6
|
+
// This file must stay self-contained and runtime-neutral (no imports, no Node or Workers
|
|
7
|
+
// APIs). The CLI ships a verbatim copy, with a header naming where it came from, and the
|
|
8
|
+
// service's tests fail if that copy drifts from the rules the API enforces.
|
|
9
|
+
//
|
|
10
|
+
// Paths are only ever D1 lookup keys. They never become part of an R2 key or a filesystem
|
|
11
|
+
// path, so these rules exist for clarity and anti-spoofing, not to prevent traversal.
|
|
12
|
+
export const MAX_PATH_BYTES = 512;
|
|
13
|
+
export const MAX_SEGMENT_BYTES = 255;
|
|
14
|
+
/** UTF-8 length in bytes. Lone surrogates count as 3 bytes (they encode as U+FFFD). */
|
|
15
|
+
export function utf8ByteLength(s) {
|
|
16
|
+
let bytes = 0;
|
|
17
|
+
for (let i = 0; i < s.length; i++) {
|
|
18
|
+
const c = s.charCodeAt(i);
|
|
19
|
+
if (c < 0x80)
|
|
20
|
+
bytes += 1;
|
|
21
|
+
else if (c < 0x800)
|
|
22
|
+
bytes += 2;
|
|
23
|
+
else if (c >= 0xd800 && c <= 0xdbff && i + 1 < s.length) {
|
|
24
|
+
const d = s.charCodeAt(i + 1);
|
|
25
|
+
if (d >= 0xdc00 && d <= 0xdfff) {
|
|
26
|
+
bytes += 4;
|
|
27
|
+
i++;
|
|
28
|
+
}
|
|
29
|
+
else
|
|
30
|
+
bytes += 3;
|
|
31
|
+
}
|
|
32
|
+
else
|
|
33
|
+
bytes += 3;
|
|
34
|
+
}
|
|
35
|
+
return bytes;
|
|
36
|
+
}
|
|
37
|
+
// C0 controls, DEL and C1 controls.
|
|
38
|
+
const CONTROL = /[\u0000-\u001f\u007f-\u009f]/u;
|
|
39
|
+
// Line and paragraph separators.
|
|
40
|
+
const LINE_SEPARATOR = /[\u2028\u2029]/u;
|
|
41
|
+
// Invisible by definition: Unicode format characters (Cf: bidi controls, zero-width space, soft
|
|
42
|
+
// hyphen, word joiner, BOM, tag characters, ...) and default-ignorable code points (Hangul fillers,
|
|
43
|
+
// combining grapheme joiner, variation selectors, unassigned ignorables, ...).
|
|
44
|
+
const INVISIBLE = /[\p{Cf}\p{Default_Ignorable_Code_Point}]/u;
|
|
45
|
+
const ZWNJ = 0x200c;
|
|
46
|
+
const ZWJ = 0x200d;
|
|
47
|
+
const VS15 = 0xfe0e;
|
|
48
|
+
const VS16 = 0xfe0f;
|
|
49
|
+
const KEYCAP = 0x20e3;
|
|
50
|
+
const JOINER_NEIGHBOUR = /[\p{L}\p{M}\p{Extended_Pictographic}\p{Emoji_Modifier}]/u;
|
|
51
|
+
const EMOJI = /\p{Emoji}/u;
|
|
52
|
+
const LETTER_OR_SYMBOL = /[\p{L}\p{S}]/u;
|
|
53
|
+
const isVariationSelector = (c) => (c >= 0xfe00 && c <= 0xfe0f) || (c >= 0xe0100 && c <= 0xe01ef);
|
|
54
|
+
const isVisible = (c) => c !== undefined && !INVISIBLE.test(String.fromCodePoint(c)) && !CONTROL.test(String.fromCodePoint(c));
|
|
55
|
+
const isAsciiKeycapBase = (c) => (c >= 0x30 && c <= 0x39) || c === 0x23 || c === 0x2a;
|
|
56
|
+
/**
|
|
57
|
+
* True when the path contains an invisible character outside the few contexts where text needs
|
|
58
|
+
* one. ZWNJ/ZWJ (emoji sequences, Persian and Indic scripts) are allowed only between two visible
|
|
59
|
+
* non-ASCII letters, marks or emoji (VS16 may precede a ZWJ, as in heart-on-fire); so
|
|
60
|
+
* "index" + ZWJ + ".html" or runs of joiners are rejected. VS15/VS16 are allowed only after an
|
|
61
|
+
* emoji, or in an ASCII keycap (digit, # or * + VS16 + U+20E3). Other variation selectors
|
|
62
|
+
* (standardised and ideographic variants) are allowed only after a visible non-ASCII letter or
|
|
63
|
+
* symbol. Everything else in INVISIBLE is rejected.
|
|
64
|
+
*/
|
|
65
|
+
function hasInvisibleCharacter(path) {
|
|
66
|
+
if (!INVISIBLE.test(path))
|
|
67
|
+
return false;
|
|
68
|
+
const cps = Array.from(path, (ch) => ch.codePointAt(0));
|
|
69
|
+
for (let i = 0; i < cps.length; i++) {
|
|
70
|
+
const c = cps[i];
|
|
71
|
+
if (!INVISIBLE.test(String.fromCodePoint(c)))
|
|
72
|
+
continue;
|
|
73
|
+
const prev = cps[i - 1];
|
|
74
|
+
const next = cps[i + 1];
|
|
75
|
+
if (c === ZWNJ || c === ZWJ) {
|
|
76
|
+
const prevOk = prev === VS16 || (isVisible(prev) && prev > 0x7f && JOINER_NEIGHBOUR.test(String.fromCodePoint(prev)));
|
|
77
|
+
const nextOk = isVisible(next) && next > 0x7f && JOINER_NEIGHBOUR.test(String.fromCodePoint(next));
|
|
78
|
+
if (prevOk && nextOk)
|
|
79
|
+
continue;
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
if (isVariationSelector(c) && isVisible(prev)) {
|
|
83
|
+
if (c === VS15 || c === VS16) {
|
|
84
|
+
if (prev > 0x7f ? EMOJI.test(String.fromCodePoint(prev)) : isAsciiKeycapBase(prev) && c === VS16 && next === KEYCAP)
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
else if (prev > 0x7f && LETTER_OR_SYMBOL.test(String.fromCodePoint(prev)))
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Validates a relative path as stored in a manifest. Accepts only well-formed NFC strings of
|
|
96
|
+
* 1 to 512 UTF-8 bytes with `/` separators, no leading or trailing `/`, no empty, `.` or `..`
|
|
97
|
+
* segment, segments of at most 255 bytes, no backslash, no control or line-separator character,
|
|
98
|
+
* and no invisible character outside the contexts hasInvisibleCharacter allows. The server
|
|
99
|
+
* rejects non-NFC input; the CLI normalises first.
|
|
100
|
+
*/
|
|
101
|
+
export function validatePath(path) {
|
|
102
|
+
if (typeof path !== "string" || path.length === 0)
|
|
103
|
+
return { ok: false, reason: "empty" };
|
|
104
|
+
// Every UTF-16 code unit is at least one UTF-8 byte: reject huge inputs before scanning them.
|
|
105
|
+
if (path.length > MAX_PATH_BYTES)
|
|
106
|
+
return { ok: false, reason: "too_long" };
|
|
107
|
+
if (!path.isWellFormed())
|
|
108
|
+
return { ok: false, reason: "ill_formed_unicode" };
|
|
109
|
+
if (utf8ByteLength(path) > MAX_PATH_BYTES)
|
|
110
|
+
return { ok: false, reason: "too_long" };
|
|
111
|
+
if (path.normalize("NFC") !== path)
|
|
112
|
+
return { ok: false, reason: "not_nfc" };
|
|
113
|
+
if (CONTROL.test(path))
|
|
114
|
+
return { ok: false, reason: "control_character" };
|
|
115
|
+
if (LINE_SEPARATOR.test(path))
|
|
116
|
+
return { ok: false, reason: "line_separator" };
|
|
117
|
+
if (hasInvisibleCharacter(path))
|
|
118
|
+
return { ok: false, reason: "invisible_character" };
|
|
119
|
+
if (path.includes("\\"))
|
|
120
|
+
return { ok: false, reason: "backslash" };
|
|
121
|
+
if (path.startsWith("/"))
|
|
122
|
+
return { ok: false, reason: "leading_slash" };
|
|
123
|
+
if (path.endsWith("/"))
|
|
124
|
+
return { ok: false, reason: "trailing_slash" };
|
|
125
|
+
for (const segment of path.split("/")) {
|
|
126
|
+
if (segment === "")
|
|
127
|
+
return { ok: false, reason: "empty_segment" };
|
|
128
|
+
if (segment === "." || segment === "..")
|
|
129
|
+
return { ok: false, reason: "dot_segment" };
|
|
130
|
+
if (utf8ByteLength(segment) > MAX_SEGMENT_BYTES)
|
|
131
|
+
return { ok: false, reason: "segment_too_long" };
|
|
132
|
+
}
|
|
133
|
+
return { ok: true };
|
|
134
|
+
}
|
|
135
|
+
// Sensitive names, matched case-insensitively.
|
|
136
|
+
const SENSITIVE_DIRECTORIES = new Set([".git", ".svn", ".hg", ".ssh", ".aws", ".gnupg", ".kube", ".docker"]);
|
|
137
|
+
const SENSITIVE_BASENAMES = new Set([
|
|
138
|
+
".env",
|
|
139
|
+
".envrc", // direnv: exported secrets
|
|
140
|
+
".dev.vars",
|
|
141
|
+
".npmrc",
|
|
142
|
+
".netrc",
|
|
143
|
+
".pypirc",
|
|
144
|
+
".htpasswd",
|
|
145
|
+
".git-credentials",
|
|
146
|
+
".pgpass",
|
|
147
|
+
"_netrc", // the name curl and git read on Windows
|
|
148
|
+
"id_rsa",
|
|
149
|
+
"id_dsa",
|
|
150
|
+
"id_ecdsa",
|
|
151
|
+
"id_ed25519",
|
|
152
|
+
"id_ecdsa_sk", // OpenSSH FIDO keys (ssh-keygen -t ecdsa-sk / ed25519-sk)
|
|
153
|
+
"id_ed25519_sk",
|
|
154
|
+
]);
|
|
155
|
+
const SENSITIVE_BASENAME_PREFIXES = [".env.", ".dev.vars."];
|
|
156
|
+
// Terraform also writes terraform.tfstate.backup and accepts *.tfvars.json; both carry the
|
|
157
|
+
// same secrets as *.tfstate and *.tfvars.
|
|
158
|
+
const SENSITIVE_SUFFIXES = [
|
|
159
|
+
".pem",
|
|
160
|
+
".key",
|
|
161
|
+
".p12",
|
|
162
|
+
".pfx",
|
|
163
|
+
".jks",
|
|
164
|
+
".keystore",
|
|
165
|
+
".ppk",
|
|
166
|
+
".tfstate",
|
|
167
|
+
".tfstate.backup",
|
|
168
|
+
".tfvars",
|
|
169
|
+
".tfvars.json",
|
|
170
|
+
];
|
|
171
|
+
// Editor and manual backup copies of a listed file are as sensitive as the file itself.
|
|
172
|
+
const BACKUP_SUFFIXES = ["~", ".bak", ".old", ".orig"];
|
|
173
|
+
/** The basename plus its backup-stripped forms ("id_rsa.bak" -> "id_rsa", "#.env#" -> ".env"). */
|
|
174
|
+
function basenameForms(base) {
|
|
175
|
+
const forms = [base];
|
|
176
|
+
if (base.length > 2 && base.startsWith("#") && base.endsWith("#"))
|
|
177
|
+
forms.push(base.slice(1, -1));
|
|
178
|
+
for (const suffix of BACKUP_SUFFIXES) {
|
|
179
|
+
if (base.endsWith(suffix) && base.length > suffix.length)
|
|
180
|
+
forms.push(base.slice(0, -suffix.length));
|
|
181
|
+
}
|
|
182
|
+
return forms;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Returns the rule a path breaks if it looks like a credential or VCS metadata, else null.
|
|
186
|
+
* Name-based only: there is no content scanning.
|
|
187
|
+
*/
|
|
188
|
+
export function sensitiveRule(path) {
|
|
189
|
+
const segments = path.toLowerCase().split("/");
|
|
190
|
+
for (const segment of segments.slice(0, -1)) {
|
|
191
|
+
if (SENSITIVE_DIRECTORIES.has(segment))
|
|
192
|
+
return `directory ${segment}`;
|
|
193
|
+
}
|
|
194
|
+
const last = segments[segments.length - 1] ?? "";
|
|
195
|
+
if (SENSITIVE_DIRECTORIES.has(last))
|
|
196
|
+
return `directory ${last}`;
|
|
197
|
+
for (const base of basenameForms(last)) {
|
|
198
|
+
if (SENSITIVE_BASENAMES.has(base))
|
|
199
|
+
return `file ${base}`;
|
|
200
|
+
for (const prefix of SENSITIVE_BASENAME_PREFIXES)
|
|
201
|
+
if (base.startsWith(prefix))
|
|
202
|
+
return `file ${prefix}*`;
|
|
203
|
+
for (const suffix of SENSITIVE_SUFFIXES)
|
|
204
|
+
if (base.endsWith(suffix))
|
|
205
|
+
return `extension ${suffix}`;
|
|
206
|
+
}
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
export function isSensitivePath(path) {
|
|
210
|
+
return sensitiveRule(path) !== null;
|
|
211
|
+
}
|
|
212
|
+
// Directory-walk exclusions for the CLI: .DS_Store, Thumbs.db, Windows desktop.ini, the macOS
|
|
213
|
+
// custom-icon file "Icon\r" (its name fails validatePath, so it would otherwise abort a publish),
|
|
214
|
+
// AppleDouble "._*" files and the __MACOSX folder (they carry extended attributes such as
|
|
215
|
+
// quarantine data and download URLs).
|
|
216
|
+
const OS_METADATA_BASENAMES = new Set([".ds_store", "thumbs.db", "desktop.ini", `icon${String.fromCharCode(13)}`]);
|
|
217
|
+
const DOTFILE_ALLOWED_SEGMENTS = new Set([".well-known"]);
|
|
218
|
+
/**
|
|
219
|
+
* Why `publish` leaves a walked path out, or null to include it. Sensitive paths and OS
|
|
220
|
+
* metadata are always excluded; dot-prefixed segments are excluded unless includeDotfiles,
|
|
221
|
+
* except `.well-known`.
|
|
222
|
+
*/
|
|
223
|
+
export function walkExclusion(path, includeDotfiles = false) {
|
|
224
|
+
if (isSensitivePath(path))
|
|
225
|
+
return "sensitive";
|
|
226
|
+
const segments = path.split("/");
|
|
227
|
+
const base = (segments[segments.length - 1] ?? "").toLowerCase();
|
|
228
|
+
if (OS_METADATA_BASENAMES.has(base) || base.startsWith("._") || segments.includes("__MACOSX"))
|
|
229
|
+
return "os_metadata";
|
|
230
|
+
if (!includeDotfiles) {
|
|
231
|
+
for (const segment of segments) {
|
|
232
|
+
if (segment.startsWith(".") && !DOTFILE_ALLOWED_SEGMENTS.has(segment.toLowerCase()))
|
|
233
|
+
return "dotfile";
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Paths that collide with an earlier one after NFC normalisation, in input order. On the server,
|
|
240
|
+
* where validatePath already requires NFC, these are exact duplicates; in the CLI walk they also
|
|
241
|
+
* catch two on-disk names (NFC and NFD) that would become the same manifest path.
|
|
242
|
+
*/
|
|
243
|
+
export function findDuplicatePaths(paths) {
|
|
244
|
+
const seen = new Map();
|
|
245
|
+
const duplicates = [];
|
|
246
|
+
for (const path of paths) {
|
|
247
|
+
const key = path.normalize("NFC");
|
|
248
|
+
const first = seen.get(key);
|
|
249
|
+
if (first === undefined)
|
|
250
|
+
seen.set(key, path);
|
|
251
|
+
else
|
|
252
|
+
duplicates.push({ path, duplicateOf: first });
|
|
253
|
+
}
|
|
254
|
+
return duplicates;
|
|
255
|
+
}
|
package/dist/walk.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// Directory walk and file hashing. The path rules are src/vendor/paths.ts, a verbatim copy of the
|
|
2
|
+
// rules the API enforces.
|
|
3
|
+
//
|
|
4
|
+
// - Regular files only. Symlinks are never followed and are reported as skipped ("symlink"); other
|
|
5
|
+
// special files (sockets, FIFOs, devices) are reported as "special".
|
|
6
|
+
// - Exclusions (walkExclusion): sensitive paths and OS metadata always, dot-prefixed segments
|
|
7
|
+
// unless --include-dotfiles (".well-known" is kept). A directory whose every descendant would be
|
|
8
|
+
// excluded (".git", "__MACOSX", a dot directory) is reported once with a trailing "/" and not
|
|
9
|
+
// descended into, so a large .git or node cache costs nothing.
|
|
10
|
+
// - Manifest paths use "/" and NFC; files are still opened by their on-disk names. A remaining path
|
|
11
|
+
// that fails validatePath, or two names that collide after NFC, stop the publish (exit 3).
|
|
12
|
+
// - Each file is opened without following symlinks, then stat-ed and MD5-hashed by streaming.
|
|
13
|
+
import { createHash } from "node:crypto";
|
|
14
|
+
import { constants } from "node:fs";
|
|
15
|
+
import { open, readdir } from "node:fs/promises";
|
|
16
|
+
import { join } from "node:path";
|
|
17
|
+
import { inputError } from "./errors.js";
|
|
18
|
+
import { findDuplicatePaths, validatePath, walkExclusion } from "./vendor/paths.js";
|
|
19
|
+
/** A walk that finds more files than this stops before hashing (the server's ceiling is 10,000). */
|
|
20
|
+
export const MAX_WALK_FILES = 10_000;
|
|
21
|
+
/** HTML and CSS files larger than this are not scanned for root-absolute references. */
|
|
22
|
+
export const SCAN_MAX_BYTES = 2 * 1024 * 1024;
|
|
23
|
+
const NOFOLLOW = constants.O_NOFOLLOW ?? 0;
|
|
24
|
+
/** Size and MD5 of a regular file, read once by streaming; refuses symlinks and special files. */
|
|
25
|
+
export async function hashFile(absPath, displayPath) {
|
|
26
|
+
let handle;
|
|
27
|
+
try {
|
|
28
|
+
handle = await open(absPath, constants.O_RDONLY | NOFOLLOW);
|
|
29
|
+
}
|
|
30
|
+
catch (err) {
|
|
31
|
+
const code = err.code;
|
|
32
|
+
if (code === "ELOOP")
|
|
33
|
+
throw inputError("symlink_refused", `${displayPath} is a symbolic link; symlinks are never followed.`, { path: displayPath });
|
|
34
|
+
if (code === "ENOENT")
|
|
35
|
+
throw inputError("file_not_found", `${displayPath} does not exist.`, { path: displayPath });
|
|
36
|
+
throw inputError("unreadable_file", `${displayPath} cannot be read (${code ?? "error"}).`, { path: displayPath });
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
const stat = await handle.stat();
|
|
40
|
+
if (!stat.isFile())
|
|
41
|
+
throw inputError("not_a_file", `${displayPath} is not a regular file.`, { path: displayPath });
|
|
42
|
+
const hash = createHash("md5");
|
|
43
|
+
let size = 0;
|
|
44
|
+
for await (const chunk of handle.createReadStream({ autoClose: false })) {
|
|
45
|
+
size += chunk.byteLength;
|
|
46
|
+
hash.update(chunk);
|
|
47
|
+
}
|
|
48
|
+
if (size !== stat.size)
|
|
49
|
+
throw inputError("file_changed", `${displayPath} changed while it was being read; try again.`, { path: displayPath });
|
|
50
|
+
return { size, md5: hash.digest("hex"), dev: stat.dev, ino: stat.ino };
|
|
51
|
+
}
|
|
52
|
+
finally {
|
|
53
|
+
await handle.close();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
async function collect(root, includeDotfiles) {
|
|
57
|
+
const candidates = [];
|
|
58
|
+
const skipped = [];
|
|
59
|
+
const pending = [{ abs: root, raw: "" }];
|
|
60
|
+
while (pending.length) {
|
|
61
|
+
const dir = pending.pop();
|
|
62
|
+
let entries;
|
|
63
|
+
try {
|
|
64
|
+
entries = await readdir(dir.abs, { withFileTypes: true });
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
throw inputError("unreadable_directory", `${dir.raw || "."} cannot be read (${err.code ?? "error"}).`, { path: dir.raw || "." });
|
|
68
|
+
}
|
|
69
|
+
for (const entry of entries) {
|
|
70
|
+
const raw = dir.raw ? `${dir.raw}/${entry.name}` : entry.name;
|
|
71
|
+
const path = raw.normalize("NFC");
|
|
72
|
+
const abs = join(dir.abs, entry.name);
|
|
73
|
+
if (entry.isDirectory()) {
|
|
74
|
+
// Probe with a harmless child name: non-null means the directory itself excludes everything.
|
|
75
|
+
const excluded = walkExclusion(`${path}/x`, includeDotfiles);
|
|
76
|
+
if (excluded)
|
|
77
|
+
skipped.push({ path: `${path}/`, reason: excluded });
|
|
78
|
+
else
|
|
79
|
+
pending.push({ abs, raw });
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
const excluded = walkExclusion(path, includeDotfiles);
|
|
83
|
+
if (excluded)
|
|
84
|
+
skipped.push({ path, reason: excluded });
|
|
85
|
+
else if (entry.isSymbolicLink())
|
|
86
|
+
skipped.push({ path, reason: "symlink" });
|
|
87
|
+
else if (!entry.isFile())
|
|
88
|
+
skipped.push({ path, reason: "special" });
|
|
89
|
+
else
|
|
90
|
+
candidates.push({ path, raw, absPath: abs });
|
|
91
|
+
if (candidates.length > MAX_WALK_FILES) {
|
|
92
|
+
throw inputError("walk_too_many_files", `The directory holds more than ${MAX_WALK_FILES} files to publish.`, { limit: MAX_WALK_FILES });
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return { candidates, skipped };
|
|
97
|
+
}
|
|
98
|
+
const byPath = (a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0);
|
|
99
|
+
/** Walks a site directory: the files to publish (sorted, hashed) and what was skipped. */
|
|
100
|
+
export async function walkSite(root, options) {
|
|
101
|
+
const { candidates, skipped } = await collect(root, options.includeDotfiles);
|
|
102
|
+
const invalid = [];
|
|
103
|
+
for (const c of candidates) {
|
|
104
|
+
const check = validatePath(c.path);
|
|
105
|
+
if (!check.ok)
|
|
106
|
+
invalid.push({ path: c.path, reason: check.reason });
|
|
107
|
+
}
|
|
108
|
+
if (invalid.length) {
|
|
109
|
+
throw inputError("invalid_path", `${invalid.length} file name(s) cannot be published as they are; rename them.`, { paths: invalid.sort(byPath).slice(0, 20), count: invalid.length });
|
|
110
|
+
}
|
|
111
|
+
const duplicates = findDuplicatePaths(candidates.map((c) => c.raw));
|
|
112
|
+
if (duplicates.length) {
|
|
113
|
+
throw inputError("duplicate_path", "Two file names become the same path after Unicode normalisation (NFC).", {
|
|
114
|
+
paths: duplicates.slice(0, 20).map((d) => ({ path: d.path.normalize("NFC"), duplicate_of: d.duplicateOf })),
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
candidates.sort(byPath);
|
|
118
|
+
skipped.sort(byPath);
|
|
119
|
+
const files = [];
|
|
120
|
+
let totalBytes = 0;
|
|
121
|
+
for (const c of candidates) {
|
|
122
|
+
const { size, md5, dev, ino } = await hashFile(c.absPath, c.path);
|
|
123
|
+
files.push({ path: c.path, absPath: c.absPath, size, md5, dev, ino });
|
|
124
|
+
totalBytes += size;
|
|
125
|
+
}
|
|
126
|
+
return { files, skipped, totalBytes };
|
|
127
|
+
}
|
|
128
|
+
// Root-absolute references break in path mode, where every site lives under /<id>/ on the shared
|
|
129
|
+
// sites origin.
|
|
130
|
+
const HTML_ATTRIBUTE = /\b(?:src|href|action|formaction|poster|data|srcset)\s*=\s*["']?\s*\/(?![/\\])/i;
|
|
131
|
+
const CSS_URL = /url\(\s*["']?\s*\/(?![/\\])|@import\s+["']\/(?![/\\])/i;
|
|
132
|
+
const BASE_ROOT = /<base\s[^>]*href\s*=\s*["']?\s*\/(?![/\\])/i;
|
|
133
|
+
/** HTML and CSS files that reference root-absolute URLs ("/app.js") or set <base href="/">. */
|
|
134
|
+
export async function findRootAbsoluteReferences(files) {
|
|
135
|
+
const found = [];
|
|
136
|
+
for (const f of files) {
|
|
137
|
+
const lower = f.path.toLowerCase();
|
|
138
|
+
const isHtml = lower.endsWith(".html") || lower.endsWith(".htm");
|
|
139
|
+
const isCss = lower.endsWith(".css");
|
|
140
|
+
if ((!isHtml && !isCss) || f.size > SCAN_MAX_BYTES)
|
|
141
|
+
continue;
|
|
142
|
+
let handle;
|
|
143
|
+
try {
|
|
144
|
+
handle = await open(f.absPath, constants.O_RDONLY | NOFOLLOW);
|
|
145
|
+
const text = (await handle.readFile()).toString("utf8");
|
|
146
|
+
const base = isHtml && BASE_ROOT.test(text);
|
|
147
|
+
if (base || CSS_URL.test(text) || (isHtml && HTML_ATTRIBUTE.test(text)))
|
|
148
|
+
found.push({ path: f.path, base });
|
|
149
|
+
}
|
|
150
|
+
catch {
|
|
151
|
+
// A file that vanished is reported by the upload; the scan is advisory.
|
|
152
|
+
}
|
|
153
|
+
finally {
|
|
154
|
+
await handle?.close();
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return found;
|
|
158
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,56 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@instahost/cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Instant hosting and sharing for AI agents: share a file or publish a static site and get an HTTPS URL. The unscoped npm package named instahost is unrelated.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"hosting",
|
|
7
|
+
"share",
|
|
8
|
+
"static-site",
|
|
9
|
+
"agents",
|
|
10
|
+
"cli",
|
|
11
|
+
"instahost"
|
|
12
|
+
],
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"homepage": "https://instahost.dev/docs/",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/instahost-dev/instahost.git",
|
|
18
|
+
"directory": "packages/cli"
|
|
19
|
+
},
|
|
20
|
+
"bugs": "https://github.com/instahost-dev/instahost/issues",
|
|
21
|
+
"type": "module",
|
|
22
|
+
"bin": {
|
|
23
|
+
"instahost": "./bin/instahost.mjs"
|
|
24
|
+
},
|
|
25
|
+
"exports": {
|
|
26
|
+
".": "./dist/index.js",
|
|
27
|
+
"./paths": "./dist/vendor/paths.js",
|
|
28
|
+
"./node-check": "./src/node-check.mjs",
|
|
29
|
+
"./package.json": "./package.json"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"bin",
|
|
33
|
+
"dist",
|
|
34
|
+
"src/vendor/paths.ts",
|
|
35
|
+
"src/node-check.mjs",
|
|
36
|
+
"README.md",
|
|
37
|
+
"LICENSE"
|
|
38
|
+
],
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=20"
|
|
41
|
+
},
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/node": "20.19.43",
|
|
47
|
+
"typescript": "6.0.3"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "node scripts/clean.mjs dist && tsc -p tsconfig.build.json",
|
|
51
|
+
"build:test": "node scripts/clean.mjs build && tsc -p tsconfig.test.json",
|
|
52
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
53
|
+
"test": "pnpm run build && node --test test/*.test.ts",
|
|
54
|
+
"test:compiled": "node --test build/test/*.test.js"
|
|
55
|
+
}
|
|
6
56
|
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// Node version gate for bin/instahost.mjs. Plain JavaScript that every Node version can parse,
|
|
2
|
+
// typed with JSDoc, so the check runs before the compiled CLI is imported.
|
|
3
|
+
|
|
4
|
+
/** The oldest supported Node.js release: the compiled CLI needs nothing newer than Node 20. */
|
|
5
|
+
export const MIN_NODE = /** @type {const} */ ([20, 0]);
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* True when the version string (process.versions.node) is older than MIN_NODE or not a version.
|
|
9
|
+
* @param {string} version
|
|
10
|
+
* @returns {boolean}
|
|
11
|
+
*/
|
|
12
|
+
export function nodeTooOld(version) {
|
|
13
|
+
const match = /^(\d+)\.(\d+)\.\d+/.exec(String(version));
|
|
14
|
+
if (!match) return true;
|
|
15
|
+
const major = Number(match[1]);
|
|
16
|
+
const minor = Number(match[2]);
|
|
17
|
+
return major < MIN_NODE[0] || (major === MIN_NODE[0] && minor < MIN_NODE[1]);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The single --json document printed when Node is too old (usage error, exit code 2).
|
|
22
|
+
* @param {string} version
|
|
23
|
+
*/
|
|
24
|
+
export function tooOldDocument(version) {
|
|
25
|
+
return {
|
|
26
|
+
ok: false,
|
|
27
|
+
error: {
|
|
28
|
+
code: "node_too_old",
|
|
29
|
+
message: `Node.js >= ${MIN_NODE[0]} is required (this is ${version}).`,
|
|
30
|
+
details: { required: `>=${MIN_NODE.join(".")}.0`, found: String(version) },
|
|
31
|
+
exit_code: 2,
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|