├── .gitignore
├── LICENSE
├── README.md
├── back-end
├── .gitignore
├── package-lock.json
├── package.json
└── src
│ ├── app.js
│ ├── server.js
│ ├── subscriptionHandler.js
│ └── swagger.json
├── front-end-react
├── .gitignore
├── README.md
├── config
│ ├── env.js
│ ├── jest
│ │ ├── cssTransform.js
│ │ └── fileTransform.js
│ ├── modules.js
│ ├── paths.js
│ ├── pnpTs.js
│ ├── webpack.config.js
│ └── webpackDevServer.config.js
├── package-lock.json
├── package.json
├── public
│ ├── favicon.ico
│ ├── images
│ │ ├── jason-leung-HM6TMmevbZQ-unsplash.jpg
│ │ └── repository-open-graph-template.png
│ ├── index.html
│ ├── new-product-jason-leung-HM6TMmevbZQ-unsplash.html
│ ├── robots.txt
│ └── sw.js
├── scripts
│ ├── build.js
│ ├── start.js
│ └── test.js
└── src
│ ├── App.css
│ ├── App.js
│ ├── App.test.js
│ ├── PushNotificationDemo.js
│ ├── index.css
│ ├── index.js
│ ├── logo.svg
│ ├── push-notifications.js
│ ├── usePushNotifications.js
│ └── utils
│ └── http.js
├── front-end
├── .babelrc
├── .browserslistrc
├── .eslintrc
├── .gitignore
├── README.md
├── package.json
├── public
│ ├── .gitkeep
│ ├── images
│ │ ├── jason-leung-HM6TMmevbZQ-unsplash.jpg
│ │ └── repository-open-graph-template.png
│ ├── new-product-jason-leung-HM6TMmevbZQ-unsplash.html
│ └── sw.js
├── src
│ ├── index.html
│ ├── scripts
│ │ ├── index.js
│ │ ├── push-notifications.js
│ │ └── utils
│ │ │ └── http.js
│ └── styles
│ │ └── index.scss
└── webpack
│ ├── webpack.common.js
│ ├── webpack.config.dev.js
│ └── webpack.config.prod.js
├── package.json
└── push-notification.jpg
/.gitignore:
--------------------------------------------------------------------------------
1 |
2 | front-end-react/yarn\.lock
3 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 Lorenzo Spinelli
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | > How to implement Web Push Notifications with live demo and instructions: https://push-notifications-demo.netlify.app/
2 |
3 | ![A push notificatin][pushNotificaiton]
4 |
5 | [pushNotificaiton]: https://github.com/Spyna/push-notification-demo/raw/master/push-notification.jpg "Push notification"
6 |
7 | # push-notification-demo
8 |
9 | This mono-repo project is a demo for this article https://itnext.io/an-introduction-to-web-push-notifications-in-javascript-a701783917ce
10 |
11 | The project is composed by two parts:
12 |
13 | * a front end written is HTML + JavaScript
14 |
15 | * a back end written in JavaScript + NodeJS
16 |
17 | ## Run locally
18 |
19 | ### font-end
20 |
21 | ```
22 | cd front-end
23 | npm install
24 | npm start
25 | ```
26 | Next, open http://localhost:9000.
27 |
28 | ### back-end
29 |
30 | ```
31 | cd back-end
32 | npm install
33 | export CORS_ORIGIN=http://localhost:9000 && node src/server.js
34 | ```
35 |
36 | `export CORS_ORIGIN=http://localhost:9000` means: accept Cross Origin call from this host.
37 |
38 | ## deploying to your server
39 |
40 | If you want to deploy the front-end to your server remember to modify the back-end host in the webpack config file: `webpack.config.prod.js`: `'process.env.PUSH_SERVER_URL': JSON.stringify('https://push-notification-demo-server.herokuapp.com'),`
41 |
42 | [](https://github.com/igrigorik/ga-beacon)
43 |
--------------------------------------------------------------------------------
/back-end/.gitignore:
--------------------------------------------------------------------------------
1 | # dependencies
2 | node_modules
3 |
4 | # production
5 | build
6 |
7 | # misc
8 | .DS_Store
9 |
10 | npm-debug.log
11 | yarn-error.log
12 | yarn.lock
13 | .yarnclean
14 | .vscode
15 |
--------------------------------------------------------------------------------
/back-end/package-lock.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "back-end",
3 | "version": "1.0.0",
4 | "lockfileVersion": 1,
5 | "requires": true,
6 | "dependencies": {
7 | "accepts": {
8 | "version": "1.3.7",
9 | "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz",
10 | "integrity": "sha512-Il80Qs2WjYlJIBNzNkK6KYqlVMTbZLXgHx2oT0pU/fjRHyEp+PEfEPY0R3WCwAGVOtauxh1hOxNgIf5bv7dQpA==",
11 | "requires": {
12 | "mime-types": "~2.1.24",
13 | "negotiator": "0.6.2"
14 | }
15 | },
16 | "agent-base": {
17 | "version": "4.3.0",
18 | "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-4.3.0.tgz",
19 | "integrity": "sha512-salcGninV0nPrwpGNn4VTXBb1SOuXQBiqbrNXoeizJsHrsL6ERFM2Ne3JUSBWRE6aeNJI2ROP/WEEIDUiDe3cg==",
20 | "requires": {
21 | "es6-promisify": "^5.0.0"
22 | }
23 | },
24 | "array-flatten": {
25 | "version": "1.1.1",
26 | "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
27 | "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI="
28 | },
29 | "asn1.js": {
30 | "version": "5.3.0",
31 | "resolved": "https://registry.npmjs.org/asn1.js/-/asn1.js-5.3.0.tgz",
32 | "integrity": "sha512-WHnQJFcOrIWT1RLOkFFBQkFVvyt9BPOOrH+Dp152Zk4R993rSzXUGPmkybIcUFhHE2d/iHH+nCaOWVCDbO8fgA==",
33 | "requires": {
34 | "bn.js": "^4.0.0",
35 | "inherits": "^2.0.1",
36 | "minimalistic-assert": "^1.0.0",
37 | "safer-buffer": "^2.1.0"
38 | }
39 | },
40 | "bn.js": {
41 | "version": "4.11.8",
42 | "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.11.8.tgz",
43 | "integrity": "sha512-ItfYfPLkWHUjckQCk8xC+LwxgK8NYcXywGigJgSwOP8Y2iyWT4f2vsZnoOXTTbo+o5yXmIUJ4gn5538SO5S3gA=="
44 | },
45 | "body-parser": {
46 | "version": "1.19.0",
47 | "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.19.0.tgz",
48 | "integrity": "sha512-dhEPs72UPbDnAQJ9ZKMNTP6ptJaionhP5cBb541nXPlW60Jepo9RV/a4fX4XWW9CuFNK22krhrj1+rgzifNCsw==",
49 | "requires": {
50 | "bytes": "3.1.0",
51 | "content-type": "~1.0.4",
52 | "debug": "2.6.9",
53 | "depd": "~1.1.2",
54 | "http-errors": "1.7.2",
55 | "iconv-lite": "0.4.24",
56 | "on-finished": "~2.3.0",
57 | "qs": "6.7.0",
58 | "raw-body": "2.4.0",
59 | "type-is": "~1.6.17"
60 | }
61 | },
62 | "buffer-equal-constant-time": {
63 | "version": "1.0.1",
64 | "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz",
65 | "integrity": "sha1-+OcRMvf/5uAaXJaXpMbz5I1cyBk="
66 | },
67 | "bytes": {
68 | "version": "3.1.0",
69 | "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz",
70 | "integrity": "sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg=="
71 | },
72 | "compressible": {
73 | "version": "2.0.18",
74 | "resolved": "https://registry.npmjs.org/compressible/-/compressible-2.0.18.tgz",
75 | "integrity": "sha512-AF3r7P5dWxL8MxyITRMlORQNaOA2IkAFaTr4k7BUumjPtRpGDTZpl0Pb1XCO6JeDCBdp126Cgs9sMxqSjgYyRg==",
76 | "requires": {
77 | "mime-db": ">= 1.43.0 < 2"
78 | }
79 | },
80 | "compression": {
81 | "version": "1.7.4",
82 | "resolved": "https://registry.npmjs.org/compression/-/compression-1.7.4.tgz",
83 | "integrity": "sha512-jaSIDzP9pZVS4ZfQ+TzvtiWhdpFhE2RDHz8QJkpX9SIpLq88VueF5jJw6t+6CUQcAoA6t+x89MLrWAqpfDE8iQ==",
84 | "requires": {
85 | "accepts": "~1.3.5",
86 | "bytes": "3.0.0",
87 | "compressible": "~2.0.16",
88 | "debug": "2.6.9",
89 | "on-headers": "~1.0.2",
90 | "safe-buffer": "5.1.2",
91 | "vary": "~1.1.2"
92 | },
93 | "dependencies": {
94 | "bytes": {
95 | "version": "3.0.0",
96 | "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz",
97 | "integrity": "sha1-0ygVQE1olpn4Wk6k+odV3ROpYEg="
98 | }
99 | }
100 | },
101 | "content-disposition": {
102 | "version": "0.5.3",
103 | "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.3.tgz",
104 | "integrity": "sha512-ExO0774ikEObIAEV9kDo50o+79VCUdEB6n6lzKgGwupcVeRlhrj3qGAfwq8G6uBJjkqLrhT0qEYFcWng8z1z0g==",
105 | "requires": {
106 | "safe-buffer": "5.1.2"
107 | }
108 | },
109 | "content-type": {
110 | "version": "1.0.4",
111 | "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz",
112 | "integrity": "sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA=="
113 | },
114 | "cookie": {
115 | "version": "0.4.0",
116 | "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.0.tgz",
117 | "integrity": "sha512-+Hp8fLp57wnUSt0tY0tHEXh4voZRDnoIrZPqlo3DPiI4y9lwg/jqx+1Om94/W6ZaPDOUbnjOt/99w66zk+l1Xg=="
118 | },
119 | "cookie-signature": {
120 | "version": "1.0.6",
121 | "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz",
122 | "integrity": "sha1-4wOogrNCzD7oylE6eZmXNNqzriw="
123 | },
124 | "cors": {
125 | "version": "2.8.5",
126 | "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz",
127 | "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==",
128 | "requires": {
129 | "object-assign": "^4",
130 | "vary": "^1"
131 | }
132 | },
133 | "debug": {
134 | "version": "2.6.9",
135 | "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
136 | "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
137 | "requires": {
138 | "ms": "2.0.0"
139 | }
140 | },
141 | "depd": {
142 | "version": "1.1.2",
143 | "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz",
144 | "integrity": "sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak="
145 | },
146 | "destroy": {
147 | "version": "1.0.4",
148 | "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz",
149 | "integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA="
150 | },
151 | "ecdsa-sig-formatter": {
152 | "version": "1.0.11",
153 | "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz",
154 | "integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==",
155 | "requires": {
156 | "safe-buffer": "^5.0.1"
157 | }
158 | },
159 | "ee-first": {
160 | "version": "1.1.1",
161 | "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
162 | "integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0="
163 | },
164 | "encodeurl": {
165 | "version": "1.0.2",
166 | "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz",
167 | "integrity": "sha1-rT/0yG7C0CkyL1oCw6mmBslbP1k="
168 | },
169 | "es6-promise": {
170 | "version": "4.2.8",
171 | "resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-4.2.8.tgz",
172 | "integrity": "sha512-HJDGx5daxeIvxdBxvG2cb9g4tEvwIk3i8+nhX0yGrYmZUzbkdg8QbDevheDB8gd0//uPj4c1EQua8Q+MViT0/w=="
173 | },
174 | "es6-promisify": {
175 | "version": "5.0.0",
176 | "resolved": "https://registry.npmjs.org/es6-promisify/-/es6-promisify-5.0.0.tgz",
177 | "integrity": "sha1-UQnWLz5W6pZ8S2NQWu8IKRyKUgM=",
178 | "requires": {
179 | "es6-promise": "^4.0.3"
180 | }
181 | },
182 | "escape-html": {
183 | "version": "1.0.3",
184 | "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz",
185 | "integrity": "sha1-Aljq5NPQwJdN4cFpGI7wBR0dGYg="
186 | },
187 | "etag": {
188 | "version": "1.8.1",
189 | "resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz",
190 | "integrity": "sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc="
191 | },
192 | "express": {
193 | "version": "4.17.1",
194 | "resolved": "https://registry.npmjs.org/express/-/express-4.17.1.tgz",
195 | "integrity": "sha512-mHJ9O79RqluphRrcw2X/GTh3k9tVv8YcoyY4Kkh4WDMUYKRZUq0h1o0w2rrrxBqM7VoeUVqgb27xlEMXTnYt4g==",
196 | "requires": {
197 | "accepts": "~1.3.7",
198 | "array-flatten": "1.1.1",
199 | "body-parser": "1.19.0",
200 | "content-disposition": "0.5.3",
201 | "content-type": "~1.0.4",
202 | "cookie": "0.4.0",
203 | "cookie-signature": "1.0.6",
204 | "debug": "2.6.9",
205 | "depd": "~1.1.2",
206 | "encodeurl": "~1.0.2",
207 | "escape-html": "~1.0.3",
208 | "etag": "~1.8.1",
209 | "finalhandler": "~1.1.2",
210 | "fresh": "0.5.2",
211 | "merge-descriptors": "1.0.1",
212 | "methods": "~1.1.2",
213 | "on-finished": "~2.3.0",
214 | "parseurl": "~1.3.3",
215 | "path-to-regexp": "0.1.7",
216 | "proxy-addr": "~2.0.5",
217 | "qs": "6.7.0",
218 | "range-parser": "~1.2.1",
219 | "safe-buffer": "5.1.2",
220 | "send": "0.17.1",
221 | "serve-static": "1.14.1",
222 | "setprototypeof": "1.1.1",
223 | "statuses": "~1.5.0",
224 | "type-is": "~1.6.18",
225 | "utils-merge": "1.0.1",
226 | "vary": "~1.1.2"
227 | }
228 | },
229 | "finalhandler": {
230 | "version": "1.1.2",
231 | "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.1.2.tgz",
232 | "integrity": "sha512-aAWcW57uxVNrQZqFXjITpW3sIUQmHGG3qSb9mUah9MgMC4NeWhNOlNjXEYq3HjRAvL6arUviZGGJsBg6z0zsWA==",
233 | "requires": {
234 | "debug": "2.6.9",
235 | "encodeurl": "~1.0.2",
236 | "escape-html": "~1.0.3",
237 | "on-finished": "~2.3.0",
238 | "parseurl": "~1.3.3",
239 | "statuses": "~1.5.0",
240 | "unpipe": "~1.0.0"
241 | }
242 | },
243 | "forwarded": {
244 | "version": "0.1.2",
245 | "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz",
246 | "integrity": "sha1-mMI9qxF1ZXuMBXPozszZGw/xjIQ="
247 | },
248 | "fresh": {
249 | "version": "0.5.2",
250 | "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz",
251 | "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac="
252 | },
253 | "http-errors": {
254 | "version": "1.7.2",
255 | "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.7.2.tgz",
256 | "integrity": "sha512-uUQBt3H/cSIVfch6i1EuPNy/YsRSOUBXTVfZ+yR7Zjez3qjBz6i9+i4zjNaoqcoFVI4lQJ5plg63TvGfRSDCRg==",
257 | "requires": {
258 | "depd": "~1.1.2",
259 | "inherits": "2.0.3",
260 | "setprototypeof": "1.1.1",
261 | "statuses": ">= 1.5.0 < 2",
262 | "toidentifier": "1.0.0"
263 | }
264 | },
265 | "http_ece": {
266 | "version": "1.1.0",
267 | "resolved": "https://registry.npmjs.org/http_ece/-/http_ece-1.1.0.tgz",
268 | "integrity": "sha512-bptAfCDdPJxOs5zYSe7Y3lpr772s1G346R4Td5LgRUeCwIGpCGDUTJxRrhTNcAXbx37spge0kWEIH7QAYWNTlA==",
269 | "requires": {
270 | "urlsafe-base64": "~1.0.0"
271 | }
272 | },
273 | "https-proxy-agent": {
274 | "version": "3.0.1",
275 | "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-3.0.1.tgz",
276 | "integrity": "sha512-+ML2Rbh6DAuee7d07tYGEKOEi2voWPUGan+ExdPbPW6Z3svq+JCqr0v8WmKPOkz1vOVykPCBSuobe7G8GJUtVg==",
277 | "requires": {
278 | "agent-base": "^4.3.0",
279 | "debug": "^3.1.0"
280 | },
281 | "dependencies": {
282 | "debug": {
283 | "version": "3.2.6",
284 | "resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz",
285 | "integrity": "sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==",
286 | "requires": {
287 | "ms": "^2.1.1"
288 | }
289 | },
290 | "ms": {
291 | "version": "2.1.2",
292 | "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
293 | "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
294 | }
295 | }
296 | },
297 | "iconv-lite": {
298 | "version": "0.4.24",
299 | "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz",
300 | "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==",
301 | "requires": {
302 | "safer-buffer": ">= 2.1.2 < 3"
303 | }
304 | },
305 | "inherits": {
306 | "version": "2.0.3",
307 | "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz",
308 | "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4="
309 | },
310 | "ipaddr.js": {
311 | "version": "1.9.1",
312 | "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz",
313 | "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g=="
314 | },
315 | "jwa": {
316 | "version": "1.4.1",
317 | "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz",
318 | "integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==",
319 | "requires": {
320 | "buffer-equal-constant-time": "1.0.1",
321 | "ecdsa-sig-formatter": "1.0.11",
322 | "safe-buffer": "^5.0.1"
323 | }
324 | },
325 | "jws": {
326 | "version": "3.2.2",
327 | "resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz",
328 | "integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==",
329 | "requires": {
330 | "jwa": "^1.4.1",
331 | "safe-buffer": "^5.0.1"
332 | }
333 | },
334 | "media-typer": {
335 | "version": "0.3.0",
336 | "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz",
337 | "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g="
338 | },
339 | "merge-descriptors": {
340 | "version": "1.0.1",
341 | "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz",
342 | "integrity": "sha1-sAqqVW3YtEVoFQ7J0blT8/kMu2E="
343 | },
344 | "methods": {
345 | "version": "1.1.2",
346 | "resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz",
347 | "integrity": "sha1-VSmk1nZUE07cxSZmVoNbD4Ua/O4="
348 | },
349 | "mime": {
350 | "version": "1.6.0",
351 | "resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz",
352 | "integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg=="
353 | },
354 | "mime-db": {
355 | "version": "1.44.0",
356 | "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.44.0.tgz",
357 | "integrity": "sha512-/NOTfLrsPBVeH7YtFPgsVWveuL+4SjjYxaQ1xtM1KMFj7HdxlBlxeyNLzhyJVx7r4rZGJAZ/6lkKCitSc/Nmpg=="
358 | },
359 | "mime-types": {
360 | "version": "2.1.27",
361 | "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.27.tgz",
362 | "integrity": "sha512-JIhqnCasI9yD+SsmkquHBxTSEuZdQX5BuQnS2Vc7puQQQ+8yiP5AY5uWhpdv4YL4VM5c6iliiYWPgJ/nJQLp7w==",
363 | "requires": {
364 | "mime-db": "1.44.0"
365 | }
366 | },
367 | "minimalistic-assert": {
368 | "version": "1.0.1",
369 | "resolved": "https://registry.npmjs.org/minimalistic-assert/-/minimalistic-assert-1.0.1.tgz",
370 | "integrity": "sha512-UtJcAD4yEaGtjPezWuO9wC4nwUnVH/8/Im3yEHQP4b67cXlD/Qr9hdITCU1xDbSEXg2XKNaP8jsReV7vQd00/A=="
371 | },
372 | "minimist": {
373 | "version": "1.2.5",
374 | "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz",
375 | "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw=="
376 | },
377 | "ms": {
378 | "version": "2.0.0",
379 | "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
380 | "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
381 | },
382 | "negotiator": {
383 | "version": "0.6.2",
384 | "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz",
385 | "integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw=="
386 | },
387 | "object-assign": {
388 | "version": "4.1.1",
389 | "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
390 | "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM="
391 | },
392 | "on-finished": {
393 | "version": "2.3.0",
394 | "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz",
395 | "integrity": "sha1-IPEzZIGwg811M3mSoWlxqi2QaUc=",
396 | "requires": {
397 | "ee-first": "1.1.1"
398 | }
399 | },
400 | "on-headers": {
401 | "version": "1.0.2",
402 | "resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.2.tgz",
403 | "integrity": "sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA=="
404 | },
405 | "parseurl": {
406 | "version": "1.3.3",
407 | "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz",
408 | "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ=="
409 | },
410 | "path-to-regexp": {
411 | "version": "0.1.7",
412 | "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz",
413 | "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w="
414 | },
415 | "proxy-addr": {
416 | "version": "2.0.6",
417 | "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.6.tgz",
418 | "integrity": "sha512-dh/frvCBVmSsDYzw6n926jv974gddhkFPfiN8hPOi30Wax25QZyZEGveluCgliBnqmuM+UJmBErbAUFIoDbjOw==",
419 | "requires": {
420 | "forwarded": "~0.1.2",
421 | "ipaddr.js": "1.9.1"
422 | }
423 | },
424 | "qs": {
425 | "version": "6.7.0",
426 | "resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz",
427 | "integrity": "sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ=="
428 | },
429 | "range-parser": {
430 | "version": "1.2.1",
431 | "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz",
432 | "integrity": "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg=="
433 | },
434 | "raw-body": {
435 | "version": "2.4.0",
436 | "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.4.0.tgz",
437 | "integrity": "sha512-4Oz8DUIwdvoa5qMJelxipzi/iJIi40O5cGV1wNYp5hvZP8ZN0T+jiNkL0QepXs+EsQ9XJ8ipEDoiH70ySUJP3Q==",
438 | "requires": {
439 | "bytes": "3.1.0",
440 | "http-errors": "1.7.2",
441 | "iconv-lite": "0.4.24",
442 | "unpipe": "1.0.0"
443 | }
444 | },
445 | "safe-buffer": {
446 | "version": "5.1.2",
447 | "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
448 | "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
449 | },
450 | "safer-buffer": {
451 | "version": "2.1.2",
452 | "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
453 | "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
454 | },
455 | "send": {
456 | "version": "0.17.1",
457 | "resolved": "https://registry.npmjs.org/send/-/send-0.17.1.tgz",
458 | "integrity": "sha512-BsVKsiGcQMFwT8UxypobUKyv7irCNRHk1T0G680vk88yf6LBByGcZJOTJCrTP2xVN6yI+XjPJcNuE3V4fT9sAg==",
459 | "requires": {
460 | "debug": "2.6.9",
461 | "depd": "~1.1.2",
462 | "destroy": "~1.0.4",
463 | "encodeurl": "~1.0.2",
464 | "escape-html": "~1.0.3",
465 | "etag": "~1.8.1",
466 | "fresh": "0.5.2",
467 | "http-errors": "~1.7.2",
468 | "mime": "1.6.0",
469 | "ms": "2.1.1",
470 | "on-finished": "~2.3.0",
471 | "range-parser": "~1.2.1",
472 | "statuses": "~1.5.0"
473 | },
474 | "dependencies": {
475 | "ms": {
476 | "version": "2.1.1",
477 | "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz",
478 | "integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg=="
479 | }
480 | }
481 | },
482 | "serve-static": {
483 | "version": "1.14.1",
484 | "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.14.1.tgz",
485 | "integrity": "sha512-JMrvUwE54emCYWlTI+hGrGv5I8dEwmco/00EvkzIIsR7MqrHonbD9pO2MOfFnpFntl7ecpZs+3mW+XbQZu9QCg==",
486 | "requires": {
487 | "encodeurl": "~1.0.2",
488 | "escape-html": "~1.0.3",
489 | "parseurl": "~1.3.3",
490 | "send": "0.17.1"
491 | }
492 | },
493 | "setprototypeof": {
494 | "version": "1.1.1",
495 | "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.1.tgz",
496 | "integrity": "sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw=="
497 | },
498 | "statuses": {
499 | "version": "1.5.0",
500 | "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz",
501 | "integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow="
502 | },
503 | "swagger-ui-dist": {
504 | "version": "3.25.1",
505 | "resolved": "https://registry.npmjs.org/swagger-ui-dist/-/swagger-ui-dist-3.25.1.tgz",
506 | "integrity": "sha512-Sw/K95j1pT9TZtLKiHDEml7YqcXC9thTTQjxrvNgd9j1KzOIxpo/5lhHuUMAN/hxVAHetzmcBcQaBjywRXog8w=="
507 | },
508 | "swagger-ui-express": {
509 | "version": "4.1.4",
510 | "resolved": "https://registry.npmjs.org/swagger-ui-express/-/swagger-ui-express-4.1.4.tgz",
511 | "integrity": "sha512-Ea96ecpC+Iq9GUqkeD/LFR32xSs8gYqmTW1gXCuKg81c26WV6ZC2FsBSPVExQP6WkyUuz5HEiR0sEv/HCC343g==",
512 | "requires": {
513 | "swagger-ui-dist": "^3.18.1"
514 | }
515 | },
516 | "toidentifier": {
517 | "version": "1.0.0",
518 | "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz",
519 | "integrity": "sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw=="
520 | },
521 | "type-is": {
522 | "version": "1.6.18",
523 | "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz",
524 | "integrity": "sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==",
525 | "requires": {
526 | "media-typer": "0.3.0",
527 | "mime-types": "~2.1.24"
528 | }
529 | },
530 | "unpipe": {
531 | "version": "1.0.0",
532 | "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz",
533 | "integrity": "sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw="
534 | },
535 | "urlsafe-base64": {
536 | "version": "1.0.0",
537 | "resolved": "https://registry.npmjs.org/urlsafe-base64/-/urlsafe-base64-1.0.0.tgz",
538 | "integrity": "sha1-I/iQaabGL0bPOh07ABac77kL4MY="
539 | },
540 | "utils-merge": {
541 | "version": "1.0.1",
542 | "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz",
543 | "integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM="
544 | },
545 | "vary": {
546 | "version": "1.1.2",
547 | "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
548 | "integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw="
549 | },
550 | "web-push": {
551 | "version": "3.4.3",
552 | "resolved": "https://registry.npmjs.org/web-push/-/web-push-3.4.3.tgz",
553 | "integrity": "sha512-nt/hRSlfRDTwvem//7jle1+cy62lBoxFshad8ai2Q4SlHZS00oHnrw5Dul3jSWXR+bOcnZkwnRs3tW+daNTuyA==",
554 | "requires": {
555 | "asn1.js": "^5.0.0",
556 | "http_ece": "1.1.0",
557 | "https-proxy-agent": "^3.0.0",
558 | "jws": "^3.1.3",
559 | "minimist": "^1.2.0",
560 | "urlsafe-base64": "^1.0.0"
561 | }
562 | }
563 | }
564 | }
565 |
--------------------------------------------------------------------------------
/back-end/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "back-end",
3 | "version": "1.0.0",
4 | "main": "index.js",
5 | "license": "MIT",
6 | "dependencies": {
7 | "body-parser": "^1.19.0",
8 | "compression": "^1.7.4",
9 | "cors": "^2.8.5",
10 | "express": "^4.17.1",
11 | "swagger-ui-express": "^4.0.7",
12 | "web-push": "^3.3.5"
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/back-end/src/app.js:
--------------------------------------------------------------------------------
1 | const express = require("express");
2 | const compression = require("compression");
3 | const bodyParser = require("body-parser");
4 | const cors = require("cors");
5 | const subscriptionHandler = require('./subscriptionHandler')
6 |
7 | const swaggerUi = require('swagger-ui-express')
8 | const swaggerDocument = require('./swagger.json');
9 |
10 | const app = express();
11 |
12 | app.use(cors())
13 | app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
14 | app.use(compression());
15 | app.use(bodyParser.urlencoded({ extended: true }));
16 | app.use(bodyParser.json());
17 |
18 | app.post("/subscription", subscriptionHandler.handlePushNotificationSubscription);
19 | app.get("/subscription/:id", subscriptionHandler.sendPushNotification);
20 |
21 | module.exports = app;
22 |
--------------------------------------------------------------------------------
/back-end/src/server.js:
--------------------------------------------------------------------------------
1 | const app = require("./app");
2 |
3 | const port = process.env.PORT || 8080;
4 | const host = process.env.HOSTNAME || "0.0.0.0";
5 |
6 | // Launch Node.js server
7 | const server = app.listen(port, host, () => {
8 | console.log(`Node.js API server is listening on http://${host}:${port}/`);
9 | });
10 |
11 | function handleExit(err) {
12 | if (err) {
13 | errors.report(err);
14 | }
15 | if (options.exit) {
16 | process.exit();
17 | }
18 | }
19 |
20 | process.on("exit", handleExit.bind(null));
21 | process.on("SIGINT", handleExit.bind(null));
22 | process.on("SIGTERM", handleExit.bind(null));
23 | process.on("uncaughtException", handleExit.bind(null));
24 |
--------------------------------------------------------------------------------
/back-end/src/subscriptionHandler.js:
--------------------------------------------------------------------------------
1 | const subscriptions = {};
2 | var crypto = require("crypto");
3 | const webpush = require("web-push");
4 |
5 | const vapidKeys = {
6 | privateKey: "bdSiNzUhUP6piAxLH-tW88zfBlWWveIx0dAsDO66aVU",
7 | publicKey: "BIN2Jc5Vmkmy-S3AUrcMlpKxJpLeVRAfu9WBqUbJ70SJOCWGCGXKY-Xzyh7HDr6KbRDGYHjqZ06OcS3BjD7uAm8"
8 | };
9 |
10 | webpush.setVapidDetails("mailto:example@yourdomain.org", vapidKeys.publicKey, vapidKeys.privateKey);
11 |
12 | function createHash(input) {
13 | const md5sum = crypto.createHash("md5");
14 | md5sum.update(Buffer.from(input));
15 | return md5sum.digest("hex");
16 | }
17 |
18 | function handlePushNotificationSubscription(req, res) {
19 | const subscriptionRequest = req.body;
20 | const susbscriptionId = createHash(JSON.stringify(subscriptionRequest));
21 | subscriptions[susbscriptionId] = subscriptionRequest;
22 | res.status(201).json({ id: susbscriptionId });
23 | }
24 |
25 | function sendPushNotification(req, res) {
26 | const subscriptionId = req.params.id;
27 | const pushSubscription = subscriptions[subscriptionId];
28 | webpush
29 | .sendNotification(
30 | pushSubscription,
31 | JSON.stringify({
32 | title: "New Product Available ",
33 | text: "HEY! Take a look at this brand new t-shirt!",
34 | image: "/images/jason-leung-HM6TMmevbZQ-unsplash.jpg",
35 | tag: "new-product",
36 | url: "/new-product-jason-leung-HM6TMmevbZQ-unsplash.html"
37 | })
38 | )
39 | .catch(err => {
40 | console.log(err);
41 | });
42 |
43 | res.status(202).json({});
44 | }
45 |
46 | module.exports = { handlePushNotificationSubscription, sendPushNotification };
47 |
--------------------------------------------------------------------------------
/back-end/src/swagger.json:
--------------------------------------------------------------------------------
1 | {
2 | "swagger": "2.0",
3 | "info": {
4 | "version": "1.0.0",
5 | "title": "Push server API",
6 | "description": "Web push notification demo server",
7 | "license": {
8 | "name": "MIT",
9 | "url": "https://opensource.org/licenses/MIT"
10 | }
11 | },
12 | "host": "push-notification-demo-server.herokuapp.com/",
13 | "basePath": "",
14 | "tags": [
15 | {
16 | "name": "Web Push Subscription",
17 | "description": "API for Web push subscription"
18 | }
19 | ],
20 | "schemes": ["https"],
21 | "consumes": ["application/json"],
22 | "produces": ["application/json"],
23 | "paths": {
24 | "/subscription": {
25 | "post": {
26 | "tags": ["Web Push Subscription"],
27 | "description": "Recevies a push subscription and save it in memory",
28 | "parameters": [
29 | {
30 | "name": "pushSubscription",
31 | "in": "body",
32 | "description": "the subscription to be saved",
33 | "schema": {
34 | "$ref": "#/definitions/pushSubscription"
35 | }
36 | }
37 | ],
38 | "produces": ["application/json"],
39 | "responses": {
40 | "201": {
41 | "description": "everything ok",
42 | "schema": {
43 | "$ref": "#/definitions/pushSubscriptionResponse"
44 | }
45 | }
46 | }
47 | }
48 | },
49 | "/subscription/{id}": {
50 | "parameters": [
51 | {
52 | "name": "id",
53 | "in": "path",
54 | "required": true,
55 | "description": "ID of the subscription that we want to find",
56 | "type": "string"
57 | }
58 | ],
59 | "get": {
60 | "tags": ["Web Push Subscription"],
61 | "summary": "Sends a push message to the existing push subscription",
62 | "responses": {
63 | "202": {
64 | "description": "Message sent"
65 | }
66 | }
67 | }
68 | }
69 | },
70 | "definitions": {
71 | "pushSubscription": {
72 | "required": ["endpoint"],
73 | "properties": {
74 | "endpoint": {
75 | "type": "string",
76 | "uniqueItems": true
77 | },
78 | "expirationTime": {
79 | "type": "string",
80 | "uniqueItems": true
81 | },
82 | "keys": {
83 | "$ref": "#/definitions/Keys"
84 | }
85 | }
86 | },
87 | "Keys": {
88 | "properties": {
89 | "p256dh": {
90 | "type": "string",
91 | "uniqueItems": true
92 | },
93 | "auth": {
94 | "type": "string",
95 | "uniqueItems": true
96 | }
97 | }
98 | },
99 | "pushSubscriptionResponse": {
100 | "required": ["id"],
101 | "properties": {
102 | "id": {
103 | "type": "string",
104 | "uniqueItems": true
105 | }
106 | }
107 | }
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/front-end-react/.gitignore:
--------------------------------------------------------------------------------
1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2 |
3 | # dependencies
4 | /node_modules
5 | /.pnp
6 | .pnp.js
7 |
8 | # testing
9 | /coverage
10 |
11 | # production
12 | /build
13 |
14 | # misc
15 | .DS_Store
16 | .env.local
17 | .env.development.local
18 | .env.test.local
19 | .env.production.local
20 |
21 | npm-debug.log*
22 | yarn-debug.log*
23 | yarn-error.log*
24 |
--------------------------------------------------------------------------------
/front-end-react/README.md:
--------------------------------------------------------------------------------
1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
2 |
3 | ## Available Scripts
4 |
5 | In the project directory, you can run:
6 |
7 | ### `npm start`
8 |
9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
11 |
12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console.
14 |
15 | ### `npm test`
16 |
17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
19 |
20 | ### `npm run build`
21 |
22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance.
24 |
25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed!
27 |
28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
29 |
30 | ### `npm run eject`
31 |
32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!**
33 |
34 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
35 |
36 | Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
37 |
38 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
39 |
40 | ## Learn More
41 |
42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
43 |
44 | To learn React, check out the [React documentation](https://reactjs.org/).
45 |
46 | ### Code Splitting
47 |
48 | This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting
49 |
50 | ### Analyzing the Bundle Size
51 |
52 | This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size
53 |
54 | ### Making a Progressive Web App
55 |
56 | This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app
57 |
58 | ### Advanced Configuration
59 |
60 | This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration
61 |
62 | ### Deployment
63 |
64 | This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
65 |
66 | ### `npm run build` fails to minify
67 |
68 | This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
69 |
--------------------------------------------------------------------------------
/front-end-react/config/env.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | const fs = require('fs');
4 | const path = require('path');
5 | const paths = require('./paths');
6 |
7 | // Make sure that including paths.js after env.js will read .env variables.
8 | delete require.cache[require.resolve('./paths')];
9 |
10 | const NODE_ENV = process.env.NODE_ENV;
11 | if (!NODE_ENV) {
12 | throw new Error(
13 | 'The NODE_ENV environment variable is required but was not specified.'
14 | );
15 | }
16 |
17 | // https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
18 | var dotenvFiles = [
19 | `${paths.dotenv}.${NODE_ENV}.local`,
20 | `${paths.dotenv}.${NODE_ENV}`,
21 | // Don't include `.env.local` for `test` environment
22 | // since normally you expect tests to produce the same
23 | // results for everyone
24 | NODE_ENV !== 'test' && `${paths.dotenv}.local`,
25 | paths.dotenv,
26 | ].filter(Boolean);
27 |
28 | // Load environment variables from .env* files. Suppress warnings using silent
29 | // if this file is missing. dotenv will never modify any environment variables
30 | // that have already been set. Variable expansion is supported in .env files.
31 | // https://github.com/motdotla/dotenv
32 | // https://github.com/motdotla/dotenv-expand
33 | dotenvFiles.forEach(dotenvFile => {
34 | if (fs.existsSync(dotenvFile)) {
35 | require('dotenv-expand')(
36 | require('dotenv').config({
37 | path: dotenvFile,
38 | })
39 | );
40 | }
41 | });
42 |
43 | // We support resolving modules according to `NODE_PATH`.
44 | // This lets you use absolute paths in imports inside large monorepos:
45 | // https://github.com/facebook/create-react-app/issues/253.
46 | // It works similar to `NODE_PATH` in Node itself:
47 | // https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
48 | // Note that unlike in Node, only *relative* paths from `NODE_PATH` are honored.
49 | // Otherwise, we risk importing Node.js core modules into an app instead of Webpack shims.
50 | // https://github.com/facebook/create-react-app/issues/1023#issuecomment-265344421
51 | // We also resolve them to make sure all tools using them work consistently.
52 | const appDirectory = fs.realpathSync(process.cwd());
53 | process.env.NODE_PATH = (process.env.NODE_PATH || '')
54 | .split(path.delimiter)
55 | .filter(folder => folder && !path.isAbsolute(folder))
56 | .map(folder => path.resolve(appDirectory, folder))
57 | .join(path.delimiter);
58 |
59 | // Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be
60 | // injected into the application via DefinePlugin in Webpack configuration.
61 | const REACT_APP = /^REACT_APP_/i;
62 |
63 | function getClientEnvironment(publicUrl) {
64 | const raw = Object.keys(process.env)
65 | .filter(key => REACT_APP.test(key))
66 | .reduce(
67 | (env, key) => {
68 | env[key] = process.env[key];
69 | return env;
70 | },
71 | {
72 | // Useful for determining whether we’re running in production mode.
73 | // Most importantly, it switches React into the correct mode.
74 | NODE_ENV: process.env.NODE_ENV || 'development',
75 | // Useful for resolving the correct path to static assets in `public`.
76 | // For example, .
77 | // This should only be used as an escape hatch. Normally you would put
78 | // images into the `src` and `import` them in code to get their paths.
79 | PUBLIC_URL: publicUrl,
80 | }
81 | );
82 | // Stringify all values so we can feed into Webpack DefinePlugin
83 | const stringified = {
84 | 'process.env': Object.keys(raw).reduce((env, key) => {
85 | env[key] = JSON.stringify(raw[key]);
86 | return env;
87 | }, {}),
88 | };
89 |
90 | return { raw, stringified };
91 | }
92 |
93 | module.exports = getClientEnvironment;
94 |
--------------------------------------------------------------------------------
/front-end-react/config/jest/cssTransform.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | // This is a custom Jest transformer turning style imports into empty objects.
4 | // http://facebook.github.io/jest/docs/en/webpack.html
5 |
6 | module.exports = {
7 | process() {
8 | return 'module.exports = {};';
9 | },
10 | getCacheKey() {
11 | // The output is always the same.
12 | return 'cssTransform';
13 | },
14 | };
15 |
--------------------------------------------------------------------------------
/front-end-react/config/jest/fileTransform.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | const path = require('path');
4 | const camelcase = require('camelcase');
5 |
6 | // This is a custom Jest transformer turning file imports into filenames.
7 | // http://facebook.github.io/jest/docs/en/webpack.html
8 |
9 | module.exports = {
10 | process(src, filename) {
11 | const assetFilename = JSON.stringify(path.basename(filename));
12 |
13 | if (filename.match(/\.svg$/)) {
14 | // Based on how SVGR generates a component name:
15 | // https://github.com/smooth-code/svgr/blob/01b194cf967347d43d4cbe6b434404731b87cf27/packages/core/src/state.js#L6
16 | const pascalCaseFileName = camelcase(path.parse(filename).name, {
17 | pascalCase: true,
18 | });
19 | const componentName = `Svg${pascalCaseFileName}`;
20 | return `const React = require('react');
21 | module.exports = {
22 | __esModule: true,
23 | default: ${assetFilename},
24 | ReactComponent: React.forwardRef(function ${componentName}(props, ref) {
25 | return {
26 | $$typeof: Symbol.for('react.element'),
27 | type: 'svg',
28 | ref: ref,
29 | key: null,
30 | props: Object.assign({}, props, {
31 | children: ${assetFilename}
32 | })
33 | };
34 | }),
35 | };`;
36 | }
37 |
38 | return `module.exports = ${assetFilename};`;
39 | },
40 | };
41 |
--------------------------------------------------------------------------------
/front-end-react/config/modules.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | const fs = require('fs');
4 | const path = require('path');
5 | const paths = require('./paths');
6 | const chalk = require('react-dev-utils/chalk');
7 | const resolve = require('resolve');
8 |
9 | /**
10 | * Get the baseUrl of a compilerOptions object.
11 | *
12 | * @param {Object} options
13 | */
14 | function getAdditionalModulePaths(options = {}) {
15 | const baseUrl = options.baseUrl;
16 |
17 | // We need to explicitly check for null and undefined (and not a falsy value) because
18 | // TypeScript treats an empty string as `.`.
19 | if (baseUrl == null) {
20 | // If there's no baseUrl set we respect NODE_PATH
21 | // Note that NODE_PATH is deprecated and will be removed
22 | // in the next major release of create-react-app.
23 |
24 | const nodePath = process.env.NODE_PATH || '';
25 | return nodePath.split(path.delimiter).filter(Boolean);
26 | }
27 |
28 | const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
29 |
30 | // We don't need to do anything if `baseUrl` is set to `node_modules`. This is
31 | // the default behavior.
32 | if (path.relative(paths.appNodeModules, baseUrlResolved) === '') {
33 | return null;
34 | }
35 |
36 | // Allow the user set the `baseUrl` to `appSrc`.
37 | if (path.relative(paths.appSrc, baseUrlResolved) === '') {
38 | return [paths.appSrc];
39 | }
40 |
41 | // Otherwise, throw an error.
42 | throw new Error(
43 | chalk.red.bold(
44 | "Your project's `baseUrl` can only be set to `src` or `node_modules`." +
45 | ' Create React App does not support other values at this time.'
46 | )
47 | );
48 | }
49 |
50 | function getModules() {
51 | // Check if TypeScript is setup
52 | const hasTsConfig = fs.existsSync(paths.appTsConfig);
53 | const hasJsConfig = fs.existsSync(paths.appJsConfig);
54 |
55 | if (hasTsConfig && hasJsConfig) {
56 | throw new Error(
57 | 'You have both a tsconfig.json and a jsconfig.json. If you are using TypeScript please remove your jsconfig.json file.'
58 | );
59 | }
60 |
61 | let config;
62 |
63 | // If there's a tsconfig.json we assume it's a
64 | // TypeScript project and set up the config
65 | // based on tsconfig.json
66 | if (hasTsConfig) {
67 | const ts = require(resolve.sync('typescript', {
68 | basedir: paths.appNodeModules,
69 | }));
70 | config = ts.readConfigFile(paths.appTsConfig, ts.sys.readFile).config;
71 | // Otherwise we'll check if there is jsconfig.json
72 | // for non TS projects.
73 | } else if (hasJsConfig) {
74 | config = require(paths.appJsConfig);
75 | }
76 |
77 | config = config || {};
78 | const options = config.compilerOptions || {};
79 |
80 | const additionalModulePaths = getAdditionalModulePaths(options);
81 |
82 | return {
83 | additionalModulePaths: additionalModulePaths,
84 | hasTsConfig,
85 | };
86 | }
87 |
88 | module.exports = getModules();
89 |
--------------------------------------------------------------------------------
/front-end-react/config/paths.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | const path = require('path');
4 | const fs = require('fs');
5 | const url = require('url');
6 |
7 | // Make sure any symlinks in the project folder are resolved:
8 | // https://github.com/facebook/create-react-app/issues/637
9 | const appDirectory = fs.realpathSync(process.cwd());
10 | const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
11 |
12 | const envPublicUrl = process.env.PUBLIC_URL;
13 |
14 | function ensureSlash(inputPath, needsSlash) {
15 | const hasSlash = inputPath.endsWith('/');
16 | if (hasSlash && !needsSlash) {
17 | return inputPath.substr(0, inputPath.length - 1);
18 | } else if (!hasSlash && needsSlash) {
19 | return `${inputPath}/`;
20 | } else {
21 | return inputPath;
22 | }
23 | }
24 |
25 | const getPublicUrl = appPackageJson =>
26 | envPublicUrl || require(appPackageJson).homepage;
27 |
28 | // We use `PUBLIC_URL` environment variable or "homepage" field to infer
29 | // "public path" at which the app is served.
30 | // Webpack needs to know it to put the right
6 |
13 |
14 |
15 |
16 |