From a6172e9a9b0012c2224796fc079ff135920ef33b Mon Sep 17 00:00:00 2001 From: vanpho93 Date: Fri, 17 Feb 2023 16:07:41 +0700 Subject: [PATCH 1/9] fix: sample image data not showing Signed-off-by: vanpho93 --- .changeset/curly-clocks-notice.md | 6 ++++++ packages/api-plugin-sample-data/src/loaders/loadImages.js | 5 +++-- .../src/node/getFileDownloadHandler/requestRange.js | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 .changeset/curly-clocks-notice.md diff --git a/.changeset/curly-clocks-notice.md b/.changeset/curly-clocks-notice.md new file mode 100644 index 00000000000..3b7aa76098b --- /dev/null +++ b/.changeset/curly-clocks-notice.md @@ -0,0 +1,6 @@ +--- +"@reactioncommerce/api-plugin-sample-data": patch +"@reactioncommerce/file-collections": patch +--- + +fix: sample image data not showing diff --git a/packages/api-plugin-sample-data/src/loaders/loadImages.js b/packages/api-plugin-sample-data/src/loaders/loadImages.js index 662dfbe5da8..0d58b880d08 100644 --- a/packages/api-plugin-sample-data/src/loaders/loadImages.js +++ b/packages/api-plugin-sample-data/src/loaders/loadImages.js @@ -2,6 +2,7 @@ /* eslint-disable no-await-in-loop */ import fs from "fs"; import path from "path"; +import { fileURLToPath } from "url"; import { Readable } from "stream"; import pkg from "@reactioncommerce/file-collections"; @@ -106,8 +107,8 @@ export default async function loadImages(context, shopId) { const topProdIds = []; const fileType = "image/jpeg"; - const folderPath = "./custom-packages/api-plugin-sample-data/src/images/"; - + const currentDir = path.dirname(fileURLToPath(import.meta.url)); + const folderPath = path.join(currentDir, "../images/"); let fileList = []; try { fileList = fs.readdirSync(folderPath); diff --git a/packages/file-collections/src/node/getFileDownloadHandler/requestRange.js b/packages/file-collections/src/node/getFileDownloadHandler/requestRange.js index 7dbb0482a30..820ddd851c6 100644 --- a/packages/file-collections/src/node/getFileDownloadHandler/requestRange.js +++ b/packages/file-collections/src/node/getFileDownloadHandler/requestRange.js @@ -22,7 +22,7 @@ export default function requestRange(headers, fileSize) { } const defaultRange = { - end: fileSize - 1, + end: fileSize, len: fileSize, partial: false, size: fileSize, From ecbd5c016b492f6f9648198e367ede122e64ab14 Mon Sep 17 00:00:00 2001 From: vanpho93 Date: Tue, 28 Feb 2023 15:59:14 +0700 Subject: [PATCH 2/9] fix: request range case Signed-off-by: vanpho93 --- .../src/node/getFileDownloadHandler/requestRange.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/file-collections/src/node/getFileDownloadHandler/requestRange.js b/packages/file-collections/src/node/getFileDownloadHandler/requestRange.js index 820ddd851c6..76b7367c64d 100644 --- a/packages/file-collections/src/node/getFileDownloadHandler/requestRange.js +++ b/packages/file-collections/src/node/getFileDownloadHandler/requestRange.js @@ -57,7 +57,7 @@ export default function requestRange(headers, fileSize) { // Fix invalid (non-numeric) ranges if (String(startByte) !== start) startByte = 0; - if ((String(endByte) !== end) || endByte === 0) endByte = fileSize - 1; + if ((String(endByte) !== end) || endByte === 0) endByte = fileSize; if (start >= end) { return { @@ -66,7 +66,7 @@ export default function requestRange(headers, fileSize) { }; } - const partSize = (endByte - startByte) + 1; + const partSize = endByte - startByte; return { end, len: partSize, From 6a506d87d64bcb12f10683f86a12bdb4fc0af8ff Mon Sep 17 00:00:00 2001 From: vanpho93 Date: Mon, 6 Mar 2023 09:19:25 +0700 Subject: [PATCH 3/9] feat: add unit test for requestRange file Signed-off-by: vanpho93 --- packages/file-collections/package.json | 3 + .../requestRange.test.js | 83 +++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 packages/file-collections/src/node/getFileDownloadHandler/requestRange.test.js diff --git a/packages/file-collections/package.json b/packages/file-collections/package.json index 50afc760040..2774c457ae1 100644 --- a/packages/file-collections/package.json +++ b/packages/file-collections/package.json @@ -64,6 +64,9 @@ "main": "./dist/node/index.js", "scripts": { "build": "rm -rf dist/** && babel src --out-dir dist --ignore \"**/*.test.js\"", + "test": "jest", + "test:watch": "jest --watch", + "test:file": "jest --no-cache --watch --coverage=false", "prepublishOnly": "npm run build" }, "dependencies": { diff --git a/packages/file-collections/src/node/getFileDownloadHandler/requestRange.test.js b/packages/file-collections/src/node/getFileDownloadHandler/requestRange.test.js new file mode 100644 index 00000000000..406e0c7f3f6 --- /dev/null +++ b/packages/file-collections/src/node/getFileDownloadHandler/requestRange.test.js @@ -0,0 +1,83 @@ +import requestRange from "./requestRange.js"; + +test("should return default setting when range header is not present", () => { + const headers = {}; + const fileSize = 100; + const result = requestRange(headers, fileSize); + expect(result).toEqual({ + end: fileSize, + len: fileSize, + partial: false, + size: fileSize, + start: 0, + unit: "bytes" + }); +}); + +test("should return correct range when range header is present", () => { + const headers = { range: "bytes=0-999" }; + const fileSize = 1000; + const result = requestRange(headers, fileSize); + expect(result).toEqual({ + end: "999", + len: 999, + partial: true, + size: 1000, + start: "0", + unit: "bytes" + }); +}); + +test("should return the correct range when the range header request first half part of the file", () => { + const headers = { range: "bytes=0-499" }; + const fileSize = 1000; + const result = requestRange(headers, fileSize); + expect(result).toEqual({ + end: "499", + len: 499, + partial: true, + size: 1000, + start: "0", + unit: "bytes" + }); +}); + +test("should return error when range header is present but file size is not", () => { + const headers = { range: "bytes=0-10" }; + const fileSize = null; + const result = requestRange(headers, fileSize); + expect(result).toEqual({ + errorCode: 416, + errorMessage: "Requested Range Not Satisfiable (Unknown File Size)" + }); +}); + +test("should return error when range header is present but invalid", () => { + const headers = { range: "bytes" }; + const fileSize = 100; + const result = requestRange(headers, fileSize); + expect(result).toEqual({ + errorCode: 416, + errorMessage: "Requested Range Unit Not Satisfiable" + }); +}) + +test('should return error when range header is present but unit is not a "bytes"', () => { + const headers = { range: "k_bytes=0-10" }; + const fileSize = 100; + const result = requestRange(headers, fileSize); + expect(result).toEqual({ + errorCode: 416, + errorMessage: "Requested Range Unit Not Satisfiable" + }); +}); + +test("should return error when range header is present but start is greater than end", () => { + const headers = { range: "bytes=10-0" }; + const fileSize = 100; + const result = requestRange(headers, fileSize); + expect(result).toEqual({ + errorCode: 416, + errorMessage: "Requested Range Not Satisfiable" + }); +}); From 7bcdb335a16f4b437ff2276c7877028dde6e46db Mon Sep 17 00:00:00 2001 From: vanpho93 Date: Mon, 6 Mar 2023 09:29:21 +0700 Subject: [PATCH 4/9] fix: lint fail on requestRange file Signed-off-by: vanpho93 --- .../requestRange.test.js | 102 +++++++++--------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/packages/file-collections/src/node/getFileDownloadHandler/requestRange.test.js b/packages/file-collections/src/node/getFileDownloadHandler/requestRange.test.js index 406e0c7f3f6..59e7fdd5dd6 100644 --- a/packages/file-collections/src/node/getFileDownloadHandler/requestRange.test.js +++ b/packages/file-collections/src/node/getFileDownloadHandler/requestRange.test.js @@ -15,69 +15,69 @@ test("should return default setting when range header is not present", () => { }); test("should return correct range when range header is present", () => { - const headers = { range: "bytes=0-999" }; - const fileSize = 1000; - const result = requestRange(headers, fileSize); - expect(result).toEqual({ - end: "999", - len: 999, - partial: true, - size: 1000, - start: "0", - unit: "bytes" - }); + const headers = { range: "bytes=0-999" }; + const fileSize = 1000; + const result = requestRange(headers, fileSize); + expect(result).toEqual({ + end: "999", + len: 999, + partial: true, + size: 1000, + start: "0", + unit: "bytes" + }); }); test("should return the correct range when the range header request first half part of the file", () => { - const headers = { range: "bytes=0-499" }; - const fileSize = 1000; - const result = requestRange(headers, fileSize); - expect(result).toEqual({ - end: "499", - len: 499, - partial: true, - size: 1000, - start: "0", - unit: "bytes" - }); + const headers = { range: "bytes=0-499" }; + const fileSize = 1000; + const result = requestRange(headers, fileSize); + expect(result).toEqual({ + end: "499", + len: 499, + partial: true, + size: 1000, + start: "0", + unit: "bytes" + }); }); test("should return error when range header is present but file size is not", () => { - const headers = { range: "bytes=0-10" }; - const fileSize = null; - const result = requestRange(headers, fileSize); - expect(result).toEqual({ - errorCode: 416, - errorMessage: "Requested Range Not Satisfiable (Unknown File Size)" - }); + const headers = { range: "bytes=0-10" }; + const fileSize = null; + const result = requestRange(headers, fileSize); + expect(result).toEqual({ + errorCode: 416, + errorMessage: "Requested Range Not Satisfiable (Unknown File Size)" + }); }); test("should return error when range header is present but invalid", () => { - const headers = { range: "bytes" }; - const fileSize = 100; - const result = requestRange(headers, fileSize); - expect(result).toEqual({ - errorCode: 416, - errorMessage: "Requested Range Unit Not Satisfiable" - }); -}) + const headers = { range: "bytes" }; + const fileSize = 100; + const result = requestRange(headers, fileSize); + expect(result).toEqual({ + errorCode: 416, + errorMessage: "Requested Range Unit Not Satisfiable" + }); +}); test('should return error when range header is present but unit is not a "bytes"', () => { - const headers = { range: "k_bytes=0-10" }; - const fileSize = 100; - const result = requestRange(headers, fileSize); - expect(result).toEqual({ - errorCode: 416, - errorMessage: "Requested Range Unit Not Satisfiable" - }); + const headers = { range: "k_bytes=0-10" }; + const fileSize = 100; + const result = requestRange(headers, fileSize); + expect(result).toEqual({ + errorCode: 416, + errorMessage: "Requested Range Unit Not Satisfiable" + }); }); test("should return error when range header is present but start is greater than end", () => { - const headers = { range: "bytes=10-0" }; - const fileSize = 100; - const result = requestRange(headers, fileSize); - expect(result).toEqual({ - errorCode: 416, - errorMessage: "Requested Range Not Satisfiable" - }); + const headers = { range: "bytes=10-0" }; + const fileSize = 100; + const result = requestRange(headers, fileSize); + expect(result).toEqual({ + errorCode: 416, + errorMessage: "Requested Range Not Satisfiable" + }); }); From b0d3bbfa331cd0bf92a2b3b9d3fd3677c1c2a631 Mon Sep 17 00:00:00 2001 From: vanpho93 Date: Mon, 6 Mar 2023 10:42:13 +0700 Subject: [PATCH 5/9] feat: update GridFSStore file Signed-off-by: vanpho93 --- .../file-collections-sa-gridfs/src/GridFSStore.js | 2 +- .../node/getFileDownloadHandler/requestRange.js | 6 +++--- .../getFileDownloadHandler/requestRange.test.js | 14 +++++++------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/file-collections-sa-gridfs/src/GridFSStore.js b/packages/file-collections-sa-gridfs/src/GridFSStore.js index 913143d16b5..d5af54dbf88 100644 --- a/packages/file-collections-sa-gridfs/src/GridFSStore.js +++ b/packages/file-collections-sa-gridfs/src/GridFSStore.js @@ -46,7 +46,7 @@ export default class GridFSStore extends StorageAdapter { // Add range if this should be a partial read if (typeof startPos === "number" && typeof endPos === "number") { opts.start = startPos; - opts.end = endPos; + opts.end = endPos + 1; } debug("GridFSStore _getReadStream opts:", opts); diff --git a/packages/file-collections/src/node/getFileDownloadHandler/requestRange.js b/packages/file-collections/src/node/getFileDownloadHandler/requestRange.js index 76b7367c64d..7dbb0482a30 100644 --- a/packages/file-collections/src/node/getFileDownloadHandler/requestRange.js +++ b/packages/file-collections/src/node/getFileDownloadHandler/requestRange.js @@ -22,7 +22,7 @@ export default function requestRange(headers, fileSize) { } const defaultRange = { - end: fileSize, + end: fileSize - 1, len: fileSize, partial: false, size: fileSize, @@ -57,7 +57,7 @@ export default function requestRange(headers, fileSize) { // Fix invalid (non-numeric) ranges if (String(startByte) !== start) startByte = 0; - if ((String(endByte) !== end) || endByte === 0) endByte = fileSize; + if ((String(endByte) !== end) || endByte === 0) endByte = fileSize - 1; if (start >= end) { return { @@ -66,7 +66,7 @@ export default function requestRange(headers, fileSize) { }; } - const partSize = endByte - startByte; + const partSize = (endByte - startByte) + 1; return { end, len: partSize, diff --git a/packages/file-collections/src/node/getFileDownloadHandler/requestRange.test.js b/packages/file-collections/src/node/getFileDownloadHandler/requestRange.test.js index 59e7fdd5dd6..9d476fecb46 100644 --- a/packages/file-collections/src/node/getFileDownloadHandler/requestRange.test.js +++ b/packages/file-collections/src/node/getFileDownloadHandler/requestRange.test.js @@ -19,11 +19,11 @@ test("should return correct range when range header is present", () => { const fileSize = 1000; const result = requestRange(headers, fileSize); expect(result).toEqual({ - end: "999", - len: 999, - partial: true, + end: 999, + len: 1000, + partial: false, size: 1000, - start: "0", + start: 0, unit: "bytes" }); }); @@ -33,11 +33,11 @@ test("should return the correct range when the range header request first half p const fileSize = 1000; const result = requestRange(headers, fileSize); expect(result).toEqual({ - end: "499", - len: 499, + end: 499, + len: 500, partial: true, size: 1000, - start: "0", + start: 0, unit: "bytes" }); }); From 64f438b5f86f8a9c5b1c014b47acc7a2be2670ad Mon Sep 17 00:00:00 2001 From: vanpho93 Date: Mon, 6 Mar 2023 10:46:22 +0700 Subject: [PATCH 6/9] fix: unit test fail on requestRange file Signed-off-by: vanpho93 --- .../src/node/getFileDownloadHandler/requestRange.js | 4 ++-- .../src/node/getFileDownloadHandler/requestRange.test.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/file-collections/src/node/getFileDownloadHandler/requestRange.js b/packages/file-collections/src/node/getFileDownloadHandler/requestRange.js index 7dbb0482a30..dd51589d417 100644 --- a/packages/file-collections/src/node/getFileDownloadHandler/requestRange.js +++ b/packages/file-collections/src/node/getFileDownloadHandler/requestRange.js @@ -68,11 +68,11 @@ export default function requestRange(headers, fileSize) { const partSize = (endByte - startByte) + 1; return { - end, + end: endByte, len: partSize, partial: (partSize < fileSize), size: fileSize, - start, + start: startByte, unit }; } diff --git a/packages/file-collections/src/node/getFileDownloadHandler/requestRange.test.js b/packages/file-collections/src/node/getFileDownloadHandler/requestRange.test.js index 9d476fecb46..a35f1d93307 100644 --- a/packages/file-collections/src/node/getFileDownloadHandler/requestRange.test.js +++ b/packages/file-collections/src/node/getFileDownloadHandler/requestRange.test.js @@ -5,7 +5,7 @@ test("should return default setting when range header is not present", () => { const fileSize = 100; const result = requestRange(headers, fileSize); expect(result).toEqual({ - end: fileSize, + end: fileSize - 1, len: fileSize, partial: false, size: fileSize, From 106eaac4c3649b88cb0c74c6ca51f412a9ada78a Mon Sep 17 00:00:00 2001 From: vanpho93 Date: Mon, 6 Mar 2023 15:06:31 +0700 Subject: [PATCH 7/9] fix: update expect value for requestRange test Signed-off-by: vanpho93 --- .../src/node/getFileDownloadHandler/requestRange.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/file-collections/src/node/getFileDownloadHandler/requestRange.test.js b/packages/file-collections/src/node/getFileDownloadHandler/requestRange.test.js index a35f1d93307..f6412d255d2 100644 --- a/packages/file-collections/src/node/getFileDownloadHandler/requestRange.test.js +++ b/packages/file-collections/src/node/getFileDownloadHandler/requestRange.test.js @@ -5,10 +5,10 @@ test("should return default setting when range header is not present", () => { const fileSize = 100; const result = requestRange(headers, fileSize); expect(result).toEqual({ - end: fileSize - 1, - len: fileSize, + end: 99, + len: 100, partial: false, - size: fileSize, + size: 100, start: 0, unit: "bytes" }); From 74b0cddfe7275fa1cc1f3e4410cd68166e0e77d1 Mon Sep 17 00:00:00 2001 From: vanpho93 Date: Mon, 6 Mar 2023 15:20:52 +0700 Subject: [PATCH 8/9] feat: check content start-end for requestRange Signed-off-by: vanpho93 --- .changeset/curly-clocks-notice.md | 4 ++-- .../src/node/getFileDownloadHandler/requestRange.js | 2 +- .../node/getFileDownloadHandler/requestRange.test.js | 12 +++++++++++- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/.changeset/curly-clocks-notice.md b/.changeset/curly-clocks-notice.md index 3b7aa76098b..c6c4e3586d5 100644 --- a/.changeset/curly-clocks-notice.md +++ b/.changeset/curly-clocks-notice.md @@ -1,6 +1,6 @@ --- -"@reactioncommerce/api-plugin-sample-data": patch -"@reactioncommerce/file-collections": patch +"@reactioncommerce/api-plugin-sample-data": minor +"@reactioncommerce/file-collections": minor --- fix: sample image data not showing diff --git a/packages/file-collections/src/node/getFileDownloadHandler/requestRange.js b/packages/file-collections/src/node/getFileDownloadHandler/requestRange.js index dd51589d417..a5b1de96101 100644 --- a/packages/file-collections/src/node/getFileDownloadHandler/requestRange.js +++ b/packages/file-collections/src/node/getFileDownloadHandler/requestRange.js @@ -59,7 +59,7 @@ export default function requestRange(headers, fileSize) { if (String(startByte) !== start) startByte = 0; if ((String(endByte) !== end) || endByte === 0) endByte = fileSize - 1; - if (start >= end) { + if (startByte >= endByte || endByte >= fileSize) { return { errorCode: 416, errorMessage: "Requested Range Not Satisfiable" diff --git a/packages/file-collections/src/node/getFileDownloadHandler/requestRange.test.js b/packages/file-collections/src/node/getFileDownloadHandler/requestRange.test.js index f6412d255d2..e793d7175af 100644 --- a/packages/file-collections/src/node/getFileDownloadHandler/requestRange.test.js +++ b/packages/file-collections/src/node/getFileDownloadHandler/requestRange.test.js @@ -73,7 +73,17 @@ test('should return error when range header is present but unit is not a "bytes" }); test("should return error when range header is present but start is greater than end", () => { - const headers = { range: "bytes=10-0" }; + const headers = { range: "bytes=10-9" }; + const fileSize = 100; + const result = requestRange(headers, fileSize); + expect(result).toEqual({ + errorCode: 416, + errorMessage: "Requested Range Not Satisfiable" + }); +}); + +test("should return error when range header is present but end is greater than file size", () => { + const headers = { range: "bytes=0-1000" }; const fileSize = 100; const result = requestRange(headers, fileSize); expect(result).toEqual({ From 7f450c8c26ccdbf3a779cb49065d61ee590f09eb Mon Sep 17 00:00:00 2001 From: vanpho93 Date: Mon, 6 Mar 2023 17:34:21 +0700 Subject: [PATCH 9/9] feat: add changeset for file-collections-sa-gridfs plugin Signed-off-by: vanpho93 --- .changeset/curly-clocks-notice.md | 1 + 1 file changed, 1 insertion(+) diff --git a/.changeset/curly-clocks-notice.md b/.changeset/curly-clocks-notice.md index c6c4e3586d5..d64cbcf3839 100644 --- a/.changeset/curly-clocks-notice.md +++ b/.changeset/curly-clocks-notice.md @@ -1,6 +1,7 @@ --- "@reactioncommerce/api-plugin-sample-data": minor "@reactioncommerce/file-collections": minor +"@reactioncommerce/file-collections-sa-gridfs": minor --- fix: sample image data not showing