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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//! NSID - Namespaced Identifier
//!
//! nsids identify lexicon schemas and record types.
//! format: <reversed-domain>.<name>
//!
//! validation rules:
//! - max 317 characters
//! - 3+ segments separated by dots
//! - domain authority: reversed domain (lowercase + digits + hyphens)
//! - name segment: letters and digits only, cannot start with digit
//! - each segment: 1-63 characters
//!
//! examples:
//! - app.bsky.feed.post
//! - com.atproto.repo.createRecord
//!
//! see: https://atproto.com/specs/nsid
const std = @import("std");
pub const Nsid = struct {
/// the nsid string (borrowed, not owned)
raw: []const u8,
/// offset where the name segment starts
name_start: usize,
pub const max_length = 317;
pub const max_segment_length = 63;
/// parse an nsid string. returns null if invalid.
pub fn parse(s: []const u8) ?Nsid {
// length check
if (s.len == 0 or s.len > max_length) return null;
var segment_count: usize = 0;
var segment_start: usize = 0;
var last_dot: usize = 0;
for (s, 0..) |c, i| {
if (c == '.') {
const segment = s[segment_start..i];
// all segments except last must be valid domain segments
if (!isValidDomainSegment(segment, segment_count == 0)) return null;
segment_count += 1;
last_dot = i;
segment_start = i + 1;
}
}
// validate final segment (name)
const name_seg = s[segment_start..];
if (!isValidNameSegment(name_seg)) return null;
segment_count += 1;
// must have at least 3 segments
if (segment_count < 3) return null;
return .{
.raw = s,
.name_start = last_dot + 1,
};
}
/// the full nsid string
pub fn str(self: Nsid) []const u8 {
return self.raw;
}
/// the domain authority portion (reversed domain)
pub fn authority(self: Nsid) []const u8 {
return self.raw[0 .. self.name_start - 1];
}
/// the name segment
pub fn name(self: Nsid) []const u8 {
return self.raw[self.name_start..];
}
/// the authority as a domain name: the authority segments reversed, the
/// name dropped ("app.bsky.feed.post" -> "feed.bsky.app"). lexicon
/// resolution looks up `_lexicon.` + this name, and only this name.
/// writes into `buf` and returns the written slice.
pub fn authorityDomain(self: Nsid, buf: *[max_length]u8) []const u8 {
const auth = self.authority();
var pos: usize = 0;
var segments = std.mem.splitBackwardsScalar(u8, auth, '.');
while (segments.next()) |segment| {
if (pos != 0) {
buf[pos] = '.';
pos += 1;
}
@memcpy(buf[pos..][0..segment.len], segment);
pos += segment.len;
}
return buf[0..pos];
}
fn isValidDomainSegment(seg: []const u8, is_first: bool) bool {
// 1-63 characters
if (seg.len == 0 or seg.len > max_segment_length) return false;
// cannot start or end with hyphen
if (seg[0] == '-' or seg[seg.len - 1] == '-') return false;
// first segment (TLD) must start with a letter
if (is_first and !(seg[0] >= 'a' and seg[0] <= 'z')) return false;
// lowercase letters, digits, and hyphens only
for (seg) |c| {
const valid = (c >= 'a' and c <= 'z') or
(c >= '0' and c <= '9') or
c == '-';
if (!valid) return false;
}
return true;
}
fn isValidNameSegment(seg: []const u8) bool {
// 1-63 characters
if (seg.len == 0 or seg.len > max_segment_length) return false;
// cannot start with digit
const first = seg[0];
if (first >= '0' and first <= '9') return false;
// letters and digits only (no hyphens in name)
for (seg) |c| {
const valid = (c >= 'a' and c <= 'z') or
(c >= 'A' and c <= 'Z') or
(c >= '0' and c <= '9');
if (!valid) return false;
}
return true;
}
};
// === tests from atproto.com/specs/nsid ===
test "valid: common nsids" {
const nsid1 = Nsid.parse("app.bsky.feed.post") orelse return error.InvalidNsid;
try std.testing.expectEqualStrings("app.bsky.feed", nsid1.authority());
try std.testing.expectEqualStrings("post", nsid1.name());
const nsid2 = Nsid.parse("com.atproto.repo.createRecord") orelse return error.InvalidNsid;
try std.testing.expectEqualStrings("com.atproto.repo", nsid2.authority());
try std.testing.expectEqualStrings("createRecord", nsid2.name());
}
test "valid: minimum 3 segments" {
try std.testing.expect(Nsid.parse("a.b.c") != null);
try std.testing.expect(Nsid.parse("com.example.thing") != null);
}
test "valid: many segments" {
try std.testing.expect(Nsid.parse("net.users.bob.ping") != null);
try std.testing.expect(Nsid.parse("a.b.c.d.e.f") != null);
}
test "valid: name with numbers" {
try std.testing.expect(Nsid.parse("com.example.thing2") != null);
try std.testing.expect(Nsid.parse("app.bsky.feed.getPost1") != null);
}
test "valid: mixed case in name" {
try std.testing.expect(Nsid.parse("com.example.fooBar") != null);
try std.testing.expect(Nsid.parse("com.example.FooBar") != null);
}
test "invalid: only 2 segments" {
try std.testing.expect(Nsid.parse("com.example") == null);
try std.testing.expect(Nsid.parse("a.b") == null);
}
test "invalid: name starts with digit" {
try std.testing.expect(Nsid.parse("com.example.3") == null);
try std.testing.expect(Nsid.parse("com.example.3thing") == null);
}
test "invalid: name contains hyphen" {
try std.testing.expect(Nsid.parse("com.example.foo-bar") == null);
}
test "invalid: domain segment uppercase" {
try std.testing.expect(Nsid.parse("COM.example.thing") == null);
try std.testing.expect(Nsid.parse("com.EXAMPLE.thing") == null);
}
test "invalid: empty segment" {
try std.testing.expect(Nsid.parse(".example.thing") == null);
try std.testing.expect(Nsid.parse("com..thing") == null);
try std.testing.expect(Nsid.parse("com.example.") == null);
}
test "invalid: segment starts with hyphen" {
try std.testing.expect(Nsid.parse("-com.example.thing") == null);
try std.testing.expect(Nsid.parse("com.-example.thing") == null);
}
test "invalid: segment ends with hyphen" {
try std.testing.expect(Nsid.parse("com-.example.thing") == null);
try std.testing.expect(Nsid.parse("com.example-.thing") == null);
}
test "invalid: non-ascii" {
// this would be "com.exa💩ple.thing" but we just use a byte > 127
var buf = "com.example.thing".*;
buf[5] = 200; // non-ascii byte
try std.testing.expect(Nsid.parse(&buf) == null);
}
test "invalid: special characters" {
try std.testing.expect(Nsid.parse("com.example.thing!") == null);
try std.testing.expect(Nsid.parse("com.example.thing@") == null);
try std.testing.expect(Nsid.parse("com.example.thing*") == null);
}
test "authorityDomain reverses the authority and drops the name" {
var buf: [Nsid.max_length]u8 = undefined;
// the lexicon spec's examples
try std.testing.expectEqualStrings("toy.app", Nsid.parse("app.toy.record").?.authorityDomain(&buf));
try std.testing.expectEqualStrings(
"blogging.lab.dept.university.edu",
Nsid.parse("edu.university.dept.lab.blogging.getBlogPost").?.authorityDomain(&buf),
);
try std.testing.expectEqualStrings("feed.bsky.app", Nsid.parse("app.bsky.feed.post").?.authorityDomain(&buf));
}