@twin.org/core 0.0.4-next.7 → 0.0.4-next.9

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 (91) hide show
  1. package/dist/es/encoding/base64.js +1 -1
  2. package/dist/es/encoding/base64.js.map +1 -1
  3. package/dist/es/errors/conflictError.js +1 -1
  4. package/dist/es/errors/conflictError.js.map +1 -1
  5. package/dist/es/errors/generalError.js +1 -1
  6. package/dist/es/errors/generalError.js.map +1 -1
  7. package/dist/es/errors/notImplementedError.js +1 -1
  8. package/dist/es/errors/notImplementedError.js.map +1 -1
  9. package/dist/es/errors/validationError.js +1 -1
  10. package/dist/es/errors/validationError.js.map +1 -1
  11. package/dist/es/helpers/arrayHelper.js +1 -1
  12. package/dist/es/helpers/arrayHelper.js.map +1 -1
  13. package/dist/es/helpers/errorHelper.js +1 -1
  14. package/dist/es/helpers/errorHelper.js.map +1 -1
  15. package/dist/es/helpers/jsonHelper.js +1 -1
  16. package/dist/es/helpers/jsonHelper.js.map +1 -1
  17. package/dist/es/helpers/objectHelper.js +2 -2
  18. package/dist/es/helpers/objectHelper.js.map +1 -1
  19. package/dist/es/helpers/stringHelper.js +2 -2
  20. package/dist/es/helpers/stringHelper.js.map +1 -1
  21. package/dist/es/index.js +4 -0
  22. package/dist/es/index.js.map +1 -1
  23. package/dist/es/models/IComponent.js.map +1 -1
  24. package/dist/es/models/ILocale.js.map +1 -1
  25. package/dist/es/models/ISharedObjectBufferOptions.js +4 -0
  26. package/dist/es/models/ISharedObjectBufferOptions.js.map +1 -0
  27. package/dist/es/models/ISharedObjectBufferWorkerMessage.js +2 -0
  28. package/dist/es/models/ISharedObjectBufferWorkerMessage.js.map +1 -0
  29. package/dist/es/models/IValidationFailure.js.map +1 -1
  30. package/dist/es/models/sharedObjectBufferMessageTypes.js +13 -0
  31. package/dist/es/models/sharedObjectBufferMessageTypes.js.map +1 -0
  32. package/dist/es/utils/asyncCache.js +1 -1
  33. package/dist/es/utils/asyncCache.js.map +1 -1
  34. package/dist/es/utils/coerce.js +10 -20
  35. package/dist/es/utils/coerce.js.map +1 -1
  36. package/dist/es/utils/compression.js +1 -1
  37. package/dist/es/utils/compression.js.map +1 -1
  38. package/dist/es/utils/is.js +9 -9
  39. package/dist/es/utils/is.js.map +1 -1
  40. package/dist/es/utils/sharedObjectBuffer.js +305 -0
  41. package/dist/es/utils/sharedObjectBuffer.js.map +1 -0
  42. package/dist/es/utils/sharedStore.js +2 -2
  43. package/dist/es/utils/sharedStore.js.map +1 -1
  44. package/dist/types/errors/conflictError.d.ts +1 -1
  45. package/dist/types/errors/generalError.d.ts +1 -1
  46. package/dist/types/errors/notImplementedError.d.ts +1 -1
  47. package/dist/types/errors/validationError.d.ts +1 -1
  48. package/dist/types/helpers/arrayHelper.d.ts +1 -1
  49. package/dist/types/helpers/errorHelper.d.ts +1 -1
  50. package/dist/types/helpers/jsonHelper.d.ts +1 -1
  51. package/dist/types/helpers/objectHelper.d.ts +2 -2
  52. package/dist/types/helpers/stringHelper.d.ts +2 -2
  53. package/dist/types/index.d.ts +4 -0
  54. package/dist/types/models/IComponent.d.ts +2 -2
  55. package/dist/types/models/ILocale.d.ts +1 -1
  56. package/dist/types/models/ISharedObjectBufferOptions.d.ts +17 -0
  57. package/dist/types/models/ISharedObjectBufferWorkerMessage.d.ts +29 -0
  58. package/dist/types/models/IValidationFailure.d.ts +1 -1
  59. package/dist/types/models/sharedObjectBufferMessageTypes.d.ts +13 -0
  60. package/dist/types/utils/asyncCache.d.ts +1 -1
  61. package/dist/types/utils/coerce.d.ts +10 -20
  62. package/dist/types/utils/compression.d.ts +1 -1
  63. package/dist/types/utils/is.d.ts +9 -9
  64. package/dist/types/utils/sharedObjectBuffer.d.ts +74 -0
  65. package/dist/types/utils/sharedStore.d.ts +2 -2
  66. package/docs/changelog.md +39 -0
  67. package/docs/reference/classes/ArrayHelper.md +1 -1
  68. package/docs/reference/classes/AsyncCache.md +1 -1
  69. package/docs/reference/classes/Coerce.md +10 -50
  70. package/docs/reference/classes/Compression.md +1 -1
  71. package/docs/reference/classes/ConflictError.md +1 -1
  72. package/docs/reference/classes/ErrorHelper.md +1 -1
  73. package/docs/reference/classes/GeneralError.md +1 -1
  74. package/docs/reference/classes/Is.md +9 -9
  75. package/docs/reference/classes/JsonHelper.md +1 -1
  76. package/docs/reference/classes/NotImplementedError.md +1 -1
  77. package/docs/reference/classes/ObjectHelper.md +2 -2
  78. package/docs/reference/classes/SharedObjectBuffer.md +192 -0
  79. package/docs/reference/classes/SharedStore.md +2 -2
  80. package/docs/reference/classes/StringHelper.md +16 -13
  81. package/docs/reference/classes/ValidationError.md +1 -1
  82. package/docs/reference/index.md +5 -0
  83. package/docs/reference/interfaces/IComponent.md +2 -2
  84. package/docs/reference/interfaces/ILocale.md +1 -1
  85. package/docs/reference/interfaces/ISharedObjectBufferOptions.md +33 -0
  86. package/docs/reference/interfaces/ISharedObjectBufferWorkerMessage.md +44 -0
  87. package/docs/reference/interfaces/IValidationFailure.md +1 -1
  88. package/docs/reference/type-aliases/SharedObjectBufferMessageTypes.md +5 -0
  89. package/docs/reference/variables/SharedObjectBufferMessageTypes.md +13 -0
  90. package/locales/en.json +5 -0
  91. package/package.json +2 -2
