Skip to content

Commit fa606c2

Browse files
authored
Merge pull request #41 from oss-slu/issue-33
Added changes for issue 33
2 parents 2fc46cc + 0c6842e commit fa606c2

7 files changed

Lines changed: 103 additions & 3 deletions

File tree

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,27 @@ Stores important bits of knowledge in structured JSON-LD objects:
2626
1. **Open and Free**—expose all contributions immediately without charge to write or read;
2727
1. **Attributed and Versioned**—always include asserted ownership and transaction metadata so consumers can evaluate trustworthiness and relevance.
2828

29+
### Programmatic usage
30+
This project exposes a single public entry point at the package root (`index.js`). Only a few
31+
functions are exported – everything else lives in internal modules and is intentionally
32+
kept private. Example:
33+
34+
```js
35+
import { app, createServer, start } from 'rerum_server'
36+
37+
// `app` is the configured Express application; you can pass it to Supertest or reuse it
38+
// inside another HTTP stack.
39+
40+
const server = createServer(8080) // returns a http.Server but does not listen
41+
server.listen()
42+
43+
// or simply
44+
start(8080) // convenience helper that both creates and listens
45+
```
46+
47+
Consumers no longer need to reach into `./app.js` or other deep paths – if it isn't
48+
exported here it isn't part of the stable API.
49+
2950
## What we add
3051
You will find a `__rerum` property on anything you read from this repository. This is written onto
3152
all objects by the server and is not editable by the client applications. While applications may assert

__tests__/public_api.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { app, createServer, start } from '../index.js'
2+
3+
describe('public API entry point', () => {
4+
test('exports an express app instance', () => {
5+
expect(app).toBeDefined()
6+
expect(typeof app.use).toBe('function') // express app
7+
})
8+
9+
test('createServer returns a http.Server', () => {
10+
const server = createServer(0) // port 0 for ephemeral
11+
expect(server).toBeDefined()
12+
expect(typeof server.listen).toBe('function')
13+
server.close()
14+
})
15+
16+
test('start starts the server and returns it', (done) => {
17+
const server = start(0)
18+
server.on('listening', () => {
19+
server.close(() => done())
20+
})
21+
})
22+
})

__tests__/routes_mounted.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
import request from "supertest"
99
import api_routes from "../routes/api-routes.js"
10-
import app from "../app.js"
10+
// leverage the public entry point instead of a deep path
11+
import app from "../index.js"
1112
import fs from "fs"
1213

1314
let app_stack = app.router.stack

index.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import http from 'http'
2+
import app from './app.js'
3+
4+
/**
5+
* Express application instance used throughout the project. Exported
6+
* primarily for testing or embedding inside another server.
7+
*
8+
* ```js
9+
* import { app } from 'rerum_server'
10+
* ```
11+
*/
12+
export { app }
13+
14+
/**
15+
* Default export is the express app largely for backwards compatibility
16+
* with consumers that do `import app from 'rerum_server'`.
17+
*/
18+
export default app
19+
20+
/**
21+
* Helper that creates an HTTP server for the configured express app.
22+
* The returned server is **not** listening yet; caller may attach
23+
* additional listeners or configure timeouts before calling
24+
* `server.listen(...)`.
25+
*
26+
* @param {number|string} [port=process.env.PORT||3001] port to assign to
27+
* the express app and eventually listen on
28+
* @returns {import('http').Server} http server instance
29+
*/
30+
export function createServer(port = process.env.PORT ?? 3001) {
31+
app.set('port', port)
32+
const server = http.createServer(app)
33+
34+
server.keepAliveTimeout = 8 * 1000
35+
server.headersTimeout = 8.5 * 1000
36+
37+
return server
38+
}
39+
40+
/**
41+
* Convenience function to start the server immediately. Returns the
42+
* server instance so callers can close it in tests or hook events.
43+
*
44+
* @param {number|string} [port] optional port override
45+
* @returns {import('http').Server}
46+
*/
47+
export function start(port) {
48+
const p = port ?? process.env.PORT ?? 3001
49+
const server = createServer(p)
50+
server.listen(p)
51+
server.on('listening', () => {
52+
console.log('LISTENING ON ' + p)
53+
})
54+
return server
55+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"node": ">=24.12.0",
2424
"npm": ">=11.7.0"
2525
},
26+
"main": "index.js",
2627
"scripts": {
2728
"start": "node ./bin/rerum_v1.js",
2829
"test": "jest",

routes/__tests__/crud_routes_function.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import request from 'supertest'
1010
//Fun fact, if you don't require app, you don't get coverage even though the tests run just fine.
11-
import app from '../../app.js'
11+
import app from '../../index.js' // use public API instead of deep path
1212
//This is so we can do Mongo specific things with the objects in this test, like actually remove them from the db.
1313
import controller from '../../db-controller.js'
1414

routes/__tests__/overwrite.test.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import request from 'supertest'
2-
import app from '../../app.js'
2+
import app from '../../index.js' // public entry point
33
import { jest } from '@jest/globals'
44

55
// Mock the database and auth modules

0 commit comments

Comments
 (0)