@lango-project/lango-boss 0.0.0-stage → 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 (117) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +2 -0
  3. package/README.md +111 -2
  4. package/dist/.lango/bignumber.mjs +3119 -0
  5. package/dist/.lango/localization.mjs +108 -0
  6. package/dist/.lango/runtime.mjs +725 -0
  7. package/dist/.lango/vendor/i18next.LICENSE +21 -0
  8. package/dist/.lango/vendor/i18next.mjs +2434 -0
  9. package/dist/.lango/xmldom/conventions.js +429 -0
  10. package/dist/.lango/xmldom/dom-parser.js +591 -0
  11. package/dist/.lango/xmldom/dom.js +3759 -0
  12. package/dist/.lango/xmldom/entities.js +2171 -0
  13. package/dist/.lango/xmldom/errors.js +205 -0
  14. package/dist/.lango/xmldom/grammar.js +559 -0
  15. package/dist/.lango/xmldom/index.js +41 -0
  16. package/dist/.lango/xmldom/package.json +1 -0
  17. package/dist/.lango/xmldom/sax.js +983 -0
  18. package/dist/.lango/xmldom.LICENSE.md +8 -0
  19. package/dist/boss.mjs +136 -0
  20. package/dist/boss.mjs.map +1 -0
  21. package/dist/claim_watch.mjs +99 -0
  22. package/dist/claim_watch.mjs.map +1 -0
  23. package/dist/cli.mjs +143 -0
  24. package/dist/cli.mjs.map +1 -0
  25. package/dist/configuration.mjs +58 -0
  26. package/dist/configuration.mjs.map +1 -0
  27. package/dist/dispatcher.mjs +108 -0
  28. package/dist/dispatcher.mjs.map +1 -0
  29. package/dist/driver.mjs +98 -0
  30. package/dist/driver.mjs.map +1 -0
  31. package/dist/envelope.mjs +92 -0
  32. package/dist/envelope.mjs.map +1 -0
  33. package/dist/library.mjs +139 -0
  34. package/dist/library.mjs.map +1 -0
  35. package/dist/management.mjs +360 -0
  36. package/dist/management.mjs.map +1 -0
  37. package/dist/management_transaction.mjs +26 -0
  38. package/dist/management_transaction.mjs.map +1 -0
  39. package/dist/namespace.mjs +27 -0
  40. package/dist/namespace.mjs.map +1 -0
  41. package/dist/queue.mjs +161 -0
  42. package/dist/queue.mjs.map +1 -0
  43. package/dist/reads.mjs +359 -0
  44. package/dist/reads.mjs.map +1 -0
  45. package/dist/retention.mjs +133 -0
  46. package/dist/retention.mjs.map +1 -0
  47. package/dist/schedule_sync.mjs +222 -0
  48. package/dist/schedule_sync.mjs.map +1 -0
  49. package/dist/schedule_transaction.mjs +24 -0
  50. package/dist/schedule_transaction.mjs.map +1 -0
  51. package/dist/schedules.mjs +201 -0
  52. package/dist/schedules.mjs.map +1 -0
  53. package/dist/transaction.mjs +16 -0
  54. package/dist/transaction.mjs.map +1 -0
  55. package/dist/validation.mjs +90 -0
  56. package/dist/validation.mjs.map +1 -0
  57. package/dist/worker.mjs +316 -0
  58. package/dist/worker.mjs.map +1 -0
  59. package/docs/guide.md +88 -0
  60. package/docs/management.md +93 -0
  61. package/docs/producer.md +115 -0
  62. package/docs/reads.md +107 -0
  63. package/docs/reference.md +77 -0
  64. package/docs/release.md +103 -0
  65. package/docs/retention.md +47 -0
  66. package/docs/scheduling.md +79 -0
  67. package/docs/worker.md +90 -0
  68. package/examples/reports/README.md +118 -0
  69. package/examples/reports/app.lango +133 -0
  70. package/examples/reports/deploy.xml +6 -0
  71. package/examples/reports/dist/.lango/bignumber.mjs +3119 -0
  72. package/examples/reports/dist/.lango/localization.mjs +108 -0
  73. package/examples/reports/dist/.lango/runtime.mjs +725 -0
  74. package/examples/reports/dist/.lango/vendor/i18next.LICENSE +21 -0
  75. package/examples/reports/dist/.lango/vendor/i18next.mjs +2434 -0
  76. package/examples/reports/dist/.lango/xmldom/conventions.js +429 -0
  77. package/examples/reports/dist/.lango/xmldom/dom-parser.js +591 -0
  78. package/examples/reports/dist/.lango/xmldom/dom.js +3759 -0
  79. package/examples/reports/dist/.lango/xmldom/entities.js +2171 -0
  80. package/examples/reports/dist/.lango/xmldom/errors.js +205 -0
  81. package/examples/reports/dist/.lango/xmldom/grammar.js +559 -0
  82. package/examples/reports/dist/.lango/xmldom/index.js +41 -0
  83. package/examples/reports/dist/.lango/xmldom/package.json +1 -0
  84. package/examples/reports/dist/.lango/xmldom/sax.js +983 -0
  85. package/examples/reports/dist/.lango/xmldom.LICENSE.md +8 -0
  86. package/examples/reports/dist/app.mjs +137 -0
  87. package/examples/reports/dist/app.mjs.map +1 -0
  88. package/examples/reports/dist/handlers.mjs +202 -0
  89. package/examples/reports/dist/handlers.mjs.map +1 -0
  90. package/examples/reports/dist/setup.mjs +14 -0
  91. package/examples/reports/dist/setup.mjs.map +1 -0
  92. package/examples/reports/handlers.lango +153 -0
  93. package/examples/reports/jobs.xml +5 -0
  94. package/examples/reports/package.json +23 -0
  95. package/examples/reports/routes.xml +4 -0
  96. package/examples/reports/setup.lango +17 -0
  97. package/package.json +60 -4
  98. package/src/boss.lango +92 -0
  99. package/src/claim_watch.lango +58 -0
  100. package/src/cli.lango +103 -0
  101. package/src/configuration.lango +43 -0
  102. package/src/dispatcher.lango +75 -0
  103. package/src/driver.lango +72 -0
  104. package/src/envelope.lango +56 -0
  105. package/src/library.lango +66 -0
  106. package/src/management.lango +302 -0
  107. package/src/management_transaction.lango +28 -0
  108. package/src/namespace.lango +15 -0
  109. package/src/queue.lango +115 -0
  110. package/src/reads.lango +277 -0
  111. package/src/retention.lango +107 -0
  112. package/src/schedule_sync.lango +190 -0
  113. package/src/schedule_transaction.lango +22 -0
  114. package/src/schedules.lango +165 -0
  115. package/src/transaction.lango +23 -0
  116. package/src/validation.lango +45 -0
  117. package/src/worker.lango +230 -0
