@based/db 0.0.71 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +105 -77
- package/dist/lib/darwin_aarch64/include/libdeflate.h +23 -0
- package/dist/lib/darwin_aarch64/include/selva/db.h +0 -41
- package/dist/lib/darwin_aarch64/include/selva/fast_linear_search.h +6 -2
- package/dist/lib/darwin_aarch64/include/selva/fields.h +52 -37
- package/dist/lib/darwin_aarch64/include/selva/gmtime.h +137 -0
- package/dist/lib/darwin_aarch64/include/selva/mblen.h +40 -0
- package/dist/lib/darwin_aarch64/include/selva/selva_string.h +8 -5
- package/dist/lib/darwin_aarch64/include/selva/thread.h +37 -0
- package/dist/lib/darwin_aarch64/include/selva/types.h +18 -4
- package/dist/lib/darwin_aarch64/libdeflate.dylib +0 -0
- package/dist/lib/darwin_aarch64/libjemalloc_selva.2.dylib +0 -0
- package/dist/lib/darwin_aarch64/libnode-v22.node +0 -0
- package/dist/lib/darwin_aarch64/libnode-v23.node +0 -0
- package/dist/lib/darwin_aarch64/libnode-v24.node +0 -0
- package/dist/lib/darwin_aarch64/libselva.dylib +0 -0
- package/dist/lib/darwin_aarch64/libxxhash.dylib +0 -0
- package/dist/lib/linux_aarch64/include/libdeflate.h +23 -0
- package/dist/lib/linux_aarch64/include/selva/db.h +0 -41
- package/dist/lib/linux_aarch64/include/selva/fast_linear_search.h +6 -2
- package/dist/lib/linux_aarch64/include/selva/fields.h +52 -37
- package/dist/lib/linux_aarch64/include/selva/gmtime.h +137 -0
- package/dist/lib/linux_aarch64/include/selva/mblen.h +40 -0
- package/dist/lib/linux_aarch64/include/selva/selva_string.h +8 -5
- package/dist/lib/linux_aarch64/include/selva/thread.h +37 -0
- package/dist/lib/linux_aarch64/include/selva/types.h +18 -4
- package/dist/lib/linux_aarch64/libdeflate.so +0 -0
- package/dist/lib/linux_aarch64/libnode-v22.node +0 -0
- package/dist/lib/linux_aarch64/libnode-v23.node +0 -0
- package/dist/lib/linux_aarch64/libnode-v24.node +0 -0
- package/dist/lib/linux_aarch64/libselva.so +0 -0
- package/dist/lib/linux_x86_64/include/libdeflate.h +23 -0
- package/dist/lib/linux_x86_64/include/selva/db.h +0 -41
- package/dist/lib/linux_x86_64/include/selva/fast_linear_search.h +6 -2
- package/dist/lib/linux_x86_64/include/selva/fields.h +52 -37
- package/dist/lib/linux_x86_64/include/selva/gmtime.h +137 -0
- package/dist/lib/linux_x86_64/include/selva/mblen.h +40 -0
- package/dist/lib/linux_x86_64/include/selva/selva_string.h +8 -5
- package/dist/lib/linux_x86_64/include/selva/thread.h +37 -0
- package/dist/lib/linux_x86_64/include/selva/types.h +18 -4
- package/dist/lib/linux_x86_64/libdeflate.so +0 -0
- package/dist/lib/linux_x86_64/libnode-v22.node +0 -0
- package/dist/lib/linux_x86_64/libnode-v23.node +0 -0
- package/dist/lib/linux_x86_64/libnode-v24.node +0 -0
- package/dist/lib/linux_x86_64/libselva.so +0 -0
- package/dist/src/client/index.d.ts +16 -21
- package/dist/src/client/index.js +48 -59
- package/dist/src/client/modify/Ctx.d.ts +38 -0
- package/dist/src/client/modify/Ctx.js +30 -0
- package/dist/src/client/modify/Tmp.d.ts +19 -0
- package/dist/src/client/modify/Tmp.js +67 -0
- package/dist/src/client/modify/create/index.d.ts +6 -0
- package/dist/src/client/modify/create/index.js +191 -0
- package/dist/src/client/modify/create/mark.d.ts +7 -0
- package/dist/src/client/modify/create/mark.js +42 -0
- package/dist/src/client/modify/cursor.d.ts +10 -0
- package/dist/src/client/modify/cursor.js +48 -0
- package/dist/src/client/modify/delete/index.d.ts +2 -0
- package/dist/src/client/modify/delete/index.js +39 -0
- package/dist/src/client/modify/drain.d.ts +7 -0
- package/dist/src/client/modify/drain.js +77 -0
- package/dist/src/client/modify/edges/binary.d.ts +3 -0
- package/dist/src/client/modify/edges/binary.js +28 -0
- package/dist/src/client/modify/edges/cardinality.d.ts +3 -0
- package/dist/src/client/modify/edges/cardinality.js +15 -0
- package/dist/src/client/modify/edges/header.d.ts +5 -0
- package/dist/src/client/modify/edges/header.js +21 -0
- package/dist/src/client/modify/edges/index.d.ts +3 -0
- package/dist/src/client/modify/edges/index.js +128 -0
- package/dist/src/client/modify/edges/reference.d.ts +3 -0
- package/dist/src/client/modify/edges/reference.js +31 -0
- package/dist/src/client/modify/edges/references.d.ts +3 -0
- package/dist/src/client/modify/edges/references.js +38 -0
- package/dist/src/client/modify/edges/separate.d.ts +3 -0
- package/dist/src/client/modify/edges/separate.js +24 -0
- package/dist/src/client/modify/edges/string.d.ts +3 -0
- package/dist/src/client/modify/edges/string.js +30 -0
- package/dist/src/client/modify/error.d.ts +7 -0
- package/dist/src/client/modify/error.js +68 -0
- package/dist/src/client/modify/expire/index.d.ts +2 -0
- package/dist/src/client/modify/expire/index.js +31 -0
- package/dist/src/client/modify/props/alias.d.ts +3 -0
- package/dist/src/client/modify/props/alias.js +26 -0
- package/dist/src/client/modify/props/binary.d.ts +5 -0
- package/dist/src/client/modify/props/binary.js +52 -0
- package/dist/src/client/modify/props/cardinality.d.ts +4 -0
- package/dist/src/client/modify/props/cardinality.js +46 -0
- package/dist/src/client/modify/props/delete.d.ts +3 -0
- package/dist/src/client/modify/props/delete.js +13 -0
- package/dist/src/client/modify/props/fixed.d.ts +4 -0
- package/dist/src/client/modify/props/fixed.js +105 -0
- package/dist/src/client/modify/props/increment.d.ts +3 -0
- package/dist/src/client/modify/props/increment.js +28 -0
- package/dist/src/client/modify/props/json.d.ts +3 -0
- package/dist/src/client/modify/props/json.js +5 -0
- package/dist/src/client/modify/props/main.d.ts +4 -0
- package/dist/src/client/modify/props/main.js +22 -0
- package/dist/src/client/modify/props/object.d.ts +3 -0
- package/dist/src/client/modify/props/object.js +37 -0
- package/dist/src/client/modify/props/reference.d.ts +6 -0
- package/dist/src/client/modify/props/reference.js +92 -0
- package/dist/src/client/modify/props/references.d.ts +3 -0
- package/dist/src/client/modify/props/references.js +231 -0
- package/dist/src/client/modify/props/separate.d.ts +3 -0
- package/dist/src/client/modify/props/separate.js +43 -0
- package/dist/src/client/modify/props/string.d.ts +5 -0
- package/dist/src/client/modify/props/string.js +51 -0
- package/dist/src/client/modify/props/text.d.ts +3 -0
- package/dist/src/client/modify/props/text.js +35 -0
- package/dist/src/client/modify/props/vector.d.ts +3 -0
- package/dist/src/client/modify/props/vector.js +30 -0
- package/dist/src/client/modify/resize.d.ts +3 -0
- package/dist/src/client/modify/resize.js +20 -0
- package/dist/src/client/modify/types.d.ts +3 -4
- package/dist/src/client/modify/types.js +2 -1
- package/dist/src/client/modify/uint.d.ts +7 -0
- package/dist/src/client/modify/uint.js +27 -0
- package/dist/src/client/modify/update/index.d.ts +3 -0
- package/dist/src/client/modify/update/index.js +75 -0
- package/dist/src/client/modify/upsert/index.d.ts +3 -0
- package/dist/src/client/modify/{upsert.js → upsert/index.js} +4 -4
- package/dist/src/client/modify/validate.d.ts +6 -0
- package/dist/src/client/modify/validate.js +24 -0
- package/dist/src/client/query/BasedDbQuery.d.ts +17 -14
- package/dist/src/client/query/BasedDbQuery.js +90 -93
- package/dist/src/client/query/{BasedIterable.d.ts → BasedQueryResponse.d.ts} +5 -4
- package/dist/src/client/query/{BasedIterable.js → BasedQueryResponse.js} +15 -10
- package/dist/src/client/query/aggregates/aggregation.d.ts +5 -3
- package/dist/src/client/query/aggregates/aggregation.js +105 -48
- package/dist/src/client/query/aggregates/types.d.ts +33 -11
- package/dist/src/client/query/aggregates/types.js +29 -11
- package/dist/src/client/query/debug.js +1 -1
- package/dist/src/client/query/display.d.ts +1 -1
- package/dist/src/client/query/display.js +21 -4
- package/dist/src/client/query/filter/FilterBranch.js +2 -2
- package/dist/src/client/query/filter/convertFilter.d.ts +3 -2
- package/dist/src/client/query/filter/convertFilter.js +13 -1
- package/dist/src/client/query/filter/createFixedFilterBuffer.js +16 -30
- package/dist/src/client/query/filter/createReferenceFilter.js +6 -20
- package/dist/src/client/query/filter/createVariableFilterBuffer.js +15 -32
- package/dist/src/client/query/filter/parseFilterValue.js +3 -11
- package/dist/src/client/query/filter/primitiveFilter.js +1 -1
- package/dist/src/client/query/filter/toBuffer.js +9 -15
- package/dist/src/client/query/filter/types.d.ts +6 -6
- package/dist/src/client/query/filter/types.js +8 -8
- package/dist/src/client/query/include/include.d.ts +3 -0
- package/dist/src/client/query/include/include.js +58 -0
- package/dist/src/client/query/include/props.d.ts +7 -7
- package/dist/src/client/query/include/props.js +26 -21
- package/dist/src/client/query/include/toByteCode.js +188 -0
- package/dist/src/client/query/include/utils.d.ts +3 -1
- package/dist/src/client/query/include/utils.js +13 -0
- package/dist/src/client/query/include/walk.d.ts +2 -2
- package/dist/src/client/query/include/walk.js +38 -23
- package/dist/src/client/query/query.d.ts +2 -3
- package/dist/src/client/query/query.js +2 -3
- package/dist/src/client/query/queryDef.d.ts +4 -2
- package/dist/src/client/query/queryDef.js +3 -5
- package/dist/src/client/query/queryDefToReadSchema.d.ts +3 -0
- package/dist/src/client/query/queryDefToReadSchema.js +126 -0
- package/dist/src/client/query/registerQuery.js +15 -4
- package/dist/src/client/query/search/index.d.ts +2 -1
- package/dist/src/client/query/search/index.js +9 -2
- package/dist/src/client/query/sort.js +3 -0
- package/dist/src/client/query/subscription/index.d.ts +1 -1
- package/dist/src/client/query/subscription/index.js +2 -2
- package/dist/src/client/query/subscription/types.d.ts +1 -1
- package/dist/src/client/query/thresholds.d.ts +1 -1
- package/dist/src/client/query/thresholds.js +1 -1
- package/dist/src/client/query/toByteCode/{toBuffer.js → toByteCode.js} +8 -8
- package/dist/src/client/query/types.d.ts +40 -25
- package/dist/src/client/query/types.js +2 -6
- package/dist/src/client/query/validation.d.ts +6 -0
- package/dist/src/client/query/validation.js +31 -7
- package/dist/src/client/setLocalClientSchema.js +6 -10
- package/dist/src/client/string.d.ts +3 -7
- package/dist/src/client/string.js +18 -47
- package/dist/src/hooks.d.ts +2 -3
- package/dist/src/hooks.js +14 -9
- package/dist/src/index.d.ts +3 -5
- package/dist/src/index.js +4 -6
- package/dist/src/native.d.ts +3 -2
- package/dist/src/native.js +9 -8
- package/dist/src/server/IoWorker.js +2 -3
- package/dist/src/server/QueryWorker.js +1 -1
- package/dist/src/server/blocks.js +5 -4
- package/dist/src/server/dbHash.js +1 -1
- package/dist/src/server/index.d.ts +4 -6
- package/dist/src/server/index.js +63 -96
- package/dist/src/server/migrate/index.d.ts +2 -4
- package/dist/src/server/migrate/index.js +31 -16
- package/dist/src/server/migrate/worker.js +2 -27
- package/dist/src/server/save.js +3 -2
- package/dist/src/server/schema.js +9 -19
- package/dist/src/server/start.d.ts +1 -0
- package/dist/src/server/start.js +18 -10
- package/dist/src/server/tree.d.ts +20 -0
- package/dist/src/server/tree.js +17 -2
- package/dist/src/server/workers/DbWorker.d.ts +1 -0
- package/dist/src/server/workers/DbWorker.js +8 -1
- package/dist/src/server/workers/io_worker.js +1 -1
- package/dist/src/server/workers/worker.js +9 -4
- package/dist/src/table.d.ts +4 -0
- package/dist/src/table.js +97 -0
- package/package.json +18 -17
- package/dist/lib/darwin_aarch64/include/selva/worker_ctx.h +0 -29
- package/dist/lib/linux_aarch64/include/selva/worker_ctx.h +0 -29
- package/dist/lib/linux_x86_64/include/selva/worker_ctx.h +0 -29
- package/dist/src/client/flushModify.d.ts +0 -36
- package/dist/src/client/flushModify.js +0 -197
- package/dist/src/client/modify/ModifyRes.d.ts +0 -23
- package/dist/src/client/modify/ModifyRes.js +0 -101
- package/dist/src/client/modify/alias.d.ts +0 -4
- package/dist/src/client/modify/alias.js +0 -57
- package/dist/src/client/modify/binary.d.ts +0 -6
- package/dist/src/client/modify/binary.js +0 -80
- package/dist/src/client/modify/cardinality.d.ts +0 -6
- package/dist/src/client/modify/cardinality.js +0 -63
- package/dist/src/client/modify/create.d.ts +0 -5
- package/dist/src/client/modify/create.js +0 -228
- package/dist/src/client/modify/delete.d.ts +0 -4
- package/dist/src/client/modify/delete.js +0 -47
- package/dist/src/client/modify/expire.d.ts +0 -3
- package/dist/src/client/modify/expire.js +0 -25
- package/dist/src/client/modify/fixed.d.ts +0 -5
- package/dist/src/client/modify/fixed.js +0 -235
- package/dist/src/client/modify/index.d.ts +0 -4
- package/dist/src/client/modify/index.js +0 -4
- package/dist/src/client/modify/json.d.ts +0 -4
- package/dist/src/client/modify/json.js +0 -26
- package/dist/src/client/modify/modify.d.ts +0 -5
- package/dist/src/client/modify/modify.js +0 -134
- package/dist/src/client/modify/references/appendEdgeRefs.d.ts +0 -4
- package/dist/src/client/modify/references/appendEdgeRefs.js +0 -33
- package/dist/src/client/modify/references/edge.d.ts +0 -5
- package/dist/src/client/modify/references/edge.js +0 -336
- package/dist/src/client/modify/references/getEdgeSize.d.ts +0 -3
- package/dist/src/client/modify/references/getEdgeSize.js +0 -27
- package/dist/src/client/modify/references/reference.d.ts +0 -11
- package/dist/src/client/modify/references/reference.js +0 -124
- package/dist/src/client/modify/references/references.d.ts +0 -16
- package/dist/src/client/modify/references/references.js +0 -316
- package/dist/src/client/modify/setCursor.d.ts +0 -5
- package/dist/src/client/modify/setCursor.js +0 -33
- package/dist/src/client/modify/string.d.ts +0 -5
- package/dist/src/client/modify/string.js +0 -71
- package/dist/src/client/modify/text.d.ts +0 -8
- package/dist/src/client/modify/text.js +0 -69
- package/dist/src/client/modify/update.d.ts +0 -6
- package/dist/src/client/modify/update.js +0 -90
- package/dist/src/client/modify/upsert.d.ts +0 -3
- package/dist/src/client/modify/vector.d.ts +0 -4
- package/dist/src/client/modify/vector.js +0 -53
- package/dist/src/client/query/include/toBuffer.js +0 -123
- package/dist/src/client/query/read/read.d.ts +0 -9
- package/dist/src/client/query/read/read.js +0 -483
- /package/dist/src/client/query/include/{toBuffer.d.ts → toByteCode.d.ts} +0 -0
- /package/dist/src/client/query/toByteCode/{toBuffer.d.ts → toByteCode.d.ts} +0 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { DbClient } from '../../index.js';
|
|
2
|
+
import { Ctx } from './Ctx.js';
|
|
3
|
+
export declare const reset: (ctx: Ctx) => void;
|
|
4
|
+
export declare const cancel: (ctx: Ctx, error: Error) => void;
|
|
5
|
+
export declare const consume: (ctx: Ctx) => Uint8Array;
|
|
6
|
+
export declare const drain: (db: DbClient, ctx: Ctx) => Promise<void>;
|
|
7
|
+
export declare const schedule: (db: DbClient, ctx: Ctx) => Promise<void>;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { writeUint16, writeUint32 } from '@based/utils';
|
|
2
|
+
import { rejectTmp, resolveTmp } from './Tmp.js';
|
|
3
|
+
export const reset = (ctx) => {
|
|
4
|
+
ctx.index = 8;
|
|
5
|
+
ctx.max = ctx.array.buffer.maxByteLength - 4;
|
|
6
|
+
ctx.size = ctx.array.buffer.byteLength - 4;
|
|
7
|
+
ctx.cursor = {};
|
|
8
|
+
ctx.created = {};
|
|
9
|
+
ctx.batch = {};
|
|
10
|
+
};
|
|
11
|
+
export const cancel = (ctx, error) => {
|
|
12
|
+
const { batch } = ctx;
|
|
13
|
+
reset(ctx);
|
|
14
|
+
batch.error = error;
|
|
15
|
+
batch.promises?.forEach(rejectTmp);
|
|
16
|
+
};
|
|
17
|
+
export const consume = (ctx) => {
|
|
18
|
+
if (ctx.index > ctx.array.byteLength) {
|
|
19
|
+
throw new Error('Invalid size - modify buffer length mismatch');
|
|
20
|
+
}
|
|
21
|
+
const typeIds = Object.keys(ctx.created);
|
|
22
|
+
const typeSize = typeIds.length * 6 + 4;
|
|
23
|
+
const payload = ctx.array.subarray(0, ctx.index + typeSize);
|
|
24
|
+
let i = payload.byteLength - 4;
|
|
25
|
+
writeUint32(payload, ctx.index, i);
|
|
26
|
+
for (const typeId of typeIds) {
|
|
27
|
+
const count = ctx.created[typeId];
|
|
28
|
+
i -= 6;
|
|
29
|
+
writeUint16(payload, Number(typeId), i);
|
|
30
|
+
writeUint32(payload, count, i + 2);
|
|
31
|
+
}
|
|
32
|
+
reset(ctx);
|
|
33
|
+
return payload;
|
|
34
|
+
};
|
|
35
|
+
export const drain = (db, ctx) => {
|
|
36
|
+
if (ctx.index > 8) {
|
|
37
|
+
const { batch } = ctx;
|
|
38
|
+
const payload = consume(ctx);
|
|
39
|
+
ctx.draining = db.hooks
|
|
40
|
+
.flushModify(payload)
|
|
41
|
+
.then(({ offsets, dbWriteTime }) => {
|
|
42
|
+
db.writeTime += dbWriteTime ?? 0;
|
|
43
|
+
batch.ready = true;
|
|
44
|
+
batch.offsets = offsets;
|
|
45
|
+
batch.promises?.forEach(resolveTmp);
|
|
46
|
+
batch.promises = null;
|
|
47
|
+
})
|
|
48
|
+
.catch((e) => {
|
|
49
|
+
console.error(e);
|
|
50
|
+
batch.ready = true;
|
|
51
|
+
batch.error = e;
|
|
52
|
+
batch.promises?.forEach(rejectTmp);
|
|
53
|
+
batch.promises = null;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return ctx.draining;
|
|
57
|
+
};
|
|
58
|
+
export const schedule = (db, ctx) => {
|
|
59
|
+
if (ctx.scheduled || ctx.index === 8) {
|
|
60
|
+
return ctx.scheduled;
|
|
61
|
+
}
|
|
62
|
+
ctx.scheduled = new Promise((resolve) => {
|
|
63
|
+
if (db.flushTime === 0) {
|
|
64
|
+
process.nextTick(() => {
|
|
65
|
+
ctx.scheduled = null;
|
|
66
|
+
resolve(drain(db, ctx));
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
setTimeout(() => {
|
|
71
|
+
ctx.scheduled = null;
|
|
72
|
+
resolve(drain(db, ctx));
|
|
73
|
+
}, db.flushTime);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
//# sourceMappingURL=drain.js.map
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { STRING } from '@based/schema/def';
|
|
2
|
+
import { reserve } from '../resize.js';
|
|
3
|
+
import { getBuffer, writeBinaryRaw } from '../props/binary.js';
|
|
4
|
+
import { writeU32 } from '../uint.js';
|
|
5
|
+
import { writeEdgeHeader } from './header.js';
|
|
6
|
+
import { PROP_CURSOR_SIZE } from '../cursor.js';
|
|
7
|
+
export const writeBinaryEdge = (ctx, edge, val) => {
|
|
8
|
+
let size = 0;
|
|
9
|
+
if (val !== null) {
|
|
10
|
+
const buf = getBuffer(val);
|
|
11
|
+
if (!buf || !edge.validation(buf, edge)) {
|
|
12
|
+
throw [edge, val];
|
|
13
|
+
}
|
|
14
|
+
size = buf.byteLength;
|
|
15
|
+
val = buf;
|
|
16
|
+
}
|
|
17
|
+
if (size) {
|
|
18
|
+
reserve(ctx, PROP_CURSOR_SIZE + size + 10);
|
|
19
|
+
writeEdgeHeader(ctx, edge, STRING);
|
|
20
|
+
writeBinaryRaw(ctx, val);
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
reserve(ctx, 3 + 4);
|
|
24
|
+
writeEdgeHeader(ctx, edge, STRING);
|
|
25
|
+
writeU32(ctx, 0);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
//# sourceMappingURL=binary.js.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { CARDINALITY } from '@based/schema/def';
|
|
2
|
+
import { reserve } from '../resize.js';
|
|
3
|
+
import { writeEdgeHeader } from './header.js';
|
|
4
|
+
import { writeCardinalityRaw } from '../props/cardinality.js';
|
|
5
|
+
import { PROP_CURSOR_SIZE } from '../cursor.js';
|
|
6
|
+
export const writeCardinalityEdge = (ctx, edge, val) => {
|
|
7
|
+
if (!Array.isArray(val)) {
|
|
8
|
+
val = [val];
|
|
9
|
+
}
|
|
10
|
+
const size = 4 + val.length * 8;
|
|
11
|
+
reserve(ctx, PROP_CURSOR_SIZE + size);
|
|
12
|
+
writeEdgeHeader(ctx, edge, CARDINALITY);
|
|
13
|
+
writeCardinalityRaw(ctx, edge, val, size);
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=cardinality.js.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { PropDefEdge } from '@based/schema/def';
|
|
2
|
+
import { Ctx } from '../Ctx.js';
|
|
3
|
+
export declare const writeEdgeHeader: (ctx: Ctx, edge: PropDefEdge, type: number) => void;
|
|
4
|
+
export declare const writeEdgeHeaderMain: (ctx: Ctx) => void;
|
|
5
|
+
export declare const writeEdgeHeaderPartial: (ctx: Ctx) => void;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { MICRO_BUFFER } from '@based/schema/def';
|
|
2
|
+
import { UPDATE, UPDATE_PARTIAL } from '../types.js';
|
|
3
|
+
export const writeEdgeHeader = (ctx, edge, type) => {
|
|
4
|
+
ctx.array[ctx.index] = UPDATE;
|
|
5
|
+
ctx.array[ctx.index + 1] = edge.prop;
|
|
6
|
+
ctx.array[ctx.index + 2] = type;
|
|
7
|
+
ctx.index += 3;
|
|
8
|
+
};
|
|
9
|
+
export const writeEdgeHeaderMain = (ctx) => {
|
|
10
|
+
ctx.array[ctx.index] = UPDATE;
|
|
11
|
+
ctx.array[ctx.index + 1] = 0;
|
|
12
|
+
ctx.array[ctx.index + 2] = MICRO_BUFFER;
|
|
13
|
+
ctx.index += 3;
|
|
14
|
+
};
|
|
15
|
+
export const writeEdgeHeaderPartial = (ctx) => {
|
|
16
|
+
ctx.array[ctx.index] = UPDATE_PARTIAL;
|
|
17
|
+
ctx.array[ctx.index + 1] = 0;
|
|
18
|
+
ctx.array[ctx.index + 2] = MICRO_BUFFER;
|
|
19
|
+
ctx.index += 3;
|
|
20
|
+
};
|
|
21
|
+
//# sourceMappingURL=header.js.map
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { writeSeparateEdge } from './separate.js';
|
|
2
|
+
import { DECREMENT, INCREMENT, UPDATE } from '../types.js';
|
|
3
|
+
import { writeEdgeHeaderMain, writeEdgeHeaderPartial } from './header.js';
|
|
4
|
+
import { writeU16, writeU32 } from '../uint.js';
|
|
5
|
+
import { writeFixed } from '../props/fixed.js';
|
|
6
|
+
import { writeUint16, writeUint32 } from '@based/utils';
|
|
7
|
+
import { reserve } from '../resize.js';
|
|
8
|
+
import { PROP_CURSOR_SIZE } from '../cursor.js';
|
|
9
|
+
const setDefaultEdges = (def, val) => {
|
|
10
|
+
if (def.hasDefaultEdges) {
|
|
11
|
+
for (const key in def.edges) {
|
|
12
|
+
const edge = def.edges[key];
|
|
13
|
+
if (edge.separate && val[key] === undefined) {
|
|
14
|
+
val[key] = edge.default;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
export const writeEdges = (ctx, def, obj, isSingleRefFix) => {
|
|
20
|
+
const index = ctx.index;
|
|
21
|
+
ctx.index += 4;
|
|
22
|
+
const start = ctx.index;
|
|
23
|
+
let hasIncr = false;
|
|
24
|
+
let mainSize = 0;
|
|
25
|
+
let mainFields;
|
|
26
|
+
let operation;
|
|
27
|
+
setDefaultEdges(def, obj);
|
|
28
|
+
for (const key in obj) {
|
|
29
|
+
let val = obj[key];
|
|
30
|
+
if (key === 'id' || key === '$index' || val === undefined) {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
const edge = def.edges[key];
|
|
34
|
+
if (!edge) {
|
|
35
|
+
throw [def, obj];
|
|
36
|
+
}
|
|
37
|
+
if (edge.separate) {
|
|
38
|
+
writeSeparateEdge(ctx, edge, val);
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (typeof val !== 'object' || val === null) {
|
|
42
|
+
operation = UPDATE;
|
|
43
|
+
}
|
|
44
|
+
else if (val.increment > 0) {
|
|
45
|
+
operation = INCREMENT;
|
|
46
|
+
hasIncr = true;
|
|
47
|
+
val = val.increment;
|
|
48
|
+
}
|
|
49
|
+
else if (val.increment < 0) {
|
|
50
|
+
operation = DECREMENT;
|
|
51
|
+
hasIncr = true;
|
|
52
|
+
val = val.increment;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
throw [edge, val];
|
|
56
|
+
}
|
|
57
|
+
if (!hasIncr && def.edgeMainLen === edge.len) {
|
|
58
|
+
reserve(ctx, 3 + 4 + edge.len);
|
|
59
|
+
writeEdgeHeaderMain(ctx);
|
|
60
|
+
writeU32(ctx, edge.len);
|
|
61
|
+
writeFixed(ctx, edge, val);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
mainSize += edge.len;
|
|
65
|
+
if (mainFields) {
|
|
66
|
+
const len = mainFields.length;
|
|
67
|
+
for (let i = 0; i < len; i += 3) {
|
|
68
|
+
if (edge.start < mainFields[i].start) {
|
|
69
|
+
mainFields.splice(i, 0, edge, val, operation);
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
else if (mainFields[len - i - 3].start < edge.start) {
|
|
73
|
+
mainFields.splice(len - i, 0, edge, val, operation);
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
mainFields = [edge, val, operation];
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (mainFields || def.hasDefaultEdges) {
|
|
84
|
+
if (!hasIncr && mainSize === def.edgeMainLen) {
|
|
85
|
+
reserve(ctx, 3 + 4 + mainSize);
|
|
86
|
+
writeEdgeHeaderMain(ctx);
|
|
87
|
+
writeU32(ctx, mainSize);
|
|
88
|
+
for (let i = 0; i < mainFields.length; i += 3) {
|
|
89
|
+
const edge = mainFields[i];
|
|
90
|
+
const val = mainFields[i + 1];
|
|
91
|
+
writeFixed(ctx, edge, val);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
mainFields ??= [];
|
|
96
|
+
const mainFieldsStartSize = mainFields.length * 2;
|
|
97
|
+
reserve(ctx, PROP_CURSOR_SIZE + 4 + 2 + mainFieldsStartSize + def.edgeMainLen);
|
|
98
|
+
writeEdgeHeaderPartial(ctx);
|
|
99
|
+
writeU32(ctx, mainFieldsStartSize + def.edgeMainLen);
|
|
100
|
+
writeU16(ctx, def.edgeMainLen);
|
|
101
|
+
// Index of start of fields
|
|
102
|
+
const sIndex = ctx.index;
|
|
103
|
+
ctx.index += mainFieldsStartSize;
|
|
104
|
+
// Add zeroes
|
|
105
|
+
ctx.array.set(def.edgeMainEmpty, ctx.index);
|
|
106
|
+
// Keep track of written bytes from append fixed
|
|
107
|
+
let startMain = ctx.index;
|
|
108
|
+
for (let i = 0; i < mainFields.length; i += 3) {
|
|
109
|
+
const edge = mainFields[i];
|
|
110
|
+
const value = mainFields[i + 1];
|
|
111
|
+
const operation = mainFields[i + 2];
|
|
112
|
+
const sIndexI = i * 2 + sIndex;
|
|
113
|
+
writeUint16(ctx.array, edge.start, sIndexI);
|
|
114
|
+
writeUint16(ctx.array, edge.len, sIndexI + 2);
|
|
115
|
+
ctx.array[sIndexI + 4] = operation;
|
|
116
|
+
ctx.array[sIndexI + 5] = edge.typeIndex;
|
|
117
|
+
ctx.index = startMain + edge.start;
|
|
118
|
+
// Add null support (defaults)
|
|
119
|
+
writeFixed(ctx, edge, value);
|
|
120
|
+
}
|
|
121
|
+
// Correction append fixed value writes the len
|
|
122
|
+
ctx.index = startMain + def.edgeMainLen;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
const size = ctx.index - start + (isSingleRefFix ? 4 : 0);
|
|
126
|
+
writeUint32(ctx.array, size, index);
|
|
127
|
+
};
|
|
128
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { REFERENCE } from '@based/schema/def';
|
|
2
|
+
import { reserve } from '../resize.js';
|
|
3
|
+
import { writeU32 } from '../uint.js';
|
|
4
|
+
import { writeEdgeHeader } from './header.js';
|
|
5
|
+
export const writeReferenceEdge = (ctx, edge, val) => {
|
|
6
|
+
if (val === null) {
|
|
7
|
+
reserve(ctx, 3 + 4);
|
|
8
|
+
writeEdgeHeader(ctx, edge, REFERENCE);
|
|
9
|
+
writeU32(ctx, 0);
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
if (typeof val === 'object') {
|
|
13
|
+
if (val.id) {
|
|
14
|
+
val = val.id;
|
|
15
|
+
}
|
|
16
|
+
else if (typeof val.then === 'function') {
|
|
17
|
+
throw val;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
if (typeof val === 'number') {
|
|
21
|
+
if (!edge.validation(val, edge)) {
|
|
22
|
+
throw [edge, val];
|
|
23
|
+
}
|
|
24
|
+
reserve(ctx, 3 + 4);
|
|
25
|
+
writeEdgeHeader(ctx, edge, REFERENCE);
|
|
26
|
+
writeU32(ctx, val);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
throw [edge, val];
|
|
30
|
+
};
|
|
31
|
+
//# sourceMappingURL=reference.js.map
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { REFERENCES } from '@based/schema/def';
|
|
2
|
+
import { reserve } from '../resize.js';
|
|
3
|
+
import { writeU32 } from '../uint.js';
|
|
4
|
+
import { writeEdgeHeader } from './header.js';
|
|
5
|
+
export const writeReferencesEdge = (ctx, edge, vals) => {
|
|
6
|
+
if (vals === null) {
|
|
7
|
+
reserve(ctx, 3 + 4);
|
|
8
|
+
writeEdgeHeader(ctx, edge, REFERENCES);
|
|
9
|
+
writeU32(ctx, 0);
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
if (!Array.isArray(vals)) {
|
|
13
|
+
throw [edge, vals];
|
|
14
|
+
}
|
|
15
|
+
const size = vals.length * 4;
|
|
16
|
+
reserve(ctx, 3 + 4 + size);
|
|
17
|
+
writeEdgeHeader(ctx, edge, REFERENCES);
|
|
18
|
+
writeU32(ctx, size);
|
|
19
|
+
for (let val of vals) {
|
|
20
|
+
if (typeof val === 'object') {
|
|
21
|
+
if (val.id) {
|
|
22
|
+
val = val.id;
|
|
23
|
+
}
|
|
24
|
+
else if (typeof val.then === 'function') {
|
|
25
|
+
throw val;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (typeof val === 'number') {
|
|
29
|
+
if (!edge.validation(val, edge)) {
|
|
30
|
+
throw [edge, vals];
|
|
31
|
+
}
|
|
32
|
+
writeU32(ctx, val);
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
throw [edge, vals];
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
//# sourceMappingURL=references.js.map
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { BINARY, STRING, REFERENCE, REFERENCES, CARDINALITY, } from '@based/schema/def';
|
|
2
|
+
import { writeBinaryEdge } from './binary.js';
|
|
3
|
+
import { writeStringEdge } from './string.js';
|
|
4
|
+
import { writeReferenceEdge } from './reference.js';
|
|
5
|
+
import { writeReferencesEdge } from './references.js';
|
|
6
|
+
import { writeCardinalityEdge } from './cardinality.js';
|
|
7
|
+
export const writeSeparateEdge = (ctx, edge, val) => {
|
|
8
|
+
if (edge.typeIndex === BINARY) {
|
|
9
|
+
writeBinaryEdge(ctx, edge, val);
|
|
10
|
+
}
|
|
11
|
+
else if (edge.typeIndex === STRING) {
|
|
12
|
+
writeStringEdge(ctx, edge, val);
|
|
13
|
+
}
|
|
14
|
+
else if (edge.typeIndex === REFERENCE) {
|
|
15
|
+
writeReferenceEdge(ctx, edge, val);
|
|
16
|
+
}
|
|
17
|
+
else if (edge.typeIndex === REFERENCES) {
|
|
18
|
+
writeReferencesEdge(ctx, edge, val);
|
|
19
|
+
}
|
|
20
|
+
else if (edge.typeIndex === CARDINALITY) {
|
|
21
|
+
writeCardinalityEdge(ctx, edge, val);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
//# sourceMappingURL=separate.js.map
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { STRING } from '@based/schema/def';
|
|
2
|
+
import { ENCODER } from '@based/utils';
|
|
3
|
+
import { write } from '../../string.js';
|
|
4
|
+
import { reserve } from '../resize.js';
|
|
5
|
+
import { RANGE_ERR } from '../types.js';
|
|
6
|
+
import { writeU32 } from '../uint.js';
|
|
7
|
+
import { writeEdgeHeader } from './header.js';
|
|
8
|
+
export const writeStringEdge = (ctx, edge, val) => {
|
|
9
|
+
if (val === null) {
|
|
10
|
+
reserve(ctx, 3 + 4);
|
|
11
|
+
writeEdgeHeader(ctx, edge, STRING);
|
|
12
|
+
writeU32(ctx, 0);
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
if (!edge.validation(val, edge)) {
|
|
16
|
+
throw [val, edge];
|
|
17
|
+
}
|
|
18
|
+
const maxSize = val instanceof Uint8Array
|
|
19
|
+
? val.byteLength
|
|
20
|
+
: ENCODER.encode(val).byteLength + 6;
|
|
21
|
+
reserve(ctx, 3 + maxSize + 4);
|
|
22
|
+
writeEdgeHeader(ctx, edge, STRING);
|
|
23
|
+
const realSize = write(ctx, val, ctx.index + 4, !edge.compression);
|
|
24
|
+
if (realSize === null) {
|
|
25
|
+
throw RANGE_ERR;
|
|
26
|
+
}
|
|
27
|
+
writeU32(ctx, realSize);
|
|
28
|
+
ctx.index += realSize;
|
|
29
|
+
};
|
|
30
|
+
//# sourceMappingURL=string.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { DbClient } from '../../index.js';
|
|
2
|
+
import { create } from './create/index.js';
|
|
3
|
+
import { Ctx } from './Ctx.js';
|
|
4
|
+
import { del } from './delete/index.js';
|
|
5
|
+
import { expire } from './expire/index.js';
|
|
6
|
+
import { update } from './update/index.js';
|
|
7
|
+
export declare const handleError: (db: DbClient, ctx: Ctx, fn: typeof create | typeof update | typeof del | typeof expire, args: IArguments, e: any) => Promise<number>;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { isPropDef, REVERSE_TYPE_INDEX_MAP, } from '@based/schema/def';
|
|
2
|
+
import { drain } from './drain.js';
|
|
3
|
+
import { Tmp } from './Tmp.js';
|
|
4
|
+
import { RANGE_ERR } from './types.js';
|
|
5
|
+
const MAGIC_KEY = Math.random().toString(36).substring(2);
|
|
6
|
+
const MAGIC_REG = RegExp(`("${MAGIC_KEY}|${MAGIC_KEY}")`, 'g');
|
|
7
|
+
const walk = (val) => {
|
|
8
|
+
if (typeof val === 'object' && val !== null) {
|
|
9
|
+
if (Array.isArray(val)) {
|
|
10
|
+
return val.map(walk);
|
|
11
|
+
}
|
|
12
|
+
const obj = {};
|
|
13
|
+
for (const key in val) {
|
|
14
|
+
obj[MAGIC_KEY + key + MAGIC_KEY] = walk(val[key]);
|
|
15
|
+
}
|
|
16
|
+
return obj;
|
|
17
|
+
}
|
|
18
|
+
return val;
|
|
19
|
+
};
|
|
20
|
+
const parseVal = (val) => {
|
|
21
|
+
if (typeof val === 'object' && val !== null) {
|
|
22
|
+
const str = JSON.stringify(walk(val)).replace(MAGIC_REG, '');
|
|
23
|
+
val = str;
|
|
24
|
+
}
|
|
25
|
+
if (typeof val === 'string' && val.length > 35) {
|
|
26
|
+
return val.slice(0, 35) + `... (${val.length - 35} more characters)`;
|
|
27
|
+
}
|
|
28
|
+
return val;
|
|
29
|
+
};
|
|
30
|
+
const parseErrorMsg = (prop, val, msg) => {
|
|
31
|
+
if (isPropDef(prop)) {
|
|
32
|
+
if (msg) {
|
|
33
|
+
return `Invalid value at '${prop.path.join('.')}'. Expected ${msg} received '${parseVal(val)}'`;
|
|
34
|
+
}
|
|
35
|
+
return `Invalid value at '${prop.path.join('.')}'. Expected ${REVERSE_TYPE_INDEX_MAP[prop.typeIndex]}, received '${parseVal(val)}'`;
|
|
36
|
+
}
|
|
37
|
+
return `Unknown property '${val}'. Expected one of: ${Object.keys(prop).join(', ')}`;
|
|
38
|
+
};
|
|
39
|
+
export const handleError = (db, ctx, fn, args, e) => {
|
|
40
|
+
ctx.index = ctx.start;
|
|
41
|
+
ctx.cursor = {};
|
|
42
|
+
if (e === RANGE_ERR) {
|
|
43
|
+
if (ctx.start === 8) {
|
|
44
|
+
throw 'Out of range. Not enough space for this payload';
|
|
45
|
+
}
|
|
46
|
+
drain(db, ctx);
|
|
47
|
+
return fn.apply(null, args);
|
|
48
|
+
}
|
|
49
|
+
if (typeof e.then === 'function') {
|
|
50
|
+
if (e.id)
|
|
51
|
+
process.exit();
|
|
52
|
+
return e.then((id) => {
|
|
53
|
+
if (!(e instanceof Tmp)) {
|
|
54
|
+
e.id = id;
|
|
55
|
+
}
|
|
56
|
+
return fn.apply(null, args);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
if (typeof e === 'string') {
|
|
60
|
+
throw Error(e);
|
|
61
|
+
}
|
|
62
|
+
if (Array.isArray(e)) {
|
|
63
|
+
const [def, val, msg] = e;
|
|
64
|
+
throw Error(parseErrorMsg(def, val, msg));
|
|
65
|
+
}
|
|
66
|
+
throw e;
|
|
67
|
+
};
|
|
68
|
+
//# sourceMappingURL=error.js.map
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { getValidSchema, validateId } from '../validate.js';
|
|
2
|
+
import { handleError } from '../error.js';
|
|
3
|
+
import { reserve } from '../resize.js';
|
|
4
|
+
import { NODE_CURSOR_SIZE, TYPE_CURSOR_SIZE, writeNodeCursor, writeTypeCursor, } from '../cursor.js';
|
|
5
|
+
import { EXPIRE } from '../types.js';
|
|
6
|
+
import { schedule } from '../drain.js';
|
|
7
|
+
import { Tmp } from '../Tmp.js';
|
|
8
|
+
import { writeU32, writeU8 } from '../uint.js';
|
|
9
|
+
export function expire(db, type, id, seconds) {
|
|
10
|
+
const ctx = db.modifyCtx;
|
|
11
|
+
const schema = getValidSchema(db, type);
|
|
12
|
+
try {
|
|
13
|
+
ctx.id = id;
|
|
14
|
+
ctx.start = ctx.index;
|
|
15
|
+
ctx.schema = schema;
|
|
16
|
+
ctx.operation = EXPIRE;
|
|
17
|
+
validateId(id);
|
|
18
|
+
reserve(ctx, TYPE_CURSOR_SIZE + NODE_CURSOR_SIZE + 5);
|
|
19
|
+
writeTypeCursor(ctx);
|
|
20
|
+
writeNodeCursor(ctx);
|
|
21
|
+
writeU8(ctx, EXPIRE);
|
|
22
|
+
writeU32(ctx, seconds);
|
|
23
|
+
const tmp = new Tmp(ctx, id);
|
|
24
|
+
schedule(db, ctx);
|
|
25
|
+
return tmp;
|
|
26
|
+
}
|
|
27
|
+
catch (e) {
|
|
28
|
+
return handleError(db, ctx, expire, arguments, e);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { deleteProp } from './delete.js';
|
|
2
|
+
import { validate } from '../validate.js';
|
|
3
|
+
import { ENCODER } from '@based/utils';
|
|
4
|
+
import { reserve } from '../resize.js';
|
|
5
|
+
import { PROP_CURSOR_SIZE, writePropCursor } from '../cursor.js';
|
|
6
|
+
import { writeU32, writeU8, writeU8Array } from '../uint.js';
|
|
7
|
+
import { markString } from '../create/mark.js';
|
|
8
|
+
export const writeAlias = (ctx, def, val) => {
|
|
9
|
+
if (val === null) {
|
|
10
|
+
deleteProp(ctx, def);
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
validate(def, val);
|
|
14
|
+
const buf = ENCODER.encode(val);
|
|
15
|
+
if (buf.byteLength === 0) {
|
|
16
|
+
deleteProp(ctx, def);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
reserve(ctx, PROP_CURSOR_SIZE + 5 + buf.byteLength);
|
|
20
|
+
writePropCursor(ctx, def);
|
|
21
|
+
writeU8(ctx, ctx.operation);
|
|
22
|
+
writeU32(ctx, buf.byteLength);
|
|
23
|
+
writeU8Array(ctx, buf);
|
|
24
|
+
markString(ctx, def);
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=alias.js.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { PropDef } from '@based/schema/def';
|
|
2
|
+
import { Ctx } from '../Ctx.js';
|
|
3
|
+
export declare const getBuffer: (val: any) => Uint8Array;
|
|
4
|
+
export declare const writeBinaryRaw: (ctx: Ctx, val: Uint8Array) => void;
|
|
5
|
+
export declare const writeBinary: (ctx: Ctx, def: PropDef, val: any, validated?: boolean) => void;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { writeU16, writeU32, writeU8, writeU8Array } from '../uint.js';
|
|
2
|
+
import { PROP_CURSOR_SIZE, writePropCursor } from '../cursor.js';
|
|
3
|
+
import { validate } from '../validate.js';
|
|
4
|
+
import native from '../../../native.js';
|
|
5
|
+
import { reserve } from '../resize.js';
|
|
6
|
+
import { deleteProp } from './delete.js';
|
|
7
|
+
import { ENCODER } from '@based/utils';
|
|
8
|
+
export const getBuffer = (val) => {
|
|
9
|
+
if (typeof val === 'string') {
|
|
10
|
+
return ENCODER.encode(val);
|
|
11
|
+
}
|
|
12
|
+
if (typeof val !== 'object') {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
if (val instanceof Uint8Array) {
|
|
16
|
+
return val;
|
|
17
|
+
}
|
|
18
|
+
if (val.buffer instanceof ArrayBuffer) {
|
|
19
|
+
return new Uint8Array(val.buffer, 0, val.byteLength);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
export const writeBinaryRaw = (ctx, val) => {
|
|
23
|
+
const size = val.byteLength + 6;
|
|
24
|
+
const crc = native.crc32(val);
|
|
25
|
+
writeU32(ctx, size);
|
|
26
|
+
writeU16(ctx, 0);
|
|
27
|
+
writeU8Array(ctx, val);
|
|
28
|
+
writeU32(ctx, crc);
|
|
29
|
+
};
|
|
30
|
+
export const writeBinary = (ctx, def, val, validated) => {
|
|
31
|
+
if (val === null) {
|
|
32
|
+
deleteProp(ctx, def);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const buf = getBuffer(val);
|
|
36
|
+
if (buf === undefined) {
|
|
37
|
+
throw [def, val];
|
|
38
|
+
}
|
|
39
|
+
if (!validated) {
|
|
40
|
+
validate(def, buf);
|
|
41
|
+
}
|
|
42
|
+
if (!buf.byteLength) {
|
|
43
|
+
deleteProp(ctx, def);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const size = buf.byteLength + 6;
|
|
47
|
+
reserve(ctx, PROP_CURSOR_SIZE + size + 11);
|
|
48
|
+
writePropCursor(ctx, def);
|
|
49
|
+
writeU8(ctx, ctx.operation);
|
|
50
|
+
writeBinaryRaw(ctx, buf);
|
|
51
|
+
};
|
|
52
|
+
//# sourceMappingURL=binary.js.map
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { PropDef, PropDefEdge } from '@based/schema/def';
|
|
2
|
+
import { Ctx } from '../Ctx.js';
|
|
3
|
+
export declare const writeCardinalityRaw: (ctx: Ctx, def: PropDef | PropDefEdge, val: any[], sizeFixBecauseEdgeIsDifferent?: number) => void;
|
|
4
|
+
export declare const writeCardinality: (ctx: Ctx, def: PropDef, val: any) => void;
|