Skip to content

Commit 7c5e57e

Browse files
committed
fix waosm compressor
1 parent b2f364f commit 7c5e57e

File tree

8 files changed

+312
-399
lines changed

8 files changed

+312
-399
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "wao",
3-
"version": "0.12.4",
3+
"version": "0.12.5",
44
"bin": "./src/cli.js",
55
"type": "module",
66
"main": "dist/cjs/index.js",

src/waosm-node.js

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,9 @@
11
export * from "./waosm-node/waosm_bg.js"
22
import * as wasmBindings from "./waosm-node/waosm_bg.js"
33
import { __wbg_set_wasm } from "./waosm-node/waosm_bg.js"
4-
import { readFileSync } from "fs"
5-
import { resolve } from "path"
6-
7-
const run = dirname => {
8-
const wasm = readFileSync(resolve(dirname, "./waosm-node/waosm_bg.wasm"))
9-
const wasmModule = new WebAssembly.Module(wasm)
10-
const wasmInstance = new WebAssembly.Instance(wasmModule, {
11-
"./waosm_bg.js": wasmBindings,
12-
})
13-
__wbg_set_wasm(wasmInstance.exports)
14-
}
15-
16-
let dirname
17-
if (typeof __filename !== "undefined" && typeof __dirname !== "undefined")
18-
run(__dirname)
19-
else {
20-
import("./dirname.js")
21-
.then(({ default: dirname }) => run(dirname))
22-
.catch(e => {
23-
console.log(e)
24-
})
25-
}
4+
import wasmBinary from "./waosm-node/waosm_bg.wasm.js"
5+
const wasmModule = new WebAssembly.Module(wasmBinary)
6+
const wasmInstance = new WebAssembly.Instance(wasmModule, {
7+
"./waosm_bg.js": wasmBindings,
8+
})
9+
__wbg_set_wasm(wasmInstance.exports)

src/waosm-node/waosm_bg.js

Lines changed: 97 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,167 +1,147 @@
1-
let wasm
1+
let wasm;
22
export function __wbg_set_wasm(val) {
3-
wasm = val
3+
wasm = val;
44
}
55

6-
const heap = new Array(128).fill(undefined)
76

8-
heap.push(undefined, null, true, false)
7+
const heap = new Array(128).fill(undefined);
98

10-
function getObject(idx) {
11-
return heap[idx]
12-
}
9+
heap.push(undefined, null, true, false);
10+
11+
function getObject(idx) { return heap[idx]; }
1312

14-
let heap_next = heap.length
13+
let heap_next = heap.length;
1514

1615
function addHeapObject(obj) {
17-
if (heap_next === heap.length) heap.push(heap.length + 1)
18-
const idx = heap_next
19-
heap_next = heap[idx]
16+
if (heap_next === heap.length) heap.push(heap.length + 1);
17+
const idx = heap_next;
18+
heap_next = heap[idx];
2019

21-
heap[idx] = obj
22-
return idx
20+
heap[idx] = obj;
21+
return idx;
2322
}
2423

2524
function dropObject(idx) {
26-
if (idx < 132) return
27-
heap[idx] = heap_next
28-
heap_next = idx
25+
if (idx < 132) return;
26+
heap[idx] = heap_next;
27+
heap_next = idx;
2928
}
3029

3130
function takeObject(idx) {
32-
const ret = getObject(idx)
33-
dropObject(idx)
34-
return ret
31+
const ret = getObject(idx);
32+
dropObject(idx);
33+
return ret;
3534
}
3635

37-
const lTextDecoder =
38-
typeof TextDecoder === "undefined"
39-
? (0, module.require)("util").TextDecoder
40-
: TextDecoder
36+
const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;
4137

42-
let cachedTextDecoder = new lTextDecoder("utf-8", {
43-
ignoreBOM: true,
44-
fatal: true,
45-
})
38+
let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });
4639

47-
cachedTextDecoder.decode()
40+
cachedTextDecoder.decode();
4841

49-
let cachedUint8ArrayMemory0 = null
42+
let cachedUint8ArrayMemory0 = null;
5043

5144
function getUint8ArrayMemory0() {
52-
if (
53-
cachedUint8ArrayMemory0 === null ||
54-
cachedUint8ArrayMemory0.byteLength === 0
55-
) {
56-
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer)
57-
}
58-
return cachedUint8ArrayMemory0
45+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
46+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
47+
}
48+
return cachedUint8ArrayMemory0;
5949
}
6050

6151
function getStringFromWasm0(ptr, len) {
62-
ptr = ptr >>> 0
63-
return cachedTextDecoder.decode(
64-
getUint8ArrayMemory0().subarray(ptr, ptr + len)
65-
)
52+
ptr = ptr >>> 0;
53+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
6654
}
6755

