rayforce-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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 RayforceDB
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,305 @@
1
+ # RayforceDB WASM SDK
2
+
3
+ Full-featured JavaScript SDK for [RayforceDB](https://rayforcedb.com) with zero-copy ArrayBuffer views over native vectors.
4
+
5
+ ## Features
6
+
7
+ - 🚀 **Zero-Copy Data Access** - TypedArray views directly over WASM memory
8
+ - 📊 **Full Type System** - All Rayforce types: scalars, vectors, lists, dicts, tables
9
+ - 🔍 **Query Builder** - Fluent API for building and executing queries
10
+ - 💾 **Memory Efficient** - No data copying for vector operations
11
+ - 🌐 **CDN Ready** - UMD bundle for script tag usage
12
+ - ⚡ **SIMD Optimized** - Native WASM SIMD support
13
+
14
+ ## Quick Start
15
+
16
+ ### ES6 Module
17
+
18
+ ```javascript
19
+ import { createRayforceSDK, Types } from 'rayforce-wasm';
20
+
21
+ // Initialize
22
+ const createRayforce = (await import('./rayforce.js')).default;
23
+ const wasm = await createRayforce();
24
+ const rf = createRayforceSDK(wasm);
25
+
26
+ // Evaluate expressions
27
+ const result = rf.eval('(+ 1 2 3)');
28
+ console.log(result.toJS()); // 6
29
+
30
+ // Create vectors with zero-copy access
31
+ const vec = rf.vector(Types.I64, [1, 2, 3, 4, 5]);
32
+ const view = vec.typedArray; // BigInt64Array - no copy!
33
+ view[0] = 100n;
34
+ console.log(vec.toJS()); // [100, 2, 3, 4, 5]
35
+ ```
36
+
37
+ ### CDN / Script Tag
38
+
39
+ ```html
40
+ <script src="https://cdn.jsdelivr.net/npm/rayforce-wasm/dist/rayforce.umd.js"></script>
41
+ <script>
42
+ Rayforce.init().then(rf => {
43
+ console.log(rf.eval('(sum (til 100))').toJS()); // 4950
44
+ });
45
+ </script>
46
+ ```
47
+
48
+ ## Installation
49
+
50
+ ```bash
51
+ # From npm (coming soon)
52
+ npm install rayforce-wasm
53
+
54
+ # Build from source
55
+ make wasm
56
+ ```
57
+
58
+ ## API Reference
59
+
60
+ ### Initialization
61
+
62
+ ```javascript
63
+ // ES6
64
+ import { createRayforceSDK, Types } from './rayforce.sdk.js';
65
+ const rf = createRayforceSDK(wasmModule);
66
+
67
+ // UMD
68
+ const rf = await Rayforce.init({ wasmPath: './rayforce.js' });
69
+ ```
70
+
71
+ ### Evaluation
72
+
73
+ ```javascript
74
+ // Evaluate Rayfall expression
75
+ const result = rf.eval('(+ 1 2 3)');
76
+ console.log(result.toJS()); // 6
77
+
78
+ // With source tracking (for better error messages)
79
+ const result = rf.eval('(sum data)', 'myfile.ray');
80
+ ```
81
+
82
+ ### Type Constructors
83
+
84
+ ```javascript
85
+ // Scalars
86
+ rf.b8(true) // Boolean
87
+ rf.i64(42) // 64-bit integer
88
+ rf.f64(3.14) // 64-bit float
89
+ rf.symbol('name') // Interned symbol
90
+ rf.string('hello') // String
91
+ rf.date(new Date()) // Date
92
+ rf.time(new Date()) // Time
93
+ rf.timestamp(new Date()) // Timestamp
94
+
95
+ // Vectors (with zero-copy TypedArray access)
96
+ rf.vector(Types.I64, [1, 2, 3])
97
+ rf.vector(Types.F64, 1000000) // Pre-allocate
98
+
99
+ // Containers
100
+ rf.list([1, 'two', 3.0]) // Mixed-type list
101
+ rf.dict({ a: 1, b: 2 }) // Dictionary
102
+ rf.table({ // Table
103
+ id: [1, 2, 3],
104
+ name: ['Alice', 'Bob', 'Carol'],
105
+ score: [95.5, 87.3, 92.1]
106
+ })
107
+ ```
108
+
109
+ ### Zero-Copy Vector Access
110
+
111
+ ```javascript
112
+ // Create vector
113
+ const vec = rf.vector(Types.F64, 1000000);
114
+
115
+ // Get TypedArray view (zero-copy!)
116
+ const view = vec.typedArray; // Float64Array
117
+
118
+ // Fast iteration
119
+ for (let i = 0; i < view.length; i++) {
120
+ view[i] = Math.random();
121
+ }
122
+
123
+ // Convert to JS array (copies data)
124
+ const jsArray = vec.toJS();
125
+ ```
126
+
127
+ ### Tables
128
+
129
+ ```javascript
130
+ // Create table
131
+ const table = rf.table({
132
+ id: [1, 2, 3],
133
+ name: ['Alice', 'Bob', 'Carol'],
134
+ score: [95.5, 87.3, 92.1]
135
+ });
136
+
137
+ // Access columns
138
+ const names = table.col('name'); // Vector
139
+ const scores = table.col('score'); // Vector
140
+
141
+ // Access rows
142
+ const row = table.row(0); // Dict
143
+ console.log(row.toJS()); // { id: 1, name: 'Alice', score: 95.5 }
144
+
145
+ // Convert to arrays
146
+ console.log(table.toJS()); // { id: [...], name: [...], score: [...] }
147
+ console.log(table.toRows()); // [{ id: 1, ... }, { id: 2, ... }, ...]
148
+
149
+ // Metadata
150
+ console.log(table.columnNames()); // ['id', 'name', 'score']
151
+ console.log(table.rowCount); // 3
152
+ ```
153
+
154
+ ### Query Builder
155
+
156
+ ```javascript
157
+ // Select with filter
158
+ const result = table
159
+ .select('name', 'score')
160
+ .where(rf.col('score').gt(90))
161
+ .execute();
162
+
163
+ // Aggregations
164
+ const stats = table
165
+ .select('department')
166
+ .withColumn('avg_score', rf.col('score').avg())
167
+ .withColumn('max_score', rf.col('score').max())
168
+ .groupBy('department')
169
+ .execute();
170
+
171
+ // Using expressions
172
+ const expr = rf.col('score').gt(80).and(rf.col('active').eq(true));
173
+ const filtered = table.where(expr).execute();
174
+ ```
175
+
176
+ ### Type Constants
177
+
178
+ ```javascript
179
+ import { Types } from 'rayforce-wasm';
180
+
181
+ Types.LIST // 0 - Mixed-type list
182
+ Types.B8 // 1 - Boolean
183
+ Types.U8 // 2 - Unsigned byte
184
+ Types.I16 // 3 - 16-bit integer
185
+ Types.I32 // 4 - 32-bit integer
186
+ Types.I64 // 5 - 64-bit integer
187
+ Types.SYMBOL // 6 - Interned string
188
+ Types.DATE // 7 - Date
189
+ Types.TIME // 8 - Time
190
+ Types.TIMESTAMP // 9 - Timestamp
191
+ Types.F64 // 10 - 64-bit float
192
+ Types.GUID // 11 - UUID
193
+ Types.C8 // 12 - Character
194
+ Types.TABLE // 98 - Table
195
+ Types.DICT // 99 - Dictionary
196
+ ```
197
+
198
+ ### Expression Builder
199
+
200
+ ```javascript
201
+ // Column reference
202
+ const col = rf.col('price');
203
+
204
+ // Comparisons
205
+ col.eq(100) // =
206
+ col.ne(100) // <>
207
+ col.lt(100) // <
208
+ col.le(100) // <=
209
+ col.gt(100) // >
210
+ col.ge(100) // >=
211
+
212
+ // Logical
213
+ col.gt(50).and(col.lt(100))
214
+ col.eq(0).or(col.eq(100))
215
+ col.gt(0).not()
216
+
217
+ // Aggregations
218
+ col.sum()
219
+ col.avg()
220
+ col.min()
221
+ col.max()
222
+ col.count()
223
+ col.first()
224
+ col.last()
225
+ col.distinct()
226
+ ```
227
+
228
+ ## Build from Source
229
+
230
+ ### Prerequisites
231
+
232
+ - [Emscripten SDK](https://emscripten.org/docs/getting_started/downloads.html)
233
+ - Make
234
+
235
+ ### Build
236
+
237
+ ```bash
238
+ # Clone repository
239
+ git clone https://github.com/RayforceDB/rayforce-wasm.git
240
+ cd rayforce-wasm
241
+
242
+ # Install Emscripten (if not already installed)
243
+ git clone https://github.com/emscripten-core/emsdk.git
244
+ cd emsdk && ./emsdk install latest && ./emsdk activate latest
245
+ source emsdk_env.sh
246
+ cd ..
247
+
248
+ # Build from GitHub sources
249
+ make app
250
+
251
+ # Or build from local ../rayforce sources
252
+ make dev
253
+
254
+ # Start dev server
255
+ make serve
256
+ # Open http://localhost:8080/examples/
257
+ ```
258
+
259
+ ### Build Outputs
260
+
261
+ ```
262
+ dist/
263
+ ├── rayforce.js # WASM loader (ES6)
264
+ ├── rayforce.wasm # WASM binary
265
+ ├── rayforce.sdk.js # SDK module (ES6)
266
+ ├── rayforce.umd.js # SDK bundle (UMD)
267
+ └── index.js # Entry point
268
+ ```
269
+
270
+ ## Performance
271
+
272
+ The SDK provides zero-copy access to WASM memory through TypedArray views:
273
+
274
+ ```javascript
275
+ // Create 1M element vector - ~8ms
276
+ const vec = rf.vector(Types.F64, 1000000);
277
+
278
+ // Get TypedArray view - ~0.01ms (just creates view)
279
+ const view = vec.typedArray;
280
+
281
+ // Fill via TypedArray - ~15ms
282
+ for (let i = 0; i < view.length; i++) {
283
+ view[i] = Math.random();
284
+ }
285
+
286
+ // Native sum - ~2ms
287
+ const sum = rf.eval(`(sum ${vec.toString()})`);
288
+ ```
289
+
290
+ ## Browser Support
291
+
292
+ - Chrome 89+ (WASM SIMD)
293
+ - Firefox 89+ (WASM SIMD)
294
+ - Safari 15+ (WASM SIMD)
295
+ - Edge 89+ (WASM SIMD)
296
+
297
+ ## License
298
+
299
+ MIT License - see [LICENSE](LICENSE)
300
+
301
+ ## Links
302
+
303
+ - [RayforceDB](https://rayforcedb.com)
304
+ - [Documentation](https://rayforcedb.com/docs/)
305
+ - [GitHub](https://github.com/RayforceDB/rayforce)
package/dist/index.js ADDED
@@ -0,0 +1,215 @@
1
+ /**
2
+ * RayforceDB JavaScript SDK - Main Entry Point
3
+ *
4
+ * This is the main entry point for the RayforceDB SDK.
5
+ * It initializes the WASM module and provides the full SDK API.
6
+ *
7
+ * Usage (ESM):
8
+ * import { init } from 'rayforce';
9
+ * const rf = await init();
10
+ * const result = rf.eval('(+ 1 2)');
11
+ *
12
+ * Usage (CDN script tag):
13
+ * <script src="https://cdn.../rayforce.umd.js"></script>
14
+ * <script>
15
+ * Rayforce.init().then(rf => {
16
+ * console.log(rf.eval('(+ 1 2)').toJS());
17
+ * });
18
+ * </script>
19
+ *
20
+ * @module rayforce
21
+ * @version 0.1.0
22
+ */
23
+
24
+ import { createRayforceSDK, Types, Expr } from './rayforce.sdk.js';
25
+
26
+ // SDK version
27
+ export const version = '0.1.0';
28
+
29
+ // Re-export types and utilities
30
+ export { Types, Expr };
31
+ export * from './rayforce.sdk.js';
32
+
33
+ // Global SDK instance (for singleton pattern)
34
+ let _sdkInstance = null;
35
+ let _initPromise = null;
36
+
37
+ /**
38
+ * Initialize the RayforceDB SDK.
39
+ *
40
+ * @param {Object} [options] - Configuration options
41
+ * @param {string} [options.wasmPath] - Custom path to rayforce.js WASM loader
42
+ * @param {boolean} [options.singleton=true] - Use singleton pattern (reuse instance)
43
+ * @param {Function} [options.onReady] - Callback when WASM is fully initialized
44
+ * @returns {Promise<RayforceSDK>} The initialized SDK instance
45
+ *
46
+ * @example
47
+ * // Basic usage
48
+ * const rf = await init();
49
+ *
50
+ * // Custom WASM path
51
+ * const rf = await init({ wasmPath: './dist/rayforce.js' });
52
+ *
53
+ * // Force new instance
54
+ * const rf = await init({ singleton: false });
55
+ */
56
+ export async function init(options = {}) {
57
+ const {
58
+ wasmPath = './rayforce.js',
59
+ singleton = true,
60
+ onReady = null,
61
+ } = options;
62
+
63
+ // Return existing instance if singleton
64
+ if (singleton && _sdkInstance !== null) {
65
+ return _sdkInstance;
66
+ }
67
+
68
+ // Wait for existing init if in progress
69
+ if (singleton && _initPromise !== null) {
70
+ return _initPromise;
71
+ }
72
+
73
+ const initFn = async () => {
74
+ try {
75
+ // Dynamic import of WASM module
76
+ const wasmModule = await import(wasmPath);
77
+ const createRayforce = wasmModule.default;
78
+
79
+ // Initialize WASM
80
+ const wasm = await createRayforce({
81
+ // Optional: configure WASM module
82
+ rayforce_ready: (msg) => {
83
+ if (onReady) onReady(msg);
84
+ }
85
+ });
86
+
87
+ // Create SDK wrapper
88
+ const sdk = createRayforceSDK(wasm);
89
+
90
+ if (singleton) {
91
+ _sdkInstance = sdk;
92
+ }
93
+
94
+ return sdk;
95
+ } catch (error) {
96
+ if (singleton) {
97
+ _initPromise = null;
98
+ }
99
+ throw new Error(`Failed to initialize RayforceDB: ${error.message}`);
100
+ }
101
+ };
102
+
103
+ if (singleton) {
104
+ _initPromise = initFn();
105
+ return _initPromise;
106
+ }
107
+
108
+ return initFn();
109
+ }
110
+
111
+ /**
112
+ * Get the singleton SDK instance (must call init() first)
113
+ * @returns {RayforceSDK|null}
114
+ */
115
+ export function getInstance() {
116
+ return _sdkInstance;
117
+ }
118
+
119
+ /**
120
+ * Check if SDK is initialized
121
+ * @returns {boolean}
122
+ */
123
+ export function isInitialized() {
124
+ return _sdkInstance !== null;
125
+ }
126
+
127
+ /**
128
+ * Reset the singleton instance (for testing)
129
+ */
130
+ export function reset() {
131
+ _sdkInstance = null;
132
+ _initPromise = null;
133
+ }
134
+
135
+ // ============================================================================
136
+ // Convenience Functions (work on singleton instance)
137
+ // ============================================================================
138
+
139
+ /**
140
+ * Evaluate a Rayfall expression using singleton instance
141
+ * @param {string} code
142
+ * @returns {RayObject}
143
+ */
144
+ export function evaluate(code) {
145
+ if (!_sdkInstance) {
146
+ throw new Error('RayforceDB not initialized. Call init() first.');
147
+ }
148
+ return _sdkInstance.eval(code);
149
+ }
150
+
151
+ /**
152
+ * Create common types using singleton instance
153
+ */
154
+ export const create = {
155
+ get i64() {
156
+ if (!_sdkInstance) throw new Error('RayforceDB not initialized');
157
+ return (v) => _sdkInstance.i64(v);
158
+ },
159
+ get f64() {
160
+ if (!_sdkInstance) throw new Error('RayforceDB not initialized');
161
+ return (v) => _sdkInstance.f64(v);
162
+ },
163
+ get string() {
164
+ if (!_sdkInstance) throw new Error('RayforceDB not initialized');
165
+ return (v) => _sdkInstance.string(v);
166
+ },
167
+ get symbol() {
168
+ if (!_sdkInstance) throw new Error('RayforceDB not initialized');
169
+ return (v) => _sdkInstance.symbol(v);
170
+ },
171
+ get vector() {
172
+ if (!_sdkInstance) throw new Error('RayforceDB not initialized');
173
+ return (type, data) => _sdkInstance.vector(type, data);
174
+ },
175
+ get list() {
176
+ if (!_sdkInstance) throw new Error('RayforceDB not initialized');
177
+ return (items) => _sdkInstance.list(items);
178
+ },
179
+ get dict() {
180
+ if (!_sdkInstance) throw new Error('RayforceDB not initialized');
181
+ return (obj) => _sdkInstance.dict(obj);
182
+ },
183
+ get table() {
184
+ if (!_sdkInstance) throw new Error('RayforceDB not initialized');
185
+ return (cols) => _sdkInstance.table(cols);
186
+ },
187
+ get date() {
188
+ if (!_sdkInstance) throw new Error('RayforceDB not initialized');
189
+ return (v) => _sdkInstance.date(v);
190
+ },
191
+ get time() {
192
+ if (!_sdkInstance) throw new Error('RayforceDB not initialized');
193
+ return (v) => _sdkInstance.time(v);
194
+ },
195
+ get timestamp() {
196
+ if (!_sdkInstance) throw new Error('RayforceDB not initialized');
197
+ return (v) => _sdkInstance.timestamp(v);
198
+ },
199
+ };
200
+
201
+ // ============================================================================
202
+ // Default Export
203
+ // ============================================================================
204
+
205
+ export default {
206
+ init,
207
+ getInstance,
208
+ isInitialized,
209
+ reset,
210
+ evaluate,
211
+ create,
212
+ Types,
213
+ Expr,
214
+ version,
215
+ };