| 31 | |
| 32 | export class StoragePlugin extends BasePlugin { |
| 33 | static async updateServer(expressApp: Express, httpServer: AppiumServer): Promise<void> { |
| 34 | const serverBasePath = httpServer.basePath; |
| 35 | const buildHandler = |
| 36 | (methodName: string, prefix: string, endpoint: string, isDeprecated: boolean) => |
| 37 | async (req: Request, res: Response) => { |
| 38 | // the server base path is intentionally omitted from the log message |
| 39 | const routePath = `${prefix}${endpoint}`; |
| 40 | if (isDeprecated && !deprecatedRoutesLogged.has(routePath)) { |
| 41 | deprecatedRoutesLogged.add(routePath); |
| 42 | log.warn( |
| 43 | `The '${routePath}' endpoint has been deprecated and will be removed in a future version ` + |
| 44 | `of the storage plugin. Please use '${STORAGE_PREFIX}${endpoint}' instead`, |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | let status = 200; |
| 49 | let body: any; |
| 50 | try { |
| 51 | const value = await STORAGE_HANDLERS[methodName](req, httpServer, `${serverBasePath}${prefix}`); |
| 52 | body = {value: value ?? null}; |
| 53 | } catch (e) { |
| 54 | [status, body] = getResponseForW3CError(e); |
| 55 | } |
| 56 | log.debug( |
| 57 | 'Responding to %s with %s', |
| 58 | methodName, |
| 59 | logger.markSensitive(util.truncateString(JSON.stringify(body.value), {length: 200})), |
| 60 | ); |
| 61 | res.set('content-type', 'application/json; charset=utf-8'); |
| 62 | res.status(status).send(body); |
| 63 | }; |
| 64 | |
| 65 | for (const [prefix, isDeprecated] of [ |
| 66 | [STORAGE_PREFIX, false], |
| 67 | [DEPRECATED_STORAGE_PREFIX, true], |
| 68 | ] as const) { |
| 69 | for (const [method, endpoint, handlerName] of [ |
| 70 | ['post', '/add', STORAGE_HANDLERS.addStorageItem.name], |
| 71 | ['get', '/list', STORAGE_HANDLERS.listStorageItems.name], |
| 72 | ['post', '/reset', STORAGE_HANDLERS.resetStorage.name], |
| 73 | ['post', '/delete', STORAGE_HANDLERS.deleteStorageItem.name], |
| 74 | ] as const) { |
| 75 | expressApp[method]( |
| 76 | `${serverBasePath}${prefix}${endpoint}`, |
| 77 | buildHandler(handlerName, prefix, endpoint, isDeprecated), |
| 78 | ); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | STORAGE_HANDLERS.addStorageItem = async function addStorageItem( |