dsh-sticky 0.3.1 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/docs/compatibility.md +5 -3
- package/docs/data-format.md +8 -4
- package/docs/editor.md +7 -1
- package/lib/client.js +90 -93
- package/lib/index.js +4 -10
- package/package.json +3 -3
package/lib/index.js
CHANGED
|
@@ -1,21 +1,15 @@
|
|
|
1
1
|
import{join as e}from"node:path";import{randomBytes as t,randomUUID as n}from"node:crypto";import{mkdir as r,readFile as i,readdir as a,rename as o,rm as s,writeFile as c}from"node:fs/promises";import{homedir as l}from"node:os";const u=[`yellow`,`green`,`blue`,`violet`,`red`,`gray`],d=/^[A-Za-z0-9_-]{1,64}$/;function f(e){if(!d.test(e))throw Error(`invalid note id ${JSON.stringify(e)}`);return e}function p(e){if(typeof e!=`object`||!e||Array.isArray(e))return{};let t={};for(let[n,r]of Object.entries(e))typeof r==`number`&&Number.isInteger(r)&&(r<1||r>4e3||(t[n]=r));return t}const m=`.tmp`;var h=class extends Error{code;constructor(e,t){super(t),this.name=`StickyError`,this.code=e}};function g(e){return typeof e==`string`&&u.includes(e)}function _(t,n=process.env,r=l()){let i=t?.trim()??``;if(i!==``)return v(i);let a=n.DSH_HOME?.trim()??``;if(a!==``)return v(a);if(r.trim()===``)throw new h(`sticky/io`,`cannot resolve the Harness home for sticky notes`);return e(r,`.dsh`)}function v(t){return t===`~`?l():t.startsWith(`~/`)||t.startsWith(`~\\`)?e(l(),t.slice(2)):t}function y(e,t){for(let t of e.split(`
|
|
2
|
-
`)){let e=t.replace(/^\s{0,3}(?:#{1,6}\s+|>\s?|[-*+]\s+|\d+[.)]\s+)/,``).replace(
|
|
2
|
+
`)){let e=t.replace(/^\s{0,3}(?:#{1,6}\s+|>\s?|[-*+]\s+|\d+[.)]\s+)/,``).replace(/\*\*(\S(?:[^*]*\S)?)\*\*/g,`$1`).replace(/\*(\S(?:[^*]*\S)?)\*/g,`$1`).replace(/~~(\S(?:[^~]*\S)?)~~/g,`$1`).trim();if(e===``)continue;let n=[...e];return n.length<=80?e:n.slice(0,80).join(``)}return t}function b(e){return new h(`sticky/invalid-record`,e)}function ee(e,t,n){if(typeof e!=`object`||!e)throw b(`document is not an object`);let r=e;if(typeof r.id!=`string`||!d.test(r.id))throw b(`invalid id`);if(r.id!==t)throw b(`document id ${JSON.stringify(r.id)} does not match the file name ${JSON.stringify(t)}`);if(typeof r.version==`number`&&r.version>1)throw b(`document version ${String(r.version)} is newer than the supported 1`);if(typeof r.markdown!=`string`)throw b(`invalid markdown`);if(!g(r.color))throw b(`invalid color`);let i=typeof r.title==`string`?r.title.trim():``,a=r.archived===!0;return{version:typeof r.version==`number`?r.version:1,id:t,title:i===``?y(r.markdown,n):i,markdown:r.markdown,color:r.color,pinned:!a&&r.pinned===!0,archived:a,imageWidths:p(r.imageWidths),createdAt:typeof r.createdAt==`number`?r.createdAt:0,updatedAt:typeof r.updatedAt==`number`?r.updatedAt:0}}function te(e,t){return e.pinned===t.pinned?e.updatedAt===t.updatedAt?e.id===t.id?0:e.id<t.id?-1:1:t.updatedAt-e.updatedAt:e.pinned?-1:1}function ne(e,t){let n=Object.keys(e);return n.length===Object.keys(t).length&&n.every(n=>e[n]===t[n])}function x(e,t){return e.title!==t.title||e.markdown!==t.markdown||e.color!==t.color||!ne(e.imageWidths,t.imageWidths)}function S(e){if(!d.test(e))throw new h(`sticky/bad-request`,`invalid note id ${JSON.stringify(e)}`);return e}function C(e){return e?.code===`ENOENT`}function w(e){return e instanceof Error?e.message:String(e)}var re=class{directory;labels;now;images;chains=/* @__PURE__ */ new Map;ready;constructor(e,t,n=Date.now,r){this.directory=e,this.labels=t,this.now=n,this.images=r}async ensure(){this.ready??=r(this.directory,{recursive:!0,mode:448}).then(()=>void 0,e=>{throw this.ready=void 0,new h(`sticky/io`,`cannot create ${this.directory}: ${w(e)}`)}),await this.ready}async scan(){await this.ensure();try{return await a(this.directory)}catch(e){
|
|
3
3
|
/* v8 ignore next -- ensure() just recreated the directory, so between
|
|
4
4
|
ensure() and readdir nothing testable can go wrong; whatever lands here
|
|
5
|
-
is an unreproducible race, and
|
|
5
|
+
is an unreproducible race, and an empty answer is the honest one. */
|
|
6
6
|
if(C(e))return[];
|
|
7
7
|
/* v8 ignore next -- same unreproducible race; see the comment above */
|
|
8
|
-
throw new h(`sticky/io`,`cannot read ${this.directory}: ${w(e)}`)}
|
|
8
|
+
throw new h(`sticky/io`,`cannot read ${this.directory}: ${w(e)}`)}}documentIdOf(e){if(!e.endsWith(`.json`)||e.endsWith(m))return;let t=e.slice(0,-5);return d.test(t)?t:void 0}async list(){let e=[];for(let t of await this.scan()){let n=this.documentIdOf(t);if(n===void 0)continue;let r;try{r=await this.readOne(n)}catch(e){this.warn(t,e);continue}
|
|
9
9
|
/* v8 ignore next -- a listed file exists; readOne answers undefined only
|
|
10
10
|
when the document is missing, and a document cannot vanish between its
|
|
11
11
|
own readdir row and its read inside one testable iteration. */
|
|
12
|
-
r!==void 0&&
|
|
13
|
-
/* v8 ignore next -- ensure() just recreated the directory; a failure
|
|
14
|
-
between the two is an unreproducible race, and an empty answer is the
|
|
15
|
-
honest one. */
|
|
16
|
-
if(C(e))return[];
|
|
17
|
-
/* v8 ignore next -- same unreproducible race; see the comment above */
|
|
18
|
-
throw new h(`sticky/io`,`cannot read ${this.directory}: ${w(e)}`)}let t=[];for(let n of e){if(!n.endsWith(`.json`)||n.endsWith(m))continue;let e=n.slice(0,-5);d.test(e)&&t.push(e)}return t}async get(e){return S(e),await this.readOne(e)}fieldsOf(e){let t=e.color??`yellow`;if(!g(t))throw new h(`sticky/bad-request`,`unknown color ${JSON.stringify(t)}`);let n=e.markdown??``;if(n.length>2e5)throw new h(`sticky/bad-request`,`markdown exceeds 200000 characters`);let r=typeof e.title==`string`?e.title.trim():``;if(r.length>200)throw new h(`sticky/bad-request`,`title exceeds 200 characters`);return{markdown:n,color:t,title:r===``?y(n,this.labels.untitled):r,imageWidths:p(e.imageWidths)}}async save(e){if(e.id===void 0)throw new h(`sticky/bad-request`,`id is required`);let t=this.fieldsOf(e),n=S(e.id);return await this.serialize(n,async()=>{let r=await this.readOne(n);if(r===void 0)throw new h(`sticky/not-found`,`no note ${JSON.stringify(n)}`);let i={...r,...t,pinned:e.pinned??r.pinned,updatedAt:x(r,t)?this.now():r.updatedAt},a=await this.write(i);return await this.images?.collect(n,r.markdown,t.markdown),a})}async create(e,t){let n=S(e),{markdown:r,color:i,title:a,imageWidths:o}=this.fieldsOf(t),s=this.now();return await this.serialize(n,async()=>{if(await this.readOne(n)!==void 0)throw new h(`sticky/conflict`,`note ${JSON.stringify(n)} already exists`);let e={version:1,id:n,title:a,markdown:r,color:i,pinned:t.pinned===!0,archived:!1,imageWidths:o,createdAt:s,updatedAt:s};return await this.write(e)})}async setArchived(e,t){return await this.patch(S(e),{archived:t})}async setPinned(e,t){return await this.patch(S(e),{pinned:t})}async remove(e){let t=S(e);await this.ensure(),await this.serialize(t,async()=>{try{await s(this.pathOf(t),{force:!0})}catch(e){throw new h(`sticky/io`,`cannot delete note ${JSON.stringify(t)}: ${w(e)}`)}await this.images?.removeNote(t)})}async patch(e,t){return await this.serialize(e,async()=>{let n=await this.readOne(e);if(n===void 0)throw new h(`sticky/not-found`,`no note ${JSON.stringify(e)}`);return await this.write({...n,...t})})}pathOf(t){return e(this.directory,`${t}.json`)}async readOne(e){let t;try{t=await i(this.pathOf(e),`utf8`)}catch(t){if(C(t))return;throw new h(`sticky/io`,`cannot read note ${JSON.stringify(e)}: ${w(t)}`)}let n;try{n=JSON.parse(t)}catch(t){throw b(`note ${JSON.stringify(e)} is not JSON: ${w(t)}`)}return ee(n,this.labels.untitled)}async write(e){await this.ensure();let t=e.archived?{...e,pinned:!1}:e,r=this.pathOf(t.id),i=`${r}.${String(process.pid)}.${n().slice(0,8)}${m}`;try{await c(i,`${JSON.stringify(t,void 0,2)}\n`,{encoding:`utf8`,mode:384}),await o(i,r)}catch(e){throw await s(i,{force:!0}).catch(()=>void 0),new h(`sticky/io`,`cannot write note ${JSON.stringify(t.id)}: ${w(e)}`)}return t}async serialize(e,t){let n=(this.chains.get(e)??Promise.resolve()).then(t,t);this.chains.set(e,n);try{return await n}finally{this.chains.get(e)===n&&this.chains.delete(e)}}warn(e,t){process.emitWarning(`dsh-sticky: skipped unreadable note ${e}: ${w(t)}`,{code:`DSH_STICKY_SKIPPED_NOTE`})}};const T=`notes-asset:`,E={"image/png":`png`,"image/jpeg":`jpg`,"image/webp":`webp`,"image/gif":`gif`},ie=/^(\d{10,16})-([0-9a-f]{6})\.(png|jpg|webp|gif)$/;function D(e){return typeof e==`string`&&Object.hasOwn(E,e)}function O(e){return E[e]}function k(e){let t=/\.([a-z]+)$/.exec(e)?.[1];if(t===void 0)return;let n=Object.entries(E).find(([,e])=>e===t);return n===void 0?void 0:n[0]}function ae(e,t){return`${T}${e}/${t}`}function A(e){if(typeof e!=`string`||!e.startsWith(`notes-asset:`))return;let t=e.slice(12),n=t.indexOf(`/`);if(n<=0)return;let r=t.slice(0,n),i=t.slice(n+1);if(d.test(r)&&ie.test(i))return{noteId:r,file:i}}function oe(){return/notes-asset:[^\s)]+/g}function j(e){let t=/* @__PURE__ */ new Set;for(let n of e.matchAll(oe())){let e=A(n[0]);e!==void 0&&t.add(e.file)}return[...t]}function M(e){return e?.code===`ENOENT`}var N=class{directory;now;ready;constructor(e,t=Date.now){this.directory=e,this.now=t}async ensure(){this.ready??=r(this.directory,{recursive:!0,mode:448}).then(()=>void 0,e=>{throw this.ready=void 0,new h(`sticky/io`,`cannot create ${this.directory}: ${w(e)}`)}),await this.ready}directoryOf(t){return e(this.directory,t)}async write(n,i,a){if(a.byteLength===0)throw new h(`sticky/bad-request`,`image is empty`);if(a.byteLength>5242880)throw new h(`sticky/bad-request`,`image exceeds 5242880 bytes`);await this.ensure();let l=this.directoryOf(n);try{await r(l,{recursive:!0,mode:448})}catch(e){throw new h(`sticky/io`,`cannot create ${l}: ${w(e)}`)}let u=`${String(this.now())}-${t(3).toString(`hex`)}.${O(i)}`,d=e(l,u),f=`${d}..tmp`;try{await c(f,a,{mode:384}),await o(f,d)}catch(e){throw await s(f,{force:!0}).catch(()=>void 0),new h(`sticky/io`,`cannot write image ${u}: ${w(e)}`)}return u}async read(t,n){let r=k(n);if(r===void 0)throw new h(`sticky/bad-request`,`not an image file: ${n}`);let a;try{a=await i(e(this.directoryOf(t),n))}catch(e){throw M(e)?new h(`sticky/not-found`,`no image ${n}`):new h(`sticky/io`,`cannot read image ${n}: ${w(e)}`)}return{mediaType:r,data:a.toString(`base64`)}}async removeNote(e){try{await s(this.directoryOf(e),{recursive:!0,force:!0})}catch(t){throw new h(`sticky/io`,`cannot delete images for ${e}: ${w(t)}`)}}async collect(t,n,r){if(n===void 0)return;let i=new Set(j(r)),a=j(n).filter(e=>!i.has(e));await Promise.all(a.map(async n=>{try{await s(e(this.directoryOf(t),n),{force:!0})}catch(e){process.emitWarning(`dsh-sticky: could not remove image ${n}: ${w(e)}`,{code:`DSH_STICKY_IMAGE_DELETE_FAILED`})}}))}async sweep(t,n){let r=/* @__PURE__ */ new Map;for(let e of n)r.set(e.id,new Set(j(e.markdown)));let i=/* @__PURE__ */ new Map;for(let e of t)i.set(e,r.get(e));let o;try{o=await a(this.directory)}catch(e){if(M(e))return 0;throw new h(`sticky/io`,`cannot read ${this.directory}: ${w(e)}`)}let c=0;for(let t of o){if(!i.has(t)){try{c+=(await a(this.directoryOf(t))).length}catch{
|
|
12
|
+
r!==void 0&&e.push(r)}return e.sort(te)}async ids(){let e=[];for(let t of await this.scan()){let n=this.documentIdOf(t);n!==void 0&&e.push(n)}return e}async get(e){return S(e),await this.readOne(e)}fieldsOfCreate(e){let t=e.markdown??``;if(t.length>2e5)throw new h(`sticky/bad-request`,`markdown exceeds 200000 characters`);let n=typeof e.title==`string`?e.title.trim():``;if(n.length>200)throw new h(`sticky/bad-request`,`title exceeds 200 characters`);let r=e.color??`yellow`;if(!g(r))throw new h(`sticky/bad-request`,`unknown color ${JSON.stringify(r)}`);return{markdown:t,color:r,title:n===``?y(t,this.labels.untitled):n,imageWidths:p(e.imageWidths)}}fieldsOfSave(e){if(e.title===void 0||e.markdown===void 0||e.color===void 0)throw new h(`sticky/bad-request`,`a save carries title, markdown, and color`);return this.fieldsOfCreate(e)}async save(e){if(e.id===void 0)throw new h(`sticky/bad-request`,`id is required`);let t=this.fieldsOfSave(e),n=S(e.id);return await this.serialize(n,async()=>{let r=await this.readOne(n);if(r===void 0)throw new h(`sticky/not-found`,`no note ${JSON.stringify(n)}`);let i={...t,imageWidths:e.imageWidths===void 0?r.imageWidths:t.imageWidths},a={...r,...i,pinned:e.pinned??r.pinned,updatedAt:x(r,i)?this.now():r.updatedAt},o=await this.write(a);return await this.images?.collect(n,r.markdown,i.markdown),o})}async create(e,t){let n=S(e),{markdown:r,color:i,title:a,imageWidths:o}=this.fieldsOfCreate(t),s=this.now();return await this.serialize(n,async()=>{if(await this.readOne(n)!==void 0)throw new h(`sticky/conflict`,`note ${JSON.stringify(n)} already exists`);let e={version:1,id:n,title:a,markdown:r,color:i,pinned:t.pinned===!0,archived:!1,imageWidths:o,createdAt:s,updatedAt:s};return await this.write(e)})}async setArchived(e,t){return await this.patch(S(e),{archived:t})}async setPinned(e,t){return await this.patch(S(e),{pinned:t})}async remove(e){let t=S(e);await this.ensure(),await this.serialize(t,async()=>{try{await s(this.pathOf(t),{force:!0})}catch(e){throw new h(`sticky/io`,`cannot delete note ${JSON.stringify(t)}: ${w(e)}`)}await this.images?.removeNote(t)})}async patch(e,t){return await this.serialize(e,async()=>{let n=await this.readOne(e);if(n===void 0)throw new h(`sticky/not-found`,`no note ${JSON.stringify(e)}`);return await this.write({...n,...t})})}pathOf(t){return e(this.directory,`${t}.json`)}async readOne(e){let t;try{t=await i(this.pathOf(e),`utf8`)}catch(t){if(C(t))return;throw new h(`sticky/io`,`cannot read note ${JSON.stringify(e)}: ${w(t)}`)}let n;try{n=JSON.parse(t)}catch(t){throw b(`note ${JSON.stringify(e)} is not JSON: ${w(t)}`)}return ee(n,e,this.labels.untitled)}async write(e){await this.ensure();let t=e.archived?{...e,pinned:!1}:e,r=this.pathOf(t.id);if(t.id!==this.documentIdOf(`${t.id}.json`))throw new h(`sticky/bad-request`,`cannot write note ${JSON.stringify(t.id)} under a different id`);let i=`${r}.${String(process.pid)}.${n().slice(0,8)}${m}`;try{await c(i,`${JSON.stringify(t,void 0,2)}\n`,{encoding:`utf8`,mode:384}),await o(i,r)}catch(e){throw await s(i,{force:!0}).catch(()=>void 0),new h(`sticky/io`,`cannot write note ${JSON.stringify(t.id)}: ${w(e)}`)}return t}async serialize(e,t){let n=(this.chains.get(e)??Promise.resolve()).then(t,t);this.chains.set(e,n);try{return await n}finally{this.chains.get(e)===n&&this.chains.delete(e)}}warn(e,t){process.emitWarning(`dsh-sticky: skipped unreadable note ${e}: ${w(t)}`,{code:`DSH_STICKY_SKIPPED_NOTE`})}};const T=`notes-asset:`,E={"image/png":`png`,"image/jpeg":`jpg`,"image/webp":`webp`,"image/gif":`gif`},ie=/^(\d{1,16})-([0-9a-f]{6})\.(png|jpg|webp|gif)$/;function D(e){return typeof e==`string`&&Object.hasOwn(E,e)}function O(e){return E[e]}function k(e){let t=/\.([a-z]+)$/.exec(e)?.[1];if(t===void 0)return;let n=Object.entries(E).find(([,e])=>e===t);return n===void 0?void 0:n[0]}function ae(e,t){return`${T}${e}/${t}`}function A(e){if(typeof e!=`string`||!e.startsWith(`notes-asset:`))return;let t=e.slice(12),n=t.indexOf(`/`);if(n<=0)return;let r=t.slice(0,n),i=t.slice(n+1);if(d.test(r)&&ie.test(i))return{noteId:r,file:i}}function oe(){return/notes-asset:[^\s)]+/g}function j(e){let t=/* @__PURE__ */ new Set;for(let n of e.matchAll(oe())){let e=A(n[0]);e!==void 0&&t.add(e.file)}return[...t]}function M(e){return e?.code===`ENOENT`}var N=class{directory;now;ready;constructor(e,t=Date.now){this.directory=e,this.now=t}async ensure(){this.ready??=r(this.directory,{recursive:!0,mode:448}).then(()=>void 0,e=>{throw this.ready=void 0,new h(`sticky/io`,`cannot create ${this.directory}: ${w(e)}`)}),await this.ready}directoryOf(t){return e(this.directory,t)}async write(n,i,a){if(a.byteLength===0)throw new h(`sticky/bad-request`,`image is empty`);if(a.byteLength>5242880)throw new h(`sticky/bad-request`,`image exceeds 5242880 bytes`);await this.ensure();let l=this.directoryOf(n);try{await r(l,{recursive:!0,mode:448})}catch(e){throw new h(`sticky/io`,`cannot create ${l}: ${w(e)}`)}let u=`${String(this.now())}-${t(3).toString(`hex`)}.${O(i)}`,d=e(l,u),f=`${d}..tmp`;try{await c(f,a,{mode:384}),await o(f,d)}catch(e){throw await s(f,{force:!0}).catch(()=>void 0),new h(`sticky/io`,`cannot write image ${u}: ${w(e)}`)}return u}async read(t,n){let r=k(n);if(r===void 0)throw new h(`sticky/bad-request`,`not an image file: ${n}`);let a;try{a=await i(e(this.directoryOf(t),n))}catch(e){throw M(e)?new h(`sticky/not-found`,`no image ${n}`):new h(`sticky/io`,`cannot read image ${n}: ${w(e)}`)}return{mediaType:r,data:a.toString(`base64`)}}async removeNote(e){try{await s(this.directoryOf(e),{recursive:!0,force:!0})}catch(t){throw new h(`sticky/io`,`cannot delete images for ${e}: ${w(t)}`)}}async collect(t,n,r){if(n===void 0)return;let i=new Set(j(r)),a=j(n).filter(e=>!i.has(e));await Promise.all(a.map(async n=>{try{await s(e(this.directoryOf(t),n),{force:!0})}catch(e){process.emitWarning(`dsh-sticky: could not remove image ${n}: ${w(e)}`,{code:`DSH_STICKY_IMAGE_DELETE_FAILED`})}}))}async sweep(t,n){let r=/* @__PURE__ */ new Map;for(let e of n)r.set(e.id,new Set(j(e.markdown)));let i=/* @__PURE__ */ new Map;for(let e of t)i.set(e,r.get(e));let o;try{o=await a(this.directory)}catch(e){if(M(e))return 0;throw new h(`sticky/io`,`cannot read ${this.directory}: ${w(e)}`)}let c=0;for(let t of o){if(!i.has(t)){try{c+=(await a(this.directoryOf(t))).length}catch{
|
|
19
13
|
/* v8 ignore next -- the directory was listed a moment ago; a failure
|
|
20
14
|
here is an unreproducible race, and the remove below still runs. */
|
|
21
15
|
c+=0}await s(this.directoryOf(t),{recursive:!0,force:!0});continue}let n=i.get(t);if(n===void 0)continue;let r;try{r=await a(this.directoryOf(t))}catch{
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-sticky",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"description": "侧栏便签:卡片墙 + 文档式 Markdown 编辑器,便签存在 $DSH_HOME/storages/dsh-sticky/notes 下的独立 JSON 文件里。",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -105,8 +105,8 @@
|
|
|
105
105
|
"react-dom": "18.3.1"
|
|
106
106
|
},
|
|
107
107
|
"engines": {
|
|
108
|
-
"node": "
|
|
109
|
-
"dsh": ">=0.2.2-rc.3"
|
|
108
|
+
"node": "^24.22.3 || >=26.13.1",
|
|
109
|
+
"dsh": ">=0.2.2-rc.3 <0.2.4-0"
|
|
110
110
|
},
|
|
111
111
|
"publishConfig": {
|
|
112
112
|
"access": "public"
|