@nocobase/plugin-flow-engine 2.0.0-alpha.2

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 (125) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +29 -0
  3. package/build.config.ts +22 -0
  4. package/client.d.ts +2 -0
  5. package/client.js +1 -0
  6. package/dist/client/index.d.ts +15 -0
  7. package/dist/client/index.js +10 -0
  8. package/dist/externalVersion.js +21 -0
  9. package/dist/index.d.ts +10 -0
  10. package/dist/index.js +48 -0
  11. package/dist/locale/en-US.json +61 -0
  12. package/dist/locale/index.d.ts +141 -0
  13. package/dist/locale/index.js +79 -0
  14. package/dist/locale/zh-CN.json +61 -0
  15. package/dist/node_modules/ses/LICENSE +201 -0
  16. package/dist/node_modules/ses/LICENSE-aura +16 -0
  17. package/dist/node_modules/ses/LICENSE-caja +13 -0
  18. package/dist/node_modules/ses/LICENSE-corejs +19 -0
  19. package/dist/node_modules/ses/LICENSE-v8 +9 -0
  20. package/dist/node_modules/ses/assert-shim.js +1 -0
  21. package/dist/node_modules/ses/compartment-shim.js +1 -0
  22. package/dist/node_modules/ses/console-shim.js +1 -0
  23. package/dist/node_modules/ses/dist/lockdown.cjs +13912 -0
  24. package/dist/node_modules/ses/dist/lockdown.umd.js +13912 -0
  25. package/dist/node_modules/ses/dist/lockdown.umd.min.js +1 -0
  26. package/dist/node_modules/ses/dist/ses-hermes.cjs +13912 -0
  27. package/dist/node_modules/ses/dist/ses.cjs +1 -0
  28. package/dist/node_modules/ses/dist/ses.umd.js +13912 -0
  29. package/dist/node_modules/ses/dist/ses.umd.min.js +1 -0
  30. package/dist/node_modules/ses/dist/types.d.cts +606 -0
  31. package/dist/node_modules/ses/index.js +18 -0
  32. package/dist/node_modules/ses/lockdown-shim.js +1 -0
  33. package/dist/node_modules/ses/lockdown.js +1 -0
  34. package/dist/node_modules/ses/package.json +1 -0
  35. package/dist/node_modules/ses/src/assert-shim.js +4 -0
  36. package/dist/node_modules/ses/src/assert-sloppy-mode.js +11 -0
  37. package/dist/node_modules/ses/src/cauterize-property.js +69 -0
  38. package/dist/node_modules/ses/src/commons.js +425 -0
  39. package/dist/node_modules/ses/src/compartment-evaluate.js +93 -0
  40. package/dist/node_modules/ses/src/compartment-shim.js +22 -0
  41. package/dist/node_modules/ses/src/compartment.js +477 -0
  42. package/dist/node_modules/ses/src/console-shim.js +50 -0
  43. package/dist/node_modules/ses/src/enable-property-overrides.js +211 -0
  44. package/dist/node_modules/ses/src/enablements.js +244 -0
  45. package/dist/node_modules/ses/src/error/assert.js +584 -0
  46. package/dist/node_modules/ses/src/error/console.js +541 -0
  47. package/dist/node_modules/ses/src/error/fatal-assert.js +54 -0
  48. package/dist/node_modules/ses/src/error/internal-types.js +98 -0
  49. package/dist/node_modules/ses/src/error/note-log-args.js +77 -0
  50. package/dist/node_modules/ses/src/error/stringify-utils.js +195 -0
  51. package/dist/node_modules/ses/src/error/tame-console.js +197 -0
  52. package/dist/node_modules/ses/src/error/tame-error-constructor.js +284 -0
  53. package/dist/node_modules/ses/src/error/tame-v8-error-constructor.js +386 -0
  54. package/dist/node_modules/ses/src/error/types.js +59 -0
  55. package/dist/node_modules/ses/src/error/unhandled-rejection.js +122 -0
  56. package/dist/node_modules/ses/src/eval-scope.js +89 -0
  57. package/dist/node_modules/ses/src/get-anonymous-intrinsics.js +181 -0
  58. package/dist/node_modules/ses/src/get-source-url.js +50 -0
  59. package/dist/node_modules/ses/src/global-object.js +175 -0
  60. package/dist/node_modules/ses/src/intrinsics.js +192 -0
  61. package/dist/node_modules/ses/src/lockdown-shim.js +37 -0
  62. package/dist/node_modules/ses/src/lockdown.js +558 -0
  63. package/dist/node_modules/ses/src/make-eval-function.js +28 -0
  64. package/dist/node_modules/ses/src/make-evaluate.js +110 -0
  65. package/dist/node_modules/ses/src/make-function-constructor.js +79 -0
  66. package/dist/node_modules/ses/src/make-hardener.js +275 -0
  67. package/dist/node_modules/ses/src/make-safe-evaluator.js +112 -0
  68. package/dist/node_modules/ses/src/module-instance.js +497 -0
  69. package/dist/node_modules/ses/src/module-link.js +159 -0
  70. package/dist/node_modules/ses/src/module-load.js +719 -0
  71. package/dist/node_modules/ses/src/module-proxy.js +200 -0
  72. package/dist/node_modules/ses/src/permits-intrinsics.js +291 -0
  73. package/dist/node_modules/ses/src/permits.js +1761 -0
  74. package/dist/node_modules/ses/src/reporting-types.d.ts +13 -0
  75. package/dist/node_modules/ses/src/reporting.js +105 -0
  76. package/dist/node_modules/ses/src/scope-constants.js +180 -0
  77. package/dist/node_modules/ses/src/shim-arraybuffer-transfer.js +85 -0
  78. package/dist/node_modules/ses/src/sloppy-globals-scope-terminator.js +61 -0
  79. package/dist/node_modules/ses/src/strict-scope-terminator.js +99 -0
  80. package/dist/node_modules/ses/src/tame-date-constructor.js +127 -0
  81. package/dist/node_modules/ses/src/tame-domains.js +41 -0
  82. package/dist/node_modules/ses/src/tame-faux-data-properties.js +210 -0
  83. package/dist/node_modules/ses/src/tame-function-constructors.js +140 -0
  84. package/dist/node_modules/ses/src/tame-function-tostring.js +50 -0
  85. package/dist/node_modules/ses/src/tame-harden.js +29 -0
  86. package/dist/node_modules/ses/src/tame-locale-methods.js +78 -0
  87. package/dist/node_modules/ses/src/tame-math-object.js +41 -0
  88. package/dist/node_modules/ses/src/tame-module-source.js +51 -0
  89. package/dist/node_modules/ses/src/tame-regenerator-runtime.js +29 -0
  90. package/dist/node_modules/ses/src/tame-regexp-constructor.js +65 -0
  91. package/dist/node_modules/ses/src/tame-symbol-constructor.js +64 -0
  92. package/dist/node_modules/ses/src/transforms.js +267 -0
  93. package/dist/node_modules/ses/tools.js +25 -0
  94. package/dist/node_modules/ses/types.d.ts +606 -0
  95. package/dist/server/actions/ui-schema-action.d.ts +27 -0
  96. package/dist/server/actions/ui-schema-action.js +118 -0
  97. package/dist/server/collections/flowModelTreePath.d.ts +11 -0
  98. package/dist/server/collections/flowModelTreePath.js +74 -0
  99. package/dist/server/collections/flowModels.d.ts +11 -0
  100. package/dist/server/collections/flowModels.js +57 -0
  101. package/dist/server/collections/flowsql.d.ts +10 -0
  102. package/dist/server/collections/flowsql.js +51 -0
  103. package/dist/server/dao/ui_schema_node_dao.d.ts +26 -0
  104. package/dist/server/dao/ui_schema_node_dao.js +24 -0
  105. package/dist/server/helper.d.ts +8 -0
  106. package/dist/server/helper.js +9 -0
  107. package/dist/server/index.d.ts +9 -0
  108. package/dist/server/index.js +42 -0
  109. package/dist/server/model.d.ts +12 -0
  110. package/dist/server/model.js +38 -0
  111. package/dist/server/plugin.d.ts +26 -0
  112. package/dist/server/plugin.js +270 -0
  113. package/dist/server/repository.d.ts +116 -0
  114. package/dist/server/repository.js +1209 -0
  115. package/dist/server/server.d.ts +16 -0
  116. package/dist/server/server.js +198 -0
  117. package/dist/server/template/contexts.d.ts +73 -0
  118. package/dist/server/template/contexts.js +233 -0
  119. package/dist/server/template/resolver.d.ts +30 -0
  120. package/dist/server/template/resolver.js +225 -0
  121. package/dist/server/variables/registry.d.ts +42 -0
  122. package/dist/server/variables/registry.js +299 -0
  123. package/package.json +28 -0
  124. package/server.d.ts +2 -0
  125. package/server.js +1 -0
