@cvfile/cv-detector 0.1.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.
- package/LICENSE +190 -0
- package/README.md +28 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +199 -0
- package/dist/index.js.map +1 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
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 describing the origin of the Work and
|
|
141
|
+
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 Support. While redistributing the Work or
|
|
166
|
+
Derivative Works thereof, You may choose to offer, and charge a
|
|
167
|
+
fee for, acceptance of support, warranty, indemnity, or other
|
|
168
|
+
liability obligations and/or rights consistent with this License.
|
|
169
|
+
However, in accepting such obligations, You may act only on Your
|
|
170
|
+
own behalf and on Your sole responsibility, not on behalf of any
|
|
171
|
+
other Contributor, and only if You agree to indemnify, defend,
|
|
172
|
+
and hold each Contributor harmless for any liability incurred by,
|
|
173
|
+
or claims asserted against, such Contributor by reason of your
|
|
174
|
+
accepting any such warranty or support.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright 2026 The cvfile.org authors
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# @cvfile/cv-detector (TypeScript)
|
|
2
|
+
|
|
3
|
+
```bash
|
|
4
|
+
pnpm add @cvfile/cv-detector
|
|
5
|
+
```
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { readFile } from 'node:fs/promises';
|
|
9
|
+
import { detect, unwrap } from '@cvfile/cv-detector';
|
|
10
|
+
|
|
11
|
+
const data = await readFile('resume.pdf');
|
|
12
|
+
const det = detect(data);
|
|
13
|
+
if (!det.isCvFile) {
|
|
14
|
+
console.log('plain PDF, OCR as usual');
|
|
15
|
+
} else {
|
|
16
|
+
const payload = await unwrap(data);
|
|
17
|
+
if (payload) {
|
|
18
|
+
const markdown = new TextDecoder().decode(payload.bytes);
|
|
19
|
+
console.log(`got ${payload.name} (${payload.mimeType}, ${markdown.length} chars)`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
`detect()` is dependency free (regex over PDF bytes, runs anywhere a
|
|
25
|
+
`Uint8Array` runs). `unwrap()` uses [`pdf-lib`](https://pdf-lib.js.org/) to
|
|
26
|
+
parse the `/AF` Associated Files array.
|
|
27
|
+
|
|
28
|
+
See `../README.md` for the cross-language story and rationale.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tiny standalone detector for the .cv open file format.
|
|
3
|
+
*
|
|
4
|
+
* A .cv file is a valid PDF that carries Markdown and HTML payloads via PDF
|
|
5
|
+
* Associated Files (/AF). Crawlers that already read application/pdf can use
|
|
6
|
+
* this module to (a) detect a .cv wrapper inside an arbitrary PDF and (b)
|
|
7
|
+
* unwrap the canonical Markdown payload directly, skipping OCR over the
|
|
8
|
+
* visual layer entirely.
|
|
9
|
+
*
|
|
10
|
+
* Detection is dependency free (regex over the PDF bytes). Unwrap depends on
|
|
11
|
+
* pdf-lib only because PDF stream parsing without a library is genuinely
|
|
12
|
+
* error prone.
|
|
13
|
+
*
|
|
14
|
+
* Spec: https://cvfile.org/spec/
|
|
15
|
+
*/
|
|
16
|
+
export declare const CV_NAMESPACE_URI = "http://ns.cvfile.org/cv/1.0/";
|
|
17
|
+
export interface CvDetection {
|
|
18
|
+
isCvFile: boolean;
|
|
19
|
+
version?: string;
|
|
20
|
+
primaryPayload?: string;
|
|
21
|
+
primaryLanguage?: string;
|
|
22
|
+
generator?: string;
|
|
23
|
+
}
|
|
24
|
+
export interface UnwrappedPayload {
|
|
25
|
+
name: string;
|
|
26
|
+
mimeType: string;
|
|
27
|
+
bytes: Uint8Array;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Detect whether `pdfBytes` is a .cv file. Zero dependencies, byte scan only.
|
|
31
|
+
* Any false positive on the namespace check is filtered out by the follow-up
|
|
32
|
+
* cv:version regex.
|
|
33
|
+
*/
|
|
34
|
+
export declare function detect(pdfBytes: Uint8Array): CvDetection;
|
|
35
|
+
/**
|
|
36
|
+
* Extract one /AF Associated File from a .cv file by name. If `payloadName`
|
|
37
|
+
* is omitted, returns the payload declared by cv:primaryPayload (typically
|
|
38
|
+
* `resume.md`). Returns null when the input is not a .cv or the named
|
|
39
|
+
* payload is not present.
|
|
40
|
+
*/
|
|
41
|
+
export declare function unwrap(pdfBytes: Uint8Array, payloadName?: string): Promise<UnwrappedPayload | null>;
|
|
42
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAaH,eAAO,MAAM,gBAAgB,iCAAiC,CAAC;AAE/D,MAAM,WAAW,WAAW;IAC1B,QAAQ,EAAE,OAAO,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,UAAU,CAAC;CACnB;AAED;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,QAAQ,EAAE,UAAU,GAAG,WAAW,CAoBxD;AAED;;;;;GAKG;AACH,wBAAsB,MAAM,CAC1B,QAAQ,EAAE,UAAU,EACpB,WAAW,CAAC,EAAE,MAAM,GACnB,OAAO,CAAC,gBAAgB,GAAG,IAAI,CAAC,CAwClC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tiny standalone detector for the .cv open file format.
|
|
3
|
+
*
|
|
4
|
+
* A .cv file is a valid PDF that carries Markdown and HTML payloads via PDF
|
|
5
|
+
* Associated Files (/AF). Crawlers that already read application/pdf can use
|
|
6
|
+
* this module to (a) detect a .cv wrapper inside an arbitrary PDF and (b)
|
|
7
|
+
* unwrap the canonical Markdown payload directly, skipping OCR over the
|
|
8
|
+
* visual layer entirely.
|
|
9
|
+
*
|
|
10
|
+
* Detection is dependency free (regex over the PDF bytes). Unwrap depends on
|
|
11
|
+
* pdf-lib only because PDF stream parsing without a library is genuinely
|
|
12
|
+
* error prone.
|
|
13
|
+
*
|
|
14
|
+
* Spec: https://cvfile.org/spec/
|
|
15
|
+
*/
|
|
16
|
+
import { PDFArray, PDFDict, PDFDocument, PDFHexString, PDFName, PDFStream, PDFString, decodePDFRawStream, } from 'pdf-lib';
|
|
17
|
+
export const CV_NAMESPACE_URI = 'http://ns.cvfile.org/cv/1.0/';
|
|
18
|
+
/**
|
|
19
|
+
* Detect whether `pdfBytes` is a .cv file. Zero dependencies, byte scan only.
|
|
20
|
+
* Any false positive on the namespace check is filtered out by the follow-up
|
|
21
|
+
* cv:version regex.
|
|
22
|
+
*/
|
|
23
|
+
export function detect(pdfBytes) {
|
|
24
|
+
if (pdfBytes.length < 4 || !hasPrefix(pdfBytes, '%PDF')) {
|
|
25
|
+
return { isCvFile: false };
|
|
26
|
+
}
|
|
27
|
+
const text = bytesToLatin1(pdfBytes);
|
|
28
|
+
if (!text.includes(CV_NAMESPACE_URI)) {
|
|
29
|
+
return { isCvFile: false };
|
|
30
|
+
}
|
|
31
|
+
const version = innerTag(text, 'cv:version');
|
|
32
|
+
if (!version) {
|
|
33
|
+
return { isCvFile: false };
|
|
34
|
+
}
|
|
35
|
+
const out = { isCvFile: true, version };
|
|
36
|
+
const pp = innerTag(text, 'cv:primaryPayload');
|
|
37
|
+
if (pp)
|
|
38
|
+
out.primaryPayload = pp;
|
|
39
|
+
const pl = innerTag(text, 'cv:primaryLanguage');
|
|
40
|
+
if (pl)
|
|
41
|
+
out.primaryLanguage = pl;
|
|
42
|
+
const gen = innerTag(text, 'cv:generator');
|
|
43
|
+
if (gen)
|
|
44
|
+
out.generator = gen;
|
|
45
|
+
return out;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Extract one /AF Associated File from a .cv file by name. If `payloadName`
|
|
49
|
+
* is omitted, returns the payload declared by cv:primaryPayload (typically
|
|
50
|
+
* `resume.md`). Returns null when the input is not a .cv or the named
|
|
51
|
+
* payload is not present.
|
|
52
|
+
*/
|
|
53
|
+
export async function unwrap(pdfBytes, payloadName) {
|
|
54
|
+
const det = detect(pdfBytes);
|
|
55
|
+
if (!det.isCvFile)
|
|
56
|
+
return null;
|
|
57
|
+
const target = payloadName ?? det.primaryPayload;
|
|
58
|
+
if (!target)
|
|
59
|
+
return null;
|
|
60
|
+
let pdfDoc;
|
|
61
|
+
try {
|
|
62
|
+
pdfDoc = await PDFDocument.load(pdfBytes, { ignoreEncryption: true, updateMetadata: false });
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
const afRaw = pdfDoc.catalog.get(PDFName.of('AF'));
|
|
68
|
+
if (!afRaw)
|
|
69
|
+
return null;
|
|
70
|
+
const afArray = pdfDoc.context.lookup(afRaw, PDFArray);
|
|
71
|
+
for (let i = 0; i < afArray.size(); i += 1) {
|
|
72
|
+
const filespec = pdfDoc.context.lookup(afArray.get(i), PDFDict);
|
|
73
|
+
const name = readString(filespec, PDFName.of('UF')) ?? readString(filespec, PDFName.of('F')) ?? '';
|
|
74
|
+
if (name !== target)
|
|
75
|
+
continue;
|
|
76
|
+
const efRaw = filespec.get(PDFName.of('EF'));
|
|
77
|
+
if (!efRaw)
|
|
78
|
+
continue;
|
|
79
|
+
const ef = pdfDoc.context.lookup(efRaw, PDFDict);
|
|
80
|
+
const streamRef = ef.get(PDFName.of('UF')) ?? ef.get(PDFName.of('F'));
|
|
81
|
+
if (!streamRef)
|
|
82
|
+
continue;
|
|
83
|
+
const stream = pdfDoc.context.lookup(streamRef);
|
|
84
|
+
if (!(stream instanceof PDFStream))
|
|
85
|
+
continue;
|
|
86
|
+
const bytes = decodeStream(stream);
|
|
87
|
+
if (!bytes)
|
|
88
|
+
continue;
|
|
89
|
+
const subtype = stream.dict.get(PDFName.of('Subtype')) ?? filespec.get(PDFName.of('Subtype'));
|
|
90
|
+
const mimeType = subtype instanceof PDFName ? decodeMimeName(subtype) : 'application/octet-stream';
|
|
91
|
+
return { name, mimeType, bytes };
|
|
92
|
+
}
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
function hasPrefix(bytes, prefix) {
|
|
96
|
+
for (let i = 0; i < prefix.length; i += 1) {
|
|
97
|
+
if (bytes[i] !== prefix.charCodeAt(i))
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
function bytesToLatin1(bytes) {
|
|
103
|
+
let out = '';
|
|
104
|
+
const chunk = 0x8000;
|
|
105
|
+
for (let i = 0; i < bytes.length; i += chunk) {
|
|
106
|
+
out += String.fromCharCode(...bytes.subarray(i, Math.min(i + chunk, bytes.length)));
|
|
107
|
+
}
|
|
108
|
+
return out;
|
|
109
|
+
}
|
|
110
|
+
function innerTag(text, tag) {
|
|
111
|
+
const re = new RegExp(`<${escapeRegex(tag)}>([^<]*)</${escapeRegex(tag)}>`);
|
|
112
|
+
const m = re.exec(text);
|
|
113
|
+
return m ? m[1].trim() : undefined;
|
|
114
|
+
}
|
|
115
|
+
function escapeRegex(s) {
|
|
116
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
117
|
+
}
|
|
118
|
+
function decodeMimeName(name) {
|
|
119
|
+
return name
|
|
120
|
+
.asString()
|
|
121
|
+
.slice(1)
|
|
122
|
+
.replace(/#([0-9a-fA-F]{2})/g, (_m, hex) => String.fromCharCode(parseInt(hex, 16)));
|
|
123
|
+
}
|
|
124
|
+
function readString(dict, key) {
|
|
125
|
+
const value = dict.get(key);
|
|
126
|
+
if (!value)
|
|
127
|
+
return undefined;
|
|
128
|
+
if (value instanceof PDFString)
|
|
129
|
+
return decodePdfStringEscapes(value.asString());
|
|
130
|
+
if (value instanceof PDFHexString)
|
|
131
|
+
return value.decodeText();
|
|
132
|
+
return undefined;
|
|
133
|
+
}
|
|
134
|
+
function decodeStream(stream) {
|
|
135
|
+
try {
|
|
136
|
+
return decodePDFRawStream(stream).decode();
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
// PDF spec §7.3.4.2: \n \r \t \b \f \( \) \\ and three-digit octal \NNN.
|
|
143
|
+
// pypdf escapes characters like '.' as octal (e.g. \056); we must decode so
|
|
144
|
+
// "resume\056md" comes back as "resume.md".
|
|
145
|
+
function decodePdfStringEscapes(s) {
|
|
146
|
+
let out = '';
|
|
147
|
+
let i = 0;
|
|
148
|
+
while (i < s.length) {
|
|
149
|
+
const c = s[i];
|
|
150
|
+
if (c !== '\\') {
|
|
151
|
+
out += c;
|
|
152
|
+
i += 1;
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
const next = s[i + 1];
|
|
156
|
+
if (next === undefined) {
|
|
157
|
+
out += c;
|
|
158
|
+
i += 1;
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
if (next >= '0' && next <= '7') {
|
|
162
|
+
let octal = next;
|
|
163
|
+
let j = i + 2;
|
|
164
|
+
while (j < s.length && octal.length < 3 && s[j] >= '0' && s[j] <= '7') {
|
|
165
|
+
octal += s[j];
|
|
166
|
+
j += 1;
|
|
167
|
+
}
|
|
168
|
+
out += String.fromCharCode(parseInt(octal, 8));
|
|
169
|
+
i = j;
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
switch (next) {
|
|
173
|
+
case 'n':
|
|
174
|
+
out += '\n';
|
|
175
|
+
break;
|
|
176
|
+
case 'r':
|
|
177
|
+
out += '\r';
|
|
178
|
+
break;
|
|
179
|
+
case 't':
|
|
180
|
+
out += '\t';
|
|
181
|
+
break;
|
|
182
|
+
case 'b':
|
|
183
|
+
out += '\b';
|
|
184
|
+
break;
|
|
185
|
+
case 'f':
|
|
186
|
+
out += '\f';
|
|
187
|
+
break;
|
|
188
|
+
case '(':
|
|
189
|
+
case ')':
|
|
190
|
+
case '\\':
|
|
191
|
+
out += next;
|
|
192
|
+
break;
|
|
193
|
+
default: out += next;
|
|
194
|
+
}
|
|
195
|
+
i += 2;
|
|
196
|
+
}
|
|
197
|
+
return out;
|
|
198
|
+
}
|
|
199
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EACL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
+
"description": "Tiny reference sniffer for the .cv open file format. Detects .cv wrapping inside any application/pdf and unwraps the canonical Markdown payload.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"homepage": "https://cvfile.org",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/cvfile/cv.git",
|
|
11
|
+
"directory": "tools/cv-detector/typescript"
|
|
12
|
+
},
|
|
13
|
+
"bugs": "https://github.com/cvfile/cv/issues",
|
|
14
|
+
"keywords": [
|
|
15
|
+
"cv",
|
|
16
|
+
"resume",
|
|
17
|
+
"pdf",
|
|
18
|
+
"open-format",
|
|
19
|
+
"rag",
|
|
20
|
+
"crawler",
|
|
21
|
+
"sniffer"
|
|
22
|
+
],
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=18"
|
|
25
|
+
},
|
|
26
|
+
"main": "./dist/index.js",
|
|
27
|
+
"module": "./dist/index.js",
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"import": "./dist/index.js"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist",
|
|
37
|
+
"README.md"
|
|
38
|
+
],
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"pdf-lib": "^1.17.1"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/node": "^22.10.5",
|
|
44
|
+
"typescript": "^5.7.3",
|
|
45
|
+
"vitest": "^2.1.8"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "tsc -p tsconfig.json",
|
|
49
|
+
"test": "vitest run"
|
|
50
|
+
}
|
|
51
|
+
}
|