-
Notifications
You must be signed in to change notification settings - Fork 643
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(MemoryStorage): ignore invalid files for request queues (#2132)
Closes #1985
- Loading branch information
1 parent
86647ce
commit fa58581
Showing
2 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
58 changes: 58 additions & 0 deletions
58
packages/memory-storage/test/request-queue/ignore-non-json-files.test.ts
This file contains 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,58 @@ | ||
import { randomUUID } from 'node:crypto'; | ||
import { rm, writeFile } from 'node:fs/promises'; | ||
import { resolve } from 'node:path'; | ||
|
||
import { MemoryStorage } from '@crawlee/memory-storage'; | ||
import type { InternalRequest } from '@crawlee/memory-storage/src/resource-clients/request-queue'; | ||
import type { RequestSchema } from '@crawlee/types'; | ||
import { ensureDir } from 'fs-extra'; | ||
|
||
describe('when falling back to fs, Request queue should ignore non-JSON files', () => { | ||
const tmpLocation = resolve(__dirname, './tmp/req-queue-ignore-non-json'); | ||
const storage = new MemoryStorage({ | ||
localDataDirectory: tmpLocation, | ||
}); | ||
|
||
beforeAll(async () => { | ||
// Create "default" request queue and give it faulty entries | ||
await ensureDir(resolve(storage.requestQueuesDirectory, 'default')); | ||
await writeFile(resolve(storage.requestQueuesDirectory, 'default/__metadata__.json'), JSON.stringify({ | ||
id: randomUUID(), | ||
name: 'default', | ||
createdAt: new Date(2022, 0, 1), | ||
accessedAt: new Date(2022, 0, 1), | ||
modifiedAt: new Date(2022, 0, 1), | ||
})); | ||
|
||
await writeFile(resolve(storage.requestQueuesDirectory, 'default/123.json'), JSON.stringify({ | ||
id: '123', | ||
orderNo: 1, | ||
url: 'http://example.com', | ||
uniqueKey: 'owo', | ||
method: 'GET', | ||
retryCount: 0, | ||
json: JSON.stringify({ | ||
uniqueKey: 'owo', | ||
url: 'http://example.com', | ||
id: '123', | ||
} satisfies RequestSchema), | ||
} satisfies InternalRequest)); | ||
|
||
await writeFile(resolve(storage.requestQueuesDirectory, 'default/.DS_Store'), 'owo'); | ||
await writeFile(resolve(storage.requestQueuesDirectory, 'default/invalid.txt'), 'owo'); | ||
}); | ||
|
||
afterAll(async () => { | ||
await rm(tmpLocation, { force: true, recursive: true }); | ||
}); | ||
|
||
test('attempting to list "default" request queue should ignore non-JSON files', async () => { | ||
const defaultQueueInfo = await storage.requestQueues().getOrCreate('default'); | ||
const defaultQueue = storage.requestQueue(defaultQueueInfo.id); | ||
|
||
expect(defaultQueueInfo.name).toEqual('default'); | ||
|
||
const requests = await defaultQueue.listHead(); | ||
expect(requests.items).toHaveLength(1); | ||
}); | ||
}); |