Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
56e185e
Add support for sudo and mu-auth-scope for queries
madnificent Jan 3, 2024
0774828
Merge tag 'v1.8.0' into feature/query-meta
madnificent Apr 8, 2024
a4d647a
Export sparqlEscapeDecimal
madnificent Apr 13, 2024
307fef4
Work esmodule support assuming client package.json
madnificent Oct 20, 2024
b407bea
Add package.json for mu
madnificent Oct 20, 2024
2b1fdbd
Ensure package.json has type: module
madnificent Oct 21, 2024
713a2e4
add package lock to dockerfile, add gitignore
Rahien Dec 7, 2024
88437be
use new docker file syntax to not get warnings on build
Rahien Dec 7, 2024
e911218
rewire build to dist and keep old sources around to help vscode out w…
Rahien Dec 7, 2024
0657711
add NO_BABEL_NODE env variable that can be set to true to use regular…
Rahien Dec 7, 2024
0958d44
fix typo in error handling example in Readme
x-m-el Dec 20, 2024
c9cdd4a
merge package jsons, keep template modules by default
Rahien Dec 22, 2024
23b0abd
add install script to fetch combined node_modules folder
Rahien Dec 8, 2024
2247678
add install script
Rahien Dec 22, 2024
b31f1a1
no need for adding package-lock anymore, handled more transparently
Rahien Dec 22, 2024
83c8b69
allow simple services without package json to also run mu script inst…
Rahien Dec 24, 2024
430d50f
Merge branch 'feature/query-meta'
madnificent Jan 13, 2025
27bcd38
Merge remote-tracking branch 'origin/master'
madnificent Jan 13, 2025
8d00c53
use new docker file syntax to not get warnings on build
Rahien Dec 7, 2024
a759a7b
Add gitignore
Rahien Dec 7, 2024
d9de551
Merge branch 'master' into rahien/rewire-merge-packages
madnificent Jan 13, 2025
dcbc2da
Rename file for easier reading
madnificent Jan 13, 2025
d267699
Code conventions and questions
madnificent Jan 13, 2025
33c1aef
Remove option to override template dependencies
madnificent Jan 13, 2025
0a4b51a
Remove option to run BABEL_NODE
madnificent Jan 13, 2025
71b73ac
Rename /tmp/package.json to /tmp/merged-package.json
madnificent Jan 13, 2025
23225b8
Remove support for -clean in script
madnificent Jan 13, 2025
9b36818
Don't require package-lock.json to be created
madnificent Jan 13, 2025
6035957
Add commentary to run.sh
madnificent Jan 13, 2025
737b881
Place service-package-old.json in /tmp/ and rename
madnificent Jan 13, 2025
bbdfa03
Add comment on intention of --delete or not in rsync
madnificent Jan 13, 2025
f0424f7
Disable transpilation of current mu sources
madnificent Jan 13, 2025
89bda40
Use feature branch in dev script
madnificent Jan 13, 2025
2be5a1e
Rename to setup-ide script
madnificent Jan 13, 2025
3dd6eab
Add some JSDOC for testing
madnificent Jan 13, 2025
fa4ea4f
Just hacks, revert and cleanly push
madnificent Jan 13, 2025
cb627b6
Revert "Disable transpilation of current mu sources"
madnificent Jan 13, 2025
aefa9bb
Merge branch 'feature/support-esm-imports' into feature/dev-experienc…
madnificent Jan 13, 2025
f99fd0b
More just hacks to revert and cleanly push
madnificent Jan 13, 2025
444497e
Always run installation script for now
madnificent Jan 16, 2025
2e65d27
Splitting up dependency installation
madnificent Jan 31, 2025
f6ebaad
Fix docker-rsync so it doesn't glob
madnificent Jan 31, 2025
45d715d
Ensure sources exist in transpile-sources
madnificent Jan 31, 2025
d99206e
Only generate inline sourcemaps
madnificent Feb 10, 2025
898b17a
Clean up dev state
madnificent Feb 10, 2025
a2df321
Fix coffeescript sourcemaps
madnificent Feb 10, 2025
9895f5c
Transpile to same folder as input files
erikap Feb 10, 2025
6c1eaac
Remove intermediate /usr/src/processing folder from build process
erikap Feb 10, 2025
76e4d5d
Use dist instead of build
madnificent Feb 10, 2025
10ec954
Even faster transpilation of sources
madnificent Feb 17, 2025
66c17b8
Extract code to run in production
madnificent Feb 17, 2025
dd5115d
Merge remote-tracking branch 'origin/master' into remove-sparql-clien…
nvdk Mar 31, 2025
715f7fe
add support for scope and sudo options
nvdk Mar 31, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
19 changes: 10 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,30 @@ FROM node:20-bookworm

