presidium-db-client-server 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ArrayEventEmitter.js +21 -0
- package/AsyncIterator.js +38 -0
- package/BinaryEntriesStream.js +75 -0
- package/BinaryStream.js +54 -0
- package/DatabaseHandler.js +292 -0
- package/DatabaseServer.js +101 -0
- package/DiskHashTableClient.js +224 -0
- package/DiskSortedHashTableClient.js +321 -0
- package/README.md +54 -2
- package/TextEntriesStream.js +73 -0
- package/TextStream.js +55 -0
- package/buffersToEntry.js +44 -0
- package/handleServerResponse.js +72 -0
- package/package.json +45 -4
package/TextStream.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Presidium DB Client and Server may be freely distributed under the CFOSS license.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const EventEmitter = require("events")
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @name TextStream
|
|
9
|
+
*
|
|
10
|
+
* @docs
|
|
11
|
+
* ```coffeescript [specscript]
|
|
12
|
+
* module stream 'https://nodejs.org/docs/latest-v26.x/api/stream.html'
|
|
13
|
+
* module events 'https://nodejs.org/docs/latest-v26.x/api/events.html'
|
|
14
|
+
*
|
|
15
|
+
* TextStream(readable stream.Readable, delimiter string) -> textStream events.EventEmitter
|
|
16
|
+
*
|
|
17
|
+
* textStream.on("data", (text string, encoding string) -> ())
|
|
18
|
+
*
|
|
19
|
+
* textStream.on("end", () -> ())
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
class TextStream extends EventEmitter {
|
|
23
|
+
constructor(readable, delimiter, encoding) {
|
|
24
|
+
super()
|
|
25
|
+
|
|
26
|
+
delimiter ??= ""
|
|
27
|
+
encoding ??= "utf8"
|
|
28
|
+
|
|
29
|
+
let s = ""
|
|
30
|
+
readable.on('data', (chunk, encoding) => {
|
|
31
|
+
if (typeof chunk == 'string') {
|
|
32
|
+
s += Buffer.from(chunk, encoding).toString(encoding)
|
|
33
|
+
} else {
|
|
34
|
+
s += chunk.toString(encoding)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
let delimiterIndex = s.indexOf(delimiter)
|
|
38
|
+
while (delimiterIndex > -1) {
|
|
39
|
+
const text = s.slice(0, delimiterIndex)
|
|
40
|
+
this.emit("data", text, encoding)
|
|
41
|
+
s = s.slice(delimiterIndex + 1)
|
|
42
|
+
delimiterIndex = s.indexOf(delimiter)
|
|
43
|
+
}
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
readable.on('end', () => {
|
|
47
|
+
if (s.length > 0) {
|
|
48
|
+
this.emit("data", s, encoding)
|
|
49
|
+
}
|
|
50
|
+
this.emit("end")
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
module.exports = TextStream
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Presidium DB Client and Server may be freely distributed under the CFOSS license.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @name buffersToEntry
|
|
7
|
+
*
|
|
8
|
+
* @docs
|
|
9
|
+
* ```specscript
|
|
10
|
+
* buffersToEntry(
|
|
11
|
+
* buffers Array<Buffer>,
|
|
12
|
+
* entryLength number,
|
|
13
|
+
* encoding string,
|
|
14
|
+
* valueType string,
|
|
15
|
+
* ) -> Array<string, Buffer>
|
|
16
|
+
*
|
|
17
|
+
* buffersToEntry(
|
|
18
|
+
* buffers Array<Buffer>,
|
|
19
|
+
* entryLength number,
|
|
20
|
+
* encoding string,
|
|
21
|
+
* valueType string,
|
|
22
|
+
* sortValueType string
|
|
23
|
+
* ) -> Array<string, Buffer>
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
function buffersToEntry(buffers, entryLength, encoding, valueType, sortValueType) {
|
|
27
|
+
if (entryLength == 3) {
|
|
28
|
+
const key = buffers[0].toString(encoding)
|
|
29
|
+
const value = valueType == "string"
|
|
30
|
+
? buffers[1].toString(encoding)
|
|
31
|
+
: buffers[1]
|
|
32
|
+
const sortValue = sortValueType == "number"
|
|
33
|
+
? Number(buffers[2].toString(encoding))
|
|
34
|
+
: buffers[2].toString(encoding)
|
|
35
|
+
return [key, value, sortValue]
|
|
36
|
+
}
|
|
37
|
+
const key = buffers[0].toString(encoding)
|
|
38
|
+
const value = valueType == "string"
|
|
39
|
+
? buffers[1].toString(encoding)
|
|
40
|
+
: buffers[1]
|
|
41
|
+
return [key, value]
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
module.exports = buffersToEntry
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Presidium DB Client and Server may be freely distributed under the CFOSS license.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const Readable = require("presidium/Readable")
|
|
6
|
+
const TextStream = require("./TextStream")
|
|
7
|
+
const BinaryStream = require("./BinaryStream")
|
|
8
|
+
const TextEntriesStream = require("./TextEntriesStream")
|
|
9
|
+
const BinaryEntriesStream = require("./BinaryEntriesStream")
|
|
10
|
+
const AsyncIterator = require("./AsyncIterator")
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @name handleServerResponse
|
|
14
|
+
*
|
|
15
|
+
* @docs
|
|
16
|
+
* ```specscript
|
|
17
|
+
* module http 'https://nodejs.org/docs/latest-v26.x/api/http.html'
|
|
18
|
+
* module events 'https://nodejs.org/docs/latest-v26.x/api/events.html'
|
|
19
|
+
*
|
|
20
|
+
* handleServerResponse(response http.ServerResponse, options {
|
|
21
|
+
* textStream: boolean,
|
|
22
|
+
* binaryStream: boolean,
|
|
23
|
+
* textEntriesStream: boolean,
|
|
24
|
+
* binaryEntriesStream: boolean,
|
|
25
|
+
* delimiter: string,
|
|
26
|
+
* entryLength: number,
|
|
27
|
+
* }) -> data object|events.EventEmitter
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
async function handleServerResponse(response, options = {}) {
|
|
31
|
+
const {
|
|
32
|
+
textStream,
|
|
33
|
+
binaryStream,
|
|
34
|
+
textEntriesStream,
|
|
35
|
+
binaryEntriesStream,
|
|
36
|
+
delimiter = "",
|
|
37
|
+
entryLength,
|
|
38
|
+
} = options
|
|
39
|
+
|
|
40
|
+
const encoding = response.headers["content-encoding"] ?? "utf8"
|
|
41
|
+
|
|
42
|
+
if (response.ok) {
|
|
43
|
+
if (textStream) {
|
|
44
|
+
const stream = new TextStream(response, delimiter, encoding)
|
|
45
|
+
return AsyncIterator.fromEvents(stream)
|
|
46
|
+
}
|
|
47
|
+
if (binaryStream) {
|
|
48
|
+
const stream = new BinaryStream(response, delimiter)
|
|
49
|
+
return AsyncIterator.fromEvents(stream)
|
|
50
|
+
}
|
|
51
|
+
if (textEntriesStream) {
|
|
52
|
+
const stream = new TextEntriesStream(response, entryLength, delimiter, encoding)
|
|
53
|
+
return AsyncIterator.fromEvents(stream, "entry")
|
|
54
|
+
}
|
|
55
|
+
if (binaryEntriesStream) {
|
|
56
|
+
const stream = new BinaryEntriesStream(response, entryLength, delimiter, encoding)
|
|
57
|
+
return AsyncIterator.fromEvents(stream, "entry")
|
|
58
|
+
}
|
|
59
|
+
if (response.headers["content-type"] == "application/json") {
|
|
60
|
+
return Readable.JSON(response)
|
|
61
|
+
}
|
|
62
|
+
if (response.headers["content-type"] == "application/octet-stream") {
|
|
63
|
+
return Readable.Buffer(response)
|
|
64
|
+
}
|
|
65
|
+
return Readable.Text(response)
|
|
66
|
+
}
|
|
67
|
+
const message = await Readable.Text(response)
|
|
68
|
+
const error = new Error(message)
|
|
69
|
+
throw error
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
module.exports = handleServerResponse
|
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "presidium-db-client-server",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Presidium DB client and server library",
|
|
5
|
+
"author": "Richard Tong",
|
|
6
|
+
"license": "CFOSS",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "github:richytong/presidium-db-client-server"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/richytong/presidium-db-client-server",
|
|
12
|
+
"files": [
|
|
13
|
+
"ArrayEventEmitter.js",
|
|
14
|
+
"AsyncIterator.js",
|
|
15
|
+
"BinaryEntriesStream.js",
|
|
16
|
+
"BinaryStream.js",
|
|
17
|
+
"DatabaseHandler.js",
|
|
18
|
+
"DatabaseServer.js",
|
|
19
|
+
"DiskHashTableClient.js",
|
|
20
|
+
"DiskSortedHashTableClient.js",
|
|
21
|
+
"TextEntriesStream.js",
|
|
22
|
+
"TextStream.js",
|
|
23
|
+
"buffersToEntry.js",
|
|
24
|
+
"handleServerResponse.js"
|
|
25
|
+
],
|
|
26
|
+
"keywords": [
|
|
27
|
+
"presidium",
|
|
28
|
+
"db",
|
|
29
|
+
"database",
|
|
30
|
+
"client",
|
|
31
|
+
"server",
|
|
32
|
+
"key-value",
|
|
33
|
+
"node",
|
|
34
|
+
"nodejs",
|
|
35
|
+
"javascript"
|
|
36
|
+
],
|
|
37
|
+
"scripts": {
|
|
38
|
+
"test": "node test.js"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"presidium": "^4.5.0",
|
|
42
|
+
"rubico": "^2.20.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"codecov": "^3.8.2"
|
|
46
|
+
}
|
|
47
|
+
}
|