@k8ts/instruments 0.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.
Files changed (307) hide show
  1. package/LICENSE.md +7 -0
  2. package/README.md +3 -0
  3. package/dist/_embedder/base.d.ts +11 -0
  4. package/dist/_embedder/base.d.ts.map +1 -0
  5. package/dist/_embedder/base.js +39 -0
  6. package/dist/_embedder/base.js.map +1 -0
  7. package/dist/_embedder/index.d.ts +2 -0
  8. package/dist/_embedder/index.d.ts.map +1 -0
  9. package/dist/_embedder/index.js +18 -0
  10. package/dist/_embedder/index.js.map +1 -0
  11. package/dist/_string/index.d.ts +6 -0
  12. package/dist/_string/index.d.ts.map +1 -0
  13. package/dist/_string/index.js +22 -0
  14. package/dist/_string/index.js.map +1 -0
  15. package/dist/_string/markers.d.ts +3 -0
  16. package/dist/_string/markers.d.ts.map +1 -0
  17. package/dist/_string/markers.js +48 -0
  18. package/dist/_string/markers.js.map +1 -0
  19. package/dist/_string/post-processor.d.ts +7 -0
  20. package/dist/_string/post-processor.d.ts.map +1 -0
  21. package/dist/_string/post-processor.js +30 -0
  22. package/dist/_string/post-processor.js.map +1 -0
  23. package/dist/_string/pretty-objects.d.ts +27 -0
  24. package/dist/_string/pretty-objects.d.ts.map +1 -0
  25. package/dist/_string/pretty-objects.js +192 -0
  26. package/dist/_string/pretty-objects.js.map +1 -0
  27. package/dist/_string/pretty-print.d.ts +2 -0
  28. package/dist/_string/pretty-print.d.ts.map +1 -0
  29. package/dist/_string/pretty-print.js +27 -0
  30. package/dist/_string/pretty-print.js.map +1 -0
  31. package/dist/_string/super-script.d.ts +2 -0
  32. package/dist/_string/super-script.d.ts.map +1 -0
  33. package/dist/_string/super-script.js +20 -0
  34. package/dist/_string/super-script.js.map +1 -0
  35. package/dist/api-kind/index.d.ts +43 -0
  36. package/dist/api-kind/index.d.ts.map +1 -0
  37. package/dist/api-kind/index.js +102 -0
  38. package/dist/api-kind/index.js.map +1 -0
  39. package/dist/cmd/cli-command.d.ts +23 -0
  40. package/dist/cmd/cli-command.d.ts.map +1 -0
  41. package/dist/cmd/cli-command.js +73 -0
  42. package/dist/cmd/cli-command.js.map +1 -0
  43. package/dist/cmd/cli-term.d.ts +25 -0
  44. package/dist/cmd/cli-term.d.ts.map +1 -0
  45. package/dist/cmd/cli-term.js +55 -0
  46. package/dist/cmd/cli-term.js.map +1 -0
  47. package/dist/cmd/index.d.ts +3 -0
  48. package/dist/cmd/index.d.ts.map +1 -0
  49. package/dist/cmd/index.js +7 -0
  50. package/dist/cmd/index.js.map +1 -0
  51. package/dist/cmd/types.d.ts +5 -0
  52. package/dist/cmd/types.d.ts.map +1 -0
  53. package/dist/cmd/types.js +3 -0
  54. package/dist/cmd/types.js.map +1 -0
  55. package/dist/displayers/displayers.d.ts +33 -0
  56. package/dist/displayers/displayers.d.ts.map +1 -0
  57. package/dist/displayers/displayers.js +129 -0
  58. package/dist/displayers/displayers.js.map +1 -0
  59. package/dist/displayers/index.d.ts +2 -0
  60. package/dist/displayers/index.d.ts.map +1 -0
  61. package/dist/displayers/index.js +18 -0
  62. package/dist/displayers/index.js.map +1 -0
  63. package/dist/env/env.d.ts +20 -0
  64. package/dist/env/env.d.ts.map +1 -0
  65. package/dist/env/env.js +68 -0
  66. package/dist/env/env.js.map +1 -0
  67. package/dist/env/index.d.ts +3 -0
  68. package/dist/env/index.d.ts.map +1 -0
  69. package/dist/env/index.js +7 -0
  70. package/dist/env/index.js.map +1 -0
  71. package/dist/env/types.d.ts +4 -0
  72. package/dist/env/types.d.ts.map +1 -0
  73. package/dist/env/types.js +3 -0
  74. package/dist/env/types.js.map +1 -0
  75. package/dist/env/validate-name.d.ts +2 -0
  76. package/dist/env/validate-name.d.ts.map +1 -0
  77. package/dist/env/validate-name.js +12 -0
  78. package/dist/env/validate-name.js.map +1 -0
  79. package/dist/error.d.ts +8 -0
  80. package/dist/error.d.ts.map +1 -0
  81. package/dist/error.js +20 -0
  82. package/dist/error.js.map +1 -0
  83. package/dist/forward-exports/delayed-exports.d.ts +18 -0
  84. package/dist/forward-exports/delayed-exports.d.ts.map +1 -0
  85. package/dist/forward-exports/delayed-exports.js +119 -0
  86. package/dist/forward-exports/delayed-exports.js.map +1 -0
  87. package/dist/forward-exports/index.d.ts +2 -0
  88. package/dist/forward-exports/index.d.ts.map +1 -0
  89. package/dist/forward-exports/index.js +18 -0
  90. package/dist/forward-exports/index.js.map +1 -0
  91. package/dist/graph/base-node.d.ts +34 -0
  92. package/dist/graph/base-node.d.ts.map +1 -0
  93. package/dist/graph/base-node.js +102 -0
  94. package/dist/graph/base-node.js.map +1 -0
  95. package/dist/graph/base-origin-entity.d.ts +19 -0
  96. package/dist/graph/base-origin-entity.d.ts.map +1 -0
  97. package/dist/graph/base-origin-entity.js +105 -0
  98. package/dist/graph/base-origin-entity.js.map +1 -0
  99. package/dist/graph/dependencies.d.ts +12 -0
  100. package/dist/graph/dependencies.d.ts.map +1 -0
  101. package/dist/graph/dependencies.js +82 -0
  102. package/dist/graph/dependencies.js.map +1 -0
  103. package/dist/graph/index.d.ts +7 -0
  104. package/dist/graph/index.d.ts.map +1 -0
  105. package/dist/graph/index.js +23 -0
  106. package/dist/graph/index.js.map +1 -0
  107. package/dist/graph/origin-node.d.ts +32 -0
  108. package/dist/graph/origin-node.d.ts.map +1 -0
  109. package/dist/graph/origin-node.js +161 -0
  110. package/dist/graph/origin-node.js.map +1 -0
  111. package/dist/graph/relations.d.ts +26 -0
  112. package/dist/graph/relations.d.ts.map +1 -0
  113. package/dist/graph/relations.js +38 -0
  114. package/dist/graph/relations.js.map +1 -0
  115. package/dist/graph/resource-node.d.ts +28 -0
  116. package/dist/graph/resource-node.d.ts.map +1 -0
  117. package/dist/graph/resource-node.js +144 -0
  118. package/dist/graph/resource-node.js.map +1 -0
  119. package/dist/image/family.d.ts +26 -0
  120. package/dist/image/family.d.ts.map +1 -0
  121. package/dist/image/family.js +63 -0
  122. package/dist/image/family.js.map +1 -0
  123. package/dist/image/index.d.ts +2 -0
  124. package/dist/image/index.d.ts.map +1 -0
  125. package/dist/image/index.js +9 -0
  126. package/dist/image/index.js.map +1 -0
  127. package/dist/index.d.ts +19 -0
  128. package/dist/index.d.ts.map +1 -0
  129. package/dist/index.js +35 -0
  130. package/dist/index.js.map +1 -0
  131. package/dist/kind-map/index.d.ts +44 -0
  132. package/dist/kind-map/index.d.ts.map +1 -0
  133. package/dist/kind-map/index.js +123 -0
  134. package/dist/kind-map/index.js.map +1 -0
  135. package/dist/manifest/index.d.ts +46 -0
  136. package/dist/manifest/index.d.ts.map +1 -0
  137. package/dist/manifest/index.js +21 -0
  138. package/dist/manifest/index.js.map +1 -0
  139. package/dist/manifest/manifest-builder.d.ts +45 -0
  140. package/dist/manifest/manifest-builder.d.ts.map +1 -0
  141. package/dist/manifest/manifest-builder.js +66 -0
  142. package/dist/manifest/manifest-builder.js.map +1 -0
  143. package/dist/ports/error.d.ts +5 -0
  144. package/dist/ports/error.d.ts.map +1 -0
  145. package/dist/ports/error.js +13 -0
  146. package/dist/ports/error.js.map +1 -0
  147. package/dist/ports/index.d.ts +7 -0
  148. package/dist/ports/index.d.ts.map +1 -0
  149. package/dist/ports/index.js +13 -0
  150. package/dist/ports/index.js.map +1 -0
  151. package/dist/ports/map.d.ts +13 -0
  152. package/dist/ports/map.d.ts.map +1 -0
  153. package/dist/ports/map.js +38 -0
  154. package/dist/ports/map.js.map +1 -0
  155. package/dist/ports/set.d.ts +18 -0
  156. package/dist/ports/set.d.ts.map +1 -0
  157. package/dist/ports/set.js +60 -0
  158. package/dist/ports/set.js.map +1 -0
  159. package/dist/ports/tools/entry.d.ts +6 -0
  160. package/dist/ports/tools/entry.d.ts.map +1 -0
  161. package/dist/ports/tools/entry.js +36 -0
  162. package/dist/ports/tools/entry.js.map +1 -0
  163. package/dist/ports/tools/parse.d.ts +3 -0
  164. package/dist/ports/tools/parse.d.ts.map +1 -0
  165. package/dist/ports/tools/parse.js +35 -0
  166. package/dist/ports/tools/parse.js.map +1 -0
  167. package/dist/ports/types.d.ts +37 -0
  168. package/dist/ports/types.d.ts.map +1 -0
  169. package/dist/ports/types.js +3 -0
  170. package/dist/ports/types.js.map +1 -0
  171. package/dist/producer/index.d.ts +2 -0
  172. package/dist/producer/index.d.ts.map +1 -0
  173. package/dist/producer/index.js +18 -0
  174. package/dist/producer/index.js.map +1 -0
  175. package/dist/producer/producer.d.ts +6 -0
  176. package/dist/producer/producer.d.ts.map +1 -0
  177. package/dist/producer/producer.js +13 -0
  178. package/dist/producer/producer.js.map +1 -0
  179. package/dist/ref-key/index.d.ts +2 -0
  180. package/dist/ref-key/index.d.ts.map +1 -0
  181. package/dist/ref-key/index.js +18 -0
  182. package/dist/ref-key/index.js.map +1 -0
  183. package/dist/ref-key/ref-key.d.ts +23 -0
  184. package/dist/ref-key/ref-key.d.ts.map +1 -0
  185. package/dist/ref-key/ref-key.js +67 -0
  186. package/dist/ref-key/ref-key.js.map +1 -0
  187. package/dist/reference/forward-ref.d.ts +22 -0
  188. package/dist/reference/forward-ref.d.ts.map +1 -0
  189. package/dist/reference/forward-ref.js +126 -0
  190. package/dist/reference/forward-ref.js.map +1 -0
  191. package/dist/reference/index.d.ts +4 -0
  192. package/dist/reference/index.d.ts.map +1 -0
  193. package/dist/reference/index.js +8 -0
  194. package/dist/reference/index.js.map +1 -0
  195. package/dist/reference/refable.d.ts +12 -0
  196. package/dist/reference/refable.d.ts.map +1 -0
  197. package/dist/reference/refable.js +3 -0
  198. package/dist/reference/refable.js.map +1 -0
  199. package/dist/resources/index.d.ts +3 -0
  200. package/dist/resources/index.d.ts.map +1 -0
  201. package/dist/resources/index.js +19 -0
  202. package/dist/resources/index.js.map +1 -0
  203. package/dist/resources/objects.d.ts +22 -0
  204. package/dist/resources/objects.d.ts.map +1 -0
  205. package/dist/resources/objects.js +93 -0
  206. package/dist/resources/objects.js.map +1 -0
  207. package/dist/resources/parser.d.ts +4 -0
  208. package/dist/resources/parser.d.ts.map +1 -0
  209. package/dist/resources/parser.js +21 -0
  210. package/dist/resources/parser.js.map +1 -0
  211. package/dist/resources/types.d.ts +18 -0
  212. package/dist/resources/types.d.ts.map +1 -0
  213. package/dist/resources/types.js +3 -0
  214. package/dist/resources/types.js.map +1 -0
  215. package/dist/src.tsbuildinfo +1 -0
  216. package/dist/test.tsbuildinfo +1 -0
  217. package/dist/tracing/git.d.ts +19 -0
  218. package/dist/tracing/git.d.ts.map +1 -0
  219. package/dist/tracing/git.js +45 -0
  220. package/dist/tracing/git.js.map +1 -0
  221. package/dist/tracing/index.d.ts +4 -0
  222. package/dist/tracing/index.d.ts.map +1 -0
  223. package/dist/tracing/index.js +20 -0
  224. package/dist/tracing/index.js.map +1 -0
  225. package/dist/tracing/trace.d.ts +11 -0
  226. package/dist/tracing/trace.d.ts.map +1 -0
  227. package/dist/tracing/trace.js +27 -0
  228. package/dist/tracing/trace.js.map +1 -0
  229. package/dist/tracing/traced.d.ts +4 -0
  230. package/dist/tracing/traced.d.ts.map +1 -0
  231. package/dist/tracing/traced.js +6 -0
  232. package/dist/tracing/traced.js.map +1 -0
  233. package/dist/units/index.d.ts +3 -0
  234. package/dist/units/index.d.ts.map +1 -0
  235. package/dist/units/index.js +22 -0
  236. package/dist/units/index.js.map +1 -0
  237. package/dist/units/unit-parser.d.ts +19 -0
  238. package/dist/units/unit-parser.d.ts.map +1 -0
  239. package/dist/units/unit-parser.js +53 -0
  240. package/dist/units/unit-parser.js.map +1 -0
  241. package/dist/units/units.d.ts +37 -0
  242. package/dist/units/units.d.ts.map +1 -0
  243. package/dist/units/units.js +36 -0
  244. package/dist/units/units.js.map +1 -0
  245. package/package.json +89 -0
  246. package/src/_embedder/base.ts +42 -0
  247. package/src/_embedder/index.ts +1 -0
  248. package/src/_string/index.ts +5 -0
  249. package/src/_string/markers.ts +47 -0
  250. package/src/_string/post-processor.ts +25 -0
  251. package/src/_string/pretty-objects.ts +65 -0
  252. package/src/_string/pretty-print.ts +24 -0
  253. package/src/_string/super-script.ts +17 -0
  254. package/src/api-kind/index.ts +134 -0
  255. package/src/cmd/cli-command.ts +89 -0
  256. package/src/cmd/cli-term.ts +60 -0
  257. package/src/cmd/index.ts +2 -0
  258. package/src/cmd/types.ts +4 -0
  259. package/src/displayers/displayers.ts +147 -0
  260. package/src/displayers/index.ts +1 -0
  261. package/src/env/env.ts +74 -0
  262. package/src/env/index.ts +2 -0
  263. package/src/env/types.ts +7 -0
  264. package/src/env/validate-name.ts +10 -0
  265. package/src/error.ts +15 -0
  266. package/src/forward-exports/delayed-exports.ts +155 -0
  267. package/src/forward-exports/index.ts +1 -0
  268. package/src/graph/base-node.ts +125 -0
  269. package/src/graph/base-origin-entity.ts +44 -0
  270. package/src/graph/dependencies.ts +27 -0
  271. package/src/graph/index.ts +6 -0
  272. package/src/graph/origin-node.ts +118 -0
  273. package/src/graph/relations.ts +62 -0
  274. package/src/graph/resource-node.ts +104 -0
  275. package/src/image/family.ts +71 -0
  276. package/src/image/index.ts +1 -0
  277. package/src/index.ts +18 -0
  278. package/src/kind-map/index.ts +150 -0
  279. package/src/manifest/index.ts +53 -0
  280. package/src/manifest/manifest-builder.ts +98 -0
  281. package/src/ports/error.ts +8 -0
  282. package/src/ports/index.ts +8 -0
  283. package/src/ports/map.ts +41 -0
  284. package/src/ports/set.ts +79 -0
  285. package/src/ports/tools/entry.ts +43 -0
  286. package/src/ports/tools/parse.ts +40 -0
  287. package/src/ports/types.ts +42 -0
  288. package/src/producer/index.ts +1 -0
  289. package/src/producer/producer.ts +18 -0
  290. package/src/ref-key/index.ts +1 -0
  291. package/src/ref-key/ref-key.ts +76 -0
  292. package/src/reference/forward-ref.ts +122 -0
  293. package/src/reference/index.ts +3 -0
  294. package/src/reference/refable.ts +11 -0
  295. package/src/resources/index.ts +2 -0
  296. package/src/resources/objects.ts +99 -0
  297. package/src/resources/parser.ts +24 -0
  298. package/src/resources/types.ts +23 -0
  299. package/src/tracing/git.ts +51 -0
  300. package/src/tracing/index.ts +3 -0
  301. package/src/tracing/trace.ts +31 -0
  302. package/src/tracing/traced.ts +4 -0
  303. package/src/tsconfig.json +10 -0
  304. package/src/units/index.ts +2 -0
  305. package/src/units/unit-parser.ts +63 -0
  306. package/src/units/units.ts +53 -0
  307. package/tsconfig.json +15 -0
