fastdb4ts 0.0.2 → 0.2.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.
@@ -0,0 +1,44 @@
1
+ import { ArtifactSet, CodegenOptions, CodegenTarget } from './codegen.js';
2
+ export declare enum Profile {
3
+ RecordV1 = 1,
4
+ ObjectGraphV1 = 2
5
+ }
6
+ export declare class Capabilities {
7
+ readonly profile: Profile;
8
+ readonly semanticFlags: bigint;
9
+ readonly operationFlags: bigint;
10
+ readonly codegenTargetFlags: bigint;
11
+ readonly directBuildStatus: number;
12
+ constructor(profile: Profile, semanticFlags: bigint, operationFlags: bigint, codegenTargetFlags: bigint, directBuildStatus: number);
13
+ }
14
+ declare const specHandleValue: unique symbol;
15
+ export declare class CompiledSpec {
16
+ #private;
17
+ private constructor();
18
+ static compile(source: Uint8Array): CompiledSpec;
19
+ clone(): CompiledSpec;
20
+ dispose(): void;
21
+ [specHandleValue](): number;
22
+ canonicalJson(): Uint8Array;
23
+ manifestJson(): Uint8Array;
24
+ sha256(): Uint8Array;
25
+ profile(): Profile;
26
+ capabilities(): Capabilities;
27
+ generate(target: CodegenTarget, options?: CodegenOptions): ArtifactSet;
28
+ entryCount(): number;
29
+ entryId(index: number): string;
30
+ entryIndex(identifier: string): number;
31
+ componentCount(): number;
32
+ componentId(index: number): string;
33
+ componentIndex(identifier: string): number;
34
+ componentFieldCount(componentIndex: number): number;
35
+ componentFieldId(componentIndex: number, fieldIndex: number): string;
36
+ componentFieldIndex(componentIndex: number, identifier: string): number;
37
+ private requireHandle;
38
+ private queryBlob;
39
+ private queryCount;
40
+ private queryIndexedId;
41
+ private queryNamedIndex;
42
+ }
43
+ export declare function compiledSpecHandle(spec: CompiledSpec): number;
44
+ export {};
@@ -0,0 +1,220 @@
1
+ import { CAPABILITIES_SIZE, POINTER_SIZE, SHA256_SIZE, checkedU32, copyBytes, payloadModule, readU32, readU64, withAllocation, withInputBytes, } from './abi.js';
2
+ import { PayloadError, bindingError, checkStatus } from './error.js';
3
+ import { CodegenOptions, generateArtifacts, } from './codegen.js';
4
+ export var Profile;
5
+ (function (Profile) {
6
+ Profile[Profile["RecordV1"] = 1] = "RecordV1";
7
+ Profile[Profile["ObjectGraphV1"] = 2] = "ObjectGraphV1";
8
+ })(Profile || (Profile = {}));
9
+ export class Capabilities {
10
+ profile;
11
+ semanticFlags;
12
+ operationFlags;
13
+ codegenTargetFlags;
14
+ directBuildStatus;
15
+ constructor(profile, semanticFlags, operationFlags, codegenTargetFlags, directBuildStatus) {
16
+ this.profile = profile;
17
+ this.semanticFlags = semanticFlags;
18
+ this.operationFlags = operationFlags;
19
+ this.codegenTargetFlags = codegenTargetFlags;
20
+ this.directBuildStatus = directBuildStatus;
21
+ }
22
+ }
23
+ const encoder = new TextEncoder();
24
+ const decoder = new TextDecoder('utf-8', { fatal: true });
25
+ const handleConstructionToken = Symbol('fastdb.payload.specHandleConstructionToken');
26
+ const specHandleValue = Symbol('fastdb.payload.specHandleValue');
27
+ const specFinalizer = new FinalizationRegistry((handle) => {
28
+ try {
29
+ payloadModule()._fdb_payload_v1_spec_release(handle);
30
+ }
31
+ catch {
32
+ // Explicit dispose is authoritative; finalization is only a fallback.
33
+ }
34
+ });
35
+ export class CompiledSpec {
36
+ #handle;
37
+ #finalizerToken = {};
38
+ constructor(handle, token) {
39
+ if (token !== handleConstructionToken) {
40
+ throw new TypeError('CompiledSpec handles are created only by FastDB');
41
+ }
42
+ if (handle === 0) {
43
+ throw bindingError('FastDB Core returned success without a compiled spec', 'missing_spec_handle');
44
+ }
45
+ this.#handle = handle;
46
+ specFinalizer.register(this, handle, this.#finalizerToken);
47
+ }
48
+ static compile(source) {
49
+ const module = payloadModule();
50
+ return withInputBytes(module, source, (sourcePointer, sourceSize) => withAllocation(module, POINTER_SIZE * 2, (outputs) => {
51
+ const status = module._fdb_payload_v1_spec_compile_json(sourcePointer, sourceSize, 0, outputs, outputs + POINTER_SIZE);
52
+ checkStatus(status, outputs + POINTER_SIZE);
53
+ return new CompiledSpec(readU32(module, outputs), handleConstructionToken);
54
+ }));
55
+ }
56
+ clone() {
57
+ const handle = this.requireHandle();
58
+ payloadModule()._fdb_payload_v1_spec_retain(handle);
59
+ return new CompiledSpec(handle, handleConstructionToken);
60
+ }
61
+ dispose() {
62
+ if (this.#handle !== 0) {
63
+ const handle = this.#handle;
64
+ this.#handle = 0;
65
+ specFinalizer.unregister(this.#finalizerToken);
66
+ payloadModule()._fdb_payload_v1_spec_release(handle);
67
+ }
68
+ }
69
+ [specHandleValue]() {
70
+ return this.requireHandle();
71
+ }
72
+ canonicalJson() {
73
+ const module = payloadModule();
74
+ return this.queryBlob(module._fdb_payload_v1_spec_canonical_json.bind(module));
75
+ }
76
+ manifestJson() {
77
+ const module = payloadModule();
78
+ return this.queryBlob(module._fdb_payload_v1_spec_manifest_json.bind(module));
79
+ }
80
+ sha256() {
81
+ const module = payloadModule();
82
+ return withAllocation(module, SHA256_SIZE + POINTER_SIZE, (outputs) => {
83
+ const status = module._fdb_payload_v1_spec_sha256(this.requireHandle(), outputs, outputs + SHA256_SIZE);
84
+ checkStatus(status, outputs + SHA256_SIZE);
85
+ return module.HEAPU8.slice(outputs, outputs + SHA256_SIZE);
86
+ });
87
+ }
88
+ profile() {
89
+ const module = payloadModule();
90
+ return withAllocation(module, POINTER_SIZE * 2, (outputs) => {
91
+ const status = module._fdb_payload_v1_spec_profile(this.requireHandle(), outputs, outputs + POINTER_SIZE);
92
+ checkStatus(status, outputs + POINTER_SIZE);
93
+ return checkedProfile(readU32(module, outputs));
94
+ });
95
+ }
96
+ capabilities() {
97
+ const module = payloadModule();
98
+ return withAllocation(module, CAPABILITIES_SIZE + POINTER_SIZE, (outputs) => {
99
+ module._fdb_payload_v1_capabilities_init(outputs);
100
+ const status = module._fdb_payload_v1_spec_capabilities(this.requireHandle(), outputs, outputs + CAPABILITIES_SIZE);
101
+ checkStatus(status, outputs + CAPABILITIES_SIZE);
102
+ return new Capabilities(checkedProfile(readU32(module, outputs + 4)), readU64(module, outputs + 8), readU64(module, outputs + 16), readU64(module, outputs + 24), readU32(module, outputs + 32));
103
+ });
104
+ }
105
+ generate(target, options = new CodegenOptions()) {
106
+ return generateArtifacts(this.requireHandle(), target, options);
107
+ }
108
+ entryCount() {
109
+ const module = payloadModule();
110
+ return this.queryCount(module._fdb_payload_v1_spec_entry_count.bind(module));
111
+ }
112
+ entryId(index) {
113
+ const module = payloadModule();
114
+ return this.queryIndexedId(module._fdb_payload_v1_spec_entry_id.bind(module), checkedU32(index, 'entryIndex'));
115
+ }
116
+ entryIndex(identifier) {
117
+ const module = payloadModule();
118
+ return this.queryNamedIndex(module._fdb_payload_v1_spec_entry_index.bind(module), identifier);
119
+ }
120
+ componentCount() {
121
+ const module = payloadModule();
122
+ return this.queryCount(module._fdb_payload_v1_spec_component_count.bind(module));
123
+ }
124
+ componentId(index) {
125
+ const module = payloadModule();
126
+ return this.queryIndexedId(module._fdb_payload_v1_spec_component_id.bind(module), checkedU32(index, 'componentIndex'));
127
+ }
128
+ componentIndex(identifier) {
129
+ const module = payloadModule();
130
+ return this.queryNamedIndex(module._fdb_payload_v1_spec_component_index.bind(module), identifier);
131
+ }
132
+ componentFieldCount(componentIndex) {
133
+ const module = payloadModule();
134
+ return withAllocation(module, POINTER_SIZE * 2, (outputs) => {
135
+ const status = module._fdb_payload_v1_spec_component_field_count(this.requireHandle(), checkedU32(componentIndex, 'componentIndex'), outputs, outputs + POINTER_SIZE);
136
+ checkStatus(status, outputs + POINTER_SIZE);
137
+ return readU32(module, outputs);
138
+ });
139
+ }
140
+ componentFieldId(componentIndex, fieldIndex) {
141
+ const module = payloadModule();
142
+ const bytes = this.queryBlob((spec, outBlob, outError) => module._fdb_payload_v1_spec_component_field_id(spec, checkedU32(componentIndex, 'componentIndex'), checkedU32(fieldIndex, 'fieldIndex'), outBlob, outError));
143
+ try {
144
+ return decoder.decode(bytes);
145
+ }
146
+ catch (error) {
147
+ throw bindingError(`FastDB Core returned a non-UTF-8 field identifier: ${String(error)}`, 'invalid_utf8_identifier');
148
+ }
149
+ }
150
+ componentFieldIndex(componentIndex, identifier) {
151
+ const module = payloadModule();
152
+ const bytes = encoder.encode(identifier);
153
+ return withInputBytes(module, bytes, (data, size) => withAllocation(module, POINTER_SIZE * 2, (outputs) => {
154
+ const status = module._fdb_payload_v1_spec_component_field_index(this.requireHandle(), checkedU32(componentIndex, 'componentIndex'), data, size, outputs, outputs + POINTER_SIZE);
155
+ checkStatus(status, outputs + POINTER_SIZE);
156
+ return readU32(module, outputs);
157
+ }));
158
+ }
159
+ requireHandle() {
160
+ if (this.#handle === 0) {
161
+ throw bindingError('CompiledSpec is disposed', 'disposed_handle');
162
+ }
163
+ return this.#handle;
164
+ }
165
+ queryBlob(query) {
166
+ const module = payloadModule();
167
+ return withAllocation(module, POINTER_SIZE * 2, (outputs) => {
168
+ const status = query(this.requireHandle(), outputs, outputs + POINTER_SIZE);
169
+ checkStatus(status, outputs + POINTER_SIZE);
170
+ const blob = readU32(module, outputs);
171
+ if (blob === 0) {
172
+ throw bindingError('FastDB Core returned success without a blob', 'missing_blob_handle');
173
+ }
174
+ try {
175
+ return copyBytes(module, module._fdb_payload_v1_blob_data(blob), module._fdb_payload_v1_blob_size(blob));
176
+ }
177
+ finally {
178
+ module._fdb_payload_v1_blob_release(blob);
179
+ }
180
+ });
181
+ }
182
+ queryCount(query) {
183
+ const module = payloadModule();
184
+ return withAllocation(module, POINTER_SIZE * 2, (outputs) => {
185
+ const status = query(this.requireHandle(), outputs, outputs + POINTER_SIZE);
186
+ checkStatus(status, outputs + POINTER_SIZE);
187
+ return readU32(module, outputs);
188
+ });
189
+ }
190
+ queryIndexedId(query, index) {
191
+ const bytes = this.queryBlob((spec, outBlob, outError) => query(spec, index, outBlob, outError));
192
+ try {
193
+ return decoder.decode(bytes);
194
+ }
195
+ catch (error) {
196
+ throw bindingError(`FastDB Core returned a non-UTF-8 identifier: ${String(error)}`, 'invalid_utf8_identifier');
197
+ }
198
+ }
199
+ queryNamedIndex(query, identifier) {
200
+ const module = payloadModule();
201
+ const bytes = encoder.encode(identifier);
202
+ return withInputBytes(module, bytes, (data, size) => withAllocation(module, POINTER_SIZE * 2, (outputs) => {
203
+ const status = query(this.requireHandle(), data, size, outputs, outputs + POINTER_SIZE);
204
+ checkStatus(status, outputs + POINTER_SIZE);
205
+ return readU32(module, outputs);
206
+ }));
207
+ }
208
+ }
209
+ export function compiledSpecHandle(spec) {
210
+ if (!(spec instanceof CompiledSpec)) {
211
+ throw new TypeError('spec must be CompiledSpec');
212
+ }
213
+ return CompiledSpec.prototype[specHandleValue].call(spec);
214
+ }
215
+ function checkedProfile(value) {
216
+ if (value === Profile.RecordV1 || value === Profile.ObjectGraphV1) {
217
+ return value;
218
+ }
219
+ throw new PayloadError(7002, 'UNSUPPORTED_ABI', '/profile', 'FastDB Core returned an unknown payload profile', `{"actual":${value},"reason":"unknown_profile"}`);
220
+ }
package/dist/schema.js CHANGED
@@ -1,3 +1,4 @@
1
+ // Standalone schema metadata; not fastdb.payload.v1 authority.
1
2
  import { FastdbSchemaError } from './errors.js';
2
3
  import { isListField, isNumericField, isRefField, isScalarField, } from './types.js';
3
4
  const SCHEMA_CACHE = new WeakMap();
@@ -1,7 +1,8 @@
1
1
  import { Feature, type FeatureClass } from './feature.js';
2
+ import { type FastdbDatabaseBytes } from './database-buffer.js';
2
3
  export declare class FastSerializer {
3
4
  static dumps(obj: Feature): Uint8Array;
4
- static loads<T extends Feature>(data: Uint8Array | ArrayBuffer, rootType: FeatureClass<T>): T | null;
5
+ static loads<T extends Feature>(data: FastdbDatabaseBytes, rootType: FeatureClass<T>): T | null;
5
6
  }
6
7
  declare class ByteWriter {
7
8
  private buf;
@@ -1,8 +1,10 @@
1
+ // Legacy standalone serializer; not fastdb.payload.v1 or an external RPC format.
1
2
  import { createFeature, Feature } from './feature.js';
2
3
  import { getClassSchema, resolveListItem, } from './schema.js';
3
4
  import { FastdbRuntimeError, FastdbUsageError } from './errors.js';
5
+ import { loadDatabaseFromBytes } from './database-buffer.js';
4
6
  import { getInitializedFastdbModule } from './wasm-loader.js';
5
- import { isListField, isRefField } from './types.js';
7
+ import { coerceBoolScalar, isListField, isRefField } from './types.js';
6
8
  const NUMERIC_LIST_LAYER_PREFIX = '__fastser_list__|';
7
9
  const TEXT_ENCODER = new TextEncoder();
8
10
  const TEXT_DECODER = new TextDecoder();
@@ -85,7 +87,7 @@ export class FastSerializer {
85
87
  }
86
88
  switch (field.entry.kind) {
87
89
  case 'bool':
88
- layer.setFieldInt(dbFieldIndex, value ? 1 : 0);
90
+ layer.setFieldInt(dbFieldIndex, coerceBoolScalar(value) ? 1 : 0);
89
91
  break;
90
92
  case 'u8':
91
93
  case 'u16':
@@ -124,8 +126,7 @@ export class FastSerializer {
124
126
  }
125
127
  static loads(data, rootType) {
126
128
  const module = getInitializedFastdbModule();
127
- const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
128
- const db = withHeapBytes(module, bytes, (ptr, size) => module.WxDatabase.loadFromHeap(ptr, size));
129
+ const db = loadDatabaseFromBytes(module, data);
129
130
  if (db.getLayerCount() === 0) {
130
131
  return null;
131
132
  }
package/dist/table.js CHANGED
@@ -49,6 +49,9 @@ export class Table {
49
49
  if (!def) {
50
50
  return undefined;
51
51
  }
52
+ if (def.entry.kind === 'list') {
53
+ throw new FastdbRuntimeError(`Column accessor "${prop}" is a list field; read it from table rows instead of as a strided column.`);
54
+ }
52
55
  return new StridedColumn(table.module, layer, def.index, def.entry.originType);
53
56
  },
54
57
  });
package/dist/types.d.ts CHANGED
@@ -23,6 +23,7 @@ export type ListItemDef = FieldTypeDef<ScalarFieldKind> | FeatureClassLike | (()
23
23
  export interface ListFieldDef extends FieldTypeDef<'list'> {
24
24
  item: ListItemDef;
25
25
  }
26
+ export declare function coerceBoolScalar(value: unknown): boolean;
26
27
  export declare const BOOL: FieldTypeDef<"bool">;
27
28
  export declare const U8: FieldTypeDef<"u8">;
28
29
  export declare const U16: FieldTypeDef<"u16">;
package/dist/types.js CHANGED
@@ -1,3 +1,34 @@
1
+ import { FastdbUsageError } from './errors.js';
2
+ const TRUE_BOOL_STRINGS = new Set(['1', 'true', 't', 'yes', 'y', 'on']);
3
+ const FALSE_BOOL_STRINGS = new Set(['0', 'false', 'f', 'no', 'n', 'off']);
4
+ export function coerceBoolScalar(value) {
5
+ if (typeof value === 'boolean') {
6
+ return value;
7
+ }
8
+ if (typeof value === 'string') {
9
+ const normalized = value.trim().toLowerCase();
10
+ if (TRUE_BOOL_STRINGS.has(normalized)) {
11
+ return true;
12
+ }
13
+ if (FALSE_BOOL_STRINGS.has(normalized)) {
14
+ return false;
15
+ }
16
+ throw new FastdbUsageError(`cannot coerce ${JSON.stringify(value)} to fastdb bool scalar; expected bool, 0/1, or true/false string.`);
17
+ }
18
+ if (typeof value === 'number') {
19
+ if (value === 0 || value === 1) {
20
+ return value === 1;
21
+ }
22
+ throw new FastdbUsageError(`cannot coerce ${String(value)} to fastdb bool scalar; expected bool, 0/1, or true/false string.`);
23
+ }
24
+ if (typeof value === 'bigint') {
25
+ if (value === 0n || value === 1n) {
26
+ return value === 1n;
27
+ }
28
+ throw new FastdbUsageError(`cannot coerce ${String(value)} to fastdb bool scalar; expected bool, 0/1, or true/false string.`);
29
+ }
30
+ throw new FastdbUsageError(`cannot coerce ${String(value)} to fastdb bool scalar; expected bool, 0/1, or true/false string.`);
31
+ }
1
32
  function makeFieldType(kind, originType, options) {
2
33
  return Object.freeze({
3
34
  kind,