@fc3/mmcadi 0.1.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.
- package/LICENSE +673 -0
- package/README.md +5 -0
- package/bin/compile +63 -0
- package/bin/copy-static-files +13 -0
- package/bin/install +5 -0
- package/bin/precompile +55 -0
- package/bin/prepublish +7 -0
- package/bin/remap-paths +113 -0
- package/bin/test +9 -0
- package/config/tsconfig.json +37 -0
- package/package.json +33 -0
- package/src/bootstrap/start.ts +12 -0
- package/src/client/bootstrap.ts +5 -0
- package/src/client/client.ts +54 -0
- package/src/client/enum/key-code.ts +34 -0
- package/src/client/enum/page-type.ts +19 -0
- package/src/client/factory/page.ts +61 -0
- package/src/client/page/confirm-delete-block.ts +30 -0
- package/src/client/page/create-block.ts +6 -0
- package/src/client/page/day.ts +8 -0
- package/src/client/page/edit-block.ts +43 -0
- package/src/client/page/edit-custom.ts +16 -0
- package/src/client/page/home.ts +8 -0
- package/src/client/page/login.ts +8 -0
- package/src/client/page/logout.ts +8 -0
- package/src/client/page/month.ts +8 -0
- package/src/client/page/move-block.ts +8 -0
- package/src/client/page/select-block-type.ts +56 -0
- package/src/client/page/settings.ts +8 -0
- package/src/client/page/view-custom.ts +665 -0
- package/src/client/page/week.ts +8 -0
- package/src/client/page/year.ts +8 -0
- package/src/client/page.ts +128 -0
- package/src/client/shim/execute.ts +5 -0
- package/src/client/shim/logger.ts +44 -0
- package/src/client/utility/get-meta-value.ts +23 -0
- package/src/common/builder/block/background.ts +25 -0
- package/src/common/builder/block/folder.ts +43 -0
- package/src/common/builder/block/header.ts +15 -0
- package/src/common/builder/block/link.ts +32 -0
- package/src/common/builder/block/multicolumn.ts +16 -0
- package/src/common/builder/block/note.ts +13 -0
- package/src/common/builder/block/whitespace.ts +13 -0
- package/src/common/utility/get-parent-path.ts +29 -0
- package/src/constant/month-names.ts +16 -0
- package/src/enum/action-type.ts +11 -0
- package/src/enum/block-type.ts +18 -0
- package/src/enum/calendar-view-mode.ts +9 -0
- package/src/enum/color.ts +15 -0
- package/src/enum/default-role.ts +6 -0
- package/src/enum/directory-item-type.ts +6 -0
- package/src/enum/emoji.ts +94 -0
- package/src/enum/field-type.ts +8 -0
- package/src/enum/header-size.ts +7 -0
- package/src/enum/permission-type.ts +8 -0
- package/src/enum/time-place-visibility.ts +8 -0
- package/src/enum/tool-type.ts +14 -0
- package/src/index.ts +8 -0
- package/src/interface/hydration-callback.ts +7 -0
- package/src/interface/provider.ts +8 -0
- package/src/interface/repository.ts +12 -0
- package/src/lib/array/add.ts +10 -0
- package/src/lib/array/assert-is-empty.ts +23 -0
- package/src/lib/array/clone.ts +5 -0
- package/src/lib/array/difference.ts +8 -0
- package/src/lib/array/empty.ts +6 -0
- package/src/lib/array/equal.ts +24 -0
- package/src/lib/array/get-first-value.ts +5 -0
- package/src/lib/array/get-last-value.ts +5 -0
- package/src/lib/array/get-max-value.ts +5 -0
- package/src/lib/array/get-value-after.ts +23 -0
- package/src/lib/array/get-value-before.ts +23 -0
- package/src/lib/array/get-values-between.ts +18 -0
- package/src/lib/array/index.ts +5 -0
- package/src/lib/array/is-empty.ts +6 -0
- package/src/lib/array/remove.ts +19 -0
- package/src/lib/array/sum.ts +11 -0
- package/src/lib/array/unique.ts +13 -0
- package/src/lib/environment/bootstrap.ts +1 -0
- package/src/lib/environment/enum/environment-type.ts +8 -0
- package/src/lib/environment/enum/service-type.ts +5 -0
- package/src/lib/environment/index.ts +7 -0
- package/src/lib/environment/utility/get-environment-type.ts +28 -0
- package/src/lib/environment/utility/is-browser.ts +5 -0
- package/src/lib/environment/utility/is-development.ts +10 -0
- package/src/lib/environment/utility/is-production-or-staging.ts +8 -0
- package/src/lib/environment/utility/is-production.ts +10 -0
- package/src/lib/environment/utility/is-staging.ts +10 -0
- package/src/lib/errors/enum/error-type.ts +13 -0
- package/src/lib/errors/error/base.ts +40 -0
- package/src/lib/errors/error/conflict.ts +16 -0
- package/src/lib/errors/error/generic.ts +23 -0
- package/src/lib/errors/error/invalid.ts +14 -0
- package/src/lib/errors/error/invariant-violation.ts +5 -0
- package/src/lib/errors/error/not-allowed.ts +14 -0
- package/src/lib/errors/error/not-found.ts +16 -0
- package/src/lib/errors/error/not-implemented.ts +20 -0
- package/src/lib/errors/error/omitted.ts +14 -0
- package/src/lib/errors/error/timeout.ts +14 -0
- package/src/lib/errors/error/unauthorized.ts +14 -0
- package/src/lib/errors/index.ts +18 -0
- package/src/lib/errors/sugar/invariant.ts +7 -0
- package/src/lib/errors/type/coded-error.ts +7 -0
- package/src/lib/errors/type/error-data.ts +11 -0
- package/src/lib/errors/utility/create-error-for-error-type.ts +60 -0
- package/src/lib/errors/utility/create-error-from-ambiguous-json-data.ts +26 -0
- package/src/lib/errors/utility/error-matches.ts +28 -0
- package/src/lib/errors/utility/get-error-type-for-posix-error-code.ts +37 -0
- package/src/lib/errors/utility/get-error-type-for-status-code.ts +38 -0
- package/src/lib/errors/utility/get-message-for-error-type.ts +29 -0
- package/src/lib/errors/utility/matches-posix-error-code.ts +11 -0
- package/src/lib/errors/utility/wrap-error.ts +39 -0
- package/src/lib/event-emitter/index.ts +63 -0
- package/src/lib/execution-context/execution-context.ts +90 -0
- package/src/lib/execution-context/index.ts +8 -0
- package/src/lib/execution-context/type/session.ts +7 -0
- package/src/lib/feature/feature.ts +108 -0
- package/src/lib/feature/index.ts +3 -0
- package/src/lib/filesystem/bootstrap.ts +1 -0
- package/src/lib/filesystem/enum/file-extension.ts +19 -0
- package/src/lib/filesystem/index.ts +4 -0
- package/src/lib/filesystem/utility/exists.ts +21 -0
- package/src/lib/filesystem/utility/get-extension-for-filepath.ts +20 -0
- package/src/lib/filesystem/utility/strip-extension.ts +26 -0
- package/src/lib/grammar/index.ts +3 -0
- package/src/lib/grammar/noun-inflector.ts +104 -0
- package/src/lib/grammar/utility/get-words-from-identifier.ts +9 -0
- package/src/lib/grammar/verb-inflector.ts +344 -0
- package/src/lib/http/buffer-stream.ts +76 -0
- package/src/lib/http/enum/content-type.ts +19 -0
- package/src/lib/http/enum/cookie-attribute.ts +11 -0
- package/src/lib/http/enum/header.ts +29 -0
- package/src/lib/http/enum/method.ts +11 -0
- package/src/lib/http/enum/same-site-policy.ts +7 -0
- package/src/lib/http/enum/status-code.ts +9 -0
- package/src/lib/http/factory/body-parser.ts +55 -0
- package/src/lib/http/index.ts +24 -0
- package/src/lib/http/interface/client-request.ts +15 -0
- package/src/lib/http/interface/incoming-message.ts +24 -0
- package/src/lib/http/interface/network-library.ts +9 -0
- package/src/lib/http/interface/request-callback.ts +7 -0
- package/src/lib/http/isomorphic/request.ts +77 -0
- package/src/lib/http/isomorphic/response.ts +77 -0
- package/src/lib/http/isomorphic.ts +20 -0
- package/src/lib/http/parser/accept-header.ts +35 -0
- package/src/lib/http/parser/basic-auth-header.ts +48 -0
- package/src/lib/http/parser/body/json.ts +14 -0
- package/src/lib/http/parser/body/multipart.ts +89 -0
- package/src/lib/http/parser/body/url-encoded.ts +16 -0
- package/src/lib/http/parser/body.ts +44 -0
- package/src/lib/http/parser/cookie.ts +114 -0
- package/src/lib/http/parser/querystring.ts +92 -0
- package/src/lib/http/parser/url.ts +76 -0
- package/src/lib/http/request/incoming.ts +299 -0
- package/src/lib/http/request/outgoing.ts +276 -0
- package/src/lib/http/request.ts +26 -0
- package/src/lib/http/response/incoming.ts +243 -0
- package/src/lib/http/response/outgoing.ts +160 -0
- package/src/lib/http/response.ts +74 -0
- package/src/lib/http/serializer/basic-auth-header.ts +27 -0
- package/src/lib/http/serializer/cookie.ts +90 -0
- package/src/lib/http/serializer/query-data.ts +54 -0
- package/src/lib/http/transaction.ts +320 -0
- package/src/lib/http/type/basic-auth-credentials.ts +6 -0
- package/src/lib/http/type/header-map.ts +5 -0
- package/src/lib/http/type/log-attributes.ts +12 -0
- package/src/lib/http/type/path-parameters.ts +5 -0
- package/src/lib/http/type/query-data.ts +7 -0
- package/src/lib/http/type/query-value.ts +11 -0
- package/src/lib/http/type/request-options.ts +16 -0
- package/src/lib/http/type/transaction-data.ts +4 -0
- package/src/lib/http/utility/get-content-type-for-extension.ts +47 -0
- package/src/lib/http/utility/get-library-for-protocol.ts +25 -0
- package/src/lib/http/utility/get-status-code-for-error-type.ts +37 -0
- package/src/lib/http/utility/get-status-code-for-error.ts +13 -0
- package/src/lib/http/utility/is-client-error-status.ts +15 -0
- package/src/lib/http/utility/is-error-status.ts +9 -0
- package/src/lib/http/utility/is-server-error-status.ts +7 -0
- package/src/lib/http/utility/is-success-status.ts +7 -0
- package/src/lib/http/utility/proxy-request.ts +58 -0
- package/src/lib/http/utility/serialize-request-options.ts +9 -0
- package/src/lib/http/utility/slug-generator.ts +20 -0
- package/src/lib/key-generator/index.ts +3 -0
- package/src/lib/key-generator/key-generator.ts +7 -0
- package/src/lib/logger/enum/ansi-escape-code.ts +14 -0
- package/src/lib/logger/enum/log-level.ts +8 -0
- package/src/lib/logger/formatter.ts +59 -0
- package/src/lib/logger/index.ts +7 -0
- package/src/lib/logger/interface/error-reporter.ts +7 -0
- package/src/lib/logger/interface/logger.ts +13 -0
- package/src/lib/logger/logger.ts +239 -0
- package/src/lib/logger/type/error-parameters.ts +9 -0
- package/src/lib/logger/type/loggable-data.ts +5 -0
- package/src/lib/logger/type/loggable-value.ts +1 -0
- package/src/lib/metrics/dispatcher.ts +264 -0
- package/src/lib/metrics/enum/dispatch-mode.ts +6 -0
- package/src/lib/metrics/index.ts +2 -0
- package/src/lib/metrics/settings-hydrator.ts +51 -0
- package/src/lib/metrics/type/dispatcher-settings.ts +12 -0
- package/src/lib/metrics/type/tag-map.ts +5 -0
- package/src/lib/network/bootstrap.ts +1 -0
- package/src/lib/network/enum/connection-status.ts +14 -0
- package/src/lib/network/enum/http-status-code.ts +75 -0
- package/src/lib/network/enum/network-protocol.ts +10 -0
- package/src/lib/network/event-emitter.ts +63 -0
- package/src/lib/network/index.ts +7 -0
- package/src/lib/network/interface/socket.ts +42 -0
- package/src/lib/network/type/socket-options.ts +6 -0
- package/src/lib/network/utility/get-protocol-from-url.ts +15 -0
- package/src/lib/operation/index.ts +6 -0
- package/src/lib/operation/operation/auto-logging.ts +94 -0
- package/src/lib/operation/operation/silent.ts +17 -0
- package/src/lib/operation/operation.ts +80 -0
- package/src/lib/process/bootstrap.ts +1 -0
- package/src/lib/process/enum/posix-error-code.ts +138 -0
- package/src/lib/process/enum/process-signal.ts +68 -0
- package/src/lib/process/error.ts +6 -0
- package/src/lib/process/execute.ts +77 -0
- package/src/lib/process/index.ts +4 -0
- package/src/lib/process/type/command-options.ts +9 -0
- package/src/lib/process/utility/bind-process-lifespan-to.ts +10 -0
- package/src/lib/promise-wrapper/index.ts +3 -0
- package/src/lib/promise-wrapper/promise-wrapper.ts +51 -0
- package/src/lib/random/chance.ts +5 -0
- package/src/lib/random/element.ts +7 -0
- package/src/lib/random/elements.ts +12 -0
- package/src/lib/random/enum.ts +34 -0
- package/src/lib/random/enums.ts +11 -0
- package/src/lib/random/index.ts +3 -0
- package/src/lib/random/number.ts +8 -0
- package/src/lib/server/base.ts +194 -0
- package/src/lib/server/enum/server-event.ts +5 -0
- package/src/lib/server/enum/server-status.ts +10 -0
- package/src/lib/server/http/endpoint/ping.ts +21 -0
- package/src/lib/server/http/endpoint.ts +345 -0
- package/src/lib/server/http/interface/endpoint-constructor.ts +12 -0
- package/src/lib/server/http/interface/result-serializer.ts +5 -0
- package/src/lib/server/http/middleware.ts +18 -0
- package/src/lib/server/http/result-serializer/html.ts +92 -0
- package/src/lib/server/http/result-serializer/json.ts +23 -0
- package/src/lib/server/http/route.ts +104 -0
- package/src/lib/server/http/router.ts +125 -0
- package/src/lib/server/http/type/session-resolver.ts +8 -0
- package/src/lib/server/http/utility/create-server.ts +34 -0
- package/src/lib/server/http/utility/path-parser.ts +49 -0
- package/src/lib/server/http/utility/standardize-html-indentation.ts +54 -0
- package/src/lib/server/http/websocket-wrapper.ts +83 -0
- package/src/lib/server/http.ts +301 -0
- package/src/lib/server/index.ts +5 -0
- package/src/lib/server/tcp.ts +53 -0
- package/src/lib/server/type/server-options.ts +10 -0
- package/src/lib/string/calculate-length.ts +17 -0
- package/src/lib/string/capitalize.ts +8 -0
- package/src/lib/string/create-slug.ts +14 -0
- package/src/lib/string/enum/ansi-escape-code.ts +14 -0
- package/src/lib/string/enum/string-encoding.ts +9 -0
- package/src/lib/string/hyphenate.ts +5 -0
- package/src/lib/string/index.ts +3 -0
- package/src/lib/string/pad.ts +14 -0
- package/src/lib/string/trim-multiline.ts +21 -0
- package/src/lib/string/trim.ts +8 -0
- package/src/lib/tcp/channel.ts +13 -0
- package/src/lib/tcp/connection/incoming.ts +72 -0
- package/src/lib/tcp/connection/outgoing.ts +219 -0
- package/src/lib/tcp/connection.ts +360 -0
- package/src/lib/tcp/enum/connection-event.ts +12 -0
- package/src/lib/tcp/enum/message-type.ts +9 -0
- package/src/lib/tcp/index.ts +1 -0
- package/src/lib/tcp/type/connection-parameters.ts +6 -0
- package/src/lib/tcp/type/message/channel-close.ts +9 -0
- package/src/lib/tcp/type/message/channel-open.ts +9 -0
- package/src/lib/tcp/type/message.ts +10 -0
- package/src/lib/test/index.ts +2 -0
- package/src/lib/test/utility/build-config.ts +17 -0
- package/src/lib/test/utility/build-execution-context.ts +11 -0
- package/src/lib/time/bootstrap.ts +1 -0
- package/src/lib/time/enum/time-interval.ts +12 -0
- package/src/lib/time/index.ts +4 -0
- package/src/lib/time/utility/format-duration.ts +139 -0
- package/src/lib/time/utility/format-relative-duration.ts +22 -0
- package/src/lib/time/utility/sleep.ts +9 -0
- package/src/operation.ts +18 -0
- package/src/server/endpoint/abstract-page.ts +126 -0
- package/src/server/endpoint/action/create.ts +545 -0
- package/src/server/endpoint/action/get.ts +231 -0
- package/src/server/endpoint/base.ts +51 -0
- package/src/server/endpoint/day.ts +13 -0
- package/src/server/endpoint/generic-page.ts +14 -0
- package/src/server/endpoint/index.ts +11 -0
- package/src/server/endpoint/login.ts +45 -0
- package/src/server/endpoint/logout.ts +38 -0
- package/src/server/endpoint/month.ts +35 -0
- package/src/server/endpoint/robots.ts +18 -0
- package/src/server/endpoint/session/create.ts +62 -0
- package/src/server/endpoint/settings/get.ts +40 -0
- package/src/server/endpoint/settings/update.ts +44 -0
- package/src/server/endpoint/today.ts +15 -0
- package/src/server/endpoint/week.ts +34 -0
- package/src/server/endpoint/year.ts +34 -0
- package/src/server/enum/custom-page-type.ts +10 -0
- package/src/server/factory/block-result-serializer.ts +115 -0
- package/src/server/middleware/base.ts +6 -0
- package/src/server/middleware/error-handler.ts +177 -0
- package/src/server/middleware/static-file.ts +83 -0
- package/src/server/operation/add-block.ts +110 -0
- package/src/server/operation/create-session.ts +43 -0
- package/src/server/operation/delete-block.ts +96 -0
- package/src/server/operation/demote-block.ts +108 -0
- package/src/server/operation/destroy-session.ts +26 -0
- package/src/server/operation/load-account.ts +30 -0
- package/src/server/operation/load-block.ts +48 -0
- package/src/server/operation/load-directory-items.ts +75 -0
- package/src/server/operation/load-page.ts +368 -0
- package/src/server/operation/promote-block.ts +104 -0
- package/src/server/operation/save-page.ts +60 -0
- package/src/server/operation/update-account-settings.ts +52 -0
- package/src/server/operation/update-block.ts +124 -0
- package/src/server/repository/memory.ts +112 -0
- package/src/server/result-serializer/action.ts +939 -0
- package/src/server/result-serializer/base.ts +1267 -0
- package/src/server/result-serializer/block/audio.ts +33 -0
- package/src/server/result-serializer/block/background.ts +20 -0
- package/src/server/result-serializer/block/calendar.ts +148 -0
- package/src/server/result-serializer/block/directory.ts +57 -0
- package/src/server/result-serializer/block/ephemeral.ts +14 -0
- package/src/server/result-serializer/block/folder.ts +106 -0
- package/src/server/result-serializer/block/header.ts +31 -0
- package/src/server/result-serializer/block/image.ts +46 -0
- package/src/server/result-serializer/block/link.ts +40 -0
- package/src/server/result-serializer/block/list.ts +140 -0
- package/src/server/result-serializer/block/multi-column.ts +97 -0
- package/src/server/result-serializer/block/note.ts +44 -0
- package/src/server/result-serializer/block/todo.ts +78 -0
- package/src/server/result-serializer/block/whitespace.ts +15 -0
- package/src/server/result-serializer/block.ts +248 -0
- package/src/server/result-serializer/common/time-place-options.ts +33 -0
- package/src/server/result-serializer/login.ts +58 -0
- package/src/server/result-serializer/month.ts +170 -0
- package/src/server/result-serializer/page.ts +548 -0
- package/src/server/result-serializer/settings.ts +85 -0
- package/src/server/result-serializer/week.ts +195 -0
- package/src/server/result-serializer/year.ts +160 -0
- package/src/server/utility/accepts-content-type.ts +16 -0
- package/src/server/utility/accepts-html.ts +9 -0
- package/src/server/utility/accepts-json.ts +9 -0
- package/src/server/utility/add-days.ts +11 -0
- package/src/server/utility/create-date.ts +23 -0
- package/src/server/utility/create-index-path.ts +5 -0
- package/src/server/utility/create-slug.ts +9 -0
- package/src/server/utility/create-toast-path.ts +19 -0
- package/src/server/utility/date-less-than.ts +5 -0
- package/src/server/utility/date-occurs-before.ts +22 -0
- package/src/server/utility/date-to-key.ts +7 -0
- package/src/server/utility/date-to-label.ts +5 -0
- package/src/server/utility/date-to-path.ts +9 -0
- package/src/server/utility/dates-are-equal.ts +22 -0
- package/src/server/utility/dates-are-in-order.ts +9 -0
- package/src/server/utility/dates-are-on-the-same-day.ts +9 -0
- package/src/server/utility/dates-in-same-month.ts +8 -0
- package/src/server/utility/day-to-date.ts +40 -0
- package/src/server/utility/decrement-date.ts +11 -0
- package/src/server/utility/get-block-at-index-path.ts +32 -0
- package/src/server/utility/get-block-permissions.ts +15 -0
- package/src/server/utility/get-compact-label-for-date.ts +8 -0
- package/src/server/utility/get-custom-page-type-for-path.ts +37 -0
- package/src/server/utility/get-element-id-for-block.ts +15 -0
- package/src/server/utility/get-emoji-for-block-type.ts +39 -0
- package/src/server/utility/get-emoji-for-tool-type.ts +20 -0
- package/src/server/utility/get-end-of-month.ts +13 -0
- package/src/server/utility/get-end-of-week.ts +13 -0
- package/src/server/utility/get-filepath-for-folder-block.ts +23 -0
- package/src/server/utility/get-hex-for-color.ts +31 -0
- package/src/server/utility/get-label-for-time-place-visibility.ts +23 -0
- package/src/server/utility/get-month-name.ts +18 -0
- package/src/server/utility/get-next-day.ts +7 -0
- package/src/server/utility/get-next-month.ts +12 -0
- package/src/server/utility/get-next-week.ts +12 -0
- package/src/server/utility/get-next-year.ts +13 -0
- package/src/server/utility/get-ordinal-label-for-week.ts +11 -0
- package/src/server/utility/get-ordinal-of-week-within-month.ts +15 -0
- package/src/server/utility/get-padded-date.ts +11 -0
- package/src/server/utility/get-parent-index-path.ts +18 -0
- package/src/server/utility/get-path-for-folder-block.ts +25 -0
- package/src/server/utility/get-path-from-url.ts +13 -0
- package/src/server/utility/get-previous-index-path.ts +27 -0
- package/src/server/utility/get-previous-month.ts +12 -0
- package/src/server/utility/get-previous-week.ts +11 -0
- package/src/server/utility/get-previous-year.ts +13 -0
- package/src/server/utility/get-random-emoji-for-date.ts +109 -0
- package/src/server/utility/get-short-label-for-date.ts +11 -0
- package/src/server/utility/get-short-month-name.ts +9 -0
- package/src/server/utility/get-short-ordinal.ts +26 -0
- package/src/server/utility/get-siblings-and-index-for-index-path.ts +67 -0
- package/src/server/utility/get-start-of-month.ts +11 -0
- package/src/server/utility/get-start-of-week.ts +12 -0
- package/src/server/utility/get-start-of-year.ts +12 -0
- package/src/server/utility/get-weekday-name.ts +24 -0
- package/src/server/utility/increment-date.ts +15 -0
- package/src/server/utility/is-index-path.ts +5 -0
- package/src/server/utility/key-to-date.ts +25 -0
- package/src/server/utility/month-has-week-containing-date.ts +13 -0
- package/src/server/utility/month-to-date.ts +28 -0
- package/src/server/utility/month-to-path.ts +10 -0
- package/src/server/utility/months-are-equal.ts +8 -0
- package/src/server/utility/parse-index-path.ts +23 -0
- package/src/server/utility/path-is-date.ts +13 -0
- package/src/server/utility/path-to-date.ts +24 -0
- package/src/server/utility/subtract-days.ts +7 -0
- package/src/server/utility/validate-month.ts +10 -0
- package/src/server/utility/validate-page.ts +46 -0
- package/src/server/utility/validate-path.ts +9 -0
- package/src/server/utility/validate-url.ts +12 -0
- package/src/server/utility/week-greater-than.ts +10 -0
- package/src/server/utility/week-to-date.ts +8 -0
- package/src/server/utility/week-to-path.ts +12 -0
- package/src/server/utility/weeks-are-equal.ts +8 -0
- package/src/server/utility/year-to-date.ts +26 -0
- package/src/server/utility/year-to-path.ts +7 -0
- package/src/server/utility/years-are-equal.ts +8 -0
- package/src/server.ts +140 -0
- package/src/test.ts +195 -0
- package/src/type/account-settings.ts +9 -0
- package/src/type/account.ts +11 -0
- package/src/type/action/add-block.ts +74 -0
- package/src/type/action/complete-block.ts +10 -0
- package/src/type/action/delete-block.ts +11 -0
- package/src/type/action/demote-block.ts +9 -0
- package/src/type/action/edit-block.ts +11 -0
- package/src/type/action/promote-block.ts +9 -0
- package/src/type/action.ts +9 -0
- package/src/type/block/audio.ts +8 -0
- package/src/type/block/background.ts +10 -0
- package/src/type/block/directory.ts +18 -0
- package/src/type/block/ephemeral.ts +9 -0
- package/src/type/block/folder.ts +14 -0
- package/src/type/block/header.ts +11 -0
- package/src/type/block/image.ts +8 -0
- package/src/type/block/link.ts +12 -0
- package/src/type/block/list.ts +24 -0
- package/src/type/block/multi-column.ts +10 -0
- package/src/type/block/note.ts +9 -0
- package/src/type/block/todo.ts +10 -0
- package/src/type/block/whitespace.ts +9 -0
- package/src/type/block-metadata.ts +14 -0
- package/src/type/block.ts +18 -0
- package/src/type/day.ts +5 -0
- package/src/type/field-definition.ts +8 -0
- package/src/type/field-value.ts +5 -0
- package/src/type/file.ts +6 -0
- package/src/type/month.ts +5 -0
- package/src/type/page.ts +8 -0
- package/src/type/price.ts +11 -0
- package/src/type/resource.ts +5 -0
- package/src/type/runtime-config.ts +13 -0
- package/src/type/server-settings.ts +17 -0
- package/src/type/toast.ts +8 -0
- package/src/type/week.ts +5 -0
- package/src/type/year.ts +5 -0
- package/test/unit/index.ts +11 -0
- package/test/unit/server/operation/promote-block.ts +84 -0
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import PromiseWrapper from 'lib/promise-wrapper';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
InvariantViolation,
|
|
5
|
+
InvalidError,
|
|
6
|
+
createErrorFromAmbiguousJsonData
|
|
7
|
+
} from 'lib/errors';
|
|
8
|
+
|
|
9
|
+
import {isBrowser} from 'lib/environment';
|
|
10
|
+
import {TagMap} from 'lib/metrics';
|
|
11
|
+
|
|
12
|
+
import Response from 'lib/http/response';
|
|
13
|
+
import HeaderMap from 'lib/http/type/header-map';
|
|
14
|
+
import StatusCode from 'lib/http/enum/status-code';
|
|
15
|
+
import ContentType from 'lib/http/enum/content-type';
|
|
16
|
+
import BufferStream from 'lib/http/buffer-stream';
|
|
17
|
+
import IncomingMessage from 'lib/http/interface/incoming-message';
|
|
18
|
+
|
|
19
|
+
class IncomingResponse extends Response {
|
|
20
|
+
private native_response: IncomingMessage | undefined;
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
22
|
+
private promise_wrapper: PromiseWrapper<object> | undefined;
|
|
23
|
+
private stream: BufferStream | undefined;
|
|
24
|
+
private url: string | undefined;
|
|
25
|
+
|
|
26
|
+
public constructor() {
|
|
27
|
+
super();
|
|
28
|
+
|
|
29
|
+
this.bindHandlers();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public setNativeResponse(native_response: IncomingMessage): void {
|
|
33
|
+
this.native_response = native_response;
|
|
34
|
+
|
|
35
|
+
this.setStatusCode(native_response.statusCode as StatusCode);
|
|
36
|
+
this.setHeaders(native_response.headers as HeaderMap);
|
|
37
|
+
|
|
38
|
+
this.assignHandlers();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public setUrl(url: string): this {
|
|
42
|
+
this.url = url;
|
|
43
|
+
|
|
44
|
+
const parsed_url = new URL(url);
|
|
45
|
+
|
|
46
|
+
return this.setPath(parsed_url.pathname);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public getUrl(): string {
|
|
50
|
+
if (this.url === undefined) {
|
|
51
|
+
throw new Error('Tried to read url, but it was not set');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return this.url;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
58
|
+
public getPromise(): Promise<object> {
|
|
59
|
+
const promise_wrapper = this.getPromiseWrapper();
|
|
60
|
+
|
|
61
|
+
return promise_wrapper.getPromise();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
public getStream(): BufferStream {
|
|
65
|
+
if (this.stream !== undefined) {
|
|
66
|
+
return this.stream;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (isBrowser()) {
|
|
70
|
+
throw new InvariantViolation('Tried to create buffer stream in browser');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
this.stream = new BufferStream();
|
|
74
|
+
|
|
75
|
+
return this.stream;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public getNativeResponse(): IncomingMessage {
|
|
79
|
+
if (this.native_response === undefined) {
|
|
80
|
+
throw new Error('Tried to read native response, but it was not set');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return this.native_response;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
protected getCountMetricName(): string {
|
|
87
|
+
return 'incoming_responses';
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
protected getTimingMetricName(): string {
|
|
91
|
+
return 'incoming_response_time';
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
protected getMetricTags(): TagMap {
|
|
95
|
+
return {
|
|
96
|
+
status: this.getStatusCode()
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private bindHandlers(): void {
|
|
101
|
+
this.handleResponseData = this.handleResponseData.bind(this);
|
|
102
|
+
this.handleResponseEnd = this.handleResponseEnd.bind(this);
|
|
103
|
+
this.handleResponseError = this.handleResponseError.bind(this);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
private assignHandlers(): void {
|
|
107
|
+
const native_response = this.getNativeResponse();
|
|
108
|
+
|
|
109
|
+
native_response.on('data', this.handleResponseData);
|
|
110
|
+
native_response.on('end', this.handleResponseEnd);
|
|
111
|
+
native_response.on('error', this.handleResponseError);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
private handleResponseData(data: Buffer): void {
|
|
115
|
+
if (this.isStreaming() && !this.hasErrorStatus()) {
|
|
116
|
+
this.appendToStream(data);
|
|
117
|
+
} else {
|
|
118
|
+
this.appendToBody(data);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
private appendToStream(data: Buffer): void {
|
|
123
|
+
const stream = this.getStream();
|
|
124
|
+
|
|
125
|
+
stream.write(data);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
private handleResponseEnd(): void {
|
|
129
|
+
this.end();
|
|
130
|
+
|
|
131
|
+
if (this.isStreaming()) {
|
|
132
|
+
this.endStream();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
let parsed_body;
|
|
136
|
+
|
|
137
|
+
if (this.shouldParseBody()) {
|
|
138
|
+
try {
|
|
139
|
+
parsed_body = this.parseBody();
|
|
140
|
+
} catch (error) {
|
|
141
|
+
if (!(error instanceof Error)) {
|
|
142
|
+
throw error;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const wrapped_error = new InvalidError(error.message);
|
|
146
|
+
|
|
147
|
+
return void this.rejectPromise(wrapped_error);
|
|
148
|
+
}
|
|
149
|
+
} else {
|
|
150
|
+
parsed_body = {};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (this.hasErrorStatus()) {
|
|
154
|
+
const status_code = this.getStatusCode();
|
|
155
|
+
const error = createErrorFromAmbiguousJsonData(parsed_body, status_code);
|
|
156
|
+
|
|
157
|
+
return void this.rejectPromise(error);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return void this.fulfillPromise(parsed_body);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
private endStream(): void {
|
|
164
|
+
const stream = this.getStream();
|
|
165
|
+
|
|
166
|
+
stream.end();
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
170
|
+
private parseBody(): object {
|
|
171
|
+
const body_string = this.getBodyAsString();
|
|
172
|
+
|
|
173
|
+
return JSON.parse(body_string);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
private hasEmptyBody(): boolean {
|
|
177
|
+
return this.getBody().length === 0;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
private shouldParseBody(): boolean {
|
|
181
|
+
if (this.isStreaming() && !this.hasErrorStatus()) {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const content_type = this.getContentType();
|
|
186
|
+
|
|
187
|
+
if (content_type !== ContentType.JSON) {
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (this.wasRedirected()) {
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (this.hasEmptyBody()) {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return true;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
private wasRedirected(): boolean {
|
|
203
|
+
const status_code = this.getStatusCode();
|
|
204
|
+
|
|
205
|
+
return (
|
|
206
|
+
status_code >= StatusCode.MULTIPLE_CHOICES &&
|
|
207
|
+
status_code <= StatusCode.PERMANENT_REDIRECT
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
private handleResponseError(error: Error): void {
|
|
212
|
+
this.rejectPromise(error);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
216
|
+
private fulfillPromise(result: object): void {
|
|
217
|
+
const promise_wrapper = this.getPromiseWrapper();
|
|
218
|
+
|
|
219
|
+
promise_wrapper.resolve(result);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
private rejectPromise(error: Error): void {
|
|
223
|
+
const promise_wrapper = this.getPromiseWrapper();
|
|
224
|
+
|
|
225
|
+
promise_wrapper.reject(error);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
229
|
+
private getPromiseWrapper(): PromiseWrapper<object> {
|
|
230
|
+
if (this.promise_wrapper === undefined) {
|
|
231
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
232
|
+
this.promise_wrapper = new PromiseWrapper<object>();
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return this.promise_wrapper;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
private isStreaming(): boolean {
|
|
239
|
+
return this.stream !== undefined;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export default IncomingResponse;
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import HTTP from 'http';
|
|
2
|
+
|
|
3
|
+
import {StringEncoding} from 'lib/string';
|
|
4
|
+
import {wrapError} from 'lib/errors';
|
|
5
|
+
import {TagMap} from 'lib/metrics';
|
|
6
|
+
|
|
7
|
+
import Response from 'lib/http/response';
|
|
8
|
+
import HttpHeader from 'lib/http/enum/header';
|
|
9
|
+
import StatusCode from 'lib/http/enum/status-code';
|
|
10
|
+
import ContentType from 'lib/http/enum/content-type';
|
|
11
|
+
import getStatusCodeForErrorType from 'lib/http/utility/get-status-code-for-error-type';
|
|
12
|
+
|
|
13
|
+
class OutgoingResponse extends Response {
|
|
14
|
+
private native_response: HTTP.ServerResponse;
|
|
15
|
+
|
|
16
|
+
public constructor(native_response: HTTP.ServerResponse) {
|
|
17
|
+
super();
|
|
18
|
+
|
|
19
|
+
this.native_response = native_response;
|
|
20
|
+
this.bindHandlers();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
24
|
+
public sendPromise(promise: Promise<object | string | Buffer>): void {
|
|
25
|
+
promise.then(this.send).catch(this.error);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public render(body: string): void {
|
|
29
|
+
this.setHeaderValue(HttpHeader.CONTENT_TYPE, ContentType.HTML);
|
|
30
|
+
this.sendString(body);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
34
|
+
public send(body: object | string | Buffer): void {
|
|
35
|
+
if (typeof body === 'string') {
|
|
36
|
+
this.sendString(body);
|
|
37
|
+
} else if (body instanceof Buffer) {
|
|
38
|
+
this.sendBuffer(body);
|
|
39
|
+
} else {
|
|
40
|
+
this.sendObject(body);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public error(error: Error): void {
|
|
45
|
+
const wrapped_error = wrapError(error);
|
|
46
|
+
|
|
47
|
+
this.getLogger().error(wrapped_error);
|
|
48
|
+
|
|
49
|
+
const error_type = wrapped_error.getType();
|
|
50
|
+
const status_code = getStatusCodeForErrorType(error_type);
|
|
51
|
+
const error_data = wrapped_error.getData();
|
|
52
|
+
|
|
53
|
+
this.setStatusCode(status_code);
|
|
54
|
+
this.send(error_data);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
public redirectToUrl(url: string): void {
|
|
58
|
+
this.setHeaderValue(HttpHeader.LOCATION, url);
|
|
59
|
+
this.setStatusCode(StatusCode.REDIRECT);
|
|
60
|
+
this.send('');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public setHeaderValue(key: HttpHeader, value: string | number): this {
|
|
64
|
+
const native_response = this.getNativeResponse();
|
|
65
|
+
|
|
66
|
+
if (key === HttpHeader.CONTENT_TYPE) {
|
|
67
|
+
switch (value) {
|
|
68
|
+
case ContentType.HTML:
|
|
69
|
+
case ContentType.TEXT:
|
|
70
|
+
case ContentType.JSON:
|
|
71
|
+
value = `${value}; charset=UTF-8`;
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
native_response.setHeader(key, value);
|
|
77
|
+
|
|
78
|
+
return super.setHeaderValue(key, value);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
public setStatusCode(status_code: StatusCode): this {
|
|
82
|
+
const native_response = this.getNativeResponse();
|
|
83
|
+
|
|
84
|
+
native_response.statusCode = status_code;
|
|
85
|
+
|
|
86
|
+
return super.setStatusCode(status_code);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
public getNativeResponse(): HTTP.ServerResponse {
|
|
90
|
+
return this.native_response;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
protected getCountMetricName(): string {
|
|
94
|
+
return 'outgoing_responses';
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
protected getTimingMetricName(): string {
|
|
98
|
+
return 'outgoing_response_time';
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
protected getMetricTags(): TagMap {
|
|
102
|
+
return {
|
|
103
|
+
status: this.getStatusCode(),
|
|
104
|
+
endpoint: 'UPDATE ME'
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Overwrite the parent class's end() implementation in order to first
|
|
110
|
+
* forward along the response body to the underlying native response.
|
|
111
|
+
*/
|
|
112
|
+
protected end(): void {
|
|
113
|
+
const native_response = this.getNativeResponse();
|
|
114
|
+
const body = this.getBody();
|
|
115
|
+
|
|
116
|
+
// TODO: Add validation ensuring that the response body is empty
|
|
117
|
+
// if a 204 NO CONTENT response status code has been assigned.
|
|
118
|
+
|
|
119
|
+
native_response.end(body);
|
|
120
|
+
|
|
121
|
+
super.end();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
private bindHandlers(): void {
|
|
125
|
+
this.error = this.error.bind(this);
|
|
126
|
+
this.send = this.send.bind(this);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
130
|
+
private sendObject(body_object: object): void {
|
|
131
|
+
const body_string = JSON.stringify(body_object);
|
|
132
|
+
|
|
133
|
+
if (!this.hasHeaderValue(HttpHeader.CONTENT_TYPE)) {
|
|
134
|
+
this.setHeaderValue(HttpHeader.CONTENT_TYPE, ContentType.JSON);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
this.sendString(body_string);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
private sendString(body_string: string): void {
|
|
141
|
+
if (!this.hasHeaderValue(HttpHeader.CONTENT_TYPE)) {
|
|
142
|
+
this.setHeaderValue(HttpHeader.CONTENT_TYPE, ContentType.TEXT);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const body_buffer = Buffer.from(body_string, StringEncoding.UTF8);
|
|
146
|
+
|
|
147
|
+
return this.sendBuffer(body_buffer);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
private sendBuffer(body_buffer: Buffer): void {
|
|
151
|
+
const byte_length = body_buffer.length;
|
|
152
|
+
|
|
153
|
+
this.setHeaderValue(HttpHeader.CONTENT_LENGTH, byte_length);
|
|
154
|
+
this.setBody(body_buffer);
|
|
155
|
+
|
|
156
|
+
this.end();
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export default OutgoingResponse;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import {LogLevel} from 'lib/logger';
|
|
2
|
+
|
|
3
|
+
import StatusCode from 'lib/http/enum/status-code';
|
|
4
|
+
import Transaction from 'lib/http/transaction';
|
|
5
|
+
import LogAttributes from 'lib/http/type/log-attributes';
|
|
6
|
+
import isErrorStatus from 'lib/http/utility/is-error-status';
|
|
7
|
+
import isSuccessStatus from 'lib/http/utility/is-success-status';
|
|
8
|
+
import isServerErrorStatus from 'lib/http/utility/is-server-error-status';
|
|
9
|
+
import isClientErrorStatus from 'lib/http/utility/is-client-error-status';
|
|
10
|
+
|
|
11
|
+
abstract class HttpResponse extends Transaction {
|
|
12
|
+
private status_code: StatusCode = StatusCode.SUCCESS;
|
|
13
|
+
|
|
14
|
+
public getStatusCode(): StatusCode {
|
|
15
|
+
return this.status_code;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public setStatusCode(status_code: StatusCode): this {
|
|
19
|
+
this.status_code = status_code;
|
|
20
|
+
return this;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
protected hasErrorStatus(): boolean {
|
|
24
|
+
const status_code = this.getStatusCode();
|
|
25
|
+
|
|
26
|
+
return isErrorStatus(status_code);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
protected getLogAttributes(): LogAttributes {
|
|
30
|
+
return {
|
|
31
|
+
class_name: this.constructor.name,
|
|
32
|
+
status_code: this.getStatusCode(),
|
|
33
|
+
http_method: this.getMethod(),
|
|
34
|
+
path: this.getSanitizedPath(),
|
|
35
|
+
body: this.getBodyAsString()
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
protected getLogLevel(): LogLevel {
|
|
40
|
+
if (this.hasServerErrorStatus()) {
|
|
41
|
+
return LogLevel.ERROR;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (this.hasClientErrorStatus()) {
|
|
45
|
+
return LogLevel.WARNING;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (this.hasSuccessStatus()) {
|
|
49
|
+
return LogLevel.SUCCESS;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return LogLevel.INFO;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
private hasServerErrorStatus(): boolean {
|
|
56
|
+
const status_code = this.getStatusCode();
|
|
57
|
+
|
|
58
|
+
return isServerErrorStatus(status_code);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private hasClientErrorStatus(): boolean {
|
|
62
|
+
const status_code = this.getStatusCode();
|
|
63
|
+
|
|
64
|
+
return isClientErrorStatus(status_code);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private hasSuccessStatus(): boolean {
|
|
68
|
+
const status_code = this.getStatusCode();
|
|
69
|
+
|
|
70
|
+
return isSuccessStatus(status_code);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export default HttpResponse;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import {StringEncoding} from 'lib/string';
|
|
2
|
+
|
|
3
|
+
import BasicAuthCredentials from 'lib/http/type/basic-auth-credentials';
|
|
4
|
+
|
|
5
|
+
class BasicAuthHeaderSerializer {
|
|
6
|
+
private credentials: BasicAuthCredentials;
|
|
7
|
+
|
|
8
|
+
public constructor(credentials: BasicAuthCredentials) {
|
|
9
|
+
this.credentials = credentials;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
public serialize(): string {
|
|
13
|
+
const credentials = this.getCredentials();
|
|
14
|
+
|
|
15
|
+
const input = `${credentials.username}:${credentials.password}`;
|
|
16
|
+
const buffer = Buffer.from(input);
|
|
17
|
+
const base64 = buffer.toString(StringEncoding.BASE64);
|
|
18
|
+
|
|
19
|
+
return `Basic ${base64}`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
private getCredentials(): BasicAuthCredentials {
|
|
23
|
+
return this.credentials;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export default BasicAuthHeaderSerializer;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This class builds a cookie value for a given session id.
|
|
3
|
+
* The resultant cookie will be set on an outbound response back to the browser,
|
|
4
|
+
* and the browser will store it locally for reuse on subsequent requests.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {TimeInterval} from 'lib/time';
|
|
8
|
+
|
|
9
|
+
import SameSitePolicy from 'lib/http/enum/same-site-policy';
|
|
10
|
+
import CookieAttribute from 'lib/http/enum/cookie-attribute';
|
|
11
|
+
|
|
12
|
+
class CookieSerializer {
|
|
13
|
+
private static duration = TimeInterval.ONE_MONTH;
|
|
14
|
+
private static same_site_policy = SameSitePolicy.NONE;
|
|
15
|
+
|
|
16
|
+
public static setDuration(duration: number): void {
|
|
17
|
+
this.duration = duration;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public static getDuration(): number {
|
|
21
|
+
return this.duration;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public static setSameSitePolicy(same_site_policy: SameSitePolicy): void {
|
|
25
|
+
this.same_site_policy = same_site_policy;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public static getSameSitePolicy(): SameSitePolicy {
|
|
29
|
+
return this.same_site_policy;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
private session_id: string;
|
|
33
|
+
private domain: string | undefined;
|
|
34
|
+
|
|
35
|
+
public constructor(session_id: string, domain?: string) {
|
|
36
|
+
this.session_id = session_id;
|
|
37
|
+
this.domain = domain;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public serialize(): string {
|
|
41
|
+
const cookie_parts = [
|
|
42
|
+
`${CookieAttribute.SESSION}=${this.getSessionId()}`,
|
|
43
|
+
`${CookieAttribute.PATH}=${this.getPath()}`,
|
|
44
|
+
`${CookieAttribute.EXPIRES}=${this.getExpirationTimestamp()}`,
|
|
45
|
+
`${CookieAttribute.SAMESITE}=${this.getSameSitePolicy()}`
|
|
46
|
+
];
|
|
47
|
+
|
|
48
|
+
const domain = this.getDomain();
|
|
49
|
+
|
|
50
|
+
if (domain !== undefined) {
|
|
51
|
+
cookie_parts.push(`${CookieAttribute.DOMAIN}=${domain}`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
cookie_parts.push(CookieAttribute.HTTPONLY);
|
|
55
|
+
|
|
56
|
+
if (this.isSecure()) {
|
|
57
|
+
cookie_parts.push(CookieAttribute.SECURE);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return cookie_parts.join('; ');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private getSessionId(): string {
|
|
64
|
+
return this.session_id;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private getDomain(): string | undefined {
|
|
68
|
+
return this.domain;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private isSecure(): boolean {
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
private getPath(): string {
|
|
76
|
+
return '/';
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
private getExpirationTimestamp(): string {
|
|
80
|
+
const timestamp = Date.now() + CookieSerializer.getDuration();
|
|
81
|
+
|
|
82
|
+
return new Date(timestamp).toUTCString();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private getSameSitePolicy(): SameSitePolicy {
|
|
86
|
+
return CookieSerializer.getSameSitePolicy();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export default CookieSerializer;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import {encode} from 'querystring';
|
|
2
|
+
|
|
3
|
+
import QueryData from 'lib/http/type/query-data';
|
|
4
|
+
import QueryValue from 'lib/http/type/query-value';
|
|
5
|
+
|
|
6
|
+
class QueryDataSerializer {
|
|
7
|
+
private data: QueryData;
|
|
8
|
+
|
|
9
|
+
public constructor(data: QueryData) {
|
|
10
|
+
this.data = data;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public serialize(): string {
|
|
14
|
+
const data = this.getData();
|
|
15
|
+
|
|
16
|
+
// TODO: Probably a better way to do this "am I running in the browser"
|
|
17
|
+
// check. Can put it somewhere and recycle it; it isn't like we need the
|
|
18
|
+
// typeof check here for Typescript purposes, after all.
|
|
19
|
+
if (typeof encode === 'function') {
|
|
20
|
+
return encode(data);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const pairs: string[] = [];
|
|
24
|
+
const keys = Object.keys(data);
|
|
25
|
+
|
|
26
|
+
function append(key: string, value: QueryValue): void {
|
|
27
|
+
const encoded_value = encodeURIComponent(value as string);
|
|
28
|
+
const pair = `${key}=${encoded_value}`;
|
|
29
|
+
|
|
30
|
+
pairs.push(pair);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
keys.forEach((key) => {
|
|
34
|
+
// TODO: Should we encode the value?
|
|
35
|
+
const value = data[key];
|
|
36
|
+
|
|
37
|
+
if (Array.isArray(value)) {
|
|
38
|
+
value.forEach((nested_value) => {
|
|
39
|
+
append(key, nested_value);
|
|
40
|
+
});
|
|
41
|
+
} else {
|
|
42
|
+
append(key, value);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
return pairs.join('&');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
private getData(): QueryData {
|
|
50
|
+
return this.data;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export default QueryDataSerializer;
|