From 0e6465e0b7b499c13ec32bd0765941371b633535 Mon Sep 17 00:00:00 2001 From: Soumyajit Samanta Date: Sun, 24 Aug 2025 14:53:35 +0530 Subject: [PATCH] Removed circular dependency via unnecessary method as build tools still complain --- src/gifcodec.js | 19 ++++--------------- src/gifutil.js | 11 ++++++----- test/test_decoding.js | 20 ++++++++++---------- test/test_properties.js | 24 ++++++++++++------------ 4 files changed, 32 insertions(+), 42 deletions(-) diff --git a/src/gifcodec.js b/src/gifcodec.js index 1663542..d1330d7 100644 --- a/src/gifcodec.js +++ b/src/gifcodec.js @@ -2,18 +2,7 @@ const Omggif = require('omggif'); const { Gif, GifError } = require('./gif'); - -// allow circular dependency with GifUtil -function GifUtil() { - const data = require('./gifutil'); - - GifUtil = function () { - return data; - }; - - return data; -} - +const GifUtil = require('./gifutil'); const { GifFrame } = require('./gifframe'); const PER_GIF_OVERHEAD = 200; // these are guesses at upper limits @@ -102,7 +91,7 @@ class GifCodec if (frames === null || frames.length === 0) { throw new GifError("there are no frames"); } - const dims = GifUtil().getMaxDimensions(frames); + const dims = GifUtil.getMaxDimensions(frames); spec = Object.assign({}, spec); // don't munge caller's spec spec.width = dims.maxWidth; @@ -179,10 +168,10 @@ class GifCodec _encodeGif(frames, spec) { let colorInfo; if (spec.colorScope === Gif.LocalColorsOnly) { - colorInfo = GifUtil().getColorInfo(frames, 0); + colorInfo = GifUtil.getColorInfo(frames, 0); } else { - colorInfo = GifUtil().getColorInfo(frames, 256); + colorInfo = GifUtil.getColorInfo(frames, 256); if (!colorInfo.colors) { // if global palette impossible if (spec.colorScope === Gif.GlobalColorsOnly) { throw new GifError( diff --git a/src/gifutil.js b/src/gifutil.js index 300ef92..afce570 100644 --- a/src/gifutil.js +++ b/src/gifutil.js @@ -8,12 +8,9 @@ const ImageQ = require('image-q'); const BitmapImage = require('./bitmapimage'); const { GifFrame } = require('./gifframe'); const { GifError } = require('./gif'); -const { GifCodec } = require('./gifcodec'); const INVALID_SUFFIXES = ['.jpg', '.jpeg', '.png', '.bmp']; -const defaultCodec = new GifCodec(); - /** * cloneFrames() clones provided frames. It's a utility method for cloning an entire array of frames at once. * @@ -212,7 +209,9 @@ exports.quantizeWu = function (imageOrImages, maxColorIndexes, significantBits, */ exports.read = function (source, decoder) { - decoder = decoder || defaultCodec; + if(decoder == null){ + throw "Decoder cannot be null. Provide decoder" + } if (Buffer.isBuffer(source)) { return decoder.decodeGif(source); } @@ -253,7 +252,9 @@ exports.shareAsJimp = function (jimp, bitmapImageToShare) { */ exports.write = function (path, frames, spec, encoder) { - encoder = encoder || defaultCodec; + if(encoder == null){ + throw "Encoder cannot be null. Provide encoder" + } const matches = path.match(/\.[a-zA-Z]+$/); // prevent accidents if (matches !== null && INVALID_SUFFIXES.includes(matches[0].toLowerCase())) diff --git a/test/test_decoding.js b/test/test_decoding.js index 9fba8c4..da8ef6c 100644 --- a/test/test_decoding.js +++ b/test/test_decoding.js @@ -11,7 +11,7 @@ describe("single frame decoding", () => { const name = 'singleFrameMonoOpaque'; const bitmap = Tools.getBitmap(name); - return GifUtil.read(Tools.getGifPath(name)) + return GifUtil.read(Tools.getGifPath(name), new GifCodec()) .then(gif => { _compareGifToSeries(gif, [bitmap], { @@ -25,7 +25,7 @@ describe("single frame decoding", () => { const name = 'singleFrameMultiOpaque'; const bitmap = Tools.getBitmap(name); - return GifUtil.read(Tools.getGifPath(name)) + return GifUtil.read(Tools.getGifPath(name), new GifCodec()) .then(gif => { _compareGifToSeries(gif, [bitmap], { @@ -39,7 +39,7 @@ describe("single frame decoding", () => { const name = 'singleFrameNoColorTrans'; const bitmap = Tools.getBitmap(name, 0); - return GifUtil.read(Tools.getGifPath(name)) + return GifUtil.read(Tools.getGifPath(name), new GifCodec()) .then(gif => { _compareGifToSeries(gif, [bitmap], { @@ -53,7 +53,7 @@ describe("single frame decoding", () => { const name = 'singleFrameMonoTrans'; const bitmap = Tools.getBitmap(name, 0); - return GifUtil.read(Tools.getGifPath(name)) + return GifUtil.read(Tools.getGifPath(name), new GifCodec()) .then(gif => { _compareGifToSeries(gif, [bitmap], { @@ -67,7 +67,7 @@ describe("single frame decoding", () => { const name = 'singleFrameMultiTrans'; const bitmap = Tools.getBitmap(name, 0); - return GifUtil.read(Tools.getGifPath(name)) + return GifUtil.read(Tools.getGifPath(name), new GifCodec()) .then(gif => { _compareGifToSeries(gif, [bitmap], { @@ -100,7 +100,7 @@ describe("multiframe decoding", () => { const name = 'twoFrameMultiOpaque'; const series = Tools.getSeries(name); - return GifUtil.read(Tools.getGifPath(name)) + return GifUtil.read(Tools.getGifPath(name), new GifCodec()) .then(gif => { _compareGifToSeries(gif, series, { @@ -115,7 +115,7 @@ describe("multiframe decoding", () => { const name = 'threeFrameMonoTrans'; const series = Tools.getSeries(name); - return GifUtil.read(Tools.getGifPath(name)) + return GifUtil.read(Tools.getGifPath(name), new GifCodec()) .then(gif => { _compareGifToSeries(gif, series, { @@ -128,7 +128,7 @@ describe("multiframe decoding", () => { it("reads a large multiframe file w/out transparency", () => { - return GifUtil.read(Tools.getGifPath('nburling-public')) + return GifUtil.read(Tools.getGifPath('nburling-public'), new GifCodec()) .then(gif => { assert.strictEqual(gif.width, 238); @@ -154,7 +154,7 @@ describe("multiframe decoding", () => { it("reads a large multiframe file w/ offsets, w/out transparency", () => { - return GifUtil.read(Tools.getGifPath('rnaples-offsets-public')) + return GifUtil.read(Tools.getGifPath('rnaples-offsets-public'), new GifCodec()) .then(gif => { assert.strictEqual(gif.width, 480); @@ -205,7 +205,7 @@ describe("partial frame decoding", () => { it("renders partial frames properly onto full frames", () => { let actualBitmapImage; - return GifUtil.read(Tools.getGifPath('rnaples-offsets-public')) + return GifUtil.read(Tools.getGifPath('rnaples-offsets-public'), new GifCodec()) .then(actualGif => { actualBitmapImage = actualGif.frames[10]; return Tools.loadBitmapImage(Tools.getImagePath('rnaples-frame-10.png')); diff --git a/test/test_properties.js b/test/test_properties.js index 7080c28..254d932 100644 --- a/test/test_properties.js +++ b/test/test_properties.js @@ -2,7 +2,7 @@ const assert = require('chai').assert; const Tools = require('./lib/tools'); -const { Gif, GifFrame, GifCodec, GifError, GifUtil } = require('../src/index'); +const { GifFrame, GifCodec, GifUtil } = require('../src/index'); const defaultCodec = new GifCodec(); @@ -58,7 +58,7 @@ describe("Gif loop count", () => { it("decodes an infinite loop", () => { - return GifUtil.read(Tools.getGifPath('countConstantDelay0')) + return GifUtil.read(Tools.getGifPath('countConstantDelay0'), defaultCodec) .then(gif => { assert.strictEqual(gif.loops, 0); @@ -67,7 +67,7 @@ describe("Gif loop count", () => { it("decodes a single-pass loop", () => { - return GifUtil.read(Tools.getGifPath('countConstantDelay1')) + return GifUtil.read(Tools.getGifPath('countConstantDelay1'), defaultCodec) .then(gif => { assert.strictEqual(gif.loops, 1); @@ -76,7 +76,7 @@ describe("Gif loop count", () => { it("decodes a three-pass loop", () => { - return GifUtil.read(Tools.getGifPath('countConstantDelay3')) + return GifUtil.read(Tools.getGifPath('countConstantDelay3'), defaultCodec) .then(gif => { assert.strictEqual(gif.loops, 3); @@ -103,7 +103,7 @@ describe("Gif transparency", () => { it("indicates no transparency", () => { - return GifUtil.read(Tools.getGifPath('twoFrameMultiOpaque')) + return GifUtil.read(Tools.getGifPath('twoFrameMultiOpaque'), defaultCodec) .then(readGif => { assert.strictEqual(readGif.usesTransparency, false); @@ -117,7 +117,7 @@ describe("Gif transparency", () => { it("indicates transparency", () => { - return GifUtil.read(Tools.getGifPath('threeFrameMonoTrans')) + return GifUtil.read(Tools.getGifPath('threeFrameMonoTrans'), defaultCodec) .then(readGif => { assert.strictEqual(readGif.usesTransparency, true); @@ -134,7 +134,7 @@ describe("GifFrame x/y-offsets", () => { it("accommodates/encodes offsets", () => { - return GifUtil.read(Tools.getGifPath('count5x7')) + return GifUtil.read(Tools.getGifPath('count5x7'), defaultCodec) .then(readGif => { const frames = readGif.frames; @@ -174,7 +174,7 @@ describe("GifFrame delay", () => { it("confirms a constant frame-delay GIF", () => { - return GifUtil.read(Tools.getGifPath('countConstantDelay0')) + return GifUtil.read(Tools.getGifPath('countConstantDelay0'), defaultCodec) .then(readGif => { readGif.frames.forEach(frame => { @@ -186,7 +186,7 @@ describe("GifFrame delay", () => { it("confirms a variable frame-delay GIF", () => { - return GifUtil.read(Tools.getGifPath('countIncreasingDelay')) + return GifUtil.read(Tools.getGifPath('countIncreasingDelay'), defaultCodec) .then(readGif => { const frames = readGif.frames; @@ -198,7 +198,7 @@ describe("GifFrame delay", () => { it("encodes varying frame delays", () => { - return GifUtil.read(Tools.getGifPath('countConstantDelay0')) + return GifUtil.read(Tools.getGifPath('countConstantDelay0'), defaultCodec) .then(readGif => { const frames = readGif.frames; @@ -225,7 +225,7 @@ describe("GifFrame disposal method", () => { it("encodes/decodes disposal", () => { - return GifUtil.read(Tools.getGifPath('countConstantDelay0')) + return GifUtil.read(Tools.getGifPath('countConstantDelay0'), defaultCodec) .then(readGif => { const frames = readGif.frames; @@ -255,7 +255,7 @@ describe("GifFrame disposal method", () => { }); function _verifyEncodesLoopCount(sourceFilename, loopCount) { - return GifUtil.read(Tools.getGifPath(sourceFilename)) + return GifUtil.read(Tools.getGifPath(sourceFilename), defaultCodec) .then(readGif => { return defaultCodec.encodeGif(readGif.frames, { loops: loopCount });