@@ -0,0 +1,192 @@
1
+ # Class: SharedObjectBuffer
2
+
3
+ Manages per-object SharedArrayBuffers that store objects as UTF-8 JSON.
4
+ Buffer layout: 4-byte Int32 header (current data byte length) followed by the JSON-encoded object.
5
+ Buffers are explicitly created with create and cached in SharedStore.
6
+ On a worker thread an existing buffer is fetched via a MessagePort handshake
7
+ (same protocol as Mutex) and cached locally.
8
+ Buffers grow automatically when the payload exceeds capacity; when the payload drops well below
9
+ capacity the buffer is replaced with a smaller one. The caller must hold the objectId-keyed Mutex
10
+ around every read and write. The main thread's worker message handler must forward messages to
11
+ both Mutex.handleWorkerMessage and SharedObjectBuffer.handleWorkerMessage.
12
+
13
+ ## Constructors
14
+
15
+ ### Constructor
16
+
17
+ > **new SharedObjectBuffer**(): `SharedObjectBuffer`
18
+
19
+ #### Returns
20
+
21
+ `SharedObjectBuffer`
22
+
23
+ ## Properties
24
+
25
+ ### CLASS\_NAME {#class_name}
26
+
27
+ > `readonly` `static` **CLASS\_NAME**: `string`
28
+
29
+ Runtime name for the class.
30
+
31
+ ***
32
+
33
+ ### DEFAULT\_CAPACITY\_BYTES {#default_capacity_bytes}
34
+
35
+ > `readonly` `static` **DEFAULT\_CAPACITY\_BYTES**: `number`
36
+
37
+ Default payload capacity per object (1 MiB). The first caller that creates the buffer
38
+ for an object determines its initial capacity; later callers that pass a different value
39
+ are ignored.
40
+
41
+ ***
42
+
43
+ ### MAX\_CAPACITY\_BYTES {#max_capacity_bytes}
44
+
45
+ > `readonly` `static` **MAX\_CAPACITY\_BYTES**: `number`
46
+
47
+ Default upper bound for how large a buffer may grow (256 MiB).
48
+ Override per-object via the maxCapacityBytes option on create.
49
+
50
+ ## Methods
51
+
52
+ ### create() {#create}
53
+
54
+ > `static` **create**(`objectId`, `options?`): `Promise`\<`void`\>
55
+
56
+ Create the buffer for the given objectId if it does not already exist.
57
+ Must be called while holding Mutex.lock(objectId).
58
+
59
+ #### Parameters
60
+
61
+ ##### objectId
62
+
63
+ `string`
64
+
65
+ The object id that identifies the buffer.
66
+
67
+ ##### options?
68
+
69
+ [`ISharedObjectBufferOptions`](../interfaces/ISharedObjectBufferOptions.md)
70
+
71
+ Optional capacity configuration used when creating the buffer.
72
+
73
+ #### Returns
74
+
75
+ `Promise`\<`void`\>
76
+
77
+ ***
78
+
79
+ ### read() {#read}
80
+
81
+ > `static` **read**\<`T`\>(`objectId`): `Promise`\<`T` \| `undefined`\>
82
+
83
+ Read and decode the object stored for the given objectId.
84
+ Must be called while holding Mutex.lock(objectId).
85
+
86
+ #### Type Parameters
87
+
88
+ ##### T
89
+
90
+ `T`
91
+
92
+ #### Parameters
93
+
94
+ ##### objectId
95
+
96
+ `string`
97
+
98
+ The object id that identifies the buffer.
99
+
100
+ #### Returns
101
+
102
+ `Promise`\<`T` \| `undefined`\>
103
+
104
+ The stored object, or undefined when nothing has been written yet.
105
+
106
+ ***
107
+
108
+ ### write() {#write}
109
+
110
+ > `static` **write**\<`T`\>(`objectId`, `value`): `Promise`\<`void`\>
111
+
112
+ Encode and write the object into the buffer for the given objectId.
113
+ The buffer must already exist, usually by calling create first.
114
+ When the encoded payload exceeds the current buffer capacity the buffer is grown
115
+ in-place via SharedArrayBuffer.grow so every thread with a reference sees the
116
+ new size without any pointer swap. When the payload is smaller than
117
+ _SHRINK_THRESHOLD of the current capacity and the capacity exceeds
118
+ DEFAULT_CAPACITY_BYTES, the buffer is replaced with a smaller one on the calling thread.
119
+ Must be called while holding Mutex.lock(objectId).
120
+
121
+ #### Type Parameters
122
+
123
+ ##### T
124
+
125
+ `T`
126
+
127
+ #### Parameters
128
+
129
+ ##### objectId
130
+
131
+ `string`
132
+
133
+ The object id that identifies the buffer.
134
+
135
+ ##### value
136
+
137
+ `T`
138
+
139
+ The object to persist.
140
+
141
+ #### Returns
142
+
143
+ `Promise`\<`void`\>
144
+
145
+ ***
146
+
147
+ ### remove() {#remove}
148
+
149
+ > `static` **remove**(`objectId`): `void`
150
+
151
+ Remove the stored object and release the buffer for the given objectId.
152
+ The entry is deleted from the local cache so subsequent reads or writes will
153
+ create or fetch a fresh buffer. Worker threads that have cached the old buffer
154
+ reference continue using it until they restart or re-request via the worker protocol.
155
+ Must be called while holding Mutex.lock(objectId).
156
+
157
+ #### Parameters
158
+
159
+ ##### objectId
160
+
161
+ `string`
162
+
163
+ The object id that identifies the buffer.
164
+
165
+ #### Returns
166
+
167
+ `void`
168
+
169
+ ***
170
+
171
+ ### handleWorkerMessage() {#handleworkermessage}
172
+
173
+ > `static` **handleWorkerMessage**(`msg`): `boolean`
174
+
175
+ Inspect a message from a worker thread and, if it is a SharedObjectBuffer
176
+ buffer-fetch request, respond to it synchronously.
177
+ Call this from the main thread's worker message handler alongside
178
+ Mutex.handleWorkerMessage.
179
+
180
+ #### Parameters
181
+
182
+ ##### msg
183
+
184
+ `unknown`
185
+
186
+ The raw message received from the worker.
187
+
188
+ #### Returns
189
+
190
+ `boolean`
191
+
192
+ True if the message was a SharedObjectBuffer protocol message, false otherwise.
@@ -1,7 +1,7 @@
1
1
  # Class: SharedStore
