@loomwork/classify 0.0.0-stage → 0.1.1
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 +202 -0
- package/NOTICE +8 -0
- package/README.md +73 -2
- package/dist/classify.d.ts +47 -0
- package/dist/classify.d.ts.map +1 -0
- package/dist/classify.js +147 -0
- package/dist/classify.js.map +1 -0
- package/dist/esm/classify.js +135 -0
- package/dist/esm/classify.js.map +1 -0
- package/dist/esm/expand.js +87 -0
- package/dist/esm/expand.js.map +1 -0
- package/dist/esm/external.js +33 -0
- package/dist/esm/external.js.map +1 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/redact.js +56 -0
- package/dist/esm/redact.js.map +1 -0
- package/dist/esm/taint.js +142 -0
- package/dist/esm/taint.js.map +1 -0
- package/dist/esm/unreachable.js +126 -0
- package/dist/esm/unreachable.js.map +1 -0
- package/dist/expand.d.ts +16 -0
- package/dist/expand.d.ts.map +1 -0
- package/dist/expand.js +91 -0
- package/dist/expand.js.map +1 -0
- package/dist/external.d.ts +15 -0
- package/dist/external.d.ts.map +1 -0
- package/dist/external.js +38 -0
- package/dist/external.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +38 -0
- package/dist/index.js.map +1 -0
- package/dist/redact.d.ts +4 -0
- package/dist/redact.d.ts.map +1 -0
- package/dist/redact.js +60 -0
- package/dist/redact.js.map +1 -0
- package/dist/taint.d.ts +85 -0
- package/dist/taint.d.ts.map +1 -0
- package/dist/taint.js +152 -0
- package/dist/taint.js.map +1 -0
- package/dist/unreachable.d.ts +20 -0
- package/dist/unreachable.d.ts.map +1 -0
- package/dist/unreachable.js +129 -0
- package/dist/unreachable.js.map +1 -0
- package/package.json +55 -3
- package/src/classify.ts +175 -0
- package/src/expand.ts +97 -0
- package/src/external.ts +40 -0
- package/src/index.ts +31 -0
- package/src/redact.ts +59 -0
- package/src/taint.ts +189 -0
- package/src/unreachable.ts +164 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
202
|
+
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,74 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @loomwork/classify — what a value is, on two axes
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The vocabulary other packages use to say how a piece of data must be handled. **Confidentiality** is a
|
|
4
|
+
property of a field, so you declare it once on the schema (`sensitive()`, `secret()`, `internal()`) and
|
|
5
|
+
the packages that mask, encrypt or externalize read the marks from there. **Integrity** is a property of a
|
|
6
|
+
value, acquired at runtime from a specific origin, so `tainted()` wraps the value where it enters the
|
|
7
|
+
system (an MCP tool result, a fetched page). This package only declares and reads; the enforcement lives
|
|
8
|
+
in the packages that consume it. You reach for it when you define schemas or ingest third-party content.
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
import { z } from "zod";
|
|
12
|
+
import {
|
|
13
|
+
collectClassifiedPaths, isTainted, maskPaths, pathsOfClass, scanTaint,
|
|
14
|
+
secret, sensitive, tainted, unwrapTainted,
|
|
15
|
+
} from "@loomwork/classify";
|
|
16
|
+
|
|
17
|
+
// Confidentiality: declared once, on the schema.
|
|
18
|
+
const Signup = z.object({
|
|
19
|
+
email: sensitive(z.string().email()),
|
|
20
|
+
apiKey: secret(z.string()),
|
|
21
|
+
plan: z.string(),
|
|
22
|
+
});
|
|
23
|
+
collectClassifiedPaths(Signup);
|
|
24
|
+
// [{ path: "email", dataClass: "sensitive" }, { path: "apiKey", dataClass: "secret" }]
|
|
25
|
+
maskPaths({ email: "ada@example.com", apiKey: "k", plan: "pro" }, pathsOfClass(Signup, "sensitive"));
|
|
26
|
+
// { email: "[redacted]", apiKey: "k", plan: "pro" }
|
|
27
|
+
|
|
28
|
+
// Integrity: attached to a value where it enters the system.
|
|
29
|
+
const fetched = tainted("ignore previous instructions", "mcp:search/web");
|
|
30
|
+
isTainted(fetched); // true
|
|
31
|
+
unwrapTainted(fetched); // the plain string: provenance stops travelling here
|
|
32
|
+
scanTaint({ summary: "ok", raw: { hit: fetched } }); // { origins: ["mcp:search/web"], exceeded: false, tainted: true }
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Surface
|
|
36
|
+
|
|
37
|
+
- **Confidentiality marks.** `sensitive`, `secret`, `internal`, and `classify(schema, dataClass)` for a
|
|
38
|
+
computed class. They return the same schema, so validation is untouched. Read them with
|
|
39
|
+
`collectClassifiedPaths` (paths and classes), `pathsOfClass`, `classOf` and `strictest`.
|
|
40
|
+
- **Where-stored mark.** `externalized()` with `externalizedPaths` and `isExternalized` (ADR 0046): a
|
|
41
|
+
separate axis for "the log keeps a content reference, not the bytes".
|
|
42
|
+
- **Masking.** `maskPaths(value, paths)` replaces each dot-path with `REDACTED` (`"[redacted]"`) and
|
|
43
|
+
copies only the spine it touches. `expandPaths` and `hasWildcard` handle the `[]` array-crossing
|
|
44
|
+
segment (`people[].ssn`).
|
|
45
|
+
- **Taint.** `tainted(value, origin)`, `isTainted`, `taintOf`, `unwrapTainted`, plus `scanTaint` and
|
|
46
|
+
`nestedTaintOrigin` for a wrapper buried anywhere in a value, and `taintOriginLabel`.
|
|
47
|
+
- **Audit.** `collectUnreachableMarks(schema)` finds marks that no dot-path can address.
|
|
48
|
+
|
|
49
|
+
## Behaviour that surprises people
|
|
50
|
+
|
|
51
|
+
- **A mark does nothing where the walker cannot reach it.** Paths cross objects and arrays only. A mark
|
|
52
|
+
under a record's values, a union branch, a tuple member or an intersection is not masked, encrypted or
|
|
53
|
+
externalized, and nothing says so at runtime; `collectUnreachableMarks` is the pre-flight check.
|
|
54
|
+
- **Marks live in a `WeakMap` keyed by the schema instance**, not on the schema. The walker looks through
|
|
55
|
+
optional, nullable, default and effects wrappers, but a different schema object than the one you marked
|
|
56
|
+
carries no mark.
|
|
57
|
+
- **`isTainted` looks at the top of a value only.** A producer that taints part of a result leaves a plain
|
|
58
|
+
object with a wrapper inside; use `scanTaint` for that. The scan is iterative and cycle-safe, and fails
|
|
59
|
+
closed: past depth 64 or 100,000 nodes it reports `exceeded: true` and `tainted: true`.
|
|
60
|
+
- **Re-tainting keeps the original origin.** Provenance records where content entered.
|
|
61
|
+
- **Taint is not retrofitted.** A log written before a producer applied `tainted()` replays without it.
|
|
62
|
+
- **The origin is display text.** It often contains a third-party-controlled fragment and is escaped where
|
|
63
|
+
it is rendered.
|
|
64
|
+
|
|
65
|
+
## Install
|
|
66
|
+
|
|
67
|
+
Unpublished for now: inside this repo use `workspace:*`; from a sibling checkout use a `link:` override (see [AGENTS.md](../../AGENTS.md)).
|
|
68
|
+
|
|
69
|
+
## See also
|
|
70
|
+
|
|
71
|
+
- API reference: section "`@loomwork/classify` — what a value IS, on two axes" in [docs/api-reference.org](../../docs/api-reference.org). It lists `classify(schema)` as returning paths; in the source that is `collectClassifiedPaths`.
|
|
72
|
+
- Guides: [data classification](../../docs/data-classification.md), [data protection patterns](../../docs/data-protection-patterns.md), [isolation and untrusted content](../../docs/user-guide/isolation-and-untrusted-content.md).
|
|
73
|
+
- ADRs: [0085 untrusted-content integrity axis](../../docs/adr/0085-untrusted-content-integrity-axis.md), [0046 content store seam](../../docs/adr/0046-content-store-seam-thin-authoritative-log.md).
|
|
74
|
+
- Source: `packages/classify/src`.
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
/** The three data-protection handling classes, ordered most-restrictive first. */
|
|
3
|
+
export type DataClass = "secret" | "sensitive" | "internal";
|
|
4
|
+
/**
|
|
5
|
+
* Tag a schema with a {@link DataClass}. Returns the SAME schema (validation unchanged) so
|
|
6
|
+
* it drops into a `z.object({ … })` shape transparently. Prefer the {@link secret} /
|
|
7
|
+
* {@link sensitive} / {@link internal} sugar; use this directly only for a computed class.
|
|
8
|
+
*/
|
|
9
|
+
export declare function classify<T extends z.ZodTypeAny>(schema: T, dataClass: DataClass): T;
|
|
10
|
+
/** Mark a field `"secret"` — never persisted; substituted before it reaches the log. */
|
|
11
|
+
export declare const secret: <T extends z.ZodTypeAny>(schema: T) => T;
|
|
12
|
+
/** Mark a field `"sensitive"` — encrypted-at-rest, crypto-shreddable, scrubbed from egress. */
|
|
13
|
+
export declare const sensitive: <T extends z.ZodTypeAny>(schema: T) => T;
|
|
14
|
+
/** Mark a field `"internal"` — persisted in the clear but never egressed externally. */
|
|
15
|
+
export declare const internal: <T extends z.ZodTypeAny>(schema: T) => T;
|
|
16
|
+
/** The class a schema was tagged with, or `undefined` if unclassified. */
|
|
17
|
+
export declare function classOf(schema: unknown): DataClass | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* Generic marked-path walker — the shared engine behind {@link collectClassifiedPaths} and the
|
|
20
|
+
* {@link externalized} marker. Walks an object schema, and for every node whose `markOf`
|
|
21
|
+
* returns a value emits `{ path, mark }`, recursing through nested objects and transparent
|
|
22
|
+
* wrappers. A marked *object* is reported at its own path and NOT descended into (the coarser
|
|
23
|
+
* mark wins — a marked address object is treated as a unit). Recurses THROUGH arrays with a
|
|
24
|
+
* `[]` crossing segment (`people[].ssn`), which the sinks expand against the concrete value
|
|
25
|
+
* (#260) — a mark inside an array element used to collect no path and silently do nothing.
|
|
26
|
+
* Deterministic: schema-declaration order (P2).
|
|
27
|
+
*/
|
|
28
|
+
export declare function collectMarkedPaths<T>(schema: z.ZodTypeAny, markOf: (node: z.ZodTypeAny) => T | undefined, prefix?: string): {
|
|
29
|
+
path: string;
|
|
30
|
+
mark: T;
|
|
31
|
+
}[];
|
|
32
|
+
/** A classified leaf/subtree and its dot-path from the walked root. */
|
|
33
|
+
export interface ClassifiedPath {
|
|
34
|
+
/** Dot-path from the schema root, e.g. `"user.ssn"`. Empty for the root itself. */
|
|
35
|
+
path: string;
|
|
36
|
+
dataClass: DataClass;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Walk an object schema and collect every classified field as a `{ path, dataClass }` (over the
|
|
40
|
+
* `secret`/`sensitive`/`internal` taxonomy). Thin wrapper over {@link collectMarkedPaths}.
|
|
41
|
+
*/
|
|
42
|
+
export declare function collectClassifiedPaths(schema: z.ZodTypeAny, prefix?: string): ClassifiedPath[];
|
|
43
|
+
/** The subset of {@link collectClassifiedPaths} matching one class (e.g. only `"sensitive"`). */
|
|
44
|
+
export declare function pathsOfClass(schema: z.ZodTypeAny, dataClass: DataClass): string[];
|
|
45
|
+
/** Strictest of two classes (secret > sensitive > internal); `undefined` is weakest. */
|
|
46
|
+
export declare function strictest(a: DataClass | undefined, b: DataClass | undefined): DataClass | undefined;
|
|
47
|
+
//# sourceMappingURL=classify.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"classify.d.ts","sourceRoot":"","sources":["../src/classify.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAoB7B,kFAAkF;AAClF,MAAM,MAAM,SAAS,GAAG,QAAQ,GAAG,WAAW,GAAG,UAAU,CAAC;AAW5D;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,CAAC,SAAS,CAAC,CAAC,UAAU,EAAE,MAAM,EAAE,CAAC,EAAE,SAAS,EAAE,SAAS,GAAG,CAAC,CAGnF;AAED,wFAAwF;AACxF,eAAO,MAAM,MAAM,GAAI,CAAC,SAAS,CAAC,CAAC,UAAU,EAAE,QAAQ,CAAC,KAAG,CAA+B,CAAC;AAC3F,+FAA+F;AAC/F,eAAO,MAAM,SAAS,GAAI,CAAC,SAAS,CAAC,CAAC,UAAU,EAAE,QAAQ,CAAC,KAAG,CAAkC,CAAC;AACjG,wFAAwF;AACxF,eAAO,MAAM,QAAQ,GAAI,CAAC,SAAS,CAAC,CAAC,UAAU,EAAE,QAAQ,CAAC,KAAG,CAAiC,CAAC;AAE/F,0EAA0E;AAC1E,wBAAgB,OAAO,CAAC,MAAM,EAAE,OAAO,GAAG,SAAS,GAAG,SAAS,CAE9D;AA4CD;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAClC,MAAM,EAAE,CAAC,CAAC,UAAU,EACpB,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,CAAC,UAAU,KAAK,CAAC,GAAG,SAAS,EAC7C,MAAM,SAAK,GACV;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,CAAC,CAAA;CAAE,EAAE,CAsB7B;AASD,uEAAuE;AACvE,MAAM,WAAW,cAAc;IAC7B,mFAAmF;IACnF,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,SAAS,CAAC;CACtB;AAED;;;GAGG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,CAAC,CAAC,UAAU,EAAE,MAAM,SAAK,GAAG,cAAc,EAAE,CAK1F;AAED,iGAAiG;AACjG,wBAAgB,YAAY,CAAC,MAAM,EAAE,CAAC,CAAC,UAAU,EAAE,SAAS,EAAE,SAAS,GAAG,MAAM,EAAE,CAIjF;AAED,wFAAwF;AACxF,wBAAgB,SAAS,CACvB,CAAC,EAAE,SAAS,GAAG,SAAS,EACxB,CAAC,EAAE,SAAS,GAAG,SAAS,GACvB,SAAS,GAAG,SAAS,CAIvB"}
|
package/dist/classify.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.internal = exports.sensitive = exports.secret = void 0;
|
|
4
|
+
exports.classify = classify;
|
|
5
|
+
exports.classOf = classOf;
|
|
6
|
+
exports.collectMarkedPaths = collectMarkedPaths;
|
|
7
|
+
exports.collectClassifiedPaths = collectClassifiedPaths;
|
|
8
|
+
exports.pathsOfClass = pathsOfClass;
|
|
9
|
+
exports.strictest = strictest;
|
|
10
|
+
/** Rank for "the strictest class wins" merges (secret > sensitive > internal). */
|
|
11
|
+
const RANK = { secret: 3, sensitive: 2, internal: 1 };
|
|
12
|
+
// A schema instance → its class. A WeakMap (not a mutated property) keeps the marker
|
|
13
|
+
// non-invasive: the Zod schema is returned byte-identical, its validation untouched, and
|
|
14
|
+
// a schema that is GC'd drops its entry. Keyed by the exact instance the author passed,
|
|
15
|
+
// which is the same instance a walker meets in `.shape`, so lookups line up.
|
|
16
|
+
const registry = new WeakMap();
|
|
17
|
+
/**
|
|
18
|
+
* Tag a schema with a {@link DataClass}. Returns the SAME schema (validation unchanged) so
|
|
19
|
+
* it drops into a `z.object({ … })` shape transparently. Prefer the {@link secret} /
|
|
20
|
+
* {@link sensitive} / {@link internal} sugar; use this directly only for a computed class.
|
|
21
|
+
*/
|
|
22
|
+
function classify(schema, dataClass) {
|
|
23
|
+
registry.set(schema, dataClass);
|
|
24
|
+
return schema;
|
|
25
|
+
}
|
|
26
|
+
/** Mark a field `"secret"` — never persisted; substituted before it reaches the log. */
|
|
27
|
+
const secret = (schema) => classify(schema, "secret");
|
|
28
|
+
exports.secret = secret;
|
|
29
|
+
/** Mark a field `"sensitive"` — encrypted-at-rest, crypto-shreddable, scrubbed from egress. */
|
|
30
|
+
const sensitive = (schema) => classify(schema, "sensitive");
|
|
31
|
+
exports.sensitive = sensitive;
|
|
32
|
+
/** Mark a field `"internal"` — persisted in the clear but never egressed externally. */
|
|
33
|
+
const internal = (schema) => classify(schema, "internal");
|
|
34
|
+
exports.internal = internal;
|
|
35
|
+
/** The class a schema was tagged with, or `undefined` if unclassified. */
|
|
36
|
+
function classOf(schema) {
|
|
37
|
+
return typeof schema === "object" && schema !== null ? registry.get(schema) : undefined;
|
|
38
|
+
}
|
|
39
|
+
// Unwrap the transparent wrappers Zod adds (.optional()/.nullable()/.default()/.refine())
|
|
40
|
+
// so a marker survives being wrapped AFTER classification, and so an object nested under a
|
|
41
|
+
// wrapper is still walked. Returns the wrapper chain (outermost→innermost) for class lookup
|
|
42
|
+
// plus the innermost node for shape inspection.
|
|
43
|
+
function chain(schema) {
|
|
44
|
+
const nodes = [];
|
|
45
|
+
let cur = schema;
|
|
46
|
+
// Guard against a pathological self-referential def; real schemas terminate quickly.
|
|
47
|
+
for (let i = 0; i < 100; i++) {
|
|
48
|
+
nodes.push(cur);
|
|
49
|
+
const def = cur._def;
|
|
50
|
+
const typeName = def?.typeName;
|
|
51
|
+
let next;
|
|
52
|
+
if (typeName === "ZodOptional" || typeName === "ZodNullable" || typeName === "ZodDefault") {
|
|
53
|
+
next = def?.innerType;
|
|
54
|
+
}
|
|
55
|
+
else if (typeName === "ZodEffects") {
|
|
56
|
+
next = def?.schema;
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
if (next === undefined)
|
|
62
|
+
break;
|
|
63
|
+
cur = next;
|
|
64
|
+
}
|
|
65
|
+
return { nodes, inner: cur };
|
|
66
|
+
}
|
|
67
|
+
/** The object `.shape` of a node, or `undefined` when it is not a `ZodObject`. */
|
|
68
|
+
function objectShape(node) {
|
|
69
|
+
const def = node._def;
|
|
70
|
+
if (def?.typeName !== "ZodObject" || typeof def.shape !== "function")
|
|
71
|
+
return undefined;
|
|
72
|
+
return def.shape();
|
|
73
|
+
}
|
|
74
|
+
/** The mark on a schema, looking through any transparent wrappers around it (first wins). */
|
|
75
|
+
function effectiveMark(schema, markOf) {
|
|
76
|
+
for (const node of chain(schema).nodes) {
|
|
77
|
+
const mark = markOf(node);
|
|
78
|
+
if (mark !== undefined)
|
|
79
|
+
return mark;
|
|
80
|
+
}
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Generic marked-path walker — the shared engine behind {@link collectClassifiedPaths} and the
|
|
85
|
+
* {@link externalized} marker. Walks an object schema, and for every node whose `markOf`
|
|
86
|
+
* returns a value emits `{ path, mark }`, recursing through nested objects and transparent
|
|
87
|
+
* wrappers. A marked *object* is reported at its own path and NOT descended into (the coarser
|
|
88
|
+
* mark wins — a marked address object is treated as a unit). Recurses THROUGH arrays with a
|
|
89
|
+
* `[]` crossing segment (`people[].ssn`), which the sinks expand against the concrete value
|
|
90
|
+
* (#260) — a mark inside an array element used to collect no path and silently do nothing.
|
|
91
|
+
* Deterministic: schema-declaration order (P2).
|
|
92
|
+
*/
|
|
93
|
+
function collectMarkedPaths(schema, markOf, prefix = "") {
|
|
94
|
+
const out = [];
|
|
95
|
+
const mark = effectiveMark(schema, markOf);
|
|
96
|
+
if (mark !== undefined && prefix !== "") {
|
|
97
|
+
out.push({ path: prefix, mark });
|
|
98
|
+
return out;
|
|
99
|
+
}
|
|
100
|
+
const inner = chain(schema).inner;
|
|
101
|
+
const element = arrayElement(inner);
|
|
102
|
+
if (element !== undefined) {
|
|
103
|
+
// Cross the array. The prefix gains `[]`, so `people` + element mark `ssn` yields
|
|
104
|
+
// `people[].ssn`; a mark on the ELEMENT type itself yields `people[]`.
|
|
105
|
+
return collectMarkedPaths(element, markOf, `${prefix}[]`);
|
|
106
|
+
}
|
|
107
|
+
const shape = objectShape(inner);
|
|
108
|
+
if (shape !== undefined) {
|
|
109
|
+
for (const [key, child] of Object.entries(shape)) {
|
|
110
|
+
const childPath = prefix === "" ? key : `${prefix}.${key}`;
|
|
111
|
+
out.push(...collectMarkedPaths(child, markOf, childPath));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return out;
|
|
115
|
+
}
|
|
116
|
+
/** The element type, if this node is an array. */
|
|
117
|
+
function arrayElement(node) {
|
|
118
|
+
const def = node._def;
|
|
119
|
+
if (def?.typeName !== "ZodArray")
|
|
120
|
+
return undefined;
|
|
121
|
+
return def.type;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Walk an object schema and collect every classified field as a `{ path, dataClass }` (over the
|
|
125
|
+
* `secret`/`sensitive`/`internal` taxonomy). Thin wrapper over {@link collectMarkedPaths}.
|
|
126
|
+
*/
|
|
127
|
+
function collectClassifiedPaths(schema, prefix = "") {
|
|
128
|
+
return collectMarkedPaths(schema, (node) => classOf(node), prefix).map((m) => ({
|
|
129
|
+
path: m.path,
|
|
130
|
+
dataClass: m.mark,
|
|
131
|
+
}));
|
|
132
|
+
}
|
|
133
|
+
/** The subset of {@link collectClassifiedPaths} matching one class (e.g. only `"sensitive"`). */
|
|
134
|
+
function pathsOfClass(schema, dataClass) {
|
|
135
|
+
return collectClassifiedPaths(schema)
|
|
136
|
+
.filter((c) => c.dataClass === dataClass)
|
|
137
|
+
.map((c) => c.path);
|
|
138
|
+
}
|
|
139
|
+
/** Strictest of two classes (secret > sensitive > internal); `undefined` is weakest. */
|
|
140
|
+
function strictest(a, b) {
|
|
141
|
+
if (a === undefined)
|
|
142
|
+
return b;
|
|
143
|
+
if (b === undefined)
|
|
144
|
+
return a;
|
|
145
|
+
return RANK[a] >= RANK[b] ? a : b;
|
|
146
|
+
}
|
|
147
|
+
//# sourceMappingURL=classify.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"classify.js","sourceRoot":"","sources":["../src/classify.ts"],"names":[],"mappings":";;;AAqCA,4BAGC;AAUD,0BAEC;AAsDD,gDA0BC;AAoBD,wDAKC;AAGD,oCAIC;AAGD,8BAOC;AAvJD,kFAAkF;AAClF,MAAM,IAAI,GAA8B,EAAE,MAAM,EAAE,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,CAAC;AAEjF,qFAAqF;AACrF,yFAAyF;AACzF,wFAAwF;AACxF,6EAA6E;AAC7E,MAAM,QAAQ,GAAG,IAAI,OAAO,EAAqB,CAAC;AAElD;;;;GAIG;AACH,SAAgB,QAAQ,CAAyB,MAAS,EAAE,SAAoB;IAC9E,QAAQ,CAAC,GAAG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,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;QAC7E,IAAI,EAAE,CAAC,CAAC,IAAI;QACZ,SAAS,EAAE,CAAC,CAAC,IAAI;KAClB,CAAC,CAAC,CAAC;AACN,CAAC;AAED,iGAAiG;AACjG,SAAgB,YAAY,CAAC,MAAoB,EAAE,SAAoB;IACrE,OAAO,sBAAsB,CAAC,MAAM,CAAC;SAClC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,SAAS,KAAK,SAAS,CAAC;SACxC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;AACxB,CAAC;AAED,wFAAwF;AACxF,SAAgB,SAAS,CACvB,CAAwB,EACxB,CAAwB;IAExB,IAAI,CAAC,KAAK,SAAS;QAAE,OAAO,CAAC,CAAC;IAC9B,IAAI,CAAC,KAAK,SAAS;QAAE,OAAO,CAAC,CAAC;IAC9B,OAAO,IAAI,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACpC,CAAC"}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/** Rank for "the strictest class wins" merges (secret > sensitive > internal). */
|
|
2
|
+
const RANK = { secret: 3, sensitive: 2, internal: 1 };
|
|
3
|
+
// A schema instance → its class. A WeakMap (not a mutated property) keeps the marker
|
|
4
|
+
// non-invasive: the Zod schema is returned byte-identical, its validation untouched, and
|
|
5
|
+
// a schema that is GC'd drops its entry. Keyed by the exact instance the author passed,
|
|
6
|
+
// which is the same instance a walker meets in `.shape`, so lookups line up.
|
|
7
|
+
const registry = new WeakMap();
|
|
8
|
+
/**
|
|
9
|
+
* Tag a schema with a {@link DataClass}. Returns the SAME schema (validation unchanged) so
|
|
10
|
+
* it drops into a `z.object({ … })` shape transparently. Prefer the {@link secret} /
|
|
11
|
+
* {@link sensitive} / {@link internal} sugar; use this directly only for a computed class.
|
|
12
|
+
*/
|
|
13
|
+
export function classify(schema, dataClass) {
|
|
14
|
+
registry.set(schema, dataClass);
|
|
15
|
+
return schema;
|
|
16
|
+
}
|
|
17
|
+
/** Mark a field `"secret"` — never persisted; substituted before it reaches the log. */
|
|
18
|
+
export const secret = (schema) => classify(schema, "secret");
|
|
19
|
+
/** Mark a field `"sensitive"` — encrypted-at-rest, crypto-shreddable, scrubbed from egress. */
|
|
20
|
+
export const sensitive = (schema) => classify(schema, "sensitive");
|
|
21
|
+
/** Mark a field `"internal"` — persisted in the clear but never egressed externally. */
|
|
22
|
+
export const internal = (schema) => classify(schema, "internal");
|
|
23
|
+
/** The class a schema was tagged with, or `undefined` if unclassified. */
|
|
24
|
+
export function classOf(schema) {
|
|
25
|
+
return typeof schema === "object" && schema !== null ? registry.get(schema) : undefined;
|
|
26
|
+
}
|
|
27
|
+
// Unwrap the transparent wrappers Zod adds (.optional()/.nullable()/.default()/.refine())
|
|
28
|
+
// so a marker survives being wrapped AFTER classification, and so an object nested under a
|
|
29
|
+
// wrapper is still walked. Returns the wrapper chain (outermost→innermost) for class lookup
|
|
30
|
+
// plus the innermost node for shape inspection.
|
|
31
|
+
function chain(schema) {
|
|
32
|
+
const nodes = [];
|
|
33
|
+
let cur = schema;
|
|
34
|
+
// Guard against a pathological self-referential def; real schemas terminate quickly.
|
|
35
|
+
for (let i = 0; i < 100; i++) {
|
|
36
|
+
nodes.push(cur);
|
|
37
|
+
const def = cur._def;
|
|
38
|
+
const typeName = def?.typeName;
|
|
39
|
+
let next;
|
|
40
|
+
if (typeName === "ZodOptional" || typeName === "ZodNullable" || typeName === "ZodDefault") {
|
|
41
|
+
next = def?.innerType;
|
|
42
|
+
}
|
|
43
|
+
else if (typeName === "ZodEffects") {
|
|
44
|
+
next = def?.schema;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
if (next === undefined)
|
|
50
|
+
break;
|
|
51
|
+
cur = next;
|
|
52
|
+
}
|
|
53
|
+
return { nodes, inner: cur };
|
|
54
|
+
}
|
|
55
|
+
/** The object `.shape` of a node, or `undefined` when it is not a `ZodObject`. */
|
|
56
|
+
function objectShape(node) {
|
|
57
|
+
const def = node._def;
|
|
58
|
+
if (def?.typeName !== "ZodObject" || typeof def.shape !== "function")
|
|
59
|
+
return undefined;
|
|
60
|
+
return def.shape();
|
|
61
|
+
}
|
|
62
|
+
/** The mark on a schema, looking through any transparent wrappers around it (first wins). */
|
|
63
|
+
function effectiveMark(schema, markOf) {
|
|
64
|
+
for (const node of chain(schema).nodes) {
|
|
65
|
+
const mark = markOf(node);
|
|
66
|
+
if (mark !== undefined)
|
|
67
|
+
return mark;
|
|
68
|
+
}
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Generic marked-path walker — the shared engine behind {@link collectClassifiedPaths} and the
|
|
73
|
+
* {@link externalized} marker. Walks an object schema, and for every node whose `markOf`
|
|
74
|
+
* returns a value emits `{ path, mark }`, recursing through nested objects and transparent
|
|
75
|
+
* wrappers. A marked *object* is reported at its own path and NOT descended into (the coarser
|
|
76
|
+
* mark wins — a marked address object is treated as a unit). Recurses THROUGH arrays with a
|
|
77
|
+
* `[]` crossing segment (`people[].ssn`), which the sinks expand against the concrete value
|
|
78
|
+
* (#260) — a mark inside an array element used to collect no path and silently do nothing.
|
|
79
|
+
* Deterministic: schema-declaration order (P2).
|
|
80
|
+
*/
|
|
81
|
+
export function collectMarkedPaths(schema, markOf, prefix = "") {
|
|
82
|
+
const out = [];
|
|
83
|
+
const mark = effectiveMark(schema, markOf);
|
|
84
|
+
if (mark !== undefined && prefix !== "") {
|
|
85
|
+
out.push({ path: prefix, mark });
|
|
86
|
+
return out;
|
|
87
|
+
}
|
|
88
|
+
const inner = chain(schema).inner;
|
|
89
|
+
const element = arrayElement(inner);
|
|
90
|
+
if (element !== undefined) {
|
|
91
|
+
// Cross the array. The prefix gains `[]`, so `people` + element mark `ssn` yields
|
|
92
|
+
// `people[].ssn`; a mark on the ELEMENT type itself yields `people[]`.
|
|
93
|
+
return collectMarkedPaths(element, markOf, `${prefix}[]`);
|
|
94
|
+
}
|
|
95
|
+
const shape = objectShape(inner);
|
|
96
|
+
if (shape !== undefined) {
|
|
97
|
+
for (const [key, child] of Object.entries(shape)) {
|
|
98
|
+
const childPath = prefix === "" ? key : `${prefix}.${key}`;
|
|
99
|
+
out.push(...collectMarkedPaths(child, markOf, childPath));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return out;
|
|
103
|
+
}
|
|
104
|
+
/** The element type, if this node is an array. */
|
|
105
|
+
function arrayElement(node) {
|
|
106
|
+
const def = node._def;
|
|
107
|
+
if (def?.typeName !== "ZodArray")
|
|
108
|
+
return undefined;
|
|
109
|
+
return def.type;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Walk an object schema and collect every classified field as a `{ path, dataClass }` (over the
|
|
113
|
+
* `secret`/`sensitive`/`internal` taxonomy). Thin wrapper over {@link collectMarkedPaths}.
|
|
114
|
+
*/
|
|
115
|
+
export function collectClassifiedPaths(schema, prefix = "") {
|
|
116
|
+
return collectMarkedPaths(schema, (node) => classOf(node), prefix).map((m) => ({
|
|
117
|
+
path: m.path,
|
|
118
|
+
dataClass: m.mark,
|
|
119
|
+
}));
|
|
120
|
+
}
|
|
121
|
+
/** The subset of {@link collectClassifiedPaths} matching one class (e.g. only `"sensitive"`). */
|
|
122
|
+
export function pathsOfClass(schema, dataClass) {
|
|
123
|
+
return collectClassifiedPaths(schema)
|
|
124
|
+
.filter((c) => c.dataClass === dataClass)
|
|
125
|
+
.map((c) => c.path);
|
|
126
|
+
}
|
|
127
|
+
/** Strictest of two classes (secret > sensitive > internal); `undefined` is weakest. */
|
|
128
|
+
export function strictest(a, b) {
|
|
129
|
+
if (a === undefined)
|
|
130
|
+
return b;
|
|
131
|
+
if (b === undefined)
|
|
132
|
+
return a;
|
|
133
|
+
return RANK[a] >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