@sedum-dev/core 0.1.0-alpha.4 → 0.1.0-alpha.5

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.
@@ -0,0 +1,100 @@
1
+ // src/script-registry.ts
2
+ import { realpathSync } from "fs";
3
+ import path from "path";
4
+ import { fileURLToPath } from "url";
5
+ import { inspect } from "util";
6
+ var REGISTRY = /* @__PURE__ */ Symbol.for("sedum.script.registry");
7
+ var SECRET = /* @__PURE__ */ Symbol.for("sedum.script.secret");
8
+ var Secret = class {
9
+ #value;
10
+ [SECRET];
11
+ constructor(value) {
12
+ this.#value = value;
13
+ this[SECRET] = () => this.#value;
14
+ }
15
+ toString() {
16
+ return "[secret]";
17
+ }
18
+ toJSON() {
19
+ return "[secret]";
20
+ }
21
+ [inspect.custom]() {
22
+ return "[secret]";
23
+ }
24
+ };
25
+ function secret(value) {
26
+ if (typeof value !== "string")
27
+ throw new TypeError("secret() takes a string value.");
28
+ return new Secret(value);
29
+ }
30
+ function isSecret(value) {
31
+ return typeof value === "object" && value !== null && typeof value[SECRET] === "function";
32
+ }
33
+ function revealSecret(value) {
34
+ return value[SECRET]();
35
+ }
36
+ function scriptRegistry() {
37
+ const holder = globalThis;
38
+ return holder[REGISTRY] ??= { pending: [] };
39
+ }
40
+ function test(title, optionsOrBody, maybeBody) {
41
+ const body = typeof optionsOrBody === "function" ? optionsOrBody : maybeBody;
42
+ const options = typeof optionsOrBody === "function" ? {} : optionsOrBody;
43
+ scriptRegistry().pending.push({
44
+ title,
45
+ options: options ?? {},
46
+ body,
47
+ callSite: testFileFrame(new Error().stack)
48
+ });
49
+ }
50
+ var TOP_LEVEL = /^\s*at (?:async )?(?:Object\.<anonymous> \()?(?:file:\/\/)?[^()\s][^()]*:\d+:\d+\)?\s*$/u;
51
+ function testFileFrame(stack) {
52
+ const frames = stack?.split("\n").slice(2) ?? [];
53
+ const inTests = frames.filter(
54
+ (frame) => parseFrame(frame)?.file.endsWith(".test.ts")
55
+ );
56
+ return inTests.find((frame) => TOP_LEVEL.test(frame)) ?? inTests[0] ?? frames[0];
57
+ }
58
+ function parseFrame(frame) {
59
+ const match = /(?:\(|at\s)([^()\s][^()]*?):(\d+):(\d+)\)?\s*$/u.exec(
60
+ frame.trim()
61
+ );
62
+ if (!match) return void 0;
63
+ let file = match[1];
64
+ if (file.startsWith("file://"))
65
+ try {
66
+ file = fileURLToPath(file);
67
+ } catch {
68
+ }
69
+ return { file, line: Number(match[2]), col: Number(match[3]) };
70
+ }
71
+ function pathKey(file) {
72
+ let resolved = path.resolve(file);
73
+ try {
74
+ resolved = realpathSync.native(resolved);
75
+ } catch {
76
+ }
77
+ return process.platform === "win32" ? resolved.toLowerCase() : resolved;
78
+ }
79
+ function samePath(a, b) {
80
+ return a === b || pathKey(a) === pathKey(b);
81
+ }
82
+ function sourceInFile(stack, file) {
83
+ for (const frame of stack?.split("\n").slice(1) ?? []) {
84
+ const source = parseFrame(frame);
85
+ if (source && samePath(source.file, file)) return { ...source, file };
86
+ }
87
+ return void 0;
88
+ }
89
+
90
+ export {
91
+ secret,
92
+ isSecret,
93
+ revealSecret,
94
+ scriptRegistry,
95
+ test,
96
+ parseFrame,
97
+ pathKey,
98
+ samePath,
99
+ sourceInFile
100
+ };