LABEL maintainer="team@semantic.works"

RUN apt-get update && apt-get -y upgrade && apt-get -y install git openssh-client rsync
RUN apt-get update && apt-get -y upgrade && apt-get -y install git openssh-client rsync jq

ENV MU_SPARQL_ENDPOINT 'http://database:8890/sparql'
ENV MU_APPLICATION_GRAPH 'http://mu.semte.ch/application'
ENV NODE_ENV 'production'
ENV MU_SPARQL_ENDPOINT='http://database:8890/sparql'
ENV MU_APPLICATION_GRAPH='http://mu.semte.ch/application'
ENV NODE_ENV='production'

ENV HOST '0.0.0.0'
ENV PORT '80'
ENV HOST='0.0.0.0'
ENV PORT='80'

ENV LOG_SPARQL_ALL 'true'
ENV DEBUG_AUTH_HEADERS 'true'
ENV LOG_SPARQL_ALL='true'
ENV DEBUG_AUTH_HEADERS='true'

WORKDIR /usr/src/app
COPY package.json /usr/src/app/package.json
COPY ./scripts /app/scripts
RUN npm install
COPY . /usr/src/app
RUN chmod +x /usr/src/app/run-development.sh
RUN chmod +x /usr/src/app/run-production.sh
RUN chmod +x /usr/src/app/build-production.sh

EXPOSE ${PORT}

CMD bash boot.sh
CMD ["bash", "boot.sh"]

