Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6510646
Showing
11 changed files
with
3,479 additions
and
0 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 @@ | ||
node_modules |
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 @@ | ||
module.exports = {presets: ['@babel/preset-env']} |
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,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.
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 @@ | ||
<!DOCTYPE html> | ||
|
||
<body> | ||
<script src="main.js"></script> | ||
</body> |
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,3 @@ | ||
let worker = new Worker('backend.js'); | ||
|
||
// alert(window.PERSISTENT) |
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,7 @@ | ||
{ | ||
"dependencies": { | ||
"@babel/preset-env": "^7.14.7", | ||
"babel": "^6.23.0", | ||
"jest": "^27.0.5" | ||
} | ||
} |
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,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); | ||
}; |
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,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) {} | ||
} |
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,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([]); | ||
}); | ||
}); |
Oops, something went wrong.