@promigence/sdk 0.1.0-beta.12
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/NOTICE +14 -0
- package/README.md +54 -0
- package/dist/blake3.d.ts +10 -0
- package/dist/blake3.js +111 -0
- package/dist/bundle.d.ts +96 -0
- package/dist/bundle.js +227 -0
- package/dist/ca-guard.d.ts +1 -0
- package/dist/ca-guard.js +166 -0
- package/dist/caller.d.ts +73 -0
- package/dist/caller.js +47 -0
- package/dist/cjs/blake3.d.ts +10 -0
- package/dist/cjs/blake3.js +116 -0
- package/dist/cjs/bundle.d.ts +96 -0
- package/dist/cjs/bundle.js +242 -0
- package/dist/cjs/ca-guard.d.ts +1 -0
- package/dist/cjs/ca-guard.js +174 -0
- package/dist/cjs/caller.d.ts +73 -0
- package/dist/cjs/caller.js +57 -0
- package/dist/cjs/client.d.ts +3015 -0
- package/dist/cjs/client.js +2617 -0
- package/dist/cjs/command-errors.d.ts +41 -0
- package/dist/cjs/command-errors.js +43 -0
- package/dist/cjs/context.d.ts +119 -0
- package/dist/cjs/context.js +598 -0
- package/dist/cjs/core.d.ts +285 -0
- package/dist/cjs/core.js +290 -0
- package/dist/cjs/credstore.d.ts +90 -0
- package/dist/cjs/credstore.js +219 -0
- package/dist/cjs/dispatcher.d.ts +58 -0
- package/dist/cjs/dispatcher.js +91 -0
- package/dist/cjs/github.d.ts +158 -0
- package/dist/cjs/github.js +84 -0
- package/dist/cjs/gitlab.d.ts +104 -0
- package/dist/cjs/gitlab.js +24 -0
- package/dist/cjs/handles.d.ts +82 -0
- package/dist/cjs/handles.js +139 -0
- package/dist/cjs/hint.d.ts +1 -0
- package/dist/cjs/hint.js +10 -0
- package/dist/cjs/http2.d.ts +206 -0
- package/dist/cjs/http2.js +1932 -0
- package/dist/cjs/index.d.ts +34 -0
- package/dist/cjs/index.js +31 -0
- package/dist/cjs/keyed.gen.d.ts +1 -0
- package/dist/cjs/keyed.gen.js +10 -0
- package/dist/cjs/models.gen.d.ts +6162 -0
- package/dist/cjs/models.gen.js +2 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/proxy.d.ts +40 -0
- package/dist/cjs/proxy.js +257 -0
- package/dist/cjs/pty.d.ts +171 -0
- package/dist/cjs/pty.js +504 -0
- package/dist/cjs/sandbox.d.ts +537 -0
- package/dist/cjs/sandbox.js +736 -0
- package/dist/cjs/ssh.d.ts +25 -0
- package/dist/cjs/ssh.js +117 -0
- package/dist/cjs/trace.d.ts +33 -0
- package/dist/cjs/trace.js +88 -0
- package/dist/cjs/transport-worker.d.ts +1 -0
- package/dist/cjs/transport-worker.js +468 -0
- package/dist/cjs/warm.d.ts +1 -0
- package/dist/cjs/warm.js +43 -0
- package/dist/client.d.ts +3015 -0
- package/dist/client.js +2583 -0
- package/dist/command-errors.d.ts +41 -0
- package/dist/command-errors.js +36 -0
- package/dist/context.d.ts +119 -0
- package/dist/context.js +588 -0
- package/dist/core.d.ts +285 -0
- package/dist/core.js +266 -0
- package/dist/credstore.d.ts +90 -0
- package/dist/credstore.js +206 -0
- package/dist/dispatcher.d.ts +58 -0
- package/dist/dispatcher.js +86 -0
- package/dist/github.d.ts +158 -0
- package/dist/github.js +78 -0
- package/dist/gitlab.d.ts +104 -0
- package/dist/gitlab.js +20 -0
- package/dist/handles.d.ts +82 -0
- package/dist/handles.js +135 -0
- package/dist/hint.d.ts +1 -0
- package/dist/hint.js +7 -0
- package/dist/http2.d.ts +206 -0
- package/dist/http2.js +1921 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +15 -0
- package/dist/keyed.gen.d.ts +1 -0
- package/dist/keyed.gen.js +7 -0
- package/dist/models.gen.d.ts +6162 -0
- package/dist/models.gen.js +1 -0
- package/dist/proxy.d.ts +40 -0
- package/dist/proxy.js +249 -0
- package/dist/pty.d.ts +171 -0
- package/dist/pty.js +496 -0
- package/dist/sandbox.d.ts +537 -0
- package/dist/sandbox.js +725 -0
- package/dist/ssh.d.ts +25 -0
- package/dist/ssh.js +114 -0
- package/dist/trace.d.ts +33 -0
- package/dist/trace.js +80 -0
- package/dist/transport-worker.d.ts +1 -0
- package/dist/transport-worker.js +466 -0
- package/dist/warm.d.ts +1 -0
- package/dist/warm.js +40 -0
- package/package.json +54 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export interface SshTunnelOpts {
|
|
2
|
+
/** `https://api…/v1/sandboxes/:id/ssh/connect` */
|
|
3
|
+
url: string;
|
|
4
|
+
/** A `prom_ssh_` access credential, or an API key. */
|
|
5
|
+
token: string;
|
|
6
|
+
userAgent: string;
|
|
7
|
+
caller?: string;
|
|
8
|
+
timeoutMs?: number;
|
|
9
|
+
/** Bytes from the sandbox's SSH server. */
|
|
10
|
+
onData: (data: Uint8Array) => void;
|
|
11
|
+
/** The tunnel closed. */
|
|
12
|
+
onClose: () => void;
|
|
13
|
+
/** Build the error to reject with when the server does not switch protocols. */
|
|
14
|
+
refuse: (status: number, text: string) => Error;
|
|
15
|
+
}
|
|
16
|
+
export interface SshTunnel {
|
|
17
|
+
/** Send bytes to the sandbox's SSH server. */
|
|
18
|
+
write(data: Uint8Array): void;
|
|
19
|
+
/** Close the tunnel. */
|
|
20
|
+
close(): void;
|
|
21
|
+
/** Resolves when the tunnel is finished. */
|
|
22
|
+
done: Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
/** Open the tunnel. Resolves once the socket has switched protocols. */
|
|
25
|
+
export declare function connectSshTunnel(opts: SshTunnelOpts): Promise<SshTunnel>;
|
package/dist/cjs/ssh.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.connectSshTunnel = connectSshTunnel;
|
|
4
|
+
const node_crypto_1 = require("node:crypto");
|
|
5
|
+
const caller_js_1 = require("./caller.js");
|
|
6
|
+
const proxy_js_1 = require("./proxy.js");
|
|
7
|
+
const pty_js_1 = require("./pty.js");
|
|
8
|
+
function connectSshTunnel(opts) {
|
|
9
|
+
const url = new URL(opts.url);
|
|
10
|
+
const key = (0, node_crypto_1.randomBytes)(16).toString("base64");
|
|
11
|
+
const req = (0, proxy_js_1.requestVia)(url, {
|
|
12
|
+
method: "GET",
|
|
13
|
+
agent: false,
|
|
14
|
+
headers: {
|
|
15
|
+
connection: "Upgrade",
|
|
16
|
+
upgrade: "websocket",
|
|
17
|
+
"sec-websocket-version": "13",
|
|
18
|
+
"sec-websocket-key": key,
|
|
19
|
+
authorization: `Bearer ${opts.token}`,
|
|
20
|
+
"user-agent": opts.userAgent,
|
|
21
|
+
...(opts.caller ? { [caller_js_1.CALLER_HEADER]: opts.caller } : {}),
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
return new Promise((resolve, reject) => {
|
|
25
|
+
let settled = false;
|
|
26
|
+
const fail = (e) => {
|
|
27
|
+
if (!settled) {
|
|
28
|
+
settled = true;
|
|
29
|
+
reject(e);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
const timer = setTimeout(() => {
|
|
33
|
+
req.destroy();
|
|
34
|
+
fail(opts.refuse(504, JSON.stringify({
|
|
35
|
+
error: {
|
|
36
|
+
code: "timeout",
|
|
37
|
+
message: `the SSH tunnel to ${url.host} did not open within ${opts.timeoutMs ?? 30_000} ms`,
|
|
38
|
+
fix: "Retry; if it repeats, check the sandbox is ready.",
|
|
39
|
+
},
|
|
40
|
+
})));
|
|
41
|
+
}, opts.timeoutMs ?? 30_000);
|
|
42
|
+
req.on("error", (e) => {
|
|
43
|
+
clearTimeout(timer);
|
|
44
|
+
fail(opts.refuse(0, JSON.stringify({
|
|
45
|
+
error: {
|
|
46
|
+
code: "unreachable",
|
|
47
|
+
message: `cannot reach ${url.host}: ${e.code ?? e.message}`,
|
|
48
|
+
},
|
|
49
|
+
})));
|
|
50
|
+
});
|
|
51
|
+
req.on("response", (res) => {
|
|
52
|
+
clearTimeout(timer);
|
|
53
|
+
const parts = [];
|
|
54
|
+
res.on("data", (d) => parts.push(d));
|
|
55
|
+
res.on("end", () => fail(opts.refuse(res.statusCode ?? 0, Buffer.concat(parts).toString("utf8"))));
|
|
56
|
+
});
|
|
57
|
+
req.on("upgrade", (res, socket, head) => {
|
|
58
|
+
clearTimeout(timer);
|
|
59
|
+
if (res.headers["sec-websocket-accept"] !== (0, pty_js_1.wsAccept)(key)) {
|
|
60
|
+
socket.destroy();
|
|
61
|
+
fail(opts.refuse(502, JSON.stringify({
|
|
62
|
+
error: {
|
|
63
|
+
code: "internal",
|
|
64
|
+
message: "the tunnel answered the WebSocket handshake with the wrong accept key",
|
|
65
|
+
},
|
|
66
|
+
})));
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
socket.setNoDelay(true);
|
|
70
|
+
const reader = new pty_js_1.FrameReader();
|
|
71
|
+
let resolveDone;
|
|
72
|
+
const done = new Promise((r) => {
|
|
73
|
+
resolveDone = r;
|
|
74
|
+
});
|
|
75
|
+
let finished = false;
|
|
76
|
+
const finish = () => {
|
|
77
|
+
if (finished)
|
|
78
|
+
return;
|
|
79
|
+
finished = true;
|
|
80
|
+
opts.onClose();
|
|
81
|
+
resolveDone();
|
|
82
|
+
};
|
|
83
|
+
const onFrames = (chunk) => {
|
|
84
|
+
for (const f of reader.push(chunk)) {
|
|
85
|
+
if (f.opcode === 0x2 || f.opcode === 0x1) {
|
|
86
|
+
opts.onData(new Uint8Array(f.payload));
|
|
87
|
+
}
|
|
88
|
+
else if (f.opcode === 0x9) {
|
|
89
|
+
socket.write((0, pty_js_1.encodeFrame)(0xa, f.payload));
|
|
90
|
+
}
|
|
91
|
+
else if (f.opcode === 0x8) {
|
|
92
|
+
if (!socket.destroyed)
|
|
93
|
+
socket.end((0, pty_js_1.encodeFrame)(0x8, f.payload.subarray(0, 2)));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
socket.on("data", onFrames);
|
|
98
|
+
socket.on("close", finish);
|
|
99
|
+
socket.on("error", finish);
|
|
100
|
+
if (head.length)
|
|
101
|
+
onFrames(head);
|
|
102
|
+
settled = true;
|
|
103
|
+
resolve({
|
|
104
|
+
write(data) {
|
|
105
|
+
if (!socket.destroyed)
|
|
106
|
+
socket.write((0, pty_js_1.encodeFrame)(0x2, data));
|
|
107
|
+
},
|
|
108
|
+
close() {
|
|
109
|
+
if (!socket.destroyed)
|
|
110
|
+
socket.end((0, pty_js_1.encodeFrame)(0x8, Buffer.from([0x03, 0xe8])));
|
|
111
|
+
},
|
|
112
|
+
done,
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
req.end();
|
|
116
|
+
});
|
|
117
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-request phase stamps for measuring the SDK's own client-side cost.
|
|
3
|
+
*
|
|
4
|
+
* Off unless `PROMIGENCE_SDK_TRACE=1`, in which case every `request()` records `performance.now()`
|
|
5
|
+
* at each phase it passes through and the process prints one JSON line per request to stderr
|
|
6
|
+
* when it exits (buffered: writing in the hot path would be the skew being measured). Nothing
|
|
7
|
+
* here is part of the API.
|
|
8
|
+
*/
|
|
9
|
+
export interface TraceStamp {
|
|
10
|
+
seq: number;
|
|
11
|
+
method: string;
|
|
12
|
+
path: string;
|
|
13
|
+
/** `performance.now()` at each phase, by name. */
|
|
14
|
+
t: Record<string, number>;
|
|
15
|
+
/** How the request travelled: "h2" or "fetch". */
|
|
16
|
+
via?: string;
|
|
17
|
+
/** The server's wall stamps (unix ns) when the answer carried them (`x-promigence-rx-ns` / `-tx-ns`). */
|
|
18
|
+
rx?: string;
|
|
19
|
+
tx?: string;
|
|
20
|
+
status?: number;
|
|
21
|
+
/** Over the transport thread: which of its connections (0-based, in the order they came up). */
|
|
22
|
+
conn?: number;
|
|
23
|
+
}
|
|
24
|
+
export declare const TRACE_ON: boolean;
|
|
25
|
+
/** Run `fn` with tracing off. */
|
|
26
|
+
export declare function untraced<T>(fn: () => T): T;
|
|
27
|
+
/** Start a stamp for one request, or `undefined` when tracing is off (the common case: one branch). */
|
|
28
|
+
export declare function traceBegin(method: string, path: string): TraceStamp | undefined;
|
|
29
|
+
export declare function mark(s: TraceStamp | undefined, phase: string): void;
|
|
30
|
+
/** A one-off process event (a constructor, a handshake step), for the same output. */
|
|
31
|
+
export declare function traceEvent(name: string): void;
|
|
32
|
+
/** A process event that happened at `t` (`performance.now()` terms), recorded now: one another thread reported. */
|
|
33
|
+
export declare function traceEventAt(name: string, t: number): void;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TRACE_ON = void 0;
|
|
4
|
+
exports.untraced = untraced;
|
|
5
|
+
exports.traceBegin = traceBegin;
|
|
6
|
+
exports.mark = mark;
|
|
7
|
+
exports.traceEvent = traceEvent;
|
|
8
|
+
exports.traceEventAt = traceEventAt;
|
|
9
|
+
const node_fs_1 = require("node:fs");
|
|
10
|
+
const core_js_1 = require("./core.js");
|
|
11
|
+
exports.TRACE_ON = (() => {
|
|
12
|
+
try {
|
|
13
|
+
return typeof process === "object" && process.env?.["PROMIGENCE_SDK_TRACE"] === "1";
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
})();
|
|
19
|
+
const stamps = [];
|
|
20
|
+
let seq = 0;
|
|
21
|
+
let hooked = false;
|
|
22
|
+
let muted = 0;
|
|
23
|
+
function untraced(fn) {
|
|
24
|
+
muted++;
|
|
25
|
+
try {
|
|
26
|
+
return fn();
|
|
27
|
+
}
|
|
28
|
+
finally {
|
|
29
|
+
muted--;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function traceBegin(method, path) {
|
|
33
|
+
if (!exports.TRACE_ON || muted > 0)
|
|
34
|
+
return undefined;
|
|
35
|
+
const s = { seq: seq++, method, path, t: { call: performance.now() } };
|
|
36
|
+
stamps.push(s);
|
|
37
|
+
if (!hooked) {
|
|
38
|
+
hooked = true;
|
|
39
|
+
process.once("exit", flush);
|
|
40
|
+
}
|
|
41
|
+
return s;
|
|
42
|
+
}
|
|
43
|
+
function mark(s, phase) {
|
|
44
|
+
if (s !== undefined)
|
|
45
|
+
s.t[phase] = performance.now();
|
|
46
|
+
}
|
|
47
|
+
const events = [];
|
|
48
|
+
function traceEvent(name) {
|
|
49
|
+
if (!exports.TRACE_ON || muted > 0)
|
|
50
|
+
return;
|
|
51
|
+
events.push([name, performance.now()]);
|
|
52
|
+
if (!hooked) {
|
|
53
|
+
hooked = true;
|
|
54
|
+
process.once("exit", flush);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function traceEventAt(name, t) {
|
|
58
|
+
if (!exports.TRACE_ON || muted > 0)
|
|
59
|
+
return;
|
|
60
|
+
events.push([name, t]);
|
|
61
|
+
if (!hooked) {
|
|
62
|
+
hooked = true;
|
|
63
|
+
process.once("exit", flush);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function modulePath() {
|
|
67
|
+
const frame = (new Error().stack ?? "").split("\n")[1] ?? "";
|
|
68
|
+
const m = /\((.*):\d+:\d+\)\s*$/.exec(frame) ?? /at (.*):\d+:\d+\s*$/.exec(frame);
|
|
69
|
+
return m?.[1] ?? "";
|
|
70
|
+
}
|
|
71
|
+
function flush() {
|
|
72
|
+
const head = {
|
|
73
|
+
trace: "promigence-sdk",
|
|
74
|
+
version: core_js_1.VERSION,
|
|
75
|
+
node: process.versions.node,
|
|
76
|
+
timeOrigin: performance.timeOrigin,
|
|
77
|
+
module: modulePath(),
|
|
78
|
+
requests: stamps.length,
|
|
79
|
+
events,
|
|
80
|
+
};
|
|
81
|
+
let out = `PROMIGENCE_SDK_TRACE ${JSON.stringify(head)}\n`;
|
|
82
|
+
for (const s of stamps)
|
|
83
|
+
out += `PROMIGENCE_SDK_TRACE ${JSON.stringify(s)}\n`;
|
|
84
|
+
try {
|
|
85
|
+
(0, node_fs_1.writeSync)(2, out);
|
|
86
|
+
}
|
|
87
|
+
catch { }
|
|
88
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|