Download next.js/packages/next/src/server/web/sandbox/fetch-inline-assets.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.34 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/packages/next/src/server/web/sandbox/fetch-inline-assets.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/next.js/packages/next/src/server/web/sandbox/fetch-inline-assets.ts
-
curl -L -o fetch-inline-assets.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/packages/next/src/server/web/sandbox/fetch-inline-assets.ts
1.34 kB
| import type { EdgeFunctionDefinition } from '../../../build/webpack/plugins/middleware-plugin' | |
| import { createReadStream, promises as fs } from 'fs' | |
| import { requestToBodyStream } from '../../body-streams' | |
| import { resolve } from 'path' | |
| /** | |
| * Short-circuits the `fetch` function | |
| * to return a stream for a given asset, if a user used `new URL("file", import.meta.url)`. | |
| * This allows to embed assets in Edge Runtime. | |
| */ | |
| export async function fetchInlineAsset(options: { | |
| input: RequestInfo | URL | |
| distDir: string | |
| assets: EdgeFunctionDefinition['assets'] | |
| context: { Response: typeof Response; ReadableStream: typeof ReadableStream } | |
| }): Promise<Response | undefined> { | |
| const inputString = String(options.input) | |
| if (!inputString.startsWith('blob:')) { | |
| return | |
| } | |
| const name = inputString.replace('blob:', '') | |
| const asset = options.assets | |
| ? options.assets.find((x) => x.name === name) | |
| : { | |
| name, | |
| filePath: name, | |
| } | |
| if (!asset) { | |
| return | |
| } | |
| const filePath = resolve(options.distDir, asset.filePath) | |
| const fileIsReadable = await fs.access(filePath).then( | |
| () => true, | |
| () => false | |
| ) | |
| if (fileIsReadable) { | |
| const readStream = createReadStream(filePath) | |
| return new options.context.Response( | |
| requestToBodyStream(options.context, Uint8Array, readStream) | |
| ) | |
| } | |
| } | |