@crawlee/memory-storage 3.0.0-alpha.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/LICENSE.md +201 -0
  2. package/README.md +214 -0
  3. package/body-parser.d.ts +13 -0
  4. package/body-parser.d.ts.map +1 -0
  5. package/body-parser.js +65 -0
  6. package/body-parser.js.map +1 -0
  7. package/consts.d.ts +17 -0
  8. package/consts.d.ts.map +1 -0
  9. package/consts.js +22 -0
  10. package/consts.js.map +1 -0
  11. package/index.d.ts +2 -0
  12. package/index.d.ts.map +1 -0
  13. package/index.js +5 -0
  14. package/index.js.map +1 -0
  15. package/index.mjs +4 -0
  16. package/memory-storage.d.ts +29 -0
  17. package/memory-storage.d.ts.map +1 -0
  18. package/memory-storage.js +102 -0
  19. package/memory-storage.js.map +1 -0
  20. package/package.json +60 -0
  21. package/resource-clients/common/base-client.d.ts +8 -0
  22. package/resource-clients/common/base-client.d.ts.map +1 -0
  23. package/resource-clients/common/base-client.js +22 -0
  24. package/resource-clients/common/base-client.js.map +1 -0
  25. package/resource-clients/dataset-collection.d.ts +14 -0
  26. package/resource-clients/dataset-collection.d.ts.map +1 -0
  27. package/resource-clients/dataset-collection.js +60 -0
  28. package/resource-clients/dataset-collection.js.map +1 -0
  29. package/resource-clients/dataset.d.ts +42 -0
  30. package/resource-clients/dataset.d.ts.map +1 -0
  31. package/resource-clients/dataset.js +235 -0
  32. package/resource-clients/dataset.js.map +1 -0
  33. package/resource-clients/key-value-store-collection.d.ts +14 -0
  34. package/resource-clients/key-value-store-collection.d.ts.map +1 -0
  35. package/resource-clients/key-value-store-collection.js +60 -0
  36. package/resource-clients/key-value-store-collection.js.map +1 -0
  37. package/resource-clients/key-value-store.d.ts +36 -0
  38. package/resource-clients/key-value-store.d.ts.map +1 -0
  39. package/resource-clients/key-value-store.js +300 -0
  40. package/resource-clients/key-value-store.js.map +1 -0
  41. package/resource-clients/request-queue-collection.d.ts +14 -0
  42. package/resource-clients/request-queue-collection.d.ts.map +1 -0
  43. package/resource-clients/request-queue-collection.js +60 -0
  44. package/resource-clients/request-queue-collection.js.map +1 -0
  45. package/resource-clients/request-queue.d.ts +48 -0
  46. package/resource-clients/request-queue.d.ts.map +1 -0
  47. package/resource-clients/request-queue.js +353 -0
  48. package/resource-clients/request-queue.js.map +1 -0
  49. package/tsconfig.build.tsbuildinfo +1 -0
  50. package/utils.d.ts +44 -0
  51. package/utils.d.ts.map +1 -0
  52. package/utils.js +59 -0
  53. package/utils.js.map +1 -0
  54. package/workers/file-storage-worker-emulator.d.ts +5 -0
  55. package/workers/file-storage-worker-emulator.d.ts.map +1 -0
  56. package/workers/file-storage-worker-emulator.js +12 -0
  57. package/workers/file-storage-worker-emulator.js.map +1 -0
  58. package/workers/file-storage-worker.d.ts +2 -0
  59. package/workers/file-storage-worker.d.ts.map +1 -0
  60. package/workers/file-storage-worker.js +15 -0
  61. package/workers/file-storage-worker.js.map +1 -0
  62. package/workers/instance.d.ts +4 -0
  63. package/workers/instance.d.ts.map +1 -0
  64. package/workers/instance.js +35 -0
  65. package/workers/instance.js.map +1 -0
  66. package/workers/worker-utils.d.ts +9 -0
  67. package/workers/worker-utils.d.ts.map +1 -0
  68. package/workers/worker-utils.js +79 -0
  69. package/workers/worker-utils.js.map +1 -0
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FileStorageWorkerEmulator = void 0;
4
+ const worker_utils_1 = require("./worker-utils");
5
+ class FileStorageWorkerEmulator {
6
+ postMessage(value) {
7
+ void (0, worker_utils_1.handleMessage)(value);
8
+ }
9
+ terminate() { }
10
+ }
11
+ exports.FileStorageWorkerEmulator = FileStorageWorkerEmulator;
12
+ //# sourceMappingURL=file-storage-worker-emulator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-storage-worker-emulator.js","sourceRoot":"","sources":["../../src/workers/file-storage-worker-emulator.ts"],"names":[],"mappings":";;;AAAA,iDAA+C;AAE/C,MAAa,yBAAyB;IAClC,WAAW,CAAC,KAAU;QAClB,KAAK,IAAA,4BAAa,EAAC,KAAK,CAAC,CAAC;IAC9B,CAAC;IAED,SAAS,KAAU,CAAC;CACvB;AAND,8DAMC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=file-storage-worker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-storage-worker.d.ts","sourceRoot":"","sources":["../../src/workers/file-storage-worker.ts"],"names":[],"mappings":""}
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const node_worker_threads_1 = require("node:worker_threads");
4
+ const worker_utils_1 = require("./worker-utils");
5
+ if (node_worker_threads_1.isMainThread || !node_worker_threads_1.parentPort) {
6
+ throw new Error('This file should only be run in a worker thread!');
7
+ }
8
+ // Keep worker alive
9
+ setInterval(() => {
10
+ node_worker_threads_1.parentPort.postMessage('ping');
11
+ }, 30000);
12
+ node_worker_threads_1.parentPort.on('message', async (message) => {
13
+ await (0, worker_utils_1.handleMessage)(message);
14
+ });
15
+ //# sourceMappingURL=file-storage-worker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-storage-worker.js","sourceRoot":"","sources":["../../src/workers/file-storage-worker.ts"],"names":[],"mappings":";;AAAA,6DAA+D;AAE/D,iDAA+C;AAE/C,IAAI,kCAAY,IAAI,CAAC,gCAAU,EAAE;IAC7B,MAAM,IAAI,KAAK,CAAC,kDAAkD,CAAC,CAAC;CACvE;AAED,oBAAoB;AACpB,WAAW,CAAC,GAAG,EAAE;IACb,gCAAW,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;AACpC,CAAC,EAAE,KAAM,CAAC,CAAC;AAEX,gCAAW,CAAC,EAAE,CAAC,SAAS,EAAE,KAAK,EAAE,OAA8B,EAAE,EAAE;IAC/D,MAAM,IAAA,4BAAa,EAAC,OAAO,CAAC,CAAC;AACjC,CAAC,CAAC,CAAC"}
@@ -0,0 +1,4 @@
1
+ import { WorkerReceivedMessage } from '../utils';
2
+ export declare function sendWorkerMessage(message: WorkerReceivedMessage): void;
3
+ export declare function initWorkerIfNeeded(): void;
4
+ //# sourceMappingURL=instance.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"instance.d.ts","sourceRoot":"","sources":["../../src/workers/instance.ts"],"names":[],"mappings":"AAGA,OAAO,EAAoB,qBAAqB,EAAE,MAAM,UAAU,CAAC;AAKnE,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,qBAAqB,QAE/D;AAED,wBAAgB,kBAAkB,SAsBjC"}
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.initWorkerIfNeeded = exports.sendWorkerMessage = void 0;
4
+ const node_fs_1 = require("node:fs");
5
+ const node_path_1 = require("node:path");
6
+ const node_worker_threads_1 = require("node:worker_threads");
7
+ const utils_1 = require("../utils");
8
+ const file_storage_worker_emulator_1 = require("./file-storage-worker-emulator");
9
+ let workerInstance;
10
+ function sendWorkerMessage(message) {
11
+ workerInstance.postMessage(message);
12
+ }
13
+ exports.sendWorkerMessage = sendWorkerMessage;
14
+ function initWorkerIfNeeded() {
15
+ if (workerInstance) {
16
+ return;
17
+ }
18
+ process.on('exit', () => {
19
+ void workerInstance.terminate();
20
+ });
21
+ const workerPath = (0, node_path_1.resolve)(__dirname, './file-storage-worker.js');
22
+ const exists = (0, node_fs_1.existsSync)(workerPath);
23
+ if (exists) {
24
+ workerInstance = new node_worker_threads_1.Worker(workerPath);
25
+ workerInstance.once('exit', (code) => {
26
+ utils_1.memoryStorageLog.debug(`File storage worker exited with code ${code}`);
27
+ initWorkerIfNeeded();
28
+ });
29
+ }
30
+ else {
31
+ workerInstance = new file_storage_worker_emulator_1.FileStorageWorkerEmulator();
32
+ }
33
+ }
34
+ exports.initWorkerIfNeeded = initWorkerIfNeeded;
35
+ //# sourceMappingURL=instance.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"instance.js","sourceRoot":"","sources":["../../src/workers/instance.ts"],"names":[],"mappings":";;;AAAA,qCAAqC;AACrC,yCAAoC;AACpC,6DAA6C;AAC7C,oCAAmE;AACnE,iFAA2E;AAE3E,IAAI,cAAkD,CAAC;AAEvD,SAAgB,iBAAiB,CAAC,OAA8B;IAC5D,cAAc,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;AACxC,CAAC;AAFD,8CAEC;AAED,SAAgB,kBAAkB;IAC9B,IAAI,cAAc,EAAE;QAChB,OAAO;KACV;IAED,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,GAAG,EAAE;QACpB,KAAK,cAAc,CAAC,SAAS,EAAE,CAAC;IACpC,CAAC,CAAC,CAAC;IAEH,MAAM,UAAU,GAAG,IAAA,mBAAO,EAAC,SAAS,EAAE,0BAA0B,CAAC,CAAC;IAClE,MAAM,MAAM,GAAG,IAAA,oBAAU,EAAC,UAAU,CAAC,CAAC;IAEtC,IAAI,MAAM,EAAE;QACR,cAAc,GAAG,IAAI,4BAAM,CAAC,UAAU,CAAC,CAAC;QAEvC,cAAyB,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE;YAC7C,wBAAgB,CAAC,KAAK,CAAC,wCAAwC,IAAI,EAAE,CAAC,CAAC;YACvE,kBAAkB,EAAE,CAAC;QACzB,CAAC,CAAC,CAAC;KACN;SAAM;QACH,cAAc,GAAG,IAAI,wDAAyB,EAAE,CAAC;KACpD;AACL,CAAC;AAtBD,gDAsBC"}
@@ -0,0 +1,9 @@
1
+ import { WorkerReceivedMessage } from '../utils';
2
+ export interface WorkerDirectoryMap {
3
+ datasetsDirectory: string;
4
+ keyValueStoresDirectory: string;
5
+ requestQueuesDirectory: string;
6
+ }
7
+ export declare type EntityTypeToDirectoryMap = Map<'datasets' | 'keyValueStores' | 'requestQueues', string>;
8
+ export declare function handleMessage(message: WorkerReceivedMessage): Promise<void>;
9
+ //# sourceMappingURL=worker-utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worker-utils.d.ts","sourceRoot":"","sources":["../../src/workers/worker-utils.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,qBAAqB,EAA2D,MAAM,UAAU,CAAC;AAI1G,MAAM,WAAW,kBAAkB;IAC/B,iBAAiB,EAAE,MAAM,CAAC;IAC1B,uBAAuB,EAAE,MAAM,CAAC;IAChC,sBAAsB,EAAE,MAAM,CAAC;CAClC;AAED,oBAAY,wBAAwB,GAAG,GAAG,CAAC,UAAU,GAAG,gBAAgB,GAAG,eAAe,EAAE,MAAM,CAAC,CAAC;AAEpG,wBAAsB,aAAa,CAAC,OAAO,EAAE,qBAAqB,iBAajE"}
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.handleMessage = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const log_1 = tslib_1.__importDefault(require("@apify/log"));
6
+ const fs_extra_1 = require("fs-extra");
7
+ const promises_1 = require("node:fs/promises");
8
+ const node_path_1 = require("node:path");
9
+ const workerLog = log_1.default.child({ prefix: 'MemoryStorageWorker' });
10
+ async function handleMessage(message) {
11
+ switch (message.action) {
12
+ case 'update-metadata':
13
+ await updateMetadata(message);
14
+ break;
15
+ case 'update-entries':
16
+ await updateItems(message);
17
+ break;
18
+ default:
19
+ // @ts-expect-error We're keeping this to make eslint happy + in the event we add a new action without adding checks for it
20
+ // we should be aware of them
21
+ workerLog.warning(`Unknown worker message action ${message.action}`);
22
+ }
23
+ }
24
+ exports.handleMessage = handleMessage;
25
+ async function updateMetadata(message) {
26
+ workerLog.info(`Updating metadata for ${message.entityType} with id ${message.id}`);
27
+ // Ensure the directory for the entity exists
28
+ const dir = message.entityDirectory;
29
+ await (0, fs_extra_1.ensureDir)(dir);
30
+ // Write the metadata to the file
31
+ const filePath = (0, node_path_1.resolve)(dir, '__metadata__.json');
32
+ await (0, promises_1.writeFile)(filePath, JSON.stringify(message.data, null, '\t'));
33
+ }
34
+ async function updateItems(message) {
35
+ workerLog.info(`Updating entries for ${message.entityType} with id ${message.id}`);
36
+ // Ensure the directory for the entity exists
37
+ const dir = message.entityDirectory;
38
+ await (0, fs_extra_1.ensureDir)(dir);
39
+ switch (message.entityType) {
40
+ case 'requestQueues': {
41
+ // Write the entries to the file
42
+ const filePath = (0, node_path_1.resolve)(dir, 'entries.json');
43
+ await (0, promises_1.writeFile)(filePath, JSON.stringify(message.data, null, '\t'));
44
+ break;
45
+ }
46
+ case 'datasets': {
47
+ // Save all the new items to the disk
48
+ for (const [idx, data] of message.data) {
49
+ await (0, promises_1.writeFile)((0, node_path_1.resolve)(dir, `${idx}.json`), JSON.stringify(data, null, '\t'));
50
+ }
51
+ break;
52
+ }
53
+ case 'keyValueStores': {
54
+ // Create files for the record
55
+ const { action, record } = message.data;
56
+ const itemPath = (0, node_path_1.resolve)(dir, `${record.key}.${record.extension}`);
57
+ switch (action) {
58
+ case 'delete':
59
+ await (0, promises_1.rm)(itemPath, { force: true, recursive: true });
60
+ break;
61
+ case 'set': {
62
+ await (0, promises_1.rm)(itemPath, { force: true, recursive: true });
63
+ const metadataPath = (0, node_path_1.resolve)(dir, `${record.key}.__metadata__.json`);
64
+ await (0, promises_1.writeFile)(metadataPath, JSON.stringify({
65
+ key: record.key,
66
+ contentType: record.contentType ?? 'unknown/no content type',
67
+ extension: record.extension,
68
+ }, null, '\t'));
69
+ await (0, promises_1.writeFile)(itemPath, record.value);
70
+ break;
71
+ }
72
+ default:
73
+ }
74
+ break;
75
+ }
76
+ default:
77
+ }
78
+ }
79
+ //# sourceMappingURL=worker-utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worker-utils.js","sourceRoot":"","sources":["../../src/workers/worker-utils.ts"],"names":[],"mappings":";;;;AAAA,6DAAoC;AACpC,uCAAqC;AACrC,+CAAiD;AACjD,yCAAoC;AAGpC,MAAM,SAAS,GAAG,aAAU,CAAC,KAAK,CAAC,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