@coffre/db 0.14.4 → 0.15.0
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 +2 -1
- package/dist/change-records-_fKvahvs.js +55 -0
- package/dist/connect.d.ts +22 -10
- package/dist/connect.js +74 -12
- package/dist/database-CVteLhp1.d.ts +410 -0
- package/dist/{database-BadNeeN-.js → database-D55ENPX-.js} +46 -25
- package/dist/dialect.d.ts +4 -3
- package/dist/dialect.js +14 -5
- package/dist/grants.d.ts +4 -4
- package/dist/grants.js +4 -4
- package/dist/hyperdrive.d.ts +48 -23
- package/dist/hyperdrive.js +168 -91
- package/dist/index.d.ts +3 -3
- package/dist/index.js +1 -1
- package/dist/log-DaD7fyUq.d.ts +722 -0
- package/dist/log.d.ts +2 -596
- package/dist/log.js +224 -10
- package/dist/migrate.d.ts +43 -3
- package/dist/migrate.js +149 -29
- package/dist/migrations/postgres/0021_prune_vault_releases.sql +19 -0
- package/dist/migrations/postgres/0022_log_015.sql +672 -0
- package/dist/migrations/postgres/0023_change_path.sql +153 -0
- package/dist/migrations/postgres/0024_use_015.sql +556 -0
- package/dist/migrations/postgres/0025_app_change.sql +221 -0
- package/dist/migrations/postgres/0026_issue_lock.sql +106 -0
- package/dist/migrations/postgres/0027_retention.sql +118 -0
- package/dist/migrations/postgres/0028_use_stalls.sql +594 -0
- package/dist/migrations/postgres/0029_use_objects.sql +887 -0
- package/dist/migrations/postgres/0030_unarchive_restores.sql +259 -0
- package/dist/migrations/postgres/0031_app_read.sql +65 -0
- package/dist/migrations/postgres/0032_secrets_live_index.sql +4 -0
- package/dist/migrations/postgres/0033_use_generations.sql +582 -0
- package/dist/migrations/postgres/0034_vault_credentials.sql +153 -0
- package/dist/migrations/postgres/0035_first_days.sql +3 -0
- package/dist/migrations/postgres/0036_vault_issues.sql +91 -0
- package/dist/migrations/postgres/0037_vault_bindings.sql +271 -0
- package/dist/migrations/postgres/0040_binding_origin.sql +159 -0
- package/dist/migrations/postgres/0041_cutover.sql +132 -0
- package/dist/migrations/postgres/0042_sign_in_index.sql +7 -0
- package/dist/migrations/postgres/0043_issue_revoke_index.sql +4 -0
- package/dist/migrations/postgres/0044_use_by_name.sql +705 -0
- package/dist/migrations/postgres/0045_decide_first.sql +878 -0
- package/dist/migrations/postgres/0046_way_in_uses.sql +1149 -0
- package/dist/migrations/postgres/0047_cutover_finish.sql +268 -0
- package/dist/migrations/postgres/0048_use_batch.sql +59 -0
- package/dist/migrations/postgres/0049_reads.sql +171 -0
- package/dist/migrations/postgres/meta/0021_snapshot.json +3334 -0
- package/dist/migrations/postgres/meta/0022_snapshot.json +3831 -0
- package/dist/migrations/postgres/meta/0023_snapshot.json +3831 -0
- package/dist/migrations/postgres/meta/0024_snapshot.json +3831 -0
- package/dist/migrations/postgres/meta/0025_snapshot.json +3831 -0
- package/dist/migrations/postgres/meta/0026_snapshot.json +3831 -0
- package/dist/migrations/postgres/meta/0027_snapshot.json +3853 -0
- package/dist/migrations/postgres/meta/0028_snapshot.json +3853 -0
- package/dist/migrations/postgres/meta/0029_snapshot.json +3859 -0
- package/dist/migrations/postgres/meta/0030_snapshot.json +3859 -0
- package/dist/migrations/postgres/meta/0031_snapshot.json +3859 -0
- package/dist/migrations/postgres/meta/0032_snapshot.json +3875 -0
- package/dist/migrations/postgres/meta/0033_snapshot.json +3875 -0
- package/dist/migrations/postgres/meta/0034_snapshot.json +3875 -0
- package/dist/migrations/postgres/meta/0035_snapshot.json +3875 -0
- package/dist/migrations/postgres/meta/0036_snapshot.json +3875 -0
- package/dist/migrations/postgres/meta/0037_snapshot.json +3875 -0
- package/dist/migrations/postgres/meta/0038_snapshot.json +3881 -0
- package/dist/migrations/postgres/meta/0039_snapshot.json +3881 -0
- package/dist/migrations/postgres/meta/0040_snapshot.json +3931 -0
- package/dist/migrations/postgres/meta/0041_snapshot.json +3981 -0
- package/dist/migrations/postgres/meta/0042_snapshot.json +3981 -0
- package/dist/migrations/postgres/meta/0043_snapshot.json +3981 -0
- package/dist/migrations/postgres/meta/0044_snapshot.json +3991 -0
- package/dist/migrations/postgres/meta/0045_snapshot.json +3997 -0
- package/dist/migrations/postgres/meta/0046_snapshot.json +3997 -0
- package/dist/migrations/postgres/meta/0047_snapshot.json +4234 -0
- package/dist/migrations/postgres/meta/_journal.json +189 -0
- package/dist/migrations/sqlite/0018_log_015.sql +84 -0
- package/dist/migrations/sqlite/0019_retention.sql +2 -0
- package/dist/migrations/sqlite/0020_ended_references.sql +1 -0
- package/dist/migrations/sqlite/0021_secrets_live_index.sql +1 -0
- package/dist/migrations/sqlite/0022_binding_origin.sql +1 -0
- package/dist/migrations/sqlite/0023_sign_in_index.sql +2 -0
- package/dist/migrations/sqlite/0024_issue_revoke_index.sql +2 -0
- package/dist/migrations/sqlite/0025_way_in_uses.sql +42 -0
- package/dist/migrations/sqlite/0026_cutover_finish.sql +11 -0
- package/dist/migrations/sqlite/0027_reads.sql +38 -0
- package/dist/migrations/sqlite/meta/0018_snapshot.json +3529 -0
- package/dist/migrations/sqlite/meta/0019_snapshot.json +3538 -0
- package/dist/migrations/sqlite/meta/0020_snapshot.json +3545 -0
- package/dist/migrations/sqlite/meta/0021_snapshot.json +3553 -0
- package/dist/migrations/sqlite/meta/0022_snapshot.json +3560 -0
- package/dist/migrations/sqlite/meta/0023_snapshot.json +3579 -0
- package/dist/migrations/sqlite/meta/0024_snapshot.json +3598 -0
- package/dist/migrations/sqlite/meta/0025_snapshot.json +3609 -0
- package/dist/migrations/sqlite/meta/0026_snapshot.json +3616 -0
- package/dist/migrations/sqlite/meta/0027_snapshot.json +3834 -0
- package/dist/migrations/sqlite/meta/_journal.json +70 -0
- package/dist/platform-Y3gICK6n.d.ts +58 -0
- package/dist/portable.d.ts +2 -2
- package/dist/postgres-BI3xTnyw.js +1718 -0
- package/dist/postgres-platform-node.d.ts +14 -0
- package/dist/postgres-platform-node.js +21 -0
- package/dist/postgres-platform-workerd.d.ts +61 -0
- package/dist/postgres-platform-workerd.js +163 -0
- package/dist/{schema-z8GcN_Xu.d.ts → schema-DgoPqqsI.d.ts} +1329 -9
- package/dist/schema-sqlite.d.ts +1308 -8
- package/dist/schema-sqlite.js +156 -20
- package/dist/schema-version.d.ts +1 -1
- package/dist/schema-version.js +261 -2
- package/dist/schema.d.ts +2 -2
- package/dist/schema.js +229 -22
- package/package.json +13 -5
- package/src/active-subject.ts +9 -0
- package/src/change-records.ts +38 -0
- package/src/connect.ts +67 -13
- package/src/database.ts +67 -51
- package/src/dialect.ts +17 -8
- package/src/grants.ts +3 -3
- package/src/hyperdrive.ts +181 -104
- package/src/log.ts +244 -11
- package/src/migrate.ts +157 -30
- package/src/migrations/postgres/0021_prune_vault_releases.sql +19 -0
- package/src/migrations/postgres/0022_log_015.sql +672 -0
- package/src/migrations/postgres/0023_change_path.sql +153 -0
- package/src/migrations/postgres/0024_use_015.sql +556 -0
- package/src/migrations/postgres/0025_app_change.sql +221 -0
- package/src/migrations/postgres/0026_issue_lock.sql +106 -0
- package/src/migrations/postgres/0027_retention.sql +118 -0
- package/src/migrations/postgres/0028_use_stalls.sql +594 -0
- package/src/migrations/postgres/0029_use_objects.sql +887 -0
- package/src/migrations/postgres/0030_unarchive_restores.sql +259 -0
- package/src/migrations/postgres/0031_app_read.sql +65 -0
- package/src/migrations/postgres/0032_secrets_live_index.sql +4 -0
- package/src/migrations/postgres/0033_use_generations.sql +582 -0
- package/src/migrations/postgres/0034_vault_credentials.sql +153 -0
- package/src/migrations/postgres/0035_first_days.sql +3 -0
- package/src/migrations/postgres/0036_vault_issues.sql +91 -0
- package/src/migrations/postgres/0037_vault_bindings.sql +271 -0
- package/src/migrations/postgres/0040_binding_origin.sql +159 -0
- package/src/migrations/postgres/0041_cutover.sql +132 -0
- package/src/migrations/postgres/0042_sign_in_index.sql +7 -0
- package/src/migrations/postgres/0043_issue_revoke_index.sql +4 -0
- package/src/migrations/postgres/0044_use_by_name.sql +705 -0
- package/src/migrations/postgres/0045_decide_first.sql +878 -0
- package/src/migrations/postgres/0046_way_in_uses.sql +1149 -0
- package/src/migrations/postgres/0047_cutover_finish.sql +268 -0
- package/src/migrations/postgres/0048_use_batch.sql +59 -0
- package/src/migrations/postgres/0049_reads.sql +171 -0
- package/src/migrations/postgres/meta/0021_snapshot.json +3334 -0
- package/src/migrations/postgres/meta/0022_snapshot.json +3831 -0
- package/src/migrations/postgres/meta/0023_snapshot.json +3831 -0
- package/src/migrations/postgres/meta/0024_snapshot.json +3831 -0
- package/src/migrations/postgres/meta/0025_snapshot.json +3831 -0
- package/src/migrations/postgres/meta/0026_snapshot.json +3831 -0
- package/src/migrations/postgres/meta/0027_snapshot.json +3853 -0
- package/src/migrations/postgres/meta/0028_snapshot.json +3853 -0
- package/src/migrations/postgres/meta/0029_snapshot.json +3859 -0
- package/src/migrations/postgres/meta/0030_snapshot.json +3859 -0
- package/src/migrations/postgres/meta/0031_snapshot.json +3859 -0
- package/src/migrations/postgres/meta/0032_snapshot.json +3875 -0
- package/src/migrations/postgres/meta/0033_snapshot.json +3875 -0
- package/src/migrations/postgres/meta/0034_snapshot.json +3875 -0
- package/src/migrations/postgres/meta/0035_snapshot.json +3875 -0
- package/src/migrations/postgres/meta/0036_snapshot.json +3875 -0
- package/src/migrations/postgres/meta/0037_snapshot.json +3875 -0
- package/src/migrations/postgres/meta/0038_snapshot.json +3881 -0
- package/src/migrations/postgres/meta/0039_snapshot.json +3881 -0
- package/src/migrations/postgres/meta/0040_snapshot.json +3931 -0
- package/src/migrations/postgres/meta/0041_snapshot.json +3981 -0
- package/src/migrations/postgres/meta/0042_snapshot.json +3981 -0
- package/src/migrations/postgres/meta/0043_snapshot.json +3981 -0
- package/src/migrations/postgres/meta/0044_snapshot.json +3991 -0
- package/src/migrations/postgres/meta/0045_snapshot.json +3997 -0
- package/src/migrations/postgres/meta/0046_snapshot.json +3997 -0
- package/src/migrations/postgres/meta/0047_snapshot.json +4234 -0
- package/src/migrations/postgres/meta/_journal.json +189 -0
- package/src/migrations/sqlite/0018_log_015.sql +84 -0
- package/src/migrations/sqlite/0019_retention.sql +2 -0
- package/src/migrations/sqlite/0020_ended_references.sql +1 -0
- package/src/migrations/sqlite/0021_secrets_live_index.sql +1 -0
- package/src/migrations/sqlite/0022_binding_origin.sql +1 -0
- package/src/migrations/sqlite/0023_sign_in_index.sql +2 -0
- package/src/migrations/sqlite/0024_issue_revoke_index.sql +2 -0
- package/src/migrations/sqlite/0025_way_in_uses.sql +42 -0
- package/src/migrations/sqlite/0026_cutover_finish.sql +11 -0
- package/src/migrations/sqlite/0027_reads.sql +38 -0
- package/src/migrations/sqlite/meta/0018_snapshot.json +3529 -0
- package/src/migrations/sqlite/meta/0019_snapshot.json +3538 -0
- package/src/migrations/sqlite/meta/0020_snapshot.json +3545 -0
- package/src/migrations/sqlite/meta/0021_snapshot.json +3553 -0
- package/src/migrations/sqlite/meta/0022_snapshot.json +3560 -0
- package/src/migrations/sqlite/meta/0023_snapshot.json +3579 -0
- package/src/migrations/sqlite/meta/0024_snapshot.json +3598 -0
- package/src/migrations/sqlite/meta/0025_snapshot.json +3609 -0
- package/src/migrations/sqlite/meta/0026_snapshot.json +3616 -0
- package/src/migrations/sqlite/meta/0027_snapshot.json +3834 -0
- package/src/migrations/sqlite/meta/_journal.json +70 -0
- package/src/postgres-js/README.md +183 -0
- package/src/postgres-js/UNLICENSE +24 -0
- package/src/postgres-js/bytes.ts +89 -0
- package/src/postgres-js/connection.ts +882 -0
- package/src/postgres-js/errors.ts +84 -0
- package/src/postgres-js/index.ts +516 -0
- package/src/postgres-js/platform.node.ts +29 -0
- package/src/postgres-js/platform.ts +60 -0
- package/src/postgres-js/platform.workerd.ts +225 -0
- package/src/postgres-js/query.ts +123 -0
- package/src/postgres-js/queue.ts +42 -0
- package/src/postgres-js/result.ts +46 -0
- package/src/postgres-js/types.ts +52 -0
- package/src/postgres.ts +375 -26
- package/src/schema-version.ts +2 -2
- package/src/schema.sqlite.ts +200 -9
- package/src/schema.ts +294 -8
- package/src/verify-runtime.ts +18 -29
- package/LICENSE +0 -21
- package/dist/database-CFhTnnzM.d.ts +0 -73
- package/dist/postgres-Bmar8y0q.js +0 -29
- package/dist/relations-BfR3JjT1.js +0 -38
- package/src/relations.ts +0 -40
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { t as Platform } from "./platform-Y3gICK6n.js";
|
|
2
|
+
//#region src/postgres-js/platform.node.d.ts
|
|
3
|
+
export declare const tcp: Platform['tcp'];
|
|
4
|
+
export declare const startTls: Platform['startTls'];
|
|
5
|
+
export declare const isIP: Platform['isIP'];
|
|
6
|
+
export declare const setImmediate: Platform['setImmediate'];
|
|
7
|
+
export declare const clearImmediate: Platform['clearImmediate'];
|
|
8
|
+
export declare const now: Platform['now'];
|
|
9
|
+
export declare const randomBytes: Platform['randomBytes'];
|
|
10
|
+
export declare const pbkdf2: Platform['pbkdf2'];
|
|
11
|
+
export declare const hmac: Platform['hmac'];
|
|
12
|
+
export declare const sha256: Platform['sha256'];
|
|
13
|
+
export declare const md5: Platform['md5'];
|
|
14
|
+
//#endregion
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import net from "node:net";
|
|
2
|
+
import tls from "node:tls";
|
|
3
|
+
import crypto from "node:crypto";
|
|
4
|
+
import { performance } from "node:perf_hooks";
|
|
5
|
+
//#region src/postgres-js/platform.node.ts
|
|
6
|
+
const tcp = (to, connected) => "path" in to ? net.connect(to.path, connected) : net.connect(to.port, to.host, connected);
|
|
7
|
+
const startTls = (socket, options) => tls.connect({
|
|
8
|
+
...options,
|
|
9
|
+
socket
|
|
10
|
+
});
|
|
11
|
+
const isIP = (host) => net.isIP(host);
|
|
12
|
+
const setImmediate = (fn) => globalThis.setImmediate(fn);
|
|
13
|
+
const clearImmediate = (handle) => globalThis.clearImmediate(handle);
|
|
14
|
+
const now = () => performance.now();
|
|
15
|
+
const randomBytes = (length) => crypto.randomBytes(length);
|
|
16
|
+
const pbkdf2 = async (password, salt, iterations, length) => crypto.pbkdf2Sync(password, salt, iterations, length, "sha256");
|
|
17
|
+
const hmac = async (key, text) => crypto.createHmac("sha256", key).update(text).digest();
|
|
18
|
+
const sha256 = async (data) => crypto.createHash("sha256").update(data).digest();
|
|
19
|
+
const md5 = async (data) => crypto.createHash("md5").update(data).digest("hex");
|
|
20
|
+
//#endregion
|
|
21
|
+
export { clearImmediate, hmac, isIP, md5, now, pbkdf2, randomBytes, setImmediate, sha256, startTls, tcp };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { t as Platform } from "./platform-Y3gICK6n.js";
|
|
2
|
+
import { EventEmitter } from "node:events";
|
|
3
|
+
//#region src/postgres-js/platform.workerd.d.ts
|
|
4
|
+
/** A socket of `cloudflare:sockets`, what of it this uses. */
|
|
5
|
+
interface RawSocket {
|
|
6
|
+
readonly readable: ReadableStream<Uint8Array>;
|
|
7
|
+
readonly writable: WritableStream<Uint8Array>;
|
|
8
|
+
readonly closed: Promise<void>;
|
|
9
|
+
close(): Promise<void>;
|
|
10
|
+
startTls(options: {
|
|
11
|
+
servername?: string;
|
|
12
|
+
}): RawSocket;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* A `cloudflare:sockets` socket as an event emitter: `connect` once it can
|
|
16
|
+
* write, `data` for each chunk read, `error` then `close` when it fails.
|
|
17
|
+
* With TLS to follow, it reads one chunk alone, the server's answer to the
|
|
18
|
+
* SSLRequest, so that the reader is free for `startTls`.
|
|
19
|
+
*/
|
|
20
|
+
export declare class WorkerdSocket extends EventEmitter {
|
|
21
|
+
readyState: string;
|
|
22
|
+
raw: RawSocket | null;
|
|
23
|
+
/** Destroyed before `connect` made its socket: closed as soon as it has. */
|
|
24
|
+
destroyed: boolean;
|
|
25
|
+
writer: WritableStreamDefaultWriter<Uint8Array> | null;
|
|
26
|
+
reader: ReadableStreamDefaultReader<Uint8Array> | null;
|
|
27
|
+
connect(port: number, host: string, tls: boolean): Promise<void>;
|
|
28
|
+
/** Upgraded to TLS, the same emitter: `secureConnect` once the plain socket under it closes. */
|
|
29
|
+
upgrade(servername: string | undefined): this;
|
|
30
|
+
write(data: Uint8Array, callback?: () => void): boolean;
|
|
31
|
+
end(data?: Uint8Array): unknown;
|
|
32
|
+
/**
|
|
33
|
+
* Closed at once, as Node's `destroy`, whatever it is still writing or opening: its writes aborted and its reads
|
|
34
|
+
* cancelled, so that nothing buffered is sent, then the raw socket closed, which on its own waits until the socket
|
|
35
|
+
* has opened and every write has gone (workerd's `Socket::close`). Upstream ended it, which waits for a write stuck
|
|
36
|
+
* under backpressure too (reviews r2 and r3 of #438). Its TCP connection workerd closes with the request, whose I/O
|
|
37
|
+
* it is: locally, not before (packages/vault/test/workers-socket.test.ts). Before `connect` has made the raw socket,
|
|
38
|
+
* it is made only to be closed. Says `close` once, a task later, as Node's does.
|
|
39
|
+
*/
|
|
40
|
+
destroy(): void;
|
|
41
|
+
/** The raw socket's streams torn down, then the socket closed (`destroy`). */
|
|
42
|
+
abort(): void;
|
|
43
|
+
read(): Promise<void>;
|
|
44
|
+
/** The server's answer to the SSLRequest; failed, or the socket closed or destroyed first, said as any read's end. */
|
|
45
|
+
readFirst(): Promise<void>;
|
|
46
|
+
close(): void;
|
|
47
|
+
/** Said once: past `close` (a `destroy`'s, whose aborted streams fail what was under way), nothing more. */
|
|
48
|
+
error(error: unknown): void;
|
|
49
|
+
}
|
|
50
|
+
export declare const tcp: Platform['tcp'];
|
|
51
|
+
export declare const startTls: Platform['startTls'];
|
|
52
|
+
export declare const isIP: Platform['isIP'];
|
|
53
|
+
export declare const setImmediate: Platform['setImmediate'];
|
|
54
|
+
export declare const clearImmediate: Platform['clearImmediate'];
|
|
55
|
+
export declare const now: Platform['now'];
|
|
56
|
+
export declare const randomBytes: Platform['randomBytes'];
|
|
57
|
+
export declare const pbkdf2: Platform['pbkdf2'];
|
|
58
|
+
export declare const hmac: Platform['hmac'];
|
|
59
|
+
export declare const sha256: Platform['sha256'];
|
|
60
|
+
export declare const md5: Platform['md5'];
|
|
61
|
+
//#endregion
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { Buffer } from "node:buffer";
|
|
2
|
+
import { EventEmitter } from "node:events";
|
|
3
|
+
//#region src/postgres-js/platform.workerd.ts
|
|
4
|
+
const v4Seg = "(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])";
|
|
5
|
+
const v4Str = `(${v4Seg}[.]){3}${v4Seg}`;
|
|
6
|
+
const IPv4Reg = new RegExp(`^${v4Str}$`);
|
|
7
|
+
const v6Seg = "(?:[0-9a-fA-F]{1,4})";
|
|
8
|
+
const IPv6Reg = new RegExp(`^((?:${v6Seg}:){7}(?:${v6Seg}|:)|(?:${v6Seg}:){6}(?:${v4Str}|:${v6Seg}|:)|(?:${v6Seg}:){5}(?::${v4Str}|(:${v6Seg}){1,2}|:)|(?:${v6Seg}:){4}(?:(:${v6Seg}){0,1}:${v4Str}|(:${v6Seg}){1,3}|:)|(?:${v6Seg}:){3}(?:(:${v6Seg}){0,2}:${v4Str}|(:${v6Seg}){1,4}|:)|(?:${v6Seg}:){2}(?:(:${v6Seg}){0,3}:${v4Str}|(:${v6Seg}){1,5}|:)|(?:${v6Seg}:){1}(?:(:${v6Seg}){0,4}:${v4Str}|(:${v6Seg}){1,6}|:)|(?::((?::${v6Seg}){0,5}:${v4Str}|(?::${v6Seg}){1,7}|:)))(%[0-9a-zA-Z-.:]{1,})?\$`);
|
|
9
|
+
/**
|
|
10
|
+
* A `cloudflare:sockets` socket as an event emitter: `connect` once it can
|
|
11
|
+
* write, `data` for each chunk read, `error` then `close` when it fails.
|
|
12
|
+
* With TLS to follow, it reads one chunk alone, the server's answer to the
|
|
13
|
+
* SSLRequest, so that the reader is free for `startTls`.
|
|
14
|
+
*/
|
|
15
|
+
var WorkerdSocket = class extends EventEmitter {
|
|
16
|
+
readyState = "open";
|
|
17
|
+
raw = null;
|
|
18
|
+
/** Destroyed before `connect` made its socket: closed as soon as it has. */
|
|
19
|
+
destroyed = false;
|
|
20
|
+
writer = null;
|
|
21
|
+
reader = null;
|
|
22
|
+
async connect(port, host, tls) {
|
|
23
|
+
try {
|
|
24
|
+
this.readyState = "opening";
|
|
25
|
+
const { connect } = await import("cloudflare:sockets");
|
|
26
|
+
const raw = this.raw = connect(host + ":" + port, tls ? { secureTransport: "starttls" } : {});
|
|
27
|
+
if (this.destroyed) return this.abort();
|
|
28
|
+
raw.closed.then(() => {
|
|
29
|
+
if (this.readyState !== "upgrade") return this.close();
|
|
30
|
+
this.readyState = "open";
|
|
31
|
+
this.emit("secureConnect");
|
|
32
|
+
}, (error) => this.error(error));
|
|
33
|
+
this.writer = raw.writable.getWriter();
|
|
34
|
+
this.reader = raw.readable.getReader();
|
|
35
|
+
tls ? this.readFirst() : this.read();
|
|
36
|
+
this.writer.ready.then(() => {
|
|
37
|
+
this.readyState = "open";
|
|
38
|
+
this.emit("connect");
|
|
39
|
+
});
|
|
40
|
+
} catch (error) {
|
|
41
|
+
this.error(error);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/** Upgraded to TLS, the same emitter: `secureConnect` once the plain socket under it closes. */
|
|
45
|
+
upgrade(servername) {
|
|
46
|
+
this.writer.releaseLock();
|
|
47
|
+
this.reader.releaseLock();
|
|
48
|
+
this.readyState = "upgrading";
|
|
49
|
+
const raw = this.raw = this.raw.startTls({ servername });
|
|
50
|
+
raw.closed.then(() => this.close(), (error) => this.error(error));
|
|
51
|
+
this.writer = raw.writable.getWriter();
|
|
52
|
+
this.reader = raw.readable.getReader();
|
|
53
|
+
this.writer.ready.then(() => {
|
|
54
|
+
this.read();
|
|
55
|
+
this.readyState = "upgrade";
|
|
56
|
+
});
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
write(data, callback) {
|
|
60
|
+
this.writer.write(data).then(callback, (error) => this.error(error));
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
end(data) {
|
|
64
|
+
return data ? this.write(data, () => void this.raw.close()) : this.raw.close();
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Closed at once, as Node's `destroy`, whatever it is still writing or opening: its writes aborted and its reads
|
|
68
|
+
* cancelled, so that nothing buffered is sent, then the raw socket closed, which on its own waits until the socket
|
|
69
|
+
* has opened and every write has gone (workerd's `Socket::close`). Upstream ended it, which waits for a write stuck
|
|
70
|
+
* under backpressure too (reviews r2 and r3 of #438). Its TCP connection workerd closes with the request, whose I/O
|
|
71
|
+
* it is: locally, not before (packages/vault/test/workers-socket.test.ts). Before `connect` has made the raw socket,
|
|
72
|
+
* it is made only to be closed. Says `close` once, a task later, as Node's does.
|
|
73
|
+
*/
|
|
74
|
+
destroy() {
|
|
75
|
+
if (this.destroyed) return;
|
|
76
|
+
this.destroyed = true;
|
|
77
|
+
if (this.raw !== null) this.abort();
|
|
78
|
+
setTimeout(() => this.close(), 0);
|
|
79
|
+
}
|
|
80
|
+
/** The raw socket's streams torn down, then the socket closed (`destroy`). */
|
|
81
|
+
abort() {
|
|
82
|
+
const raw = this.raw;
|
|
83
|
+
(this.writer ?? raw.writable).abort().catch(() => {});
|
|
84
|
+
(this.reader ?? raw.readable).cancel().catch(() => {});
|
|
85
|
+
raw.close().catch(() => {});
|
|
86
|
+
}
|
|
87
|
+
async read() {
|
|
88
|
+
try {
|
|
89
|
+
for (;;) {
|
|
90
|
+
const { done, value } = await this.reader.read();
|
|
91
|
+
if (done) return;
|
|
92
|
+
this.emit("data", Buffer.from(value));
|
|
93
|
+
}
|
|
94
|
+
} catch (error) {
|
|
95
|
+
this.error(error);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/** The server's answer to the SSLRequest; failed, or the socket closed or destroyed first, said as any read's end. */
|
|
99
|
+
async readFirst() {
|
|
100
|
+
try {
|
|
101
|
+
const { done, value } = await this.reader.read();
|
|
102
|
+
if (done) return this.close();
|
|
103
|
+
this.emit("data", Buffer.from(value));
|
|
104
|
+
} catch (error) {
|
|
105
|
+
this.error(error);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
close() {
|
|
109
|
+
if (this.readyState === "closed") return;
|
|
110
|
+
this.readyState = "closed";
|
|
111
|
+
this.emit("close");
|
|
112
|
+
}
|
|
113
|
+
/** Said once: past `close` (a `destroy`'s, whose aborted streams fail what was under way), nothing more. */
|
|
114
|
+
error(error) {
|
|
115
|
+
if (this.readyState === "closed" || this.destroyed) return;
|
|
116
|
+
this.emit("error", error);
|
|
117
|
+
this.close();
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
const tcp = (to, connected) => {
|
|
121
|
+
if ("path" in to) throw new Error("a Unix socket cannot be reached from workerd");
|
|
122
|
+
const socket = new WorkerdSocket();
|
|
123
|
+
socket.once("connect", connected);
|
|
124
|
+
socket.connect(to.port, to.host, to.tls);
|
|
125
|
+
return socket;
|
|
126
|
+
};
|
|
127
|
+
const startTls = (socket, { servername }) => socket.upgrade(servername);
|
|
128
|
+
const isIP = (host) => IPv4Reg.test(host) ? 4 : IPv6Reg.test(host) ? 6 : 0;
|
|
129
|
+
let ids = 1;
|
|
130
|
+
const tasks = /* @__PURE__ */ new Set();
|
|
131
|
+
const setImmediate = (fn) => {
|
|
132
|
+
const id = ids++;
|
|
133
|
+
tasks.add(id);
|
|
134
|
+
queueMicrotask(() => {
|
|
135
|
+
if (tasks.has(id)) {
|
|
136
|
+
fn();
|
|
137
|
+
tasks.delete(id);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
return id;
|
|
141
|
+
};
|
|
142
|
+
const clearImmediate = (handle) => {
|
|
143
|
+
tasks.delete(handle);
|
|
144
|
+
};
|
|
145
|
+
const now = () => performance.now();
|
|
146
|
+
const randomBytes = (length) => crypto.getRandomValues(Buffer.alloc(length));
|
|
147
|
+
const encoder = new TextEncoder();
|
|
148
|
+
/** `x`, as WebCrypto types the bytes it takes: a Buffer's memory is never shared. */
|
|
149
|
+
const own = (x) => x;
|
|
150
|
+
const pbkdf2 = async (password, salt, iterations, length) => Buffer.from(await crypto.subtle.deriveBits({
|
|
151
|
+
name: "PBKDF2",
|
|
152
|
+
hash: "SHA-256",
|
|
153
|
+
salt: own(salt),
|
|
154
|
+
iterations
|
|
155
|
+
}, await crypto.subtle.importKey("raw", encoder.encode(password), "PBKDF2", false, ["deriveBits"]), length * 8));
|
|
156
|
+
const hmac = async (key, text) => Buffer.from(await crypto.subtle.sign("HMAC", await crypto.subtle.importKey("raw", own(key), {
|
|
157
|
+
name: "HMAC",
|
|
158
|
+
hash: "SHA-256"
|
|
159
|
+
}, false, ["sign"]), encoder.encode(text)));
|
|
160
|
+
const sha256 = async (data) => Buffer.from(await crypto.subtle.digest("SHA-256", own(data)));
|
|
161
|
+
const md5 = async (data) => Buffer.from(await crypto.subtle.digest("MD5", typeof data === "string" ? encoder.encode(data) : own(data))).toString("hex");
|
|
162
|
+
//#endregion
|
|
163
|
+
export { WorkerdSocket, clearImmediate, hmac, isIP, md5, now, pbkdf2, randomBytes, setImmediate, sha256, startTls, tcp };
|