@wix/origami-site 1.0.47 → 1.0.48

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wix/origami-site",
3
- "version": "1.0.47",
3
+ "version": "1.0.48",
4
4
  "description": "Runs an exported Wix origami site: file-based routing, Wix dynamic pages, popups and build checks. Use with @wix/origami-site-astro or @wix/origami-site-express.",
5
5
  "type": "module",
6
6
  "files": [
@@ -46,7 +46,7 @@
46
46
  }
47
47
  },
48
48
  "dependencies": {
49
- "@wix/origami-types": "1.4.2",
49
+ "@wix/origami-types": "1.4.3",
50
50
  "@wix/builder-error-pages": "^1.1.6",
51
51
  "@wix/origami-transforms": "0.1.53",
52
52
  "@babel/parser": "^7.25.0",
@@ -93,5 +93,5 @@
93
93
  "optional": true
94
94
  }
95
95
  },
96
- "falconPackageHash": "1e2452377de58cd3b9b6f5a917197f046975086e82a1133d989665d8"
96
+ "falconPackageHash": "66e94276469f66d5f2bf10d55f9fe98ec085c8e2e9d915735bc44d45"
97
97
  }
@@ -1,4 +1,4 @@
1
- import type { SiteRouteTable, PageRoute, RouterRoute, AccessRule } from '@wix/origami-types'
1
+ import type { SiteRouteTable, PageRoute, RouterRoute, BrokenRouterRoute, AccessRule } from '@wix/origami-types'
2
2
  export type { AccessRule } from '@wix/origami-types'
3
3
  // PLAN-QUESTION: Logger is unnamed structurally in the plan; only warn/error are used.
4
4
  export type Logger = Pick<Console, 'warn' | 'error'>