2
2
 
3
- Provide a store for shared objects which can be accesses through multiple
4
- instance loads of a packages.
3
+ Provide a store for shared objects which can be accessed through multiple
4
+ instance loads of a package.
5
5
 
6
6
  ## Constructors
7
7
 
@@ -297,19 +297,6 @@ The string split into words.
297
297
  > `static` **isUtf8**(`data`): `boolean`
298
298
 
299
299
  Check if a Node.js Buffer or Uint8Array is UTF-8.
300
- Url https://tools.ietf.org/html/rfc3629
301
- Source https://github.com/hcodes/isutf8
302
- UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4.
303
- UTF8-1 = %x00-7F.
304
- UTF8-2 = %xC2-DF UTF8-tail.
305
- UTF8-3 = %xE0 %xA0-BF UTF8-tail.
306
- - %xE1-EC 2( UTF8-tail ).
307
- - %xED %x80-9F UTF8-tail.
308
- - %xEE-EF 2( UTF8-tail ).
309
- UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ).
310
- - %xF1-F3 3( UTF8-tail ).
311
- - %xF4 %x80-8F 2( UTF8-tail ).
312
- UTF8-tail = %x80-BF.
313
300
 
314
301
  #### Parameters
315
302
 
@@ -324,3 +311,19 @@ The data to check.
324
311
  `boolean`
