@formata/stof 0.9.6

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/dist/doc.js ADDED
@@ -0,0 +1,194 @@
1
+ //
2
+ // Copyright 2025 Formata, Inc. All rights reserved.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ //
16
+ // @deno-types="./pkg/stof.d.ts"
17
+ import init, { Stof, StofFunc } from './pkg/stof.js';
18
+ // @deno-types="./pkg/stof.d.ts"
19
+ export * from './pkg/stof.js';
20
+ /**
21
+ * Template function for a document.
22
+ * Stof must be initialized before with `await Doc.initialize()`.
23
+ */
24
+ export function stof(strings, ...values) {
25
+ const doc = new StofDoc();
26
+ let result = '';
27
+ for (let i = 0; i < strings.length; i++) {
28
+ result += strings[i];
29
+ if (i < values.length)
30
+ result += values[i];
31
+ }
32
+ doc.parse(result);
33
+ return doc;
34
+ }
35
+ /**
36
+ * Stof document.
37
+ */
38
+ export class StofDoc {
39
+ /**
40
+ * Initialize Stof WASM.
41
+ */
42
+ static async initialize(data) {
43
+ // @ts-ignore this exists
44
+ return StofDoc.initialized ?? await (StofDoc.initialized = init(data));
45
+ }
46
+ /**
47
+ * Constructor.
48
+ * Make sure to call initalize before using.
49
+ */
50
+ constructor(stof = new Stof()) {
51
+ this.stof = stof;
52
+ }
53
+ /**
54
+ * Create & initialize (if needed).
55
+ */
56
+ static async new() {
57
+ await StofDoc.initialize();
58
+ return new StofDoc();
59
+ }
60
+ /**
61
+ * Parse a JS object into a StofDoc.
62
+ */
63
+ static async parse(obj) {
64
+ const doc = await StofDoc.new();
65
+ doc.stof.objImport(obj, null);
66
+ return doc;
67
+ }
68
+ /**
69
+ * Sync parse a JS object into a StofDoc.
70
+ * Note: make sure initialize has been called on the wasm.
71
+ */
72
+ static sync_parse(obj) {
73
+ const doc = new StofDoc();
74
+ doc.stof.objImport(obj, null);
75
+ return doc;
76
+ }
77
+ /**
78
+ * Parse string source, array, or a JS record.
79
+ */
80
+ parse(src, format = "stof", node = null, profile = 'prod') {
81
+ if (typeof src === 'string') {
82
+ return this.stof.stringImport(src, format, node, profile);
83
+ }
84
+ else if (src instanceof Uint8Array) {
85
+ return this.stof.binaryImport(src, format, node, profile);
86
+ }
87
+ return this.stof.objImport(src, node);
88
+ }
89
+ /**
90
+ * Add JS library function.
91
+ */
92
+ // deno-lint-ignore ban-types
93
+ lib(library, name, func, is_async = false) {
94
+ this.stof.js_library_function(new StofFunc(library, name, func, is_async));
95
+ }
96
+ /**
97
+ * Run this document with a given set of Stof attributes.
98
+ * Will run all #[main] functions by default.
99
+ */
100
+ async run(attr = 'main') {
101
+ return await this.stof.run(attr);
102
+ }
103
+ /**
104
+ * Run this document with a given set of Stof attributes (synchronously).
105
+ * Will run all #[main] functions by default.
106
+ * Note: any async TS library functions called will not work with synchronous exec (ex. fetch).
107
+ */
108
+ sync_run(attr = 'main') {
109
+ return this.stof.sync_run(attr);
110
+ }
111
+ /**
112
+ * Call a specific Stof function by path/name.
113
+ */
114
+ async call(path, ...args) {
115
+ if (!path.includes('.'))
116
+ path = 'root.' + path; // assume root node if not specified
117
+ return await this.stof.call(path, args);
118
+ }
119
+ /**
120
+ * Call a specific Stof function by path/name.
121
+ * Note: any async TS library functions called will not work with synchronous exec (ex. fetch).
122
+ */
123
+ sync_call(path, ...args) {
124
+ if (!path.includes('.'))
125
+ path = 'root.' + path; // assume root node if not specified
126
+ return this.stof.sync_call(path, args);
127
+ }
128
+ /**
129
+ * Get a value from this graph by path and an optional starting object ID.
130
+ */
131
+ get(path, start_obj_id = null) {
132
+ if (!path.includes('.'))
133
+ path = 'self.' + path;
134
+ return this.stof.get(path, start_obj_id);
135
+ }
136
+ /**
137
+ * Set a value on this graph by path.
138
+ * Returns true if successfully set.
139
+ */
140
+ set(path, value, start_obj_id = null) {
141
+ if (!path.includes('.'))
142
+ path = 'self.' + path;
143
+ return this.stof.set(path, value, start_obj_id);
144
+ }
145
+ /**
146
+ * Stringify this doc into a format (JSON by default).
147
+ */
148
+ stringify(format = "json", node = null) {
149
+ return this.stof.stringExport(format, node);
150
+ }
151
+ /**
152
+ * Blobify this doc (or a specific node) into a format (JSON by default).
153
+ */
154
+ blobify(format = "json", node = null) {
155
+ return this.stof.binaryExport(format, node);
156
+ }
157
+ /**
158
+ * To JS record.
159
+ */
160
+ record(node = null) {
161
+ return JSON.parse(this.stringify('json', node));
162
+ }
163
+ /*****************************************************************************
164
+ * Network.
165
+ *****************************************************************************/
166
+ /**
167
+ * Send Stof doc string body as an HTTP request.
168
+ */
169
+ static async send(url, stof, method = 'POST', bearer, headers = {}) {
170
+ headers['Content-Type'] = 'application/stof';
171
+ if (bearer !== undefined)
172
+ headers['Authorization'] = `Bearer ${bearer}`;
173
+ return await fetch(url, {
174
+ method,
175
+ headers: headers,
176
+ body: stof
177
+ });
178
+ }
179
+ /**
180
+ * Send this document ('bstf' format) as an HTTP request.
181
+ */
182
+ async send(url, method = 'POST', bearer, headers = {}) {
183
+ headers['Content-Type'] = 'application/bstf';
184
+ if (bearer !== undefined)
185
+ headers['Authorization'] = `Bearer ${bearer}`;
186
+ const body = this.stof.binaryExport('bstf', null); // Uint8Array
187
+ return await fetch(url, {
188
+ method,
189
+ headers: headers,
190
+ body
191
+ });
192
+ }
193
+ }
194
+ //# sourceMappingURL=doc.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"doc.js","sourceRoot":"","sources":["../doc.ts"],"names":[],"mappings":"AAAA,EAAE;AACF,oDAAoD;AACpD,EAAE;AACF,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0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,aAAa;QAChE,OAAO,MAAM,KAAK,CAAC,GAAG,EAAE;YACpB,MAAM;YACN,OAAO,EAAE,OAAsB;YAC/B,IAAI;SACP,CAAC,CAAC;IACP,CAAC;CACJ"}
@@ -0,0 +1,141 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ export class Stof {
5
+ free(): void;
6
+ [Symbol.dispose](): void;
7
+ /**
8
+ * Binary export (Uint8Array), using a format of choice.
9
+ * Format can also be a content type (for HTTP-like situations).
10
+ */
11
+ binaryExport(format: string, node: any): any;
12
+ /**
13
+ * Binary import (Uint8Array), using a format of choice.
14
+ * Format can also be a content type (for HTTP-like situations).
15
+ */
16
+ binaryImport(bytes: any, format: string, node: any, profile: string): boolean;
17
+ /**
18
+ * Import a JS object value.
19
+ */
20
+ objImport(js_obj: any, node: any): boolean;
21
+ /**
22
+ * String export, using a format of choice.
23
+ */
24
+ stringExport(format: string, node: any): string;
25
+ /**
26
+ * String import, using a format of choice (including stof).
27
+ */
28
+ stringImport(src: string, format: string, node: any, profile: string): boolean;
29
+ /**
30
+ * Insert a JS function as a library function, available in Stof.
31
+ */
32
+ js_library_function(func: StofFunc): void;
33
+ /**
34
+ * Get a value from this graph using the Stof runtime (all language features supported).
35
+ */
36
+ get(path: string, start: any): any;
37
+ /**
38
+ * Construct a new document.
39
+ */
40
+ constructor();
41
+ /**
42
+ * Run functions with the given attribute(s) in this document.
43
+ * Attributes defaults to #[main] functions if null or undefined.
44
+ */
45
+ run(attributes: any): Promise<string>;
46
+ /**
47
+ * Set a value onto this graph using the Stof runtime.
48
+ */
49
+ set(path: string, value: any, start: any): boolean;
50
+ /**
51
+ * Call a singular function in the document (by path).
52
+ * If no arguments, pass undefined as args.
53
+ * Otherwise, pass an array of arguments as args.
54
+ */
55
+ call(path: string, args: any): Promise<any>;
56
+ /**
57
+ * Parse Stof into this document, optionally within the specified node (pass null for root node).
58
+ */
59
+ parse(stof: string, node: any, profile: string): boolean;
60
+ /**
61
+ * Synchronous run functions with the given attribute(s) in this document.
62
+ * Attributes defaults to #[main] functions if null or undefined.
63
+ * Async TS lib functions will not work with this, but it will be faster.
64
+ */
65
+ sync_run(attributes: any): string;
66
+ /**
67
+ * Synchronous call a singular function in the document (by path).
68
+ * If no arguments, pass undefined as args.
69
+ * Otherwise, pass an array of arguments as args.
70
+ * Async TS lib functions will not work with this, but it will be faster.
71
+ */
72
+ sync_call(path: string, args: any): any;
73
+ }
74
+
75
+ export class StofFunc {
76
+ free(): void;
77
+ [Symbol.dispose](): void;
78
+ /**
79
+ * Create a new Stof function from a JS function.
80
+ */
81
+ constructor(library: string, name: string, js_function: any, is_async: boolean);
82
+ }
83
+
84
+ export function start(): void;
85
+
86
+ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
87
+
88
+ export interface InitOutput {
89
+ readonly memory: WebAssembly.Memory;
90
+ readonly __wbg_stoffunc_free: (a: number, b: number) => void;
91
+ readonly stoffunc_new: (a: number, b: number, c: number, d: number, e: any, f: number) => number;
92
+ readonly __wbg_stof_free: (a: number, b: number) => void;
93
+ readonly start: () => void;
94
+ readonly stof_binaryExport: (a: number, b: number, c: number, d: any) => [number, number, number];
95
+ readonly stof_binaryImport: (a: number, b: any, c: number, d: number, e: any, f: number, g: number) => [number, number, number];
96
+ readonly stof_call: (a: number, b: number, c: number, d: any) => any;
97
+ readonly stof_get: (a: number, b: number, c: number, d: any) => any;
98
+ readonly stof_js_library_function: (a: number, b: number) => void;
99
+ readonly stof_new: () => number;
100
+ readonly stof_objImport: (a: number, b: any, c: any) => [number, number, number];
101
+ readonly stof_parse: (a: number, b: number, c: number, d: any, e: number, f: number) => [number, number, number];
102
+ readonly stof_run: (a: number, b: any) => any;
103
+ readonly stof_set: (a: number, b: number, c: number, d: any, e: any) => number;
104
+ readonly stof_stringExport: (a: number, b: number, c: number, d: any) => [number, number, number, number];
105
+ readonly stof_stringImport: (a: number, b: number, c: number, d: number, e: number, f: any, g: number, h: number) => [number, number, number];
106
+ readonly stof_sync_call: (a: number, b: number, c: number, d: any) => [number, number, number];
107
+ readonly stof_sync_run: (a: number, b: any) => [number, number, number, number];
108
+ readonly wasm_bindgen__convert__closures_____invoke__h53d5cf04cab8438f: (a: number, b: number, c: any) => void;
109
+ readonly wasm_bindgen__closure__destroy__h72b14ab7db8750ca: (a: number, b: number) => void;
110
+ readonly wasm_bindgen__convert__closures_____invoke__ha84735728bfe97a9: (a: number, b: number, c: any, d: any) => void;
111
+ readonly __wbindgen_malloc: (a: number, b: number) => number;
112
+ readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
113
+ readonly __wbindgen_exn_store: (a: number) => void;
114
+ readonly __externref_table_alloc: () => number;
115
+ readonly __wbindgen_externrefs: WebAssembly.Table;
116
+ readonly __wbindgen_free: (a: number, b: number, c: number) => void;
117
+ readonly __externref_table_dealloc: (a: number) => void;
118
+ readonly __wbindgen_start: () => void;
119
+ }
120
+
121
+ export type SyncInitInput = BufferSource | WebAssembly.Module;
122
+
123
+ /**
124
+ * Instantiates the given `module`, which can either be bytes or
125
+ * a precompiled `WebAssembly.Module`.
126
+ *
127
+ * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
128
+ *
129
+ * @returns {InitOutput}
130
+ */
131
+ export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
132
+
133
+ /**
134
+ * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
135
+ * for everything else, calls `WebAssembly.instantiate` directly.
136
+ *
137
+ * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
138
+ *
139
+ * @returns {Promise<InitOutput>}
140
+ */
141
+ export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;