@hallmaster/docker.js 0.0.1

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.
@@ -0,0 +1,21 @@
1
+ import type { DockerVersion } from "./types/DockerVersion";
2
+ import type { DockerInfo } from "./types/DockerInfo";
3
+ export default class DockerSocket {
4
+ private readonly apiBaseURL;
5
+ private dockerSocketPath;
6
+ private dockerVersion;
7
+ private dockerAuthToken;
8
+ constructor(unixSocketPath?: string, apiBaseURL?: string);
9
+ isReady(): Promise<boolean>;
10
+ private request;
11
+ init(): Promise<void>;
12
+ get version(): DockerVersion;
13
+ get token(): string;
14
+ apiCall<T>(method: Request["method"], urlPath: string, options?: {
15
+ headers?: Record<string, string>;
16
+ body?: string | Buffer | DataView;
17
+ }): Promise<T>;
18
+ authenticate(serverAddress: string, username: string, password: string): Promise<void>;
19
+ info(): Promise<DockerInfo>;
20
+ }
21
+ //# sourceMappingURL=DockerSocket.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DockerSocket.d.ts","sourceRoot":"","sources":["../src/DockerSocket.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAQrD,MAAM,CAAC,OAAO,OAAO,YAAY;IAS7B,OAAO,CAAC,QAAQ,CAAC,UAAU;IAR7B,OAAO,CAAC,gBAAgB,CAAS;IAEjC,OAAO,CAAC,aAAa,CAA8B;IAEnD,OAAO,CAAC,eAAe,CAAuB;gBAG5C,cAAc,GAAE,MAA+B,EAC9B,UAAU,GAAE,MAA2B;IAKpD,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC;YAiBnB,OAAO;IA+Df,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAa3B,IAAI,OAAO,kBAKV;IAED,IAAI,KAAK,WAKR;IAEK,OAAO,CAAC,CAAC,EACb,MAAM,EAAE,OAAO,CAAC,QAAQ,CAAC,EACzB,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE;QACR,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QACjC,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,QAAQ,CAAC;KACnC,GACA,OAAO,CAAC,CAAC,CAAC;IAYP,YAAY,CAChB,aAAa,EAAE,MAAM,EACrB,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,IAAI,CAAC;IAoBV,IAAI,IAAI,OAAO,CAAC,UAAU,CAAC;CAGlC"}
@@ -0,0 +1,107 @@
1
+ import http from "node:http";
2
+ import fs from "node:fs/promises";
3
+ import path from "node:path/posix";
4
+ export default class DockerSocket {
5
+ apiBaseURL;
6
+ dockerSocketPath;
7
+ dockerVersion = null;
8
+ dockerAuthToken = null;
9
+ constructor(unixSocketPath = "/var/run/docker.sock", apiBaseURL = "http://localhost") {
10
+ this.apiBaseURL = apiBaseURL;
11
+ this.dockerSocketPath = unixSocketPath;
12
+ }
13
+ async isReady() {
14
+ let stat;
15
+ try {
16
+ stat = await fs.lstat(this.dockerSocketPath);
17
+ }
18
+ catch {
19
+ return false;
20
+ }
21
+ if (stat.isSymbolicLink()) {
22
+ this.dockerSocketPath = await fs.readlink(this.dockerSocketPath);
23
+ return await this.isReady();
24
+ }
25
+ return stat.isSocket();
26
+ }
27
+ async request(method, path, options) {
28
+ const url = new URL(path, this.apiBaseURL);
29
+ const requestOptions = {
30
+ hostname: url.hostname,
31
+ host: url.host,
32
+ protocol: url.protocol,
33
+ port: url.port,
34
+ path: url.pathname,
35
+ headers: options?.headers,
36
+ method: method,
37
+ socketPath: this.dockerSocketPath,
38
+ };
39
+ return new Promise((resolve, reject) => {
40
+ const request = http.request(requestOptions, (response) => {
41
+ if (null !== response.errored) {
42
+ reject(response.errored);
43
+ return;
44
+ }
45
+ response.once("error", reject);
46
+ let bodyChunks = [];
47
+ response.on("data", (data) => bodyChunks.push(data));
48
+ response.once("end", () => resolve({ response, body: Buffer.concat(bodyChunks) }));
49
+ });
50
+ request.once("error", reject);
51
+ if (undefined !== options?.body) {
52
+ const body = options.body instanceof Buffer
53
+ ? options.body
54
+ : Buffer.from(options.body.toString("utf-8"));
55
+ const contentLength = options?.headers?.["Content-Length"] ??
56
+ options?.headers?.["Transfer-Encoding"] ??
57
+ null;
58
+ if (null === contentLength) {
59
+ request.setHeader("Content-Length", Buffer.byteLength(body));
60
+ }
61
+ request.write(body);
62
+ }
63
+ request.end();
64
+ });
65
+ }
66
+ async init() {
67
+ const isSocketAvailable = await this.isReady();
68
+ if (!isSocketAvailable) {
69
+ throw new Error(`Cannot connect to the Docker daemon at unix://${this.dockerSocketPath}. Is the docker daemon running?`);
70
+ }
71
+ const response = await this.request("GET", "/version");
72
+ this.dockerVersion = JSON.parse(response.body.toString("utf-8"));
73
+ }
74
+ get version() {
75
+ if (null === this.dockerVersion) {
76
+ throw new Error("DockerSocket: Wrapper uninitialized");
77
+ }
78
+ return this.dockerVersion;
79
+ }
80
+ get token() {
81
+ if (null === this.dockerAuthToken) {
82
+ throw new Error("DockerSocket: Unauthorized");
83
+ }
84
+ return this.dockerAuthToken;
85
+ }
86
+ async apiCall(method, urlPath, options) {
87
+ const { ApiVersion } = this.version;
88
+ const response = await this.request(method, path.join("/", `v${ApiVersion}`, urlPath), options);
89
+ return JSON.parse(response.body.toString("utf-8"));
90
+ }
91
+ async authenticate(serverAddress, username, password) {
92
+ const tokenPayload = JSON.stringify({ serverAddress, username, password });
93
+ const response = await this.apiCall("POST", "/auth", {
94
+ headers: {
95
+ "Content-Type": "application/json",
96
+ },
97
+ body: tokenPayload,
98
+ });
99
+ if (response.Status !== "Login Succeeded") {
100
+ throw new Error("DockerSocket: Invalid credentials");
101
+ }
102
+ this.dockerAuthToken = Buffer.from(tokenPayload, "utf-8").toString("base64");
103
+ }
104
+ async info() {
105
+ return await this.apiCall("GET", "/info");
106
+ }
107
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":""}
package/dist/index.js ADDED
@@ -0,0 +1,12 @@
1
+ import DockerSocket from "./DockerSocket";
2
+ (async function () {
3
+ const socket = new DockerSocket();
4
+ await socket.init();
5
+ console.log(await socket.info());
6
+ // await socket.authenticate(
7
+ // "ghcr.io",
8
+ // "github_username",
9
+ // "github_pat",
10
+ // );
11
+ // console.log(socket.token);
12
+ })();
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@hallmaster/docker.js",
3
+ "description": "A TypeScript Docker API wrapper mainly used for the Hallmaster project.",
4
+ "version": "0.0.1",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "private": false,
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.js",
11
+ "types": "./dist/index.d.ts"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "scripts": {
18
+ "clean": "rm -rf dist",
19
+ "build": "tsc",
20
+ "prepublishOnly": "pnpm run build",
21
+ "publish": "npm publish --access=public"
22
+ },
23
+ "devDependencies": {
24
+ "@types/node": "^25.0.9",
25
+ "typescript": "^5.9.3"
26
+ }
27
+ }