325
312
 
326
313
  True if the data is utf8.
314
+
315
+ #### See
316
+
317
+ - https://tools.ietf.org/html/rfc3629
318
+ - https://github.com/hcodes/isutf8
319
+ UTF8-char = UTF8-1 / UTF8-2 / UTF8-3 / UTF8-4.
320
+ UTF8-1 = %x00-7F.
321
+ UTF8-2 = %xC2-DF UTF8-tail.
322
+ UTF8-3 = %xE0 %xA0-BF UTF8-tail.
323
+ - %xE1-EC 2( UTF8-tail ).
324
+ - %xED %x80-9F UTF8-tail.
325
+ - %xEE-EF 2( UTF8-tail ).
326
+ UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ).
327
+ - %xF1-F3 3( UTF8-tail ).
328
+ - %xF4 %x80-8F 2( UTF8-tail ).
329
+ UTF8-tail = %x80-BF.
@@ -88,7 +88,7 @@ The cause of the error.
88
88
 
89
89
  > `readonly` `static` **CLASS\_NAME**: `string`
90
90
 
91
- Runtime name for the class.s
91
+ Runtime name for the class.
92
92
 
93
93
  ## Methods
94
94
 
@@ -40,6 +40,7 @@
40
40
  - [I18n](classes/I18n.md)
41
41
  - [Is](classes/Is.md)
42
42
  - [Mutex](classes/Mutex.md)
43
+ - [SharedObjectBuffer](classes/SharedObjectBuffer.md)
43
44
  - [SharedStore](classes/SharedStore.md)