# This stuff only runs when building an image from the template
ONBUILD RUN rm -Rf /app/scripts
Expand Down
51 changes: 48 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ mu.app.get('/', function( req, res ) {
```

The following helper functions are provided by the template
- `query(query) => Promise`: Function for sending queries to the triplestore
- `update(query) => Promise`: Function for sending updates to the triplestore
- `query(queryString, options) => Promise`: Function for sending queries to the triplestore. Options is an object which may include `sudo` and `scope` keys.
- `update(query, options) => Promise`: Function for sending updates to the triplestore. Options is an object which may include `sudo` and `scope` keys.
- `uuid() => string`: Generates a random UUID (e.g. to construct new resource URIs)

The following SPARQL escape helpers are provided to construct safe SPARQL query strings
Expand All @@ -217,6 +217,49 @@ The following SPARQL escape helpers are provided to construct safe SPARQL query
- `sparqlEscapeBool(value) => string`: The given value is evaluated to a boolean value in javascript. E.g. the string value `'0'` evaluates to `false` in javascript.
- `sparqlEscape(value, type) => string`: Function to escape a value in SPARQL according to the given type. Type must be one of `'string'`, `'uri'`, `'int'`, `'float'`, `'date'`, `'dateTime'`, `'bool'`.

### Executing queries

#### Example
```js
import { query } from 'mu';

const queryString = `
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?name WHERE {
?person a dbo:Person ;
dbo:birthPlace <http://dbpedia.org/resource/Belgium> ;
dbo:name ?name .
} LIMIT 10
`;

const options = {
scope: "my-scope",
extraHeaders: {
"X-Custom-Header": "value"
},
};

response = await query(queryString, options);
```

#### Parameters
The `query` and `update` helpers accept the same parameters.

| Parameter | Type | Default | Description |
|--------------------------|-----------|----------------------|------------------------------------------------------------------------------------|
| `queryString` | `string` | _Required_ | The SPARQL query to execute. |
| `options` | `object` | `{}` | Optional configuration settings. |
| `options.sudo` | `boolean` | `false` | Whether to enable sudo mode (requires environment permission). |
| `options.scope` | `string` | _(Env default)_ | The authentication scope to use. Defaults to `DEFAULT_MU_AUTH_SCOPE` if available. |

#### Experimental Parameters
| `options.sparqlEndpoint` | `string` | `MU_SPARQL_ENDPOINT` | The SPARQL endpoint to send the request to. |
| `options.extraHeaders` | `object` | `{}` | Additional headers to send in the request. |
| `options.authUser` | `string` | `undefined` | The username for HTTP authentication (optional). |
| `options.authPassword` | `string` | `undefined` | The password for HTTP authentication (optional). |
| `options.authType` | `string` | "digest"` | The type of authentication to use. (`"digest"` or `"basic"`) |


### Error handling
The template offers [an error handler](https://expressjs.com/en/guide/error-handling.html) to send error responses in a JSON:API compliant way. The handler can be imported from `'mu'` and need to be loaded at the end.

Expand All @@ -227,7 +270,7 @@ app.get('/hello', function( req, res, next ) {
try {
...
} catch (e) {
next(new Error('Oops, something went wrong.))
next(new Error('Oops, something went wrong.'))
}
});

Expand All @@ -254,6 +297,8 @@ The following environment variables can be configured:
- `MAX_BODY_SIZE` (default: `100kb`): max size of the request body. See [ExpressJS documentation](https://expressjs.com/en/resources/middleware/body-parser.html#limit).
- `HOST` (default: `0.0.0.0`): The hostname you want the service to bind to.
- `PORT` (default: `80`): The port you want the service to bind to.
- `ALLOW_MU_AUTH_SUDO`: Allow sudo queries when the service requests it.
- `DEFAULT_MU_AUTH_SCOPE`: Default mu-auth-scope to use for calls.


#### Mounting `/config`
Expand Down
8 changes: 5 additions & 3 deletions babel.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
{
"targets": {
"node": 18
}
},
"modules": false
}
],
["@babel/preset-typescript"]
],
"plugins": [
["@babel/plugin-proposal-decorators", { "version": "2023-05" }]
["@babel/plugin-proposal-decorators", { "version": "2023-05" }],
["babel-plugin-add-import-extension"]
],
"ignore": [
"./node_modules",
"/usr/src/processing/build/node_modules"
"/usr/src/dist/node_modules"
]
}
45 changes: 1 addition & 44 deletions boot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,5 @@ then
--exec /usr/src/app/run-development.sh
elif [ "$NODE_ENV" == "production" ]
then
diff -rq /app /app.original > /dev/null
APP_FILES_CHANGED="$?"
diff -rq /config /config.original > /dev/null
CONFIG_FILES_CHANGED="$?"

if [ ! -f /usr/src/build/app.js ]
then
echo "No built sources found. If you mount new sources, please set the NODE_ENV=\"development\" environment variable."
sleep 5;
exit 1;
elif [ $APP_FILES_CHANGED != "0" ]
then
echo "Built sources are not the same as sources available in /app. If you mount new sources, please set the NODE_ENV=\"development\" environment variable."
sleep 5;
exit 1;
elif [ $CONFIG_FILES_CHANGED != "0" ]
then
echo "Rebuilding sources to include /config."

# move new configuration into app for transpilation
if [[ "$(ls -A /config 2> /dev/null)" ]]
then
cp -Rf /config/* /usr/src/app/app/config/
fi

# make a backup of the used configuration so we can detect changes
rm -Rf /config.original
mkdir /config.original
if [[ "$(ls -A /config 2> /dev/null)" ]]
then
cp -Rf /config/* /config.original
fi

# transpile sources
cd /usr/src/app/
./transpile-sources.sh

# boot transpiled sources
cd /usr/src/build/
exec node ./app.js
else
cd /usr/src/build/
exec node ./app.js
fi
/usr/src/app/run-production.sh
fi
9 changes: 2 additions & 7 deletions build-production.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ fi
cp -r /app /app.original

# Install custom packages if need be
if [ -f ./app/package.json ]
then
echo "Running npm install"
cd /usr/src/app/app/
npm install
cd /usr/src/app/
fi
./prepare-package-json.sh
./npm-install-dependencies.sh production

./transpile-sources.sh
2 changes: 1 addition & 1 deletion helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ function docker-rsync() {
# --numeric-ids: use uuid by number instead of by name
# --info: silent output
# --no-compress: no compression algorithm
rsync -aHAWXS --numeric-ids --info= --no-compress $@
rsync -aHAWXS --numeric-ids --info= --no-compress "$@"
}
4 changes: 2 additions & 2 deletions helpers/mu/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { app, errorHandler } from './server';
import sparql from './sparql';
import { app, errorHandler } from './server.js';
import sparql from './sparql.js';
import { v1 as uuidV1 } from 'uuid';

// generates a uuid
Expand Down
9 changes: 9 additions & 0 deletions helpers/mu/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "mu",
"version": "0.0.1",
"description": "Framework layer functions for mu-javascript-template.",
"main": "index.js",
"type": "module",
"author": "Semantic Works",
"license": "MIT"
}
Loading