Skip to content

Commit 0e252e9

Browse files
committed
feat(@sirutils/nuts): init structure
1 parent a0efe18 commit 0e252e9

File tree

19 files changed

+210
-31
lines changed

19 files changed

+210
-31
lines changed

bun.lockb

64 Bytes
Binary file not shown.

packages/nuts/package.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"files": ["dist"],
77
"exports": {
88
".": {
9-
"default": "./dist/index.js",
10-
"types": "./dist/index.d.ts"
9+
"default": "./dist/core/index.js",
10+
"types": "./dist/core/index.d.ts"
1111
}
1212
},
1313

@@ -19,11 +19,15 @@
1919
},
2020

2121
"devDependencies": {
22-
"@sirutils/builder": "workspace:*"
22+
"@sirutils/builder": "workspace:*",
23+
"cbor-x": "^1.6.0"
2324
},
2425
"dependencies": {
2526
"@sirutils/core": "workspace:*",
2627
"@sirutils/schema": "workspace:*",
2728
"@sirutils/toolbox": "workspace:*"
29+
},
30+
"peerDependencies": {
31+
"cbor-x": "^1.6.0"
2832
}
2933
}
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import type { Duplex } from 'node:stream'
2+
import type { BlobType } from '@sirutils/core'
3+
4+
import type { createNuts } from '../plugin/create'
5+
import type { NutsTags } from '../tag'
6+
7+
declare global {
8+
namespace Sirutils {
9+
interface CustomErrors {
10+
nuts: NutsTags
11+
}
12+
13+
interface PluginDefinitions {
14+
nuts: Sirutils.PluginSystem.ExtractDefinition<typeof createNuts>
15+
'nuts-serializer': Sirutils.PluginSystem.Context<BlobType, Sirutils.Nuts.SerializerApi>
16+
}
17+
18+
namespace Nuts {
19+
interface Options {
20+
id?: string
21+
environment?: 'production' | 'development' | 'test'
22+
23+
port?: string
24+
host?: string
25+
logs?: boolean
26+
}
27+
28+
interface Broker {}
29+
30+
interface BaseApi {}
31+
interface BrokerApi {
32+
broker: Sirutils.Nuts.Broker
33+
}
34+
35+
type Context = Sirutils.PluginSystem.Context<
36+
Sirutils.Nuts.Options,
37+
Sirutils.Nuts.BaseApi & Sirutils.Nuts.BrokerApi
38+
>
39+
40+
interface SerializerApi {
41+
encode: <T = unknown>(data: T) => Buffer
42+
decode: <T = unknown>(data: string | Buffer) => T
43+
44+
encodeStream: () => Duplex
45+
decodeStream: () => Duplex
46+
}
47+
}
48+
}
49+
}
File renamed without changes.
File renamed without changes.
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { createPlugin } from '@sirutils/core'
2+
3+
import { nutsTags } from '../tag'
4+
import { brokerActions } from './internal/broker'
5+
6+
export const createNuts = createPlugin<Sirutils.Nuts.Options, Sirutils.Nuts.BaseApi>(
7+
{
8+
name: 'nuts',
9+
version: '0.2.3',
10+
11+
dependencies: {
12+
'nuts-serializer': '*',
13+
},
14+
},
15+
ctx => {
16+
return {
17+
broker: {},
18+
}
19+
},
20+
nutsTags.plugin
21+
)
22+
.register(brokerActions)
23+
.lock()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { createActions } from '@sirutils/core'
2+
3+
import { nutsTags } from '../../tag'
4+
5+
export const brokerActions = createActions(
6+
(context: Sirutils.Nuts.Context): Sirutils.Nuts.BrokerApi => {
7+
const serializer = context.lookup('nuts-serializer')
8+
9+
return {
10+
broker: {},
11+
}
12+
},
13+
nutsTags.broker
14+
)
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import { createTag } from './internal/tag'
1+
import { tagBuilder } from '@sirutils/core'
2+
3+
export const createTag = tagBuilder('@sirutils/nuts')
24

35
export const nutsTags = {
46
// internal
57
logger: createTag('logger'),
68

79
plugin: createTag('plugin'),
10+
broker: createTag('broker'),
811
} as const
912

