|
| 1 | +/* |
| 2 | + * Copyright (C) 2021 HERE Europe B.V. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + * License-Filename: LICENSE |
| 18 | + */ |
| 19 | + |
| 20 | +"use strict"; |
| 21 | + |
| 22 | +const http = require("http"); |
| 23 | +const fs = require("fs"); |
| 24 | + |
| 25 | +const HOST = "127.0.0.1"; |
| 26 | +const PORT = 8080; |
| 27 | +const RESPONSE_DELAY_MS = 300; |
| 28 | + |
| 29 | +const routing = { |
| 30 | + "/": `<!DOCTYPE html> |
| 31 | + <html lang="en"> |
| 32 | + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
| 33 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 34 | + <meta name="title" content="MultiPartUpload example webapp"> |
| 35 | + <meta name=”description” content="MultiPartUpload example webapp"> |
| 36 | + <body> |
| 37 | + <button id="abort">Abort</button> |
| 38 | + <div id="app"></div> |
| 39 | + <script src="https://unpkg.com/@here/olp-sdk-core/bundle.umd.min.js"></script> |
| 40 | + <script src="https://unpkg.com/@here/olp-sdk-authentication/bundle.umd.min.js"></script> |
| 41 | + <script src="https://unpkg.com/@here/olp-sdk-dataservice-api/bundle.umd.min.js"></script> |
| 42 | + <script src="https://unpkg.com/@here/olp-sdk-dataservice-write/bundle.umd.min.js"></script> |
| 43 | + <script src="ui.js"></script> |
| 44 | + </body> |
| 45 | + </html>`, |
| 46 | + "/ui.js": fs.readFileSync("./ui.js").toString(), |
| 47 | + "/oauth2/token": () => ({ |
| 48 | + accessToken: "mocked-access-token", |
| 49 | + tokenType: "bearer", |
| 50 | + expiresIn: 99999999, |
| 51 | + }), |
| 52 | + "/api/lookup/resources/hrn:here:data::mocked:catalog/apis": () => [ |
| 53 | + { |
| 54 | + api: "blob", |
| 55 | + version: "v1", |
| 56 | + baseURL: `http://${HOST}:${PORT}/blobstore/v1`, |
| 57 | + parameters: {}, |
| 58 | + }, |
| 59 | + { |
| 60 | + api: "publish", |
| 61 | + version: "v2", |
| 62 | + baseURL: `http://${HOST}:${PORT}/publish/v2`, |
| 63 | + parameters: {}, |
| 64 | + }, |
| 65 | + ], |
| 66 | + "/publish/v2/publications": () => ({ |
| 67 | + catalogId: "catalog", |
| 68 | + catalogVersion: 999, |
| 69 | + details: { |
| 70 | + expires: 9999999999999, |
| 71 | + message: "", |
| 72 | + modified: 9999999999999, |
| 73 | + started: 9999999999999, |
| 74 | + state: "initialized", |
| 75 | + }, |
| 76 | + id: "mocked-publication-id", |
| 77 | + layerIds: ["mocked-layer"], |
| 78 | + versionDependencies: [], |
| 79 | + }), |
| 80 | + "/blobstore/v1/layers/mocked-layer/data/mocked-datahandle/multiparts": () => ({ |
| 81 | + links: { |
| 82 | + status: { |
| 83 | + href: `http://${HOST}:${PORT}/blobstore/v1/catalogs/hrn:here:data::mocked:catalog/layers/mocked-layer/data/mocked-datahandle/multiparts/mocked-blob-token`, |
| 84 | + method: "GET", |
| 85 | + }, |
| 86 | + delete: { |
| 87 | + href: `http://${HOST}:${PORT}/blobstore/v1/catalogs/hrn:here:data::mocked:catalog/layers/mocked-layer/data/mocked-datahandle/multiparts/mocked-blob-token`, |
| 88 | + method: "DELETE", |
| 89 | + }, |
| 90 | + uploadPart: { |
| 91 | + href: `http://${HOST}:${PORT}/blobstore/v1/catalogs/hrn:here:data::mocked:catalog/layers/mocked-layer/data/mocked-datahandle/multiparts/mocked-blob-token/parts`, |
| 92 | + method: "POST", |
| 93 | + }, |
| 94 | + complete: { |
| 95 | + href: `http://${HOST}:${PORT}/blobstore/v1/catalogs/hrn:here:data::mocked:catalog/layers/mocked-layer/data/mocked-datahandle/multiparts/mocked-blob-token`, |
| 96 | + method: "PUT", |
| 97 | + }, |
| 98 | + }, |
| 99 | + }), |
| 100 | + "/blobstore/v1/catalogs/hrn:here:data::mocked:catalog/layers/mocked-layer/data/mocked-datahandle/multiparts/mocked-blob-token/parts": ( |
| 101 | + req, |
| 102 | + res |
| 103 | + ) => { |
| 104 | + res.setHeader("ETag", Math.random() * 10000); |
| 105 | + res.setHeader("Access-Control-Expose-Headers", "ETag"); |
| 106 | + return { |
| 107 | + status: res.statusCode, |
| 108 | + }; |
| 109 | + }, |
| 110 | + "/blobstore/v1/catalogs/hrn:here:data::mocked:catalog/layers/mocked-layer/data/mocked-datahandle/multiparts/mocked-blob-token": () => ({ |
| 111 | + ok: true, |
| 112 | + }), |
| 113 | + "/publish/v2/layers/mocked-layer/publications/mocked-publication-id/partitions": () => ({ |
| 114 | + ok: true, |
| 115 | + }), |
| 116 | + "/publish/v2/publications/mocked-publication-id": () => ({ |
| 117 | + ok: true, |
| 118 | + }), |
| 119 | +}; |
| 120 | + |
| 121 | +const types = { |
| 122 | + object: JSON.stringify, |
| 123 | + string: (s) => s, |
| 124 | + number: (val) => `${val}`, |
| 125 | + undefined: () => "Not Found", |
| 126 | + function: (fn, req, res) => JSON.stringify(fn(req, res)), |
| 127 | +}; |
| 128 | + |
| 129 | +const server = http.createServer((req, res) => { |
| 130 | + console.dir({ type: "Reguest", method: req.method, url: req.url }); |
| 131 | + const queryIndex = req.url.indexOf("?"); |
| 132 | + const adaptedUrl = |
| 133 | + queryIndex !== -1 ? req.url.substr(0, queryIndex) : req.url; |
| 134 | + |
| 135 | + const data = routing[adaptedUrl]; |
| 136 | + const type = typeof data; |
| 137 | + const serialiser = types[type]; |
| 138 | + const result = serialiser(data, req, res); |
| 139 | + |
| 140 | + res.setHeader("Access-Control-Allow-Origin", "*"); |
| 141 | + res.setHeader( |
| 142 | + "Access-Control-Allow-Headers", |
| 143 | + "authorization,content-type, ETag" |
| 144 | + ); |
| 145 | + res.setHeader("Access-Control-Allow-Methods", "*"); |
| 146 | + |
| 147 | + res.statusCode = type === "undefined" ? 404 : 200; |
| 148 | + setTimeout(() => { |
| 149 | + res.end(result); |
| 150 | + }, RESPONSE_DELAY_MS); |
| 151 | +}); |
| 152 | + |
| 153 | +server.listen(PORT, HOST, () => { |
| 154 | + console.log(`Server running at http://${HOST}:${PORT}`); |
| 155 | +}); |
0 commit comments