├── .gitignore
├── .jshintrc
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── SECURITY.md
├── bin
└── json-silo
├── data
└── .gitignore
├── lib
├── databasemanager.js
├── esmapdbmanager.js
├── imagesmanager.js
├── jsonsilo.js
├── routes
│ ├── responsehandler.js
│ ├── store.js
│ └── stories.js
└── storiesmanager.js
├── package.json
└── web
└── json-silo
├── images
├── json-silo.png
├── pareto-anywhere-logo-nav.png
└── reelyactive-logo-square-nav.png
├── index.html
├── js
├── bootstrappyactive.bundle.min.js
├── bootstrappyactive.bundle.min.js.map
├── color-modes.js
├── cormorant.js
├── cuttlefish-story.js
├── fontawesome-reelyactive.min.js
├── fontawesome-reelyactive.min.js.LICENSE.txt
├── jsonsilo.js
└── stories.js
├── stories
└── index.html
└── style
├── bootstrappyactive.min.css
└── bootstrappyactive.min.css.map
/.gitignore:
--------------------------------------------------------------------------------
1 | # Logs
2 | logs
3 | *.log
4 |
5 | # Runtime data
6 | pids
7 | *.pid
8 | *.seed
9 |
10 | # Directory for instrumented libs generated by jscoverage/JSCover
11 | lib-cov
12 |
13 | # Coverage directory used by tools like istanbul
14 | coverage
15 |
16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17 | .grunt
18 |
19 | # Compiled binary addons (http://nodejs.org/api/addons.html)
20 | build/Release
21 |
22 | # Dependency directory
23 | # Commenting this out is preferred by some people, see
24 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
25 | node_modules
26 |
27 | # Users Environment Variables
28 | .lock-wscript
29 |
--------------------------------------------------------------------------------
/.jshintrc:
--------------------------------------------------------------------------------
1 | {
2 | "curly": true,
3 | "eqeqeq": true,
4 | "funcscope": true,
5 | "freeze": true,
6 | "maxdepth": 3,
7 | "maxstatements": 15,
8 | "eqnull": true,
9 | "node": true
10 | }
11 |
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Contributor Covenant Code of Conduct
2 |
3 | ## Our Pledge
4 |
5 | We as members, contributors, and leaders pledge to make participation in our
6 | community a harassment-free experience for everyone, regardless of age, body
7 | size, visible or invisible disability, ethnicity, sex characteristics, gender
8 | identity and expression, level of experience, education, socio-economic status,
9 | nationality, personal appearance, race, religion, or sexual identity
10 | and orientation.
11 |
12 | We pledge to act and interact in ways that contribute to an open, welcoming,
13 | diverse, inclusive, and healthy community.
14 |
15 | ## Our Standards
16 |
17 | Examples of behavior that contributes to a positive environment for our
18 | community include:
19 |
20 | * Demonstrating empathy and kindness toward other people
21 | * Being respectful of differing opinions, viewpoints, and experiences
22 | * Giving and gracefully accepting constructive feedback
23 | * Accepting responsibility and apologizing to those affected by our mistakes,
24 | and learning from the experience
25 | * Focusing on what is best not just for us as individuals, but for the
26 | overall community
27 |
28 | Examples of unacceptable behavior include:
29 |
30 | * The use of sexualized language or imagery, and sexual attention or
31 | advances of any kind
32 | * Trolling, insulting or derogatory comments, and personal or political attacks
33 | * Public or private harassment
34 | * Publishing others' private information, such as a physical or email
35 | address, without their explicit permission
36 | * Other conduct which could reasonably be considered inappropriate in a
37 | professional setting
38 |
39 | ## Enforcement Responsibilities
40 |
41 | Community leaders are responsible for clarifying and enforcing our standards of
42 | acceptable behavior and will take appropriate and fair corrective action in
43 | response to any behavior that they deem inappropriate, threatening, offensive,
44 | or harmful.
45 |
46 | Community leaders have the right and responsibility to remove, edit, or reject
47 | comments, commits, code, wiki edits, issues, and other contributions that are
48 | not aligned to this Code of Conduct, and will communicate reasons for moderation
49 | decisions when appropriate.
50 |
51 | ## Scope
52 |
53 | This Code of Conduct applies within all community spaces, and also applies when
54 | an individual is officially representing the community in public spaces.
55 | Examples of representing our community include using an official e-mail address,
56 | posting via an official social media account, or acting as an appointed
57 | representative at an online or offline event.
58 |
59 | ## Enforcement
60 |
61 | Instances of abusive, harassing, or otherwise unacceptable behavior may be
62 | reported to the community leaders responsible for enforcement at
63 | conduct@reelyactive.com.
64 | All complaints will be reviewed and investigated promptly and fairly.
65 |
66 | All community leaders are obligated to respect the privacy and security of the
67 | reporter of any incident.
68 |
69 | ## Enforcement Guidelines
70 |
71 | Community leaders will follow these Community Impact Guidelines in determining
72 | the consequences for any action they deem in violation of this Code of Conduct:
73 |
74 | ### 1. Correction
75 |
76 | **Community Impact**: Use of inappropriate language or other behavior deemed
77 | unprofessional or unwelcome in the community.
78 |
79 | **Consequence**: A private, written warning from community leaders, providing
80 | clarity around the nature of the violation and an explanation of why the
81 | behavior was inappropriate. A public apology may be requested.
82 |
83 | ### 2. Warning
84 |
85 | **Community Impact**: A violation through a single incident or series
86 | of actions.
87 |
88 | **Consequence**: A warning with consequences for continued behavior. No
89 | interaction with the people involved, including unsolicited interaction with
90 | those enforcing the Code of Conduct, for a specified period of time. This
91 | includes avoiding interactions in community spaces as well as external channels
92 | like social media. Violating these terms may lead to a temporary or
93 | permanent ban.
94 |
95 | ### 3. Temporary Ban
96 |
97 | **Community Impact**: A serious violation of community standards, including
98 | sustained inappropriate behavior.
99 |
100 | **Consequence**: A temporary ban from any sort of interaction or public
101 | communication with the community for a specified period of time. No public or
102 | private interaction with the people involved, including unsolicited interaction
103 | with those enforcing the Code of Conduct, is allowed during this period.
104 | Violating these terms may lead to a permanent ban.
105 |
106 | ### 4. Permanent Ban
107 |
108 | **Community Impact**: Demonstrating a pattern of violation of community
109 | standards, including sustained inappropriate behavior, harassment of an
110 | individual, or aggression toward or disparagement of classes of individuals.
111 |
112 | **Consequence**: A permanent ban from any sort of public interaction within
113 | the community.
114 |
115 | ## Attribution
116 |
117 | This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118 | version 2.0, available at
119 | https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
120 |
121 | Community Impact Guidelines were inspired by [Mozilla's code of conduct
122 | enforcement ladder](https://github.com/mozilla/diversity).
123 |
124 | [homepage]: https://www.contributor-covenant.org
125 |
126 | For answers to common questions about this code of conduct, see the FAQ at
127 | https://www.contributor-covenant.org/faq. Translations are available at
128 | https://www.contributor-covenant.org/translations.
129 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | How to contribute
2 | =================
3 |
4 | First and foremost: _thank you for your interest in contributing to this open source software project!_
5 |
6 | The many ways in which you can contribute include:
7 | - [reporting an issue](#reporting-an-issue)
8 | - [fixing an issue](#fixing-an-issue)
9 | - [adding a feature](#adding-a-feature)
10 | - [financial support](#financial-support)
11 | - [love and encouragement](#love-and-encouragement)
12 |
13 | [](CODE_OF_CONDUCT.md)
14 | Kindly observe the standard [code of conduct](CODE_OF_CONDUCT.md) however you choose to contribute.
15 |
16 |
17 | Reporting an issue
18 | ------------------
19 |
20 | We invite contributors to __create an issue on GitHub__ whenever a significant bug or impedement to the existing functionality of the project is encountered. Kindly include sufficient detail to easily replicate the issue as this will maximise the chances of a swift resolution.
21 |
22 |
23 | Fixing an issue
24 | ---------------
25 |
26 | We invite contributors who are comfortable fixing any significant and obvious issue they discover to __create a pull request__ on the appropriate project branch.
27 |
28 | :information_source: Kindly observe reelyActive's [Node.js style guide](https://github.com/reelyactive/node-style-guide) and/or [Web style guide](https://github.com/reelyactive/web-style-guide) as applicable.
29 |
30 |
31 | Adding a feature
32 | ----------------
33 |
34 | We invite _new_ contributors to __fork the project__, add new features, and then [contact us](https://www.reelyactive.com/contact/) to share their work.
35 |
36 | We invite _veteran_ contributors to __create a pull request__ on the appropriate branch for each new feature they develop.
37 |
38 | :information_source: Kindly observe reelyActive's [Node.js style guide](https://github.com/reelyactive/node-style-guide) and/or [Web style guide](https://github.com/reelyactive/web-style-guide) as applicable.
39 |
40 |
41 | Financial support
42 | -----------------
43 |
44 | Open source development of this and other reelyActive software packages is supported primarily by [annual subscriptions](https://www.reelyactive.com/pricing/) from clients, partners and members of our community. If you're in a position to contribute financially, please consider doing so.
45 |
46 |
47 | Love and encouragement
48 | ----------------------
49 |
50 | This open source project exists because of a perceived community need and the tireless efforts of its contributors. Consider encouraging them by:
51 | - :star: starring the project on GitHub
52 | - :busts_in_silhouette: sharing the project with friends and colleagues to raise awareness
53 | - :heart: surprising the community and its contributors with random acts of kindness
54 |
55 |
56 | These contribution guidelines were drafted by [the reelyActive team](https://reelyactive.com/team/) and we invite you to adapt them for your own projects under a [Creative Commons Attribution 4.0 International License](https://creativecommons.org/licenses/by/4.0/).
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2014-2024 reelyactive
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.
22 |
23 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | json-silo
2 | =========
3 |
4 |
5 | A lightweight digital twins store and API for the IoT
6 | -----------------------------------------------------
7 |
8 | The __json-silo__ is a digital twins store and an accessory module of [Pareto Anywhere](https://www.reelyactive.com/pareto/anywhere/) open source middleware for [context-aware physical spaces](https://www.reelyactive.com/context-aware-physical-spaces/).
9 |
10 | 
11 |
12 | Specifically, the __json-silo__ stores "stories" which are machine-readable representations of people, products, places, etc. in the form of JSON-LD and Schema.org. Additionally, it provides a simple mechanism for file storage and retrieval, for any accompanying metadata such as images. The __json-silo__ can run standalone, even on resource-constrained devices.
13 |
14 |
15 | Installation
16 | ------------
17 |
18 | npm install json-silo
19 |
20 |
21 | Hello json-silo!
22 | ----------------
23 |
24 | npm start
25 |
26 | Browse to [localhost:3001/json-silo/](http://localhost:3001/json-silo/) and observe the json-silo logo. Interact with the json-silo via its REST API.
27 |
28 |
29 | REST API
30 | --------
31 |
32 | The __json-silo__'s REST API includes the following two base routes:
33 | - /stories _for retrieving/specifying digital twins_
34 | - /store _for storing and retrieving metadata such as /images_
35 |
36 |
37 | ### POST /stories
38 |
39 | Create a story. The _id_ of the created story is provided in the response.
40 |
41 | #### Example request
42 |
43 | | Method | Route | Content-Type |
44 | |:-------|:---------|:-----------------|
45 | | POST | /stories | application/json |
46 |
47 | {
48 | "@context": {
49 | "schema": "https://schema.org/"
50 | },
51 | "@graph": [
52 | {
53 | "@id": "person",
54 | "@type": "schema:Person",
55 | "schema:givenName": "barnowl"
56 | }
57 | ]
58 | }
59 |
60 | #### Example response
61 |
62 | {
63 | "_meta": {
64 | "message": "ok",
65 | "statusCode": 200
66 | },
67 | "_links": {
68 | "self": {
69 | "href": "http://localhost:3000/stories"
70 | }
71 | },
72 | "stories": {
73 | "barnowl": {
74 | "@context": {
75 | "schema": "https://schema.org/"
76 | },
77 | "@graph": [
78 | {
79 | "@id": "person",
80 | "@type": "schema:Person",
81 | "schema:givenName": "barnowl"
82 | }
83 | ]
84 | }
85 | }
86 | }
87 |
88 |
89 | ### GET /stories/{id}
90 |
91 | Retrieve the story with the given _id_.
92 |
93 | #### Example request
94 |
95 | | Method | Route | Content-Type |
96 | |:-------|:-----------------|:-----------------|
97 | | GET | /stories/barnowl | application/json |
98 |
99 | #### Example response
100 |
101 | {
102 | "_meta": {
103 | "message": "ok",
104 | "statusCode": 200
105 | },
106 | "_links": {
107 | "self": {
108 | "href": "http://localhost:3000/stories/barnowl"
109 | }
110 | },
111 | "stories": {
112 | "barnowl": {
113 | "@context": {
114 | "schema": "https://schema.org/"
115 | },
116 | "@graph": [
117 | {
118 | "@id": "person",
119 | "@type": "schema:Person",
120 | "schema:givenName": "barnowl"
121 | }
122 | ]
123 | }
124 | }
125 | }
126 |
127 |
128 | ### POST /store/images
129 |
130 | Upload an image to the store. The _id_ (modified filename) of the uploaded image is provided in the response. The image is modified using [sharp](https://github.com/lovell/sharp) to conform to standard dimensions before it is stored on the filesystem.
131 |
132 | #### Example request
133 |
134 | | Method | Route | Content-Type |
135 | |:-------|:--------------|:--------------------|
136 | | POST | /store/images | multipart/form-data |
137 |
138 |
139 | ### GET /store/images/{id}
140 |
141 | Retrieve the image with the given _id_ (filename).
142 |
143 | #### Example request
144 |
145 | | Method | Route | Content-Type |
146 | |:-------|:---------------------------|:--------------------|
147 | | POST | /store/images/12345678.jpg | multipart/form-data |
148 |
149 |
150 | What's in a name?
151 | -----------------
152 |
153 | The __json-silo__ is exactly that: a data silo for [JSON](https://en.wikipedia.org/wiki/JSON)! Simple enough, right? So why does it have a grain silo with a hockey mask for a mascot?
154 |
155 | At reelyActive we've always been outspoken about the need for an open Internet of Things as opposed to a bunch of siloed applications. In 2013, on social media we recycled the ["More cowbell" meme](https://en.wikipedia.org/wiki/More_cowbell) with an image of Will Ferrell banging on a grain silo with the caption ["The Internet of Things does not need More Silo"](https://reelyactive.github.io/images/moreSilo.jpg). When it came time to create a mascot for the __json-silo__, we decided to start with that grain silo.
156 |
157 | Now, how do you visually represent JSON in association with a grain silo? Sure, we could have slapped the official JSON logo on that silo, but where's the fun in that? Instead, for those of us who grew up in the eighties, hearing "JSON" out of context (pun intended) evokes the image of [Jason Voorhees](https://en.wikipedia.org/wiki/Jason_Voorhees) from the Friday the 13th series of films, specifically the iconic hockey goaltender mask he wore. Not only does that "Jason" mask make for a silly visual pun, it also gives a nod to our hometown heritage, where [Jacques Plante](https://en.wikipedia.org/wiki/Jacques_Plante) of the Montreal Canadiens was the first goaltender to wear such a mask full-time, which would later become standard practice. We'd be pleased to see the use of personal data lockers become standard practice too.
158 |
159 | 
160 |
161 |
162 | What's next?
163 | ------------
164 |
165 | __json-silo__ v1.0.0 was released in August 2019, superseding all earlier versions, the latest of which remains available in the [release-0.5 branch](https://github.com/reelyactive/json-silo/tree/release-0.5) and as [json-silo@0.5.2 on npm](https://www.npmjs.com/package/json-silo/v/0.5.2).
166 |
167 | __json-silo__ v1.1.0 migrates to [ESMapDB](https://github.com/reelyactive/esmapdb) from [NeDB](https://github.com/louischatriot/nedb). If upgrading from a previous version, any stored stories will need to be recreated.
168 |
169 |
170 | Contributing
171 | ------------
172 |
173 | Discover [how to contribute](CONTRIBUTING.md) to this open source project which upholds a standard [code of conduct](CODE_OF_CONDUCT.md).
174 |
175 |
176 | Security
177 | --------
178 |
179 | Consult our [security policy](SECURITY.md) for best practices using this open source software and to report vulnerabilities.
180 |
181 |
182 | License
183 | -------
184 |
185 | MIT License
186 |
187 | Copyright (c) 2014-2024 [reelyActive](https://www.reelyactive.com)
188 |
189 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
190 |
191 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
192 |
193 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
194 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
195 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
196 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
198 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
199 | THE SOFTWARE.
200 |
201 |
--------------------------------------------------------------------------------
/SECURITY.md:
--------------------------------------------------------------------------------
1 | Using reelyActive open source software securely
2 | ===============================================
3 |
4 | If you're reading this, it is likely because you take open source software security seriously. _Thank you!_
5 |
6 |
7 | Keep up to date
8 | ---------------
9 |
10 | Unless otherwise specified, it is recommended to regularly update to the most recent version of this software package, and to review the security test results, including any [Dependabot alerts](https://docs.github.com/code-security/dependabot/dependabot-alerts) listed under the Security tab of this GitHub repository, ideally automating this process and including an alerting feature.
11 |
12 |
13 | Disclose a vulnerability
14 | ------------------------
15 |
16 | Should you discover a novel security issue or vulnerability, kindly __report your findings privately__, either to __security@reelyactive.com__, or with the __Report a vulnerability__ feature under the Security tab of this GitHub repository.
17 |
18 | Kindly provide sufficient detail to effectively address, if not resolve, the issue, as well as a means of contact should additional detail be required, _and so that we may return our thanks!_
19 |
20 |
21 | These security guidelines were drafted by [the reelyActive team](https://www.reelyactive.com/team/) and we invite you to adapt them for your own projects under a [Creative Commons Attribution 4.0 International License](https://creativecommons.org/licenses/by/4.0/).
--------------------------------------------------------------------------------
/bin/json-silo:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env node
2 |
3 | const http = require('http');
4 | const express = require('express');
5 | const bodyParser = require('body-parser');
6 | const JSONSilo = require('../lib/jsonsilo.js');
7 |
8 |
9 | const PORT = process.env.PORT || 3001;
10 |
11 |
12 | let app = express();
13 | app.use(bodyParser.json({ limit: "50mb", extended: true }));
14 |
15 | let server = http.createServer(app);
16 | server.listen(PORT, function() {
17 | console.log('json-silo instance is listening on port', PORT);
18 | });
19 |
20 | const options = { app: app };
21 |
22 | let stories = new JSONSilo(options);
--------------------------------------------------------------------------------
/data/.gitignore:
--------------------------------------------------------------------------------
1 | # Include the empty data folder in the repo
2 | *
3 | !.gitignore
4 |
--------------------------------------------------------------------------------
/lib/databasemanager.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright reelyActive 2014-2022
3 | * We believe in an open Internet of Things
4 | */
5 |
6 |
7 | const ESMapDBManager = require('./esmapdbmanager');
8 |
9 |
10 | /**
11 | * DatabaseManager Class
12 | * Manages the database(s) in which the data is stored, abstracting away the
13 | * implementation details.
14 | */
15 | class DatabaseManager {
16 |
17 | /**
18 | * DatabaseManager constructor
19 | * @param {Object} options The options as a JSON object.
20 | * @constructor
21 | */
22 | constructor(options) {
23 | options = options || {};
24 |
25 | this.database = new ESMapDBManager(options);
26 | }
27 |
28 | /**
29 | * Delete the pair associated with the given key from database.
30 | * @param {String} key The key to look up.
31 | * @param {function} callback Function to call on completion.
32 | */
33 | delete(key, callback) {
34 | this.database.delete(key, callback);
35 | }
36 |
37 | /**
38 | * Get the value associated with the given key from database.
39 | * @param {String} key The key to look up.
40 | * @param {function} callback Function to call on completion.
41 | */
42 | get(key, callback) {
43 | this.database.get(key, callback);
44 | }
45 |
46 | /**
47 | * Determine if the given key exists in the database.
48 | * @param {String} key The key to look up.
49 | * @param {function} callback Function to call on completion.
50 | */
51 | has(key, callback) {
52 | this.database.has(key, callback);
53 | }
54 |
55 | /**
56 | * Set the value associated with the given key in the database.
57 | * @param {String} key The key to look up.
58 | * @param {function} callback Function to call on completion.
59 | */
60 | set(key, value, callback) {
61 | this.database.set(key, value, callback);
62 | }
63 |
64 | /**
65 | * Find all key/value pairs in the database that match the given query.
66 | * @param {Object} query The query parameters.
67 | * @param {function} callback Function to call on completion.
68 | */
69 | find(query, callback) {
70 | this.database.find(query, callback);
71 | }
72 |
73 | }
74 |
75 |
76 | module.exports = DatabaseManager;
--------------------------------------------------------------------------------
/lib/esmapdbmanager.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright reelyActive 2015-2022
3 | * We believe in an open Internet of Things
4 | */
5 |
6 |
7 | const ESMapDB = require('esmapdb');
8 |
9 |
10 | const DEFAULT_DATA_FOLDER = 'data';
11 | const DEFAULT_ASSOCIATION_DB = 'stories';
12 |
13 |
14 | /**
15 | * ESMapDBManager Class
16 | * Manages an ESMapDB database instance.
17 | */
18 | class ESMapDBManager {
19 |
20 | /**
21 | * EsMapDBManager constructor
22 | * @param {Object} options The options as a JSON object.
23 | * @constructor
24 | */
25 | constructor(options) {
26 | options = options || {};
27 |
28 | let persistentLocation = DEFAULT_DATA_FOLDER + '/' + DEFAULT_ASSOCIATION_DB;
29 | this.database = new ESMapDB({ createInMemory: true,
30 | createPersistent: true,
31 | persistentLocation: persistentLocation,
32 | persistentValueEncoding: 'json' });
33 | }
34 |
35 | /**
36 | * Delete the pair associated with the given key from database.
37 | * @param {String} key The key to look up.
38 | * @param {function} callback Function to call on completion.
39 | */
40 | delete(key, callback) {
41 | this.database.delete(key, callback);
42 | }
43 |
44 | /**
45 | * Get the value associated with the given key from database.
46 | * @param {String} key The key to look up.
47 | * @param {function} callback Function to call on completion.
48 | */
49 | get(key, callback) {
50 | this.database.get(key, callback);
51 | }
52 |
53 | /**
54 | * Determine if the given key exists in the database.
55 | * @param {String} key The key to look up.
56 | * @param {function} callback Function to call on completion.
57 | */
58 | has(key, callback) {
59 | this.database.has(key, callback);
60 | }
61 |
62 | /**
63 | * Set the value associated with the given key in the database.
64 | * @param {String} key The key to look up.
65 | * @param {function} callback Function to call on completion.
66 | */
67 | set(key, value, callback) {
68 | this.database.set(key, value, callback);
69 | }
70 |
71 | /**
72 | * Find all key/value pairs in the database that match the given query.
73 | * @param {Object} query The query parameters.
74 | * @param {function} callback Function to call on completion.
75 | */
76 | find(query, callback) {
77 | let result = new Map();
78 |
79 | this.database.forEach((value, key) => {
80 | // TODO: handle query rather than assuming 'get all'
81 | result.set(key, value);
82 | });
83 |
84 | callback(undefined, result);
85 |
86 | return result;
87 | }
88 |
89 | }
90 |
91 |
92 | module.exports = ESMapDBManager;
93 |
--------------------------------------------------------------------------------
/lib/imagesmanager.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright reelyActive 2014-2022
3 | * We believe in an open Internet of Things
4 | */
5 |
6 |
7 | const crypto = require('crypto');
8 | const fs = require('fs');
9 | const path = require('path');
10 | const sharp = require('sharp');
11 |
12 |
13 | const DEFAULT_IMAGES_FOLDER = 'data/images';
14 | const DEFAULT_IMAGE_WIDTH_PIXELS = 480;
15 | const DEFAULT_IMAGE_HEIGHT_PIXELS = 480;
16 | const DEFAULT_IDENTIFIER_LENGTH = 4;
17 | const HTTP_STATUS_OK = 200;
18 | const HTTP_STATUS_CREATED = 201;
19 | const HTTP_STATUS_BAD_REQUEST = 400;
20 | const HTTP_STATUS_NOT_FOUND = 404;
21 | const HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE = 415;
22 |
23 |
24 | /**
25 | * ImagesManager Class
26 | * Manages the storage and retrieval of images.
27 | */
28 | class ImagesManager {
29 |
30 | /**
31 | * ImagesManager constructor
32 | * @param {Object} options The options as a JSON object.
33 | * @constructor
34 | */
35 | constructor(options) {
36 | let self = this;
37 | options = options || {};
38 |
39 | self.identifierLength = options.identifierLength ||
40 | DEFAULT_IDENTIFIER_LENGTH;
41 |
42 | fs.mkdir(DEFAULT_IMAGES_FOLDER, { recursive: true }, (err) => {
43 | if(err) {
44 | console.log('json-silo could not create/access images directory:',
45 | DEFAULT_IMAGES_FOLDER, ' Image store will not function.');
46 | }
47 | });
48 | }
49 |
50 | /**
51 | * Create a new image
52 | * @param {Object} req The HTTP request.
53 | * @param {Object} res The HTTP result.
54 | * @param {callback} callback Function to call on completion.
55 | */
56 | create(req, res, callback) {
57 | let self = this;
58 |
59 | if(req.file === undefined) {
60 | return callback(HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE);
61 | }
62 | else {
63 | let fileName = crypto.randomBytes(self.identifierLength).toString('hex')
64 | + path.extname(req.file.originalname);
65 | let filePath = path.resolve(DEFAULT_IMAGES_FOLDER + '/' + fileName);
66 |
67 | sharp(req.file.buffer)
68 | .resize(DEFAULT_IMAGE_WIDTH_PIXELS, DEFAULT_IMAGE_HEIGHT_PIXELS)
69 | .toFile(filePath, (err, info) => {
70 | if(err) {
71 | return callback(HTTP_STATUS_BAD_REQUEST);
72 | }
73 |
74 | let images = {};
75 | images[fileName] = info;
76 |
77 | return callback(HTTP_STATUS_CREATED, { images: images });
78 | });
79 | }
80 | }
81 |
82 | /**
83 | * Retrieve an existing image
84 | * @param {String} fileName The filename of the image.
85 | * @param {callback} callback Function to call on completion.
86 | */
87 | retrieve(fileName, callback) {
88 | let filePath = path.resolve(DEFAULT_IMAGES_FOLDER + '/' + fileName);
89 |
90 | fs.access(filePath, fs.F_OK, (err) => {
91 | if(err) {
92 | return callback(HTTP_STATUS_NOT_FOUND);
93 | }
94 | return callback(HTTP_STATUS_OK, filePath);
95 | });
96 | }
97 |
98 | }
99 |
100 |
101 | module.exports = ImagesManager;
102 |
--------------------------------------------------------------------------------
/lib/jsonsilo.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright reelyActive 2014-2022
3 | * We believe in an open Internet of Things
4 | */
5 |
6 |
7 | const express = require('express');
8 | const path = require('path');
9 | const DatabaseManager = require('./databasemanager');
10 | const StoriesManager = require('./storiesmanager');
11 | const ImagesManager = require('./imagesmanager');
12 |
13 |
14 | /**
15 | * JSONSilo Class
16 | * Data silo for digital twins in context-aware physical spaces.
17 | */
18 | class JSONSilo {
19 |
20 | /**
21 | * JSONSilo constructor
22 | * @param {Object} options The configuration options.
23 | * @constructor
24 | */
25 | constructor(options) {
26 | let self = this;
27 | options = options || {};
28 |
29 | if(options.app) {
30 | configureExpress(options.app, self);
31 | }
32 |
33 | this.database = new DatabaseManager(options);
34 | this.stories = new StoriesManager(options, self.database);
35 | this.images = new ImagesManager(options);
36 |
37 | console.log('reelyActive json-silo instance is hosting digital twins in an open IoT');
38 | }
39 |
40 | }
41 |
42 |
43 | /**
44 | * Configure the routes of the API.
45 | * @param {Express} app The Express app.
46 | * @param {JSONSilo} instance The JSON Silo instance.
47 | */
48 | function configureExpress(app, instance) {
49 | app.use(function(req, res, next) {
50 | req.jsonsilo = instance;
51 | next();
52 | });
53 | app.use('/stories', require('./routes/stories'));
54 | app.use('/store', require('./routes/store'));
55 | app.use('/', express.static(path.resolve(__dirname + '/../web')));
56 | }
57 |
58 |
59 | module.exports = JSONSilo;
60 |
--------------------------------------------------------------------------------
/lib/routes/responsehandler.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright reelyActive 2014-2019
3 | * We believe in an open Internet of Things
4 | */
5 |
6 | // TODO: move to a library
7 |
8 |
9 | const MESSAGE_OK = "ok";
10 | const MESSAGE_CREATED = "created";
11 | const MESSAGE_NOTFOUND = "notFound";
12 | const MESSAGE_BADREQUEST = "badRequest";
13 | const MESSAGE_NOTIMPLEMENTED = "notImplemented";
14 | const MESSAGE_SERVICEUNAVAILABLE = "serviceUnavailable";
15 | const CODE_OK = 200;
16 | const CODE_CREATED = 201;
17 | const CODE_NOTFOUND = 404;
18 | const CODE_BADREQUEST = 400;
19 | const CODE_NOTIMPLEMENTED = 501;
20 | const CODE_SERVICEUNAVAILABLE = 503;
21 |
22 |
23 | /**
24 | * Prepare the JSON for an API query response.
25 | * @param {Object} req The HTTP request.
26 | * @param {Number} status Integer status code.
27 | * @param {Object} data The data to include in the response.
28 | */
29 | function prepareResponse(req, status, data) {
30 | let rootUrl = req.protocol + '://' + req.get('host');
31 | let queryPath = req.originalUrl;
32 | let response = {};
33 |
34 | prepareMeta(response, status);
35 | prepareLinks(response, rootUrl, queryPath);
36 | prepareData(response, data);
37 |
38 | return response;
39 | }
40 |
41 |
42 | /**
43 | * Prepares and adds the _meta to the given API query response.
44 | * @param {Object} response JSON representation of the response.
45 | * @param {Number} status Integer status code.
46 | */
47 | function prepareMeta(response, status) {
48 | switch(status) {
49 | case CODE_OK:
50 | response._meta = { "message": MESSAGE_OK,
51 | "statusCode": CODE_OK };
52 | break;
53 | case CODE_CREATED:
54 | response._meta = { "message": MESSAGE_CREATED,
55 | "statusCode": CODE_CREATED };
56 | break;
57 | case CODE_NOTFOUND:
58 | response._meta = { "message": MESSAGE_NOTFOUND,
59 | "statusCode": CODE_NOTFOUND };
60 | break;
61 | case CODE_NOTIMPLEMENTED:
62 | response._meta = { "message": MESSAGE_NOTIMPLEMENTED,
63 | "statusCode": CODE_NOTIMPLEMENTED };
64 | break;
65 | case CODE_SERVICEUNAVAILABLE:
66 | response._meta = { "message": MESSAGE_SERVICEUNAVAILABLE,
67 | "statusCode": CODE_SERVICEUNAVAILABLE };
68 | break;
69 | default:
70 | response._meta = { "message": MESSAGE_BADREQUEST,
71 | "statusCode": CODE_BADREQUEST };
72 | }
73 | }
74 |
75 |
76 | /**
77 | * Prepares and adds the _links to the given API query response.
78 | * @param {Object} response JSON representation of the response.
79 | * @param {String} rootUrl The root URL of the original query.
80 | * @param {String} queryPath The query path of the original query.
81 | */
82 | function prepareLinks(response, rootUrl, queryPath) {
83 | response._links = {};
84 | response._links.self = { "href": rootUrl + queryPath };
85 | }
86 |
87 |
88 | /**
89 | * Prepares and adds the data to the given API query response.
90 | * @param {Object} response JSON representation of the response.
91 | * @param {Object} data The data to add to the response.
92 | */
93 | function prepareData(response, data) {
94 | if(!data) {
95 | return;
96 | }
97 |
98 | for(let property in data) {
99 | let item = data[property];
100 |
101 | if(Array.isArray(item)) {
102 | response[property] = {};
103 | item.forEach(function(element) {
104 | if(element.hasOwnProperty('_id')) {
105 | let id = element._id;
106 | delete element._id;
107 | response[property][id] = element;
108 | }
109 | else {
110 | // TODO: handle elegantly the lack of _id
111 | }
112 | });
113 | }
114 | else {
115 | response[property] = item;
116 | }
117 | }
118 | }
119 |
120 |
121 | module.exports.prepareResponse = prepareResponse;
122 |
--------------------------------------------------------------------------------
/lib/routes/store.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright reelyActive 2019-2022
3 | * We believe in an open Internet of Things
4 | */
5 |
6 |
7 | const express = require('express');
8 | const multer = require('multer');
9 | const path = require('path');
10 | const responseHandler = require('./responsehandler');
11 |
12 |
13 | const SUPPORTED_IMAGE_FILE_TYPES = /jpeg|jpg|png|gif/;
14 | const IMAGE_FILE_NAME = 'image';
15 |
16 |
17 | let router = express.Router();
18 | let upload = multer({ fileFilter: fileFilter });
19 |
20 |
21 | router.route('/images')
22 | .post(upload.single(IMAGE_FILE_NAME), function(req, res) {
23 | createImage(req, res);
24 | });
25 |
26 | router.route('/images/:id')
27 | .get(function(req, res) {
28 | retrieveImage(req, res);
29 | });
30 |
31 |
32 | /**
33 | * Create the given image.
34 | * @param {Object} req The HTTP request.
35 | * @param {Object} res The HTTP result.
36 | */
37 | function createImage(req, res) {
38 | let images = req.jsonsilo.images;
39 |
40 | images.create(req, res, function(status, data) {
41 | let response = responseHandler.prepareResponse(req, status, data);
42 | res.status(status).json(response);
43 | });
44 | }
45 |
46 |
47 | /**
48 | * Retrieve the given image.
49 | * @param {Object} req The HTTP request.
50 | * @param {Object} res The HTTP result.
51 | */
52 | function retrieveImage(req, res) {
53 | let images = req.jsonsilo.images;
54 | let imageFileName = req.params.id;
55 |
56 | images.retrieve(imageFileName, function(status, data) {
57 | if(data) {
58 | res.sendFile(data);
59 | }
60 | else {
61 | res.status(status).send();
62 | }
63 | });
64 | }
65 |
66 |
67 | /**
68 | * Reject image files with incompatible mimetype and/or extname.
69 | * @param {Object} req The HTTP request.
70 | * @param {Object} file The multipart file.
71 | * @param {function} callback Function to call on completion.
72 | */
73 | function fileFilter(req, file, callback) {
74 | let isValidExtname = SUPPORTED_IMAGE_FILE_TYPES
75 | .test(path.extname(file.originalname)
76 | .toLowerCase());
77 | let isValidMimetype = SUPPORTED_IMAGE_FILE_TYPES.test(file.mimetype);
78 |
79 | if(isValidMimetype && isValidExtname) {
80 | return callback(null, true);
81 | }
82 | else {
83 | return callback(null, false);
84 | }
85 | }
86 |
87 |
88 | module.exports = router;
89 |
--------------------------------------------------------------------------------
/lib/routes/stories.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright reelyActive 2014-2019
3 | * We believe in an open Internet of Things
4 | */
5 |
6 |
7 | const express = require('express');
8 | const path = require('path');
9 | const responseHandler = require('./responsehandler');
10 |
11 |
12 | let router = express.Router();
13 |
14 | router.route('/')
15 | .post(function(req, res) {
16 | createStory(req, res);
17 | });
18 |
19 | router.route('/:id')
20 | .get(function(req, res) {
21 | retrieveStory(req, res);
22 | });
23 |
24 |
25 | /**
26 | * Create the given story
27 | * @param {Object} req The HTTP request.
28 | * @param {Object} res The HTTP result.
29 | */
30 | function createStory(req, res) {
31 | let story = req.body;
32 | let stories = req.jsonsilo.stories;
33 | stories.create(story, function(status, data) {
34 | let response = responseHandler.prepareResponse(req, status, data);
35 | res.status(status).json(response);
36 | });
37 | }
38 |
39 |
40 | /**
41 | * Retrieve the given story
42 | * @param {Object} req The HTTP request.
43 | * @param {Object} res The HTTP result.
44 | */
45 | function retrieveStory(req, res) {
46 | switch(req.accepts(['json','html'])) {
47 | case 'html':
48 | res.sendFile(path.resolve(__dirname + '/../../web/json-silo/stories/index.html'));
49 | break;
50 | default:
51 | let id = req.params.id;
52 | let stories = req.jsonsilo.stories;
53 | stories.retrieve(id, function(status, data) {
54 | let response = responseHandler.prepareResponse(req, status, data);
55 | res.status(status).json(response);
56 | });
57 | }
58 | }
59 |
60 |
61 | module.exports = router;
62 |
--------------------------------------------------------------------------------
/lib/storiesmanager.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright reelyActive 2014-2022
3 | * We believe in an open Internet of Things
4 | */
5 |
6 |
7 | const crypto = require('crypto');
8 |
9 |
10 | const DEFAULT_IDENTIFIER_LENGTH = 4;
11 | const HTTP_STATUS_OK = 200;
12 | const HTTP_STATUS_CREATED = 201;
13 | const HTTP_STATUS_BAD_REQUEST = 400;
14 | const HTTP_STATUS_NOT_FOUND = 404;
15 |
16 |
17 | /**
18 | * StoriesManager Class
19 | * Manages the storage and retrieval of JSON stories.
20 | */
21 | class StoriesManager {
22 |
23 | /**
24 | * StoriesManager constructor
25 | * @param {Object} options The options as a JSON object.
26 | * @param {DatabaseManager} database The database manager.
27 | * @constructor
28 | */
29 | constructor(options, database) {
30 | let self = this;
31 | options = options || {};
32 | self.database = database;
33 | self.identifierLength = options.identifierLength ||
34 | DEFAULT_IDENTIFIER_LENGTH;
35 | }
36 |
37 | /**
38 | * Create a new story
39 | * @param {String} story The story to create in the database
40 | * @param {callback} callback Function to call on completion
41 | */
42 | create(story, callback) {
43 | let self = this;
44 | let key = crypto.randomBytes(this.identifierLength).toString('hex');
45 |
46 | this.database.has(key, function(err, isPresent) {
47 | if(err) {
48 | return callback(HTTP_STATUS_BAD_REQUEST);
49 | }
50 | else if(isPresent) {
51 | return create(story, callback);
52 | }
53 | else {
54 | self.database.set(key, story, function(err) {
55 | if(err) {
56 | return callback(HTTP_STATUS_BAD_REQUEST);
57 | }
58 | else {
59 | let data = { stories: {} };
60 | data.stories[key] = story;
61 | return callback(HTTP_STATUS_CREATED, data);
62 | }
63 | });
64 | }
65 | });
66 | }
67 |
68 | /**
69 | * Retrieve an existing story
70 | * @param {String} id The id of the story
71 | * @param {callback} callback Function to call on completion
72 | */
73 | retrieve(id, callback) {
74 | this.database.get(id, function(err, story) {
75 | if(err) {
76 | return callback(HTTP_STATUS_BAD_REQUEST);
77 | }
78 | else if(story === undefined) {
79 | return callback(HTTP_STATUS_NOT_FOUND);
80 | }
81 | else {
82 | let data = { stories: {} };
83 | data.stories[id] = story;
84 | return callback(HTTP_STATUS_OK, data);
85 | }
86 | });
87 | }
88 |
89 | }
90 |
91 |
92 | module.exports = StoriesManager;
93 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "json-silo",
3 | "homepage": "https://www.reelyactive.com",
4 | "author": {
5 | "name": "reelyActive",
6 | "email": "info@reelyactive.com"
7 | },
8 | "description": "A data silo for digital twins in context-aware physical spaces. We believe in an open Internet of Things.",
9 | "keywords": [
10 | "reelyActive",
11 | "IoT",
12 | "Personal Data Locker",
13 | "digital twins",
14 | "Schema.org",
15 | "JSON-LD"
16 | ],
17 | "version": "1.1.5",
18 | "engines": {
19 | "node": ">=10.0.0"
20 | },
21 | "main": "./lib/jsonsilo.js",
22 | "scripts": {
23 | "start": "node bin/json-silo"
24 | },
25 | "repository": {
26 | "type": "git",
27 | "url": "https://github.com/reelyactive/json-silo.git"
28 | },
29 | "dependencies": {
30 | "esmapdb": "^0.3.0",
31 | "express": "^4.19.2",
32 | "multer": "^1.4.5-lts.1",
33 | "sharp": "^0.33.4"
34 | },
35 | "devDependencies": {},
36 | "bugs": {
37 | "url": "https://github.com/reelyactive/json-silo/issues"
38 | },
39 | "contributors": [
40 | {
41 | "name": "Jeffrey Dungen"
42 | },
43 | {
44 | "name": "George Koulouris"
45 | },
46 | {
47 | "name": "Furaha Damien"
48 | }
49 | ],
50 | "license": "MIT"
51 | }
52 |
--------------------------------------------------------------------------------
/web/json-silo/images/json-silo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/reelyactive/json-silo/1ece5c5202e4b5e9a7798b20def56c22648b583c/web/json-silo/images/json-silo.png
--------------------------------------------------------------------------------
/web/json-silo/images/pareto-anywhere-logo-nav.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/reelyactive/json-silo/1ece5c5202e4b5e9a7798b20def56c22648b583c/web/json-silo/images/pareto-anywhere-logo-nav.png
--------------------------------------------------------------------------------
/web/json-silo/images/reelyactive-logo-square-nav.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/reelyactive/json-silo/1ece5c5202e4b5e9a7798b20def56c22648b583c/web/json-silo/images/reelyactive-logo-square-nav.png
--------------------------------------------------------------------------------
/web/json-silo/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |