Skip to content

Commit fe280fa

Browse files
committed
chore: Add package adapter-memory
1 parent 332bcc5 commit fe280fa

File tree

10 files changed

+3112
-12
lines changed

10 files changed

+3112
-12
lines changed

packages/adapter-flat-file-db/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/adapter-flat-file-db/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"repository": "https://github.com/micro-analytics/micro-analytics",
77
"main": "dist.js",
88
"scripts": {
9-
"test": "jest",
109
"build": "babel index.js -o dist.js",
1110
"prepublish": "npm run build"
1211
},
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
const path = require('path')
2-
const flatfile = require('flat-file-db')
3-
const promisify = require('then-flat-file-db')
4-
const test = require('micro-analytics-cli/adapter-tests/unit-tests')
5-
6-
const db = promisify(flatfile.sync(path.resolve(process.cwd(), process.env.DB_NAME || 'views.db')))
7-
const adapter = require('./index')
1+
const path = require('path');
2+
const flatfile = require('flat-file-db');
3+
const promisify = require('then-flat-file-db');
4+
const test = require('micro-analytics-adapter-utils/unit-tests');
85

6+
const db = promisify(
7+
flatfile.sync(path.resolve(process.cwd(), process.env.DB_NAME || 'views.db'))
8+
);
9+
const adapter = require('./index');
910

1011
test({
1112
name: 'flat-file-db',
1213
modulePath: path.resolve(__dirname, './index.js'),
1314
beforeAll: () => {
14-
adapter.init({ dbName: 'views.db' })
15+
adapter.init({ dbName: 'views.db' });
1516
},
1617
beforeEach: () => {
1718
return db.clear();
18-
}
19-
})
19+
},
20+
});

packages/adapter-memory/index.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
const Observable = require('zen-observable');
2+
3+
let data = {};
4+
let handlers = [];
5+
6+
const escapeRegexp = require('escape-regex');
7+
8+
const keyRegex = str => {
9+
str = str.split('*').map(s => escapeRegexp(s)).join('*');
10+
return new RegExp('^' + str.replace('*', '.*'));
11+
};
12+
13+
const observable = new Observable(observer => {
14+
handlers.push(data => observer.next(data));
15+
let index = handlers.length;
16+
return () => {
17+
handlers = [...handlers.slice(0, index), ...handlers.slice(index)];
18+
};
19+
});
20+
21+
function get(key, options) {
22+
const value = data[key] || { views: [] };
23+
return Promise.resolve({
24+
views: value.views.filter(view => {
25+
if (options && options.before && view.time > options.before) return false;
26+
if (options && options.after && view.time < options.after) return false;
27+
return true;
28+
}),
29+
});
30+
}
31+
32+
function keys() {
33+
return Promise.resolve(Object.keys(data));
34+
}
35+
36+
function put(key, value) {
37+
data[key] = value;
38+
handlers.forEach(handler => {
39+
handler({ key, value });
40+
});
41+
42+
return Promise.resolve();
43+
}
44+
45+
async function getAll(options) {
46+
const value = {};
47+
const _keys = (await keys()).filter(
48+
key =>
49+
options.ignoreWildcard
50+
? key.startsWith(options.pathname)
51+
: key.match(keyRegex(options.pathname))
52+
);
53+
54+
for (let key of _keys) {
55+
value[key] = await get(key, {
56+
before: options.before,
57+
after: options.after,
58+
});
59+
}
60+
61+
return Promise.resolve(value);
62+
}
63+
64+
function has(key) {
65+
return Promise.resolve({}.hasOwnProperty.call(data, key));
66+
}
67+
68+
function clear() {
69+
data = {};
70+
}
71+
72+
function subscribe(listener) {
73+
return observable.subscribe(listener);
74+
}
75+
76+
module.exports = { get, put, getAll, has, keys, clear, subscribe };

packages/adapter-memory/license

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Rolf Erik Lekang
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "micro-analytics-adapter-memory",
3+
"version": "1.0.0",
4+
"description": "An in memory adapter for micro-analytics",
5+
"license": "MIT",
6+
"repository": "https://github.com/micro-analytics/micro-analytics",
7+
"main": "index.js",
8+
"author": "Rolf Erik Lekang <[email protected]>",
9+
"engineStrict": true,
10+
"engines": {
11+
"node": ">=6.0.0"
12+
},
13+
"keywords": [
14+
"micro-analytics",
15+
"adapter"
16+
],
17+
"dependencies": {},
18+
"devDependencies": {
19+
"micro-analytics-adapter-utils": "^0.0.1"
20+
}
21+
}

packages/adapter-memory/readme.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# micro-analytics-adapter-memory
2+
3+
This is an in-memory adapter for [micro-analytics][].
4+
5+
#### Why would you want to to use an in memory adapter for micro-analytics?
6+
7+
It can be useful for testing and when deploying test deployments to now.sh.
8+
now does not support writing to the filesystem, which makes micro-analytics
9+
unable to start with the flat-file-db-adapter. In production use, you would
10+
probably want to use a real database if you care about the data collected.
11+
12+
## Usage
13+
14+
```
15+
npm install micro-analytics-cli micro-analytics-adapter-memory
16+
micro-analytics --adapter memory
17+
```
18+
19+
and open [localhost:3000](https://localhost:3000).
20+
21+
[micro-analytics]: https://github.com/micro-analytics

packages/adapter-memory/test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const test = require('micro-analytics-adapter-utils/unit-tests');
2+
const path = require('path');
3+
4+
const adapter = require('./index');
5+
6+
test({
7+
name: 'memory',
8+
modulePath: path.resolve(__dirname, './index.js'),
9+
beforeEach: () => {
10+
adapter.clear();
11+
},
12+
});

0 commit comments

Comments
 (0)