Skip to content

Commit e9934f6

Browse files
committed
Revert attempt to make project into type "module" (BL-12843)
This was causing an undetermined error when deployed to a real server.
1 parent 96582c3 commit e9934f6

9 files changed

+65
-57
lines changed

.eslintrc.json

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
},
77
"parserOptions": {
88
"ecmaVersion": 2021,
9-
"sourceType": "module",
109
"requireConfigFile": false
1110
},
1211
"parser": "@babel/eslint-parser",

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,7 @@ node_modules
2828

2929
# Emacs
3030
*~
31+
3132
*.orig
33+
34+
.eslintcache

bloomFirebaseAuthAdapter.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import jwt from "jsonwebtoken";
2-
import { get as httpGet } from "./httpsRequest.js";
1+
const httpsRequest = require("./httpsRequest");
2+
const jwt = require("jsonwebtoken");
33

44
// This adapter, modified from the 'apple' one in parse-server, validates a user when
55
// presented with a valid, current firebase-auth token from the appropriate domain whose email
@@ -21,7 +21,7 @@ let currentKey;
2121
const getPublicKeys = async () => {
2222
let data;
2323
try {
24-
data = await httpGet(TOKEN_ISSUER);
24+
data = await httpsRequest.get(TOKEN_ISSUER);
2525
} catch (e) {
2626
if (currentKey) {
2727
return currentKey;
@@ -121,7 +121,7 @@ function validateAppId() {
121121
return Promise.resolve();
122122
}
123123

124-
export default {
124+
module.exports = {
125125
validateAppId,
126126
validateAuthData,
127127
};

cloud/emails.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Parse.Cloud.define("testBookSaved", async () => {
4747

4848
// Send an email to notify about a newly created book.
4949
// It is sent to an internal address, set by environment variable EMAIL_BOOK_EVENT_RECIPIENT on the server.
50-
export const sendEmailAboutNewBookAsync = async (parseBook) => {
50+
exports.sendEmailAboutNewBookAsync = async (parseBook) => {
5151
var bookId = parseBook.id;
5252
var query = new Parse.Query("books");
5353
query.equalTo("objectId", bookId);
@@ -105,7 +105,7 @@ function sendEmailAboutBookAsync(
105105
};
106106
Object.assign(/*target=*/ data, /*source=*/ dataForEmailClientJson);
107107

108-
const mailgun = import("mailgun-js");
108+
const mailgun = require("mailgun-js");
109109
const mg = mailgun({
110110
apiKey: process.env.MAILGUN_API_KEY,
111111
domain: "bloomlibrary.org",

cloud/main.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import "./emails.js"; // allows email-specific cloud functions to be defined
2-
import "./utility.js"; // utility functions which don't belong in the main file
1+
require("./emails.js"); // allows email-specific could functions to be defined
2+
require("./utility.js"); // utility functions which don't belong in the main file
33

44
// This function will call save on every book. This is useful for
55
// applying the functionality in beforeSaveBook to every book,

httpsRequest.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// This helper file is needed by bloomFirebaseAuthAdapter.
22
// It was copied (and slightly modified) from a file required by apple authAdapter in parser-server core code.
3-
import https from "https";
3+
const https = require("https");
44

55
function makeCallback(resolve, reject, noJSON) {
66
return function (res) {
@@ -23,19 +23,21 @@ function makeCallback(resolve, reject, noJSON) {
2323
};
2424
}
2525

26-
export function get(options, noJSON = false) {
26+
function get(options, noJSON = false) {
2727
return new Promise((resolve, reject) => {
2828
https
2929
.get(options, makeCallback(resolve, reject, noJSON))
3030
.on("error", reject);
3131
});
3232
}
3333

34-
export function request(options, postData) {
34+
function request(options, postData) {
3535
return new Promise((resolve, reject) => {
3636
const req = https.request(options, makeCallback(resolve, reject));
3737
req.on("error", reject);
3838
req.write(postData);
3939
req.end();
4040
});
4141
}
42+
43+
module.exports = { get, request };

index.js

+30-26
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import express from "express";
2-
import { ParseServer } from "parse-server";
3-
import http from "http";
4-
import ParseDashboard from "parse-dashboard";
5-
import BloomFirebaseAuthAdapter from "./bloomFirebaseAuthAdapter.js";
1+
// Feb 2023, we tried to upgrade this project to be of type "module" and use import/export instead of require.
2+
// It worked locally, but when we deployed to a real server, the parse server failed to start.
3+
// I never could determine why. So I punted and reverted to using require.
4+
const express = require("express");
5+
const ParseServer = require("parse-server").ParseServer;
6+
const ParseDashboard = require("parse-dashboard");
7+
const BloomFirebaseAuthAdapter = require("./bloomFirebaseAuthAdapter");
68

79
const databaseUri = process.env.DATABASE_URI || process.env.MONGODB_URI;
810

@@ -11,10 +13,10 @@ if (!databaseUri) {
1113
}
1214

1315
const serverConfig = {
14-
databaseURI: databaseUri || "mongodb://localhost:27017/dev",
15-
cloud: function () {
16-
import("./cloud/main.js");
17-
},
16+
// Somehow, node 18 causes localhost to try to resolve as IPv6 here which can break things.
17+
// Using 127.0.0.1 instead works around that.
18+
databaseURI: databaseUri || "mongodb://127.0.0.1:27017/dev",
19+
cloud: process.env.CLOUD_CODE_MAIN || __dirname + "/cloud/main.js",
1820
appId: process.env.APP_ID || "myAppId",
1921
masterKey: process.env.MASTER_KEY || "123",
2022
readOnlyMasterKey: process.env.READ_ONLY_MASTER_KEY || "ro",
@@ -61,24 +63,26 @@ const app = express();
6163
// Serve the Parse API on the /parse URL prefix
6264
const mountPath = process.env.PARSE_MOUNT || "/parse";
6365
const server = new ParseServer(serverConfig);
64-
await server.start();
65-
app.use(mountPath, server.app);
66+
// For an unknown reason, when deployed on a real server, await server.start() causes the server to never successfully start.
67+
server.start().then(() => {
68+
app.use(mountPath, server.app);
6669

67-
// The main thing here is the google-site-verification meta tag.
68-
// This lets us access the site on the Google Search Console.
69-
app.get("/", function (req, res) {
70-
res.status(200).send(
71-
"<html>" +
72-
'<head><meta name="google-site-verification" content="dm8VsqC5uw-fikoD-4ZxYbPfzV-qYyrPCJq7aIgvlJo" /></head>' +
73-
'<body><a href="https://bloomlibrary.org">Bloom Library</a></body>' +
74-
"</html>"
75-
);
76-
});
70+
// The main thing here is the google-site-verification meta tag.
71+
// This lets us access the site on the Google Search Console.
72+
app.get("/", function (req, res) {
73+
res.status(200).send(
74+
"<html>" +
75+
'<head><meta name="google-site-verification" content="dm8VsqC5uw-fikoD-4ZxYbPfzV-qYyrPCJq7aIgvlJo" /></head>' +
76+
'<body><a href="https://bloomlibrary.org">Bloom Library</a></body>' +
77+
"</html>"
78+
);
79+
});
7780

78-
app.use("/dashboard", dashboard);
81+
app.use("/dashboard", dashboard);
7982

80-
const port = process.env.PORT || 1337;
81-
const httpServer = http.createServer(app);
82-
httpServer.listen(port, function () {
83-
console.log("bloom-parse-server running on port " + port + ".");
83+
const port = process.env.PORT || 1337;
84+
const httpServer = require("http").createServer(app);
85+
httpServer.listen(port, function () {
86+
console.log("bloom-parse-server running on port " + port + ".");
87+
});
8488
});

package-lock.json

+18-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"main": "index.js",
1111
"scripts": {
1212
"start": "node index.js",
13+
"lint": "eslint --cache ./cloud && eslint --cache index.js",
1314
"postinstall": "patch-package"
1415
},
1516
"// mailgun-js is deprecated; we should replace it with officially-supported mailgun.js": " ",
@@ -21,7 +22,6 @@
2122
"parse-server": "7.0.0-alpha.1",
2223
"patch-package": "8.0.0"
2324
},
24-
"type": "module",
2525
"devDependencies": {
2626
"@babel/eslint-parser": "7.21.3",
2727
"eslint": "8.38.0",

0 commit comments

Comments
 (0)