-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from SimplyEdit/sharedArrayBuffer
Shared array buffer implementation
- Loading branch information
Showing
23 changed files
with
8,494 additions
and
520 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export default { | ||
addPerson: (dataspace, command, request, meta) => { | ||
dataspace.people.push(command.value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { v4 as uuid } from 'uuid' | ||
import JSONTag from '@muze-nl/jsontag' | ||
|
||
// run command to localhost:3000 | ||
|
||
let id = uuid() | ||
let commandStr = `{ | ||
"id": "${id}", | ||
"name": "addPerson", | ||
"value": { | ||
"name": "Some Stormtrooper", | ||
"gender": "male", | ||
"homeworld": <link>"http://swapi.co/api/planets/1/" | ||
} | ||
}` | ||
|
||
|
||
async function main() { | ||
let response = await fetch('http://localhost:3000/command', { | ||
method: 'POST', | ||
headers: { | ||
'Accept': 'application/jsontag', | ||
'Content-Type': 'application/jsontag' | ||
}, | ||
body: commandStr | ||
}) | ||
if (!response.ok) { | ||
let text = await response.text() | ||
console.error(response.status+': '+response.statusText, text) | ||
} else { | ||
let data = await response.json() | ||
console.log('response:', data) | ||
} | ||
} | ||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,7 @@ | ||
import SimplyStore from '@muze-nl/simplystore' | ||
import fs from 'fs' | ||
import JSONTag from '@muze-nl/jsontag' | ||
|
||
let str = fs.readFileSync(process.cwd()+'/data.jsontag','utf-8') | ||
const data = JSONTag.parse(str) | ||
import SimplyStore from '../src/server.mjs' | ||
|
||
SimplyStore.run({ | ||
dataspace: data | ||
}) | ||
|
||
datafile: process.cwd()+'/data.jsontag', | ||
commandsFile: process.cwd()+'/commands.mjs', | ||
commandLog: process.cwd()+'/command-log.jsontag', | ||
}) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import JSONTag from '@muze-nl/jsontag' | ||
import fastStringify from '../src/fastStringify.mjs' | ||
import fs from 'node:fs' | ||
|
||
if (process.argv.length<=3) { | ||
console.log('usage: node ./convert.mjs {inputfile} {outputfile}') | ||
process.exit() | ||
} | ||
|
||
// parse command line | ||
let inputFile = process.argv[2] | ||
let outputFile = process.argv[3] | ||
|
||
// load file | ||
let input = fs.readFileSync(inputFile, 'utf-8') | ||
|
||
// parse jsontag | ||
let data = JSONTag.parse(input) | ||
|
||
// write resultset to output | ||
let strData = fastStringify(data) | ||
|
||
fs.writeFileSync(outputFile, strData) | ||
|
||
console.log('Converted data written to ',outputFile) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import JSONTag from '@muze-nl/jsontag' | ||
import {source} from './symbols.mjs' | ||
import fastParse from './fastParse.mjs' | ||
import {stringToSAB,resultSetStringify} from './fastStringify.mjs' | ||
import writeFileAtomic from 'write-file-atomic' | ||
|
||
let commands = {} | ||
let resultSet = [] | ||
let dataspace | ||
let datafile | ||
let meta = {} | ||
let metaProxy = { | ||
index: { | ||
} | ||
} | ||
|
||
export const metaIdProxy = { | ||
forEach: (callback) => { | ||
meta.index.id.forEach((ref,id) => { | ||
callback({ | ||
deref: () => { | ||
return resultSet[ref] | ||
} | ||
},id) | ||
}) | ||
}, | ||
set: (id,ref) => { | ||
//FICME: is this correct? | ||
meta.index.id.set(id, resultSet.length-1) | ||
}, | ||
get: (id) => { | ||
let index = meta.index.id.get(id) | ||
if (index || index===0) { | ||
return { | ||
deref: () => { | ||
return resultSet[index] | ||
} | ||
} | ||
} | ||
}, | ||
has: (id) => { | ||
return meta.index.id.has(id) | ||
} | ||
} | ||
|
||
export const FastJSONTag = { | ||
getType: (obj) => JSONTag.getType(obj[source]), | ||
getAttribute: (obj, attr) => JSONTag.getAttribute(obj[source],attr), | ||
setAttribute: (obj, attr, value) => JSONTag.setAttribute(obj[source], attr, value), | ||
getAttributes: (obj) => JSONTag.getAttributes(obj[source]), | ||
getAttributeString: (obj) => JSONTag.getAttributesString(obj[source]), | ||
getTypeString: (obj) => JSONTag.getTypeString(obj[source]) | ||
} | ||
|
||
export async function initialize(task) { | ||
resultSet = fastParse(task.data, task.meta, false) // false means mutable | ||
dataspace = resultSet[0] | ||
meta = task.meta | ||
metaProxy.index.id = metaIdProxy | ||
datafile = task.datafile | ||
commands = await import(task.commandsFile).then(mod => { | ||
return mod.default | ||
}) | ||
} | ||
|
||
export default async function runCommand(commandStr, request) { | ||
let task = JSONTag.parse(commandStr, null, metaProxy) | ||
if (!task.id) { throw new Error('missing command id')} | ||
if (!task.name) { throw new Error('missing command name parameter')} | ||
let response = { | ||
jsontag: true | ||
} | ||
if (commands[task.name]) { | ||
try { | ||
commands[task.name](dataspace, task, request, metaProxy) | ||
FastJSONTag.setAttribute(dataspace, 'command', task.id) | ||
|
||
const strData = resultSetStringify(resultSet) | ||
const uint8sab = stringToSAB(strData) | ||
response.data = uint8sab | ||
response.meta = { | ||
index: { | ||
id: meta.index.id | ||
} | ||
} | ||
|
||
await writeFileAtomic(datafile, uint8sab) | ||
} catch(err) { | ||
console.error('error',err) | ||
response.code = 422; | ||
response.body = '<object class="Error">{"message":'+JSON.stringify(''+err)+',"code":422}' | ||
} | ||
} else { | ||
console.error('Command not found', task.name, commands) | ||
response.code = 404 | ||
response.body = '<object class="Error">{"message":"Command '+task.name+' not found","code":404}' | ||
} | ||
return response | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { parentPort } from 'node:worker_threads' | ||
import runCommand, { initialize } from '../src/command-worker-module.mjs' | ||
|
||
parentPort.on('message', async data => { | ||
let result | ||
try { | ||
await initialize(data) | ||
result = await runCommand(data.command) | ||
} catch(err) { | ||
result = { error: err.message } | ||
} | ||
parentPort.postMessage(result) | ||
}) |
Oops, something went wrong.