Skip to content
Myr

fix(ssg): preserve binary files as-is (#2275)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(ssg): preserve binary files as-is * denoify * test(ssg): cleanup * test(ssg): merge tests for the right content and paths

authored by berlysia · committed by GitHub · parent a32482d · Browse files · 3 files+44 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+44 added−6 removed

  • tests:+42 added−2 removed
  • other:+2 added−4 removed

3 files+44 added−6 removed

modified:deno_dist/helper/ssg/index.ts+1 added-2 removed
1import { replaceUrlParam } from '../../client/utils.ts'2import type { Context } from '../../context.ts'3import type { Hono } from '../../hono.ts'4import type { Env, MiddlewareHandler, Schema } from '../../types.ts'5import { bufferToString } from '../../utils/buffer.ts'5import { getExtension } from '../../utils/mime.ts'6import { joinPaths, dirname, filterStaticGenerateRoutes } from './utils.ts'78const SSG_CONTEXT = 'HONO_SSG_CONTEXT'
191 unmodified lines
200 await fsModule.mkdir(dirPath, { recursive: true })201 if (typeof content === 'string') {202 await fsModule.writeFile(filePath, content)203 } else if (content instanceof ArrayBuffer) {205 await fsModule.writeFile(filePath, bufferToString(content))204 await fsModule.writeFile(filePath, new Uint8Array(content))205 }206 files.push(filePath)207 }208
86 unmodified lines
modified:src/helper/ssg/index.test.tsx+42 added-2 removed
283 unmodified lines
284285describe('saveContentToFiles function', () => {286 let fsMock: FileSystemModule287 let htmlMap: Map<string, { content: string | ArrayBuffer; mimeType: string }>288 // tar.gz, testdir/test.txt289 const gzFileBuffer = Buffer.from(290 'H4sIAAAAAAAAA+3SQQrCMBSE4aw9RU6gSc3LO0/FLgqukgj29qZgsQgqCEHE/9vMIoEMTMqQy3FMO9OQq1RkTq/i1rkwPkiMUXWvnXG+U/XGSstSi3MufbLWHIZ0mvLYP7v37vxHldv+c27LpbR4Yx44hvBi/3DfX3zdP0j9Eta1KPPoz/ef+mnz7Q4AAAAAAAAAAAAAAAAAPnMFqt1/BQAoAAA=',291 'base64'292 )293 const gzFileArrayBuffer = gzFileBuffer.buffer.slice(294 gzFileBuffer.byteOffset,295 gzFileBuffer.byteLength + gzFileBuffer.byteOffset296 )297 // PNG, red dot (1x1)298 const pngFileBuffer = Buffer.from(299 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGP4z8AAAAMBAQDJ/pLvAAAAAElFTkSuQmCCAAAALw',300 'base64'301 )302 const pngFileArrayBuffer = pngFileBuffer.buffer.slice(303 pngFileBuffer.byteOffset,304 pngFileBuffer.byteLength + pngFileBuffer.byteOffset305 )306307 beforeEach(() => {308 fsMock = {309 writeFile: vi.fn(() => Promise.resolve()),
4 unmodified lines
314 ['/index.html', { content: 'Home Page2', mimeType: 'text/html' }],315 ['/about', { content: 'About Page', mimeType: 'text/html' }],316 ['/about/', { content: 'About Page', mimeType: 'text/html' }],317 ['/bravo/index.html', { content: 'About Page', mimeType: 'text/html' }],300 ['/bravo/index.tar.gz', { content: 'About Page', mimeType: 'application/gzip' }],318 ['/bravo/release-4.0.0', { content: 'Release 4.0.0', mimeType: 'text/html' }],319 ['/bravo/2024.02.18-sweet-memories', { content: 'Sweet Memories', mimeType: 'text/html' }],320 ['/bravo/deep.dive.to.html', { content: 'Deep Dive To HTML', mimeType: 'text/html' }],321 ['/bravo/alert.js', { content: 'alert("evil content")', mimeType: 'text/html' }],322 [323 '/bravo/index.tar.gz',324 {325 content: gzFileArrayBuffer,326 mimeType: 'application/gzip',327 },328 ],329 [330 '/bravo/dot.png',331 {332 content: pngFileArrayBuffer,333 mimeType: 'image/png',334 },335 ],336 ['/bravo.text/index.html', { content: 'About Page', mimeType: 'text/html' }],337 ['/bravo.text/', { content: 'Bravo Page', mimeType: 'text/html' }],338 ])339 })
5 unmodified lines
345 expect(fsMock.writeFile).toHaveBeenCalledWith('static/index.html', 'Home Page2')346 expect(fsMock.writeFile).toHaveBeenCalledWith('static/about.html', 'About Page')347 expect(fsMock.writeFile).toHaveBeenCalledWith('static/about/index.html', 'About Page')348 expect(fsMock.writeFile).toHaveBeenCalledWith('static/bravo/index.html', 'About Page')318 expect(fsMock.writeFile).toHaveBeenCalledWith('static/bravo/index.tar.gz', 'About Page')349 expect(fsMock.writeFile).toHaveBeenCalledWith(350 'static/bravo/release-4.0.0.html',351 'Release 4.0.0'352 )
10 unmodified lines
363 'alert("evil content")'364 )365 expect(fsMock.writeFile).toHaveBeenCalledWith('static/bravo.text/index.html', 'About Page')366 expect(fsMock.writeFile).toHaveBeenCalledWith('static/bravo.text/index.html', 'Bravo Page')367368 // binary files369 expect(fsMock.writeFile).toHaveBeenCalledWith(370 'static/bravo/index.tar.gz',371 new Uint8Array(gzFileArrayBuffer)372 )373 expect(fsMock.writeFile).toHaveBeenCalledWith(374 'static/bravo/dot.png',375 new Uint8Array(pngFileArrayBuffer)376 )377 })378379 it('should correctly create directories if they do not exist', async () => {380 await saveContentToFiles(htmlMap, fsMock, './static')
90 unmodified lines
modified:src/helper/ssg/index.ts+1 added-2 removed
1import { replaceUrlParam } from '../../client/utils'2import type { Context } from '../../context'3import type { Hono } from '../../hono'4import type { Env, MiddlewareHandler, Schema } from '../../types'5import { bufferToString } from '../../utils/buffer'5import { getExtension } from '../../utils/mime'6import { joinPaths, dirname, filterStaticGenerateRoutes } from './utils'78const SSG_CONTEXT = 'HONO_SSG_CONTEXT'
191 unmodified lines
200 await fsModule.mkdir(dirPath, { recursive: true })201 if (typeof content === 'string') {202 await fsModule.writeFile(filePath, content)203 } else if (content instanceof ArrayBuffer) {205 await fsModule.writeFile(filePath, bufferToString(content))204 await fsModule.writeFile(filePath, new Uint8Array(content))205 }206 files.push(filePath)207 }208
86 unmodified lines