mindmount/dist/mount.service.js
2026-09-11 23:30:20 -04:00

106 lines
No EOL
4.6 KiB
JavaScript

"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var MountService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MountService = void 0;
const common_1 = require("@nestjs/common");
let MountService = MountService_1 = class MountService {
logger = new common_1.Logger(MountService_1.name);
gapmindToken;
gapmindBase() {
return (process.env.GAPMIND_URL || 'http://127.0.0.1:3002').replace(/\/$/, '');
}
socmindBase() {
return (process.env.SOCMIND_URL || 'http://127.0.0.1:3001').replace(/\/$/, '');
}
requireMountAuth(authorization) {
const expected = process.env.MOUNT_API_TOKEN;
if (!expected)
return;
if (authorization !== `Bearer ${expected}`) {
const err = new Error('Invalid mount credential.');
err.status = 401;
throw err;
}
}
serviceToken() {
return (this.gapmindToken ||
process.env.GAPMIND_TOKEN ||
undefined);
}
async onModuleInit() {
if (process.env.GAPMIND_TOKEN) {
this.logger.log('Using configured GAPMIND_TOKEN, skipping auto-signup.');
return;
}
const email = process.env.GAPMIND_EMAIL || 'mindmount@local';
const password = process.env.GAPMIND_PASSWORD || 'mindmount-local';
try {
const res = await fetch(`${this.gapmindBase()}/api/accounts/signup`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email, password }),
});
if (res.status === 400) {
this.logger.log('gapmind account already exists; /ips/* will rely on caller tokens (or set GAPMIND_TOKEN).');
return;
}
if (!res.ok) {
this.logger.warn(`gapmind auto-signup failed (${res.status}); /ips/* will rely on caller tokens.`);
return;
}
const data = (await res.json());
this.gapmindToken = data.token || data.apiKey || data.apiToken;
if (this.gapmindToken)
this.logger.log('gapmind instance account provisioned.');
else
this.logger.warn('gapmind signup returned no token; relying on caller tokens.');
}
catch (e) {
this.logger.warn(`gapmind unreachable at startup (${e instanceof Error ? e.message : e}); /ips/* will rely on caller tokens.`);
}
}
async proxy(base, subpath, method, query, inHeaders, body, injectToken) {
const url = `${base}${subpath}${query ? `?${query}` : ''}`;
const headers = {};
for (const [k, v] of Object.entries(inHeaders)) {
const key = k.toLowerCase();
if (['host', 'connection', 'content-length', 'transfer-encoding'].includes(key))
continue;
if (typeof v === 'string')
headers[k] = v;
else if (Array.isArray(v))
headers[k] = v.join(', ');
}
if (injectToken && !headers['authorization'] && !headers['Authorization']) {
const token = this.serviceToken();
if (token)
headers['Authorization'] = `Bearer ${token}`;
}
const hasBody = body !== undefined && body !== null && body !== '' && method !== 'GET' && method !== 'HEAD';
const res = await fetch(url, {
method,
headers: {
...headers,
...(hasBody ? { 'Content-Type': 'application/json' } : {}),
},
body: hasBody ? (typeof body === 'string' ? body : JSON.stringify(body)) : undefined,
});
const buffer = Buffer.from(await res.arrayBuffer());
return {
status: res.status,
contentType: res.headers.get('content-type') || undefined,
buffer,
};
}
};
exports.MountService = MountService;
exports.MountService = MountService = MountService_1 = __decorate([
(0, common_1.Injectable)()
], MountService);
//# sourceMappingURL=mount.service.js.map