@brainervirus/workit-core 1.0.11 → 1.0.12

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,33 @@
1
+ import { createHash } from "node:crypto";
2
+ import { readFileSync } from "node:fs";
3
+
4
+ /** sha256 of the exact bytes, lowercase hex. */
5
+ export const sha256Hex = (bytes: string | Uint8Array): string =>
6
+ createHash("sha256").update(bytes).digest("hex");
7
+
8
+ /** sha256 of a bundle file's bytes, or null when it cannot be read. */
9
+ export const bundleHashOfFile = (file: string): string | null => {
10
+ try {
11
+ return sha256Hex(readFileSync(file));
12
+ } catch {
13
+ return null;
14
+ }
15
+ };
16
+
17
+ // Segments of package-manager ephemeral caches (pnpm dlx, npx, pacquet).
18
+ // Installs resolving through these break when the cache is cleared; one
19
+ // table here replaces the per-callsite substring lists.
20
+ const EPHEMERAL_CACHE_SEGMENTS = [
21
+ "/caches/pnpm/dlx/",
22
+ "/.cache/pnpm/dlx/",
23
+ "/_npx/",
24
+ "/.pacquet/",
25
+ "/library/caches/pnpm/",
26
+ ];
27
+
28
+ /** True when the entry resolves through a package-manager ephemeral cache. */
29
+ export const isEphemeralCachePath = (entry: string): boolean => {
30
+ if (!entry) return false;
31
+ const normalized = entry.replaceAll("\\", "/").toLowerCase();
32
+ return EPHEMERAL_CACHE_SEGMENTS.some((segment) => normalized.includes(segment));
33
+ };
@@ -147,6 +147,113 @@ export const refSchema = z.discriminatedUnion("kind", [
147
147
  ]);
148
148
  export type Ref = z.infer<typeof refSchema>;
149
149
 
150
+ /** Maps one reference during record traversal. Returning null marks the
151
+ * reference unportable: arrays drop it, nullable fields null it, and
152
+ * non-nullable positions propagate the drop to their owner. */
153
+ export type RefMap = (ref: Ref) => Ref | null;
154
+
155
+ type RewriteOutcome = { drop: boolean; value?: unknown };
156
+
157
+ const rewriteUnknown = (value: unknown, map: RefMap): RewriteOutcome => {
158
+ if (Array.isArray(value)) {
159
+ const out: unknown[] = [];
160
+ for (const item of value) {
161
+ const rewritten = rewriteUnknown(item, map);
162
+ if (!rewritten.drop) out.push(rewritten.value);
163
+ }
164
+ return { drop: false, value: out };
165
+ }
166
+ if (typeof value === "object" && value !== null) {
167
+ if (refSchema.safeParse(value).success) {
168
+ const mapped = map(value as Ref);
169
+ return mapped === null ? { drop: true } : { drop: false, value: mapped };
170
+ }
171
+ const out: Record<string, unknown> = {};
172
+ for (const [key, item] of Object.entries(value)) {
173
+ const rewritten = rewriteUnknown(item, map);
174
+ out[key] = rewritten.drop ? null : rewritten.value;
175
+ }
176
+ return { drop: false, value: out };
177
+ }
178
+ return { drop: false, value };
179
+ };
180
+
181
+ const rewriteNode = (value: unknown, schema: z.ZodType, map: RefMap): RewriteOutcome => {
182
+ // Note: this zod version's classic accessors return core types, hence the
183
+ // casts below. Runtime behavior is identical; only the annotations differ.
184
+ if (
185
+ typeof value === "object" &&
186
+ value !== null &&
187
+ !Array.isArray(value) &&
188
+ refSchema.safeParse(value).success
189
+ ) {
190
+ const mapped = map(value as Ref);
191
+ return mapped === null ? { drop: true } : { drop: false, value: mapped };
192
+ }
193
+ let node: z.ZodType = schema;
194
+ while (
195
+ node instanceof z.ZodNullable ||
196
+ node instanceof z.ZodOptional ||
197
+ node instanceof z.ZodDefault
198
+ ) {
199
+ node = node.unwrap() as z.ZodType;
200
+ }
201
+ if (node === refSchema) {
202
+ if (!refSchema.safeParse(value).success) return { drop: false, value };
203
+ const mapped = map(value as Ref);
204
+ return mapped === null ? { drop: true } : { drop: false, value: mapped };
205
+ }
206
+ if (node instanceof z.ZodArray) {
207
+ if (!Array.isArray(value)) return { drop: false, value };
208
+ const out: unknown[] = [];
209
+ for (const item of value) {
210
+ const rewritten = rewriteNode(item, node.element as z.ZodType, map);
211
+ if (!rewritten.drop) out.push(rewritten.value);
212
+ }
213
+ return { drop: false, value: out };
214
+ }
215
+ if (node instanceof z.ZodObject) {
216
+ if (typeof value !== "object" || value === null || Array.isArray(value))
217
+ return { drop: false, value };
218
+ const shape = node.shape as Record<string, z.ZodType>;
219
+ const record = value as Record<string, unknown>;
220
+ const out: Record<string, unknown> = {};
221
+ for (const [key, item] of Object.entries(record)) {
222
+ if (!(key in shape)) {
223
+ const scrubbed = rewriteUnknown(item, map);
224
+ out[key] = scrubbed.drop ? null : scrubbed.value;
225
+ continue;
226
+ }
227
+ const child = shape[key];
228
+ const rewritten = rewriteNode(item, child, map);
229
+ if (rewritten.drop) {
230
+ if (child instanceof z.ZodOptional || child instanceof z.ZodDefault) continue;
231
+ if (child instanceof z.ZodNullable) {
232
+ out[key] = null;
233
+ continue;
234
+ }
235
+ return { drop: true };
236
+ }
237
+ out[key] = rewritten.value;
238
+ }
239
+ return { drop: false, value: out };
240
+ }
241
+ return { drop: false, value };
242
+ };
243
+
244
+ /**
245
+ * Schema-driven reference rewrite over an already-validated record subtree.
246
+ * Every `Ref` anywhere in the value is mapped; unportable references are
247
+ * dropped from arrays, nulled in nullable fields, and propagated upward
248
+ * anywhere else — so a new ref-bearing field can never silently slip
249
+ * through export or import. Unknown (schema-absent) keys are scrubbed the
250
+ * same way. Returns null only when the root itself is an unportable ref.
251
+ */
252
+ export const rewriteRecordRefs = (value: unknown, schema: z.ZodType, map: RefMap): unknown => {
253
+ const rewritten = rewriteNode(value, schema, map);
254
+ return rewritten.drop ? null : rewritten.value;
255
+ };
256
+
150
257
  export const provenanceSchema = z
151
258
  .object({
152
259
  kind: z.enum(["host_observed", "agent_reported", "imported"]),