@tradejs/node 1.0.2 → 1.0.4

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.
@@ -1,227 +0,0 @@
1
- import {
2
- importTradejsModule,
3
- loadTradejsConfig,
4
- resolvePluginModuleSpecifier
5
- } from "./chunk-DE7ADBIR.mjs";
6
-
7
- // src/connectorsRegistry.ts
8
- import { logger } from "@tradejs/infra/logger";
9
- var connectorCreators = /* @__PURE__ */ new Map();
10
- var providerToConnectorName = /* @__PURE__ */ new Map();
11
- var pluginsLoadPromise = null;
12
- var BUILTIN_CONNECTOR_NAMES = {
13
- ByBit: "ByBit",
14
- Binance: "Binance",
15
- Coinbase: "Coinbase",
16
- Test: "Test"
17
- };
18
- var normalizeProvider = (value) => String(value ?? "").trim().toLowerCase();
19
- var toUniqueModules = (modules = []) => [
20
- ...new Set(modules.map((moduleName) => moduleName.trim()).filter(Boolean))
21
- ];
22
- var findConnectorNameInsensitive = (name) => {
23
- const normalized = name.trim().toLowerCase();
24
- if (!normalized) {
25
- return null;
26
- }
27
- for (const existingName of connectorCreators.keys()) {
28
- if (existingName.toLowerCase() === normalized) {
29
- return existingName;
30
- }
31
- }
32
- return null;
33
- };
34
- var normalizeProviders = (providers, connectorName) => {
35
- const list = Array.isArray(providers) ? providers.map((item) => normalizeProvider(item)).filter(Boolean) : [];
36
- const deduped = [...new Set(list)];
37
- if (deduped.length) {
38
- return deduped;
39
- }
40
- return [normalizeProvider(connectorName)];
41
- };
42
- var registerProvider = (provider, connectorName, source) => {
43
- const existing = providerToConnectorName.get(provider);
44
- if (existing && existing !== connectorName) {
45
- logger.warn(
46
- 'Skip duplicate connector provider "%s" from %s: already mapped to %s',
47
- provider,
48
- source,
49
- existing
50
- );
51
- return;
52
- }
53
- providerToConnectorName.set(provider, connectorName);
54
- };
55
- var registerEntry = (entry, source) => {
56
- const connectorName = String(entry?.name ?? "").trim();
57
- if (!connectorName) {
58
- logger.warn("Skip connector entry without name from %s", source);
59
- return;
60
- }
61
- if (typeof entry.creator !== "function") {
62
- logger.warn(
63
- 'Skip connector entry "%s" from %s: creator must be a function',
64
- connectorName,
65
- source
66
- );
67
- return;
68
- }
69
- const existingByName = findConnectorNameInsensitive(connectorName);
70
- if (existingByName) {
71
- logger.warn(
72
- 'Skip duplicate connector "%s" from %s: already registered as %s',
73
- connectorName,
74
- source,
75
- existingByName
76
- );
77
- return;
78
- }
79
- connectorCreators.set(connectorName, entry.creator);
80
- const providers = normalizeProviders(entry.providers, connectorName);
81
- for (const provider of providers) {
82
- registerProvider(provider, connectorName, source);
83
- }
84
- };
85
- var registerEntries = (entries, source) => {
86
- for (const entry of entries) {
87
- registerEntry(entry, source);
88
- }
89
- };
90
- var extractConnectorPluginDefinition = (moduleExport) => {
91
- if (!moduleExport || typeof moduleExport !== "object") {
92
- return null;
93
- }
94
- const candidate = moduleExport;
95
- if (Array.isArray(candidate.connectorEntries)) {
96
- return {
97
- connectorEntries: candidate.connectorEntries
98
- };
99
- }
100
- const defaultExport = candidate.default;
101
- if (defaultExport && Array.isArray(defaultExport.connectorEntries)) {
102
- return {
103
- connectorEntries: defaultExport.connectorEntries
104
- };
105
- }
106
- return null;
107
- };
108
- var importConnectorPluginModule = async (moduleName) => {
109
- if (typeof importTradejsModule === "function") {
110
- return importTradejsModule(moduleName);
111
- }
112
- return import(
113
- /* webpackIgnore: true */
114
- moduleName
115
- );
116
- };
117
- var ensureConnectorPluginsLoaded = async () => {
118
- if (pluginsLoadPromise) {
119
- return pluginsLoadPromise;
120
- }
121
- pluginsLoadPromise = (async () => {
122
- const config = await loadTradejsConfig();
123
- const connectorModules = toUniqueModules(config.connectors);
124
- if (!connectorModules.length) {
125
- return;
126
- }
127
- for (const moduleName of connectorModules) {
128
- try {
129
- const resolvedModuleName = resolvePluginModuleSpecifier(moduleName);
130
- const moduleExport = await importConnectorPluginModule(resolvedModuleName);
131
- const pluginDefinition = extractConnectorPluginDefinition(moduleExport);
132
- if (!pluginDefinition) {
133
- logger.warn(
134
- 'Skip connector plugin "%s": export { connectorEntries } is missing',
135
- moduleName
136
- );
137
- continue;
138
- }
139
- registerEntries(pluginDefinition.connectorEntries, moduleName);
140
- } catch (error) {
141
- logger.warn(
142
- 'Failed to load connector plugin "%s": %s',
143
- moduleName,
144
- String(error)
145
- );
146
- }
147
- }
148
- })();
149
- return pluginsLoadPromise;
150
- };
151
- var getConnectorCreatorByName = async (connectorName) => {
152
- await ensureConnectorPluginsLoaded();
153
- const raw = String(connectorName ?? "").trim();
154
- if (!raw) {
155
- return void 0;
156
- }
157
- const direct = connectorCreators.get(raw);
158
- if (direct) {
159
- return direct;
160
- }
161
- const existing = findConnectorNameInsensitive(raw);
162
- if (!existing) {
163
- return void 0;
164
- }
165
- return connectorCreators.get(existing);
166
- };
167
- var getConnectorNameByProvider = async (provider) => {
168
- await ensureConnectorPluginsLoaded();
169
- const normalized = normalizeProvider(provider);
170
- if (!normalized) {
171
- return void 0;
172
- }
173
- return providerToConnectorName.get(normalized);
174
- };
175
- var getConnectorCreatorByProvider = async (provider) => {
176
- const connectorName = await getConnectorNameByProvider(provider);
177
- if (!connectorName) {
178
- return void 0;
179
- }
180
- return connectorCreators.get(connectorName);
181
- };
182
- var resolveConnectorName = async (providerOrName) => {
183
- const raw = String(providerOrName ?? "").trim();
184
- if (!raw) {
185
- return void 0;
186
- }
187
- const byProvider = await getConnectorNameByProvider(raw);
188
- if (byProvider) {
189
- return byProvider;
190
- }
191
- const byName = await getConnectorCreatorByName(raw);
192
- if (!byName) {
193
- return void 0;
194
- }
195
- return findConnectorNameInsensitive(raw) ?? void 0;
196
- };
197
- var getAvailableConnectorNames = async () => {
198
- await ensureConnectorPluginsLoaded();
199
- return [...connectorCreators.keys()].sort((a, b) => a.localeCompare(b));
200
- };
201
- var getAvailableConnectorProviders = async () => {
202
- await ensureConnectorPluginsLoaded();
203
- return [...providerToConnectorName.keys()].sort((a, b) => a.localeCompare(b));
204
- };
205
- var registerConnectorEntries = (entries) => {
206
- registerEntries(entries, "runtime");
207
- };
208
- var resetConnectorRegistryCache = () => {
209
- connectorCreators.clear();
210
- providerToConnectorName.clear();
211
- pluginsLoadPromise = null;
212
- };
213
- var DEFAULT_CONNECTOR_NAME = BUILTIN_CONNECTOR_NAMES.ByBit;
214
-
215
- export {
216
- BUILTIN_CONNECTOR_NAMES,
217
- ensureConnectorPluginsLoaded,
218
- getConnectorCreatorByName,
219
- getConnectorNameByProvider,
220
- getConnectorCreatorByProvider,
221
- resolveConnectorName,
222
- getAvailableConnectorNames,
223
- getAvailableConnectorProviders,
224
- registerConnectorEntries,
225
- resetConnectorRegistryCache,
226
- DEFAULT_CONNECTOR_NAME
227
- };
@@ -1,201 +0,0 @@
1
- import {
2
- importTradejsModule,
3
- loadTradejsConfig,
4
- resolvePluginModuleSpecifier
5
- } from "./chunk-DE7ADBIR.mjs";
6
-
7
- // src/strategy/manifests.ts
8
- import {
9
- registerIndicatorEntries,
10
- resetIndicatorRegistryCache
11
- } from "@tradejs/core/indicators";
12
- import { logger } from "@tradejs/infra/logger";
13
- var strategyCreators = /* @__PURE__ */ new Map();
14
- var strategyManifestsMap = /* @__PURE__ */ new Map();
15
- var pluginsLoadPromise = null;
16
- var toUniqueModules = (modules = []) => [
17
- ...new Set(modules.map((moduleName) => moduleName.trim()).filter(Boolean))
18
- ];
19
- var getConfiguredPluginModuleNames = async () => {
20
- const config = await loadTradejsConfig();
21
- return {
22
- strategyModules: toUniqueModules(config.strategies),
23
- indicatorModules: toUniqueModules(config.indicators)
24
- };
25
- };
26
- var extractModuleEntries = (moduleExport, key) => {
27
- if (!moduleExport || typeof moduleExport !== "object") {
28
- return null;
29
- }
30
- const candidate = moduleExport;
31
- if (Array.isArray(candidate[key])) {
32
- return candidate[key];
33
- }
34
- const defaultExport = candidate.default;
35
- if (defaultExport && Array.isArray(defaultExport[key])) {
36
- return defaultExport[key];
37
- }
38
- return null;
39
- };
40
- var extractStrategyPluginDefinition = (moduleExport) => {
41
- const strategyEntries = extractModuleEntries(
42
- moduleExport,
43
- "strategyEntries"
44
- );
45
- return strategyEntries ? { strategyEntries } : null;
46
- };
47
- var extractIndicatorPluginDefinition = (moduleExport) => {
48
- const indicatorEntries = extractModuleEntries(
49
- moduleExport,
50
- "indicatorEntries"
51
- );
52
- return indicatorEntries ? { indicatorEntries } : null;
53
- };
54
- var registerEntries = (entries, source) => {
55
- for (const entry of entries) {
56
- const strategyName = entry.manifest?.name;
57
- if (!strategyName) {
58
- logger.warn("Skip strategy entry without name from %s", source);
59
- continue;
60
- }
61
- if (strategyCreators.has(strategyName)) {
62
- logger.warn(
63
- 'Skip duplicate strategy "%s" from %s: already registered',
64
- strategyName,
65
- source
66
- );
67
- continue;
68
- }
69
- strategyCreators.set(strategyName, entry.creator);
70
- strategyManifestsMap.set(strategyName, entry.manifest);
71
- }
72
- };
73
- var importStrategyPluginModule = async (moduleName) => {
74
- if (typeof importTradejsModule === "function") {
75
- return importTradejsModule(moduleName);
76
- }
77
- return import(
78
- /* webpackIgnore: true */
79
- moduleName
80
- );
81
- };
82
- var ensureStrategyPluginsLoaded = async () => {
83
- if (pluginsLoadPromise) {
84
- return pluginsLoadPromise;
85
- }
86
- pluginsLoadPromise = (async () => {
87
- const { strategyModules, indicatorModules } = await getConfiguredPluginModuleNames();
88
- const strategySet = new Set(strategyModules);
89
- const indicatorSet = new Set(indicatorModules);
90
- const pluginModuleNames = [
91
- .../* @__PURE__ */ new Set([...strategyModules, ...indicatorModules])
92
- ];
93
- if (!pluginModuleNames.length) return;
94
- for (const moduleName of pluginModuleNames) {
95
- try {
96
- const resolvedModuleName = resolvePluginModuleSpecifier(moduleName);
97
- const moduleExport = await importStrategyPluginModule(resolvedModuleName);
98
- if (strategySet.has(moduleName)) {
99
- const pluginDefinition = extractStrategyPluginDefinition(moduleExport);
100
- if (!pluginDefinition) {
101
- logger.warn(
102
- 'Skip strategy plugin "%s": export { strategyEntries } is missing',
103
- moduleName
104
- );
105
- } else {
106
- registerEntries(pluginDefinition.strategyEntries, moduleName);
107
- }
108
- }
109
- if (indicatorSet.has(moduleName)) {
110
- const indicatorPluginDefinition = extractIndicatorPluginDefinition(moduleExport);
111
- if (!indicatorPluginDefinition) {
112
- logger.warn(
113
- 'Skip indicator plugin "%s": export { indicatorEntries } is missing',
114
- moduleName
115
- );
116
- } else {
117
- registerIndicatorEntries(
118
- indicatorPluginDefinition.indicatorEntries,
119
- moduleName
120
- );
121
- }
122
- }
123
- if (!strategySet.has(moduleName) && !indicatorSet.has(moduleName)) {
124
- logger.warn(
125
- 'Skip plugin "%s": no strategy/indicator sections requested in config',
126
- moduleName
127
- );
128
- }
129
- } catch (error) {
130
- logger.warn(
131
- 'Failed to load plugin "%s": %s',
132
- moduleName,
133
- String(error)
134
- );
135
- }
136
- }
137
- })();
138
- return pluginsLoadPromise;
139
- };
140
- var ensureIndicatorPluginsLoaded = ensureStrategyPluginsLoaded;
141
- var getStrategyCreator = async (name) => {
142
- await ensureStrategyPluginsLoaded();
143
- return strategyCreators.get(name);
144
- };
145
- var getAvailableStrategyNames = async () => {
146
- await ensureStrategyPluginsLoaded();
147
- return [...strategyCreators.keys()].sort((a, b) => a.localeCompare(b));
148
- };
149
- var getRegisteredStrategies = () => {
150
- return Object.fromEntries(strategyCreators.entries());
151
- };
152
- var getRegisteredManifests = () => {
153
- return [...strategyManifestsMap.values()];
154
- };
155
- var getStrategyManifest = (name) => {
156
- return name ? strategyManifestsMap.get(name) : void 0;
157
- };
158
- var isKnownStrategy = (name) => {
159
- return strategyCreators.has(name);
160
- };
161
- var registerStrategyEntries = (entries) => {
162
- registerEntries(entries, "runtime");
163
- };
164
- var resetStrategyRegistryCache = () => {
165
- strategyCreators.clear();
166
- strategyManifestsMap.clear();
167
- resetIndicatorRegistryCache();
168
- pluginsLoadPromise = null;
169
- };
170
- var strategies = new Proxy(
171
- {},
172
- {
173
- get: (_target, property) => {
174
- if (typeof property !== "string") {
175
- return void 0;
176
- }
177
- return strategyCreators.get(property);
178
- },
179
- ownKeys: () => {
180
- return [...strategyCreators.keys()];
181
- },
182
- getOwnPropertyDescriptor: () => ({
183
- enumerable: true,
184
- configurable: true
185
- })
186
- }
187
- );
188
-
189
- export {
190
- ensureStrategyPluginsLoaded,
191
- ensureIndicatorPluginsLoaded,
192
- getStrategyCreator,
193
- getAvailableStrategyNames,
194
- getRegisteredStrategies,
195
- getRegisteredManifests,
196
- getStrategyManifest,
197
- isKnownStrategy,
198
- registerStrategyEntries,
199
- resetStrategyRegistryCache,
200
- strategies
201
- };