Skip to content
This repository was archived by the owner on Oct 25, 2023. It is now read-only.

replace StorageApi global var with a make fn #394

Merged
merged 1 commit into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions archaeologist/src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
NodeUtil,
TotalUserActivity,
ResourceVisit,
smuggler,
makeDatacenterStorageApi,
UserExternalPipelineId,
NodeCreatedVia,
UserExternalPipelineIngestionProgress,
Expand Down Expand Up @@ -877,7 +877,7 @@ browser.contextMenus.onClicked.addListener(
}
)

const storage: StorageApi = smuggler
const storage: StorageApi = makeDatacenterStorageApi()

auth.register()
browserBookmarks.register(storage)
Expand Down
7 changes: 5 additions & 2 deletions archaeologist/src/content/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import browser from 'webextension-polyfill'
import { PostHog } from 'posthog-js'
import { v4 as uuidv4 } from 'uuid'

import { NodeUtil, NodeType, smuggler } from 'smuggler-api'
import { NodeUtil, NodeType, makeDatacenterStorageApi } from 'smuggler-api'
import type { TNode, TNodeJson } from 'smuggler-api'
import { genOriginId, OriginIdentity, log, productanalytics } from 'armoury'
import * as truthsayer_archaeologist_communication from 'truthsayer-archaeologist-communication'
Expand Down Expand Up @@ -390,7 +390,10 @@ const App = () => {
return (
<AppErrorBoundary>
<ContentContext.Provider
value={{ analytics: state.analytics, storage: smuggler }}
value={{
analytics: state.analytics,
storage: makeDatacenterStorageApi(),
}}
>
<BrowserHistoryImportControlPortal
progress={state.browserHistoryUploadProgress}
Expand Down
4 changes: 2 additions & 2 deletions archaeologist/src/popup/PopUpApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { mazed } from '../util/mazed'
import { MdiLaunch } from 'elementary'
import { productanalytics } from 'armoury'
import { PopUpContext } from './context'
import { smuggler } from 'smuggler-api'
import { makeDatacenterStorageApi } from 'smuggler-api'

const AppContainer = styled.div`
width: 340px;
Expand Down Expand Up @@ -74,7 +74,7 @@ export const PopUpApp = () => {

return (
<AppContainer>
<PopUpContext.Provider value={{ storage: smuggler }}>
<PopUpContext.Provider value={{ storage: makeDatacenterStorageApi() }}>
{state.userUid == null ? <LoginPage /> : <ViewActiveTabStatus />}
</PopUpContext.Provider>
</AppContainer>
Expand Down
113 changes: 59 additions & 54 deletions smuggler-api/src/api_cloud.ts → smuggler-api/src/api_datacenter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Implementation of smuggler APIs like @see StorageApi which interact with a
* cloud-hosted infrastructure to perform their job.
* Implementation of smuggler APIs like @see StorageApi which, to perform their job,
* interact with an infrastructure hosted in Mazed's datacenter.
*/

import {
Expand Down Expand Up @@ -233,7 +233,7 @@ async function lookupNodes(key: NodeLookupKey, signal?: AbortSignal) {
} else if ('webBookmark' in key) {
const { id, stableUrl } = genOriginId(key.webBookmark.url)
const query = { ...SLICE_ALL, origin: { id } }
const iter = smuggler.node.slice(query)
const iter = _getNodesSliceIter(query)

for (let node = await iter.next(); node != null; node = await iter.next()) {
const nodeUrl = node.extattrs?.web?.url
Expand All @@ -245,7 +245,7 @@ async function lookupNodes(key: NodeLookupKey, signal?: AbortSignal) {
} else if ('webQuote' in key) {
const { id, stableUrl } = genOriginId(key.webQuote.url)
const query = { ...SLICE_ALL, origin: { id } }
const iter = smuggler.node.slice(query)
const iter = _getNodesSliceIter(query)

const nodes: TNode[] = []
for (let node = await iter.next(); node != null; node = await iter.next()) {
Expand All @@ -261,7 +261,7 @@ async function lookupNodes(key: NodeLookupKey, signal?: AbortSignal) {
} else if ('url' in key) {
const { id, stableUrl } = genOriginId(key.url)
const query = { ...SLICE_ALL, origin: { id } }
const iter = smuggler.node.slice(query)
const iter = _getNodesSliceIter(query)

const nodes: TNode[] = []
for (let node = await iter.next(); node != null; node = await iter.next()) {
Expand Down Expand Up @@ -957,62 +957,67 @@ function _makeResponseError(response: Response, message?: string): Error {
})
}

export const smuggler: StorageApi & AuthenticationApi = {
getAuth,
node: {
get: getNode,
update: updateNode,
create: createNode,
createOrUpdate: createOrUpdateNode,
slice: _getNodesSliceIter,
lookup: lookupNodes,
delete: deleteNode,
bulkDelete: bulkDeleteNodes,
batch: {
/**
* Caution: these methods are not cached
*/
get: getNodeBatch,
export function makeDatacenterStorageApi(): StorageApi {
return {
node: {
get: getNode,
update: updateNode,
create: createNode,
createOrUpdate: createOrUpdateNode,
slice: _getNodesSliceIter,
lookup: lookupNodes,
delete: deleteNode,
bulkDelete: bulkDeleteNodes,
batch: {
/**
* Caution: these methods are not cached
*/
get: getNodeBatch,
},
url: makeDirectUrl,
},
url: makeDirectUrl,
},
blob: {
upload: uploadFiles,
sourceUrl: makeBlobSourceUrl,
},
blob_index: {
build: buildFilesSearchIndex,
cfg: {
supportsMime: mimeTypeIsSupportedByBuildIndex,
blob: {
upload: uploadFiles,
sourceUrl: makeBlobSourceUrl,
},
},
edge: {
create: createEdge,
get: getNodeAllEdges,
sticky: switchEdgeStickiness,
delete: deleteEdge,
},
blob_index: {
build: buildFilesSearchIndex,
cfg: {
supportsMime: mimeTypeIsSupportedByBuildIndex,
},
},
edge: {
create: createEdge,
get: getNodeAllEdges,
sticky: switchEdgeStickiness,
delete: deleteEdge,
},
activity: {
external: {
add: addExternalUserActivity,
get: getExternalUserActivity,
},
association: {
record: recordExternalAssociation,
get: getExternalAssociation,
},
},
external: {
ingestion: {
get: getUserIngestionProgress,
advance: advanceUserIngestionProgress,
},
},
}
}

export const authentication: AuthenticationApi = {
getAuth,
session: {
create: createSession,
delete: deleteSession,
update: updateSession,
},
activity: {
external: {
add: addExternalUserActivity,
get: getExternalUserActivity,
},
association: {
record: recordExternalAssociation,
get: getExternalAssociation,
},
},
external: {
ingestion: {
get: getUserIngestionProgress,
advance: advanceUserIngestionProgress,
},
},
user: {
password: {
recover: passwordRecoverRequest,
Expand Down
4 changes: 2 additions & 2 deletions smuggler-api/src/auth/account.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { smuggler } from './../api_cloud'
import { authentication } from '../api_datacenter'
import { authCookie } from './cookie'
import { AccountInterface } from './account_interface'
import type { LocalCrypto } from './account_interface'
Expand Down Expand Up @@ -40,7 +40,7 @@ export class UserAccount extends AnonymousAccount {
}

static async create(signal?: AbortSignal): Promise<AccountInterface> {
const user = await smuggler.getAuth({ signal }).catch(() => {
const user = await authentication.getAuth({ signal }).catch(() => {
return null
})
if (!user) {
Expand Down
4 changes: 2 additions & 2 deletions smuggler-api/src/auth/knocker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { smuggler, SmugglerError } from '../api_cloud'
import { authentication, SmugglerError } from '../api_datacenter'
import { StatusCode } from './../status_codes'
import { authCookie } from './cookie'

Expand Down Expand Up @@ -106,7 +106,7 @@ export class Knocker {
const now = unixtime.now()
if (this.#knockingPeriodSeconds < now - lastUpdateTime) {
log.debug('Knock-knock smuggler', now, lastUpdateTime)
await smuggler.session.update(this.#abortController.signal)
await authentication.session.update(this.#abortController.signal)
this.setLastUpdate({ time: now })

try {
Expand Down
2 changes: 1 addition & 1 deletion smuggler-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ export * from './node_slice_iterator'
export * from './storage_api'
export * from './storage_api_throwing'
export * from './authentication_api'
export { smuggler } from './api_cloud'
export { authentication, makeDatacenterStorageApi } from './api_datacenter'
export { steroid } from './steroid/steroid'
2 changes: 1 addition & 1 deletion smuggler-api/src/steroid/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export type CreateNodeFromLocalBinaryArgs = {

/**
* Upload a local binary file as a *fully featured* Mazed node
* (as opposed to, for example, @see smuggler.blob.upload that
* (as opposed to, for example, @see StorageApi.blob.upload that
* at the time of this writing creates a node that *doesn't support some
* Mazed features* like search index).
*/
Expand Down
4 changes: 2 additions & 2 deletions truthsayer/src/account/create/Signup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { goto, History, routes } from '../../lib/route'

import { log } from 'armoury'

import { smuggler } from 'smuggler-api'
import { authentication } from 'smuggler-api'
import { Link } from 'react-router-dom'

type SignupProps = {
Expand Down Expand Up @@ -92,7 +92,7 @@ class SignupImpl extends React.Component<SignupProps, SignupState> {
this.setState({
errorMsg: undefined,
})
smuggler.user
authentication.user
.register({
name: this.state.name,
email: this.state.email,
Expand Down
4 changes: 2 additions & 2 deletions truthsayer/src/auth/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import PropTypes from 'prop-types'
import { css } from '@emotion/react'
import { withRouter, Link } from 'react-router-dom'

import { smuggler } from 'smuggler-api'
import { authentication } from 'smuggler-api'
import { goto } from '../lib/route'

class Login extends React.Component {
Expand Down Expand Up @@ -54,7 +54,7 @@ class Login extends React.Component {
})
const { email, password } = this.state
const permissions = null
smuggler.session
authentication.session
.create(
email,
password,
Expand Down
4 changes: 2 additions & 2 deletions truthsayer/src/auth/Logout.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import PropTypes from 'prop-types'

import { withRouter } from 'react-router-dom'
import { goto } from '../lib/route'
import { smuggler } from 'smuggler-api'
import { authentication } from 'smuggler-api'

import { MzdGlobalContext } from './../lib/global'

Expand All @@ -26,7 +26,7 @@ class Logout extends React.Component {
const account = this.context.account
const isAuthenticated = account != null && account.isAuthenticated()
if (isAuthenticated) {
smuggler.session
authentication.session
.delete({
signal: this.fetchAbortController.signal,
})
Expand Down
4 changes: 2 additions & 2 deletions truthsayer/src/auth/PasswordChange.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react'
import { Badge, Button, Card, Col, Container, Form, Row } from 'react-bootstrap'

import PropTypes from 'prop-types'
import { smuggler } from 'smuggler-api'
import { authentication } from 'smuggler-api'
import { withRouter } from 'react-router-dom'

class PasswordChange extends React.Component {
Expand Down Expand Up @@ -54,7 +54,7 @@ class PasswordChange extends React.Component {

onSubmit = (event) => {
event.preventDefault()
smuggler.user.password
authentication.user.password
.change({
old_password: this.state.password,
new_password: this.state.new_password,
Expand Down
4 changes: 2 additions & 2 deletions truthsayer/src/auth/PasswordRecoverForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Card, Button, Form, Container, Row, Col } from 'react-bootstrap'
import PropTypes from 'prop-types'
import { withRouter } from 'react-router-dom'

import { smuggler } from 'smuggler-api'
import { authentication } from 'smuggler-api'

class PasswordRecoverForm extends React.Component {
constructor(props) {
Expand Down Expand Up @@ -36,7 +36,7 @@ class PasswordRecoverForm extends React.Component {

onSubmit = (event) => {
event.preventDefault()
smuggler.user.password
authentication.user.password
.reset({
token: this.props.token,
new_password: this.state.password,
Expand Down
4 changes: 2 additions & 2 deletions truthsayer/src/auth/PasswordRecoverRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {

import { Emoji } from './../lib/Emoji'
import PropTypes from 'prop-types'
import { smuggler } from 'smuggler-api'
import { authentication } from 'smuggler-api'
import { withRouter } from 'react-router-dom'

class PasswordRecoverRequest extends React.Component {
Expand Down Expand Up @@ -67,7 +67,7 @@ class PasswordRecoverRequest extends React.Component {

onSubmit = (event) => {
event.preventDefault()
smuggler.user.password
authentication.user.password
.recover({
email: this.state.email,
signal: this.abortControler.signal,
Expand Down
Loading