@@ -0,0 +1,13 @@
1
+ /* eslint-disable no-restricted-globals */
2
+
3
+ export type Reporter = {
4
+ warn: (...message: Array<any>) => void;
5
+ error: (...message: Array<string>) => void;
6
+ };
7
+
8
+ export type GroupReporter = Reporter & {
9
+ groupCollapsed?: (label: string) => void;
10
+ groupEnd?: () => void;
11
+ };
12
+
13
+ // Console implements GroupReporter
@@ -0,0 +1,105 @@
1
+ import { functionBind, globalThis } from './commons.js';
2
+ import { assert } from './error/assert.js';
3
+
4
+ /**
5
+ * @import {Reporter, GroupReporter} from './reporting-types.js'
6
+ */
7
+
8
+ /**
9
+ * Creates a suitable reporter for internal errors and warnings out of the
10
+ * Node.js console.error to ensure all messages to go stderr, including the
11
+ * group label.
12
+ * Accounts for the extra space introduced by console.error as a delimiter
13
+ * between the indent and subsequent arguments.
14
+ *
15
+ * @param {(...message: Array<any>) => void} print
16
+ */
17
+ const makeReportPrinter = print => {
18
+ let indent = false;
19
+ /** @param {Array<any>} args */
20
+ const printIndent = (...args) => {
21
+ if (indent) {
22
+ print(' ', ...args);
23
+ } else {
24
+ print(...args);
25
+ }
26
+ };
27
+ return /** @type {GroupReporter} */ ({
28
+ warn(...args) {
29
+ printIndent(...args);
30
+ },
31
+ error(...args) {
32
+ printIndent(...args);
33
+ },
34
+ groupCollapsed(...args) {
35
+ assert(!indent);
36
+ print(...args);
37
+ indent = true;
38
+ },
39
+ groupEnd() {
40
+ indent = false;
41
+ },
42
+ });
43
+ };
44
+
45
+ const mute = () => {};
46
+
47
+ /**
48
+ * @param {"platform" | "console" | "none"} reporting
49
+ */
50
+ export const chooseReporter = reporting => {
51
+ if (reporting === 'none') {
52
+ return makeReportPrinter(mute);
53
+ }
54
+ if (
55
+ reporting === 'console' ||
56
+ globalThis.window === globalThis ||
57
+ globalThis.importScripts !== undefined
58
+ ) {
59
+ return console;
60
+ }
61
+ if (globalThis.console !== undefined) {
62
+ // On Node.js, we send all feedback to stderr, regardless of purported level.
63
+ const console = globalThis.console;
64
+ const error = functionBind(console.error, console);
65
+ return makeReportPrinter(error);
66
+ }
67
+ if (globalThis.print !== undefined) {
68
+ return makeReportPrinter(globalThis.print);
69
+ }
70
+ return makeReportPrinter(mute);
71
+ };
72
+
73
+ /**
74
+ * @param {string} groupLabel
75
+ * @param {GroupReporter} console
76
+ * @param {(internalConsole: Reporter) => void} callback
77
+ */
78
+ export const reportInGroup = (groupLabel, console, callback) => {
79
+ const { warn, error, groupCollapsed, groupEnd } = console;
80
+ const grouping = groupCollapsed && groupEnd;
81
+ let groupStarted = false;
82
+ try {
83
+ return callback({
84
+ warn(...args) {
85
+ if (grouping && !groupStarted) {
86
+ groupCollapsed(groupLabel);
87
+ groupStarted = true;
88
+ }
89
+ warn(...args);
90
+ },
91
+ error(...args) {
92
+ if (grouping && !groupStarted) {
93
+ groupCollapsed(groupLabel);
94
+ groupStarted = true;
95
+ }
96
+ error(...args);
97
+ },
98
+ });
99
+ } finally {
100
+ if (grouping && groupStarted) {
101
+ groupEnd();
102
+ groupStarted = false;
103
+ }
104
+ }
105
+ };
@@ -0,0 +1,180 @@
1
+ import {
2
+ arrayFilter,
3
+ arrayIncludes,
4
+ getOwnPropertyDescriptor,
5
+ getOwnPropertyNames,
6
+ hasOwn,
7
+ regexpTest,
8
+ } from './commons.js';
9
+
10
+ /**
11
+ * keywords
12
+ * In JavaScript you cannot use these reserved words as variables.
13
+ * See 11.6.1 Identifier Names
14
+ */
15
+ const keywords = [
16
+ // 11.6.2.1 Keywords
17
+ 'await',
18
+ 'break',
19
+ 'case',
20
+ 'catch',
21
+ 'class',
22
+ 'const',
23
+ 'continue',
24
+ 'debugger',
25
+ 'default',
26
+ 'delete',
27
+ 'do',
28
+ 'else',
29
+ 'export',
30
+ 'extends',
31
+ 'finally',
32
+ 'for',
33
+ 'function',
34
+ 'if',
35
+ 'import',
36
+ 'in',
37
+ 'instanceof',
38
+ 'new',
39
+ 'return',
40
+ 'super',
41
+ 'switch',
42
+ 'this',
43
+ 'throw',
44
+ 'try',
45
+ 'typeof',
46
+ 'var',
47
+ 'void',
48
+ 'while',
49
+ 'with',
50
+ 'yield',
51
+
52
+ // Also reserved when parsing strict mode code
53
+ 'let',
54
+ 'static',
55
+
56
+ // 11.6.2.2 Future Reserved Words
57
+ 'enum',
58
+
59
+ // Also reserved when parsing strict mode code
60
+ 'implements',
61
+ 'package',
62
+ 'protected',
63
+ 'interface',
64
+ 'private',
65
+ 'public',
66
+
67
+ // Reserved but not mentioned in specs
68
+ 'await',
69
+
70
+ 'null',
71
+ 'true',
72
+ 'false',
73
+
74
+ 'this',
75
+ 'arguments',
76
+ ];
77
+
78
+ /**
79
+ * identifierPattern
80
+ * Simplified validation of identifier names: may only contain alphanumeric
81
+ * characters (or "$" or "_"), and may not start with a digit. This is safe
82
+ * and does not reduces the compatibility of the shim. The motivation for
83
+ * this limitation was to decrease the complexity of the implementation,
84
+ * and to maintain a resonable level of performance.
85
+ * Note: \w is equivalent [a-zA-Z_0-9]
86
+ * See 11.6.1 Identifier Names
87
+ */
88
+ const identifierPattern = /^[a-zA-Z_$][\w$]*$/;
89
+
90
+ /**
91
+ * isValidIdentifierName()
92
+ * What variable names might it bring into scope? These include all
93
+ * property names which can be variable names, including the names
94
+ * of inherited properties. It excludes symbols and names which are
95
+ * keywords. We drop symbols safely. Currently, this shim refuses
96
+ * service if any of the names are keywords or keyword-like. This is
97
+ * safe and only prevent performance optimization.
98
+ *
99
+ * @param {string} name
100
+ */
101
+ export const isValidIdentifierName = name => {
102
+ // Ensure we have a valid identifier. We use regexpTest rather than
103
+ // /../.test() to guard against the case where RegExp has been poisoned.
104
+ return (
105
+ name !== 'eval' &&
106
+ !arrayIncludes(keywords, name) &&
107
+ regexpTest(identifierPattern, name)
108
+ );
109
+ };
110
+
111
+ /*
112
+ * isImmutableDataProperty
113
+ */
114
+
115
+ function isImmutableDataProperty(obj, name) {
116
+ const desc = getOwnPropertyDescriptor(obj, name);
117
+ return (
118
+ desc &&
119
+ //
120
+ // The getters will not have .writable, don't let the falsyness of
121
+ // 'undefined' trick us: test with === false, not ! . However descriptors
122
+ // inherit from the (potentially poisoned) global object, so we might see
123
+ // extra properties which weren't really there. Accessor properties have
124
+ // 'get/set/enumerable/configurable', while data properties have
125
+ // 'value/writable/enumerable/configurable'.
126
+ desc.configurable === false &&
127
+ desc.writable === false &&
128
+ //
129
+ // Checks for data properties because they're the only ones we can
130
+ // optimize (accessors are most likely non-constant). Descriptors can't
131
+ // can't have accessors and value properties at the same time, therefore
132
+ // this check is sufficient. Using explicit own property deal with the
133
+ // case where Object.prototype has been poisoned.
134
+ hasOwn(desc, 'value')
135
+ );
136
+ }
137
+
138
+ /**
139
+ * getScopeConstants()
140
+ * What variable names might it bring into scope? These include all
141
+ * property names which can be variable names, including the names
142
+ * of inherited properties. It excludes symbols and names which are
143
+ * keywords. We drop symbols safely. Currently, this shim refuses
144
+ * service if any of the names are keywords or keyword-like. This is
145
+ * safe and only prevent performance optimization.
146
+ *
147
+ * @param {object} globalObject
148
+ * @param {object} moduleLexicals
149
+ */
150
+ export const getScopeConstants = (globalObject, moduleLexicals = {}) => {
151
+ // getOwnPropertyNames() does ignore Symbols so we don't need to
152
+ // filter them out.
153
+ const globalObjectNames = getOwnPropertyNames(globalObject);
154
+ const moduleLexicalNames = getOwnPropertyNames(moduleLexicals);
155
+
156
+ // Collect all valid & immutable identifiers from the endowments.
157
+ const moduleLexicalConstants = arrayFilter(
158
+ moduleLexicalNames,
159
+ name =>
160
+ isValidIdentifierName(name) &&
161
+ isImmutableDataProperty(moduleLexicals, name),
162
+ );
163
+
164
+ // Collect all valid & immutable identifiers from the global that
165
+ // are also not present in the endowments (immutable or not).
166
+ const globalObjectConstants = arrayFilter(
167
+ globalObjectNames,
168
+ name =>
169
+ // Can't define a constant: it would prevent a
170
+ // lookup on the endowments.
171
+ !arrayIncludes(moduleLexicalNames, name) &&
172
+ isValidIdentifierName(name) &&
173
+ isImmutableDataProperty(globalObject, name),
174
+ );
175
+
176
+ return {
177
+ globalObjectConstants,
178
+ moduleLexicalConstants,
179
+ };
180
+ };
@@ -0,0 +1,85 @@
1
+ import {
2
+ ArrayBuffer,
3
+ arrayBufferPrototype,
4
+ arrayBufferSlice,
5
+ arrayBufferGetByteLength,
6
+ Uint8Array,
7
+ typedArraySet,
8
+ globalThis,
9
+ TypeError,
10
+ defineProperty,
11
+ } from './commons.js';
12
+
13
+ export const shimArrayBufferTransfer = () => {
14
+ if (typeof arrayBufferPrototype.transfer === 'function') {
15
+ // Assume already exists so does not need to be shimmed.
16
+ // Such conditional shimming is ok in this case since ArrayBuffer.p.transfer
17
+ // is already officially part of JS.
18
+ //
19
+ // Empty object because this shim has nothing for `addIntrinsics` to add.
20
+ return {};
21
+ }
22
+ const clone = globalThis.structuredClone;
23
+ if (typeof clone !== 'function') {
24
+ // On a platform with neither `Array.prototype.transfer`
25
+ // nor `structuredClone`, this shim does nothing.
26
+ // For example, Node <= 16 has neither.
27
+ //
28
+ // Empty object because this shim has nothing for `addIntrinsics` to add.
29
+ return {};
30
+ // TODO Rather than doing nothing, should the endo ses-shim throw
31
+ // in this case?
32
+ // throw TypeError(
33
+ // `Can only shim missing ArrayBuffer.prototype.transfer on a platform with "structuredClone"`,
34
+ // );
35
+ // For example, endo no longer supports Node <= 16. All browsers have
36
+ // `structuredClone`. XS has `Array.prototype.transfer`. Are there still
37
+ // any platforms without both that Endo should still support?
38
+ // What about Hermes?
39
+ }
40
+
41
+ /**
42
+ * @type {ThisType<ArrayBuffer>}
43
+ */
44
+ const methods = {
45
+ /**
46
+ * @param {number} [newLength]
47
+ */
48
+ transfer(newLength = undefined) {
49
+ // Using this builtin getter also ensures that `this` is a genuine
50
+ // ArrayBuffer.
51
+ const oldLength = arrayBufferGetByteLength(this);
52
+ if (newLength === undefined || newLength === oldLength) {
53
+ return clone(this, { transfer: [this] });
54
+ }
55
+ if (typeof newLength !== 'number') {
56
+ throw TypeError(`transfer newLength if provided must be a number`);
57
+ }
58
+ if (newLength > oldLength) {
59
+ const result = new ArrayBuffer(newLength);
60
+ const taOld = new Uint8Array(this);
61
+ const taNew = new Uint8Array(result);
62
+ typedArraySet(taNew, taOld);
63
+ // Using clone only to detach, and only after the copy succeeds
64
+ clone(this, { transfer: [this] });
65
+ return result;
66
+ } else {
67
+ const result = arrayBufferSlice(this, 0, newLength);
68
+ // Using clone only to detach, and only after the slice succeeds
69
+ clone(this, { transfer: [this] });
70
+ return result;
71
+ }
72
+ },
73
+ };
74
+
75
+ defineProperty(arrayBufferPrototype, 'transfer', {
76
+ // @ts-expect-error
77
+ value: methods.transfer,
78
+ writable: true,
79
+ enumerable: false,
80
+ configurable: true,
81
+ });
82
+
83
+ // Empty object because this shim has nothing for `addIntrinsics` to add.
84
+ return {};
85
+ };
@@ -0,0 +1,61 @@
1
+ import {
2
+ Proxy,
3
+ create,
4
+ freeze,
5
+ getOwnPropertyDescriptors,
6
+ reflectSet,
7
+ } from './commons.js';
8
+ import {
9
+ strictScopeTerminatorHandler,
10
+ alwaysThrowHandler,
11
+ } from './strict-scope-terminator.js';
12
+
13
+ /**
14
+ * `freeze` but not `harden` the proxy target so it remains trapping.
15
+ * Thus, it should not be shared outside this module.
16
+ *
17
+ * @see https://github.com/endojs/endo/blob/master/packages/ses/docs/preparing-for-stabilize.md
18
+ */
19
+ const objTarget = freeze({ __proto__: null });
20
+
21
+ /*
22
+ * createSloppyGlobalsScopeTerminator()
23
+ * strictScopeTerminatorHandler manages a scopeTerminator Proxy which serves as
24
+ * the final scope boundary that will always return "undefined" in order
25
+ * to prevent access to "start compartment globals". When "sloppyGlobalsMode"
26
+ * is true, the Proxy will perform sets on the "globalObject".
27
+ */
28
+ export const createSloppyGlobalsScopeTerminator = globalObject => {
29
+ const scopeProxyHandlerProperties = {
30
+ // inherit scopeTerminator behavior
31
+ ...strictScopeTerminatorHandler,
32
+
33
+ // Redirect set properties to the globalObject.
34
+ set(_shadow, prop, value) {
35
+ return reflectSet(globalObject, prop, value);
36
+ },
37
+
38
+ // Always claim to have a potential property in order to be the recipient of a set
39
+ has(_shadow, _prop) {
40
+ return true;
41
+ },
42
+ };
43
+
44
+ // The scope handler's prototype is a proxy that throws if any trap other
45
+ // than get/set/has are run (like getOwnPropertyDescriptors, apply,
46
+ // getPrototypeOf).
47
+ const sloppyGlobalsScopeTerminatorHandler = freeze(
48
+ create(
49
+ alwaysThrowHandler,
50
+ getOwnPropertyDescriptors(scopeProxyHandlerProperties),
51
+ ),
52
+ );
53
+
54
+ const sloppyGlobalsScopeTerminator = new Proxy(
55
+ objTarget,
56
+ sloppyGlobalsScopeTerminatorHandler,
57
+ );
58
+
59
+ return sloppyGlobalsScopeTerminator;
60
+ };
61
+ freeze(createSloppyGlobalsScopeTerminator);
@@ -0,0 +1,99 @@
1
+ import {
2
+ Proxy,
3
+ String,
4
+ TypeError,
5
+ ReferenceError,
6
+ create,
7
+ freeze,
8
+ getOwnPropertyDescriptors,
9
+ } from './commons.js';
10
+ import { assert } from './error/assert.js';
11
+
12
+ const { Fail, quote: q } = assert;
13
+
14
+ /**
15
+ * `freeze` but not `harden` the proxy target so it remains trapping.
16
+ * Thus, it should not be shared outside this module.
17
+ *
18
+ * @see https://github.com/endojs/endo/blob/master/packages/ses/docs/preparing-for-stabilize.md
19
+ */
20
+ const objTarget = freeze({ __proto__: null });
21
+
22
+ /**
23
+ * alwaysThrowHandler
24
+ * This is an object that throws if any property is called. It's used as
25
+ * a proxy handler which throws on any trap called.
26
+ * It's made from a proxy with a get trap that throws. It's safe to
27
+ * create one and share it between all Proxy handlers.
28
+ */
29
+ export const alwaysThrowHandler = new Proxy(
30
+ objTarget,
31
+ freeze({
32
+ get(_shadow, prop) {
33
+ Fail`Please report unexpected scope handler trap: ${q(String(prop))}`;
34
+ },
35
+ }),
36
+ );
37
+
38
+ /*
39
+ * scopeProxyHandlerProperties
40
+ * scopeTerminatorHandler manages a strictScopeTerminator Proxy which serves as
41
+ * the final scope boundary that will always return "undefined" in order
42
+ * to prevent access to "start compartment globals".
43
+ */
44
+ const scopeProxyHandlerProperties = {
45
+ get(_shadow, _prop) {
46
+ return undefined;
47
+ },
48
+
49
+ set(_shadow, prop, _value) {
50
+ // We should only hit this if the has() hook returned true matches the v8
51
+ // ReferenceError message "Uncaught ReferenceError: xyz is not defined"
52
+ throw ReferenceError(`${String(prop)} is not defined`);
53
+ },
54
+
55
+ has(_shadow, prop) {
56
+ // we must at least return true for all properties on the realm globalThis
57
+ return true;
58
+ },
59
+
60
+ // note: this is likely a bug of safari
61
+ // https://bugs.webkit.org/show_bug.cgi?id=195534
62
+ getPrototypeOf(_shadow) {
63
+ return null;
64
+ },
65
+
66
+ // See https://github.com/endojs/endo/issues/1510
67
+ // TODO: report as bug to v8 or Chrome, and record issue link here.
68
+ getOwnPropertyDescriptor(_shadow, prop) {
69
+ // Coerce with `String` in case prop is a symbol.
70
+ const quotedProp = q(String(prop));
71
+ // eslint-disable-next-line @endo/no-polymorphic-call
72
+ console.warn(
73
+ `getOwnPropertyDescriptor trap on scopeTerminatorHandler for ${quotedProp}`,
74
+ TypeError().stack,
75
+ );
76
+ return undefined;
77
+ },
78
+
79
+ // See https://github.com/endojs/endo/issues/1490
80
+ // TODO Report bug to JSC or Safari
81
+ ownKeys(_shadow) {
82
+ return [];
83
+ },
84
+ };
85
+
86
+ // The scope handler's prototype is a proxy that throws if any trap other
87
+ // than get/set/has are run (like getOwnPropertyDescriptors, apply,
88
+ // getPrototypeOf).
89
+ export const strictScopeTerminatorHandler = freeze(
90
+ create(
91
+ alwaysThrowHandler,
92
+ getOwnPropertyDescriptors(scopeProxyHandlerProperties),
93
+ ),
94
+ );
95
+
96
+ export const strictScopeTerminator = new Proxy(
97
+ objTarget,
98
+ strictScopeTerminatorHandler,
99
+ );
@@ -0,0 +1,127 @@
1
+ // @ts-check
2
+
3
+ import {
4
+ Date,
5
+ TypeError,
6
+ apply,
7
+ construct,
8
+ defineProperties,
9
+ } from './commons.js';
10
+
11
+ export default function tameDateConstructor() {
12
+ const OriginalDate = Date;
13
+ const DatePrototype = OriginalDate.prototype;
14
+
15
+ // Use concise methods to obtain named functions without constructors.
16
+ const tamedMethods = {
17
+ /**
18
+ * `%SharedDate%.now()` throw a `TypeError` starting with "secure mode".
19
+ * See https://github.com/endojs/endo/issues/910#issuecomment-1581855420
20
+ */
21
+ now() {
22
+ throw TypeError('secure mode Calling %SharedDate%.now() throws');
23
+ },
24
+ };
25
+
26
+ /**
27
+ * Tame the Date constructor.
28
+ * See https://github.com/endojs/endo/issues/910#issuecomment-1581855420
29
+ *
30
+ * Common behavior
31
+ * * `new Date(x)` coerces x into a number and then returns a Date
32
+ * for that number of millis since the epoch
33
+ * * `new Date(NaN)` returns a Date object which stringifies to
34
+ * 'Invalid Date'
35
+ * * `new Date(undefined)` returns a Date object which stringifies to
36
+ * 'Invalid Date'
37
+ *
38
+ * OriginalDate (normal standard) behavior preserved by
39
+ * `%InitialDate%`.
40
+ * * `Date(anything)` gives a string with the current time
41
+ * * `new Date()` returns the current time, as a Date object
42
+ *
43
+ * `%SharedDate%` behavior
44
+ * * `Date(anything)` throws a TypeError starting with "secure mode"
45
+ * * `new Date()` throws a TypeError starting with "secure mode"
46
+ *
47
+ * @param {{powers?: string}} [opts]
48
+ */
49
+ const makeDateConstructor = ({ powers = 'none' } = {}) => {
50
+ let ResultDate;
51
+ if (powers === 'original') {
52
+ // eslint-disable-next-line no-shadow
53
+ ResultDate = function Date(...rest) {
54
+ if (new.target === undefined) {
55
+ return apply(OriginalDate, undefined, rest);
56
+ }
57
+ return construct(OriginalDate, rest, new.target);
58
+ };
59
+ } else {
60
+ // eslint-disable-next-line no-shadow
61
+ ResultDate = function Date(...rest) {
62
+ if (new.target === undefined) {
63
+ throw TypeError(
64
+ 'secure mode Calling %SharedDate% constructor as a function throws',
65
+ );
66
+ }
67
+ if (rest.length === 0) {
68
+ throw TypeError(
69
+ 'secure mode Calling new %SharedDate%() with no arguments throws',
70
+ );
71
+ }
72
+ return construct(OriginalDate, rest, new.target);
73
+ };
74
+ }
75
+
76
+ defineProperties(ResultDate, {
77
+ length: { value: 7 },
78
+ prototype: {
79
+ value: DatePrototype,
80
+ writable: false,
81
+ enumerable: false,
82
+ configurable: false,
83
+ },
84
+ parse: {
85
+ value: OriginalDate.parse,
86
+ writable: true,
87
+ enumerable: false,
88
+ configurable: true,
89
+ },
90
+ UTC: {
91
+ value: OriginalDate.UTC,
92
+ writable: true,
93
+ enumerable: false,
94
+ configurable: true,
95
+ },
96
+ });
97
+ return ResultDate;
98
+ };
99
+ const InitialDate = makeDateConstructor({ powers: 'original' });
100
+ const SharedDate = makeDateConstructor({ powers: 'none' });
101
+
102
+ defineProperties(InitialDate, {
103
+ now: {
104
+ value: OriginalDate.now,
105
+ writable: true,
106
+ enumerable: false,
107
+ configurable: true,
108
+ },
109
+ });
110
+ defineProperties(SharedDate, {
111
+ now: {
112
+ value: tamedMethods.now,
113
+ writable: true,
114
+ enumerable: false,
115
+ configurable: true,
116
+ },
117
+ });
118
+
119
+ defineProperties(DatePrototype, {
120
+ constructor: { value: SharedDate },
121
+ });
122
+
123
+ return {
124
+ '%InitialDate%': InitialDate,
125
+ '%SharedDate%': SharedDate,
126
+ };
127
+ }