FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScriptSolidServer/clock-updater.mjs at gh-pages · JavaScriptSolidServer/JavaScriptSolidServer · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
JavaScriptSolidServer
/
JavaScriptSolidServer
Public
Notifications
You must be signed in to change notification settings
Fork
10
Star
28
Code
Issues
162
Pull requests
10
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScriptSolidServer
/
clock-updater.mjs
Copy path
More file actions
More file actions
Latest commit
History
History
History
61 lines (51 loc) · 1.82 KB
Breadcrumbs
JavaScriptSolidServer
/
clock-updater.mjs
Copy path
File metadata and controls
61 lines (51 loc) · 1.82 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Clock Updater - Updates the Solid clock every second using Nostr auth
* Usage: node clock-updater.mjs
*/
import
{
getPublicKey
,
nip98Token
}
from
'./src/nostr/event.js'
;
// Nostr keypair (in production, load from env/file)
const
SK_HEX
=
'3f188544fb81bd324ead7be9697fd9503d18345e233a7b0182915b0b582ddd70'
;
const
sk
=
Uint8Array
.
from
(
Buffer
.
from
(
SK_HEX
,
'hex'
)
)
;
const
pk
=
getPublicKey
(
sk
)
;
const
CLOCK_URL
=
'https://solid.social/melvin/public/clock.json'
;
async
function
updateClock
(
)
{
const
now
=
Math
.
floor
(
Date
.
now
(
)
/
1000
)
;
const
isoDate
=
new
Date
(
now
*
1000
)
.
toISOString
(
)
;
const
clockData
=
{
'@context'
:
{
'schema'
:
'http://schema.org/'
}
,
'@id'
:
'#clock'
,
'@type'
:
'schema:Clock'
,
'schema:dateModified'
:
isoDate
,
'schema:value'
:
now
}
;
try
{
// Serialize once: the same bytes feed both the NIP-98 payload hash
// and the fetch body. nip98Token requires bytes (not an object) so
// the `payload` tag matches what the server actually receives.
const
bodyBytes
=
JSON
.
stringify
(
clockData
)
;
const
token
=
nip98Token
(
CLOCK_URL
,
'PUT'
,
sk
,
bodyBytes
)
;
const
res
=
await
fetch
(
CLOCK_URL
,
{
method
:
'PUT'
,
headers
:
{
'Content-Type'
:
'application/ld+json'
,
'Authorization'
:
'Nostr '
+
token
}
,
body
:
bodyBytes
}
)
;
const
time
=
isoDate
.
split
(
'T'
)
[
1
]
.
replace
(
'Z'
,
''
)
;
if
(
res
.
ok
)
{
process
.
stdout
.
write
(
`\r
${
time
}
- Updated`
)
;
}
else
{
console
.
log
(
`\n
${
time
}
- Error:
${
res
.
status
}
${
res
.
statusText
}
`
)
;
}
}
catch
(
err
)
{
console
.
log
(
`\nError:
${
err
.
message
}
`
)
;
}
}
console
.
log
(
'Clock Updater started'
)
;
console
.
log
(
'did:nostr:'
,
'did:nostr:'
+
pk
)
;
console
.
log
(
'Target:'
,
CLOCK_URL
)
;
console
.
log
(
'Press Ctrl+C to stop\n'
)
;
// Run immediately, then every second
updateClock
(
)
;
setInterval
(
updateClock
,
1000
)
;
Back
|
FazBrowse Home
|
New Git URL