|
| 1 | +import fs from 'node:fs' |
| 2 | +import os from 'node:os' |
| 3 | +import path from 'node:path' |
| 4 | + |
| 5 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest' |
| 6 | + |
| 7 | +import type { UploadClient, UploadFileArgs } from '../../src/utils/upload-types.js' |
| 8 | +import { uploadFolder } from '../../src/utils/uploader.js' |
| 9 | + |
| 10 | +/** |
| 11 | + * The manifest's `fallback` is what a gateway serves for a path the manifest |
| 12 | + * does not list. Without it an `arweave/paths` manifest 404s every route of a |
| 13 | + * single-page app that is not a real file — the root loads, `/settings` does |
| 14 | + * not — so these pin the shape as well as the presence. |
| 15 | + */ |
| 16 | + |
| 17 | +interface ArweaveManifest { |
| 18 | + fallback?: { id: string } |
| 19 | + index?: { path: string } |
| 20 | + manifest: string |
| 21 | + paths: Record<string, { id: string }> |
| 22 | + version: string |
| 23 | +} |
| 24 | + |
| 25 | +let folder: string |
| 26 | + |
| 27 | +/** Deterministic ids so a manifest entry can be traced back to its file. */ |
| 28 | +function idFor(index: number): string { |
| 29 | + return `tx${String(index).padStart(41, '0')}` |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Records every upload and exposes the manifest, which is the last file sent |
| 34 | + * and the only one tagged as a manifest. |
| 35 | + */ |
| 36 | +function stubClient(): { |
| 37 | + client: UploadClient |
| 38 | + manifest: () => ArweaveManifest |
| 39 | + uploadCount: () => number |
| 40 | +} { |
| 41 | + let counter = 0 |
| 42 | + let manifestJson: string | undefined |
| 43 | + |
| 44 | + const client: UploadClient = { |
| 45 | + async uploadFile(args: UploadFileArgs) { |
| 46 | + const isManifest = args.dataItemOpts?.tags?.some( |
| 47 | + (t) => t.name === 'Content-Type' && t.value === 'application/x.arweave-manifest+json', |
| 48 | + ) |
| 49 | + |
| 50 | + if (isManifest && args.fileStreamFactory) { |
| 51 | + const stream = args.fileStreamFactory() as AsyncIterable<Buffer> |
| 52 | + const chunks: Buffer[] = [] |
| 53 | + for await (const chunk of stream) chunks.push(Buffer.from(chunk)) |
| 54 | + manifestJson = Buffer.concat(chunks).toString('utf8') |
| 55 | + } |
| 56 | + |
| 57 | + counter += 1 |
| 58 | + return { id: idFor(counter) } |
| 59 | + }, |
| 60 | + } |
| 61 | + |
| 62 | + return { |
| 63 | + client, |
| 64 | + manifest() { |
| 65 | + if (!manifestJson) throw new Error('no manifest was uploaded') |
| 66 | + return JSON.parse(manifestJson) as ArweaveManifest |
| 67 | + }, |
| 68 | + uploadCount: () => counter, |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +function write(name: string, body: string): void { |
| 73 | + const full = path.join(folder, name) |
| 74 | + fs.mkdirSync(path.dirname(full), { recursive: true }) |
| 75 | + fs.writeFileSync(full, body) |
| 76 | +} |
| 77 | + |
| 78 | +beforeEach(() => { |
| 79 | + folder = fs.mkdtempSync(path.join(os.tmpdir(), 'ario-deploy-fallback-')) |
| 80 | +}) |
| 81 | + |
| 82 | +afterEach(() => { |
| 83 | + fs.rmSync(folder, { force: true, recursive: true }) |
| 84 | +}) |
| 85 | + |
| 86 | +describe('uploadFolder manifest fallback', () => { |
| 87 | + it('uses 404.html as the fallback when the build emits one', async () => { |
| 88 | + write('index.html', '<html>index</html>') |
| 89 | + write('404.html', '<html>fallback</html>') |
| 90 | + write('assets/app.js', 'console.log(1)') |
| 91 | + |
| 92 | + const { client, manifest } = stubClient() |
| 93 | + await uploadFolder(client, folder) |
| 94 | + const m = manifest() |
| 95 | + |
| 96 | + expect(m.fallback).toBeDefined() |
| 97 | + expect(m.fallback?.id).toBe(m.paths['404.html'].id) |
| 98 | + }) |
| 99 | + |
| 100 | + it('carries an id, not a path — the v0.2.0 spec differs from `index`', async () => { |
| 101 | + write('index.html', '<html>index</html>') |
| 102 | + write('404.html', '<html>fallback</html>') |
| 103 | + |
| 104 | + const { client, manifest } = stubClient() |
| 105 | + await uploadFolder(client, folder) |
| 106 | + const m = manifest() |
| 107 | + |
| 108 | + // `index` takes { path }; `fallback` takes { id }. Getting this wrong |
| 109 | + // produces a manifest a gateway silently ignores. |
| 110 | + expect(m.index).toEqual({ path: 'index.html' }) |
| 111 | + expect(Object.keys(m.fallback ?? {})).toEqual(['id']) |
| 112 | + }) |
| 113 | + |
| 114 | + it('omits fallback entirely when there is no 404.html and no flag', async () => { |
| 115 | + write('index.html', '<html>index</html>') |
| 116 | + write('assets/app.js', 'console.log(1)') |
| 117 | + |
| 118 | + const { client, manifest } = stubClient() |
| 119 | + await uploadFolder(client, folder) |
| 120 | + |
| 121 | + expect(manifest().fallback).toBeUndefined() |
| 122 | + }) |
| 123 | + |
| 124 | + it('honours an explicit fallbackFile over 404.html', async () => { |
| 125 | + write('index.html', '<html>index</html>') |
| 126 | + write('404.html', '<html>fallback</html>') |
| 127 | + |
| 128 | + const { client, manifest } = stubClient() |
| 129 | + await uploadFolder(client, folder, { fallbackFile: 'index.html' }) |
| 130 | + const m = manifest() |
| 131 | + |
| 132 | + expect(m.fallback?.id).toBe(m.paths['index.html'].id) |
| 133 | + expect(m.fallback?.id).not.toBe(m.paths['404.html'].id) |
| 134 | + }) |
| 135 | + |
| 136 | + it('lets a single-page app opt in without inventing a 404 file', async () => { |
| 137 | + write('index.html', '<html>index</html>') |
| 138 | + |
| 139 | + const { client, manifest } = stubClient() |
| 140 | + await uploadFolder(client, folder, { fallbackFile: 'index.html' }) |
| 141 | + |
| 142 | + expect(manifest().fallback?.id).toBe(manifest().paths['index.html'].id) |
| 143 | + }) |
| 144 | + |
| 145 | + it('rejects a missing fallback before uploading, so a typo costs nothing', async () => { |
| 146 | + write('index.html', '<html>index</html>') |
| 147 | + write('assets/app.js', 'console.log(1)') |
| 148 | + |
| 149 | + const { client, uploadCount } = stubClient() |
| 150 | + |
| 151 | + // Every file is paid for on upload. Validating after the fact would bill |
| 152 | + // the whole deploy for a mistyped flag and then throw. |
| 153 | + await expect(uploadFolder(client, folder, { fallbackFile: 'missing.html' })).rejects.toThrow() |
| 154 | + expect(uploadCount()).toBe(0) |
| 155 | + }) |
| 156 | + |
| 157 | + it('fails loudly when the named fallback file is not in the folder', async () => { |
| 158 | + write('index.html', '<html>index</html>') |
| 159 | + |
| 160 | + const { client } = stubClient() |
| 161 | + |
| 162 | + // Silently skipping would ship a manifest whose deep links 404, which is |
| 163 | + // exactly the failure this option exists to prevent. |
| 164 | + await expect(uploadFolder(client, folder, { fallbackFile: 'missing.html' })).rejects.toThrow( |
| 165 | + /Fallback file not found in folder: missing\.html/, |
| 166 | + ) |
| 167 | + }) |
| 168 | +}) |
0 commit comments