From a2f7ee044b04c913ad789ea69c0f3193b79a764f Mon Sep 17 00:00:00 2001 From: bcheidemann Date: Sun, 21 Feb 2021 10:18:57 +0000 Subject: [PATCH 01/13] chore: show console logs when running tests --- karma.conf.js | 1 + 1 file changed, 1 insertion(+) diff --git a/karma.conf.js b/karma.conf.js index 231678d..3944bc9 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -6,6 +6,7 @@ module.exports = function(config) { frameworks: ['mocha', 'chai'], reporters: ['mocha', 'summary'], client: { + captureConsole: true, mocha: { ui: 'bdd', timeout: 5000, From 7ec1fed51f9bc6f9f3e19e212d0ca50bcaf77008 Mon Sep 17 00:00:00 2001 From: bcheidemann Date: Sun, 21 Feb 2021 10:21:03 +0000 Subject: [PATCH 02/13] chore: use regenerator runtime in tests for async/await --- karma.conf.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/karma.conf.js b/karma.conf.js index 3944bc9..aa7d677 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -2,7 +2,10 @@ module.exports = function(config) { config.set({ singleRun: true, basePath: '', - files: ['tests/dist/index.js'], + files: [ + 'node_modules/regenerator-runtime/runtime.js', + 'tests/dist/index.js' + ], frameworks: ['mocha', 'chai'], reporters: ['mocha', 'summary'], client: { From 0f5fe64ae8e5e1271a71a939c42d13a49a6c44c2 Mon Sep 17 00:00:00 2001 From: bcheidemann Date: Sun, 21 Feb 2021 10:25:43 +0000 Subject: [PATCH 03/13] chore: npm install regenerator-runtime --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 21d612b..d5584da 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ "minimatch": "^3.0.4" }, "devDependencies": { + "regenerator-runtime": "^0.13.7", "chai": "^4.2.0", "chai-datetime": "^1.5.0", "eslint": "^6.8.0", From ce809c4ac93d01a360d35f41d34963ed2bf3f67e Mon Sep 17 00:00:00 2001 From: bcheidemann Date: Sun, 7 Mar 2021 14:58:09 +0000 Subject: [PATCH 04/13] feat: add buffer shim --- shims/buffer.js | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 shims/buffer.js diff --git a/shims/buffer.js b/shims/buffer.js new file mode 100644 index 0000000..746e7eb --- /dev/null +++ b/shims/buffer.js @@ -0,0 +1,7 @@ +const { Buffer } = require('filer'); + +module.exports = { + __esModule: true, + default: Buffer, + Buffer, +}; From d264113e0ddd4ebdb4c8816673ec920cf580e2b0 Mon Sep 17 00:00:00 2001 From: bcheidemann Date: Sun, 7 Mar 2021 14:58:35 +0000 Subject: [PATCH 05/13] docs: add documentation for use of buffer shim to README.md --- README.md | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/README.md b/README.md index 5965163..6a64714 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,51 @@ shim will ensure that calls to path are appropriately handled by filer. import path from 'path'; ``` +It may be necessary in certain cases to shim the node.js [Buffer object](http://nodejs.org/api/buffer.html). This can be impoerted as follows: + +```javascript +import path from 'path'; +``` + +As such it can be shimmed in much the same way as path and fs: + +```javascript +// webpack.config.js +module.exports = { + resolve: { + alias: { + 'buffer': 'filer/shims/buffer.js', + } + } +} +``` + +However, the Buffer object is globally defined in a node environment and many third party libraries will not import (or require) it. +Using the resolve alias alone will be ineffective in such cases. Instead we must expand our webpack config to use webpacks +[provide plugin](https://webpack.js.org/plugins/provide-plugin/), which will automatically load the module without the need for an import +or require. This can be implemented as follows: + +```javascript +// webpack.config.js +const webpack = require('webpack'); + +module.exports = { + resolve: { + alias: { + 'buffer': 'filer/shims/buffer.js', + } + }, + plugins: [ + new webpack.ProvidePlugin({ + Buffer: 'buffer', + }), + ] +} +``` + +This will, in effect, make the Buffer object shim globally available in the same way that the node.js +[Buffer object](http://nodejs.org/api/buffer.html) is in a node environment. + ### Getting Started Filer is as close to the node.js [fs module](http://nodejs.org/api/fs.html) as possible, From aa152955b50f9a8450f4a458b634f24c57ff79fe Mon Sep 17 00:00:00 2001 From: bcheidemann Date: Sun, 7 Mar 2021 15:01:05 +0000 Subject: [PATCH 06/13] refactor: use correct path import in shim --- shims/path.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shims/path.js b/shims/path.js index ffd0088..46dd292 100644 --- a/shims/path.js +++ b/shims/path.js @@ -1,4 +1,4 @@ -const path = require("./path.js"); +const { path } = require('../src/index'); module.exports = { __esModule: true, From da65e06e3c841b9d9bec400dd17a75d74feeb774 Mon Sep 17 00:00:00 2001 From: bcheidemann Date: Sun, 7 Mar 2021 15:01:51 +0000 Subject: [PATCH 07/13] test: run fs shim spec test --- tests/index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/index.js b/tests/index.js index beb517f..a0f6e3b 100644 --- a/tests/index.js +++ b/tests/index.js @@ -3,6 +3,9 @@ * get them running by default. */ +// Shims +require('./spec/shims/fs.spec'); + // Filer require('./spec/filer.spec'); require('./spec/filer.buffer.spec.js'); From d6b29226aa37559fceeadb6641ec045fd195893b Mon Sep 17 00:00:00 2001 From: bcheidemann Date: Sun, 7 Mar 2021 15:02:29 +0000 Subject: [PATCH 08/13] refactor: return result of fn when wrapping in utils.shimIndexDB --- tests/lib/test-utils.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/lib/test-utils.js b/tests/lib/test-utils.js index 2c58bb3..fd4461a 100644 --- a/tests/lib/test-utils.js +++ b/tests/lib/test-utils.js @@ -52,11 +52,13 @@ function shimIndexedDB(fn) { global.indexedDB = require('fake-indexeddb'); } - fn(); + var result = fn(); if(addShim) { delete global.indexedDB; } + + return result; } function setup(callback) { From 8b57d299f4c0e276f59c2f7644119cb58e147b91 Mon Sep 17 00:00:00 2001 From: bcheidemann Date: Sun, 7 Mar 2021 15:05:18 +0000 Subject: [PATCH 09/13] test: add test for fs shim --- tests/spec/shims/fs.spec.js | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 tests/spec/shims/fs.spec.js diff --git a/tests/spec/shims/fs.spec.js b/tests/spec/shims/fs.spec.js new file mode 100644 index 0000000..d8fb5a4 --- /dev/null +++ b/tests/spec/shims/fs.spec.js @@ -0,0 +1,44 @@ +'use strict'; +const expect = require('chai').expect; +const utils = require('../../lib/test-utils'); +const fs = utils.shimIndexedDB(() => require('../../../shims/fs').default); + +describe.only('fs shim', () => { + it('should be defined', () => { + expect(fs).to.not.be.undefined; + }); + + it('should be an object', () => { + expect(typeof fs).to.equal('object'); + }); + + it('should return a function when accessing fs.writeFile', () => { + expect(typeof fs.writeFile).to.equal('function'); + }); + + it('should call callback when calling fs.writeFile', (done) => { + fs.writeFile('/test.txt', 'test', function(err) { + if(err) throw err; + + done(); + }); + }); + + it('should return an object when accessing fs.promises', () => { + expect(typeof fs.promises).to.equal('object'); + }); + + it('should return a function when accessing fs.promises.writeFile', () => { + expect(typeof fs.promises.writeFile).to.equal('function'); + }); + + it('should return a promise which resolves when calling fs.promises.writeFile', (done) => { + const writeFilePromise = fs.promises.writeFile('/test2.txt', ''); + expect(writeFilePromise instanceof Promise).to.equal(true); + writeFilePromise.then(() => { + done(); + }).catch((err) => { + done(err); + }); + }); +}); From 6027376efaa3f6a8bed2e5908d7cf53feebd5386 Mon Sep 17 00:00:00 2001 From: bcheidemann Date: Sun, 7 Mar 2021 15:20:58 +0000 Subject: [PATCH 10/13] refactor: use correct import for Buffer in buffer shim --- shims/buffer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shims/buffer.js b/shims/buffer.js index 746e7eb..8b15727 100644 --- a/shims/buffer.js +++ b/shims/buffer.js @@ -1,4 +1,4 @@ -const { Buffer } = require('filer'); +const { Buffer } = require('../src/index'); module.exports = { __esModule: true, From 15be384940da87d2ecb7d2488791ed9553925426 Mon Sep 17 00:00:00 2001 From: bcheidemann Date: Sun, 7 Mar 2021 15:21:11 +0000 Subject: [PATCH 11/13] test: run tests for path and buffer --- tests/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/index.js b/tests/index.js index a0f6e3b..e9245e6 100644 --- a/tests/index.js +++ b/tests/index.js @@ -5,6 +5,8 @@ // Shims require('./spec/shims/fs.spec'); +require('./spec/shims/path.spec'); +require('./spec/shims/buffer.spec'); // Filer require('./spec/filer.spec'); From 3a8a59d362af1c3f616be56cb6c89629a0576ede Mon Sep 17 00:00:00 2001 From: bcheidemann Date: Sun, 7 Mar 2021 15:21:28 +0000 Subject: [PATCH 12/13] test: add spec for buffer shim --- tests/spec/shims/buffer.spec.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 tests/spec/shims/buffer.spec.js diff --git a/tests/spec/shims/buffer.spec.js b/tests/spec/shims/buffer.spec.js new file mode 100644 index 0000000..888207a --- /dev/null +++ b/tests/spec/shims/buffer.spec.js @@ -0,0 +1,23 @@ +'use strict'; +const expect = require('chai').expect; +const bufferDefault = require('../../../shims/buffer').default; +const bufferNamed = require('../../../shims/buffer').Buffer; +const bufferActual = require('../../../src/index').Buffer; + +describe.only('path shim', () => { + it('default export should be defined', () => { + expect(bufferDefault).to.not.be.undefined; + }); + + it('named export should be defined', () => { + expect(bufferNamed).to.not.be.undefined; + }); + + it('default export should be re-exposing Buffer', () => { + expect(bufferDefault).to.equal(bufferActual); + }); + + it('named export should be re-exposing Buffer', () => { + expect(bufferNamed).to.equal(bufferActual); + }); +}); From a2151cab25b48471770a2a3e3b6c958cdfbb1d95 Mon Sep 17 00:00:00 2001 From: bcheidemann Date: Sun, 7 Mar 2021 15:21:39 +0000 Subject: [PATCH 13/13] test: add spec for path shim --- tests/spec/shims/path.spec.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 tests/spec/shims/path.spec.js diff --git a/tests/spec/shims/path.spec.js b/tests/spec/shims/path.spec.js new file mode 100644 index 0000000..0432aca --- /dev/null +++ b/tests/spec/shims/path.spec.js @@ -0,0 +1,14 @@ +'use strict'; +const expect = require('chai').expect; +const path = require('../../../shims/path').default; +const pathActual = require('../../../src/index').path; + +describe.only('path shim', () => { + it('should be defined', () => { + expect(path).to.not.be.undefined; + }); + + it('should be re-exposing path', () => { + expect(path).to.equal(pathActual); + }); +});