44
45
  - [Validation](classes/Validation.md)
45
46
 
@@ -56,6 +57,8 @@
56
57
  - [ILocalesIndex](interfaces/ILocalesIndex.md)
57
58
  - [IMutexWorkerMessage](interfaces/IMutexWorkerMessage.md)
58
59
  - [IPatchOperation](interfaces/IPatchOperation.md)
60
+ - [ISharedObjectBufferOptions](interfaces/ISharedObjectBufferOptions.md)
61
+ - [ISharedObjectBufferWorkerMessage](interfaces/ISharedObjectBufferWorkerMessage.md)
59
62
  - [IUrlParts](interfaces/IUrlParts.md)
60
63
  - [IValidationFailure](interfaces/IValidationFailure.md)
61
64
 
@@ -65,6 +68,7 @@
65
68
  - [CompressionType](type-aliases/CompressionType.md)
66
69
  - [HealthStatus](type-aliases/HealthStatus.md)
67
70
  - [MutexMessageTypes](type-aliases/MutexMessageTypes.md)
71
+ - [SharedObjectBufferMessageTypes](type-aliases/SharedObjectBufferMessageTypes.md)
68
72
  - [ObjectOrArray](type-aliases/ObjectOrArray.md)
69
73
  - [SingleOccurrenceArray](type-aliases/SingleOccurrenceArray.md)
70
74
  - [SingleOccurrenceArrayDepthHelper](type-aliases/SingleOccurrenceArrayDepthHelper.md)
@@ -76,3 +80,4 @@
76
80
  - [CompressionType](variables/CompressionType.md)
77
81
  - [HealthStatus](variables/HealthStatus.md)
78
82
  - [MutexMessageTypes](variables/MutexMessageTypes.md)
83
+ - [SharedObjectBufferMessageTypes](variables/SharedObjectBufferMessageTypes.md)
@@ -80,7 +80,7 @@ The node logging component type.
80
80
 
81
81
  `Promise`\<`void`\>
82
82
 
83
- Nothing.
83
+ A promise that resolves when the component has started.
84
84
 
85
85
  ***
86
86
 
@@ -102,7 +102,7 @@ The node logging component type.
102
102
 
103
103
  `Promise`\<`void`\>
104
104
 
105
- Nothing.
105
+ A promise that resolves when the component has stopped.
106
106
 
107
107
  ***
108
108
 
@@ -1,6 +1,6 @@
1
1
  # Interface: ILocale
2
2
 
3
- Model for a local.
3
+ Model for a locale.
4
4
 
5
5
  ## Properties
6
6
 
@@ -0,0 +1,33 @@
1
+ # Interface: ISharedObjectBufferOptions
2
+
3
+ Options for configuring buffer capacity when creating a shared object buffer.
4
+
5
+ ## Properties
6
+
7
+ ### initialCapacityBytes? {#initialcapacitybytes}
8
+
9
+ > `optional` **initialCapacityBytes?**: `number`
10
+
11
+ Initial payload capacity hint in bytes.
12
+ Only honoured when the buffer does not yet exist; ignored on subsequent writes.
13
+
14
+ #### Default
15
+
16
+ ```ts
17
+ 1 MiB.
18
+ ```
19
+
20
+ ***
21
+
22
+ ### maxCapacityBytes? {#maxcapacitybytes}
23
+
24
+ > `optional` **maxCapacityBytes?**: `number`
25
+
26
+ Maximum allowed payload capacity in bytes. The buffer will never grow beyond this limit.
27
+ Only honoured when the buffer does not yet exist; ignored on subsequent writes.
28
+
29
+ #### Default
30
+
31
+ ```ts
32
+ 256 MiB.
33
+ ```
@@ -0,0 +1,44 @@
1
+ # Interface: ISharedObjectBufferWorkerMessage
2
+
3
+ Message sent from a worker thread to the main thread to request the SharedArrayBuffer for an object.
4
+
5
+ ## Properties
6
+
7
+ ### type {#type}
8
+
9
+ > **type**: `"twin:sharedObjectBuffer:getBuffer"`
10
+
11
+ The message type discriminant.
12
+
13
+ ***
14
+
15
+ ### objectId {#objectid}
16
+
17
+ > **objectId**: `string`
18
+
19
+ The object id name that identifies which buffer is being requested.
20
+
21
+ ***
22
+
23
+ ### signal {#signal}
24
+
25
+ > **signal**: `SharedArrayBuffer`
26
+
27
+ One-shot SharedArrayBuffer used for the Atomics.wait/notify handshake so the
28
+ worker can block synchronously until the main thread has posted the response.
29
+
30
+ ***
31
+
32
+ ### port {#port}
33
+
34
+ > **port**: `MessagePort`
35
+
36
+ MessagePort through which the main thread returns the object buffer.
37
+
38
+ ***
39
+
40
+ ### options? {#options}
41
+
42
+ > `optional` **options?**: [`ISharedObjectBufferOptions`](ISharedObjectBufferOptions.md)
43
+
44
+ Options for creating or fetching the buffer.
@@ -16,7 +16,7 @@ The property that failed validation.
16
16
 
