Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 1x 1x 1x 1x 7x 1x 3x 3x 1x 10x 10x 11x 1x 12x 12x 12x 11x 11x 10x 9x 9x 9x 9x 3x 3x 2x 1x 7x 6x 6x 3x 6x 6x 3x 3x 6x 2x 6x 2x 1x 1x | /**
* GET /v1/store/installed
*
* The packs the active flow installed, the most recently installed first
* (UC-STORE-015.1), each with `installedVersion`, the lowest version among the
* flow's copies (written on its `PackImportsTable` row by installs and
* upgrades), so that the Store tells "Update available (v1 → v2)" without
* reading the copies. One Query on `flowIndex` per page, then reads bounded by
* the page size: the packs (`BatchGet`) and what any page of the Store shows.
* A pack deleted since is left out, so a page may come shorter.
*
* Whether an update can be applied is still decided by the upgrade preview's
* permissions: this list only needs the flow's entities to be of use to the
* caller (`entities:create`, or a read permission on entities).
*
* Query params:
* - limit: page size (default 30, max 100)
* - nextToken: opaque cursor returned by the previous page
*
* @implements UC-STORE-015.1
*/
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient, QueryCommand } = require('@aws-sdk/lib-dynamodb')
const {
authenticateModerator: authenticateUser,
errorResponse,
jsonResponse
} = require('../utils/moderation/access')
const { permissionChecker } = require('../utils/PermissionChecker')
const { packsTable, batchGet, buildPackPresenter } = require('../utils/packs')
const docClient = DynamoDBDocumentClient.from(new DynamoDBClient({}))
const DEFAULT_LIMIT = 30
const MAX_LIMIT = 100
const importsTable = () => process.env.PACK_IMPORTS_TABLE_NAME
const encodeToken = (key) => Buffer.from(JSON.stringify(key)).toString('base64')
function decodeToken(token) {
try {
return JSON.parse(Buffer.from(token, 'base64').toString())
} catch {
return null
}
}
/**
* Whether the caller may create entities or read some: an entity-scoped read
* grant (`entities-{id}:read`) counts, as the flow's copies are entities.
*/
async function mayUseEntities(auth) {
const permissions =
auth.type === 'apikey'
? auth.permissions || []
: (await permissionChecker.getUserPermissions(auth.userId, auth.flowId)).permissions || []
return permissions.some(
(permission) =>
permissionChecker.matchesPermission(permission, 'entities:create') ||
// Any entity: `entities:read` and wildcards grant it for every id
permissionChecker.matchesPermission(permission, 'entities-any:read') ||
/^entities-[^:]+:(read|\*)$/.test(permission)
)
}
exports.handler = async (event) => {
try {
const caller = await authenticateUser(event)
if (caller.response) return caller.response
const { auth } = caller
if (!auth.flowId) return errorResponse(403, 'NO_ACTIVE_FLOW')
if (!(await mayUseEntities(auth))) return errorResponse(403, 'Permission denied')
const rawLimit = parseInt(event.queryStringParameters?.limit, 10)
const limit = Number.isFinite(rawLimit)
? Math.min(Math.max(rawLimit, 1), MAX_LIMIT)
: DEFAULT_LIMIT
let exclusiveStartKey
const nextToken = event.queryStringParameters?.nextToken
if (nextToken) {
const key = decodeToken(nextToken)
// A client-controlled cursor must stay in the active flow
if (
key?.flowId !== auth.flowId ||
typeof key.packId !== 'string' ||
typeof key.importedAt !== 'string'
) {
return errorResponse(400, 'Invalid nextToken')
}
exclusiveStartKey = { packId: key.packId, flowId: key.flowId, importedAt: key.importedAt }
}
const result = await docClient.send(
new QueryCommand({
TableName: importsTable(),
IndexName: 'flowIndex',
KeyConditionExpression: 'flowId = :flowId',
ExpressionAttributeValues: { ':flowId': auth.flowId },
ScanIndexForward: false,
Limit: limit,
ExclusiveStartKey: exclusiveStartKey
})
)
const rows = result.Items || []
const found = await batchGet(
docClient,
packsTable(),
rows.map((row) => ({ id: row.packId }))
)
const packById = new Map(found.map((pack) => [pack.id, pack]))
const installed = rows
.map((row) => ({ row, pack: packById.get(row.packId) }))
.filter(({ pack }) => pack?.currentVersion)
const present = await buildPackPresenter(
docClient,
installed.map(({ pack }) => pack),
auth.userId
)
return jsonResponse(200, {
items: installed.map(({ row, pack }) => ({
...present(pack),
// Rows from before versions were remembered have none until the backfill
installedVersion: typeof row.version === 'number' ? row.version : null,
importedAt: row.importedAt
})),
nextToken: result.LastEvaluatedKey ? encodeToken(result.LastEvaluatedKey) : null
})
} catch (error) {
console.error('[store/installed] Exception:', error.message, error.stack)
return errorResponse(500, 'Failed to list the installed packs')
}
}
|