1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | return new (P || (P = Promise))(function (resolve, reject) {
|
4 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
7 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8 | });
|
9 | };
|
10 | Object.defineProperty(exports, "__esModule", { value: true });
|
11 | const events_1 = require("events");
|
12 | const path_1 = require("path");
|
13 | const core_1 = require("@cortexql/core");
|
14 | const paths_1 = require("../paths");
|
15 | const expressRequestContextParams_1 = require("../hooks/expressRequestContextParams");
|
16 | const webSocketTransportContextParams_1 = require("../hooks/webSocketTransportContextParams");
|
17 | const context_1 = require("../hooks/context");
|
18 | const contextExecution_1 = require("../hooks/contextExecution");
|
19 | const __1 = require("..");
|
20 | const getSchema_1 = require("../api/getSchema");
|
21 | const contextId_1 = require("../hooks/contextId");
|
22 | const contextExecutionId_1 = require("../hooks/contextExecutionId");
|
23 | contextId_1.default.addFilter('default', () => {
|
24 | return `${(new Date()).getTime()}.${Math.floor(Math.random() * Math.pow(10, 5))}`;
|
25 | });
|
26 | const $$executionSeq = Symbol();
|
27 | contextExecutionId_1.default.addFilter('default', (value, { context }) => {
|
28 | if (typeof context[$$executionSeq] === 'undefined') {
|
29 | context[$$executionSeq] = -1;
|
30 | }
|
31 | context[$$executionSeq] = context[$$executionSeq] + 1;
|
32 | return `${context.id}.${context[$$executionSeq]}`;
|
33 | });
|
34 | function createContextId(context) {
|
35 | return contextId_1.default.filter(null, { context });
|
36 | }
|
37 | exports.createContextId = createContextId;
|
38 | function createExecutionContextId(context, info) {
|
39 | return contextExecutionId_1.default.filter(null, { context, info });
|
40 | }
|
41 | exports.createExecutionContextId = createExecutionContextId;
|
42 | function createContextNamespace(context, info, id) {
|
43 | let { operationName, operationType, } = context;
|
44 | let namespace;
|
45 | if (typeof info !== 'undefined') {
|
46 | if (typeof operationType === 'undefined') {
|
47 | operationType = info.operation.operation;
|
48 | }
|
49 | if (typeof operationName === 'undefined'
|
50 | && typeof info.operation.name !== 'undefined') {
|
51 | operationName = info.operation.name.value;
|
52 | }
|
53 | operationType = `${operationType}.${info.fieldName}`;
|
54 | if (info.path.key !== info.fieldName) {
|
55 | if (typeof operationName === 'undefined') {
|
56 | operationName = '';
|
57 | }
|
58 | operationName = `${operationName}.${info.path.key}`;
|
59 | }
|
60 | }
|
61 | if (typeof operationType !== 'undefined') {
|
62 | namespace = `${operationType}`;
|
63 | }
|
64 | else {
|
65 | namespace = '';
|
66 | }
|
67 | if (typeof operationName !== 'undefined') {
|
68 | namespace = `${namespace}@${operationName}`;
|
69 | }
|
70 | return `${namespace}::${typeof id !== 'undefined' ? id : context.id}`;
|
71 | }
|
72 | exports.createContextNamespace = createContextNamespace;
|
73 | class BaseContext {
|
74 | constructor(params, operationName, operationType, id) {
|
75 | this.params = params;
|
76 | this.operationName = operationName;
|
77 | this.operationType = operationType;
|
78 | this.id = id;
|
79 | this.context = undefined;
|
80 | this.debug = ((...args) => {
|
81 | Object.defineProperty(this, 'debug', {
|
82 | configurable: true,
|
83 | value: core_1.debug(createContextNamespace(this)),
|
84 | });
|
85 | return this.debug(...args);
|
86 | });
|
87 | this.event = new events_1.EventEmitter();
|
88 | this.schema = getSchema_1.getSchema();
|
89 | this.execute = (document, variableValues, operationName) => __awaiter(this, void 0, void 0, function* () { return __1.execute(document, this.params, variableValues, operationName); });
|
90 | this.subscribe = (document, variableValues, operationName) => __awaiter(this, void 0, void 0, function* () { return __1.subscribe(document, this.params, variableValues, operationName); });
|
91 | Object.defineProperty(this, 'completed', { value: false, configurable: true });
|
92 | this.end(() => Object.defineProperty(this, 'completed', { value: true }));
|
93 | if (typeof this.id === 'undefined') {
|
94 | this.id = createContextId(this);
|
95 | }
|
96 | }
|
97 | execution(info) {
|
98 | return createExecutionContext(this, info);
|
99 | }
|
100 | end(handler, reason) {
|
101 | if (handler == null || handler instanceof Error) {
|
102 | if (this.completed) {
|
103 | throw new Error('The execution has already ended');
|
104 | }
|
105 | this.completionReason = reason;
|
106 | if (handler instanceof Error) {
|
107 | this.completionError = handler;
|
108 | this.event.emit('end', handler, reason);
|
109 | }
|
110 | else {
|
111 | this.event.emit('end', null, reason);
|
112 | }
|
113 | }
|
114 | else {
|
115 | if (this.completed) {
|
116 | process.nextTick(handler);
|
117 | }
|
118 | else {
|
119 | this.event.once('end', handler);
|
120 | }
|
121 | }
|
122 | }
|
123 | }
|
124 | exports.BaseContext = BaseContext;
|
125 | ;
|
126 | const $$executionCount = Symbol();
|
127 | class BaseExecutionContext {
|
128 | constructor(context, info) {
|
129 | this.context = context;
|
130 | this.info = info;
|
131 | this.isExecution = true;
|
132 | this.id = createExecutionContextId(context, info);
|
133 | const namespace = createContextNamespace(context, info, this.id);
|
134 | this.debug = core_1.debug(namespace);
|
135 | if (typeof context[$$executionCount] === 'undefined') {
|
136 | context[$$executionCount] = 0;
|
137 | }
|
138 | context[$$executionCount] = context[$$executionCount] + 1;
|
139 | this.context.end(() => {
|
140 | if (!this.completed) {
|
141 | this.end(null, 'context');
|
142 | }
|
143 | });
|
144 | this.end(() => {
|
145 | context[$$executionCount] = context[$$executionCount] - 1;
|
146 | if (context[$$executionCount] <= 0) {
|
147 | if (!this.context.completed) {
|
148 | this.context.end();
|
149 | }
|
150 | }
|
151 | });
|
152 | Object.defineProperty(this, 'completed', { value: false, configurable: true });
|
153 | this.end(() => Object.defineProperty(this, 'completed', { value: true }));
|
154 | }
|
155 | end(handler, reason) {
|
156 | if (handler == null || handler instanceof Error) {
|
157 | if (this.completed) {
|
158 | throw new Error('The execution has already ended');
|
159 | }
|
160 | this.completionReason = reason;
|
161 | if (handler instanceof Error) {
|
162 | this.completionError = handler;
|
163 | this.context.event.emit('endExecution', handler, reason);
|
164 | }
|
165 | else {
|
166 | this.context.event.emit('endExecution', null, reason);
|
167 | }
|
168 | }
|
169 | else {
|
170 | if (this.completed) {
|
171 | process.nextTick(handler);
|
172 | }
|
173 | else {
|
174 | this.context.event.once('endExecution', handler);
|
175 | }
|
176 | }
|
177 | }
|
178 | }
|
179 | exports.BaseExecutionContext = BaseExecutionContext;
|
180 | function createContext(params, operationName, operationType, executionId) {
|
181 | return __awaiter(this, void 0, void 0, function* () {
|
182 | const { Context } = require(path_1.resolve(paths_1.getApiPath(), 'context'));
|
183 | let context = new Context(params, operationName, operationType, executionId);
|
184 | yield context_1.default.do({ context, params });
|
185 | return context;
|
186 | });
|
187 | }
|
188 | exports.createContext = createContext;
|
189 | function createExecutionContext(context, info) {
|
190 | return __awaiter(this, void 0, void 0, function* () {
|
191 | const contextModule = require(path_1.resolve(paths_1.getApiPath(), 'context'));
|
192 | let execution;
|
193 | if ('ExecutionContext' in contextModule) {
|
194 | const { ExecutionContext } = contextModule;
|
195 | execution = new ExecutionContext(context, info);
|
196 | }
|
197 | else {
|
198 | execution = new BaseExecutionContext(context, info);
|
199 | }
|
200 | yield contextExecution_1.default.do({ execution, context, info });
|
201 | return Object.assign(execution, Object.assign({}, context, execution));
|
202 | });
|
203 | }
|
204 | exports.createExecutionContext = createExecutionContext;
|
205 | function getOperationFromQuery(query) {
|
206 | const match = query.match(/^\s*([^\s]+)(?:\s+([^\s]+))?\s*{/);
|
207 | if (match != null) {
|
208 | return {
|
209 | operationName: match[2],
|
210 | operationType: match[1],
|
211 | };
|
212 | }
|
213 | return {
|
214 | operationName: undefined,
|
215 | operationType: undefined,
|
216 | };
|
217 | }
|
218 | exports.getOperationFromQuery = getOperationFromQuery;
|
219 | function createContextFromExpressRequest(req, params) {
|
220 | return __awaiter(this, void 0, void 0, function* () {
|
221 | let contextParams = Object.assign({}, params);
|
222 | contextParams = expressRequestContextParams_1.default.filter(contextParams, { req, params });
|
223 | const query = req.method === 'POST' ? req.body.query : req.query;
|
224 | const { operationName, operationType, } = getOperationFromQuery(query);
|
225 | return yield createContext(contextParams, operationName, operationType);
|
226 | });
|
227 | }
|
228 | exports.createContextFromExpressRequest = createContextFromExpressRequest;
|
229 | function createContextFromWebSocketTransport(webSocket, message, params) {
|
230 | return __awaiter(this, void 0, void 0, function* () {
|
231 | let contextParams = Object.assign({}, (typeof message !== 'undefined' && typeof message.payload !== 'undefined' ? message.payload.context : null));
|
232 | contextParams = webSocketTransportContextParams_1.default.filter(contextParams, { webSocket, message, params });
|
233 | let operationName;
|
234 | let operationType;
|
235 | if (typeof params.query === 'string') {
|
236 | ({
|
237 | operationName,
|
238 | operationType,
|
239 | } = getOperationFromQuery(params.query));
|
240 | }
|
241 | return yield createContext(contextParams, operationName, operationType);
|
242 | });
|
243 | }
|
244 | exports.createContextFromWebSocketTransport = createContextFromWebSocketTransport;
|
245 |
|
\ | No newline at end of file |