@ducklings/workers 1.4.3-dev.1 → 1.4.3-dev.2
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/README.md +72 -0
- package/dist/vite-plugin.d.ts +78 -0
- package/dist/vite-plugin.js +46 -0
- package/dist/vite-plugin.js.map +1 -0
- package/dist/wasm.d.ts +3 -0
- package/package.json +19 -4
package/README.md
CHANGED
|
@@ -35,6 +35,78 @@ export default {
|
|
|
35
35
|
};
|
|
36
36
|
```
|
|
37
37
|
|
|
38
|
+
## Singleton Initialization (Recommended)
|
|
39
|
+
|
|
40
|
+
For production use, reuse the database and connection across requests to avoid re-initialization overhead:
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import { init, DuckDB, type Connection } from '@ducklings/workers';
|
|
44
|
+
import wasmModule from '@ducklings/workers/wasm';
|
|
45
|
+
|
|
46
|
+
// Global state (reused across requests in the same Worker instance)
|
|
47
|
+
let db: DuckDB | null = null;
|
|
48
|
+
let conn: Connection | null = null;
|
|
49
|
+
let initialized = false;
|
|
50
|
+
|
|
51
|
+
async function ensureInitialized(): Promise<void> {
|
|
52
|
+
if (initialized && db && conn) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
await init({ wasmModule });
|
|
57
|
+
db = new DuckDB();
|
|
58
|
+
conn = db.connect();
|
|
59
|
+
initialized = true;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export default {
|
|
63
|
+
async fetch(request: Request): Promise<Response> {
|
|
64
|
+
await ensureInitialized();
|
|
65
|
+
|
|
66
|
+
const rows = await conn!.query('SELECT 42 as answer');
|
|
67
|
+
return Response.json(rows);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Vite Plugin
|
|
73
|
+
|
|
74
|
+
For projects using Vite to build Cloudflare Workers, we provide a plugin that handles WASM file resolution and copying:
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
// vite.config.ts
|
|
78
|
+
import { defineConfig } from 'vite';
|
|
79
|
+
import { ducklingsWorkerPlugin } from '@ducklings/workers/vite-plugin';
|
|
80
|
+
|
|
81
|
+
export default defineConfig({
|
|
82
|
+
plugins: [ducklingsWorkerPlugin()],
|
|
83
|
+
build: {
|
|
84
|
+
rollupOptions: {
|
|
85
|
+
external: [/\.wasm$/],
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Plugin Options
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
ducklingsWorkerPlugin({
|
|
95
|
+
// Output directory for the build (default: 'dist')
|
|
96
|
+
outDir: 'dist',
|
|
97
|
+
|
|
98
|
+
// Name of the WASM file in the output directory (default: 'duckdb-workers.wasm')
|
|
99
|
+
wasmFileName: 'duckdb-workers.wasm',
|
|
100
|
+
|
|
101
|
+
// Whether to copy the WASM file to the output directory (default: true)
|
|
102
|
+
copyWasm: true,
|
|
103
|
+
})
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
The plugin:
|
|
107
|
+
- Resolves `@ducklings/workers/wasm` imports to a relative path for wrangler
|
|
108
|
+
- Copies the WASM file to your output directory after build
|
|
109
|
+
|
|
38
110
|
## Features
|
|
39
111
|
|
|
40
112
|
- Async API - all query methods return Promises
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vite plugin for @ducklings/workers on Cloudflare Workers
|
|
3
|
+
*
|
|
4
|
+
* This plugin handles WASM file resolution and copying for Cloudflare Workers deployments.
|
|
5
|
+
*
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Vite Plugin interface (minimal definition to avoid hard dependency on vite types)
|
|
10
|
+
*/
|
|
11
|
+
interface VitePlugin {
|
|
12
|
+
name: string;
|
|
13
|
+
enforce?: 'pre' | 'post';
|
|
14
|
+
configResolved?: (config: {
|
|
15
|
+
root: string;
|
|
16
|
+
}) => void;
|
|
17
|
+
resolveId?: (id: string) => {
|
|
18
|
+
id: string;
|
|
19
|
+
external: true;
|
|
20
|
+
} | null;
|
|
21
|
+
closeBundle?: () => void;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Configuration options for the Ducklings Workers Vite plugin.
|
|
25
|
+
*/
|
|
26
|
+
interface DucklingsWorkersPluginOptions {
|
|
27
|
+
/**
|
|
28
|
+
* Output directory for the build.
|
|
29
|
+
* @default 'dist'
|
|
30
|
+
*/
|
|
31
|
+
outDir?: string;
|
|
32
|
+
/**
|
|
33
|
+
* Name of the WASM file in the output directory.
|
|
34
|
+
* @default 'duckdb-workers.wasm'
|
|
35
|
+
*/
|
|
36
|
+
wasmFileName?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Whether to copy the WASM file to the output directory.
|
|
39
|
+
* Set to false if you handle WASM file copying separately.
|
|
40
|
+
* @default true
|
|
41
|
+
*/
|
|
42
|
+
copyWasm?: boolean;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Resolves the path to the WASM file bundled with @ducklings/workers.
|
|
46
|
+
*
|
|
47
|
+
* @returns The absolute path to the WASM file
|
|
48
|
+
*/
|
|
49
|
+
declare function getWasmPath(): string;
|
|
50
|
+
/**
|
|
51
|
+
* Vite plugin for Cloudflare Workers that handles Ducklings Workers WASM integration.
|
|
52
|
+
*
|
|
53
|
+
* This plugin:
|
|
54
|
+
* - Resolves `@ducklings/workers/wasm` imports to a relative path for wrangler
|
|
55
|
+
* - Optionally copies the WASM file to your output directory
|
|
56
|
+
*
|
|
57
|
+
* @param options - Plugin configuration options
|
|
58
|
+
* @returns Vite plugin
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* // vite.config.ts
|
|
63
|
+
* import { defineConfig } from 'vite';
|
|
64
|
+
* import { ducklingsWorkerPlugin } from '@ducklings/workers/vite-plugin';
|
|
65
|
+
*
|
|
66
|
+
* export default defineConfig({
|
|
67
|
+
* plugins: [ducklingsWorkerPlugin()],
|
|
68
|
+
* build: {
|
|
69
|
+
* rollupOptions: {
|
|
70
|
+
* external: [/\.wasm$/],
|
|
71
|
+
* },
|
|
72
|
+
* },
|
|
73
|
+
* });
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
declare function ducklingsWorkerPlugin(options?: DucklingsWorkersPluginOptions): VitePlugin;
|
|
77
|
+
|
|
78
|
+
export { type DucklingsWorkersPluginOptions, ducklingsWorkerPlugin as default, ducklingsWorkerPlugin, getWasmPath };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { dirname, resolve } from 'path';
|
|
2
|
+
import { mkdirSync, copyFileSync } from 'fs';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
|
|
5
|
+
// Ducklings Workers - Vite Plugin for Cloudflare Workers
|
|
6
|
+
|
|
7
|
+
function getWasmPath() {
|
|
8
|
+
const currentDir = dirname(fileURLToPath(import.meta.url));
|
|
9
|
+
return resolve(currentDir, "wasm", "duckdb-workers.wasm");
|
|
10
|
+
}
|
|
11
|
+
function ducklingsWorkerPlugin(options = {}) {
|
|
12
|
+
const { outDir = "dist", wasmFileName = "duckdb-workers.wasm", copyWasm = true } = options;
|
|
13
|
+
let projectRoot;
|
|
14
|
+
return {
|
|
15
|
+
name: "ducklings-workers-plugin",
|
|
16
|
+
enforce: "pre",
|
|
17
|
+
configResolved(config) {
|
|
18
|
+
projectRoot = config.root;
|
|
19
|
+
},
|
|
20
|
+
// Resolve WASM imports from the package to relative path
|
|
21
|
+
resolveId(id) {
|
|
22
|
+
if (id === "@ducklings/workers/wasm") {
|
|
23
|
+
return { id: `./${wasmFileName}`, external: true };
|
|
24
|
+
}
|
|
25
|
+
return null;
|
|
26
|
+
},
|
|
27
|
+
// Copy WASM file to output directory after build
|
|
28
|
+
closeBundle() {
|
|
29
|
+
if (!copyWasm) return;
|
|
30
|
+
const wasmSrcPath = getWasmPath();
|
|
31
|
+
const wasmDestPath = resolve(projectRoot, outDir, wasmFileName);
|
|
32
|
+
try {
|
|
33
|
+
mkdirSync(resolve(projectRoot, outDir), { recursive: true });
|
|
34
|
+
copyFileSync(wasmSrcPath, wasmDestPath);
|
|
35
|
+
console.log(`[ducklings] Copied WASM file to ${outDir}/${wasmFileName}`);
|
|
36
|
+
} catch (e) {
|
|
37
|
+
console.error("[ducklings] Failed to copy WASM file:", e);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
var vite_plugin_default = ducklingsWorkerPlugin;
|
|
43
|
+
|
|
44
|
+
export { vite_plugin_default as default, ducklingsWorkerPlugin, getWasmPath };
|
|
45
|
+
//# sourceMappingURL=vite-plugin.js.map
|
|
46
|
+
//# sourceMappingURL=vite-plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/vite-plugin/index.ts"],"names":[],"mappings":";;;;;;AAoDO,SAAS,WAAA,GAAsB;AAEpC,EAAA,MAAM,UAAA,GAAa,OAAA,CAAQ,aAAA,CAAc,MAAA,CAAA,IAAA,CAAY,GAAG,CAAC,CAAA;AAEzD,EAAA,OAAO,OAAA,CAAQ,UAAA,EAAY,MAAA,EAAQ,qBAAqB,CAAA;AAC1D;AA4BO,SAAS,qBAAA,CAAsB,OAAA,GAAyC,EAAC,EAAe;AAC7F,EAAA,MAAM,EAAE,MAAA,GAAS,MAAA,EAAQ,eAAe,qBAAA,EAAuB,QAAA,GAAW,MAAK,GAAI,OAAA;AAEnF,EAAA,IAAI,WAAA;AAEJ,EAAA,OAAO;AAAA,IACL,IAAA,EAAM,0BAAA;AAAA,IACN,OAAA,EAAS,KAAA;AAAA,IAET,eAAe,MAAA,EAAQ;AACrB,MAAA,WAAA,GAAc,MAAA,CAAO,IAAA;AAAA,IACvB,CAAA;AAAA;AAAA,IAGA,UAAU,EAAA,EAAI;AACZ,MAAA,IAAI,OAAO,yBAAA,EAA2B;AAEpC,QAAA,OAAO,EAAE,EAAA,EAAI,CAAA,EAAA,EAAK,YAAY,CAAA,CAAA,EAAI,UAAU,IAAA,EAAc;AAAA,MAC5D;AACA,MAAA,OAAO,IAAA;AAAA,IACT,CAAA;AAAA;AAAA,IAGA,WAAA,GAAc;AACZ,MAAA,IAAI,CAAC,QAAA,EAAU;AAEf,MAAA,MAAM,cAAc,WAAA,EAAY;AAChC,MAAA,MAAM,YAAA,GAAe,OAAA,CAAQ,WAAA,EAAa,MAAA,EAAQ,YAAY,CAAA;AAE9D,MAAA,IAAI;AACF,QAAA,SAAA,CAAU,QAAQ,WAAA,EAAa,MAAM,GAAG,EAAE,SAAA,EAAW,MAAM,CAAA;AAC3D,QAAA,YAAA,CAAa,aAAa,YAAY,CAAA;AACtC,QAAA,OAAA,CAAQ,GAAA,CAAI,CAAA,gCAAA,EAAmC,MAAM,CAAA,CAAA,EAAI,YAAY,CAAA,CAAE,CAAA;AAAA,MACzE,SAAS,CAAA,EAAG;AACV,QAAA,OAAA,CAAQ,KAAA,CAAM,yCAAyC,CAAC,CAAA;AAAA,MAC1D;AAAA,IACF;AAAA,GACF;AACF;AAEA,IAAO,mBAAA,GAAQ","file":"vite-plugin.js","sourcesContent":["/**\n * Vite plugin for @ducklings/workers on Cloudflare Workers\n *\n * This plugin handles WASM file resolution and copying for Cloudflare Workers deployments.\n *\n * @packageDocumentation\n */\n\nimport { resolve, dirname } from 'node:path';\nimport { copyFileSync, mkdirSync } from 'node:fs';\nimport { fileURLToPath } from 'node:url';\n\n/**\n * Vite Plugin interface (minimal definition to avoid hard dependency on vite types)\n */\ninterface VitePlugin {\n name: string;\n enforce?: 'pre' | 'post';\n configResolved?: (config: { root: string }) => void;\n resolveId?: (id: string) => { id: string; external: true } | null;\n closeBundle?: () => void;\n}\n\n/**\n * Configuration options for the Ducklings Workers Vite plugin.\n */\nexport interface DucklingsWorkersPluginOptions {\n /**\n * Output directory for the build.\n * @default 'dist'\n */\n outDir?: string;\n\n /**\n * Name of the WASM file in the output directory.\n * @default 'duckdb-workers.wasm'\n */\n wasmFileName?: string;\n\n /**\n * Whether to copy the WASM file to the output directory.\n * Set to false if you handle WASM file copying separately.\n * @default true\n */\n copyWasm?: boolean;\n}\n\n/**\n * Resolves the path to the WASM file bundled with @ducklings/workers.\n *\n * @returns The absolute path to the WASM file\n */\nexport function getWasmPath(): string {\n // Get the directory of this module\n const currentDir = dirname(fileURLToPath(import.meta.url));\n // WASM file is in the wasm subdirectory relative to dist\n return resolve(currentDir, 'wasm', 'duckdb-workers.wasm');\n}\n\n/**\n * Vite plugin for Cloudflare Workers that handles Ducklings Workers WASM integration.\n *\n * This plugin:\n * - Resolves `@ducklings/workers/wasm` imports to a relative path for wrangler\n * - Optionally copies the WASM file to your output directory\n *\n * @param options - Plugin configuration options\n * @returns Vite plugin\n *\n * @example\n * ```typescript\n * // vite.config.ts\n * import { defineConfig } from 'vite';\n * import { ducklingsWorkerPlugin } from '@ducklings/workers/vite-plugin';\n *\n * export default defineConfig({\n * plugins: [ducklingsWorkerPlugin()],\n * build: {\n * rollupOptions: {\n * external: [/\\.wasm$/],\n * },\n * },\n * });\n * ```\n */\nexport function ducklingsWorkerPlugin(options: DucklingsWorkersPluginOptions = {}): VitePlugin {\n const { outDir = 'dist', wasmFileName = 'duckdb-workers.wasm', copyWasm = true } = options;\n\n let projectRoot: string;\n\n return {\n name: 'ducklings-workers-plugin',\n enforce: 'pre' as const,\n\n configResolved(config) {\n projectRoot = config.root;\n },\n\n // Resolve WASM imports from the package to relative path\n resolveId(id) {\n if (id === '@ducklings/workers/wasm') {\n // Return external reference that will be resolved at runtime by wrangler\n return { id: `./${wasmFileName}`, external: true as const };\n }\n return null;\n },\n\n // Copy WASM file to output directory after build\n closeBundle() {\n if (!copyWasm) return;\n\n const wasmSrcPath = getWasmPath();\n const wasmDestPath = resolve(projectRoot, outDir, wasmFileName);\n\n try {\n mkdirSync(resolve(projectRoot, outDir), { recursive: true });\n copyFileSync(wasmSrcPath, wasmDestPath);\n console.log(`[ducklings] Copied WASM file to ${outDir}/${wasmFileName}`);\n } catch (e) {\n console.error('[ducklings] Failed to copy WASM file:', e);\n }\n },\n };\n}\n\nexport default ducklingsWorkerPlugin;\n"]}
|
package/dist/wasm.d.ts
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ducklings/workers",
|
|
3
|
-
"version": "1.4.3-dev.
|
|
3
|
+
"version": "1.4.3-dev.2",
|
|
4
4
|
"description": "Minimal DuckDB WASM for Cloudflare Workers and serverless environments",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -12,8 +12,15 @@
|
|
|
12
12
|
"import": "./dist/index.js",
|
|
13
13
|
"require": "./dist/index.cjs"
|
|
14
14
|
},
|
|
15
|
-
"./wasm":
|
|
16
|
-
|
|
15
|
+
"./wasm": {
|
|
16
|
+
"types": "./dist/wasm.d.ts",
|
|
17
|
+
"default": "./dist/wasm/duckdb-workers.wasm"
|
|
18
|
+
},
|
|
19
|
+
"./wasm/*": "./dist/wasm/*",
|
|
20
|
+
"./vite-plugin": {
|
|
21
|
+
"types": "./dist/vite-plugin.d.ts",
|
|
22
|
+
"import": "./dist/vite-plugin.js"
|
|
23
|
+
}
|
|
17
24
|
},
|
|
18
25
|
"files": [
|
|
19
26
|
"dist",
|
|
@@ -21,7 +28,7 @@
|
|
|
21
28
|
],
|
|
22
29
|
"scripts": {
|
|
23
30
|
"build": "tsup && pnpm postbuild",
|
|
24
|
-
"postbuild": "mkdir -p dist/wasm && cp ../../dist/duckdb-workers.js dist/wasm/ && cp ../../dist/duckdb-workers.wasm dist/wasm/",
|
|
31
|
+
"postbuild": "mkdir -p dist/wasm && cp ../../dist/duckdb-workers.js dist/wasm/ && cp ../../dist/duckdb-workers.wasm dist/wasm/ && cp src/wasm.d.ts dist/wasm.d.ts",
|
|
25
32
|
"dev": "tsup --watch",
|
|
26
33
|
"test": "vitest run --reporter=dot",
|
|
27
34
|
"test:watch": "vitest --reporter=dot",
|
|
@@ -46,6 +53,14 @@
|
|
|
46
53
|
"typescript": "^5.9.3",
|
|
47
54
|
"vitest": "^4.0.16"
|
|
48
55
|
},
|
|
56
|
+
"peerDependencies": {
|
|
57
|
+
"vite": "^5.0.0 || ^6.0.0 || ^7.0.0"
|
|
58
|
+
},
|
|
59
|
+
"peerDependenciesMeta": {
|
|
60
|
+
"vite": {
|
|
61
|
+
"optional": true
|
|
62
|
+
}
|
|
63
|
+
},
|
|
49
64
|
"keywords": [
|
|
50
65
|
"duckdb",
|
|
51
66
|
"wasm",
|