mirror of
https://github.com/sub-store-org/Sub-Store.git
synced 2026-12-01 08:51:41 +00:00
feat: add lifetime duration
This commit is contained in:
3 files changed
+154
-1
No files matched your search
@@ -180,6 +180,30 @@ const DURATION_INPUT_UNIT_MS = {
|
||||
year: 365 * 24 * 60 * 60 * 1000,
|
||||
};
|
||||
|
||||
// 长期有效: 不设置 exp, 直到分享被删除前始终有效
|
||||
const LIFETIME_DURATION_UNIT = 'lifetime';
|
||||
|
||||
function isLifetimeDuration(options = {}) {
|
||||
const rawExpiresUnit = options?.expiresUnit;
|
||||
return (
|
||||
typeof rawExpiresUnit === 'string' &&
|
||||
rawExpiresUnit.trim() === LIFETIME_DURATION_UNIT
|
||||
);
|
||||
}
|
||||
|
||||
function resolveLifetimeDuration(options = {}) {
|
||||
const rawExpiresIn = options?.expiresIn;
|
||||
if (rawExpiresIn != null && rawExpiresIn !== '') {
|
||||
throw new RequestInvalidError(
|
||||
'INVALID_DURATION_INPUT',
|
||||
`expiresIn option is not allowed for lifetime duration: ${rawExpiresIn}`,
|
||||
);
|
||||
}
|
||||
return {
|
||||
expiresUnit: LIFETIME_DURATION_UNIT,
|
||||
};
|
||||
}
|
||||
|
||||
function resolveDurationInput(options = {}, durationExpiration) {
|
||||
const rawExpiresValue = options?.expiresValue;
|
||||
const rawExpiresUnit = options?.expiresUnit;
|
||||
@@ -329,12 +353,15 @@ function createTokenItem(payload, options = {}) {
|
||||
inferLegacyExpirationMode(options);
|
||||
let durationExpiration = null;
|
||||
let durationInput = null;
|
||||
let lifetimeDuration = null;
|
||||
let exp;
|
||||
let countExpiration = null;
|
||||
if (expirationMode === 'datetime') {
|
||||
exp = resolveExactExpiration(options);
|
||||
} else if (expirationMode === 'count') {
|
||||
countExpiration = resolveCountExpiration(options);
|
||||
} else if (expirationMode === 'duration' && isLifetimeDuration(options)) {
|
||||
lifetimeDuration = resolveLifetimeDuration(options);
|
||||
} else {
|
||||
durationExpiration = resolveDurationExpiration(options, {
|
||||
required: expirationMode === 'duration',
|
||||
@@ -364,7 +391,7 @@ function createTokenItem(payload, options = {}) {
|
||||
? 'datetime'
|
||||
: expirationMode === 'count'
|
||||
? 'count'
|
||||
: durationExpiration
|
||||
: durationExpiration || lifetimeDuration
|
||||
? 'duration'
|
||||
: undefined;
|
||||
const safePayload = { ...payload };
|
||||
@@ -388,6 +415,8 @@ function createTokenItem(payload, options = {}) {
|
||||
count: countExpiration.count,
|
||||
usedCount: countExpiration.usedCount,
|
||||
}
|
||||
: lifetimeDuration
|
||||
? { ...lifetimeDuration }
|
||||
: durationExpiration
|
||||
? {
|
||||
expiresIn: durationExpiration.rawExpiresIn,
|
||||
|
||||
@@ -448,6 +448,73 @@ describe('archive routes', function () {
|
||||
expect(state[ARCHIVES_KEY]).to.deep.equal([]);
|
||||
});
|
||||
|
||||
it('preserves lifetime duration shares on restore', async function () {
|
||||
state[SUBS_KEY] = [
|
||||
{
|
||||
name: 'shared-sub',
|
||||
source: 'remote',
|
||||
process: [],
|
||||
},
|
||||
];
|
||||
state[TOKENS_KEY] = [
|
||||
{
|
||||
token: 'duration-lifetime',
|
||||
type: 'sub',
|
||||
name: 'shared-sub',
|
||||
displayName: 'Lifetime Share',
|
||||
mode: 'duration',
|
||||
expiresUnit: 'lifetime',
|
||||
},
|
||||
];
|
||||
|
||||
const deleteHandler = getHandler(
|
||||
registerTokenRoutes,
|
||||
'DELETE',
|
||||
'/api/token/:token',
|
||||
);
|
||||
const deleteRes = createResponse('/api/token/:token');
|
||||
|
||||
deleteHandler(
|
||||
{
|
||||
params: { token: 'duration-lifetime' },
|
||||
query: {
|
||||
mode: 'archive',
|
||||
type: 'sub',
|
||||
name: 'shared-sub',
|
||||
},
|
||||
},
|
||||
deleteRes,
|
||||
);
|
||||
|
||||
expect(deleteRes.body.status).to.equal('success');
|
||||
expect(state[TOKENS_KEY]).to.deep.equal([]);
|
||||
expect(state[ARCHIVES_KEY]).to.have.length(1);
|
||||
|
||||
const restoreHandler = getHandler(
|
||||
registerArchiveRoutes,
|
||||
'POST',
|
||||
'/api/archives/:id/restore',
|
||||
);
|
||||
const restoreRes = createResponse('/api/archives/:id/restore');
|
||||
|
||||
restoreHandler(
|
||||
{
|
||||
params: { id: state[ARCHIVES_KEY][0].id },
|
||||
query: {},
|
||||
},
|
||||
restoreRes,
|
||||
);
|
||||
|
||||
expect(restoreRes.body.status).to.equal('success');
|
||||
expect(state[TOKENS_KEY]).to.have.length(1);
|
||||
expect(state[TOKENS_KEY][0].token).to.equal('duration-lifetime');
|
||||
expect(state[TOKENS_KEY][0].mode).to.equal('duration');
|
||||
expect(state[TOKENS_KEY][0].expiresUnit).to.equal('lifetime');
|
||||
expect(state[TOKENS_KEY][0]).to.not.have.property('expiresIn');
|
||||
expect(state[TOKENS_KEY][0]).to.not.have.property('exp');
|
||||
expect(state[ARCHIVES_KEY]).to.deep.equal([]);
|
||||
});
|
||||
|
||||
it('restores legacy exp-only share tokens as exact-datetime shares', async function () {
|
||||
state[SUBS_KEY] = [
|
||||
{
|
||||
|
||||
@@ -280,6 +280,63 @@ describe('token routes', function () {
|
||||
expect(state[TOKENS_KEY][0].expiresUnit).to.equal('season');
|
||||
});
|
||||
|
||||
it('stores lifetime duration shares without expiration', function () {
|
||||
const token = createTokenItem(
|
||||
{
|
||||
type: 'sub',
|
||||
name: 'shared-sub',
|
||||
token: 'duration-lifetime',
|
||||
},
|
||||
{
|
||||
mode: 'duration',
|
||||
expiresValue: '35',
|
||||
expiresUnit: 'lifetime',
|
||||
},
|
||||
);
|
||||
|
||||
expect(token.mode).to.equal('duration');
|
||||
expect(token.expiresUnit).to.equal('lifetime');
|
||||
expect(token).to.not.have.property('exp');
|
||||
expect(token).to.not.have.property('expiresIn');
|
||||
expect(token).to.not.have.property('expiresValue');
|
||||
expect(state[TOKENS_KEY][0].expiresUnit).to.equal('lifetime');
|
||||
expect(state[TOKENS_KEY][0]).to.not.have.property('exp');
|
||||
|
||||
const found = findShareToken({
|
||||
token: 'duration-lifetime',
|
||||
type: 'sub',
|
||||
name: 'shared-sub',
|
||||
});
|
||||
expect(found.token).to.equal('duration-lifetime');
|
||||
});
|
||||
|
||||
it('rejects lifetime duration shares with expiresIn', function () {
|
||||
let capturedError;
|
||||
|
||||
try {
|
||||
createTokenItem(
|
||||
{
|
||||
type: 'sub',
|
||||
name: 'shared-sub',
|
||||
token: 'duration-lifetime-conflict',
|
||||
},
|
||||
{
|
||||
mode: 'duration',
|
||||
expiresIn: '1d',
|
||||
expiresUnit: 'lifetime',
|
||||
},
|
||||
);
|
||||
} catch (error) {
|
||||
capturedError = error;
|
||||
}
|
||||
|
||||
expect(capturedError).to.include({
|
||||
type: 'RequestInvalidError',
|
||||
code: 'INVALID_DURATION_INPUT',
|
||||
});
|
||||
expect(state[TOKENS_KEY]).to.deep.equal([]);
|
||||
});
|
||||
|
||||
it('rejects invalid duration input metadata', function () {
|
||||
for (const options of [
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user