68-
let WASM_VECTOR_LEN = 0
56+
let WASM_VECTOR_LEN = 0;
6957

7058
function passArray8ToWasm0(arg, malloc) {
71-
const ptr = malloc(arg.length * 1, 1) >>> 0
72-
getUint8ArrayMemory0().set(arg, ptr / 1)
73-
WASM_VECTOR_LEN = arg.length
74-
return ptr
59+
const ptr = malloc(arg.length * 1, 1) >>> 0;
60+
getUint8ArrayMemory0().set(arg, ptr / 1);
61+
WASM_VECTOR_LEN = arg.length;
62+
return ptr;
7563
}
7664

77-
const WaosmFinalization =
78-
typeof FinalizationRegistry === "undefined"
65+
const WaosmFinalization = (typeof FinalizationRegistry === 'undefined')
7966
? { register: () => {}, unregister: () => {} }
80-
: new FinalizationRegistry(ptr => wasm.__wbg_waosm_free(ptr >>> 0, 1))
67+
: new FinalizationRegistry(ptr => wasm.__wbg_waosm_free(ptr >>> 0, 1));
8168

8269
export class Waosm {
83-
__destroy_into_raw() {
84-
const ptr = this.__wbg_ptr
85-
this.__wbg_ptr = 0
86-
WaosmFinalization.unregister(this)
87-
return ptr
88-
}
89-
90-
free() {
91-
const ptr = this.__destroy_into_raw()
92-
wasm.__wbg_waosm_free(ptr, 0)
93-
}
94-
constructor() {
95-
const ret = wasm.waosm_new()
96-
this.__wbg_ptr = ret >>> 0
97-
WaosmFinalization.register(this, this.__wbg_ptr, this)
98-
return this
99-
}
100-
/**
101-
* @param {Uint8Array} data
102-
* @returns {Uint8Array}
103-
*/
104-
compress(data) {
105-
const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc)
106-
const len0 = WASM_VECTOR_LEN
107-
const ret = wasm.waosm_compress(this.__wbg_ptr, ptr0, len0)
108-
return takeObject(ret)
109-
}
110-
/**
111-
* @param {Uint8Array} data
112-
* @param {number} decompressed_size
113-
* @returns {Uint8Array}
114-
*/
115-
decompress(data, decompressed_size) {
116-
const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc)
117-
const len0 = WASM_VECTOR_LEN
118-
const ret = wasm.waosm_decompress(
119-
this.__wbg_ptr,
120-
ptr0,
121-
len0,
122-
decompressed_size
123-
)
124-
return takeObject(ret)
125-
}
70+
71+
__destroy_into_raw() {
72+
const ptr = this.__wbg_ptr;
73+
this.__wbg_ptr = 0;
74+
WaosmFinalization.unregister(this);
75+
return ptr;
76+
}
77+
78+
free() {
79+
const ptr = this.__destroy_into_raw();
80+
wasm.__wbg_waosm_free(ptr, 0);
81+
}
82+
constructor() {
83+
const ret = wasm.waosm_new();
84+
this.__wbg_ptr = ret >>> 0;
85+
WaosmFinalization.register(this, this.__wbg_ptr, this);
86+
return this;
87+
}
88+
/**
89+
* @param {Uint8Array} data
90+
* @returns {Uint8Array}
91+
*/
92+
compress(data) {
93+
const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
94+
const len0 = WASM_VECTOR_LEN;
95+
const ret = wasm.waosm_compress(this.__wbg_ptr, ptr0, len0);
96+
return takeObject(ret);
97+
}
98+
/**
99+
* @param {Uint8Array} data
100+
* @param {number} decompressed_size
101+
* @returns {Uint8Array}
102+
*/
103+
decompress(data, decompressed_size) {
104+
const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
105+
const len0 = WASM_VECTOR_LEN;
106+
const ret = wasm.waosm_decompress(this.__wbg_ptr, ptr0, len0, decompressed_size);
107+
return takeObject(ret);
108+
}
126109
}
127110

128111
export function __wbg_buffer_609cc3eee51ed158(arg0) {
129-
const ret = getObject(arg0).buffer
130-
return addHeapObject(ret)
131-
}
112+
const ret = getObject(arg0).buffer;
113+
return addHeapObject(ret);
114+
};
132115

133116
export function __wbg_length_a446193dc22c12f8(arg0) {
134-
const ret = getObject(arg0).length
135-
return ret
136-
}
117+
const ret = getObject(arg0).length;
118+
return ret;
119+
};
137120

