@noego/sqlego 0.1.2-dev.1 → 0.1.2-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 +78 -17
- package/bin/sqlego.cjs +49 -11
- package/dist/binary.d.ts +47 -0
- package/dist/client.d.ts +14 -10
- package/dist/index.d.ts +2 -1
- package/dist/index.js +120 -44
- package/dist/protocol.d.ts +6 -6
- package/package.json +10 -2
- package/src/binary.ts +89 -0
- package/src/client.ts +53 -29
- package/src/index.ts +4 -1
- package/bin/sqlego +0 -4
package/README.md
CHANGED
|
@@ -16,16 +16,67 @@ process just asks the owner.
|
|
|
16
16
|
|
|
17
17
|
| Piece | Runtime | Role |
|
|
18
18
|
| --- | --- | --- |
|
|
19
|
-
|
|
|
19
|
+
| `@noego/sqlego-<platform>-<arch>` (`npm/<platform>/`) | Bun, compiled single binary | CLI **and** server (`sqlego serve <file>` is what clients spawn); one package per platform, one binary each |
|
|
20
20
|
| `bin/sqlego.cjs` | Node | The `sqlego` command npm installs: runs the binary for this platform (npm shims cannot launch a native `.exe` on Windows) |
|
|
21
|
-
| `@noego/sqlego` (`src/client.ts`) | Node or Bun; `node:net` only | Protocol-only client — no SQLite dependency, no native addon |
|
|
21
|
+
| `@noego/sqlego` (`src/client.ts`) | Node or Bun; `node:net` only | Protocol-only client — no SQLite dependency, no native addon, no binary |
|
|
22
22
|
| `src/server.ts` | Bun (`bun:sqlite`) | The owner: per file one `Database` and one socket; many files per process; idle release |
|
|
23
23
|
|
|
24
|
-
Platforms: macOS (arm64, x64), Linux (x64, arm64), Windows x64.
|
|
25
|
-
build
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
Platforms: macOS (arm64, x64), Linux (x64, arm64, glibc), Windows x64. The
|
|
25
|
+
Windows build is cross-compiled and exercised only as a build; it has not been
|
|
26
|
+
run on a Windows machine yet.
|
|
27
|
+
|
|
28
|
+
## Install
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
npm install @noego/sqlego
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
`@noego/sqlego` is the client and the `sqlego` command; it carries no binary.
|
|
35
|
+
It lists the five platform packages as `optionalDependencies`, pinned to its
|
|
36
|
+
own exact version, and npm installs only the one whose `os`/`cpu` match the
|
|
37
|
+
machine (about 60–110 MB unpacked, instead of every platform's binary):
|
|
38
|
+
|
|
39
|
+
| Package | Platform |
|
|
40
|
+
| --- | --- |
|
|
41
|
+
| `@noego/sqlego-darwin-arm64` | macOS, Apple silicon |
|
|
42
|
+
| `@noego/sqlego-darwin-x64` | macOS, Intel |
|
|
43
|
+
| `@noego/sqlego-linux-x64` | Linux x64 (glibc) |
|
|
44
|
+
| `@noego/sqlego-linux-arm64` | Linux arm64 (glibc) |
|
|
45
|
+
| `@noego/sqlego-win32-x64` | Windows x64 |
|
|
46
|
+
|
|
47
|
+
Installing with `--omit=optional` (or `--no-optional`) skips the platform
|
|
48
|
+
package; `connect` then fails with code `binary-missing`, and the message
|
|
49
|
+
names the package to install and `SQLEGO_BIN`. Installing on one machine and
|
|
50
|
+
copying `node_modules` to another platform has the same effect.
|
|
51
|
+
|
|
52
|
+
### Where the server binary comes from
|
|
53
|
+
|
|
54
|
+
In order, and nothing else:
|
|
55
|
+
|
|
56
|
+
1. `SQLEGO_BIN`, the path of a `sqlego` binary, when set. Use it for an
|
|
57
|
+
install without optional dependencies, a platform without a package, or a
|
|
58
|
+
binary you ship yourself.
|
|
59
|
+
2. Under Bun only: the running process when it is the compiled `sqlego`, or
|
|
60
|
+
`bun src/cli.ts` when running from source.
|
|
61
|
+
3. The installed platform package for this machine, resolved the way Node
|
|
62
|
+
resolves an import (`require.resolve`).
|
|
63
|
+
4. A local build next to the package: `bin/sqlego-<platform>-<arch>[.exe]`.
|
|
64
|
+
|
|
65
|
+
Only the host's platform is ever looked up, so a binary built for another
|
|
66
|
+
platform is never chosen. (Before 0.1.2-dev.2 the package shipped an
|
|
67
|
+
unqualified `bin/sqlego`, the publisher's macOS arm64 build, and picked it on
|
|
68
|
+
every host.) `serverCommand` on `connect` bypasses all of this.
|
|
69
|
+
|
|
70
|
+
### Electron
|
|
71
|
+
|
|
72
|
+
Electron packs `node_modules` into `app.asar`, and no executable can start
|
|
73
|
+
from inside that archive. When the resolved binary's path runs through
|
|
74
|
+
`app.asar`, sqlego uses the same path under `app.asar.unpacked`. Tell the
|
|
75
|
+
packager to unpack the platform package, e.g. electron-builder
|
|
76
|
+
`"asarUnpack": ["node_modules/@noego/sqlego-*/**"]`, or Electron Forge
|
|
77
|
+
`packagerConfig.asar.unpack: "**/node_modules/@noego/sqlego-*/**"`. Build the
|
|
78
|
+
app on (or for) each target platform so its platform package is the one
|
|
79
|
+
installed; otherwise set `SQLEGO_BIN` to a binary you bundle.
|
|
29
80
|
|
|
30
81
|
## Lifecycle
|
|
31
82
|
|
|
@@ -198,9 +249,8 @@ A `SqlegoClientError` with code `tx-expired` means the server rolled the
|
|
|
198
249
|
transaction back (lease, hard cap, or disconnect); `tx-busy` means another
|
|
199
250
|
client's transaction kept the request waiting too long.
|
|
200
251
|
|
|
201
|
-
Server location:
|
|
202
|
-
`
|
|
203
|
-
else `sqlego` on `PATH`; pass `serverCommand` to override.
|
|
252
|
+
Server location: see [Where the server binary comes from](#where-the-server-binary-comes-from);
|
|
253
|
+
pass `serverCommand` to override.
|
|
204
254
|
|
|
205
255
|
Other `connect` options: `serverPragmas` (the owner must have these pragmas,
|
|
206
256
|
see above), `maxOwners` (owner processes, see above), `idleTimeoutMs`, `connectTimeoutMs`,
|
|
@@ -211,17 +261,28 @@ see above), `maxOwners` (owner processes, see above), `idleTimeoutMs`, `connectT
|
|
|
211
261
|
```
|
|
212
262
|
bun install
|
|
213
263
|
bun test
|
|
214
|
-
bun run build # → dist/ and bin/sqlego (host executable)
|
|
264
|
+
bun run build # → dist/ and bin/sqlego-<host platform>-<arch> (host executable)
|
|
215
265
|
bun run build:bin:all # → bin/sqlego-{darwin,linux}-{arm64,x64}, bin/sqlego-win32-x64.exe
|
|
266
|
+
bun scripts/release.ts --channel dev --pack ./out # build + stage + check all six packages, write tarballs, publish nothing
|
|
216
267
|
```
|
|
217
268
|
|
|
218
269
|
`bun run build` runs the fresh binary with `--version` and fails unless it
|
|
219
|
-
reports `<package.json version>+<build id>`, so a stale
|
|
220
|
-
|
|
270
|
+
reports `<package.json version>+<build id>`, so a stale binary cannot be
|
|
271
|
+
mistaken for the current one.
|
|
272
|
+
|
|
273
|
+
### Releasing
|
|
274
|
+
|
|
275
|
+
`.github/workflows/release.yml` runs `scripts/release.ts` on every push to
|
|
276
|
+
`release-candidate` (next `<base>-dev.N`, dist-tag `dev`) and `main`
|
|
277
|
+
(`package.json`'s version, dist-tag `latest`, only when npm does not have it,
|
|
278
|
+
then tagged `v<version>`). It builds every platform once, copies each binary
|
|
279
|
+
into `npm/<platform>/` (binaries are gitignored), sets all six packages to one
|
|
280
|
+
version, fails if a platform package lacks its binary or the main package
|
|
281
|
+
carries one, and publishes the five platform packages before the main package.
|
|
221
282
|
|
|
222
283
|
### Versions
|
|
223
284
|
|
|
224
|
-
`package.json` is the only version source
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
285
|
+
`package.json` is the only version source; the release workflow stamps it
|
|
286
|
+
into the platform packages. The stray tag `v0.1.1` predates npm (the first
|
|
287
|
+
publish reset to 0.0.1); 0.1.2 moves past it so git tags and npm versions
|
|
288
|
+
order the same way from here on.
|
package/bin/sqlego.cjs
CHANGED
|
@@ -2,25 +2,63 @@
|
|
|
2
2
|
'use strict';
|
|
3
3
|
/**
|
|
4
4
|
* The `sqlego` command npm installs. It runs the compiled server binary for
|
|
5
|
-
* this platform
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
5
|
+
* this platform; npm's command shims cannot launch a native executable
|
|
6
|
+
* directly on Windows, which is why this launcher exists. Clients spawn the
|
|
7
|
+
* binary itself, not this.
|
|
8
|
+
*
|
|
9
|
+
* Lookup (the same as src/binary.ts, which this CommonJS file cannot import):
|
|
10
|
+
* 1. SQLEGO_BIN;
|
|
11
|
+
* 2. the platform package @noego/sqlego-<platform>-<arch>;
|
|
12
|
+
* 3. a local build, bin/sqlego-<platform>-<arch>[.exe], beside this file.
|
|
13
|
+
* Only the host's platform is looked for; a path through Electron's app.asar
|
|
14
|
+
* is rewritten to app.asar.unpacked.
|
|
9
15
|
*/
|
|
10
16
|
const { spawnSync } = require('node:child_process');
|
|
11
17
|
const { existsSync } = require('node:fs');
|
|
12
18
|
const path = require('node:path');
|
|
13
19
|
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
20
|
+
const SUPPORTED = ['darwin-arm64', 'darwin-x64', 'linux-x64', 'linux-arm64', 'win32-x64'];
|
|
21
|
+
const key = `${process.platform}-${process.arch}`;
|
|
22
|
+
const packageName = `@noego/sqlego-${key}`;
|
|
23
|
+
const exe = process.platform === 'win32' ? '.exe' : '';
|
|
24
|
+
const unpacked = (file) => file.replace(/([\\/])app\.asar(?=[\\/])/, '$1app.asar.unpacked');
|
|
25
|
+
|
|
26
|
+
function locate() {
|
|
27
|
+
const configured = process.env.SQLEGO_BIN;
|
|
28
|
+
if (configured) return { binary: configured };
|
|
29
|
+
const looked = [];
|
|
30
|
+
let manifest = null;
|
|
31
|
+
try {
|
|
32
|
+
manifest = require.resolve(`${packageName}/package.json`);
|
|
33
|
+
} catch {
|
|
34
|
+
manifest = null;
|
|
35
|
+
}
|
|
36
|
+
if (manifest) {
|
|
37
|
+
const fromPackage = unpacked(path.join(path.dirname(manifest), `sqlego${exe}`));
|
|
38
|
+
if (existsSync(fromPackage)) return { binary: fromPackage };
|
|
39
|
+
looked.push(fromPackage);
|
|
40
|
+
}
|
|
41
|
+
const local = unpacked(path.join(__dirname, `sqlego-${key}${exe}`));
|
|
42
|
+
if (existsSync(local)) return { binary: local };
|
|
43
|
+
looked.push(local);
|
|
44
|
+
const reason = !SUPPORTED.includes(key)
|
|
45
|
+
? `sqlego publishes no server binary for ${key} (supported: ${SUPPORTED.join(', ')})`
|
|
46
|
+
: manifest
|
|
47
|
+
? `the package ${packageName} is installed but has no binary`
|
|
48
|
+
: `the package ${packageName} is not installed (npm installs it as an optional dependency of @noego/sqlego; it is skipped by --omit=optional / --no-optional, and on a platform that does not match)`;
|
|
49
|
+
return {
|
|
50
|
+
error: `no sqlego server binary for ${key}: ${reason}; looked in ${looked.join(', ')}. Reinstall without --omit=optional, or set SQLEGO_BIN to the path of a sqlego binary.`,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const found = locate();
|
|
55
|
+
if (found.error) {
|
|
56
|
+
process.stderr.write(`sqlego: ${found.error}\n`);
|
|
19
57
|
process.exit(1);
|
|
20
58
|
}
|
|
21
|
-
const result = spawnSync(binary, process.argv.slice(2), { stdio: 'inherit', windowsHide: true });
|
|
59
|
+
const result = spawnSync(found.binary, process.argv.slice(2), { stdio: 'inherit', windowsHide: true });
|
|
22
60
|
if (result.error) {
|
|
23
|
-
process.stderr.write(`sqlego: cannot run ${binary}: ${result.error.message}\n`);
|
|
61
|
+
process.stderr.write(`sqlego: cannot run ${found.binary}: ${result.error.message}\n`);
|
|
24
62
|
process.exit(1);
|
|
25
63
|
}
|
|
26
64
|
process.exit(result.status === null ? 1 : result.status);
|
package/dist/binary.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Result } from '@noego/trust';
|
|
2
|
+
/**
|
|
3
|
+
* Where the compiled `sqlego` server binary lives. Pure: every input that
|
|
4
|
+
* touches the outside world (environment, module resolution, the file
|
|
5
|
+
* system) is passed in, so the lookup order can be tested for any platform.
|
|
6
|
+
*
|
|
7
|
+
* Order:
|
|
8
|
+
* 1. `SQLEGO_BIN`, when set.
|
|
9
|
+
* 2. The platform package `@noego/sqlego-<platform>-<arch>`, an optional
|
|
10
|
+
* dependency of `@noego/sqlego` that npm installs only on its own os/cpu.
|
|
11
|
+
* 3. A local build, `bin/sqlego-<platform>-<arch>[.exe]` next to the package
|
|
12
|
+
* (a source checkout after `bun run build`).
|
|
13
|
+
* Only the host's platform name is ever looked for, so a binary built for
|
|
14
|
+
* another platform is never chosen. A path inside an Electron `app.asar`
|
|
15
|
+
* archive is rewritten to `app.asar.unpacked`, where an executable can run.
|
|
16
|
+
*/
|
|
17
|
+
/** The platforms a server binary is published for, as Node names them. */
|
|
18
|
+
export declare const SUPPORTED_PLATFORMS: readonly string[];
|
|
19
|
+
export type BinaryLookup = Readonly<{
|
|
20
|
+
platform: string;
|
|
21
|
+
arch: string;
|
|
22
|
+
/** `process.env.SQLEGO_BIN`. */
|
|
23
|
+
configured: string | undefined;
|
|
24
|
+
/** Absolute path of a package's `package.json`, or null when it is not installed. */
|
|
25
|
+
resolvePackageJson: (name: string) => string | null;
|
|
26
|
+
exists: (file: string) => boolean;
|
|
27
|
+
/** The package's own `bin/` directory, or null when it cannot be located. */
|
|
28
|
+
localBinDirectory: string | null;
|
|
29
|
+
}>;
|
|
30
|
+
export type BinaryMissing = Readonly<{
|
|
31
|
+
kind: 'binary-missing';
|
|
32
|
+
message: string;
|
|
33
|
+
}>;
|
|
34
|
+
/** `@noego/sqlego-<platform>-<arch>`: the package holding this platform's binary. */
|
|
35
|
+
export declare function platformPackageName(platform?: string, arch?: string): string;
|
|
36
|
+
/** The binary's file name inside its platform package. */
|
|
37
|
+
export declare function binaryFileName(platform?: string): string;
|
|
38
|
+
/** The binary's file name in a local `bin/` build: platform-qualified, so a foreign build is never mistaken for the host's. */
|
|
39
|
+
export declare function localBinaryName(platform?: string, arch?: string): string;
|
|
40
|
+
/**
|
|
41
|
+
* Electron packs `node_modules` into `app.asar`, a read-only archive no
|
|
42
|
+
* executable can be started from; packaging tools unpack native files to
|
|
43
|
+
* `app.asar.unpacked` beside it. A path through `app.asar` is rewritten to
|
|
44
|
+
* the unpacked twin; any other path is returned unchanged.
|
|
45
|
+
*/
|
|
46
|
+
export declare function unpackedAsarPath(file: string): string;
|
|
47
|
+
export declare function locateServerBinary(lookup: BinaryLookup): Result<string, BinaryMissing>;
|
package/dist/client.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { Result } from '@noego/trust';
|
|
2
|
+
import { type BinaryMissing } from './binary.ts';
|
|
1
3
|
import { type BatchResult, type BeginResult, type ExecResult, type HelloResult, type RunResult, type SqlParams, type SqlRow, type Statement, type StatementResult, type StatusResult, type TransactionMode } from './protocol.ts';
|
|
2
4
|
import { type FilePaths } from './paths.ts';
|
|
3
5
|
/**
|
|
@@ -191,15 +193,17 @@ export declare class SqlegoTransaction {
|
|
|
191
193
|
rollback(): Promise<void>;
|
|
192
194
|
}
|
|
193
195
|
/**
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
*
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
*
|
|
203
|
-
* `sqlego` on PATH.
|
|
196
|
+
* How to start a server, or why there is no way to:
|
|
197
|
+
* 1. `SQLEGO_BIN`, when set;
|
|
198
|
+
* 2. under Bun only — this very process when it is the compiled `sqlego`
|
|
199
|
+
* binary, or the source entry (`bun src/cli.ts`) when running from
|
|
200
|
+
* source; both run on the host by definition;
|
|
201
|
+
* 3. the platform package `@noego/sqlego-<platform>-<arch>`;
|
|
202
|
+
* 4. a local `bin/sqlego-<platform>-<arch>[.exe]` build.
|
|
203
|
+
* Nothing else: not an unqualified `bin/sqlego` (in 0.1.2-dev.0 and earlier
|
|
204
|
+
* that was the publisher's darwin-arm64 build, wrong on every other host),
|
|
205
|
+
* not `sqlego` on PATH. See `locateServerBinary` for 1, 3 and 4.
|
|
204
206
|
*/
|
|
207
|
+
export declare function resolveServerCommand(): Result<readonly string[], BinaryMissing>;
|
|
208
|
+
/** Public, exception-based form of `resolveServerCommand`: throws an Error naming what to install or set. */
|
|
205
209
|
export declare function defaultServerCommand(): readonly string[];
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export { DEFAULT_MAX_OWNERS, SqlegoClient, SqlegoClientError, SqlegoTransaction, defaultServerCommand,
|
|
1
|
+
export { DEFAULT_MAX_OWNERS, SqlegoClient, SqlegoClientError, SqlegoTransaction, defaultServerCommand, type BeginOptions, type ClientOptions, } from './client.ts';
|
|
2
|
+
export { SUPPORTED_PLATFORMS, binaryFileName, localBinaryName, locateServerBinary, platformPackageName, unpackedAsarPath, type BinaryLookup, } from './binary.ts';
|
|
2
3
|
export { USES_NAMED_PIPES, isNamedPipe, pathsFor, runtimeDirectory, canonicalFile, type FilePaths, type ServerMeta } from './paths.ts';
|
|
3
4
|
export { PROTOCOL_VERSION, type SqlParams, type SqlRow, type SqlValue, type Statement, type RunResult, type QueryResult, type BatchResult, type StatementResult, type ExecResult, type BeginResult, type StatusResult, type HelloResult, type TransactionMode, type TransactionStatus, } from './protocol.ts';
|
|
4
5
|
export { VERSION } from './version.ts';
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
// src/client.ts
|
|
2
2
|
import { spawn } from "node:child_process";
|
|
3
3
|
import { closeSync, openSync, readdirSync as readdirSync2, readFileSync as readFileSync2, rmSync, writeSync, existsSync as existsSync3 } from "node:fs";
|
|
4
|
+
import { createRequire } from "node:module";
|
|
4
5
|
import net from "node:net";
|
|
5
|
-
import
|
|
6
|
+
import path4 from "node:path";
|
|
6
7
|
import { fileURLToPath as fileURLToPath2 } from "node:url";
|
|
7
8
|
|
|
8
9
|
// node_modules/@noego/trust/dist/index.js
|
|
@@ -46,6 +47,49 @@ function translate(result, mappers) {
|
|
|
46
47
|
var Result = { ok, err };
|
|
47
48
|
var Trust = { try: tryResult, tryAsync: tryResultAsync, decide, translate };
|
|
48
49
|
|
|
50
|
+
// src/binary.ts
|
|
51
|
+
import path from "node:path";
|
|
52
|
+
var SUPPORTED_PLATFORMS = ["darwin-arm64", "darwin-x64", "linux-x64", "linux-arm64", "win32-x64"];
|
|
53
|
+
function platformPackageName(platform = process.platform, arch = process.arch) {
|
|
54
|
+
return `@noego/sqlego-${platform}-${arch}`;
|
|
55
|
+
}
|
|
56
|
+
function binaryFileName(platform = process.platform) {
|
|
57
|
+
return platform === "win32" ? "sqlego.exe" : "sqlego";
|
|
58
|
+
}
|
|
59
|
+
function localBinaryName(platform = process.platform, arch = process.arch) {
|
|
60
|
+
return `sqlego-${platform}-${arch}${platform === "win32" ? ".exe" : ""}`;
|
|
61
|
+
}
|
|
62
|
+
function unpackedAsarPath(file) {
|
|
63
|
+
return file.replace(/([\\/])app\.asar(?=[\\/])/, "$1app.asar.unpacked");
|
|
64
|
+
}
|
|
65
|
+
function locateServerBinary(lookup) {
|
|
66
|
+
if (lookup.configured !== undefined && lookup.configured.length > 0)
|
|
67
|
+
return Result.ok(lookup.configured);
|
|
68
|
+
const name = platformPackageName(lookup.platform, lookup.arch);
|
|
69
|
+
const manifest = lookup.resolvePackageJson(name);
|
|
70
|
+
const looked = [];
|
|
71
|
+
if (manifest !== null) {
|
|
72
|
+
const fromPackage = unpackedAsarPath(path.join(path.dirname(manifest), binaryFileName(lookup.platform)));
|
|
73
|
+
if (lookup.exists(fromPackage))
|
|
74
|
+
return Result.ok(fromPackage);
|
|
75
|
+
looked.push(fromPackage);
|
|
76
|
+
}
|
|
77
|
+
if (lookup.localBinDirectory !== null) {
|
|
78
|
+
const local = unpackedAsarPath(path.join(lookup.localBinDirectory, localBinaryName(lookup.platform, lookup.arch)));
|
|
79
|
+
if (lookup.exists(local))
|
|
80
|
+
return Result.ok(local);
|
|
81
|
+
looked.push(local);
|
|
82
|
+
}
|
|
83
|
+
const key = `${lookup.platform}-${lookup.arch}`;
|
|
84
|
+
const supported = SUPPORTED_PLATFORMS.includes(key);
|
|
85
|
+
const reason = !supported ? `sqlego publishes no server binary for ${key} (supported: ${SUPPORTED_PLATFORMS.join(", ")})` : manifest === null ? `the package ${name} is not installed (npm installs it as an optional dependency of @noego/sqlego; it is skipped by --omit=optional / --no-optional, and on a platform that does not match)` : `the package ${name} is installed but has no binary`;
|
|
86
|
+
const where = looked.length > 0 ? `; looked in ${looked.join(", ")}` : "";
|
|
87
|
+
return Result.err({
|
|
88
|
+
kind: "binary-missing",
|
|
89
|
+
message: `no sqlego server binary for ${key}: ${reason}${where}. Reinstall without --omit=optional, or set SQLEGO_BIN to the path of a sqlego binary.`
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
49
93
|
// src/protocol.ts
|
|
50
94
|
var PROTOCOL_VERSION = 3;
|
|
51
95
|
var INT64_MIN = -9223372036854775808n;
|
|
@@ -472,10 +516,10 @@ function replaceBigInt(_key, value) {
|
|
|
472
516
|
import { createHash } from "node:crypto";
|
|
473
517
|
import { mkdirSync, realpathSync, existsSync } from "node:fs";
|
|
474
518
|
import { tmpdir } from "node:os";
|
|
475
|
-
import
|
|
519
|
+
import path2 from "node:path";
|
|
476
520
|
function resolveRuntimeDirectory() {
|
|
477
521
|
const configured = process.env.SQLEGO_RUNTIME_DIR;
|
|
478
|
-
const directory = configured && configured.length > 0 ? configured :
|
|
522
|
+
const directory = configured && configured.length > 0 ? configured : path2.join(tmpdir(), "sqlego");
|
|
479
523
|
const created = Trust.try(() => mkdirSync(directory, { recursive: true }), (cause) => ({ kind: "runtime-directory", message: `cannot create sqlego runtime directory ${directory}: ${errorMessage(cause)}` }));
|
|
480
524
|
if (!created.ok)
|
|
481
525
|
return created;
|
|
@@ -483,17 +527,17 @@ function resolveRuntimeDirectory() {
|
|
|
483
527
|
}
|
|
484
528
|
function resolveCanonicalFile(file) {
|
|
485
529
|
return Trust.try(() => {
|
|
486
|
-
const absolute =
|
|
530
|
+
const absolute = path2.resolve(file);
|
|
487
531
|
const missing = [];
|
|
488
532
|
let existing = absolute;
|
|
489
533
|
while (!existsSync(existing)) {
|
|
490
|
-
missing.unshift(
|
|
491
|
-
const parent =
|
|
534
|
+
missing.unshift(path2.basename(existing));
|
|
535
|
+
const parent = path2.dirname(existing);
|
|
492
536
|
if (parent === existing)
|
|
493
537
|
break;
|
|
494
538
|
existing = parent;
|
|
495
539
|
}
|
|
496
|
-
return
|
|
540
|
+
return path2.join(realpathSync(existing), ...missing);
|
|
497
541
|
}, (cause) => ({ kind: "canonical-path", message: `cannot resolve ${file}: ${errorMessage(cause)}` }));
|
|
498
542
|
}
|
|
499
543
|
var MAX_SOCKET_PATH_BYTES = 100;
|
|
@@ -511,7 +555,7 @@ function resolvePaths(file) {
|
|
|
511
555
|
const derived = Trust.try(() => {
|
|
512
556
|
const key = createHash("sha1").update(canonical.value).digest("hex").slice(0, 16);
|
|
513
557
|
const scope = createHash("sha1").update(directory.value.toLowerCase()).digest("hex").slice(0, 8);
|
|
514
|
-
const socket = USES_NAMED_PIPES ? `\\\\.\\pipe\\sqlego-${scope}-${key}` :
|
|
558
|
+
const socket = USES_NAMED_PIPES ? `\\\\.\\pipe\\sqlego-${scope}-${key}` : path2.join(directory.value, `${key}.sock`);
|
|
515
559
|
return { key, socket, socketBytes: Buffer.byteLength(socket) };
|
|
516
560
|
}, (cause) => ({ kind: "hash-failed", message: `cannot hash ${canonical.value}: ${errorMessage(cause)}` }));
|
|
517
561
|
if (!derived.ok)
|
|
@@ -527,8 +571,8 @@ function resolvePaths(file) {
|
|
|
527
571
|
file: canonical.value,
|
|
528
572
|
key,
|
|
529
573
|
socket,
|
|
530
|
-
lock:
|
|
531
|
-
meta:
|
|
574
|
+
lock: path2.join(directory.value, `${key}.lock`),
|
|
575
|
+
meta: path2.join(directory.value, `${key}.json`)
|
|
532
576
|
});
|
|
533
577
|
return Result.ok(paths);
|
|
534
578
|
}
|
|
@@ -579,12 +623,12 @@ function isProcessAlive(pid) {
|
|
|
579
623
|
// src/version.ts
|
|
580
624
|
import { createHash as createHash2 } from "node:crypto";
|
|
581
625
|
import { existsSync as existsSync2, readdirSync, readFileSync } from "node:fs";
|
|
582
|
-
import
|
|
626
|
+
import path3 from "node:path";
|
|
583
627
|
import { fileURLToPath } from "node:url";
|
|
584
628
|
// package.json
|
|
585
629
|
var package_default = {
|
|
586
630
|
name: "@noego/sqlego",
|
|
587
|
-
version: "0.1.2-dev.
|
|
631
|
+
version: "0.1.2-dev.2",
|
|
588
632
|
description: "One owner per SQLite file, many files per owner process. Clients (CLI, Node, Bun) pass SQL over a local socket (Unix) or named pipe (Windows); the owner starts on demand and exits when idle.",
|
|
589
633
|
type: "module",
|
|
590
634
|
license: "ISC",
|
|
@@ -611,7 +655,7 @@ var package_default = {
|
|
|
611
655
|
files: [
|
|
612
656
|
"src",
|
|
613
657
|
"dist",
|
|
614
|
-
"bin",
|
|
658
|
+
"bin/sqlego.cjs",
|
|
615
659
|
"README.md"
|
|
616
660
|
],
|
|
617
661
|
scripts: {
|
|
@@ -621,6 +665,7 @@ var package_default = {
|
|
|
621
665
|
"build:lib": "bun scripts/build.ts lib",
|
|
622
666
|
"build:bin": "bun scripts/build.ts bin",
|
|
623
667
|
"build:bin:all": "bun scripts/build.ts bin:all",
|
|
668
|
+
release: "bun scripts/release.ts",
|
|
624
669
|
typecheck: "bunx tsc --noEmit",
|
|
625
670
|
trust: "trust check src",
|
|
626
671
|
test: "bun test"
|
|
@@ -632,6 +677,13 @@ var package_default = {
|
|
|
632
677
|
},
|
|
633
678
|
dependencies: {
|
|
634
679
|
"@noego/trust": "0.5.0-dev.0"
|
|
680
|
+
},
|
|
681
|
+
optionalDependencies: {
|
|
682
|
+
"@noego/sqlego-darwin-arm64": "0.1.2-dev.2",
|
|
683
|
+
"@noego/sqlego-darwin-x64": "0.1.2-dev.2",
|
|
684
|
+
"@noego/sqlego-linux-x64": "0.1.2-dev.2",
|
|
685
|
+
"@noego/sqlego-linux-arm64": "0.1.2-dev.2",
|
|
686
|
+
"@noego/sqlego-win32-x64": "0.1.2-dev.2"
|
|
635
687
|
}
|
|
636
688
|
};
|
|
637
689
|
|
|
@@ -643,15 +695,15 @@ function sourceBuildId(sourceDirectory) {
|
|
|
643
695
|
const hash = createHash2("sha1");
|
|
644
696
|
for (const name of readdirSync(sourceDirectory).filter((entry) => entry.endsWith(".ts")).sort()) {
|
|
645
697
|
hash.update(name);
|
|
646
|
-
hash.update(readFileSync(
|
|
698
|
+
hash.update(readFileSync(path3.join(sourceDirectory, name)));
|
|
647
699
|
}
|
|
648
700
|
return hash.digest("hex").slice(0, 12);
|
|
649
701
|
}, (cause) => ({ kind: "source-unreadable", message: `cannot hash sources in ${sourceDirectory}: ${errorMessage(cause)}` }));
|
|
650
702
|
}
|
|
651
703
|
function resolveBuildId() {
|
|
652
|
-
if ("
|
|
653
|
-
return "
|
|
654
|
-
const directory = Trust.try(() =>
|
|
704
|
+
if ("183b9c5689c3".length > 0)
|
|
705
|
+
return "183b9c5689c3";
|
|
706
|
+
const directory = Trust.try(() => path3.dirname(fileURLToPath(import.meta.url)), (cause) => ({ kind: "module-path", message: errorMessage(cause) }));
|
|
655
707
|
if (!directory.ok)
|
|
656
708
|
return decideUnknownBuild(directory.error.kind);
|
|
657
709
|
const id = sourceBuildId(directory.value);
|
|
@@ -880,7 +932,7 @@ class SqlegoClient {
|
|
|
880
932
|
return Number.isFinite(configured) && configured >= 0 ? Math.floor(configured) : DEFAULT_MAX_OWNERS;
|
|
881
933
|
}
|
|
882
934
|
spawnLockPath() {
|
|
883
|
-
return
|
|
935
|
+
return path4.join(path4.dirname(this.paths.lock), "spawn.lock");
|
|
884
936
|
}
|
|
885
937
|
async placeFile(deadline) {
|
|
886
938
|
const budget = this.ownerBudget();
|
|
@@ -923,7 +975,7 @@ class SqlegoClient {
|
|
|
923
975
|
return Result.err({ kind: "connect-timeout", message: `no sqlego owner took on ${this.paths.file}${why}` });
|
|
924
976
|
}
|
|
925
977
|
currentOwners() {
|
|
926
|
-
const directory =
|
|
978
|
+
const directory = path4.dirname(this.paths.lock);
|
|
927
979
|
const names = Trust.try(() => readdirSync2(directory), (cause) => ({ kind: "runtime-directory", message: `cannot list sqlego owners in ${directory}: ${errorMessage(cause)}` }));
|
|
928
980
|
if (!names.ok)
|
|
929
981
|
return names;
|
|
@@ -931,7 +983,7 @@ class SqlegoClient {
|
|
|
931
983
|
for (const name of names.value) {
|
|
932
984
|
if (!name.endsWith(".json"))
|
|
933
985
|
continue;
|
|
934
|
-
const text = Trust.try(() => readFileSync2(
|
|
986
|
+
const text = Trust.try(() => readFileSync2(path4.join(directory, name), "utf8"), () => ({ kind: "invalid", message: "unreadable" }));
|
|
935
987
|
const json = text.ok ? fromJson(text.value) : text;
|
|
936
988
|
const meta = json.ok ? parseServerMeta(json.value) : json;
|
|
937
989
|
if (meta.ok) {
|
|
@@ -967,7 +1019,10 @@ class SqlegoClient {
|
|
|
967
1019
|
}
|
|
968
1020
|
}
|
|
969
1021
|
spawnServer() {
|
|
970
|
-
const
|
|
1022
|
+
const resolved = this.options.serverCommand ? Result.ok(this.options.serverCommand) : resolveServerCommand();
|
|
1023
|
+
if (!resolved.ok)
|
|
1024
|
+
return resolved;
|
|
1025
|
+
const [command, ...args] = resolved.value;
|
|
971
1026
|
const pragmaArgs = (this.options.serverPragmas ?? []).flatMap((pragma) => ["--pragma", pragma]);
|
|
972
1027
|
return Trust.try(() => {
|
|
973
1028
|
const child = spawn(command, [...args, "serve", this.paths.file, "--idle-ms", String(this.options.idleTimeoutMs ?? 30000), ...pragmaArgs], {
|
|
@@ -1160,40 +1215,56 @@ function savepointName(name) {
|
|
|
1160
1215
|
return name;
|
|
1161
1216
|
return unwrap(Result.err({ kind: "bad-request", message: `savepoint name must be an identifier (letters, digits, _), not "${name}"` }));
|
|
1162
1217
|
}
|
|
1163
|
-
function
|
|
1164
|
-
const suffix = platform === "win32" ? ".exe" : "";
|
|
1165
|
-
return [`sqlego${suffix}`, `sqlego-${platform}-${arch}${suffix}`];
|
|
1166
|
-
}
|
|
1167
|
-
function defaultServerCommand() {
|
|
1218
|
+
function resolveServerCommand() {
|
|
1168
1219
|
const configured = process.env.SQLEGO_BIN;
|
|
1169
1220
|
if (configured && configured.length > 0)
|
|
1170
|
-
return [configured];
|
|
1171
|
-
const
|
|
1172
|
-
const located = Trust.try(() => path3.dirname(fileURLToPath2(import.meta.url)), (cause) => ({ kind: "module-path", message: errorMessage(cause) }));
|
|
1221
|
+
return Result.ok([configured]);
|
|
1222
|
+
const located = Trust.try(() => path4.dirname(fileURLToPath2(import.meta.url)), (cause) => ({ kind: "module-path", message: errorMessage(cause) }));
|
|
1173
1223
|
let here = null;
|
|
1174
1224
|
if (located.ok)
|
|
1175
1225
|
here = located.value;
|
|
1176
1226
|
else if (located.error.kind === "module-path")
|
|
1177
1227
|
here = null;
|
|
1178
1228
|
if (process.versions.bun) {
|
|
1229
|
+
const entry = process.argv[1] ?? "";
|
|
1230
|
+
if (path4.basename(process.execPath).startsWith("sqlego"))
|
|
1231
|
+
return Result.ok([process.execPath]);
|
|
1179
1232
|
if (entry.endsWith("cli.ts"))
|
|
1180
|
-
return [process.execPath, entry];
|
|
1233
|
+
return Result.ok([process.execPath, entry]);
|
|
1181
1234
|
if (entry.endsWith(".ts") && here !== null) {
|
|
1182
|
-
const cli =
|
|
1235
|
+
const cli = path4.join(here, "cli.ts");
|
|
1183
1236
|
if (existsSync3(cli))
|
|
1184
|
-
return [process.execPath, cli];
|
|
1185
|
-
}
|
|
1186
|
-
if (!entry.endsWith(".js") && !entry.endsWith(".ts"))
|
|
1187
|
-
return [process.execPath];
|
|
1188
|
-
}
|
|
1189
|
-
if (here !== null) {
|
|
1190
|
-
for (const name of platformBinaryNames()) {
|
|
1191
|
-
const local = path3.join(here, "..", "bin", name);
|
|
1192
|
-
if (existsSync3(local))
|
|
1193
|
-
return [local];
|
|
1237
|
+
return Result.ok([process.execPath, cli]);
|
|
1194
1238
|
}
|
|
1195
1239
|
}
|
|
1196
|
-
|
|
1240
|
+
const binary = locateServerBinary({
|
|
1241
|
+
platform: process.platform,
|
|
1242
|
+
arch: process.arch,
|
|
1243
|
+
configured: undefined,
|
|
1244
|
+
resolvePackageJson: (name) => resolvePackageJson(name, here),
|
|
1245
|
+
exists: existsSync3,
|
|
1246
|
+
localBinDirectory: here === null ? null : path4.join(here, "..", "bin")
|
|
1247
|
+
});
|
|
1248
|
+
if (!binary.ok)
|
|
1249
|
+
return binary;
|
|
1250
|
+
return Result.ok([binary.value]);
|
|
1251
|
+
}
|
|
1252
|
+
function defaultServerCommand() {
|
|
1253
|
+
const command = resolveServerCommand();
|
|
1254
|
+
if (command.ok)
|
|
1255
|
+
return command.value;
|
|
1256
|
+
throw new Error(command.error.message);
|
|
1257
|
+
}
|
|
1258
|
+
function resolvePackageJson(name, here) {
|
|
1259
|
+
if (here === null)
|
|
1260
|
+
return null;
|
|
1261
|
+
const resolved = Trust.try(() => createRequire(path4.join(here, "index.js")).resolve(`${name}/package.json`), (cause) => ({ kind: "not-installed", message: errorMessage(cause) }));
|
|
1262
|
+
if (resolved.ok)
|
|
1263
|
+
return typeof resolved.value === "string" ? resolved.value : null;
|
|
1264
|
+
if (resolved.error.kind === "not-installed")
|
|
1265
|
+
return null;
|
|
1266
|
+
const unhandled = resolved.error.kind;
|
|
1267
|
+
return unhandled;
|
|
1197
1268
|
}
|
|
1198
1269
|
var probeFailed = (message) => Result.err({ kind: "probe-failed", message });
|
|
1199
1270
|
function oneShot(socketPath, body, parse, timeoutMs) {
|
|
@@ -1308,15 +1379,20 @@ function sleep(ms) {
|
|
|
1308
1379
|
export {
|
|
1309
1380
|
DEFAULT_MAX_OWNERS,
|
|
1310
1381
|
PROTOCOL_VERSION,
|
|
1382
|
+
SUPPORTED_PLATFORMS,
|
|
1311
1383
|
SqlegoClient,
|
|
1312
1384
|
SqlegoClientError,
|
|
1313
1385
|
SqlegoTransaction,
|
|
1314
1386
|
USES_NAMED_PIPES,
|
|
1315
1387
|
VERSION,
|
|
1388
|
+
binaryFileName,
|
|
1316
1389
|
canonicalFile,
|
|
1317
1390
|
defaultServerCommand,
|
|
1318
1391
|
isNamedPipe,
|
|
1392
|
+
localBinaryName,
|
|
1393
|
+
locateServerBinary,
|
|
1319
1394
|
pathsFor,
|
|
1320
|
-
|
|
1321
|
-
runtimeDirectory
|
|
1395
|
+
platformPackageName,
|
|
1396
|
+
runtimeDirectory,
|
|
1397
|
+
unpackedAsarPath
|
|
1322
1398
|
};
|
package/dist/protocol.d.ts
CHANGED
|
@@ -232,25 +232,25 @@ export type Response = {
|
|
|
232
232
|
* `undefined` for `undefined`, a function or a symbol, and throws on a cycle
|
|
233
233
|
* or a `toJSON` that throws; both are this one failure.
|
|
234
234
|
*/
|
|
235
|
-
export declare function toJson(value: unknown, indent?: number): import("@noego/trust").Err<{
|
|
235
|
+
export declare function toJson(value: unknown, indent?: number): import("@noego/trust").Ok<string> | import("@noego/trust").Err<{
|
|
236
236
|
kind: "encode-failed";
|
|
237
237
|
message: string;
|
|
238
|
-
}
|
|
238
|
+
}>;
|
|
239
239
|
/** One frame: the message's JSON and the newline that ends it. */
|
|
240
|
-
export declare function encodeFrame(message: Request | Response): import("@noego/trust").Err<{
|
|
240
|
+
export declare function encodeFrame(message: Request | Response): import("@noego/trust").Ok<string> | import("@noego/trust").Err<{
|
|
241
241
|
kind: "encode-failed";
|
|
242
242
|
message: string;
|
|
243
|
-
}
|
|
243
|
+
}>;
|
|
244
244
|
/**
|
|
245
245
|
* One request frame with its parameters in wire form: every `bigint` and
|
|
246
246
|
* `Uint8Array` parameter becomes its tag; other values go as they are (the
|
|
247
247
|
* server validates them). A `bigint` outside the signed 64-bit range is a
|
|
248
248
|
* `bad-request` here, before anything is sent.
|
|
249
249
|
*/
|
|
250
|
-
export declare function encodeRequestFrame(request: Request): import("@noego/trust").Err<{
|
|
250
|
+
export declare function encodeRequestFrame(request: Request): import("@noego/trust").Ok<string> | import("@noego/trust").Err<{
|
|
251
251
|
kind: "encode-failed";
|
|
252
252
|
message: string;
|
|
253
|
-
}> | import("@noego/trust").
|
|
253
|
+
}> | import("@noego/trust").Err<{
|
|
254
254
|
kind: "bad-request";
|
|
255
255
|
message: string;
|
|
256
256
|
}>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@noego/sqlego",
|
|
3
|
-
"version": "0.1.2-dev.
|
|
3
|
+
"version": "0.1.2-dev.2",
|
|
4
4
|
"description": "One owner per SQLite file, many files per owner process. Clients (CLI, Node, Bun) pass SQL over a local socket (Unix) or named pipe (Windows); the owner starts on demand and exits when idle.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "ISC",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"files": [
|
|
28
28
|
"src",
|
|
29
29
|
"dist",
|
|
30
|
-
"bin",
|
|
30
|
+
"bin/sqlego.cjs",
|
|
31
31
|
"README.md"
|
|
32
32
|
],
|
|
33
33
|
"scripts": {
|
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
"build:lib": "bun scripts/build.ts lib",
|
|
38
38
|
"build:bin": "bun scripts/build.ts bin",
|
|
39
39
|
"build:bin:all": "bun scripts/build.ts bin:all",
|
|
40
|
+
"release": "bun scripts/release.ts",
|
|
40
41
|
"typecheck": "bunx tsc --noEmit",
|
|
41
42
|
"trust": "trust check src",
|
|
42
43
|
"test": "bun test"
|
|
@@ -48,5 +49,12 @@
|
|
|
48
49
|
},
|
|
49
50
|
"dependencies": {
|
|
50
51
|
"@noego/trust": "0.5.0-dev.0"
|
|
52
|
+
},
|
|
53
|
+
"optionalDependencies": {
|
|
54
|
+
"@noego/sqlego-darwin-arm64": "0.1.2-dev.2",
|
|
55
|
+
"@noego/sqlego-darwin-x64": "0.1.2-dev.2",
|
|
56
|
+
"@noego/sqlego-linux-x64": "0.1.2-dev.2",
|
|
57
|
+
"@noego/sqlego-linux-arm64": "0.1.2-dev.2",
|
|
58
|
+
"@noego/sqlego-win32-x64": "0.1.2-dev.2"
|
|
51
59
|
}
|
|
52
60
|
}
|
package/src/binary.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { Result } from '@noego/trust';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Where the compiled `sqlego` server binary lives. Pure: every input that
|
|
6
|
+
* touches the outside world (environment, module resolution, the file
|
|
7
|
+
* system) is passed in, so the lookup order can be tested for any platform.
|
|
8
|
+
*
|
|
9
|
+
* Order:
|
|
10
|
+
* 1. `SQLEGO_BIN`, when set.
|
|
11
|
+
* 2. The platform package `@noego/sqlego-<platform>-<arch>`, an optional
|
|
12
|
+
* dependency of `@noego/sqlego` that npm installs only on its own os/cpu.
|
|
13
|
+
* 3. A local build, `bin/sqlego-<platform>-<arch>[.exe]` next to the package
|
|
14
|
+
* (a source checkout after `bun run build`).
|
|
15
|
+
* Only the host's platform name is ever looked for, so a binary built for
|
|
16
|
+
* another platform is never chosen. A path inside an Electron `app.asar`
|
|
17
|
+
* archive is rewritten to `app.asar.unpacked`, where an executable can run.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** The platforms a server binary is published for, as Node names them. */
|
|
21
|
+
export const SUPPORTED_PLATFORMS: readonly string[] = ['darwin-arm64', 'darwin-x64', 'linux-x64', 'linux-arm64', 'win32-x64'];
|
|
22
|
+
|
|
23
|
+
export type BinaryLookup = Readonly<{
|
|
24
|
+
platform: string;
|
|
25
|
+
arch: string;
|
|
26
|
+
/** `process.env.SQLEGO_BIN`. */
|
|
27
|
+
configured: string | undefined;
|
|
28
|
+
/** Absolute path of a package's `package.json`, or null when it is not installed. */
|
|
29
|
+
resolvePackageJson: (name: string) => string | null;
|
|
30
|
+
exists: (file: string) => boolean;
|
|
31
|
+
/** The package's own `bin/` directory, or null when it cannot be located. */
|
|
32
|
+
localBinDirectory: string | null;
|
|
33
|
+
}>;
|
|
34
|
+
|
|
35
|
+
export type BinaryMissing = Readonly<{ kind: 'binary-missing'; message: string }>;
|
|
36
|
+
|
|
37
|
+
/** `@noego/sqlego-<platform>-<arch>`: the package holding this platform's binary. */
|
|
38
|
+
export function platformPackageName(platform: string = process.platform, arch: string = process.arch): string {
|
|
39
|
+
return `@noego/sqlego-${platform}-${arch}`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The binary's file name inside its platform package. */
|
|
43
|
+
export function binaryFileName(platform: string = process.platform): string {
|
|
44
|
+
return platform === 'win32' ? 'sqlego.exe' : 'sqlego';
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The binary's file name in a local `bin/` build: platform-qualified, so a foreign build is never mistaken for the host's. */
|
|
48
|
+
export function localBinaryName(platform: string = process.platform, arch: string = process.arch): string {
|
|
49
|
+
return `sqlego-${platform}-${arch}${platform === 'win32' ? '.exe' : ''}`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Electron packs `node_modules` into `app.asar`, a read-only archive no
|
|
54
|
+
* executable can be started from; packaging tools unpack native files to
|
|
55
|
+
* `app.asar.unpacked` beside it. A path through `app.asar` is rewritten to
|
|
56
|
+
* the unpacked twin; any other path is returned unchanged.
|
|
57
|
+
*/
|
|
58
|
+
export function unpackedAsarPath(file: string): string {
|
|
59
|
+
return file.replace(/([\\/])app\.asar(?=[\\/])/, '$1app.asar.unpacked');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function locateServerBinary(lookup: BinaryLookup): Result<string, BinaryMissing> {
|
|
63
|
+
if (lookup.configured !== undefined && lookup.configured.length > 0) return Result.ok(lookup.configured);
|
|
64
|
+
const name = platformPackageName(lookup.platform, lookup.arch);
|
|
65
|
+
const manifest = lookup.resolvePackageJson(name);
|
|
66
|
+
const looked: string[] = [];
|
|
67
|
+
if (manifest !== null) {
|
|
68
|
+
const fromPackage = unpackedAsarPath(path.join(path.dirname(manifest), binaryFileName(lookup.platform)));
|
|
69
|
+
if (lookup.exists(fromPackage)) return Result.ok(fromPackage);
|
|
70
|
+
looked.push(fromPackage);
|
|
71
|
+
}
|
|
72
|
+
if (lookup.localBinDirectory !== null) {
|
|
73
|
+
const local = unpackedAsarPath(path.join(lookup.localBinDirectory, localBinaryName(lookup.platform, lookup.arch)));
|
|
74
|
+
if (lookup.exists(local)) return Result.ok(local);
|
|
75
|
+
looked.push(local);
|
|
76
|
+
}
|
|
77
|
+
const key = `${lookup.platform}-${lookup.arch}`;
|
|
78
|
+
const supported = SUPPORTED_PLATFORMS.includes(key);
|
|
79
|
+
const reason = !supported
|
|
80
|
+
? `sqlego publishes no server binary for ${key} (supported: ${SUPPORTED_PLATFORMS.join(', ')})`
|
|
81
|
+
: manifest === null
|
|
82
|
+
? `the package ${name} is not installed (npm installs it as an optional dependency of @noego/sqlego; it is skipped by --omit=optional / --no-optional, and on a platform that does not match)`
|
|
83
|
+
: `the package ${name} is installed but has no binary`;
|
|
84
|
+
const where = looked.length > 0 ? `; looked in ${looked.join(', ')}` : '';
|
|
85
|
+
return Result.err({
|
|
86
|
+
kind: 'binary-missing',
|
|
87
|
+
message: `no sqlego server binary for ${key}: ${reason}${where}. Reinstall without --omit=optional, or set SQLEGO_BIN to the path of a sqlego binary.`,
|
|
88
|
+
});
|
|
89
|
+
}
|
package/src/client.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
2
|
import { closeSync, openSync, readdirSync, readFileSync, rmSync, writeSync, existsSync } from 'node:fs';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
3
4
|
import net from 'node:net';
|
|
4
5
|
import path from 'node:path';
|
|
5
6
|
import { fileURLToPath } from 'node:url';
|
|
6
7
|
import { Result, Trust } from '@noego/trust';
|
|
8
|
+
import { locateServerBinary, type BinaryMissing } from './binary.ts';
|
|
7
9
|
import {
|
|
8
10
|
PROTOCOL_VERSION,
|
|
9
11
|
encodeRequestFrame,
|
|
@@ -509,7 +511,9 @@ export class SqlegoClient {
|
|
|
509
511
|
}
|
|
510
512
|
|
|
511
513
|
private spawnServer() {
|
|
512
|
-
const
|
|
514
|
+
const resolved = this.options.serverCommand ? Result.ok(this.options.serverCommand) : resolveServerCommand();
|
|
515
|
+
if (!resolved.ok) return resolved;
|
|
516
|
+
const [command, ...args] = resolved.value;
|
|
513
517
|
const pragmaArgs = (this.options.serverPragmas ?? []).flatMap((pragma) => ['--pragma', pragma]);
|
|
514
518
|
return Trust.try(
|
|
515
519
|
() => {
|
|
@@ -765,48 +769,68 @@ function savepointName(name: string): string {
|
|
|
765
769
|
}
|
|
766
770
|
|
|
767
771
|
/**
|
|
768
|
-
*
|
|
769
|
-
*
|
|
770
|
-
*
|
|
772
|
+
* How to start a server, or why there is no way to:
|
|
773
|
+
* 1. `SQLEGO_BIN`, when set;
|
|
774
|
+
* 2. under Bun only — this very process when it is the compiled `sqlego`
|
|
775
|
+
* binary, or the source entry (`bun src/cli.ts`) when running from
|
|
776
|
+
* source; both run on the host by definition;
|
|
777
|
+
* 3. the platform package `@noego/sqlego-<platform>-<arch>`;
|
|
778
|
+
* 4. a local `bin/sqlego-<platform>-<arch>[.exe]` build.
|
|
779
|
+
* Nothing else: not an unqualified `bin/sqlego` (in 0.1.2-dev.0 and earlier
|
|
780
|
+
* that was the publisher's darwin-arm64 build, wrong on every other host),
|
|
781
|
+
* not `sqlego` on PATH. See `locateServerBinary` for 1, 3 and 4.
|
|
771
782
|
*/
|
|
772
|
-
export function
|
|
773
|
-
const suffix = platform === 'win32' ? '.exe' : '';
|
|
774
|
-
return [`sqlego${suffix}`, `sqlego-${platform}-${arch}${suffix}`];
|
|
775
|
-
}
|
|
776
|
-
|
|
777
|
-
/**
|
|
778
|
-
* How to start a server: SQLEGO_BIN if set; the compiled `sqlego` binary when
|
|
779
|
-
* running as one; the dev entry (`bun src/cli.ts`) when running from source
|
|
780
|
-
* under Bun; the binary for this platform in the package's `bin/`; else
|
|
781
|
-
* `sqlego` on PATH.
|
|
782
|
-
*/
|
|
783
|
-
export function defaultServerCommand(): readonly string[] {
|
|
783
|
+
export function resolveServerCommand(): Result<readonly string[], BinaryMissing> {
|
|
784
784
|
const configured = process.env.SQLEGO_BIN;
|
|
785
|
-
if (configured && configured.length > 0) return [configured];
|
|
786
|
-
const entry = process.argv[1] ?? '';
|
|
785
|
+
if (configured && configured.length > 0) return Result.ok([configured]);
|
|
787
786
|
const located = Trust.try(
|
|
788
787
|
() => path.dirname(fileURLToPath(import.meta.url)),
|
|
789
788
|
(cause) => ({ kind: 'module-path' as const, message: errorMessage(cause) }),
|
|
790
789
|
);
|
|
791
|
-
// A module that cannot locate itself has no sibling cli.ts or bin/ to offer.
|
|
790
|
+
// A module that cannot locate itself has no sibling cli.ts, package or bin/ to offer.
|
|
792
791
|
let here: string | null = null;
|
|
793
792
|
if (located.ok) here = located.value;
|
|
794
793
|
else if (located.error.kind === 'module-path') here = null;
|
|
795
794
|
if (process.versions.bun) {
|
|
796
|
-
|
|
795
|
+
const entry = process.argv[1] ?? '';
|
|
796
|
+
if (path.basename(process.execPath).startsWith('sqlego')) return Result.ok([process.execPath]);
|
|
797
|
+
if (entry.endsWith('cli.ts')) return Result.ok([process.execPath, entry]);
|
|
797
798
|
if (entry.endsWith('.ts') && here !== null) {
|
|
798
799
|
const cli = path.join(here, 'cli.ts');
|
|
799
|
-
if (existsSync(cli)) return [process.execPath, cli];
|
|
800
|
-
}
|
|
801
|
-
if (!entry.endsWith('.js') && !entry.endsWith('.ts')) return [process.execPath];
|
|
802
|
-
}
|
|
803
|
-
if (here !== null) {
|
|
804
|
-
for (const name of platformBinaryNames()) {
|
|
805
|
-
const local = path.join(here, '..', 'bin', name);
|
|
806
|
-
if (existsSync(local)) return [local];
|
|
800
|
+
if (existsSync(cli)) return Result.ok([process.execPath, cli]);
|
|
807
801
|
}
|
|
808
802
|
}
|
|
809
|
-
|
|
803
|
+
const binary = locateServerBinary({
|
|
804
|
+
platform: process.platform,
|
|
805
|
+
arch: process.arch,
|
|
806
|
+
configured: undefined,
|
|
807
|
+
resolvePackageJson: (name) => resolvePackageJson(name, here),
|
|
808
|
+
exists: existsSync,
|
|
809
|
+
localBinDirectory: here === null ? null : path.join(here, '..', 'bin'),
|
|
810
|
+
});
|
|
811
|
+
if (!binary.ok) return binary;
|
|
812
|
+
return Result.ok([binary.value]);
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
/** Public, exception-based form of `resolveServerCommand`: throws an Error naming what to install or set. */
|
|
816
|
+
export function defaultServerCommand(): readonly string[] {
|
|
817
|
+
const command = resolveServerCommand();
|
|
818
|
+
if (command.ok) return command.value;
|
|
819
|
+
//trust:accept public API contract: defaultServerCommand throws an Error when no server binary can be found
|
|
820
|
+
throw new Error(command.error.message);
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
/** A package's `package.json`, resolved the way Node resolves an import from this module; null when not installed. */
|
|
824
|
+
function resolvePackageJson(name: string, here: string | null): string | null {
|
|
825
|
+
if (here === null) return null;
|
|
826
|
+
const resolved = Trust.try(
|
|
827
|
+
(): unknown => createRequire(path.join(here, 'index.js')).resolve(`${name}/package.json`),
|
|
828
|
+
(cause) => ({ kind: 'not-installed' as const, message: errorMessage(cause) }),
|
|
829
|
+
);
|
|
830
|
+
if (resolved.ok) return typeof resolved.value === 'string' ? resolved.value : null;
|
|
831
|
+
if (resolved.error.kind === 'not-installed') return null;
|
|
832
|
+
const unhandled: never = resolved.error.kind;
|
|
833
|
+
return unhandled;
|
|
810
834
|
}
|
|
811
835
|
|
|
812
836
|
/** Why a one-shot request to another owner got no result: the transport, or the owner's own error reply. */
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
export {
|
|
2
|
-
DEFAULT_MAX_OWNERS, SqlegoClient, SqlegoClientError, SqlegoTransaction, defaultServerCommand,
|
|
2
|
+
DEFAULT_MAX_OWNERS, SqlegoClient, SqlegoClientError, SqlegoTransaction, defaultServerCommand, type BeginOptions, type ClientOptions,
|
|
3
3
|
} from './client.ts';
|
|
4
|
+
export {
|
|
5
|
+
SUPPORTED_PLATFORMS, binaryFileName, localBinaryName, locateServerBinary, platformPackageName, unpackedAsarPath, type BinaryLookup,
|
|
6
|
+
} from './binary.ts';
|
|
4
7
|
export { USES_NAMED_PIPES, isNamedPipe, pathsFor, runtimeDirectory, canonicalFile, type FilePaths, type ServerMeta } from './paths.ts';
|
|
5
8
|
export {
|
|
6
9
|
PROTOCOL_VERSION,
|
package/bin/sqlego
DELETED