@qelos/integrator-nest 4.0.1 → 4.1.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/dist/cookies.d.ts +19 -0
- package/dist/cookies.js +43 -0
- package/dist/cookies.js.map +1 -0
- package/dist/cookies.test.d.ts +1 -0
- package/dist/cookies.test.js +76 -0
- package/dist/cookies.test.js.map +1 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +8 -1
- package/dist/index.js.map +1 -1
- package/dist/middleware.js +71 -31
- package/dist/middleware.js.map +1 -1
- package/dist/module-options.d.ts +3 -0
- package/dist/module-options.js +21 -2
- package/dist/module-options.js.map +1 -1
- package/dist/module.d.ts +6 -3
- package/dist/module.js +38 -4
- package/dist/module.js.map +1 -1
- package/dist/proxy-target.d.ts +17 -0
- package/dist/proxy-target.js +29 -0
- package/dist/proxy-target.js.map +1 -0
- package/dist/proxy-target.test.d.ts +1 -0
- package/dist/proxy-target.test.js +86 -0
- package/dist/proxy-target.test.js.map +1 -0
- package/dist/proxy.middleware.d.ts +7 -0
- package/dist/proxy.middleware.js +197 -0
- package/dist/proxy.middleware.js.map +1 -0
- package/dist/request-utils.d.ts +2 -6
- package/dist/request-utils.js +1 -86
- package/dist/request-utils.js.map +1 -1
- package/dist/sdk-factory.d.ts +2 -10
- package/dist/sdk-factory.js +19 -77
- package/dist/sdk-factory.js.map +1 -1
- package/dist/types.d.ts +13 -41
- package/package.json +2 -2
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const node_test_1 = require("node:test");
|
|
7
|
+
const strict_1 = __importDefault(require("node:assert/strict"));
|
|
8
|
+
const proxy_target_1 = require("./proxy-target");
|
|
9
|
+
const PROXY_ENV_KEYS = [
|
|
10
|
+
'QELOS_PROXY_TARGET',
|
|
11
|
+
'QELOS_IP',
|
|
12
|
+
'QELOS_API_IP',
|
|
13
|
+
];
|
|
14
|
+
function clearProxyEnv() {
|
|
15
|
+
for (const key of PROXY_ENV_KEYS) {
|
|
16
|
+
delete process.env[key];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
(0, node_test_1.describe)('resolveQelosProxyTarget', () => {
|
|
20
|
+
const savedEnv = {};
|
|
21
|
+
(0, node_test_1.beforeEach)(() => {
|
|
22
|
+
for (const key of PROXY_ENV_KEYS) {
|
|
23
|
+
savedEnv[key] = process.env[key];
|
|
24
|
+
}
|
|
25
|
+
clearProxyEnv();
|
|
26
|
+
});
|
|
27
|
+
(0, node_test_1.afterEach)(() => {
|
|
28
|
+
for (const key of PROXY_ENV_KEYS) {
|
|
29
|
+
if (savedEnv[key] === undefined) {
|
|
30
|
+
delete process.env[key];
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
process.env[key] = savedEnv[key];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
(0, node_test_1.it)('uses QELOS_PROXY_TARGET when set, even if other env vars and appUrl exist', () => {
|
|
38
|
+
process.env.QELOS_PROXY_TARGET = 'https://from-proxy-env.example';
|
|
39
|
+
process.env.QELOS_IP = 'https://from-qelos-ip.example';
|
|
40
|
+
process.env.QELOS_API_IP = 'https://from-qelos-api-ip.example';
|
|
41
|
+
const config = {
|
|
42
|
+
appUrl: 'https://from-app-url.example',
|
|
43
|
+
};
|
|
44
|
+
strict_1.default.equal((0, proxy_target_1.resolveQelosProxyTarget)(config), 'https://from-proxy-env.example');
|
|
45
|
+
});
|
|
46
|
+
(0, node_test_1.it)('falls back to QELOS_IP when QELOS_PROXY_TARGET is unset', () => {
|
|
47
|
+
process.env.QELOS_IP = 'https://from-qelos-ip.example';
|
|
48
|
+
process.env.QELOS_API_IP = 'https://from-qelos-api-ip.example';
|
|
49
|
+
const config = {
|
|
50
|
+
appUrl: 'https://from-app-url.example',
|
|
51
|
+
};
|
|
52
|
+
strict_1.default.equal((0, proxy_target_1.resolveQelosProxyTarget)(config), 'https://from-qelos-ip.example');
|
|
53
|
+
});
|
|
54
|
+
(0, node_test_1.it)('falls back to QELOS_API_IP when higher-priority env vars are unset', () => {
|
|
55
|
+
process.env.QELOS_API_IP = 'https://from-qelos-api-ip.example';
|
|
56
|
+
const config = {
|
|
57
|
+
appUrl: 'https://from-app-url.example',
|
|
58
|
+
};
|
|
59
|
+
strict_1.default.equal((0, proxy_target_1.resolveQelosProxyTarget)(config), 'https://from-qelos-api-ip.example');
|
|
60
|
+
});
|
|
61
|
+
(0, node_test_1.it)('falls back to config.appUrl when no env var is set', () => {
|
|
62
|
+
const config = {
|
|
63
|
+
appUrl: 'https://from-app-url.example',
|
|
64
|
+
};
|
|
65
|
+
strict_1.default.equal((0, proxy_target_1.resolveQelosProxyTarget)(config), 'https://from-app-url.example');
|
|
66
|
+
});
|
|
67
|
+
(0, node_test_1.it)('returns undefined when nothing is configured', () => {
|
|
68
|
+
const config = { appUrl: '' };
|
|
69
|
+
strict_1.default.equal((0, proxy_target_1.resolveQelosProxyTarget)(config), undefined);
|
|
70
|
+
});
|
|
71
|
+
(0, node_test_1.it)('treats whitespace-only values as unset', () => {
|
|
72
|
+
process.env.QELOS_PROXY_TARGET = ' ';
|
|
73
|
+
process.env.QELOS_IP = '\t';
|
|
74
|
+
const config = {
|
|
75
|
+
appUrl: 'https://from-app-url.example',
|
|
76
|
+
};
|
|
77
|
+
strict_1.default.equal((0, proxy_target_1.resolveQelosProxyTarget)(config), 'https://from-app-url.example');
|
|
78
|
+
});
|
|
79
|
+
(0, node_test_1.it)('trims surrounding whitespace from resolved values', () => {
|
|
80
|
+
const config = {
|
|
81
|
+
appUrl: ' https://trimmed.example ',
|
|
82
|
+
};
|
|
83
|
+
strict_1.default.equal((0, proxy_target_1.resolveQelosProxyTarget)(config), 'https://trimmed.example');
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
//# sourceMappingURL=proxy-target.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proxy-target.test.js","sourceRoot":"","sources":["../src/proxy-target.test.ts"],"names":[],"mappings":";;;;;AAAA,yCAAgE;AAChE,gEAAwC;AACxC,iDAAyD;AAGzD,MAAM,cAAc,GAAG;IACrB,oBAAoB;IACpB,UAAU;IACV,cAAc;CACN,CAAC;AAEX,SAAS,aAAa;IACpB,KAAK,MAAM,GAAG,IAAI,cAAc,EAAE,CAAC;QACjC,OAAO,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAC1B,CAAC;AACH,CAAC;AAED,IAAA,oBAAQ,EAAC,yBAAyB,EAAE,GAAG,EAAE;IACvC,MAAM,QAAQ,GAAuC,EAAE,CAAC;IAExD,IAAA,sBAAU,EAAC,GAAG,EAAE;QACd,KAAK,MAAM,GAAG,IAAI,cAAc,EAAE,CAAC;YACjC,QAAQ,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACnC,CAAC;QACD,aAAa,EAAE,CAAC;IAClB,CAAC,CAAC,CAAC;IAEH,IAAA,qBAAS,EAAC,GAAG,EAAE;QACb,KAAK,MAAM,GAAG,IAAI,cAAc,EAAE,CAAC;YACjC,IAAI,QAAQ,CAAC,GAAG,CAAC,KAAK,SAAS,EAAE,CAAC;gBAChC,OAAO,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;YAC1B,CAAC;iBAAM,CAAC;gBACN,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC;YACnC,CAAC;QACH,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,IAAA,cAAE,EAAC,2EAA2E,EAAE,GAAG,EAAE;QACnF,OAAO,CAAC,GAAG,CAAC,kBAAkB,GAAG,gCAAgC,CAAC;QAClE,OAAO,CAAC,GAAG,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EAAE,CAAC;QAC/C,gBAAM,CAAC,KAAK,CAAC,IAAA,sCAAuB,EAAC,MAAM,CAAC,EAAE,SAAS,CAAC,CAAC;IAC3D,CAAC,CAAC,CAAC;IAEH,IAAA,cAAE,EAAC,wCAAwC,EAAE,GAAG,EAAE;QAChD,OAAO,CAAC,GAAG,CAAC,kBAAkB,GAAG,KAAK,CAAC;QACvC,OAAO,CAAC,GAAG,CAAC,QAAQ,GAAG,IAAI,CAAC;QAC5B,MAAM,MAAM,GAAoB;YAC9B,MAAM,EAAE,8BAA8B;SACvC,CAAC;QACF,gBAAM,CAAC,KAAK,CACV,IAAA,sCAAuB,EAAC,MAAM,CAAC,EAC/B,8BAA8B,CAC/B,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,IAAA,cAAE,EAAC,mDAAmD,EAAE,GAAG,EAAE;QAC3D,MAAM,MAAM,GAAoB;YAC9B,MAAM,EAAE,6BAA6B;SACtC,CAAC;QACF,gBAAM,CAAC,KAAK,CACV,IAAA,sCAAuB,EAAC,MAAM,CAAC,EAC/B,yBAAyB,CAC1B,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type NestMiddleware } from '@nestjs/common';
|
|
2
|
+
import type { QelosModuleOptions } from './types';
|
|
3
|
+
export declare class QelosProxyMiddleware implements NestMiddleware {
|
|
4
|
+
private readonly options;
|
|
5
|
+
constructor(options: QelosModuleOptions);
|
|
6
|
+
use(req: unknown, res: unknown, _next: (err?: unknown) => void): void;
|
|
7
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
19
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
20
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
21
|
+
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;
|
|
22
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
23
|
+
};
|
|
24
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
25
|
+
if (mod && mod.__esModule) return mod;
|
|
26
|
+
var result = {};
|
|
27
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
28
|
+
__setModuleDefault(result, mod);
|
|
29
|
+
return result;
|
|
30
|
+
};
|
|
31
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
32
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
33
|
+
};
|
|
34
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
35
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
exports.QelosProxyMiddleware = void 0;
|
|
39
|
+
const common_1 = require("@nestjs/common");
|
|
40
|
+
const http = __importStar(require("node:http"));
|
|
41
|
+
const https = __importStar(require("node:https"));
|
|
42
|
+
const node_url_1 = require("node:url");
|
|
43
|
+
const constants_1 = require("./constants");
|
|
44
|
+
const cookies_1 = require("./cookies");
|
|
45
|
+
const proxy_target_1 = require("./proxy-target");
|
|
46
|
+
const HOP_BY_HOP = new Set([
|
|
47
|
+
'connection',
|
|
48
|
+
'keep-alive',
|
|
49
|
+
'proxy-authenticate',
|
|
50
|
+
'proxy-authorization',
|
|
51
|
+
'te',
|
|
52
|
+
'trailer',
|
|
53
|
+
'transfer-encoding',
|
|
54
|
+
'upgrade',
|
|
55
|
+
'host',
|
|
56
|
+
'content-length',
|
|
57
|
+
]);
|
|
58
|
+
function getIncomingMessage(req) {
|
|
59
|
+
if (req &&
|
|
60
|
+
typeof req === 'object' &&
|
|
61
|
+
'raw' in req &&
|
|
62
|
+
req.raw &&
|
|
63
|
+
typeof req.raw.pipe === 'function') {
|
|
64
|
+
return req.raw;
|
|
65
|
+
}
|
|
66
|
+
return req;
|
|
67
|
+
}
|
|
68
|
+
function getServerResponse(res) {
|
|
69
|
+
if (res &&
|
|
70
|
+
typeof res === 'object' &&
|
|
71
|
+
'raw' in res &&
|
|
72
|
+
res.raw &&
|
|
73
|
+
typeof res.raw.setHeader === 'function') {
|
|
74
|
+
return res.raw;
|
|
75
|
+
}
|
|
76
|
+
return res;
|
|
77
|
+
}
|
|
78
|
+
function getRequestUrl(req) {
|
|
79
|
+
const r = req;
|
|
80
|
+
if (typeof r.originalUrl === 'string')
|
|
81
|
+
return r.originalUrl;
|
|
82
|
+
if (typeof r.url === 'string')
|
|
83
|
+
return r.url;
|
|
84
|
+
return '';
|
|
85
|
+
}
|
|
86
|
+
function buildForwardedHeaders(source, targetHost) {
|
|
87
|
+
const out = {};
|
|
88
|
+
for (const [name, value] of Object.entries(source)) {
|
|
89
|
+
if (value === undefined)
|
|
90
|
+
continue;
|
|
91
|
+
if (HOP_BY_HOP.has(name.toLowerCase()))
|
|
92
|
+
continue;
|
|
93
|
+
out[name] = value;
|
|
94
|
+
}
|
|
95
|
+
out.host = targetHost;
|
|
96
|
+
return out;
|
|
97
|
+
}
|
|
98
|
+
function sendJsonError(res, status, body) {
|
|
99
|
+
const framework = res;
|
|
100
|
+
if (typeof framework.status === 'function' && typeof framework.json === 'function') {
|
|
101
|
+
framework.status(status);
|
|
102
|
+
framework.json(body);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const raw = getServerResponse(res);
|
|
106
|
+
if (!raw.headersSent) {
|
|
107
|
+
raw.statusCode = status;
|
|
108
|
+
raw.setHeader('content-type', 'application/json');
|
|
109
|
+
raw.end(JSON.stringify(body));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
let QelosProxyMiddleware = class QelosProxyMiddleware {
|
|
113
|
+
constructor(options) {
|
|
114
|
+
this.options = options;
|
|
115
|
+
}
|
|
116
|
+
use(req, res, _next) {
|
|
117
|
+
const request = req;
|
|
118
|
+
const base = (0, proxy_target_1.resolveQelosProxyTarget)(this.options.config);
|
|
119
|
+
if (!base) {
|
|
120
|
+
sendJsonError(res, 503, {
|
|
121
|
+
code: 'QELOS_PROXY_NOT_CONFIGURED',
|
|
122
|
+
message: '[@qelos/integrator-nest] Qelos API proxy is not configured. ' +
|
|
123
|
+
'Set config.appUrl (or QELOS_PROXY_TARGET / QELOS_IP / QELOS_API_IP in development).',
|
|
124
|
+
});
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
let target;
|
|
128
|
+
try {
|
|
129
|
+
const baseUrl = new node_url_1.URL(base);
|
|
130
|
+
const incomingUrl = getRequestUrl(request);
|
|
131
|
+
target = new node_url_1.URL(incomingUrl, baseUrl);
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
sendJsonError(res, 502, {
|
|
135
|
+
code: 'QELOS_PROXY_BAD_TARGET',
|
|
136
|
+
message: '[@qelos/integrator-nest] Invalid proxy target URL.',
|
|
137
|
+
});
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const client = target.protocol === 'https:' ? https : http;
|
|
141
|
+
const hostHeader = request.headers.host;
|
|
142
|
+
const originalHost = typeof hostHeader === 'string'
|
|
143
|
+
? hostHeader
|
|
144
|
+
: Array.isArray(hostHeader)
|
|
145
|
+
? hostHeader[0]
|
|
146
|
+
: undefined;
|
|
147
|
+
const nodeReq = getIncomingMessage(req);
|
|
148
|
+
const forwardedHeaders = buildForwardedHeaders(nodeReq.headers, target.host);
|
|
149
|
+
const upstreamReq = client.request({
|
|
150
|
+
protocol: target.protocol,
|
|
151
|
+
hostname: target.hostname,
|
|
152
|
+
port: target.port || (target.protocol === 'https:' ? 443 : 80),
|
|
153
|
+
method: nodeReq.method,
|
|
154
|
+
path: target.pathname + target.search,
|
|
155
|
+
headers: forwardedHeaders,
|
|
156
|
+
}, (upstreamRes) => {
|
|
157
|
+
const rawRes = getServerResponse(res);
|
|
158
|
+
const status = upstreamRes.statusCode ?? 502;
|
|
159
|
+
for (const [name, value] of Object.entries(upstreamRes.headers)) {
|
|
160
|
+
if (value === undefined)
|
|
161
|
+
continue;
|
|
162
|
+
const lower = name.toLowerCase();
|
|
163
|
+
if (HOP_BY_HOP.has(lower))
|
|
164
|
+
continue;
|
|
165
|
+
if (lower === 'set-cookie') {
|
|
166
|
+
const values = Array.isArray(value) ? value : [value];
|
|
167
|
+
rawRes.setHeader('set-cookie', values.map((v) => (0, cookies_1.rewriteSetCookieDomain)(v, originalHost)));
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
rawRes.setHeader(name, value);
|
|
171
|
+
}
|
|
172
|
+
rawRes.statusCode = status;
|
|
173
|
+
upstreamRes.pipe(rawRes);
|
|
174
|
+
});
|
|
175
|
+
upstreamReq.on('error', (err) => {
|
|
176
|
+
const rawRes = getServerResponse(res);
|
|
177
|
+
if (!rawRes.headersSent) {
|
|
178
|
+
sendJsonError(res, 502, {
|
|
179
|
+
code: 'QELOS_PROXY_UPSTREAM_ERROR',
|
|
180
|
+
message: err.message,
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
rawRes.end();
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
nodeReq.on('aborted', () => upstreamReq.destroy());
|
|
188
|
+
nodeReq.pipe(upstreamReq);
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
exports.QelosProxyMiddleware = QelosProxyMiddleware;
|
|
192
|
+
exports.QelosProxyMiddleware = QelosProxyMiddleware = __decorate([
|
|
193
|
+
(0, common_1.Injectable)(),
|
|
194
|
+
__param(0, (0, common_1.Inject)(constants_1.QELOS_MODULE_OPTIONS)),
|
|
195
|
+
__metadata("design:paramtypes", [Object])
|
|
196
|
+
], QelosProxyMiddleware);
|
|
197
|
+
//# sourceMappingURL=proxy.middleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proxy.middleware.js","sourceRoot":"","sources":["../src/proxy.middleware.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAIwB;AAExB,gDAAkC;AAClC,kDAAoC;AACpC,uCAA+B;AAC/B,2CAAmD;AACnD,uCAAmD;AACnD,iDAAyD;AAGzD,MAAM,UAAU,GAAG,IAAI,GAAG,CAAC;IACzB,YAAY;IACZ,YAAY;IACZ,oBAAoB;IACpB,qBAAqB;IACrB,IAAI;IACJ,SAAS;IACT,mBAAmB;IACnB,SAAS;IACT,MAAM;IACN,gBAAgB;CACjB,CAAC,CAAC;AAEH,SAAS,kBAAkB,CAAC,GAAY;IACtC,IACE,GAAG;QACH,OAAO,GAAG,KAAK,QAAQ;QACvB,KAAK,IAAI,GAAG;QACX,GAAsC,CAAC,GAAG;QAC3C,OAAQ,GAAqC,CAAC,GAAG,CAAC,IAAI,KAAK,UAAU,EACrE,CAAC;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AE,CAAC,CAAC;QACnD,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;IAC5B,CAAC;CACF,CAAA;AA1FY,oDAAoB;+BAApB,oBAAoB;IADhC,IAAA,mBAAU,GAAE;IAGR,WAAA,IAAA,eAAM,EAAC,gCAAoB,CAAC,CAAA;;GAFpB,oBAAoB,CA0FhC"}
|
package/dist/request-utils.d.ts
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
import type { AnyRequest, AnyResponse, QelosNestConfig
|
|
2
|
-
export declare
|
|
3
|
-
export declare const DEFAULT_REFRESH_COOKIE = "q_refresh_token";
|
|
4
|
-
export declare function readCookie(request: AnyRequest, name: string): string | undefined;
|
|
5
|
-
export declare function readTokens(request: AnyRequest, config: QelosNestConfig): QelosTokenPair;
|
|
6
|
-
export declare function writeTokensToCookies(response: AnyResponse, config: QelosNestConfig, tokens: ResolvedTokens): void;
|
|
1
|
+
import type { AnyRequest, AnyResponse, QelosNestConfig } from './types';
|
|
2
|
+
export declare function appendSetCookie(response: AnyResponse, value: string): void;
|
|
7
3
|
export declare function shouldSkip(request: AnyRequest, config: QelosNestConfig): boolean;
|
package/dist/request-utils.js
CHANGED
|
@@ -1,61 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.readCookie = readCookie;
|
|
5
|
-
exports.readTokens = readTokens;
|
|
6
|
-
exports.writeTokensToCookies = writeTokensToCookies;
|
|
3
|
+
exports.appendSetCookie = appendSetCookie;
|
|
7
4
|
exports.shouldSkip = shouldSkip;
|
|
8
|
-
exports.DEFAULT_ACCESS_COOKIE = 'q_access_token';
|
|
9
|
-
exports.DEFAULT_REFRESH_COOKIE = 'q_refresh_token';
|
|
10
|
-
function readCookie(request, name) {
|
|
11
|
-
// Prefer cookie-parser / @fastify/cookie's parsed output when available.
|
|
12
|
-
if (request.cookies && typeof request.cookies[name] === 'string') {
|
|
13
|
-
return request.cookies[name];
|
|
14
|
-
}
|
|
15
|
-
const cookieHeader = request.headers.cookie;
|
|
16
|
-
const header = Array.isArray(cookieHeader) ? cookieHeader.join('; ') : cookieHeader;
|
|
17
|
-
if (!header)
|
|
18
|
-
return undefined;
|
|
19
|
-
const prefix = name + '=';
|
|
20
|
-
for (const part of header.split(';')) {
|
|
21
|
-
const trimmed = part.trim();
|
|
22
|
-
if (trimmed.startsWith(prefix)) {
|
|
23
|
-
try {
|
|
24
|
-
return decodeURIComponent(trimmed.slice(prefix.length));
|
|
25
|
-
}
|
|
26
|
-
catch {
|
|
27
|
-
return trimmed.slice(prefix.length);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
return undefined;
|
|
32
|
-
}
|
|
33
|
-
function readTokens(request, config) {
|
|
34
|
-
const accessCookie = config.accessTokenCookie || exports.DEFAULT_ACCESS_COOKIE;
|
|
35
|
-
const refreshCookie = config.refreshTokenCookie || exports.DEFAULT_REFRESH_COOKIE;
|
|
36
|
-
const cookieAccess = readCookie(request, accessCookie);
|
|
37
|
-
const cookieRefresh = readCookie(request, refreshCookie);
|
|
38
|
-
const rawAuth = request.headers.authorization;
|
|
39
|
-
const authHeader = Array.isArray(rawAuth) ? rawAuth[0] : rawAuth;
|
|
40
|
-
const headerAccess = authHeader && authHeader.toLowerCase().startsWith('bearer ')
|
|
41
|
-
? authHeader.slice(7).trim()
|
|
42
|
-
: undefined;
|
|
43
|
-
return {
|
|
44
|
-
accessToken: headerAccess || cookieAccess || undefined,
|
|
45
|
-
refreshToken: cookieRefresh || undefined,
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
function serializeCookie(name, value, secure) {
|
|
49
|
-
const parts = [
|
|
50
|
-
`${name}=${encodeURIComponent(value)}`,
|
|
51
|
-
'Path=/',
|
|
52
|
-
'HttpOnly',
|
|
53
|
-
'SameSite=Lax',
|
|
54
|
-
];
|
|
55
|
-
if (secure)
|
|
56
|
-
parts.push('Secure');
|
|
57
|
-
return parts.join('; ');
|
|
58
|
-
}
|
|
59
5
|
function appendSetCookie(response, value) {
|
|
60
6
|
const setHeader = response.setHeader || response.header;
|
|
61
7
|
const getHeader = response.getHeader;
|
|
@@ -74,37 +20,6 @@ function appendSetCookie(response, value) {
|
|
|
74
20
|
setHeader.call(response, 'set-cookie', next);
|
|
75
21
|
}
|
|
76
22
|
}
|
|
77
|
-
function writeTokensToCookies(response, config, tokens) {
|
|
78
|
-
const accessCookie = config.accessTokenCookie || exports.DEFAULT_ACCESS_COOKIE;
|
|
79
|
-
const refreshCookie = config.refreshTokenCookie || exports.DEFAULT_REFRESH_COOKIE;
|
|
80
|
-
const secure = !/^http:\/\//i.test(config.appUrl);
|
|
81
|
-
const cookieOptions = {
|
|
82
|
-
httpOnly: true,
|
|
83
|
-
secure,
|
|
84
|
-
sameSite: 'lax',
|
|
85
|
-
path: '/',
|
|
86
|
-
};
|
|
87
|
-
// Express
|
|
88
|
-
if (typeof response.cookie === 'function') {
|
|
89
|
-
response.cookie(accessCookie, tokens.accessToken, cookieOptions);
|
|
90
|
-
if (tokens.refreshToken) {
|
|
91
|
-
response.cookie(refreshCookie, tokens.refreshToken, cookieOptions);
|
|
92
|
-
}
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
// Fastify (with @fastify/cookie)
|
|
96
|
-
if (typeof response.setCookie === 'function') {
|
|
97
|
-
response.setCookie(accessCookie, tokens.accessToken, cookieOptions);
|
|
98
|
-
if (tokens.refreshToken) {
|
|
99
|
-
response.setCookie(refreshCookie, tokens.refreshToken, cookieOptions);
|
|
100
|
-
}
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
appendSetCookie(response, serializeCookie(accessCookie, tokens.accessToken, secure));
|
|
104
|
-
if (tokens.refreshToken) {
|
|
105
|
-
appendSetCookie(response, serializeCookie(refreshCookie, tokens.refreshToken, secure));
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
23
|
function shouldSkip(request, config) {
|
|
109
24
|
if (!config.skipPaths?.length)
|
|
110
25
|
return false;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request-utils.js","sourceRoot":"","sources":["../src/request-utils.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"request-utils.js","sourceRoot":"","sources":["../src/request-utils.ts"],"names":[],"mappings":";;AAEA,0CAgBC;AAED,gCASC;AA3BD,SAAgB,eAAe,CAAC,QAAqB,EAAE,KAAa;IAClE,MAAM,SAAS,GAAG,QAAQ,CAAC,SAAS,IAAI,QAAQ,CAAC,MAAM,CAAC;IACxD,MAAM,SAAS,GAAG,QAAQ,CAAC,SAAS,CAAC;IACrC,MAAM,QAAQ,GACZ,OAAO,SAAS,KAAK,UAAU,CAAC,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IACvF,IAAI,IAAc,CAAC;IACnB,IAAI,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC5B,IAAI,GAAG,CAAC,GAAG,QAAQ,EAAE,KAAK,CAAC,CAAC;IAC9B,CAAC;SAAM,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE,CAAC;QACxC,IAAI,GAAG,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;IAC3B,CAAC;SAAM,CAAC;QACN,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC;IACjB,CAAC;IACD,IAAI,OAAO,SAAS,KAAK,UAAU,EAAE,CAAC;QACpC,SAAS,CAAC,IAAI,CAAC,QAAQ,EAAE,YAAY,EAAE,IAAI,CAAC,CAAC;IAC/C,CAAC;AACH,CAAC;AAED,SAAgB,UAAU,CACxB,OAAmB,EACnB,MAAuB;IAEvB,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM;QAAE,OAAO,KAAK,CAAC;IAC5C,MAAM,GAAG,GAAG,OAAO,CAAC,IAAI,IAAI,OAAO,CAAC,GAAG,IAAI,EAAE,CAAC;IAC9C,MAAM,QAAQ,GAAG,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IAClC,MAAM,IAAI,GAAG,QAAQ,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC;IAC1D,OAAO,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC;AACpE,CAAC"}
|
package/dist/sdk-factory.d.ts
CHANGED
|
@@ -1,15 +1,7 @@
|
|
|
1
1
|
import QelosSDK from '@qelos/sdk';
|
|
2
|
-
import type { AnyRequest,
|
|
2
|
+
import type { AnyRequest, QelosNestConfig } from './types';
|
|
3
3
|
export interface CreateSdkParams {
|
|
4
4
|
config: QelosNestConfig;
|
|
5
|
-
/**
|
|
6
|
-
* Tokens for the current request. The factory mutates this object in place
|
|
7
|
-
* when a token refresh occurs, so callers can read the latest pair after
|
|
8
|
-
* the SDK has been used.
|
|
9
|
-
*/
|
|
10
|
-
tokens: QelosTokenPair;
|
|
11
5
|
request: AnyRequest;
|
|
12
|
-
response: AnyResponse;
|
|
13
|
-
onTokenRefresh?: TokenRefreshHook;
|
|
14
6
|
}
|
|
15
|
-
export declare function createRequestSdk({ config,
|
|
7
|
+
export declare function createRequestSdk({ config, request, }: CreateSdkParams): QelosSDK;
|
package/dist/sdk-factory.js
CHANGED
|
@@ -5,93 +5,35 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.createRequestSdk = createRequestSdk;
|
|
7
7
|
const sdk_1 = __importDefault(require("@qelos/sdk"));
|
|
8
|
-
|
|
9
|
-
'/api/token/refresh',
|
|
10
|
-
'/api/cookie/refresh',
|
|
11
|
-
'/api/signin',
|
|
12
|
-
'/api/signup',
|
|
13
|
-
]);
|
|
14
|
-
function createRequestSdk({ config, tokens, request, response, onTokenRefresh, }) {
|
|
15
|
-
let sdk;
|
|
16
|
-
let refreshInFlight = null;
|
|
8
|
+
function createRequestSdk({ config, request, }) {
|
|
17
9
|
const baseOptions = config.sdkOptions || {};
|
|
18
|
-
async function performRefresh() {
|
|
19
|
-
if (!tokens.refreshToken && !tokens.accessToken) {
|
|
20
|
-
throw new Error('no refresh token available');
|
|
21
|
-
}
|
|
22
|
-
const previous = {
|
|
23
|
-
accessToken: tokens.accessToken,
|
|
24
|
-
refreshToken: tokens.refreshToken,
|
|
25
|
-
};
|
|
26
|
-
let refreshed;
|
|
27
|
-
if (tokens.refreshToken) {
|
|
28
|
-
const result = await sdk.authentication.refreshToken(tokens.refreshToken);
|
|
29
|
-
refreshed = {
|
|
30
|
-
accessToken: result.payload.token,
|
|
31
|
-
refreshToken: result.payload.refreshToken,
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
const result = await sdk.authentication.refreshCookieToken(tokens.accessToken);
|
|
36
|
-
refreshed = {
|
|
37
|
-
accessToken: result.payload.cookieToken,
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
tokens.accessToken = refreshed.accessToken;
|
|
41
|
-
tokens.refreshToken = refreshed.refreshToken;
|
|
42
|
-
if (onTokenRefresh) {
|
|
43
|
-
await onTokenRefresh({
|
|
44
|
-
request,
|
|
45
|
-
response,
|
|
46
|
-
oldTokens: previous,
|
|
47
|
-
newTokens: refreshed,
|
|
48
|
-
sdk,
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
function ensureRefresh() {
|
|
53
|
-
if (!refreshInFlight) {
|
|
54
|
-
refreshInFlight = performRefresh().finally(() => {
|
|
55
|
-
refreshInFlight = null;
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
return refreshInFlight;
|
|
59
|
-
}
|
|
60
10
|
const options = {
|
|
61
11
|
appUrl: config.appUrl,
|
|
62
12
|
fetch: globalThis.fetch,
|
|
63
|
-
forceRefresh: !config.apiToken,
|
|
64
13
|
...baseOptions,
|
|
65
14
|
};
|
|
66
15
|
if (config.apiToken) {
|
|
67
16
|
options.apiToken = config.apiToken;
|
|
17
|
+
return new sdk_1.default(options);
|
|
68
18
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
19
|
+
options.extraHeaders = async () => {
|
|
20
|
+
const headers = {};
|
|
21
|
+
const cookieHeader = request.headers.cookie;
|
|
22
|
+
const cookie = typeof cookieHeader === 'string'
|
|
23
|
+
? cookieHeader
|
|
24
|
+
: Array.isArray(cookieHeader)
|
|
25
|
+
? cookieHeader.join('; ')
|
|
26
|
+
: undefined;
|
|
27
|
+
if (cookie) {
|
|
28
|
+
headers.cookie = cookie;
|
|
72
29
|
}
|
|
73
|
-
|
|
74
|
-
|
|
30
|
+
const rawAuth = request.headers.authorization;
|
|
31
|
+
const authHeader = Array.isArray(rawAuth) ? rawAuth[0] : rawAuth;
|
|
32
|
+
if (authHeader) {
|
|
33
|
+
headers.authorization = authHeader;
|
|
75
34
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
return headers;
|
|
80
|
-
}
|
|
81
|
-
if (forceRefresh && tokens.refreshToken) {
|
|
82
|
-
await ensureRefresh();
|
|
83
|
-
}
|
|
84
|
-
const token = sdk?.authentication?.accessToken || tokens.accessToken;
|
|
85
|
-
if (token) {
|
|
86
|
-
headers.authorization = 'Bearer ' + token;
|
|
87
|
-
}
|
|
88
|
-
return headers;
|
|
89
|
-
};
|
|
90
|
-
options.onFailedRefreshToken = async () => {
|
|
91
|
-
await ensureRefresh();
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
sdk = new sdk_1.default(options);
|
|
95
|
-
return sdk;
|
|
35
|
+
return headers;
|
|
36
|
+
};
|
|
37
|
+
return new sdk_1.default(options);
|
|
96
38
|
}
|
|
97
39
|
//# sourceMappingURL=sdk-factory.js.map
|
package/dist/sdk-factory.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sdk-factory.js","sourceRoot":"","sources":["../src/sdk-factory.ts"],"names":[],"mappings":";;;;;
|
|
1
|
+
{"version":3,"file":"sdk-factory.js","sourceRoot":"","sources":["../src/sdk-factory.ts"],"names":[],"mappings":";;;;;AASA,4CAsCC;AA/CD,qDAAkC;AASlC,SAAgB,gBAAgB,CAAC,EAC/B,MAAM,EACN,OAAO,GACS;IAChB,MAAM,WAAW,GAA6B,MAAM,CAAC,UAAU,IAAI,EAAE,CAAC;IAEtE,MAAM,OAAO,GAAoB;QAC/B,MAAM,EAAE,MAAM,CAAC,MAAM;QACrB,KAAK,EAAE,UAAU,CAAC,KAA6B;QAC/C,GAAG,WAAW;KACf,CAAC;IAEF,IAAI,MAAM,CAAC,QAAQ,EAAE,CAAC;QACpB,OAAO,CAAC,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC;QACnC,OAAO,IAAI,aAAQ,CAAC,OAAO,CAAC,CAAC;IAC/B,CAAC;IAED,OAAO,CAAC,YAAY,GAAG,KAAK,IAAI,EAAE;QAChC,MAAM,OAAO,GAA2B,EAAE,CAAC;QAC3C,MAAM,YAAY,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC;QAC5C,MAAM,MAAM,GACV,OAAO,YAAY,KAAK,QAAQ;YAC9B,CAAC,CAAC,YAAY;YACd,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,YAAY,CAAC;gBAC3B,CAAC,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC;gBACzB,CAAC,CAAC,SAAS,CAAC;QAClB,IAAI,MAAM,EAAE,CAAC;YACX,OAAO,CAAC,MAAM,GAAG,MAAM,CAAC;QAC1B,CAAC;QACD,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,aAAa,CAAC;QAC9C,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,CAAC,O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