Skip to content

Commit

Permalink
Fix serialization error
Browse files Browse the repository at this point in the history
  • Loading branch information
codetheweb committed Apr 4, 2024
1 parent abdfbd3 commit 9c423b4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@semantic-release/git": "10.0.1",
"@semantic-release/npm": "9.0.1",
"@semantic-release/release-notes-generator": "10.0.3",
"@types/lodash": "4.17.0",
"@types/node": "18.7.14",
"@types/object-hash": "2.2.1",
"@types/pg": "8.6.5",
Expand All @@ -46,6 +47,7 @@
"dependencies": {
"async-mutex": "0.4.0",
"birpc": "0.2.17",
"lodash": "4.17.21",
"nanoid": "4.0.0",
"object-hash": "3.0.0",
"pg": "8.8.0"
Expand Down
39 changes: 39 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,39 @@ import { once } from "node:events"
import { createBirpc } from "birpc"
import { SharedWorkerFunctions, TestWorkerFunctions } from "./lib/rpc"
import { ExecResult } from "testcontainers"
import isPlainObject from "lodash/isPlainObject"

// https://stackoverflow.com/a/30580513
const isSerializable = (obj: Record<any, any>): boolean => {
var isNestedSerializable
function isPlain(val: any) {
return (
typeof val === "undefined" ||
typeof val === "string" ||
typeof val === "boolean" ||
typeof val === "number" ||
Array.isArray(val) ||
isPlainObject(val)
)
}
if (!isPlain(obj)) {
return false
}
for (var property in obj) {
if (obj.hasOwnProperty(property)) {
if (!isPlain(obj[property])) {
return false
}
if (typeof obj[property] == "object") {
isNestedSerializable = isSerializable(obj[property])
if (!isNestedSerializable) {
return false
}
}
}
}
return true
}

const getWorker = async (
initialData: InitialWorkerData,
Expand Down Expand Up @@ -132,6 +165,12 @@ export const getTestPostgresDatabaseFactory = <

await teardownConnection(connectionDetails)

if (hookResult && !isSerializable(hookResult)) {
throw new TypeError(
"Return value of beforeTemplateIsBaked() hook could not be serialized. Make sure it returns only JSON-serializable values."
)
}

return hookResult
}
},
Expand Down
9 changes: 7 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,11 @@
"@types/docker-modem" "*"
"@types/node" "*"

"@types/lodash@4.17.0":
version "4.17.0"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.17.0.tgz#d774355e41f372d5350a4d0714abb48194a489c3"
integrity sha512-t7dhREVv6dbNj0q17X12j7yDG4bD/DHYX7o5/DbDxobP0HnGPgpRz2Ej77aL7TZT3DSw13fqUTj8J4mMnqa7WA==

"@types/minimist@^1.2.0":
version "1.2.2"
resolved "https://registry.npmjs.org/@types/minimist/-/minimist-1.2.2.tgz"
Expand Down Expand Up @@ -2893,9 +2898,9 @@ lodash.uniqby@^4.7.0:
resolved "https://registry.npmjs.org/lodash.uniqby/-/lodash.uniqby-4.7.0.tgz"
integrity sha512-e/zcLx6CSbmaEgFHCA7BnoQKyCtKMxnuWrJygbwPs/AIn+IMKl66L8/s+wBUn5LRw2pZx3bUHibiV1b6aTWIww==

lodash@^4.17.15, lodash@^4.17.21, lodash@^4.17.4:
lodash@4.17.21, lodash@^4.17.15, lodash@^4.17.21, lodash@^4.17.4:
version "4.17.21"
resolved "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==

lru-cache@^6.0.0:
Expand Down

0 comments on commit 9c423b4

Please sign in to comment.