@milaboratories/pframes-rs-wasm 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/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@milaboratories/pframes-rs-wasm",
3
+ "version": "0.1.0",
4
+ "files": [
5
+ "generated",
6
+ "src"
7
+ ],
8
+ "type": "module",
9
+ "exports": {
10
+ ".": "./src/index.ts"
11
+ },
12
+ "devDependencies": {
13
+ "@bytecodealliance/jco": "^1.16.1",
14
+ "@bytecodealliance/preview2-shim": "0.17.8",
15
+ "@milaboratories/pl-model-common": "1.24.4",
16
+ "@milaboratories/pl-model-middle-layer": "1.11.5",
17
+ "@milaboratories/ts-configs": "1.2.0",
18
+ "rimraf": "^6.1.2",
19
+ "typescript": "^5.9.3"
20
+ },
21
+ "peerDependencies": {
22
+ "@bytecodealliance/preview2-shim": "0.17.8",
23
+ "@milaboratories/pl-model-common": "1.24.4",
24
+ "@milaboratories/pl-model-middle-layer": "1.11.5"
25
+ },
26
+ "scripts": {
27
+ "component-transpile": "rimraf generated && jco transpile -o generated --no-nodejs-compat --no-namespaced-exports",
28
+ "build:rust": "cargo build",
29
+ "build:ts": "node --run component-transpile -- ../../target/wasm32-wasip2/debug/pframes_rs_wasm.wasm",
30
+ "build": "node --run build:rust && node --run build:ts",
31
+ "postbuild": "rimraf package.tgz && pnpm pack --out package.tgz",
32
+ "ci-build:rust": "cargo build -r --locked",
33
+ "ci-build:ts": "node --run component-transpile -- ../../target/wasm32-wasip2/release/pframes_rs_wasm.wasm -O -- -O4",
34
+ "ci-build": "node --run ci-build:rust && node --run ci-build:ts",
35
+ "postci-build": "node --run postbuild",
36
+ "preci-test": "node --run ci-build",
37
+ "ci-test": "exit 0",
38
+ "pretest": "node --run build",
39
+ "test": "exit 0"
40
+ }
41
+ }
package/src/axes.ts ADDED
@@ -0,0 +1,20 @@
1
+ import type {
2
+ AxesId,
3
+ AxesSpec,
4
+ SingleAxisSelector
5
+ } from '@milaboratories/pl-model-common';
6
+ import { spec as bindings } from '../generated/pframes_rs_wasm.js';
7
+
8
+ export function expand(spec: AxesSpec): AxesId {
9
+ return JSON.parse(bindings.Axes.fromJson(JSON.stringify(spec)).expand());
10
+ }
11
+
12
+ export function collapse(ids: AxesId): AxesSpec {
13
+ return JSON.parse(bindings.Axes.collapse(JSON.stringify(ids)));
14
+ }
15
+
16
+ export function find(spec: AxesSpec, selector: SingleAxisSelector): number {
17
+ return JSON.parse(
18
+ bindings.Axes.fromJson(JSON.stringify(spec)).find(JSON.stringify(selector))
19
+ );
20
+ }
package/src/index.ts ADDED
@@ -0,0 +1,15 @@
1
+ import type { PFrameInternal } from '@milaboratories/pl-model-middle-layer';
2
+ import * as axes from './axes';
3
+ import { PFrame } from './p-frame';
4
+
5
+ export const {
6
+ createPFrame,
7
+ expandAxes,
8
+ collapseAxes,
9
+ findAxis
10
+ }: PFrameInternal.PFrameWasmAPI = {
11
+ createPFrame: (spec) => new PFrame(spec),
12
+ expandAxes: axes.expand,
13
+ collapseAxes: axes.collapse,
14
+ findAxis: axes.find
15
+ };
package/src/p-frame.ts ADDED
@@ -0,0 +1,110 @@
1
+ import type {
2
+ JoinEntry,
3
+ PColumnSpec,
4
+ PColumnValue,
5
+ PObjectId,
6
+ QuerySpec
7
+ } from '@milaboratories/pl-model-common';
8
+ import type { PFrameInternal } from '@milaboratories/pl-model-middle-layer';
9
+ import { spec as bindings } from '../generated/pframes_rs_wasm.js';
10
+
11
+ export class PFrame implements PFrameInternal.PFrameWasm {
12
+ #frame: bindings.Frame;
13
+ #disposables: DisposableStack;
14
+
15
+ constructor(spec: Record<string, PColumnSpec>) {
16
+ using disposer = new DisposableStack();
17
+ this.#frame = bindings.Frame.fromJson(JSON.stringify(spec));
18
+ // Unfortunately, jco generates incorrect types
19
+ // actual spec.PFrame is indeed Disposable
20
+ disposer.use(this.#frame as unknown as Disposable);
21
+ this.#disposables = disposer.move();
22
+ }
23
+
24
+ deleteColumns(
25
+ request: PFrameInternal.DeleteColumnFromColumnsRequest
26
+ ): PFrameInternal.DeleteColumnFromColumnsResponse {
27
+ return JSON.parse(bindings.Frame.deleteColumns(JSON.stringify(request)));
28
+ }
29
+
30
+ findColumns(
31
+ request: PFrameInternal.FindColumnsRequest
32
+ ): PFrameInternal.FindColumnsResponse {
33
+ return JSON.parse(this.#frame.findColumns(JSON.stringify(request)));
34
+ }
35
+
36
+ evaluateQuery(request: QuerySpec): PFrameInternal.EvaluateQueryResponse {
37
+ return JSON.parse(this.#frame.evaluateQuery(JSON.stringify(request)));
38
+ }
39
+
40
+ rewriteLegacyQuery(request: PFrameInternal.LegacyQuery): QuerySpec {
41
+ const src = joinEntryToInternal(request.src);
42
+ return JSON.parse(
43
+ this.#frame.rewriteLegacyQuery(JSON.stringify({ ...request, src }))
44
+ );
45
+ }
46
+
47
+ [Symbol.dispose](): void {
48
+ this.#disposables.dispose();
49
+ }
50
+ }
51
+
52
+ function joinEntryToInternal(
53
+ entry: JoinEntry<PObjectId>
54
+ ): PFrameInternal.JoinEntryV4 {
55
+ const type = entry.type;
56
+ switch (type) {
57
+ case 'column':
58
+ return {
59
+ type: 'column',
60
+ columnId: entry.column
61
+ };
62
+ case 'slicedColumn':
63
+ return {
64
+ type: 'slicedColumn',
65
+ columnId: entry.column,
66
+ newId: entry.newId,
67
+ axisFilters: entry.axisFilters
68
+ };
69
+ case 'artificialColumn':
70
+ return {
71
+ type: 'artificialColumn',
72
+ columnId: entry.column,
73
+ newId: entry.newId,
74
+ axesIndices: entry.axesIndices
75
+ };
76
+ case 'inlineColumn':
77
+ return {
78
+ type: 'inlineColumn',
79
+ newId: entry.column.id,
80
+ spec: entry.column.spec,
81
+ dataInfo: {
82
+ type: 'Json',
83
+ keyLength: entry.column.spec.axesSpec.length,
84
+ data: entry.column.data.reduce(
85
+ (acc, row) => {
86
+ acc[JSON.stringify(row.key)] = row.val;
87
+ return acc;
88
+ },
89
+ {} as Record<string, PColumnValue>
90
+ )
91
+ }
92
+ };
93
+ case 'inner':
94
+ case 'full':
95
+ return {
96
+ type: entry.type,
97
+ entries: entry.entries.map((col) => joinEntryToInternal(col))
98
+ };
99
+ case 'outer':
100
+ return {
101
+ type: 'outer',
102
+ primary: joinEntryToInternal(entry.primary),
103
+ secondary: entry.secondary.map((col) => joinEntryToInternal(col))
104
+ };
105
+ default:
106
+ throw new Error(
107
+ `unsupported PFrame join entry type: ${type satisfies never}`
108
+ );
109
+ }
110
+ }