From 6e62b33d54b1602a458c00c40fd97539bef55b08 Mon Sep 17 00:00:00 2001 From: abetomo Date: Fri, 17 Dec 2021 09:00:48 +0900 Subject: [PATCH] feat: implement processing in memory --- README.md | 5 --- examples/example.js | 5 --- examples/example.mjs | 5 --- lib/__tests__/server.js | 3 +- lib/__tests__/simply_imitated_sqs.js | 51 +--------------------- lib/server.js | 1 - lib/simply_imitated_sqs.js | 29 +------------ package-lock.json | 63 +++++++++++++++++++++------- package.json | 2 +- 9 files changed, 52 insertions(+), 112 deletions(-) diff --git a/README.md b/README.md index 78a9cf1..6137027 100644 --- a/README.md +++ b/README.md @@ -75,11 +75,6 @@ try { } catch (err) { console.error(err) } - -// !!! SimplyImitatedSQS creates a file to store the queue, so remove it. -if (process.env.LOCAL_TEST === '1') { - sqs.clear() -} ``` ### Starting http server diff --git a/examples/example.js b/examples/example.js index ff6fe6e..c909d97 100644 --- a/examples/example.js +++ b/examples/example.js @@ -53,11 +53,6 @@ Promise.resolve().then(() => { resolve() }) }) -}).then(() => { - if (process.env.LOCAL_TEST === '1') { - // !!! SimplyImitatedSQS creates a file to store the queue, so remove it. - sqs.clear() - } }) /* diff --git a/examples/example.mjs b/examples/example.mjs index bab957e..b845ff2 100644 --- a/examples/example.mjs +++ b/examples/example.mjs @@ -52,8 +52,3 @@ try { } catch (err) { console.error(err) } - -// !!! SimplyImitatedSQS creates a file to store the queue, so remove it. -if (process.env.LOCAL_TEST === '1') { - sqs.clear() -} diff --git a/lib/__tests__/server.js b/lib/__tests__/server.js index 77169b2..c4f10df 100644 --- a/lib/__tests__/server.js +++ b/lib/__tests__/server.js @@ -2,13 +2,12 @@ const SimplyImitatedSQSHttpServer = require('../server') -/* global describe, test, expect, beforeEach, afterEach */ +/* global describe, test, expect, beforeEach */ describe('web server', () => { let server = null beforeEach(() => { server = new SimplyImitatedSQSHttpServer() }) - afterEach(() => server.sqs.clear()) test('server is instanceOf SimplyImitatedSQSHttpServer', () => { expect(server).toBeInstanceOf(SimplyImitatedSQSHttpServer) diff --git a/lib/__tests__/simply_imitated_sqs.js b/lib/__tests__/simply_imitated_sqs.js index bc87ed3..9d02bab 100644 --- a/lib/__tests__/simply_imitated_sqs.js +++ b/lib/__tests__/simply_imitated_sqs.js @@ -1,67 +1,18 @@ 'use strict' -const os = require('os') -const fs = require('fs') -const path = require('path') const SimplyImitatedSQS = require('../simply_imitated_sqs') -/* global describe, test, expect, beforeEach, afterEach */ +/* global describe, test, expect, beforeEach */ describe('simply_imitated_sqs', () => { let sqs = null beforeEach(() => { sqs = new SimplyImitatedSQS() }) - afterEach(() => sqs.clear()) test('sqs is instanceOf SimplyImitatedSQS', () => { expect(sqs).toBeInstanceOf(SimplyImitatedSQS) }) - describe('defaultDbFile()', () => { - test('File path including pid', () => { - expect(sqs.defaultDbFile()) - .toMatch(/SimplyImitatedSQS-\d{1,}\.json/) - }) - }) - - describe('sqs.dbFile variable', () => { - test('File path including pid', () => { - expect(sqs.dbFile) - .toMatch(/SimplyImitatedSQS-\d{1,}\.json/) - }) - - test('Specify file path at instance creation', () => { - const filePath = path.join(os.tmpdir(), 'SimplyImitatedSQS-test.json') - const _sqs = new SimplyImitatedSQS(filePath) - expect(_sqs.dbFile).toBe(filePath) - _sqs.clear() - }) - }) - - describe('reload() and clear()', () => { - test('Remove the file with clear and delete vsq. Recreate with reload', () => { - const dbFile = sqs.dbFile - expect(fs.existsSync(dbFile)).toBeTruthy() - - sqs.clear() - expect(fs.existsSync(dbFile)).toBeFalsy() - expect(sqs.vsq).toBeUndefined() - - sqs.reload() - expect(fs.existsSync(dbFile)).toBeTruthy() - expect(sqs.vsq).toBeDefined() - }) - - test('Exceptions do not occur even if clear() is executed continuously', () => { - const func = () => { - sqs.clear() - sqs.clear() - return true - } - expect(func()).toBeTruthy() - }) - }) - describe('md5_(str)', () => { test('value of md5', () => { expect(sqs.md5_('hoge')).toBe('ea703e7aa1efda0064eaa507d9e8ab7e') diff --git a/lib/server.js b/lib/server.js index 3f07e75..0877113 100644 --- a/lib/server.js +++ b/lib/server.js @@ -152,7 +152,6 @@ class SimplyImitatedSQSHttpServer { close (callback) { if (this.server == null) return - this.sqs.clear() if (callback == null) callback = () => process.exit() return this.server.close(callback) } diff --git a/lib/simply_imitated_sqs.js b/lib/simply_imitated_sqs.js index 72a76ad..c15db89 100644 --- a/lib/simply_imitated_sqs.js +++ b/lib/simply_imitated_sqs.js @@ -1,38 +1,13 @@ 'use strict' -const os = require('os') -const fs = require('fs') -const path = require('path') const crypto = require('crypto') const EventEmitter = require('events').EventEmitter const VerySimpleQueueLikeSQS = require('@abetomo/vsq').SQS class SimplyImitatedSQS { - constructor (dbFile) { + constructor () { this.vsq = new VerySimpleQueueLikeSQS() - this.dbFile = (() => { - if (dbFile != null) return dbFile - return this.defaultDbFile() - })() - this.vsq.load(this.dbFile) - } - - defaultDbFile () { - const getRandomInt = () => Math.floor(Math.random() * 10000000) - return path.join( - os.tmpdir(), - `SimplyImitatedSQS-${getRandomInt()}.json` - ) - } - - reload () { - this.vsq = new VerySimpleQueueLikeSQS() - this.vsq.load(this.dbFile) - } - - clear () { - delete this.vsq - if (fs.existsSync(this.dbFile)) fs.unlinkSync(this.dbFile) + this.vsq.load(':memory:') } md5_ (str) { diff --git a/package-lock.json b/package-lock.json index 40ba917..dd77cad 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "2.0.0", "license": "MIT", "dependencies": { - "@abetomo/vsq": "^4.0.0" + "@abetomo/vsq": "^4.1.1" }, "bin": { "sis-local-sqs-start": "bin/webserver.js" @@ -23,11 +23,12 @@ } }, "node_modules/@abetomo/vsq": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/@abetomo/vsq/-/vsq-4.0.0.tgz", - "integrity": "sha512-vj4jhCX+m3MZoslchBhOD0dNNVXfLFl35PyANn7pzLS1NEzlcvIv2xGOwd2C1iViyK5qbiPCuLRlZZEFFN0Ygw==", + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/@abetomo/vsq/-/vsq-4.1.1.tgz", + "integrity": "sha512-JcxIesPnz4ZaB4wYOLHTJIcwZEqeJ9nkzkx5CnsXr8Tt7wFsFqEM65ZXaqvdaqnWRFKaKalTiVqKBRHA62HEhA==", "dependencies": { - "commander": "^6.2.1", + "commander": "^8.2.0", + "memfs": "^3.4.0", "uuid": "^8.3.2" }, "bin": { @@ -1604,11 +1605,11 @@ } }, "node_modules/commander": { - "version": "6.2.1", - "resolved": "https://registry.npmjs.org/commander/-/commander-6.2.1.tgz", - "integrity": "sha512-U7VdrJFnJgo4xjrHpTzu0yrHPGImdsmD95ZlgYSEajAn2JKzDhDTPG9kBTefmObL2w/ngeZnilk+OV9CG3d7UA==", + "version": "8.3.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-8.3.0.tgz", + "integrity": "sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww==", "engines": { - "node": ">= 6" + "node": ">= 12" } }, "node_modules/concat-map": { @@ -2783,6 +2784,11 @@ "node": ">= 6" } }, + "node_modules/fs-monkey": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/fs-monkey/-/fs-monkey-1.0.3.tgz", + "integrity": "sha512-cybjIfiiE+pTWicSCLFHSrXZ6EilF30oh91FDP9S2B051prEa7QWfrVTQm10/dDpswBDXZugPa1Ogu8Yh+HV0Q==" + }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -4367,6 +4373,17 @@ "tmpl": "1.0.x" } }, + "node_modules/memfs": { + "version": "3.4.0", + "resolved": "https://registry.npmjs.org/memfs/-/memfs-3.4.0.tgz", + "integrity": "sha512-o/RfP0J1d03YwsAxyHxAYs2kyJp55AFkMazlFAZFR2I2IXkxiUTXRabJ6RmNNCQ83LAD2jy52Khj0m3OffpNdA==", + "dependencies": { + "fs-monkey": "1.0.3" + }, + "engines": { + "node": ">= 4.0.0" + } + }, "node_modules/merge-stream": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz", @@ -6201,11 +6218,12 @@ }, "dependencies": { "@abetomo/vsq": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/@abetomo/vsq/-/vsq-4.0.0.tgz", - "integrity": "sha512-vj4jhCX+m3MZoslchBhOD0dNNVXfLFl35PyANn7pzLS1NEzlcvIv2xGOwd2C1iViyK5qbiPCuLRlZZEFFN0Ygw==", + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/@abetomo/vsq/-/vsq-4.1.1.tgz", + "integrity": "sha512-JcxIesPnz4ZaB4wYOLHTJIcwZEqeJ9nkzkx5CnsXr8Tt7wFsFqEM65ZXaqvdaqnWRFKaKalTiVqKBRHA62HEhA==", "requires": { - "commander": "^6.2.1", + "commander": "^8.2.0", + "memfs": "^3.4.0", "uuid": "^8.3.2" } }, @@ -7436,9 +7454,9 @@ } }, "commander": { - "version": "6.2.1", - "resolved": "https://registry.npmjs.org/commander/-/commander-6.2.1.tgz", - "integrity": "sha512-U7VdrJFnJgo4xjrHpTzu0yrHPGImdsmD95ZlgYSEajAn2JKzDhDTPG9kBTefmObL2w/ngeZnilk+OV9CG3d7UA==" + "version": "8.3.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-8.3.0.tgz", + "integrity": "sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww==" }, "concat-map": { "version": "0.0.1", @@ -8319,6 +8337,11 @@ "mime-types": "^2.1.12" } }, + "fs-monkey": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/fs-monkey/-/fs-monkey-1.0.3.tgz", + "integrity": "sha512-cybjIfiiE+pTWicSCLFHSrXZ6EilF30oh91FDP9S2B051prEa7QWfrVTQm10/dDpswBDXZugPa1Ogu8Yh+HV0Q==" + }, "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -9502,6 +9525,14 @@ "tmpl": "1.0.x" } }, + "memfs": { + "version": "3.4.0", + "resolved": "https://registry.npmjs.org/memfs/-/memfs-3.4.0.tgz", + "integrity": "sha512-o/RfP0J1d03YwsAxyHxAYs2kyJp55AFkMazlFAZFR2I2IXkxiUTXRabJ6RmNNCQ83LAD2jy52Khj0m3OffpNdA==", + "requires": { + "fs-monkey": "1.0.3" + } + }, "merge-stream": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz", diff --git a/package.json b/package.json index 411b2fa..a76e541 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ }, "homepage": "https://github.com/abetomo/Simply-imitated-SQS-for-testing#readme", "dependencies": { - "@abetomo/vsq": "^4.0.0" + "@abetomo/vsq": "^4.1.1" }, "files": [ "LICENSE",