Skip to content

Commit

Permalink
feat: add fflate js file as assets and calling custom fetchWasm f…
Browse files Browse the repository at this point in the history
…unction to load surrealdb.wasm.gz file
  • Loading branch information
limcheekin committed Dec 19, 2024
1 parent 4baa33b commit 5cb7a21
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 1,630 deletions.
1 change: 1 addition & 0 deletions assets/js/fflate-0.8.2.min.js

Large diffs are not rendered by default.

160 changes: 2 additions & 158 deletions assets/wasm/surrealdb/surrealdb.js
Original file line number Diff line number Diff line change
@@ -1,165 +1,9 @@
import * as fflate from "https://cdn.skypack.dev/[email protected]?min";

// REF: https://stackoverflow.com/questions/46338176/javascript-reading-local-file-to-uint8array-fast
(async function loadWasm() {
if (wasm !== undefined) return wasm;

async function fetchWasm() {
const input = new URL("surrealdb.wasm.gz", import.meta.url);
const imports = __wbg_get_imports();
const response = await fetch(input);

__wbg_init_memory(imports);

const data = new Uint8Array(await response.arrayBuffer());
const decompressed_data = fflate.gunzipSync(data);
const bytes = decompressed_data.buffer;

const { instance, module } = await WebAssembly.instantiate(bytes, imports);

console.log("Surreal had been initialized!");

return __wbg_finalize_init(instance, module);
})();

class SurrealWrapper {
async set(key, value) {
await this.db.set(key, JSON.parse(value));
}

async unset(key) {
await this.db.unset(key);
}

async signup(credentials) {
return await this.db.signup(JSON.parse(credentials));
}

async signin(credentials) {
return await this.db.signin(JSON.parse(credentials));
}

async invalidate() {
await this.db.invalidate();
}

async authenticate(token) {
await this.db.authenticate(token);
}

async patch(resource, data) {
return await this.db.patch(resource, JSON.parse(data));
}

async version() {
return await this.db.version();
}

async health() {
await this.db.health();
}
/**
* Construct the database engine
*
* ```js
* const db = new SurrealDB();
* ```
*/
constructor() {
this.db = new Surreal();
Object.freeze(this);
}

/* @param {string} endpoint
* @param {any} opts
* @returns {Promise<void>}
*/
async connect(endpoint, opts) {
const options = typeof opts == "string" ? JSON.parse(opts) : opts;
await this.db.connect(endpoint, options);
}

/**
* @param {any} value
* @returns {Promise<void>}
*/
// await db.use({ ns: "test", db: "test" });
async use(value) {
const nsdb = typeof value == "string" ? JSON.parse(value) : value;
await this.db.use(nsdb);
}

/**
* @param {string} resource
* @param {any} data
* @returns {Promise<any>}
*/
async create(resource, data) {
const value = typeof data == "string" ? JSON.parse(data) : data;
console.log("surrealdb.js create() value", value);
const result = await this.db.create(resource, value);
console.log("surrealdb.js create()", result);
return result;
}

/**
* @param {string} resource
* @param {any} data
* @returns {Promise<any>}
*/
async update(resource, data) {
const value = typeof data == "string" ? JSON.parse(data) : data;
const result = await this.db.update(resource, value);
console.log("surrealdb.js update()", result);
return result;
}

/**
* @param {string} resource
* @param {any} data
* @returns {Promise<any>}
*/
async merge(resource, data) {
const value = typeof data == "string" ? JSON.parse(data) : data;
const result = await this.db.merge(resource, value);
console.log("surrealdb.js merge()", result);
return result;
}
/**
* @param {string} resource
* @returns {Promise<any>}
*/
async select(resource) {
const result = await this.db.select(resource);
console.log("surrealdb.js select()", result);
return result;
}

/**
* @param {string} sql
* @param {any} bindings
* @returns {Promise<any>}
*/
async query(sql, bindings) {
console.log("surrealdb.js query()", sql, bindings);
const bindings_value =
typeof bindings == "string" ? JSON.parse(bindings) : bindings;
console.log("surrealdb.js query() bindings_value", bindings_value);
const result = await this.db.query(sql, bindings_value);
console.log("surrealdb.js query() result", result);
return result;
}

/**
* @param {string} resource
* @returns {Promise<any>}
*/
async delete(resource) {
const result = await this.db.delete(resource);
console.log("surrealdb.js delete()", result);
return result;
}
}

if (typeof window !== "undefined") {
window.SurrealWrapper = SurrealWrapper;
return bytes;
}
1,489 changes: 21 additions & 1,468 deletions assets/wasm/surrealdb/surrealdb_wasm.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ dev_dependencies:
flutter:
assets:
- assets/wasm/surrealdb/
- assets/js/
9 changes: 6 additions & 3 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@
const serviceWorkerVersion = null;
</script>
<script type="module">
import { Surreal, StringRecordId } from "/assets/packages/surrealdb_js/assets/js/index.bundled.mjs";
import { surrealdbWasmEngines } from "./assets/wasm/surrealdb/index.bundled.js";
import * as fflate from './assets/js/fflate-0.8.2.min.js';
globalThis.fflate = fflate;
</script>
<script type="module">
import { Surreal, StringRecordId } from "/assets/packages/surrealdb_js/assets/js/index.bundled.mjs";
import { surrealdbWasmEngines } from "./assets/wasm/surrealdb/surrealdb_wasm.js";

// expose the type to the global scope
globalThis.SurrealJS = Surreal;
globalThis.StringRecordId = StringRecordId;
globalThis.WasmEngine = surrealdbWasmEngines;
Expand Down
6 changes: 5 additions & 1 deletion web/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
<head>
<meta charset="UTF-8" />
<title>Development server</title>
<script type="module">
import * as fflate from './assets/js/fflate-0.8.2.min.js';
globalThis.fflate = fflate;
</script>
</head>
<body>
<p>Open a console to access the Surreal class!</p>
<script type="module">
import { Surreal, StringRecordId } from "/assets/packages/surrealdb_js/assets/js/index.bundled.mjs";
import { surrealdbWasmEngines } from "./assets/wasm/surrealdb/esm.bundled.js";
import { surrealdbWasmEngines } from "./assets/wasm/surrealdb/surrealdb_wasm.js";
// expose the type to the global scope
globalThis.SurrealJS = Surreal;
globalThis.StringRecordId = StringRecordId;
Expand Down

0 comments on commit 5cb7a21

Please sign in to comment.