Skip to content

Commit 5cb7a21

Browse files
committed
feat: add fflate js file as assets and calling custom fetchWasm function to load surrealdb.wasm.gz file
1 parent 4baa33b commit 5cb7a21

File tree

6 files changed

+36
-1630
lines changed

6 files changed

+36
-1630
lines changed

assets/js/fflate-0.8.2.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/wasm/surrealdb/surrealdb.js

Lines changed: 2 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -1,165 +1,9 @@
1-
import * as fflate from "https://cdn.skypack.dev/[email protected]?min";
2-
31
// REF: https://stackoverflow.com/questions/46338176/javascript-reading-local-file-to-uint8array-fast
4-
(async function loadWasm() {
5-
if (wasm !== undefined) return wasm;
6-
2+
async function fetchWasm() {
73
const input = new URL("surrealdb.wasm.gz", import.meta.url);
8-
const imports = __wbg_get_imports();
94
const response = await fetch(input);
10-
11-
__wbg_init_memory(imports);
12-
135
const data = new Uint8Array(await response.arrayBuffer());
146
const decompressed_data = fflate.gunzipSync(data);
157
const bytes = decompressed_data.buffer;
16-
17-
const { instance, module } = await WebAssembly.instantiate(bytes, imports);
18-
19-
console.log("Surreal had been initialized!");
20-
21-
return __wbg_finalize_init(instance, module);
22-
})();
23-
24-
class SurrealWrapper {
25-
async set(key, value) {
26-
await this.db.set(key, JSON.parse(value));
27-
}
28-
29-
async unset(key) {
30-
await this.db.unset(key);
31-
}
32-
33-
async signup(credentials) {
34-
return await this.db.signup(JSON.parse(credentials));
35-
}
36-
37-
async signin(credentials) {
38-
return await this.db.signin(JSON.parse(credentials));
39-
}
40-
41-
async invalidate() {
42-
await this.db.invalidate();
43-
}
44-
45-
async authenticate(token) {
46-
await this.db.authenticate(token);
47-
}
48-
49-
async patch(resource, data) {
50-
return await this.db.patch(resource, JSON.parse(data));
51-
}
52-
53-
async version() {
54-
return await this.db.version();
55-
}
56-
57-
async health() {
58-
await this.db.health();
59-
}
60-
/**
61-
* Construct the database engine
62-
*
63-
* ```js
64-
* const db = new SurrealDB();
65-
* ```
66-
*/
67-
constructor() {
68-
this.db = new Surreal();
69-
Object.freeze(this);
70-
}
71-
72-
/* @param {string} endpoint
73-
* @param {any} opts
74-
* @returns {Promise<void>}
75-
*/
76-
async connect(endpoint, opts) {
77-
const options = typeof opts == "string" ? JSON.parse(opts) : opts;
78-
await this.db.connect(endpoint, options);
79-
}
80-
81-
/**
82-
* @param {any} value
83-
* @returns {Promise<void>}
84-
*/
85-
// await db.use({ ns: "test", db: "test" });
86-
async use(value) {
87-
const nsdb = typeof value == "string" ? JSON.parse(value) : value;
88-
await this.db.use(nsdb);
89-
}
90-
91-
/**
92-
* @param {string} resource
93-
* @param {any} data
94-
* @returns {Promise<any>}
95-
*/
96-
async create(resource, data) {
97-
const value = typeof data == "string" ? JSON.parse(data) : data;
98-
console.log("surrealdb.js create() value", value);
99-
const result = await this.db.create(resource, value);
100-
console.log("surrealdb.js create()", result);
101-
return result;
102-
}
103-
104-
/**
105-
* @param {string} resource
106-
* @param {any} data
107-
* @returns {Promise<any>}
108-
*/
109-
async update(resource, data) {
110-
const value = typeof data == "string" ? JSON.parse(data) : data;
111-
const result = await this.db.update(resource, value);
112-
console.log("surrealdb.js update()", result);
113-
return result;
114-
}
115-
116-
/**
117-
* @param {string} resource
118-
* @param {any} data
119-
* @returns {Promise<any>}
120-
*/
121-
async merge(resource, data) {
122-
const value = typeof data == "string" ? JSON.parse(data) : data;
123-
const result = await this.db.merge(resource, value);
124-
console.log("surrealdb.js merge()", result);
125-
return result;
126-
}
127-
/**
128-
* @param {string} resource
129-
* @returns {Promise<any>}
130-
*/
131-
async select(resource) {
132-
const result = await this.db.select(resource);
133-
console.log("surrealdb.js select()", result);
134-
return result;
135-
}
136-
137-
/**
138-
* @param {string} sql
139-
* @param {any} bindings
140-
* @returns {Promise<any>}
141-
*/
142-
async query(sql, bindings) {
143-
console.log("surrealdb.js query()", sql, bindings);
144-
const bindings_value =
145-
typeof bindings == "string" ? JSON.parse(bindings) : bindings;
146-
console.log("surrealdb.js query() bindings_value", bindings_value);
147-
const result = await this.db.query(sql, bindings_value);
148-
console.log("surrealdb.js query() result", result);
149-
return result;
150-
}
151-
152-
/**
153-
* @param {string} resource
154-
* @returns {Promise<any>}
155-
*/
156-
async delete(resource) {
157-
const result = await this.db.delete(resource);
158-
console.log("surrealdb.js delete()", result);
159-
return result;
160-
}
161-
}
162-
163-
if (typeof window !== "undefined") {
164-
window.SurrealWrapper = SurrealWrapper;
8+
return bytes;
1659
}

assets/wasm/surrealdb/surrealdb_wasm.js

Lines changed: 21 additions & 1468 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ dev_dependencies:
2424
flutter:
2525
assets:
2626
- assets/wasm/surrealdb/
27+
- assets/js/

web/index.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@
4040
const serviceWorkerVersion = null;
4141
</script>
4242
<script type="module">
43-
import { Surreal, StringRecordId } from "/assets/packages/surrealdb_js/assets/js/index.bundled.mjs";
44-
import { surrealdbWasmEngines } from "./assets/wasm/surrealdb/index.bundled.js";
43+
import * as fflate from './assets/js/fflate-0.8.2.min.js';
44+
globalThis.fflate = fflate;
45+
</script>
46+
<script type="module">
47+
import { Surreal, StringRecordId } from "/assets/packages/surrealdb_js/assets/js/index.bundled.mjs";
48+
import { surrealdbWasmEngines } from "./assets/wasm/surrealdb/surrealdb_wasm.js";
4549

46-
// expose the type to the global scope
4750
globalThis.SurrealJS = Surreal;
4851
globalThis.StringRecordId = StringRecordId;
4952
globalThis.WasmEngine = surrealdbWasmEngines;

web/test.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
<head>
44
<meta charset="UTF-8" />
55
<title>Development server</title>
6+
<script type="module">
7+
import * as fflate from './assets/js/fflate-0.8.2.min.js';
8+
globalThis.fflate = fflate;
9+
</script>
610
</head>
711
<body>
812
<p>Open a console to access the Surreal class!</p>
913
<script type="module">
1014
import { Surreal, StringRecordId } from "/assets/packages/surrealdb_js/assets/js/index.bundled.mjs";
11-
import { surrealdbWasmEngines } from "./assets/wasm/surrealdb/esm.bundled.js";
15+
import { surrealdbWasmEngines } from "./assets/wasm/surrealdb/surrealdb_wasm.js";
1216
// expose the type to the global scope
1317
globalThis.SurrealJS = Surreal;
1418
globalThis.StringRecordId = StringRecordId;

0 commit comments

Comments
 (0)