MCPcopy
hub / github.com/garrytan/gstack / parsePdfFromFile

Function parsePdfFromFile

browse/src/meta-commands.ts:140–182  ·  view source on GitHub ↗
(payloadPath: string)

Source from the content-addressed store, hash-verified

138}
139
140export function parsePdfFromFile(payloadPath: string): ParsedPdfArgs {
141 // Parity with load-html --from-file (browse/src/write-commands.ts) and
142 // the direct load-html <file> path: every caller-supplied file path
143 // must pass validateReadPath so the safe-dirs policy can't be skirted
144 // by routing reads through the --from-file shortcut.
145 try {
146 validateReadPath(path.resolve(payloadPath));
147 } catch {
148 throw new Error(
149 `pdf: --from-file ${payloadPath} must be under ${SAFE_DIRECTORIES.join(' or ')} (security policy). Copy the payload into the project tree or /tmp first.`
150 );
151 }
152 const raw = fs.readFileSync(payloadPath, 'utf8');
153 let json: any;
154 try {
155 json = JSON.parse(raw);
156 } catch (err) {
157 const msg = err instanceof Error ? err.message : String(err);
158 throw new Error(`pdf: --from-file ${payloadPath} is not valid JSON (${msg}).`);
159 }
160 if (json === null || typeof json !== 'object' || Array.isArray(json)) {
161 throw new Error(`pdf: --from-file ${payloadPath} must be a JSON object, got ${Array.isArray(json) ? 'array' : typeof json}.`);
162 }
163 const out: ParsedPdfArgs = {
164 output: json.output || `${TEMP_DIR}/browse-page.pdf`,
165 format: json.format,
166 width: json.width,
167 height: json.height,
168 marginTop: json.marginTop,
169 marginRight: json.marginRight,
170 marginBottom: json.marginBottom,
171 marginLeft: json.marginLeft,
172 headerTemplate: json.headerTemplate,
173 footerTemplate: json.footerTemplate,
174 pageNumbers: json.pageNumbers === true,
175 tagged: json.tagged === true,
176 outline: json.outline === true,
177 printBackground: json.printBackground === true,
178 preferCSSPageSize: json.preferCSSPageSize === true,
179 toc: json.toc === true,
180 };
181 return out;
182}
183
184function requireValue(args: string[], i: number, flag: string): string {
185 const v = args[i];

Calls 1

validateReadPathFunction · 0.90

Tested by

no test coverage detected