MCPcopy
hub / github.com/apocas/dockerode / withSession

Function withSession

lib/session.js:6–61  ·  view source on GitHub ↗
(docker, auth, handler)

Source from the content-addressed store, hash-verified

4 crypto = require("crypto");
5
6function withSession(docker, auth, handler) {
7 const sessionId = crypto.randomUUID();
8
9 const opts = {
10 method: "POST",
11 path: "/session",
12 hijack: true,
13 headers: {
14 Upgrade: "h2c",
15 "X-Docker-Expose-Session-Uuid": sessionId,
16 "X-Docker-Expose-Session-Name": "testcontainers",
17 },
18 statusCodes: {
19 200: true,
20 500: "server error",
21 },
22 };
23
24 docker.modem.dial(opts, function (err, socket) {
25 if (err) {
26 return handler(err, null, () => undefined);
27 }
28
29 const server = new grpc.Server();
30 const creds = grpc.ServerCredentials.createInsecure();
31 const injector = server.createConnectionInjector(creds);
32 injector.injectConnection(socket);
33
34 const pkg = protoLoader.loadSync(
35 path.resolve(__dirname, "proto", "auth.proto")
36 );
37 const service = grpc.loadPackageDefinition(pkg);
38
39 server.addService(service.moby.filesync.v1.Auth.service, {
40 Credentials({ request }, callback) {
41 // We probably want to have the possibility to pass credentials per
42 // hots. The correct one could be returned based on `request.Host`
43 if (auth) {
44 callback(null, {
45 Username: auth.username,
46 Secret: auth.password,
47 });
48 } else {
49 callback(null, {});
50 }
51 },
52 });
53
54 function done() {
55 server.forceShutdown();
56 socket.end();
57 }
58
59 handler(null, sessionId, done);
60 });
61}
62
63module.exports = withSession;

Callers 1

dialWithSessionFunction · 0.85

Calls 1

handlerFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…