@agnostack/next-shopify 1.15.0-beta.9 → 1.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (324) hide show
  1. package/CHANGELOG.md +189 -0
  2. package/README.md +9 -8
  3. package/dist/lib/handlers/api/auth.js +15 -11
  4. package/dist/lib/handlers/api/auth.js.map +1 -1
  5. package/dist/lib/handlers/api/billing.js +13 -9
  6. package/dist/lib/handlers/api/billing.js.map +1 -1
  7. package/dist/lib/handlers/api/callback.js +37 -33
  8. package/dist/lib/handlers/api/callback.js.map +1 -1
  9. package/dist/lib/handlers/api/index.js +25 -7
  10. package/dist/lib/handlers/api/index.js.map +1 -1
  11. package/dist/lib/handlers/api/uninstall.js +12 -8
  12. package/dist/lib/handlers/api/uninstall.js.map +1 -1
  13. package/dist/lib/handlers/api/verifiable/appInstallation.js +11 -7
  14. package/dist/lib/handlers/api/verifiable/appInstallation.js.map +1 -1
  15. package/dist/lib/handlers/api/verifiable/graph.js +26 -22
  16. package/dist/lib/handlers/api/verifiable/graph.js.map +1 -1
  17. package/dist/lib/handlers/api/verifiable/index.js +21 -5
  18. package/dist/lib/handlers/api/verifiable/index.js.map +1 -1
  19. package/dist/lib/handlers/api/verifiable/proxy.js +39 -32
  20. package/dist/lib/handlers/api/verifiable/proxy.js.map +1 -1
  21. package/dist/lib/handlers/api/verifiable/rest.js +23 -19
  22. package/dist/lib/handlers/api/verifiable/rest.js.map +1 -1
  23. package/dist/lib/handlers/api/verifiable/token.js +27 -23
  24. package/dist/lib/handlers/api/verifiable/token.js.map +1 -1
  25. package/dist/lib/handlers/api/webhook.js +13 -9
  26. package/dist/lib/handlers/api/webhook.js.map +1 -1
  27. package/dist/lib/handlers/index.js +18 -2
  28. package/dist/lib/handlers/index.js.map +1 -1
  29. package/dist/lib/handlers/ssr/index.js +18 -2
  30. package/dist/lib/handlers/ssr/index.js.map +1 -1
  31. package/dist/lib/handlers/ssr/protect.js +24 -17
  32. package/dist/lib/handlers/ssr/protect.js.map +1 -1
  33. package/dist/lib/handlers/ssr/wrap.js +10 -6
  34. package/dist/lib/handlers/ssr/wrap.js.map +1 -1
  35. package/dist/lib/index.js +23 -4
  36. package/dist/lib/index.js.map +1 -1
  37. package/dist/lib/utils/billing.js +23 -18
  38. package/dist/lib/utils/billing.js.map +1 -1
  39. package/dist/lib/utils/crypto.js +25 -15
  40. package/dist/lib/utils/crypto.js.map +1 -1
  41. package/dist/lib/utils/firebase.js +29 -17
  42. package/dist/lib/utils/firebase.js.map +1 -1
  43. package/dist/lib/utils/headers.js +13 -6
  44. package/dist/lib/utils/headers.js.map +1 -1
  45. package/dist/lib/utils/index.js +24 -8
  46. package/dist/lib/utils/index.js.map +1 -1
  47. package/dist/lib/utils/session.js +64 -59
  48. package/dist/lib/utils/session.js.map +1 -1
  49. package/dist/lib/utils/shopify-api-js/oauth.js +23 -16
  50. package/dist/lib/utils/shopify-api-js/oauth.js.map +1 -1
  51. package/dist/lib/utils/shopify.js +37 -25
  52. package/dist/lib/utils/shopify.js.map +1 -1
  53. package/dist/lib/utils/token.js +15 -11
  54. package/dist/lib/utils/token.js.map +1 -1
  55. package/dist/plugins/index.js +18 -2
  56. package/dist/plugins/index.js.map +1 -1
  57. package/dist/plugins/withShopify.js +83 -79
  58. package/dist/plugins/withShopify.js.map +1 -1
  59. package/dist/react/hooks/index.js +17 -1
  60. package/dist/react/hooks/index.js.map +1 -1
  61. package/dist/react/hooks/useVerification.d.ts +1 -1
  62. package/dist/react/hooks/useVerification.js +23 -7
  63. package/dist/react/hooks/useVerification.js.map +1 -1
  64. package/dist/react/index.js +18 -2
  65. package/dist/react/index.js.map +1 -1
  66. package/dist/runtime/index.js +17 -1
  67. package/dist/runtime/index.js.map +1 -1
  68. package/dist/shared/WebCrypto.d.ts +55 -0
  69. package/dist/shared/WebCrypto.d.ts.map +1 -0
  70. package/dist/shared/WebCrypto.js +342 -0
  71. package/dist/shared/WebCrypto.js.map +1 -0
  72. package/dist/shared/constants.js +15 -11
  73. package/dist/shared/constants.js.map +1 -1
  74. package/dist/shared/datetime.js +110 -83
  75. package/dist/shared/datetime.js.map +1 -1
  76. package/dist/shared/display.js +231 -151
  77. package/dist/shared/display.js.map +1 -1
  78. package/dist/shared/errors.js +12 -5
  79. package/dist/shared/errors.js.map +1 -1
  80. package/dist/shared/graph.js +14 -11
  81. package/dist/shared/graph.js.map +1 -1
  82. package/dist/shared/index.d.ts +1 -1
  83. package/dist/shared/index.d.ts.map +1 -1
  84. package/dist/shared/index.js +32 -11
  85. package/dist/shared/index.js.map +1 -1
  86. package/dist/shared/match.js +25 -17
  87. package/dist/shared/match.js.map +1 -1
  88. package/dist/shared/request.js +19 -13
  89. package/dist/shared/request.js.map +1 -1
  90. package/dist/shared/shopify.js +31 -21
  91. package/dist/shared/shopify.js.map +1 -1
  92. package/dist/shared/uuid.js +8 -3
  93. package/dist/shared/uuid.js.map +1 -1
  94. package/dist/shared/verification.d.ts +12 -5
  95. package/dist/shared/verification.d.ts.map +1 -1
  96. package/dist/shared/verification.js +34 -22
  97. package/dist/shared/verification.js.map +1 -1
  98. package/dist/umd/{verification.js → verification/index.js} +714 -352
  99. package/dist/umd/verification/index.js.map +1 -0
  100. package/dist/umd/{react → verification/react}/hooks/useVerification.d.ts +1 -1
  101. package/dist/umd/verification/shared/WebCrypto.d.ts +55 -0
  102. package/dist/umd/verification/shared/WebCrypto.d.ts.map +1 -0
  103. package/dist/umd/{shared → verification/shared}/index.d.ts +1 -1
  104. package/dist/umd/{shared → verification/shared}/index.d.ts.map +1 -1
  105. package/dist/umd/{shared → verification/shared}/verification.d.ts +12 -5
  106. package/dist/umd/{shared → verification/shared}/verification.d.ts.map +1 -1
  107. package/dist/umd/verification/{index.d.ts → verification/index.d.ts} +1 -0
  108. package/dist/umd/verification/{index.d.ts.map → verification/index.d.ts.map} +1 -1
  109. package/dist/umd/verification/{utils → verification/utils}/rawbody.d.ts.map +1 -1
  110. package/dist/umd/verification/verification/verification.d.ts +10 -0
  111. package/dist/umd/verification/{verification.d.ts.map → verification/verification.d.ts.map} +1 -1
  112. package/dist/verification/index.d.ts +1 -0
  113. package/dist/verification/index.d.ts.map +1 -1
  114. package/dist/verification/index.js +19 -2
  115. package/dist/verification/index.js.map +1 -1
  116. package/dist/verification/utils/errors.js +5 -1
  117. package/dist/verification/utils/errors.js.map +1 -1
  118. package/dist/verification/utils/index.js +18 -2
  119. package/dist/verification/utils/index.js.map +1 -1
  120. package/dist/verification/utils/rawbody.d.ts.map +1 -1
  121. package/dist/verification/utils/rawbody.js +39 -36
  122. package/dist/verification/utils/rawbody.js.map +1 -1
  123. package/dist/verification/verification.d.ts +3 -1
  124. package/dist/verification/verification.d.ts.map +1 -1
  125. package/dist/verification/verification.js +71 -61
  126. package/dist/verification/verification.js.map +1 -1
  127. package/node_modules/bytes/History.md +97 -0
  128. package/node_modules/bytes/LICENSE +23 -0
  129. package/node_modules/bytes/Readme.md +152 -0
  130. package/node_modules/bytes/index.js +170 -0
  131. package/node_modules/bytes/package.json +42 -0
  132. package/node_modules/depd/History.md +103 -0
  133. package/node_modules/depd/LICENSE +22 -0
  134. package/node_modules/depd/Readme.md +280 -0
  135. package/node_modules/depd/index.js +538 -0
  136. package/node_modules/depd/lib/browser/index.js +77 -0
  137. package/node_modules/depd/package.json +45 -0
  138. package/node_modules/http-errors/HISTORY.md +180 -0
  139. package/node_modules/http-errors/LICENSE +23 -0
  140. package/node_modules/http-errors/README.md +169 -0
  141. package/node_modules/http-errors/index.js +289 -0
  142. package/node_modules/http-errors/package.json +50 -0
  143. package/node_modules/iconv-lite/Changelog.md +162 -0
  144. package/node_modules/iconv-lite/LICENSE +21 -0
  145. package/node_modules/iconv-lite/README.md +156 -0
  146. package/node_modules/iconv-lite/encodings/dbcs-codec.js +555 -0
  147. package/node_modules/iconv-lite/encodings/dbcs-data.js +176 -0
  148. package/node_modules/iconv-lite/encodings/index.js +22 -0
  149. package/node_modules/iconv-lite/encodings/internal.js +188 -0
  150. package/node_modules/iconv-lite/encodings/sbcs-codec.js +72 -0
  151. package/node_modules/iconv-lite/encodings/sbcs-data-generated.js +451 -0
  152. package/node_modules/iconv-lite/encodings/sbcs-data.js +174 -0
  153. package/node_modules/iconv-lite/encodings/tables/big5-added.json +122 -0
  154. package/node_modules/iconv-lite/encodings/tables/cp936.json +264 -0
  155. package/node_modules/iconv-lite/encodings/tables/cp949.json +273 -0
  156. package/node_modules/iconv-lite/encodings/tables/cp950.json +177 -0
  157. package/node_modules/iconv-lite/encodings/tables/eucjp.json +182 -0
  158. package/node_modules/iconv-lite/encodings/tables/gb18030-ranges.json +1 -0
  159. package/node_modules/iconv-lite/encodings/tables/gbk-added.json +55 -0
  160. package/node_modules/iconv-lite/encodings/tables/shiftjis.json +125 -0
  161. package/node_modules/iconv-lite/encodings/utf16.js +177 -0
  162. package/node_modules/iconv-lite/encodings/utf7.js +290 -0
  163. package/node_modules/iconv-lite/lib/bom-handling.js +52 -0
  164. package/node_modules/iconv-lite/lib/extend-node.js +217 -0
  165. package/node_modules/iconv-lite/lib/index.d.ts +24 -0
  166. package/node_modules/iconv-lite/lib/index.js +153 -0
  167. package/node_modules/iconv-lite/lib/streams.js +121 -0
  168. package/node_modules/iconv-lite/package.json +46 -0
  169. package/node_modules/inherits/LICENSE +16 -0
  170. package/node_modules/inherits/README.md +42 -0
  171. package/node_modules/inherits/inherits.js +9 -0
  172. package/node_modules/inherits/inherits_browser.js +27 -0
  173. package/node_modules/inherits/package.json +29 -0
  174. package/node_modules/raw-body/HISTORY.md +308 -0
  175. package/node_modules/raw-body/LICENSE +22 -0
  176. package/node_modules/raw-body/README.md +223 -0
  177. package/node_modules/raw-body/SECURITY.md +24 -0
  178. package/node_modules/raw-body/index.d.ts +87 -0
  179. package/node_modules/raw-body/index.js +336 -0
  180. package/node_modules/raw-body/package.json +49 -0
  181. package/node_modules/safer-buffer/LICENSE +21 -0
  182. package/node_modules/safer-buffer/Porting-Buffer.md +268 -0
  183. package/node_modules/safer-buffer/Readme.md +156 -0
  184. package/node_modules/safer-buffer/dangerous.js +58 -0
  185. package/node_modules/safer-buffer/package.json +34 -0
  186. package/node_modules/safer-buffer/safer.js +77 -0
  187. package/node_modules/safer-buffer/tests.js +406 -0
  188. package/node_modules/setprototypeof/LICENSE +13 -0
  189. package/node_modules/setprototypeof/README.md +31 -0
  190. package/node_modules/setprototypeof/index.d.ts +2 -0
  191. package/node_modules/setprototypeof/index.js +17 -0
  192. package/node_modules/setprototypeof/package.json +38 -0
  193. package/node_modules/setprototypeof/test/index.js +24 -0
  194. package/node_modules/statuses/HISTORY.md +82 -0
  195. package/node_modules/statuses/LICENSE +23 -0
  196. package/node_modules/statuses/README.md +136 -0
  197. package/node_modules/statuses/codes.json +65 -0
  198. package/node_modules/statuses/index.js +146 -0
  199. package/node_modules/statuses/package.json +49 -0
  200. package/node_modules/toidentifier/HISTORY.md +9 -0
  201. package/node_modules/toidentifier/LICENSE +21 -0
  202. package/node_modules/toidentifier/README.md +61 -0
  203. package/node_modules/toidentifier/index.js +32 -0
  204. package/node_modules/toidentifier/package.json +38 -0
  205. package/node_modules/unpipe/HISTORY.md +4 -0
  206. package/node_modules/unpipe/LICENSE +22 -0
  207. package/node_modules/unpipe/README.md +43 -0
  208. package/node_modules/unpipe/index.js +69 -0
  209. package/node_modules/unpipe/package.json +27 -0
  210. package/package.json +15 -14
  211. package/dist/shared/subtlecrypto.d.ts +0 -41
  212. package/dist/shared/subtlecrypto.d.ts.map +0 -1
  213. package/dist/shared/subtlecrypto.js +0 -256
  214. package/dist/shared/subtlecrypto.js.map +0 -1
  215. package/dist/umd/shared/subtlecrypto.d.ts +0 -41
  216. package/dist/umd/shared/subtlecrypto.d.ts.map +0 -1
  217. package/dist/umd/verification/verification.d.ts +0 -8
  218. package/dist/umd/verification.js.map +0 -1
  219. /package/dist/umd/{lib → verification/lib}/handlers/api/auth.d.ts +0 -0
  220. /package/dist/umd/{lib → verification/lib}/handlers/api/auth.d.ts.map +0 -0
  221. /package/dist/umd/{lib → verification/lib}/handlers/api/billing.d.ts +0 -0
  222. /package/dist/umd/{lib → verification/lib}/handlers/api/billing.d.ts.map +0 -0
  223. /package/dist/umd/{lib → verification/lib}/handlers/api/callback.d.ts +0 -0
  224. /package/dist/umd/{lib → verification/lib}/handlers/api/callback.d.ts.map +0 -0
  225. /package/dist/umd/{lib → verification/lib}/handlers/api/index.d.ts +0 -0
  226. /package/dist/umd/{lib → verification/lib}/handlers/api/index.d.ts.map +0 -0
  227. /package/dist/umd/{lib → verification/lib}/handlers/api/types.d.ts +0 -0
  228. /package/dist/umd/{lib → verification/lib}/handlers/api/types.d.ts.map +0 -0
  229. /package/dist/umd/{lib → verification/lib}/handlers/api/uninstall.d.ts +0 -0
  230. /package/dist/umd/{lib → verification/lib}/handlers/api/uninstall.d.ts.map +0 -0
  231. /package/dist/umd/{lib → verification/lib}/handlers/api/verifiable/appInstallation.d.ts +0 -0
  232. /package/dist/umd/{lib → verification/lib}/handlers/api/verifiable/appInstallation.d.ts.map +0 -0
  233. /package/dist/umd/{lib → verification/lib}/handlers/api/verifiable/graph.d.ts +0 -0
  234. /package/dist/umd/{lib → verification/lib}/handlers/api/verifiable/graph.d.ts.map +0 -0
  235. /package/dist/umd/{lib → verification/lib}/handlers/api/verifiable/index.d.ts +0 -0
  236. /package/dist/umd/{lib → verification/lib}/handlers/api/verifiable/index.d.ts.map +0 -0
  237. /package/dist/umd/{lib → verification/lib}/handlers/api/verifiable/proxy.d.ts +0 -0
  238. /package/dist/umd/{lib → verification/lib}/handlers/api/verifiable/proxy.d.ts.map +0 -0
  239. /package/dist/umd/{lib → verification/lib}/handlers/api/verifiable/rest.d.ts +0 -0
  240. /package/dist/umd/{lib → verification/lib}/handlers/api/verifiable/rest.d.ts.map +0 -0
  241. /package/dist/umd/{lib → verification/lib}/handlers/api/verifiable/token.d.ts +0 -0
  242. /package/dist/umd/{lib → verification/lib}/handlers/api/verifiable/token.d.ts.map +0 -0
  243. /package/dist/umd/{lib → verification/lib}/handlers/api/webhook.d.ts +0 -0
  244. /package/dist/umd/{lib → verification/lib}/handlers/api/webhook.d.ts.map +0 -0
  245. /package/dist/umd/{lib → verification/lib}/handlers/index.d.ts +0 -0
  246. /package/dist/umd/{lib → verification/lib}/handlers/index.d.ts.map +0 -0
  247. /package/dist/umd/{lib → verification/lib}/handlers/ssr/index.d.ts +0 -0
  248. /package/dist/umd/{lib → verification/lib}/handlers/ssr/index.d.ts.map +0 -0
  249. /package/dist/umd/{lib → verification/lib}/handlers/ssr/protect.d.ts +0 -0
  250. /package/dist/umd/{lib → verification/lib}/handlers/ssr/protect.d.ts.map +0 -0
  251. /package/dist/umd/{lib → verification/lib}/handlers/ssr/types.d.ts +0 -0
  252. /package/dist/umd/{lib → verification/lib}/handlers/ssr/types.d.ts.map +0 -0
  253. /package/dist/umd/{lib → verification/lib}/handlers/ssr/wrap.d.ts +0 -0
  254. /package/dist/umd/{lib → verification/lib}/handlers/ssr/wrap.d.ts.map +0 -0
  255. /package/dist/umd/{lib → verification/lib}/handlers/types.d.ts +0 -0
  256. /package/dist/umd/{lib → verification/lib}/handlers/types.d.ts.map +0 -0
  257. /package/dist/umd/{lib → verification/lib}/index.d.ts +0 -0
  258. /package/dist/umd/{lib → verification/lib}/index.d.ts.map +0 -0
  259. /package/dist/umd/{lib → verification/lib}/types.d.ts +0 -0
  260. /package/dist/umd/{lib → verification/lib}/types.d.ts.map +0 -0
  261. /package/dist/umd/{lib → verification/lib}/utils/billing.d.ts +0 -0
  262. /package/dist/umd/{lib → verification/lib}/utils/billing.d.ts.map +0 -0
  263. /package/dist/umd/{lib → verification/lib}/utils/crypto.d.ts +0 -0
  264. /package/dist/umd/{lib → verification/lib}/utils/crypto.d.ts.map +0 -0
  265. /package/dist/umd/{lib → verification/lib}/utils/firebase.d.ts +0 -0
  266. /package/dist/umd/{lib → verification/lib}/utils/firebase.d.ts.map +0 -0
  267. /package/dist/umd/{lib → verification/lib}/utils/headers.d.ts +0 -0
  268. /package/dist/umd/{lib → verification/lib}/utils/headers.d.ts.map +0 -0
  269. /package/dist/umd/{lib → verification/lib}/utils/index.d.ts +0 -0
  270. /package/dist/umd/{lib → verification/lib}/utils/index.d.ts.map +0 -0
  271. /package/dist/umd/{lib → verification/lib}/utils/session.d.ts +0 -0
  272. /package/dist/umd/{lib → verification/lib}/utils/session.d.ts.map +0 -0
  273. /package/dist/umd/{lib → verification/lib}/utils/shopify-api-js/oauth.d.ts +0 -0
  274. /package/dist/umd/{lib → verification/lib}/utils/shopify-api-js/oauth.d.ts.map +0 -0
  275. /package/dist/umd/{lib → verification/lib}/utils/shopify.d.ts +0 -0
  276. /package/dist/umd/{lib → verification/lib}/utils/shopify.d.ts.map +0 -0
  277. /package/dist/umd/{lib → verification/lib}/utils/token.d.ts +0 -0
  278. /package/dist/umd/{lib → verification/lib}/utils/token.d.ts.map +0 -0
  279. /package/dist/umd/{lib → verification/lib}/utils/types.d.ts +0 -0
  280. /package/dist/umd/{lib → verification/lib}/utils/types.d.ts.map +0 -0
  281. /package/dist/umd/{plugins → verification/plugins}/index.d.ts +0 -0
  282. /package/dist/umd/{plugins → verification/plugins}/index.d.ts.map +0 -0
  283. /package/dist/umd/{plugins → verification/plugins}/types.d.ts +0 -0
  284. /package/dist/umd/{plugins → verification/plugins}/types.d.ts.map +0 -0
  285. /package/dist/umd/{plugins → verification/plugins}/withShopify.d.ts +0 -0
  286. /package/dist/umd/{plugins → verification/plugins}/withShopify.d.ts.map +0 -0
  287. /package/dist/umd/{react → verification/react}/hooks/index.d.ts +0 -0
  288. /package/dist/umd/{react → verification/react}/hooks/index.d.ts.map +0 -0
  289. /package/dist/umd/{react → verification/react}/hooks/useVerification.d.ts.map +0 -0
  290. /package/dist/umd/{react → verification/react}/index.d.ts +0 -0
  291. /package/dist/umd/{react → verification/react}/index.d.ts.map +0 -0
  292. /package/dist/umd/{react → verification/react}/types.d.ts +0 -0
  293. /package/dist/umd/{react → verification/react}/types.d.ts.map +0 -0
  294. /package/dist/umd/{runtime → verification/runtime}/index.d.ts +0 -0
  295. /package/dist/umd/{runtime → verification/runtime}/index.d.ts.map +0 -0
  296. /package/dist/umd/{runtime → verification/runtime}/types.d.ts +0 -0
  297. /package/dist/umd/{runtime → verification/runtime}/types.d.ts.map +0 -0
  298. /package/dist/umd/{shared → verification/shared}/constants.d.ts +0 -0
  299. /package/dist/umd/{shared → verification/shared}/constants.d.ts.map +0 -0
  300. /package/dist/umd/{shared → verification/shared}/datetime.d.ts +0 -0
  301. /package/dist/umd/{shared → verification/shared}/datetime.d.ts.map +0 -0
  302. /package/dist/umd/{shared → verification/shared}/display.d.ts +0 -0
  303. /package/dist/umd/{shared → verification/shared}/display.d.ts.map +0 -0
  304. /package/dist/umd/{shared → verification/shared}/errors.d.ts +0 -0
  305. /package/dist/umd/{shared → verification/shared}/errors.d.ts.map +0 -0
  306. /package/dist/umd/{shared → verification/shared}/graph.d.ts +0 -0
  307. /package/dist/umd/{shared → verification/shared}/graph.d.ts.map +0 -0
  308. /package/dist/umd/{shared → verification/shared}/match.d.ts +0 -0
  309. /package/dist/umd/{shared → verification/shared}/match.d.ts.map +0 -0
  310. /package/dist/umd/{shared → verification/shared}/request.d.ts +0 -0
  311. /package/dist/umd/{shared → verification/shared}/request.d.ts.map +0 -0
  312. /package/dist/umd/{shared → verification/shared}/shopify.d.ts +0 -0
  313. /package/dist/umd/{shared → verification/shared}/shopify.d.ts.map +0 -0
  314. /package/dist/umd/{shared → verification/shared}/types.d.ts +0 -0
  315. /package/dist/umd/{shared → verification/shared}/types.d.ts.map +0 -0
  316. /package/dist/umd/{shared → verification/shared}/uuid.d.ts +0 -0
  317. /package/dist/umd/{shared → verification/shared}/uuid.d.ts.map +0 -0
  318. /package/dist/umd/verification/{types.d.ts → verification/types.d.ts} +0 -0
  319. /package/dist/umd/verification/{types.d.ts.map → verification/types.d.ts.map} +0 -0
  320. /package/dist/umd/verification/{utils → verification/utils}/errors.d.ts +0 -0
  321. /package/dist/umd/verification/{utils → verification/utils}/errors.d.ts.map +0 -0
  322. /package/dist/umd/verification/{utils → verification/utils}/index.d.ts +0 -0
  323. /package/dist/umd/verification/{utils → verification/utils}/index.d.ts.map +0 -0
  324. /package/dist/umd/verification/{utils → verification/utils}/rawbody.d.ts +0 -0
