@hitrading/ctp-node 2.0.0 → 3.0.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.
Files changed (79) hide show
  1. package/CHANGELOG.md +67 -0
  2. package/README.md +47 -32
  3. package/README.zh-CN.md +37 -27
  4. package/RELEASING.md +78 -0
  5. package/dist/client.d.ts +36 -15
  6. package/dist/client.d.ts.map +1 -1
  7. package/dist/client.js +234 -62
  8. package/dist/client.js.map +1 -1
  9. package/dist/codec.d.ts.map +1 -1
  10. package/dist/codec.js +84 -19
  11. package/dist/codec.js.map +1 -1
  12. package/dist/generated/structs.gen.d.ts +5461 -5461
  13. package/dist/generated/structs.gen.d.ts.map +1 -1
  14. package/dist/generated/trader.gen.d.ts.map +1 -1
  15. package/dist/generated/trader.gen.js +788 -94
  16. package/dist/generated/trader.gen.js.map +1 -1
  17. package/dist/index.cjs +1875 -415
  18. package/dist/index.d.cts +1 -1
  19. package/dist/index.d.ts +1 -1
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js.map +1 -1
  22. package/dist/market-data.d.ts +41 -24
  23. package/dist/market-data.d.ts.map +1 -1
  24. package/dist/market-data.js +148 -29
  25. package/dist/market-data.js.map +1 -1
  26. package/dist/native-binding.d.ts.map +1 -1
  27. package/dist/native-binding.js +4 -0
  28. package/dist/native-binding.js.map +1 -1
  29. package/dist/trader.d.ts +111 -71
  30. package/dist/trader.d.ts.map +1 -1
  31. package/dist/trader.js +774 -180
  32. package/dist/trader.js.map +1 -1
  33. package/dist/validation.d.ts +27 -0
  34. package/dist/validation.d.ts.map +1 -0
  35. package/dist/validation.js +85 -0
  36. package/dist/validation.js.map +1 -0
  37. package/docs/API.md +1437 -0
  38. package/docs/API.zh-CN.md +1278 -0
  39. package/docs/MIGRATION.md +54 -0
  40. package/docs/assets/validation.svg +27 -0
  41. package/docs/integration.md +60 -0
  42. package/docs/native-hooks.md +281 -0
  43. package/docs/native-hooks.zh-CN.md +205 -0
  44. package/docs/troubleshooting.md +257 -0
  45. package/docs/troubleshooting.zh-CN.md +220 -0
  46. package/package.json +12 -4
  47. package/prebuilds/linux-x64/@hitrading+ctp-node.node +0 -0
  48. package/prebuilds/win32-x64/@hitrading+ctp-node.node +0 -0
  49. package/scripts/bench-decode.mjs +37 -0
  50. package/scripts/bench-fullmarket-offline.mjs +207 -0
  51. package/scripts/stress-fullmarket.mjs +255 -0
  52. package/src/addon.cc +2 -2
  53. package/src/client.ts +255 -65
  54. package/src/codec.ts +86 -19
  55. package/src/generated/structs.gen.ts +5461 -5461
  56. package/src/generated/trader.gen.ts +309 -94
  57. package/src/generated/traderreq.gen.cc +194 -18
  58. package/src/generated/traderreq.gen.h +4 -0
  59. package/src/generated/traderspi.gen.cc +129 -15
  60. package/src/generated/traderspi.gen.h +4 -2
  61. package/src/index.ts +8 -1
  62. package/src/market-data.ts +199 -46
  63. package/src/native/arm.cc +114 -30
  64. package/src/native/arm.h +16 -4
  65. package/src/native/channel.cc +209 -81
  66. package/src/native/channel.h +65 -24
  67. package/src/native/ctp_field.h +23 -0
  68. package/src/native/mdapi.cc +582 -69
  69. package/src/native/mdspi.cc +219 -16
  70. package/src/native/mdspi.h +59 -10
  71. package/src/native/order_capability.h +75 -0
  72. package/src/native/risk.cc +2030 -213
  73. package/src/native/risk.h +431 -78
  74. package/src/native/shared_handle.h +37 -0
  75. package/src/native/snapshot.h +75 -16
  76. package/src/native/traderapi.cc +753 -124
  77. package/src/native-binding.ts +4 -0
  78. package/src/trader.ts +971 -204
  79. package/src/validation.ts +96 -0
