-
Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(files): index generated docs without compiling and fix the docx sandbox bundle #7386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { readFileSync } from 'node:fs' | ||
| import { describe, expect, it } from 'vitest' | ||
| import { evaluateSandboxBundle } from '@/lib/execution/sandbox/bundles/verify' | ||
| import type { SandboxBundleName } from '@/lib/execution/sandbox/types' | ||
|
|
||
| function loadCheckedInBundle(name: SandboxBundleName): Record<string, unknown> { | ||
| const source = readFileSync(new URL(`./${name}.cjs`, import.meta.url), 'utf-8') | ||
| return evaluateSandboxBundle(source, name) as Record<string, unknown> | ||
| } | ||
|
|
||
| /** | ||
| * The checked-in bundles are what Trigger.dev workers run verbatim, so this is | ||
| * the only place a bundle that throws while being evaluated is caught before a | ||
| * deploy. Each case asserts the surface the matching sandbox task's bootstrap | ||
| * and finalize scripts reach for. | ||
| */ | ||
| describe('sandbox bundles', () => { | ||
| it('docx evaluates in a bare context and exposes the docx-generate surface', () => { | ||
| const docx = loadCheckedInBundle('docx') | ||
| expect(typeof docx.Document).toBe('function') | ||
| expect(typeof docx.Packer).toBe('function') | ||
| expect(typeof docx.ImageRun).toBe('function') | ||
| expect(typeof docx.Paragraph).toBe('function') | ||
| }) | ||
|
|
||
| it('pdf-lib evaluates in a bare context and exposes the pdf-generate surface', () => { | ||
| const pdfLib = loadCheckedInBundle('pdf-lib') | ||
| expect(typeof pdfLib.PDFDocument).toBe('function') | ||
| expect(typeof pdfLib.rgb).toBe('function') | ||
| expect(typeof pdfLib.StandardFonts).toBe('object') | ||
| }) | ||
|
|
||
| it('pptxgenjs evaluates in a bare context and exposes its constructor', () => { | ||
| const source = readFileSync(new URL('./pptxgenjs.cjs', import.meta.url), 'utf-8') | ||
| expect(typeof evaluateSandboxBundle(source, 'pptxgenjs')).toBe('function') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import vm from 'node:vm' | ||
| import type { SandboxBundleName } from '@/lib/execution/sandbox/types' | ||
|
|
||
| /** | ||
| * Evaluates a built sandbox bundle the way the isolated-vm worker will: as a | ||
| * classic script in a context that has timers, `console`, and the text codecs | ||
| * but no `require`, `process`, or `Buffer` of its own. Returns the export the | ||
| * bundle registered on `globalThis.__bundles`, or throws with the bundle's own | ||
| * error, so a bundle that references a helper the bundler never emitted fails | ||
| * at build time and in the test suite instead of on the first document | ||
| * generated in production. | ||
| */ | ||
| export function evaluateSandboxBundle(source: string, name: SandboxBundleName): unknown { | ||
| const context: Record<string, unknown> = { | ||
| setTimeout, | ||
| clearTimeout, | ||
| setInterval, | ||
| clearInterval, | ||
| queueMicrotask, | ||
| console, | ||
| TextEncoder, | ||
| TextDecoder, | ||
| } | ||
| context.globalThis = context | ||
| vm.createContext(context) | ||
| vm.runInContext(source, context, { filename: `sandbox/${name}.cjs` }) | ||
|
|
||
| const bundles = context.__bundles | ||
| const bundle = | ||
| typeof bundles === 'object' && bundles !== null | ||
| ? (bundles as Record<string, unknown>)[name] | ||
| : undefined | ||
| if (bundle === undefined || bundle === null) { | ||
| throw new Error( | ||
| `Sandbox bundle "${name}" evaluated without registering globalThis.__bundles["${name}"]` | ||
| ) | ||
| } | ||
| return bundle | ||
| } |
114 changes: 114 additions & 0 deletions
114
apps/sim/lib/uploads/contexts/workspace/workspace-file-manager-download.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const { mockDownloadFile } = vi.hoisted(() => ({ | ||
| mockDownloadFile: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/billing/storage', () => ({ | ||
| decrementStorageUsageForBillingContextInTx: vi.fn(), | ||
| incrementStorageUsageForBillingContextInTx: vi.fn(), | ||
| maybeNotifyStorageLimitForBillingContext: vi.fn(), | ||
| resolveStorageBillingContext: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/uploads', () => ({ | ||
| getServePathPrefix: vi.fn(() => '/api/files/serve/s3/'), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/uploads/core/storage-service', () => ({ | ||
| deleteFile: vi.fn(), | ||
| downloadFile: mockDownloadFile, | ||
| hasCloudStorage: vi.fn(() => false), | ||
| headObject: vi.fn(), | ||
| uploadFile: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/uploads/contexts/workspace/workspace-file-folder-manager', () => ({ | ||
| assertWorkspaceFileFolderTarget: vi.fn(async () => null), | ||
| buildWorkspaceFileFolderPathMap: vi.fn(() => new Map()), | ||
| fileNameExistsInWorkspaceFolder: vi.fn(async () => false), | ||
| findWorkspaceFileFolderIdByPath: vi.fn(), | ||
| getWorkspaceFileFolderPath: vi.fn(), | ||
| listWorkspaceFileFolders: vi.fn(async () => []), | ||
| normalizeWorkspaceFileItemName: vi.fn((name: string) => name), | ||
| resolveWorkspaceFileFolderTarget: vi.fn(async () => null), | ||
| })) | ||
|
|
||
| import { assertKnownSizeWithinLimit, isPayloadSizeLimitError } from '@/lib/core/utils/stream-limits' | ||
| import { | ||
| fetchWorkspaceFileBuffer, | ||
| type WorkspaceFileRecord, | ||
| } from '@/lib/uploads/contexts/workspace/workspace-file-manager' | ||
|
|
||
| const FILE: WorkspaceFileRecord = { | ||
| id: 'file-1', | ||
| workspaceId: 'workspace-1', | ||
| name: 'notes.txt', | ||
| key: 'workspace/workspace-1/notes.txt', | ||
| path: '/api/files/serve/workspace/workspace-1/notes.txt', | ||
| size: 5, | ||
| type: 'text/plain', | ||
| uploadedBy: 'user-1', | ||
| uploadedAt: new Date('2026-09-01T00:00:00.000Z'), | ||
| updatedAt: new Date('2026-09-01T00:00:00.000Z'), | ||
| } | ||
|
|
||
| function sizeLimitError(): unknown { | ||
| try { | ||
| assertKnownSizeWithinLimit(2, 1, 'test') | ||
| } catch (error) { | ||
| return error | ||
| } | ||
| throw new Error('assertKnownSizeWithinLimit did not throw') | ||
| } | ||
|
|
||
| describe('fetchWorkspaceFileBuffer', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| it('forwards the byte ceiling and the cancellation signal to storage', async () => { | ||
| const bytes = Buffer.from('hello') | ||
| mockDownloadFile.mockResolvedValue(bytes) | ||
| const signal = new AbortController().signal | ||
|
|
||
| await expect(fetchWorkspaceFileBuffer(FILE, { maxBytes: 10, signal })).resolves.toBe(bytes) | ||
| expect(mockDownloadFile).toHaveBeenCalledWith({ | ||
| key: FILE.key, | ||
| context: 'workspace', | ||
| maxBytes: 10, | ||
| signal, | ||
| }) | ||
| }) | ||
|
|
||
| it('surfaces a cancelled read as the abort rather than a download failure', async () => { | ||
| const controller = new AbortController() | ||
| mockDownloadFile.mockImplementation(async () => { | ||
| controller.abort() | ||
| throw new Error('read interrupted') | ||
| }) | ||
|
|
||
| await expect( | ||
| fetchWorkspaceFileBuffer(FILE, { maxBytes: 10, signal: controller.signal }) | ||
| ).rejects.toMatchObject({ name: 'AbortError' }) | ||
| }) | ||
|
|
||
| it('rethrows a byte-ceiling breach unwrapped', async () => { | ||
| mockDownloadFile.mockRejectedValue(sizeLimitError()) | ||
|
|
||
| await expect(fetchWorkspaceFileBuffer(FILE, { maxBytes: 10 })).rejects.toSatisfy( | ||
| isPayloadSizeLimitError | ||
| ) | ||
| }) | ||
|
|
||
| it('wraps other transport failures', async () => { | ||
| mockDownloadFile.mockRejectedValue(new Error('socket hang up')) | ||
|
|
||
| await expect(fetchWorkspaceFileBuffer(FILE, { maxBytes: 10 })).rejects.toThrow( | ||
| 'Failed to download file: socket hang up' | ||
| ) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.