@atproto/syntax
Advanced tools
Comparing version 0.1.4 to 0.1.5
# @atproto/syntax | ||
## 0.1.5 | ||
### Patch Changes | ||
- [#1908](https://github.com/bluesky-social/atproto/pull/1908) [`3c0ef382`](https://github.com/bluesky-social/atproto/commit/3c0ef382c12a413cc971ae47ffb341236c545f60) Thanks [@gaearon](https://github.com/gaearon)! - prevent unnecessary throw/catch on uri syntax | ||
## 0.1.4 | ||
@@ -4,0 +10,0 @@ |
@@ -5,1 +5,4 @@ export * from './handle'; | ||
export * from './aturi'; | ||
export * from './tid'; | ||
export * from './recordkey'; | ||
export * from './datetime'; |
@@ -28,5 +28,8 @@ "use strict"; | ||
INVALID_HANDLE: () => INVALID_HANDLE, | ||
InvalidDatetimeError: () => InvalidDatetimeError, | ||
InvalidDidError: () => InvalidDidError, | ||
InvalidHandleError: () => InvalidHandleError, | ||
InvalidNsidError: () => InvalidNsidError, | ||
InvalidRecordKeyError: () => InvalidRecordKeyError, | ||
InvalidTidError: () => InvalidTidError, | ||
NSID: () => NSID, | ||
@@ -37,2 +40,3 @@ ReservedHandleError: () => ReservedHandleError, | ||
ensureValidAtUriRegex: () => ensureValidAtUriRegex, | ||
ensureValidDatetime: () => ensureValidDatetime, | ||
ensureValidDid: () => ensureValidDid, | ||
@@ -44,5 +48,12 @@ ensureValidDidRegex: () => ensureValidDidRegex, | ||
ensureValidNsidRegex: () => ensureValidNsidRegex, | ||
ensureValidRecordKey: () => ensureValidRecordKey, | ||
ensureValidTid: () => ensureValidTid, | ||
isValidDatetime: () => isValidDatetime, | ||
isValidHandle: () => isValidHandle, | ||
isValidRecordKey: () => isValidRecordKey, | ||
isValidTid: () => isValidTid, | ||
isValidTld: () => isValidTld, | ||
normalizeAndEnsureValidHandle: () => normalizeAndEnsureValidHandle, | ||
normalizeDatetime: () => normalizeDatetime, | ||
normalizeDatetimeAlways: () => normalizeDatetimeAlways, | ||
normalizeHandle: () => normalizeHandle | ||
@@ -256,9 +267,9 @@ }); | ||
try { | ||
ensureValidHandle(parts[2]); | ||
} catch { | ||
try { | ||
if (parts[2].startsWith("did:")) { | ||
ensureValidDid(parts[2]); | ||
} catch { | ||
throw new Error("ATURI authority must be a valid handle or DID"); | ||
} else { | ||
ensureValidHandle(parts[2]); | ||
} | ||
} catch { | ||
throw new Error("ATURI authority must be a valid handle or DID"); | ||
} | ||
@@ -439,2 +450,123 @@ if (parts.length >= 4) { | ||
} | ||
// src/tid.ts | ||
var ensureValidTid = (tid) => { | ||
if (tid.length != 13) { | ||
throw new InvalidTidError("TID must be 13 characters"); | ||
} | ||
if (!/^[234567abcdefghij][234567abcdefghijklmnopqrstuvwxyz]{12}$/.test(tid)) { | ||
throw new InvalidTidError("TID syntax not valid (regex)"); | ||
} | ||
}; | ||
var isValidTid = (tid) => { | ||
try { | ||
ensureValidTid(tid); | ||
} catch (err) { | ||
if (err instanceof InvalidTidError) { | ||
return false; | ||
} | ||
throw err; | ||
} | ||
return true; | ||
}; | ||
var InvalidTidError = class extends Error { | ||
}; | ||
// src/recordkey.ts | ||
var ensureValidRecordKey = (rkey) => { | ||
if (rkey.length > 512 || rkey.length < 1) { | ||
throw new InvalidRecordKeyError("record key must be 1 to 512 characters"); | ||
} | ||
if (!/^[a-zA-Z0-9_~.-]{1,512}$/.test(rkey)) { | ||
throw new InvalidRecordKeyError("record key syntax not valid (regex)"); | ||
} | ||
if (rkey == "." || rkey == "..") | ||
throw new InvalidRecordKeyError('record key can not be "." or ".."'); | ||
}; | ||
var isValidRecordKey = (rkey) => { | ||
try { | ||
ensureValidRecordKey(rkey); | ||
} catch (err) { | ||
if (err instanceof InvalidRecordKeyError) { | ||
return false; | ||
} | ||
throw err; | ||
} | ||
return true; | ||
}; | ||
var InvalidRecordKeyError = class extends Error { | ||
}; | ||
// src/datetime.ts | ||
var ensureValidDatetime = (dtStr) => { | ||
const date = new Date(dtStr); | ||
if (isNaN(date.getTime())) { | ||
throw new InvalidDatetimeError("datetime did not parse as ISO 8601"); | ||
} | ||
if (date.toISOString().startsWith("-")) { | ||
throw new InvalidDatetimeError("datetime normalized to a negative time"); | ||
} | ||
if (!/^[0-9]{4}-[01][0-9]-[0-3][0-9]T[0-2][0-9]:[0-6][0-9]:[0-6][0-9](.[0-9]{1,20})?(Z|([+-][0-2][0-9]:[0-5][0-9]))$/.test(dtStr)) { | ||
throw new InvalidDatetimeError("datetime didn't validate via regex"); | ||
} | ||
if (dtStr.length > 64) { | ||
throw new InvalidDatetimeError("datetime is too long (64 chars max)"); | ||
} | ||
if (dtStr.endsWith("-00:00")) { | ||
throw new InvalidDatetimeError('datetime can not use "-00:00" for UTC timezone'); | ||
} | ||
if (dtStr.startsWith("000")) { | ||
throw new InvalidDatetimeError("datetime so close to year zero not allowed"); | ||
} | ||
}; | ||
var isValidDatetime = (dtStr) => { | ||
try { | ||
ensureValidDatetime(dtStr); | ||
} catch (err) { | ||
if (err instanceof InvalidDatetimeError) { | ||
return false; | ||
} | ||
throw err; | ||
} | ||
return true; | ||
}; | ||
var normalizeDatetime = (dtStr) => { | ||
if (isValidDatetime(dtStr)) { | ||
const outStr = new Date(dtStr).toISOString(); | ||
if (isValidDatetime(outStr)) { | ||
return outStr; | ||
} | ||
} | ||
if (!/.*(([+-]\d\d:?\d\d)|[a-zA-Z])$/.test(dtStr)) { | ||
const date2 = new Date(dtStr + "Z"); | ||
if (!isNaN(date2.getTime())) { | ||
const tzStr = date2.toISOString(); | ||
if (isValidDatetime(tzStr)) { | ||
return tzStr; | ||
} | ||
} | ||
} | ||
const date = new Date(dtStr); | ||
if (isNaN(date.getTime())) { | ||
throw new InvalidDatetimeError("datetime did not parse as any timestamp format"); | ||
} | ||
const isoStr = date.toISOString(); | ||
if (isValidDatetime(isoStr)) { | ||
return isoStr; | ||
} else { | ||
throw new InvalidDatetimeError("datetime normalized to invalid timestamp string"); | ||
} | ||
}; | ||
var normalizeDatetimeAlways = (dtStr) => { | ||
try { | ||
return normalizeDatetime(dtStr); | ||
} catch (err) { | ||
if (err instanceof InvalidDatetimeError) { | ||
return new Date(0).toISOString(); | ||
} | ||
throw err; | ||
} | ||
}; | ||
var InvalidDatetimeError = class extends Error { | ||
}; | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -447,5 +579,8 @@ 0 && (module.exports = { | ||
INVALID_HANDLE, | ||
InvalidDatetimeError, | ||
InvalidDidError, | ||
InvalidHandleError, | ||
InvalidNsidError, | ||
InvalidRecordKeyError, | ||
InvalidTidError, | ||
NSID, | ||
@@ -456,2 +591,3 @@ ReservedHandleError, | ||
ensureValidAtUriRegex, | ||
ensureValidDatetime, | ||
ensureValidDid, | ||
@@ -463,7 +599,14 @@ ensureValidDidRegex, | ||
ensureValidNsidRegex, | ||
ensureValidRecordKey, | ||
ensureValidTid, | ||
isValidDatetime, | ||
isValidHandle, | ||
isValidRecordKey, | ||
isValidTid, | ||
isValidTld, | ||
normalizeAndEnsureValidHandle, | ||
normalizeDatetime, | ||
normalizeDatetimeAlways, | ||
normalizeHandle | ||
}); | ||
//# sourceMappingURL=index.js.map |
@@ -5,3 +5,3 @@ const base = require('../../jest.config.base.js') | ||
...base, | ||
displayName: 'Identifier', | ||
displayName: 'Syntax', | ||
} |
{ | ||
"name": "@atproto/syntax", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "Validation for atproto identifiers and formats: DID, handle, NSID, AT URI, etc", |
@@ -41,9 +41,9 @@ import { ensureValidHandle, ensureValidHandleRegex } from './handle' | ||
try { | ||
ensureValidHandle(parts[2]) | ||
} catch { | ||
try { | ||
if (parts[2].startsWith('did:')) { | ||
ensureValidDid(parts[2]) | ||
} catch { | ||
throw new Error('ATURI authority must be a valid handle or DID') | ||
} else { | ||
ensureValidHandle(parts[2]) | ||
} | ||
} catch { | ||
throw new Error('ATURI authority must be a valid handle or DID') | ||
} | ||
@@ -50,0 +50,0 @@ |
@@ -5,1 +5,4 @@ export * from './handle' | ||
export * from './aturi' | ||
export * from './tid' | ||
export * from './recordkey' | ||
export * from './datetime' |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
122949
36
2491