@amxts/core 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.
- package/LICENSE +21 -0
- package/README.md +162 -0
- package/README.ru.md +162 -0
- package/amxmodx/base/include/amxconst.inc +594 -0
- package/amxmodx/base/include/amxmisc.inc +902 -0
- package/amxmodx/base/include/amxmodx.inc +3447 -0
- package/amxmodx/base/include/amxmodx_version.inc +20 -0
- package/amxmodx/base/include/cellarray.inc +525 -0
- package/amxmodx/base/include/cellstack.inc +166 -0
- package/amxmodx/base/include/celltrie.inc +412 -0
- package/amxmodx/base/include/core.inc +202 -0
- package/amxmodx/base/include/csstats.inc +298 -0
- package/amxmodx/base/include/csstats_const.inc +29 -0
- package/amxmodx/base/include/cstrike.inc +1287 -0
- package/amxmodx/base/include/cstrike_const.inc +521 -0
- package/amxmodx/base/include/csx.inc +259 -0
- package/amxmodx/base/include/cvars.inc +547 -0
- package/amxmodx/base/include/datapack.inc +161 -0
- package/amxmodx/base/include/dbi.inc +154 -0
- package/amxmodx/base/include/dodconst.inc +152 -0
- package/amxmodx/base/include/dodfun.inc +152 -0
- package/amxmodx/base/include/dodstats.inc +69 -0
- package/amxmodx/base/include/dodx.inc +161 -0
- package/amxmodx/base/include/engine.inc +1343 -0
- package/amxmodx/base/include/engine_const.inc +310 -0
- package/amxmodx/base/include/engine_stocks.inc +248 -0
- package/amxmodx/base/include/esf.inc +70 -0
- package/amxmodx/base/include/esf_const.inc +74 -0
- package/amxmodx/base/include/fakemeta.inc +1127 -0
- package/amxmodx/base/include/fakemeta_const.inc +793 -0
- package/amxmodx/base/include/fakemeta_stocks.inc +395 -0
- package/amxmodx/base/include/fakemeta_util.inc +882 -0
- package/amxmodx/base/include/file.inc +666 -0
- package/amxmodx/base/include/float.inc +422 -0
- package/amxmodx/base/include/fun.inc +333 -0
- package/amxmodx/base/include/gameconfig.inc +93 -0
- package/amxmodx/base/include/geoip.inc +264 -0
- package/amxmodx/base/include/ham_const.inc +4352 -0
- package/amxmodx/base/include/hamsandwich.inc +439 -0
- package/amxmodx/base/include/hlsdk_const.inc +808 -0
- package/amxmodx/base/include/json.inc +777 -0
- package/amxmodx/base/include/lang.inc +119 -0
- package/amxmodx/base/include/message_const.inc +1130 -0
- package/amxmodx/base/include/message_stocks.inc +161 -0
- package/amxmodx/base/include/messages.inc +604 -0
- package/amxmodx/base/include/newmenus.inc +387 -0
- package/amxmodx/base/include/ns.inc +754 -0
- package/amxmodx/base/include/ns_const.inc +196 -0
- package/amxmodx/base/include/nvault.inc +154 -0
- package/amxmodx/base/include/regex.inc +348 -0
- package/amxmodx/base/include/sockets.inc +183 -0
- package/amxmodx/base/include/sorting.inc +107 -0
- package/amxmodx/base/include/sqlx.inc +582 -0
- package/amxmodx/base/include/string.inc +786 -0
- package/amxmodx/base/include/string_const.inc +156 -0
- package/amxmodx/base/include/string_stocks.inc +325 -0
- package/amxmodx/base/include/textparse_ini.inc +214 -0
- package/amxmodx/base/include/textparse_smc.inc +261 -0
- package/amxmodx/base/include/tfcconst.inc +84 -0
- package/amxmodx/base/include/tfcstats.inc +62 -0
- package/amxmodx/base/include/tfcx.inc +148 -0
- package/amxmodx/base/include/time.inc +118 -0
- package/amxmodx/base/include/tsconst.inc +154 -0
- package/amxmodx/base/include/tsfun.inc +198 -0
- package/amxmodx/base/include/tsstats.inc +65 -0
- package/amxmodx/base/include/tsx.inc +88 -0
- package/amxmodx/base/include/vault.inc +60 -0
- package/amxmodx/base/include/vector.inc +131 -0
- package/amxmodx/base/include/xs.inc +1570 -0
- package/as/amxts.d.ts +153 -0
- package/as/constants.ts +4730 -0
- package/as/effects.ts +1449 -0
- package/as/entities.ts +5482 -0
- package/as/events.ts +3054 -0
- package/as/facade.ts +4639 -0
- package/as/flags.ts +299 -0
- package/as/fs.ts +287 -0
- package/as/hooks.ts +18536 -0
- package/as/kit.ts +25 -0
- package/as/lib/check.ts +86 -0
- package/as/modules/http.ts +184 -0
- package/as/natives.ts +11845 -0
- package/as/os.ts +30 -0
- package/as/promise.ts +1392 -0
- package/as/promise.types.d.ts +82 -0
- package/as/remote.ts +288 -0
- package/as/tsconfig.json +22 -0
- package/as/vector.ts +65 -0
- package/bin/amxts.mjs +10 -0
- package/includes/menu_core.inc +355 -0
- package/includes/order.txt +54 -0
- package/includes/sources.json +27 -0
- package/includes/universal_config.inc +355 -0
- package/lint/oxlint-plugin.mjs +77 -0
- package/node_modules/assemblyscript/LICENSE +201 -0
- package/node_modules/assemblyscript/NOTICE +93 -0
- package/node_modules/assemblyscript/package.json +63 -0
- package/node_modules/assemblyscript/std/README.md +6 -0
- package/node_modules/assemblyscript/std/assembly/array.ts +526 -0
- package/node_modules/assemblyscript/std/assembly/arraybuffer.ts +77 -0
- package/node_modules/assemblyscript/std/assembly/atomics.ts +127 -0
- package/node_modules/assemblyscript/std/assembly/bindings/asyncify.ts +16 -0
- package/node_modules/assemblyscript/std/assembly/bindings/dom.ts +291 -0
- package/node_modules/assemblyscript/std/assembly/bindings/node.ts +6 -0
- package/node_modules/assemblyscript/std/assembly/builtins.ts +2631 -0
- package/node_modules/assemblyscript/std/assembly/compat.ts +2 -0
- package/node_modules/assemblyscript/std/assembly/console.ts +42 -0
- package/node_modules/assemblyscript/std/assembly/crypto.ts +9 -0
- package/node_modules/assemblyscript/std/assembly/dataview.ts +181 -0
- package/node_modules/assemblyscript/std/assembly/date.ts +373 -0
- package/node_modules/assemblyscript/std/assembly/diagnostics.ts +11 -0
- package/node_modules/assemblyscript/std/assembly/error.ts +44 -0
- package/node_modules/assemblyscript/std/assembly/function.ts +38 -0
- package/node_modules/assemblyscript/std/assembly/index.d.ts +2822 -0
- package/node_modules/assemblyscript/std/assembly/iterator.ts +35 -0
- package/node_modules/assemblyscript/std/assembly/map.ts +260 -0
- package/node_modules/assemblyscript/std/assembly/math.ts +3289 -0
- package/node_modules/assemblyscript/std/assembly/memory.ts +123 -0
- package/node_modules/assemblyscript/std/assembly/number.ts +388 -0
- package/node_modules/assemblyscript/std/assembly/object.ts +36 -0
- package/node_modules/assemblyscript/std/assembly/performance.ts +9 -0
- package/node_modules/assemblyscript/std/assembly/polyfills.ts +27 -0
- package/node_modules/assemblyscript/std/assembly/process.ts +50 -0
- package/node_modules/assemblyscript/std/assembly/reference.ts +48 -0
- package/node_modules/assemblyscript/std/assembly/regexp.ts +12 -0
- package/node_modules/assemblyscript/std/assembly/rt/README.md +83 -0
- package/node_modules/assemblyscript/std/assembly/rt/common.ts +81 -0
- package/node_modules/assemblyscript/std/assembly/rt/index-incremental.ts +2 -0
- package/node_modules/assemblyscript/std/assembly/rt/index-minimal.ts +2 -0
- package/node_modules/assemblyscript/std/assembly/rt/index-stub.ts +1 -0
- package/node_modules/assemblyscript/std/assembly/rt/index.d.ts +37 -0
- package/node_modules/assemblyscript/std/assembly/rt/itcms.ts +419 -0
- package/node_modules/assemblyscript/std/assembly/rt/rtrace.ts +15 -0
- package/node_modules/assemblyscript/std/assembly/rt/stub.ts +133 -0
- package/node_modules/assemblyscript/std/assembly/rt/tcms.ts +254 -0
- package/node_modules/assemblyscript/std/assembly/rt/tlsf.ts +592 -0
- package/node_modules/assemblyscript/std/assembly/rt.ts +87 -0
- package/node_modules/assemblyscript/std/assembly/set.ts +220 -0
- package/node_modules/assemblyscript/std/assembly/shared/feature.ts +65 -0
- package/node_modules/assemblyscript/std/assembly/shared/runtime.ts +11 -0
- package/node_modules/assemblyscript/std/assembly/shared/target.ts +11 -0
- package/node_modules/assemblyscript/std/assembly/shared/tsconfig.json +11 -0
- package/node_modules/assemblyscript/std/assembly/shared/typeinfo.ts +72 -0
- package/node_modules/assemblyscript/std/assembly/staticarray.ts +401 -0
- package/node_modules/assemblyscript/std/assembly/string.ts +847 -0
- package/node_modules/assemblyscript/std/assembly/symbol.ts +114 -0
- package/node_modules/assemblyscript/std/assembly/table.ts +16 -0
- package/node_modules/assemblyscript/std/assembly/tsconfig.json +6 -0
- package/node_modules/assemblyscript/std/assembly/typedarray.ts +1945 -0
- package/node_modules/assemblyscript/std/assembly/uri.ts +17 -0
- package/node_modules/assemblyscript/std/assembly/util/bytes.ts +107 -0
- package/node_modules/assemblyscript/std/assembly/util/casemap.ts +497 -0
- package/node_modules/assemblyscript/std/assembly/util/error.ts +54 -0
- package/node_modules/assemblyscript/std/assembly/util/hash.ts +117 -0
- package/node_modules/assemblyscript/std/assembly/util/math.ts +1922 -0
- package/node_modules/assemblyscript/std/assembly/util/memory.ts +290 -0
- package/node_modules/assemblyscript/std/assembly/util/number.ts +873 -0
- package/node_modules/assemblyscript/std/assembly/util/sort.ts +313 -0
- package/node_modules/assemblyscript/std/assembly/util/string.ts +1202 -0
- package/node_modules/assemblyscript/std/assembly/util/uri.ts +275 -0
- package/node_modules/assemblyscript/std/assembly/vector.ts +4 -0
- package/node_modules/assemblyscript/std/assembly.json +16 -0
- package/node_modules/assemblyscript/std/portable/index.d.ts +462 -0
- package/node_modules/assemblyscript/std/portable/index.js +416 -0
- package/node_modules/assemblyscript/std/portable.json +11 -0
- package/node_modules/assemblyscript/std/types/assembly/index.d.ts +1 -0
- package/node_modules/assemblyscript/std/types/assembly/package.json +3 -0
- package/node_modules/assemblyscript/std/types/portable/index.d.ts +1 -0
- package/node_modules/assemblyscript/std/types/portable/package.json +3 -0
- package/node_modules/assemblyscript/tsconfig-base.json +13 -0
- package/package.json +112 -0
- package/runtime/deps/assemblyscript/LICENSE +201 -0
- package/runtime/deps/assemblyscript/NOTICE +93 -0
- package/runtime/deps/assemblyscript/bin/asc.js +35 -0
- package/runtime/deps/assemblyscript/dist/asc.js +25035 -0
- package/runtime/deps/assemblyscript/dist/assemblyscript.js +337 -0
- package/runtime/deps/assemblyscript/node_modules/binaryen/LICENSE +201 -0
- package/runtime/deps/assemblyscript/node_modules/binaryen/index.js +28905 -0
- package/runtime/deps/assemblyscript/node_modules/binaryen/package.json +50 -0
- package/runtime/deps/assemblyscript/node_modules/long/LICENSE +202 -0
- package/runtime/deps/assemblyscript/node_modules/long/index.js +1581 -0
- package/runtime/deps/assemblyscript/node_modules/long/package.json +58 -0
- package/runtime/deps/assemblyscript/package.json +114 -0
- package/runtime/host/amxts.inc +85 -0
- package/runtime/licenses/AMXX-LICENSE.txt +34 -0
- package/runtime/licenses/AssemblyScript-LICENSE +201 -0
- package/runtime/licenses/AssemblyScript-NOTICE +93 -0
- package/runtime/licenses/Binaryen-LICENSE +201 -0
- package/runtime/licenses/Bun-LICENSE.md +84 -0
- package/runtime/licenses/GPL-3.0.txt +674 -0
- package/runtime/licenses/LLVM-LICENSE.TXT +279 -0
- package/runtime/licenses/README.md +25 -0
- package/runtime/licenses/WAMR-LICENSE +219 -0
- package/runtime/natives.txt +1095 -0
- package/runtime/patches/README.md +59 -0
- package/runtime/patches/assemblyscript-0.28.20-amxts.patch +14174 -0
- package/runtime/patches/wamr-2.4.5-amxts.patch +364 -0
- package/scripts/apply-docs.ts +508 -0
- package/scripts/asc.ts +27 -0
- package/scripts/auto-imports.ts +258 -0
- package/scripts/build-wasm.ts +595 -0
- package/scripts/check.ts +93 -0
- package/scripts/compile-cache.ts +179 -0
- package/scripts/compile.ts +401 -0
- package/scripts/docker-server.ts +73 -0
- package/scripts/docs/as/amxts.ts +210 -0
- package/scripts/docs/as/effects.ts +1684 -0
- package/scripts/docs/as/facade.ts +3302 -0
- package/scripts/docs/as/fs.ts +173 -0
- package/scripts/docs/as/lib/check.ts +68 -0
- package/scripts/docs/as/modules/http.ts +66 -0
- package/scripts/docs/as/os.ts +16 -0
- package/scripts/docs/as/promise.ts +132 -0
- package/scripts/docs/as/promise.types.ts +63 -0
- package/scripts/docs/as/vector.ts +48 -0
- package/scripts/docs/entities.ts +2338 -0
- package/scripts/docs/events.ts +245 -0
- package/scripts/docs/game.ts +497 -0
- package/scripts/docs/messages.ts +148 -0
- package/scripts/file-contents.ts +74 -0
- package/scripts/includes.ts +134 -0
- package/scripts/player-fields.ts +866 -0
- package/scripts/plugin-cache.ts +92 -0
- package/scripts/plugin-natives.ts +1157 -0
- package/scripts/prebuilt.ts +268 -0
- package/scripts/prepare.ts +134 -0
- package/scripts/project.ts +973 -0
- package/scripts/run.ts +28 -0
- package/scripts/shared-modules.ts +906 -0
- package/scripts/system.ts +33 -0
- package/scripts/tracked-fs.ts +115 -0
- package/scripts/typed-configs.ts +788 -0
- package/scripts/ui.ts +77 -0
- package/src/cli-api.mjs +175 -0
- package/src/parser/include-parser.ts +506 -0
- package/src/system.mjs +155 -0
- package/src/testing/compile-cache.ts +31 -0
- package/src/testing/compile.ts +222 -0
- package/src/testing/coroutines.ts +191 -0
- package/src/testing/index.ts +39 -0
- package/src/testing/kits.ts +55 -0
- package/src/testing/memory.ts +125 -0
- package/src/testing/natives.ts +925 -0
- package/src/testing/server.ts +1874 -0
- package/src/testing/setup.ts +63 -0
- package/src/testing/tables.ts +139 -0
- package/src/types.ts +51 -0
package/as/kit.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// What a module's own code needs besides the facade:
|
|
2
|
+
//
|
|
3
|
+
// import { defineModule, PawnFunction, caller } from "@amxts/core/kit";
|
|
4
|
+
//
|
|
5
|
+
// defineModule is also global (amxts.d.ts), so a module file may leave it
|
|
6
|
+
// unimported. The rest serves a module's natives for Pawn plugins: calling a
|
|
7
|
+
// Pawn plugin's public back, reading and filling its `Array:`, a menu of any
|
|
8
|
+
// length. They live in the facade - this file only gathers them - and a
|
|
9
|
+
// plugin never needs them: what a plugin does has a facade object of its own.
|
|
10
|
+
export {
|
|
11
|
+
caller,
|
|
12
|
+
cellArrayRows,
|
|
13
|
+
cellsText,
|
|
14
|
+
colorTags,
|
|
15
|
+
createCellArray,
|
|
16
|
+
defineModule,
|
|
17
|
+
destroyCellArray,
|
|
18
|
+
menuColors,
|
|
19
|
+
PawnCall,
|
|
20
|
+
PawnFunction,
|
|
21
|
+
publicFor,
|
|
22
|
+
pushCellArrayRow,
|
|
23
|
+
showMenu,
|
|
24
|
+
textCells,
|
|
25
|
+
} from "./facade";
|
package/as/lib/check.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// Checks for the api-* plugins: each one exercises a piece of the plugin API
|
|
2
|
+
// on a live server and logs what it expected beside what it got.
|
|
3
|
+
//
|
|
4
|
+
// const check = new Checks("api-async", player);
|
|
5
|
+
// check.expect(`${response.status}`, "status").toBe("200");
|
|
6
|
+
// check.expect(player.gravity, "gravity").toBeCloseTo(0.5);
|
|
7
|
+
// check.done();
|
|
8
|
+
//
|
|
9
|
+
// Written like `expect` in bun:test and vitest, the second argument being
|
|
10
|
+
// vitest's message. Every line goes to the server console with the plugin's
|
|
11
|
+
// tag, so one grep of the log answers "does this part of the API work":
|
|
12
|
+
// `[api-async] ok ...` or `[api-async] FAIL ...`. The player who ran the check
|
|
13
|
+
// gets the total in chat.
|
|
14
|
+
//
|
|
15
|
+
// A check nobody ran in the game - a suite of `bun run test:server`, started
|
|
16
|
+
// over rcon - is made without a player, and its total goes to the log alone:
|
|
17
|
+
//
|
|
18
|
+
// const check = new Checks("cvar");
|
|
19
|
+
import { Player, print } from "~/facade";
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Checks of one piece of the API on a live server. Each goes to the server
|
|
23
|
+
* console with the tag, as `[tag] ok ...` or `[tag] FAIL ...`; the player
|
|
24
|
+
* who ran them gets the total in chat.
|
|
25
|
+
*
|
|
26
|
+
* ```ts
|
|
27
|
+
* const check = new Checks("api-async", player);
|
|
28
|
+
* check.expect(player.gravity, "gravity").toBeCloseTo(0.5);
|
|
29
|
+
* check.done();
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export class Checks {
|
|
33
|
+
/** The number of checks passed so far. */
|
|
34
|
+
passed = 0;
|
|
35
|
+
/** The number of checks failed so far. */
|
|
36
|
+
failed = 0;
|
|
37
|
+
|
|
38
|
+
constructor(
|
|
39
|
+
/** The checks' tag: every log line starts with it, `[cvar]`. */
|
|
40
|
+
public tag: string,
|
|
41
|
+
/** The player who ran the checks, if any: the total is printed to him too. */
|
|
42
|
+
public player?: Player,
|
|
43
|
+
) {
|
|
44
|
+
console.log(player != null ? `[${tag}] --- run by ${player.name}` : `[${tag}] --- run`);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Starts a check of a value; `what` names it in the log: `check.expect(player.gravity, "gravity")`. */
|
|
48
|
+
expect<T>(got: T, what?: string) {
|
|
49
|
+
return new Expectation<T>(this, got, what ?? "");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Prints the total, to the log and to the player. */
|
|
53
|
+
done() {
|
|
54
|
+
const line = `[${this.tag}] ${this.passed} ok, ${this.failed} failed`;
|
|
55
|
+
console.log(line);
|
|
56
|
+
if (this.player != null) print(this.player, line);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** @hidden */
|
|
60
|
+
record(what: string, ok: boolean, got: string, expected: string) {
|
|
61
|
+
if (ok) {
|
|
62
|
+
this.passed++;
|
|
63
|
+
console.log(`[${this.tag}] ok ${what}: ${got}`);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
this.failed++;
|
|
68
|
+
console.log(`[${this.tag}] FAIL ${what}: got ${got}, expected ${expected}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** One value under check, from `check.expect(...)`: `toBe` or `toBeCloseTo` logs the result. */
|
|
73
|
+
export class Expectation<T> {
|
|
74
|
+
constructor(private checks: Checks, private got: T, private what: string) {}
|
|
75
|
+
|
|
76
|
+
/** Checks the value equals `expected`: the same number, text or boolean. */
|
|
77
|
+
toBe(expected: T) {
|
|
78
|
+
this.checks.record(this.what, this.got == expected, `${this.got}`, `${expected}`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Checks the number is within `0.001` of `expected`: a value the game stores comes back rounded (`0.5` as `0.49999`). */
|
|
82
|
+
toBeCloseTo(expected: number) {
|
|
83
|
+
const got = this.got as number;
|
|
84
|
+
this.checks.record(this.what, Math.abs(got - expected) < 0.001, `${got}`, `${expected}`);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
// fetch() over the easy_http module.
|
|
2
|
+
//
|
|
3
|
+
// An extension, not the core: easy_http is a third-party AMX Mod X module
|
|
4
|
+
// (github.com/Next21Team/AmxxEasyHttp), so a plugin imports this file itself -
|
|
5
|
+
// `import { fetch } from "~/modules/http"` - and a server without the module
|
|
6
|
+
// gets one clear line in the log instead of a missing-native error.
|
|
7
|
+
//
|
|
8
|
+
// const response = await fetch("https://example.com/");
|
|
9
|
+
// if (response.ok) console.log(response.text);
|
|
10
|
+
//
|
|
11
|
+
// fetch("https://example.com/")
|
|
12
|
+
// .then((response) => console.log(`${response.status}: ${response.text}`))
|
|
13
|
+
// .catch((error) => console.log(error.message));
|
|
14
|
+
//
|
|
15
|
+
// A real Promise (as/promise.ts): awaited in an async function, or given
|
|
16
|
+
// .then and .catch. `signal` cancels the request, as the Fetch standard's does.
|
|
17
|
+
import { CellBuffer, WideHandler } from "~/facade";
|
|
18
|
+
import {
|
|
19
|
+
LibraryExists, ezhttp_cancel_request, ezhttp_create_options, ezhttp_delete, ezhttp_get,
|
|
20
|
+
ezhttp_get_downloaded_bytes, ezhttp_get_error_code, ezhttp_get_error_message, ezhttp_get_http_code,
|
|
21
|
+
ezhttp_option_set_body, ezhttp_option_set_header, ezhttp_patch, ezhttp_post, ezhttp_put
|
|
22
|
+
} from "~/natives";
|
|
23
|
+
import { LibType_Library } from "~/constants";
|
|
24
|
+
|
|
25
|
+
/** A request's options besides its URL. Every field is optional. */
|
|
26
|
+
export class RequestInit {
|
|
27
|
+
/** The request's method, one of `"GET"` (the default), `"POST"`, `"PUT"`, `"PATCH"`, `"DELETE"`. */
|
|
28
|
+
method?: string = "GET";
|
|
29
|
+
/** The text sent with the request, such as JSON for a POST; empty by default. */
|
|
30
|
+
body?: string = "";
|
|
31
|
+
/** The request's headers, as pairs: `[["Content-Type", "application/json"]]`. */
|
|
32
|
+
headers?: string[][] = [];
|
|
33
|
+
/**
|
|
34
|
+
* A signal that cancels the request; the promise then rejects with an
|
|
35
|
+
* Error named `"AbortError"`. In an async command handler or player event,
|
|
36
|
+
* the player leaving cancels the request too.
|
|
37
|
+
*/
|
|
38
|
+
signal?: AbortSignal | null = null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The server's response to a request. */
|
|
42
|
+
export class Response {
|
|
43
|
+
constructor(
|
|
44
|
+
/** The response's HTTP status: `200`, `404`, ... */
|
|
45
|
+
public status: number,
|
|
46
|
+
/** The response's body, as text. */
|
|
47
|
+
public text: string
|
|
48
|
+
) {}
|
|
49
|
+
|
|
50
|
+
/** `true` for a 2xx status. */
|
|
51
|
+
get ok() {
|
|
52
|
+
return this.status >= 200 && this.status < 300;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Sends a request. The promise is fulfilled with the response on a later
|
|
58
|
+
* frame, and rejected when there is none - no connection, a bad URL, a
|
|
59
|
+
* timeout, no `easy_http`, or an abort. An HTTP error such as `404` is a
|
|
60
|
+
* response, as in fetch: check `response.ok`.
|
|
61
|
+
*
|
|
62
|
+
* Pawn: `ezhttp_get`, `ezhttp_post` (easy_http)
|
|
63
|
+
*/
|
|
64
|
+
export function fetch(url: string, init: RequestInit = {}): Promise<Response> {
|
|
65
|
+
const promise = __co_promise<Response>();
|
|
66
|
+
|
|
67
|
+
if (LibraryExists("easy_http", LibType_Library) == 0) {
|
|
68
|
+
console.error(`fetch(${url}): the easy_http module is not loaded - add it to modules.ini`);
|
|
69
|
+
__co_reject(promise, new Error("easy_http is not loaded"));
|
|
70
|
+
return promise;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const request = new Request(promise);
|
|
74
|
+
const guard = new __AbortGuard(request, init.signal as AbortSignal | null);
|
|
75
|
+
request.guard = guard;
|
|
76
|
+
const aborted = guard.aborted;
|
|
77
|
+
if (aborted) {
|
|
78
|
+
guard.release();
|
|
79
|
+
__co_reject(promise, aborted);
|
|
80
|
+
return promise;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
let options = 0;
|
|
84
|
+
// An optional field reads as `T | undefined`, though a field the literal
|
|
85
|
+
// leaves out holds its default: `||` makes the type plain.
|
|
86
|
+
const body = init.body || "";
|
|
87
|
+
const headers = init.headers || [];
|
|
88
|
+
|
|
89
|
+
if (body.length > 0 || headers.length > 0) {
|
|
90
|
+
options = ezhttp_create_options();
|
|
91
|
+
if (body.length > 0) ezhttp_option_set_body(options, body);
|
|
92
|
+
for (const pair of headers) ezhttp_option_set_header(options, pair[0], pair[1]);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const callback = completeName();
|
|
96
|
+
if (callback.length == 0) {
|
|
97
|
+
guard.release();
|
|
98
|
+
__co_reject(promise, new Error("no callback slot left"));
|
|
99
|
+
return promise;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const method = init.method || "GET";
|
|
103
|
+
let id = 0;
|
|
104
|
+
|
|
105
|
+
if (method == "GET") id = ezhttp_get(url, callback, options);
|
|
106
|
+
else if (method == "POST") id = ezhttp_post(url, callback, options);
|
|
107
|
+
else if (method == "PUT") id = ezhttp_put(url, callback, options);
|
|
108
|
+
else if (method == "PATCH") id = ezhttp_patch(url, callback, options);
|
|
109
|
+
else if (method == "DELETE") id = ezhttp_delete(url, callback, options);
|
|
110
|
+
else {
|
|
111
|
+
guard.release();
|
|
112
|
+
__co_reject(promise, new Error(`easy_http has no ${method}`));
|
|
113
|
+
return promise;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
request.id = id;
|
|
117
|
+
pending.set(id, request);
|
|
118
|
+
return promise;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** A request under way, and what cancels it when a signal aborts. */
|
|
122
|
+
class Request extends __AbortWatch {
|
|
123
|
+
id = 0;
|
|
124
|
+
guard: __AbortGuard | null = null;
|
|
125
|
+
|
|
126
|
+
constructor(public promise: Promise<Response>) {
|
|
127
|
+
super();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
run(reason: Error): void {
|
|
131
|
+
if (!pending.has(this.id)) return;
|
|
132
|
+
pending.delete(this.id);
|
|
133
|
+
ezhttp_cancel_request(this.id);
|
|
134
|
+
this.settled();
|
|
135
|
+
__co_reject(this.promise, reason);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
settled(): void {
|
|
139
|
+
const guard = this.guard;
|
|
140
|
+
if (guard) guard.release();
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const pending = new Map<number, Request>();
|
|
145
|
+
|
|
146
|
+
// Must match SLOT_REUSED in runtime/src/module.cpp, and SHAPE_WIDE the facade's.
|
|
147
|
+
const REUSED = 0x10000;
|
|
148
|
+
const SHAPE_WIDE = 1;
|
|
149
|
+
|
|
150
|
+
// @ts-ignore: decorator
|
|
151
|
+
@external("env", "ezhttp_get_data") declare function _getData(request: i32, buffer: i32, length: i32): i32;
|
|
152
|
+
// @ts-ignore: decorator
|
|
153
|
+
@external("env", "slot") declare function _slot(fn: i32, shape: i32, key: string, fallback: i32): i32;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* The host public that calls onComplete. Not publicFor: that one answers ""
|
|
157
|
+
* after a hot reload so a registration is not made twice, and every request
|
|
158
|
+
* names its callback afresh, so the name is needed every time.
|
|
159
|
+
*/
|
|
160
|
+
function completeName() {
|
|
161
|
+
const handler: WideHandler = onComplete;
|
|
162
|
+
const slot = _slot(handler.index, SHAPE_WIDE, "ezhttp:complete", 0);
|
|
163
|
+
if (slot < 0) return "";
|
|
164
|
+
return `__amxts_cb${slot & (REUSED - 1)}`;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// easy_http calls on_complete(request_id) once, on the frame the answer came in.
|
|
168
|
+
function onComplete(id: number, b: number, c: number, d: number) {
|
|
169
|
+
if (!pending.has(id)) return;
|
|
170
|
+
const request = pending.get(id);
|
|
171
|
+
pending.delete(id);
|
|
172
|
+
request.settled();
|
|
173
|
+
|
|
174
|
+
if (ezhttp_get_error_code(id) != 0) {
|
|
175
|
+
__co_reject(request.promise, new Error(ezhttp_get_error_message(id)));
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// The whole body, however long: the generated ezhttp_get_data stops at 255.
|
|
180
|
+
const size = ezhttp_get_downloaded_bytes(id) + 1;
|
|
181
|
+
const cells = new CellBuffer(size);
|
|
182
|
+
_getData(<i32>id, <i32>cells.address, <i32>size - 1);
|
|
183
|
+
__co_resolve(request.promise, new Response(ezhttp_get_http_code(id), cells.text()));
|
|
184
|
+
}
|