@@ -0,0 +1,96 @@
1
+ /** Runtime request/config objects must have predictable own-property semantics.
2
+ * Accept ordinary object literals and null-prototype dictionaries; reject class
3
+ * instances and inherited fields so a value cannot reach the CTP wire without
4
+ * appearing in the caller's explicit request shape. */
5
+ export function assertPlainRecord(
6
+ value: unknown,
7
+ label: string
8
+ ): asserts value is Record<string, unknown> {
9
+ if (value === null || typeof value !== "object" || Array.isArray(value))
10
+ throw new TypeError(`${label} must be a plain object`);
11
+ const prototype = Object.getPrototypeOf(value);
12
+ if (prototype !== Object.prototype && prototype !== null)
13
+ throw new TypeError(`${label} must be a plain object`);
14
+ }
15
+
16
+ /** Return every own key, including non-enumerable keys. Symbols cannot name a
17
+ * CTP field and are rejected rather than silently ignored. */
18
+ export function ownStringKeys(value: unknown, label: string): string[] {
19
+ assertPlainRecord(value, label);
20
+ const keys = Reflect.ownKeys(value);
21
+ if (keys.some((key) => typeof key !== "string"))
22
+ throw new TypeError(`${label} contains an unsupported symbol field`);
23
+ return keys as string[];
24
+ }
25
+
26
+ /** Snapshot only explicit values into a null-prototype object before handing a
27
+ * request to N-API. This prevents Object.prototype pollution from being seen by
28
+ * native Has()/Get() and treats an explicit `undefined` like an omitted optional
29
+ * field, matching the binary codec. */
30
+ export function copyOwnRecord<T extends object>(value: T, label: string): T {
31
+ const source = value as Record<string, unknown>;
32
+ const copy = Object.create(null) as Record<string, unknown>;
33
+ for (const key of ownStringKeys(value, label)) {
34
+ const field = source[key];
35
+ if (field !== undefined) copy[key] = field;
36
+ }
37
+ return copy as T;
38
+ }
39
+
40
+ /** Validate and snapshot a dense instrument-id list in one pass. Array iteration
41
+ * helpers such as some() skip holes; treating a hole as an omitted CTP
42
+ * InstrumentID can accidentally turn a scoped metadata request into a query-all
43
+ * request, so holes and inherited indices are rejected (the same explicit-own-
44
+ * value rule as request objects). Callers that cross an async boundary must use
45
+ * this copy: otherwise an accessor or ordinary mutation after validation can
46
+ * change a scoped request into another query. */
47
+ export function copyDenseStringArray(
48
+ value: unknown,
49
+ label: string,
50
+ allowEmpty = true
51
+ ): string[] {
52
+ if (!Array.isArray(value))
53
+ throw new TypeError(`${label} must be ${allowEmpty ? "an" : "a non-empty"} instrument-id array`);
54
+ const length = value.length;
55
+ if (!allowEmpty && length === 0)
56
+ throw new TypeError(`${label} must be a non-empty instrument-id array`);
57
+ const copy = new Array<string>(length);
58
+ for (let i = 0; i < length; i++) {
59
+ if (!Object.prototype.hasOwnProperty.call(value, i)) {
60
+ throw new TypeError(`${label} must contain dense, non-empty strings without NUL`);
61
+ }
62
+ const item: unknown = value[i]; // read an accessor exactly once
63
+ if (typeof item !== "string" || item.length === 0 || item.includes("\0"))
64
+ throw new TypeError(`${label} must contain dense, non-empty strings without NUL`);
65
+ copy[i] = item;
66
+ }
67
+ return copy;
68
+ }
69
+
70
+ /** Copy a constructor front list without allowing Array.prototype values to
71
+ * become connection addresses. Empty entries are preserved for the native
72
+ * normalization that skips them; at least one non-empty front is still
73
+ * enforced natively. */
74
+ export function copyOwnFronts(
75
+ value: string | string[],
76
+ label: string
77
+ ): string | string[] {
78
+ if (typeof value === "string") {
79
+ if (value.includes("\0")) throw new TypeError(`${label} must not contain NUL`);
80
+ return value;
81
+ }
82
+ if (!Array.isArray(value))
83
+ throw new TypeError(`${label} must be a string or string array`);
84
+ const length = value.length;
85
+ const copy: string[] = new Array(length);
86
+ for (let i = 0; i < length; i++) {
87
+ if (!Object.prototype.hasOwnProperty.call(value, i)) {
88
+ throw new TypeError(`${label} must contain only explicit strings without NUL`);
89
+ }
90
+ const item: unknown = value[i]; // snapshot an accessor exactly once
91
+ if (typeof item !== "string" || item.includes("\0"))
92
+ throw new TypeError(`${label} must contain only explicit strings without NUL`);
93
+ copy[i] = item;
94
+ }
95
+ return copy;
96
+ }