@sonordev/contracts 2.2.0 → 2.4.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.
@@ -0,0 +1,87 @@
1
+ /**
2
+ * JSON-LD template placeholders: found once, dropped wherever sonor-api serves
3
+ * stored schema to a site, and never stored as implemented by register-schema.
4
+ *
5
+ * Stored JSON-LD (seo_schema_markup.schema_json, seo_pages.managed_schema) can
6
+ * carry a template's unfilled slots. Schema extracted from a site's source,
7
+ * where the base URL was a variable, comes back as https://example.com; an AI
8
+ * filling a template leaves "Example", "+1-000-000-0000", "[Resident Name]",
9
+ * "{plan.name}", or an object that is only a "note" saying what goes there.
10
+ * site-kit renders all of it on the live page, where search engines and AI
11
+ * crawlers take it as the business's identity. Observed 2026-10-01: a live
12
+ * site served an Organization named "Example", phone +1-000-000-0000, on its
13
+ * home page, and dozens more of its implemented rows carried example.com URLs
14
+ * and unfilled template slots.
15
+ *
16
+ * Conservative on purpose, because a false positive removes real schema from a
17
+ * live site:
18
+ * - Domains: only the reserved example domains (example.com, example.net,
19
+ * example.org, their subdomains, and the .example TLD), and only when the
20
+ * whole value is a URL, host or email address. A real domain with
21
+ * "example" in its path, or prose that mentions example.com, is fine.
22
+ * - Names: whole-value matches only (trimmed, case-insensitive) on name-like
23
+ * properties. "Example Plumbing Co" is a real name.
24
+ * - Phones (telephone, faxNumber, tel: links): all zeros, 123-456-7890,
25
+ * XXX-XXX-XXXX, and 555-0100 through 555-0199, the North American range
26
+ * reserved for fiction. 555-0200 and 555-1212 are real numbers.
27
+ * - Template slots: a value that is wholly a bracketed slot ("[YYYY-MM-DD]")
28
+ * or a JSX expression ("{plan.name}"), a {{mustache}} slot, a
29
+ * REPLACE_WITH_ marker, a title-case field name in brackets inside text
30
+ * ("Call [Phone Number]"), or a JSX member expression inside a URL. A URI
31
+ * template's {search_term_string} is how schema.org spells a SearchAction
32
+ * and is never a finding.
33
+ * - Annotations: an object that holds nothing but an AI note. A note key on
34
+ * a real node is stripped (it isn't schema.org vocabulary) and the node
35
+ * stays.
36
+ *
37
+ * The unit is the NODE: the innermost object with @type or @id (or a top-level
38
+ * or @graph member) whose own values hold the placeholder. A placeholder node
39
+ * is dropped whole, since its other values came from the same unfilled
40
+ * template. Its real siblings, and a real parent it hangs off, stay: an
41
+ * FAQPage keeps its questions when only its publisher was a placeholder. A
42
+ * node left with nothing but JSON-LD keywords once its placeholder children
43
+ * are gone (a BreadcrumbList whose every item was a placeholder) goes too, so
44
+ * the site falls back to what it generates itself.
45
+ *
46
+ * Single source of truth for "is this stored schema a placeholder", published
47
+ * as `@sonordev/contracts/schema-placeholders`: sonor-api's public schema reads
48
+ * and register-schema, and site-kit's ManagedSchema and LLMSchema (so a site
49
+ * drops a placeholder whichever Sonor source served it) all call it. Add new
50
+ * shapes here, with a test, never in a caller.
51
+ */
52
+ export type SchemaPlaceholderReason = 'example_domain' | 'placeholder_phone' | 'placeholder_name' | 'template_token' | 'annotation' | 'emptied';
53
+ export interface SchemaPlaceholderEvidence {
54
+ reason: SchemaPlaceholderReason;
55
+ /** The property that held the value. */
56
+ key: string;
57
+ /** The value, cut short for logs. */
58
+ value: string;
59
+ }
60
+ export interface SchemaPlaceholderNode {
61
+ /** Where the node sits: '$' for the root, then .key and [index] steps. */
62
+ path: string;
63
+ /** The node's @type ('A,B' when it has several), or null. */
64
+ type: string | null;
65
+ reasons: SchemaPlaceholderReason[];
66
+ evidence: SchemaPlaceholderEvidence[];
67
+ }
68
+ export interface SchemaPlaceholderScan<T> {
69
+ /** The value without its placeholder nodes: the same reference when nothing was found, null when nothing real is left. */
70
+ value: T | null;
71
+ /** Every node removed, outermost only (a dropped node's children go with it). */
72
+ dropped: SchemaPlaceholderNode[];
73
+ /** AI note keys stripped from nodes that were kept. */
74
+ notes: Array<{
75
+ path: string;
76
+ key: string;
77
+ }>;
78
+ }
79
+ /**
80
+ * The value with its placeholder nodes removed, and what was removed. Pure; the
81
+ * input is never mutated.
82
+ */
83
+ export declare function withoutSchemaPlaceholders<T>(value: T): SchemaPlaceholderScan<T>;
84
+ /** Which nodes of a JSON-LD value are placeholders, and why. */
85
+ export declare function findSchemaPlaceholders(value: unknown): SchemaPlaceholderNode[];
86
+ /** One line a person can act on: 'Organization at $: name "Example" is a placeholder name'. */
87
+ export declare function describeSchemaPlaceholder(node: SchemaPlaceholderNode): string;
@@ -0,0 +1,321 @@
1
+ 'use strict';
2
+
3
+ // src/contracts/schema-placeholders.ts
4
+ var RESERVED_EXAMPLE_DOMAINS = ["example.com", "example.net", "example.org"];
5
+ var RESERVED_EXAMPLE_TLD = "example";
6
+ var PLACEHOLDER_NAMES = /* @__PURE__ */ new Set([
7
+ "example",
8
+ "example business",
9
+ "example company",
10
+ "example organization",
11
+ "your business",
12
+ "your business name",
13
+ "your company",
14
+ "your company name",
15
+ "your organization",
16
+ "your organization name",
17
+ "business name",
18
+ "company name",
19
+ "organization name",
20
+ "your name",
21
+ "lorem ipsum"
22
+ ]);
23
+ var NAME_KEYS = /* @__PURE__ */ new Set([
24
+ "name",
25
+ "legalName",
26
+ "alternateName",
27
+ "author",
28
+ "creator",
29
+ "publisher",
30
+ "brand",
31
+ "provider",
32
+ "seller",
33
+ "manufacturer",
34
+ "organizer",
35
+ "founder"
36
+ ]);
37
+ var PHONE_KEYS = /* @__PURE__ */ new Set(["telephone", "faxNumber"]);
38
+ var URI_TEMPLATE_KEYS = /* @__PURE__ */ new Set(["target", "urlTemplate"]);
39
+ var BODY_TEXT_KEYS = /* @__PURE__ */ new Set(["articleBody"]);
40
+ var ANNOTATION_KEYS = /* @__PURE__ */ new Set(["note", "notes", "_note", "_notes", "_comment", "_comments", "__comment", "$comment"]);
41
+ var STRUCTURAL_KEYS = /* @__PURE__ */ new Set(["@context", "@type", "@id", "position"]);
42
+ var MUSTACHE_SLOT = /\{\{\s*[#^/>&]?\s*[A-Za-z_$][\w$.[\]-]*\s*\}\}/;
43
+ var JSX_EXPRESSION_VALUE = /^\{\s*[A-Za-z_$][\w$]*(?:\??\.[A-Za-z_$][\w$]*|\[[^\]]*\])+[^{}]*\}$/;
44
+ var JSX_MEMBER_IN_TEXT = /\{\s*[A-Za-z_$][\w$]*(?:\??\.[A-Za-z_$][\w$]*)+\s*\}/;
45
+ var BRACKET_SLOT_VALUE = /^\[\s*[A-Za-z][^[\]"{}<>]{0,80}\]$/;
46
+ var BRACKET_SLOT_IN_TEXT = /\[\s*((?:your|insert your|enter your|add your)\s+[^[\]]{1,60}?|(?:(?:business|company|organization|brand|full|first|last|resident|client|customer|author)\s+)?(?:name|phone(?:\s+number)?|telephone|email(?:\s+address)?|(?:street\s+)?address|city|state|zip(?:\s+code)?|postal\s+code|website|url|logo|date))\s*\](?!\()/gi;
47
+ var REPLACE_MARKER = /\bREPLACE(?:_WITH_\w*|_ME\b)/;
48
+ var UPPER_SLOT_VALUE = /^YOUR_[A-Z0-9_]+$/;
49
+ var MAX_DEPTH = 40;
50
+ var MAX_EVIDENCE = 5;
51
+ var EVIDENCE_VALUE_LENGTH = 120;
52
+ function withoutSchemaPlaceholders(value) {
53
+ const state = { dropped: [], notes: [] };
54
+ if (value === null || value === void 0) return { value: null, ...state };
55
+ const result = visitRoot(value, state);
56
+ return { value: result, ...state };
57
+ }
58
+ function findSchemaPlaceholders(value) {
59
+ return withoutSchemaPlaceholders(value).dropped;
60
+ }
61
+ var REASON_TEXT = {
62
+ example_domain: "is on a reserved example domain",
63
+ placeholder_phone: "is a placeholder phone number",
64
+ placeholder_name: "is a placeholder name",
65
+ template_token: "is an unfilled template slot",
66
+ annotation: "is an AI note, not schema",
67
+ emptied: "held only placeholders"
68
+ };
69
+ function describeSchemaPlaceholder(node) {
70
+ const where = `${node.type || "Node"} at ${node.path}`;
71
+ const first = node.evidence[0];
72
+ const detail = first ? `${first.key} ${JSON.stringify(first.value)} ${REASON_TEXT[first.reason]}` : REASON_TEXT[node.reasons[0]];
73
+ return node.reasons.includes("emptied") ? `${where} has nothing real left: ${detail}` : `${where}: ${detail}`;
74
+ }
75
+ function visitRoot(value, state) {
76
+ if (Array.isArray(value)) {
77
+ const out = visitArray(value, "$", state, 0, true);
78
+ return out.removed && out.value.length === 0 ? null : out.value;
79
+ }
80
+ if (!isPlainObject(value)) return value;
81
+ if (isGraphWrapper(value)) {
82
+ const out = visitContainer(value, "$", state, 0);
83
+ return out.removedChild && !hasContentBeyond(out.value, /* @__PURE__ */ new Set(["@context"])) ? null : out.value;
84
+ }
85
+ const visit = visitNode(value, "$", state, 0);
86
+ return visit.keep ? visit.value : null;
87
+ }
88
+ function visitNode(node, path, state, depth) {
89
+ if (isAnnotationOnly(node)) {
90
+ state.dropped.push({ path, type: null, reasons: ["annotation"], evidence: annotationEvidence(node) });
91
+ return { keep: false };
92
+ }
93
+ const evidence = [];
94
+ collectEvidence(node, evidence, depth);
95
+ if (evidence.length) {
96
+ state.dropped.push({ path, type: typeOf(node), reasons: unique(evidence.map((e) => e.reason)), evidence });
97
+ return { keep: false };
98
+ }
99
+ const droppedMark = state.dropped.length;
100
+ const notesMark = state.notes.length;
101
+ const pruned = visitContainer(node, path, state, depth);
102
+ if (pruned.removedChild && !hasContentBeyond(pruned.value, STRUCTURAL_KEYS)) {
103
+ const children = state.dropped.splice(droppedMark);
104
+ state.notes.length = notesMark;
105
+ state.dropped.push({
106
+ path,
107
+ type: typeOf(node),
108
+ reasons: unique(["emptied", ...children.flatMap((c) => c.reasons)]),
109
+ evidence: children.flatMap((c) => c.evidence).slice(0, MAX_EVIDENCE)
110
+ });
111
+ return { keep: false };
112
+ }
113
+ return { keep: true, value: pruned.value };
114
+ }
115
+ function visitContainer(obj, path, state, depth) {
116
+ if (depth >= MAX_DEPTH) return { value: obj, removedChild: false };
117
+ let changed = false;
118
+ let removedChild = false;
119
+ const out = {};
120
+ for (const [key, child] of Object.entries(obj)) {
121
+ if (key === "@context") {
122
+ out[key] = child;
123
+ continue;
124
+ }
125
+ if (ANNOTATION_KEYS.has(key)) {
126
+ state.notes.push({ path, key });
127
+ changed = true;
128
+ continue;
129
+ }
130
+ const childPath = stepKey(path, key);
131
+ if (Array.isArray(child)) {
132
+ const pruned = visitArray(child, childPath, state, depth + 1, key === "@graph");
133
+ if (pruned.removed) removedChild = true;
134
+ if (pruned.value !== child) changed = true;
135
+ if (pruned.removed && pruned.value.length === 0) continue;
136
+ out[key] = pruned.value;
137
+ continue;
138
+ }
139
+ if (isPlainObject(child)) {
140
+ if (isNestedNode(child)) {
141
+ const visit = visitNode(child, childPath, state, depth + 1);
142
+ if (!visit.keep) {
143
+ changed = true;
144
+ removedChild = true;
145
+ continue;
146
+ }
147
+ if (visit.value !== child) changed = true;
148
+ out[key] = visit.value;
149
+ continue;
150
+ }
151
+ const pruned = visitContainer(child, childPath, state, depth + 1);
152
+ if (pruned.removedChild) removedChild = true;
153
+ if (pruned.value !== child) changed = true;
154
+ out[key] = pruned.value;
155
+ continue;
156
+ }
157
+ out[key] = child;
158
+ }
159
+ return { value: changed ? out : obj, removedChild };
160
+ }
161
+ function visitArray(items, path, state, depth, units) {
162
+ if (depth >= MAX_DEPTH) return { value: items, removed: false };
163
+ let changed = false;
164
+ let removed = false;
165
+ const out = [];
166
+ items.forEach((item, index) => {
167
+ const itemPath = `${path}[${index}]`;
168
+ if (Array.isArray(item)) {
169
+ const pruned = visitArray(item, itemPath, state, depth + 1, units);
170
+ if (pruned.removed) removed = true;
171
+ if (pruned.value !== item) changed = true;
172
+ out.push(pruned.value);
173
+ return;
174
+ }
175
+ if (isPlainObject(item) && (units || isNestedNode(item))) {
176
+ const visit = visitNode(item, itemPath, state, depth + 1);
177
+ if (!visit.keep) {
178
+ changed = true;
179
+ removed = true;
180
+ return;
181
+ }
182
+ if (visit.value !== item) changed = true;
183
+ out.push(visit.value);
184
+ return;
185
+ }
186
+ if (isPlainObject(item)) {
187
+ const pruned = visitContainer(item, itemPath, state, depth + 1);
188
+ if (pruned.removedChild) removed = true;
189
+ if (pruned.value !== item) changed = true;
190
+ out.push(pruned.value);
191
+ return;
192
+ }
193
+ out.push(item);
194
+ });
195
+ return { value: changed ? out : items, removed };
196
+ }
197
+ function collectEvidence(holder, out, depth) {
198
+ for (const [key, value] of Object.entries(holder)) {
199
+ if (key === "@context" || ANNOTATION_KEYS.has(key)) continue;
200
+ collectFromValue(holder, key, value, out, depth);
201
+ }
202
+ }
203
+ function collectFromValue(holder, key, value, out, depth) {
204
+ if (depth >= MAX_DEPTH || out.length >= MAX_EVIDENCE) return;
205
+ if (typeof value === "string") {
206
+ for (const reason of placeholderReasons(key, value, holder)) {
207
+ if (out.length >= MAX_EVIDENCE) return;
208
+ out.push({ reason, key, value: cutForLog(value) });
209
+ }
210
+ return;
211
+ }
212
+ if (Array.isArray(value)) {
213
+ for (const item of value) collectFromValue(holder, key, item, out, depth + 1);
214
+ return;
215
+ }
216
+ if (isPlainObject(value) && !isNestedNode(value)) collectEvidence(value, out, depth + 1);
217
+ }
218
+ function placeholderReasons(key, raw, holder) {
219
+ const value = raw.trim();
220
+ if (!value) return [];
221
+ const reasons = [];
222
+ const host = urlHost(value);
223
+ if (host && isReservedExampleHost(host)) reasons.push("example_domain");
224
+ if ((PHONE_KEYS.has(key) || /^tel:/i.test(value)) && isPlaceholderPhone(value)) reasons.push("placeholder_phone");
225
+ if (NAME_KEYS.has(key) && !("value" in holder) && PLACEHOLDER_NAMES.has(normalizeName(value))) {
226
+ reasons.push("placeholder_name");
227
+ }
228
+ if (isTemplateSlot(key, value)) reasons.push("template_token");
229
+ return reasons;
230
+ }
231
+ function isTemplateSlot(key, value) {
232
+ if (JSX_EXPRESSION_VALUE.test(value) || BRACKET_SLOT_VALUE.test(value) || UPPER_SLOT_VALUE.test(value)) return true;
233
+ if (REPLACE_MARKER.test(value)) return true;
234
+ if (!BODY_TEXT_KEYS.has(key)) {
235
+ if (MUSTACHE_SLOT.test(value)) return true;
236
+ for (const match of value.matchAll(BRACKET_SLOT_IN_TEXT)) {
237
+ if (/^[A-Z]/.test(match[1])) return true;
238
+ }
239
+ }
240
+ return !URI_TEMPLATE_KEYS.has(key) && isUrlOrPath(value) && JSX_MEMBER_IN_TEXT.test(value);
241
+ }
242
+ function urlHost(value) {
243
+ const mailto = /^mailto:[^@\s?]+@([^?\s#/]+)/i.exec(value);
244
+ if (mailto) return mailto[1];
245
+ const url = /^(?:[a-z][a-z0-9+.-]*:)?\/\/(?:[^/?#@\s]*@)?([^/?#:\s]+)/i.exec(value);
246
+ if (url) return url[1];
247
+ const email = /^[^\s@/:]+@([^\s@/:?#]+)$/.exec(value);
248
+ if (email) return email[1];
249
+ const bare = /^((?:[a-z0-9-]+\.)+[a-z][a-z0-9-]*)\.?(?::\d+)?(?:[/?#]\S*)?$/i.exec(value);
250
+ return bare ? bare[1] : null;
251
+ }
252
+ function isReservedExampleHost(host) {
253
+ const h = host.toLowerCase().replace(/\.$/, "");
254
+ if (h === RESERVED_EXAMPLE_TLD || h.endsWith(`.${RESERVED_EXAMPLE_TLD}`)) return true;
255
+ return RESERVED_EXAMPLE_DOMAINS.some((domain) => h === domain || h.endsWith(`.${domain}`));
256
+ }
257
+ function isUrlOrPath(value) {
258
+ return /^(?:[a-z][a-z0-9+.-]*:)?\/\//i.test(value) || value.startsWith("/");
259
+ }
260
+ function isPlaceholderPhone(raw) {
261
+ const value = raw.replace(/^tel:/i, "").trim();
262
+ if (/^[+\d\s().xX-]+$/.test(value) && (value.match(/x/gi)?.length ?? 0) >= 4) return true;
263
+ let digits = value.replace(/\D/g, "");
264
+ if (digits.length === 11 && digits.startsWith("1")) digits = digits.slice(1);
265
+ if (digits.length >= 7 && /^0+$/.test(digits)) return true;
266
+ if (digits === "1234567890" || digits === "1234567") return true;
267
+ const local = digits.length === 10 ? digits.slice(3) : digits.length === 7 ? digits : null;
268
+ return local !== null && /^55501\d\d$/.test(local);
269
+ }
270
+ function normalizeName(value) {
271
+ return value.replace(/\s+/g, " ").trim().replace(/[.,]+$/, "").toLowerCase();
272
+ }
273
+ function isPlainObject(value) {
274
+ return typeof value === "object" && value !== null && !Array.isArray(value);
275
+ }
276
+ function isNestedNode(value) {
277
+ return "@type" in value || "@id" in value || isAnnotationOnly(value);
278
+ }
279
+ function isGraphWrapper(value) {
280
+ return "@graph" in value && !("@type" in value) && !("@id" in value);
281
+ }
282
+ function isAnnotationOnly(value) {
283
+ if ("@type" in value || "@id" in value || "@graph" in value) return false;
284
+ const keys = Object.keys(value).filter((key) => key !== "@context");
285
+ return keys.length > 0 && keys.every((key) => ANNOTATION_KEYS.has(key));
286
+ }
287
+ function annotationEvidence(node) {
288
+ return Object.entries(node).filter(([key]) => ANNOTATION_KEYS.has(key)).slice(0, MAX_EVIDENCE).map(([key, value]) => ({
289
+ reason: "annotation",
290
+ key,
291
+ value: cutForLog(typeof value === "string" ? value : JSON.stringify(value) ?? "")
292
+ }));
293
+ }
294
+ function hasContentBeyond(obj, ignored) {
295
+ return Object.entries(obj).some(([key, value]) => !ignored.has(key) && hasContent(value));
296
+ }
297
+ function hasContent(value) {
298
+ if (value === null || value === void 0 || value === "") return false;
299
+ if (Array.isArray(value)) return value.length > 0;
300
+ if (isPlainObject(value)) return Object.keys(value).length > 0;
301
+ return true;
302
+ }
303
+ function typeOf(node) {
304
+ const type = node["@type"];
305
+ if (typeof type === "string") return type;
306
+ if (Array.isArray(type)) return type.filter((t) => typeof t === "string").join(",") || null;
307
+ return null;
308
+ }
309
+ function stepKey(path, key) {
310
+ return /^[A-Za-z_$][\w$]*$/.test(key) ? `${path}.${key}` : `${path}[${JSON.stringify(key)}]`;
311
+ }
312
+ function unique(values) {
313
+ return [...new Set(values)];
314
+ }
315
+ function cutForLog(value) {
316
+ return value.length > EVIDENCE_VALUE_LENGTH ? `${value.slice(0, EVIDENCE_VALUE_LENGTH)}...` : value;
317
+ }
318
+
319
+ exports.describeSchemaPlaceholder = describeSchemaPlaceholder;
320
+ exports.findSchemaPlaceholders = findSchemaPlaceholders;
321
+ exports.withoutSchemaPlaceholders = withoutSchemaPlaceholders;