fork fork fork

This commit is contained in:
parent 56d2b9cae0
commit 2b2b899e15
Signed by: tablet
GPG Key ID: 924A5F6AF051E87C
12 changed files with 141 additions and 65 deletions

@ -10,8 +10,6 @@ $prodFlag = If($prod) {"production"} Else {""}
node .\esbuild.config.mjs -- $prodFlag node .\esbuild.config.mjs -- $prodFlag
if($LASTEXITCODE -ne 0) { if($LASTEXITCODE -ne 0) {
$host.SetShouldExit(1);
exit 1;
} }
$pluginName = "sqlite3-opfs-test-plugin" $pluginName = "sqlite3-opfs-test-plugin"

@ -1,28 +1,11 @@
import esbuild from "esbuild"; import esbuild from "esbuild";
import process from "process"; import process from "process";
import builtins from "builtin-modules"; import builtins from "builtin-modules";
import { TsconfigPathsPlugin } from "@esbuild-plugins/tsconfig-paths";
import inlineWorkerPlugin from "esbuild-plugin-inline-worker"; import inlineWorkerPlugin from "esbuild-plugin-inline-worker";
import inlineImportPlugin from "esbuild-plugin-inline-import";
import fs from "fs"; import fs from "fs";
const external = [
async function build(prod) {
fs.mkdirSync("build/plugin", { recursive: true });
const result = await esbuild
.build({
plugins: [
inlineWorkerPlugin({
alias: {
"@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-worker1-bundler-friendly":
"node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-worker1-bundler-friendly.mjs",
},
target: "es2020",
format: "cjs",
sourcemap: prod ? false : "inline",
}),
],
entryPoints: ["src/main.ts"],
bundle: true,
external: [
"obsidian", "obsidian",
"electron", "electron",
"@codemirror/autocomplete", "@codemirror/autocomplete",
@ -37,18 +20,42 @@ async function build(prod) {
"@lezer/highlight", "@lezer/highlight",
"@lezer/lr", "@lezer/lr",
"pdfjs-dist", "pdfjs-dist",
"path",
"url",
"electron",
...builtins, ...builtins,
];
async function build(prod) {
fs.mkdirSync("build/plugin", { recursive: true });
const result = await esbuild
.build({
plugins: [
TsconfigPathsPlugin({}),
inlineImportPlugin(),
inlineWorkerPlugin({
plugins: [TsconfigPathsPlugin({}), inlineImportPlugin()],
external,
target: "es2020",
format: "cjs",
sourcemap: prod ? false : "inline",
banner: {
js: "",
},
define: {
"import.meta.url": "globalThis.__importMetaUrl",
},
}),
], ],
entryPoints: ["src/main.ts"],
bundle: true,
external,
format: "cjs", format: "cjs",
target: "es2020", target: "es2020",
logLevel: "info", logLevel: "info",
sourcemap: prod ? false : "inline", sourcemap: prod ? false : "inline",
treeShaking: true, treeShaking: true,
outfile: "build/plugin/main.js", outfile: "build/plugin/main.js",
alias: {
"@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-worker1-bundler-friendly":
"node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-worker1-bundler-friendly.mjs",
},
}) })
.catch(() => process.exit(1)); .catch(() => process.exit(1));
@ -56,7 +63,7 @@ async function build(prod) {
fs.copyFileSync("manifest.json", "build/plugin/manifest.json"); fs.copyFileSync("manifest.json", "build/plugin/manifest.json");
fs.copyFileSync("src/styles.css", "build/plugin/styles.css"); fs.copyFileSync("src/styles.css", "build/plugin/styles.css");
fs.copyFileSync( fs.copyFileSync(
"node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3.wasm", "node_modules/@btfash/sqlite-wasm/sqlite-wasm/jswasm/sqlite3.wasm",
"build/plugin/sqlite3.wasm" "build/plugin/sqlite3.wasm"
); );
// fs.writeFileSync("build/meta.json", JSON.stringify(result.metafile)); // fs.writeFileSync("build/meta.json", JSON.stringify(result.metafile));

@ -12,17 +12,19 @@
"author": "", "author": "",
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@esbuild-plugins/tsconfig-paths": "^0.1.2",
"@types/node": "^16.11.6", "@types/node": "^16.11.6",
"@typescript-eslint/eslint-plugin": "5.29.0", "@typescript-eslint/eslint-plugin": "5.29.0",
"@typescript-eslint/parser": "5.29.0", "@typescript-eslint/parser": "5.29.0",
"builtin-modules": "3.3.0", "builtin-modules": "3.3.0",
"esbuild": "0.17.3", "esbuild": "0.17.3",
"esbuild-plugin-inline-import": "^1.1.0",
"obsidian": "latest", "obsidian": "latest",
"tslib": "2.4.0", "tslib": "2.4.0",
"typescript": "4.7.4" "typescript": "4.7.4",
"esbuild-plugin-inline-worker": "^0.1.1"
}, },
"dependencies": { "dependencies": {
"@sqlite.org/sqlite-wasm": "^3.45.1-build1", "@btfash/sqlite-wasm": "3.46.2"
"esbuild-plugin-inline-worker": "^0.1.1"
} }
} }