1013
export type NutsTags = (typeof nutsTags)[keyof typeof nutsTags]

packages/nuts/src/definitions/nuts.ts

-11
This file was deleted.

packages/nuts/src/internal/tag.ts

-3
This file was deleted.

packages/nuts/src/plugin/create.ts

-11
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type { Options as CborOptions, Decoder, Encoder } from 'cbor-x'
2+
3+
import type { createCbor } from '../plugin/create'
4+
import type { CborTags } from '../tag'
5+
6+
declare global {
7+
namespace Sirutils {
8+
interface CustomErrors {
9+
'nuts-cbor': CborTags
10+
}
11+
12+
interface PluginDefinitions {
13+
'nuts-cbor': Sirutils.PluginSystem.ExtractDefinition<typeof createCbor>
14+
}
15+
16+
namespace Nuts {
17+
namespace Cbor {
18+
interface Options extends CborOptions {}
19+
20+
interface BaseApi {
21+
$encoder: Encoder
22+
$decoder: Decoder
23+
}
24+
25+
interface SerializerApi extends Sirutils.Nuts.SerializerApi {}
26+
27+
type Context = Sirutils.PluginSystem.Context<
28+
Sirutils.Nuts.Cbor.Options,
29+
Sirutils.Nuts.Cbor.BaseApi & Sirutils.Nuts.Cbor.SerializerApi
30+
>
31+
}
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/// <reference types="@sirutils/core" />
2+
/// <reference types="@sirutils/toolbox" />
3+
/// <reference types="@sirutils/schema" />
4+
5+
import '../../core'
6+
import './cbor'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import './definitions'
2+
3+
export * from './plugin/create'
4+
5+
export * from './tag'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { createPlugin } from '@sirutils/core'
2+
import { Decoder, Encoder } from 'cbor-x'
3+
4+
import { cborTags } from '../tag'
5+
import { serializerApiActions } from './internal/api'
6+
7+
export const createCbor = createPlugin<Sirutils.Nuts.Cbor.Options, Sirutils.Nuts.Cbor.BaseApi>(
8+
{
9+
name: 'nuts-serializer', // cbor
10+
version: '0.2.3',
11+
12+
dependencies: {},
13+
},
14+
context => {
15+
const $encoder = new Encoder(context.options)
16+
const $decoder = new Decoder(context.options)
17+
18+
return {
19+
$encoder,
20+
$decoder,
21+
}
22+
},
23+
cborTags.plugin,
24+
{}
25+
)
26+
.register(serializerApiActions)
27+
.lock()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { createActions } from '@sirutils/core'
2+
import { DecoderStream, EncoderStream } from 'cbor-x'
3+
4+
import { cborTags } from '../../tag'
5+
6+
export const serializerApiActions = createActions(
7+
(context: Sirutils.Nuts.Cbor.Context): Sirutils.Nuts.Cbor.SerializerApi => {
8+
return {
9+
encode: data => {
10+
return context.api.$encoder.encode(data)
11+
},
12+
decode: data => {
13+
if (typeof data === 'string') {
14+
return context.api.$decoder.decode(Buffer.from(data))
15+
}
16+
17+
return context.api.$decoder.decode(data)
18+
},
19+
20+
encodeStream: () => new EncoderStream(context.options),
21+
decodeStream: () => new DecoderStream(context.options),
22+
}
23+
},
24+
cborTags.api
25+
)
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { tagBuilder } from '@sirutils/core'
2+
3+
export const createTag = tagBuilder('@sirutils/nuts#cbor')
4+
5+
export const cborTags = {
6+
plugin: createTag('plugin'),
7+
8+
api: createTag('api'),
9+
} as const
10+
11+
export type CborTags = (typeof cborTags)[keyof typeof cborTags]

packages/nuts/test/app.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
import { createNuts } from '../src'
1+
import { createNuts } from '../src/core'
2+
import { createCbor } from '../src/serializer-cbor'
23

3-
export const nuts = await createNuts()
4+
const [serializer] = await Promise.all([createCbor()])
5+
6+
export const nuts = await createNuts({}, serializer)

0 commit comments

Comments
 (0)