Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
fergiemcdowall committed Aug 29, 2024
1 parent dae6d6c commit b2fe25e
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 151 deletions.
9 changes: 5 additions & 4 deletions bin/norch
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
#!/usr/bin/env node

import { Norch } from '../src/Norch.js'
import { Command } from 'commander'
import { readFileSync } from 'fs'
import { Norch } from '../src/Norch.js'
import { readFileSync } from 'node:fs'

const program = new Command()

const configFile = JSON.parse(readFileSync('./src/config.json', 'utf8'))
const version = JSON.parse(readFileSync('package.json', 'utf8')).version

program
.version(version)
.option('-p, --port <port>', 'specify the port', Number, 3030)
.option('-p, --port <port>', 'specify the port', Number, configFile.port)
.option(
'-n, --name <name>',
'specify the name/location of the index',
String,
'norch-data'
configFile.name
)
.parse(process.argv)

Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
"commander": "^7.1.0",
"levelout": "^1.0.0",
"mime": "^2.5.2",
"search-index": "5.1.0"
"search-index": "5.1.3",
"server-destroy": "^1.0.1"
},
"devDependencies": {
"level-out": "^1.0.1",
"server-destroy": "^1.0.1",
"standard": "^17.0.0",
"stopword": "^2.0.5",
"swagger-jsdoc": "^6.2.5",
Expand Down
14 changes: 12 additions & 2 deletions src/API.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
export class API {
constructor(index, sendResponse) {
constructor (index, sendResponse, events) {
this.index = index
this.sendResponse = sendResponse
this.events = events
this.ready = false
events.on('ready', () => (this.ready = true))
}

params = (req, name) =>
Expand Down Expand Up @@ -351,7 +354,7 @@ export class API {
* $ref: '#/components/schemas/Document'
* responses:
* 200:
* description: Successfully imported
* description: Successfully written
*/
PUT_RAW = (req, res) => {
let body = ''
Expand Down Expand Up @@ -521,4 +524,11 @@ export class API {
res
)
)

READY = (req, res) =>
this.ready
? this.sendJSONResponse({ READY: true }, res)
: this.events.on('ready', () =>
this.sendJSONResponse({ READY: true }, res)
)
}
26 changes: 18 additions & 8 deletions src/Norch.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import EventEmitter from 'events'
import enableDestroy from 'server-destroy'
import mime from 'mime' // commonjs module: can't do named export for 'getType'
import { API } from './API.js'
import { SearchIndex } from 'search-index'
Expand All @@ -7,21 +9,28 @@ import { readdirSync, readFileSync } from 'node:fs'
import { resolve, dirname } from 'path'

export class Norch {
constructor(ops = {}) {
this.index = new SearchIndex(ops)
constructor (ops = {}) {
const configFile = JSON.parse(readFileSync('./src/config.json', 'utf8'))
this.options = Object.assign(configFile, ops)
this.index = new SearchIndex(this.options)
this.events = new EventEmitter()
this.index.EVENTS.on('ready', () => {
this.splash(this.index, ops)
this.splash(this.index, this.options.port)
.then(() =>
this.createNorchServer(new API(this.index, this.sendResponse))
this.createNorchServer(
new API(this.index, this.sendResponse, this.events)
)
)
.then(server => {
server.listen(ops.port || 3030)
return server
server.listen(this.options.port)
enableDestroy(server)
this.server = server
this.events.emit('ready')
})
})
}

splash = (index, ops) =>
splash = (index, port) =>
Promise.all([
index.LAST_UPDATED(),
index.CREATED(),
Expand All @@ -48,7 +57,7 @@ export class Norch {
index contains \x1b[1m${res[2]}\x1b[0m documents
created \x1b[1m${res[1]}\x1b[0m
last updated \x1b[1m${res[0]}\x1b[0m
listening on port \x1b[1m${ops.port}\x1b[0m
listening on port \x1b[1m${port}\x1b[0m
`
)
)
Expand All @@ -72,6 +81,7 @@ export class Norch {

// Serve up static files files
if (this.files(fileDirs).includes(pathname)) {
console.log('BOOOOOOM')
console.log(dirname(fileURLToPath(import.meta.url)))
return this.sendFileResponse(pathname, res)
}
Expand Down
4 changes: 4 additions & 0 deletions src/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"port": 3030,
"name": "norch-data"
}
126 changes: 67 additions & 59 deletions test/EXPORT-IMPORT-test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
const enableDestroy = require('server-destroy')
const filename = process.env.SANDBOX + '/' + __filename.split('/').pop()
const fs = require('fs/promises')
const norch = require('../')
const sw = require('stopword')
const test = require('tape')

test(__filename, t => {
import sw from 'stopword'
import test from 'tape'
import { Norch } from '../src/Norch.js'
import { writeFile, readFile } from 'fs/promises'

const filename = process.env.SANDBOX + '/IMPORT-EXPORT-test'

test(filename, t => {
t.end()
})

test('start a norch PUT and EXPORT contents', async t => {
const nrch = await norch({
index: filename,
stopwords: sw.eng
const nrch = new Norch({
name: filename,
stopwords: sw.eng,
storeVectors: false
})

enableDestroy(nrch)
await new Promise(resolve =>
nrch.events.on('ready', () => {
t.ok('server is ready')
resolve()
})
)

await fetch('http://localhost:3030/PUT', {
method: 'POST',
Expand All @@ -42,30 +48,28 @@ test('start a norch PUT and EXPORT contents', async t => {
await fetch('http://localhost:3030/SEARCH?STRING=interesting document')
.then(res => res.json())
.then(json =>
t.isEquivalent(
{
RESULT: [
{
_id: 'one',
_match: [
{ FIELD: 'content', VALUE: 'document', SCORE: '1.00' },
{ FIELD: 'content', VALUE: 'interesting', SCORE: '1.00' }
],
_score: 2.2
}
],
RESULT_LENGTH: 1
},
json
)
t.isEquivalent(json, {
PAGING: { NUMBER: 0, SIZE: 20, TOTAL: 1, DOC_OFFSET: 0 },
RESULT: [
{
_id: 'one',
_match: [
{ FIELD: 'content', VALUE: 'document', SCORE: '1.00' },
{ FIELD: 'content', VALUE: 'interesting', SCORE: '1.00' }
],
_score: 2.2
}
],
RESULT_LENGTH: 1
})
)
.catch(t.error)

await fetch('http://localhost:3030/EXPORT')
.then(res => res.json())
.then(async json => {
t.isEquivalent(json.slice(0, -2), [
{ key: ['CREATED_WITH'], value: 'search-index@4.0.0' },
{ key: ['CREATED_WITH'], value: 'search-index@5.1.2' },
{ key: ['DOCUMENT_COUNT'], value: 2 },
{
key: ['DOC_RAW', 'one'],
Expand All @@ -83,22 +87,28 @@ test('start a norch PUT and EXPORT contents', async t => {
},
{ key: ['IDX', 'content', ['interesting', '1.00']], value: ['one'] }
])

return json
})
.then(json => fs.writeFile(filename + '-export.json', JSON.stringify(json)))
.then(json => writeFile(filename + '-export.json', JSON.stringify(json)))
.catch(t.error)

nrch.destroy()
nrch.server.destroy()
})

test('start another norch and IMPORT', async t => {
const nrch = await norch({
index: filename + '-2',
const nrch = new Norch({
name: filename + '-2',
port: 3031,
stopwords: sw.eng
})

enableDestroy(nrch)
await new Promise(resolve =>
nrch.events.on('ready', () => {
t.ok('server is ready')
resolve()
})
)

await fetch('http://localhost:3030/SEARCH?STRING=interesting document').catch(
() => t.pass('correct- this index is no longer available')
Expand All @@ -107,42 +117,40 @@ test('start another norch and IMPORT', async t => {
await fetch('http://localhost:3031/SEARCH?STRING=interesting document')
.then(res => res.json())
.then(json =>
t.isEquivalent(
{
RESULT: [],
RESULT_LENGTH: 0
},
json
)
t.isEquivalent(json, {
RESULT: [],
RESULT_LENGTH: 0,
PAGING: { NUMBER: 0, SIZE: 20, TOTAL: 0, DOC_OFFSET: 0 }
})
)
.catch(t.error)

await fetch('http://localhost:3031/IMPORT', {
method: 'POST',
body: await fs.readFile(filename + '-export.json')
body: await readFile(filename + '-export.json')
})

await new Promise(resolve => setTimeout(resolve, 1000))

await fetch('http://localhost:3031/SEARCH?STRING=interesting document')
.then(res => res.json())
.then(json =>
t.isEquivalent(
{
RESULT: [
{
_id: 'one',
_match: [
{ FIELD: 'content', VALUE: 'document', SCORE: '1.00' },
{ FIELD: 'content', VALUE: 'interesting', SCORE: '1.00' }
],
_score: 2.2
}
],
RESULT_LENGTH: 1
},
json
)
t.isEquivalent(json, {
RESULT: [
{
_id: 'one',
_match: [
{ FIELD: 'content', VALUE: 'document', SCORE: '1.00' },
{ FIELD: 'content', VALUE: 'interesting', SCORE: '1.00' }
],
_score: 2.2
}
],
RESULT_LENGTH: 1,
PAGING: { NUMBER: 0, SIZE: 20, TOTAL: 1, DOC_OFFSET: 0 }
})
)
.catch(t.error)

nrch.destroy()
nrch.server.destroy()
})
Loading

0 comments on commit b2fe25e

Please sign in to comment.