File
Blob: src/cloudflare/internal/test/to-markdown/to-markdown-mock.js
| 1 | // Copyright (c) 2024 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | |
| 5 | function base64ToBlob(base64, mimeType) { |
| 6 | const binaryString = atob(base64); // Decode Base64 |
| 7 | const len = binaryString.length; |
| 8 | const bytes = new Uint8Array(len); |
| 9 | for (let i = 0; i < len; i++) { |
| 10 | bytes[i] = binaryString.charCodeAt(i); |
| 11 | } |
| 12 | return new Blob([bytes], { type: mimeType }); |
| 13 | } |
| 14 | |
| 15 | export default { |
| 16 | async fetch(request, env, ctx) { |
| 17 | const url = new URL(request.url); |
| 18 | |
| 19 | if (url.pathname === '/to-everything/markdown/transformer') { |
| 20 | const body = await request.json(); |
| 21 | const decoder = new TextDecoder('utf-8'); |
| 22 | |
| 23 | const result = []; |
| 24 | |
| 25 | for (const file of body.files) { |
| 26 | if (file.name === 'headers.md') { |
| 27 | const newHeaders = new Headers(request.headers); |
| 28 | newHeaders.delete('content-length'); |
| 29 | result.push({ |
| 30 | name: file.name, |
| 31 | mimeType: file.mimeType, |
| 32 | format: 'markdown', |
| 33 | tokens: 0, |
| 34 | data: Object.fromEntries(newHeaders.entries()), |
| 35 | }); |
| 36 | } else { |
| 37 | const fileblob = await base64ToBlob(file.data, file.mimeType); |
| 38 | const arr = await fileblob.arrayBuffer(); |
| 39 | result.push({ |
| 40 | name: file.name, |
| 41 | mimeType: file.mimeType, |
| 42 | format: 'markdown', |
| 43 | tokens: 0, |
| 44 | data: decoder.decode(arr), |
| 45 | }); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | return Response.json({ |
| 50 | success: true, |
| 51 | result: result, |
| 52 | }); |
| 53 | } |
| 54 | |
| 55 | if (url.pathname === '/to-everything/markdown/supported') { |
| 56 | return Response.json({ |
| 57 | success: true, |
| 58 | result: [ |
| 59 | { |
| 60 | extension: '.md', |
| 61 | mimeType: 'text/markdown', |
| 62 | }, |
| 63 | { |
| 64 | extension: '.png', |
| 65 | mimeType: 'image/png', |
| 66 | }, |
| 67 | ], |
| 68 | }); |
| 69 | } |
| 70 | }, |
| 71 | }; |