forked from bluesky-social/atproto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* handle package * forgot a couple things * few more fixes
- Loading branch information
Showing
14 changed files
with
158 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Handle | ||
|
||
Validation logic for AT handles | ||
|
||
## Usage | ||
|
||
```typescript | ||
import * as handle from '@atproto/handle' | ||
|
||
isValid('alice.test', ['.test']) // returns true | ||
ensureValid('alice.test', ['.test']) // returns void | ||
|
||
isValid('al!ce.test', ['.test']) // returns false | ||
isValid('al!ce.test', ['.test']) // throws | ||
``` | ||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('../../babel.config.js') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const base = require('../../jest.config.base.js') | ||
|
||
module.exports = { | ||
...base, | ||
displayName: 'Handle', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "@atproto/handle", | ||
"version": "0.0.1", | ||
"main": "src/index.ts", | ||
"scripts": { | ||
"test": "jest", | ||
"prettier": "prettier --check src/", | ||
"prettier:fix": "prettier --write src/", | ||
"lint": "eslint . --ext .ts,.tsx", | ||
"lint:fix": "yarn lint --fix", | ||
"verify": "run-p prettier lint", | ||
"verify:fix": "yarn prettier:fix && yarn lint:fix", | ||
"build": "esbuild src/index.ts --define:process.env.NODE_ENV=\\\"production\\\" --bundle --platform=node --sourcemap --outfile=dist/index.js", | ||
"postbuild": "tsc --build tsconfig.build.json" | ||
}, | ||
"license": "MIT", | ||
"dependencies": { | ||
"@sideway/address": "^5.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import * as address from '@sideway/address' | ||
import { reservedSubdomains } from './reserved' | ||
|
||
export const ensureValid = ( | ||
handle: string, | ||
availableUserDomains: string[], | ||
): void => { | ||
if (handle.startsWith('did:')) { | ||
throw new InvalidHandleError( | ||
'Cannot register a handle that starts with `did:`', | ||
) | ||
} | ||
const supportedDomain = availableUserDomains.find((domain) => | ||
handle.endsWith(domain), | ||
) | ||
if (!supportedDomain) { | ||
throw new InvalidHandleError('Not a supported handle domain') | ||
} | ||
const front = handle.slice(0, handle.length - supportedDomain.length) | ||
if (front.length < 2) { | ||
throw new InvalidHandleError('Handle too short') | ||
} else if (front.length > 20) { | ||
throw new InvalidHandleError('Handle too long') | ||
} | ||
|
||
if (reservedSubdomains[front]) { | ||
throw new InvalidHandleError('Reserved handle') | ||
} | ||
|
||
if (front.indexOf('.') > -1) { | ||
throw new InvalidHandleError('Invalid characters in handle') | ||
} | ||
|
||
const isValid = address.isDomainValid(handle) | ||
if (!isValid) { | ||
throw new InvalidHandleError('Invalid characters in handle') | ||
} | ||
} | ||
export const isValid = ( | ||
handle: string, | ||
availableUserDomains: string[], | ||
): boolean => { | ||
try { | ||
ensureValid(handle, availableUserDomains) | ||
} catch (err) { | ||
if (err instanceof InvalidHandleError) { | ||
return false | ||
} | ||
throw err | ||
} | ||
return true | ||
} | ||
|
||
export class InvalidHandleError extends Error {} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { ensureValid } from '../src' | ||
|
||
describe('handle validation', () => { | ||
const domains = ['.bsky.app', '.test'] | ||
const check = (toCheck: string) => () => { | ||
return ensureValid(toCheck, domains) | ||
} | ||
|
||
it('allows valid handles', () => { | ||
check('john.test') | ||
check('john.bsky.app') | ||
}) | ||
it('errors on invalid handles', () => { | ||
expect(check('did:john.test')).toThrow( | ||
'Cannot register a handle that starts with `did:`', | ||
) | ||
expect(check('john.bsky.io')).toThrow('Not a supported handle domain') | ||
expect(check('john.com')).toThrow('Not a supported handle domain') | ||
expect(check('j.test')).toThrow('Handle too short') | ||
expect(check('jayromy-johnber123456.test')).toThrow('Handle too long') | ||
expect(check('john.test.bsky.app')).toThrow('Invalid characters in handle') | ||
expect(check('john..test')).toThrow('Invalid characters in handle') | ||
expect(check('jo_hn.test')).toThrow('Invalid characters in handle') | ||
expect(check('jo!hn.test')).toThrow('Invalid characters in handle') | ||
expect(check('jo%hn.test')).toThrow('Invalid characters in handle') | ||
expect(check('jo&hn.test')).toThrow('Invalid characters in handle') | ||
expect(check('jo*hn.test')).toThrow('Invalid characters in handle') | ||
expect(check('jo|hn.test')).toThrow('Invalid characters in handle') | ||
expect(check('jo:hn.test')).toThrow('Invalid characters in handle') | ||
expect(check('jo/hn.test')).toThrow('Invalid characters in handle') | ||
expect(check('about.test')).toThrow('Reserved handle') | ||
expect(check('atp.test')).toThrow('Reserved handle') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"exclude": ["**/*.spec.ts", "**/*.test.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "./dist", // Your outDir, | ||
"emitDeclarationOnly": true | ||
}, | ||
"include": ["./src","__tests__/**/**.ts"], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters