@stacksjs/defaults 0.75.568 → 0.75.569

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.
@@ -100,6 +100,14 @@ The adapter (`HqServiceAdapter`) is what the service implements: `approveUrl`,
100
100
  page calls `describeConnect(code)` to show the app name and
101
101
  `approveConnect(code, user)` once a signed-in user confirms.
102
102
 
103
+ An approve link can be minted by anyone for any app name, so approving on the
104
+ link alone would let a signed-in person be talked into handing over their keys.
105
+ `confirmationCode(code)` is a six-character code derived from the connect code.
106
+ Send it as `confirmCode` beside `code` and `approveUrl` (`buddy hq:connect`
107
+ prints it), ask for it on the approve page without rendering it, and call
108
+ `approveConnect` only once `confirmationMatches(code, typed)` holds. Limit the
109
+ attempts per person.
110
+
103
111
  Codes are single-use and expire after ten minutes. A store holds only the
104
112
  SHA-256 of a code, and the approved keys wait encrypted under a key derived from
105
113
  the code, so the table never contains a pollable code or a readable key. Use
@@ -55,7 +55,7 @@ Browser runtime bindings are documented separately in `stacks-auto-imports`.
55
55
  | `@stacksjs/git` | [`stacks-git`](../stacks-git/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/git/src/index.ts) | 2 |
56
56
  | `@stacksjs/github` | [`stacks-git`](../stacks-git/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/github/src/index.ts) | 8 |
57
57
  | `@stacksjs/health` | [`stacks-health`](../stacks-health/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/health/src/index.ts) | 2 |
58
- | `@stacksjs/hq` | [`stacks-hq`](../stacks-hq/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/hq/src/index.ts) | 7 |
58
+ | `@stacksjs/hq` | [`stacks-hq`](../stacks-hq/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/hq/src/index.ts) | 8 |
59
59
  | `@stacksjs/http` | [`stacks-http`](../stacks-http/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/http/src/index.ts) | 3 |
60
60
  | `@stacksjs/i18n` | [`stacks-i18n`](../stacks-i18n/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/i18n/src/index.ts) | 5 |
61
61
  | `@stacksjs/image` | [`stacks-image`](../stacks-image/SKILL.md) | [entrypoint](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/image/src/index.ts) | 4 |
