diff --git a/methods/product/get_images.js b/methods/product/get_images.js index 5339b3c..9fa8aaa 100644 --- a/methods/product/get_images.js +++ b/methods/product/get_images.js @@ -3,11 +3,30 @@ const { isLegacyApi } = require('../../src/utils'); const { loadConfig, isConfigured } = require('../../src/module-config'); const { PERMISSIONS, requirePermission } = require('../../src/register-permissions'); +const MAX_PRODUCT_IDS = 100; +const IMAGE_CONCURRENCY = 5; + +async function mapWithConcurrency(values, concurrency, worker) { + let nextIndex = 0; + const runners = Array.from({ length: Math.min(concurrency, values.length) }, async () => { + while (nextIndex < values.length) { + const index = nextIndex++; + await worker(values[index]); + } + }); + await Promise.all(runners); +} + module.exports = async function(params, ctx) { requirePermission(PERMISSIONS.ORDERS_VIEW, ctx); - const productIds = Array.isArray(params?.productIds) ? params.productIds : []; + const productIds = [...new Set( + (Array.isArray(params?.productIds) ? params.productIds : []) + .map(value => String(value || '').trim()) + .filter(Boolean) + )]; if (productIds.length === 0) return {}; + if (productIds.length > MAX_PRODUCT_IDS) throw new Error('productIds exceeds the 100 item limit'); const config = await loadConfig(); if (!isConfigured(config)) return {}; @@ -15,12 +34,14 @@ module.exports = async function(params, ctx) { const api = new PrestashopApi(config.apiUrl, config.apiKey, isLegacyApi(config.versionCode)); const result = {}; - for (const productId of productIds) { + await mapWithConcurrency(productIds, IMAGE_CONCURRENCY, async productId => { try { const url = await api.getProductCoverImageUrl(productId); if (url) result[String(productId)] = url; } catch (_) { /* product not found or no image */ } - } + }); return result; }; + +module.exports._private = { mapWithConcurrency, MAX_PRODUCT_IDS, IMAGE_CONCURRENCY }; diff --git a/src/orders-api.js b/src/orders-api.js index b97be68..d4303ed 100644 --- a/src/orders-api.js +++ b/src/orders-api.js @@ -89,8 +89,8 @@ function buildSearchParams(params = {}) { } const searchParams = { - page: parseInt(cleanParams.page, 10) || 1, - perPage: parseInt(cleanParams.perPage, 10) || 20, + page: Math.min(1_000_000, Math.max(1, parseInt(cleanParams.page, 10) || 1)), + perPage: Math.min(100, Math.max(1, parseInt(cleanParams.perPage, 10) || 20)), source: { moduleCode: MODULE_CODE, _moduleCodes: MODULE_CODES @@ -110,7 +110,7 @@ function buildSearchParams(params = {}) { } if (cleanParams.query && isUsableParam(cleanParams.query)) { - searchParams.query = String(cleanParams.query).trim(); + searchParams.query = String(cleanParams.query).trim().slice(0, 200); } const passthroughKeys = [ diff --git a/test/product-images.test.js b/test/product-images.test.js new file mode 100644 index 0000000..5ed2336 --- /dev/null +++ b/test/product-images.test.js @@ -0,0 +1,52 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const Module = require('node:module'); +const path = require('node:path'); + +function loadMethod({ onFetch }) { + const methodPath = path.resolve(__dirname, '../methods/product/get_images.js'); + const originalLoad = Module._load; + Module._load = function(request, parent, isMain) { + if (request === '../../src/PrestaApi') { + return class PrestashopApi { + async getProductCoverImageUrl(id) { return onFetch(id); } + }; + } + if (request === '../../src/utils') return { isLegacyApi: () => false }; + if (request === '../../src/module-config') { + return { loadConfig: async () => ({ apiUrl: 'https://shop.test', apiKey: 'key' }), isConfigured: () => true }; + } + if (request === '../../src/register-permissions') { + return { PERMISSIONS: { ORDERS_VIEW: 'orders.view' }, requirePermission() {} }; + } + return originalLoad.call(this, request, parent, isMain); + }; + delete require.cache[methodPath]; + try { + return require(methodPath); + } finally { + Module._load = originalLoad; + } +} + +test('product image lookup deduplicates IDs and bounds external concurrency', async () => { + let active = 0; + let maxActive = 0; + const calls = []; + const method = loadMethod({ + onFetch: async id => { + calls.push(id); + active += 1; + maxActive = Math.max(maxActive, active); + await new Promise(resolve => setImmediate(resolve)); + active -= 1; + return `https://img.test/${id}.jpg`; + } + }); + + const result = await method({ productIds: ['1', '1', '2', '3', '4', '5', '6'] }, {}); + + assert.deepEqual(calls.sort(), ['1', '2', '3', '4', '5', '6']); + assert.equal(maxActive, 5); + assert.equal(result['6'], 'https://img.test/6.jpg'); +});