@ -9,7 +9,7 @@ import {
Setting, Setting,
} from "obsidian"; } from "obsidian";
import TestWorker from "sqlite3.worker"; import TestWorker from "sqlite3.worker";
import { sqlite3Worker1Promiser } from "@sqlite.org/sqlite-wasm"; import { sqlite3Worker1Promiser } from "@btfash/sqlite-wasm/index.mjs";
// Remember to rename these classes and interfaces! // Remember to rename these classes and interfaces!
@ -61,10 +61,11 @@ export default class MyPlugin extends Plugin {
let w = this.worker; let w = this.worker;
this.worker.postMessage({ this.worker.postMessage({
type: "init", type: "init",
root: this.locateFile("", ""),
path: this.locateFile("sqlite3.wasm", ""), path: this.locateFile("sqlite3.wasm", ""),
}); });
this.promiser = await new Promise<typeof this.promiser>((resolve) => { this.promiser = await new Promise<typeof this.promiser>((resolve) => {
const _promiser = sqlite3Worker1Promiser({ const _promiser = (sqlite3Worker1Promiser as (...args: any[]) => any)({
print: this.printNotice, print: this.printNotice,
printErr: console.error, printErr: console.error,
locateFile: this.locateFile.bind(this), locateFile: this.locateFile.bind(this),
@ -79,8 +80,8 @@ export default class MyPlugin extends Plugin {
async start() { async start() {
let openRes = await this.promiser("open", { let openRes = await this.promiser("open", {
filename: "/test.sqlite2", filename: "/test.sqlite3",
vfs: "opfs-sahpool", vfs: "opfs",
}); });
const { dbId } = openRes; const { dbId } = openRes;
console.log(openRes); console.log(openRes);

5
src/sqlite.d.ts vendored

@ -1,5 +0,0 @@
import "@sqlite.org/sqlite-wasm";
declare module "@sqlite.org/sqlite-wasm" {
export const sqlite3Worker1Promiser: any;
}
declare module "@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-worker1-bundler-friendly";

@ -1,19 +1,22 @@
import sqlite3InitModule from "@sqlite.org/sqlite-wasm"; import sqlite3InitModule from "@btfash/sqlite-wasm/sqlite-wasm/jswasm/sqlite3.mjs";
(async () => {})(); import str from "inline:../node_modules/@btfash/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-opfs-async-proxy.js"
global.__importMetaUrl = ""
let sqlite3; let sqlite3;
globalThis
let inited = false;
onmessage = async (event) => { onmessage = async (event) => {
console.log(event); console.log(event);
if (event.data.type == "init") { if (event.data.type == "init" && !inited) {
globalThis.__importMetaUrl = event.data.root
sqlite3 = await sqlite3InitModule({ sqlite3 = await sqlite3InitModule({
locateFile: (path, prefix) => event.data.path, locateFile: (path: string, prefix: string) => event.data.path,
}); });
// debugger; // debugger;
console.log(`Running sqlite version: ${sqlite3.version.libVersion}`); console.log(`Running sqlite version: ${sqlite3.version.libVersion}`);
await sqlite3.installOpfsSAHPoolVfs({ await sqlite3.installOpfsVfs({
clearOnInit: false, proxyUri: URL.createObjectURL(new Blob([str], {type: "application/javascript"}))
initialCapacity: 65536,
}); });
sqlite3.initWorker1API(); sqlite3.initWorker1API();
console.log(sqlite3.capi.sqlite3_vfs_find("opfs-sah")); console.log(sqlite3.capi.sqlite3_vfs_find("opfs"));
} }
}; };

4
src/typings/other.d.ts vendored Normal file

@ -0,0 +1,4 @@
declare global {
var __importMetaUrl: string
}
export {}

8
src/typings/sqlite.d.ts vendored Normal file

@ -0,0 +1,8 @@
declare module "@btfash/sqlite-wasm/index.mjs" {
import blah from "@btfash/sqlite-wasm";
export const sqlite3Worker1Promiser: any;
export {blah as default};
}
declare module "@btfash/sqlite-wasm/sqlite-wasm/jswasm/sqlite3.mjs";

6
src/typings/workers.d.ts vendored Normal file

@ -0,0 +1,6 @@
declare module "sqlite3.worker" {
const WorkerFactory: new () => Worker;
export default WorkerFactory;
}
declare module "inline:../node_modules/@btfash/sqlite-wasm/sqlite-wasm/jswasm/sqlite3-opfs-async-proxy.js"

4
src/workers.d.ts vendored

@ -1,4 +0,0 @@
declare module "sqlite3.worker" {
const WorkerFactory: new () => Worker;
export default WorkerFactory;
}

@ -11,6 +11,7 @@
"importHelpers": true, "importHelpers": true,
"isolatedModules": true, "isolatedModules": true,
"strictNullChecks": true, "strictNullChecks": true,
"allowSyntheticDefaultImports": true,
"lib": [ "lib": [
"DOM", "DOM",
"ES5", "ES5",
@ -18,7 +19,7 @@
"ES7" "ES7"
] ]
}, },
"files": ["src/typings/sqlite.d.ts"],
"include": [ "include": [
"**/*.ts" "**/*.ts"
] ]

@ -2,6 +2,20 @@
# yarn lockfile v1 # yarn lockfile v1
"@btfash/sqlite-wasm@3.46.2":
version "3.46.2"
resolved "https://registry.yarnpkg.com/@btfash/sqlite-wasm/-/sqlite-wasm-3.46.2.tgz#56446e4e401e3544c34a8660b258918fbd4574ab"
integrity sha512-IPgIlas0XqCQ9/6kfQUd9VmO/diRoZ/ETzpoWAEIk2Bqz7xVCVsswmm/POZBkayGTMnJ331yJDc3iTjgJnYKKw==
"@esbuild-plugins/tsconfig-paths@^0.1.2":
version "0.1.2"
resolved "https://registry.yarnpkg.com/@esbuild-plugins/tsconfig-paths/-/tsconfig-paths-0.1.2.tgz#1955de0a124ecf4364717a2fadbfbea876955232"
integrity sha512-TusFR26Y+Ze+Zm+NdfqZTSG4XyrXKxIaAfYCL3jASEI/gHjSdoCujATjzNWaaXs6Sk6Bv2D7NLr4Jdz1gysy/Q==
dependencies:
debug "^4.3.1"
find-up "^5.0.0"
strip-json-comments "^3.1.1"
"@esbuild/aix-ppc64@0.20.1": "@esbuild/aix-ppc64@0.20.1":
version "0.20.1" version "0.20.1"
resolved "https://registry.yarnpkg.com/@esbuild/aix-ppc64/-/aix-ppc64-0.20.1.tgz#eafa8775019b3650a77e8310ba4dbd17ca7af6d5" resolved "https://registry.yarnpkg.com/@esbuild/aix-ppc64/-/aix-ppc64-0.20.1.tgz#eafa8775019b3650a77e8310ba4dbd17ca7af6d5"
@ -248,11 +262,6 @@
"@nodelib/fs.scandir" "2.1.5" "@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0" fastq "^1.6.0"
"@sqlite.org/sqlite-wasm@^3.45.1-build1":
version "3.45.1-build1"
resolved "https://registry.yarnpkg.com/@sqlite.org/sqlite-wasm/-/sqlite-wasm-3.45.1-build1.tgz#648f6a0a0a4c3a67aff24b0e1331af655b86fb60"
integrity sha512-1EgshFNhVeBtZ9KtQPm3PzzJ2CtpmXAq2DAPywy7WZ3gOK6p5n8TY+M+mBMpQCF5cLqrdNFb3Kp9uNie9rUAHw==
"@types/codemirror@5.60.8": "@types/codemirror@5.60.8":
version "5.60.8" version "5.60.8"
resolved "https://registry.yarnpkg.com/@types/codemirror/-/codemirror-5.60.8.tgz#b647d04b470e8e1836dd84b2879988fc55c9de68" resolved "https://registry.yarnpkg.com/@types/codemirror/-/codemirror-5.60.8.tgz#b647d04b470e8e1836dd84b2879988fc55c9de68"
@ -384,6 +393,13 @@ commondir@^1.0.1:
resolved "https://registry.yarnpkg.com/commondir/-/commondir-1.0.1.tgz#ddd800da0c66127393cca5950ea968a3aaf1253b" resolved "https://registry.yarnpkg.com/commondir/-/commondir-1.0.1.tgz#ddd800da0c66127393cca5950ea968a3aaf1253b"
integrity sha512-W9pAhw0ja1Edb5GVdIF1mjZw/ASI0AlShXM83UUGe2DVr5TdAPEA1OA8m/g8zWp9x6On7gqufY+FatDbC3MDQg== integrity sha512-W9pAhw0ja1Edb5GVdIF1mjZw/ASI0AlShXM83UUGe2DVr5TdAPEA1OA8m/g8zWp9x6On7gqufY+FatDbC3MDQg==
debug@^4.3.1:
version "4.3.5"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.5.tgz#e83444eceb9fedd4a1da56d671ae2446a01a6e1e"
integrity sha512-pt0bNEmneDIvdL1Xsd9oDQ/wrQRkXDT4AUWlNZNPKvW5x/jyO9VFXkJUP07vQ2upmw5PlaITaPKc31jK13V+jg==
dependencies:
ms "2.1.2"
debug@^4.3.4: debug@^4.3.4:
version "4.3.4" version "4.3.4"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.4.tgz#1319f6579357f2338d3337d2cdd4914bb5dcc865" resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.4.tgz#1319f6579357f2338d3337d2cdd4914bb5dcc865"
@ -398,6 +414,11 @@ dir-glob@^3.0.1:
dependencies: dependencies:
path-type "^4.0.0" path-type "^4.0.0"
esbuild-plugin-inline-import@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/esbuild-plugin-inline-import/-/esbuild-plugin-inline-import-1.1.0.tgz#9a581a50918021c48962da2309aecf24d3fb9fcd"
integrity sha512-b0xX4tPKBdRjX1CkzpnULpEdeTo9vxD+wf83PKvgUYnOEaJfVxLey4q+sfTUPAdDnRRYasJsQUgQW7/e2Gm5Dw==
esbuild-plugin-inline-worker@^0.1.1: esbuild-plugin-inline-worker@^0.1.1:
version "0.1.1" version "0.1.1"
resolved "https://registry.yarnpkg.com/esbuild-plugin-inline-worker/-/esbuild-plugin-inline-worker-0.1.1.tgz#f21a610ad7410972a408272e0d254654381eac58" resolved "https://registry.yarnpkg.com/esbuild-plugin-inline-worker/-/esbuild-plugin-inline-worker-0.1.1.tgz#f21a610ad7410972a408272e0d254654381eac58"
@ -547,6 +568,14 @@ find-up@^4.0.0:
locate-path "^5.0.0" locate-path "^5.0.0"
path-exists "^4.0.0" path-exists "^4.0.0"
find-up@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/find-up/-/find-up-5.0.0.tgz#4c92819ecb7083561e4f4a240a86be5198f536fc"
integrity sha512-78/PXT1wlLLDgTzDs7sjq9hzz0vXD+zn+7wypEe4fXQxCmdmqfGsEPQxmiCSQI3ajFV91bVSsvNtrJRiW6nGng==
dependencies:
locate-path "^6.0.0"
path-exists "^4.0.0"
functional-red-black-tree@^1.0.1: functional-red-black-tree@^1.0.1:
version "1.0.1" version "1.0.1"
resolved "https://registry.yarnpkg.com/functional-red-black-tree/-/functional-red-black-tree-1.0.1.tgz#1b0ab3bd553b2a0d6399d29c0e3ea0b252078327" resolved "https://registry.yarnpkg.com/functional-red-black-tree/-/functional-red-black-tree-1.0.1.tgz#1b0ab3bd553b2a0d6399d29c0e3ea0b252078327"
@ -600,6 +629,13 @@ locate-path@^5.0.0:
dependencies: dependencies:
p-locate "^4.1.0" p-locate "^4.1.0"
locate-path@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-6.0.0.tgz#55321eb309febbc59c4801d931a72452a681d286"
integrity sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw==
dependencies:
p-locate "^5.0.0"
lru-cache@^6.0.0: lru-cache@^6.0.0:
version "6.0.0" version "6.0.0"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-6.0.0.tgz#6d6fe6570ebd96aaf90fcad1dafa3b2566db3a94" resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-6.0.0.tgz#6d6fe6570ebd96aaf90fcad1dafa3b2566db3a94"
@ -652,6 +688,13 @@ p-limit@^2.2.0:
dependencies: dependencies:
p-try "^2.0.0" p-try "^2.0.0"
p-limit@^3.0.2:
version "3.1.0"
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-3.1.0.tgz#e1daccbe78d0d1388ca18c64fea38e3e57e3706b"
integrity sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ==
dependencies:
yocto-queue "^0.1.0"
p-locate@^4.1.0: p-locate@^4.1.0:
version "4.1.0" version "4.1.0"
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-4.1.0.tgz#a3428bb7088b3a60292f66919278b7c297ad4f07" resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-4.1.0.tgz#a3428bb7088b3a60292f66919278b7c297ad4f07"
@ -659,6 +702,13 @@ p-locate@^4.1.0:
dependencies: dependencies:
p-limit "^2.2.0" p-limit "^2.2.0"
p-locate@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-5.0.0.tgz#83c8315c6785005e3bd021839411c9e110e6d834"
integrity sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw==
dependencies:
p-limit "^3.0.2"
p-try@^2.0.0: p-try@^2.0.0:
version "2.2.0" version "2.2.0"
resolved "https://registry.yarnpkg.com/p-try/-/p-try-2.2.0.tgz#cb2868540e313d61de58fafbe35ce9004d5540e6" resolved "https://registry.yarnpkg.com/p-try/-/p-try-2.2.0.tgz#cb2868540e313d61de58fafbe35ce9004d5540e6"
@ -725,10 +775,10 @@ slash@^3.0.0:
resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634" resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634"
integrity sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q== integrity sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==
sqlite-wasm-esm@^0.0.30: strip-json-comments@^3.1.1:
version "0.0.30" version "3.1.1"
resolved "https://registry.yarnpkg.com/sqlite-wasm-esm/-/sqlite-wasm-esm-0.0.30.tgz#b25492775bc9f26d260d683820787b840b466592" resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-3.1.1.tgz#31f1281b3832630434831c310c01cccda8cbe006"
integrity sha512-rLl+STKLfGXyBcpQlH6uEMMh76YXixY3s+qDEMzIiMMsyN7iXLmo4Mk1Su/6GoJFprSWP+cgOCWQsAbLELCEPg== integrity sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==
to-regex-range@^5.0.1: to-regex-range@^5.0.1:
version "5.0.1" version "5.0.1"
@ -763,3 +813,8 @@ yallist@^4.0.0:
version "4.0.0" version "4.0.0"
resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72" resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"
integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A== integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==
yocto-queue@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"
integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==