@@ -0,0 +1,76 @@
1
+ import { hash } from "immutable"
2
+ import { Kind } from "../api-kind"
3
+ import { InstrumentsError } from "../error"
4
+ export type RefKey<Kind extends string = string, Name extends string = string> = RefKey.RefKey<
5
+ Kind,
6
+ Name
7
+ >
8
+ export namespace RefKey {
9
+ export function make<K extends string, Name extends string>(
10
+ kind: Kind.Identifier<K>,
11
+ name: Name
12
+ ): RefKey<K, Name> {
13
+ return new RefKey(kind, name)
14
+ }
15
+ export type Input = RefKey | RefKey["string"]
16
+ export type Format<Kind extends string, Name extends string> = `${Kind}/${Name}`
17
+ const separator = "/"
18
+ export interface Parsed {
19
+ kind: string
20
+ name: string
21
+ }
22
+
23
+ export function parse(ref: string) {
24
+ const result = tryParse(ref)
25
+ if (!result) {
26
+ throw new InstrumentsError(`Could not parse reference key: ${ref}`)
27
+ }
28
+ return result
29
+ }
30
+
31
+ export function tryParse(ref: string): Parsed | undefined {
32
+ if (typeof ref !== "string") {
33
+ return undefined
34
+ }
35
+ if (ref == null) {
36
+ return undefined
37
+ }
38
+ if (typeof ref === "object") {
39
+ return undefined
40
+ }
41
+ const [kind, name] = ref.split(separator).map(s => s.trim())
42
+ if (!kind || !name) {
43
+ return undefined
44
+ }
45
+ return {
46
+ kind,
47
+ name
48
+ }
49
+ }
50
+ export class RefKey<K extends string = string, Name extends string = string> {
51
+ constructor(
52
+ readonly kind: Kind.Identifier<K>,
53
+ readonly name: Name
54
+ ) {}
55
+
56
+ get string(): Format<K, Name> {
57
+ return [this.kind.name, this.name].join(separator) as Format<K, Name>
58
+ }
59
+
60
+ equals(other: RefKey): boolean {
61
+ if (other == null) {
62
+ return false
63
+ }
64
+ if (typeof other !== "object") {
65
+ return false
66
+ }
67
+ return this.kind.equals(other.kind) && this.name === other.name
68
+ }
69
+ hashCode() {
70
+ return hash(this.string)
71
+ }
72
+ toString() {
73
+ return this.string
74
+ }
75
+ }
76
+ }
@@ -0,0 +1,122 @@
1
+ import type { Doddle } from "doddle"
2
+ import { hash, List } from "immutable"
3
+ import { ProxyOperationError } from "../error"
4
+ import { RefKey } from "../ref-key/ref-key"
5
+
6
+ export type ForwardRef<T extends object> = ForwardRef.Core<T> & T
7
+ export namespace ForwardRef {
8
+ export function is(x: any): boolean {
9
+ return "__reference_props__" in x
10
+ }
11
+ export function make<T extends object>(props: Props<T>): ForwardRef<T> {
12
+ const core = new Core(props)
13
+ return new Proxy(core, new Handler(core)) as ForwardRef<T>
14
+ }
15
+ export interface Props<Referenced extends object> {
16
+ readonly key: RefKey
17
+ readonly namespace?: string
18
+ readonly origin: object
19
+ readonly resolver: Doddle<Referenced>
20
+ readonly class: Function
21
+ }
22
+
23
+ export class Core<T extends object> {
24
+ #props: Props<T>
25
+ constructor(props: Props<T>) {
26
+ this.#props = props
27
+ }
28
+
29
+ equals(other: any) {
30
+ const resolved = this.#props.resolver.pull() as any
31
+ if (ForwardRef.is(other)) {
32
+ return other.equals(resolved)
33
+ }
34
+ if ("equals" in resolved) {
35
+ return resolved.equals(other)
36
+ }
37
+ return resolved === other
38
+ }
39
+
40
+ hashCode() {
41
+ return hash(List.of(this.#props.key, this.#props.origin))
42
+ }
43
+
44
+ get __reference_props__() {
45
+ return this.#props
46
+ }
47
+ }
48
+
49
+ class Handler<T extends object> implements ProxyHandler<T> {
50
+ get _props() {
51
+ return this._core.__reference_props__
52
+ }
53
+ constructor(private readonly _core: Core<T>) {}
54
+
55
+ get(target: T, prop: PropertyKey) {
56
+ const { _props, _core } = this
57
+ if (Reflect.has(_core, prop)) {
58
+ return Reflect.get(_core, prop)
59
+ }
60
+ const resource = _props.resolver.pull() as any
61
+ const result = Reflect.get(resource, prop)
62
+ if (typeof result === "function") {
63
+ return result.bind(resource)
64
+ }
65
+ return result
66
+ }
67
+ getPrototypeOf(target: T) {
68
+ const { _props } = this
69
+ return _props.class.prototype
70
+ }
71
+ has(target: T, prop: PropertyKey) {
72
+ const { _props, _core } = this
73
+ if (Reflect.has(_core, prop)) {
74
+ return true
75
+ }
76
+ const resource = _props.resolver.pull() as any
77
+ return Reflect.has(resource, prop)
78
+ }
79
+ getOwnPropertyDescriptor(_: T, p: string | symbol): PropertyDescriptor | undefined {
80
+ const { _core, _props } = this
81
+ const desc = Object.getOwnPropertyDescriptor(_core, p)
82
+ if (desc) {
83
+ desc.configurable = false
84
+ return desc
85
+ }
86
+ const resource = _props.resolver.pull() as any
87
+ const resourceDesc = Object.getOwnPropertyDescriptor(resource, p)
88
+ return resourceDesc
89
+ }
90
+ get referant() {
91
+ return this._core.__reference_props__.key.string
92
+ }
93
+ #createImmutableError(action: string) {
94
+ return new ProxyOperationError(
95
+ `Tried to ${action} a forward reference to ${this.referant}, but it cannot be modified.`
96
+ )
97
+ }
98
+
99
+ defineProperty(_: any, property: string | symbol, desc: PropertyDescriptor): boolean {
100
+ throw this.#createImmutableError(`define property ${String(property)} on`)
101
+ }
102
+ deleteProperty(_: T, p: string | symbol): boolean {
103
+ throw this.#createImmutableError(`delete property ${String(p)} from`)
104
+ }
105
+ preventExtensions(): boolean {
106
+ return true
107
+ }
108
+ isExtensible(): boolean {
109
+ return false
110
+ }
111
+ set(_: T, p: string | symbol): boolean {
112
+ throw this.#createImmutableError(`set property ${String(p)} on`)
113
+ }
114
+ ownKeys(): ArrayLike<string | symbol> {
115
+ const pulled = this._props.resolver.pull() as any
116
+ return [...Reflect.ownKeys(this._core), ...Reflect.ownKeys(pulled)]
117
+ }
118
+ setPrototypeOf(): boolean {
119
+ throw this.#createImmutableError(`set the prototype of`)
120
+ }
121
+ }
122
+ }
@@ -0,0 +1,3 @@
1
+ export { RefKey } from "../ref-key"
2
+ export { ForwardRef } from "./forward-ref"
3
+ export { Kinded, LiveRefable } from "./refable"
@@ -0,0 +1,11 @@
1
+ import type { Kind } from "../api-kind"
2
+ import type { RefKey } from "../ref-key"
3
+
4
+ export type Kinded = { kind: Kind.Kind }
5
+ export type Refable<Kind extends string, Name extends string> = {
6
+ node: {
7
+ key: RefKey<Kind, Name>
8
+ }
9
+ }
10
+ export type LiveRefable<Object extends Kinded = Kinded, Name extends string = string> = Object &
11
+ Refable<Object["kind"]["name"], Name>
@@ -0,0 +1,2 @@
1
+ export * from "./objects"
2
+ export * from "./types"
@@ -0,0 +1,99 @@
1
+ import { Map, Set } from "immutable"
2
+ import { isEmpty, merge } from "lodash"
3
+ import { string, type Parjser } from "parjs"
4
+ import { mapConst, or } from "parjs/combinators"
5
+ import { InstrumentsError } from "../error"
6
+ import type { UnitParser, UnitValue } from "../units"
7
+ import { createResourceParser } from "./parser"
8
+ import type { mt_Resource_Input_Map, mt_Resource_Unit_Map, ReqLimit } from "./types"
9
+
10
+ export class ResourcesMap<const RM extends mt_Resource_Unit_Map<RM>> {
11
+ constructor(private _map: Map<string, ReqLimit>) {}
12
+
13
+ toObject() {
14
+ const kubernetesForm = this._map.map((value, key) => {
15
+ const result = {} as any
16
+ if (value.request) {
17
+ result.requests = {
18
+ [key]: value.request.str
19
+ }
20
+ }
21
+ if (value.limit) {
22
+ result.limits = {
23
+ [key]: value.limit.str
24
+ }
25
+ }
26
+ if (!isEmpty(result)) {
27
+ return result
28
+ }
29
+ return undefined
30
+ })
31
+
32
+ return merge({}, ...kubernetesForm.values())
33
+ }
34
+ }
35
+
36
+ export class ResourcesSpec<const RM extends mt_Resource_Unit_Map<RM>> {
37
+ readonly _unitParsers: Map<string, Parjser<UnitValue | undefined>>
38
+ readonly _reqLimitParsers: Map<string, Parjser<ReqLimit>>
39
+ constructor(_unitMap: Map<string, UnitParser>) {
40
+ const questionMarkParser = string("?").pipe(mapConst(undefined))
41
+ this._unitParsers = _unitMap.map(parser => parser.parser.pipe(or(questionMarkParser)))
42
+ this._reqLimitParsers = _unitMap.map(parser => createResourceParser(parser))
43
+ }
44
+
45
+ private _parseUnitValue(resource: string, input: string): UnitValue | undefined {
46
+ const pUnitValue = this._unitParsers.get(input)
47
+ if (!pUnitValue) {
48
+ throw new InstrumentsError(`No parser found for ${input}`)
49
+ }
50
+ return pUnitValue.parse(input).value
51
+ }
52
+
53
+ private _parseReqLimit(resource: string, input: string): ReqLimit {
54
+ const pReqLimit = this._reqLimitParsers.get(resource)
55
+ if (!pReqLimit) {
56
+ throw new InstrumentsError(`No parser found for ${resource}`)
57
+ }
58
+ return pReqLimit.parse(input).value
59
+ }
60
+ __INPUT__!: mt_Resource_Input_Map<RM>
61
+
62
+ parse<const R extends mt_Resource_Input_Map<RM>>(input: R): ResourcesMap<RM> {
63
+ const allKeys = Set([...Object.keys(input), ...this._unitParsers.keys()]).toMap()
64
+ const map = allKeys.map((_, key) => {
65
+ const value = input[key as keyof R]
66
+ const pUnitValue = this._unitParsers.get(key)
67
+ if (!pUnitValue) {
68
+ throw new InstrumentsError(`No parser found for ${String(key)}`)
69
+ }
70
+ if (!value) {
71
+ throw new InstrumentsError(`No value found for ${String(key)}`)
72
+ }
73
+
74
+ if (Array.isArray(value)) {
75
+ const [req, limit] = value.map(v => {
76
+ return pUnitValue.parse(v as any).value
77
+ })
78
+ return {
79
+ request: req,
80
+ limit: limit
81
+ }
82
+ } else if (typeof value === "string") {
83
+ return this._parseReqLimit(key as string, value)
84
+ } else {
85
+ return {
86
+ request: this._parseUnitValue(key as string, value.request),
87
+ limit: this._parseUnitValue(key as string, value.limit)
88
+ }
89
+ }
90
+ })
91
+ return new ResourcesMap(map as Map<string, ReqLimit>)
92
+ }
93
+
94
+ static make<const RM extends mt_Resource_Unit_Map<RM>>(unitMap: {
95
+ [K in keyof RM]: UnitParser<RM[K]>
96
+ }) {
97
+ return new ResourcesSpec<RM>(Map(unitMap) as Map<string, UnitParser>)
98
+ }
99
+ }
@@ -0,0 +1,24 @@
1
+ import { manySepBy, map, must } from "parjs/combinators"
2
+ import type { UnitParser } from "../units/unit-parser"
3
+ import type { ReqLimit } from "./types"
4
+
5
+ export function createResourceParser(pUnitValue: UnitParser) {
6
+ return pUnitValue.parser.pipe(
7
+ manySepBy("--->", 2),
8
+ must(x => {
9
+ if (x.length !== 2) {
10
+ return {
11
+ kind: "Hard",
12
+ reason: "Expected exactly 2 values"
13
+ }
14
+ }
15
+ return true
16
+ }),
17
+ map(([a, b]) => {
18
+ return {
19
+ request: a,
20
+ limit: b
21
+ } as ReqLimit
22
+ })
23
+ )
24
+ }
@@ -0,0 +1,23 @@
1
+ import type { UnitValue } from "../units/unit-parser"
2
+ export type _limit_term<Unit extends string> = `${number}${Unit}` | "?"
3
+
4
+ export interface InputReqLimitObject<Unit extends string> {
5
+ readonly request: _limit_term<Unit>
6
+ readonly limit: _limit_term<Unit>
7
+ }
8
+ export type InputReqLimitArray<Unit extends string> = [_limit_term<Unit>, _limit_term<Unit>]
9
+ export type mt_Resource_Unit_Map<T> = Record<keyof T, string>
10
+
11
+ export type InputReqLimitString<U extends string> = `${_limit_term<U>}--->${_limit_term<U>}`
12
+
13
+ export interface ReqLimit<Unit extends string = string> {
14
+ readonly request?: UnitValue<Unit>
15
+ readonly limit?: UnitValue<Unit>
16
+ }
17
+ export type InputReqLimit<Unit extends string> =
18
+ | InputReqLimitString<Unit>
19
+ | InputReqLimitObject<Unit>
20
+ | InputReqLimitArray<Unit>
21
+ export type mt_Resource_Input_Map<ResourceUnit extends mt_Resource_Unit_Map<ResourceUnit>> = {
22
+ [K in keyof ResourceUnit]: InputReqLimit<ResourceUnit[K]>
23
+ }
@@ -0,0 +1,51 @@
1
+ import dayjs from "dayjs"
2
+ import simpleGit from "simple-git"
3
+
4
+ export interface GitTraceOptions {
5
+ cwd: string
6
+ absolute: boolean
7
+ }
8
+
9
+ export interface GitTraceProps {
10
+ commit: {
11
+ sha: string
12
+ message: string
13
+ author: string
14
+ date: string
15
+ }
16
+ }
17
+ export class GitTrace {
18
+ private constructor(readonly props: GitTraceProps) {}
19
+
20
+ get text() {
21
+ const hashPart = this.props.commit.sha.slice(0, 7)
22
+ const shortDate = dayjs(this.props.commit.date).format("YYYY-MM-DD")
23
+ const shortMessage = this.props.commit.message.split("\n")[0].slice(0, 20)
24
+ const author = this.props.commit.author
25
+ return `${hashPart} '${shortMessage}⋯' (${author}@${shortDate})`
26
+ }
27
+
28
+ static async make(options?: Partial<GitTraceOptions>) {
29
+ options = {
30
+ cwd: ".",
31
+ absolute: false,
32
+ ...options
33
+ }
34
+ const sg = simpleGit(options.cwd)
35
+ const isRepo = await sg.checkIsRepo()
36
+ if (!isRepo) {
37
+ return undefined
38
+ }
39
+ const l = await sg.log({
40
+ maxCount: 1
41
+ })
42
+ return new GitTrace({
43
+ commit: {
44
+ sha: l.latest!.hash,
45
+ message: l.latest!.message,
46
+ author: l.latest!.author_name,
47
+ date: l.latest!.date
48
+ }
49
+ })
50
+ }
51
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./git"
2
+ export * from "./trace"
3
+ export * from "./traced"
@@ -0,0 +1,31 @@
1
+ import { defaults } from "lodash"
2
+ import { relative, resolve } from "path"
3
+ import StackTracey from "stacktracey"
4
+
5
+ export interface TraceFormatOptions {
6
+ cwd: string
7
+ absolute: boolean
8
+ }
9
+
10
+ export class Trace {
11
+ readonly trace: StackTracey.Entry
12
+ constructor(trace: StackTracey) {
13
+ const findGoodFrame = (e: StackTracey.Entry) => {
14
+ return !e.native && e.file && !e.file.includes("node:")
15
+ }
16
+ this.trace = trace.filter(x => !!findGoodFrame(x)).at(0)
17
+ }
18
+
19
+ format(inOptions?: Partial<TraceFormatOptions>) {
20
+ const e = this.trace
21
+ const options = defaults(inOptions, {
22
+ cwd: ".",
23
+ absolute: false
24
+ }) as TraceFormatOptions
25
+ options.cwd = resolve(options.cwd)
26
+ const sourcePath = options.absolute ? e.file : relative(options.cwd, e.file)
27
+ const relativeToCwd = relative(options.cwd, sourcePath)
28
+ const sourceLocation = `${relativeToCwd}:${e.line}:${e.column}`
29
+ return sourceLocation
30
+ }
31
+ }
@@ -0,0 +1,4 @@
1
+ import { Embedder } from "../_embedder"
2
+ import { Trace } from "./trace"
3
+
4
+ export const TraceEmbedder = new Embedder<object, Trace>("TRACED")
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "../../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "rootDir": ".",
5
+ "noEmit": false,
6
+ "outDir": "../dist",
7
+ "tsBuildInfoFile": "../dist/src.tsbuildinfo"
8
+ },
9
+ "include": ["**/*.ts"]
10
+ }
@@ -0,0 +1,2 @@
1
+ export { UnitParser, UnitValue } from "./unit-parser"
2
+ export * from "./units"
@@ -0,0 +1,63 @@
1
+ import { Set } from "immutable"
2
+ import { int, letter, type Parjser } from "parjs"
3
+ import { many1, map, stringify, then } from "parjs/combinators"
4
+ import { InstrumentsError } from "../error"
5
+ const pUnit = letter().pipe(many1(2), stringify())
6
+
7
+ const pValueUnit = int().pipe(
8
+ then(pUnit),
9
+ map(arr => {
10
+ const [value, unit] = arr
11
+ return { value, unit }
12
+ })
13
+ )
14
+ export class UnitValue<Unit extends string = string> {
15
+ constructor(
16
+ readonly unit: Unit,
17
+ readonly value: number,
18
+ readonly type: string
19
+ ) {}
20
+
21
+ get str() {
22
+ return `${this.value}${this.unit}`
23
+ }
24
+
25
+ toString() {
26
+ return this.str
27
+ }
28
+ }
29
+
30
+ export class UnitParser<const Unit extends string = string> {
31
+ readonly parser: Parjser<UnitValue<Unit>>
32
+ constructor(
33
+ private readonly unitType: string,
34
+ private readonly _units: Set<Unit>
35
+ ) {
36
+ this.parser = pValueUnit.pipe(
37
+ map(({ value, unit }) => {
38
+ const gotType = this._units.has(unit as any)
39
+ if (!gotType) {
40
+ throw new InstrumentsError(
41
+ `Unit ${unit} is not a valid unit of type ${this.unitType}`
42
+ )
43
+ }
44
+ return new UnitValue<Unit>(unit as any, value, this.unitType)
45
+ })
46
+ )
47
+ }
48
+
49
+ parse = (input: `${number}${Unit}`) => {
50
+ const pUnitValue = this.parser
51
+ const result = pUnitValue.parse(input)
52
+ if (result.kind !== "OK") {
53
+ throw new InstrumentsError(`Failed to parse ${this.unitType}`, {
54
+ more: result.trace.toString()
55
+ })
56
+ }
57
+ return result.value
58
+ }
59
+
60
+ static make<const Unit extends string>(unitType: string, units: Set<Unit>) {
61
+ return new UnitParser<Unit>(unitType, units)
62
+ }
63
+ }
@@ -0,0 +1,53 @@
1
+ import { Set } from "immutable"
2
+ import { UnitParser } from "./unit-parser"
3
+
4
+ export type G = `${number}G`
5
+ export const G = (n: number) => `${n}G` as G
6
+
7
+ export type M = `${number}M`
8
+ export const M = (n: number) => `${n}M` as M
9
+
10
+ export type K = `${number}K`
11
+ export const K = (n: number) => `${n}K` as K
12
+
13
+ export type T = `${number}T`
14
+ export const T = (n: number) => `${n}T` as T
15
+
16
+ export type Gi = `${number}Gi`
17
+ export const Gi = (n: number) => `${n}Gi` as Gi
18
+
19
+ export type Mi = `${number}Mi`
20
+ export const Mi = (n: number) => `${n}Mi` as Mi
21
+
22
+ export type Ki = `${number}Ki`
23
+ export const Ki = (n: number) => `${n}Ki` as Ki
24
+
25
+ export type m = `${number}m`
26
+ export const m = (n: number) => `${n}m` as m
27
+
28
+ export type h = `${number}h`
29
+ export const h = (n: number) => `${n}h` as h
30
+
31
+ export type d = `${number}d`
32
+ export const d = (n: number) => `${n * 24}h` as d
33
+
34
+ export type s = `${number}s`
35
+ export const s = (n: number) => `${n}s` as s
36
+
37
+ export type ms = `${number}ms`
38
+ export const ms = (n: number) => `${n}ms` as ms
39
+
40
+ export interface UnitDefinition<Unit extends string> {}
41
+
42
+ export namespace Unit {
43
+ export type Data = M | G | T | K | Mi | Gi | Ki
44
+ export type Cpu = m
45
+ export type Time = m | h | d | s | ms
46
+ export type Any = Data | Cpu | Time
47
+
48
+ export const Cpu = UnitParser.make("cpu", Set(["m"]))
49
+
50
+ export const Data = UnitParser.make("data", Set(["M", "G", "T", "K", "Mi", "Gi", "Ki"]))
51
+
52
+ export const Time = UnitParser.make("time", Set(["m", "h", "d", "s", "ms"]))
53
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "references": [
3
+ {
4
+ "path": "./src/tsconfig.json"
5
+ },
6
+ {
7
+ "path": "./test/tsconfig.json"
8
+ }
9
+ ],
10
+ "compilerOptions": {
11
+ "composite": true,
12
+ "noEmit": true
13
+ },
14
+ "files": []
15
+ }