hackmud-script-manager 0.20.4-23a791c → 0.20.4-34f0749

Sign up to get free protection for your applications and to get access to all the features.
package/env.d.ts CHANGED
@@ -1,19 +1,9 @@
1
- type Replace<T, R> = Omit<T, Extract<keyof R, keyof T>> & R
1
+ type Replace<A, B> = Omit<A, keyof B> & B
2
2
  type ScriptSuccess<T = object> = { ok: true } & T
3
3
  type ScriptFailure = { ok: false, msg?: string }
4
4
  type ScriptResponse<T = object> = ScriptSuccess<T> | ScriptFailure
5
5
  type ErrorScripts = Record<string, () => ScriptFailure>
6
-
7
- type AllOptional<T> = {
8
- [K in keyof T]-?: {} extends Pick<T, K> ? true : false
9
- }[keyof T]
10
-
11
- type Scriptor<Args = unknown, Ret = unknown> = {
12
- name: string,
13
- call: AllOptional<Args> extends true
14
- ? (args?: Args) => Ret
15
- : (args: Args) => Ret
16
- }
6
+ type Scriptor<TArgs extends any[] = any[]> = { name: string, call: (...args: TArgs) => unknown }
17
7
 
18
8
  type Subscripts = Record<string, Record<string, (...args: any) => any>> & {
19
9
  accts: ErrorScripts
@@ -41,7 +31,7 @@ type UpgradeRarityString = "`0noob`" | "`1kiddie`" | "`2h4x0r`" | "`3h4rdc0r3`"
41
31
  type UpgradeRarityNumber = 0 | 1 | 2 | 3 | 4 | 5;
42
32
  type UpgradeRarity = UpgradeRarityString | UpgradeRarityNumber;
43
33
 
44
- type UpgradeCore = {
34
+ type UpgradeBase = {
45
35
  name: string
46
36
  type: "lock" | "script_space" | "chat" | "script" | "tool" | "bot_brain" | "glam"
47
37
  up_class?: -1 | 0 | 1 | 2 | 3
@@ -53,9 +43,9 @@ type UpgradeCore = {
53
43
  description: string
54
44
  }
55
45
 
56
- type Upgrade = UpgradeCore & Record<string, null | boolean | number | string>
46
+ type Upgrade = UpgradeBase & Record<string, null | boolean | number | string>
57
47
 
58
- type CLIUpgrade = Omit<UpgradeCore, `rarity`> & {
48
+ type CliUpgrade = Omit<UpgradeBase, `rarity`> & {
59
49
  [x: string]: null | boolean | number | string
60
50
  rarity: UpgradeRarityString
61
51
  }
@@ -155,7 +145,16 @@ type Fullsec = Subscripts & PlayerFullsec & {
155
145
  market: {
156
146
  /** **FULLSEC** */ browse: {
157
147
  (args:
158
- Partial<{ seller: string, listed_before: number, listed_after: number, cost: number | string } & Omit<CLIUpgrade, "rarity">>
148
+ Partial<{
149
+ seller: string | MongoQuerySelector<string>,
150
+ listed_before: number | MongoQuerySelector<number>,
151
+ listed_after: number,
152
+ cost: number | MongoQuerySelector<number> | string,
153
+ rarity: UpgradeRarityNumber | MongoQuerySelector<UpgradeRarityNumber>,
154
+ name: string | MongoQuerySelector<string>
155
+ } & Omit<{
156
+ [k in keyof CliUpgrade]: CliUpgrade[k] | MongoQuerySelector<CliUpgrade[k]>
157
+ }, "rarity">>
159
158
  ): { i: string, name: string, rarity: Upgrade["rarity"], cost: number }[] | ScriptFailure
160
159
 
161
160
  <I extends string>(args: { i: I }): {
@@ -357,7 +356,7 @@ type Fullsec = Subscripts & PlayerFullsec & {
357
356
  * const arr = [ 1, 2, 2, 3, 2 ]
358
357
  *
359
358
  * $D(uniq(arr)) // [ 1, 2, 3, 2 ] */
360
- uniq: (array: T[]) => T[]
359
+ uniq: <T>(array: T[]) => T[]
361
360
 
362
361
  /** Sorts an array of numbers or number-coercible strings in descending order. */
363
362
  u_sort_num_arr_desc: <T>(array: T[]) => T[]
@@ -424,17 +423,17 @@ type Fullsec = Subscripts & PlayerFullsec & {
424
423
  upgrades_of_owner: {
425
424
  <F extends Partial<Upgrade & { loaded: boolean }> = object>(args?: { filter?: F, full?: false }): (
426
425
  Omit<
427
- Pick<UpgradeCore, "tier" | "rarity" | "name" | "type" | "i" | "loaded">,
426
+ Pick<UpgradeBase, "tier" | "rarity" | "name" | "type" | "i" | "loaded">,
428
427
  keyof F
429
428
  > & Pick<F, "tier" | "rarity" | "name" | "type" | "i" | "loaded">
430
429
  )[] | ScriptFailure
431
430
 
432
431
  <F extends Partial<Upgrade & { loaded: boolean }> = object>(args: { filter?: F, full: true }): (
433
- Omit<UpgradeCore, keyof F> & F & Record<string, null | boolean | number | string>
432
+ Omit<UpgradeBase, keyof F> & F & Record<string, null | boolean | number | string>
434
433
  )[] | ScriptFailure
435
434
 
436
435
  <I extends number>(args: { i: I }): (
437
- Omit<UpgradeCore, "i"> & { [x: string]: null | boolean | number | string, i: I }
436
+ Omit<UpgradeBase, "i"> & { [x: string]: null | boolean | number | string, i: I }
438
437
  ) | ScriptFailure
439
438
  }
440
439
 
@@ -511,7 +510,7 @@ type Highsec = Fullsec & PlayerHighsec & {
511
510
  /** **HIGHSEC** */
512
511
  upgrades: {
513
512
  <I extends number>(args: { i: I }): (
514
- Omit<UpgradeCore, "i"> & { [x: string]: null | boolean | number | string, i: I }
513
+ Omit<UpgradeBase, "i"> & { [x: string]: null | boolean | number | string, i: I }
515
514
  ) | ScriptFailure
516
515
 
517
516
  <F extends Partial<Upgrade & { loaded: boolean }> = object>(args?: {
@@ -519,20 +518,20 @@ type Highsec = Fullsec & PlayerHighsec & {
519
518
  is_script?: true
520
519
  full?: false
521
520
  }): (
522
- Omit<Pick<UpgradeCore, "tier" | "rarity" | "name" | "type" | "i" | "loaded">, keyof F> & F &
521
+ Omit<Pick<UpgradeBase, "tier" | "rarity" | "name" | "type" | "i" | "loaded">, keyof F> & F &
523
522
  Record<string, null | boolean | number | string>
524
523
  )[] | ScriptFailure
525
524
 
526
525
  <F extends Partial<Upgrade & { loaded: boolean }> = object>(args?:
527
526
  { filter?: F, is_script?: true, full: true }
528
- ): (Omit<UpgradeCore, keyof F> & F & Record<string, null | boolean | number | string>)[] | ScriptFailure
527
+ ): (Omit<UpgradeBase, keyof F> & F & Record<string, null | boolean | number | string>)[] | ScriptFailure
529
528
 
530
529
  (args?: { filter?: Partial<Upgrade & { loaded: boolean }>, is_script: false, full?: false }):
531
530
  { msg: string, upgrades: string[] } | ScriptFailure
532
531
 
533
532
  <F extends Partial<Upgrade & { loaded: boolean }> = object>(
534
533
  args?: { filter?: F, is_script: false, full: true }
535
- ): (Omit<UpgradeCore, keyof F | `rarity`> & F & {
534
+ ): (Omit<UpgradeBase, keyof F | `rarity`> & F & {
536
535
  [x: string]: null | boolean | number | string
537
536
  rarity: UpgradeRarityString
538
537
  })[] | ScriptFailure
@@ -714,10 +713,50 @@ type Nullsec = Lowsec & PlayerNullsec & {
714
713
  }
715
714
  }
716
715
 
716
+ type MongoTypeString = "minKey" | "double" | "string" | "object" | "array" | "binData" | "undefined" | "objectId" |
717
+ "bool" | "date" | "null" | "regex" | "dbPointer" | "javascript" | "symbol" | "int" | "timestamp" | "long" | "decimal" | "maxKey";
718
+ type MongoTypeNumber = -1 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 16 | 17 | 18 | 19 | 127;
719
+
717
720
  type MongoValue = string | number | boolean | Date | MongoValue[] | { [key: string]: MongoValue } | null
718
721
 
719
722
  type MongoCommandValue = string | number | boolean | Date | MongoCommandValue[] | { [key: string]: MongoCommandValue } |
720
- null | undefined
723
+ null | undefined
724
+
725
+ /**
726
+ * Currently unused
727
+ */
728
+ type MongoLogicalSelectors<T extends MongoValue = MongoValue> = {
729
+ $not: T | MongoComparisonSelectors<T> | MongoLogicalSelectors<T>
730
+ $nor: T[]
731
+ $or: T[]
732
+ $and: T[]
733
+ }
734
+
735
+ type MongoArraySelectors<T extends Array<MongoValue> = Array<MongoValue>> = {
736
+ $all: T
737
+ $elemMatch: T
738
+ $size: number
739
+ }
740
+
741
+ type MongoComparisonSelectors<T extends MongoValue = MongoValue> = {
742
+ $eq: T
743
+ $gt: T
744
+ $gte: T
745
+ $in: T[]
746
+ $lt: T
747
+ $lte: T
748
+ $ne: T
749
+ $nin: T[]
750
+ }
751
+
752
+ type MongoElementSelectors = {
753
+ $exists: boolean
754
+ $type: MongoTypeNumber | MongoTypeString
755
+ }
756
+
757
+ type MongoQuerySelector<T extends MongoValue = MongoValue> = Partial<T extends MongoValue[] ?
758
+ (MongoArraySelectors<T> & MongoElementSelectors & MongoComparisonSelectors<T>) :
759
+ (MongoElementSelectors & MongoComparisonSelectors<T>)>
721
760
 
722
761
  type Query = { [key: string]: MongoValue | Query } & { _id?: Id, $in?: MongoValue[] }
723
762
  type Projection = Record<string, boolean | 0 | 1>
@@ -766,7 +805,7 @@ type Cursor = {
766
805
  ObjectId: () => any
767
806
  }
768
807
 
769
- type CLIContext = {
808
+ type CliContext = {
770
809
  /** The name of the user who is calling the script. */ caller: string
771
810
  /** The name of this script. */ this_script: string
772
811
  /** The number of columns in the caller’s terminal. */ cols: number
@@ -774,17 +813,23 @@ type CLIContext = {
774
813
 
775
814
  /** The name of the script that directly called this script, or null if called on the command line or as a
776
815
  * scriptor. */ calling_script: null
816
+ is_scriptor?: undefined
817
+ is_brain?: undefined
777
818
  }
778
819
 
779
- type SubscriptContext = Replace<CLIContext, {
820
+ type SubscriptContext = Replace<CliContext, {
780
821
  /** The name of the script that directly called this script, or null if called on the command line or as a scriptor.
781
822
  */
782
823
  calling_script: string
783
824
  }>
784
825
 
785
- type ScriptorContext = CLIContext & { /** Whether the script is being run as a scriptor. */ is_scriptor: true }
786
- type BrainContext = CLIContext & { /** Whether the script is being run via a bot brain. */ is_brain: true }
787
- type Context = CLIContext | SubscriptContext | ScriptorContext | BrainContext
826
+ type ScriptorContext =
827
+ Replace<CliContext, { /** Whether the script is being run as a scriptor. */ is_scriptor: true }>
828
+
829
+ type BrainContext =
830
+ Replace<CliContext, { /** Whether the script is being run via a bot brain. */ is_brain: true }>
831
+
832
+ type Context = CliContext | SubscriptContext | ScriptorContext | BrainContext
788
833
 
789
834
  /** Subscript space that can call FULLSEC scripts. */ declare const $fs: Fullsec
790
835
 
@@ -827,6 +872,8 @@ declare const $0s: typeof $ns
827
872
  * } */
828
873
  declare const $s: Nullsec
829
874
 
875
+ type ObjectId = { $oid: string }
876
+
830
877
  declare const $db: {
831
878
  /** Insert a document or documents into a collection.
832
879
  * @param documents A document or array of documents to insert into the collection. */
@@ -920,6 +967,8 @@ declare const $db: {
920
967
  signature: { hash: "Undefined Conversion", keyId: "Undefined Conversion" }
921
968
  }
922
969
  }
970
+
971
+ ObjectId: () => ObjectId
923
972
  }
924
973
 
925
974
  /** Debug Log.
@@ -956,7 +1005,7 @@ declare const $FMCL: undefined | true
956
1005
  * @example
957
1006
  * if (!$G.dbCache)
958
1007
  * $G.dbCache = $db.f({ whatever: true }).first() */
959
- declare const $G: any
1008
+ declare const $G: Record<string | symbol, any>
960
1009
 
961
1010
  /** This contains a JS timestamp (not Date) set immediately before your code begins running.
962
1011
  * @example
@@ -1005,3 +1054,14 @@ declare const _FULL_SCRIPT_NAME: string
1005
1054
  *
1006
1055
  * In rare cases where it's not known at build time, it's `-1`. */
1007
1056
  declare const _SECLEVEL: -1 | 0 | 1 | 2 | 3 | 4
1057
+
1058
+ type DeepFreeze<T> = { readonly [P in keyof T]: DeepFreeze<T[P]> }
1059
+
1060
+ /** Recursively
1061
+ * [`Object.freeze()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze)
1062
+ * an object and its properties' objects and its properties' objects and so on.
1063
+ *
1064
+ * [Official Hackmud Wiki](https://wiki.hackmud.com/scripting/extensions/deep_freeze) */
1065
+ declare const DEEP_FREEZE: <T>(value: T) => DeepFreeze<T>
1066
+
1067
+ declare const _RUN_ID: string
@@ -1,5 +1,6 @@
1
1
  import { readDirectoryWithStats } from "@samual/lib/readDirectoryWithStats"
2
2
  import { basename, resolve } from "path"
3
+ import * as PathPosix from "path/posix"
3
4
  async function generateTypeDeclaration(sourceDirectory, hackmudPath) {
4
5
  const users = new Set()
5
6
  if (hackmudPath)
@@ -29,7 +30,7 @@ async function generateTypeDeclaration(sourceDirectory, hackmudPath) {
29
30
  }
30
31
  })
31
32
  )
32
- sourceDirectory = resolve(sourceDirectory)
33
+ sourceDirectory = PathPosix.resolve(sourceDirectory)
33
34
  let o = ""
34
35
  for (const script of wildScripts) o += `type $${script}$ = typeof import("${sourceDirectory}/${script}").default\n`
35
36
  o += "\n"
package/index.js CHANGED
@@ -7,6 +7,7 @@ export { syncMacros } from "./syncMacros.js"
7
7
  export { watch } from "./watch.js"
8
8
  import "@samual/lib/readDirectoryWithStats"
9
9
  import "path"
10
+ import "path/posix"
10
11
  import "@babel/generator"
11
12
  import "@babel/parser"
12
13
  import "@babel/plugin-proposal-decorators"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hackmud-script-manager",
3
- "version": "0.20.4-23a791c",
3
+ "version": "0.20.4-34f0749",
4
4
  "description": "Script manager for game hackmud, with minification, TypeScript support, and player script type definition generation.",
5
5
  "keywords": [
6
6
  "api",
@@ -323,7 +323,7 @@ async function processScript(
323
323
  trailingComma: "none"
324
324
  })
325
325
  }
326
- code = postprocess(code, seclevel, uniqueId)
326
+ code = postprocess(code, uniqueId)
327
327
  if (includesIllegalString(code))
328
328
  throw Error(
329
329
  'you found a weird edge case where I wasn\'t able to replace illegal strings like "SC$", please report thx'
@@ -43,15 +43,6 @@ async function minify(file, { uniqueId = "00000000000", mangleNames = !1, forceQ
43
43
  )
44
44
  }
45
45
  }
46
- const hashGReferencePaths = getReferencePathsToGlobal(`$${uniqueId}$GLOBAL$`, program)
47
- if (hashGReferencePaths.length > 3) {
48
- for (const path of hashGReferencePaths) path.replaceWith(t.identifier(`_${uniqueId}_G_`))
49
- mainFunctionPath.node.body.body.unshift(
50
- t.variableDeclaration("let", [
51
- t.variableDeclarator(t.identifier(`_${uniqueId}_G_`), t.identifier(`$${uniqueId}$GLOBAL$`))
52
- ])
53
- )
54
- }
55
46
  const jsonValues = []
56
47
  let scriptBeforeJSONValueReplacement,
57
48
  comment,
@@ -61,7 +52,7 @@ async function minify(file, { uniqueId = "00000000000", mangleNames = !1, forceQ
61
52
  traverse(fileBeforeJSONValueReplacement, {
62
53
  MemberExpression({ node: memberExpression }) {
63
54
  if (!memberExpression.computed) {
64
- assert("Identifier" == memberExpression.property.type, "src/processScript/minify.ts:127:60")
55
+ assert("Identifier" == memberExpression.property.type, "src/processScript/minify.ts:115:60")
65
56
  if ("prototype" == memberExpression.property.name) {
66
57
  memberExpression.computed = !0
67
58
  memberExpression.property = t.identifier(`_${uniqueId}_PROTOTYPE_PROPERTY_`)
@@ -173,7 +164,7 @@ async function minify(file, { uniqueId = "00000000000", mangleNames = !1, forceQ
173
164
  },
174
165
  MemberExpression({ node: memberExpression }) {
175
166
  if (!memberExpression.computed) {
176
- assert("Identifier" == memberExpression.property.type, "src/processScript/minify.ts:261:62")
167
+ assert("Identifier" == memberExpression.property.type, "src/processScript/minify.ts:249:62")
177
168
  if (!(memberExpression.property.name.length < 3)) {
178
169
  memberExpression.computed = !0
179
170
  memberExpression.property = t.stringLiteral(memberExpression.property.name)
@@ -247,7 +238,7 @@ async function minify(file, { uniqueId = "00000000000", mangleNames = !1, forceQ
247
238
  })
248
239
  await Promise.all(promises)
249
240
  const functionDeclaration = file.program.body[0]
250
- assert("FunctionDeclaration" == functionDeclaration.type, "src/processScript/minify.ts:366:61")
241
+ assert("FunctionDeclaration" == functionDeclaration.type, "src/processScript/minify.ts:354:61")
251
242
  if (jsonValues.length) {
252
243
  hasComment = !0
253
244
  if (1 == jsonValues.length)
@@ -259,7 +250,10 @@ async function minify(file, { uniqueId = "00000000000", mangleNames = !1, forceQ
259
250
  t.memberExpression(
260
251
  t.taggedTemplateExpression(
261
252
  t.memberExpression(
262
- t.callExpression(t.identifier(`$${uniqueId}$SUBSCRIPT$scripts$quine$`), []),
253
+ t.callExpression(
254
+ t.identifier(`$${uniqueId}$4$SUBSCRIPT$scripts$quine$`),
255
+ []
256
+ ),
263
257
  t.identifier("split")
264
258
  ),
265
259
  t.templateLiteral([t.templateElement({ raw: "\t", cooked: "\t" }, !0)], [])
@@ -283,7 +277,7 @@ async function minify(file, { uniqueId = "00000000000", mangleNames = !1, forceQ
283
277
  t.memberExpression(
284
278
  t.taggedTemplateExpression(
285
279
  t.memberExpression(
286
- t.callExpression(t.identifier(`$${uniqueId}$SUBSCRIPT$scripts$quine$`), []),
280
+ t.callExpression(t.identifier(`$${uniqueId}$4$SUBSCRIPT$scripts$quine$`), []),
287
281
  t.identifier("split")
288
282
  ),
289
283
  t.templateLiteral([t.templateElement({ raw: "\t", cooked: "\t" }, !0)], [])
@@ -308,7 +302,7 @@ async function minify(file, { uniqueId = "00000000000", mangleNames = !1, forceQ
308
302
  t.memberExpression(
309
303
  t.taggedTemplateExpression(
310
304
  t.memberExpression(
311
- t.callExpression(t.identifier(`$${uniqueId}$SUBSCRIPT$scripts$quine$`), []),
305
+ t.callExpression(t.identifier(`$${uniqueId}$4$SUBSCRIPT$scripts$quine$`), []),
312
306
  t.identifier("split")
313
307
  ),
314
308
  t.templateLiteral([t.templateElement({ raw: "\t", cooked: "\t" }, !0)], [])
@@ -364,7 +358,7 @@ async function minify(file, { uniqueId = "00000000000", mangleNames = !1, forceQ
364
358
  )
365
359
  }
366
360
  if (1 == forceQuineCheats) return code
367
- assert(scriptBeforeJSONValueReplacement, "src/processScript/minify.ts:497:43")
361
+ assert(scriptBeforeJSONValueReplacement, "src/processScript/minify.ts:485:43")
368
362
  return (
369
363
  countHackmudCharacters(scriptBeforeJSONValueReplacement) <=
370
364
  countHackmudCharacters(code) + Number(hasComment)
@@ -380,7 +374,7 @@ function parseObjectExpression(node, o) {
380
374
  "Identifier" == property.key.type ||
381
375
  "NumericLiteral" == property.key.type ||
382
376
  "StringLiteral" == property.key.type,
383
- "src/processScript/minify.ts:519:4"
377
+ "src/processScript/minify.ts:507:4"
384
378
  )
385
379
  if ("ArrayExpression" == property.value.type) {
386
380
  const childArray = []
@@ -1 +1 @@
1
- export declare const postprocess: (code: string, seclevel: number, uniqueId: string) => string;
1
+ export declare const postprocess: (code: string, uniqueId: string) => string;
@@ -1,12 +1,12 @@
1
- const postprocess = (code, seclevel, uniqueId) =>
1
+ const postprocess = (code, uniqueId) =>
2
2
  code
3
- .replace(/^function\s*\w+\(/, "function(")
3
+ .replace(/^function\s*[\w$]+\(/, "function(")
4
4
  .replace(RegExp(`\\$${uniqueId}\\$\\\\(?:\\\\)?\\$SC_DOLLAR\\$`, "g"), "S\\C$")
5
5
  .replace(RegExp(`\\$${uniqueId}\\$\\\\(?:\\\\)?\\$DB_DOLLAR\\$`, "g"), "D\\B$")
6
6
  .replace(RegExp(`\\$${uniqueId}\\$\\\\(?:\\\\)?\\$D\\$`, "g"), "_\\_D_S")
7
7
  .replace(RegExp(`\\$${uniqueId}\\$\\\\(?:\\\\)?\\$FMCL\\$`, "g"), "_\\_FMCL_")
8
8
  .replace(RegExp(`\\$${uniqueId}\\$\\\\(?:\\\\)?\\$G\\$`, "g"), "_\\_G_")
9
- .replace(RegExp(`\\$${uniqueId}\\$SUBSCRIPT\\$(\\w+)\\$(\\w+)\\$`, "g"), `#${"nlmhf"[seclevel]}s.$1.$2`)
9
+ .replace(RegExp(`\\$${uniqueId}\\$(\\d)\\$SUBSCRIPT\\$(\\w+)\\$(\\w+)\\$`, "g"), "#$1s.$2.$3")
10
10
  .replace(RegExp(`\\$${uniqueId}\\$DEBUG\\$`, "g"), "#D")
11
11
  .replace(RegExp(`\\$${uniqueId}\\$FMCL\\$`, "g"), "#FMCL")
12
12
  .replace(RegExp(`\\$${uniqueId}\\$GLOBAL\\$`, "g"), "#G")
@@ -98,8 +98,10 @@ async function preprocess(code, { uniqueId = "00000000000" } = {}) {
98
98
  t.stringLiteral(resolve("proxy-polyfill/src/proxy.js", import.meta.url).slice(7))
99
99
  )
100
100
  )
101
- return 1 == program.node.body.length && "FunctionDeclaration" == program.node.body[0].type ?
102
- { code: "export default " + generate(file).code }
103
- : { code: generate(file).code }
101
+ if (1 == program.node.body.length && "FunctionDeclaration" == program.node.body[0].type)
102
+ throw Error(
103
+ "Scripts that only contain a single function declaration are no longer supported.\nPrefix the function declaration with `export default`."
104
+ )
105
+ return { code: generate(file).code }
104
106
  }
105
107
  export { preprocess }