@@ -466,7 +466,7 @@ Evidence directory: [10 retained tests](https://github.com/stacksjs/stacks/blob/
466
466
 
467
467
  Guidance: [`stacks-events`](../stacks-events/SKILL.md). Source: [storage/framework/core/events/src/index.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/events/src/index.ts).
468
468
 
469
- Declared runtime exports: `all`, `appListenersBooted`, `bootAppListeners`, `createEmitter`, `default`, `defineEvents`, `defineListener`, `discoverListeners`, `dispatch`, `dispatchAndCollect`, `dispatchAsync`, `emitter`, `events`, `listen`, `mitt`, `off`, `once`, `registerAppListeners`, `resetListenerRegistry`, `resetNameRegistries`, `scope`, `scopedEvents`, `useEvent`, `useEvents`, `useListen`.
469
+ Declared runtime exports: `all`, `appListenersBooted`, `bootAppListeners`, `createEmitter`, `default`, `defineEvents`, `defineListener`, `discoverListeners`, `dispatch`, `dispatchAndCollect`, `dispatchAndCollectInPlace`, `dispatchAsync`, `emitter`, `events`, `listen`, `mitt`, `off`, `once`, `registerAppListeners`, `resetListenerRegistry`, `resetNameRegistries`, `scope`, `scopedEvents`, `useEvent`, `useEvents`, `useListen`.
470
470
 
471
471
  Local source modules: [src/discover.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/events/src/discover.ts).
472
472
 
@@ -548,7 +548,7 @@ Read the entrypoint and package export map for re-exported or type-only surfaces
548
548
 
549
549
  Local source modules: [src/client.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/hq/src/client.ts), [src/meter.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/hq/src/meter.ts), [src/service.ts](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/hq/src/service.ts).
550
550
 
551
- Evidence directory: [7 retained tests](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/hq/tests). These files are evidence pointers, not a claim that all tests were run for this catalog.
551
+ Evidence directory: [8 retained tests](https://github.com/stacksjs/stacks/blob/main/storage/framework/core/hq/tests). These files are evidence pointers, not a claim that all tests were run for this catalog.
552
552
 
553
553
  ### @stacksjs/http
554
554
 
@@ -76,43 +76,77 @@ for (const post of Post.where('published', true).get()) titles.push(post.title)
76
76
  the abandoned statement; MySQL cannot, so there it may still be applied once
77
77
  the lock is gone.
78
78
 
79
- ### Writes: in place unless the model asks for work around them
79
+ ### Writes: in place, with the work around them
80
80
 
81
81
  ```ts
82
- // Declares nothing to wait for: the write is the row.
83
- const note = Note.create({ title: 'Draft' })
84
- note.update({ title: 'Final' })
85
- Note.delete(note.id)
82
+ // Lifecycle events, a search index, a broadcast: the write is still the row.
83
+ const order = Order.create({ total: 1200 })
84
+ order.update({ total: 1500 })
85
+ Order.delete(order.id)
86
86
 
87
- // Declares `observe`: every write is a promise.
88
- const order = await Order.create({ total: 1200 })
87
+ // Declares `useSoftDeletes`: every write is a promise.
88
+ const note = await Note.create({ title: 'Draft' })
89
89
  ```
90
90
 
91
- A model's writes wait, and are typed as promises, when its definition
92
- declares any of:
91
+ What a model declares around its writes happens like this:
92
+
93
+ | Declaration | When it runs |
94
+ |---|---|
95
+ | `traits.observe` | listeners are called in place, in order; the row comes back after them |
96
+ | `traits.useSearch` | the index update starts with the write and finishes after it |
97
+ | `traits.broadcastOn` | the broadcast starts with the write and finishes after it |
98
+ | `set: { ... }`, `hooks: { ... }` written without `async` | in place, as part of the write |
99
+
100
+ A before-listener (`saving`, `creating`, `updating`, `deleting`) cancels the
101
+ write by returning `false` or throwing, and the write throws where it stands.
102
+ An after-listener that is `async` runs on after the row is back. A
103
+ before-listener that is `async` still has to be waited for, since it may
104
+ cancel: that one write becomes a promise, and the ORM warns once per event.
105
+ Keep before-listeners synchronous.
106
+
107
+ A model's writes wait, and are typed as promises, only when its definition
108
+ declares something a write cannot do in place:
93
109
 
94
110
  | Declaration | What the write waits for |
95
111
  |---|---|
96
- | `traits.observe` | lifecycle event listeners |
97
- | `traits.useSearch` | the search index update |
98
- | `traits.broadcastOn` | the broadcast |
99
112
  | `traits.useSoftDeletes` / `softDeletable` | the soft-delete transaction |
100
113
  | `traits.useAudit`, `traits.useActivityLog` | the audit or activity row |
101
- | `set: { ... }` | a setter, which may hash or call out |
102
- | `hooks: { ... }` | the model's own hooks |
114
+ | a `set:` member written `async` | the setter |
115
+ | a `hooks:` member written `async` | the hook |
103
116
 
104
117
  It is decided from the declaration, not from what happens to run: on such a
105
118
  model `create`, `createMany`, `firstOrCreate`, `updateOrCreate`, `update`,
106
119
  `save`, `delete`, `forceDelete` and a query's `delete()` are promises every
107
- time, and a failure is a rejection. On a model with none of these they return
108
- the value, and a failure (a guarded column, a validation rule) throws where
109
- the call stands. `modelWritesWait(definition)` answers it at runtime and
110
- `ModelWritesWait<typeof Model>` in the types. Reads never wait on either kind.
111
-
112
- When in doubt, `await` the write: it is correct on every model. An instance
113
- write on a model with an async setter still goes through `saveAsync()` /
120
+ time, and a failure is a rejection. On any other model they return the value,
121
+ and a failure (a guarded column, a validation rule, a listener that refuses)
122
+ throws where the call stands. `modelWritesWait(definition)` answers it at
123
+ runtime and `ModelWritesWait<typeof Model>` in the types. Reads never wait on
124
+ either kind.
125
+
126
+ Write setters so they answer in place. A password setter uses `makeHashSync`
127
+ from `@stacksjs/security`, as the default `User` does: it holds the thread for
128
+ the length of the hash (about a quarter of a second for bcrypt at 12 rounds),
129
+ and `User.create(...)` is the row. Where that pause is the worse trade, make
130
+ the setter `async` with `makeHash` and `await` that model's writes. An
131
+ instance write on a model with an async setter goes through `saveAsync()` /
114
132
  `updateAsync()`.
115
133
 
134
+ Work that finishes after the write (the index update, the broadcast, an
135
+ `async` after-listener) is logged if it fails and never fails the write. A
136
+ server needs nothing more. A script that ends with `process.exit()` waits
137
+ first:
138
+
139
+ ```ts
140
+ import { modelWorkSettled } from '@stacksjs/orm'
141
+
142
+ Product.create({ name: 'Widget' })
143
+ await modelWorkSettled() // index updates and broadcasts are done; rejects if one failed
144
+ process.exit(0)
145
+ ```
146
+
147
+ Application seeders and tinker snippets already do this. When in doubt,
148
+ `await` the write: it is correct on every model.
149
+
116
150
  ## Write behavior
117
151
 
118
152
  Ordinary create/update paths apply mass-assignment rules, declared validation,
@@ -80,6 +80,9 @@ evaluates it at global scope:
80
80
  - a query needs no `await` at the top level (see "No await at the top level");
81
81
  top-level `await` still works;
82
82
  - a relative `import('./x')` resolves against the session's directory;
83
+ - a snippet is done when the work its model writes left running is done: the
84
+ search-index update and the broadcast, for up to five seconds. What they
85
+ print lands in that snippet's frames, and `durationMs` does not count them;
83
86
  - nothing is written to the project.
84
87
 
85
88
  It resolves with `{ ok, value, thrown, frames, durationMs }` and never rejects
@@ -71,13 +71,18 @@ const decrypted = await decrypt(encrypted, 'custom-passphrase')
71
71
  ## Password Hashing
72
72
 
73
73
  ```typescript
74
- import { make, check, needsRehash, info, detectAlgorithm } from '@stacksjs/security'
74
+ import { make, makeSync, check, needsRehash, info, detectAlgorithm } from '@stacksjs/security'
75
75
 
76
76
  // Hash a password
77
77
  const hash = await make('password123') // bcrypt (default)
78
78
  const hash = await make('password123', { algorithm: 'argon2' })
79
79
  const hash = await make('password123', { rounds: 14 }) // bcrypt rounds
80
80
 
81
+ // The same hash, answered in place. For a model `set:` hook, so the write it
82
+ // belongs to stays in place. It holds the thread for the length of the hash
83
+ // (about a quarter of a second for bcrypt at 12 rounds).
84
+ const hashedNow = makeSync('password123')
85
+
81
86
  // Verify
82
87
  const valid = await check('password123', hash) // true/false
83
88
 
@@ -95,6 +100,7 @@ const algo = detectAlgorithm(hash) // 'bcrypt' | 'argon2' | 'argon2id' | ...
95
100
  - `hashNeedsRehash` = `needsRehash`
96
101
  - `hashInfo` = `info`
97
102
  - `makeHash` = `make`
103
+ - `hashMakeSync` = `makeHashSync` = `makeSync`
98
104
  - `verifyHash` = `check`
99
105
 
100
106
  ### Algorithm-Specific Functions
@@ -1,6 +1,6 @@
1
1
  import type { Attributes } from '@stacksjs/types'
2
2
  import { defineModel, selfOwnership } from '@stacksjs/orm'
3
- import { makeHash } from '@stacksjs/security'
3
+ import { makeHashSync } from '@stacksjs/security'
4
4
  // soon, these will be auto-imported
5
5
  import { schema } from '@stacksjs/validation/runtime'
6
6
  import { PASSWORD_MAX_LENGTH, PASSWORD_MIN_LENGTH } from '../password-policy'
@@ -154,9 +154,11 @@ export default defineModel({
154
154
  },
155
155
 
156
156
  set: {
157
- password: async (attributes: Attributes) => {
158
- return await makeHash(attributes.password, { algorithm: 'bcrypt' })
159
- },
157
+ // Hashed in place, so `User.create(...)` is the row and not a promise of
158
+ // one. That holds the thread for the length of the hash (about a quarter
159
+ // of a second at 12 rounds). An app that would rather wait for it without
160
+ // blocking makes this `async` with `makeHash`, and awaits its User writes.
161
+ password: (attributes: Attributes) => makeHashSync(attributes.password, { algorithm: 'bcrypt' }),
160
162
  },
161
163
  dashboard: {
162
164
  highlight: true,
@@ -2,7 +2,7 @@
2
2
  "publisher": "Stacks",
3
3
  "name": "vscode-stacks",
4
4
  "displayName": "Stacks",
5
- "version": "0.75.568",
5
+ "version": "0.75.569",
6
6
  "description": "Stacks, built into VS Code: stx templates, pickier linting and formatting, encrypted env files, the dev server preview and every buddy command. No other extensions required.",
7
7
  "license": "MIT",
8
8
  "funding": "https://github.com/sponsors/chrisbbreuer",
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@stacksjs/defaults",
3
3
  "type": "module",
4
4
  "sideEffects": false,
5
- "version": "0.75.568",
5
+ "version": "0.75.569",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "git+https://github.com/stacksjs/stacks.git",
@@ -45,10 +45,10 @@
45
45
  "dependencies": {
46
46
  "@iconify-json/f7": "^1.2.2",
47
47
  "@iconify-json/hugeicons": "^1.2.35",
48
- "@stacksjs/mobile": "0.75.568",
48
+ "@stacksjs/mobile": "0.75.569",
49
49
  "@stacksjs/sanitizer": "^0.2.864",
50
50
  "@stacksjs/stx": "^0.2.864",
51
- "@stacksjs/strings": "0.75.568",
51
+ "@stacksjs/strings": "0.75.569",
52
52
  "@xterm/addon-fit": "^0.11.0",
53
53
  "@xterm/xterm": "^6.0.0",
54
54
  "ts-qr-codes": "^0.1.8"