|
| 1 | +import type { IncomingMessage, Server, ServerResponse } from 'node:http' |
| 2 | +import type { AddressInfo } from 'node:net' |
| 3 | +import type { Payload } from 'payload' |
| 4 | + |
| 5 | +import { del, list } from '@vercel/blob' |
| 6 | +import { upload } from '@vercel/blob/client' |
| 7 | +import dotenv from 'dotenv' |
| 8 | +import { readFileSync } from 'fs' |
| 9 | +import { createServer } from 'node:http' |
| 10 | +import path from 'path' |
| 11 | +import { fileURLToPath } from 'url' |
| 12 | +import { afterAll, afterEach, beforeAll, describe, expect, it } from 'vitest' |
| 13 | + |
| 14 | +import type { NextRESTClient } from '../__helpers/shared/NextRESTClient.js' |
| 15 | + |
| 16 | +import { initPayloadInt } from '../__helpers/shared/initPayloadInt.js' |
| 17 | +import { prefix } from './shared.js' |
| 18 | + |
| 19 | +const filename = fileURLToPath(import.meta.url) |
| 20 | +const dirname = path.dirname(filename) |
| 21 | + |
| 22 | +// Load emulated env vars so @vercel/blob SDK uses the local emulator |
| 23 | +dotenv.config({ path: path.resolve(dirname, '../plugin-cloud-storage/.env.emulated') }) |
| 24 | + |
| 25 | +let payload: Payload |
| 26 | +let restClient: NextRESTClient |
| 27 | +let httpServer: Server |
| 28 | +let handleUploadUrl: string |
| 29 | + |
| 30 | +const serverHandlerPath = '/vercel-blob-client-upload-route' |
| 31 | + |
| 32 | +describe('@payloadcms/storage-vercel-blob clientUploads', () => { |
| 33 | + beforeAll(async () => { |
| 34 | + ;({ payload, restClient } = await initPayloadInt( |
| 35 | + dirname, |
| 36 | + undefined, |
| 37 | + undefined, |
| 38 | + path.resolve(dirname, 'clientUploads.config.ts'), |
| 39 | + )) |
| 40 | + |
| 41 | + // Start a real HTTP server to bridge upload()'s undici fetch calls to restClient. |
| 42 | + // @vercel/blob/client uses undici internally, so a real HTTP server is required. |
| 43 | + httpServer = createServer(async (req: IncomingMessage, res: ServerResponse) => { |
| 44 | + const chunks: Buffer[] = [] |
| 45 | + req.on('data', (chunk: Buffer) => chunks.push(chunk)) |
| 46 | + await new Promise<void>((resolve) => req.on('end', resolve)) |
| 47 | + |
| 48 | + const body = Buffer.concat(chunks).toString() |
| 49 | + const headers: Record<string, string> = {} |
| 50 | + for (const [key, value] of Object.entries(req.headers)) { |
| 51 | + if (typeof value === 'string') { |
| 52 | + headers[key] = value |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + const response = await restClient.POST(serverHandlerPath as `/${string}`, { body, headers }) |
| 57 | + const responseBody = await response.text() |
| 58 | + |
| 59 | + res.writeHead(response.status, { |
| 60 | + 'content-type': response.headers.get('content-type') ?? 'application/json', |
| 61 | + }) |
| 62 | + res.end(responseBody) |
| 63 | + }) |
| 64 | + |
| 65 | + await new Promise<void>((resolve) => httpServer.listen(0, '127.0.0.1', resolve)) |
| 66 | + const port = (httpServer.address() as AddressInfo).port |
| 67 | + handleUploadUrl = `http://127.0.0.1:${port}` |
| 68 | + }) |
| 69 | + |
| 70 | + afterAll(async () => { |
| 71 | + httpServer.close() |
| 72 | + await payload.destroy() |
| 73 | + }) |
| 74 | + |
| 75 | + afterEach(async () => { |
| 76 | + const { blobs } = await list() |
| 77 | + |
| 78 | + if (blobs.length > 0) { |
| 79 | + await del(blobs.map((b) => b.url)) |
| 80 | + } |
| 81 | + }) |
| 82 | + |
| 83 | + it('should upload a file via client upload flow', async () => { |
| 84 | + const file = readFileSync(path.resolve(dirname, '../uploads/image.png')) |
| 85 | + const pathname = 'image.png' |
| 86 | + |
| 87 | + const result = await upload(pathname, new Blob([file], { type: 'image/png' }), { |
| 88 | + access: 'public', |
| 89 | + clientPayload: 'media', |
| 90 | + contentType: 'image/png', |
| 91 | + handleUploadUrl, |
| 92 | + }) |
| 93 | + |
| 94 | + expect(result.url).toBeDefined() |
| 95 | + expect(result.url).toContain(pathname) |
| 96 | + |
| 97 | + const { blobs } = await list() |
| 98 | + const uploaded = blobs.find((b) => b.pathname === pathname) |
| 99 | + expect(uploaded).toBeDefined() |
| 100 | + }) |
| 101 | + |
| 102 | + it("should reject upload when 'x-disallow-access' header is set", async () => { |
| 103 | + const file = readFileSync(path.resolve(dirname, '../uploads/image.png')) |
| 104 | + |
| 105 | + await expect( |
| 106 | + upload('image.png', new Blob([file], { type: 'image/png' }), { |
| 107 | + access: 'public', |
| 108 | + clientPayload: 'media', |
| 109 | + handleUploadUrl, |
| 110 | + headers: { 'x-disallow-access': 'true' }, |
| 111 | + }), |
| 112 | + ).rejects.toThrow() |
| 113 | + }) |
| 114 | + |
| 115 | + it('should reject upload when no collection slug is provided', async () => { |
| 116 | + const file = readFileSync(path.resolve(dirname, '../uploads/image.png')) |
| 117 | + |
| 118 | + await expect( |
| 119 | + upload('image.png', new Blob([file], { type: 'image/png' }), { |
| 120 | + access: 'public', |
| 121 | + handleUploadUrl, |
| 122 | + }), |
| 123 | + ).rejects.toThrow() |
| 124 | + }) |
| 125 | + |
| 126 | + it('should upload a file with prefix via client upload flow', async () => { |
| 127 | + const file = readFileSync(path.resolve(dirname, '../uploads/image.png')) |
| 128 | + const pathname = `${prefix}/image.png` |
| 129 | + |
| 130 | + const result = await upload(pathname, new Blob([file], { type: 'image/png' }), { |
| 131 | + access: 'public', |
| 132 | + clientPayload: 'media-with-prefix', |
| 133 | + contentType: 'image/png', |
| 134 | + handleUploadUrl, |
| 135 | + }) |
| 136 | + |
| 137 | + expect(result.url).toBeDefined() |
| 138 | + expect(result.url).toContain(prefix) |
| 139 | + expect(result.url).toContain('image.png') |
| 140 | + |
| 141 | + const { blobs } = await list() |
| 142 | + const uploaded = blobs.find((b) => b.pathname === pathname) |
| 143 | + expect(uploaded).toBeDefined() |
| 144 | + }) |
| 145 | +}) |
0 commit comments