|
| 1 | +import fsPromises from 'fs/promises'; |
| 2 | + |
| 3 | +import pagesManifest from '../.asset-manifests/pages.mjs'; |
| 4 | +import snippetsManifest from '../.asset-manifests/snippets.mjs'; |
| 5 | + |
| 6 | +export function readdir(path, options, cb) { |
| 7 | + const withFileTypes = !!options.withFileTypes; |
| 8 | + |
| 9 | + if (!withFileTypes) { |
| 10 | + // TODO: also support withFileTypes false |
| 11 | + throw new Error('fs#readdir please call readdir with withFileTypes true'); |
| 12 | + } |
| 13 | + |
| 14 | + console.log('fs#readdir', path); |
| 15 | + |
| 16 | + const result = findInDirentLikes(path); |
| 17 | + |
| 18 | + const results = |
| 19 | + !result || result.type !== 'directory' |
| 20 | + ? [] |
| 21 | + : result.children.map(c => ({ |
| 22 | + name: c.name, |
| 23 | + parentPath: c.parentPath, |
| 24 | + path: c.path, |
| 25 | + isFile: () => c.type === 'file', |
| 26 | + isDirectory: () => c.type === 'directory', |
| 27 | + })); |
| 28 | + |
| 29 | + cb?.(null, results); |
| 30 | +} |
| 31 | + |
| 32 | +function findInDirentLikes(path) { |
| 33 | + if (!path.startsWith('/pages') && !path.startsWith('/snippets')) { |
| 34 | + return null; |
| 35 | + } |
| 36 | + |
| 37 | + // remove the leading `/` |
| 38 | + path = path.slice(1); |
| 39 | + |
| 40 | + const paths = path.split('/'); |
| 41 | + |
| 42 | + const manifestType = paths.shift(); |
| 43 | + |
| 44 | + const manifest = manifestType === 'pages' ? pagesManifest : snippetsManifest; |
| 45 | + |
| 46 | + return recursivelyFindInDirentLikes(paths, manifest); |
| 47 | + function recursivelyFindInDirentLikes(paths, direntLikes) { |
| 48 | + const [current, ...restOfPaths] = paths; |
| 49 | + const found = direntLikes.find(item => item.name === current); |
| 50 | + if (!found) return null; |
| 51 | + if (restOfPaths.length === 0) return found; |
| 52 | + if (found.type !== 'directory') return null; |
| 53 | + return recursivelyFindInDirentLikes(restOfPaths, found.children); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +export function exists(path, cb) { |
| 58 | + const result = existsImpl(path); |
| 59 | + console.log('fs#exists', path, result); |
| 60 | + cb(result); |
| 61 | +} |
| 62 | + |
| 63 | +export function existsSync(path) { |
| 64 | + const result = existsImpl(path); |
| 65 | + console.log('fs#existsSync', path, result); |
| 66 | + return result; |
| 67 | +} |
| 68 | + |
| 69 | +function existsImpl(path) { |
| 70 | + if (!path.startsWith('/pages') && !path.startsWith('/snippets')) { |
| 71 | + return false; |
| 72 | + } |
| 73 | + return !!findInDirentLikes(path); |
| 74 | +} |
| 75 | + |
| 76 | +export function realpathSync() { |
| 77 | + return true; |
| 78 | +} |
| 79 | + |
| 80 | +const cloudflareContextSymbol = Symbol.for('__cloudflare-context__'); |
| 81 | + |
| 82 | +export function createReadStream(path) { |
| 83 | + console.log('fs#createReadStream', path); |
| 84 | + |
| 85 | + const { env } = global[cloudflareContextSymbol]; |
| 86 | + return env.ASSETS.fetch(new URL(`/${path}`, 'https://jamesrocks/')); |
| 87 | +} |
| 88 | + |
| 89 | +export default { |
| 90 | + readdir, |
| 91 | + exists, |
| 92 | + existsSync, |
| 93 | + realpathSync, |
| 94 | + promises: fsPromises, |
| 95 | +}; |
0 commit comments