@hyperframes/core 0.9.89 → 0.9.90

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.
@@ -57,3 +57,10 @@ export declare function realProjectRoot(root: string): string;
57
57
  * A `dir` outside `root` is created recursively, but only while `root` exists.
58
58
  */
59
59
  export declare function mkdirWithinProject(root: string, dir: string): void;
60
+ /** The real path; for a path not there (yet, or any more), the nearest existing folder's real path plus the rest. */
61
+ export declare function realFilePath(filePath: string): string;
62
+ export type EntryKind = "file" | "folder";
63
+ /** A script only Node runs, whatever folder holds it. */
64
+ export declare function nodeOnlyScript(relPath: string): boolean;
65
+ /** A Node-only script, or `.js`/`.mjs` or an extensionless file below (at any depth, past the project too) a package.json that is not the scaffold's. */
66
+ export declare function nodeMayRun(projectDir: string, relPath: string, kind: EntryKind): boolean;
package/dist/safePath.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { resolve, sep, join, dirname, basename, relative, isAbsolute, parse } from "node:path";
2
- import { lstatSync, mkdirSync, realpath as realpathWalk, realpathSync, statSync } from "node:fs";
2
+ import { lstatSync, mkdirSync, readFileSync, realpath as realpathWalk, realpathSync, statSync, } from "node:fs";
3
3
  import { realpath as realpathNative } from "node:fs/promises";
4
4
  import { promisify } from "node:util";
5
5
  import { projectPathSegments } from "@hyperframes/parsers/private-path";
@@ -295,3 +295,74 @@ export function mkdirWithinProject(root, dir) {
295
295
  }
296
296
  }
297
297
  }
298
+ /** The real path; for a path not there (yet, or any more), the nearest existing folder's real path plus the rest. */
299
+ export function realFilePath(filePath) {
300
+ try {
301
+ return realpath(filePath);
302
+ }
303
+ catch {
304
+ const dir = dirname(filePath);
305
+ return dir === filePath ? filePath : join(realFilePath(dir), basename(filePath));
306
+ }
307
+ }
308
+ const NODE_ONLY_SCRIPT = /\.(?:[cm]?ts|tsx|jsx|cjs)$/;
309
+ const BROWSER_OR_NODE_SCRIPT = /\.m?js$/;
310
+ const isExtensionless = (name) => name !== "" && !name.includes(".");
311
+ /** A script only Node runs, whatever folder holds it. */
312
+ export function nodeOnlyScript(relPath) {
313
+ return NODE_ONLY_SCRIPT.test(foldedPath(relPath).split("/").at(-1) ?? "");
314
+ }
315
+ /** A Node-only script, or `.js`/`.mjs` or an extensionless file below (at any depth, past the project too) a package.json that is not the scaffold's. */
316
+ export function nodeMayRun(projectDir, relPath, kind) {
317
+ if (nodeOnlyScript(relPath))
318
+ return true;
319
+ const name = foldedPath(relPath).split("/").at(-1) ?? "";
320
+ const sharedWithNode = BROWSER_OR_NODE_SCRIPT.test(name) || (kind === "file" && isExtensionless(name));
321
+ if (!sharedWithNode)
322
+ return false;
323
+ const folder = dirname(resolve(projectDir, relPath));
324
+ return [folder, realFilePath(folder)].some((from) => foldersUp(from).some(manifestBringsNode));
325
+ }
326
+ function foldersUp(from) {
327
+ const parent = dirname(from);
328
+ return parent === from ? [from] : [from, ...foldersUp(parent)];
329
+ }
330
+ const manifests = new Map();
331
+ function manifestBringsNode(folder) {
332
+ const path = join(folder, "package.json");
333
+ const stat = statSync(path, { throwIfNoEntry: false });
334
+ if (!stat?.isFile())
335
+ return false;
336
+ const key = `${stat.ino}:${stat.size}:${stat.mtimeMs}`;
337
+ const cached = manifests.get(path);
338
+ if (cached?.stat === key)
339
+ return cached.bringsNode;
340
+ const bringsNode = !isScaffoldManifest(readPackageJson(path));
341
+ manifests.set(path, { stat: key, bringsNode });
342
+ return bringsNode;
343
+ }
344
+ function readPackageJson(path) {
345
+ try {
346
+ return JSON.parse(readFileSync(path, "utf-8"));
347
+ }
348
+ catch {
349
+ return null;
350
+ }
351
+ }
352
+ const INERT_FIELDS = new Set("name version private type description license author contributors keywords homepage repository bugs engines".split(" "));
353
+ const HYPERFRAMES_COMMAND = /^(?:npx (?:--yes |-y )?)?hyperframes(?:@[\w.+-]+)?(?: [\w@=:./+-]+)*$/;
354
+ /** A manifest with nothing but inert fields and scripts that only call the hyperframes CLI, as `hyperframes init` writes. */
355
+ function isScaffoldManifest(pkg) {
356
+ if (!pkg || typeof pkg !== "object" || Array.isArray(pkg))
357
+ return false;
358
+ return Object.entries(pkg).every(([field, value]) => field === "scripts" ? onlyHyperframesScripts(value) : INERT_FIELDS.has(field));
359
+ }
360
+ function onlyHyperframesScripts(scripts) {
361
+ if (!scripts || typeof scripts !== "object" || Array.isArray(scripts))
362
+ return false;
363
+ return Object.values(scripts).every((script) => typeof script === "string" &&
364
+ script.split(" && ").every((command) => HYPERFRAMES_COMMAND.test(command.trim())));
365
+ }
366
+ const foldedPath = (path) => projectPathSegments(path)
367
+ .filter((segment) => segment !== "" && segment !== ".")
368
+ .join("/");
@@ -129,6 +129,20 @@ function redactFilePaths(value) {
129
129
  .replace(BARE_RELATIVE_PATH, "[path]")
130
130
  .replace(ASSET_BASENAME, "[file]"));