17
17
  > **reason**: `string`
18
18
 
19
- The reason the validation failed as an i18 resource error.
19
+ The reason the validation failed as an i18n resource key.
20
20
 
21
21
  ***
22
22
 
@@ -0,0 +1,5 @@
1
+ # Type Alias: SharedObjectBufferMessageTypes
2
+
3
+ > **SharedObjectBufferMessageTypes** = *typeof* [`SharedObjectBufferMessageTypes`](../variables/SharedObjectBufferMessageTypes.md)\[keyof *typeof* [`SharedObjectBufferMessageTypes`](../variables/SharedObjectBufferMessageTypes.md)\]
4
+
5
+ Union of all SharedObjectBuffer message type strings.
@@ -0,0 +1,13 @@
1
+ # Variable: SharedObjectBufferMessageTypes
2
+
3
+ > `const` **SharedObjectBufferMessageTypes**: `object`
4
+
5
+ Message type constants for the SharedObjectBuffer worker-to-main-thread protocol.
6
+
7
+ ## Type Declaration
8
+
9
+ ### GetBuffer {#getbuffer}
10
+
11
+ > `readonly` **GetBuffer**: `"twin:sharedObjectBuffer:getBuffer"` = `"twin:sharedObjectBuffer:getBuffer"`
12
+
13
+ Worker requests the SharedArrayBuffer for a named object from the main thread.
package/locales/en.json CHANGED
@@ -110,6 +110,11 @@
110
110
  "lockAlreadyReleased": "The key \"{key}\" is not currently locked",
111
111
  "lockTimeout": "Failed to acquire lock for key \"{key}\" within the timeout of {timeout} milliseconds",
112
112
  "bufferFetchFailed": "Failed to retrieve shared buffer for key \"{key}\" from the main thread"
113
+ },
114
+ "sharedObjectBuffer": {
115
+ "notCreated": "The shared buffer for object \"{objectId}\" has not been created",
116
+ "capacityExceeded": "The payload size of {required} bytes exceeds the maximum capacity of {capacity} bytes for object \"{objectId}\"",
117
+ "bufferFetchFailed": "Failed to retrieve shared buffer for object \"{objectId}\" from the main thread"
113
118
  }
114
119
  },
115
120
  "errorNames": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/core",
3
- "version": "0.0.4-next.7",
3
+ "version": "0.0.4-next.9",
4
4
  "description": "Helper methods/classes for data type checking/validation/guarding/error handling",
5
5
  "repository": {
6
6
  "type": "git",
@@ -14,7 +14,7 @@
14
14
  "node": ">=20.0.0"
15
15
  },
16
16
  "dependencies": {
17
- "@twin.org/nameof": "0.0.4-next.7",
17
+ "@twin.org/nameof": "0.0.4-next.9",
18
18
  "intl-messageformat": "11.2.6",
19
19
  "rfc6902": "5.2.0"
20
20
  },