@@ -36,6 +36,7 @@ export interface OrigamiSite {
36
36
  export type RouteMatch =
37
37
  | { kind: 'page'; route: PageRoute }
38
38
  | { kind: 'router'; route: RouterRoute; suffix: string } // '/a/b/c'; '/' for bare '/team'
39
+ | { kind: 'brokenRouter'; route: BrokenRouterRoute } // every path under a broken router's prefix
39
40
  | { kind: 'reserved'; path: string } // /_origami/*
40
41
  | { kind: 'notFound' }
41
42
 
@@ -74,7 +75,7 @@ export interface AccessHandler {
74
75
  }
75
76
 
76
77
 
77
- export { SiteTreeError, RouterServiceError } from './requests/errors.js'
78
+ export { SiteTreeError, RouterServiceError, brokenRouterError } from './requests/errors.js'
78
79
  /** The default TokenProvider: the visitor's builder-auth cookie when fresh, else a one-off anonymous token. Stores nothing. */
79
80
  export function builderAuthTokenProvider(options: { clientId: string; siteId: string; wixApiBaseUrl?: string; fetch?: typeof fetch; timeoutMs?: number }): TokenProvider
80
81
  export function createSite(options: SiteOptions): Promise<OrigamiSite>
@@ -1,6 +1,6 @@
1
1
  export { readSiteTree } from './site-tree/readSiteTree.js'
2
2
  export { writeBuiltTable, readBuiltTable } from './site-tree/builtTable.js'
3
- export { SiteTreeError, RouterServiceError } from './requests/errors.js'
3
+ export { SiteTreeError, RouterServiceError, brokenRouterError } from './requests/errors.js'
4
4
  export { builderAuthTokenProvider } from './tokens/builderAuthTokenProvider.js'
5
5
  export { createSite, noAccessRules } from './requests/createSite.js'
6
6
  export { runInRequestContext, getRequestContext, getAccessToken } from './requests/context.js'
@@ -5,7 +5,7 @@ import { callRouter } from '../router/index.js'
5
5
  import { pickRolePage } from '../router/pickRolePage.js'
6
6
  import { builderAuthTokenProvider } from '../tokens/builderAuthTokenProvider.js'
7
7
  import { createEmbeds, noEmbeds } from '../embeds/index.js'
8
- import { RouterServiceError, SiteTreeError } from './errors.js'
8
+ import { RouterServiceError, SiteTreeError, brokenRouterError } from './errors.js'
9
9
  import { normalizeRequestPath } from './urlPath.js'
10
10
  import { canBePagePath } from './canBePagePath.js'
11
11
  import { getAccessToken, runInRequestContext } from './context.js'
@@ -71,7 +71,9 @@ export async function createSite(options) {
71
71
  // The bare prefix and every path below it go to the router: /team, /team/john, /team/a/b/c.
72
72
  const [, prefix, ...rest] = path.split('/')
73
73
  const router = current.routers.find(candidate => candidate.prefix === prefix)
74
- return router ? { kind: 'router', route: router, suffix: `/${rest.join('/')}` } : { kind: 'notFound' }
74
+ if (router) return { kind: 'router', route: router, suffix: `/${rest.join('/')}` }
75
+ const brokenRouter = current.brokenRouters?.find(candidate => candidate.prefix === prefix)
76
+ return brokenRouter ? { kind: 'brokenRouter', route: brokenRouter } : { kind: 'notFound' }
75
77
  }
76
78
  /** @param {{ itemPath: string, access: import('@wix/origami-types').AccessRule[] }} route @param {Request} request @param {import('../index.js').RequestContext} context */
77
79
  async function checkAccess(route, request, context) {
@@ -163,7 +165,8 @@ export async function createSite(options) {
163
165
  current = await table()
164
166
  match = matchPath(path, current)
165
167
  }
166
- kind = match.kind === 'router' ? 'router' : match.kind === 'page' ? 'page' : 'not_found'
168
+ kind = match.kind === 'router' || match.kind === 'brokenRouter' ? 'router' : match.kind === 'page' ? 'page' : 'not_found'
169
+ if (match.kind === 'brokenRouter') return errorPage(brokenRouterError(match.route), 500, mode, logger)
167
170
  if (match.kind === 'router') return await resolveRouter(match, url, /** @type {string} */ (path), current, request, context)
168
171
  if (match.kind === 'page') {
169
172
  const denied = await checkAccess(match.route, request, context)
@@ -2,7 +2,7 @@ export class SiteTreeError extends Error {
2
2
  code:
3
3
  | 'DUPLICATE_URL' // two folders give the same URL
4
4
  | 'ROUTE_JSON_INVALID' // not JSON, or a required field missing or of the wrong type
5
- | 'ROUTE_JSON_MISSING_SERVICE_URL' // no routerServiceUrl: origami refuses to start
5
+ | 'ROUTE_JSON_MISSING_SERVICE_URL' // no routerServiceUrl: the router's URLs answer 500
6
6
  | 'ROUTE_JSON_SIBLINGS_DIFFER' // [a]/route.json and [b]/route.json differ
7
7
  | 'ROLE_FOLDER_MISSING' // pages.member = 'team-member' but no [team-member]/ folder
8
8
  | 'ROLE_PAGE_HAS_NO_FILES' // [x]/ has a route.json but no page files
@@ -16,8 +16,11 @@ export class SiteTreeError extends Error {
16
16
  | 'UNSUPPORTED_ACCESS_RULE' // see section 4
17
17
  constructor(code: SiteTreeError['code'], paths: string[], message: string)
18
18
  paths: string[]
19
+ reason: string
19
20
  }
20
21
 
22
+ export function brokenRouterError(router: import('@wix/origami-types').BrokenRouterRoute): SiteTreeError
23
+
21
24
  export class RouterServiceError extends Error {
22
25
  constructor(code: RouterServiceError['code'], routerPrefix: string, message: string, details?: { status?: number; cause?: unknown })
23
26
  code: 'TIMEOUT' | 'NETWORK' | 'HTTP_STATUS' | 'BAD_RESPONSE'
@@ -10,9 +10,15 @@ export class SiteTreeError extends Error {
10
10
  this.code = code
11
11
  this.error_name = code
12
12
  this.paths = [...paths]
13
+ this.reason = message
13
14
  }
14
15
  }
15
16
 
17
+ /** The error a broken router's URLs answer with.
18
+ * @param {import('@wix/origami-types').BrokenRouterRoute} router */
19
+ export const brokenRouterError = ({ error: { code, paths, reason } }) =>
20
+ new SiteTreeError(/** @type {SiteTreeError['code']} */ (code), paths, reason)
21
+
16
22
  export class RouterServiceError extends Error {
17
23
  /**
18
24
  * @param {import('./errors.js').RouterServiceError['code']} code
@@ -129,18 +129,29 @@ export const readSiteTree = async root => {
129
129
  : { items: [], routers: [] }
130
130
  /** @type {import('@wix/origami-types').RouterRoute[]} */
131
131
  const routers = []
132
+ /** @type {import('@wix/origami-types').BrokenRouterRoute[]} */
133
+ const brokenRouters = []
132
134
  const prefixes = new Set(routerPaths.filter(path => isBracket(basename(path))).map(path => dirname(path)))
133
135
  for (const prefix of prefixes) {
134
136
  if (prefix === 'pages/error404')
135
137
  throw new SiteTreeError('NOT_FOUND_HAS_CHILDREN', [`src/${prefix}`], 'The not-found page cannot have children.')
136
- routers.push(readRouter(resolve(src, prefix), prefix, readdirSync(resolve(src, prefix), { withFileTypes: true }), items))
138
+ try {
139
+ routers.push(readRouter(resolve(src, prefix), prefix, readdirSync(resolve(src, prefix), { withFileTypes: true }), items))
140
+ } catch (error) {
141
+ // A bad router folder fails only the router's own URLs. A [page] folder outside a prefix is no router, so it
142
+ // has no URLs of its own to fail.
143
+ if (!(error instanceof SiteTreeError) || error.code === 'BRACKET_OUTSIDE_PREFIX') throw error
144
+ brokenRouters.push({ key: prefix, prefix: basename(prefix), error: { code: error.code, paths: error.paths, reason: error.reason } })
145
+ }
137
146
  }
147
+ const inBrokenRouter = path => brokenRouters.some(router => path === router.key || path.startsWith(`${router.key}/`))
138
148
  for (const path of routerPaths.filter(path => basename(path) === 'route.json')) {
149
+ if (inBrokenRouter(path)) continue
139
150
  if (!routers.some(router => Object.values(router.rolePages).some(page => path === `${page.itemPath}/route.json`)))
140
151
  throw new SiteTreeError('ROUTE_JSON_INVALID', [`src/${path}`], 'route.json belongs in a [page] folder of a router prefix.')
141
152
  }
142
153
  for (const itemPath of items) {
143
- if (routers.some(router => Object.values(router.rolePages).some(page => page.itemPath === itemPath))) continue
154
+ if (inBrokenRouter(itemPath) || routers.some(router => Object.values(router.rolePages).some(page => page.itemPath === itemPath))) continue
144
155
  const path = `src/${itemPath}`
145
156
  const metadata = pageMetadata(resolve(src, itemPath))
146
157
  if (itemPath === 'pages/error404') notFound = { key: itemPath, itemPath, ...metadata }
@@ -156,10 +167,10 @@ export const readSiteTree = async root => {
156
167
  if (notFoundChildren.length)
157
168
  throw new SiteTreeError('NOT_FOUND_HAS_CHILDREN', ['src/pages/error404', ...notFoundChildren.map(page => `src/${page.itemPath}`)],
158
169
  'The not-found page cannot have children.')
159
- for (const router of routers) {
170
+ for (const router of [...routers, ...brokenRouters]) {
160
171
  const page = pages.find(candidate => candidate.url === `/${router.prefix}` || candidate.url.startsWith(`/${router.prefix}/`))
161
172
  if (page) throw new SiteTreeError('PREFIX_COLLIDES_WITH_PAGE', [`src/${router.key}`, `src/${page.itemPath}`],
162
173
  `Every URL under /${router.prefix} goes to the Wix router, so "${page.url}" can't be a page.`)
163
174
  }
164
- return { version: 2, pages, routers, notFound, popups: [] }
175
+ return { version: 2, pages, routers, ...(brokenRouters.length ? { brokenRouters } : {}), notFound, popups: [] }
165
176
  }