131
131
  }
132
+ // The path patterns cost up to the square of a run's length. Before any of them runs, input is cut at whitespace so
133
+ // no path is split, and a long run keeps only its two ends: its start still names a path, its end what follows.
134
+ const MAX_REDACTED_INPUT = 16 * 1024;
135
+ const RUN_KEPT = 256;
136
+ const LONG_RUN = new RegExp(String.raw `(?<![^\s/\\'"])[^\s/\\'"]{${RUN_KEPT + 1},}`, "g");
137
+ function cutAtWhitespace(value) {
138
+ if (value.length <= MAX_REDACTED_INPUT)
139
+ return value;
140
+ let end = MAX_REDACTED_INPUT;
141
+ while (end >= 0 && !/\s/.test(value.charAt(end)))
142
+ end--;
143
+ return `${value.slice(0, end + 1)}…`;
144
+ }
132
145
  export function redactTelemetryString(value, maxLength = MAX_TELEMETRY_STRING_LENGTH) {
133
- return truncateTelemetryString(redactFilePaths(redactUrlQueryStrings(value)), maxLength);
146
+ const shortRuns = redactUrlQueryStrings(cutAtWhitespace(value)).replace(LONG_RUN, (run) => `${run.slice(0, RUN_KEPT / 2)}…${run.slice(-RUN_KEPT / 2)}`);
147
+ return truncateTelemetryString(redactFilePaths(shortRuns), maxLength);
134
148
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyperframes/core",
3
- "version": "0.9.89",
3
+ "version": "0.9.90",
4
4
  "description": "",
5
5
  "repository": {
6
6
  "type": "git",
@@ -505,9 +505,9 @@
505
505
  "linkedom": "^0.18.12",
506
506
  "postcss": "^8.5.8",
507
507
  "wawoff2": "^2.0.1",
508
- "@hyperframes/lint": "0.9.89",
509
- "@hyperframes/parsers": "0.9.89",
510
- "@hyperframes/studio-server": "0.9.89"
508
+ "@hyperframes/lint": "0.9.90",
509
+ "@hyperframes/studio-server": "0.9.90",
510
+ "@hyperframes/parsers": "0.9.90"
511
511
  },
512
512
  "devDependencies": {
513
513
  "@fontsource/archivo-black": "^5.2.8",