138-
export function __wbg_newwithbyteoffsetandlength_d97e637ebe145a9a(
139-
arg0,
140-
arg1,
141-
arg2
142-
) {
143-
const ret = new Uint8Array(getObject(arg0), arg1 >>> 0, arg2 >>> 0)
144-
return addHeapObject(ret)
145-
}
121+
export function __wbg_newwithbyteoffsetandlength_d97e637ebe145a9a(arg0, arg1, arg2) {
122+
const ret = new Uint8Array(getObject(arg0), arg1 >>> 0, arg2 >>> 0);
123+
return addHeapObject(ret);
124+
};
146125

147126
export function __wbg_newwithlength_a381634e90c276d4(arg0) {
148-
const ret = new Uint8Array(arg0 >>> 0)
149-
return addHeapObject(ret)
150-
}
127+
const ret = new Uint8Array(arg0 >>> 0);
128+
return addHeapObject(ret);
129+
};
151130

152131
export function __wbg_set_65595bdd868b3009(arg0, arg1, arg2) {
153-
getObject(arg0).set(getObject(arg1), arg2 >>> 0)
154-
}
132+
getObject(arg0).set(getObject(arg1), arg2 >>> 0);
133+
};
155134

156135
export function __wbindgen_memory() {
157-
const ret = wasm.memory
158-
return addHeapObject(ret)
159-
}
136+
const ret = wasm.memory;
137+
return addHeapObject(ret);
138+
};
160139

161140
export function __wbindgen_object_drop_ref(arg0) {
162-
takeObject(arg0)
163-
}
141+
takeObject(arg0);
142+
};
164143

165144
export function __wbindgen_throw(arg0, arg1) {
166-
throw new Error(getStringFromWasm0(arg0, arg1))
167-
}
145+
throw new Error(getStringFromWasm0(arg0, arg1));
146+
};
147+

src/waosm-node/waosm_bg.wasm.js

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/weavedb.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function frombits(bitArray) {
5959

6060
return result
6161
}
62-
62+
let init = {}
6363
export default class WeaveDB {
6464
constructor(ar, dir) {
6565
dir ??= resolve(import.meta.dirname, ".db")
@@ -178,10 +178,14 @@ export default class WeaveDB {
178178
for (let i = 0; i < FS.streams.length; i++) {
179179
if (FS.streams[i].fd === fd) stream = FS.streams[i]
180180
}
181+
if (!stream) return 0
182+
if (init[fd] === false) stream.position = 0
183+
init[fd] = true
184+
181185
// Satisfy what we can with the cache first
182186
let bytes_read = this.readFromCache(stream, dst_ptr, to_read)
183-
stream.node.position += bytes_read
184-
stream.lastReadPosition = stream.node.position
187+
stream.position += bytes_read
188+
stream.lastReadPosition = stream.position
185189
dst_ptr += bytes_read
186190
to_read -= bytes_read
187191

@@ -195,7 +199,7 @@ export default class WeaveDB {
195199
const chunk_download_sz = Math.max(to_read, CACHE_SZ)
196200
const to = Math.min(
197201
stream.node.total_size,
198-
stream.node.position + chunk_download_sz
202+
stream.position + chunk_download_sz
199203
)
200204
let data = val
201205
if (!data) {
@@ -290,7 +294,7 @@ export default class WeaveDB {
290294
mod.HEAP8.set(chunk_bytes.subarray(0, write_length), dst_ptr)
291295
dst_ptr += write_length
292296
bytes_read += write_length
293-
stream.node.position += write_length
297+
stream.position += write_length
294298
to_read -= write_length
295299
}
296300

@@ -332,7 +336,7 @@ export default class WeaveDB {
332336
)
333337

334338
// Update the last read position
335-
stream.lastReadPosition = stream.node.position
339+
stream.lastReadPosition = stream.position
336340
return bytes_read
337341
},
338342
close(fd) {
@@ -345,8 +349,8 @@ export default class WeaveDB {
345349

346350
readFromCache(stream, dst_ptr, length) {
347351
// Check if the cache has been invalidated by a seek
348-
if (stream.lastReadPosition !== stream.node.position) {
349-
//console.log("KV: Invalidating cache for fd: ", stream.fd, " Current pos: ", stream.node.position, " Last read pos: ", stream.lastReadPosition)
352+
if (stream.lastReadPosition !== stream.position) {
353+
//console.log("KV: Invalidating cache for fd: ", stream.fd, " Current pos: ", stream.position, " Last read pos: ", stream.lastReadPosition)
350354
stream.node.cache = new Uint8Array(0)
351355
return 0
352356
}

0 commit comments

Comments
 (0)