@@ -1,7 +1,7 @@
1
1
  (function (global, factory) {
2
2
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
3
  typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["@agnostack/next-shopify/umd-verification"] = {}));
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["@agnostack/next-shopify/verification"] = {}));
5
5
  })(this, (function (exports) { 'use strict';
6
6
 
7
7
  const COLLECTION_ROOT = 'oauth-data';
@@ -227,11 +227,41 @@
227
227
  ONLINE: 'online',
228
228
  };
229
229
 
230
- var n,r=((n=function(){return r}).toString=n.toLocaleString=n[Symbol.toPrimitive]=function(){return ""},n.valueOf=function(){return !1},new Proxy(Object.freeze(n),{get:function(n,t){return n.hasOwnProperty(t)?n[t]:r}})),u=function(n){return n===r};
230
+ class VerificationError extends Error {
231
+ constructor(message, _data) {
232
+ super(message);
233
+ const { code = 500, ...data } = _data ?? {};
234
+ this.code = code;
235
+ this.data = data;
236
+ this.name = 'VerificationError';
237
+ Object.setPrototypeOf(this, VerificationError.prototype);
238
+ }
239
+ }
231
240
 
232
- let win = global.window;
233
- let exists = (variable) => !u(variable);
234
- let window = typeof win !== "undefined" ? win : r;
241
+ const getChunkedRawBody = async (readable) => {
242
+ if (readable?.rawBody) {
243
+ return readable.rawBody
244
+ }
245
+
246
+ // TODO: move to req.text() after next 13.5: https://github.com/vercel/next.js/discussions/13405
247
+ try {
248
+ const getRawBody = (await Promise.resolve().then(function () { return index; })).default;
249
+ return getRawBody(readable)
250
+ } catch (error) {
251
+ console.error(`Failed to import 'raw-body', please ensure the dependency is installed`);
252
+ throw error
253
+ }
254
+ };
255
+
256
+ // TODO: explore returning mutated request object adding on req.rawBody??
257
+ const ensureRawBody = async (req) => (
258
+ getChunkedRawBody(req)
259
+ .then((_rawBody) => _rawBody.toString())
260
+ .catch((error) => {
261
+ console.error(`Error getting raw body for '${req?.url}'`, error);
262
+ throw error
263
+ })
264
+ );
235
265
 
236
266
  /* eslint-disable no-use-before-define */
237
267
 
@@ -333,438 +363,429 @@
333
363
  return ![...ensureArray(falsy), '', 'false'].includes(string)
334
364
  };
335
365
 
336
- const getWebCrypto = async () => {
337
- try {
338
- const crypto = (await import('isomorphic-webcrypto')).default;
339
- return crypto
340
- } catch (error) {
341
- console.error('Failed to import isomorphic-webcrypto', error);
342
- throw error
343
- }
344
- };
366
+ const TEMP_HOSTNAME = 'xyz.com';
367
+ const REMOVABLE_KEYS = ['shop', 'host'];
345
368
 
346
- const getTextDecoder = async () => {
347
- if (exists(window) && (typeof window?.TextDecoder === 'function')) {
348
- return window.TextDecoder
349
- }
369
+ const getRequestMethod = (body, _method) => {
370
+ const method = _method ?? (body ? 'POST' : 'GET');
350
371
 
351
- try {
352
- const TextDecoder = (await import('util')).TextDecoder;
353
- return TextDecoder
354
- } catch (error) {
355
- console.error('Failed to import utils.TextDecoder', error);
356
- throw error
357
- }
372
+ return uppercase(method)
358
373
  };
359
374
 
360
- const getTextEncoder = async () => {
361
- if (exists(window) && (typeof window?.TextEncoder === 'function')) {
362
- return window.TextEncoder
363
- }
364
-
365
- try {
366
- const TextEncoder = (await import('util')).TextEncoder;
367
- return TextEncoder
368
- } catch (error) {
369
- console.error('Failed to import utils.TextEncoder', error);
370
- throw error
375
+ const convertToURL = (uri) => {
376
+ if (stringEmpty(uri)) {
377
+ return undefined
371
378
  }
372
- };
373
379
 
374
- const timingSafeEqual = (value1, value2) => {
375
- if (
376
- (value1 == undefined) ||
377
- (value2 == undefined) ||
378
- (value1.length !== value2.length)
379
- ) {
380
- return false
381
- }
380
+ const protocolRegex = /^(https?:\/\/).+/i;
382
381
 
383
- let result = 0;
384
- // eslint-disable-next-line no-plusplus
385
- for (let i = 0; i < value1.length; i++) {
386
- // eslint-disable-next-line no-bitwise
387
- result |= value1[i] ^ value2[i];
382
+ if (!protocolRegex.test(uri)) {
383
+ return new URL(`https://${TEMP_HOSTNAME}${ensureLeadingSlash(uri)}`)
388
384
  }
389
385
 
390
- return (result === 0)
386
+ return new URL(uri)
391
387
  };
392
388
 
393
- const stringToHex = (stringValue) => (
394
- Array.from(ensureString(stringValue), (char) => (
395
- char.charCodeAt(0).toString(16).padStart(2, '0')
396
- )).join('')
397
- );
389
+ const normalizeURIParts = (uri) => {
390
+ const urlObject = convertToURL(uri);
391
+ if (!urlObject) {
392
+ return undefined
393
+ }
398
394
 
399
- const hexToString = (hexValue) => (
400
- ensureArray(
401
- ensureString(hexValue).match(/.{1,2}/g)
402
- )
403
- .map((byte) => String.fromCharCode(parseInt(byte, 16)))
404
- .join('')
405
- );
395
+ REMOVABLE_KEYS.forEach((key) => urlObject.searchParams.delete(key));
406
396
 
407
- const arrayBufferToString = async (arrayBuffer) => {
408
- const uint8Array = new Uint8Array(arrayBuffer);
409
- const Decoder = await getTextDecoder();
410
- return new Decoder().decode(uint8Array)
397
+ return {
398
+ ...(urlObject.hostname !== TEMP_HOSTNAME) && {
399
+ origin: urlObject.origin,
400
+ },
401
+ pathname: urlObject.pathname,
402
+ search: urlObject.search,
403
+ }
411
404
  };
412
405
 
413
- const arrayToArrayBuffer = (array) => (
414
- (ArrayBuffer.from != undefined)
415
- ? ArrayBuffer.from(array)
416
- : new Uint8Array(array).buffer
417
- );
406
+ var n,r=((n=function(){return r}).toString=n.toLocaleString=n[Symbol.toPrimitive]=function(){return ""},n.valueOf=function(){return !1},new Proxy(Object.freeze(n),{get:function(n,t){return n.hasOwnProperty(t)?n[t]:r}})),u=function(n){return n===r};
418
407
 
419
- const ensureArrayBuffer = (arrayOrArrayBuffer) => (
420
- (arrayOrArrayBuffer instanceof ArrayBuffer)
421
- ? arrayOrArrayBuffer
422
- : arrayToArrayBuffer(arrayOrArrayBuffer)
423
- );
408
+ let win = global.window;
409
+ let exists = (variable) => !u(variable);
410
+ let window = typeof win !== "undefined" ? win : r;
424
411
 
425
- const generateKeyPair = async () => {
426
- const crypto = await getWebCrypto();
427
- const keyPair = await crypto.subtle.generateKey(
428
- {
429
- name: 'ECDH',
430
- namedCurve: 'P-256',
431
- },
432
- true,
433
- ['deriveKey']
434
- );
412
+ class WebCrypto {
413
+ constructor({ crypto: _crypto, util: _util } = {}) {
414
+ this._crypto = _crypto ?? {};
415
+ this._util = _util ?? {};
416
+ }
435
417
 
436
- return keyPair
437
- };
418
+ get subtle() {
419
+ return this._crypto?.subtle
420
+ }
438
421
 
439
- const getKeyOperations = (keyType) => {
440
- switch (keyType) {
441
- case 'private':
442
- case 'privateKey': {
443
- return ['deriveKey']
422
+ async getWebCrypto() {
423
+ if (!this._crypto?.subtle) {
424
+ try {
425
+ this._crypto = (await import('isomorphic-webcrypto')).default;
426
+ } catch (_ignore) {
427
+ console.info('Failed to import isomorphic-webcrypto, retrying w/ node crypto');
428
+ try {
429
+ this._crypto = (await import('crypto')).default;
430
+ } catch (error) {
431
+ // eslint-disable-next-line max-len
432
+ console.error(`Failed to import node crypto, ensure 'isomorphic-webcrypto' (or node 'crypto') is installed and/or pass in implementation via 'new WebCrypto({ crypto })'`);
433
+ throw error
434
+ }
435
+ }
444
436
  }
445
437
 
446
- case 'secret':
447
- case 'secretKey':
448
- case 'sharedSecret': {
449
- return ['encrypt', 'decrypt']
438
+ if (!this._crypto?.subtle) {
439
+ throw new Error('Invalid crypto, missing subtle')
450
440
  }
451
441
 
452
- default: {
453
- return []
454
- }
442
+ return this._crypto
455
443
  }
456
- };
457
444
 
458
- const getKeyAlgorythm = (keyType) => {
459
- switch (keyType) {
460
- case 'derivedKey':
461
- case 'derived':
462
- case 'secret':
463
- case 'secretKey':
464
- case 'sharedSecret': {
465
- return {
466
- name: 'AES-GCM',
467
- }
445
+ async getTextDecoder() {
446
+ if (this._util?.TextDecoder) {
447
+ return this._util.TextDecoder
468
448
  }
469
449
 
470
- default: {
471
- return {
472
- name: 'ECDH',
473
- namedCurve: 'P-256',
474
- }
450
+ if (exists(window) && typeof window?.TextDecoder === 'function') {
451
+ return window.TextDecoder
475
452
  }
476
- }
477
- };
478
453
 
479
- const generateHMAC = async (message, derivedKey) => {
480
- if (!message || !derivedKey) {
481
- return undefined
482
- }
454
+ try {
455
+ const TextDecoder = (await import('util')).TextDecoder;
456
+ this._util.TextDecoder = TextDecoder;
483
457
 
484
- const crypto = await getWebCrypto();
485
- const Encoder = await getTextEncoder();
458
+ return TextDecoder
459
+ } catch (error) {
460
+ console.error(`Failed to import 'utils.TextDecoder', ensure 'util' is available and/or pass in implementation via 'new WebCrypto({ util })'`);
461
+ throw error
462
+ }
463
+ }
486
464
 
487
- const signature = await crypto.subtle.sign(
488
- 'HMAC',
489
- derivedKey,
490
- new Encoder().encode(message)
491
- );
465
+ async getTextEncoder() {
466
+ if (this._util?.TextEncoder) {
467
+ return this._util.TextEncoder
468
+ }
492
469
 
493
- return stringToHex(
494
- arrayBufferToString(signature)
495
- )
496
- };
470
+ if (exists(window) && typeof window?.TextEncoder === 'function') {
471
+ return window.TextEncoder
472
+ }
497
473
 
498
- const verifyHMAC = async (message, derivedKey, verifiableHMAC) => {
499
- const calculatedHMAC = await generateHMAC(message, derivedKey);
474
+ try {
475
+ const TextEncoder = (await import('util')).TextEncoder;
476
+ this._util.TextEncoder = TextEncoder;
500
477
 
501
- return timingSafeEqual(calculatedHMAC, verifiableHMAC)
502
- };
478
+ return TextEncoder
479
+ } catch (error) {
480
+ console.error(`Failed to import 'utils.TextEncoder', ensure 'util' is available and/or pass in implementation via 'new WebCrypto({ util })'`);
481
+ throw error
482
+ }
483
+ }
503
484
 
504
- const getStorableKey = async (key) => {
505
- const crypto = await getWebCrypto();
485
+ timingSafeEqual(value1, value2) {
486
+ if (
487
+ (value1 == undefined) ||
488
+ (value2 == undefined) ||
489
+ (value1.length !== value2.length)
490
+ ) {
491
+ return false
492
+ }
506
493
 
507
- const exportedJWK = await crypto.subtle.exportKey('jwk', key);
508
- return stringToHex(JSON.stringify(exportedJWK))
509
- };
494
+ let result = 0;
495
+ // eslint-disable-next-line no-plusplus
496
+ for (let i = 0; i < value1.length; i++) {
497
+ // eslint-disable-next-line no-bitwise
498
+ result |= value1[i] ^ value2[i];
499
+ }
510
500
 
511
- const restoreStorableKey = async (keyType, storableHex) => {
512
- if (!storableHex) {
513
- return undefined
501
+ return (result === 0)
514
502
  }
515
- const crypto = await getWebCrypto();
516
503
 
517
- const exportedJWK = JSON.parse(hexToString(storableHex) || '{}');
518
- if (objectEmpty(exportedJWK)) {
519
- return undefined
504
+ stringToHex(stringValue) {
505
+ return (
506
+ Array.from(ensureString(stringValue), (char) => (
507
+ char.charCodeAt(0).toString(16).padStart(2, '0')
508
+ )).join('')
509
+ )
520
510
  }
521
511
 
522
- return crypto.subtle.importKey(
523
- 'jwk',
524
- exportedJWK,
525
- getKeyAlgorythm(keyType),
526
- true,
527
- getKeyOperations(keyType)
528
- )
529
- };
512
+ hexToString(hexValue) {
513
+ return (
514
+ ensureArray(
515
+ ensureString(hexValue).match(/.{1,2}/g)
516
+ )
517
+ .map((byte) => String.fromCharCode(parseInt(byte, 16)))
518
+ .join('')
519
+ )
520
+ }
530
521
 
531
- const getStorableKeyPair = async (keyPair) => {
532
- const storableKeys = {};
522
+ async arrayBufferToString(arrayBuffer) {
523
+ const uint8Array = new Uint8Array(arrayBuffer);
524
+ const Decoder = await this.getTextDecoder();
525
+ return new Decoder().decode(uint8Array)
526
+ }
533
527
 
534
- // eslint-disable-next-line no-restricted-syntax
535
- for (const [keyType, key] of Object.entries(keyPair)) {
536
- // eslint-disable-next-line no-await-in-loop
537
- storableKeys[keyType] = await getStorableKey(key);
528
+ arrayToArrayBuffer(array) {
529
+ return (
530
+ (ArrayBuffer.from != undefined)
531
+ ? ArrayBuffer.from(array)
532
+ : new Uint8Array(array).buffer
533
+ )
538
534
  }
539
535
 
540
- return storableKeys
541
- };
536
+ ensureArrayBuffer(arrayOrArrayBuffer) {
537
+ return (
538
+ (arrayOrArrayBuffer instanceof ArrayBuffer)
539
+ ? arrayOrArrayBuffer
540
+ : this.arrayToArrayBuffer(arrayOrArrayBuffer)
541
+ )
542
+ }
542
543
 
543
- const restoreStorableKeyPair = async (keyPair) => {
544
- const restoredKeys = {};
544
+ async generateKeyPair() {
545
+ const crypto = await this.getWebCrypto();
546
+ const keyPair = await crypto.subtle.generateKey(
547
+ {
548
+ name: 'ECDH',
549
+ namedCurve: 'P-256',
550
+ },
551
+ true,
552
+ ['deriveKey']
553
+ );
545
554
 
546
- // eslint-disable-next-line no-restricted-syntax
547
- for (const [keyType, key] of Object.entries(keyPair)) {
548
- // eslint-disable-next-line no-await-in-loop
549
- restoredKeys[keyType] = await restoreStorableKey(keyType, key);
555
+ return keyPair
550
556
  }
551
557
 
552
- return restoredKeys
553
- };
558
+ getKeyOperations(keyType) {
559
+ switch (keyType) {
560
+ case 'private':
561
+ case 'privateKey': {
562
+ return ['deriveKey']
563
+ }
554
564
 
555
- const deriveSharedKey = async ({ publicKey, privateKey }) => {
556
- if (!publicKey || !privateKey) {
557
- return undefined
565
+ case 'secret':
566
+ case 'secretKey':
567
+ case 'sharedSecret': {
568
+ return ['encrypt', 'decrypt']
569
+ }
570
+
571
+ default: {
572
+ return []
573
+ }
574
+ }
558
575
  }
559
576
 
560
- const crypto = await getWebCrypto();
561
- const derivedKey = await crypto.subtle.deriveKey(
562
- {
563
- name: 'ECDH',
564
- public: publicKey,
565
- },
566
- privateKey,
567
- {
568
- name: 'AES-GCM',
569
- length: 256,
570
- },
571
- true,
572
- ['encrypt', 'decrypt']
573
- );
574
- return derivedKey
575
- };
577
+ getKeyAlgorythm(keyType) {
578
+ switch (keyType) {
579
+ case 'derivedKey':
580
+ case 'derived':
581
+ case 'secret':
582
+ case 'secretKey':
583
+ case 'sharedSecret': {
584
+ return {
585
+ name: 'AES-GCM',
586
+ }
587
+ }
576
588
 
577
- const deriveHMACKey = async ({ publicKey, privateKey }) => {
578
- if (!publicKey || !privateKey) {
579
- return undefined
589
+ default: {
590
+ return {
591
+ name: 'ECDH',
592
+ namedCurve: 'P-256',
593
+ }
594
+ }
595
+ }
580
596
  }
581
597
 
582
- const crypto = await getWebCrypto();
583
- const derivedKey = await crypto.subtle.deriveKey(
584
- {
585
- name: 'ECDH',
586
- public: publicKey,
587
- },
588
- privateKey,
589
- {
590
- name: 'HMAC',
591
- hash: { name: 'SHA-256' },
592
- length: 256, // Adjusted key length, e.g., 128 bits
593
- },
594
- true,
595
- ['sign', 'verify']
596
- );
597
- return derivedKey
598
- };
598
+ async generateHMAC(message, derivedKey) {
599
+ if (!message || !derivedKey) {
600
+ return undefined
601
+ }
599
602
 
600
- const getVerificationKeys = async ({ publicKey, privateKey }) => {
601
- if (!publicKey || !privateKey) {
602
- return {}
603
- }
603
+ const crypto = await this.getWebCrypto();
604
+ const Encoder = await this.getTextEncoder();
604
605
 
605
- const sharedKeyPair = await restoreStorableKeyPair({ publicKey, privateKey });
606
- const derivedHMACKey = await deriveHMACKey(sharedKeyPair);
607
- const derivedSecretKey = await deriveSharedKey(sharedKeyPair);
606
+ const signature = await crypto.subtle.sign(
607
+ 'HMAC',
608
+ derivedKey,
609
+ new Encoder().encode(message)
610
+ );
608
611
 
609
- return {
610
- derivedSecretKey,
611
- derivedHMACKey,
612
+ return this.stringToHex(
613
+ this.arrayBufferToString(signature)
614
+ )
612
615
  }
613
- };
614
616
 
615
- const encryptMessage = async (decryptedMessage, derivedKey) => {
616
- if (!decryptedMessage || !derivedKey) {
617
- return undefined
617
+ async verifyHMAC(message, derivedKey, verifiableHMAC) {
618
+ const calculatedHMAC = await this.generateHMAC(message, derivedKey);
619
+
620
+ return this.timingSafeEqual(calculatedHMAC, verifiableHMAC)
618
621
  }
619
622
 
620
- const crypto = await getWebCrypto();
621
- const iv = crypto.getRandomValues(new Uint8Array(12));
622
- const Encoder = await getTextEncoder();
623
- const encodedMessage = new Encoder().encode(decryptedMessage);
624
- const ciphertext = await crypto.subtle.encrypt(
625
- {
626
- name: 'AES-GCM',
627
- iv,
628
- },
629
- derivedKey,
630
- encodedMessage
631
- );
632
-
633
- const encryptedMessage = new Uint8Array([
634
- ...iv,
635
- ...new Uint8Array(ciphertext)
636
- ]);
637
- return Array.from(encryptedMessage)
638
- };
623
+ async getStorableKey(key) {
624
+ const crypto = await this.getWebCrypto();
639
625
 
640
- const decryptMessage = async (encryptedMessage, derivedKey) => {
641
- if (!encryptedMessage || !derivedKey) {
642
- return undefined
626
+ const exportedJWK = await crypto.subtle.exportKey('jwk', key);
627
+ return this.stringToHex(JSON.stringify(exportedJWK))
643
628
  }
644
629
 
645
- const crypto = await getWebCrypto();
646
- // NOTE: this presumed an array or arrayBuffer coming in as encryptedMessage (will fail w/ IV error if its a string)
647
- const encryptedArrayBuffer = ensureArrayBuffer(encryptedMessage);
648
- const iv = encryptedArrayBuffer.slice(0, 12);
649
- const ciphertext = encryptedArrayBuffer.slice(12);
630
+ async restoreStorableKey(keyType, storableHex) {
631
+ if (!storableHex) {
632
+ return undefined
633
+ }
634
+ const crypto = await this.getWebCrypto();
650
635
 
651
- const decryptedArrayBuffer = await crypto.subtle.decrypt(
652
- {
653
- name: 'AES-GCM',
654
- iv,
655
- },
656
- derivedKey,
657
- ciphertext
658
- );
636
+ const exportedJWK = JSON.parse(this.hexToString(storableHex) || '{}');
637
+ if (objectEmpty(exportedJWK)) {
638
+ return undefined
639
+ }
659
640
 
660
- const Decoder = await getTextDecoder();
661
- const decryptedMessage = new Decoder().decode(decryptedArrayBuffer);
662
- return decryptedMessage
663
- };
641
+ return crypto.subtle.importKey(
642
+ 'jwk',
643
+ exportedJWK,
644
+ this.getKeyAlgorythm(keyType),
645
+ true,
646
+ this.getKeyOperations(keyType)
647
+ )
648
+ }
664
649
 
665
- const subtle = {
666
- generateKeyPair,
667
- generateHMAC,
668
- verifyHMAC,
669
- getStorableKey,
670
- getStorableKeyPair,
671
- restoreStorableKeyPair,
672
- deriveSharedKey,
673
- deriveHMACKey,
674
- getVerificationKeys,
675
- encryptMessage,
676
- decryptMessage,
677
- };
650
+ async getStorableKeyPair(keyPair) {
651
+ const storableKeys = {};
678
652
 
679
- const TEMP_HOSTNAME = 'xyz.com';
680
- const REMOVABLE_KEYS = ['shop', 'host'];
653
+ // eslint-disable-next-line no-restricted-syntax
654
+ for (const [keyType, key] of Object.entries(keyPair)) {
655
+ // eslint-disable-next-line no-await-in-loop
656
+ storableKeys[keyType] = await this.getStorableKey(key);
657
+ }
681
658
 
682
- const getRequestMethod = (body, _method) => {
683
- const method = _method ?? (body ? 'POST' : 'GET');
659
+ return storableKeys
660
+ }
684
661
 
685
- return uppercase(method)
686
- };
662
+ async restoreStorableKeyPair(keyPair) {
663
+ const restoredKeys = {};
687
664
 
688
- const convertToURL = (uri) => {
689
- if (stringEmpty(uri)) {
690
- return undefined
665
+ // eslint-disable-next-line no-restricted-syntax
666
+ for (const [keyType, key] of Object.entries(keyPair)) {
667
+ // eslint-disable-next-line no-await-in-loop
668
+ restoredKeys[keyType] = await this.restoreStorableKey(keyType, key);
669
+ }
670
+
671
+ return restoredKeys
691
672
  }
692
673
 
693
- const protocolRegex = /^(https?:\/\/).+/i;
674
+ async deriveSharedKey({ publicKey, privateKey }) {
675
+ if (!publicKey || !privateKey) {
676
+ return undefined
677
+ }
694
678
 
695
- if (!protocolRegex.test(uri)) {
696
- return new URL(`https://${TEMP_HOSTNAME}${ensureLeadingSlash(uri)}`)
679
+ const crypto = await this.getWebCrypto();
680
+ const derivedKey = await crypto.subtle.deriveKey(
681
+ {
682
+ name: 'ECDH',
683
+ public: publicKey,
684
+ },
685
+ privateKey,
686
+ {
687
+ name: 'AES-GCM',
688
+ length: 256,
689
+ },
690
+ true,
691
+ ['encrypt', 'decrypt']
692
+ );
693
+ return derivedKey
697
694
  }
698
695
 
699
- return new URL(uri)
700
- };
696
+ async deriveHMACKey({ publicKey, privateKey }) {
697
+ if (!publicKey || !privateKey) {
698
+ return undefined
699
+ }
701
700
 
702
- const normalizeURIParts = (uri) => {
703
- const urlObject = convertToURL(uri);
704
- if (!urlObject) {
705
- return undefined
701
+ const crypto = await this.getWebCrypto();
702
+ const derivedKey = await crypto.subtle.deriveKey(
703
+ {
704
+ name: 'ECDH',
705
+ public: publicKey,
706
+ },
707
+ privateKey,
708
+ {
709
+ name: 'HMAC',
710
+ hash: { name: 'SHA-256' },
711
+ length: 256, // Adjusted key length, e.g., 128 bits
712
+ },
713
+ true,
714
+ ['sign', 'verify']
715
+ );
716
+ return derivedKey
706
717
  }
707
718
 
708
- REMOVABLE_KEYS.forEach((key) => urlObject.searchParams.delete(key));
719
+ async getVerificationKeys({ publicKey, privateKey }) {
720
+ if (!publicKey || !privateKey) {
721
+ return {}
722
+ }
709
723
 
710
- return {
711
- ...(urlObject.hostname !== TEMP_HOSTNAME) && {
712
- origin: urlObject.origin,
713
- },
714
- pathname: urlObject.pathname,
715
- search: urlObject.search,
716
- }
717
- };
724
+ const sharedKeyPair = await this.restoreStorableKeyPair({ publicKey, privateKey });
725
+ const derivedHMACKey = await this.deriveHMACKey(sharedKeyPair);
726
+ const derivedSecretKey = await this.deriveSharedKey(sharedKeyPair);
718
727
 
719
- class VerificationError extends Error {
720
- constructor(message, _data) {
721
- super(message);
722
- const { code = 500, ...data } = _data ?? {};
723
- this.code = code;
724
- this.data = data;
725
- this.name = 'VerificationError';
726
- Object.setPrototypeOf(this, VerificationError.prototype);
728
+ return {
729
+ derivedSecretKey,
730
+ derivedHMACKey,
731
+ }
727
732
  }
728
- }
729
-
730
- // import getRawBody from 'raw-body'
731
- // import * as getRawBody from 'raw-body'
732
733
 
733
- const getChunkedRawBody = async (readable) => {
734
- const chunks = [];
734
+ async encryptMessage(decryptedMessage, derivedKey) {
735
+ if (!decryptedMessage || !derivedKey) {
736
+ return undefined
737
+ }
735
738
 
736
- for await (const chunk of readable) {
737
- chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
739
+ const crypto = await this.getWebCrypto();
740
+ const iv = crypto.getRandomValues(new Uint8Array(12));
741
+ const Encoder = await this.getTextEncoder();
742
+ const encodedMessage = new Encoder().encode(decryptedMessage);
743
+ const ciphertext = await crypto.subtle.encrypt(
744
+ {
745
+ name: 'AES-GCM',
746
+ iv,
747
+ },
748
+ derivedKey,
749
+ encodedMessage
750
+ );
751
+
752
+ const encryptedMessage = new Uint8Array([
753
+ ...iv,
754
+ ...new Uint8Array(ciphertext)
755
+ ]);
756
+ return Array.from(encryptedMessage)
738
757
  }
739
758
 
740
- return Buffer.concat(chunks)
741
- };
759
+ async decryptMessage(encryptedMessage, derivedKey) {
760
+ if (!encryptedMessage || !derivedKey) {
761
+ return undefined
762
+ }
742
763
 
743
- // TODO: explore returning mutated request object adding on req.rawBody??
744
- // TODO: move to req.text() after next 13.5: https://github.com/vercel/next.js/discussions/13405
745
- const ensureRawBody = async (req) => (
746
- getChunkedRawBody(req)
747
- // getRawBody(req)
748
- .then((_rawBody) => _rawBody.toString())
749
- .catch((error) => {
750
- // NOTE: this is intended to ensure if getRawBody is called 2x w/in a route that it returns the already streamed body
751
- if ((error?.type === 'stream.not.readable')) {
752
- if (!req?.rawBody) {
753
- // eslint-disable-next-line max-len
754
- console.info('Ignoring error reading raw requst body. This can occur if the body is attempted to be read twice in a single request. If intentional, please mutate the request to add the result of this call as req.rawBody (to prevent this warning).');
755
- return req?.body
756
- }
764
+ const crypto = await this.getWebCrypto();
765
+ // NOTE: this presumed an array or arrayBuffer coming in as encryptedMessage (will fail w/ IV error if its a string)
766
+ const encryptedArrayBuffer = this.ensureArrayBuffer(encryptedMessage);
767
+ const iv = encryptedArrayBuffer.slice(0, 12);
768
+ const ciphertext = encryptedArrayBuffer.slice(12);
757
769
 
758
- return req?.rawBody
759
- }
770
+ const decryptedArrayBuffer = await crypto.subtle.decrypt(
771
+ {
772
+ name: 'AES-GCM',
773
+ iv,
774
+ },
775
+ derivedKey,
776
+ ciphertext
777
+ );
778
+
779
+ const Decoder = await this.getTextDecoder();
780
+ const decryptedMessage = new Decoder().decode(decryptedArrayBuffer);
781
+ return decryptedMessage
782
+ }
783
+ }
760
784
 
761
- console.error(`Error getting raw body for '${req?.url}'`, error);
762
- throw error
763
- })
764
- );
785
+ const getVerificationHelpers = ({ keyPairs, crypto: _crypto, util: _util } = {}) => {
786
+ const webCrypto = new WebCrypto({ crypto: _crypto, util: _util });
765
787
 
766
- const getVerificationHelpers = ({ keyPairs }) => (
767
- async (req, params) => {
788
+ return async (req, params) => {
768
789
  const {
769
790
  'x-public-key': apiKey,
770
791
  'x-ephemeral-key': ephemeralPublicKey,
@@ -808,7 +829,7 @@
808
829
  throw new VerificationError('Invalid or missing authorization', { code: 401 })
809
830
  }
810
831
 
811
- verificationKeys = await subtle.getVerificationKeys({
832
+ verificationKeys = await webCrypto.getVerificationKeys({
812
833
  publicKey: ephemeralPublicKey,
813
834
  privateKey: keyPairs.shared.privateKey,
814
835
  });
@@ -824,7 +845,7 @@
824
845
  ...normalizeURIParts(uri),
825
846
  });
826
847
 
827
- const isValid = await subtle.verifyHMAC(
848
+ const isValid = await webCrypto.verifyHMAC(
828
849
  verificationPayload,
829
850
  verificationKeys.derivedHMACKey,
830
851
  authSignature
@@ -835,7 +856,7 @@
835
856
  }
836
857
 
837
858
  if (!disableRecryption && requestBody) {
838
- const decryptedMessage = await subtle.decryptMessage(requestBody, verificationKeys.derivedSecretKey);
859
+ const decryptedMessage = await webCrypto.decryptMessage(requestBody, verificationKeys.derivedSecretKey);
839
860
  requestBody = safeParse(decryptedMessage);
840
861
  }
841
862
  }
@@ -845,7 +866,7 @@
845
866
  return response
846
867
  }
847
868
 
848
- return subtle.encryptMessage(JSON.stringify(response), verificationKeys.derivedSecretKey)
869
+ return webCrypto.encryptMessage(JSON.stringify(response), verificationKeys.derivedSecretKey)
849
870
  };
850
871
 
851
872
  return { rawBody, requestBody, processResponse }
@@ -854,7 +875,346 @@
854
875
  throw error
855
876
  }
856
877
  }
857
- );
878
+ };
879
+
880
+ /*!
881
+ * raw-body
882
+ * Copyright(c) 2013-2014 Jonathan Ong
883
+ * Copyright(c) 2014-2022 Douglas Christopher Wilson
884
+ * MIT Licensed
885
+ */
886
+
887
+ /**
888
+ * Module dependencies.
889
+ * @private
890
+ */
891
+
892
+ var asyncHooks = tryRequireAsyncHooks();
893
+ var bytes = require('bytes');
894
+ var createError = require('http-errors');
895
+ var iconv = require('iconv-lite');
896
+ var unpipe = require('unpipe');
897
+
898
+ /**
899
+ * Module exports.
900
+ * @public
901
+ */
902
+
903
+ module.exports = getRawBody;
904
+
905
+ /**
906
+ * Module variables.
907
+ * @private
908
+ */
909
+
910
+ var ICONV_ENCODING_MESSAGE_REGEXP = /^Encoding not recognized: /;
911
+
912
+ /**
913
+ * Get the decoder for a given encoding.
914
+ *
915
+ * @param {string} encoding
916
+ * @private
917
+ */
918
+
919
+ function getDecoder (encoding) {
920
+ if (!encoding) return null
921
+
922
+ try {
923
+ return iconv.getDecoder(encoding)
924
+ } catch (e) {
925
+ // error getting decoder
926
+ if (!ICONV_ENCODING_MESSAGE_REGEXP.test(e.message)) throw e
927
+
928
+ // the encoding was not found
929
+ throw createError(415, 'specified encoding unsupported', {
930
+ encoding: encoding,
931
+ type: 'encoding.unsupported'
932
+ })
933
+ }
934
+ }
935
+
936
+ /**
937
+ * Get the raw body of a stream (typically HTTP).
938
+ *
939
+ * @param {object} stream
940
+ * @param {object|string|function} [options]
941
+ * @param {function} [callback]
942
+ * @public
943
+ */
944
+
945
+ function getRawBody (stream, options, callback) {
946
+ var done = callback;
947
+ var opts = options || {};
948
+
949
+ // light validation
950
+ if (stream === undefined) {
951
+ throw new TypeError('argument stream is required')
952
+ } else if (typeof stream !== 'object' || stream === null || typeof stream.on !== 'function') {
953
+ throw new TypeError('argument stream must be a stream')
954
+ }
955
+
956
+ if (options === true || typeof options === 'string') {
957
+ // short cut for encoding
958
+ opts = {
959
+ encoding: options
960
+ };
961
+ }
962
+
963
+ if (typeof options === 'function') {
964
+ done = options;
965
+ opts = {};
966
+ }
967
+
968
+ // validate callback is a function, if provided
969
+ if (done !== undefined && typeof done !== 'function') {
970
+ throw new TypeError('argument callback must be a function')
971
+ }
972
+
973
+ // require the callback without promises
974
+ if (!done && !global.Promise) {
975
+ throw new TypeError('argument callback is required')
976
+ }
977
+
978
+ // get encoding
979
+ var encoding = opts.encoding !== true
980
+ ? opts.encoding
981
+ : 'utf-8';
982
+
983
+ // convert the limit to an integer
984
+ var limit = bytes.parse(opts.limit);
985
+
986
+ // convert the expected length to an integer
987
+ var length = opts.length != null && !isNaN(opts.length)
988
+ ? parseInt(opts.length, 10)
989
+ : null;
990
+
991
+ if (done) {
992
+ // classic callback style
993
+ return readStream(stream, encoding, length, limit, wrap(done))
994
+ }
995
+
996
+ return new Promise(function executor (resolve, reject) {
997
+ readStream(stream, encoding, length, limit, function onRead (err, buf) {
998
+ if (err) return reject(err)
999
+ resolve(buf);
1000
+ });
1001
+ })
1002
+ }
1003
+
1004
+ /**
1005
+ * Halt a stream.
1006
+ *
1007
+ * @param {Object} stream
1008
+ * @private
1009
+ */
1010
+
1011
+ function halt (stream) {
1012
+ // unpipe everything from the stream
1013
+ unpipe(stream);
1014
+
1015
+ // pause stream
1016
+ if (typeof stream.pause === 'function') {
1017
+ stream.pause();
1018
+ }
1019
+ }
1020
+
1021
+ /**
1022
+ * Read the data from the stream.
1023
+ *
1024
+ * @param {object} stream
1025
+ * @param {string} encoding
1026
+ * @param {number} length
1027
+ * @param {number} limit
1028
+ * @param {function} callback
1029
+ * @public
1030
+ */
1031
+
1032
+ function readStream (stream, encoding, length, limit, callback) {
1033
+ var complete = false;
1034
+ var sync = true;
1035
+
1036
+ // check the length and limit options.
1037
+ // note: we intentionally leave the stream paused,
1038
+ // so users should handle the stream themselves.
1039
+ if (limit !== null && length !== null && length > limit) {
1040
+ return done(createError(413, 'request entity too large', {
1041
+ expected: length,
1042
+ length: length,
1043
+ limit: limit,
1044
+ type: 'entity.too.large'
1045
+ }))
1046
+ }
1047
+
1048
+ // streams1: assert request encoding is buffer.
1049
+ // streams2+: assert the stream encoding is buffer.
1050
+ // stream._decoder: streams1
1051
+ // state.encoding: streams2
1052
+ // state.decoder: streams2, specifically < 0.10.6
1053
+ var state = stream._readableState;
1054
+ if (stream._decoder || (state && (state.encoding || state.decoder))) {
1055
+ // developer error
1056
+ return done(createError(500, 'stream encoding should not be set', {
1057
+ type: 'stream.encoding.set'
1058
+ }))
1059
+ }
1060
+
1061
+ if (typeof stream.readable !== 'undefined' && !stream.readable) {
1062
+ return done(createError(500, 'stream is not readable', {
1063
+ type: 'stream.not.readable'
1064
+ }))
1065
+ }
1066
+
1067
+ var received = 0;
1068
+ var decoder;
1069
+
1070
+ try {
1071
+ decoder = getDecoder(encoding);
1072
+ } catch (err) {
1073
+ return done(err)
1074
+ }
1075
+
1076
+ var buffer = decoder
1077
+ ? ''
1078
+ : [];
1079
+
1080
+ // attach listeners
1081
+ stream.on('aborted', onAborted);
1082
+ stream.on('close', cleanup);
1083
+ stream.on('data', onData);
1084
+ stream.on('end', onEnd);
1085
+ stream.on('error', onEnd);
1086
+
1087
+ // mark sync section complete
1088
+ sync = false;
1089
+
1090
+ function done () {
1091
+ var args = new Array(arguments.length);
1092
+
1093
+ // copy arguments
1094
+ for (var i = 0; i < args.length; i++) {
1095
+ args[i] = arguments[i];
1096
+ }
1097
+
1098
+ // mark complete
1099
+ complete = true;
1100
+
1101
+ if (sync) {
1102
+ process.nextTick(invokeCallback);
1103
+ } else {
1104
+ invokeCallback();
1105
+ }
1106
+
1107
+ function invokeCallback () {
1108
+ cleanup();
1109
+
1110
+ if (args[0]) {
1111
+ // halt the stream on error
1112
+ halt(stream);
1113
+ }
1114
+
1115
+ callback.apply(null, args);
1116
+ }
1117
+ }
1118
+
1119
+ function onAborted () {
1120
+ if (complete) return
1121
+
1122
+ done(createError(400, 'request aborted', {
1123
+ code: 'ECONNABORTED',
1124
+ expected: length,
1125
+ length: length,
1126
+ received: received,
1127
+ type: 'request.aborted'
1128
+ }));
1129
+ }
1130
+
1131
+ function onData (chunk) {
1132
+ if (complete) return
1133
+
1134
+ received += chunk.length;
1135
+
1136
+ if (limit !== null && received > limit) {
1137
+ done(createError(413, 'request entity too large', {
1138
+ limit: limit,
1139
+ received: received,
1140
+ type: 'entity.too.large'
1141
+ }));
1142
+ } else if (decoder) {
1143
+ buffer += decoder.write(chunk);
1144
+ } else {
1145
+ buffer.push(chunk);
1146
+ }
1147
+ }
1148
+
1149
+ function onEnd (err) {
1150
+ if (complete) return
1151
+ if (err) return done(err)
1152
+
1153
+ if (length !== null && received !== length) {
1154
+ done(createError(400, 'request size did not match content length', {
1155
+ expected: length,
1156
+ length: length,
1157
+ received: received,
1158
+ type: 'request.size.invalid'
1159
+ }));
1160
+ } else {
1161
+ var string = decoder
1162
+ ? buffer + (decoder.end() || '')
1163
+ : Buffer.concat(buffer);
1164
+ done(null, string);
1165
+ }
1166
+ }
1167
+
1168
+ function cleanup () {
1169
+ buffer = null;
1170
+
1171
+ stream.removeListener('aborted', onAborted);
1172
+ stream.removeListener('data', onData);
1173
+ stream.removeListener('end', onEnd);
1174
+ stream.removeListener('error', onEnd);
1175
+ stream.removeListener('close', cleanup);
1176
+ }
1177
+ }
1178
+
1179
+ /**
1180
+ * Try to require async_hooks
1181
+ * @private
1182
+ */
1183
+
1184
+ function tryRequireAsyncHooks () {
1185
+ try {
1186
+ return require('async_hooks')
1187
+ } catch (e) {
1188
+ return {}
1189
+ }
1190
+ }
1191
+
1192
+ /**
1193
+ * Wrap function with async resource, if possible.
1194
+ * AsyncResource.bind static method backported.
1195
+ * @private
1196
+ */
1197
+
1198
+ function wrap (fn) {
1199
+ var res;
1200
+
1201
+ // create anonymous resource
1202
+ if (asyncHooks.AsyncResource) {
1203
+ res = new asyncHooks.AsyncResource(fn.name || 'bound-anonymous-fn');
1204
+ }
1205
+
1206
+ // incompatible node.js
1207
+ if (!res || !res.runInAsyncScope) {
1208
+ return fn
1209
+ }
1210
+
1211
+ // return bound function
1212
+ return res.runInAsyncScope.bind(res, fn, null)
1213
+ }
1214
+
1215
+ var index = /*#__PURE__*/Object.freeze({
1216
+ __proto__: null
1217
+ });
858
1218
 
859
1219
  exports.API_ROUTE_NAMES = API_ROUTE_NAMES;
860
1220
  exports.API_WEBHOOK_TOPICS = API_WEBHOOK_TOPICS;
@@ -863,10 +1223,12 @@
863
1223
  exports.BILLING_INTERVALS = BILLING_INTERVALS;
864
1224
  exports.COLLECTION_ROOT = COLLECTION_ROOT;
865
1225
  exports.PAGE_ROUTE_NAMES = PAGE_ROUTE_NAMES;
1226
+ exports.VerificationError = VerificationError;
866
1227
  exports.convertEventTopic = convertEventTopic;
1228
+ exports.ensureRawBody = ensureRawBody;
867
1229
  exports.getVerificationHelpers = getVerificationHelpers;
868
1230
 
869
1231
  Object.defineProperty(exports, '__esModule', { value: true });
870
1232
 
871
1233
  }));
872
- //# sourceMappingURL=verification.js.map
1234
+ //# sourceMappingURL=index.js.map