72 lines
1.6 KiB
JavaScript
72 lines
1.6 KiB
JavaScript
function createCSVScanner(onRow, cols) {
|
|
let buffer = '';
|
|
|
|
return function scanChunk(chunk) {
|
|
buffer += chunk;
|
|
const lines = buffer.split('\n');
|
|
buffer = lines.pop(); // Save incomplete line
|
|
|
|
for (const line of lines)
|
|
{
|
|
const fields = [];
|
|
let start = 0;
|
|
let col = 0;
|
|
for (let i = 0; i < line.length; i++) {
|
|
if (line[i] === ',' && col < cols-1) {
|
|
fields.push(line.slice(start, i));
|
|
start = i + 1;
|
|
col++;
|
|
}
|
|
}
|
|
fields.push(line.slice(start, line.length-1));
|
|
onRow(...fields);
|
|
}
|
|
|
|
};
|
|
}
|
|
|
|
async function DB(url, cols, onRow) {
|
|
const response = await fetch(url);
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
}
|
|
|
|
const reader = response.body.getReader();
|
|
const decoder = new TextDecoder("utf-8");
|
|
|
|
const scanner = createCSVScanner(onRow, cols);
|
|
|
|
while (true)
|
|
{
|
|
const { done, value } = await reader.read();
|
|
if (done) break;
|
|
const chunk = decoder.decode(value, { stream: true });
|
|
scanner(chunk);
|
|
}
|
|
}
|
|
|
|
|
|
const instances = {};
|
|
/** @import DBTypes from "./types.d.ts" */
|
|
/** @type {DBTypes.Builder} */
|
|
export default function(params)
|
|
{
|
|
const obj = {
|
|
list:{},
|
|
load(){
|
|
let index = 0;
|
|
this.list = {};
|
|
return DB(import.meta.resolve(params.path), params.cols, (...args)=>{
|
|
const [id, data] = params.load(index, ...args);
|
|
id && (this.list[id] = data);
|
|
index++;
|
|
});
|
|
},
|
|
find(id){return this.list[id];},
|
|
save(){},
|
|
};
|
|
|
|
return obj;
|
|
}
|