@@ -0,0 +1,8 @@
1
+ Copyright 2019 - present Christopher J. Brody and other contributors, as listed in: https://github.com/xmldom/xmldom/graphs/contributors
2
+ Copyright 2012 - 2017 @jindw <jindw@xidea.org> and other contributors, as listed in: https://github.com/jindw/xmldom/graphs/contributors
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,137 @@
1
+ #!/usr/bin/env node
2
+ import { initialize } from "./setup.mjs";
3
+ import * as boss from "@lango-project/lango-boss";
4
+ import * as server from "@lango-project/lango-server";
5
+ import * as path from "node:path";
6
+ import * as crypto from "node:crypto";
7
+ import {
8
+ hostGlobal as __hostGlobal,
9
+ println as __println,
10
+ bindContexts as __bindContexts,
11
+ fromJson as __fromJson,
12
+ toJson as __toJson,
13
+ requireBoolean as __requireBoolean,
14
+ normalize as __normalize,
15
+ boundary as __boundary,
16
+ requireArity as __requireArity,
17
+ withArity as __withArity,
18
+ } from "./.lango/runtime.mjs";
19
+
20
+ export function main(args) {
21
+ __requireArity(arguments.length, [0], "main");
22
+ args = __boundary(args);
23
+ let directory = __normalize(__hostGlobal(["process"]).cwd());
24
+ if (__requireBoolean(__requireBoolean(__normalize(args.length) === 1) && __requireBoolean(__normalize(args[0]) === "setup"))) {
25
+ return __normalize(server.withDatabase(directory, initialize));
26
+ }
27
+ if (__requireBoolean(__requireBoolean(__normalize(args.length) === 1) && __requireBoolean(__normalize(args[0]) === "server"))) {
28
+ return serve(directory);
29
+ }
30
+ if (__requireBoolean(__requireBoolean(__normalize(args.length) >= 2) && __requireBoolean(__normalize(args[0]) === "operator"))) {
31
+ return operate(directory, args);
32
+ }
33
+ throw "Usage: node dist/app.mjs setup|server|operator <action> [target]";
34
+ return null;
35
+ }
36
+
37
+ function serve(directory) {
38
+ __requireArity(arguments.length, [0], "serve");
39
+ directory = __boundary(directory);
40
+ let producer = new boss.Boss(directory);
41
+ let state = { http: null, stopping: false };
42
+ let stop = __bindContexts(() => {
43
+ if (__requireBoolean(__normalize(state.stopping))) {
44
+ return null;
45
+ }
46
+ state.stopping = true;
47
+ return __normalize(state.http.close()).finally(__bindContexts(() => {
48
+ return __normalize(producer.close());
49
+ return null;
50
+ })).catch(__withArity(__bindContexts((error) => {
51
+ error = __boundary(error);
52
+ __normalize(__hostGlobal(["process"]).stderr.write(`Report server shutdown failed: ${error}\n`));
53
+ __hostGlobal(["process"]).exitCode = 1;
54
+ return null;
55
+ }), [0], "closure"));
56
+ return null;
57
+ });
58
+ return __normalize(producer.start()).then(__withArity(__bindContexts((ignored) => {
59
+ ignored = __boundary(ignored);
60
+ let builder = new server.Builder(directory);
61
+ __normalize(builder.loadDeployment());
62
+ __normalize(builder.addMiddleware("client", { module: "./dist/handlers.mjs", handler: "guard" }));
63
+ __normalize(builder.event({ name: "register", module: "./dist/handlers.mjs", handler: "register" }));
64
+ for (const handler of ["submit", "status", "whoami"]) {
65
+ __normalize(builder.endpoint({ path: `/${handler}`, module: "./dist/handlers.mjs", handler: handler, policy: "anonymous", middleware: "client" }));
66
+ }
67
+ __normalize(producer.configure(builder));
68
+ state.http = __normalize(builder.build());
69
+ return __normalize(state.http.start());
70
+ return null;
71
+ }), [0], "closure")).then(__withArity(__bindContexts((address) => {
72
+ address = __boundary(address);
73
+ __println(`Listening on ${__normalize(address.address)}:${__normalize(address.port)}`);
74
+ __normalize(__hostGlobal(["process"]).once("SIGTERM", stop));
75
+ __normalize(__hostGlobal(["process"]).once("SIGINT", stop));
76
+ return null;
77
+ }), [0], "closure")).catch(__withArity(__bindContexts((error) => {
78
+ error = __boundary(error);
79
+ return __normalize(producer.close()).finally(__bindContexts(() => {
80
+ if (__requireBoolean(__normalize(state.http) !== null)) {
81
+ return __normalize(state.http.close());
82
+ }
83
+ return null;
84
+ return null;
85
+ })).finally(__bindContexts(() => {
86
+ throw error;
87
+ return null;
88
+ }));
89
+ return null;
90
+ }), [0], "closure"));
91
+ return null;
92
+ }
93
+
94
+ function operate(directory, args) {
95
+ __requireArity(arguments.length, [0, 1], "operate");
96
+ directory = __boundary(directory);
97
+ args = __boundary(args);
98
+ let action = __normalize(args[1]);
99
+ let read = ["jobs", "queues", "schedules"];
100
+ let changes = ["job", "retry", "cancel", "pauseQueue", "resumeQueue", "pauseSchedule", "resumeSchedule"];
101
+ if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__normalize(read.includes(action)))) && __requireBoolean(!__requireBoolean(__normalize(changes.includes(action))))) || __requireBoolean(__normalize(args.length) > 3))) {
102
+ throw "Unknown operator action";
103
+ }
104
+ if (__requireBoolean(__requireBoolean(__normalize(changes.includes(action))) && __requireBoolean(__normalize(args.length) !== 3))) {
105
+ throw "Operator action requires a target";
106
+ }
107
+ let producer = new boss.Boss(directory);
108
+ let state = { executor: null };
109
+ return __normalize(producer.start()).then(__withArity(__bindContexts((ignored) => {
110
+ ignored = __boundary(ignored);
111
+ state.executor = __normalize(producer.executor({ identity: { module: "./dist/handlers.mjs", handler: "identity" }, workers: 1 }));
112
+ return __normalize(state.executor.start());
113
+ return null;
114
+ }), [0], "closure")).then(__withArity(__bindContexts((ignored) => {
115
+ ignored = __boundary(ignored);
116
+ return __normalize(state.executor.execute({ id: `operator:${__normalize(crypto.randomUUID())}`, path: "/operator", input: { action: action, target: __normalize(args[2]) }, source: { kind: "system", identity: "operator" } }));
117
+ return null;
118
+ }), [0], "closure")).then(__withArity(__bindContexts((result) => {
119
+ result = __boundary(result);
120
+ __println(__toJson(result));
121
+ return null;
122
+ }), [0], "closure")).finally(__bindContexts(() => {
123
+ if (__requireBoolean(__normalize(state.executor) !== null)) {
124
+ return __normalize(state.executor.close());
125
+ }
126
+ return null;
127
+ return null;
128
+ })).finally(__bindContexts(() => {
129
+ return __normalize(producer.close());
130
+ return null;
131
+ }));
132
+ return null;
133
+ }
134
+ const { configure } = await import("./.lango/runtime.mjs");
135
+ configure({ output: (value) => console.log(value) });
136
+ await main(process.argv.slice(2));
137
+ //# sourceMappingURL=app.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"app.mjs","sources":["app.lango"],"sourcesContent":["import js \"@lango-project/lango-boss\" as boss\nimport js \"@lango-project/lango-server\" as server\nimport js \"node:path\" as path\nimport js \"node:crypto\" as crypto\nimport js global process\nimport \"./setup\"\n initialize()\n\nexport main(args)\n\nmain(args)\n directory = process.cwd()\n if args.length == 1 and args[0] == \"setup\"\n return server/withDatabase(directory, initialize)\n if args.length == 1 and args[0] == \"server\"\n return serve(directory)\n if args.length >= 2 and args[0] == \"operator\"\n return operate(directory, args)\n throw \"Usage: node dist/app.mjs setup|server|operator <action> [target]\"\n\nserve(directory)\n producer = new boss/Boss(directory)\n state = {\n http: null\n stopping: false\n }\n stop = ()\n capture state\n if state.stopping\n return null\n state.stopping = true\n return chain state.http.close()\n finally\n capture producer\n return producer.close()\n catch(error)\n process.stderr.write(\"Report server shutdown failed: ${error}\\n\")\n process.exitCode = 1\n return chain producer.start()\n then(ignored)\n capture producer\n capture state\n capture directory\n builder = new server/Builder(directory)\n builder.loadDeployment()\n builder.addMiddleware(\"client\", {\n module: \"./dist/handlers.mjs\"\n handler: \"guard\"\n })\n builder.event({\n name: \"register\"\n module: \"./dist/handlers.mjs\"\n handler: \"register\"\n })\n for [\"submit\", \"status\", \"whoami\"] as handler\n builder.endpoint({\n path: \"/${handler}\"\n module: \"./dist/handlers.mjs\"\n handler: handler\n policy: \"anonymous\"\n middleware: \"client\"\n })\n producer.configure(builder)\n state.http = builder.build()\n return state.http.start()\n then(address)\n capture stop\n println(\"Listening on ${address.address}:${address.port}\")\n process.once(\"SIGTERM\", stop)\n process.once(\"SIGINT\", stop)\n catch(error)\n capture producer\n return chain producer.close()\n finally\n capture state\n if state.http != null\n return state.http.close()\n return null\n finally\n capture error\n throw error\n\noperate(directory, args)\n action = args[1]\n read = [\"jobs\", \"queues\", \"schedules\"]\n changes = [\"job\", \"retry\", \"cancel\", \"pauseQueue\", \"resumeQueue\", \"pauseSchedule\", \"resumeSchedule\"]\n if (!read.includes(action) and !changes.includes(action)) or args.length > 3\n throw \"Unknown operator action\"\n if changes.includes(action) and args.length != 3\n throw \"Operator action requires a target\"\n producer = new boss/Boss(directory)\n state = {\n executor: null\n }\n return chain producer.start()\n then(ignored)\n capture producer\n capture state\n state.executor = producer.executor({\n identity: {\n module: \"./dist/handlers.mjs\"\n handler: \"identity\"\n }\n workers: 1\n })\n return state.executor.start()\n then(ignored)\n capture state\n capture args\n capture action\n capture crypto\n return state.executor.execute({\n id: \"operator:${crypto/randomUUID()}\"\n path: \"/operator\"\n input: {\n action: action\n target: args[2]\n }\n source: {\n kind: \"system\"\n identity: \"operator\"\n }\n })\n then(result)\n println(to_json(result))\n finally\n capture state\n if state.executor != null\n return state.executor.close()\n return null\n finally\n capture producer\n return producer.close()\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;AAUA;;;;;;;;;;;;;;;;;AAUA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8DA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,202 @@
1
+ import * as boss from "@lango-project/lango-boss";
2
+ import {
3
+ hostGlobal as __hostGlobal,
4
+ contextGet as __contextGet,
5
+ contextSet as __contextSet,
6
+ contextRestore as __contextRestore,
7
+ isNumber as __isNumber,
8
+ isString as __isString,
9
+ isBool as __isBool,
10
+ isObject as __isObject,
11
+ isArray as __isArray,
12
+ requireBoolean as __requireBoolean,
13
+ normalize as __normalize,
14
+ finite as __finite,
15
+ call as __call,
16
+ boundary as __boundary,
17
+ startTrans as __startTrans,
18
+ commitTrans as __commitTrans,
19
+ rollbackTrans as __rollbackTrans,
20
+ transState as __transState,
21
+ query as __query,
22
+ requireArity as __requireArity,
23
+ } from "./.lango/runtime.mjs";
24
+
25
+ export function register(input) {
26
+ const __contextScope = [];
27
+ try {
28
+ __requireArity(arguments.length, [0], "register");
29
+ input = __boundary(input);
30
+ let row = __query("insert into boss_demo_clients (client_id, registered_ip, user_agent)\nvalues (:id, :ip, :agent) on conflict do nothing", ["id","ip","agent"]);
31
+ row.id = __normalize(__contextGet("token").clientId);
32
+ row.ip = __normalize(input.ip);
33
+ row.agent = __normalize(input.userAgent);
34
+ __normalize(row.execute());
35
+ __normalize(__contextGet("token").addPolicy("reports"));
36
+ return null;
37
+ return null;
38
+ } finally {
39
+ __contextRestore(__contextScope);
40
+ }
41
+ }
42
+
43
+ function client(id) {
44
+ __requireArity(arguments.length, [0], "client");
45
+ id = __boundary(id);
46
+ let query = __query("select banned, can_submit from boss_demo_clients where client_id = :id", ["id"]);
47
+ query.id = id;
48
+ __normalize(query.execute());
49
+ return __requireBoolean(__requireBoolean(__normalize(query.next())) && __requireBoolean(!__requireBoolean(__normalize(query.banned)))) && __requireBoolean(__normalize(query.can_submit));
50
+ return null;
51
+ }
52
+
53
+ export function guard(handler, path, input, represent = null) {
54
+ const __contextScope = [];
55
+ try {
56
+ __requireArity(arguments.length, [0, 1, 2], "guard");
57
+ handler = __boundary(handler);
58
+ path = __boundary(path);
59
+ input = __boundary(input);
60
+ represent = __boundary(represent);
61
+ if (__requireBoolean(!__requireBoolean(client(__normalize(__contextGet("token").clientId))))) {
62
+ __normalize(__contextGet("response").reject(403, "ClientRejected"));
63
+ }
64
+ return __call(handler, input);
65
+ return null;
66
+ } finally {
67
+ __contextRestore(__contextScope);
68
+ }
69
+ }
70
+
71
+ export function whoami(input) {
72
+ const __contextScope = [];
73
+ try {
74
+ __requireArity(arguments.length, [0], "whoami");
75
+ input = __boundary(input);
76
+ return { clientId: __normalize(__contextGet("token").clientId) };
77
+ return null;
78
+ } finally {
79
+ __contextRestore(__contextScope);
80
+ }
81
+ }
82
+
83
+ export function submit(input) {
84
+ const __contextScope = [];
85
+ try {
86
+ __requireArity(arguments.length, [0], "submit");
87
+ input = __boundary(input);
88
+ if (__requireBoolean(__requireBoolean(__requireBoolean(!__requireBoolean(__isString(__normalize(input.message)))) || __requireBoolean(input.message.length === 0)) || __requireBoolean(input.message.length > 200))) {
89
+ __normalize(__contextGet("response").reject(400, "InvalidMessage"));
90
+ }
91
+ if (__requireBoolean(__requireBoolean(__normalize(input.failOnce) !== null) && __requireBoolean(!__requireBoolean(__isBool(__normalize(input.failOnce)))))) {
92
+ __normalize(__contextGet("response").reject(400, "InvalidFailOnce"));
93
+ }
94
+ __startTrans();
95
+ let now = __normalize(__hostGlobal(["Date"]).now());
96
+ let quota = __query("update boss_demo_clients set\n submissions = case when window_started <= :cutoff then 1 else submissions + 1 end,\n window_started = case when window_started <= :cutoff then :now else window_started end\nwhere client_id = :id returning submissions", ["cutoff","now","id"]);
97
+ quota.id = __normalize(__contextGet("token").clientId);
98
+ quota.now = now;
99
+ quota.cutoff = __finite(now - 60000);
100
+ __normalize(quota.execute());
101
+ __normalize(quota.next());
102
+ if (__requireBoolean(__normalize(quota.submissions) > 3)) {
103
+ __rollbackTrans();
104
+ __normalize(__contextGet("response").reject(429, "SubmissionLimit"));
105
+ }
106
+ let id = __normalize(__contextGet("queue").send("/reports/create", { message: __normalize(input.message), failOnce: __normalize(input.failOnce) === true }));
107
+ __commitTrans();
108
+ return { jobId: id };
109
+ return null;
110
+ } finally {
111
+ __contextRestore(__contextScope);
112
+ }
113
+ }
114
+
115
+ export function status(input) {
116
+ const __contextScope = [];
117
+ try {
118
+ __requireArity(arguments.length, [0], "status");
119
+ input = __boundary(input);
120
+ return __normalize(__contextGet("queue").get(__normalize(input.jobId)));
121
+ return null;
122
+ } finally {
123
+ __contextRestore(__contextScope);
124
+ }
125
+ }
126
+
127
+ export function identity(input) {
128
+ __requireArity(arguments.length, [0], "identity");
129
+ input = __boundary(input);
130
+ let source = __normalize(input.source);
131
+ if (__requireBoolean(__normalize(source.kind) === "system")) {
132
+ if (__normalize(source.identity) === "job_user") {
133
+ return { identity: "job_user", policies: ["reports"] };
134
+ } else if (__normalize(source.identity) === "operator") {
135
+ return { identity: "operator", policies: ["boss.manage"] };
136
+ }
137
+ return null;
138
+ }
139
+ if (__requireBoolean(__requireBoolean(__requireBoolean(__normalize(source.userId) !== null) || __requireBoolean(__normalize(source.grantId) !== null)) || __requireBoolean(!__requireBoolean(client(__normalize(source.clientId)))))) {
140
+ return null;
141
+ }
142
+ return { clientId: __normalize(source.clientId), policies: ["reports"] };
143
+ return null;
144
+ }
145
+
146
+ export function createReport(input) {
147
+ const __contextScope = [];
148
+ try {
149
+ __requireArity(arguments.length, [0], "createReport");
150
+ input = __boundary(input);
151
+ if (__requireBoolean(__requireBoolean(__normalize(input.failOnce) === true) && __requireBoolean(__normalize(__contextGet("job").attempt) === 1))) {
152
+ __normalize(__contextGet("job").reject("DemoFailure"));
153
+ }
154
+ __startTrans();
155
+ let insert = __query("insert into boss_demo_reports (job_id, client_id, profile, message)\nvalues (:id, :client, :profile, :message) on conflict (job_id) do nothing", ["id","client","profile","message"]);
156
+ insert.id = __normalize(__contextGet("job").id);
157
+ insert.client = __normalize(__contextGet("token").clientId);
158
+ insert.profile = __normalize(__contextGet("token").get("identity"));
159
+ insert.message = __normalize(input.message);
160
+ __normalize(insert.execute());
161
+ let row = __query("select message, client_id, profile from boss_demo_reports where job_id = :id", ["id"]);
162
+ row.id = __normalize(__contextGet("job").id);
163
+ __normalize(row.execute());
164
+ __normalize(row.next());
165
+ let result = { message: __normalize(row.message), clientId: __normalize(row.client_id), profile: __normalize(row.profile) };
166
+ __commitTrans();
167
+ return result;
168
+ return null;
169
+ } finally {
170
+ __contextRestore(__contextScope);
171
+ }
172
+ }
173
+
174
+ export function operator(input) {
175
+ __requireArity(arguments.length, [0], "operator");
176
+ input = __boundary(input);
177
+ if (__normalize(input.action) === "jobs") {
178
+ return __normalize(boss.jobs(__normalize(input.options)));
179
+ } else if (__normalize(input.action) === "job") {
180
+ return __normalize(boss.job(__normalize(input.target)));
181
+ } else if (__normalize(input.action) === "retry") {
182
+ return __normalize(boss.retry(__normalize(input.target)));
183
+ } else if (__normalize(input.action) === "cancel") {
184
+ return __normalize(boss.cancel(__normalize(input.target)));
185
+ } else if (__normalize(input.action) === "queues") {
186
+ return __normalize(boss.queues());
187
+ } else if (__normalize(input.action) === "schedules") {
188
+ return __normalize(boss.schedules());
189
+ } else if (__normalize(input.action) === "pauseQueue") {
190
+ return __normalize(boss.pauseQueue(__normalize(input.target)));
191
+ } else if (__normalize(input.action) === "resumeQueue") {
192
+ return __normalize(boss.resumeQueue(__normalize(input.target)));
193
+ } else if (__normalize(input.action) === "pauseSchedule") {
194
+ return __normalize(boss.pauseSchedule(__normalize(input.target)));
195
+ } else if (__normalize(input.action) === "resumeSchedule") {
196
+ return __normalize(boss.resumeSchedule(__normalize(input.target)));
197
+ } else {
198
+ throw "Unknown operator action";
199
+ }
200
+ return null;
201
+ }
202
+ //# sourceMappingURL=handlers.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handlers.mjs","sources":["handlers.lango"],"sourcesContent":["import js \"@lango-project/lango-boss\" as boss\nimport js global Date\n\nexport register(input)\nexport guard(handler, path, input, represent = null)\nexport submit(input)\nexport status(input)\nexport whoami(input)\nexport identity(input)\nexport createReport(input)\nexport operator(input)\n\nregister(input)\n context token\n row = SQL\n insert into boss_demo_clients (client_id, registered_ip, user_agent)\n values (:id, :ip, :agent) on conflict do nothing\n row.id = token.clientId?\n row.ip = input.ip\n row.agent = input.userAgent\n row.execute()\n token.addPolicy(\"reports\")\n return null\n\nclient(id)\n query = SQL\n select banned, can_submit from boss_demo_clients where client_id = :id\n query.id = id\n query.execute()\n return query.next() and !query.banned and query.can_submit\n\nguard(handler, path, input, represent = null)\n context token\n context response\n if !client(token.clientId?)\n response.reject(403, \"ClientRejected\")\n return handler(input)\n\nwhoami(input)\n context token\n return {\n clientId: token.clientId?\n }\n\nsubmit(input)\n context response\n context token\n context queue\n if !is_string(input.message) or input.message.length == 0 or input.message.length > 200\n response.reject(400, \"InvalidMessage\")\n if input.failOnce != null and !is_bool(input.failOnce)\n response.reject(400, \"InvalidFailOnce\")\n start_trans()\n now = Date/now()\n quota = SQL\n update boss_demo_clients set\n submissions = case when window_started <= :cutoff then 1 else submissions + 1 end,\n window_started = case when window_started <= :cutoff then :now else window_started end\n where client_id = :id returning submissions\n quota.id = token.clientId?\n quota.now = now\n quota.cutoff = now - 60000\n quota.execute()\n quota.next()\n if quota.submissions > 3\n rollback_trans()\n response.reject(429, \"SubmissionLimit\")\n id = queue.send(\"/reports/create\", {\n message: input.message\n failOnce: input.failOnce == true\n })\n commit_trans()\n return {\n jobId: id\n }\n\nstatus(input)\n context queue\n return queue.get(input.jobId)\n\nidentity(input)\n source = input.source\n if source.kind == \"system\"\n switch source.identity\n case \"job_user\"\n return {\n identity: \"job_user\"\n policies: [\"reports\"]\n }\n case \"operator\"\n return {\n identity: \"operator\"\n policies: [\"boss.manage\"]\n }\n return null\n if source.userId != null or source.grantId != null or !client(source.clientId)\n return null\n return {\n clientId: source.clientId\n policies: [\"reports\"]\n }\n\ncreateReport(input)\n context job\n context token\n if input.failOnce == true and job.attempt? == 1\n job.reject(\"DemoFailure\")\n start_trans()\n insert = SQL\n insert into boss_demo_reports (job_id, client_id, profile, message)\n values (:id, :client, :profile, :message) on conflict (job_id) do nothing\n insert.id = job.id?\n insert.client = token.clientId?\n insert.profile = token.get(\"identity\")\n insert.message = input.message\n insert.execute()\n row = SQL\n select message, client_id, profile from boss_demo_reports where job_id = :id\n row.id = job.id?\n row.execute()\n row.next()\n result = {\n message: row.message\n clientId: row.client_id\n profile: row.profile\n }\n commit_trans()\n return result\n\noperator(input)\n switch input.action\n case \"jobs\"\n return boss/jobs(input.options)\n case \"job\"\n return boss/job(input.target)\n case \"retry\"\n return boss/retry(input.target)\n case \"cancel\"\n return boss/cancel(input.target)\n case \"queues\"\n return boss/queues()\n case \"schedules\"\n return boss/schedules()\n case \"pauseQueue\"\n return boss/pauseQueue(input.target)\n case \"resumeQueue\"\n return boss/resumeQueue(input.target)\n case \"pauseSchedule\"\n return boss/pauseSchedule(input.target)\n case \"resumeSchedule\"\n return boss/resumeSchedule(input.target)\n else\n throw \"Unknown operator action\"\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAYA;;;;;;;;;;;;;;;;;;AAYA;;;;;;;;;;AAOA;;;;;;;;;;;;;;;;;;AAOA;;;;;;;;;;;;AAMA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCA;;;;;;;;;;;;AAIA;;;;;;;;;;;;;;;;;;;AAsBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,14 @@
1
+ import {
2
+ normalize as __normalize,
3
+ query as __query,
4
+ } from "./.lango/runtime.mjs";
5
+
6
+ export function initialize() {
7
+ let clients = __query("create table if not exists boss_demo_clients (\n client_id text primary key, banned boolean not null default false,\n can_submit boolean not null default true, registered_ip text, user_agent text,\n window_started bigint not null default 0, submissions integer not null default 0\n)", []);
8
+ __normalize(clients.execute());
9
+ let reports = __query("create table if not exists boss_demo_reports (\n job_id text primary key, client_id text, profile text, message text not null,\n created_at timestamptz not null default now()\n)", []);
10
+ __normalize(reports.execute());
11
+ return null;
12
+ return null;
13
+ }
14
+ //# sourceMappingURL=setup.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setup.mjs","sources":["setup.lango"],"sourcesContent":["export initialize()\n\ninitialize()\n clients = SQL\n create table if not exists boss_demo_clients (\n client_id text primary key, banned boolean not null default false,\n can_submit boolean not null default true, registered_ip text, user_agent text,\n window_started bigint not null default 0, submissions integer not null default 0\n )\n clients.execute()\n reports = SQL\n create table if not exists boss_demo_reports (\n job_id text primary key, client_id text, profile text, message text not null,\n created_at timestamptz not null default now()\n )\n reports.execute()\n return null\n"],"names":[],"mappings":";;;;;AAEA;;;;;;;;;"}
@@ -0,0 +1,153 @@
1
+ import js "@lango-project/lango-boss" as boss
2
+ import js global Date
3
+
4
+ export register(input)
5
+ export guard(handler, path, input, represent = null)
6
+ export submit(input)
7
+ export status(input)
8
+ export whoami(input)
9
+ export identity(input)
10
+ export createReport(input)
11
+ export operator(input)
12
+
13
+ register(input)
14
+ context token
15
+ row = SQL
16
+ insert into boss_demo_clients (client_id, registered_ip, user_agent)
17
+ values (:id, :ip, :agent) on conflict do nothing
18
+ row.id = token.clientId?
19
+ row.ip = input.ip
20
+ row.agent = input.userAgent
21
+ row.execute()
22
+ token.addPolicy("reports")
23
+ return null
24
+
25
+ client(id)
26
+ query = SQL
27
+ select banned, can_submit from boss_demo_clients where client_id = :id
28
+ query.id = id
29
+ query.execute()
30
+ return query.next() and !query.banned and query.can_submit
31
+
32
+ guard(handler, path, input, represent = null)
33
+ context token
34
+ context response
35
+ if !client(token.clientId?)
36
+ response.reject(403, "ClientRejected")
37
+ return handler(input)
38
+
39
+ whoami(input)
40
+ context token
41
+ return {
42
+ clientId: token.clientId?
43
+ }
44
+
45
+ submit(input)
46
+ context response
47
+ context token
48
+ context queue
49
+ if !is_string(input.message) or input.message.length == 0 or input.message.length > 200
50
+ response.reject(400, "InvalidMessage")
51
+ if input.failOnce != null and !is_bool(input.failOnce)
52
+ response.reject(400, "InvalidFailOnce")
53
+ start_trans()
54
+ now = Date/now()
55
+ quota = SQL
56
+ update boss_demo_clients set
57
+ submissions = case when window_started <= :cutoff then 1 else submissions + 1 end,
58
+ window_started = case when window_started <= :cutoff then :now else window_started end
59
+ where client_id = :id returning submissions
60
+ quota.id = token.clientId?
61
+ quota.now = now
62
+ quota.cutoff = now - 60000
63
+ quota.execute()
64
+ quota.next()
65
+ if quota.submissions > 3
66
+ rollback_trans()
67
+ response.reject(429, "SubmissionLimit")
68
+ id = queue.send("/reports/create", {
69
+ message: input.message
70
+ failOnce: input.failOnce == true
71
+ })
72
+ commit_trans()
73
+ return {
74
+ jobId: id
75
+ }
76
+
77
+ status(input)
78
+ context queue
79
+ return queue.get(input.jobId)
80
+
81
+ identity(input)
82
+ source = input.source
83
+ if source.kind == "system"
84
+ switch source.identity
85
+ case "job_user"
86
+ return {
87
+ identity: "job_user"
88
+ policies: ["reports"]
89
+ }
90
+ case "operator"
91
+ return {
92
+ identity: "operator"
93
+ policies: ["boss.manage"]
94
+ }
95
+ return null
96
+ if source.userId != null or source.grantId != null or !client(source.clientId)
97
+ return null
98
+ return {
99
+ clientId: source.clientId
100
+ policies: ["reports"]
101
+ }
102
+
103
+ createReport(input)
104
+ context job
105
+ context token
106
+ if input.failOnce == true and job.attempt? == 1
107
+ job.reject("DemoFailure")
108
+ start_trans()
109
+ insert = SQL
110
+ insert into boss_demo_reports (job_id, client_id, profile, message)
111
+ values (:id, :client, :profile, :message) on conflict (job_id) do nothing
112
+ insert.id = job.id?
113
+ insert.client = token.clientId?
114
+ insert.profile = token.get("identity")
115
+ insert.message = input.message
116
+ insert.execute()
117
+ row = SQL
118
+ select message, client_id, profile from boss_demo_reports where job_id = :id
119
+ row.id = job.id?
120
+ row.execute()
121
+ row.next()
122
+ result = {
123
+ message: row.message
124
+ clientId: row.client_id
125
+ profile: row.profile
126
+ }
127
+ commit_trans()
128
+ return result
129
+
130
+ operator(input)
131
+ switch input.action
132
+ case "jobs"
133
+ return boss/jobs(input.options)
134
+ case "job"
135
+ return boss/job(input.target)
136
+ case "retry"
137
+ return boss/retry(input.target)
138
+ case "cancel"
139
+ return boss/cancel(input.target)
140
+ case "queues"
141
+ return boss/queues()
142
+ case "schedules"
143
+ return boss/schedules()
144
+ case "pauseQueue"
145
+ return boss/pauseQueue(input.target)
146
+ case "resumeQueue"
147
+ return boss/resumeQueue(input.target)
148
+ case "pauseSchedule"
149
+ return boss/pauseSchedule(input.target)
150
+ case "resumeSchedule"
151
+ return boss/resumeSchedule(input.target)
152
+ else
153
+ throw "Unknown operator action"
@@ -0,0 +1,5 @@
1
+ <jobs identity="job_user" revision="1" timeZone="UTC">
2
+ <job name="minute-report" path="/reports/create" cron="* * * * *" missed="once">
3
+ <data>{"message":"Scheduled report"}</data>
4
+ </job>
5
+ </jobs>
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@lango-example/boss-reports",
3
+ "version": "0.1.0",
4
+ "private": true,
5
+ "type": "module",
6
+ "exports": {
7
+ ".": "./dist/handlers.mjs"
8
+ },
9
+ "scripts": {
10
+ "build": "lango build app.lango --out dist/app.mjs --executable && lango build handlers.lango --out dist/handlers.mjs",
11
+ "setup": "node dist/app.mjs setup",
12
+ "start": "node dist/app.mjs server",
13
+ "worker": "lango-boss worker . --identity-module ./dist/handlers.mjs --identity-handler identity",
14
+ "operator": "node dist/app.mjs operator"
15
+ },
16
+ "dependencies": {
17
+ "@lango-project/lango-boss": "^0.1.0",
18
+ "@lango-project/lango-server": "^0.2.4"
19
+ },
20
+ "devDependencies": {
21
+ "@lango-project/lango-js": "^0.1.18"
22
+ }
23
+ }
@@ -0,0 +1,4 @@
1
+ <routes>
2
+ <job path="/reports/create" module="./dist/handlers.mjs" handler="createReport" queue="reports" policy="reports"/>
3
+ <job path="/operator" module="./dist/handlers.mjs" handler="operator" queue="operations" policy="boss.manage"/>
4
+ </routes>
@@ -0,0 +1,17 @@
1
+ export initialize()
2
+
3
+ initialize()
4
+ clients = SQL
5
+ create table if not exists boss_demo_clients (
6
+ client_id text primary key, banned boolean not null default false,
7
+ can_submit boolean not null default true, registered_ip text, user_agent text,
8
+ window_started bigint not null default 0, submissions integer not null default 0
9
+ )
10
+ clients.execute()
11
+ reports = SQL
12
+ create table if not exists boss_demo_reports (
13
+ job_id text primary key, client_id text, profile text, message text not null,
14
+ created_at timestamptz not null default now()
15
+ )
16
+ reports.execute()
17
+ return null