Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
jlongster committed Jun 28, 2021
0 parents commit 6510646
Show file tree
Hide file tree
Showing 11 changed files with 3,479 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
1 change: 1 addition & 0 deletions babel.config.js
@@ -0,0 +1 @@
module.exports = {presets: ['@babel/preset-env']}
48 changes: 48 additions & 0 deletions backend.js
@@ -0,0 +1,48 @@
let syncifyWorker = new Worker('syncify.js');

function get_string(buf) {
return String.fromCharCode
.apply(null, new Uint16Array(buf))
.replace(/\u0000/g, '');
}

function put_string(buf, str) {
var bufView = new Uint16Array(buf);
for (var i = 0, strLen = str.length; i < strLen; i++) {
Atomics.store(bufView, i, str.charCodeAt(i));
}
return buf;
}

function loadURL(sab, urlSab, url) {
sab[0] = 0;

let view = new Int32Array(sab);
let urlView = new Int32Array(urlSab);

put_string(urlSab, url);
Atomics.notify(urlView, 0, 1);
Atomics.wait(view, 0, 0);

return JSON.parse(get_string(sab));
}

function run() {
// SYNCHRONOUS!!
console.log(
loadURL(sab, urlSab, 'https://jsonplaceholder.typicode.com/todos/1')
);
}

let sab = new SharedArrayBuffer(10000);
let urlSab = new SharedArrayBuffer(10000);
syncifyWorker.postMessage([sab, urlSab]);

syncifyWorker.onmessage = msg => {
switch (msg.data.type) {
case 'syncify-ready':
// run();
let f = self.webkitRequestFileSystemSync(self.PERSISTENT, 1000);
console.log(f);
}
};
Empty file added favicon.ico
Empty file.
5 changes: 5 additions & 0 deletions index.html
@@ -0,0 +1,5 @@
<!DOCTYPE html>

<body>
<script src="main.js"></script>
</body>
3 changes: 3 additions & 0 deletions main.js
@@ -0,0 +1,3 @@
let worker = new Worker('backend.js');

// alert(window.PERSISTENT)
7 changes: 7 additions & 0 deletions package.json
@@ -0,0 +1,7 @@
{
"dependencies": {
"@babel/preset-env": "^7.14.7",
"babel": "^6.23.0",
"jest": "^27.0.5"
}
}
42 changes: 42 additions & 0 deletions syncify.js
@@ -0,0 +1,42 @@
function get_string(buf) {
return String.fromCharCode
.apply(null, new Uint16Array(buf))
.replace(/\u0000/g, '');
}

function put_string(buf, str) {
var bufView = new Uint16Array(buf);
for (var i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}

async function run(url, sab) {
let res = await fetch(url);
let json = await res.json();

put_string(sab, JSON.stringify(json));

let view = new Int32Array(sab);
Atomics.notify(view, 0, 1);
}

async function listen(sab, urlSab) {
let view = new Int32Array(urlSab);

while (1) {
Atomics.wait(view, 0, 0);

let url = get_string(urlSab);
view[0] = 0;
await run(url, sab);
}
}

self.onmessage = msg => {
postMessage({ type: 'syncify-ready' });

let [sab, urlSab] = msg.data;
listen(sab, urlSab);
};
89 changes: 89 additions & 0 deletions virtual-file.js
@@ -0,0 +1,89 @@
function range(start, end, step) {
let r = [];
for (let i = start; i <= end; i += step) {
r.push(i);
}
return r;
}

export function concatChunks(chunks, start, end) {
let buffer = new Buffer(end - start);
let view = new Uint8Array(buffer);

let cursor = 0;
for (let i = 0; i < chunks.length; i++) {
console.log(chunks[i]);
let cstart = 0;
let cend = chunks[i].byteLength;
if (start > chunks[i].pos) {
cstart = start - chunks[i].pos;
}
if (end < chunks[i].pos + chunks[i].data.byteLength) {
cend = end - chunks[i].pos;
}

console.log(cstart, cend);
view.set(new Uint8Array(chunks[i], cstart, cend), cursor);
cursor += cend - cstart;
}
return buffer;
}

class File {
constructor(chunkSize) {
this.chunkSize = chunkSize;
// TODO: make LRU cache
this.cache = new Map();
}

loadMissing(boundaries) {}

updateCache(chunks) {
for (let i = 0; i < chunks.length; i++) {
this.cache.set(chunks[i].pos, chunks[i]);
}
}

getBoundaryIndexes(start, end) {
let startC = start - (start % this.chunkSize);
let endC = end - 1 - ((end - 1) % this.chunkSize);

return range(startC, endC, this.chunkSize);
}

load(start, end) {
let indexes = this.getBoundaryIndexes(start, end);
let status = boundaries.reduce(
(acc, b) => {
let cached = this.chunks.get(b);
if (cached) {
acc.cached.push(cached);
} else {
acc.missing.push(cached);
}
return acc;
},
{ cached: [], missing: [] }
);

let missingChunks = this.loadMissing(status.missing);

let allChunks = status.cached.concat(missingChunks);
allChunks.sort((c1, c2) => {
return c1.pos - c2.pos;
});

this.updateCache(allChunks);
return concatChunks(allChunks, start, end);
}

read(buffer, offset, length, position) {
let readBuffer = this.load(position, position + length);
let view = new Uint8Array(buffer);
toView.set(new Uint8Array(readBuffer), offset);
// TODO: need to check end of file
return length;
}

write(buffer, offset, length, position) {}
}
16 changes: 16 additions & 0 deletions virtual-file.test.js
@@ -0,0 +1,16 @@
import { concatChunks } from './virtual-file';

function makeChunks(chunkSize, data) {
let arr = [];
for (let i = 0; i < data.length; i += chunkSize) {
arr.push({ pos: i, data: Int8Array.from(data.slice(i, i + chunkSize)) });
}
return arr;
}

describe('concatChunks', () => {
test('works', () => {
let chunks = makeChunks(3, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
expect(Array.from(concatChunks(chunks, 1, 7))).toBe([]);
});
});

0 comments on commit 6510646

Please sign in to comment.